forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtags.el
3971 lines (3610 loc) · 166 KB
/
rtags.el
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; rtags.el --- A front-end for rtags
;; Copyright (C) 2011-2015 Jan Erik Hanssen and Anders Bakken
;; Author: Jan Erik Hanssen <[email protected]>
;; Anders Bakken <[email protected]>
;; URL: http://rtags.net
;; Version: 2.0
;; This file is not part of GNU Emacs.
;; This file is part of RTags (http://rtags.net).
;;
;; RTags is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.
;;
;; RTags is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
;; GNU General Public License for more details.
;;
;; You should have received a copy of the GNU General Public License
;; along with RTags. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;;; Code:
(defgroup rtags nil
"Minor mode for rtags."
:group 'tools
:prefix "rtags-")
(require 'bookmark)
(require 'cc-mode)
(require 'tramp)
(require 'simple)
(if (or (> emacs-major-version 24)
(< emacs-major-version 23)
(and (= emacs-major-version 24)
(>= emacs-minor-version 3)))
(progn
(require 'cl-lib)
(defalias 'defun* 'cl-defun)) ;; cl-lib has own namespace now
(eval-when-compile
(require 'cl)))
(require 'compile)
(require 'thingatpt)
(require 'repeat)
(defconst rtags-popup-available (require 'popup nil t))
(defvar rtags-last-completions nil)
(defvar rtags-last-prepare-completion-position nil) ;; cons (buffer . offset)
(defvar rtags-path-filter nil)
(defvar rtags-path-filter-regex nil)
(defvar rtags-range-filter nil)
(defvar rtags-mode-hook nil)
(defvar rtags-diagnostics-hook nil)
(defvar rtags-jump-hook nil)
(defvar rtags-diagnostics-suspended nil)
(defvar rtags-taglist-hook nil)
(defface rtags-path nil "Path" :group 'rtags)
(defface rtags-context nil "Context" :group 'rtags)
(defvar rtags-path-face 'rtags-path "Path part")
(defvar rtags-context-face 'rtags-context "Context part")
(defconst rtags-buffer-name "*RTags*")
(defconst rtags-diagnostics-buffer-name "*RTags Diagnostics*")
(defconst rtags-diagnostics-raw-buffer-name " *RTags Raw*")
(defvar rtags-last-request-not-indexed nil)
(defvar rtags-last-request-not-connected nil)
(defvar rtags-buffer-bookmarks 0)
(defvar rtags-diagnostics-process nil)
(defvar rtags-diagnostics-starting nil)
(defvar rtags-tramp-enabled nil)
(defvar rtags-buffer-follows-sandbox-id-match 'ask
"Tells the way current buffer follows sandbox-id in case
match fails at a query to rc/rdm backend.
nil - perform current query without updating diagnostics buffer.
Diagnostics will be away from current context.
ask - ask the user if sandbox should be changed. After 'yes',
perform the command once more.
t - change the sandbox and do the command.
Note: if *RTags Diagnostics* is not running, then the 'match check'
is not performed, because sandbox tracking is not needed then.
Note: it is recommended to run each sandbox is separate emacs process.")
(defun rtags-remove (predicate seq &optional not)
(let ((ret))
(while seq
(let ((matched (funcall predicate (car seq))))
(when (cond ((and matched not))
((and (not matched) (not not)))
(t nil))
(setq ret (append ret (list (car seq)))))
(setq seq (cdr seq))))
ret))
(defun rtags-remove-last-if-duplicated (seq) ;; destroys seq
(let ((newitem (car (last seq))))
(when (> (length (member newitem seq)) 1)
(nbutlast seq 1))
seq))
(defun rtags-is-indexable-default (buffer)
(let ((filename (buffer-file-name buffer)))
(when filename
(let ((suffix (and (string-match "\.\\([^.]+\\)$" filename) (match-string 1 filename))))
(or (not suffix)
(and (member (downcase suffix) (list "cpp" "h" "cc" "c" "cp" "cxx" "m" "mm" "tcc" "txx" "moc" "hxx" "hh" "hpp")) t))))))
(defcustom rtags-enabled t
"Whether rtags is enabled. We try to do nothing when it's not."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-find-file-absolute nil
"Whether rtags-find-file shows absolute paths"
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-close-taglist-on-focus-lost nil
"Whether rtags-taglist should close when it loses focus"
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-close-taglist-on-selection t
"Whether rtags-taglist should close when something is selected"
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-follow-symbol-try-harder t
"Fall back to string-matching if follow symbol fails."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-reindex-on-save nil
"Explicitly reindex files on save. This should only be useful if your file system watching is not working."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-use-filename-completion t
"Whether rtags special filename completion is enabled. Set to nil to enable ido-ubiquitous etc."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-diagnostics-use-pipe t
"Whether diagnostics should use pipes. If you're running emacs in cygwin you might have to set this to nil."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-autostart-diagnostics nil
"Whether rtags automatically will restart diagnostics."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-spellcheck-enabled t
"Whether rtags does syntax checking with overlays etc to mark errors, warnings and fixups."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-verbose-results nil
"Print more verbose results buffer."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-sort-references-by-input t
"Whether rtags sorts the references based on the input to `rtags-find-references'."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-completions-enabled nil
"Whether completions are enabled."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-completions-timer-interval 1
"Interval for completions timer. nil means, don't preemptively prepare completions."
:group 'rtags
:type '(choice (const :tag "Unset" nil) number)
:safe 'numberp)
(defcustom rtags-update-current-project-timer-interval .5
"Interval for update current project timer."
:group 'rtags
:type 'number
:safe 'numberp)
(defcustom rtags-wildcard-symbol-names t
"Allow use of * and ? to match symbol names."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-tracking nil
"When on automatically jump to symbol under cursor in *RTags* buffer."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-socket-file nil
"Socket file to pass to rc"
:group 'rtags
:type 'string
:safe 'stringp)
(defcustom rtags-track-container nil
"When on continually update current container (function/class/namespace) on intervals."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-error-timer-interval .5
"Interval for minibuffer error timer."
:group 'rtags
:type 'number
:safe 'numberp)
(defcustom rtags-display-current-error-as-message t
"Display error under cursor using (message)."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-display-current-error-as-tooltip nil
"Display error under cursor using popup-tip (requires 'popup)."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-display-summary-as-tooltip rtags-popup-available
"Display help / summary text using popup-tip (requires 'popup)."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-tooltips-enabled rtags-popup-available
"Display help / summary text when hovering over symbols."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-error-timer-interval .5
"Interval for minibuffer error timer."
:group 'rtags
:type 'number
:safe 'numberp)
(defcustom rtags-tracking-timer-interval .5
"Interval for tracking timer."
:group 'rtags
:type 'number
:safe 'numberp)
(defcustom rtags-container-timer-interval .5
"Interval for container timer"
:group 'rtags
:type 'number
:safe 'numberp)
(defcustom rtags-current-container-hook nil
"Run after rtags has set the current container."
:group 'rtags
:type 'hook)
(defcustom rtags-is-indexable 'rtags-is-indexable-default
"What function to call for expansions."
:group 'rtags
:type 'function)
(defcustom rtags-after-find-file-hook nil
"Run after rtags has jumped to a location possibly in a new file."
:group 'rtags
:type 'hook)
(defcustom rtags-mode-hook nil
"Run when rtags-mode is started."
:group 'rtags
:type 'hook)
(defcustom rtags-diagnostics-hook nil
"Run after diagnostics have been parsed."
:group 'rtags
:type 'hook)
(defcustom rtags-completions-hook nil
"Run after completions have been parsed."
:group 'rtags
:type 'hook)
(defcustom rtags-edit-hook nil
"Run before rtags tries to modify a buffer (from rtags-rename)
return t if rtags is allowed to modify this file."
:group 'rtags
:type 'hook)
(defcustom rtags-jump-to-first-match t
"If t, jump to first match."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-highlight-current-line t
"If t, highlight the current line in *RTags* buffer."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-timeout nil
"Max amount of ms to wait before timing out requests."
:group 'rtags
:type '(choice (const :tag "Unset" nil) integer)
:safe 'integerp)
(defcustom rtags-path nil
"Path to rtags executables."
:group 'rtags
:type '(choice (const :tag "Unset" nil) directory)
:risky t)
(defcustom rtags-max-bookmark-count 100
"How many bookmarks to keep on the stack."
:group 'rtags
:type 'integer
:safe 'integerp)
(defcustom rtags-rc-log-enabled nil
"If t, log rc commands and responses."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-show-containing-function nil
"If t, pass -o to rc to include containing function."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defface rtags-warnline
'((((class color) (background dark)) (:background "blue"))
(((class color) (background light)) (:background "blue"))
(t (:bold t)))
"Face used for marking error lines."
:group 'rtags)
(defface rtags-current-line
'((((class color) (background dark)) (:background "gray19"))
(((class color) (background light)) (:background "LightGray"))
(t (:bold t)))
"Face used for highlighting current line."
:group 'rtags)
(defface rtags-errline
'((((class color) (background dark)) (:background "red"))
(((class color) (background light)) (:background "red"))
(t (:bold t)))
"Face used for marking warning lines."
:group 'rtags)
(defface rtags-fixitline
'((((class color) (background dark)) (:background "goldenrod4"))
(((class color) (background light)) (:background "goldenrod4"))
(t (:bold t)))
"Face used for marking fixit lines."
:group 'rtags)
(defface rtags-skippedline
'((((class color) (background dark)) (:background "gray12"))
(((class color) (background light)) (:background "light gray")))
"Face used for marking skipped lines."
:group 'rtags)
(defconst rtags-verbose-results-delimiter "------------------------------------------")
(defcustom rtags-enable-unsaved-reparsing t
"Whether rtags will reparse unsaved buffers as needed."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-reparse-timeout nil
"Max number of ms you're willing to wait for a reparse to finish."
:group 'rtags
:type '(choice (const :tag "Unset" nil) integer)
:safe 'integerp)
(defcustom rtags-find-file-case-insensitive nil
"Treat files case-insensitively."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-symbolnames-case-insensitive nil
"Treat symbol names case-insensitively."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-find-file-prefer-exact-match t
"Jump directly to files that exactly match the filename for `rtags-find-file'."
:group 'rtags
:type 'boolean
:safe 'booleanp)
(defcustom rtags-other-window-window-size-percentage 30
"Percentage size of other buffer."
:group 'rtags
:type 'integer)
(defcustom rtags-split-window-function 'split-window
"Function to split window. default is `split-window'."
:group 'rtags
:type 'function)
(defun rtags-get-buffer (&optional name)
(unless name
(setq name rtags-buffer-name))
(when (get-buffer name)
(kill-buffer name))
(generate-new-buffer name))
;; for old emacsen
(defun rtags-string-prefix-p (str1 str2 &optional ignore-case)
"Return non-nil if STR1 is a prefix of STR2.
If IGNORE-CASE is non-nil, the comparison is done without paying attention
to case differences."
(eq t (compare-strings str1 nil nil
str2 0 (length str1) ignore-case)))
(defun rtags-is-rtags-buffer (&optional buffer)
(and (not (buffer-file-name buffer))
(rtags-string-prefix-p "*RTags" (buffer-name buffer))))
(defun rtags-has-diagnostics ()
(and (get-buffer rtags-diagnostics-buffer-name)
rtags-diagnostics-process
(not (eq (process-status rtags-diagnostics-process) 'exit))
(not (eq (process-status rtags-diagnostics-process) 'signal))
(> (process-id rtags-diagnostics-process) 0)))
;;;###autoload
(defun rtags-bury-or-delete ()
(interactive)
(if (> (length (window-list)) 1)
(delete-window)
(bury-buffer)))
(defvar rtags-mode-map nil)
;; assign command to keys
(setq rtags-mode-map (make-sparse-keymap))
(define-key rtags-mode-map (kbd "RET") 'rtags-select-other-window)
(define-key rtags-mode-map (kbd "M-RET") 'rtags-select)
(define-key rtags-mode-map [mouse-1] 'rtags-select-other-window)
(define-key rtags-mode-map [mouse-2] 'rtags-select-other-window)
(define-key rtags-mode-map (kbd "M-o") 'rtags-show-in-other-window)
(define-key rtags-mode-map (kbd "s") 'rtags-show-in-other-window)
(define-key rtags-mode-map (kbd "SPC") 'rtags-select-and-remove-rtags-buffer)
(define-key rtags-mode-map (kbd "q") 'rtags-bury-or-delete)
(define-key rtags-mode-map (kbd "j") 'next-line)
(define-key rtags-mode-map (kbd "k") 'previous-line)
(define-key rtags-mode-map (kbd "n") 'next-line)
(define-key rtags-mode-map (kbd "p") 'previous-line)
(defvar rtags-dependency-tree-mode-map nil)
(setq rtags-dependency-tree-mode-map (make-sparse-keymap))
(define-key rtags-dependency-tree-mode-map (kbd "TAB") 'rtags-dependency-tree-toggle-current-expanded)
(define-key rtags-dependency-tree-mode-map (kbd "e") 'rtags-dependency-tree-expand-all)
(define-key rtags-dependency-tree-mode-map (kbd "c") 'rtags-dependency-tree-collapse-all)
(define-key rtags-dependency-tree-mode-map (kbd "-") 'rtags-dependency-tree-collapse-current)
(define-key rtags-dependency-tree-mode-map (kbd "+") 'rtags-dependency-tree-expand-current)
(define-key rtags-dependency-tree-mode-map (kbd "P") 'rtags-dependency-tree-find-path)
(define-key rtags-dependency-tree-mode-map (kbd "f") 'rtags-dependency-tree-find-path)
(define-key rtags-dependency-tree-mode-map (kbd "n") 'rtags-dependency-tree-next-level)
(define-key rtags-dependency-tree-mode-map (kbd "p") 'rtags-dependency-tree-previous-level)
(define-key rtags-dependency-tree-mode-map (kbd "RET") 'rtags-select-other-window)
(define-key rtags-dependency-tree-mode-map (kbd "M-RET") 'rtags-select)
(define-key rtags-dependency-tree-mode-map [mouse-1] 'rtags-select-other-window)
(define-key rtags-dependency-tree-mode-map [mouse-2] 'rtags-select-other-window)
(define-key rtags-dependency-tree-mode-map (kbd "M-o") 'rtags-show-in-other-window)
(define-key rtags-dependency-tree-mode-map (kbd "s") 'rtags-show-in-other-window)
(define-key rtags-dependency-tree-mode-map (kbd "SPC") 'rtags-select-and-remove-rtags-buffer)
(define-key rtags-dependency-tree-mode-map (kbd "k") 'previous-line)
(define-key rtags-dependency-tree-mode-map (kbd "j") 'next-line)
(define-key rtags-dependency-tree-mode-map (kbd "q") 'rtags-bury-or-delete)
(defvar rtags-references-tree-mode-map nil)
(setq rtags-references-tree-mode-map (make-sparse-keymap))
(define-key rtags-references-tree-mode-map (kbd "TAB") 'rtags-references-tree-toggle-current-expanded)
(define-key rtags-references-tree-mode-map (kbd "-") 'rtags-references-tree-collapse-current)
(define-key rtags-references-tree-mode-map (kbd "+") 'rtags-references-tree-expand-current)
(define-key rtags-references-tree-mode-map (kbd "n") 'rtags-references-tree-next-level)
(define-key rtags-references-tree-mode-map (kbd "p") 'rtags-references-tree-previous-level)
(define-key rtags-references-tree-mode-map (kbd "RET") 'rtags-select-other-window)
(define-key rtags-references-tree-mode-map (kbd "M-RET") 'rtags-select)
(define-key rtags-references-tree-mode-map [mouse-1] 'rtags-select-other-window)
(define-key rtags-references-tree-mode-map [mouse-2] 'rtags-select-other-window)
(define-key rtags-references-tree-mode-map (kbd "M-o") 'rtags-show-in-other-window)
(define-key rtags-references-tree-mode-map (kbd "s") 'rtags-show-in-other-window)
(define-key rtags-references-tree-mode-map (kbd "SPC") 'rtags-select-and-remove-rtags-buffer)
(define-key rtags-references-tree-mode-map (kbd "k") 'previous-line)
(define-key rtags-references-tree-mode-map (kbd "j") 'next-line)
(define-key rtags-references-tree-mode-map (kbd "q") 'rtags-bury-or-delete)
(defvar rtags-current-file nil)
(make-variable-buffer-local 'rtags-current-file)
(defvar rtags-current-project nil)
(make-variable-buffer-local 'rtags-current-project)
(defconst rtags-c++-keywords '("alignas" "alignof" "and" "and_eq" "asm" "bitand" "bitor"
"break" "case" "catch" "class" "compl" "const" "constexpr" "const_cast"
"continue" "decltype" "default" "delete" "do" "double" "dynamic_cast"
"else" "enum" "explicit" "export" "extern" "false" "float" "for" "friend"
"goto" "if" "inline" "mutable" "namespace" "new" "noexcept" "not"
"not_eq" "nullptr" "operator" "or" "or_eq" "private" "protected" "public"
"register" "reinterpret_cast" "return" "sizeof" "static" "static_assert"
"static_cast" "struct" "switch" "template" "this" "thread_local" "throw"
"true" "try" "typedef" "typeid" "typename" "union" "using" "virtual"
"void" "volatile" "while" "xor" "xor_eq"))
(defconst rtags-c++-templates '("list" "vector" "map" "set" "unordered_map" "multiset" "multimap"
"shared_ptr" "weak_ptr" "unique_ptr" "unique_lock"))
(defconst rtags-c++-types '("char" "double" "float" "int" "long" "short" "signed" "unsigned"
"void" "u?int[0-9]+_t" "bool" "wchar_t" "std::string" "std::mutex"))
(defvar rtags-font-lock-keywords
`((,"^\\(.*?:[0-9]+:[0-9]+:\\).*$"
(1 font-lock-string-face))
(,"^\\([A-Za-z0-9/._-]*\\)$"
(1 font-lock-string-face))
;; (,(concat "^" rtags-verbose-results-delimiter "$")
;; (1 font-lock-builtin-face))
(,"^[ \t]+\\(.*\\)$"
(1 font-lock-function-name-face))))
(defvar rtags-current-line-overlay nil)
(defun rtags-update-current-line ()
(when (overlayp rtags-current-line-overlay)
(move-overlay rtags-current-line-overlay (point-at-bol) (point-at-eol))))
(define-derived-mode rtags-mode fundamental-mode
(set (make-local-variable 'font-lock-defaults)
'(rtags-font-lock-keywords (save-excursion
(goto-char (point-min))
(when (search-forward "'\"'" nil t)
t))))
(set (make-local-variable 'rtags-current-file) nil)
(set (make-local-variable 'rtags-current-project) nil)
(setq mode-name "rtags")
(use-local-map rtags-mode-map)
(run-hooks 'rtags-mode-hook)
(goto-char (point-min))
(setq next-error-function 'rtags-next-prev-match)
(when rtags-highlight-current-line
(let ((overlay (make-overlay (point-at-bol) (point-at-eol) (current-buffer))))
(overlay-put overlay 'face 'rtags-current-line)
(setq-local rtags-current-line-overlay overlay)))
(setq buffer-read-only t))
(defun rtags-wrap-word (word)
(concat "[^A-Za-z0-9_]\\(" word "\\)[^A-Za-z0-9_]"))
(font-lock-add-keywords 'rtags-mode
(mapcar (lambda (keyword)
(cons (rtags-wrap-word keyword) 'font-lock-keyword-face))
rtags-c++-keywords))
(defun rtags-make-type (type) (cons (rtags-wrap-word type) 'font-lock-type-face))
(font-lock-add-keywords 'rtags-mode
(mapcar (function rtags-make-type) rtags-c++-types))
(font-lock-add-keywords 'rtags-mode
(mapcar (function rtags-make-type)
(let ((ret)
(templates rtags-c++-templates))
(while templates
(let ((template (car templates)))
(push (concat "std::" template " *<[^<>]*<[^<>]*<[^<>]*>[^>]*>[^>]*>") ret)
(push (concat "std::" template " *<[^<>]*<[^<>]*>[^>]*>") ret)
(push (concat "std::" template " *<[^<>]*>") ret))
(setq templates (cdr templates)))
ret)))
(define-derived-mode rtags-dependency-tree-mode fundamental-mode
;; (set (make-local-variable 'font-lock-defaults) '(rtags-font-lock-keywords))
(setq mode-name "rtags-dependency-tree-mode")
(use-local-map rtags-dependency-tree-mode-map)
(goto-char (point-min))
(setq buffer-read-only t))
(define-derived-mode rtags-references-tree-mode fundamental-mode
(set (make-local-variable 'font-lock-defaults) '(rtags-font-lock-keywords))
(setq mode-name "rtags-references-tree-mode")
(use-local-map rtags-references-tree-mode-map)
(goto-char (point-min))
(setq buffer-read-only t))
(defun rtags-reset-bookmarks ()
(setq rtags-buffer-bookmarks 0)
(mapcar (lambda (bookmark) (when (string-match "^RTags_" bookmark) (bookmark-delete bookmark))) (bookmark-all-names)))
;;;###autoload
(defun rtags-next-match () (interactive) (rtags-next-prev-match 1 nil))
;;;###autoload
(defun rtags-previous-match () (interactive) (rtags-next-prev-match -1 nil))
(defun rtags-next-prev-match (by reset)
(when (get-buffer rtags-buffer-name)
(let ((target)
(next (> by 0))
(win (get-buffer-window rtags-buffer-name)))
(when win
(select-window win))
(set-buffer rtags-buffer-name)
(when reset
(goto-char (point-min)))
(when (and rtags-use-helm
(boundp 'helm-action-buffer)
(get-buffer-window helm-action-buffer 'visible)
(fboundp 'helm-keyboard-quit))
(helm-keyboard-quit))
(when (> (count-lines (point-max) (point-min)) 1)
(while (not (eq by 0))
(cond ((and next
(goto-char (point-at-eol))
(re-search-forward "^\\(.*?\\):\\([0-9]+\\):\\([0-9]+\\)" nil t)))
(next
(goto-char (point-min))
(message "%s Wrapped" rtags-buffer-name))
((and (not next)
(goto-char (point-at-bol))
(re-search-backward "^\\(.*?\\):\\([0-9]+\\):\\([0-9]+\\)" nil t)))
(t
(goto-char (point-max))
(re-search-backward "^\\(.*?\\):\\([0-9]+\\):\\([0-9]+\\)" nil t))
(message "%s Wrapped" rtags-buffer-name))
(beginning-of-line)
(if next
(decf by)
(incf by)))
(when rtags-highlight-current-line
(rtags-update-current-line))
(if win
(rtags-select-other-window)
(rtags-select))))))
;;;###autoload
(defun rtags-next-diag () (interactive) (rtags-next-prev-diag t))
;;;###autoload
(defun rtags-previous-diag () (interactive) (rtags-next-prev-diag nil))
(defun rtags-next-prev-diag (next)
(when (get-buffer rtags-diagnostics-buffer-name)
(let (target
(win (get-buffer-window rtags-diagnostics-buffer-name)))
(when win
(select-window win))
(set-buffer rtags-diagnostics-buffer-name)
(when (not (= (point-max) (point-min)))
(cond ((and (= (point-at-bol) (point-min)) (not next))
(setq target (- (point-max) 1))
(message "*RTags Diagnostics* Wrapped"))
((and (>= (+ (point-at-eol) 1) (point-max)) next)
(setq target (point-min))
(message "*RTags Diagnostics* Wrapped"))
(next
(setq target (point-at-bol 2)))
(t
(setq target (point-at-bol 0))))
(goto-char target)
(beginning-of-line)
(if win
(rtags-select-other-window)
(rtags-select))))))
(defun rtags-executable-find (exe)
(cond ((tramp-tramp-file-p default-directory) exe)
;; for tramp let's rely on `tramp-remote-path`, so if You have some *debug*
;; directory to store rtags binaries, just put it to `tramp-remote-path`
(rtags-path
(or
(let ((file (expand-file-name exe rtags-path)))
(and (file-executable-p file) file))
(let ((file (expand-file-name exe (concat rtags-path "/bin/"))))
(and (file-executable-p file) file))))
(t (executable-find exe))))
(defun rtags-remove-keyword-params (seq)
(when seq
(let ((head (car seq))
(tail (cdr seq)))
(if (keywordp head)
(rtags-remove-keyword-params (cdr tail))
(cons head (rtags-remove-keyword-params tail))))))
(defun rtags-combine-strings (list)
(let (ret)
(while list
(setq ret (or (and ret (concat ret " " (car list))) (car list)))
(setq list (cdr list)))
ret))
(defun rtags-diagnostics-is-running ()
(and rtags-diagnostics-process
(not (eq (process-status rtags-diagnostics-process) 'exit))
(not (eq (process-status rtags-diagnostics-process) 'signal))))
(defun rtags-get-sandbox-id (path)
"Returns vector to uniquely define sandbox the path belongs to.
Each host, the emacs is currently connected can be understood as separate sandbox.
nil identifies the local (non-tramp)"
(if (not (tramp-tramp-file-p path))
nil
(let ((path-vec (tramp-dissect-file-name path)))
(aset path-vec 3 nil)
path-vec)))
(defun rtags-sandbox-id-matches ()
"Returns true if current buffer is within *current* sandbox.
*RTags Diagnostics* buffer's sandbox is the *current* sandbox.
If *RTags Diagnostics* does not exist, then t is returned (ie. match for everyone)
Additionally for debugging purposes this method handles `rtags-tramp-enabled` fuse"
(let (sandbox-match)
(if (and (tramp-tramp-file-p default-directory) (not rtags-tramp-enabled))
(message "RTags @ remote site functionality disabled")
(if (not (and (rtags-diagnostics-is-running) (get-buffer rtags-diagnostics-buffer-name)))
(setq sandbox-match t)
(let ((current-buff-sandbox-id (rtags-get-sandbox-id default-directory))
(buf-name (buffer-name))
sandbox-id
diag-start)
(when (get-buffer rtags-diagnostics-buffer-name)
(with-current-buffer rtags-diagnostics-buffer-name
(setq sandbox-id (rtags-get-sandbox-id default-directory))
(setq sandbox-match (equal current-buff-sandbox-id sandbox-id))
(when (not sandbox-match)
(cond ((eq rtags-buffer-follows-sandbox-id-match 'ask)
(when (y-or-n-p (format "RTags sandbox mismatch! %s: %s; rtags: %s. Change sandbox?: "
buf-name
(if current-buff-sandbox-id
(apply 'tramp-make-tramp-file-name (append current-buff-sandbox-id nil))
"local")
(if sandbox-id
(apply 'tramp-make-tramp-file-name (append sandbox-id nil))
"local")))
(setq diag-start t)))
((eq rtags-buffer-follows-sandbox-id-match nil)
(setq sandbox-match t))
(t ;; (eq rtags-buffer-follows-sandbox-id-match t)
(setq sandbox-match t
diag-start t))))))
(when diag-start
(rtags-diagnostics t)
(message "Done. Issue command once more")))))
sandbox-match))
(defun rtags-call-process-region
(start end program &optional delete buffer display &rest args)
"Use Tramp to handle `call-process-region'.
Fixes a bug in `tramp-handle-call-process-region'.
Function based on org-babel-tramp-handle-call-process-region"
(if (and (featurep 'tramp) (file-remote-p default-directory))
(let ((tmpfile (tramp-compat-make-temp-file "")))
(write-region start end tmpfile)
(when delete (delete-region start end))
(unwind-protect
;; (apply 'call-process program tmpfile buffer display args)
;; bug in tramp
(apply 'process-file program tmpfile buffer display args)
(delete-file tmpfile)))
(apply 'call-process-region
start end program delete buffer display args)))
(defun rtags-trampify (absolute-location)
"if absolute-location is tramped, then return it.
Otherwise if default-directory is tramp one, then uses it to convert absolute-location to remote.
absolute-location can of course be a path"
(if (or (not (tramp-tramp-file-p default-directory))
(tramp-tramp-file-p absolute-location))
absolute-location
(let ((location-vec (tramp-dissect-file-name default-directory)))
(aset location-vec 3 absolute-location)
(apply 'tramp-make-tramp-file-name (append location-vec nil)))))
(defun rtags-untrampify (location)
"gets path segment from tramp path. For non-tramp location just return it non-modified.
Can be used both for path and location."
(if (tramp-tramp-file-p location)
(tramp-file-name-localname
(tramp-dissect-file-name location))
location))
(defun* rtags-call-rc (&rest arguments
&key (path (buffer-file-name))
unsaved
async ;; nil or a cons (process-filter . sentinel)
path-filter
path-filter-regex
range-filter
(output (list t nil)) ; not supported for async
range-min
range-max
noerror
timeout
silent-query
&allow-other-keys)
(save-excursion
(let ((rc (rtags-executable-find "rc")) result)
(if (not rc)
(unless noerror (error "Can't find rc"))
(setq rtags-last-request-not-connected nil)
(setq rtags-last-request-not-indexed nil)
(when (and async (not (consp async)))
(error "Invalid argument. async must be a cons or nil"))
(setq arguments (rtags-remove-keyword-params arguments))
(setq arguments (rtags-remove '(lambda (arg) (not arg)) arguments))
(setq arguments (mapcar 'rtags-untrampify arguments))
;; other way to ignore colors would IMHO be to configure tramp,
;; but: do we need colors from rc?
(push "--no-color" arguments)
(setq path (rtags-untrampify path))
(when path-filter
(push (concat "--path-filter=" (rtags-untrampify path-filter)) arguments)
(when path-filter-regex
(push "-Z" arguments)))
(when (and unsaved (buffer-file-name unsaved))
(push (format "--unsaved-file=%s:%d"
(rtags-untrampify (buffer-file-name unsaved))
(with-current-buffer unsaved
(rtags-buffer-size)))
arguments))
(when silent-query
(push "--silent-query" arguments))
(when range-filter
(push (format "--range-filter=%d-%d"
(or range-min (rtags-offset (point-min)))
(or range-max (rtags-offset (point-max))))
arguments))
(when (or timeout rtags-timeout)
(push (format "--timeout=%d" (or timeout rtags-timeout)) arguments))
(when (and rtags-show-containing-function (not (member "-N" arguments)))
(push "-o" arguments))
(cond ((stringp path) (push (concat "--current-file=" path) arguments))
(path nil)
(default-directory (push (concat "--current-file=" (rtags-untrampify default-directory)) arguments))
(t nil))
(when rtags-socket-file
(push (concat "-n" (expand-file-name rtags-socket-file)) arguments))
(when rtags-rc-log-enabled
(rtags-log (concat rc " " (rtags-combine-strings arguments))))
(let ((result (cond ((and unsaved async)
(let ((proc (apply #'start-file-process "rc" (current-buffer) rc arguments)))
(with-current-buffer unsaved
(save-restriction
(widen)
(process-send-region proc (point-min) (point-max))))
proc))
(async (apply #'start-file-process "rc" (current-buffer) rc arguments))
((and unsaved (or (buffer-modified-p unsaved)
(not (buffer-file-name unsaved))))
(let ((output-buffer (current-buffer)))
(with-current-buffer unsaved
(save-restriction
(widen)
(apply #'rtags-call-process-region (point-min) (point-max) rc
nil output-buffer nil arguments)))))
(unsaved (apply #'process-file
rc (rtags-untrampify (buffer-file-name unsaved)) output nil arguments) nil)
(t (apply #'process-file rc nil output nil arguments)))))
(if (processp result)
(progn
(set-process-query-on-exit-flag result nil)
(set-process-filter result (car async))
(set-process-sentinel result (cdr async)))
(goto-char (point-min))
(save-excursion
(and (cond ((re-search-forward "^Can't seem to connect to server" nil t)
(erase-buffer)
(setq rtags-last-request-not-connected t)
(unless noerror
(message "Can't seem to connect to server. Is rdm running?"))
nil)
((re-search-forward "^Not indexed" nil t)
(erase-buffer)
(setq rtags-last-request-not-indexed t)
nil)
((looking-at "Project loading")
(erase-buffer)
(message "Project loading...")
t)
(t))
(eq result 0)
rtags-autostart-diagnostics (rtags-diagnostics)))))
(or async (> (point-max) (point-min)))))))
(defvar rtags-preprocess-keymap (make-sparse-keymap))
(define-key rtags-preprocess-keymap (kbd "q") 'rtags-bury-or-delete)
(set-keymap-parent rtags-preprocess-keymap c++-mode-map)
(define-derived-mode rtags-preprocess-mode c++-mode
(setq mode-name "rtags-preprocess")
(use-local-map rtags-preprocess-mode-map)
(when(buffer-file-name)
(error "Set buffer with file %s read only " (buffer-file-name)))
(setq buffer-read-only t))
(defun rtags-builds (&optional file)
(with-temp-buffer
(rtags-call-rc :path file "--builds" file)
(buffer-string)))
(defmacro rtags-called-interactively-p ()
(if (or (> emacs-major-version 23)
(and (>= emacs-major-version 23)
(>= emacs-minor-version 2)))
;; defined with no argument in <=23.1
`(with-no-warnings (called-interactively-p 'interactive))
`(interactive-p)))
;;;###autoload
(defun rtags-preprocess-file (&optional buffer)
(interactive)
(when (or (not (rtags-called-interactively-p)) (rtags-sandbox-id-matches))
(unless buffer (setq buffer (current-buffer)))
(let (narrow-start narrow-end)
(when (and mark-active
(not (equal (region-beginning) (region-end))))
(setq narrow-start (+ 1 (count-lines (point-min) (region-beginning)))
narrow-end (+ 1 (count-lines (point-min) (region-end)))))
(let ((preprocess-buffer (rtags-get-buffer (format "*RTags preprocessed %s*" (buffer-file-name buffer)))))
(rtags-delete-rtags-windows)
(rtags-location-stack-push)
(with-current-buffer preprocess-buffer
(rtags-call-rc :path (buffer-file-name buffer) "--preprocess" (buffer-file-name buffer))
(when (and narrow-start narrow-end)
(let ((match-regexp (concat "^# \\([0-9]*\\) \"" (file-truename (buffer-file-name buffer)) "\""))
last-match last-line start end)
(while (re-search-forward match-regexp nil t)
(let ((current-line (string-to-number (match-string-no-properties 1))))
(when (and (not start) (> current-line narrow-start))
(setq start (+ (count-lines (point-min) last-match) (- narrow-start last-line))))
(when (and (not end) (> current-line narrow-end))
(setq end (+ (count-lines (point-min) last-match) (- narrow-end last-line))))
(setq last-line current-line)
(setq last-match (point))))
(when last-match
(unless start
(setq start (+ (count-lines (point-min) last-match) (- narrow-start last-line))))
(unless end
(setq end (+ (count-lines (point-min) last-match) (- narrow-end last-line)))))
(when (and start end)
(goto-char (point-min))
(narrow-to-region (point-at-bol (+ start 1)) (point-at-bol (+ end 1))))))
(rtags-preprocess-mode))
(display-buffer preprocess-buffer)))))
;;;###autoload
(defun rtags-set-current-project ()
(interactive)
(let ((projects nil)
(project nil)
(current ""))
(with-temp-buffer
(rtags-call-rc :path t "-w")
(goto-char (point-min))
(while (not (eobp))
(let ((line (buffer-substring-no-properties (point-at-bol) (point-at-eol))))
(cond ((string-match "^\\([^ ]+\\)[^<]*<=$" line)
(let ((name (match-string-no-properties 1 line)))
(setq projects (add-to-list 'projects name t))
(setq current name)))
((string-match "^\\([^ ]+\\)[^<]*$" line)
(setq projects (add-to-list 'projects (match-string-no-properties 1 line))))
(t)))
(forward-line)))
(setq project (completing-read
(format "RTags select project (current is %s): " current)
projects))
(when project
(find-file project))))
(defun rtags-current-symbol (&optional no-symbol-name)
(or (and mark-active (buffer-substring-no-properties (point) (mark)))
(and (not no-symbol-name) (rtags-current-symbol-name))