1 ;; etags.el --- etags facility for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1989, 1992 Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
8 ;; This file is part of GNU Emacs.
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
27 (defvar tags-file-name nil
"\
28 *File name of tags table.
29 To switch to a new tags table, setting this variable is sufficient.
30 Use the `etags' program to make a tags table file.")
31 ;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ")
34 (defvar tags-table-list nil
35 "*List of names of tags table files which are currently being searched.
36 An element of nil means to look for a file \"TAGS\" in the current directory.
37 Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
39 (defvar tags-table-list-pointer nil
40 "Pointer into `tags-table-list', or into a list of included tags tables,
41 where the current state of searching is. Use `visit-tags-table-buffer' to
42 cycle through tags tables in this list.")
44 (defvar tags-table-parent-pointer-list nil
45 "List of values to restore into `tags-table-list-pointer' when it hits nil.")
47 (defvar tags-table-set-list nil
48 "List of sets of tags table which have been used together in the past.
49 Each element is a list of strings which are file names.")
52 (defvar find-tag-hook nil
53 "*Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'.
54 The value in the buffer in which \\[find-tag] is done is used,
55 not the value in the buffer \\[find-tag] goes to.")
58 (defvar find-tag-default-function nil
59 "*If non-nil, a function of no arguments used by \\[find-tag] to pick a
60 default tag. If nil, and the symbol that is the value of `major-mode'
61 has a `find-tag-default-function' property (see `put'), that is used.
62 Otherwise, `find-tag-default' is used.")
65 (defvar default-tags-table-function nil
66 "*If non-nil, a function of no arguments to choose a default tags file
67 for a particular buffer.")
70 ;; These variables are local in tags table buffers.
72 (defvar tag-lines-already-matched nil
73 "List of positions of beginnings of lines within the tags table
74 that are already matched.")
76 (defvar tags-table-files nil
77 "List of file names covered by current tags table.
78 nil means it has not yet been computed; use `tags-table-files' to do so.")
80 (defvar tags-completion-table nil
81 "Alist of tag names defined in current tags table.")
83 (defvar tags-included-tables nil
84 "List of tags tables included by the current tags table.")
86 (defvar next-file-list nil
87 "List of files for \\[next-file] to process.")
89 ;; Hooks for file formats.
91 (defvar tags-table-format-hooks
'(etags-recognize-tags-table
92 recognize-empty-tags-table
)
93 "List of functions to be called in a tags table buffer to identify
94 the type of tags table. The functions are called in order, with no arguments,
95 until one returns non-nil. The function should make buffer-local bindings
96 of the format-parsing tags function variables if successful.")
98 (defvar file-of-tag-function nil
99 "Function to do the work of `file-of-tag' (which see).")
100 (defvar tags-table-files-function nil
101 "Function to do the work of `tags-table-files' (which see).")
102 (defvar tags-completion-table-function nil
103 "Function to build the tags-completion-table.")
104 (defvar snarf-tag-function nil
105 "Function to get info about a matched tag for `goto-tag-location-function'.")
106 (defvar goto-tag-location-function nil
107 "Function of to go to the location in the buffer specified by a tag.
108 One argument, the tag info returned by `snarf-tag-function'.")
109 (defvar find-tag-regexp-search-function nil
110 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
111 (defvar find-tag-regexp-tag-order nil
112 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
113 (defvar find-tag-regexp-next-line-after-failure-p nil
114 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
115 (defvar find-tag-search-function nil
116 "Search function passed to `find-tag-in-order' for finding a tag.")
117 (defvar find-tag-tag-order nil
118 "Tag order passed to `find-tag-in-order' for finding a tag.")
119 (defvar find-tag-next-line-after-failure-p nil
120 "Flag passed to `find-tag-in-order' for finding a tag.")
121 (defvar list-tags-function nil
122 "Function to do the work of `list-tags' (which see).")
123 (defvar tags-apropos-function nil
124 "Function to do the work of `tags-apropos' (which see).")
125 (defvar tags-included-tables-function nil
126 "Function to do the work of `tags-included-tables' (which see).")
127 (defvar verify-tags-table-function nil
128 "Function to return t iff the current buffer vontains a valid
129 \(already initialized\) tags file.")
131 (defun initialize-new-tags-table ()
132 "Initialize the tags table in the current buffer.
133 Returns non-nil iff it is a valid tags table."
134 (make-local-variable 'tag-lines-already-matched
)
135 (make-local-variable 'tags-table-files
)
136 (make-local-variable 'tags-completion-table
)
137 (make-local-variable 'tags-included-tables
)
138 (setq tags-table-files nil
139 tag-lines-already-matched nil
140 tags-completion-table nil
141 tags-included-tables nil
)
142 ;; Value is t if we have found a valid tags table buffer.
143 (let ((hooks tags-table-format-hooks
))
145 (not (funcall (car hooks
))))
146 (setq hooks
(cdr hooks
)))
150 (defun visit-tags-table (file &optional local
)
151 "Tell tags commands to use tags table file FILE.
152 FILE should be the name of a file created with the `etags' program.
153 A directory name is ok too; it means file TAGS in that directory.
155 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
156 With a prefix arg, set the buffer-local value instead.
157 When you find a tag with \\[find-tag], the buffer it finds the tag
158 in is given a local value of this variable which is the name of the tags
159 file the tag was in."
160 (interactive (list (read-file-name "Visit tags table: (default TAGS) "
162 (expand-file-name "TAGS"
166 (let ((tags-file-name file
))
168 (or (visit-tags-file t
)
169 (signal 'file-error
(list "Visiting tags table"
170 "file does not exist"
172 (setq file tags-file-name
)))
174 (setq tags-file-name file
)
175 (kill-local-variable 'tags-file-name
)
176 (setq-default tags-file-name file
)))
178 ;; Move tags-table-list-pointer along and set tags-file-name.
179 ;; Returns nil when out of tables.
180 (defun tags-next-table ()
181 (if (tags-included-tables)
183 ;; Move into the included tags tables.
184 (if tags-table-list-pointer
185 (setq tags-table-parent-pointer-list
186 (cons tags-table-list-pointer
187 tags-table-parent-pointer-list
)))
188 (setq tags-table-list-pointer tags-included-tables
))
190 (if tags-table-list-pointer
191 ;; Go to the next table in the list.
192 (setq tags-table-list-pointer
193 (cdr tags-table-list-pointer
))
195 ;; Pop back to the tags table which includes this one.
196 (setq tags-table-list-pointer
197 (car tags-table-parent-pointer-list
)
198 tags-table-parent-pointer-list
199 (cdr tags-table-parent-pointer-list
))))
201 (and tags-table-list-pointer
203 (or (car tags-table-list-pointer
)
204 ;; nil means look for TAGS in current directory.
205 (expand-file-name "TAGS" default-directory
)))))
207 (defun visit-tags-table-buffer (&optional cont
)
208 "Select the buffer containing the current tags table.
209 If optional arg is t, visit the next table in `tags-table-list'.
210 If optional arg is the atom `reset', reset to the head of `tags-table-list'.
211 If optional arg is the atom `same', don't look for a new table;
212 just select the buffer.
213 If arg is nil or absent, choose a buffer from information in
214 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
215 Returns t if it visits a tags table, or nil if there are no more in the list."
216 (cond ((eq cont
'same
)
217 (let ((tags-file-name (car tags-table-list-pointer
)))
219 (visit-tags-file nil
)))
223 (setq tags-table-list-pointer tags-table-list
)
226 (while (and (not (visit-tags-file nil
)) ;Skip over nonexistent files.
228 (not (null tags-table-list-pointer
)))
232 (or (cdr (assq 'tags-file-name
(buffer-local-variables)))
233 (and default-tags-table-function
234 (funcall default-tags-table-function
))
235 ;; Look for a tags table that contains
236 ;; tags for the current buffer's file.
237 (let ((tables tags-table-list
)
238 (this-file (buffer-file-name))
242 (let ((tags-file-name (car tables
)))
243 (if (and (file-exists-p tags-file-name
)
245 (visit-tags-file nil
)
247 (setq tags-table-files
249 tags-table-files-function
)))
250 (assoc this-file tags-file-files
)))
251 (setq found
(car tables
)
253 (setq tables
(cdr tables
))))))
255 (car tags-table-list-pointer
)
258 (read-file-name "Visit tags table: (default TAGS) "
260 (expand-file-name "TAGS"
263 (visit-tags-file t
)))))
265 ;; Visit tags-file-name and check that it's a valid tags table. Returns
266 ;; nil and does nothing if tags-file-name does not exist. Otherwise, on
267 ;; return tags-table-list and tags-table-list-pointer point to
269 (defun visit-tags-file (put-in-list)
270 (let ((file tags-file-name
))
271 (if (file-directory-p file
)
272 (setq file
(expand-file-name "TAGS" file
)))
273 (if (or (get-file-buffer file
)
274 (file-exists-p file
))
275 (if (if (get-file-buffer file
)
277 (set-buffer (get-file-buffer file
))
278 (setq win
(or verify-tags-table-function
279 (initialize-new-tags-table)))
280 (if (or (verify-visited-file-modtime (current-buffer))
282 "Tags file has changed, read new contents? ")))
283 (and win
(funcall verify-tags-table-function
))
285 (initialize-new-tags-table)))
286 (set-buffer (find-file-noselect file
))
287 (or (string= file buffer-file-name
)
288 ;; find-file-noselect has changed the file name.
289 ;; Propagate change to tags-file-name and tags-table-list.
290 (let ((tail (assoc file tags-table-list
)))
292 (setcar tail buffer-file-name
))
293 (setq tags-file-name buffer-file-name
)))
294 (initialize-new-tags-table))
297 (not (equal file
(car tags-table-list-pointer
))))
299 ;; Bury the tags table buffer so it
300 ;; doesn't get in the user's way.
301 (bury-buffer (current-buffer))
302 ;; Look for this file in the current list of tags files.
303 (if (setq elt
(member file tags-table-list
))
304 (if (eq elt tags-table-list
)
305 ;; Already at the head of the list.
307 ;; Rotate this element to the head of the search list.
308 (setq tags-table-list-pointer
309 (nconc elt tags-table-list
))
310 (while (not (eq (cdr tags-table-list
) elt
))
311 (setq tags-table-list
(cdr tags-table-list
)))
312 (setcdr tags-table-list nil
)
313 (setq tags-table-list tags-table-list-pointer
))
314 ;; The table is not in the current set.
315 ;; Try to find it in another previously used set.
316 (let ((sets tags-table-set-list
))
318 (not (setq elt
(member file
320 (setq sets
(cdr sets
)))
323 ;; Found in some other set. Switch to that
324 ;; set, making the selected tags table the head
325 ;; of the search list.
326 (or (memq tags-table-list tags-table-set-list
)
327 ;; Save the current list.
328 (setq tags-table-set-list
329 (cons tags-table-list
330 tags-table-set-list
)))
331 (setq tags-table-list
(car sets
))
332 (if (eq elt tags-table-list
)
333 ;; Already at the head of the list
335 ;; Rotate this element to the head of the list.
336 (setq tags-table-list-pointer
337 (nconc elt tags-table-list
))
338 (while (not (eq (cdr tags-table-list
) elt
))
339 (setq tags-table-list
(cdr tags-table-list
)))
340 (setcdr tags-table-list nil
)
341 (setq tags-table-list tags-table-list-pointer
)
342 (setcar sets tags-table-list
)))
343 ;; Not found in any current set.
344 (if (and tags-table-list
346 (concat "Add " file
" to current list"
347 " of tags tables? ")))
348 ;; Add it to the current list.
349 (setq tags-table-list
350 (cons file tags-table-list
))
351 ;; Make a fresh list, and store the old one.
352 (or (memq tags-table-list tags-table-set-list
)
353 (setq tags-table-set-list
354 (cons tags-table-list
355 tags-table-set-list
)))
356 (setq tags-table-list
(cons file nil
)))
357 (setq tags-table-list-pointer tags-table-list
))))
361 ;; The buffer was not valid. Don't use it again.
362 (kill-local-variable 'tags-file-name
)
363 (setq tags-file-name nil
)
364 (error "File %s is not a valid tags table" buffer-file-name
)))))
366 (defun file-of-tag ()
367 "Return the file name of the file whose tags point is within.
368 Assumes the tags table is the current buffer.
369 File name returned is relative to tags table file's directory."
370 (funcall file-of-tag-function
))
373 (defun tags-table-files ()
374 "Return a list of files in the current tags table.
375 File names returned are absolute."
377 (visit-tags-table-buffer)
379 (setq tags-table-files
380 (funcall tags-table-files-function
)))))
382 (defun tags-included-tables ()
383 "Return a list of tags tables included by the current table."
384 (or tags-included-tables
385 (setq tags-included-tables
(funcall tags-included-tables-function
))))
387 ;; Build tags-completion-table on demand. The single current tags table
388 ;; and its included tags tables (and their included tables, etc.) have
389 ;; their tags included in the completion table.
390 (defun tags-completion-table ()
391 (or tags-completion-table
394 (message "Making tags completion table for %s..." buffer-file-name
)
395 (let ((included (tags-included-tables))
396 (table (funcall tags-completion-table-function
)))
399 (let ((tags-file-name (car included
)))
400 (visit-tags-file nil
))
401 (if (tags-completion-table)
404 (intern (symbol-name sym
) table
)))
405 tags-completion-table
))
406 (setq included
(cdr included
))))
407 (setq tags-completion-table table
))
408 (message "Making tags completion table for %s...done"
410 (quit (message "Tags completion table construction aborted.")
411 (setq tags-completion-table nil
)))))
413 ;; Completion function for tags. Does normal try-completion,
414 ;; but builds tags-completion-table on demand.
415 (defun tags-complete-tag (string predicate what
)
417 (visit-tags-table-buffer)
419 (all-completions string
(tags-completion-table) predicate
)
420 (try-completion string
(tags-completion-table) predicate
))))
422 ;; Return a default tag to search for, based on the text at point.
423 (defun find-tag-default ()
425 (while (looking-at "\\sw\\|\\s_")
427 (if (or (re-search-backward "\\sw\\|\\s_"
428 (save-excursion (beginning-of-line) (point))
430 (re-search-forward "\\(\\sw\\|\\s_\\)+"
431 (save-excursion (end-of-line) (point))
433 (progn (goto-char (match-end 0))
434 (buffer-substring (point)
435 (progn (forward-sexp -
1)
436 (while (looking-at "\\s'")
441 ;; Read a tag name from the minibuffer with defaulting and completion.
442 (defun find-tag-tag (string)
443 (let* ((default (funcall (or find-tag-default-function
444 (get major-mode
'find-tag-default-function
)
446 (spec (completing-read (if default
447 (format "%s(default %s) " string default
)
449 'tags-complete-tag
)))
450 (list (if (equal spec
"")
451 (or default
(error "There is no default tag"))
455 "Last tag found by \\[find-tag].")
458 (defun find-tag-noselect (tagname &optional next-p regexp-p
)
459 "Find tag (in current tags table) whose name contains TAGNAME.
460 Returns the buffer containing the tag's definition moves its point there,
461 but does not select the buffer.
462 The default for TAGNAME is the expression in the buffer near point.
464 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
465 for another tag that matches the last tagname or regexp used. When there
466 are multiple matches for a tag, more exact matches are found first.
468 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
470 See documentation of variable `tags-file-name'."
471 (interactive (if current-prefix-arg
473 (find-tag-tag "Find tag: ")))
474 (let ((local-find-tag-hook find-tag-hook
))
476 (visit-tags-table-buffer 'same
)
477 (setq last-tag tagname
)
478 (visit-tags-table-buffer 'reset
))
480 (find-tag-in-order (if next-p last-tag tagname
)
482 find-tag-regexp-search-function
483 find-tag-search-function
)
485 find-tag-regexp-tag-order
488 find-tag-regexp-next-line-after-failure-p
489 find-tag-next-line-after-failure-p
)
490 (if regexp-p
"matching" "containing")
492 (run-hooks 'local-find-tag-hook
))))
495 (defun find-tag (tagname &optional next-p
)
496 "Find tag (in current tags table) whose name contains TAGNAME.
497 Select the buffer containing the tag's definition, and move point there.
498 The default for TAGNAME is the expression in the buffer around or before point.
500 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
501 for another tag that matches the last tagname used. When there are
502 multiple matches, more exact matches are found first.
504 See documentation of variable `tags-file-name'."
505 (interactive (if current-prefix-arg
507 (find-tag-tag "Find tag: ")))
508 (switch-to-buffer (find-tag-noselect tagname next-p
)))
509 ;;;###autoload (define-key esc-map "." 'find-tag)
512 (defun find-tag-other-window (tagname &optional next-p
)
513 "Find tag (in current tags table) whose name contains TAGNAME.
514 Select the buffer containing the tag's definition
515 in another window, and move point there.
516 The default for TAGNAME is the expression in the buffer around or before point.
518 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
519 for another tag that matches the last tagname used. When there are
520 multiple matches, more exact matches are found first.
522 See documentation of variable `tags-file-name'."
523 (interactive (if current-prefix-arg
525 (find-tag-tag "Find tag other window: ")))
526 (switch-to-buffer-other-window (find-tag-noselect tagname next-p
)))
527 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
530 (defun find-tag-other-frame (tagname &optional next-p
)
531 "Find tag (in current tag table) whose name contains TAGNAME.
532 Selects the buffer that the tag is contained in in another frame
533 and puts point at its definition.
534 If TAGNAME is a null string, the expression in the buffer
535 around or before point is used as the tag name.
536 If second arg NEXT-P is non-nil (interactively, with prefix arg),
537 searches for the next tag in the tag table
538 that matches the tagname used in the previous find-tag.
540 See documentation of variable `tags-file-name'."
541 (interactive (if current-prefix-arg
543 (find-tag-tag "Find tag other window: ")))
544 (let ((pop-up-frames t
))
545 (find-tag-other-window tagname next-p
)))
546 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
549 (defun find-tag-regexp (regexp &optional next-p other-window
)
550 "Find tag (in current tags table) whose name matches REGEXP.
551 Select the buffer containing the tag's definition and move point there.
553 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
554 for another tag that matches the last tagname used.
556 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
558 See documentation of variable `tags-file-name'."
559 (interactive (if current-prefix-arg
561 (read-string "Find tag regexp: ")))
562 (funcall (if other-window
'switch-to-buffer-other-window
'switch-to-buffer
)
563 (find-tag-noselect regexp next-p t
)))
565 ;; Internal tag finding function.
567 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
568 ;; any member of the function list ORDER (third arg). If ORDER is nil,
569 ;; use saved state to continue a previous search.
571 ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
574 ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
575 ;; point should be moved to the next line.
577 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
578 ;; beginning of tags file, and perform inner loop: for each naive match for
579 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
580 ;; qualifier-func. If it qualifies, go to the specified line in the
581 ;; specified source file and return. Qualified matches are remembered to
582 ;; avoid repetition. State is saved so that the loop can be continued.
584 (defun find-tag-in-order (pattern search-forward-func order
585 next-line-after-failure-p matching
587 (let (file ;name of file containing tag
588 tag-info
;where to find the tag in FILE
589 tags-table-file
;name of tags file
596 (visit-tags-table-buffer))
597 ;; Get a qualified match.
598 (catch 'qualified-match-found
599 (while (or first-table
600 (visit-tags-table-buffer t
))
603 (setq tag-lines-already-matched nil
))
605 (and first-search first-table
606 ;; Start at beginning of tags file.
607 (goto-char (point-min)))
608 (setq first-table nil
)
610 (setq tags-table-file buffer-file-name
)
612 (while (funcall search-forward-func pattern nil t
)
613 ;; Naive match found. Qualify the match.
614 (and (funcall (car order
) pattern
)
615 ;; Make sure it is not a previous qualified match.
616 ;; Use of `memq' depends on numbers being eq.
617 (not (memq (save-excursion (beginning-of-line) (point))
618 tag-lines-already-matched
))
619 (throw 'qualified-match-found nil
))
620 (if next-line-after-failure-p
622 ;; Try the next flavor of match.
623 (setq order
(cdr order
))
624 (goto-char (point-min)))
625 (setq order tag-order
))
626 ;; We throw out on match, so only get here if there were no matches.
627 (error "No %stags %s %s" (if first-search
"" "more ")
630 ;; Found a tag; extract location info.
632 (setq tag-lines-already-matched
(cons (point)
633 tag-lines-already-matched
))
634 ;; Expand the filename, using the tags table buffer's default-directory.
635 (setq file
(expand-file-name (file-of-tag))
636 tag-info
(funcall snarf-tag-function
))
638 ;; Get the local value in the tags table buffer.
639 (setq goto-func goto-tag-location-function
)
641 ;; Find the right line in the specified file.
642 (set-buffer (find-file-noselect file
))
645 (funcall goto-func tag-info
)
647 ;; Give this buffer a local value of tags-file-name.
648 ;; The next time visit-tags-table-buffer is called,
649 ;; it will use the same tags table that found a match in this buffer.
650 (make-local-variable 'tags-file-name
)
651 (setq tags-file-name tags-table-file
)
653 ;; Return the buffer where the tag was found.
656 ;; `etags' TAGS file format support.
658 (defun etags-recognize-tags-table ()
659 (and (eq (char-after 1) ?
\f)
660 ;; It is annoying to flash messages on the screen briefly,
661 ;; and this message is not useful. -- rms
662 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
663 (mapcar (function (lambda (elt)
664 (make-local-variable (car elt
))
665 (set (car elt
) (cdr elt
))))
666 '((file-of-tag-function . etags-file-of-tag
)
667 (tags-table-files-function . etags-tags-table-files
)
668 (tags-completion-table-function . etags-tags-completion-table
)
669 (snarf-tag-function . etags-snarf-tag
)
670 (goto-tag-location-function . etags-goto-tag-location
)
671 (find-tag-regexp-search-function . re-search-forward
)
672 (find-tag-regexp-tag-order .
(tag-re-match-p))
673 (find-tag-regexp-next-line-after-failuire-p . t
)
674 (find-tag-search-function . search-forward
)
675 (find-tag-tag-order .
(tag-exact-match-p tag-word-match-p
677 (find-tag-next-line-after-failure-p . nil
)
678 (list-tags-function . etags-list-tags
)
679 (tags-apropos-function . etags-tags-apropos
)
680 (tags-included-tables-function . etags-tags-included-tables
)
681 (verify-tags-table-function . etags-verify-tags-table
)
684 (defun etags-verify-tags-table ()
685 (= (char-after 1) ?
\f))
687 (defun etags-file-of-tag ()
689 (search-backward "\f\n")
691 (buffer-substring (point)
692 (progn (skip-chars-forward "^,") (point)))))
694 (defun etags-tags-completion-table ()
695 (let ((table (make-vector 511 0)))
697 (goto-char (point-min))
698 (while (search-forward "\177" nil t
)
699 ;; Handle multiple \177's on a line.
701 (skip-chars-backward "^-A-Za-z0-9_$\n") ;sym syntax? XXX
703 (intern (buffer-substring
706 (skip-chars-backward "-A-Za-z0-9_$")
708 ;; `::' in the middle of a C++ tag.
709 (and (= (preceding-char) ?
:)
710 (= (char-after (- (point) 2)) ?
:)
719 (defun etags-snarf-tag ()
720 (let (tag-text startpos
)
721 (search-forward "\177")
722 (setq tag-text
(buffer-substring (1- (point))
723 (save-excursion (beginning-of-line)
726 (setq startpos
(string-to-int (buffer-substring
728 (progn (skip-chars-forward "0-9")
730 ;; Leave point on the next line of the tags file.
732 (cons tag-text startpos
)))
734 (defun etags-goto-tag-location (tag-info)
735 (let ((startpos (cdr tag-info
))
736 ;; This constant is 1/2 the initial search window.
737 ;; There is no sense in making it too small,
738 ;; since just going around the loop once probably
739 ;; costs about as much as searching 2000 chars.
742 (pat (concat "^" (regexp-quote (car tag-info
)))))
744 (setq startpos
(point-min)))
745 (while (and (not found
)
747 (goto-char (- startpos offset
))
750 (re-search-forward pat
(+ startpos offset
) t
)
751 offset
(* 3 offset
))) ; expand search window
753 (re-search-forward pat nil t
)
754 (error "`%s' not found in %s; time to rerun etags"
755 pat buffer-file-name
)))
758 (defun etags-list-tags (file)
760 (if (not (search-forward (concat "\f\n" file
",") nil t
))
763 (while (not (or (eobp) (looking-at "\f")))
764 (princ (buffer-substring (point)
765 (progn (skip-chars-forward "^\177")
770 (defun etags-tags-apropos (string)
772 (while (re-search-forward string nil t
)
774 (princ (buffer-substring (point)
775 (progn (skip-chars-forward "^\177")
780 (defun etags-tags-table-files ()
783 (goto-char (point-min))
784 (while (search-forward "\f\n" nil t
)
786 (skip-chars-forward "^,\n")
787 (or (looking-at ",include$")
788 ;; Expand in the default-directory of the tags table buffer.
789 (setq files
(cons (expand-file-name (buffer-substring beg
(point)))
793 (defun etags-tags-included-tables ()
796 (goto-char (point-min))
797 (while (search-forward "\f\n" nil t
)
799 (skip-chars-forward "^,\n")
800 (if (looking-at ",include$")
801 ;; Expand in the default-directory of the tags table buffer.
802 (setq files
(cons (expand-file-name (buffer-substring beg
(point)))
806 ;; Empty tags file support.
808 (defun recognize-empty-tags-table ()
809 (and (zerop (buffer-size))
810 (mapcar (function (lambda (sym)
811 (make-local-variable sym
)
813 '(tags-table-files-function
814 tags-completion-table-function
815 find-tag-regexp-search-function
816 find-tag-search-function
817 tags-apropos-function
818 tags-included-tables-function
))
819 (set (make-local-variable 'verify-tags-table-function
)
821 (zerop (buffer-size)))))))
823 ;;; Match qualifier functions for tagnames.
825 ;; This might be a neat idea, but it's too hairy at the moment.
826 ;;(defmacro tags-with-syntax (&rest body)
827 ;; (` (let ((current (current-buffer))
828 ;; (otable (syntax-table))
829 ;; (buffer (find-file-noselect (file-of-tag)))
833 ;; (set-buffer buffer)
834 ;; (setq table (syntax-table))
835 ;; (set-buffer current)
836 ;; (set-syntax-table table)
838 ;; (set-syntax-table otable)))))
839 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
841 ;; t if point is at a tag line that matches TAG "exactly".
842 ;; point should be just after a string that matches TAG.
843 (defun tag-exact-match-p (tag)
844 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177") ;not a symbol char
846 (backward-char (1+ (length tag
)))
847 (and (looking-at "\\Sw") (looking-at "\\S_")))))
849 ;; t if point is at a tag line that matches TAG as a word.
850 ;; point should be just after a string that matches TAG.
851 (defun tag-word-match-p (tag)
852 (and (looking-at "\\b.*\177")
853 (save-excursion (backward-char (1+ (length tag
)))
854 (looking-at "\\b"))))
856 ;; t if point is in a tag line with a tag containing TAG as a substring.
857 (defun tag-any-match-p (tag)
858 (looking-at ".*\177"))
860 ;; t if point is at a tag line that matches RE as a regexp.
861 (defun tag-re-match-p (re)
865 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t
)
866 (re-search-backward re bol t
)))))
869 (defun next-file (&optional initialize novisit
)
870 "Select next file among files in current tags table.
871 Non-nil first argument (prefix arg, if interactive)
872 initializes to the beginning of the list of files in the tags table.
874 Non-nil second argument NOVISIT means use a temporary buffer
875 to save time and avoid uninteresting warnings.
877 Value is nil if the file was already visited;
878 if the file was newly read in, the value is the filename."
882 (visit-tags-table-buffer 'reset
)
883 (setq next-file-list
(tags-table-files))))
886 ;; When doing (visit-tag-table-buffer t),
887 ;; the tags table buffer must be current.
888 (if (and (visit-tags-table-buffer 'same
)
889 (visit-tags-table-buffer t
))
890 (setq next-file-list
(tags-table-files))
892 (get-buffer " *next-file*")
893 (kill-buffer " *next-file*"))
894 (error "All files processed."))))
895 (let ((new (not (get-file-buffer (car next-file-list
)))))
896 (if (not (and new novisit
))
897 (set-buffer (find-file-noselect (car next-file-list
) novisit
))
898 ;; Like find-file, but avoids random warning messages.
899 (set-buffer (get-buffer-create " *next-file*"))
900 (kill-all-local-variables)
902 (setq new
(car next-file-list
))
903 (insert-file-contents new nil
))
904 (setq next-file-list
(cdr next-file-list
))
907 (defvar tags-loop-operate nil
908 "Form for `tags-loop-continue' to eval to change one file.")
910 (defvar tags-loop-scan nil
911 "Form for `tags-loop-continue' to eval to scan one file.
912 If it returns non-nil, this file needs processing by evalling
913 \`tags-loop-operate'. Otherwise, move on to the next file.")
916 (defun tags-loop-continue (&optional first-time
)
917 "Continue last \\[tags-search] or \\[tags-query-replace] command.
918 Used noninteractively with non-nil argument to begin such a command.
919 Two variables control the processing we do on each file:
920 the value of `tags-loop-scan' is a form to be executed on each file
921 to see if it is interesting (it returns non-nil if so)
922 and `tags-loop-operate' is a form to execute to operate on an interesting file
923 If the latter returns non-nil, we exit; otherwise we scan the next file."
929 ;; Scan files quickly for the first or next interesting one.
930 (while (or first-time
933 (not (eval tags-loop-scan
))))
934 (setq new
(next-file first-time t
))
935 ;; If NEW is non-nil, we got a temp buffer,
936 ;; and NEW is the file name.
938 (and (not first-time
)
939 (> baud-rate search-slow-speed
)
941 (message "Scanning file %s..." (or new buffer-file-name
)))
942 (setq first-time nil
)
943 (goto-char (point-min)))
945 ;; If we visited it in a temp buffer, visit it now for real.
949 (set-buffer (find-file-noselect new
))
953 (switch-to-buffer (current-buffer))
955 ;; Now operate on the file.
956 ;; If value is non-nil, continue to scan the next file.
957 (eval tags-loop-operate
)))
959 (null tags-loop-operate
)
960 (message "Scanning file %s...found" buffer-file-name
))))
962 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
965 (defun tags-search (regexp)
966 "Search through all files listed in tags table for match for REGEXP.
967 Stops when a match is found.
968 To continue searching for next match, use command \\[tags-loop-continue].
970 See documentation of variable `tags-file-name'."
971 (interactive "sTags search (regexp): ")
972 (if (and (equal regexp
"")
973 (eq (car tags-loop-scan
) 're-search-forward
)
974 (eq tags-loop-operate t
))
975 ;; Continue last tags-search as if by M-,.
976 (tags-loop-continue nil
)
978 (list 're-search-forward regexp nil t
)
979 tags-loop-operate nil
)
980 (tags-loop-continue t
)))
983 (defun tags-query-replace (from to
&optional delimited
)
984 "Query-replace-regexp FROM with TO through all files listed in tags table.
985 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
986 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
987 with the command \\[tags-loop-continue].
989 See documentation of variable `tags-file-name'."
991 "sTags query replace (regexp): \nsTags query replace %s by: \nP")
992 (setq tags-loop-scan
(list 'prog1
993 (list 'if
(list 're-search-forward form nil t
)
994 ;; When we find a match, move back
995 ;; to the beginning of it so perform-replace
997 '(goto-char (match-beginning 0))))
998 tags-loop-operate
(list 'perform-replace from to t t delimited
))
999 (tags-loop-continue t
))
1002 (defun list-tags (file)
1003 "Display list of tags in file FILE.
1004 FILE should not contain a directory specification
1005 unless it has one in the tags table."
1006 (interactive (list (completing-read "List tags in file: " nil
1007 'tags-table-files t nil
)))
1008 (with-output-to-temp-buffer "*Tags List*"
1009 (princ "Tags in file ")
1013 (let ((first-time t
)
1015 (while (visit-tags-table-buffer (if first-time
'reset t
))
1016 (if (funcall list-tags-function file
)
1019 (error "File %s not in current tags tables"))))))
1022 (defun tags-apropos (regexp)
1023 "Display list of all tags in tags table REGEXP matches."
1024 (interactive "sTags apropos (regexp): ")
1025 (with-output-to-temp-buffer "*Tags List*"
1026 (princ "Tags matching regexp ")
1030 (let ((first-time nil
))
1031 (while (visit-tags-table-buffer (if first-time
'reset t
))
1032 (funcall tags-apropos-function
))))))
1034 ;;; XXX Kludge interface.
1036 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1038 (defun select-tags-table ()
1039 "Select a tags table file from a menu of those you have already used.
1040 The list of tags tables to select from is stored in `tags-table-file-list';
1041 see the doc of that variable if you want to add names to the list."
1043 (pop-to-buffer "*Tags Table List*")
1044 (setq buffer-read-only nil
)
1046 (setq selective-display t
1047 selective-display-ellipses nil
)
1048 (let ((set-list tags-table-set-list
)
1049 (desired-point nil
))
1052 (setq desired-point
(point-marker))
1053 (princ tags-table-list
(current-buffer))
1055 (prin1 (car tags-table-list
) (current-buffer)) ;invisible
1058 (if (eq (car set-list
) tags-table-list
)
1059 ;; Already printed it.
1061 (princ (car set-list
) (current-buffer))
1063 (prin1 (car (car set-list
)) (current-buffer)) ;invisible
1065 (setq set-list
(cdr set-list
)))
1069 (setq desired-point
(point-marker)))
1070 (insert tags-file-name
"\C-m")
1071 (prin1 tags-file-name
(current-buffer)) ;invisible
1073 (setq set-list
(delete tags-file-name
1074 (apply 'nconc
(cons tags-table-list
1075 (mapcar 'copy-sequence
1076 tags-table-set-list
)))))
1078 (insert (car set-list
) "\C-m")
1079 (prin1 (car set-list
) (current-buffer)) ;invisible
1081 (setq set-list
(delete (car set-list
) set-list
)))
1083 (insert-before-markers
1084 "Type `t' to select a tags table or set of tags tables:\n\n")
1086 (goto-char desired-point
))
1087 (set-window-start (selected-window) 1 t
))
1088 (set-buffer-modified-p nil
)
1089 (setq buffer-read-only t
1090 mode-name
"Select Tags Table")
1091 (let ((map (make-sparse-keymap)))
1092 (define-key map
"t" 'select-tags-table-select
)
1093 (define-key map
" " 'next-line
)
1094 (define-key map
"\^?" 'previous-line
)
1095 (define-key map
"n" 'next-line
)
1096 (define-key map
"p" 'previous-line
)
1097 (define-key map
"q" 'select-tags-table-quit
)
1098 (use-local-map map
)))
1100 (defun select-tags-table-select ()
1101 "Select the tags table named on this line."
1103 (search-forward "\C-m")
1104 (let ((name (read (current-buffer))))
1105 (visit-tags-table name
)
1106 (select-tags-table-quit)
1107 (message "Tags table now %s" name
)))
1109 (defun select-tags-table-quit ()
1110 "Kill the buffer and delete the selected window."
1112 (kill-buffer (current-buffer))
1117 (defun complete-tag ()
1118 "Perform tags completion on the text around point.
1119 Completes to the set of names listed in the current tags table.
1120 The string to complete is chosen in the same way as the default
1121 for \\[find-tag] (which see)."
1125 (error (substitute-command-keys
1126 "No tags table loaded. Try \\[visit-tags-table].")))
1127 (let ((pattern (funcall (or find-tag-default-function
1128 (get major-mode
'find-tag-default-function
)
1129 'find-tag-default
)))
1133 (error "Nothing to complete"))
1134 (search-backward pattern
)
1136 (forward-char (length pattern
))
1137 (setq completion
(try-completion pattern
'tags-complete-tag nil
))
1138 (cond ((eq completion t
))
1140 (message "Can't find completion for \"%s\"" pattern
)
1142 ((not (string= pattern completion
))
1143 (delete-region beg
(point))
1144 (insert completion
))
1146 (message "Making completion list...")
1147 (with-output-to-temp-buffer " *Completions*"
1148 (display-completion-list
1149 (all-completions pattern
'tags-complete-tag nil
)))
1150 (message "Making completion list...%s" "done")))))
1152 ;;;###autoload (define-key esc-map "\t" 'complete-tag)
1156 ;;; etags.el ends here