-
-
Notifications
You must be signed in to change notification settings - Fork 31
/
helm-gtags.el
1496 lines (1318 loc) · 57.3 KB
/
helm-gtags.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
;;; helm-gtags.el --- GNU GLOBAL helm interface -*- lexical-binding: t; -*-
;; Copyright (C) 2016 by Syohei YOSHIDA
;; Copyright (C) 2020-2024 Shen, Jen-Chieh
;; Author: Syohei YOSHIDA <syohex@gmail.com>
;; Maintainer: Jen-Chieh Shen <jcs090218@gmail.com>
;; URL: https://github.com/syohex/emacs-helm-gtags
;; Version: 1.5.7
;; Package-Requires: ((emacs "24.4") (helm "2.0"))
;; This program 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.
;; This program 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 this program. If not, see <http://www.gnu.org/licenses/>.
;;; Commentary:
;; `helm-gtags.el' is a `helm' interface of GNU Global.
;; `helm-gtags.el' is not compatible `anything-gtags.el', but `helm-gtags.el'
;; is designed for fast search.
;;
;; To use this package, add these lines to your init.el or .emacs file:
;;
;; ;; Enable helm-gtags-mode
;; (add-hook 'c-mode-hook 'helm-gtags-mode)
;; (add-hook 'c++-mode-hook 'helm-gtags-mode)
;; (add-hook 'asm-mode-hook 'helm-gtags-mode)
;;
;; ;; Set key bindings
;; (eval-after-load "helm-gtags"
;; '(progn
;; (define-key helm-gtags-mode-map (kbd "M-t") 'helm-gtags-find-tag)
;; (define-key helm-gtags-mode-map (kbd "M-r") 'helm-gtags-find-rtag)
;; (define-key helm-gtags-mode-map (kbd "M-s") 'helm-gtags-find-symbol)
;; (define-key helm-gtags-mode-map (kbd "M-g M-p") 'helm-gtags-parse-file)
;; (define-key helm-gtags-mode-map (kbd "C-c <") 'helm-gtags-previous-history)
;; (define-key helm-gtags-mode-map (kbd "C-c >") 'helm-gtags-next-history)
;; (define-key helm-gtags-mode-map (kbd "M-,") 'helm-gtags-pop-stack)))
;;
;;; Code:
(require 'cl-lib)
(require 'helm)
(require 'helm-files)
(require 'which-func)
(require 'pulse)
(require 'subr-x)
(declare-function helm-comp-read "helm-mode")
(declare-function cygwin-convert-file-name-from-windows "cygw32.c")
(declare-function cygwin-convert-file-name-to-windows "cygw32.c")
(defgroup helm-gtags nil
"GNU GLOBAL for helm."
:group 'helm)
(defcustom helm-gtags-path-style 'root
"Style of file path."
:type '(choice (const :tag "Root of the current project" root)
(const :tag "Relative from the current directory" relative)
(const :tag "Absolute Path" absolute)))
(defcustom helm-gtags-ignore-case nil
"Ignore case in each search."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-cygwin-use-global-w32-port t
"Use the GNU global win32 port in Cygwin."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-read-only nil
"Gtags read only mode."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-auto-update nil
"*If non-nil, tag files are updated whenever a file is saved."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-pulse-at-cursor t
"If non-nil, pulse at point after jumping."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-cache-select-result nil
"*If non-nil, results of helm-gtags-select and helm-gtags-select-path are cached."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-cache-max-result-size (* 10 1024 1024) ;10M
"Max size(bytes) to cache for each select result."
:type 'integer
:group 'helm-gtags)
(defcustom helm-gtags-update-interval-second 60
"Tags are updated in `after-save-hook' if this seconds is passed from \
last update.
Always update if value of this variable is nil."
:type '(choice (integer :tag "Update interval seconds")
(boolean :tag "Update every time" nil))
:group 'helm-gtags)
(defcustom helm-gtags-highlight-candidate t
"Highlight candidate or not."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-use-input-at-cursor nil
"Use input at cursor."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-prefix-key "\C-c"
"If non-nil, it is used for the prefix key of gtags-xxx command."
:type 'string
:group 'helm-gtags)
(defcustom helm-gtags-suggested-key-mapping nil
"If non-nil, suggested key mapping is enabled."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-preselect nil
"If non-nil, preselect current file and line."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-display-style nil
"Style of display result."
:type '(choice (const :tag "Show in detail" detail)
(const :tag "Normal style" nil))
:group 'helm-gtags)
(defcustom helm-gtags-fuzzy-match nil
"Enable fuzzy match."
:type 'boolean
:group 'helm-gtags)
(defcustom helm-gtags-maximum-candidates (if helm-gtags-fuzzy-match 100 9999)
"Maximum number of helm candidates."
:type 'integer
:group 'helm-gtags)
(defcustom helm-gtags-symbol-at-point-function 'helm-gtags--symbol-at-point
"Function to use to get the symbol at point."
:type 'symbol)
(defcustom helm-gtags-direct-helm-completing nil
"Use helm mode directly."
:type 'boolean
:group 'helm-gtags)
(defface helm-gtags-file
'((t :inherit font-lock-keyword-face))
"Face for line numbers in the error list.")
(defface helm-gtags-lineno
'((t :inherit font-lock-doc-face))
"Face for line numbers in the error list.")
(defface helm-gtags-match
'((t :inherit helm-match))
"Face for word matched against tagname")
(defvar helm-gtags--tag-location nil)
(defvar helm-gtags--last-update-time 0)
(defvar helm-gtags--completing-history nil)
(defvar helm-gtags--context-stack (make-hash-table :test 'equal))
(defvar helm-gtags--result-cache (make-hash-table :test 'equal))
(defvar helm-gtags--saved-context nil)
(defvar helm-gtags--use-otherwin nil)
(defvar helm-gtags--local-directory nil)
(defvar helm-gtags--parsed-file nil)
(defvar helm-gtags--current-position nil)
(defvar helm-gtags--real-tag-location nil)
(defvar helm-gtags--last-input nil)
(defvar helm-gtags--query nil)
(defvar helm-gtags--last-default-directory nil)
(defconst helm-gtags--buffer "*helm gtags*"
"Not documented.")
(defconst helm-gtags--include-regexp
"\\`\\s-*#\\(?:include\\|import\\)\\s-*[\"<]\\(?:[./]*\\)?\\(.*?\\)[\">]"
"Not documented.")
(defmacro helm-declare-obsolete-variable (old new version)
"Not documented, OLD, NEW, VERSION."
`(progn
(defvaralias ,old ,new)
(make-obsolete-variable ,old ,new ,version)))
(helm-declare-obsolete-variable
'helm-c-gtags-path-style 'helm-gtags-path-style "0.8")
(helm-declare-obsolete-variable
'helm-c-gtags-ignore-case 'helm-gtags-ignore-case "0.8")
(helm-declare-obsolete-variable
'helm-c-gtags-read-only 'helm-gtags-read-only "0.8")
;; completsion function for completing-read.
(defun helm-gtags--completing-gtags (string predicate code)
(helm-gtags--complete 'tag string predicate code))
(defun helm-gtags--completing-pattern (string predicate code)
(helm-gtags--complete 'pattern string predicate code))
(defun helm-gtags--completing-grtags (string predicate code)
(helm-gtags--complete 'rtag string predicate code))
(defun helm-gtags--completing-gsyms (string predicate code)
(helm-gtags--complete 'symbol string predicate code))
(defun helm-gtags--completing-files (string predicate code)
(helm-gtags--complete 'find-file string predicate code))
(defconst helm-gtags-comp-func-alist
'((tag . helm-gtags--completing-gtags)
(pattern . helm-gtags--completing-pattern)
(rtag . helm-gtags--completing-grtags)
(symbol . helm-gtags--completing-gsyms)
(find-file . helm-gtags--completing-files))
"Not documented.")
(defconst helm-gtags--search-option-alist
'((pattern . "-g")
(rtag . "-r")
(symbol . "-s")
(find-file . "-Poa"))
"Not documented.")
(defsubst helm-gtags--windows-p ()
"Not documented."
(memq system-type '(windows-nt ms-dos)))
(defun helm-gtags--remove-carrige-returns ()
"Not documented."
(when (helm-gtags--windows-p)
(save-excursion
(goto-char (point-min))
(while (re-search-forward "\xd" nil t)
(replace-match "")))))
;; Work around for GNU global Windows issue
(defsubst helm-gtags--use-abs-path-p (gtagslibpath)
"Not documented."
(and (helm-gtags--windows-p) gtagslibpath))
(defun helm-gtags--construct-options (type completion)
"Not documented."
(let ((find-file-p (eq type 'find-file))
(gtagslibpath (getenv "GTAGSLIBPATH"))
options)
(unless find-file-p
(push "--result=grep" options))
(when completion
(push "-c" options))
(helm-aif (assoc-default type helm-gtags--search-option-alist)
(push it options))
(when (or (eq helm-gtags-path-style 'absolute)
(helm-gtags--use-abs-path-p gtagslibpath))
(push "-a" options))
(when helm-gtags-ignore-case
(push "-i" options))
(when (and current-prefix-arg (not find-file-p))
(push "-l" options))
(when gtagslibpath
(push "-T" options))
options))
(defun helm-gtags--complete (type string predicate code)
"Not documented."
(let* ((options (helm-gtags--construct-options type t))
(args (reverse (cons string options)))
candidates)
(with-temp-buffer
(apply #'process-file "global" nil t nil args)
(goto-char (point-min))
(while (re-search-forward "^\\(.+\\)$" nil t)
(push (match-string-no-properties 1) candidates)))
(if (not code)
(try-completion string candidates predicate)
(all-completions string candidates predicate))))
(defun helm-gtags--symbol-at-point ()
(thing-at-point 'symbol))
(defun helm-gtags--token-at-point (type)
"Not documented."
(if (not (eq type 'find-file))
(funcall helm-gtags-symbol-at-point-function)
(let ((line (helm-current-line-contents)))
(when (string-match helm-gtags--include-regexp line)
(match-string-no-properties 1 line)))))
(defconst helm-gtags--prompt-alist
'((tag . "Find Definition: ")
(pattern . "Find Pattern: ")
(rtag . "Find Reference: ")
(symbol . "Find Symbol: ")
(find-file . "Find File: "))
"Not documented.")
(defun helm-gtags--read-tagname (type &optional default-tagname)
"Not documented."
(let ((tagname (helm-gtags--token-at-point type))
(prompt (assoc-default type helm-gtags--prompt-alist))
(comp-func (assoc-default type helm-gtags-comp-func-alist)))
(if (and tagname helm-gtags-use-input-at-cursor)
tagname
(when (and (not tagname) default-tagname)
(setq tagname default-tagname))
(when tagname
(setq prompt (format "%s(default \"%s\") " prompt tagname)))
(let ((completion-ignore-case helm-gtags-ignore-case)
(completing-read-function 'completing-read-default))
(if (and helm-gtags-direct-helm-completing (memq type '(tag rtag symbol find-file)))
(helm-comp-read prompt comp-func
:history 'helm-gtags--completing-history
:exec-when-only-one t
:default tagname)
(completing-read prompt comp-func nil nil nil
'helm-gtags--completing-history tagname))))))
(defun helm-gtags--path-libpath-p (tagroot)
"Not documented."
(helm-aif (getenv "GTAGSLIBPATH")
(cl-loop for path in (parse-colon-path it)
for libpath = (file-name-as-directory (expand-file-name path))
thereis (string= tagroot libpath))))
(defsubst helm-gtags--convert-cygwin-windows-file-name-p ()
"Not documented."
(and (eq system-type 'cygwin) helm-gtags-cygwin-use-global-w32-port))
(defun helm-gtags--tag-directory ()
"Not documented."
(with-temp-buffer
(helm-aif (getenv "GTAGSROOT")
it
(unless (zerop (process-file "global" nil t nil "-p"))
(error "GTAGS not found"))
(goto-char (point-min))
(when (looking-at "^\\([^\r\n]+\\)")
(let ((tag-path (match-string-no-properties 1)))
(file-name-as-directory
(if (helm-gtags--convert-cygwin-windows-file-name-p)
(cygwin-convert-file-name-from-windows tag-path)
tag-path)))))))
(defun helm-gtags--find-tag-directory ()
"Not documented."
(setq helm-gtags--real-tag-location nil)
(let ((tagroot (helm-gtags--tag-directory)))
(if (and (helm-gtags--path-libpath-p tagroot) helm-gtags--tag-location)
(progn
(setq helm-gtags--real-tag-location tagroot)
helm-gtags--tag-location)
(setq helm-gtags--tag-location tagroot))))
(defun helm-gtags--base-directory ()
"Not documented."
(let ((dir (or helm-gtags--last-default-directory
helm-gtags--local-directory
(cl-case helm-gtags-path-style
(root (or helm-gtags--real-tag-location
helm-gtags--tag-location))
(otherwise default-directory))))
(remote (file-remote-p default-directory)))
(if (and remote (not (file-remote-p dir)))
(concat remote dir)
dir)))
(defsubst helm-gtags--new-context-info (index stack)
"Not documented."
(list :index index :stack stack))
(defun helm-gtags--put-context-stack (tag-location index stack)
"Not documented."
(puthash tag-location (helm-gtags--new-context-info index stack)
helm-gtags--context-stack))
(defsubst helm-gtags--current-context ()
"Not documented."
(let ((file (buffer-file-name (current-buffer))))
(list :file file :position (point) :readonly buffer-file-read-only)))
(defsubst helm-gtags--save-current-context ()
"Not documented."
(setq helm-gtags--saved-context (helm-gtags--current-context)))
(defun helm-gtags--open-file (file readonly)
"Not documented."
(if readonly
(find-file-read-only file)
(find-file file)))
(defun helm-gtags--open-file-other-window (file readonly)
"Not documented."
(setq helm-gtags--use-otherwin nil)
(if readonly
(find-file-read-only-other-window file)
(find-file-other-window file)))
(defun helm-gtags--get-context-info ()
"Not documented."
(let* ((tag-location (helm-gtags--find-tag-directory))
(context-info (gethash tag-location helm-gtags--context-stack))
(context-stack (plist-get context-info :stack)))
(if (null context-stack)
(error "Context stack is empty(TAG at %s)" tag-location)
context-info)))
(defun helm-gtags--get-or-create-context-info ()
"Not documented."
(or (gethash helm-gtags--tag-location helm-gtags--context-stack)
(helm-gtags--new-context-info -1 nil)))
;;;###autoload
(defun helm-gtags-clear-all-cache ()
"Not documented."
(interactive)
(clrhash helm-gtags--result-cache))
;;;###autoload
(defun helm-gtags-clear-cache ()
"Not documented."
(interactive)
(helm-gtags--find-tag-directory)
(let* ((tag-location (or helm-gtags--real-tag-location
helm-gtags--tag-location))
(gtags-path (concat tag-location "GTAGS"))
(gpath-path (concat tag-location "GPATH")))
(remhash gtags-path helm-gtags--result-cache)
(remhash gpath-path helm-gtags--result-cache)))
(defun helm-gtags--move-to-context (context)
"Not documented."
(let ((file (plist-get context :file))
(curpoint (plist-get context :position))
(readonly (plist-get context :readonly)))
(helm-gtags--open-file file readonly)
(goto-char curpoint)
(recenter)))
;;;###autoload
(defun helm-gtags-next-history ()
"Jump to next position on context stack"
(interactive)
(let* ((context-info (helm-gtags--get-context-info))
(current-index (plist-get context-info :index))
(context-stack (plist-get context-info :stack))
context)
(when (<= current-index -1)
(error "This context is latest in context stack"))
(setf (nth current-index context-stack) (helm-gtags--current-context))
(cl-decf current-index)
(if (= current-index -1)
(setq context helm-gtags--current-position
helm-gtags--current-position nil)
(setq context (nth current-index context-stack)))
(helm-gtags--put-context-stack helm-gtags--tag-location
current-index context-stack)
(helm-gtags--move-to-context context)))
;;;###autoload
(defun helm-gtags-previous-history ()
"Jump to previous position on context stack"
(interactive)
(let* ((context-info (helm-gtags--get-context-info))
(current-index (plist-get context-info :index))
(context-stack (plist-get context-info :stack))
(context-length (length context-stack)))
(cl-incf current-index)
(when (>= current-index context-length)
(error "This context is last in context stack"))
(if (= current-index 0)
(setq helm-gtags--current-position (helm-gtags--current-context))
(setf (nth (- current-index 1) context-stack) (helm-gtags--current-context)))
(let ((prev-context (nth current-index context-stack)))
(helm-gtags--move-to-context prev-context))
(helm-gtags--put-context-stack helm-gtags--tag-location
current-index context-stack)))
(defun helm-gtags--get-result-cache (file)
"Not documented."
(helm-gtags--find-tag-directory)
(let* ((file-path (concat (or helm-gtags--real-tag-location
helm-gtags--tag-location)
file))
(file-mtime (nth 5 (file-attributes file-path)))
(hash-value (gethash file-path helm-gtags--result-cache))
(cached-file-mtime (nth 0 hash-value)))
(if (and cached-file-mtime (equal cached-file-mtime file-mtime))
(nth 1 hash-value)
nil)))
(defun helm-gtags--put-result-cache (file cache)
"Not documented."
(helm-gtags--find-tag-directory)
(let* ((file-path (concat (or helm-gtags--real-tag-location
helm-gtags--tag-location)
file))
(file-mtime (nth 5 (file-attributes file-path)))
(hash-value (list file-mtime cache)))
(puthash file-path hash-value helm-gtags--result-cache)))
(defun helm-gtags--referer-function (file ref-line)
"Not documented."
(let ((is-opened (cl-loop with path = (concat default-directory file)
for buf in (buffer-list)
when (string= (buffer-file-name buf) path)
return it))
retval)
(with-current-buffer (find-file-noselect file)
(goto-char (point-min))
(forward-line (1- ref-line))
(unless (zerop (current-indentation))
(setq retval (which-function)))
(unless is-opened
(kill-buffer (current-buffer)))
retval)))
(defun helm-gtags--show-detail ()
"Not documented."
(goto-char (point-min))
(while (not (eobp))
(let ((line (helm-current-line-contents)))
(let* ((file-and-line (helm-gtags--extract-file-and-line line))
(file (car file-and-line))
(ref-line (cdr file-and-line))
(ref-func (helm-gtags--referer-function file ref-line)))
(when ref-func
(search-forward ":" nil nil 2)
(insert " " ref-func "|"))
(forward-line 1)))))
(defun helm-gtags--print-path-in-gtagslibpath (args)
"Not documented."
(let ((libpath (getenv "GTAGSLIBPATH")))
(when libpath
(dolist (path (parse-colon-path libpath))
(let ((default-directory (file-name-as-directory path)))
(apply #'process-file "global" nil t nil "-Poa" args))))))
(defun helm-gtags--exec-global-command (type input &optional detail)
"Not documented."
(let ((args (helm-gtags--construct-command type input)))
(helm-gtags--find-tag-directory)
(helm-gtags--save-current-context)
(let ((buf-coding buffer-file-coding-system))
(with-current-buffer (helm-candidate-buffer 'global)
(let ((default-directory (helm-gtags--base-directory))
(input (car (last args)))
(coding-system-for-read buf-coding)
(coding-system-for-write buf-coding))
(unless (zerop (apply #'process-file "global" nil '(t nil) nil args))
(error (format "%s: not found" input)))
;; --path options does not support searching under GTAGSLIBPATH
(when (eq type 'find-file)
(helm-gtags--print-path-in-gtagslibpath args))
(helm-gtags--remove-carrige-returns)
(when detail
(helm-gtags--show-detail)))))))
(defun helm-gtags--construct-command (type &optional in)
"Not documented."
(setq helm-gtags--local-directory nil)
(let ((dir (helm-attr 'helm-gtags-base-directory (helm-get-current-source))))
(when (and dir (not (eq type 'find-file)))
(setq helm-gtags--local-directory dir)))
(let ((input (or in helm-gtags--query))
(options (helm-gtags--construct-options type nil)))
(when (string-empty-p input)
(error "Input is empty!!"))
(setq helm-gtags--last-input input)
(reverse (cons input options))))
(defun helm-gtags--tags-init (&optional input)
"Not documented."
(helm-gtags--exec-global-command 'tag input))
(defun helm-gtags--pattern-init (&optional input)
"Not documented."
(helm-gtags--exec-global-command 'pattern input helm-gtags-display-style))
(defun helm-gtags--rtags-init (&optional input)
"Not documented."
(helm-gtags--exec-global-command 'rtag input helm-gtags-display-style))
(defun helm-gtags--gsyms-init ()
"Not documented."
(helm-gtags--exec-global-command 'symbol nil helm-gtags-display-style))
(defun helm-gtags--files-init ()
"Not documented."
(helm-gtags--exec-global-command 'find-file nil))
(defun helm-gtags--real-file-name ()
"Not documented."
(let ((buffile (buffer-file-name)))
(unless buffile
(error "This buffer is not related to file."))
(if (file-remote-p buffile)
(tramp-file-name-localname (tramp-dissect-file-name buffile))
(file-truename buffile))))
(defun helm-gtags--find-tag-from-here-init ()
"Not documented."
(helm-gtags--find-tag-directory)
(helm-gtags--save-current-context)
(let ((token (helm-gtags--token-at-point 'from-here)))
(unless token
(error "Cursor is not on symbol."))
(let* ((filename (helm-gtags--real-file-name))
(from-here-opt (format "--from-here=%d:%s"
(line-number-at-pos)
(if (helm-gtags--convert-cygwin-windows-file-name-p)
(cygwin-convert-file-name-to-windows filename)
filename))))
(setq helm-gtags--last-input token)
(with-current-buffer (helm-candidate-buffer 'global)
(let* ((default-directory (helm-gtags--base-directory))
(status (process-file "global" nil '(t nil) nil
"--result=grep" from-here-opt token)))
(helm-gtags--remove-carrige-returns)
(unless (zerop status)
(cond ((= status 1)
(error "Error: %s%s" (buffer-string) filename))
((= status 3)
(error "Error: %s" (buffer-string)))
(t (error "%s: not found" token)))))))))
(defun helm-gtags--parse-file-init ()
"Not documented."
(with-current-buffer (helm-candidate-buffer 'global)
(unless (zerop (process-file "global" nil t nil
"--result=cscope" "-f" helm-gtags--parsed-file))
(error "Failed: 'global --result=cscope -f %s" helm-gtags--parsed-file))
(helm-gtags--remove-carrige-returns)))
(defun helm-gtags--push-context (context)
"Not documented."
(let* ((context-info (helm-gtags--get-or-create-context-info))
(current-index (plist-get context-info :index))
(context-stack (plist-get context-info :stack)))
(unless (= current-index -1)
(setq context-stack (nthcdr (1+ current-index) context-stack)))
(setq helm-gtags--current-position nil)
(push context context-stack)
(helm-gtags--put-context-stack helm-gtags--tag-location -1 context-stack)))
(defsubst helm-gtags--select-find-file-func ()
"Not documented."
(if helm-gtags--use-otherwin
#'helm-gtags--open-file-other-window
#'helm-gtags--open-file))
(defun helm-gtags--do-open-file (open-func file line)
"Not documented."
(funcall open-func file helm-gtags-read-only)
(goto-char (point-min))
(forward-line (1- line))
(back-to-indentation)
(recenter)
(helm-gtags--push-context helm-gtags--saved-context)
(when helm-gtags-pulse-at-cursor
(pulse-momentary-highlight-one-line (point))))
(defun helm-gtags--find-line-number (cand)
"Not documented."
(if (string-match "\\s-+\\([1-9][0-9]+\\)\\s-+" cand)
(string-to-number (match-string-no-properties 1 cand))
(error "Can't find line number in %s" cand)))
(defun helm-gtags--parse-file-action (cand)
"Not documented."
(let ((line (helm-gtags--find-line-number cand))
(open-func (helm-gtags--select-find-file-func)))
(helm-gtags--do-open-file open-func helm-gtags--parsed-file line)))
(defsubst helm-gtags--has-drive-letter-p (path)
"Not documented."
(string-match-p "\\`[a-zA-Z]:" path))
(defun helm-gtags--extract-file-and-line (cand)
"Not documented."
(if (and (helm-gtags--windows-p) (helm-gtags--has-drive-letter-p cand))
(when (string-match "\\(\\`[a-zA-Z]:[^:]+\\):\\([^:]+\\)" cand)
(cons (match-string-no-properties 1 cand)
(string-to-number (match-string-no-properties 2 cand))))
(let ((elems (split-string cand ":")))
(cons (cl-first elems) (string-to-number (cl-second elems))))))
(defun helm-gtags--action-openfile (cand)
"Not documented."
(let* ((file-and-line (helm-gtags--extract-file-and-line cand))
(filename (car file-and-line))
(line (cdr file-and-line))
(open-func (helm-gtags--select-find-file-func))
(default-directory (helm-gtags--base-directory)))
(helm-gtags--do-open-file open-func filename line)))
(defun helm-gtags--action-openfile-other-window (cand)
"Not documented."
(let ((helm-gtags--use-otherwin t))
(helm-gtags--action-openfile cand)))
(defun helm-gtags--file-content-at-pos (file pos)
"Not documented."
(with-current-buffer (find-file-noselect file)
(save-excursion
(goto-char pos)
(format "%s:%d:%s:%s"
file (line-number-at-pos)
(helm-aif (which-function) (format "[%s]" it) "")
(helm-current-line-contents)))))
(defun helm-gtags--files-candidate-transformer (file)
"Not documented."
(if (eq helm-gtags-path-style 'absolute)
file
(let ((removed-regexp (concat "\\`" helm-gtags--tag-location)))
(replace-regexp-in-string removed-regexp "" file))))
(defun helm-gtags--show-stack-init ()
"Not documented."
(cl-loop with context-stack = (plist-get (helm-gtags--get-context-info) :stack)
with stack-length = (length context-stack)
for context in (reverse context-stack)
for file = (plist-get context :file)
for pos = (plist-get context :position)
for index = (1- stack-length) then (1- index)
for line = (helm-gtags--file-content-at-pos file pos)
for cand = (helm-gtags--files-candidate-transformer line)
collect (cons cand (propertize cand 'index index))))
(defun helm-gtags--persistent-action (cand)
"Not documented."
(let* ((file-and-line (helm-gtags--extract-file-and-line cand))
(filename (car file-and-line))
(line (cdr file-and-line))
(default-directory (helm-gtags--base-directory)))
(when (eq helm-gtags-path-style 'relative)
(setq helm-gtags--last-default-directory default-directory))
(find-file filename)
(goto-char (point-min))
(forward-line (1- line))
(helm-highlight-current-line)))
(defvar helm-gtags--find-file-action
(helm-make-actions
"Open file" #'helm-gtags--action-openfile
"Open file other window" #'helm-gtags--action-openfile-other-window)
"Not documented.")
(defvar helm-source-gtags-tags
(helm-build-in-buffer-source "Jump to definitions"
:init 'helm-gtags--tags-init
:candidate-number-limit helm-gtags-maximum-candidates
:real-to-display 'helm-gtags--candidate-transformer
:persistent-action 'helm-gtags--persistent-action
:fuzzy-match helm-gtags-fuzzy-match
:action helm-gtags--find-file-action)
"Not documented.")
(defvar helm-source-gtags-pattern
(helm-build-in-buffer-source "Find pattern"
:init 'helm-gtags--pattern-init
:candidate-number-limit helm-gtags-maximum-candidates
:real-to-display 'helm-gtags--candidate-transformer
:persistent-action 'helm-gtags--persistent-action
:fuzzy-match helm-gtags-fuzzy-match
:action helm-gtags--find-file-action)
"Not documented.")
(defvar helm-source-gtags-rtags
(helm-build-in-buffer-source "Jump to references"
:init 'helm-gtags--rtags-init
:candidate-number-limit helm-gtags-maximum-candidates
:real-to-display 'helm-gtags--candidate-transformer
:persistent-action 'helm-gtags--persistent-action
:fuzzy-match helm-gtags-fuzzy-match
:action helm-gtags--find-file-action)
"Not documented.")
(defvar helm-source-gtags-gsyms
(helm-build-in-buffer-source "Jump to symbols"
:init 'helm-gtags--gsyms-init
:candidate-number-limit helm-gtags-maximum-candidates
:real-to-display 'helm-gtags--candidate-transformer
:persistent-action 'helm-gtags--persistent-action
:fuzzy-match helm-gtags-fuzzy-match
:action helm-gtags--find-file-action)
"Not documented.")
(defun helm-gtags--highlight-candidate (candidate)
"Not documented."
(let ((regexp (concat "\\_<" helm-gtags--last-input "\\_>"))
(limit (1- (length candidate)))
(last-pos 0)
(case-fold-search nil))
(while (and (< last-pos limit)
(string-match regexp candidate last-pos))
(put-text-property (match-beginning 0) (match-end 0)
'face 'helm-gtags-match
candidate)
(setq last-pos (1+ (match-end 0))))
candidate))
(defun helm-gtags--transformer-regexp (candidate)
"Not documented."
(if (and (helm-gtags--windows-p) (helm-gtags--has-drive-letter-p candidate))
"\\`\\([a-zA-Z]:[^:]+\\):\\([^:]+\\):\\(.*\\)"
"\\`\\([^:]+\\):\\([^:]+\\):\\(.*\\)"))
(defun helm-gtags--candidate-transformer (candidate)
"Not documented."
(if (not helm-gtags-highlight-candidate)
candidate
(let ((regexp (helm-gtags--transformer-regexp candidate)))
(when (string-match regexp candidate)
(format "%s:%s:%s"
(propertize (match-string 1 candidate) 'face 'helm-gtags-file)
(propertize (match-string 2 candidate) 'face 'helm-gtags-lineno)
(helm-gtags--highlight-candidate (match-string 3 candidate)))))))
(defun helm-gtags--parse-file-candidate-transformer (file)
"Not documented."
(let ((removed-file (replace-regexp-in-string "\\`\\S-+ " "" file)))
(when (string-match "\\`\\(\\S-+\\) \\(\\S-+\\) \\(.+\\)\\'" removed-file)
(format "%-25s %-5s %s"
(match-string-no-properties 1 removed-file)
(match-string-no-properties 2 removed-file)
(match-string-no-properties 3 removed-file)))))
(defvar helm-source-gtags-find-tag-from-here
(helm-build-in-buffer-source "Find tag from here"
:init 'helm-gtags--find-tag-from-here-init
:candidate-number-limit helm-gtags-maximum-candidates
:real-to-display 'helm-gtags--candidate-transformer
:persistent-action 'helm-gtags--persistent-action
:fuzzy-match helm-gtags-fuzzy-match
:action helm-gtags--find-file-action)
"Not documented.")
(defvar helm-source-gtags-parse-file
(helm-build-in-buffer-source "Parse file"
:init 'helm-gtags--parse-file-init
:candidate-number-limit helm-gtags-maximum-candidates
:real-to-display 'helm-gtags--parse-file-candidate-transformer
:fuzzy-match helm-gtags-fuzzy-match
:action 'helm-gtags--parse-file-action)
"Not documented.")
(defun helm-gtags--show-stack-action (cand)
"Not documented."
(let* ((index (get-text-property 0 'index cand))
(context-info (helm-gtags--get-context-info))
(context-stack (plist-get context-info :stack)))
(helm-gtags--put-context-stack helm-gtags--tag-location
index context-stack)
(helm-gtags--move-to-context (nth index context-stack))))
(defvar helm-source-gtags-show-stack
(helm-build-sync-source "Show Context Stack"
:candidates 'helm-gtags--show-stack-init
:volatile t
:candidate-number-limit helm-gtags-maximum-candidates
:persistent-action 'helm-gtags--persistent-action
:fuzzy-match helm-gtags-fuzzy-match
:action 'helm-gtags--show-stack-action)
"Not documented.")
;;;###autoload
(defun helm-gtags-select ()
"Not documented."
(interactive)
(helm-gtags--common '(helm-source-gtags-select) nil))
;;;###autoload
(defun helm-gtags-select-path ()
"Not documented."
(interactive)
(helm-gtags--common '(helm-source-gtags-select-path) nil))
(defsubst helm-gtags--beginning-of-defun ()
"Not documented."
(cl-case major-mode
((c-mode c++-mode java-mode) 'c-beginning-of-defun)
(php-mode 'php-beginning-of-defun)
(otherwise #'beginning-of-defun)))
(defsubst helm-gtags--end-of-defun ()
"Not documented."
(cl-case major-mode
((c-mode c++-mode java-mode malabar-mode) 'c-end-of-defun)
(php-mode 'php-end-of-defun)
(otherwise #'end-of-defun)))
(defun helm-gtags--current-funcion-bound ()
"Not documented."
(save-excursion
(let (start)
(funcall (helm-gtags--beginning-of-defun))
(setq start (line-number-at-pos))
(funcall (helm-gtags--end-of-defun))
(cons start (line-number-at-pos)))))
(defun helm-gtags--tags-refered-from-this-function ()
"Not documented."
(let* ((file (helm-gtags--real-file-name))
(bound (helm-gtags--current-funcion-bound))
(start-line (car bound))
(end-line (cdr bound)))
(with-temp-buffer
(unless (process-file "global" nil t nil "-f" "-r" file)
(error "Failed: global -f -r %s" file))
(goto-char (point-min))
(let (tagnames finish)
(while (and (not finish) (not (eobp)))
(let* ((cols (split-string (helm-current-line-contents) nil t))
(lineno (string-to-number (cl-second cols))))
(if (and (> lineno start-line) (< lineno end-line))
(let* ((tag (cl-first cols))
(elm (cl-find tag tagnames :test 'equal)))
(unless elm
(push tag tagnames)))
(when (>= lineno end-line)
(setq finish t)))
(forward-line 1)))
(reverse tagnames)))))
(defun helm-gtags--tag-in-function-persistent-action (cand)
"Not documented."
(let* ((bound (helm-gtags--current-funcion-bound))
(limit (save-excursion
(goto-char (point-min))
(forward-line (cdr bound))
(goto-char (line-end-position))
(point))))
(when (search-forward cand nil limit)
(helm-highlight-current-line))))
;;;###autoload
(defun helm-gtags-tags-in-this-function ()
"Show tagnames which are referenced in this function and jump to it."
(interactive)
(let ((tags (helm-gtags--tags-refered-from-this-function)))
(unless tags
(error "There are no tags which are refered from this function."))
(let* ((name (format "Tags in [%s]" (which-function)))
(tag (helm-comp-read
"Tagnames: " tags
:must-match t :name name
:persistent-action 'helm-gtags--tag-in-function-persistent-action)))
(helm-gtags-find-tag tag))))
(defun helm-gtags--source-select-tag (candidate)
"Not documented."
(helm-build-in-buffer-source "Select Tag"
:init (lambda () (helm-gtags--tags-init candidate))
:candidate-number-limit helm-gtags-maximum-candidates
:persistent-action 'helm-gtags--persistent-action
:fuzzy-match helm-gtags-fuzzy-match
:action helm-gtags--find-file-action))
(defun helm-gtags--source-select-rtag (candidate)
"Not documented."
(helm-build-in-buffer-source "Select Rtag"
:init (lambda () (helm-gtags--rtags-init candidate))
:candidate-number-limit helm-gtags-maximum-candidates
:persistent-action 'helm-gtags--persistent-action
:fuzzy-match helm-gtags-fuzzy-match
:action helm-gtags--find-file-action))
(defsubst helm-gtags--action-by-timer (src)
"Not documented."
(run-with-timer 0.1 nil (lambda () (helm-gtags--common (list src) nil))))
(defun helm-gtags--select-tag-action (c)
"Not documented."
(helm-gtags--action-by-timer (helm-gtags--source-select-tag c)))
(defun helm-gtags--select-rtag-action (c)
"Not documented."
(helm-gtags--action-by-timer (helm-gtags--source-select-rtag c)))
(defun helm-gtags--select-cache-init-common (args tagfile)
"Not documented."
(let ((cache (helm-gtags--get-result-cache tagfile)))