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>
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 the
23 ;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
24 ;; Boston, MA 02111-1307, USA.
31 (eval-when-compile (require 'cl
)) ; for `gensym'
34 (defvar tags-file-name nil
35 "*File name of tags table.
36 To switch to a new tags table, setting this variable is sufficient.
37 If you set this variable, do not also set `tags-table-list'.
38 Use the `etags' program to make a tags table file.")
39 ;; Make M-x set-variable tags-file-name like M-x visit-tags-table.
40 ;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ")
42 (defgroup etags nil
"Tags tables"
46 (defcustom tags-case-fold-search
'default
47 "*Whether tags operations should be case-sensitive.
48 A value of t means case-insensitive, a value of nil means case-sensitive.
49 Any other value means use the setting of `case-fold-search'."
51 :type
'(choice (const :tag
"Case-sensitive" nil
)
52 (const :tag
"Case-insensitive" t
)
53 (other :tag
"Use default" default
))
57 ;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
58 (defcustom tags-table-list nil
59 "*List of file names of tags tables to search.
60 An element that is a directory means the file \"TAGS\" in that directory.
61 To switch to a new list of tags tables, setting this variable is sufficient.
62 If you set this variable, do not also set `tags-file-name'.
63 Use the `etags' program to make a tags table file."
68 (defcustom tags-add-tables
'ask-user
69 "*Control whether to add a new tags table to the current list.
70 t means do; nil means don't (always start a new list).
71 Any other value means ask the user whether to add a new tags table
72 to the current list (as opposed to starting a new list)."
74 :type
'(choice (const :tag
"Do" t
)
75 (const :tag
"Don't" nil
)
76 (other :tag
"Ask" ask-user
)))
78 (defcustom tags-revert-without-query nil
79 "*Non-nil means reread a TAGS table without querying, if it has changed."
83 (defvar tags-table-computed-list nil
84 "List of tags tables to search, computed from `tags-table-list'.
85 This includes tables implicitly included by other tables. The list is not
86 always complete: the included tables of a table are not known until that
87 table is read into core. An element that is t is a placeholder
88 indicating that the preceding element is a table that has not been read
89 into core and might contain included tables to search.
90 See `tags-table-check-computed-list'.")
92 (defvar tags-table-computed-list-for nil
93 "Value of `tags-table-list' that `tags-table-computed-list' corresponds to.
94 If `tags-table-list' changes, `tags-table-computed-list' is thrown away and
95 recomputed; see `tags-table-check-computed-list'.")
97 (defvar tags-table-list-pointer nil
98 "Pointer into `tags-table-computed-list' for the current state of searching.
99 Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
101 (defvar tags-table-list-started-at nil
102 "Pointer into `tags-table-computed-list', where the current search started.")
104 (defvar tags-table-set-list nil
105 "List of sets of tags table which have been used together in the past.
106 Each element is a list of strings which are file names.")
109 (defcustom find-tag-hook nil
110 "*Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'.
111 The value in the buffer in which \\[find-tag] is done is used,
112 not the value in the buffer \\[find-tag] goes to."
117 (defcustom find-tag-default-function nil
118 "*A function of no arguments used by \\[find-tag] to pick a default tag.
119 If nil, and the symbol that is the value of `major-mode'
120 has a `find-tag-default-function' property (see `put'), that is used.
121 Otherwise, `find-tag-default' is used."
125 (defcustom find-tag-marker-ring-length
16
126 "*Length of marker rings `find-tag-marker-ring' and `tags-location-ring'."
131 (defcustom tags-tag-face
'default
132 "*Face for tags in the output of `tags-apropos'."
137 (defcustom tags-apropos-verbose nil
138 "If non-nil, print the name of the tags file in the *Tags List* buffer."
143 (defcustom tags-apropos-additional-actions nil
144 "Specify additional actions for `tags-apropos'.
146 If non-nil, value should be a list of triples (TITLE FUNCTION
147 TO-SEARCH). For each triple, `tags-apropos' processes TO-SEARCH and
148 lists tags from it. TO-SEARCH should be an alist, obarray, or symbol.
149 If it is a symbol, the symbol's value is used.
150 TITLE, a string, is a title used to label the additional list of tags.
151 FUNCTION is a function to call when a symbol is selected in the
152 *Tags List* buffer. It will be called with one argument SYMBOL which
153 is the symbol being selected.
157 '((\"Emacs Lisp\" Info-goto-emacs-command-node obarray)
158 (\"Common Lisp\" common-lisp-hyperspec common-lisp-hyperspec-obarray)
159 (\"SCWM\" scwm-documentation scwm-obarray))"
164 (defvar find-tag-marker-ring
(make-ring find-tag-marker-ring-length
)
165 "Ring of markers which are locations from which \\[find-tag] was invoked.")
167 (defvar default-tags-table-function nil
168 "If non-nil, a function to choose a default tags file for a buffer.
169 This function receives no arguments and should return the default
170 tags table file to use for the current buffer.")
172 (defvar tags-location-ring
(make-ring find-tag-marker-ring-length
)
173 "Ring of markers which are locations visited by \\[find-tag].
174 Pop back to the last location with \\[negative-argument] \\[find-tag].")
177 ;; These variables are local in tags table buffers.
179 (defvar tags-table-files nil
180 "List of file names covered by current tags table.
181 nil means it has not yet been computed; use `tags-table-files' to do so.")
183 (defvar tags-completion-table nil
184 "Obarray of tag names defined in current tags table.")
186 (defvar tags-included-tables nil
187 "List of tags tables included by the current tags table.")
189 (defvar next-file-list nil
190 "List of files for \\[next-file] to process.")
192 ;; Hooks for file formats.
194 (defvar tags-table-format-functions
'(etags-recognize-tags-table
195 tags-recognize-empty-tags-table
)
196 "Hook to be called in a tags table buffer to identify the type of tags table.
197 The functions are called in order, with no arguments,
198 until one returns non-nil. The function should make buffer-local bindings
199 of the format-parsing tags function variables if successful.")
201 (defvar file-of-tag-function nil
202 "Function to do the work of `file-of-tag' (which see).")
203 (defvar tags-table-files-function nil
204 "Function to do the work of `tags-table-files' (which see).")
205 (defvar tags-completion-table-function nil
206 "Function to build the `tags-completion-table'.")
207 (defvar snarf-tag-function nil
208 "Function to get info about a matched tag for `goto-tag-location-function'.")
209 (defvar goto-tag-location-function nil
210 "Function of to go to the location in the buffer specified by a tag.
211 One argument, the tag info returned by `snarf-tag-function'.")
212 (defvar find-tag-regexp-search-function nil
213 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
214 (defvar find-tag-regexp-tag-order nil
215 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
216 (defvar find-tag-regexp-next-line-after-failure-p nil
217 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
218 (defvar find-tag-search-function nil
219 "Search function passed to `find-tag-in-order' for finding a tag.")
220 (defvar find-tag-tag-order nil
221 "Tag order passed to `find-tag-in-order' for finding a tag.")
222 (defvar find-tag-next-line-after-failure-p nil
223 "Flag passed to `find-tag-in-order' for finding a tag.")
224 (defvar list-tags-function nil
225 "Function to do the work of `list-tags' (which see).")
226 (defvar tags-apropos-function nil
227 "Function to do the work of `tags-apropos' (which see).")
228 (defvar tags-included-tables-function nil
229 "Function to do the work of `tags-included-tables' (which see).")
230 (defvar verify-tags-table-function nil
231 "Function to return t iff current buffer contains valid tags file.")
233 ;; Initialize the tags table in the current buffer.
234 ;; Returns non-nil iff it is a valid tags table. On
235 ;; non-nil return, the tags table state variable are
236 ;; made buffer-local and initialized to nil.
237 (defun initialize-new-tags-table ()
238 (set (make-local-variable 'tags-table-files
) nil
)
239 (set (make-local-variable 'tags-completion-table
) nil
)
240 (set (make-local-variable 'tags-included-tables
) nil
)
241 ;; We used to initialize find-tag-marker-ring and tags-location-ring
242 ;; here, to new empty rings. But that is wrong, because those
245 ;; Value is t if we have found a valid tags table buffer.
246 (run-hook-with-args-until-success 'tags-table-format-functions
))
249 (defun visit-tags-table (file &optional local
)
250 "Tell tags commands to use tags table file FILE.
251 FILE should be the name of a file created with the `etags' program.
252 A directory name is ok too; it means file TAGS in that directory.
254 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
255 With a prefix arg, set the buffer-local value instead.
256 When you find a tag with \\[find-tag], the buffer it finds the tag
257 in is given a local value of this variable which is the name of the tags
258 file the tag was in."
259 (interactive (list (read-file-name "Visit tags table: (default TAGS) "
261 (expand-file-name "TAGS"
265 (or (stringp file
) (signal 'wrong-type-argument
(list 'stringp file
)))
266 ;; Bind tags-file-name so we can control below whether the local or
267 ;; global value gets set. Calling visit-tags-table-buffer will
268 ;; initialize a buffer for the file and set tags-file-name to the
269 ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
270 ;; initialize a buffer for FILE and set tags-file-name to the
271 ;; fully-expanded name.
272 (let ((tags-file-name file
))
274 (or (visit-tags-table-buffer file
)
275 (signal 'file-error
(list "Visiting tags table"
276 "file does not exist"
278 ;; Set FILE to the expanded name.
279 (setq file tags-file-name
)))
281 ;; Set the local value of tags-file-name.
282 (set (make-local-variable 'tags-file-name
) file
)
283 ;; Set the global value of tags-file-name.
284 (setq-default tags-file-name file
)))
286 (defun tags-table-check-computed-list ()
287 "Compute `tags-table-computed-list' from `tags-table-list' if necessary."
288 (let ((expanded-list (mapcar 'tags-expand-table-name tags-table-list
)))
289 (or (equal tags-table-computed-list-for expanded-list
)
290 ;; The list (or default-directory) has changed since last computed.
291 (let* ((compute-for (mapcar 'copy-sequence expanded-list
))
292 (tables (copy-sequence compute-for
)) ;Mutated in the loop.
297 (setq computed
(cons (car tables
) computed
)
298 table-buffer
(get-file-buffer (car tables
)))
299 (if (and table-buffer
300 ;; There is a buffer visiting the file. Now make sure
301 ;; it is initialized as a tag table buffer.
303 (tags-verify-table (buffer-file-name table-buffer
))))
305 (set-buffer table-buffer
)
306 (if (tags-included-tables)
307 ;; Insert the included tables into the list we
309 (setcdr tables
(nconc (mapcar 'tags-expand-table-name
310 (tags-included-tables))
312 ;; This table is not in core yet. Insert a placeholder
313 ;; saying we must read it into core to check for included
314 ;; tables before searching the next table in the list.
315 (setq computed
(cons t computed
)))
316 (setq tables
(cdr tables
)))
318 ;; Record the tags-table-list value (and the context of the
319 ;; current directory) we computed from.
320 (setq tags-table-computed-list-for compute-for
321 tags-table-computed-list
(nreverse computed
))))))
323 ;; Extend `tags-table-computed-list' to remove the first `t' placeholder.
324 ;; An element of the list that is `t' is a placeholder indicating that the
325 ;; preceding element is a table that has not been read into core and might
326 ;; contain included tables to search. On return, the first placeholder
327 ;; element will be gone and the element before it read into core and its
328 ;; included tables inserted into the list.
329 (defun tags-table-extend-computed-list ()
330 (let ((list tags-table-computed-list
))
331 (while (not (eq (nth 1 list
) t
))
332 (setq list
(cdr list
)))
334 (if (tags-verify-table (car list
))
335 ;; We are now in the buffer visiting (car LIST). Extract its
336 ;; list of included tables and insert it into the computed list.
337 (let ((tables (tags-included-tables))
341 (setq computed
(cons (car tables
) computed
)
342 table-buffer
(get-file-buffer (car tables
)))
345 (set-buffer table-buffer
)
346 (if (tags-included-tables)
347 ;; Insert the included tables into the list we
349 (setcdr tables
(append (tags-included-tables)
351 ;; This table is not in core yet. Insert a placeholder
352 ;; saying we must read it into core to check for included
353 ;; tables before searching the next table in the list.
354 (setq computed
(cons t computed
)))
355 (setq tables
(cdr tables
)))
356 (setq computed
(nreverse computed
))
357 ;; COMPUTED now contains the list of included tables (and
358 ;; tables included by them, etc.). Now splice this into the
360 (setcdr list
(nconc computed
(cdr (cdr list
)))))
361 ;; It was not a valid table, so just remove the following placeholder.
362 (setcdr list
(cdr (cdr list
)))))))
364 ;; Expand tags table name FILE into a complete file name.
365 (defun tags-expand-table-name (file)
366 (setq file
(expand-file-name file
))
367 (if (file-directory-p file
)
368 (expand-file-name "TAGS" file
)
371 ;; Like member, but comparison is done after tags-expand-table-name on both
372 ;; sides and elements of LIST that are t are skipped.
373 (defun tags-table-list-member (file list
)
374 (setq file
(tags-expand-table-name file
))
376 (or (eq (car list
) t
)
377 (not (string= file
(tags-expand-table-name (car list
))))))
378 (setq list
(cdr list
)))
381 (defun tags-verify-table (file)
382 "Read FILE into a buffer and verify that it is a valid tags table.
383 Sets the current buffer to one visiting FILE (if it exists).
384 Returns non-nil iff it is a valid table."
385 (if (get-file-buffer file
)
386 ;; The file is already in a buffer. Check for the visited file
387 ;; having changed since we last used it.
389 (set-buffer (get-file-buffer file
))
390 (setq win
(or verify-tags-table-function
(initialize-new-tags-table)))
391 (if (or (verify-visited-file-modtime (current-buffer))
392 ;; Decide whether to revert the file.
393 ;; revert-without-query can say to revert
394 ;; or the user can say to revert.
395 (not (or (let ((tail revert-without-query
)
398 (if (string-match (car tail
) buffer-file-name
)
400 (setq tail
(cdr tail
)))
402 tags-revert-without-query
404 (format "Tags file %s has changed, read new contents? "
406 (and verify-tags-table-function
407 (funcall verify-tags-table-function
))
409 (initialize-new-tags-table)))
410 (and (file-exists-p file
)
412 (set-buffer (find-file-noselect file
))
413 (or (string= file buffer-file-name
)
414 ;; find-file-noselect has changed the file name.
415 ;; Propagate the change to tags-file-name and tags-table-list.
416 (let ((tail (member file tags-table-list
)))
418 (setcar tail buffer-file-name
))
419 (if (eq file tags-file-name
)
420 (setq tags-file-name buffer-file-name
))))
421 (initialize-new-tags-table)))))
423 ;; Subroutine of visit-tags-table-buffer. Search the current tags tables
424 ;; for one that has tags for THIS-FILE (or that includes a table that
425 ;; does). Return the name of the first table table listing THIS-FILE; if
426 ;; the table is one included by another table, it is the master table that
427 ;; we return. If CORE-ONLY is non-nil, check only tags tables that are
428 ;; already in buffers--don't visit any new files.
429 (defun tags-table-including (this-file core-only
)
430 (let ((tables tags-table-computed-list
)
432 ;; Loop over the list, looking for a table containing tags for THIS-FILE.
433 (while (and (not found
)
437 ;; Skip tables not in core.
438 (while (eq (nth 1 tables
) t
)
439 (setq tables
(cdr (cdr tables
))))
440 (if (eq (nth 1 tables
) t
)
441 ;; This table has not been read into core yet. Read it in now.
442 (tags-table-extend-computed-list)))
445 ;; Select the tags table buffer and get the file list up to date.
446 (let ((tags-file-name (car tables
)))
447 (visit-tags-table-buffer 'same
)
448 (if (member this-file
(mapcar 'expand-file-name
451 (setq found tables
))))
452 (setq tables
(cdr tables
)))
454 ;; Now determine if the table we found was one included by another
455 ;; table, not explicitly listed. We do this by checking each
456 ;; element of the computed list to see if it appears in the user's
457 ;; explicit list; the last element we will check is FOUND itself.
458 ;; Then we return the last one which did in fact appear in
461 (elt tags-table-computed-list
))
462 (while (not (eq elt
(cdr found
)))
463 (if (tags-table-list-member (car elt
) tags-table-list
)
464 ;; This table appears in the user's list, so it could be
465 ;; the one which includes the table we found.
466 (setq could-be
(car elt
)))
469 (setq elt
(cdr elt
))))
470 ;; The last element we found in the computed list before FOUND
471 ;; that appears in the user's list will be the table that
472 ;; included the one we found.
475 ;; Subroutine of visit-tags-table-buffer. Move tags-table-list-pointer
476 ;; along and set tags-file-name. Returns nil when out of tables.
477 (defun tags-next-table ()
478 ;; If there is a placeholder element next, compute the list to replace it.
479 (while (eq (nth 1 tags-table-list-pointer
) t
)
480 (tags-table-extend-computed-list))
482 ;; Go to the next table in the list.
483 (setq tags-table-list-pointer
(cdr tags-table-list-pointer
))
484 (or tags-table-list-pointer
486 (setq tags-table-list-pointer tags-table-computed-list
))
488 (if (eq tags-table-list-pointer tags-table-list-started-at
)
489 ;; We have come full circle. No more tables.
490 (setq tags-table-list-pointer nil
)
491 ;; Set tags-file-name to the name from the list. It is already expanded.
492 (setq tags-file-name
(car tags-table-list-pointer
))))
494 (defun visit-tags-table-buffer (&optional cont
)
495 "Select the buffer containing the current tags table.
496 If optional arg is a string, visit that file as a tags table.
497 If optional arg is t, visit the next table in `tags-table-list'.
498 If optional arg is the atom `same', don't look for a new table;
499 just select the buffer visiting `tags-file-name'.
500 If arg is nil or absent, choose a first buffer from information in
501 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
502 Returns t if it visits a tags table, or nil if there are no more in the list."
504 ;; Set tags-file-name to the tags table file we want to visit.
505 (cond ((eq cont
'same
)
506 ;; Use the ambient value of tags-file-name.
509 (substitute-command-keys
510 (concat "No tags table in use; "
511 "use \\[visit-tags-table] to select one")))))
514 ;; Find the next table.
515 (if (tags-next-table)
516 ;; Skip over nonexistent files.
517 (while (and (not (or (get-file-buffer tags-file-name
)
518 (file-exists-p tags-file-name
)))
519 (tags-next-table)))))
522 ;; Pick a table out of our hat.
523 (tags-table-check-computed-list) ;Get it up to date, we might use it.
526 ;; If passed a string, use that.
530 ;; First, try a local variable.
531 (cdr (assq 'tags-file-name
(buffer-local-variables)))
532 ;; Second, try a user-specified function to guess.
533 (and default-tags-table-function
534 (funcall default-tags-table-function
))
535 ;; Third, look for a tags table that contains tags for the
536 ;; current buffer's file. If one is found, the lists will
537 ;; be frobnicated, and CONT will be set non-nil so we don't
539 (and buffer-file-name
541 ;; First check only tables already in buffers.
542 (tags-table-including buffer-file-name t
)
543 ;; Since that didn't find any, now do the
544 ;; expensive version: reading new files.
545 (tags-table-including buffer-file-name nil
)))
546 ;; Fourth, use the user variable tags-file-name, if it is
547 ;; not already in the current list.
549 (not (tags-table-list-member tags-file-name
550 tags-table-computed-list
))
552 ;; Fifth, use the user variable giving the table list.
553 ;; Find the first element of the list that actually exists.
554 (let ((list tags-table-list
)
557 (setq file
(tags-expand-table-name (car list
)))
558 (not (get-file-buffer file
))
559 (not (file-exists-p file
)))
560 (setq list
(cdr list
)))
562 ;; Finally, prompt the user for a file name.
564 (read-file-name "Visit tags table: (default TAGS) "
569 ;; Expand the table name into a full file name.
570 (setq tags-file-name
(tags-expand-table-name tags-file-name
))
572 (unless (and (eq cont t
) (null tags-table-list-pointer
))
573 ;; Verify that tags-file-name names a valid tags table.
574 ;; Bind another variable with the value of tags-file-name
575 ;; before we switch buffers, in case tags-file-name is buffer-local.
576 (let ((curbuf (current-buffer))
577 (local-tags-file-name tags-file-name
))
578 (if (tags-verify-table local-tags-file-name
)
580 ;; We have a valid tags table.
582 ;; Bury the tags table buffer so it
583 ;; doesn't get in the user's way.
584 (bury-buffer (current-buffer))
586 ;; If this was a new table selection (CONT is nil), make
587 ;; sure tags-table-list includes the chosen table, and
588 ;; update the list pointer variables.
590 ;; Look in the list for the table we chose.
591 (let ((found (tags-table-list-member
593 tags-table-computed-list
)))
595 ;; There it is. Just switch to it.
596 (setq tags-table-list-pointer found
597 tags-table-list-started-at found
)
599 ;; The table is not in the current set.
600 ;; Try to find it in another previously used set.
601 (let ((sets tags-table-set-list
))
603 (not (tags-table-list-member
606 (setq sets
(cdr sets
)))
608 ;; Found in some other set. Switch to that set.
610 (or (memq tags-table-list tags-table-set-list
)
611 ;; Save the current list.
612 (setq tags-table-set-list
613 (cons tags-table-list
614 tags-table-set-list
)))
615 (setq tags-table-list
(car sets
)))
617 ;; Not found in any existing set.
618 (if (and tags-table-list
619 (or (eq t tags-add-tables
)
622 (concat "Keep current list of "
623 "tags tables also? ")))))
624 ;; Add it to the current list.
625 (setq tags-table-list
(cons local-tags-file-name
628 ;; Make a fresh list, and store the old one.
629 (message "Starting a new list of tags tables")
630 (or (null tags-table-list
)
631 (memq tags-table-list tags-table-set-list
)
632 (setq tags-table-set-list
633 (cons tags-table-list
634 tags-table-set-list
)))
635 ;; Clear out buffers holding old tables.
636 (dolist (table tags-table-list
)
637 (let ((buffer (find-buffer-visiting table
)))
639 (kill-buffer buffer
))))
640 (setq tags-table-list
(list local-tags-file-name
))))
642 ;; Recompute tags-table-computed-list.
643 (tags-table-check-computed-list)
644 ;; Set the tags table list state variables to start
645 ;; over from tags-table-computed-list.
646 (setq tags-table-list-started-at tags-table-computed-list
647 tags-table-list-pointer
648 tags-table-computed-list
)))))
650 ;; Return of t says the tags table is valid.
653 ;; The buffer was not valid. Don't use it again.
655 (kill-local-variable 'tags-file-name
)
656 (if (eq local-tags-file-name tags-file-name
)
657 (setq tags-file-name nil
))
658 (error "File %s is not a valid tags table" local-tags-file-name
)))))
660 (defun tags-reset-tags-tables ()
661 "Reset tags state to cancel effect of any previous \\[visit-tags-table] or \\[find-tag]."
663 ;; Clear out the markers we are throwing away.
665 (while (< i find-tag-marker-ring-length
)
666 (if (aref (cddr tags-location-ring
) i
)
667 (set-marker (aref (cddr tags-location-ring
) i
) nil
))
668 (if (aref (cddr find-tag-marker-ring
) i
)
669 (set-marker (aref (cddr find-tag-marker-ring
) i
) nil
))
671 (setq tags-file-name nil
672 tags-location-ring
(make-ring find-tag-marker-ring-length
)
673 find-tag-marker-ring
(make-ring find-tag-marker-ring-length
)
675 tags-table-computed-list nil
676 tags-table-computed-list-for nil
677 tags-table-list-pointer nil
678 tags-table-list-started-at nil
679 tags-table-set-list nil
))
681 (defun file-of-tag ()
682 "Return the file name of the file whose tags point is within.
683 Assumes the tags table is the current buffer.
684 File name returned is relative to tags table file's directory."
685 (funcall file-of-tag-function
))
688 (defun tags-table-files ()
689 "Return a list of files in the current tags table.
690 Assumes the tags table is the current buffer. The file names are returned
691 as they appeared in the `etags' command that created the table, usually
692 without directory names."
694 (setq tags-table-files
695 (funcall tags-table-files-function
))))
697 (defun tags-included-tables ()
698 "Return a list of tags tables included by the current table.
699 Assumes the tags table is the current buffer."
700 (or tags-included-tables
701 (setq tags-included-tables
(funcall tags-included-tables-function
))))
703 ;; Build tags-completion-table on demand. The single current tags table
704 ;; and its included tags tables (and their included tables, etc.) have
705 ;; their tags included in the completion table.
706 (defun tags-completion-table ()
707 (or tags-completion-table
710 (message "Making tags completion table for %s..." buffer-file-name
)
711 (let ((included (tags-included-tables))
712 (table (funcall tags-completion-table-function
)))
714 ;; Iterate over the list of included tables, and combine each
715 ;; included table's completion obarray to the parent obarray.
718 (let ((tags-file-name (car included
)))
719 (visit-tags-table-buffer 'same
))
720 ;; Recurse in that buffer to compute its completion table.
721 (if (tags-completion-table)
722 ;; Combine the tables.
723 (mapatoms (lambda (sym) (intern (symbol-name sym
) table
))
724 tags-completion-table
))
725 (setq included
(cdr included
))))
726 (setq tags-completion-table table
))
727 (message "Making tags completion table for %s...done"
729 (quit (message "Tags completion table construction aborted.")
730 (setq tags-completion-table nil
)))))
732 ;; Completion function for tags. Does normal try-completion,
733 ;; but builds tags-completion-table on demand.
734 (defun tags-complete-tag (string predicate what
)
736 ;; If we need to ask for the tag table, allow that.
737 (let ((enable-recursive-minibuffers t
))
738 (visit-tags-table-buffer))
740 (all-completions string
(tags-completion-table) predicate
)
741 (try-completion string
(tags-completion-table) predicate
))))
743 ;; Return a default tag to search for, based on the text at point.
744 (defun find-tag-default ()
746 (while (looking-at "\\sw\\|\\s_")
748 (if (or (re-search-backward "\\sw\\|\\s_"
749 (save-excursion (beginning-of-line) (point))
751 (re-search-forward "\\(\\sw\\|\\s_\\)+"
752 (save-excursion (end-of-line) (point))
754 (progn (goto-char (match-end 0))
755 (buffer-substring (point)
756 (progn (forward-sexp -
1)
757 (while (looking-at "\\s'")
762 ;; Read a tag name from the minibuffer with defaulting and completion.
763 (defun find-tag-tag (string)
764 (let* ((default (funcall (or find-tag-default-function
765 (get major-mode
'find-tag-default-function
)
767 (spec (completing-read (if default
768 (format "%s(default %s) " string default
)
771 nil nil nil nil default
)))
773 (or default
(error "There is no default tag"))
777 "Last tag found by \\[find-tag].")
779 ;; Get interactive args for find-tag{-noselect,-other-window,-regexp}.
780 (defun find-tag-interactive (prompt &optional no-default
)
781 (if current-prefix-arg
782 (list nil
(if (< (prefix-numeric-value current-prefix-arg
) 0)
787 (find-tag-tag prompt
)))))
789 (defvar find-tag-history nil
)
793 (defvar etags-case-fold-search
)
794 (defvar etags-syntax-table
))
797 (defun find-tag-noselect (tagname &optional next-p regexp-p
)
798 "Find tag (in current tags table) whose name contains TAGNAME.
799 Returns the buffer containing the tag's definition and moves its point there,
800 but does not select the buffer.
801 The default for TAGNAME is the expression in the buffer near point.
803 If second arg NEXT-P is t (interactively, with prefix arg), search for
804 another tag that matches the last tagname or regexp used. When there are
805 multiple matches for a tag, more exact matches are found first. If NEXT-P
806 is the atom `-' (interactively, with prefix arg that is a negative number
807 or just \\[negative-argument]), pop back to the previous tag gone to.
809 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
811 A marker representing the point when this command is onvoked is pushed
812 onto a ring and may be popped back to with \\[pop-tag-mark].
813 Contrast this with the ring of marks gone to by the command.
815 See documentation of variable `tags-file-name'."
816 (interactive (find-tag-interactive "Find tag: "))
818 (setq find-tag-history
(cons tagname find-tag-history
))
819 ;; Save the current buffer's value of `find-tag-hook' before selecting the
820 ;; tags table buffer.
821 (let ((local-find-tag-hook find-tag-hook
))
823 ;; Pop back to a previous location.
824 (if (ring-empty-p tags-location-ring
)
825 (error "No previous tag locations")
826 (let ((marker (ring-remove tags-location-ring
0)))
828 ;; Move to the saved location.
829 (set-buffer (or (marker-buffer marker
)
830 (error "The marked buffer has been deleted")))
831 (goto-char (marker-position marker
))
832 ;; Kill that marker so it doesn't slow down editing.
833 (set-marker marker nil nil
)
834 ;; Run the user's hook. Do we really want to do this for pop?
835 (run-hooks 'local-find-tag-hook
))))
836 ;; Record whence we came.
837 (ring-insert find-tag-marker-ring
(point-marker))
839 ;; Find the same table we last used.
840 (visit-tags-table-buffer 'same
)
841 ;; Pick a table to use.
842 (visit-tags-table-buffer)
843 ;; Record TAGNAME for a future call with NEXT-P non-nil.
844 (setq last-tag tagname
))
845 ;; Record the location so we can pop back to it later.
846 (let ((marker (make-marker)))
849 ;; find-tag-in-order does the real work.
851 (if next-p last-tag tagname
)
853 find-tag-regexp-search-function
854 find-tag-search-function
)
856 find-tag-regexp-tag-order
859 find-tag-regexp-next-line-after-failure-p
860 find-tag-next-line-after-failure-p
)
861 (if regexp-p
"matching" "containing")
863 (set-marker marker
(point))
864 (run-hooks 'local-find-tag-hook
)
865 (ring-insert tags-location-ring marker
)
866 (current-buffer))))))
869 (defun find-tag (tagname &optional next-p regexp-p
)
870 "Find tag (in current tags table) whose name contains TAGNAME.
871 Select the buffer containing the tag's definition, and move point there.
872 The default for TAGNAME is the expression in the buffer around or before point.
874 If second arg NEXT-P is t (interactively, with prefix arg), search for
875 another tag that matches the last tagname or regexp used. When there are
876 multiple matches for a tag, more exact matches are found first. If NEXT-P
877 is the atom `-' (interactively, with prefix arg that is a negative number
878 or just \\[negative-argument]), pop back to the previous tag gone to.
880 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
882 A marker representing the point when this command is onvoked is pushed
883 onto a ring and may be popped back to with \\[pop-tag-mark].
884 Contrast this with the ring of marks gone to by the command.
886 See documentation of variable `tags-file-name'."
887 (interactive (find-tag-interactive "Find tag: "))
888 (let ((buf (find-tag-noselect tagname next-p regexp-p
)))
890 (switch-to-buffer buf
)
891 (error (pop-to-buffer buf
)))))
892 ;;;###autoload (define-key esc-map "." 'find-tag)
895 (defun find-tag-other-window (tagname &optional next-p regexp-p
)
896 "Find tag (in current tags table) whose name contains TAGNAME.
897 Select the buffer containing the tag's definition in another window, and
898 move point there. The default for TAGNAME is the expression in the buffer
899 around or before point.
901 If second arg NEXT-P is t (interactively, with prefix arg), search for
902 another tag that matches the last tagname or regexp used. When there are
903 multiple matches for a tag, more exact matches are found first. If NEXT-P
904 is negative (interactively, with prefix arg that is a negative number or
905 just \\[negative-argument]), pop back to the previous tag gone to.
907 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
909 A marker representing the point when this command is onvoked is pushed
910 onto a ring and may be popped back to with \\[pop-tag-mark].
911 Contrast this with the ring of marks gone to by the command.
913 See documentation of variable `tags-file-name'."
914 (interactive (find-tag-interactive "Find tag other window: "))
916 ;; This hair is to deal with the case where the tag is found in the
917 ;; selected window's buffer; without the hair, point is moved in both
918 ;; windows. To prevent this, we save the selected window's point before
919 ;; doing find-tag-noselect, and restore it after.
920 (let* ((window-point (window-point (selected-window)))
921 (tagbuf (find-tag-noselect tagname next-p regexp-p
))
922 (tagpoint (progn (set-buffer tagbuf
) (point))))
923 (set-window-point (prog1
925 (switch-to-buffer-other-window tagbuf
)
926 ;; We have to set this new window's point; it
927 ;; might already have been displaying a
928 ;; different portion of tagbuf, in which case
929 ;; switch-to-buffer-other-window doesn't set
930 ;; the window's point from the buffer.
931 (set-window-point (selected-window) tagpoint
))
933 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
936 (defun find-tag-other-frame (tagname &optional next-p
)
937 "Find tag (in current tags table) whose name contains TAGNAME.
938 Select the buffer containing the tag's definition in another frame, and
939 move point there. The default for TAGNAME is the expression in the buffer
940 around or before point.
942 If second arg NEXT-P is t (interactively, with prefix arg), search for
943 another tag that matches the last tagname or regexp used. When there are
944 multiple matches for a tag, more exact matches are found first. If NEXT-P
945 is negative (interactively, with prefix arg that is a negative number or
946 just \\[negative-argument]), pop back to the previous tag gone to.
948 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
950 A marker representing the point when this command is onvoked is pushed
951 onto a ring and may be popped back to with \\[pop-tag-mark].
952 Contrast this with the ring of marks gone to by the command.
954 See documentation of variable `tags-file-name'."
955 (interactive (find-tag-interactive "Find tag other frame: "))
956 (let ((pop-up-frames t
))
957 (find-tag-other-window tagname next-p
)))
958 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
961 (defun find-tag-regexp (regexp &optional next-p other-window
)
962 "Find tag (in current tags table) whose name matches REGEXP.
963 Select the buffer containing the tag's definition and move point there.
965 If second arg NEXT-P is t (interactively, with prefix arg), search for
966 another tag that matches the last tagname or regexp used. When there are
967 multiple matches for a tag, more exact matches are found first. If NEXT-P
968 is negative (interactively, with prefix arg that is a negative number or
969 just \\[negative-argument]), pop back to the previous tag gone to.
971 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
973 A marker representing the point when this command is onvoked is pushed
974 onto a ring and may be popped back to with \\[pop-tag-mark].
975 Contrast this with the ring of marks gone to by the command.
977 See documentation of variable `tags-file-name'."
978 (interactive (find-tag-interactive "Find tag regexp: " t
))
979 ;; We go through find-tag-other-window to do all the display hair there.
980 (funcall (if other-window
'find-tag-other-window
'find-tag
)
982 ;;;###autoload (define-key esc-map [?\C-.] 'find-tag-regexp)
984 ;;;###autoload (define-key esc-map "*" 'pop-tag-mark)
987 (defun pop-tag-mark ()
988 "Pop back to where \\[find-tag] was last invoked.
990 This is distinct from invoking \\[find-tag] with a negative argument
991 since that pops a stack of markers at which tags were found, not from
992 where they were found."
994 (if (ring-empty-p find-tag-marker-ring
)
995 (error "No previous locations for find-tag invocation"))
996 (let ((marker (ring-remove find-tag-marker-ring
0)))
997 (switch-to-buffer (or (marker-buffer marker
)
998 (error "The marked buffer has been deleted")))
999 (goto-char (marker-position marker
))
1000 (set-marker marker nil nil
)))
1002 ;; Internal tag finding function.
1004 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
1005 ;; any member of the function list ORDER (third arg). If ORDER is nil,
1006 ;; use saved state to continue a previous search.
1008 ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
1009 ;; an error message.
1011 ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
1012 ;; point should be moved to the next line.
1014 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
1015 ;; beginning of tags file, and perform inner loop: for each naive match for
1016 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
1017 ;; qualifier-func. If it qualifies, go to the specified line in the
1018 ;; specified source file and return. Qualified matches are remembered to
1019 ;; avoid repetition. State is saved so that the loop can be continued.
1021 (defvar tag-lines-already-matched nil
) ;matches remembered here between calls
1023 (defun find-tag-in-order (pattern
1026 next-line-after-failure-p
1029 (let (file ;name of file containing tag
1030 tag-info
;where to find the tag in FILE
1033 (match-marker (make-marker))
1035 (case-fold-search (if (memq tags-case-fold-search
'(nil t
))
1036 tags-case-fold-search
1042 ;; This is the start of a search for a fresh tag.
1043 ;; Clear the list of tags matched by the previous search.
1044 ;; find-tag-noselect has already put us in the first tags table
1045 ;; buffer before we got called.
1046 (setq tag-lines-already-matched nil
)
1047 ;; Continuing to search for the tag specified last time.
1048 ;; tag-lines-already-matched lists locations matched in previous
1049 ;; calls so we don't visit the same tag twice if it matches twice
1050 ;; during two passes with different qualification predicates.
1051 ;; Switch to the current tags table buffer.
1052 (visit-tags-table-buffer 'same
))
1054 ;; Get a qualified match.
1055 (catch 'qualified-match-found
1057 ;; Iterate over the list of tags tables.
1058 (while (or first-table
1059 (visit-tags-table-buffer t
))
1061 (and first-search first-table
1062 ;; Start at beginning of tags file.
1063 (goto-char (point-min)))
1065 (setq first-table nil
)
1067 ;; Iterate over the list of ordering predicates.
1069 (while (funcall search-forward-func pattern nil t
)
1070 ;; Naive match found. Qualify the match.
1071 (and (funcall (car order
) pattern
)
1072 ;; Make sure it is not a previous qualified match.
1073 (not (member (set-marker match-marker
(save-excursion
1076 tag-lines-already-matched
))
1077 (throw 'qualified-match-found nil
))
1078 (if next-line-after-failure-p
1080 ;; Try the next flavor of match.
1081 (setq order
(cdr order
))
1082 (goto-char (point-min)))
1083 (setq order tag-order
))
1084 ;; We throw out on match, so only get here if there were no matches.
1085 ;; Clear out the markers we use to avoid duplicate matches so they
1086 ;; don't slow down editting and are immediately available for GC.
1087 (while tag-lines-already-matched
1088 (set-marker (car tag-lines-already-matched
) nil nil
)
1089 (setq tag-lines-already-matched
(cdr tag-lines-already-matched
)))
1090 (set-marker match-marker nil nil
)
1091 (error "No %stags %s %s" (if first-search
"" "more ")
1094 ;; Found a tag; extract location info.
1096 (setq tag-lines-already-matched
(cons match-marker
1097 tag-lines-already-matched
))
1098 ;; Expand the filename, using the tags table buffer's default-directory.
1099 (setq file
(expand-file-name (file-of-tag))
1100 tag-info
(funcall snarf-tag-function
))
1102 ;; Get the local value in the tags table buffer before switching buffers.
1103 (setq goto-func goto-tag-location-function
)
1105 ;; Find the right line in the specified file.
1106 (set-buffer (find-file-noselect file
))
1109 (funcall goto-func tag-info
)
1111 ;; Return the buffer where the tag was found.
1114 ;; `etags' TAGS file format support.
1116 ;; If the current buffer is a valid etags TAGS file, give it local values of
1117 ;; the tags table format variables, and return non-nil.
1118 (defun etags-recognize-tags-table ()
1119 (and (etags-verify-tags-table)
1120 ;; It is annoying to flash messages on the screen briefly,
1121 ;; and this message is not useful. -- rms
1122 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
1123 (mapc (lambda (elt) (set (make-local-variable (car elt
)) (cdr elt
)))
1124 '((file-of-tag-function . etags-file-of-tag
)
1125 (tags-table-files-function . etags-tags-table-files
)
1126 (tags-completion-table-function . etags-tags-completion-table
)
1127 (snarf-tag-function . etags-snarf-tag
)
1128 (goto-tag-location-function . etags-goto-tag-location
)
1129 (find-tag-regexp-search-function . re-search-forward
)
1130 (find-tag-regexp-tag-order .
(tag-re-match-p))
1131 (find-tag-regexp-next-line-after-failure-p . t
)
1132 (find-tag-search-function . search-forward
)
1133 (find-tag-tag-order .
(tag-exact-file-name-match-p
1138 (find-tag-next-line-after-failure-p . nil
)
1139 (list-tags-function . etags-list-tags
)
1140 (tags-apropos-function . etags-tags-apropos
)
1141 (tags-included-tables-function . etags-tags-included-tables
)
1142 (verify-tags-table-function . etags-verify-tags-table
)
1145 ;; Return non-nil iff the current buffer is a valid etags TAGS file.
1146 (defun etags-verify-tags-table ()
1147 ;; Use eq instead of = in case char-after returns nil.
1148 (eq (char-after 1) ?
\f))
1150 (defun etags-file-of-tag ()
1152 (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n")
1153 (expand-file-name (buffer-substring (match-beginning 1) (match-end 1))
1154 (file-truename default-directory
))))
1157 (defun etags-tags-completion-table ()
1158 (let ((table (make-vector 511 0)))
1160 (goto-char (point-min))
1161 ;; This monster regexp matches an etags tag line.
1162 ;; \1 is the string to match;
1163 ;; \2 is not interesting;
1164 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
1165 ;; \4 is not interesting;
1166 ;; \5 is the explicitly-specified tag name.
1167 ;; \6 is the line to start searching at;
1168 ;; \7 is the char to start searching at.
1169 (while (re-search-forward
1170 "^\\(\\([^\177]+[^-a-zA-Z0-9_+*$:\177]+\\)?\
1171 \\([-a-zA-Z0-9_+*$?:]+\\)[^-a-zA-Z0-9_+*$?:\177]*\\)\177\
1172 \\(\\([^\n\001]+\\)\001\\)?\\([0-9]+\\)?,\\([0-9]+\\)?\n"
1174 (intern (if (match-beginning 5)
1175 ;; There is an explicit tag name.
1176 (buffer-substring (match-beginning 5) (match-end 5))
1177 ;; No explicit tag name. Best guess.
1178 (buffer-substring (match-beginning 3) (match-end 3)))
1182 (defun etags-snarf-tag ()
1183 (let (tag-text line startpos
)
1186 (looking-at "\f\n"))
1187 ;; The match was for a source file name, not any tag within a file.
1188 ;; Give text of t, meaning to go exactly to the location we specify,
1189 ;; the beginning of the file.
1194 ;; Find the end of the tag and record the whole tag text.
1195 (search-forward "\177")
1196 (setq tag-text
(buffer-substring (1- (point))
1197 (save-excursion (beginning-of-line)
1199 ;; Skip explicit tag name if present.
1200 (search-forward "\001" (save-excursion (forward-line 1) (point)) t
)
1201 (if (looking-at "[0-9]")
1202 (setq line
(string-to-int (buffer-substring
1204 (progn (skip-chars-forward "0-9")
1206 (search-forward ",")
1207 (if (looking-at "[0-9]")
1208 (setq startpos
(string-to-int (buffer-substring
1210 (progn (skip-chars-forward "0-9")
1212 ;; Leave point on the next line of the tags file.
1214 (cons tag-text
(cons line startpos
))))
1216 ;; TAG-INFO is a cons (TEXT LINE . POSITION) where TEXT is the initial part
1217 ;; of a line containing the tag and POSITION is the character position of
1218 ;; TEXT within the file (starting from 1); LINE is the line number. If
1219 ;; TEXT is t, it means the tag refers to exactly LINE or POSITION
1220 ;; (whichever is present, LINE having preference, no searching. Either
1221 ;; LINE or POSITION may be nil; POSITION is used if present. If the tag
1222 ;; isn't exactly at the given position then look around that position using
1223 ;; a search window which expands until it hits the start of file.
1224 (defun etags-goto-tag-location (tag-info)
1225 (let ((startpos (cdr (cdr tag-info
)))
1226 (line (car (cdr tag-info
)))
1228 (if (eq (car tag-info
) t
)
1230 (cond (line (goto-line line
))
1231 (startpos (goto-char startpos
))
1232 (t (error "etags.el BUG: bogus direct file tag")))
1233 ;; This constant is 1/2 the initial search window.
1234 ;; There is no sense in making it too small,
1235 ;; since just going around the loop once probably
1236 ;; costs about as much as searching 2000 chars.
1239 pat
(concat (if (eq selective-display t
)
1240 "\\(^\\|\^m\\)" "^")
1241 (regexp-quote (car tag-info
))))
1242 ;; The character position in the tags table is 0-origin.
1243 ;; Convert it to a 1-origin Emacs character position.
1244 (if startpos
(setq startpos
(1+ startpos
)))
1245 ;; If no char pos was given, try the given line number.
1248 (setq startpos
(progn (goto-line line
)
1251 (setq startpos
(point-min)))
1252 ;; First see if the tag is right at the specified location.
1253 (goto-char startpos
)
1254 (setq found
(looking-at pat
))
1255 (while (and (not found
)
1257 (goto-char (- startpos offset
))
1260 (re-search-forward pat
(+ startpos offset
) t
)
1261 offset
(* 3 offset
))) ; expand search window
1263 (re-search-forward pat nil t
)
1264 (error "Rerun etags: `%s' not found in %s"
1265 pat buffer-file-name
)))
1266 ;; Position point at the right place
1267 ;; if the search string matched an extra Ctrl-m at the beginning.
1268 (and (eq selective-display t
)
1271 (beginning-of-line)))
1273 (defun etags-list-tags (file)
1275 (when (search-forward (concat "\f\n" file
",") nil t
)
1277 (while (not (or (eobp) (looking-at "\f")))
1278 (let ((tag (buffer-substring (point)
1279 (progn (skip-chars-forward "^\177")
1281 (props `(action find-tag-other-window mouse-face highlight
1282 face
,tags-tag-face
))
1283 (pt (with-current-buffer standard-output
(point))))
1284 (when (looking-at "[^\n]+\001")
1285 ;; There is an explicit tag name; use that.
1286 (setq tag
(buffer-substring (1+ (point)) ; skip \177
1287 (progn (skip-chars-forward "^\001")
1290 (when (= (aref tag
0) ?\
() (princ " ...)"))
1291 (add-text-properties pt
(with-current-buffer standard-output
(point))
1292 (cons 'item
(cons tag props
)) standard-output
))
1297 (defmacro tags-with-face
(face &rest body
)
1298 "Execute BODY, give output to `standard-output' face FACE."
1299 (let ((pp (gensym "twf-")))
1300 `(let ((,pp
(with-current-buffer standard-output
(point))))
1302 (put-text-property ,pp
(with-current-buffer standard-output
(point))
1303 'face
,face standard-output
))))
1305 (defun etags-tags-apropos-additional (regexp)
1306 "Display tags matching REGEXP from `tags-apropos-additional-actions'."
1307 (with-current-buffer standard-output
1308 (dolist (oba tags-apropos-additional-actions
)
1310 (tags-with-face 'highlight
(princ (car oba
)))
1312 (let* ((props `(action ,(cadr oba
) mouse-face highlight face
1315 (symbs (car (cddr oba
)))
1316 (ins-symb (lambda (sy)
1317 (let ((sn (symbol-name sy
)))
1318 (when (string-match regexp sn
)
1319 (add-text-properties (point)
1320 (progn (princ sy
) (point))
1321 (cons 'item
(cons sn props
)))
1323 (when (symbolp symbs
)
1325 (setq symbs
(symbol-value symbs
))
1326 (insert "symbol `" (symbol-name symbs
) "' has no value\n")
1329 (mapatoms ins-symb symbs
)
1331 (funcall ins-symb
(car sy
))))
1332 (sort-lines nil beg
(point))))))
1334 (defun etags-tags-apropos (string)
1335 (when tags-apropos-verbose
1336 (princ "Tags in file `")
1337 (tags-with-face 'highlight
(princ buffer-file-name
))
1340 (while (re-search-forward string nil t
)
1342 (let ((tag (buffer-substring (point)
1343 (progn (skip-chars-forward "^\177")
1345 (props `(action find-tag-other-window mouse-face highlight
1346 face
,tags-tag-face
))
1347 (pt (with-current-buffer standard-output
(point))))
1349 (when (= (aref tag
0) ?\
() (princ " ...)"))
1350 (add-text-properties pt
(with-current-buffer standard-output
(point))
1351 `(item ,tag
,@props
) standard-output
))
1354 (when tags-apropos-verbose
(princ "\n")))
1356 (defun etags-tags-table-files ()
1359 (goto-char (point-min))
1360 (while (search-forward "\f\n" nil t
)
1363 (skip-chars-backward "^," beg
)
1364 (or (looking-at "include$")
1365 (setq files
(cons (buffer-substring beg
(1- (point))) files
))))
1368 (defun etags-tags-included-tables ()
1371 (goto-char (point-min))
1372 (while (search-forward "\f\n" nil t
)
1375 (skip-chars-backward "^," beg
)
1376 (if (looking-at "include$")
1377 ;; Expand in the default-directory of the tags table buffer.
1378 (setq files
(cons (expand-file-name (buffer-substring beg
(1- (point))))
1382 ;; Empty tags file support.
1384 ;; Recognize an empty file and give it local values of the tags table format
1385 ;; variables which do nothing.
1386 (defun tags-recognize-empty-tags-table ()
1387 (and (zerop (buffer-size))
1388 (mapc (lambda (sym) (set (make-local-variable sym
) 'ignore
))
1389 '(tags-table-files-function
1390 tags-completion-table-function
1391 find-tag-regexp-search-function
1392 find-tag-search-function
1393 tags-apropos-function
1394 tags-included-tables-function
))
1395 (set (make-local-variable 'verify-tags-table-function
)
1396 (lambda () (zerop (buffer-size))))))
1398 ;; Match qualifier functions for tagnames.
1399 ;; XXX these functions assume etags file format.
1401 ;; This might be a neat idea, but it's too hairy at the moment.
1402 ;;(defmacro tags-with-syntax (&rest body)
1403 ;; `(let ((current (current-buffer))
1404 ;; (otable (syntax-table))
1405 ;; (buffer (find-file-noselect (file-of-tag)))
1409 ;; (set-buffer buffer)
1410 ;; (setq table (syntax-table))
1411 ;; (set-buffer current)
1412 ;; (set-syntax-table table)
1414 ;; (set-syntax-table otable))))
1415 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
1417 ;; t if point is at a tag line that matches TAG exactly.
1418 ;; point should be just after a string that matches TAG.
1419 (defun tag-exact-match-p (tag)
1420 ;; The match is really exact if there is an explicit tag name.
1421 (or (and (eq (char-after (point)) ?
\001)
1422 (eq (char-after (- (point) (length tag
) 1)) ?
\177))
1423 ;; We are not on the explicit tag name, but perhaps it follows.
1424 (looking-at (concat "[^\177\n]*\177" (regexp-quote tag
) "\001"))))
1426 ;; t if point is at a tag line that matches TAG as a symbol.
1427 ;; point should be just after a string that matches TAG.
1428 (defun tag-symbol-match-p (tag)
1429 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1431 (backward-char (1+ (length tag
)))
1432 (and (looking-at "\\Sw") (looking-at "\\S_")))))
1434 ;; t if point is at a tag line that matches TAG as a word.
1435 ;; point should be just after a string that matches TAG.
1436 (defun tag-word-match-p (tag)
1437 (and (looking-at "\\b.*\177")
1438 (save-excursion (backward-char (length tag
))
1439 (looking-at "\\b"))))
1441 (defun tag-exact-file-name-match-p (tag)
1442 (and (looking-at ",")
1443 (save-excursion (backward-char (length tag
))
1444 (looking-at "\f\n"))))
1446 ;; t if point is in a tag line with a tag containing TAG as a substring.
1447 (defun tag-any-match-p (tag)
1448 (looking-at ".*\177"))
1450 ;; t if point is at a tag line that matches RE as a regexp.
1451 (defun tag-re-match-p (re)
1454 (let ((bol (point)))
1455 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t
)
1456 (re-search-backward re bol t
)))))
1458 (defcustom tags-loop-revert-buffers nil
1459 "*Non-nil means tags-scanning loops should offer to reread changed files.
1460 These loops normally read each file into Emacs, but when a file
1461 is already visited, they use the existing buffer.
1462 When this flag is non-nil, they offer to revert the existing buffer
1463 in the case where the file has changed since you visited it."
1468 (defun next-file (&optional initialize novisit
)
1469 "Select next file among files in current tags table.
1471 A first argument of t (prefix arg, if interactive) initializes to the
1472 beginning of the list of files in the tags table. If the argument is
1473 neither nil nor t, it is evalled to initialize the list of files.
1475 Non-nil second argument NOVISIT means use a temporary buffer
1476 to save time and avoid uninteresting warnings.
1478 Value is nil if the file was already visited;
1479 if the file was newly read in, the value is the filename."
1480 ;; Make the interactive arg t if there was any prefix arg.
1481 (interactive (list (if current-prefix-arg t
)))
1482 (cond ((not initialize
)
1483 ;; Not the first run.
1486 ;; Initialize the list from the tags table.
1488 ;; Visit the tags table buffer to get its list of files.
1489 (visit-tags-table-buffer)
1490 ;; Copy the list so we can setcdr below, and expand the file
1491 ;; names while we are at it, in this buffer's default directory.
1492 (setq next-file-list
(mapcar 'expand-file-name
(tags-table-files)))
1493 ;; Iterate over all the tags table files, collecting
1494 ;; a complete list of referenced file names.
1495 (while (visit-tags-table-buffer t
)
1496 ;; Find the tail of the working list and chain on the new
1497 ;; sublist for this tags table.
1498 (let ((tail next-file-list
))
1500 (setq tail
(cdr tail
)))
1501 ;; Use a copy so the next loop iteration will not modify the
1502 ;; list later returned by (tags-table-files).
1504 (setcdr tail
(mapcar 'expand-file-name
(tags-table-files)))
1505 (setq next-file-list
(mapcar 'expand-file-name
1506 (tags-table-files))))))))
1508 ;; Initialize the list by evalling the argument.
1509 (setq next-file-list
(eval initialize
))))
1510 (unless next-file-list
1512 (get-buffer " *next-file*")
1513 (kill-buffer " *next-file*"))
1514 (error "All files processed"))
1515 (let* ((next (car next-file-list
))
1516 (buffer (get-file-buffer next
))
1518 ;; Advance the list before trying to find the file.
1519 ;; If we get an error finding the file, don't get stuck on it.
1520 (setq next-file-list
(cdr next-file-list
))
1521 ;; Optionally offer to revert buffers
1522 ;; if the files have changed on disk.
1523 (and buffer tags-loop-revert-buffers
1524 (not (verify-visited-file-modtime buffer
))
1525 (with-current-buffer buffer
1527 (if (not (and new novisit
))
1528 (set-buffer (find-file-noselect next novisit
))
1529 ;; Like find-file, but avoids random warning messages.
1530 (set-buffer (get-buffer-create " *next-file*"))
1531 (kill-all-local-variables)
1534 (insert-file-contents new nil
))
1537 (defvar tags-loop-operate nil
1538 "Form for `tags-loop-continue' to eval to change one file.")
1540 (defvar tags-loop-scan
1542 (substitute-command-keys
1543 "No \\[tags-search] or \\[tags-query-replace] in progress"))
1544 "Form for `tags-loop-continue' to eval to scan one file.
1545 If it returns non-nil, this file needs processing by evalling
1546 \`tags-loop-operate'. Otherwise, move on to the next file.")
1548 (defun tags-loop-eval (form)
1549 "Evaluate FORM and return its result.
1550 Bind `case-fold-search' during the evaluation, depending on the value of
1551 `tags-case-fold-search'."
1552 (let ((case-fold-search (if (memq tags-case-fold-search
'(t nil
))
1553 tags-case-fold-search
1559 (defun tags-loop-continue (&optional first-time
)
1560 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1561 Used noninteractively with non-nil argument to begin such a command (the
1562 argument is passed to `next-file', which see).
1564 Two variables control the processing we do on each file: the value of
1565 `tags-loop-scan' is a form to be executed on each file to see if it is
1566 interesting (it returns non-nil if so) and `tags-loop-operate' is a form to
1567 evaluate to operate on an interesting file. If the latter evaluates to
1568 nil, we exit; otherwise we scan the next file."
1571 ;; Non-nil means we have finished one file
1572 ;; and should not scan it again.
1577 ;; Scan files quickly for the first or next interesting one.
1578 (while (or first-time file-finished
1581 (not (tags-loop-eval tags-loop-scan
))))
1582 (setq file-finished nil
)
1583 (setq new
(next-file first-time t
))
1584 ;; If NEW is non-nil, we got a temp buffer,
1585 ;; and NEW is the file name.
1587 (and (not first-time
)
1588 (> baud-rate search-slow-speed
)
1590 (message "Scanning file %s..." (or new buffer-file-name
)))
1591 (setq first-time nil
)
1592 (goto-char (point-min)))
1594 ;; If we visited it in a temp buffer, visit it now for real.
1596 (let ((pos (point)))
1598 (set-buffer (find-file-noselect new
))
1599 (setq new nil
) ;No longer in a temp buffer.
1603 (switch-to-buffer (current-buffer))
1605 ;; Now operate on the file.
1606 ;; If value is non-nil, continue to scan the next file.
1607 (tags-loop-eval tags-loop-operate
))
1608 (setq file-finished t
))
1610 (null tags-loop-operate
)
1611 (message "Scanning file %s...found" buffer-file-name
))))
1612 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
1615 (defun tags-search (regexp &optional file-list-form
)
1616 "Search through all files listed in tags table for match for REGEXP.
1617 Stops when a match is found.
1618 To continue searching for next match, use command \\[tags-loop-continue].
1620 See documentation of variable `tags-file-name'."
1621 (interactive "sTags search (regexp): ")
1622 (if (and (equal regexp
"")
1623 (eq (car tags-loop-scan
) 're-search-forward
)
1624 (null tags-loop-operate
))
1625 ;; Continue last tags-search as if by M-,.
1626 (tags-loop-continue nil
)
1627 (setq tags-loop-scan
1628 (list 're-search-forward
(list 'quote regexp
) nil t
)
1629 tags-loop-operate nil
)
1630 (tags-loop-continue (or file-list-form t
))))
1633 (defun tags-query-replace (from to
&optional delimited file-list-form start end
)
1634 "`Query-replace-regexp' FROM with TO through all files listed in tags table.
1635 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1636 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
1637 with the command \\[tags-loop-continue].
1639 See documentation of variable `tags-file-name'."
1640 (interactive (query-replace-read-args "Tags query replace (regexp)" t
))
1641 (setq tags-loop-scan
(list 'prog1
1642 (list 'if
(list 're-search-forward
1643 (list 'quote from
) nil t
)
1644 ;; When we find a match, move back
1645 ;; to the beginning of it so perform-replace
1647 '(goto-char (match-beginning 0))))
1648 tags-loop-operate
(list 'perform-replace
1649 (list 'quote from
) (list 'quote to
) nil nil
1650 t t
(list 'quote delimited
)))
1651 (tags-loop-continue (or file-list-form t
)))
1653 (defun tags-complete-tags-table-file (string predicate what
)
1655 ;; If we need to ask for the tag table, allow that.
1656 (let ((enable-recursive-minibuffers t
))
1657 (visit-tags-table-buffer))
1659 (all-completions string
(mapcar 'list
(tags-table-files))
1661 (try-completion string
(mapcar 'list
(tags-table-files))
1665 (defun list-tags (file &optional next-match
)
1666 "Display list of tags in file FILE.
1667 This searches only the first table in the list, and no included tables.
1668 FILE should be as it appeared in the `etags' command, usually without a
1669 directory specification."
1670 (interactive (list (completing-read "List tags in file: "
1671 'tags-complete-tags-table-file
1673 (with-output-to-temp-buffer "*Tags List*"
1674 (princ "Tags in file `")
1675 (tags-with-face 'highlight
(princ file
))
1678 (let ((first-time t
)
1680 (while (visit-tags-table-buffer (not first-time
))
1681 (setq first-time nil
)
1682 (if (funcall list-tags-function file
)
1685 (error "File %s not in current tags tables" file
)))))
1686 (with-current-buffer "*Tags List*"
1687 (setq buffer-read-only t
)
1691 (defun tags-apropos (regexp)
1692 "Display list of all tags in tags table REGEXP matches."
1693 (interactive "sTags apropos (regexp): ")
1694 (with-output-to-temp-buffer "*Tags List*"
1695 (princ "Click mouse-2 to follow tags.\n\nTags matching regexp `")
1696 (tags-with-face 'highlight
(princ regexp
))
1699 (let ((first-time t
))
1700 (while (visit-tags-table-buffer (not first-time
))
1701 (setq first-time nil
)
1702 (funcall tags-apropos-function regexp
))))
1703 (etags-tags-apropos-additional regexp
))
1704 (with-current-buffer "*Tags List*"
1705 (setq buffer-read-only t
)
1708 ;;; XXX Kludge interface.
1710 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1712 (defun select-tags-table ()
1713 "Select a tags table file from a menu of those you have already used.
1714 The list of tags tables to select from is stored in `tags-table-set-list';
1715 see the doc of that variable if you want to add names to the list."
1717 (pop-to-buffer "*Tags Table List*")
1718 (setq buffer-read-only nil
)
1720 (let ((set-list tags-table-set-list
)
1721 (desired-point nil
))
1722 (when tags-table-list
1723 (setq desired-point
(point-marker))
1724 (princ tags-table-list
(current-buffer))
1726 (prin1 (car tags-table-list
) (current-buffer)) ;invisible
1729 (unless (eq (car set-list
) tags-table-list
)
1730 (princ (car set-list
) (current-buffer))
1732 (prin1 (car (car set-list
)) (current-buffer)) ;invisible
1734 (setq set-list
(cdr set-list
)))
1735 (when tags-file-name
1737 (setq desired-point
(point-marker)))
1738 (insert tags-file-name
"\C-m")
1739 (prin1 tags-file-name
(current-buffer)) ;invisible
1741 (setq set-list
(delete tags-file-name
1742 (apply 'nconc
(cons (copy-sequence tags-table-list
)
1743 (mapcar 'copy-sequence
1744 tags-table-set-list
)))))
1746 (insert (car set-list
) "\C-m")
1747 (prin1 (car set-list
) (current-buffer)) ;invisible
1749 (setq set-list
(delete (car set-list
) set-list
)))
1751 (insert-before-markers
1752 "Type `t' to select a tags table or set of tags tables:\n\n")
1754 (goto-char desired-point
))
1755 (set-window-start (selected-window) 1 t
))
1756 (set-buffer-modified-p nil
)
1757 (select-tags-table-mode))
1759 (defvar select-tags-table-mode-map
)
1760 (let ((map (make-sparse-keymap)))
1761 (define-key map
"t" 'select-tags-table-select
)
1762 (define-key map
" " 'next-line
)
1763 (define-key map
"\^?" 'previous-line
)
1764 (define-key map
"n" 'next-line
)
1765 (define-key map
"p" 'previous-line
)
1766 (define-key map
"q" 'select-tags-table-quit
)
1767 (setq select-tags-table-mode-map map
))
1769 (defun select-tags-table-mode ()
1770 "Major mode for choosing a current tags table among those already loaded.
1772 \\{select-tags-table-mode-map}"
1774 (kill-all-local-variables)
1775 (setq buffer-read-only t
1776 major-mode
'select-tags-table-mode
1777 mode-name
"Select Tags Table")
1778 (use-local-map select-tags-table-mode-map
)
1779 (setq selective-display t
1780 selective-display-ellipses nil
))
1782 (defun select-tags-table-select ()
1783 "Select the tags table named on this line."
1785 (search-forward "\C-m")
1786 (let ((name (read (current-buffer))))
1787 (visit-tags-table name
)
1788 (select-tags-table-quit)
1789 (message "Tags table now %s" name
)))
1791 (defun select-tags-table-quit ()
1792 "Kill the buffer and delete the selected window."
1794 (quit-window t
(selected-window)))
1796 ;;; Note, there is another definition of this function in bindings.el.
1798 (defun complete-tag ()
1799 "Perform tags completion on the text around point.
1800 Completes to the set of names listed in the current tags table.
1801 The string to complete is chosen in the same way as the default
1802 for \\[find-tag] (which see)."
1807 (substitute-command-keys
1808 "No tags table loaded; try \\[visit-tags-table]")))
1809 (let ((pattern (funcall (or find-tag-default-function
1810 (get major-mode
'find-tag-default-function
)
1811 'find-tag-default
)))
1815 (error "Nothing to complete"))
1816 (search-backward pattern
)
1818 (forward-char (length pattern
))
1819 (setq completion
(tags-complete-tag pattern nil nil
))
1820 (cond ((eq completion t
))
1822 (message "Can't find completion for \"%s\"" pattern
)
1824 ((not (string= pattern completion
))
1825 (delete-region beg
(point))
1826 (insert completion
))
1828 (message "Making completion list...")
1829 (with-output-to-temp-buffer "*Completions*"
1830 (display-completion-list
1831 (all-completions pattern
'tags-complete-tag nil
)))
1832 (message "Making completion list...%s" "done")))))
1834 (dolist (x '("^No tags table in use; use .* to select one$"
1835 "^There is no default tag$"
1836 "^No previous tag locations$"
1837 "^File .* is not a valid tags table$"
1838 "^No \\(more \\|\\)tags \\(matching\\|containing\\) "
1839 "^Rerun etags: `.*' not found in "
1840 "^All files processed$"
1841 "^No .* or .* in progress$"
1842 "^File .* not in current tags tables$"
1843 "^No tags table loaded"
1844 "^Nothing to complete$"))
1845 (add-to-list 'debug-ignored-errors x
))
1849 ;;; etags.el ends here