forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrtags.el
1492 lines (1349 loc) · 56.5 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
(defgroup rtags nil
"Minor mode for rtags."
:group 'tools
:prefix "rtags-")
(require 'cl)
(require 'ido)
(require 'dabbrev)
(require 'cc-mode)
(require 'flymake)
(require 'bookmark)
(defvar rtags-last-buffer nil)
(defvar rtags-path-filter nil)
(defvar rtags-path-filter-regex nil)
(defvar rtags-range-filter nil)
(defvar rtags-mode-hook nil)
(defvar rtags-no-otherbuffer 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*")
(defvar rtags-completion nil)
(defvar rtags-completion-cache-file-name "")
(defvar rtags-completion-cache-line 0)
(defvar rtags-completion-cache-column 0)
(defvar rtags-completion-cache-line-contents "")
(defvar rtags-last-request-not-indexed nil)
(defvar rtags-buffer-bookmarks 0)
(defvar rtags-font-lock-keywords
`((,"^\\(.*:[0-9]+:[0-9]+:\\)\\(.*\\)$"
(1 font-lock-string-face)
(2 font-lock-function-name-face))))
(defun rtags-get-buffer (&optional name)
(unless name (setq name rtags-buffer-name))
(if (get-buffer name)
(kill-buffer name))
(generate-new-buffer name))
(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-buffer)
(define-key rtags-mode-map (kbd "M-RET") 'rtags-select-and-remove-rtags-buffer)
(define-key rtags-mode-map (kbd "ENTER") 'rtags-select-other-buffer)
(define-key rtags-mode-map (kbd "SPC") 'rtags-select)
(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-derived-mode rtags-mode fundamental-mode
(setq buffer-read-only nil)
(set (make-local-variable 'font-lock-defaults) '(rtags-font-lock-keywords))
(setq mode-name "rtags")
(use-local-map rtags-mode-map)
(run-hooks 'rtags-mode-hook)
(goto-char (point-min))
(let ((buf (current-buffer)))
(while (not (eobp))
(if (looking-at "^\\(.*\\):\\([0-9]+\\):\\([0-9]+\\)")
(let ((file (match-string 1))
(line (string-to-int (match-string 2)))
(column (string-to-int (match-string 3))))
(let (deactivate-mark)
(save-excursion
(set-buffer (find-file-noselect file))
(goto-line line)
(beginning-of-line)
(forward-char (- column 1))
(setq rtags-buffer-bookmarks (+ rtags-buffer-bookmarks 1))
(bookmark-set (format "R_%d" rtags-buffer-bookmarks))
(set-buffer buf)))))
(next-line))
)
(if (= (point-at-bol) (point-max))
(delete-char -1))
(goto-char (point-min))
(setq buffer-read-only t)
)
(defun rtags-reset-bookmarks ()
(while (> rtags-buffer-bookmarks 0)
(bookmark-delete (format "R_%d" rtags-buffer-bookmarks))
(setq rtags-buffer-bookmarks (- rtags-buffer-bookmarks 1)))
)
(defun rtags-next-match () (interactive) (rtags-next-prev-match t))
(defun rtags-previous-match () (interactive) (rtags-next-prev-match nil))
(defun rtags-next-prev-match (next)
(if (get-buffer rtags-buffer-name)
(let (target
(win (get-buffer-window rtags-buffer-name)))
(if win (select-window win))
(set-buffer rtags-buffer-name)
(when (> (count-lines (point-max) (point-min)) 1)
(cond ((and (= (point-at-bol) (point-min)) (not next))
(setq target (point-max))
(message "*RTags* Wrapped"))
((and (= (point-at-eol) (point-max)) next)
(setq target (point-min))
(message "*RTags* 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-buffer) (rtags-select))))))
(defun rtags-executable-find (exe)
(let ((result (if rtags-path (concat rtags-path "/bin/" exe) (executable-find exe))))
(if (file-exists-p result) result nil)))
(defun rtags-call-rc (path &rest arguments)
(apply #'rtags-call-rc-helper path nil t arguments))
(defun rtags-call-rc-unsaved (path unsaved-pos output &rest arguments)
(apply #'rtags-call-rc-helper path unsaved-pos output arguments))
(defun rtags-call-rc-helper (path unsaved-pos output &rest arguments)
(save-excursion
(let ((rc (rtags-executable-find "rc")))
(setq arguments (remove-if '(lambda (arg) (not arg))
arguments))
(if rc
(progn
(if rtags-autostart-rdm
(push (if rtags-rdm-log-enabled "--autostart-rdm=-L/tmp/rdm.log" "--autostart-rdm") arguments))
(if rtags-path-filter
(progn
(push (format "--path-filter=%s" rtags-path-filter) arguments)
(if rtags-path-filter-regex
(push "-Z" arguments))))
(if rtags-range-filter
(push rtags-range-filter arguments))
(if rtags-timeout
(push (format "--timeout=%d" rtags-timeout) arguments))
(if path
(progn
(if rtags-match-source-file-to-project
(let ((mapped (if rtags-match-source-file-to-project (apply rtags-match-source-file-to-project (list path)))))
(if (and mapped (length mapped)) (push (concat "--with-project=" mapped) arguments))))
(push (concat "--with-project=" path) arguments)))
(rtags-log (concat rc " " (combine-and-quote-strings arguments)))
(if unsaved-pos
(apply #'call-process-region (point-min) unsaved-pos rc nil (list output t) nil arguments)
(apply #'call-process rc nil (list output nil) nil arguments))
(goto-char (point-min))
(rtags-log (buffer-string))
(when (looking-at "Can't seem to connect to server")
(message "Can't seem to connect to server. Is rdm running?")
(erase-buffer))
(> (point-max) (point-min)))))))
(defun rtags-path-for-project (&optional buffer)
(expand-file-name (if (buffer-file-name buffer)
(buffer-file-name buffer)
default-directory)))
(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-diagnostics-mode-map)
(setq buffer-read-only t)
)
(defun rtags-builds (file)
(with-temp-buffer
(rtags-call-rc nil "--builds" file)
(buffer-string))
)
(defun rtags-preprocess-file (&optional build-index buffer)
(interactive "P")
(setq build-index (cond ((and build-index (not (integerp build-index)))
(read-from-minibuffer "Build index: " nil nil nil 0)) ;; should insert builds
((not build-index) 0)
(t build-index)))
(let ((fn (format "%s_%d" (buffer-file-name buffer) build-index))
bufname narrow-start narrow-end)
(if (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)))))
(if fn
(let ((preprocess-buffer (get-buffer-create (format "*RTags preprocessed %s*" fn))))
(with-current-buffer preprocess-buffer
(setq buffer-read-only nil)
(erase-buffer)
(rtags-call-rc nil "--preprocess" fn)
(if (and narrow-start narrow-end)
(let ((match-regexp (concat "^# \\([0-9]*\\) \"" (file-truename fn) "\""))
last-match last-line start end)
(while (re-search-forward match-regexp nil t)
(let ((current-line (string-to-int (match-string 1))))
(if (and (not start) (> current-line narrow-start)) (setq start (+ (count-lines (point-min) last-match) (- narrow-start last-line))))
(if (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))))
(if last-match
(progn
(if (not start) (setq start (+ (count-lines (point-min) last-match) (- narrow-start last-line))))
(if (not end) (setq end (+ (count-lines (point-min) last-match) (- narrow-end last-line))))))
(if (and start end)
(progn
(goto-char (point-min))
(narrow-to-region (point-at-bol (+ start 1)) (point-at-bol (+ end 1)))))))
(rtags-preprocess-mode))
(display-buffer preprocess-buffer))
)))
(defun rtags-reparse-file (&optional buffer)
(interactive)
(let ((path (rtags-path-for-project)))
(with-temp-buffer
(rtags-call-rc path "-V" (buffer-name buffer)))
(message (format "Dirtied %s" (buffer-name buffer)))
)
)
;; /home/abakken/dev (loaded) <=
(defun rtags-set-current-project ()
(interactive)
(let ((projects nil)
(project nil)
(current ""))
(with-temp-buffer
(rtags-call-rc nil "-w")
(goto-char (point-min))
(while (not (eobp))
(let ((line (buffer-substring (point-at-bol) (point-at-eol))))
(if (string-match "^\\([^ ]+\\)[^<]*<=$" line)
(let ((name (match-string 1 line)))
(setq projects (add-to-list 'projects name t))
(setq current name))
(if (string-match "^\\([^ ]+\\)[^<]*$" line)
(setq projects (add-to-list 'projects (match-string 1 line))))))
(next-line))
)
(setq project (ido-completing-read
(format "RTags select project (current is %s): " current)
projects))
(if project
(with-temp-buffer (rtags-call-rc nil "-w" project)))
)
)
;; (message (format "we picked %s" project))
;; (message (combine-and-quote-strings projects))
;; (while (looking-at "^\\([^ ]*\\)")
;; (message (format "%s %s" (match-string 1) (match-string 2)))
;; )
;; )
;; )
(defun rtags-find-ancestor-file (pattern)
"Find a file named \a file in as shallow a path as possible,
e.g. if there's a Makefile in /foobar/rtags/rc/Makefile and one
in /foobar/rtags/Makefile it will return the latter. Wildcards
are allowed. If multiple files match return first match. "
(let ((best nil)
(dir default-directory))
(while (cond ((string= dir "") nil)
((string= dir "/") nil)
(t t))
(let ((match (file-expand-wildcards (concat dir pattern))))
(if match
(setq best (nth 0 match))))
(setq dir (substring dir 0 (string-match "[^/]*/?$" dir))))
best))
(defun rtags-find-ancestor-file-directory (pattern)
(let ((match (rtags-find-ancestor-file pattern)))
(if match
(file-name-directory match))))
(defun rtags-default-current-project ()
(cond
((gtags-get-rootpath))
((git-root-dir))
((rtags-find-ancestor-file-directory "configure"))
((rtags-find-ancestor-file-directory "CMakeLists.txt"))
((rtags-find-ancestor-file-directory "*.pro"))
((rtags-find-ancestor-file-directory "scons.1")) ;; Is this the right way to determine this?
((rtags-find-ancestor-file-directory "autogen.*"))
((rtags-find-ancestor-file-directory "Makefile*"))
((rtags-find-ancestor-file-directory "INSTALL*"))
((rtags-find-ancestor-file-directory "README*"))
(t nil)))
(defun rtags-current-symbol (&optional no-symbol-name)
(save-excursion
(let ((name (if no-symbol-name nil (rtags-current-symbol-name nil t))))
(unless name
(cond
((looking-at "[0-9A-Za-z_]")
(while (and (not (bolp)) (looking-at "[0-9A-Za-z_]"))
(forward-char -1))
(if (not (looking-at "[0-9A-Za-z_]")) (forward-char 1)))
(t
(while (looking-at "[ \t]")
(forward-char 1))))
(if (looking-at "[A-Za-z_][A-Za-z_0-9]*")
(setq name (buffer-substring (match-beginning 0) (match-end 0)))))
name)))
(defun rtags-cursorinfo (&optional location) ;; ### I want to pass more args here
(let ((loc (if location location (rtags-current-location)))
(path (rtags-path-for-project)))
(with-temp-buffer
(rtags-call-rc path "-U" loc)
(buffer-string))))
(defun rtags-print-cursorinfo (&optional location)
(interactive)
(message "%s" (rtags-cursorinfo location)))
(defun rtags-print-enum-value-at-point (&optional location)
(interactive)
(let ((info (rtags-cursorinfo location)))
(cond ((string-match "^Enum Value: \\([0-9]+\\) *$" info)
(let ((enumval (match-string 1 info)))
(message "%s - %s - 0x%X" (rtags-current-symbol-name info) enumval (string-to-int enumval))))
((string-match "^Type: Enum *$" info)
(let ((target (rtags-target)))
(when target
(setq info (rtags-cursorinfo target))
(if (string-match "^Enum Value: \\([0-9]+\\) *$" info)
(let ((enumval (match-string 1 info)))
(message "%s - %s - 0x%X" (rtags-current-symbol-name info) enumval (string-to-int enumval)))))))
(t (message "RTags: No enum here") nil))))
(defun rtags-current-location ()
(format "%s,%d" (buffer-file-name) (- (point) 1)))
(defun rtags-log (log)
(if rtags-rc-log-enabled
(save-excursion
(set-buffer (get-buffer-create "*RTags Log*"))
(goto-char (point-max))
(setq buffer-read-only nil)
(insert "**********************************\n" log "\n")
(setq buffer-read-only t)
)
)
)
(defvar rtags-symbol-history nil)
(defun rtags-save-location ()
(setq rtags-last-buffer (current-buffer))
(rtags-bookmark-push))
(defun rtags-goto-location (location &optional nobookmark &optional otherbuffer)
;; (message (format "rtags-goto-location \"%s\"" location))
(if (length location)
(progn
(if rtags-no-otherbuffer (setq otherbuffer nil))
"Go to a location passed in. It can be either: file,12 or file:13:14 or plain file"
(cond ((string-match "\\(.*\\):\\([0-9]+\\):\\([0-9]+\\)" location)
(let ((line (string-to-int (match-string 2 location)))
(column (string-to-int (match-string 3 location))))
(if otherbuffer
(find-file-other-window (match-string 1 location))
(find-file (match-string 1 location)))
(run-hooks rtags-after-find-file-hook)
(goto-line line)
(beginning-of-line)
(forward-char (- column 1))
(unless nobookmark (rtags-bookmark-push))
t))
((string-match "\\(.*\\):\\([0-9]+\\)" location)
(let ((line (string-to-int (match-string 2 location))))
(if otherbuffer
(find-file-other-window (match-string 1 location))
(find-file (match-string 1 location)))
(run-hooks rtags-after-find-file-hook)
(goto-line line)
(unless nobookmark (rtags-bookmark-push))
t))
((string-match "\\(.*\\),\\([0-9]+\\)" location)
(let ((offset (string-to-int (match-string 2 location))))
(if otherbuffer
(find-file-other-window (match-string 1 location))
(find-file (match-string 1 location)))
(run-hooks rtags-after-find-file-hook)
(goto-char (+ offset 1))
(unless nobookmark (rtags-bookmark-push))
t))
(t
(if otherbuffer
(find-file-other-window location)
(find-file location))
)
)
)
)
)
(defun rtags-find-symbols-by-name-internal (p references filter)
(rtags-save-location)
(rtags-setup-filters filter)
(let ((tagname (rtags-current-symbol))
(switch (if references "-R" "-F"))
(path (rtags-path-for-project))
prompt
input)
(if tagname
(setq prompt (concat p ": (default " tagname ") "))
(setq prompt (concat p ": ")))
(setq input (completing-read prompt (function rtags-symbolname-complete) nil nil nil 'rtags-symbol-history))
(setq rtags-symbol-history (remove-duplicates rtags-symbol-history :from-end t :test 'equal))
(if (not (equal "" input))
(setq tagname input))
(with-current-buffer (rtags-get-buffer)
(rtags-call-rc path switch tagname "-l")
(rtags-reset-bookmarks)
(rtags-handle-completion-buffer)
)
)
)
(defun rtags-remove-completion-buffer ()
(interactive)
(kill-buffer (current-buffer))
(switch-to-buffer rtags-last-buffer))
(defun rtags-symbolname-completion-get (string)
(with-temp-buffer
(rtags-call-rc nil "-Y" "-S" string)
(eval (read (buffer-string)))))
(defun rtags-symbolname-completion-exactmatch (string)
(with-temp-buffer
(rtags-call-rc nil "-N" "-F" string)
(> (point-max) (point-min))))
(defun rtags-symbolname-complete (string predicate code)
(cond ((eq code nil)
(try-completion string (rtags-symbolname-completion-get string) predicate))
((eq code t) (rtags-symbolname-completion-get string))
((eq code 'lambda) (rtags-symbolname-completion-exactmatch string))))
(defvar rtags-bookmark-index 0)
(defvar rtags-bookmarks nil)
(defun rtags-bookmark-push ()
(let ((bm (rtags-current-location)))
(while (> rtags-bookmark-index 0)
(progn
(setq rtags-bookmark-index (- rtags-bookmark-index 1))
(pop rtags-bookmarks)
)
)
(unless (string= bm (nth 0 rtags-bookmarks))
(push bm rtags-bookmarks)
(if (> (length rtags-bookmarks) rtags-max-bookmark-count)
(nbutlast rtags-bookmarks (- (length rtags-bookmarks) rtags-max-bookmark-count))
)
)
)
)
(defun rtags-bookmark-jump (by)
(interactive)
(let ((instack (nth rtags-bookmark-index rtags-bookmarks))
(cur (rtags-current-location)))
(if (not (string= instack cur))
(rtags-goto-location instack t)
(let ((target (+ rtags-bookmark-index by)))
(if (and (>= target 0) (< target (length rtags-bookmarks)))
(progn
(setq rtags-bookmark-index target)
(rtags-goto-location (nth rtags-bookmark-index rtags-bookmarks) t)
)
)
)
)
)
)
;; **************************** API *********************************
(defcustom rtags-completion-enabled nil
"Whether rtags completion is enabled"
:group 'rtags
:type 'boolean)
(defcustom rtags-completion-timer-interval .1
"Interval for completion timer"
:group 'rtags
:type 'number)
(defcustom rtags-diagnostics-clear-buffer nil
"Whether the diagnostics buffer is cleared between each diagnostic block or not"
:group 'rtags
:type 'boolean)
(defcustom rtags-cursorinfo-timer-enabled nil
"Whether rtags cursorinfo is enabled"
:group 'rtags
:type 'boolean)
(defcustom rtags-cursorinfo-timer-interval 1
"Interval for cursorinfo timer"
:group 'rtags
:type 'number)
(defcustom rtags-expand-function '(lambda () (dabbrev-expand nil))
"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-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)
(defcustom rtags-timeout nil
"Max amount of ms to wait before timing out requests"
:group 'rtags
:type 'integer)
(defcustom rtags-path nil
"Path to rtags executables"
:group 'rtags
:type 'string)
(defcustom rtags-max-bookmark-count 100
"How many bookmarks to keep in stack"
:group 'rtags
:type 'integer)
(defcustom rtags-rc-log-enabled nil
"If t, log rc commands and responses"
:group 'rtags
:type 'boolean)
(defcustom rtags-rdm-log-enabled nil
"If t, log for autostarted rdm"
:group 'rtags
:type 'boolean)
(defcustom rtags-autostart-rdm t
"If autostart rdm"
:group 'rtags
:type 'boolean)
(defun rtags-enable-standard-keybindings (&optional map)
(interactive)
(unless map
(setq map c-mode-base-map))
(define-key map (kbd "C-x r .") (function rtags-find-symbol-at-point))
(define-key map (kbd "C-x r ,") (function rtags-find-references-at-point))
(define-key map (kbd "C-x r v") (function rtags-find-virtuals-at-point))
(define-key map (kbd "C-x r V") (function rtags-print-enum-value-at-point))
(define-key map (kbd "C-x r /") (function rtags-find-all-references-at-point))
(define-key map (kbd "C-x r >") (function rtags-find-symbol))
(define-key map (kbd "C-x r <") (function rtags-find-references))
(define-key map (kbd "C-x r [") (function rtags-bookmark-back))
(define-key map (kbd "C-x r ]") (function rtags-bookmark-forward))
(define-key map (kbd "C-x r C") (function rtags-switch-to-completion-buffer))
(define-key map (kbd "C-x r D") (function rtags-diagnostics))
(define-key map (kbd "C-x r G") (function rtags-clear-diagnostics))
(define-key map (kbd "C-x r p") (function rtags-set-current-project))
(define-key map (kbd "C-x r e") (function rtags-reparse-file))
(define-key map (kbd "C-x r E") (function rtags-preprocess-file))
(define-key map (kbd "C-x r R") (function rtags-rename-symbol))
(define-key map (kbd "C-x r U") (function rtags-print-cursorinfo))
(define-key map (kbd "C-x r O") (function rtags-goto-offset))
(define-key map (kbd "C-x r ;") (function rtags-find-file))
(define-key map (kbd "C-x r F") (function rtags-fixit))
(define-key map (kbd "C-x r B") (function rtags-show-rtags-buffer))
)
(defun rtags-print-current-location ()
(interactive)
(message (rtags-current-location)))
(defun rtags-quit-rdm () (interactive)
(call-process (rtags-executable-find "rc") nil nil nil "--quit-rdm"))
(defun rtags-switch-to-completion-buffer () (interactive)
(let ((buf (get-buffer "*RTags Completions*")))
(switch-to-buffer-other-window buf)))
(defun rtags-bookmark-forward ()
(interactive)
(rtags-bookmark-jump -1)
)
(defun rtags-bookmark-back ()
(interactive)
(rtags-bookmark-jump 1)
)
(defun rtags-bookmarks-reset ()
(interactive)
(setq rtags-bookmarks nil)
(setq rtags-bookmark-index 0)
)
(defun rtags-target (&optional location)
(let ((path (rtags-path-for-project)))
(unless location
(setq location (rtags-current-location)))
(with-temp-buffer
(rtags-call-rc path "-N" "-f" location)
(if (< (point-min) (point-max))
(let ((loc (buffer-substring (point-min) (- (point-max) 1))))
(if (string= loc "Not indexed")
(progn
(setq rtags-last-request-not-indexed t)
nil)
(setq rtags-last-request-not-indexed nil)
loc))
(progn
(setq rtags-last-request-not-indexed nil)
nil)))))
(defun rtags-setup-filters (filter)
(setq rtags-path-filter (cond ((stringp filter) filter)
(filter buffer-file-name)
(t nil)))
(setq rtags-range-filter (if (and filter
(or (not (= (point-min) 1))
(not (= (point-max) (+ (buffer-size) 1)))))
(format "--range-filter=%d-%d" (- (point-min) 1) (- (point-max) 1))
nil)))
(defalias 'rtags-find-symbol-at-point 'rtags-follow-symbol-at-point)
(defun rtags-find-symbol-at-point (&optional prefix)
"Find the natural target for the symbol under the cursor and moves to that location.
For references this means to jump to the definition/declaration of the referenced symbol (it jumps to the definition if it is indexed).
For definitions it jumps to the declaration (if there is only one) For declarations it jumps to the definition.
If called with a prefix restrict to current buffer"
(interactive "P")
(rtags-setup-filters prefix)
(rtags-save-location)
(let ((target (rtags-target)))
(if target
(rtags-goto-location target))
)
)
(defun rtags-find-references-at-point (&optional prefix)
"Find all references to the symbol under the cursor
If there's exactly one result jump directly to it.
If there's more show a buffer with the different alternatives and jump to the first one if rtags-jump-to-first-match is true.
References to references will be treated as references to the referenced symbol"
(interactive "P")
(rtags-setup-filters prefix)
(rtags-save-location)
(let ((arg (rtags-current-location)))
(with-current-buffer (rtags-get-buffer)
(rtags-call-rc nil "-l" "-r" arg)
(rtags-handle-completion-buffer))
)
)
(defun rtags-find-virtuals-at-point (&optional prefix)
(interactive "P")
"List all reimplentations of function under cursor. This includes both declarations and definitions"
(rtags-setup-filters prefix)
(rtags-save-location)
(let ((arg (rtags-current-location)))
(with-current-buffer (rtags-get-buffer)
(rtags-call-rc nil "-k" "-l" "-r" arg)
(rtags-handle-completion-buffer))
)
)
(defun rtags-find-all-references-at-point (&optional prefix)
(interactive "P")
(rtags-setup-filters prefix)
(rtags-save-location)
(let ((arg (rtags-current-location)))
(with-current-buffer (rtags-get-buffer)
(rtags-call-rc nil "-l" "-e" "-r" arg)
(rtags-handle-completion-buffer))
)
)
(defun rtags-rename-symbol ()
(interactive)
(save-some-buffers) ;; it all kinda falls apart when buffers are unsaved
(let (len file pos destructor replacewith prev (modifications 0) (filesopened 0) replacements)
(save-excursion
(if (looking-at "[0-9A-Za-z_~#]")
(progn
(while (and (> (point) 1) (looking-at "[0-9A-Za-z_~#]"))
(backward-char))
(if (not (looking-at "[0-9A-Za-z_~#]"))
(forward-char))
(setq file (buffer-file-name (current-buffer)))
(setq pos (point))
(if (looking-at "~")
(progn
(setq pos (+ pos 1))
(setq destructor t)))
(while (looking-at "[0-9A-Za-z_~#]")
(forward-char))
(setq prev (buffer-substring pos (point)))
(setq len (- (point) pos))
(setq replacewith (read-from-minibuffer (format "Replace '%s' with: " prev)))
(unless (equal replacewith "")
(if destructor
(setq pos (- pos 1)))
(with-temp-buffer
(rtags-call-rc nil "-e" "-O" "-N" "-r" (format "%s,%d" file (- pos 1)))
;; (message "Got renames %s" (buffer-string))
(dolist (line (split-string (buffer-string) "\n"))
(if (string-match "^\\(.*\\),\\([0-9]+\\)$" line)
(add-to-list 'replacements (cons (match-string 1 line) (string-to-int (match-string 2 line))) t))))
;; (message "Got %d replacements" (length replacements))
(dolist (value replacements)
(let ((buf (find-buffer-visiting (car value))))
(unless buf
(progn
(incf filesopened)
(setq buf (find-file-noselect (car value)))))
(when buf
(set-buffer buf)
(when (run-hook-with-args-until-failure rtags-edit-hook)
(incf modifications)
(goto-char (+ (cdr value) 1))
(if (looking-at "~")
(forward-char))
;; (message "About to replace %s with %s at %d in %s"
;; (buffer-substring (point) (+ (point) len)) replacewith (point) (car value))
(delete-char len)
(insert replacewith)
))
)
)
)
)
)
)
(message (format "Opened %d new files and made %d modifications" filesopened modifications)))
)
(defun rtags-find-symbol (&optional prefix)
(interactive "P")
(rtags-find-symbols-by-name-internal "Find rsymbol" nil prefix))
(defun rtags-find-references (&optional prefix)
(interactive "P")
(rtags-find-symbols-by-name-internal "Find rreferences" t prefix))
(defun rtags-find-symbol-current-file ()
(interactive)
(rtags-find-symbol t))
(defun rtags-find-references-current-file ()
(interactive)
(rtags-find-references t))
(defun rtags-dir-filter ()
(concat (substring buffer-file-name
0
(string-match
"[^/]*/?$"
buffer-file-name))
"[^/]*/?$"))
(defun rtags-find-symbol-current-dir ()
(interactive)
(setq rtags-path-filter-regex t)
(rtags-find-symbols-by-name-internal "Find rsymbol" nil (rtags-dir-filter))
(setq rtags-path-filter-regex nil))
(defun rtags-find-references-current-dir ()
(interactive)
(setq rtags-path-filter-regex t)
(rtags-find-symbols-by-name-internal "Find rreferences" t (rtags-dir-filter))
(setq rtags-path-filter-regex nil))
(defun rtags-find-symbol-start () ;; returns column
(save-excursion
(let ((looking-at-space (looking-at "[ \t\n]")))
(skip-chars-backward " \t" (point-at-bol))
(if (and (> (point) (point-at-bol)) looking-at-space)
(backward-char))
(if (looking-at "[A-Za-z0-9_]")
(c-beginning-of-current-token)
(forward-char))
(- (point) (point-at-bol))))
)
(defun rtags-post-expand ()
(save-excursion
(let ((end (point)))
(backward-char)
(c-beginning-of-current-token)
(let ((sig (gethash (buffer-substring (point) end) rtags-completion-signatures)))
(if sig
(message "%s" (combine-and-quote-strings sig "\n")))))))
(defun rtags-expand-internal ()
(save-excursion
(with-current-buffer rtags-completion
(if (= (point-min) (point-max))
(setq rtags-completion nil)
(progn
(goto-char (point-min))
(if (looking-at "Scheduled rebuild")
(progn
(setq rtags-completion nil
rtags-completion-cache-line 0
rtags-completion-cache-column 0
rtags-completion-cache-line-contents ""
rtags-completion-cache-file-name "")))))))
(if rtags-completion
(let ((was-search dabbrev-search-these-buffers-only))
(condition-case nil
(progn
(setq dabbrev-search-these-buffers-only (list rtags-completion))
(funcall rtags-expand-function)
(setq dabbrev-search-these-buffers-only was-search)
(rtags-post-expand))
(error
(setq dabbrev-search-these-buffers-only was-search))))
(when (not (string= rtags-completion-cache-file-name ""))
(funcall rtags-expand-function)
(rtags-post-expand)))
)
(defun rtags-completion-cache-is-valid ()
(and (= (line-number-at-pos) rtags-completion-cache-line)
(= (rtags-find-symbol-start) rtags-completion-cache-column)
(string= (buffer-file-name (current-buffer)) rtags-completion-cache-file-name)
(string= (buffer-substring (point-at-bol) (+ (point-at-bol) rtags-completion-cache-column))
rtags-completion-cache-line-contents)))
(defun rtags-expand ()
(interactive)
(if rtags-completion
(rtags-expand-internal)
(progn
(funcall rtags-expand-function)
(rtags-post-expand)))
)
(defun rtags-prepare-completion ()
(interactive)
;; (message "prepare completion")
(when rtags-completion-cache-timer
(cancel-timer rtags-completion-cache-timer)
(setq rtags-completion-cache-timer nil))
(when (and (not (rtags-completion-cache-is-valid))
(rtags-init-completion-stream))
(if (buffer-live-p rtags-completion)
(with-current-buffer rtags-completion
(let (deactivate-mark)
(erase-buffer))))
(save-excursion
;;(message "prepared completion")
(let* ((buffer (current-buffer))
(path (rtags-path-for-project))
(buffer-size (- (point-max) (point-min)))
(line (line-number-at-pos))
(column (rtags-find-symbol-start))
(header (format "%s:%d:%d:%d:%d\n" (buffer-file-name buffer) line (+ column 1) (- (point) 1) (- (point-max) (point-min)))))
(setq rtags-completion (get-buffer-create "*RTags Completions*")
rtags-completion-cache-file-name (buffer-file-name buffer)
rtags-completion-cache-line line
rtags-completion-cache-column column
rtags-completion-cache-line-contents (buffer-substring (point-at-bol) (+ (point-at-bol) column)))
;; (message "writing shit %s" header)
(process-send-string rtags-completion-stream-process header)
(process-send-string rtags-completion-stream-process (buffer-substring (point-min) (point-max))))
)
)
)
(defvar rtags-diagnostics-process nil)
(defun rtags-stop-diagnostics ()
(interactive)
(if (and rtags-diagnostics-process (not (eq (process-status rtags-diagnostics-process) 'exit)))
(kill-process rtags-diagnostics-process))
(if (get-buffer "*RTags Diagnostics*")
(kill-buffer "*RTags Diagnostics*")))
(defun rtags-clear-diagnostics ()
(interactive)
(when (get-buffer "*RTags Diagnostics*")
(let (deactivate-mark)
(with-current-buffer "*RTags Diagnostics*"
(setq buffer-read-only nil)
(goto-char (point-min))
(delete-char (- (point-max) (point-min)))
(setq buffer-read-only t))
)
)
)
(defvar rtags-completion-signatures (make-hash-table :test 'equal))
(defvar rtags-completion-buffer-pending nil)
(defun rtags-completion-stream-process-filter (process output)
(let* ((buf (process-buffer process))
(process-output t)
(delimiter (string-match "`" output)))
(cond (delimiter
(let (deactivate-mark)
(with-current-buffer buf
(erase-buffer)
(setq rtags-completion-signatures (make-hash-table :test 'equal)
rtags-completion-buffer-pending nil
output (substring output (+ delimiter 1))))))
((string= (substring output 0 (min 21 (length output))) "Scheduled rebuild of ")
(progn
(setq rtags-completion nil
rtags-completion-cache-line 0
rtags-completion-cache-column 0
rtags-completion-cache-line-contents ""
rtags-completion-cache-file-name ""
process-output nil)
(rtags-restart-completion-cache-timer)))
(rtags-completion-buffer-pending
(setq output (concat rtags-completion-buffer-pending output)
rtags-completion-buffer-pending nil))
(t nil))
(if process-output
(with-current-buffer buf
(let ((deactivate-mark) (continue t) (idx 0) (last 0))
(save-excursion
(goto-char (point-max))
(while continue
(setq idx (string-match "\n" output last))
(if idx
(let* ((ws (string-match " " output last))
(key (substring output last ws))
(values (gethash key rtags-completion-signatures)))
(when (and ws (< (+ ws 1) idx))
(setq values (add-to-list 'values (substring output (+ ws 1) idx)))
(puthash key values rtags-completion-signatures))
(insert (concat key "\n"))
(setq last (+ idx 1)))
(progn
(unless (= last (length output))
(setq rtags-completion-buffer-pending (substring output last)))
(setq continue nil))
)
)
)
)
)
)
)
)
(defvar rtags-completion-stream-process nil)
(defun rtags-init-completion-stream ()
(interactive)
(if (cond ((not rtags-completion-stream-process) t)
((eq (process-status rtags-completion-stream-process) 'exit) t)
((eq (process-status rtags-completion-stream-process) 'signal) t)
(t nil))
(let ((process-connection-type nil)) ; use a pipe
(if (get-buffer "*RTags Completions*")
(kill-buffer "*RTags Completions*"))
(setq rtags-completion-stream-process (start-process
"RTags Completions Stream"