forked from Andersbakken/rtags
-
Notifications
You must be signed in to change notification settings - Fork 0
/
rtags.el
2523 lines (2274 loc) · 102 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
;; Package-Requires: ((popup "0.5.3"))
;; 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 'popup)
(require 'tramp)
(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-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-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-buffer-bookmarks 0)
(defvar rtags-diagnostics-process nil)
(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")) t))))))
(defcustom rtags-enabled t
"Whether rtags is enabled. We try to do nothing when it's not"
:group 'rtags
:type 'boolean)
(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)
(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)
(defcustom rtags-diagnostics-use-pipe t
"If diagnostics can use a pipe. If you're running emacs in cygwin you might have to set this to nil"
:group 'rtags
:type 'boolean)
(defcustom rtags-autostart-diagnostics nil
"Whether rtags automatically will restart diagnostics"
:group 'rtags
:type 'boolean)
(defcustom rtags-spellcheck-enabled t
"Whether rtags does syntax checking with overlays etc to mark errors, warnings and fixups"
:group 'rtags
:type 'boolean)
(defcustom rtags-verbose-results nil
"Print more verbose results buffer"
:group 'rtags
:type 'boolean)
(defcustom rtags-sort-references-by-input t
"Whether rtags sorts the references based on the input to rtags-find-references.*"
:group 'rtags
:type 'boolean)
(defcustom rtags-completions-enabled nil
"Whether completions are enabled"
:group 'rtags
:type 'boolean)
(defcustom rtags-completions-timer-interval nil
"Interval for completions timer. nil means don't preemptively prepare completions"
:group 'rtags
:type 'number)
(defcustom rtags-wildcard-symbol-names t
"Allow use of * and ? to match symbol names"
:group 'rtags
:type 'boolean)
(defcustom rtags-tracking nil
"When on automatically jump to symbol under cursor in *RTags* buffer"
:group 'rtags
:type 'boolean)
(defcustom rtags-track-container nil
"When on continually update current container (function/class/namespace) on intervals"
:group 'rtags
:type 'boolean)
(defcustom rtags-error-timer-interval .5
"Interval for minibuffer error timer"
:group 'rtags
:type 'number)
(defcustom rtags-display-current-error-as-message t
"Display error under cursor using (message)"
:type 'boolean
:group 'rtags)
(defcustom rtags-display-current-error-as-tooltip nil
"Display error under cursor using popup-tip (requires 'popup)"
:type 'boolean
:group 'rtags)
(defcustom rtags-display-summary-as-tooltip rtags-popup-available
"Display help / summary text using popup-tip (requires 'popup)"
:type 'boolean
:group 'rtags)
(defcustom rtags-tooltips-enabled rtags-popup-available
"Display help / summary text when hovering over symbols."
:type 'boolean
:group 'rtags)
(defcustom rtags-error-timer-interval .5
"Interval for minibuffer error timer"
:group 'rtags
:type 'number)
(defcustom rtags-tracking-timer-interval .5
"Interval for tracking timer"
:group 'rtags
:type 'number)
(defcustom rtags-container-timer-interval .5
"Interval for container timer"
:group 'rtags
:type 'number)
(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)
(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-show-containing-function nil
"If t, pass -o to rc to include containing function"
:group 'rtags
:type 'boolean)
(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-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 "gray12")))
"Face used for marking skipped lines."
:group 'rtags)
(defconst rtags-verbose-results-delimiter "------------------------------------------")
(defvar rtags-font-lock-keywords
`((,"^\\(.*?:[0-9]+:[0-9]+:\\)\\(.*\\)$"
(1 font-lock-string-face)
(2 font-lock-function-name-face))
;; (,(concat "^" rtags-verbose-results-delimiter "$")
;; (1 font-lock-builtin-face))
(,"^ +\\(.*\\)$"
(1 font-lock-function-name-face))))
(defcustom rtags-timeout nil
"Max amount of ms to wait for operation to finish"
:group 'rtags
:type 'integer)
(defcustom rtags-enable-unsaved-reparsing t
"Whether rtags will reparse unsaved buffers as needed"
:group 'rtags
:type 'boolean)
(defcustom rtags-reparse-timeout nil
"Max number of ms you're willing to wait for a reparse to finish."
:group 'rtags
:type 'integer)
(defcustom rtags-find-file-case-insensitive nil
"Treat files case-insensitively"
:group 'rtags
:type 'boolean)
(defcustom rtags-symbolnames-case-insensitive nil
"Treat symbol names case-insensitively"
:group 'rtags
:type 'boolean)
(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)
(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))
(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))))
;;;###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-derived-mode rtags-mode fundamental-mode
(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))
(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 t))
;;;###autoload
(defun rtags-previous-match () (interactive) (rtags-next-prev-match nil))
(defun rtags-next-prev-match (next)
(when (get-buffer rtags-buffer-name)
(let ((target)
(win (get-buffer-window rtags-buffer-name)))
(when win
(select-window win))
(set-buffer rtags-buffer-name)
(when (> (count-lines (point-max) (point-min)) 1)
(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 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)
(let ((result (and rtags-path (concat rtags-path "/bin/" exe))))
(if (and result (file-exists-p result))
result
(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-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")) proc)
(if (not rc)
(unless noerror (error "Can't find rc"))
(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 (cl-remove-if '(lambda (arg) (not arg)) arguments))
(when path-filter
(push (concat "--path-filter=" path-filter) arguments)
(when path-filter-regex
(push "-Z" arguments)))
(when (and unsaved (buffer-file-name unsaved))
(push (format "--unsaved-file=%s:%d"
(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=" default-directory) arguments))
(t nil))
(when rtags-rc-log-enabled
(rtags-log (concat rc " " (rtags-combine-strings arguments))))
(let ((proc (cond ((and unsaved async)
(let ((proc (apply #'start-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-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 #'call-process-region (point-min) (point-max) rc
nil output-buffer nil arguments) nil))))
(unsaved (apply #'call-process rc (buffer-file-name unsaved) output nil arguments) nil)
(t (apply #'call-process rc nil output nil arguments) nil))))
(if proc
(progn
(set-process-query-on-exit-flag proc nil)
(set-process-filter proc (car async))
(set-process-sentinel proc (cdr async)))
(goto-char (point-min))
(and (cond ((looking-at "Can't seem to connect to server")
(erase-buffer)
(unless noerror
(message "Can't seem to connect to server. Is rdm running?"))
nil)
((looking-at "Project loading")
(erase-buffer)
(message "Project loading...")
t)
(t))
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)))
;;;###autoload
(defun rtags-preprocess-file (&optional buffer)
(interactive)
(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-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
(with-temp-buffer (rtags-call-rc :output nil :path t "-w" 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))
(thing-at-point 'symbol)))
(defun* rtags-symbol-info (&rest args
&key
(location nil)
(include-targets nil)
(include-references nil)
(include-parents nil)
(save-to-kill-ring nil)
(silent-query nil)
(noerror nil)
(no-reparse nil))
(interactive)
(let ((loc (or location (rtags-current-location)))
(path (buffer-file-name)))
(when (not no-reparse)
(rtags-reparse-file-if-needed))
(with-temp-buffer
(rtags-call-rc :path path
:noerror noerror
:silent-query silent-query
"-U" loc
(unless include-targets "--symbol-info-exclude-targets")
(unless include-targets "--symbol-info-exclude-references")
(when include-parents "--symbol-info-include-parents"))
(when save-to-kill-ring
(copy-region-as-kill (point-min) (point-max)))
(when (called-interactively-p 'any)
(message "%s" (buffer-string)))
(buffer-string))))
;;;###autoload
(defun rtags-print-symbol-info (&optional verbose)
(interactive "P")
(message "%s" (rtags-symbol-info :include-parents verbose
:include-targets verbose
:include-references verbose)))
;;;###autoload
(defun rtags-symbol-type ()
(interactive)
(let ((info (rtags-symbol-info)))
(when (and info (string-match "^Type: \\(.*\\)$" info))
(when (called-interactively-p 'any)
(message (match-string 1 info)))
(match-string 1 info))))
;;;###autoload
(defun rtags-print-dependencies (&optional buffer)
(interactive)
(let ((dep-buffer (rtags-get-buffer))
(fn (buffer-file-name buffer)))
(when fn
(rtags-location-stack-push)
(switch-to-buffer dep-buffer)
(rtags-call-rc :path fn "--dependencies" fn)
(rtags-mode))))
;;;###autoload
(defun rtags-print-source-arguments (&optional buffer)
(interactive)
(let ((args-buffer (rtags-get-buffer))
(source (buffer-file-name buffer)))
(when source
(rtags-location-stack-push)
(switch-to-buffer args-buffer)
(rtags-call-rc :path source "--sources" source)
(goto-char (point-min))
(when (= (point-min) (point-max))
(message "No builds for: %s" source)
(rtags-location-stack-back)
(kill-buffer args-buffer)))))
;;;###autoload
(defun rtags-print-class-hierarchy()
(interactive)
(let ((class-hierarchy-buffer (rtags-get-buffer))
(path (buffer-file-name))
(location (rtags-current-location)))
(when (and path location)
(rtags-location-stack-push)
(switch-to-buffer class-hierarchy-buffer)
(rtags-call-rc :path path "--class-hierarchy" location)
(if (> (point-max) (point-min))
(rtags-mode)
(message "No subclasses for: %s" location)
(rtags-location-stack-back)))))
;;;###autoload
(defun rtags-print-enum-value-at-point (&optional location)
(interactive)
(let ((info (rtags-symbol-info :location location)))
(cond ((string-match "^Enum Value: \\([0-9]+\\) *$" info)
(let ((enumval (match-string-no-properties 1 info)))
(message "%s - %s - 0x%X" (rtags-current-symbol-name info) enumval (string-to-number enumval))))
((string-match "^Type: Enum *$" info)
(let ((target (rtags-target)))
(when target
(setq info (rtags-symbol-info :location target))
(when (string-match "^Enum Value: \\([0-9]+\\) *$" info)
(let ((enumval (match-string-no-properties 1 info)))
(message "%s - %s - 0x%X" (rtags-current-symbol-name info) enumval (string-to-number enumval)))))))
(t (message "RTags: No enum here") nil))))
(defun rtags-buffer-is-multibyte ()
(string-match "\\butf\\b" (symbol-name buffer-file-coding-system)))
(defun rtags-point-multibyte (&optional p)
(save-restriction
(widen)
(save-excursion
(when p
(goto-char p))
(if (rtags-buffer-is-multibyte)
(let ((prev (buffer-local-value enable-multibyte-characters (current-buffer)))
(loc (local-variable-p enable-multibyte-characters))
(pos))
(set-buffer-multibyte nil)
(setq pos (point))
(set-buffer-multibyte prev)
(unless loc
(kill-local-variable enable-multibyte-characters))
pos)
(point)))))
(defun rtags-buffer-size ()
(- (rtags-point-multibyte (point-max)) (point-min)))
(defun rtags-offset (&optional p)
(1- (rtags-point-multibyte p)))
;;;###autoload
(defun rtags-goto-offset (pos)
(interactive "NOffset: ")
(if (rtags-buffer-is-multibyte)
(let ((prev (buffer-local-value enable-multibyte-characters (current-buffer)))
(loc (local-variable-p enable-multibyte-characters)))
(set-buffer-multibyte nil)
(goto-char (1+ pos))
(set-buffer-multibyte prev)
(unless loc
(kill-local-variable enable-multibyte-characters)))
(goto-char (1+ pos))))
(defun rtags-current-location (&optional offset)
(let ((fn (buffer-file-name)))
(and fn (format "%s:%d:%d:" fn (line-number-at-pos offset) (1+ (- (or offset (point)) (point-at-bol)))))))
(defun rtags-log (log)
(with-current-buffer (rtags-get-buffer-create-no-undo "*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-find-file-or-buffer (file-or-buffer &optional other-window)
(if (file-exists-p file-or-buffer)
(if other-window
(find-file-other-window file-or-buffer)
(find-file file-or-buffer))
(let ((buf (get-buffer file-or-buffer)))
(cond ((not buf) (message "No buffer named %s" file-or-buffer))
(other-window (switch-to-buffer-other-window file-or-buffer))
(t (switch-to-buffer file-or-buffer))))))
(defun rtags-goto-line-col (line column)
(let ((old (point)))
(push-mark)
(goto-char (point-min))
(condition-case nil
(progn
(forward-line (1- line))
(forward-char (1- column))
t)
(error
(goto-char old)
nil))))
(defun rtags-goto-location (location &optional nobookmark other-window)
"Go to a location passed in. It can be either: file,12 or file:13:14 or plain file"
;; (message (format "rtags-goto-location \"%s\"" location))
(when (> (length location) 0)
(cond ((string-match "\\(.*\\):\\([0-9]+\\):\\([0-9]+\\):?" location)
(let ((line (string-to-number (match-string-no-properties 2 location)))
(column (string-to-number (match-string-no-properties 3 location))))
(rtags-find-file-or-buffer (match-string-no-properties 1 location) other-window)
(run-hooks rtags-after-find-file-hook)
(rtags-goto-line-col line column)
t))
((string-match "\\(.*\\):\\([0-9]+\\):?" location)
(let ((line (string-to-number (match-string-no-properties 2 location))))
(rtags-find-file-or-buffer (match-string-no-properties 1 location) other-window)
(run-hooks rtags-after-find-file-hook)
(goto-char (point-min))
(forward-line (1- line))
t))
((string-match "\\(.*\\),\\([0-9]+\\)" location)
(let ((offset (string-to-number (match-string-no-properties 2 location))))
(rtags-find-file-or-buffer (match-string-no-properties 1 location) other-window)
(run-hooks rtags-after-find-file-hook)
(rtags-goto-offset offset)
t))
(t
(when (string-match "^ +\\(.*\\)$" location)
(setq location (match-string-no-properties 1 location)))
(rtags-find-file-or-buffer location other-window)))
(unless nobookmark (rtags-location-stack-push))))
(defvar rtags-location-stack-index 0)
(defvar rtags-location-stack nil)
(defun rtags-location-stack-push ()
(let ((bm (rtags-current-location)))
(while (> rtags-location-stack-index 0)
(decf rtags-location-stack-index)
(pop rtags-location-stack))
(unless (string= bm (nth 0 rtags-location-stack))
(push bm rtags-location-stack)
(when (> (length rtags-location-stack) rtags-max-bookmark-count)
(nbutlast rtags-location-stack (- (length rtags-location-stack) rtags-max-bookmark-count))))))
;;;###autoload
(defun rtags-location-stack-jump (by)
(interactive)
(let (;; copy of repeat-on-final-keystroke functionality from repeat.el
(repeat-char
(if (eq repeat-on-final-keystroke t)
last-command-event
(car (memq last-command-event
(listify-key-sequence
repeat-on-final-keystroke)))))
(instack (nth rtags-location-stack-index rtags-location-stack))
(cur (rtags-current-location)))
(if (not (string= instack cur))
(rtags-goto-location instack t)
(let ((target (+ rtags-location-stack-index by)))
(when (and (>= target 0) (< target (length rtags-location-stack)))
(setq rtags-location-stack-index target)
(rtags-goto-location (nth rtags-location-stack-index rtags-location-stack) t))))
(when repeat-char
(let ((map (make-sparse-keymap)))
(define-key map (vector repeat-char)
`(lambda ()
(interactive)
(rtags-location-stack-jump ,by)))
(cond ((fboundp 'set-transient-map) (set-transient-map map))
((fboundp 'set-temporary-overlay-map) (set-temporary-overlay-map map))
(t))))))
;; **************************** API *********************************
;;;###autoload
(defun rtags-enable-standard-keybindings (&optional map prefix)
(interactive)
(unless map
(setq map c-mode-base-map))
(unless prefix
(setq prefix "\C-xr"))
(ignore-errors
(define-key map (concat prefix ".") (function rtags-find-symbol-at-point))
(define-key map (concat prefix ",") (function rtags-find-references-at-point))
(define-key map (concat prefix "v") (function rtags-find-virtuals-at-point))
(define-key map (concat prefix "V") (function rtags-print-enum-value-at-point))
(define-key map (concat prefix "/") (function rtags-find-all-references-at-point))
(define-key map (concat prefix "Y") (function rtags-cycle-overlays-on-screen))
(define-key map (concat prefix ">") (function rtags-find-symbol))
(define-key map (concat prefix "<") (function rtags-find-references))
(define-key map (concat prefix "[") (function rtags-location-stack-back))
(define-key map (concat prefix "]") (function rtags-location-stack-forward))
(define-key map (concat prefix "D") (function rtags-diagnostics))
(define-key map (concat prefix "C") (function rtags-compile-file))
(define-key map (concat prefix "G") (function rtags-guess-function-at-point))
(define-key map (concat prefix "p") (function rtags-set-current-project))
(define-key map (concat prefix "P") (function rtags-print-dependencies))
(define-key map (concat prefix "e") (function rtags-reparse-file))
(define-key map (concat prefix "E") (function rtags-preprocess-file))
(define-key map (concat prefix "R") (function rtags-rename-symbol))
(define-key map (concat prefix "M") (function rtags-symbol-info))
(define-key map (concat prefix "S") (function rtags-display-summary))
(define-key map (concat prefix "O") (function rtags-goto-offset))
(define-key map (concat prefix ";") (function rtags-find-file))
(define-key map (concat prefix "F") (function rtags-fixit))
(define-key map (concat prefix "L") (function rtags-copy-and-print-current-location))
(define-key map (concat prefix "X") (function rtags-fix-fixit-at-point))
(define-key map (concat prefix "B") (function rtags-show-rtags-buffer))
(define-key map (concat prefix "I") (function rtags-imenu))
(define-key map (concat prefix "T") (function rtags-taglist))
(define-key map (concat prefix "h") (function rtags-print-class-hierarchy))
(define-key map (concat prefix "a") (function rtags-print-source-arguments))))
;;;###autoload
(defun rtags-print-current-location ()
(interactive)
(message (rtags-current-location)))
;;;###autoload
(defun rtags-location-stack-forward ()
(interactive)
(rtags-location-stack-jump -1))
;;;###autoload
(defun rtags-location-stack-back ()
(interactive)
(rtags-location-stack-jump 1))
;;;###autoload
(defun rtags-location-stack-reset ()
(interactive)
(setq rtags-location-stack nil)
(setq rtags-location-stack-index 0))
(defun rtags-not-indexed/connected-message-p (string)
(or (string= string "Not indexed\n")
(string= string "Can't seem to connect to server\n")))
(defun rtags-target (&optional filter declaration-only no-reparse no-error)
"DONT-REPARSE : do not reparse file even if it appears as modified."
(let ((path (buffer-file-name))
(location (rtags-current-location))
(unsaved (and (buffer-modified-p) (current-buffer))))
(when path
(unless no-reparse
(rtags-reparse-file-if-needed))
(with-temp-buffer
(if declaration-only
(rtags-call-rc :path path "--declaration-only" "-N" "-f" location :path-filter filter :noerror t :unsaved unsaved)
(rtags-call-rc :path path "-N" "-f" location :path-filter filter :noerror t :unsaved unsaved))
(setq rtags-last-request-not-indexed nil)
(cond ((= (point-min) (point-max))
(unless no-error (message "RTags: No target")) nil)
((rtags-not-indexed/connected-message-p (buffer-string))
(setq rtags-last-request-not-indexed t) nil)
(t (buffer-substring-no-properties (point-min) (- (point-max) 1))))))))
(defun rtags-target-declaration-first ()
"First try to find the declaration of the item (using --declaration-only), then try
to find anything about the item."
(let ((target (or (rtags-target nil t nil t)
(rtags-target nil nil t))))
target))
;; (defalias 'rtags-find-symbol-at-point 'rtags-follow-symbol-at-point)
;;;###autoload
(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-location-stack-push)
(let ((arg (rtags-current-location))
(fn (buffer-file-name)))
(rtags-reparse-file-if-needed)
(with-current-buffer (rtags-get-buffer)
(rtags-call-rc :path fn :path-filter prefix "-f" arg)
(rtags-handle-results-buffer))))
;;;###autoload
(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-location-stack-push)
(let ((arg (rtags-current-location))
(fn (buffer-file-name)))
(rtags-reparse-file-if-needed)
(with-current-buffer (rtags-get-buffer)
(rtags-call-rc :path fn :path-filter prefix "-r" arg (unless rtags-sort-references-by-input "--no-sort-references-by-input"))
(rtags-handle-results-buffer))))
;;;###autoload
(defun rtags-find-virtuals-at-point (&optional prefix)
"List all reimplentations of function under cursor. This includes both declarations and definitions"