1 ;;; etags.el --- etags facility for Emacs
2 ;; Copyright (C) 1985, 86, 88, 89, 92, 93, 94, 95, 96, 98, 2000
3 ;; Free Software Foundation, Inc.
5 ;; Author: Roland McGrath <roland@gnu.org>
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 the
22 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
23 ;; Boston, MA 02111-1307, USA.
28 (eval-when-compile (require 'cl)) ; for `gensym'
31 (defvar tags-file-name nil
32 "*File name of tags table.
33 To switch to a new tags table, setting this variable is sufficient.
34 If you set this variable, do not also set `tags-table-list'.
35 Use the `etags' program to make a tags table file.")
36 ;; Make M-x set-variable tags-file-name like M-x visit-tags-table.
37 ;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ")
39 (defgroup etags nil "Tags tables"
43 (defcustom tags-case-fold-search 'default
44 "*Whether tags operations should be case-sensitive.
45 A value of t means case-insensitive, a value of nil means case-sensitive.
46 Any other value means use the setting of `case-fold-search'."
48 :type '(choice (const :tag "Case-sensitive" nil)
49 (const :tag "Case-insensitive" t)
50 (other :tag "Use default" default))
54 ;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
55 (defcustom tags-table-list nil
56 "*List of file names of tags tables to search.
57 An element that is a directory means the file \"TAGS\" in that directory.
58 To switch to a new list of tags tables, setting this variable is sufficient.
59 If you set this variable, do not also set `tags-file-name'.
60 Use the `etags' program to make a tags table file."
65 (defcustom tags-add-tables 'ask-user
66 "*Control whether to add a new tags table to the current list.
67 t means do; nil means don't (always start a new list).
68 Any other value means ask the user whether to add a new tags table
69 to the current list (as opposed to starting a new list)."
71 :type '(choice (const :tag "Do" t)
72 (const :tag "Don't" nil)
73 (other :tag "Ask" ask-user)))
75 (defcustom tags-revert-without-query nil
76 "*Non-nil means reread a TAGS table without querying, if it has changed."
80 (defvar tags-table-computed-list nil
81 "List of tags tables to search, computed from `tags-table-list'.
82 This includes tables implicitly included by other tables. The list is not
83 always complete: the included tables of a table are not known until that
84 table is read into core. An element that is `t' is a placeholder
85 indicating that the preceding element is a table that has not been read
86 into core and might contain included tables to search.
87 See `tags-table-check-computed-list'.")
89 (defvar tags-table-computed-list-for nil
90 "Value of `tags-table-list' that `tags-table-computed-list' corresponds to.
91 If `tags-table-list' changes, `tags-table-computed-list' is thrown away and
92 recomputed; see `tags-table-check-computed-list'.")
94 (defvar tags-table-list-pointer nil
95 "Pointer into `tags-table-computed-list' for the current state of searching.
96 Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
98 (defvar tags-table-list-started-at nil
99 "Pointer into `tags-table-computed-list', where the current search started.")
101 (defvar tags-table-set-list nil
102 "List of sets of tags table which have been used together in the past.
103 Each element is a list of strings which are file names.")
106 (defcustom find-tag-hook nil
107 "*Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'.
108 The value in the buffer in which \\[find-tag] is done is used,
109 not the value in the buffer \\[find-tag] goes to."
114 (defcustom find-tag-default-function nil
115 "*A function of no arguments used by \\[find-tag] to pick a default tag.
116 If nil, and the symbol that is the value of `major-mode'
117 has a `find-tag-default-function' property (see `put'), that is used.
118 Otherwise, `find-tag-default' is used."
122 (defcustom find-tag-marker-ring-length 16
123 "*Length of marker rings `find-tag-marker-ring' and `tags-location-ring'."
128 (defcustom tags-tag-face 'default
129 "*Face for tags in the output of `tags-apropos'."
134 (defcustom tags-apropos-verbose nil
135 "If non-nil, print the name of the tags file in the *Tags List* buffer."
140 (defcustom tags-apropos-additional-actions nil
141 "Specify additional actions for `tags-apropos'.
143 If non-nil, value should be a list of triples (TITLE FUNCTION
144 TO-SEARCH). For each triple, `tags-apropos' processes TO-SEARCH and
145 lists tags from it. TO-SEARCH should be an alist, obarray, or symbol.
146 If it is a symbol, the symbol's value is used.
147 TITLE. a string, is a title used to label the additional list of tags.
148 FUNCTION is a function to call when a symbol is selected in the
149 *Tags List* buffer. It will be called with one argument SYMBOL which
150 is the symbol being selected.
154 '((\"Emacs Lisp\" Info-goto-emacs-command-node obarray)
155 (\"Common Lisp\" common-lisp-hyperspec common-lisp-hyperspec-obarray)
156 (\"SCWM\" scwm-documentation scwm-obarray))"
161 (defvar find-tag-marker-ring (make-ring find-tag-marker-ring-length)
162 "Ring of markers which are locations from which \\[find-tag] was invoked.")
164 (defvar default-tags-table-function nil
165 "If non-nil, a function to choose a default tags file for a buffer.
166 This function receives no arguments and should return the default
167 tags table file to use for the current buffer.")
169 (defvar tags-location-ring (make-ring find-tag-marker-ring-length)
170 "Ring of markers which are locations visited by \\[find-tag].
171 Pop back to the last location with \\[negative-argument] \\[find-tag].")
174 ;; These variables are local in tags table buffers.
176 (defvar tags-table-files nil
177 "List of file names covered by current tags table.
178 nil means it has not yet been computed; use `tags-table-files' to do so.")
180 (defvar tags-completion-table nil
181 "Obarray of tag names defined in current tags table.")
183 (defvar tags-included-tables nil
184 "List of tags tables included by the current tags table.")
186 (defvar next-file-list nil
187 "List of files for \\[next-file] to process.")
189 ;; Hooks for file formats.
191 (defvar tags-table-format-hooks '(etags-recognize-tags-table
192 tags-recognize-empty-tags-table)
193 "List of functions to be called in a tags table buffer to identify the type of tags table.
194 The functions are called in order, with no arguments,
195 until one returns non-nil. The function should make buffer-local bindings
196 of the format-parsing tags function variables if successful.")
198 (defvar file-of-tag-function nil
199 "Function to do the work of `file-of-tag' (which see).")
200 (defvar tags-table-files-function nil
201 "Function to do the work of `tags-table-files' (which see).")
202 (defvar tags-completion-table-function nil
203 "Function to build the tags-completion-table.")
204 (defvar snarf-tag-function nil
205 "Function to get info about a matched tag for `goto-tag-location-function'.")
206 (defvar goto-tag-location-function nil
207 "Function of to go to the location in the buffer specified by a tag.
208 One argument, the tag info returned by `snarf-tag-function'.")
209 (defvar find-tag-regexp-search-function nil
210 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
211 (defvar find-tag-regexp-tag-order nil
212 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
213 (defvar find-tag-regexp-next-line-after-failure-p nil
214 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
215 (defvar find-tag-search-function nil
216 "Search function passed to `find-tag-in-order' for finding a tag.")
217 (defvar find-tag-tag-order nil
218 "Tag order passed to `find-tag-in-order' for finding a tag.")
219 (defvar find-tag-next-line-after-failure-p nil
220 "Flag passed to `find-tag-in-order' for finding a tag.")
221 (defvar list-tags-function nil
222 "Function to do the work of `list-tags' (which see).")
223 (defvar tags-apropos-function nil
224 "Function to do the work of `tags-apropos' (which see).")
225 (defvar tags-included-tables-function nil
226 "Function to do the work of `tags-included-tables' (which see).")
227 (defvar verify-tags-table-function nil
228 "Function to return t iff current buffer contains valid tags file.")
230 ;; Initialize the tags table in the current buffer.
231 ;; Returns non-nil iff it is a valid tags table. On
232 ;; non-nil return, the tags table state variable are
233 ;; made buffer-local and initialized to nil.
234 (defun initialize-new-tags-table ()
235 (set (make-local-variable 'tags-table-files) nil)
236 (set (make-local-variable 'tags-completion-table) nil)
237 (set (make-local-variable 'tags-included-tables) nil)
238 ;; We used to initialize find-tag-marker-ring and tags-location-ring
239 ;; here, to new empty rings. But that is wrong, because those
242 ;; Value is t if we have found a valid tags table buffer.
243 (let ((hooks tags-table-format-hooks))
245 (not (funcall (car hooks))))
246 (setq hooks (cdr hooks)))
250 (defun visit-tags-table (file &optional local)
251 "Tell tags commands to use tags table file FILE.
252 FILE should be the name of a file created with the `etags' program.
253 A directory name is ok too; it means file TAGS in that directory.
255 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
256 With a prefix arg, set the buffer-local value instead.
257 When you find a tag with \\[find-tag], the buffer it finds the tag
258 in is given a local value of this variable which is the name of the tags
259 file the tag was in."
260 (interactive (list (read-file-name "Visit tags table: (default TAGS) "
262 (expand-file-name "TAGS"
266 (or (stringp file) (signal 'wrong-type-argument (list 'stringp file)))
267 ;; Bind tags-file-name so we can control below whether the local or
268 ;; global value gets set. Calling visit-tags-table-buffer will
269 ;; initialize a buffer for the file and set tags-file-name to the
270 ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
271 ;; initialize a buffer for FILE and set tags-file-name to the
272 ;; fully-expanded name.
273 (let ((tags-file-name file))
275 (or (visit-tags-table-buffer file)
276 (signal 'file-error (list "Visiting tags table"
277 "file does not exist"
279 ;; Set FILE to the expanded name.
280 (setq file tags-file-name)))
282 ;; Set the local value of tags-file-name.
283 (set (make-local-variable 'tags-file-name) file)
284 ;; Set the global value of tags-file-name.
285 (setq-default tags-file-name file)))
287 (defun tags-table-check-computed-list ()
288 "Compute `tags-table-computed-list' from `tags-table-list' if necessary."
289 (let ((expanded-list (mapcar 'tags-expand-table-name tags-table-list)))
290 (or (equal tags-table-computed-list-for expanded-list)
291 ;; The list (or default-directory) has changed since last computed.
292 (let* ((compute-for (mapcar 'copy-sequence expanded-list))
293 (tables (copy-sequence compute-for)) ;Mutated in the loop.
298 (setq computed (cons (car tables) computed)
299 table-buffer (get-file-buffer (car tables)))
300 (if (and table-buffer
301 ;; There is a buffer visiting the file. Now make sure
302 ;; it is initialized as a tag table buffer.
304 (tags-verify-table (buffer-file-name table-buffer))))
306 (set-buffer table-buffer)
307 (if (tags-included-tables)
308 ;; Insert the included tables into the list we
310 (setcdr tables (nconc (mapcar 'tags-expand-table-name
311 (tags-included-tables))
313 ;; This table is not in core yet. Insert a placeholder
314 ;; saying we must read it into core to check for included
315 ;; tables before searching the next table in the list.
316 (setq computed (cons t computed)))
317 (setq tables (cdr tables)))
319 ;; Record the tags-table-list value (and the context of the
320 ;; current directory) we computed from.
321 (setq tags-table-computed-list-for compute-for
322 tags-table-computed-list (nreverse computed))))))
324 ;; Extend `tags-table-computed-list' to remove the first `t' placeholder.
325 ;; An element of the list that is `t' is a placeholder indicating that the
326 ;; preceding element is a table that has not been read into core and might
327 ;; contain included tables to search. On return, the first placeholder
328 ;; element will be gone and the element before it read into core and its
329 ;; included tables inserted into the list.
330 (defun tags-table-extend-computed-list ()
331 (let ((list tags-table-computed-list))
332 (while (not (eq (nth 1 list) t))
333 (setq list (cdr list)))
335 (if (tags-verify-table (car list))
336 ;; We are now in the buffer visiting (car LIST). Extract its
337 ;; list of included tables and insert it into the computed list.
338 (let ((tables (tags-included-tables))
342 (setq computed (cons (car tables) computed)
343 table-buffer (get-file-buffer (car tables)))
346 (set-buffer table-buffer)
347 (if (tags-included-tables)
348 ;; Insert the included tables into the list we
350 (setcdr tables (append (tags-included-tables)
352 ;; This table is not in core yet. Insert a placeholder
353 ;; saying we must read it into core to check for included
354 ;; tables before searching the next table in the list.
355 (setq computed (cons t computed)))
356 (setq tables (cdr tables)))
357 (setq computed (nreverse computed))
358 ;; COMPUTED now contains the list of included tables (and
359 ;; tables included by them, etc.). Now splice this into the
361 (setcdr list (nconc computed (cdr (cdr list)))))
362 ;; It was not a valid table, so just remove the following placeholder.
363 (setcdr list (cdr (cdr list)))))))
365 ;; Expand tags table name FILE into a complete file name.
366 (defun tags-expand-table-name (file)
367 (setq file (expand-file-name file))
368 (if (file-directory-p file)
369 (expand-file-name "TAGS" file)
372 ;; Like member, but comparison is done after tags-expand-table-name on both
373 ;; sides and elements of LIST that are t are skipped.
374 (defun tags-table-list-member (file list)
375 (setq file (tags-expand-table-name file))
377 (or (eq (car list) t)
378 (not (string= file (tags-expand-table-name (car list))))))
379 (setq list (cdr list)))
382 (defun tags-verify-table (file)
383 "Read FILE into a buffer and verify that it is a valid tags table.
384 Sets the current buffer to one visiting FILE (if it exists).
385 Returns non-nil iff it is a valid table."
386 (if (get-file-buffer file)
387 ;; The file is already in a buffer. Check for the visited file
388 ;; having changed since we last used it.
390 (set-buffer (get-file-buffer file))
391 (setq win (or verify-tags-table-function (initialize-new-tags-table)))
392 (if (or (verify-visited-file-modtime (current-buffer))
393 ;; Decide whether to revert the file.
394 ;; revert-without-query can say to revert
395 ;; or the user can say to revert.
396 (not (or (let ((tail revert-without-query)
399 (if (string-match (car tail) buffer-file-name)
401 (setq tail (cdr tail)))
403 tags-revert-without-query
405 (format "Tags file %s has changed, read new contents? "
407 (and verify-tags-table-function
408 (funcall verify-tags-table-function))
410 (initialize-new-tags-table)))
411 (and (file-exists-p file)
413 (set-buffer (find-file-noselect file))
414 (or (string= file buffer-file-name)
415 ;; find-file-noselect has changed the file name.
416 ;; Propagate the change to tags-file-name and tags-table-list.
417 (let ((tail (member file tags-table-list)))
419 (setcar tail buffer-file-name))
420 (if (eq file tags-file-name)
421 (setq tags-file-name buffer-file-name))))
422 (initialize-new-tags-table)))))
424 ;; Subroutine of visit-tags-table-buffer. Search the current tags tables
425 ;; for one that has tags for THIS-FILE (or that includes a table that
426 ;; does). Return the name of the first table table listing THIS-FILE; if
427 ;; the table is one included by another table, it is the master table that
428 ;; we return. If CORE-ONLY is non-nil, check only tags tables that are
429 ;; already in buffers--don't visit any new files.
430 (defun tags-table-including (this-file core-only)
431 (let ((tables tags-table-computed-list)
433 ;; Loop over the list, looking for a table containing tags for THIS-FILE.
434 (while (and (not found)
438 ;; Skip tables not in core.
439 (while (eq (nth 1 tables) t)
440 (setq tables (cdr (cdr tables))))
441 (if (eq (nth 1 tables) t)
442 ;; This table has not been read into core yet. Read it in now.
443 (tags-table-extend-computed-list)))
446 ;; Select the tags table buffer and get the file list up to date.
447 (let ((tags-file-name (car tables)))
448 (visit-tags-table-buffer 'same)
449 (if (member this-file (mapcar 'expand-file-name
452 (setq found tables))))
453 (setq tables (cdr tables)))
455 ;; Now determine if the table we found was one included by another
456 ;; table, not explicitly listed. We do this by checking each
457 ;; element of the computed list to see if it appears in the user's
458 ;; explicit list; the last element we will check is FOUND itself.
459 ;; Then we return the last one which did in fact appear in
462 (elt tags-table-computed-list))
463 (while (not (eq elt (cdr found)))
464 (if (tags-table-list-member (car elt) tags-table-list)
465 ;; This table appears in the user's list, so it could be
466 ;; the one which includes the table we found.
467 (setq could-be (car elt)))
470 (setq elt (cdr elt))))
471 ;; The last element we found in the computed list before FOUND
472 ;; that appears in the user's list will be the table that
473 ;; included the one we found.
476 ;; Subroutine of visit-tags-table-buffer. Move tags-table-list-pointer
477 ;; along and set tags-file-name. Returns nil when out of tables.
478 (defun tags-next-table ()
479 ;; If there is a placeholder element next, compute the list to replace it.
480 (while (eq (nth 1 tags-table-list-pointer) t)
481 (tags-table-extend-computed-list))
483 ;; Go to the next table in the list.
484 (setq tags-table-list-pointer (cdr tags-table-list-pointer))
485 (or tags-table-list-pointer
487 (setq tags-table-list-pointer tags-table-computed-list))
489 (if (eq tags-table-list-pointer tags-table-list-started-at)
490 ;; We have come full circle. No more tables.
491 (setq tags-table-list-pointer nil)
492 ;; Set tags-file-name to the name from the list. It is already expanded.
493 (setq tags-file-name (car tags-table-list-pointer))))
495 (defun visit-tags-table-buffer (&optional cont)
496 "Select the buffer containing the current tags table.
497 If optional arg is a string, visit that file as a tags table.
498 If optional arg is t, visit the next table in `tags-table-list'.
499 If optional arg is the atom `same', don't look for a new table;
500 just select the buffer visiting `tags-file-name'.
501 If arg is nil or absent, choose a first buffer from information in
502 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
503 Returns t if it visits a tags table, or nil if there are no more in the list."
505 ;; Set tags-file-name to the tags table file we want to visit.
506 (cond ((eq cont 'same)
507 ;; Use the ambient value of tags-file-name.
510 (substitute-command-keys
511 (concat "No tags table in use; "
512 "use \\[visit-tags-table] to select one")))))
515 ;; Find the next table.
516 (if (tags-next-table)
517 ;; Skip over nonexistent files.
518 (while (and (not (or (get-file-buffer tags-file-name)
519 (file-exists-p tags-file-name)))
520 (tags-next-table)))))
523 ;; Pick a table out of our hat.
524 (tags-table-check-computed-list) ;Get it up to date, we might use it.
527 ;; If passed a string, use that.
531 ;; First, try a local variable.
532 (cdr (assq 'tags-file-name (buffer-local-variables)))
533 ;; Second, try a user-specified function to guess.
534 (and default-tags-table-function
535 (funcall default-tags-table-function))
536 ;; Third, look for a tags table that contains tags for the
537 ;; current buffer's file. If one is found, the lists will
538 ;; be frobnicated, and CONT will be set non-nil so we don't
540 (and buffer-file-name
542 ;; First check only tables already in buffers.
543 (tags-table-including buffer-file-name t)
544 ;; Since that didn't find any, now do the
545 ;; expensive version: reading new files.
546 (tags-table-including buffer-file-name nil)))
547 ;; Fourth, use the user variable tags-file-name, if it is
548 ;; not already in the current list.
550 (not (tags-table-list-member tags-file-name
551 tags-table-computed-list))
553 ;; Fifth, use the user variable giving the table list.
554 ;; Find the first element of the list that actually exists.
555 (let ((list tags-table-list)
558 (setq file (tags-expand-table-name (car list)))
559 (not (get-file-buffer file))
560 (not (file-exists-p file)))
561 (setq list (cdr list)))
563 ;; Finally, prompt the user for a file name.
565 (read-file-name "Visit tags table: (default TAGS) "
570 ;; Expand the table name into a full file name.
571 (setq tags-file-name (tags-expand-table-name tags-file-name))
573 (unless (and (eq cont t) (null tags-table-list-pointer))
574 ;; Verify that tags-file-name names a valid tags table.
575 ;; Bind another variable with the value of tags-file-name
576 ;; before we switch buffers, in case tags-file-name is buffer-local.
577 (let ((curbuf (current-buffer))
578 (local-tags-file-name tags-file-name))
579 (if (tags-verify-table local-tags-file-name)
581 ;; We have a valid tags table.
583 ;; Bury the tags table buffer so it
584 ;; doesn't get in the user's way.
585 (bury-buffer (current-buffer))
587 ;; If this was a new table selection (CONT is nil), make
588 ;; sure tags-table-list includes the chosen table, and
589 ;; update the list pointer variables.
591 ;; Look in the list for the table we chose.
592 (let ((found (tags-table-list-member
594 tags-table-computed-list)))
596 ;; There it is. Just switch to it.
597 (setq tags-table-list-pointer found
598 tags-table-list-started-at found)
600 ;; The table is not in the current set.
601 ;; Try to find it in another previously used set.
602 (let ((sets tags-table-set-list))
604 (not (tags-table-list-member
607 (setq sets (cdr sets)))
609 ;; Found in some other set. Switch to that set.
611 (or (memq tags-table-list tags-table-set-list)
612 ;; Save the current list.
613 (setq tags-table-set-list
614 (cons tags-table-list
615 tags-table-set-list)))
616 (setq tags-table-list (car sets)))
618 ;; Not found in any existing set.
619 (if (and tags-table-list
620 (or (eq t tags-add-tables)
623 (concat "Keep current list of "
624 "tags tables also? ")))))
625 ;; Add it to the current list.
626 (setq tags-table-list (cons local-tags-file-name
629 ;; Make a fresh list, and store the old one.
630 (message "Starting a new list of tags tables")
631 (or (null tags-table-list)
632 (memq tags-table-list tags-table-set-list)
633 (setq tags-table-set-list
634 (cons tags-table-list
635 tags-table-set-list)))
636 ;; Clear out buffers holding old tables.
637 (dolist (table tags-table-list)
638 (let ((buffer (find-buffer-visiting table)))
640 (kill-buffer buffer))))
641 (setq tags-table-list (list local-tags-file-name))))
643 ;; Recompute tags-table-computed-list.
644 (tags-table-check-computed-list)
645 ;; Set the tags table list state variables to start
646 ;; over from tags-table-computed-list.
647 (setq tags-table-list-started-at tags-table-computed-list
648 tags-table-list-pointer
649 tags-table-computed-list)))))
651 ;; Return of t says the tags table is valid.
654 ;; The buffer was not valid. Don't use it again.
656 (kill-local-variable 'tags-file-name)
657 (if (eq local-tags-file-name tags-file-name)
658 (setq tags-file-name nil))
659 (error "File %s is not a valid tags table" local-tags-file-name)))))
661 (defun tags-reset-tags-tables ()
662 "Reset tags state to cancel effect of any previous \\[visit-tags-table] or \\[find-tag]."
664 ;; Clear out the markers we are throwing away.
666 (while (< i find-tag-marker-ring-length)
667 (if (aref (cddr tags-location-ring) i)
668 (set-marker (aref (cddr tags-location-ring) i) nil))
669 (if (aref (cddr find-tag-marker-ring) i)
670 (set-marker (aref (cddr find-tag-marker-ring) i) nil))
672 (setq tags-file-name nil
673 tags-location-ring (make-ring find-tag-marker-ring-length)
674 find-tag-marker-ring (make-ring find-tag-marker-ring-length)
676 tags-table-computed-list nil
677 tags-table-computed-list-for nil
678 tags-table-list-pointer nil
679 tags-table-list-started-at nil
680 tags-table-set-list nil))
682 (defun file-of-tag ()
683 "Return the file name of the file whose tags point is within.
684 Assumes the tags table is the current buffer.
685 File name returned is relative to tags table file's directory."
686 (funcall file-of-tag-function))
689 (defun tags-table-files ()
690 "Return a list of files in the current tags table.
691 Assumes the tags table is the current buffer. The file names are returned
692 as they appeared in the `etags' command that created the table, usually
693 without directory names."
695 (setq tags-table-files
696 (funcall tags-table-files-function))))
698 (defun tags-included-tables ()
699 "Return a list of tags tables included by the current table.
700 Assumes the tags table is the current buffer."
701 (or tags-included-tables
702 (setq tags-included-tables (funcall tags-included-tables-function))))
704 ;; Build tags-completion-table on demand. The single current tags table
705 ;; and its included tags tables (and their included tables, etc.) have
706 ;; their tags included in the completion table.
707 (defun tags-completion-table ()
708 (or tags-completion-table
711 (message "Making tags completion table for %s..." buffer-file-name)
712 (let ((included (tags-included-tables))
713 (table (funcall tags-completion-table-function)))
715 ;; Iterate over the list of included tables, and combine each
716 ;; included table's completion obarray to the parent obarray.
719 (let ((tags-file-name (car included)))
720 (visit-tags-table-buffer 'same))
721 ;; Recurse in that buffer to compute its completion table.
722 (if (tags-completion-table)
723 ;; Combine the tables.
724 (mapatoms (lambda (sym) (intern (symbol-name sym) table))
725 tags-completion-table))
726 (setq included (cdr included))))
727 (setq tags-completion-table table))
728 (message "Making tags completion table for %s...done"
730 (quit (message "Tags completion table construction aborted.")
731 (setq tags-completion-table nil)))))
733 ;; Completion function for tags. Does normal try-completion,
734 ;; but builds tags-completion-table on demand.
735 (defun tags-complete-tag (string predicate what)
737 ;; If we need to ask for the tag table, allow that.
738 (let ((enable-recursive-minibuffers t))
739 (visit-tags-table-buffer))
741 (all-completions string (tags-completion-table) predicate)
742 (try-completion string (tags-completion-table) predicate))))
744 ;; Return a default tag to search for, based on the text at point.
745 (defun find-tag-default ()
747 (while (looking-at "\\sw\\|\\s_")
749 (if (or (re-search-backward "\\sw\\|\\s_"
750 (save-excursion (beginning-of-line) (point))
752 (re-search-forward "\\(\\sw\\|\\s_\\)+"
753 (save-excursion (end-of-line) (point))
755 (progn (goto-char (match-end 0))
756 (buffer-substring (point)
757 (progn (forward-sexp -1)
758 (while (looking-at "\\s'")
763 ;; Read a tag name from the minibuffer with defaulting and completion.
764 (defun find-tag-tag (string)
765 (let* ((default (funcall (or find-tag-default-function
766 (get major-mode 'find-tag-default-function)
768 (spec (completing-read (if default
769 (format "%s(default %s) " string default)
772 nil nil nil nil default)))
774 (or default (error "There is no default tag"))
778 "Last tag found by \\[find-tag].")
780 ;; Get interactive args for find-tag{-noselect,-other-window,-regexp}.
781 (defun find-tag-interactive (prompt &optional no-default)
782 (if current-prefix-arg
783 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
788 (find-tag-tag prompt)))))
790 (defvar find-tag-history nil)
794 (defvar etags-case-fold-search)
795 (defvar etags-syntax-table))
798 (defun find-tag-noselect (tagname &optional next-p regexp-p)
799 "Find tag (in current tags table) whose name contains TAGNAME.
800 Returns the buffer containing the tag's definition and moves its point there,
801 but does not select the buffer.
802 The default for TAGNAME is the expression in the buffer near point.
804 If second arg NEXT-P is t (interactively, with prefix arg), search for
805 another tag that matches the last tagname or regexp used. When there are
806 multiple matches for a tag, more exact matches are found first. If NEXT-P
807 is the atom `-' (interactively, with prefix arg that is a negative number
808 or just \\[negative-argument]), pop back to the previous tag gone to.
810 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
812 A marker representing the point when this command is onvoked is pushed
813 onto a ring and may be popped back to with \\[pop-tag-mark].
814 Contrast this with the ring of marks gone to by the command.
816 See documentation of variable `tags-file-name'."
817 (interactive (find-tag-interactive "Find tag: "))
819 (setq find-tag-history (cons tagname find-tag-history))
820 ;; Save the current buffer's value of `find-tag-hook' before selecting the
821 ;; tags table buffer.
822 (let ((local-find-tag-hook find-tag-hook))
824 ;; Pop back to a previous location.
825 (if (ring-empty-p tags-location-ring)
826 (error "No previous tag locations")
827 (let ((marker (ring-remove tags-location-ring 0)))
829 ;; Move to the saved location.
830 (set-buffer (or (marker-buffer marker)
831 (error "The marked buffer has been deleted")))
832 (goto-char (marker-position marker))
833 ;; Kill that marker so it doesn't slow down editing.
834 (set-marker marker nil nil)
835 ;; Run the user's hook. Do we really want to do this for pop?
836 (run-hooks 'local-find-tag-hook))))
837 ;; Record whence we came.
838 (ring-insert find-tag-marker-ring (point-marker))
840 ;; Find the same table we last used.
841 (visit-tags-table-buffer 'same)
842 ;; Pick a table to use.
843 (visit-tags-table-buffer)
844 ;; Record TAGNAME for a future call with NEXT-P non-nil.
845 (setq last-tag tagname))
846 ;; Record the location so we can pop back to it later.
847 (let ((marker (make-marker)))
850 ;; find-tag-in-order does the real work.
852 (if next-p last-tag tagname)
854 find-tag-regexp-search-function
855 find-tag-search-function)
857 find-tag-regexp-tag-order
860 find-tag-regexp-next-line-after-failure-p
861 find-tag-next-line-after-failure-p)
862 (if regexp-p "matching" "containing")
864 (set-marker marker (point))
865 (run-hooks 'local-find-tag-hook)
866 (ring-insert tags-location-ring marker)
867 (current-buffer))))))
870 (defun find-tag (tagname &optional next-p regexp-p)
871 "Find tag (in current tags table) whose name contains TAGNAME.
872 Select the buffer containing the tag's definition, and move point there.
873 The default for TAGNAME is the expression in the buffer around or before point.
875 If second arg NEXT-P is t (interactively, with prefix arg), search for
876 another tag that matches the last tagname or regexp used. When there are
877 multiple matches for a tag, more exact matches are found first. If NEXT-P
878 is the atom `-' (interactively, with prefix arg that is a negative number
879 or just \\[negative-argument]), pop back to the previous tag gone to.
881 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
883 A marker representing the point when this command is onvoked is pushed
884 onto a ring and may be popped back to with \\[pop-tag-mark].
885 Contrast this with the ring of marks gone to by the command.
887 See documentation of variable `tags-file-name'."
888 (interactive (find-tag-interactive "Find tag: "))
889 (switch-to-buffer (find-tag-noselect tagname next-p regexp-p)))
890 ;;;###autoload (define-key esc-map "." 'find-tag)
893 (defun find-tag-other-window (tagname &optional next-p regexp-p)
894 "Find tag (in current tags table) whose name contains TAGNAME.
895 Select the buffer containing the tag's definition in another window, and
896 move point there. The default for TAGNAME is the expression in the buffer
897 around or before point.
899 If second arg NEXT-P is t (interactively, with prefix arg), search for
900 another tag that matches the last tagname or regexp used. When there are
901 multiple matches for a tag, more exact matches are found first. If NEXT-P
902 is negative (interactively, with prefix arg that is a negative number or
903 just \\[negative-argument]), pop back to the previous tag gone to.
905 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
907 A marker representing the point when this command is onvoked is pushed
908 onto a ring and may be popped back to with \\[pop-tag-mark].
909 Contrast this with the ring of marks gone to by the command.
911 See documentation of variable `tags-file-name'."
912 (interactive (find-tag-interactive "Find tag other window: "))
914 ;; This hair is to deal with the case where the tag is found in the
915 ;; selected window's buffer; without the hair, point is moved in both
916 ;; windows. To prevent this, we save the selected window's point before
917 ;; doing find-tag-noselect, and restore it after.
918 (let* ((window-point (window-point (selected-window)))
919 (tagbuf (find-tag-noselect tagname next-p regexp-p))
920 (tagpoint (progn (set-buffer tagbuf) (point))))
921 (set-window-point (prog1
923 (switch-to-buffer-other-window tagbuf)
924 ;; We have to set this new window's point; it
925 ;; might already have been displaying a
926 ;; different portion of tagbuf, in which case
927 ;; switch-to-buffer-other-window doesn't set
928 ;; the window's point from the buffer.
929 (set-window-point (selected-window) tagpoint))
931 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
934 (defun find-tag-other-frame (tagname &optional next-p)
935 "Find tag (in current tags table) whose name contains TAGNAME.
936 Select the buffer containing the tag's definition in another frame, and
937 move point there. The default for TAGNAME is the expression in the buffer
938 around or before point.
940 If second arg NEXT-P is t (interactively, with prefix arg), search for
941 another tag that matches the last tagname or regexp used. When there are
942 multiple matches for a tag, more exact matches are found first. If NEXT-P
943 is negative (interactively, with prefix arg that is a negative number or
944 just \\[negative-argument]), pop back to the previous tag gone to.
946 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
948 A marker representing the point when this command is onvoked is pushed
949 onto a ring and may be popped back to with \\[pop-tag-mark].
950 Contrast this with the ring of marks gone to by the command.
952 See documentation of variable `tags-file-name'."
953 (interactive (find-tag-interactive "Find tag other frame: "))
954 (let ((pop-up-frames t))
955 (find-tag-other-window tagname next-p)))
956 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
959 (defun find-tag-regexp (regexp &optional next-p other-window)
960 "Find tag (in current tags table) whose name matches REGEXP.
961 Select the buffer containing the tag's definition and move point there.
963 If second arg NEXT-P is t (interactively, with prefix arg), search for
964 another tag that matches the last tagname or regexp used. When there are
965 multiple matches for a tag, more exact matches are found first. If NEXT-P
966 is negative (interactively, with prefix arg that is a negative number or
967 just \\[negative-argument]), pop back to the previous tag gone to.
969 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
971 A marker representing the point when this command is onvoked is pushed
972 onto a ring and may be popped back to with \\[pop-tag-mark].
973 Contrast this with the ring of marks gone to by the command.
975 See documentation of variable `tags-file-name'."
976 (interactive (find-tag-interactive "Find tag regexp: " t))
977 ;; We go through find-tag-other-window to do all the display hair there.
978 (funcall (if other-window 'find-tag-other-window 'find-tag)
980 ;;;###autoload (define-key esc-map [?\C-.] 'find-tag-regexp)
982 ;;;###autoload (define-key esc-map "*" 'pop-tag-mark)
985 (defun pop-tag-mark ()
986 "Pop back to where \\[find-tag] was last invoked.
988 This is distinct from invoking \\[find-tag] with a negative argument
989 since that pops a stack of markers at which tags were found, not from
990 where they were found."
992 (if (ring-empty-p find-tag-marker-ring)
993 (error "No previous locations for find-tag invocation"))
994 (let ((marker (ring-remove find-tag-marker-ring 0)))
995 (switch-to-buffer (or (marker-buffer marker)
996 (error "The marked buffer has been deleted")))
997 (goto-char (marker-position marker))
998 (set-marker marker nil nil)))
1000 ;; Internal tag finding function.
1002 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
1003 ;; any member of the function list ORDER (third arg). If ORDER is nil,
1004 ;; use saved state to continue a previous search.
1006 ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
1007 ;; an error message.
1009 ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
1010 ;; point should be moved to the next line.
1012 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
1013 ;; beginning of tags file, and perform inner loop: for each naive match for
1014 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
1015 ;; qualifier-func. If it qualifies, go to the specified line in the
1016 ;; specified source file and return. Qualified matches are remembered to
1017 ;; avoid repetition. State is saved so that the loop can be continued.
1019 (defvar tag-lines-already-matched nil) ;matches remembered here between calls
1021 (defun find-tag-in-order (pattern
1024 next-line-after-failure-p
1027 (let (file ;name of file containing tag
1028 tag-info ;where to find the tag in FILE
1031 (match-marker (make-marker))
1033 (case-fold-search (if (memq tags-case-fold-search '(nil t))
1034 tags-case-fold-search
1040 ;; This is the start of a search for a fresh tag.
1041 ;; Clear the list of tags matched by the previous search.
1042 ;; find-tag-noselect has already put us in the first tags table
1043 ;; buffer before we got called.
1044 (setq tag-lines-already-matched nil)
1045 ;; Continuing to search for the tag specified last time.
1046 ;; tag-lines-already-matched lists locations matched in previous
1047 ;; calls so we don't visit the same tag twice if it matches twice
1048 ;; during two passes with different qualification predicates.
1049 ;; Switch to the current tags table buffer.
1050 (visit-tags-table-buffer 'same))
1052 ;; Get a qualified match.
1053 (catch 'qualified-match-found
1055 ;; Iterate over the list of tags tables.
1056 (while (or first-table
1057 (visit-tags-table-buffer t))
1059 (and first-search first-table
1060 ;; Start at beginning of tags file.
1061 (goto-char (point-min)))
1063 (setq first-table nil)
1065 ;; Iterate over the list of ordering predicates.
1067 (while (funcall search-forward-func pattern nil t)
1068 ;; Naive match found. Qualify the match.
1069 (and (funcall (car order) pattern)
1070 ;; Make sure it is not a previous qualified match.
1071 (not (member (set-marker match-marker (save-excursion
1074 tag-lines-already-matched))
1075 (throw 'qualified-match-found nil))
1076 (if next-line-after-failure-p
1078 ;; Try the next flavor of match.
1079 (setq order (cdr order))
1080 (goto-char (point-min)))
1081 (setq order tag-order))
1082 ;; We throw out on match, so only get here if there were no matches.
1083 ;; Clear out the markers we use to avoid duplicate matches so they
1084 ;; don't slow down editting and are immediately available for GC.
1085 (while tag-lines-already-matched
1086 (set-marker (car tag-lines-already-matched) nil nil)
1087 (setq tag-lines-already-matched (cdr tag-lines-already-matched)))
1088 (set-marker match-marker nil nil)
1089 (error "No %stags %s %s" (if first-search "" "more ")
1092 ;; Found a tag; extract location info.
1094 (setq tag-lines-already-matched (cons match-marker
1095 tag-lines-already-matched))
1096 ;; Expand the filename, using the tags table buffer's default-directory.
1097 (setq file (expand-file-name (file-of-tag))
1098 tag-info (funcall snarf-tag-function))
1100 ;; Get the local value in the tags table buffer before switching buffers.
1101 (setq goto-func goto-tag-location-function)
1103 ;; Find the right line in the specified file.
1104 (set-buffer (find-file-noselect file))
1107 (funcall goto-func tag-info)
1109 ;; Return the buffer where the tag was found.
1112 ;; `etags' TAGS file format support.
1114 ;; If the current buffer is a valid etags TAGS file, give it local values of
1115 ;; the tags table format variables, and return non-nil.
1116 (defun etags-recognize-tags-table ()
1117 (and (etags-verify-tags-table)
1118 ;; It is annoying to flash messages on the screen briefly,
1119 ;; and this message is not useful. -- rms
1120 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
1121 (mapc (lambda (elt) (set (make-local-variable (car elt)) (cdr elt)))
1122 '((file-of-tag-function . etags-file-of-tag)
1123 (tags-table-files-function . etags-tags-table-files)
1124 (tags-completion-table-function . etags-tags-completion-table)
1125 (snarf-tag-function . etags-snarf-tag)
1126 (goto-tag-location-function . etags-goto-tag-location)
1127 (find-tag-regexp-search-function . re-search-forward)
1128 (find-tag-regexp-tag-order . (tag-re-match-p))
1129 (find-tag-regexp-next-line-after-failure-p . t)
1130 (find-tag-search-function . search-forward)
1131 (find-tag-tag-order . (tag-exact-file-name-match-p
1136 (find-tag-next-line-after-failure-p . nil)
1137 (list-tags-function . etags-list-tags)
1138 (tags-apropos-function . etags-tags-apropos)
1139 (tags-included-tables-function . etags-tags-included-tables)
1140 (verify-tags-table-function . etags-verify-tags-table)
1143 ;; Return non-nil iff the current buffer is a valid etags TAGS file.
1144 (defun etags-verify-tags-table ()
1145 ;; Use eq instead of = in case char-after returns nil.
1146 (eq (char-after 1) ?\f))
1148 (defun etags-file-of-tag ()
1150 (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n")
1151 (expand-file-name (buffer-substring (match-beginning 1) (match-end 1))
1152 (file-truename default-directory))))
1155 (defun etags-tags-completion-table ()
1156 (let ((table (make-vector 511 0)))
1158 (goto-char (point-min))
1159 ;; This monster regexp matches an etags tag line.
1160 ;; \1 is the string to match;
1161 ;; \2 is not interesting;
1162 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
1163 ;; \4 is not interesting;
1164 ;; \5 is the explicitly-specified tag name.
1165 ;; \6 is the line to start searching at;
1166 ;; \7 is the char to start searching at.
1167 (while (re-search-forward
1168 "^\\(\\([^\177]+[^-a-zA-Z0-9_+*$:\177]+\\)?\
1169 \\([-a-zA-Z0-9_+*$?:]+\\)[^-a-zA-Z0-9_+*$?:\177]*\\)\177\
1170 \\(\\([^\n\001]+\\)\001\\)?\\([0-9]+\\)?,\\([0-9]+\\)?\n"
1172 (intern (if (match-beginning 5)
1173 ;; There is an explicit tag name.
1174 (buffer-substring (match-beginning 5) (match-end 5))
1175 ;; No explicit tag name. Best guess.
1176 (buffer-substring (match-beginning 3) (match-end 3)))
1180 (defun etags-snarf-tag ()
1181 (let (tag-text line startpos)
1184 (looking-at "\f\n"))
1185 ;; The match was for a source file name, not any tag within a file.
1186 ;; Give text of t, meaning to go exactly to the location we specify,
1187 ;; the beginning of the file.
1192 ;; Find the end of the tag and record the whole tag text.
1193 (search-forward "\177")
1194 (setq tag-text (buffer-substring (1- (point))
1195 (save-excursion (beginning-of-line)
1197 ;; Skip explicit tag name if present.
1198 (search-forward "\001" (save-excursion (forward-line 1) (point)) t)
1199 (if (looking-at "[0-9]")
1200 (setq line (string-to-int (buffer-substring
1202 (progn (skip-chars-forward "0-9")
1204 (search-forward ",")
1205 (if (looking-at "[0-9]")
1206 (setq startpos (string-to-int (buffer-substring
1208 (progn (skip-chars-forward "0-9")
1210 ;; Leave point on the next line of the tags file.
1212 (cons tag-text (cons line startpos))))
1214 ;; TAG-INFO is a cons (TEXT LINE . POSITION) where TEXT is the initial part
1215 ;; of a line containing the tag and POSITION is the character position of
1216 ;; TEXT within the file (starting from 1); LINE is the line number. If
1217 ;; TEXT is t, it means the tag refers to exactly LINE or POSITION
1218 ;; (whichever is present, LINE having preference, no searching. Either
1219 ;; LINE or POSITION may be nil; POSITION is used if present. If the tag
1220 ;; isn't exactly at the given position then look around that position using
1221 ;; a search window which expands until it hits the start of file.
1222 (defun etags-goto-tag-location (tag-info)
1223 (let ((startpos (cdr (cdr tag-info)))
1224 (line (car (cdr tag-info)))
1226 (if (eq (car tag-info) t)
1228 (cond (line (goto-line line))
1229 (startpos (goto-char startpos))
1230 (t (error "etags.el BUG: bogus direct file tag")))
1231 ;; This constant is 1/2 the initial search window.
1232 ;; There is no sense in making it too small,
1233 ;; since just going around the loop once probably
1234 ;; costs about as much as searching 2000 chars.
1237 pat (concat (if (eq selective-display t)
1238 "\\(^\\|\^m\\)" "^")
1239 (regexp-quote (car tag-info))))
1240 ;; The character position in the tags table is 0-origin.
1241 ;; Convert it to a 1-origin Emacs character position.
1242 (if startpos (setq startpos (1+ startpos)))
1243 ;; If no char pos was given, try the given line number.
1246 (setq startpos (progn (goto-line line)
1249 (setq startpos (point-min)))
1250 ;; First see if the tag is right at the specified location.
1251 (goto-char startpos)
1252 (setq found (looking-at pat))
1253 (while (and (not found)
1255 (goto-char (- startpos offset))
1258 (re-search-forward pat (+ startpos offset) t)
1259 offset (* 3 offset))) ; expand search window
1261 (re-search-forward pat nil t)
1262 (error "Rerun etags: `%s' not found in %s"
1263 pat buffer-file-name)))
1264 ;; Position point at the right place
1265 ;; if the search string matched an extra Ctrl-m at the beginning.
1266 (and (eq selective-display t)
1269 (beginning-of-line)))
1271 (defun etags-list-tags (file)
1273 (when (search-forward (concat "\f\n" file ",") nil t)
1275 (while (not (or (eobp) (looking-at "\f")))
1276 (let ((tag (buffer-substring (point)
1277 (progn (skip-chars-forward "^\177")
1279 (props `(action find-tag-other-window mouse-face highlight
1280 face ,tags-tag-face))
1281 (pt (with-current-buffer standard-output (point))))
1282 (when (looking-at "[^\n]+\001")
1283 ;; There is an explicit tag name; use that.
1284 (setq tag (buffer-substring (1+ (point)) ; skip \177
1285 (progn (skip-chars-forward "^\001")
1288 (when (= (aref tag 0) ?\() (princ " ...)"))
1289 (add-text-properties pt (with-current-buffer standard-output (point))
1290 (cons 'item (cons tag props)) standard-output))
1295 (defmacro tags-with-face (face &rest body)
1296 "Execute BODY, give output to `standard-output' face FACE."
1297 (let ((pp (gensym "twf-")))
1298 `(let ((,pp (with-current-buffer standard-output (point))))
1300 (put-text-property ,pp (with-current-buffer standard-output (point))
1301 'face ,face standard-output))))
1303 (defun etags-tags-apropos-additional (regexp)
1304 "Display tags matching REGEXP from `tags-apropos-additional-actions'."
1305 (with-current-buffer standard-output
1306 (dolist (oba tags-apropos-additional-actions)
1308 (tags-with-face 'highlight (princ (car oba)))
1310 (let* ((props `(action ,(cadr oba) mouse-face highlight face
1313 (symbs (car (cddr oba)))
1314 (ins-symb (lambda (sy)
1315 (let ((sn (symbol-name sy)))
1316 (when (string-match regexp sn)
1317 (add-text-properties (point)
1318 (progn (princ sy) (point))
1319 (cons 'item (cons sn props)))
1321 (when (symbolp symbs)
1323 (setq symbs (symbol-value symbs))
1324 (insert "symbol `" (symbol-name symbs) "' has no value\n")
1327 (mapatoms ins-symb symbs)
1329 (funcall ins-symb (car sy))))
1330 (sort-lines nil beg (point))))))
1332 (defun etags-tags-apropos (string)
1333 (when tags-apropos-verbose
1334 (princ "Tags in file `")
1335 (tags-with-face 'highlight (princ buffer-file-name))
1338 (while (re-search-forward string nil t)
1340 (let ((tag (buffer-substring (point)
1341 (progn (skip-chars-forward "^\177")
1343 (props `(action find-tag-other-window mouse-face highlight
1344 face ,tags-tag-face))
1345 (pt (with-current-buffer standard-output (point))))
1347 (when (= (aref tag 0) ?\() (princ " ...)"))
1348 (add-text-properties pt (with-current-buffer standard-output (point))
1349 `(item ,tag ,@props) standard-output))
1352 (when tags-apropos-verbose (princ "\n")))
1354 (defun etags-tags-table-files ()
1357 (goto-char (point-min))
1358 (while (search-forward "\f\n" nil t)
1361 (skip-chars-backward "^," beg)
1362 (or (looking-at "include$")
1363 (setq files (cons (buffer-substring beg (1- (point))) files))))
1366 (defun etags-tags-included-tables ()
1369 (goto-char (point-min))
1370 (while (search-forward "\f\n" nil t)
1373 (skip-chars-backward "^," beg)
1374 (if (looking-at "include$")
1375 ;; Expand in the default-directory of the tags table buffer.
1376 (setq files (cons (expand-file-name (buffer-substring beg (1- (point))))
1380 ;; Empty tags file support.
1382 ;; Recognize an empty file and give it local values of the tags table format
1383 ;; variables which do nothing.
1384 (defun tags-recognize-empty-tags-table ()
1385 (and (zerop (buffer-size))
1386 (mapc (lambda (sym) (set (make-local-variable sym) 'ignore))
1387 '(tags-table-files-function
1388 tags-completion-table-function
1389 find-tag-regexp-search-function
1390 find-tag-search-function
1391 tags-apropos-function
1392 tags-included-tables-function))
1393 (set (make-local-variable 'verify-tags-table-function)
1394 (lambda () (zerop (buffer-size))))))
1396 ;; Match qualifier functions for tagnames.
1397 ;; XXX these functions assume etags file format.
1399 ;; This might be a neat idea, but it's too hairy at the moment.
1400 ;;(defmacro tags-with-syntax (&rest body)
1401 ;; `(let ((current (current-buffer))
1402 ;; (otable (syntax-table))
1403 ;; (buffer (find-file-noselect (file-of-tag)))
1407 ;; (set-buffer buffer)
1408 ;; (setq table (syntax-table))
1409 ;; (set-buffer current)
1410 ;; (set-syntax-table table)
1412 ;; (set-syntax-table otable))))
1413 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
1415 ;; t if point is at a tag line that matches TAG exactly.
1416 ;; point should be just after a string that matches TAG.
1417 (defun tag-exact-match-p (tag)
1418 ;; The match is really exact if there is an explicit tag name.
1419 (or (and (eq (char-after (point)) ?\001)
1420 (eq (char-after (- (point) (length tag) 1)) ?\177))
1421 ;; We are not on the explicit tag name, but perhaps it follows.
1422 (looking-at (concat "[^\177\n]*\177" (regexp-quote tag) "\001"))))
1424 ;; t if point is at a tag line that matches TAG as a symbol.
1425 ;; point should be just after a string that matches TAG.
1426 (defun tag-symbol-match-p (tag)
1427 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1429 (backward-char (1+ (length tag)))
1430 (and (looking-at "\\Sw") (looking-at "\\S_")))))
1432 ;; t if point is at a tag line that matches TAG as a word.
1433 ;; point should be just after a string that matches TAG.
1434 (defun tag-word-match-p (tag)
1435 (and (looking-at "\\b.*\177")
1436 (save-excursion (backward-char (length tag))
1437 (looking-at "\\b"))))
1439 (defun tag-exact-file-name-match-p (tag)
1440 (and (looking-at ",")
1441 (save-excursion (backward-char (length tag))
1442 (looking-at "\f\n"))))
1444 ;; t if point is in a tag line with a tag containing TAG as a substring.
1445 (defun tag-any-match-p (tag)
1446 (looking-at ".*\177"))
1448 ;; t if point is at a tag line that matches RE as a regexp.
1449 (defun tag-re-match-p (re)
1452 (let ((bol (point)))
1453 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t)
1454 (re-search-backward re bol t)))))
1456 (defcustom tags-loop-revert-buffers nil
1457 "*Non-nil means tags-scanning loops should offer to reread changed files.
1458 These loops normally read each file into Emacs, but when a file
1459 is already visited, they use the existing buffer.
1460 When this flag is non-nil, they offer to revert the existing buffer
1461 in the case where the file has changed since you visited it."
1466 (defun next-file (&optional initialize novisit)
1467 "Select next file among files in current tags table.
1469 A first argument of t (prefix arg, if interactive) initializes to the
1470 beginning of the list of files in the tags table. If the argument is
1471 neither nil nor t, it is evalled to initialize the list of files.
1473 Non-nil second argument NOVISIT means use a temporary buffer
1474 to save time and avoid uninteresting warnings.
1476 Value is nil if the file was already visited;
1477 if the file was newly read in, the value is the filename."
1478 ;; Make the interactive arg t if there was any prefix arg.
1479 (interactive (list (if current-prefix-arg t)))
1480 (cond ((not initialize)
1481 ;; Not the first run.
1484 ;; Initialize the list from the tags table.
1486 ;; Visit the tags table buffer to get its list of files.
1487 (visit-tags-table-buffer)
1488 ;; Copy the list so we can setcdr below, and expand the file
1489 ;; names while we are at it, in this buffer's default directory.
1490 (setq next-file-list (mapcar 'expand-file-name (tags-table-files)))
1491 ;; Iterate over all the tags table files, collecting
1492 ;; a complete list of referenced file names.
1493 (while (visit-tags-table-buffer t)
1494 ;; Find the tail of the working list and chain on the new
1495 ;; sublist for this tags table.
1496 (let ((tail next-file-list))
1498 (setq tail (cdr tail)))
1499 ;; Use a copy so the next loop iteration will not modify the
1500 ;; list later returned by (tags-table-files).
1502 (setcdr tail (mapcar 'expand-file-name (tags-table-files)))
1503 (setq next-file-list (mapcar 'expand-file-name
1504 (tags-table-files))))))))
1506 ;; Initialize the list by evalling the argument.
1507 (setq next-file-list (eval initialize))))
1508 (unless next-file-list
1510 (get-buffer " *next-file*")
1511 (kill-buffer " *next-file*"))
1512 (error "All files processed"))
1513 (let* ((next (car next-file-list))
1514 (buffer (get-file-buffer next))
1516 ;; Advance the list before trying to find the file.
1517 ;; If we get an error finding the file, don't get stuck on it.
1518 (setq next-file-list (cdr next-file-list))
1519 ;; Optionally offer to revert buffers
1520 ;; if the files have changed on disk.
1521 (and buffer tags-loop-revert-buffers
1522 (not (verify-visited-file-modtime buffer))
1523 (with-current-buffer buffer
1525 (if (not (and new novisit))
1526 (set-buffer (find-file-noselect next novisit))
1527 ;; Like find-file, but avoids random warning messages.
1528 (set-buffer (get-buffer-create " *next-file*"))
1529 (kill-all-local-variables)
1532 (insert-file-contents new nil))
1535 (defvar tags-loop-operate nil
1536 "Form for `tags-loop-continue' to eval to change one file.")
1538 (defvar tags-loop-scan
1540 (substitute-command-keys
1541 "No \\[tags-search] or \\[tags-query-replace] in progress"))
1542 "Form for `tags-loop-continue' to eval to scan one file.
1543 If it returns non-nil, this file needs processing by evalling
1544 \`tags-loop-operate'. Otherwise, move on to the next file.")
1546 (defun tags-loop-eval (form)
1547 "Evaluate FORM and return its result.
1548 Bind `case-fold-search' during the evaluation, depending on the value of
1549 `tags-case-fold-search'."
1550 (let ((case-fold-search (if (memq tags-case-fold-search '(t nil))
1551 tags-case-fold-search
1557 (defun tags-loop-continue (&optional first-time)
1558 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1559 Used noninteractively with non-nil argument to begin such a command (the
1560 argument is passed to `next-file', which see).
1562 Two variables control the processing we do on each file: the value of
1563 `tags-loop-scan' is a form to be executed on each file to see if it is
1564 interesting (it returns non-nil if so) and `tags-loop-operate' is a form to
1565 evaluate to operate on an interesting file. If the latter evaluates to
1566 nil, we exit; otherwise we scan the next file."
1569 ;; Non-nil means we have finished one file
1570 ;; and should not scan it again.
1575 ;; Scan files quickly for the first or next interesting one.
1576 (while (or first-time file-finished
1579 (not (tags-loop-eval tags-loop-scan))))
1580 (setq file-finished nil)
1581 (setq new (next-file first-time t))
1582 ;; If NEW is non-nil, we got a temp buffer,
1583 ;; and NEW is the file name.
1585 (and (not first-time)
1586 (> baud-rate search-slow-speed)
1588 (message "Scanning file %s..." (or new buffer-file-name)))
1589 (setq first-time nil)
1590 (goto-char (point-min)))
1592 ;; If we visited it in a temp buffer, visit it now for real.
1594 (let ((pos (point)))
1596 (set-buffer (find-file-noselect new))
1597 (setq new nil) ;No longer in a temp buffer.
1601 (switch-to-buffer (current-buffer))
1603 ;; Now operate on the file.
1604 ;; If value is non-nil, continue to scan the next file.
1605 (tags-loop-eval tags-loop-operate))
1606 (setq file-finished t))
1608 (null tags-loop-operate)
1609 (message "Scanning file %s...found" buffer-file-name))))
1610 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
1613 (defun tags-search (regexp &optional file-list-form)
1614 "Search through all files listed in tags table for match for REGEXP.
1615 Stops when a match is found.
1616 To continue searching for next match, use command \\[tags-loop-continue].
1618 See documentation of variable `tags-file-name'."
1619 (interactive "sTags search (regexp): ")
1620 (if (and (equal regexp "")
1621 (eq (car tags-loop-scan) 're-search-forward)
1622 (null tags-loop-operate))
1623 ;; Continue last tags-search as if by M-,.
1624 (tags-loop-continue nil)
1625 (setq tags-loop-scan
1626 (list 're-search-forward (list 'quote regexp) nil t)
1627 tags-loop-operate nil)
1628 (tags-loop-continue (or file-list-form t))))
1631 (defun tags-query-replace (from to &optional delimited start end file-list-form)
1632 "Query-replace-regexp FROM with TO through all files listed in tags table.
1633 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1634 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
1635 with the command \\[tags-loop-continue].
1637 See documentation of variable `tags-file-name'."
1638 (interactive (query-replace-read-args "Tags query replace (regexp)" t))
1639 (setq tags-loop-scan (list 'prog1
1640 (list 'if (list 're-search-forward
1641 (list 'quote from) nil t)
1642 ;; When we find a match, move back
1643 ;; to the beginning of it so perform-replace
1645 '(goto-char (match-beginning 0))))
1646 tags-loop-operate (list 'perform-replace
1647 (list 'quote from) (list 'quote to) nil nil
1648 t t (list 'quote delimited)))
1649 (tags-loop-continue (or file-list-form t)))
1651 (defun tags-complete-tags-table-file (string predicate what)
1653 ;; If we need to ask for the tag table, allow that.
1654 (let ((enable-recursive-minibuffers t))
1655 (visit-tags-table-buffer))
1657 (all-completions string (mapcar 'list (tags-table-files))
1659 (try-completion string (mapcar 'list (tags-table-files))
1663 (defun list-tags (file &optional next-match)
1664 "Display list of tags in file FILE.
1665 This searches only the first table in the list, and no included tables.
1666 FILE should be as it appeared in the `etags' command, usually without a
1667 directory specification."
1668 (interactive (list (completing-read "List tags in file: "
1669 'tags-complete-tags-table-file
1671 (with-output-to-temp-buffer "*Tags List*"
1672 (princ "Tags in file `")
1673 (tags-with-face 'highlight (princ file))
1676 (let ((first-time t)
1678 (while (visit-tags-table-buffer (not first-time))
1679 (setq first-time nil)
1680 (if (funcall list-tags-function file)
1683 (error "File %s not in current tags tables" file)))))
1684 (with-current-buffer "*Tags List*"
1685 (setq buffer-read-only t)
1689 (defun tags-apropos (regexp)
1690 "Display list of all tags in tags table REGEXP matches."
1691 (interactive "sTags apropos (regexp): ")
1692 (with-output-to-temp-buffer "*Tags List*"
1693 (princ "Click mouse-2 to follow tags.\n\nTags matching regexp `")
1694 (tags-with-face 'highlight (princ regexp))
1697 (let ((first-time t))
1698 (while (visit-tags-table-buffer (not first-time))
1699 (setq first-time nil)
1700 (funcall tags-apropos-function regexp))))
1701 (etags-tags-apropos-additional regexp))
1702 (with-current-buffer "*Tags List*"
1703 (setq buffer-read-only t)
1706 ;;; XXX Kludge interface.
1708 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1710 (defun select-tags-table ()
1711 "Select a tags table file from a menu of those you have already used.
1712 The list of tags tables to select from is stored in `tags-table-set-list';
1713 see the doc of that variable if you want to add names to the list."
1715 (pop-to-buffer "*Tags Table List*")
1716 (setq buffer-read-only nil)
1718 (let ((set-list tags-table-set-list)
1719 (desired-point nil))
1720 (when tags-table-list
1721 (setq desired-point (point-marker))
1722 (princ tags-table-list (current-buffer))
1724 (prin1 (car tags-table-list) (current-buffer)) ;invisible
1727 (unless (eq (car set-list) tags-table-list)
1728 (princ (car set-list) (current-buffer))
1730 (prin1 (car (car set-list)) (current-buffer)) ;invisible
1732 (setq set-list (cdr set-list)))
1733 (when tags-file-name
1735 (setq desired-point (point-marker)))
1736 (insert tags-file-name "\C-m")
1737 (prin1 tags-file-name (current-buffer)) ;invisible
1739 (setq set-list (delete tags-file-name
1740 (apply 'nconc (cons (copy-sequence tags-table-list)
1741 (mapcar 'copy-sequence
1742 tags-table-set-list)))))
1744 (insert (car set-list) "\C-m")
1745 (prin1 (car set-list) (current-buffer)) ;invisible
1747 (setq set-list (delete (car set-list) set-list)))
1749 (insert-before-markers
1750 "Type `t' to select a tags table or set of tags tables:\n\n")
1752 (goto-char desired-point))
1753 (set-window-start (selected-window) 1 t))
1754 (set-buffer-modified-p nil)
1755 (select-tags-table-mode))
1757 (defvar select-tags-table-mode-map)
1758 (let ((map (make-sparse-keymap)))
1759 (define-key map "t" 'select-tags-table-select)
1760 (define-key map " " 'next-line)
1761 (define-key map "\^?" 'previous-line)
1762 (define-key map "n" 'next-line)
1763 (define-key map "p" 'previous-line)
1764 (define-key map "q" 'select-tags-table-quit)
1765 (setq select-tags-table-mode-map map))
1767 (defun select-tags-table-mode ()
1768 "Major mode for choosing a current tags table among those already loaded.
1770 \\{select-tags-table-mode-map}"
1772 (kill-all-local-variables)
1773 (setq buffer-read-only t
1774 major-mode 'select-tags-table-mode
1775 mode-name "Select Tags Table")
1776 (use-local-map select-tags-table-mode-map)
1777 (setq selective-display t
1778 selective-display-ellipses nil))
1780 (defun select-tags-table-select ()
1781 "Select the tags table named on this line."
1783 (search-forward "\C-m")
1784 (let ((name (read (current-buffer))))
1785 (visit-tags-table name)
1786 (select-tags-table-quit)
1787 (message "Tags table now %s" name)))
1789 (defun select-tags-table-quit ()
1790 "Kill the buffer and delete the selected window."
1792 (quit-window t (selected-window)))
1794 ;;; Note, there is another definition of this function in bindings.el.
1796 (defun complete-tag ()
1797 "Perform tags completion on the text around point.
1798 Completes to the set of names listed in the current tags table.
1799 The string to complete is chosen in the same way as the default
1800 for \\[find-tag] (which see)."
1805 (substitute-command-keys
1806 "No tags table loaded; try \\[visit-tags-table]")))
1807 (let ((pattern (funcall (or find-tag-default-function
1808 (get major-mode 'find-tag-default-function)
1809 'find-tag-default)))
1813 (error "Nothing to complete"))
1814 (search-backward pattern)
1816 (forward-char (length pattern))
1817 (setq completion (tags-complete-tag pattern nil nil))
1818 (cond ((eq completion t))
1820 (message "Can't find completion for \"%s\"" pattern)
1822 ((not (string= pattern completion))
1823 (delete-region beg (point))
1824 (insert completion))
1826 (message "Making completion list...")
1827 (with-output-to-temp-buffer "*Completions*"
1828 (display-completion-list
1829 (all-completions pattern 'tags-complete-tag nil)))
1830 (message "Making completion list...%s" "done")))))
1832 (dolist (x '("^No tags table in use; use .* to select one$"
1833 "^There is no default tag$"
1834 "^No previous tag locations$"
1835 "^File .* is not a valid tags table$"
1836 "^No \\(more \\|\\)tags \\(matching\\|containing\\) "
1837 "^Rerun etags: `.*' not found in "
1838 "^All files processed$"
1839 "^No .* or .* in progress$"
1840 "^File .* not in current tags tables$"
1841 "^No tags table loaded"
1842 "^Nothing to complete$"))
1843 (add-to-list 'debug-ignored-errors x))
1847 ;;; etags.el ends here