]> code.delx.au - gnu-emacs-elpa/blob - packages/ggtags/ggtags.el
Merge branch 'master' of github.com:leoliu/ggtags
[gnu-emacs-elpa] / packages / ggtags / ggtags.el
1 ;;; ggtags.el --- GNU Global source code tagging system -*- lexical-binding: t; -*-
2
3 ;; Copyright (C) 2013 Free Software Foundation, Inc.
4
5 ;; Author: Leo Liu <sdl.web@gmail.com>
6 ;; Version: 0.7.3
7 ;; Keywords: tools, convenience
8 ;; Created: 2013-01-29
9 ;; URL: https://github.com/leoliu/ggtags
10
11 ;; This program is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation, either version 3 of the License, or
14 ;; (at your option) any later version.
15
16 ;; This program is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
20
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with this program. If not, see <http://www.gnu.org/licenses/>.
23
24 ;;; Commentary:
25
26 ;; A package to integrate GNU Global source code tagging system
27 ;; (http://www.gnu.org/software/global) with Emacs.
28 ;;
29 ;; Usage:
30 ;;
31 ;; Type `M-x ggtags-mode' to enable the minor mode, or as usual enable
32 ;; it in your desired major mode hooks. When the mode is on the symbol
33 ;; at point is underlined if it is a valid (definition) tag.
34 ;;
35 ;; `M-.' finds definition or references according to the context at
36 ;; point, i.e. if point is at a definition tag find references and
37 ;; vice versa. `M-]' finds references.
38 ;;
39 ;; If multiple matches are found, navigation mode is entered, the
40 ;; mode-line lighter changed, and a navigation menu-bar entry
41 ;; presented. In this mode, `M-n' and `M-p' moves to next and previous
42 ;; match, `M-}' and `M-{' to next and previous file respectively.
43 ;; `M-o' toggles between full and abbreviated displays of file names
44 ;; in the auxiliary popup window. When you locate the right match,
45 ;; press RET to finish which hides the auxiliary window and exits
46 ;; navigation mode. You can continue the search using `M-,'. To abort
47 ;; the search press `M-*'.
48 ;;
49 ;; Normally after a few searches a dozen buffers are created visiting
50 ;; files tracked by GNU Global. `C-c M-k' helps clean them up.
51 ;;
52 ;; Check the menu-bar entry `Ggtags' for other useful commands.
53
54 ;;; Code:
55
56 (eval-when-compile
57 (require 'cl)
58 (require 'url-parse))
59
60 (require 'compile)
61
62 (eval-when-compile
63 (unless (fboundp 'setq-local)
64 (defmacro setq-local (var val)
65 (list 'set (list 'make-local-variable (list 'quote var)) val)))
66
67 (unless (fboundp 'defvar-local)
68 (defmacro defvar-local (var val &optional docstring)
69 (declare (debug defvar) (doc-string 3))
70 (list 'progn (list 'defvar var val docstring)
71 (list 'make-variable-buffer-local (list 'quote var))))))
72
73 (eval-and-compile
74 (or (fboundp 'user-error)
75 (defalias 'user-error 'error)))
76
77 (defgroup ggtags nil
78 "GNU Global source code tagging system."
79 :group 'tools)
80
81 (defface ggtags-highlight '((t (:underline t)))
82 "Face used to highlight a valid tag at point."
83 :group 'ggtags)
84
85 (defface ggtags-global-line '((t (:inherit secondary-selection)))
86 "Face used to highlight matched line in Global buffer."
87 :group 'ggtags)
88
89 (defcustom ggtags-oversize-limit (* 50 1024 1024)
90 "The over size limit for the GTAGS file.
91 For large source trees, running 'global -u' can be expensive.
92 Thus when GTAGS file is larger than this limit, ggtags
93 automatically switches to 'global --single-update'.
94
95 Change is effective when a project's information is renewed.
96 See also `ggtags-project-duration'."
97 :safe 'numberp
98 :type '(choice (const :tag "None" nil)
99 (const :tag "Always" t)
100 number)
101 :group 'ggtags)
102
103 (defcustom ggtags-project-duration 3600
104 "Seconds to keep information of a project in memory."
105 :type 'number
106 :group 'ggtags)
107
108 (defcustom ggtags-process-environment nil
109 "Similar to `process-environment' with higher precedence.
110 Elements are run through `substitute-env-vars' before use.
111 GTAGSROOT will always be expanded to current project root
112 directory. This is intended for project-wise ggtags-specific
113 process environment settings."
114 :safe 'ggtags-list-of-string-p
115 :type '(repeat string)
116 :group 'ggtags)
117
118 (defcustom ggtags-auto-jump-to-first-match t
119 "Non-nil to automatically jump to the first match."
120 :type 'boolean
121 :group 'ggtags)
122
123 (defcustom ggtags-global-window-height 8 ; ggtags-global-mode
124 "Number of lines for the 'global' popup window.
125 If nil, use Emacs default."
126 :type '(choice (const :tag "Default" nil) integer)
127 :group 'ggtags)
128
129 (defcustom ggtags-global-abbreviate-filename 35
130 "Non-nil to display file names abbreviated e.g. \"/u/b/env\".
131 If an integer abbreviate only names longer than that number."
132 :type '(choice (const :tag "No" nil)
133 (const :tag "Always" t)
134 integer)
135 :group 'ggtags)
136
137 (defcustom ggtags-split-window-function split-window-preferred-function
138 "A function to control how ggtags pops up the auxiliary window."
139 :type 'function
140 :group 'ggtags)
141
142 (defcustom ggtags-use-idutils (and (executable-find "mkid") t)
143 "Non-nil to also generate the idutils DB."
144 :type 'boolean
145 :group 'ggtags)
146
147 (defcustom ggtags-global-output-format 'grep
148 "The output format for the 'global' command."
149 :type '(choice (const path)
150 (const ctags)
151 (const ctags-x)
152 (const grep)
153 (const cscope))
154 :group 'ggtags)
155
156 (defcustom ggtags-global-ignore-case nil
157 "Non-nil if Global should ignore case."
158 :safe 'booleanp
159 :type 'boolean
160 :group 'ggtags)
161
162 (defcustom ggtags-global-treat-text nil
163 "Non-nil if Global should include matches from text files."
164 :safe 'booleanp
165 :type 'boolean
166 :group 'ggtags)
167
168 (defcustom ggtags-mode-prefix-key "\C-c"
169 "Key binding used for `ggtags-mode-prefix-map'.
170 Users should change the value using `customize-variable' to
171 properly update `ggtags-mode-map'."
172 :set (lambda (sym value)
173 (when (bound-and-true-p ggtags-mode-map)
174 (let ((old (and (boundp sym) (symbol-value sym))))
175 (and old (define-key ggtags-mode-map old nil)))
176 (and value
177 (bound-and-true-p ggtags-mode-prefix-map)
178 (define-key ggtags-mode-map value ggtags-mode-prefix-map)))
179 (set-default sym value))
180 :type 'key-sequence
181 :group 'ggtags)
182
183 (defcustom ggtags-completing-read-function completing-read-function
184 "Ggtags specific `completing-read-function' (which see)."
185 :type 'function
186 :group 'ggtags)
187
188 (defcustom ggtags-highlight-tag-delay 0.25
189 "Time in seconds before highlighting tag at point."
190 :set (lambda (sym value)
191 (when (bound-and-true-p ggtags-highlight-tag-timer)
192 (timer-set-idle-time ggtags-highlight-tag-timer value t))
193 (set-default sym value))
194 :type 'number
195 :group 'ggtags)
196
197 (defcustom ggtags-bounds-of-tag-function (lambda ()
198 (bounds-of-thing-at-point 'symbol))
199 "Function to get the start and end locations of the tag at point."
200 :type 'function
201 :group 'ggtags)
202
203 (defvar ggtags-bug-url "https://github.com/leoliu/ggtags/issues")
204
205 (defvar ggtags-global-last-buffer nil)
206
207 (defvar ggtags-current-tag-name nil)
208
209 (defvar ggtags-highlight-tag-overlay nil)
210
211 (defvar ggtags-highlight-tag-timer nil)
212
213 ;; Used by ggtags-global-mode
214 (defvar ggtags-global-error "match"
215 "Stem of message to print when no matches are found.")
216
217 ;; http://thread.gmane.org/gmane.comp.gnu.global.bugs/1518
218 (defvar ggtags-global-has-path-style ; introduced in global 6.2.8
219 (with-demoted-errors ; in case `global' not found
220 (zerop (process-file "global" nil nil nil
221 "--path-style" "shorter" "--help")))
222 "Non-nil if `global' supports --path-style switch.")
223
224 ;; http://thread.gmane.org/gmane.comp.gnu.global.bugs/1542
225 (defvar ggtags-global-has-color
226 (with-demoted-errors
227 (zerop (process-file "global" nil nil nil "--color" "--help"))))
228
229 (defmacro ggtags-ensure-global-buffer (&rest body)
230 (declare (indent 0))
231 `(progn
232 (or (and (buffer-live-p ggtags-global-last-buffer)
233 (with-current-buffer ggtags-global-last-buffer
234 (derived-mode-p 'ggtags-global-mode)))
235 (error "No global buffer found"))
236 (with-current-buffer ggtags-global-last-buffer ,@body)))
237
238 (defmacro ggtags-with-process-environment (&rest body)
239 (declare (debug t))
240 `(let ((process-environment
241 (append (let ((process-environment process-environment))
242 (when (ggtags-find-project)
243 (setenv "GTAGSROOT" (directory-file-name
244 (ggtags-current-project-root))))
245 (mapcar #'substitute-env-vars ggtags-process-environment))
246 process-environment
247 (and (ggtags-find-project)
248 (not (ggtags-project-has-rtags (ggtags-find-project)))
249 (list "GTAGSLABEL=ctags")))))
250 ,@body))
251
252 (defun ggtags-list-of-string-p (xs)
253 "Return non-nil if XS is a list of strings."
254 (if (null xs)
255 t
256 (and (stringp (car xs))
257 (ggtags-list-of-string-p (cdr xs)))))
258
259 (defun ggtags-get-libpath ()
260 (let ((path (ggtags-with-process-environment (getenv "GTAGSLIBPATH"))))
261 (and path (split-string path (regexp-quote path-separator) t))))
262
263 (defun ggtags-process-string (program &rest args)
264 (with-temp-buffer
265 (let ((exit (apply #'process-file program nil t nil args))
266 (output (progn
267 (goto-char (point-max))
268 (skip-chars-backward " \t\n")
269 (buffer-substring (point-min) (point)))))
270 (or (zerop exit)
271 (error "`%s' non-zero exit: %s" program output))
272 output)))
273
274 (defun ggtags-tag-at-point ()
275 (let ((bounds (funcall ggtags-bounds-of-tag-function)))
276 (and bounds (buffer-substring (car bounds) (cdr bounds)))))
277
278 ;;; Store for project settings
279
280 (defvar ggtags-projects (make-hash-table :size 7 :test #'equal))
281
282 (defstruct (ggtags-project (:constructor ggtags-project--make)
283 (:copier nil)
284 (:type vector)
285 :named)
286 root dirty-p has-rtags oversize-p timestamp)
287
288 (defun ggtags-make-project (root)
289 (check-type root string)
290 (let* ((default-directory (file-name-as-directory root))
291 (rtags-size (nth 7 (file-attributes "GRTAGS")))
292 (has-rtags
293 (when rtags-size
294 (or (> rtags-size (* 32 1024))
295 (with-demoted-errors
296 (not (equal "" (ggtags-process-string "global" "-crs")))))))
297 (oversize-p (pcase ggtags-oversize-limit
298 (`nil nil)
299 (`t t)
300 (t (> (or (nth 7 (file-attributes "GTAGS")) 0)
301 ggtags-oversize-limit)))))
302 (puthash default-directory (ggtags-project--make
303 :root default-directory :has-rtags has-rtags
304 :oversize-p oversize-p :timestamp (float-time))
305 ggtags-projects)))
306
307 (defvar-local ggtags-project 'unset)
308
309 (defun ggtags-project-expired-p (project)
310 (> (- (float-time)
311 (ggtags-project-timestamp project))
312 ggtags-project-duration))
313
314 ;;;###autoload
315 (defun ggtags-find-project ()
316 (if (ggtags-project-p ggtags-project)
317 (if (not (ggtags-project-expired-p ggtags-project))
318 ggtags-project
319 (remhash (ggtags-project-root ggtags-project) ggtags-projects)
320 (kill-local-variable 'ggtags-project)
321 (ggtags-find-project))
322 (let ((root (ignore-errors (file-name-as-directory
323 ;; Resolves symbolic links
324 (ggtags-process-string "global" "-pr")))))
325 (setq ggtags-project
326 (and root (or (gethash root ggtags-projects)
327 (ggtags-make-project root)))))))
328
329 (defun ggtags-current-project-root ()
330 (and (ggtags-find-project)
331 (ggtags-project-root (ggtags-find-project))))
332
333 (defun ggtags-check-project ()
334 (or (ggtags-find-project) (error "File GTAGS not found")))
335
336 (defun ggtags-save-project-settings (&optional confirm)
337 "Save Gnu Global's specific environment variables."
338 (interactive "P")
339 (ggtags-check-project)
340 (let* ((default-directory (ggtags-current-project-root))
341 ;; Not using `ggtags-with-process-environment' to preserve
342 ;; environment variables that may be present in
343 ;; `ggtags-process-environment'.
344 (process-environment
345 (append ggtags-process-environment
346 process-environment
347 (and (not (ggtags-project-has-rtags (ggtags-find-project)))
348 (list "GTAGSLABEL=ctags"))))
349 (envlist (loop for x in '("GTAGSROOT"
350 "GTAGSDBPATH"
351 "GTAGSLIBPATH"
352 "GTAGSCONF"
353 "GTAGSLABEL"
354 "MAKEOBJDIRPREFIX"
355 "GTAGSTHROUGH"
356 "GTAGSBLANKENCODE")
357 when (getenv x)
358 collect (concat x "=" (getenv x)))))
359 (add-dir-local-variable nil 'ggtags-process-environment envlist)
360 (unless confirm (save-buffer) (kill-buffer))))
361
362 (defun ggtags-ensure-project ()
363 (interactive)
364 (or (ggtags-find-project)
365 (when (or (yes-or-no-p "File GTAGS not found; run gtags? ")
366 (user-error "Aborted"))
367 (let ((root (read-directory-name "Directory: " nil nil t))
368 (process-environment process-environment))
369 (and (zerop (length root)) (user-error "No directory chosen"))
370 (setenv "GTAGSROOT"
371 (directory-file-name (file-name-as-directory root)))
372 (ggtags-with-process-environment
373 (and (not (getenv "GTAGSLABEL"))
374 (yes-or-no-p "Use `ctags' backend? ")
375 (setenv "GTAGSLABEL" "ctags"))
376 (with-temp-message "`gtags' in progress..."
377 (let ((default-directory (file-name-as-directory root)))
378 (apply #'ggtags-process-string
379 "gtags" (and ggtags-use-idutils '("--idutils"))))))
380 (message "GTAGS generated in `%s'" root)
381 (ggtags-find-project)))))
382
383 (defun ggtags-update-tags (&optional force)
384 "Update GNU Global tag database."
385 (interactive "P")
386 (when (or force (and (ggtags-find-project)
387 (ggtags-project-dirty-p (ggtags-find-project))))
388 (ggtags-with-process-environment
389 (with-temp-message "Running `global -u'"
390 (ggtags-process-string "global" "-u")
391 (setf (ggtags-project-dirty-p (ggtags-find-project)) nil)))))
392
393 (defvar ggtags-completion-table
394 (let (cache)
395 (completion-table-dynamic
396 (lambda (prefix)
397 (when (and (ggtags-find-project)
398 (not (ggtags-project-oversize-p (ggtags-find-project))))
399 (ggtags-update-tags))
400 (unless (equal prefix (car cache))
401 (setq cache
402 (cons prefix
403 (ggtags-with-process-environment
404 (split-string
405 (apply #'ggtags-process-string
406 "global"
407 ;; Note -c alone returns only definitions
408 (if completion-ignore-case
409 (list "--ignore-case" "-Tc" prefix)
410 (list "-Tc" prefix)))
411 "\n" t)))))
412 (cdr cache)))))
413
414 (defun ggtags-read-tag ()
415 (ggtags-ensure-project)
416 (let ((default (ggtags-tag-at-point))
417 (completing-read-function ggtags-completing-read-function))
418 (setq ggtags-current-tag-name
419 (cond (current-prefix-arg
420 (completing-read
421 (format (if default "Tag (default %s): " "Tag: ") default)
422 ggtags-completion-table nil t nil nil default))
423 ((not default)
424 (user-error "No tag at point"))
425 (t (substring-no-properties default))))))
426
427 (defun ggtags-global-build-command (cmd &rest args)
428 ;; CMD can be definition, reference, symbol, grep, idutils
429 (let ((xs (append (list "global" "-v"
430 (format "--result=%s" ggtags-global-output-format)
431 (and ggtags-global-ignore-case "--ignore-case")
432 (and ggtags-global-has-color "--color")
433 (and ggtags-global-has-path-style
434 "--path-style=shorter")
435 (and ggtags-global-treat-text "--other")
436 (pcase cmd
437 ((pred stringp) cmd)
438 (`definition "-d")
439 (`reference "-r")
440 (`symbol "-s")
441 (`path "--path")
442 (`grep "--grep")
443 (`idutils "--idutils")))
444 args)))
445 (mapconcat 'identity (delq nil xs) " ")))
446
447 ;; takes three values: nil, t and a marker
448 (defvar ggtags-global-start-marker nil)
449
450 (defvar ggtags-global-exit-status 0)
451 (defvar ggtags-global-match-count 0)
452
453 (defvar ggtags-tag-ring-index nil)
454
455 (defun ggtags-global-save-start-marker ()
456 (when (markerp ggtags-global-start-marker)
457 (eval-and-compile (require 'etags))
458 (setq ggtags-tag-ring-index nil)
459 (ring-insert find-tag-marker-ring ggtags-global-start-marker)
460 (setq ggtags-global-start-marker t)))
461
462 (defun ggtags-global-start (command &optional root)
463 (let* ((default-directory (or root (ggtags-current-project-root)))
464 (split-window-preferred-function ggtags-split-window-function))
465 (setq ggtags-global-start-marker (point-marker))
466 (ggtags-navigation-mode +1)
467 (setq ggtags-global-exit-status 0
468 ggtags-global-match-count 0)
469 (ggtags-with-process-environment
470 (setq ggtags-global-last-buffer
471 (compilation-start command 'ggtags-global-mode)))))
472
473 (defun ggtags-find-tag-continue ()
474 (interactive)
475 (ggtags-ensure-global-buffer
476 (ggtags-navigation-mode +1)
477 (let ((split-window-preferred-function ggtags-split-window-function))
478 (ignore-errors (compilation-next-error 1))
479 (compile-goto-error))))
480
481 (defun ggtags-find-tag (cmd name)
482 (ggtags-check-project)
483 (ggtags-global-start (ggtags-global-build-command cmd name)))
484
485 ;;;###autoload
486 (defun ggtags-find-tag-dwim (name &optional definition)
487 "Find definitions or references of tag NAME by context.
488 If point is at a definition tag, find references, and vice versa.
489 With a prefix arg (non-nil DEFINITION) always find definitions."
490 (interactive (list (ggtags-read-tag) current-prefix-arg))
491 (if (or definition
492 (not buffer-file-name)
493 (and (ggtags-find-project)
494 (not (ggtags-project-has-rtags (ggtags-find-project)))))
495 (ggtags-find-tag 'definition name)
496 (ggtags-find-tag (format "--from-here=%d:%s"
497 (line-number-at-pos)
498 (shell-quote-argument
499 (file-relative-name buffer-file-name)))
500 name)))
501
502 (defun ggtags-find-reference (name)
503 (interactive (list (ggtags-read-tag)))
504 (ggtags-find-tag 'reference name))
505
506 (defun ggtags-find-other-symbol (name)
507 "Find tag NAME that is a reference without a definition."
508 (interactive (list (ggtags-read-tag)))
509 (ggtags-find-tag 'symbol name))
510
511 (defun ggtags-read-string (prompt)
512 "Like `read-string' but handle default automatically."
513 (ggtags-ensure-project)
514 (let ((prompt (if (string-match ": *\\'" prompt)
515 (substring prompt 0 (match-beginning 0))
516 prompt))
517 (default (ggtags-tag-at-point)))
518 (read-string (format (if default "%s (default `%s'): "
519 "%s: ")
520 prompt default)
521 nil nil (and default (substring-no-properties default)))))
522
523 (defun ggtags-grep (pattern &optional invert-match)
524 "Use `global --grep' to search for lines matching PATTERN.
525 Invert the match when called with a prefix arg \\[universal-argument]."
526 (interactive (list (ggtags-read-string (if current-prefix-arg
527 "Inverted grep pattern"
528 "Grep pattern"))
529 current-prefix-arg))
530 (ggtags-find-tag 'grep (format "%s--regexp %S"
531 (if invert-match "--invert-match " "")
532 pattern)))
533
534 (defun ggtags-idutils-query (pattern)
535 (interactive (list (ggtags-read-string "ID query pattern")))
536 (ggtags-find-tag 'idutils (format "--regexp %S" pattern)))
537
538 (defun ggtags-find-file (pattern &optional invert-match)
539 (interactive (list (ggtags-read-string (if current-prefix-arg
540 "Inverted path pattern"
541 "Path pattern"))
542 current-prefix-arg))
543 (let ((ggtags-global-output-format 'path))
544 (ggtags-find-tag 'path (format "%s--regexp %S"
545 (if invert-match "--invert-match " "")
546 pattern))))
547
548 ;; NOTE: Coloured output in grep requested: http://goo.gl/Y9IcX
549 (defun ggtags-find-tag-regexp (regexp directory)
550 "List tags matching REGEXP in DIRECTORY (default to project root)."
551 (interactive
552 (list (ggtags-read-string "POSIX regexp")
553 (if current-prefix-arg
554 (read-directory-name "Directory: " nil nil t)
555 (ggtags-current-project-root))))
556 (ggtags-check-project)
557 (let ((root (file-name-as-directory directory))
558 (cmd (ggtags-global-build-command
559 nil nil "-l" "--regexp" (prin1-to-string regexp))))
560 (ggtags-global-start cmd root)))
561
562 (defun ggtags-query-replace (from to &optional delimited)
563 "Query replace FROM with TO on files in the Global buffer.
564 If not in navigation mode, do a grep on FROM first.
565
566 Note: the regular expression FROM must be supported by both
567 Global and Emacs."
568 (interactive (query-replace-read-args "Query replace (regexp)" t t))
569 (unless (bound-and-true-p ggtags-navigation-mode)
570 (let ((ggtags-auto-jump-to-first-match nil))
571 (ggtags-grep from)))
572 (let ((file-form
573 '(let ((files))
574 (ggtags-ensure-global-buffer
575 (with-temp-message "Waiting for Grep to finish..."
576 (while (get-buffer-process (current-buffer))
577 (sit-for 0.2)))
578 (goto-char (point-min))
579 (while (ignore-errors (compilation-next-file 1) t)
580 (let ((m (get-text-property (point) 'compilation-message)))
581 (push (expand-file-name
582 (caar (compilation--loc->file-struct
583 (compilation--message->loc m))))
584 files))))
585 (ggtags-navigation-mode -1)
586 (nreverse files))))
587 (tags-query-replace from to delimited file-form)))
588
589 (defun ggtags-delete-tag-files ()
590 "Delete the tag files generated by gtags."
591 (interactive)
592 (when (ggtags-current-project-root)
593 (let ((files (directory-files (ggtags-current-project-root) t
594 (regexp-opt '("GPATH" "GRTAGS" "GTAGS" "ID"))))
595 (buffer "*GTags File List*"))
596 (or files (user-error "No tag files found"))
597 (with-output-to-temp-buffer buffer
598 (dolist (file files)
599 (princ file)
600 (princ "\n")))
601 (let ((win (get-buffer-window buffer)))
602 (unwind-protect
603 (progn
604 (fit-window-to-buffer win)
605 (when (yes-or-no-p "Remove GNU Global tag files? ")
606 (mapc 'delete-file files)
607 (remhash (ggtags-current-project-root) ggtags-projects)
608 (delete-overlay ggtags-highlight-tag-overlay)
609 (kill-local-variable 'ggtags-project)))
610 (when (window-live-p win)
611 (quit-window t win)))))))
612
613 (defun ggtags-browse-file-as-hypertext (file line)
614 "Browse FILE in hypertext (HTML) form."
615 (interactive (if (or current-prefix-arg (not buffer-file-name))
616 (list (read-file-name "Browse file: " nil nil t)
617 (read-number "Line: " 1))
618 (list buffer-file-name (line-number-at-pos))))
619 (check-type line integer)
620 (or (and file (file-exists-p file)) (error "File `%s' doesn't exist" file))
621 (ggtags-check-project)
622 (or (file-exists-p (expand-file-name "HTML" (ggtags-current-project-root)))
623 (if (yes-or-no-p "No hypertext form exists; run htags? ")
624 (let ((default-directory (ggtags-current-project-root)))
625 (ggtags-with-process-environment (ggtags-process-string "htags")))
626 (user-error "Aborted")))
627 (let ((url (ggtags-process-string
628 "gozilla" "-p" (format "+%d" line) file)))
629 (or (equal (file-name-extension
630 (url-filename (url-generic-parse-url url))) "html")
631 (user-error "No hypertext form for `%s'" file))
632 (when (called-interactively-p 'interactive)
633 (message "Browsing %s" url))
634 (browse-url url)))
635
636 (defun ggtags-next-mark (&optional arg)
637 "Move to the next (newer) mark in the tag marker ring."
638 (interactive)
639 (and (zerop (ring-length find-tag-marker-ring))
640 (user-error "No %s mark" (if arg "previous" "next")))
641 (setq ggtags-tag-ring-index
642 ;; Note `ring-minus1' gets newer item.
643 (funcall (if arg #'ring-plus1 #'ring-minus1)
644 (or ggtags-tag-ring-index
645 (progn
646 (ring-insert find-tag-marker-ring (point-marker))
647 0))
648 (ring-length find-tag-marker-ring)))
649 (let ((m (ring-ref find-tag-marker-ring ggtags-tag-ring-index))
650 (i (- (ring-length find-tag-marker-ring) ggtags-tag-ring-index))
651 (message-log-max nil))
652 (message "%d%s marker%s" i (pcase (mod i 10)
653 (1 "st")
654 (2 "nd")
655 (3 "rd")
656 (_ "th"))
657 (if (marker-buffer m) "" " (dead)"))
658 (if (not (marker-buffer m))
659 (ding)
660 (switch-to-buffer (marker-buffer m))
661 (goto-char m))))
662
663 (defun ggtags-prev-mark ()
664 "Move to the previous (older) mark in the tag marker ring."
665 (interactive)
666 (ggtags-next-mark 'previous))
667
668 (defun ggtags-global-exit-message-function (_process-status exit-status msg)
669 (setq ggtags-global-exit-status exit-status)
670 (let ((count (save-excursion
671 (goto-char (point-max))
672 (if (re-search-backward "^\\([0-9]+\\) \\w+ located" nil t)
673 (string-to-number (match-string 1))
674 0))))
675 (setq ggtags-global-match-count count)
676 ;; Clear the start marker in case of zero matches.
677 (and (zerop count) (setq ggtags-global-start-marker nil))
678 (cons (if (> exit-status 0)
679 msg
680 (format "found %d %s" count (if (= count 1) "match" "matches")))
681 exit-status)))
682
683 ;;; NOTE: Must not match the 'Global started at Mon Jun 3 10:24:13'
684 ;;; line or `compilation-auto-jump' will jump there and fail. See
685 ;;; comments before the 'gnu' entry in
686 ;;; `compilation-error-regexp-alist-alist'.
687 (defvar ggtags-global-error-regexp-alist-alist
688 (append
689 '((path "^\\(?:[^/\n]*/\\)?[^ )\t\n]+$" 0)
690 ;; ACTIVE_ESCAPE src/dialog.cc 172
691 (ctags "^\\([^ \t\n]+\\)[ \t]+\\(.*?\\)[ \t]+\\([0-9]+\\)$"
692 2 3 nil nil 2 (1 font-lock-function-name-face))
693 ;; ACTIVE_ESCAPE 172 src/dialog.cc #undef ACTIVE_ESCAPE
694 (ctags-x "^\\([^ \t\n]+\\)[ \t]+\\([0-9]+\\)[ \t]+\\(\\(?:[^/\n]*/\\)?[^ \t\n]+\\)"
695 3 2 nil nil 3 (1 font-lock-function-name-face))
696 ;; src/dialog.cc:172:#undef ACTIVE_ESCAPE
697 (grep "^\\(.+?\\):\\([0-9]+\\):\\(?:$\\|[^0-9\n]\\|[0-9][^0-9\n]\\|[0-9][0-9].\\)"
698 1 2 nil nil 1)
699 ;; src/dialog.cc ACTIVE_ESCAPE 172 #undef ACTIVE_ESCAPE
700 (cscope "^\\(.+?\\)[ \t]+\\([^ \t\n]+\\)[ \t]+\\([0-9]+\\).*\\(?:[^0-9\n]\\|[^0-9\n][0-9]\\|[^:\n][0-9][0-9]\\)$"
701 1 3 nil nil 1 (2 font-lock-function-name-face)))
702 compilation-error-regexp-alist-alist))
703
704 (defun ggtags-abbreviate-file (start end)
705 (let ((inhibit-read-only t)
706 (amount (if (numberp ggtags-global-abbreviate-filename)
707 (- (- end start) ggtags-global-abbreviate-filename)
708 999))
709 (advance-word (lambda ()
710 "Return the length of the text made invisible."
711 (let ((wend (min end (progn (forward-word 1) (point))))
712 (wbeg (max start (progn (backward-word 1) (point)))))
713 (goto-char wend)
714 (if (<= (- wend wbeg) 1)
715 0
716 (put-text-property (1+ wbeg) wend 'invisible t)
717 (1- (- wend wbeg)))))))
718 (goto-char start)
719 (while (and (> amount 0) (> end (point)))
720 (decf amount (funcall advance-word)))))
721
722 (defun ggtags-abbreviate-files (start end)
723 (goto-char start)
724 (let* ((error-re (cdr (assq ggtags-global-output-format
725 ggtags-global-error-regexp-alist-alist)))
726 (sub (cadr error-re)))
727 (when (and ggtags-global-abbreviate-filename error-re)
728 (while (re-search-forward (car error-re) end t)
729 (when (and (or (not (numberp ggtags-global-abbreviate-filename))
730 (> (length (match-string sub))
731 ggtags-global-abbreviate-filename))
732 ;; Ignore bogus file lines such as:
733 ;; Global found 2 matches at Thu Jan 31 13:45:19
734 (get-text-property (match-beginning sub) 'compilation-message))
735 (ggtags-abbreviate-file (match-beginning sub) (match-end sub)))))))
736
737 (defun ggtags-global-filter ()
738 "Called from `compilation-filter-hook' (which see)."
739 ;; Get rid of line "Using config file '/PATH/TO/.globalrc'."
740 (when (re-search-backward "^ *Using config file '.*\n"
741 compilation-filter-start t)
742 (replace-match ""))
743 (ansi-color-apply-on-region compilation-filter-start (point)))
744
745 (defun ggtags-handle-single-match (buf _how)
746 (when (and ggtags-auto-jump-to-first-match
747 ;; If exit abnormally keep the window for inspection.
748 (zerop ggtags-global-exit-status)
749 (save-excursion
750 (goto-char (point-min))
751 (not (ignore-errors
752 (goto-char (compilation-next-single-property-change
753 (point) 'compilation-message))
754 (end-of-line)
755 (compilation-next-single-property-change
756 (point) 'compilation-message)))))
757 (ggtags-navigation-mode -1)
758 ;; 0.5s delay for `ggtags-auto-jump-to-first-match'
759 (sit-for 0) ; See: http://debbugs.gnu.org/13829
760 (ggtags-navigation-mode-cleanup buf 0.5)))
761
762 (defvar ggtags-global-mode-font-lock-keywords
763 '(("^Global \\(exited abnormally\\|interrupt\\|killed\\|terminated\\)\\(?:.*with code \\([0-9]+\\)\\)?.*"
764 (1 'compilation-error)
765 (2 'compilation-error nil t))
766 ("^Global found \\([0-9]+\\)" (1 compilation-info-face))))
767
768 (define-compilation-mode ggtags-global-mode "Global"
769 "A mode for showing outputs from gnu global."
770 (setq-local compilation-error-regexp-alist
771 (list ggtags-global-output-format))
772 (setq-local compilation-auto-jump-to-first-error
773 ggtags-auto-jump-to-first-match)
774 (setq-local compilation-scroll-output 'first-error)
775 (setq-local compilation-disable-input t)
776 (setq-local compilation-always-kill t)
777 (setq-local compilation-error-face 'compilation-info)
778 (setq-local compilation-exit-message-function
779 'ggtags-global-exit-message-function)
780 (setq-local truncate-lines t)
781 (jit-lock-register #'ggtags-abbreviate-files)
782 (add-hook 'compilation-filter-hook 'ggtags-global-filter nil 'local)
783 (add-hook 'compilation-finish-functions 'ggtags-handle-single-match nil t)
784 (define-key ggtags-global-mode-map "\M-o" 'visible-mode))
785
786 ;; NOTE: Need this to avoid putting menu items in
787 ;; `emulation-mode-map-alists', which creates double entries. See
788 ;; http://i.imgur.com/VJJTzVc.png
789 (defvar ggtags-navigation-map
790 (let ((map (make-sparse-keymap)))
791 (define-key map "\M-n" 'next-error)
792 (define-key map "\M-p" 'previous-error)
793 (define-key map "\M-}" 'ggtags-navigation-next-file)
794 (define-key map "\M-{" 'ggtags-navigation-previous-file)
795 (define-key map "\M->" 'ggtags-navigation-last-error)
796 (define-key map "\M-<" 'ggtags-navigation-first-error)
797 (define-key map "\M-o" 'ggtags-navigation-visible-mode)
798 (define-key map [return] 'ggtags-navigation-mode-done)
799 (define-key map "\r" 'ggtags-navigation-mode-done)
800 ;; Intercept M-. and M-* keys
801 (define-key map [remap pop-tag-mark] 'ggtags-navigation-mode-abort)
802 (define-key map [remap ggtags-find-tag-dwim] 'undefined)
803 map))
804
805 (defvar ggtags-mode-map-alist
806 `((ggtags-navigation-mode . ,ggtags-navigation-map)))
807
808 ;; Higher priority for `ggtags-navigation-mode' to avoid being
809 ;; hijacked by modes such as `view-mode'.
810 (add-to-list 'emulation-mode-map-alists 'ggtags-mode-map-alist)
811
812 (defvar ggtags-navigation-mode-map
813 (let ((map (make-sparse-keymap))
814 (menu (make-sparse-keymap "GG-Navigation")))
815 ;; Menu items: (info "(elisp)Extended Menu Items")
816 (define-key map [menu-bar ggtags-navigation] (cons "GG-Navigation" menu))
817 ;; Ordered backwards
818 (define-key menu [visible-mode]
819 '(menu-item "Visible mode" ggtags-navigation-visible-mode
820 :button (:toggle . (ignore-errors
821 (ggtags-ensure-global-buffer
822 visible-mode)))))
823 (define-key menu [done]
824 '(menu-item "Finish navigation" ggtags-navigation-mode-done))
825 (define-key menu [abort]
826 '(menu-item "Abort" ggtags-navigation-mode-abort))
827 (define-key menu [last-error]
828 '(menu-item "Last error" ggtags-navigation-last-error))
829 (define-key menu [fist-error]
830 '(menu-item "Fist error" ggtags-navigation-first-error))
831 (define-key menu [previous-file]
832 '(menu-item "Previous file" ggtags-navigation-previous-file))
833 (define-key menu [next-file]
834 '(menu-item "Next file" ggtags-navigation-next-file))
835 (define-key menu [previous]
836 '(menu-item "Previous match" previous-error))
837 (define-key menu [next]
838 '(menu-item "Next match" next-error))
839 map))
840
841 (defun ggtags-move-to-tag (&optional name)
842 "Move to NAME tag in current line."
843 (let ((orig (point))
844 (tag (or name ggtags-current-tag-name)))
845 (beginning-of-line)
846 (if (and tag (re-search-forward
847 (concat "\\_<" (regexp-quote tag) "\\_>")
848 (line-end-position)
849 t))
850 (goto-char (match-beginning 0))
851 (goto-char orig))))
852
853 (defun ggtags-navigation-mode-cleanup (&optional buf time)
854 (let ((buf (or buf ggtags-global-last-buffer)))
855 (and (buffer-live-p buf)
856 (with-current-buffer buf
857 (when (get-buffer-process (current-buffer))
858 (kill-compilation))
859 (when (and (derived-mode-p 'ggtags-global-mode)
860 (get-buffer-window))
861 (quit-window nil (get-buffer-window)))
862 (and time (run-with-idle-timer time nil 'kill-buffer buf))))))
863
864 (defun ggtags-navigation-mode-done ()
865 (interactive)
866 (ggtags-navigation-mode -1)
867 (setq tags-loop-scan t
868 tags-loop-operate '(ggtags-find-tag-continue))
869 (ggtags-navigation-mode-cleanup))
870
871 (defun ggtags-navigation-mode-abort ()
872 (interactive)
873 (ggtags-navigation-mode -1)
874 ;; Run after (ggtags-navigation-mode -1) or
875 ;; ggtags-global-start-marker might not have been saved.
876 (when (and (not (markerp ggtags-global-start-marker))
877 ggtags-global-start-marker)
878 (setq ggtags-global-start-marker nil)
879 (pop-tag-mark))
880 (ggtags-navigation-mode-cleanup nil 0))
881
882 (defun ggtags-navigation-next-file (n)
883 (interactive "p")
884 (ggtags-ensure-global-buffer
885 (compilation-next-file n)
886 (compile-goto-error)))
887
888 (defun ggtags-navigation-previous-file (n)
889 (interactive "p")
890 (ggtags-navigation-next-file (- n)))
891
892 (defun ggtags-navigation-first-error ()
893 (interactive)
894 (ggtags-ensure-global-buffer
895 (goto-char (point-min))
896 (compilation-next-error 1)
897 (compile-goto-error)))
898
899 (defun ggtags-navigation-last-error ()
900 (interactive)
901 (ggtags-ensure-global-buffer
902 (goto-char (point-max))
903 (compilation-previous-error 1)
904 (compile-goto-error)))
905
906 (defun ggtags-navigation-visible-mode (&optional arg)
907 (interactive (list (or current-prefix-arg 'toggle)))
908 (ggtags-ensure-global-buffer
909 (visible-mode arg)))
910
911 (defvar ggtags-global-line-overlay nil)
912
913 (defun ggtags-global-next-error-hook ()
914 (ggtags-move-to-tag)
915 (ggtags-global-save-start-marker)
916 (ignore-errors
917 (ggtags-ensure-global-buffer
918 (unless (overlayp ggtags-global-line-overlay)
919 (setq ggtags-global-line-overlay (make-overlay (point) (point)))
920 (overlay-put ggtags-global-line-overlay 'face 'ggtags-global-line))
921 (move-overlay ggtags-global-line-overlay
922 (line-beginning-position) (line-end-position)
923 (current-buffer)))))
924
925 (define-minor-mode ggtags-navigation-mode nil
926 :lighter
927 (" GG[" (:eval (ggtags-ensure-global-buffer
928 (let ((index (when (get-text-property (line-beginning-position)
929 'compilation-message)
930 ;; Assume the first match appears at line 5
931 (- (line-number-at-pos) 4))))
932 `((:propertize ,(if index
933 (number-to-string (max index 0))
934 "?") face success) "/"))))
935 (:propertize (:eval (number-to-string ggtags-global-match-count))
936 face success)
937 (:eval
938 (unless (zerop ggtags-global-exit-status)
939 `(":" (:propertize ,(number-to-string ggtags-global-exit-status)
940 face error))))
941 "]")
942 :global t
943 (if ggtags-navigation-mode
944 (progn
945 (add-hook 'next-error-hook 'ggtags-global-next-error-hook)
946 (add-hook 'minibuffer-setup-hook 'ggtags-minibuffer-setup-function))
947 ;; Call `ggtags-global-save-start-marker' in case of exiting from
948 ;; `ggtags-handle-single-match' for single match.
949 (ggtags-global-save-start-marker)
950 (remove-hook 'next-error-hook 'ggtags-global-next-error-hook)
951 (remove-hook 'minibuffer-setup-hook 'ggtags-minibuffer-setup-function)))
952
953 (defun ggtags-minibuffer-setup-function ()
954 ;; Disable ggtags-navigation-mode in minibuffer.
955 (setq-local ggtags-navigation-mode nil))
956
957 (defun ggtags-kill-file-buffers (&optional interactive)
958 "Kill all buffers visiting files in current project."
959 (interactive "p")
960 (ggtags-check-project)
961 (let ((directories (cons (ggtags-current-project-root) (ggtags-get-libpath)))
962 (count 0)
963 (some (lambda (pred list)
964 (loop for x in list when (funcall pred x) return it))))
965 (dolist (buf (buffer-list))
966 (let ((file (and (buffer-live-p buf)
967 (not (eq buf (current-buffer)))
968 (buffer-file-name buf))))
969 (when (and file (funcall some
970 (lambda (dir)
971 ;; Don't use `file-in-directory-p'
972 ;; to allow symbolic links.
973 (string-prefix-p dir file))
974 directories))
975 (and (kill-buffer buf) (incf count)))))
976 (and interactive
977 (message "%d %s killed" count (if (= count 1) "buffer" "buffers")))))
978
979 (defun ggtags-after-save-function ()
980 (when (ggtags-find-project)
981 (setf (ggtags-project-dirty-p (ggtags-find-project)) t)
982 ;; When oversize update on a per-save basis.
983 (when (and buffer-file-name
984 (ggtags-project-oversize-p (ggtags-find-project)))
985 (ggtags-with-process-environment
986 (process-file "global" nil 0 nil "--single-update"
987 (file-relative-name buffer-file-name))))))
988
989 (defvar ggtags-mode-prefix-map
990 (let ((m (make-sparse-keymap)))
991 (define-key m (kbd "M-DEL") 'ggtags-delete-tag-files)
992 (define-key m "\M-p" 'ggtags-prev-mark)
993 (define-key m "\M-n" 'ggtags-next-mark)
994 (define-key m "\M-f" 'ggtags-find-file)
995 (define-key m "\M-o" 'ggtags-find-other-symbol)
996 (define-key m "\M-g" 'ggtags-grep)
997 (define-key m "\M-i" 'ggtags-idutils-query)
998 (define-key m "\M-b" 'ggtags-browse-file-as-hypertext)
999 (define-key m "\M-k" 'ggtags-kill-file-buffers)
1000 (define-key m (kbd "M-%") 'ggtags-query-replace)
1001 m))
1002
1003 (defvar ggtags-mode-map
1004 (let ((map (make-sparse-keymap))
1005 (menu (make-sparse-keymap "Ggtags")))
1006 (define-key map "\M-." 'ggtags-find-tag-dwim)
1007 (define-key map (kbd "M-]") 'ggtags-find-reference)
1008 (define-key map (kbd "C-M-.") 'ggtags-find-tag-regexp)
1009 (define-key map ggtags-mode-prefix-key ggtags-mode-prefix-map)
1010 ;; Menu items
1011 (define-key map [menu-bar ggtags] (cons "Ggtags" menu))
1012 ;; Ordered backwards
1013 (define-key menu [report-bugs]
1014 `(menu-item "Report bugs"
1015 (lambda () (interactive)
1016 (browse-url ggtags-bug-url)
1017 (message "Please visit %s" ggtags-bug-url))
1018 :help ,(format "Visit %s" ggtags-bug-url)))
1019 (define-key menu [custom-ggtags]
1020 '(menu-item "Customize Ggtags"
1021 (lambda () (interactive) (customize-group 'ggtags))))
1022 (define-key menu [save-project]
1023 '(menu-item "Save project settings" ggtags-save-project-settings))
1024 (define-key menu [sep2] menu-bar-separator)
1025 (define-key menu [browse-hypertext]
1026 '(menu-item "Browse as hypertext" ggtags-browse-file-as-hypertext
1027 :enable (ggtags-find-project)))
1028 (define-key menu [delete-tags]
1029 '(menu-item "Delete tag files" ggtags-delete-tag-files
1030 :enable (ggtags-find-project)))
1031 (define-key menu [kill-buffers]
1032 '(menu-item "Kill project file buffers" ggtags-kill-file-buffers
1033 :enable (ggtags-find-project)))
1034 (define-key menu [pop-mark]
1035 '(menu-item "Pop mark" pop-tag-mark
1036 :help "Pop to previous mark and destroy it"))
1037 (define-key menu [next-mark]
1038 '(menu-item "Next mark" ggtags-next-mark))
1039 (define-key menu [prev-mark]
1040 '(menu-item "Previous mark" ggtags-prev-mark))
1041 (define-key menu [sep1] menu-bar-separator)
1042 (define-key menu [find-file]
1043 '(menu-item "Find files" ggtags-find-file))
1044 (define-key menu [query-replace]
1045 '(menu-item "Query replace" ggtags-query-replace))
1046 (define-key menu [idutils]
1047 '(menu-item "Query idutils DB" ggtags-idutils-query))
1048 (define-key menu [grep]
1049 '(menu-item "Use grep" ggtags-grep))
1050 (define-key menu [find-symbol]
1051 '(menu-item "Find other symbol" ggtags-find-other-symbol))
1052 (define-key menu [find-tag-regexp]
1053 '(menu-item "Find tag matching regexp" ggtags-find-tag-regexp))
1054 (define-key menu [find-reference]
1055 '(menu-item "Find reference" ggtags-find-reference))
1056 (define-key menu [find-tag-continue]
1057 '(menu-item "Continue find tag" tags-loop-continue))
1058 (define-key menu [find-tag]
1059 '(menu-item "Find tag" ggtags-find-tag-dwim))
1060 (define-key menu [update-tags]
1061 '(menu-item "Update tag files" ggtags-update-tags
1062 :visible (ggtags-find-project)))
1063 (define-key menu [run-gtags]
1064 '(menu-item "Run gtags" ggtags-ensure-project
1065 :visible (not (ggtags-find-project))))
1066 map))
1067
1068 ;;;###autoload
1069 (define-minor-mode ggtags-mode nil
1070 :lighter (:eval (if ggtags-navigation-mode "" " GG"))
1071 (unless (timerp ggtags-highlight-tag-timer)
1072 (setq ggtags-highlight-tag-timer
1073 (run-with-idle-timer
1074 ggtags-highlight-tag-delay t 'ggtags-highlight-tag-at-point)))
1075 (if ggtags-mode
1076 (progn
1077 (add-hook 'after-save-hook 'ggtags-after-save-function nil t)
1078 (or (executable-find "global")
1079 (message "Failed to find GNU Global")))
1080 (remove-hook 'after-save-hook 'ggtags-after-save-function t)
1081 (and (overlayp ggtags-highlight-tag-overlay)
1082 (delete-overlay ggtags-highlight-tag-overlay))
1083 (setq ggtags-highlight-tag-overlay nil)))
1084
1085 (defvar ggtags-highlight-tag-map
1086 (let ((map (make-sparse-keymap)))
1087 (define-key map [S-down-mouse-1] 'ggtags-find-tag-dwim)
1088 (define-key map [S-down-mouse-3] 'ggtags-find-reference)
1089 map)
1090 "Keymap used for valid tag at point.")
1091
1092 (put 'ggtags-active-tag 'face 'ggtags-highlight)
1093 (put 'ggtags-active-tag 'keymap ggtags-highlight-tag-map)
1094 ;; (put 'ggtags-active-tag 'mouse-face 'match)
1095 (put 'ggtags-active-tag 'modification-hooks
1096 (list (lambda (o after &rest _args)
1097 (and (not after) (delete-overlay o)))))
1098 (put 'ggtags-active-tag 'help-echo
1099 "S-down-mouse-1 for definitions\nS-down-mouse-3 for references")
1100
1101 (defun ggtags-highlight-tag-at-point ()
1102 (when (and ggtags-mode (eq ggtags-project 'unset))
1103 (ggtags-find-project))
1104 (when (and ggtags-mode ggtags-project)
1105 (unless (overlayp ggtags-highlight-tag-overlay)
1106 (let ((o (make-overlay (point) (point) nil t)))
1107 (setq ggtags-highlight-tag-overlay o)))
1108 (let ((bounds (funcall ggtags-bounds-of-tag-function))
1109 (o ggtags-highlight-tag-overlay))
1110 (cond
1111 ((and bounds
1112 (overlay-get o 'category)
1113 (eq (overlay-buffer o) (current-buffer))
1114 (= (overlay-start o) (car bounds))
1115 (= (overlay-end o) (cdr bounds)))
1116 ;; Tag is already highlighted so do nothing.
1117 nil)
1118 ((and bounds (let ((completion-ignore-case nil))
1119 (test-completion
1120 (buffer-substring (car bounds) (cdr bounds))
1121 ggtags-completion-table)))
1122 (move-overlay o (car bounds) (cdr bounds) (current-buffer))
1123 (overlay-put o 'category 'ggtags-active-tag))
1124 (t (move-overlay o
1125 (or (car bounds) (point))
1126 (or (cdr bounds) (point))
1127 (current-buffer))
1128 (overlay-put o 'category nil))))))
1129
1130 ;;; imenu
1131
1132 (defun ggtags-goto-imenu-index (name line &rest _args)
1133 (save-restriction
1134 (widen)
1135 (goto-char (point-min))
1136 (forward-line (1- line))
1137 (ggtags-move-to-tag name)))
1138
1139 ;;;###autoload
1140 (defun ggtags-build-imenu-index ()
1141 "A function suitable for `imenu-create-index-function'."
1142 (when buffer-file-name
1143 (let ((file (file-relative-name buffer-file-name)))
1144 (with-temp-buffer
1145 (when (with-demoted-errors
1146 (zerop (ggtags-with-process-environment
1147 (process-file "global" nil t nil "-x" "-f" file))))
1148 (goto-char (point-min))
1149 (loop while (re-search-forward
1150 "^\\([^ \t]+\\)[ \t]+\\([0-9]+\\)" nil t)
1151 collect (list (match-string 1)
1152 (string-to-number (match-string 2))
1153 'ggtags-goto-imenu-index)))))))
1154
1155 ;;; hippie-expand
1156
1157 ;;;###autoload
1158 (defun try-complete-ggtags-tag (old)
1159 "A function suitable for `hippie-expand-try-functions-list'."
1160 (with-no-warnings ; to avoid loading hippie-exp
1161 (unless old
1162 (he-init-string (if (looking-back "\\_<.*" (line-beginning-position))
1163 (match-beginning 0)
1164 (point))
1165 (point))
1166 (setq he-expand-list
1167 (and (not (equal he-search-string ""))
1168 (ggtags-find-project)
1169 (sort (all-completions he-search-string
1170 ggtags-completion-table)
1171 'string-lessp))))
1172 (if (null he-expand-list)
1173 (progn
1174 (if old (he-reset-string))
1175 nil)
1176 (he-substitute-string (car he-expand-list))
1177 (setq he-expand-list (cdr he-expand-list))
1178 t)))
1179
1180 (defun ggtags-reload (&optional force)
1181 (interactive "P")
1182 (unload-feature 'ggtags force)
1183 (require 'ggtags))
1184
1185 (defun ggtags-unload-function ()
1186 (setq emulation-mode-map-alists
1187 (delq 'ggtags-mode-map-alist emulation-mode-map-alists))
1188 nil)
1189
1190 (provide 'ggtags)
1191 ;;; ggtags.el ends here