1 ;;; etags.el --- etags facility for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1989, 1992, 1993
4 ;; Free Software Foundation, Inc.
6 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs 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 2, or (at your option)
16 ;; GNU Emacs 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.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 (defvar tags-file-name nil
29 "*File name of tags table.
30 To switch to a new tags table, setting this variable is sufficient.
31 If you set this variable, do not also set `tags-table-list'.
32 Use the `etags' program to make a tags table file.")
33 ;; Make M-x set-variable tags-file-name like M-x visit-tags-table.
34 ;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ")
37 ;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
38 (defvar tags-table-list nil
39 "*List of file names of tags tables to search.
40 An element that is a directory means the file \"TAGS\" in that directory.
41 To switch to a new list of tags tables, setting this variable is sufficient.
42 If you set this variable, do not also set `tags-file-name'.
43 Use the `etags' program to make a tags table file.")
45 (defvar tags-table-list-pointer nil
46 "Pointer into `tags-table-list' where the current state of searching is.
47 Might instead point into a list of included tags tables.
48 Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
50 (defvar tags-table-list-started-at nil
51 "Pointer into `tags-table-list', where the current search started.")
53 (defvar tags-table-parent-pointer-list nil
54 "Saved state of the tags table that included this one.
55 Each element is (POINTER . STARTED-AT), giving the values of
56 `tags-table-list-pointer' and `tags-table-list-started-at' from
57 before we moved into the current table.")
59 (defvar tags-table-set-list nil
60 "List of sets of tags table which have been used together in the past.
61 Each element is a list of strings which are file names.")
64 (defvar find-tag-hook nil
65 "*Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'.
66 The value in the buffer in which \\[find-tag] is done is used,
67 not the value in the buffer \\[find-tag] goes to.")
70 (defvar find-tag-default-function nil
71 "*A function of no arguments used by \\[find-tag] to pick a default tag.
72 If nil, and the symbol that is the value of `major-mode'
73 has a `find-tag-default-function' property (see `put'), that is used.
74 Otherwise, `find-tag-default' is used.")
77 (defvar default-tags-table-function nil
78 "*If non-nil, a function of no arguments to choose a default tags file
79 for a particular buffer.")
82 ;; These variables are local in tags table buffers.
84 (defvar tag-lines-already-matched nil
85 "List of positions of beginnings of lines within the tags table
86 that are already matched.")
88 (defvar tags-table-files nil
89 "List of file names covered by current tags table.
90 nil means it has not yet been computed; use `tags-table-files' to do so.")
92 (defvar tags-completion-table nil
93 "Alist of tag names defined in current tags table.")
95 (defvar tags-included-tables nil
96 "List of tags tables included by the current tags table.")
98 (defvar next-file-list nil
99 "List of files for \\[next-file] to process.")
101 ;; Hooks for file formats.
103 (defvar tags-table-format-hooks '(etags-recognize-tags-table
104 recognize-empty-tags-table)
105 "List of functions to be called in a tags table buffer to identify
106 the type of tags table. The functions are called in order, with no arguments,
107 until one returns non-nil. The function should make buffer-local bindings
108 of the format-parsing tags function variables if successful.")
110 (defvar file-of-tag-function nil
111 "Function to do the work of `file-of-tag' (which see).")
112 (defvar tags-table-files-function nil
113 "Function to do the work of `tags-table-files' (which see).")
114 (defvar tags-completion-table-function nil
115 "Function to build the tags-completion-table.")
116 (defvar snarf-tag-function nil
117 "Function to get info about a matched tag for `goto-tag-location-function'.")
118 (defvar goto-tag-location-function nil
119 "Function of to go to the location in the buffer specified by a tag.
120 One argument, the tag info returned by `snarf-tag-function'.")
121 (defvar find-tag-regexp-search-function nil
122 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
123 (defvar find-tag-regexp-tag-order nil
124 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
125 (defvar find-tag-regexp-next-line-after-failure-p nil
126 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
127 (defvar find-tag-search-function nil
128 "Search function passed to `find-tag-in-order' for finding a tag.")
129 (defvar find-tag-tag-order nil
130 "Tag order passed to `find-tag-in-order' for finding a tag.")
131 (defvar find-tag-next-line-after-failure-p nil
132 "Flag passed to `find-tag-in-order' for finding a tag.")
133 (defvar list-tags-function nil
134 "Function to do the work of `list-tags' (which see).")
135 (defvar tags-apropos-function nil
136 "Function to do the work of `tags-apropos' (which see).")
137 (defvar tags-included-tables-function nil
138 "Function to do the work of `tags-included-tables' (which see).")
139 (defvar verify-tags-table-function nil
140 "Function to return t iff the current buffer vontains a valid
141 \(already initialized\) tags file.")
143 ;; Initialize the tags table in the current buffer.
144 ;; Returns non-nil iff it is a valid tags table. On
145 ;; non-nil return, the tags table state variable are
146 ;; made buffer-local and initialized to nil.
147 (defun initialize-new-tags-table ()
148 (set (make-local-variable 'tag-lines-already-matched) nil)
149 (set (make-local-variable 'tags-table-files) nil)
150 (set (make-local-variable 'tags-completion-table) nil)
151 (set (make-local-variable 'tags-included-tables) nil)
152 ;; Value is t if we have found a valid tags table buffer.
153 (let ((hooks tags-table-format-hooks))
155 (not (funcall (car hooks))))
156 (setq hooks (cdr hooks)))
160 (defun visit-tags-table (file &optional local)
161 "Tell tags commands to use tags table file FILE.
162 FILE should be the name of a file created with the `etags' program.
163 A directory name is ok too; it means file TAGS in that directory.
165 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
166 With a prefix arg, set the buffer-local value instead.
167 When you find a tag with \\[find-tag], the buffer it finds the tag
168 in is given a local value of this variable which is the name of the tags
169 file the tag was in."
170 (interactive (list (read-file-name "Visit tags table: (default TAGS) "
172 (expand-file-name "TAGS"
176 ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
177 ;; initialize a buffer for FILE and set tags-file-name to the
178 ;; fully-expanded name.
179 (let ((tags-file-name file))
181 (or (visit-tags-table-buffer 'same)
182 (signal 'file-error (list "Visiting tags table"
183 "file does not exist"
185 ;; Set FILE to the expanded name.
186 (setq file tags-file-name)))
188 ;; Set the local value of tags-file-name.
189 (set (make-local-variable 'tags-file-name) file)
190 ;; Set the global value of tags-file-name.
191 (setq-default tags-file-name file)))
193 ;; Move tags-table-list-pointer along and set tags-file-name.
194 ;; If NO-INCLUDES is non-nil, ignore included tags tables.
195 ;; Returns nil when out of tables.
196 (defun tags-next-table (&optional no-includes)
197 ;; Do we have any included tables?
198 (if (and (not no-includes)
199 (visit-tags-table-buffer 'same)
200 (tags-included-tables))
202 ;; Move into the included tags tables.
203 (setq tags-table-parent-pointer-list
204 ;; Save the current state of what table we are in.
205 (cons (cons tags-table-list-pointer tags-table-list-started-at)
206 tags-table-parent-pointer-list)
207 ;; Start the pointer in the list of included tables.
208 tags-table-list-pointer tags-included-tables
209 tags-table-list-started-at tags-included-tables)
211 ;; No included tables. Go to the next table in the list.
212 (setq tags-table-list-pointer
213 (cdr tags-table-list-pointer))
214 (or tags-table-list-pointer
216 (setq tags-table-list-pointer tags-table-list))
218 (if (eq tags-table-list-pointer tags-table-list-started-at)
219 ;; We have come full circle. No more tables.
220 (if tags-table-parent-pointer-list
221 ;; Pop back to the tags table which includes this one.
223 ;; Restore the state variables.
224 (setq tags-table-list-pointer
225 (car (car tags-table-parent-pointer-list))
226 tags-table-list-started-at
227 (cdr (car tags-table-parent-pointer-list))
228 tags-table-parent-pointer-list
229 (cdr tags-table-parent-pointer-list))
230 ;; Recurse to skip to the next table after the parent.
232 ;; All out of tags tables.
233 (setq tags-table-list-pointer nil))))
235 (and tags-table-list-pointer
236 ;; Set tags-file-name to the fully-expanded name.
238 (tags-expand-table-name (car tags-table-list-pointer)))))
240 ;; Expand tags table name FILE into a complete file name.
241 (defun tags-expand-table-name (file)
242 (setq file (expand-file-name file))
243 (if (file-directory-p file)
244 (expand-file-name "TAGS" file)
247 ;; Return the cdr of LIST (default: tags-table-list) whose car
248 ;; is equal to FILE after tags-expand-table-name on both sides.
249 (defun tags-table-list-member (file &optional list)
251 (setq list tags-table-list))
252 (setq file (tags-expand-table-name file))
254 (not (string= file (tags-expand-table-name (car list)))))
255 (setq list (cdr list)))
258 ;; Subroutine of visit-tags-table-buffer. Frobs its local vars.
259 ;; Search TABLES for one that has tags for THIS-FILE. Recurses
260 ;; on included tables. Returns the tail of TABLES (or of an
261 ;; inner included list) whose car is a table listing THIS-FILE.
262 (defun tags-table-including (this-file tables &optional recursing)
264 ;; Loop over TABLES, looking for one containing tags for THIS-FILE.
265 (while (and (not found)
267 (let ((tags-file-name (tags-expand-table-name (car tables))))
268 (if (or (get-file-buffer tags-file-name)
269 (file-exists-p tags-file-name)) ;XXX check all in core first.
271 ;; Select the tags table buffer and get the file list up to date.
272 (visit-tags-table-buffer 'same)
274 (setq tags-table-files
275 (funcall tags-table-files-function)))
277 (cond ((member this-file tags-table-files)
281 ((tags-included-tables)
282 ;; This table has included tables. Check them.
283 (let ((old tags-table-parent-pointer-list))
287 ;; At top level (not in an included tags
288 ;; table), set the list to nil so we can
289 ;; collect just the elts from this run.
290 (setq tags-table-parent-pointer-list nil))
292 ;; Recurse on the list of included tables.
293 (tags-table-including this-file
297 ;; One of them lists THIS-FILE.
298 ;; Set the table list state variables to move
299 ;; us inside the list of included tables.
300 (setq tags-table-parent-pointer-list
302 (cons tags-table-list-pointer
303 tags-table-list-started-at)
304 tags-table-parent-pointer-list)
305 tags-table-list-pointer found
306 tags-table-list-started-at found
307 ;; CONT is a local variable of
308 ;; our caller, visit-tags-table-buffer.
309 ;; Set it so we won't frob lists later.
312 ;; tags-table-parent-pointer-list now describes
313 ;; the path of included tables taken by recursive
314 ;; invocations of this function. The recursive
315 ;; calls have consed onto the front of the list,
316 ;; so it is now outermost first. We want it
317 ;; innermost first, so reverse it. Then append
318 ;; the old list (from before we were called the
319 ;; outermost time), to get the complete current
320 ;; state of included tables.
321 (setq tags-table-parent-pointer-list
323 tags-table-parent-pointer-list)
325 (setq tables (cdr tables)))
328 (defun visit-tags-table-buffer (&optional cont)
329 "Select the buffer containing the current tags table.
330 If optional arg is t, visit the next table in `tags-table-list'.
331 If optional arg is the atom `same', don't look for a new table;
332 just select the buffer visiting `tags-file-name'.
333 If arg is nil or absent, choose a first buffer from information in
334 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
335 Returns t if it visits a tags table, or nil if there are no more in the list."
337 ;; Set tags-file-name to the tags table file we want to visit.
338 (cond ((eq cont 'same)
339 ;; Use the ambient value of tags-file-name.
341 (error (substitute-command-keys
342 (concat "No tags table in use! "
343 "Use \\[visit-tags-table] to select one."))))
344 ;; Set CONT to nil so the code below will make sure tags-file-name
345 ;; is in tags-table-list.
349 ;; Find the next table.
350 (if (tags-next-table)
351 ;; Skip over nonexistent files.
352 (while (and (let ((file (tags-expand-table-name tags-file-name)))
353 (not (or (get-file-buffer file)
354 (file-exists-p file))))
355 (tags-next-table)))))
358 ;; Pick a table out of our hat.
361 ;; First, try a local variable.
362 (cdr (assq 'tags-file-name (buffer-local-variables)))
363 ;; Second, try a user-specified function to guess.
364 (and default-tags-table-function
365 (funcall default-tags-table-function))
366 ;; Third, look for a tags table that contains
367 ;; tags for the current buffer's file.
368 ;; If one is found, the lists will be frobnicated,
369 ;; and CONT will be set non-nil so we don't do it below.
370 (car (save-excursion (tags-table-including buffer-file-name
372 ;; Fourth, use the user variable tags-file-name, if it is not
373 ;; already in tags-table-list.
375 (not (tags-table-list-member tags-file-name))
377 ;; Fifth, use the user variable giving the table list.
378 ;; Find the first element of the list that actually exists.
379 (let ((list tags-table-list)
382 (setq file (tags-expand-table-name (car list)))
383 (not (get-file-buffer file))
384 (not (file-exists-p file)))
385 (setq list (cdr list)))
387 ;; Finally, prompt the user for a file name.
389 (read-file-name "Visit tags table: (default TAGS) "
394 ;; Expand the table name into a full file name.
395 (setq tags-file-name (tags-expand-table-name tags-file-name))
397 (if (and (eq cont t) (null tags-table-list-pointer))
398 ;; All out of tables.
401 ;; Verify that tags-file-name is a valid tags table.
402 (if (if (get-file-buffer tags-file-name)
403 ;; The file is already in a buffer. Check for the visited file
404 ;; having changed since we last used it.
406 (set-buffer (get-file-buffer tags-file-name))
407 (setq win (or verify-tags-table-function
408 (initialize-new-tags-table)))
409 (if (or (verify-visited-file-modtime (current-buffer))
411 "Tags file has changed, read new contents? ")))
412 (and win (funcall verify-tags-table-function))
414 (initialize-new-tags-table)))
415 (set-buffer (find-file-noselect tags-file-name))
416 (or (string= tags-file-name buffer-file-name)
417 ;; find-file-noselect has changed the file name.
418 ;; Propagate the change to tags-file-name and tags-table-list.
419 (let ((tail (member tags-file-name tags-table-list)))
421 (setcar tail buffer-file-name))
422 (setq tags-file-name buffer-file-name)))
423 (initialize-new-tags-table))
425 ;; We have a valid tags table.
427 ;; Bury the tags table buffer so it
428 ;; doesn't get in the user's way.
429 (bury-buffer (current-buffer))
432 ;; No list frobbing required.
435 ;; Look in the list for the table we chose.
436 (let ((elt (tags-table-list-member tags-file-name)))
438 ;; The table is not in the current set.
439 ;; Try to find it in another previously used set.
440 (let ((sets tags-table-set-list))
442 (not (setq elt (tags-table-list-member
443 tags-file-name (car sets)))))
444 (setq sets (cdr sets)))
446 ;; Found in some other set. Switch to that set.
448 (or (memq tags-table-list tags-table-set-list)
449 ;; Save the current list.
450 (setq tags-table-set-list
451 (cons tags-table-list
452 tags-table-set-list)))
453 (setq tags-table-list (car sets)))
455 ;; Not found in any existing set.
456 (if (and tags-table-list
457 (y-or-n-p (concat "Add " tags-file-name
459 " of tags tables? ")))
460 ;; Add it to the current list.
461 (setq tags-table-list (cons tags-file-name
463 ;; Make a fresh list, and store the old one.
464 (or (memq tags-table-list tags-table-set-list)
465 (setq tags-table-set-list
466 (cons tags-table-list tags-table-set-list)))
467 (setq tags-table-list (list tags-file-name)))
468 (setq elt tags-table-list))))
470 ;; Set the tags table list state variables to point at the table
471 ;; we want to use first.
472 (setq tags-table-list-started-at elt
473 tags-table-list-pointer elt)))
475 ;; Return of t says the tags table is valid.
478 ;; The buffer was not valid. Don't use it again.
479 (let ((file tags-file-name))
480 (kill-local-variable 'tags-file-name)
481 (if (eq file tags-file-name)
482 (setq tags-file-name nil)))
483 (error "File %s is not a valid tags table" buffer-file-name))))
485 (defun file-of-tag ()
486 "Return the file name of the file whose tags point is within.
487 Assumes the tags table is the current buffer.
488 File name returned is relative to tags table file's directory."
489 (funcall file-of-tag-function))
492 (defun tags-table-files ()
493 "Return a list of files in the current tags table.
494 Assumes the tags table is the current buffer.
495 File names returned are absolute."
497 (setq tags-table-files
498 (funcall tags-table-files-function))))
500 (defun tags-included-tables ()
501 "Return a list of tags tables included by the current table.
502 Assumes the tags table is the current buffer."
503 (or tags-included-tables
504 (setq tags-included-tables (funcall tags-included-tables-function))))
506 ;; Build tags-completion-table on demand. The single current tags table
507 ;; and its included tags tables (and their included tables, etc.) have
508 ;; their tags included in the completion table.
509 (defun tags-completion-table ()
510 (or tags-completion-table
513 (message "Making tags completion table for %s..." buffer-file-name)
514 (let ((included (tags-included-tables))
515 (table (funcall tags-completion-table-function)))
517 ;; Iterate over the list of included tables, and combine each
518 ;; included table's completion obarray to the parent obarray.
521 (let ((tags-file-name (car included)))
522 (visit-tags-table-buffer 'same))
523 ;; Recurse in that buffer to compute its completion table.
524 (if (tags-completion-table)
525 ;; Combine the tables.
528 (intern (symbol-name sym) table)))
529 tags-completion-table))
530 (setq included (cdr included))))
531 (setq tags-completion-table table))
532 (message "Making tags completion table for %s...done"
534 (quit (message "Tags completion table construction aborted.")
535 (setq tags-completion-table nil)))))
537 ;; Completion function for tags. Does normal try-completion,
538 ;; but builds tags-completion-table on demand.
539 (defun tags-complete-tag (string predicate what)
541 (visit-tags-table-buffer)
543 (all-completions string (tags-completion-table) predicate)
544 (try-completion string (tags-completion-table) predicate))))
546 ;; Return a default tag to search for, based on the text at point.
547 (defun find-tag-default ()
549 (while (looking-at "\\sw\\|\\s_")
551 (if (or (re-search-backward "\\sw\\|\\s_"
552 (save-excursion (beginning-of-line) (point))
554 (re-search-forward "\\(\\sw\\|\\s_\\)+"
555 (save-excursion (end-of-line) (point))
557 (progn (goto-char (match-end 0))
558 (buffer-substring (point)
559 (progn (forward-sexp -1)
560 (while (looking-at "\\s'")
565 ;; Read a tag name from the minibuffer with defaulting and completion.
566 (defun find-tag-tag (string)
567 (let* ((default (funcall (or find-tag-default-function
568 (get major-mode 'find-tag-default-function)
570 (spec (completing-read (if default
571 (format "%s(default %s) " string default)
573 'tags-complete-tag)))
575 (or default (error "There is no default tag"))
579 "Last tag found by \\[find-tag].")
582 (defun find-tag-noselect (tagname &optional next-p regexp-p)
583 "Find tag (in current tags table) whose name contains TAGNAME.
584 Returns the buffer containing the tag's definition and moves its point there,
585 but does not select the buffer.
586 The default for TAGNAME is the expression in the buffer near point.
588 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
589 for another tag that matches the last tagname or regexp used. When there
590 are multiple matches for a tag, more exact matches are found first.
592 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
594 See documentation of variable `tags-file-name'."
595 (interactive (if current-prefix-arg
597 (list (find-tag-tag "Find tag: "))))
598 ;; Save the current buffer's value of `find-tag-hook' before selecting the
599 ;; tags table buffer.
600 (let ((local-find-tag-hook find-tag-hook))
602 ;; Find the same table we last used.
603 (visit-tags-table-buffer 'same)
604 ;; Pick a table to use.
605 (visit-tags-table-buffer)
606 ;; Record TAGNAME for a future call with NEXT-P non-nil.
607 (setq last-tag tagname))
609 ;; find-tag-in-order does the real work.
610 (find-tag-in-order (if next-p last-tag tagname)
612 find-tag-regexp-search-function
613 find-tag-search-function)
615 find-tag-regexp-tag-order
618 find-tag-regexp-next-line-after-failure-p
619 find-tag-next-line-after-failure-p)
620 (if regexp-p "matching" "containing")
622 (run-hooks 'local-find-tag-hook))))
625 (defun find-tag (tagname &optional next-p)
626 "Find tag (in current tags table) whose name contains TAGNAME.
627 Select the buffer containing the tag's definition, and move point there.
628 The default for TAGNAME is the expression in the buffer around or before point.
630 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
631 for another tag that matches the last tagname used. When there are
632 multiple matches, more exact matches are found first.
634 See documentation of variable `tags-file-name'."
635 (interactive (if current-prefix-arg
637 (list (find-tag-tag "Find tag: "))))
638 (switch-to-buffer (find-tag-noselect tagname next-p)))
639 ;;;###autoload (define-key esc-map "." 'find-tag)
642 (defun find-tag-other-window (tagname &optional next-p)
643 "Find tag (in current tags table) whose name contains TAGNAME.
644 Select the buffer containing the tag's definition
645 in another window, and move point there.
646 The default for TAGNAME is the expression in the buffer around or before point.
648 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
649 for another tag that matches the last tagname used. When there are
650 multiple matches, more exact matches are found first.
652 See documentation of variable `tags-file-name'."
653 (interactive (if current-prefix-arg
655 (list (find-tag-tag "Find tag other window: "))))
656 ;; This hair is to deal with the case where the tag is found in the
657 ;; selected window's buffer; without the hair, point is moved in both
658 ;; windows. To prevent this, we save the selected window's point before
659 ;; doing find-tag-noselect, and restore it after.
660 (let* ((window-point (window-point (selected-window)))
661 (tagbuf (find-tag-noselect tagname next-p))
662 (tagpoint (progn (set-buffer tagbuf) (point))))
663 (set-window-point (prog1
665 (switch-to-buffer-other-window tagbuf)
666 ;; We have to set this new window's point; it
667 ;; might already have been displaying a
668 ;; different portion of tagbuf, in which case
669 ;; switch-to-buffer-other-window doesn't set
670 ;; the window's point from the buffer.
671 (set-window-point (selected-window) tagpoint))
673 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
676 (defun find-tag-other-frame (tagname &optional next-p)
677 "Find tag (in current tag table) whose name contains TAGNAME.
678 Selects the buffer that the tag is contained in in another frame
679 and puts point at its definition.
680 If TAGNAME is a null string, the expression in the buffer
681 around or before point is used as the tag name.
682 If second arg NEXT-P is non-nil (interactively, with prefix arg),
683 searches for the next tag in the tag table
684 that matches the tagname used in the previous find-tag.
686 See documentation of variable `tags-file-name'."
687 (interactive (if current-prefix-arg
689 (list (find-tag-tag "Find tag other window: "))))
690 (let ((pop-up-frames t))
691 (find-tag-other-window tagname next-p)))
692 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
695 (defun find-tag-regexp (regexp &optional next-p other-window)
696 "Find tag (in current tags table) whose name matches REGEXP.
697 Select the buffer containing the tag's definition and move point there.
699 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
700 for another tag that matches the last tagname used.
702 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
704 See documentation of variable `tags-file-name'."
705 (interactive (if current-prefix-arg
707 (list (read-string "Find tag regexp: "))))
708 (funcall (if other-window 'switch-to-buffer-other-window 'switch-to-buffer)
709 (find-tag-noselect regexp next-p t)))
711 ;; Internal tag finding function.
713 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
714 ;; any member of the function list ORDER (third arg). If ORDER is nil,
715 ;; use saved state to continue a previous search.
717 ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
720 ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
721 ;; point should be moved to the next line.
723 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
724 ;; beginning of tags file, and perform inner loop: for each naive match for
725 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
726 ;; qualifier-func. If it qualifies, go to the specified line in the
727 ;; specified source file and return. Qualified matches are remembered to
728 ;; avoid repetition. State is saved so that the loop can be continued.
730 (defun find-tag-in-order (pattern
733 next-line-after-failure-p
736 (let (file ;name of file containing tag
737 tag-info ;where to find the tag in FILE
738 tags-table-file ;name of tags file
744 (or first-search ;find-tag-noselect has already done it.
745 (visit-tags-table-buffer 'same))
747 ;; Get a qualified match.
748 (catch 'qualified-match-found
750 ;; Iterate over the list of tags tables.
751 (while (or first-table
752 (visit-tags-table-buffer t))
755 (setq tag-lines-already-matched nil))
757 (and first-search first-table
758 ;; Start at beginning of tags file.
759 (goto-char (point-min)))
760 (setq first-table nil)
762 (setq tags-table-file buffer-file-name)
763 ;; Iterate over the list of ordering predicates.
765 (while (funcall search-forward-func pattern nil t)
766 ;; Naive match found. Qualify the match.
767 (and (funcall (car order) pattern)
768 ;; Make sure it is not a previous qualified match.
769 ;; Use of `memq' depends on numbers being eq.
770 (not (memq (save-excursion (beginning-of-line) (point))
771 tag-lines-already-matched))
772 (throw 'qualified-match-found nil))
773 (if next-line-after-failure-p
775 ;; Try the next flavor of match.
776 (setq order (cdr order))
777 (goto-char (point-min)))
778 (setq order tag-order))
779 ;; We throw out on match, so only get here if there were no matches.
780 (error "No %stags %s %s" (if first-search "" "more ")
783 ;; Found a tag; extract location info.
785 (setq tag-lines-already-matched (cons (point)
786 tag-lines-already-matched))
787 ;; Expand the filename, using the tags table buffer's default-directory.
788 (setq file (expand-file-name (file-of-tag))
789 tag-info (funcall snarf-tag-function))
791 ;; Get the local value in the tags table buffer before switching buffers.
792 (setq goto-func goto-tag-location-function)
794 ;; Find the right line in the specified file.
795 (set-buffer (find-file-noselect file))
798 (funcall goto-func tag-info)
800 ;; Give this buffer a local value of tags-file-name.
801 ;; The next time visit-tags-table-buffer is called,
802 ;; it will use the same tags table that found a match in this buffer.
803 (make-local-variable 'tags-file-name)
804 (setq tags-file-name tags-table-file)
806 ;; Return the buffer where the tag was found.
809 ;; `etags' TAGS file format support.
811 ;; If the current buffer is a valid etags TAGS file, give it local values of
812 ;; the tags table format variables, and return non-nil.
813 (defun etags-recognize-tags-table ()
814 (and (etags-verify-tags-table)
815 ;; It is annoying to flash messages on the screen briefly,
816 ;; and this message is not useful. -- rms
817 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
818 (mapcar (function (lambda (elt)
819 (set (make-local-variable (car elt)) (cdr elt))))
820 '((file-of-tag-function . etags-file-of-tag)
821 (tags-table-files-function . etags-tags-table-files)
822 (tags-completion-table-function . etags-tags-completion-table)
823 (snarf-tag-function . etags-snarf-tag)
824 (goto-tag-location-function . etags-goto-tag-location)
825 (find-tag-regexp-search-function . re-search-forward)
826 (find-tag-regexp-tag-order . (tag-re-match-p))
827 (find-tag-regexp-next-line-after-failuire-p . t)
828 (find-tag-search-function . search-forward)
829 (find-tag-tag-order . (tag-exact-match-p tag-word-match-p
831 (find-tag-next-line-after-failure-p . nil)
832 (list-tags-function . etags-list-tags)
833 (tags-apropos-function . etags-tags-apropos)
834 (tags-included-tables-function . etags-tags-included-tables)
835 (verify-tags-table-function . etags-verify-tags-table)
838 ;; Return non-nil iff the current buffer is a valid etags TAGS file.
839 (defun etags-verify-tags-table ()
840 ;; Use eq instead of = in case char-after returns nil.
841 (eq (char-after 1) ?\f))
843 (defun etags-file-of-tag ()
845 (search-backward "\f\n")
847 (buffer-substring (point)
848 (progn (skip-chars-forward "^,") (point)))))
850 (defun etags-tags-completion-table ()
851 (let ((table (make-vector 511 0)))
853 (goto-char (point-min))
854 ;; This monster regexp matches an etags tag line.
855 ;; \1 is the string to match;
856 ;; \2 is not interesting;
857 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
858 ;; \4 is the char to start searching at;
859 ;; \5 is the line to start searching at;
860 ;; \6 is not interesting;
861 ;; \7 is the explicitly-specified tag name.
862 (while (re-search-forward
863 "^\\(\\(.+[ \t]+\\)?\\([-a-zA-Z0-9_$]+\\)[^-a-zA-Z0-9_$]*\\)\177\
864 \\([0-9]+\\),\\([0-9]+\\)\\(,\001\\([^\n]+\\)\\)?\n"
866 (intern (if (match-beginning 6)
867 ;; There is an explicit tag name.
868 (buffer-substring (match-beginning 6) (match-end 6))
869 ;; No explicit tag name. Best guess.
870 (buffer-substring (match-beginning 3) (match-end 3)))
874 (defun etags-snarf-tag ()
875 (let (tag-text startpos)
876 (search-forward "\177")
877 (setq tag-text (buffer-substring (1- (point))
878 (save-excursion (beginning-of-line)
881 (setq startpos (string-to-int (buffer-substring
883 (progn (skip-chars-forward "0-9")
885 ;; Leave point on the next line of the tags file.
887 (cons tag-text startpos)))
889 (defun etags-goto-tag-location (tag-info)
890 (let ((startpos (cdr tag-info))
891 ;; This constant is 1/2 the initial search window.
892 ;; There is no sense in making it too small,
893 ;; since just going around the loop once probably
894 ;; costs about as much as searching 2000 chars.
897 (pat (concat "^" (regexp-quote (car tag-info)))))
899 (setq startpos (point-min)))
900 (while (and (not found)
902 (goto-char (- startpos offset))
905 (re-search-forward pat (+ startpos offset) t)
906 offset (* 3 offset))) ; expand search window
908 (re-search-forward pat nil t)
909 (error "`%s' not found in %s; time to rerun etags"
910 pat buffer-file-name)))
913 (defun etags-list-tags (file)
915 (if (not (search-forward (concat "\f\n" file ",") nil t))
918 (while (not (or (eobp) (looking-at "\f")))
919 (princ (buffer-substring (point)
920 (progn (skip-chars-forward "^\177")
925 (defun etags-tags-apropos (string)
927 (while (re-search-forward string nil t)
929 (princ (buffer-substring (point)
930 (progn (skip-chars-forward "^\177")
935 (defun etags-tags-table-files ()
938 (goto-char (point-min))
939 (while (search-forward "\f\n" nil t)
941 (skip-chars-forward "^,\n")
942 (or (looking-at ",include$")
943 ;; Expand in the default-directory of the tags table buffer.
944 (setq files (cons (expand-file-name (buffer-substring beg (point)))
948 (defun etags-tags-included-tables ()
951 (goto-char (point-min))
952 (while (search-forward "\f\n" nil t)
954 (skip-chars-forward "^,\n")
955 (if (looking-at ",include$")
956 ;; Expand in the default-directory of the tags table buffer.
957 (setq files (cons (expand-file-name (buffer-substring beg (point)))
961 ;; Empty tags file support.
963 ;; Recognize an empty file and give it local values of the tags table format
964 ;; variables which do nothing.
965 (defun recognize-empty-tags-table ()
966 (and (zerop (buffer-size))
967 (mapcar (function (lambda (sym)
968 (set (make-local-variable sym) 'ignore)))
969 '(tags-table-files-function
970 tags-completion-table-function
971 find-tag-regexp-search-function
972 find-tag-search-function
973 tags-apropos-function
974 tags-included-tables-function))
975 (set (make-local-variable 'verify-tags-table-function)
977 (zerop (buffer-size)))))))
979 ;;; Match qualifier functions for tagnames.
980 ;;; XXX these functions assume etags file format.
982 ;; This might be a neat idea, but it's too hairy at the moment.
983 ;;(defmacro tags-with-syntax (&rest body)
984 ;; (` (let ((current (current-buffer))
985 ;; (otable (syntax-table))
986 ;; (buffer (find-file-noselect (file-of-tag)))
990 ;; (set-buffer buffer)
991 ;; (setq table (syntax-table))
992 ;; (set-buffer current)
993 ;; (set-syntax-table table)
995 ;; (set-syntax-table otable)))))
996 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
998 ;; t if point is at a tag line that matches TAG "exactly".
999 ;; point should be just after a string that matches TAG.
1000 (defun tag-exact-match-p (tag)
1001 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177") ;not a symbol char
1003 (backward-char (1+ (length tag)))
1004 (and (looking-at "\\Sw") (looking-at "\\S_")))))
1006 ;; t if point is at a tag line that matches TAG as a word.
1007 ;; point should be just after a string that matches TAG.
1008 (defun tag-word-match-p (tag)
1009 (and (looking-at "\\b.*\177")
1010 (save-excursion (backward-char (1+ (length tag)))
1011 (looking-at "\\b"))))
1013 ;; t if point is in a tag line with a tag containing TAG as a substring.
1014 (defun tag-any-match-p (tag)
1015 (looking-at ".*\177"))
1017 ;; t if point is at a tag line that matches RE as a regexp.
1018 (defun tag-re-match-p (re)
1021 (let ((bol (point)))
1022 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t)
1023 (re-search-backward re bol t)))))
1026 (defun next-file (&optional initialize novisit)
1027 "Select next file among files in current tags table.
1028 Non-nil first argument (prefix arg, if interactive)
1029 initializes to the beginning of the list of files in the tags table.
1031 Non-nil second argument NOVISIT means use a temporary buffer
1032 to save time and avoid uninteresting warnings.
1034 Value is nil if the file was already visited;
1035 if the file was newly read in, the value is the filename."
1039 ;; Visit the tags table buffer to get its list of files.
1040 (visit-tags-table-buffer)
1041 (setq next-file-list (tags-table-files))))
1044 ;; Get the files from the next tags table.
1045 ;; When doing (visit-tags-table-buffer t),
1046 ;; the tags table buffer must be current.
1047 (if (and (visit-tags-table-buffer 'same)
1048 (visit-tags-table-buffer t))
1049 (setq next-file-list (tags-table-files))
1051 (get-buffer " *next-file*")
1052 (kill-buffer " *next-file*"))
1053 (error "All files processed."))))
1054 (let ((new (not (get-file-buffer (car next-file-list)))))
1055 (if (not (and new novisit))
1056 (set-buffer (find-file-noselect (car next-file-list) novisit))
1057 ;; Like find-file, but avoids random warning messages.
1058 (set-buffer (get-buffer-create " *next-file*"))
1059 (kill-all-local-variables)
1061 (setq new (car next-file-list))
1062 (insert-file-contents new nil))
1063 (setq next-file-list (cdr next-file-list))
1066 (defvar tags-loop-operate nil
1067 "Form for `tags-loop-continue' to eval to change one file.")
1069 (defvar tags-loop-scan
1070 '(error (substitute-command-keys
1071 "No \\[tags-search] or \\[tags-query-replace] in progress."))
1072 "Form for `tags-loop-continue' to eval to scan one file.
1073 If it returns non-nil, this file needs processing by evalling
1074 \`tags-loop-operate'. Otherwise, move on to the next file.")
1077 (defun tags-loop-continue (&optional first-time)
1078 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1079 Used noninteractively with non-nil argument to begin such a command.
1080 Two variables control the processing we do on each file:
1081 the value of `tags-loop-scan' is a form to be executed on each file
1082 to see if it is interesting (it returns non-nil if so)
1083 and `tags-loop-operate' is a form to execute to operate on an interesting file
1084 If the latter returns non-nil, we exit; otherwise we scan the next file."
1090 ;; Scan files quickly for the first or next interesting one.
1091 (while (or first-time
1094 (not (eval tags-loop-scan))))
1095 (setq new (next-file first-time t))
1096 ;; If NEW is non-nil, we got a temp buffer,
1097 ;; and NEW is the file name.
1099 (and (not first-time)
1100 (> baud-rate search-slow-speed)
1102 (message "Scanning file %s..." (or new buffer-file-name)))
1103 (setq first-time nil)
1104 (goto-char (point-min)))
1106 ;; If we visited it in a temp buffer, visit it now for real.
1108 (let ((pos (point)))
1110 (set-buffer (find-file-noselect new))
1114 (switch-to-buffer (current-buffer))
1116 ;; Now operate on the file.
1117 ;; If value is non-nil, continue to scan the next file.
1118 (eval tags-loop-operate)))
1120 (null tags-loop-operate)
1121 (message "Scanning file %s...found" buffer-file-name))))
1122 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
1125 (defun tags-search (regexp)
1126 "Search through all files listed in tags table for match for REGEXP.
1127 Stops when a match is found.
1128 To continue searching for next match, use command \\[tags-loop-continue].
1130 See documentation of variable `tags-file-name'."
1131 (interactive "sTags search (regexp): ")
1132 (if (and (equal regexp "")
1133 (eq (car tags-loop-scan) 're-search-forward)
1134 (null tags-loop-operate))
1135 ;; Continue last tags-search as if by M-,.
1136 (tags-loop-continue nil)
1137 (setq tags-loop-scan
1138 (list 're-search-forward regexp nil t)
1139 tags-loop-operate nil)
1140 (tags-loop-continue t)))
1143 (defun tags-query-replace (from to &optional delimited)
1144 "Query-replace-regexp FROM with TO through all files listed in tags table.
1145 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1146 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
1147 with the command \\[tags-loop-continue].
1149 See documentation of variable `tags-file-name'."
1151 "sTags query replace (regexp): \nsTags query replace %s by: \nP")
1152 (setq tags-loop-scan (list 'prog1
1153 (list 'if (list 're-search-forward from nil t)
1154 ;; When we find a match, move back
1155 ;; to the beginning of it so perform-replace
1157 '(goto-char (match-beginning 0))))
1158 tags-loop-operate (list 'perform-replace from to t t delimited))
1159 (tags-loop-continue t))
1162 (defun list-tags (file)
1163 "Display list of tags in file FILE.
1164 FILE should not contain a directory specification
1165 unless it has one in the tags table."
1166 (interactive (list (completing-read "List tags in file: " nil
1167 'tags-table-files t nil)))
1168 (with-output-to-temp-buffer "*Tags List*"
1169 (princ "Tags in file ")
1173 (let ((first-time t)
1175 (while (visit-tags-table-buffer (not first-time))
1176 (if (funcall list-tags-function file)
1179 (error "File %s not in current tags tables"))))))
1182 (defun tags-apropos (regexp)
1183 "Display list of all tags in tags table REGEXP matches."
1184 (interactive "sTags apropos (regexp): ")
1185 (with-output-to-temp-buffer "*Tags List*"
1186 (princ "Tags matching regexp ")
1190 (let ((first-time t))
1191 (while (visit-tags-table-buffer (not first-time))
1192 (setq first-time nil)
1193 (funcall tags-apropos-function regexp))))))
1195 ;;; XXX Kludge interface.
1197 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1199 (defun select-tags-table ()
1200 "Select a tags table file from a menu of those you have already used.
1201 The list of tags tables to select from is stored in `tags-table-file-list';
1202 see the doc of that variable if you want to add names to the list."
1204 (pop-to-buffer "*Tags Table List*")
1205 (setq buffer-read-only nil)
1207 (setq selective-display t
1208 selective-display-ellipses nil)
1209 (let ((set-list tags-table-set-list)
1210 (desired-point nil))
1213 (setq desired-point (point-marker))
1214 (princ tags-table-list (current-buffer))
1216 (prin1 (car tags-table-list) (current-buffer)) ;invisible
1219 (if (eq (car set-list) tags-table-list)
1220 ;; Already printed it.
1222 (princ (car set-list) (current-buffer))
1224 (prin1 (car (car set-list)) (current-buffer)) ;invisible
1226 (setq set-list (cdr set-list)))
1230 (setq desired-point (point-marker)))
1231 (insert tags-file-name "\C-m")
1232 (prin1 tags-file-name (current-buffer)) ;invisible
1234 (setq set-list (delete tags-file-name
1235 (apply 'nconc (cons tags-table-list
1236 (mapcar 'copy-sequence
1237 tags-table-set-list)))))
1239 (insert (car set-list) "\C-m")
1240 (prin1 (car set-list) (current-buffer)) ;invisible
1242 (setq set-list (delete (car set-list) set-list)))
1244 (insert-before-markers
1245 "Type `t' to select a tags table or set of tags tables:\n\n")
1247 (goto-char desired-point))
1248 (set-window-start (selected-window) 1 t))
1249 (set-buffer-modified-p nil)
1250 (setq buffer-read-only t
1251 mode-name "Select Tags Table")
1252 (let ((map (make-sparse-keymap)))
1253 (define-key map "t" 'select-tags-table-select)
1254 (define-key map " " 'next-line)
1255 (define-key map "\^?" 'previous-line)
1256 (define-key map "n" 'next-line)
1257 (define-key map "p" 'previous-line)
1258 (define-key map "q" 'select-tags-table-quit)
1259 (use-local-map map)))
1261 (defun select-tags-table-select ()
1262 "Select the tags table named on this line."
1264 (search-forward "\C-m")
1265 (let ((name (read (current-buffer))))
1266 (visit-tags-table name)
1267 (select-tags-table-quit)
1268 (message "Tags table now %s" name)))
1270 (defun select-tags-table-quit ()
1271 "Kill the buffer and delete the selected window."
1273 (kill-buffer (current-buffer))
1278 (defun complete-tag ()
1279 "Perform tags completion on the text around point.
1280 Completes to the set of names listed in the current tags table.
1281 The string to complete is chosen in the same way as the default
1282 for \\[find-tag] (which see)."
1286 (error (substitute-command-keys
1287 "No tags table loaded. Try \\[visit-tags-table].")))
1288 (let ((pattern (funcall (or find-tag-default-function
1289 (get major-mode 'find-tag-default-function)
1290 'find-tag-default)))
1294 (error "Nothing to complete"))
1295 (search-backward pattern)
1297 (forward-char (length pattern))
1298 (setq completion (try-completion pattern 'tags-complete-tag nil))
1299 (cond ((eq completion t))
1301 (message "Can't find completion for \"%s\"" pattern)
1303 ((not (string= pattern completion))
1304 (delete-region beg (point))
1305 (insert completion))
1307 (message "Making completion list...")
1308 (with-output-to-temp-buffer " *Completions*"
1309 (display-completion-list
1310 (all-completions pattern 'tags-complete-tag nil)))
1311 (message "Making completion list...%s" "done")))))
1313 ;;;###autoload (define-key esc-map "\t" 'complete-tag)
1317 ;;; etags.el ends here