(tags-apropos-additional-actions): Fix :type.
[emacs.git] / lisp / progmodes / etags.el
bloba6fa0f8e2892d80b9cd4949188d5a4c74f139303
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>
6 ;; Maintainer: FSF
7 ;; Keywords: tools
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)
14 ;; any later version.
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.
26 ;;; Commentary:
28 ;;; Code:
30 (require 'ring)
31 (eval-when-compile (require 'cl)) ; for `gensym'
33 ;;;###autoload
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"
43 :group 'tools)
45 ;;;###autoload
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'."
50 :group 'etags
51 :type '(choice (const :tag "Case-sensitive" nil)
52 (const :tag "Case-insensitive" t)
53 (other :tag "Use default" default))
54 :version "21.1")
56 ;;;###autoload
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."
64 :group 'etags
65 :type '(repeat file))
67 ;;;###autoload
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)."
73 :group 'etags
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."
80 :group 'etags
81 :type 'boolean)
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.")
108 ;;;###autoload
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."
113 :group 'etags
114 :type 'hook)
116 ;;;###autoload
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."
122 :group 'etags
123 :type 'function)
125 (defcustom find-tag-marker-ring-length 16
126 "*Length of marker rings `find-tag-marker-ring' and `tags-location-ring'."
127 :group 'etags
128 :type 'integer
129 :version "20.3")
131 (defcustom tags-tag-face 'default
132 "*Face for tags in the output of `tags-apropos'."
133 :group 'etags
134 :type 'face
135 :version "21.1")
137 (defcustom tags-apropos-verbose nil
138 "If non-nil, print the name of the tags file in the *Tags List* buffer."
139 :group 'etags
140 :type 'boolean
141 :version "21.1")
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.
155 Example value:
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))"
160 :group 'etags
161 :type '(repeat (list (string :tag "Title")
162 function
163 (sexp :tag "Tags to search")))
164 :version "21.1")
166 (defvar find-tag-marker-ring (make-ring find-tag-marker-ring-length)
167 "Ring of markers which are locations from which \\[find-tag] was invoked.")
169 (defvar default-tags-table-function nil
170 "If non-nil, a function to choose a default tags file for a buffer.
171 This function receives no arguments and should return the default
172 tags table file to use for the current buffer.")
174 (defvar tags-location-ring (make-ring find-tag-marker-ring-length)
175 "Ring of markers which are locations visited by \\[find-tag].
176 Pop back to the last location with \\[negative-argument] \\[find-tag].")
178 ;; Tags table state.
179 ;; These variables are local in tags table buffers.
181 (defvar tags-table-files nil
182 "List of file names covered by current tags table.
183 nil means it has not yet been computed; use `tags-table-files' to do so.")
185 (defvar tags-completion-table nil
186 "Obarray of tag names defined in current tags table.")
188 (defvar tags-included-tables nil
189 "List of tags tables included by the current tags table.")
191 (defvar next-file-list nil
192 "List of files for \\[next-file] to process.")
194 ;; Hooks for file formats.
196 (defvar tags-table-format-functions '(etags-recognize-tags-table
197 tags-recognize-empty-tags-table)
198 "Hook to be called in a tags table buffer to identify the type of tags table.
199 The functions are called in order, with no arguments,
200 until one returns non-nil. The function should make buffer-local bindings
201 of the format-parsing tags function variables if successful.")
203 (defvar file-of-tag-function nil
204 "Function to do the work of `file-of-tag' (which see).")
205 (defvar tags-table-files-function nil
206 "Function to do the work of `tags-table-files' (which see).")
207 (defvar tags-completion-table-function nil
208 "Function to build the `tags-completion-table'.")
209 (defvar snarf-tag-function nil
210 "Function to get info about a matched tag for `goto-tag-location-function'.")
211 (defvar goto-tag-location-function nil
212 "Function of to go to the location in the buffer specified by a tag.
213 One argument, the tag info returned by `snarf-tag-function'.")
214 (defvar find-tag-regexp-search-function nil
215 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
216 (defvar find-tag-regexp-tag-order nil
217 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
218 (defvar find-tag-regexp-next-line-after-failure-p nil
219 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
220 (defvar find-tag-search-function nil
221 "Search function passed to `find-tag-in-order' for finding a tag.")
222 (defvar find-tag-tag-order nil
223 "Tag order passed to `find-tag-in-order' for finding a tag.")
224 (defvar find-tag-next-line-after-failure-p nil
225 "Flag passed to `find-tag-in-order' for finding a tag.")
226 (defvar list-tags-function nil
227 "Function to do the work of `list-tags' (which see).")
228 (defvar tags-apropos-function nil
229 "Function to do the work of `tags-apropos' (which see).")
230 (defvar tags-included-tables-function nil
231 "Function to do the work of `tags-included-tables' (which see).")
232 (defvar verify-tags-table-function nil
233 "Function to return t iff current buffer contains valid tags file.")
235 ;; Initialize the tags table in the current buffer.
236 ;; Returns non-nil iff it is a valid tags table. On
237 ;; non-nil return, the tags table state variable are
238 ;; made buffer-local and initialized to nil.
239 (defun initialize-new-tags-table ()
240 (set (make-local-variable 'tags-table-files) nil)
241 (set (make-local-variable 'tags-completion-table) nil)
242 (set (make-local-variable 'tags-included-tables) nil)
243 ;; We used to initialize find-tag-marker-ring and tags-location-ring
244 ;; here, to new empty rings. But that is wrong, because those
245 ;; are global.
247 ;; Value is t if we have found a valid tags table buffer.
248 (run-hook-with-args-until-success 'tags-table-format-functions))
250 ;;;###autoload
251 (defun visit-tags-table (file &optional local)
252 "Tell tags commands to use tags table file FILE.
253 FILE should be the name of a file created with the `etags' program.
254 A directory name is ok too; it means file TAGS in that directory.
256 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
257 With a prefix arg, set the buffer-local value instead.
258 When you find a tag with \\[find-tag], the buffer it finds the tag
259 in is given a local value of this variable which is the name of the tags
260 file the tag was in."
261 (interactive (list (read-file-name "Visit tags table: (default TAGS) "
262 default-directory
263 (expand-file-name "TAGS"
264 default-directory)
266 current-prefix-arg))
267 (or (stringp file) (signal 'wrong-type-argument (list 'stringp file)))
268 ;; Bind tags-file-name so we can control below whether the local or
269 ;; global value gets set. Calling visit-tags-table-buffer will
270 ;; initialize a buffer for the file and set tags-file-name to the
271 ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
272 ;; initialize a buffer for FILE and set tags-file-name to the
273 ;; fully-expanded name.
274 (let ((tags-file-name file))
275 (save-excursion
276 (or (visit-tags-table-buffer file)
277 (signal 'file-error (list "Visiting tags table"
278 "file does not exist"
279 file)))
280 ;; Set FILE to the expanded name.
281 (setq file tags-file-name)))
282 (if local
283 ;; Set the local value of tags-file-name.
284 (set (make-local-variable 'tags-file-name) file)
285 ;; Set the global value of tags-file-name.
286 (setq-default tags-file-name file)))
288 (defun tags-table-check-computed-list ()
289 "Compute `tags-table-computed-list' from `tags-table-list' if necessary."
290 (let ((expanded-list (mapcar 'tags-expand-table-name tags-table-list)))
291 (or (equal tags-table-computed-list-for expanded-list)
292 ;; The list (or default-directory) has changed since last computed.
293 (let* ((compute-for (mapcar 'copy-sequence expanded-list))
294 (tables (copy-sequence compute-for)) ;Mutated in the loop.
295 (computed nil)
296 table-buffer)
298 (while tables
299 (setq computed (cons (car tables) computed)
300 table-buffer (get-file-buffer (car tables)))
301 (if (and table-buffer
302 ;; There is a buffer visiting the file. Now make sure
303 ;; it is initialized as a tag table buffer.
304 (save-excursion
305 (tags-verify-table (buffer-file-name table-buffer))))
306 (save-excursion
307 (set-buffer table-buffer)
308 (if (tags-included-tables)
309 ;; Insert the included tables into the list we
310 ;; are processing.
311 (setcdr tables (nconc (mapcar 'tags-expand-table-name
312 (tags-included-tables))
313 (cdr tables)))))
314 ;; This table is not in core yet. Insert a placeholder
315 ;; saying we must read it into core to check for included
316 ;; tables before searching the next table in the list.
317 (setq computed (cons t computed)))
318 (setq tables (cdr tables)))
320 ;; Record the tags-table-list value (and the context of the
321 ;; current directory) we computed from.
322 (setq tags-table-computed-list-for compute-for
323 tags-table-computed-list (nreverse computed))))))
325 ;; Extend `tags-table-computed-list' to remove the first `t' placeholder.
326 ;; An element of the list that is `t' is a placeholder indicating that the
327 ;; preceding element is a table that has not been read into core and might
328 ;; contain included tables to search. On return, the first placeholder
329 ;; element will be gone and the element before it read into core and its
330 ;; included tables inserted into the list.
331 (defun tags-table-extend-computed-list ()
332 (let ((list tags-table-computed-list))
333 (while (not (eq (nth 1 list) t))
334 (setq list (cdr list)))
335 (save-excursion
336 (if (tags-verify-table (car list))
337 ;; We are now in the buffer visiting (car LIST). Extract its
338 ;; list of included tables and insert it into the computed list.
339 (let ((tables (tags-included-tables))
340 (computed nil)
341 table-buffer)
342 (while tables
343 (setq computed (cons (car tables) computed)
344 table-buffer (get-file-buffer (car tables)))
345 (if table-buffer
346 (save-excursion
347 (set-buffer table-buffer)
348 (if (tags-included-tables)
349 ;; Insert the included tables into the list we
350 ;; are processing.
351 (setcdr tables (append (tags-included-tables)
352 tables))))
353 ;; This table is not in core yet. Insert a placeholder
354 ;; saying we must read it into core to check for included
355 ;; tables before searching the next table in the list.
356 (setq computed (cons t computed)))
357 (setq tables (cdr tables)))
358 (setq computed (nreverse computed))
359 ;; COMPUTED now contains the list of included tables (and
360 ;; tables included by them, etc.). Now splice this into the
361 ;; current list.
362 (setcdr list (nconc computed (cdr (cdr list)))))
363 ;; It was not a valid table, so just remove the following placeholder.
364 (setcdr list (cdr (cdr list)))))))
366 ;; Expand tags table name FILE into a complete file name.
367 (defun tags-expand-table-name (file)
368 (setq file (expand-file-name file))
369 (if (file-directory-p file)
370 (expand-file-name "TAGS" file)
371 file))
373 ;; Like member, but comparison is done after tags-expand-table-name on both
374 ;; sides and elements of LIST that are t are skipped.
375 (defun tags-table-list-member (file list)
376 (setq file (tags-expand-table-name file))
377 (while (and list
378 (or (eq (car list) t)
379 (not (string= file (tags-expand-table-name (car list))))))
380 (setq list (cdr list)))
381 list)
383 (defun tags-verify-table (file)
384 "Read FILE into a buffer and verify that it is a valid tags table.
385 Sets the current buffer to one visiting FILE (if it exists).
386 Returns non-nil iff it is a valid table."
387 (if (get-file-buffer file)
388 ;; The file is already in a buffer. Check for the visited file
389 ;; having changed since we last used it.
390 (let (win)
391 (set-buffer (get-file-buffer file))
392 (setq win (or verify-tags-table-function (initialize-new-tags-table)))
393 (if (or (verify-visited-file-modtime (current-buffer))
394 ;; Decide whether to revert the file.
395 ;; revert-without-query can say to revert
396 ;; or the user can say to revert.
397 (not (or (let ((tail revert-without-query)
398 (found nil))
399 (while tail
400 (if (string-match (car tail) buffer-file-name)
401 (setq found t))
402 (setq tail (cdr tail)))
403 found)
404 tags-revert-without-query
405 (yes-or-no-p
406 (format "Tags file %s has changed, read new contents? "
407 file)))))
408 (and verify-tags-table-function
409 (funcall verify-tags-table-function))
410 (revert-buffer t t)
411 (initialize-new-tags-table)))
412 (and (file-exists-p file)
413 (progn
414 (set-buffer (find-file-noselect file))
415 (or (string= file buffer-file-name)
416 ;; find-file-noselect has changed the file name.
417 ;; Propagate the change to tags-file-name and tags-table-list.
418 (let ((tail (member file tags-table-list)))
419 (if tail
420 (setcar tail buffer-file-name))
421 (if (eq file tags-file-name)
422 (setq tags-file-name buffer-file-name))))
423 (initialize-new-tags-table)))))
425 ;; Subroutine of visit-tags-table-buffer. Search the current tags tables
426 ;; for one that has tags for THIS-FILE (or that includes a table that
427 ;; does). Return the name of the first table table listing THIS-FILE; if
428 ;; the table is one included by another table, it is the master table that
429 ;; we return. If CORE-ONLY is non-nil, check only tags tables that are
430 ;; already in buffers--don't visit any new files.
431 (defun tags-table-including (this-file core-only)
432 (let ((tables tags-table-computed-list)
433 (found nil))
434 ;; Loop over the list, looking for a table containing tags for THIS-FILE.
435 (while (and (not found)
436 tables)
438 (if core-only
439 ;; Skip tables not in core.
440 (while (eq (nth 1 tables) t)
441 (setq tables (cdr (cdr tables))))
442 (if (eq (nth 1 tables) t)
443 ;; This table has not been read into core yet. Read it in now.
444 (tags-table-extend-computed-list)))
446 (if tables
447 ;; Select the tags table buffer and get the file list up to date.
448 (let ((tags-file-name (car tables)))
449 (visit-tags-table-buffer 'same)
450 (if (member this-file (mapcar 'expand-file-name
451 (tags-table-files)))
452 ;; Found it.
453 (setq found tables))))
454 (setq tables (cdr tables)))
455 (if found
456 ;; Now determine if the table we found was one included by another
457 ;; table, not explicitly listed. We do this by checking each
458 ;; element of the computed list to see if it appears in the user's
459 ;; explicit list; the last element we will check is FOUND itself.
460 ;; Then we return the last one which did in fact appear in
461 ;; tags-table-list.
462 (let ((could-be nil)
463 (elt tags-table-computed-list))
464 (while (not (eq elt (cdr found)))
465 (if (tags-table-list-member (car elt) tags-table-list)
466 ;; This table appears in the user's list, so it could be
467 ;; the one which includes the table we found.
468 (setq could-be (car elt)))
469 (setq elt (cdr elt))
470 (if (eq t (car elt))
471 (setq elt (cdr elt))))
472 ;; The last element we found in the computed list before FOUND
473 ;; that appears in the user's list will be the table that
474 ;; included the one we found.
475 could-be))))
477 ;; Subroutine of visit-tags-table-buffer. Move tags-table-list-pointer
478 ;; along and set tags-file-name. Returns nil when out of tables.
479 (defun tags-next-table ()
480 ;; If there is a placeholder element next, compute the list to replace it.
481 (while (eq (nth 1 tags-table-list-pointer) t)
482 (tags-table-extend-computed-list))
484 ;; Go to the next table in the list.
485 (setq tags-table-list-pointer (cdr tags-table-list-pointer))
486 (or tags-table-list-pointer
487 ;; Wrap around.
488 (setq tags-table-list-pointer tags-table-computed-list))
490 (if (eq tags-table-list-pointer tags-table-list-started-at)
491 ;; We have come full circle. No more tables.
492 (setq tags-table-list-pointer nil)
493 ;; Set tags-file-name to the name from the list. It is already expanded.
494 (setq tags-file-name (car tags-table-list-pointer))))
496 (defun visit-tags-table-buffer (&optional cont)
497 "Select the buffer containing the current tags table.
498 If optional arg is a string, visit that file as a tags table.
499 If optional arg is t, visit the next table in `tags-table-list'.
500 If optional arg is the atom `same', don't look for a new table;
501 just select the buffer visiting `tags-file-name'.
502 If arg is nil or absent, choose a first buffer from information in
503 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
504 Returns t if it visits a tags table, or nil if there are no more in the list."
506 ;; Set tags-file-name to the tags table file we want to visit.
507 (cond ((eq cont 'same)
508 ;; Use the ambient value of tags-file-name.
509 (or tags-file-name
510 (error "%s"
511 (substitute-command-keys
512 (concat "No tags table in use; "
513 "use \\[visit-tags-table] to select one")))))
515 ((eq t cont)
516 ;; Find the next table.
517 (if (tags-next-table)
518 ;; Skip over nonexistent files.
519 (while (and (not (or (get-file-buffer tags-file-name)
520 (file-exists-p tags-file-name)))
521 (tags-next-table)))))
524 ;; Pick a table out of our hat.
525 (tags-table-check-computed-list) ;Get it up to date, we might use it.
526 (setq tags-file-name
528 ;; If passed a string, use that.
529 (if (stringp cont)
530 (prog1 cont
531 (setq cont nil)))
532 ;; First, try a local variable.
533 (cdr (assq 'tags-file-name (buffer-local-variables)))
534 ;; Second, try a user-specified function to guess.
535 (and default-tags-table-function
536 (funcall default-tags-table-function))
537 ;; Third, look for a tags table that contains tags for the
538 ;; current buffer's file. If one is found, the lists will
539 ;; be frobnicated, and CONT will be set non-nil so we don't
540 ;; do it below.
541 (and buffer-file-name
543 ;; First check only tables already in buffers.
544 (tags-table-including buffer-file-name t)
545 ;; Since that didn't find any, now do the
546 ;; expensive version: reading new files.
547 (tags-table-including buffer-file-name nil)))
548 ;; Fourth, use the user variable tags-file-name, if it is
549 ;; not already in the current list.
550 (and tags-file-name
551 (not (tags-table-list-member tags-file-name
552 tags-table-computed-list))
553 tags-file-name)
554 ;; Fifth, use the user variable giving the table list.
555 ;; Find the first element of the list that actually exists.
556 (let ((list tags-table-list)
557 file)
558 (while (and list
559 (setq file (tags-expand-table-name (car list)))
560 (not (get-file-buffer file))
561 (not (file-exists-p file)))
562 (setq list (cdr list)))
563 (car list))
564 ;; Finally, prompt the user for a file name.
565 (expand-file-name
566 (read-file-name "Visit tags table: (default TAGS) "
567 default-directory
568 "TAGS"
569 t))))))
571 ;; Expand the table name into a full file name.
572 (setq tags-file-name (tags-expand-table-name tags-file-name))
574 (unless (and (eq cont t) (null tags-table-list-pointer))
575 ;; Verify that tags-file-name names a valid tags table.
576 ;; Bind another variable with the value of tags-file-name
577 ;; before we switch buffers, in case tags-file-name is buffer-local.
578 (let ((curbuf (current-buffer))
579 (local-tags-file-name tags-file-name))
580 (if (tags-verify-table local-tags-file-name)
582 ;; We have a valid tags table.
583 (progn
584 ;; Bury the tags table buffer so it
585 ;; doesn't get in the user's way.
586 (bury-buffer (current-buffer))
588 ;; If this was a new table selection (CONT is nil), make
589 ;; sure tags-table-list includes the chosen table, and
590 ;; update the list pointer variables.
591 (or cont
592 ;; Look in the list for the table we chose.
593 (let ((found (tags-table-list-member
594 local-tags-file-name
595 tags-table-computed-list)))
596 (if found
597 ;; There it is. Just switch to it.
598 (setq tags-table-list-pointer found
599 tags-table-list-started-at found)
601 ;; The table is not in the current set.
602 ;; Try to find it in another previously used set.
603 (let ((sets tags-table-set-list))
604 (while (and sets
605 (not (tags-table-list-member
606 local-tags-file-name
607 (car sets))))
608 (setq sets (cdr sets)))
609 (if sets
610 ;; Found in some other set. Switch to that set.
611 (progn
612 (or (memq tags-table-list tags-table-set-list)
613 ;; Save the current list.
614 (setq tags-table-set-list
615 (cons tags-table-list
616 tags-table-set-list)))
617 (setq tags-table-list (car sets)))
619 ;; Not found in any existing set.
620 (if (and tags-table-list
621 (or (eq t tags-add-tables)
622 (and tags-add-tables
623 (y-or-n-p
624 (concat "Keep current list of "
625 "tags tables also? ")))))
626 ;; Add it to the current list.
627 (setq tags-table-list (cons local-tags-file-name
628 tags-table-list))
630 ;; Make a fresh list, and store the old one.
631 (message "Starting a new list of tags tables")
632 (or (null tags-table-list)
633 (memq tags-table-list tags-table-set-list)
634 (setq tags-table-set-list
635 (cons tags-table-list
636 tags-table-set-list)))
637 ;; Clear out buffers holding old tables.
638 (dolist (table tags-table-list)
639 ;; The list can contain items `t'.
640 (if (stringp table)
641 (let ((buffer (find-buffer-visiting table)))
642 (if buffer
643 (kill-buffer buffer)))))
644 (setq tags-table-list (list local-tags-file-name))))
646 ;; Recompute tags-table-computed-list.
647 (tags-table-check-computed-list)
648 ;; Set the tags table list state variables to start
649 ;; over from tags-table-computed-list.
650 (setq tags-table-list-started-at tags-table-computed-list
651 tags-table-list-pointer
652 tags-table-computed-list)))))
654 ;; Return of t says the tags table is valid.
657 ;; The buffer was not valid. Don't use it again.
658 (set-buffer curbuf)
659 (kill-local-variable 'tags-file-name)
660 (if (eq local-tags-file-name tags-file-name)
661 (setq tags-file-name nil))
662 (error "File %s is not a valid tags table" local-tags-file-name)))))
664 (defun tags-reset-tags-tables ()
665 "Reset tags state to cancel effect of any previous \\[visit-tags-table] or \\[find-tag]."
666 (interactive)
667 ;; Clear out the markers we are throwing away.
668 (let ((i 0))
669 (while (< i find-tag-marker-ring-length)
670 (if (aref (cddr tags-location-ring) i)
671 (set-marker (aref (cddr tags-location-ring) i) nil))
672 (if (aref (cddr find-tag-marker-ring) i)
673 (set-marker (aref (cddr find-tag-marker-ring) i) nil))
674 (setq i (1+ i))))
675 (setq tags-file-name nil
676 tags-location-ring (make-ring find-tag-marker-ring-length)
677 find-tag-marker-ring (make-ring find-tag-marker-ring-length)
678 tags-table-list nil
679 tags-table-computed-list nil
680 tags-table-computed-list-for nil
681 tags-table-list-pointer nil
682 tags-table-list-started-at nil
683 tags-table-set-list nil))
685 (defun file-of-tag ()
686 "Return the file name of the file whose tags point is within.
687 Assumes the tags table is the current buffer.
688 File name returned is relative to tags table file's directory."
689 (funcall file-of-tag-function))
691 ;;;###autoload
692 (defun tags-table-files ()
693 "Return a list of files in the current tags table.
694 Assumes the tags table is the current buffer. The file names are returned
695 as they appeared in the `etags' command that created the table, usually
696 without directory names."
697 (or tags-table-files
698 (setq tags-table-files
699 (funcall tags-table-files-function))))
701 (defun tags-included-tables ()
702 "Return a list of tags tables included by the current table.
703 Assumes the tags table is the current buffer."
704 (or tags-included-tables
705 (setq tags-included-tables (funcall tags-included-tables-function))))
707 ;; Build tags-completion-table on demand. The single current tags table
708 ;; and its included tags tables (and their included tables, etc.) have
709 ;; their tags included in the completion table.
710 (defun tags-completion-table ()
711 (or tags-completion-table
712 (condition-case ()
713 (prog2
714 (message "Making tags completion table for %s..." buffer-file-name)
715 (let ((included (tags-included-tables))
716 (table (funcall tags-completion-table-function)))
717 (save-excursion
718 ;; Iterate over the list of included tables, and combine each
719 ;; included table's completion obarray to the parent obarray.
720 (while included
721 ;; Visit the buffer.
722 (let ((tags-file-name (car included)))
723 (visit-tags-table-buffer 'same))
724 ;; Recurse in that buffer to compute its completion table.
725 (if (tags-completion-table)
726 ;; Combine the tables.
727 (mapatoms (lambda (sym) (intern (symbol-name sym) table))
728 tags-completion-table))
729 (setq included (cdr included))))
730 (setq tags-completion-table table))
731 (message "Making tags completion table for %s...done"
732 buffer-file-name))
733 (quit (message "Tags completion table construction aborted.")
734 (setq tags-completion-table nil)))))
736 ;; Completion function for tags. Does normal try-completion,
737 ;; but builds tags-completion-table on demand.
738 (defun tags-complete-tag (string predicate what)
739 (save-excursion
740 ;; If we need to ask for the tag table, allow that.
741 (let ((enable-recursive-minibuffers t))
742 (visit-tags-table-buffer))
743 (if (eq what t)
744 (all-completions string (tags-completion-table) predicate)
745 (try-completion string (tags-completion-table) predicate))))
747 ;; Return a default tag to search for, based on the text at point.
748 (defun find-tag-default ()
749 (save-excursion
750 (while (looking-at "\\sw\\|\\s_")
751 (forward-char 1))
752 (if (or (re-search-backward "\\sw\\|\\s_"
753 (save-excursion (beginning-of-line) (point))
755 (re-search-forward "\\(\\sw\\|\\s_\\)+"
756 (save-excursion (end-of-line) (point))
758 (progn (goto-char (match-end 0))
759 (buffer-substring (point)
760 (progn (forward-sexp -1)
761 (while (looking-at "\\s'")
762 (forward-char 1))
763 (point))))
764 nil)))
766 ;; Read a tag name from the minibuffer with defaulting and completion.
767 (defun find-tag-tag (string)
768 (let* ((default (funcall (or find-tag-default-function
769 (get major-mode 'find-tag-default-function)
770 'find-tag-default)))
771 (spec (completing-read (if default
772 (format "%s(default %s) " string default)
773 string)
774 'tags-complete-tag
775 nil nil nil nil default)))
776 (if (equal spec "")
777 (or default (error "There is no default tag"))
778 spec)))
780 (defvar last-tag nil
781 "Last tag found by \\[find-tag].")
783 ;; Get interactive args for find-tag{-noselect,-other-window,-regexp}.
784 (defun find-tag-interactive (prompt &optional no-default)
785 (if current-prefix-arg
786 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
789 (list (if no-default
790 (read-string prompt)
791 (find-tag-tag prompt)))))
793 (defvar find-tag-history nil)
795 ;; Dynamic bondage:
796 (eval-when-compile
797 (defvar etags-case-fold-search)
798 (defvar etags-syntax-table))
800 ;;;###autoload
801 (defun find-tag-noselect (tagname &optional next-p regexp-p)
802 "Find tag (in current tags table) whose name contains TAGNAME.
803 Returns the buffer containing the tag's definition and moves its point there,
804 but does not select the buffer.
805 The default for TAGNAME is the expression in the buffer near point.
807 If second arg NEXT-P is t (interactively, with prefix arg), search for
808 another tag that matches the last tagname or regexp used. When there are
809 multiple matches for a tag, more exact matches are found first. If NEXT-P
810 is the atom `-' (interactively, with prefix arg that is a negative number
811 or just \\[negative-argument]), pop back to the previous tag gone to.
813 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
815 A marker representing the point when this command is onvoked is pushed
816 onto a ring and may be popped back to with \\[pop-tag-mark].
817 Contrast this with the ring of marks gone to by the command.
819 See documentation of variable `tags-file-name'."
820 (interactive (find-tag-interactive "Find tag: "))
822 (setq find-tag-history (cons tagname find-tag-history))
823 ;; Save the current buffer's value of `find-tag-hook' before selecting the
824 ;; tags table buffer.
825 (let ((local-find-tag-hook find-tag-hook))
826 (if (eq '- next-p)
827 ;; Pop back to a previous location.
828 (if (ring-empty-p tags-location-ring)
829 (error "No previous tag locations")
830 (let ((marker (ring-remove tags-location-ring 0)))
831 (prog1
832 ;; Move to the saved location.
833 (set-buffer (or (marker-buffer marker)
834 (error "The marked buffer has been deleted")))
835 (goto-char (marker-position marker))
836 ;; Kill that marker so it doesn't slow down editing.
837 (set-marker marker nil nil)
838 ;; Run the user's hook. Do we really want to do this for pop?
839 (run-hooks 'local-find-tag-hook))))
840 ;; Record whence we came.
841 (ring-insert find-tag-marker-ring (point-marker))
842 (if next-p
843 ;; Find the same table we last used.
844 (visit-tags-table-buffer 'same)
845 ;; Pick a table to use.
846 (visit-tags-table-buffer)
847 ;; Record TAGNAME for a future call with NEXT-P non-nil.
848 (setq last-tag tagname))
849 ;; Record the location so we can pop back to it later.
850 (let ((marker (make-marker)))
851 (save-excursion
852 (set-buffer
853 ;; find-tag-in-order does the real work.
854 (find-tag-in-order
855 (if next-p last-tag tagname)
856 (if regexp-p
857 find-tag-regexp-search-function
858 find-tag-search-function)
859 (if regexp-p
860 find-tag-regexp-tag-order
861 find-tag-tag-order)
862 (if regexp-p
863 find-tag-regexp-next-line-after-failure-p
864 find-tag-next-line-after-failure-p)
865 (if regexp-p "matching" "containing")
866 (not next-p)))
867 (set-marker marker (point))
868 (run-hooks 'local-find-tag-hook)
869 (ring-insert tags-location-ring marker)
870 (current-buffer))))))
872 ;;;###autoload
873 (defun find-tag (tagname &optional next-p regexp-p)
874 "Find tag (in current tags table) whose name contains TAGNAME.
875 Select the buffer containing the tag's definition, and move point there.
876 The default for TAGNAME is the expression in the buffer around or before point.
878 If second arg NEXT-P is t (interactively, with prefix arg), search for
879 another tag that matches the last tagname or regexp used. When there are
880 multiple matches for a tag, more exact matches are found first. If NEXT-P
881 is the atom `-' (interactively, with prefix arg that is a negative number
882 or just \\[negative-argument]), pop back to the previous tag gone to.
884 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
886 A marker representing the point when this command is onvoked is pushed
887 onto a ring and may be popped back to with \\[pop-tag-mark].
888 Contrast this with the ring of marks gone to by the command.
890 See documentation of variable `tags-file-name'."
891 (interactive (find-tag-interactive "Find tag: "))
892 (let ((buf (find-tag-noselect tagname next-p regexp-p)))
893 (condition-case nil
894 (switch-to-buffer buf)
895 (error (pop-to-buffer buf)))))
896 ;;;###autoload (define-key esc-map "." 'find-tag)
898 ;;;###autoload
899 (defun find-tag-other-window (tagname &optional next-p regexp-p)
900 "Find tag (in current tags table) whose name contains TAGNAME.
901 Select the buffer containing the tag's definition in another window, and
902 move point there. The default for TAGNAME is the expression in the buffer
903 around or before point.
905 If second arg NEXT-P is t (interactively, with prefix arg), search for
906 another tag that matches the last tagname or regexp used. When there are
907 multiple matches for a tag, more exact matches are found first. If NEXT-P
908 is negative (interactively, with prefix arg that is a negative number or
909 just \\[negative-argument]), pop back to the previous tag gone to.
911 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
913 A marker representing the point when this command is onvoked is pushed
914 onto a ring and may be popped back to with \\[pop-tag-mark].
915 Contrast this with the ring of marks gone to by the command.
917 See documentation of variable `tags-file-name'."
918 (interactive (find-tag-interactive "Find tag other window: "))
920 ;; This hair is to deal with the case where the tag is found in the
921 ;; selected window's buffer; without the hair, point is moved in both
922 ;; windows. To prevent this, we save the selected window's point before
923 ;; doing find-tag-noselect, and restore it after.
924 (let* ((window-point (window-point (selected-window)))
925 (tagbuf (find-tag-noselect tagname next-p regexp-p))
926 (tagpoint (progn (set-buffer tagbuf) (point))))
927 (set-window-point (prog1
928 (selected-window)
929 (switch-to-buffer-other-window tagbuf)
930 ;; We have to set this new window's point; it
931 ;; might already have been displaying a
932 ;; different portion of tagbuf, in which case
933 ;; switch-to-buffer-other-window doesn't set
934 ;; the window's point from the buffer.
935 (set-window-point (selected-window) tagpoint))
936 window-point)))
937 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
939 ;;;###autoload
940 (defun find-tag-other-frame (tagname &optional next-p)
941 "Find tag (in current tags table) whose name contains TAGNAME.
942 Select the buffer containing the tag's definition in another frame, and
943 move point there. The default for TAGNAME is the expression in the buffer
944 around or before point.
946 If second arg NEXT-P is t (interactively, with prefix arg), search for
947 another tag that matches the last tagname or regexp used. When there are
948 multiple matches for a tag, more exact matches are found first. If NEXT-P
949 is negative (interactively, with prefix arg that is a negative number or
950 just \\[negative-argument]), pop back to the previous tag gone to.
952 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
954 A marker representing the point when this command is onvoked is pushed
955 onto a ring and may be popped back to with \\[pop-tag-mark].
956 Contrast this with the ring of marks gone to by the command.
958 See documentation of variable `tags-file-name'."
959 (interactive (find-tag-interactive "Find tag other frame: "))
960 (let ((pop-up-frames t))
961 (find-tag-other-window tagname next-p)))
962 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
964 ;;;###autoload
965 (defun find-tag-regexp (regexp &optional next-p other-window)
966 "Find tag (in current tags table) whose name matches REGEXP.
967 Select the buffer containing the tag's definition and move point there.
969 If second arg NEXT-P is t (interactively, with prefix arg), search for
970 another tag that matches the last tagname or regexp used. When there are
971 multiple matches for a tag, more exact matches are found first. If NEXT-P
972 is negative (interactively, with prefix arg that is a negative number or
973 just \\[negative-argument]), pop back to the previous tag gone to.
975 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
977 A marker representing the point when this command is onvoked is pushed
978 onto a ring and may be popped back to with \\[pop-tag-mark].
979 Contrast this with the ring of marks gone to by the command.
981 See documentation of variable `tags-file-name'."
982 (interactive (find-tag-interactive "Find tag regexp: " t))
983 ;; We go through find-tag-other-window to do all the display hair there.
984 (funcall (if other-window 'find-tag-other-window 'find-tag)
985 regexp next-p t))
986 ;;;###autoload (define-key esc-map [?\C-.] 'find-tag-regexp)
988 ;;;###autoload (define-key esc-map "*" 'pop-tag-mark)
990 ;;;###autoload
991 (defun pop-tag-mark ()
992 "Pop back to where \\[find-tag] was last invoked.
994 This is distinct from invoking \\[find-tag] with a negative argument
995 since that pops a stack of markers at which tags were found, not from
996 where they were found."
997 (interactive)
998 (if (ring-empty-p find-tag-marker-ring)
999 (error "No previous locations for find-tag invocation"))
1000 (let ((marker (ring-remove find-tag-marker-ring 0)))
1001 (switch-to-buffer (or (marker-buffer marker)
1002 (error "The marked buffer has been deleted")))
1003 (goto-char (marker-position marker))
1004 (set-marker marker nil nil)))
1006 ;; Internal tag finding function.
1008 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
1009 ;; any member of the function list ORDER (third arg). If ORDER is nil,
1010 ;; use saved state to continue a previous search.
1012 ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
1013 ;; an error message.
1015 ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
1016 ;; point should be moved to the next line.
1018 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
1019 ;; beginning of tags file, and perform inner loop: for each naive match for
1020 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
1021 ;; qualifier-func. If it qualifies, go to the specified line in the
1022 ;; specified source file and return. Qualified matches are remembered to
1023 ;; avoid repetition. State is saved so that the loop can be continued.
1025 (defvar tag-lines-already-matched nil) ;matches remembered here between calls
1027 (defun find-tag-in-order (pattern
1028 search-forward-func
1029 order
1030 next-line-after-failure-p
1031 matching
1032 first-search)
1033 (let (file ;name of file containing tag
1034 tag-info ;where to find the tag in FILE
1035 (first-table t)
1036 (tag-order order)
1037 (match-marker (make-marker))
1038 goto-func
1039 (case-fold-search (if (memq tags-case-fold-search '(nil t))
1040 tags-case-fold-search
1041 case-fold-search))
1043 (save-excursion
1045 (if first-search
1046 ;; This is the start of a search for a fresh tag.
1047 ;; Clear the list of tags matched by the previous search.
1048 ;; find-tag-noselect has already put us in the first tags table
1049 ;; buffer before we got called.
1050 (setq tag-lines-already-matched nil)
1051 ;; Continuing to search for the tag specified last time.
1052 ;; tag-lines-already-matched lists locations matched in previous
1053 ;; calls so we don't visit the same tag twice if it matches twice
1054 ;; during two passes with different qualification predicates.
1055 ;; Switch to the current tags table buffer.
1056 (visit-tags-table-buffer 'same))
1058 ;; Get a qualified match.
1059 (catch 'qualified-match-found
1061 ;; Iterate over the list of tags tables.
1062 (while (or first-table
1063 (visit-tags-table-buffer t))
1065 (and first-search first-table
1066 ;; Start at beginning of tags file.
1067 (goto-char (point-min)))
1069 (setq first-table nil)
1071 ;; Iterate over the list of ordering predicates.
1072 (while order
1073 (while (funcall search-forward-func pattern nil t)
1074 ;; Naive match found. Qualify the match.
1075 (and (funcall (car order) pattern)
1076 ;; Make sure it is not a previous qualified match.
1077 (not (member (set-marker match-marker (save-excursion
1078 (beginning-of-line)
1079 (point)))
1080 tag-lines-already-matched))
1081 (throw 'qualified-match-found nil))
1082 (if next-line-after-failure-p
1083 (forward-line 1)))
1084 ;; Try the next flavor of match.
1085 (setq order (cdr order))
1086 (goto-char (point-min)))
1087 (setq order tag-order))
1088 ;; We throw out on match, so only get here if there were no matches.
1089 ;; Clear out the markers we use to avoid duplicate matches so they
1090 ;; don't slow down editting and are immediately available for GC.
1091 (while tag-lines-already-matched
1092 (set-marker (car tag-lines-already-matched) nil nil)
1093 (setq tag-lines-already-matched (cdr tag-lines-already-matched)))
1094 (set-marker match-marker nil nil)
1095 (error "No %stags %s %s" (if first-search "" "more ")
1096 matching pattern))
1098 ;; Found a tag; extract location info.
1099 (beginning-of-line)
1100 (setq tag-lines-already-matched (cons match-marker
1101 tag-lines-already-matched))
1102 ;; Expand the filename, using the tags table buffer's default-directory.
1103 ;; We should be able to search for file-name backwards in file-of-tag:
1104 ;; the beginning-of-line is ok except when positionned on a "file-name" tag.
1105 (setq file (expand-file-name
1106 (if (or (eq (car order) 'tag-exact-file-name-match-p)
1107 (eq (car order) 'tag-partial-file-name-match-p))
1108 (save-excursion (next-line 1)
1109 (file-of-tag))
1110 (file-of-tag)))
1111 tag-info (funcall snarf-tag-function))
1113 ;; Get the local value in the tags table buffer before switching buffers.
1114 (setq goto-func goto-tag-location-function)
1116 ;; Find the right line in the specified file.
1117 (set-buffer (find-file-noselect file))
1118 (widen)
1119 (push-mark)
1120 (funcall goto-func tag-info)
1122 ;; Return the buffer where the tag was found.
1123 (current-buffer))))
1125 ;; `etags' TAGS file format support.
1127 ;; If the current buffer is a valid etags TAGS file, give it local values of
1128 ;; the tags table format variables, and return non-nil.
1129 (defun etags-recognize-tags-table ()
1130 (and (etags-verify-tags-table)
1131 ;; It is annoying to flash messages on the screen briefly,
1132 ;; and this message is not useful. -- rms
1133 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
1134 (mapc (lambda (elt) (set (make-local-variable (car elt)) (cdr elt)))
1135 '((file-of-tag-function . etags-file-of-tag)
1136 (tags-table-files-function . etags-tags-table-files)
1137 (tags-completion-table-function . etags-tags-completion-table)
1138 (snarf-tag-function . etags-snarf-tag)
1139 (goto-tag-location-function . etags-goto-tag-location)
1140 (find-tag-regexp-search-function . re-search-forward)
1141 (find-tag-regexp-tag-order . (tag-re-match-p))
1142 (find-tag-regexp-next-line-after-failure-p . t)
1143 (find-tag-search-function . search-forward)
1144 (find-tag-tag-order . (tag-exact-file-name-match-p
1145 tag-exact-match-p
1146 tag-symbol-match-p
1147 tag-word-match-p
1148 tag-partial-file-name-match-p
1149 tag-any-match-p))
1150 (find-tag-next-line-after-failure-p . nil)
1151 (list-tags-function . etags-list-tags)
1152 (tags-apropos-function . etags-tags-apropos)
1153 (tags-included-tables-function . etags-tags-included-tables)
1154 (verify-tags-table-function . etags-verify-tags-table)
1155 ))))
1157 ;; Return non-nil iff the current buffer is a valid etags TAGS file.
1158 (defun etags-verify-tags-table ()
1159 ;; Use eq instead of = in case char-after returns nil.
1160 (eq (char-after 1) ?\f))
1162 (defun etags-file-of-tag ()
1163 (save-excursion
1164 (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n")
1165 (expand-file-name (buffer-substring (match-beginning 1) (match-end 1))
1166 (file-truename default-directory))))
1169 (defun etags-tags-completion-table ()
1170 (let ((table (make-vector 511 0)))
1171 (save-excursion
1172 (goto-char (point-min))
1173 ;; This monster regexp matches an etags tag line.
1174 ;; \1 is the string to match;
1175 ;; \2 is not interesting;
1176 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
1177 ;; \4 is not interesting;
1178 ;; \5 is the explicitly-specified tag name.
1179 ;; \6 is the line to start searching at;
1180 ;; \7 is the char to start searching at.
1181 (while (re-search-forward
1182 "^\\(\\([^\177]+[^-a-zA-Z0-9_+*$:\177]+\\)?\
1183 \\([-a-zA-Z0-9_+*$?:]+\\)[^-a-zA-Z0-9_+*$?:\177]*\\)\177\
1184 \\(\\([^\n\001]+\\)\001\\)?\\([0-9]+\\)?,\\([0-9]+\\)?\n"
1185 nil t)
1186 (intern (if (match-beginning 5)
1187 ;; There is an explicit tag name.
1188 (buffer-substring (match-beginning 5) (match-end 5))
1189 ;; No explicit tag name. Best guess.
1190 (buffer-substring (match-beginning 3) (match-end 3)))
1191 table)))
1192 table))
1194 (defun etags-snarf-tag ()
1195 (let (tag-text line startpos)
1196 (if (save-excursion
1197 (forward-line -1)
1198 (looking-at "\f\n"))
1199 ;; The match was for a source file name, not any tag within a file.
1200 ;; Give text of t, meaning to go exactly to the location we specify,
1201 ;; the beginning of the file.
1202 (setq tag-text t
1203 line nil
1204 startpos 1)
1206 ;; Find the end of the tag and record the whole tag text.
1207 (search-forward "\177")
1208 (setq tag-text (buffer-substring (1- (point))
1209 (save-excursion (beginning-of-line)
1210 (point))))
1211 ;; Skip explicit tag name if present.
1212 (search-forward "\001" (save-excursion (forward-line 1) (point)) t)
1213 (if (looking-at "[0-9]")
1214 (setq line (string-to-int (buffer-substring
1215 (point)
1216 (progn (skip-chars-forward "0-9")
1217 (point))))))
1218 (search-forward ",")
1219 (if (looking-at "[0-9]")
1220 (setq startpos (string-to-int (buffer-substring
1221 (point)
1222 (progn (skip-chars-forward "0-9")
1223 (point)))))))
1224 ;; Leave point on the next line of the tags file.
1225 (forward-line 1)
1226 (cons tag-text (cons line startpos))))
1228 ;; TAG-INFO is a cons (TEXT LINE . POSITION) where TEXT is the initial part
1229 ;; of a line containing the tag and POSITION is the character position of
1230 ;; TEXT within the file (starting from 1); LINE is the line number. If
1231 ;; TEXT is t, it means the tag refers to exactly LINE or POSITION
1232 ;; (whichever is present, LINE having preference, no searching. Either
1233 ;; LINE or POSITION may be nil; POSITION is used if present. If the tag
1234 ;; isn't exactly at the given position then look around that position using
1235 ;; a search window which expands until it hits the start of file.
1236 (defun etags-goto-tag-location (tag-info)
1237 (let ((startpos (cdr (cdr tag-info)))
1238 (line (car (cdr tag-info)))
1239 offset found pat)
1240 (if (eq (car tag-info) t)
1241 ;; Direct file tag.
1242 (cond (line (goto-line line))
1243 (startpos (goto-char startpos))
1244 (t (error "etags.el BUG: bogus direct file tag")))
1245 ;; This constant is 1/2 the initial search window.
1246 ;; There is no sense in making it too small,
1247 ;; since just going around the loop once probably
1248 ;; costs about as much as searching 2000 chars.
1249 (setq offset 1000
1250 found nil
1251 pat (concat (if (eq selective-display t)
1252 "\\(^\\|\^m\\)" "^")
1253 (regexp-quote (car tag-info))))
1254 ;; The character position in the tags table is 0-origin.
1255 ;; Convert it to a 1-origin Emacs character position.
1256 (if startpos (setq startpos (1+ startpos)))
1257 ;; If no char pos was given, try the given line number.
1258 (or startpos
1259 (if line
1260 (setq startpos (progn (goto-line line)
1261 (point)))))
1262 (or startpos
1263 (setq startpos (point-min)))
1264 ;; First see if the tag is right at the specified location.
1265 (goto-char startpos)
1266 (setq found (looking-at pat))
1267 (while (and (not found)
1268 (progn
1269 (goto-char (- startpos offset))
1270 (not (bobp))))
1271 (setq found
1272 (re-search-forward pat (+ startpos offset) t)
1273 offset (* 3 offset))) ; expand search window
1274 (or found
1275 (re-search-forward pat nil t)
1276 (error "Rerun etags: `%s' not found in %s"
1277 pat buffer-file-name)))
1278 ;; Position point at the right place
1279 ;; if the search string matched an extra Ctrl-m at the beginning.
1280 (and (eq selective-display t)
1281 (looking-at "\^m")
1282 (forward-char 1))
1283 (beginning-of-line)))
1285 (defun etags-list-tags (file)
1286 (goto-char 1)
1287 (when (search-forward (concat "\f\n" file ",") nil t)
1288 (forward-line 1)
1289 (while (not (or (eobp) (looking-at "\f")))
1290 (let ((tag (buffer-substring (point)
1291 (progn (skip-chars-forward "^\177")
1292 (point))))
1293 (props `(action find-tag-other-window mouse-face highlight
1294 face ,tags-tag-face))
1295 (pt (with-current-buffer standard-output (point))))
1296 (when (looking-at "[^\n]+\001")
1297 ;; There is an explicit tag name; use that.
1298 (setq tag (buffer-substring (1+ (point)) ; skip \177
1299 (progn (skip-chars-forward "^\001")
1300 (point)))))
1301 (princ tag)
1302 (when (= (aref tag 0) ?\() (princ " ...)"))
1303 (add-text-properties pt (with-current-buffer standard-output (point))
1304 (cons 'item (cons tag props)) standard-output))
1305 (terpri)
1306 (forward-line 1))
1309 (defmacro tags-with-face (face &rest body)
1310 "Execute BODY, give output to `standard-output' face FACE."
1311 (let ((pp (gensym "twf-")))
1312 `(let ((,pp (with-current-buffer standard-output (point))))
1313 ,@body
1314 (put-text-property ,pp (with-current-buffer standard-output (point))
1315 'face ,face standard-output))))
1317 (defun etags-tags-apropos-additional (regexp)
1318 "Display tags matching REGEXP from `tags-apropos-additional-actions'."
1319 (with-current-buffer standard-output
1320 (dolist (oba tags-apropos-additional-actions)
1321 (princ "\n\n")
1322 (tags-with-face 'highlight (princ (car oba)))
1323 (princ":\n\n")
1324 (let* ((props `(action ,(cadr oba) mouse-face highlight face
1325 ,tags-tag-face))
1326 (beg (point))
1327 (symbs (car (cddr oba)))
1328 (ins-symb (lambda (sy)
1329 (let ((sn (symbol-name sy)))
1330 (when (string-match regexp sn)
1331 (add-text-properties (point)
1332 (progn (princ sy) (point))
1333 (cons 'item (cons sn props)))
1334 (terpri))))))
1335 (when (symbolp symbs)
1336 (if (boundp symbs)
1337 (setq symbs (symbol-value symbs))
1338 (insert "symbol `" (symbol-name symbs) "' has no value\n")
1339 (setq symbs nil)))
1340 (if (vectorp symbs)
1341 (mapatoms ins-symb symbs)
1342 (dolist (sy symbs)
1343 (funcall ins-symb (car sy))))
1344 (sort-lines nil beg (point))))))
1346 (defun etags-tags-apropos (string)
1347 (when tags-apropos-verbose
1348 (princ "Tags in file `")
1349 (tags-with-face 'highlight (princ buffer-file-name))
1350 (princ "':\n\n"))
1351 (goto-char 1)
1352 (while (re-search-forward string nil t)
1353 (beginning-of-line)
1354 (let ((tag (buffer-substring (point)
1355 (progn (skip-chars-forward "^\177")
1356 (point))))
1357 (props `(action find-tag-other-window mouse-face highlight
1358 face ,tags-tag-face))
1359 (pt (with-current-buffer standard-output (point))))
1360 (princ tag)
1361 (when (= (aref tag 0) ?\() (princ " ...)"))
1362 (add-text-properties pt (with-current-buffer standard-output (point))
1363 `(item ,tag ,@props) standard-output))
1364 (terpri)
1365 (forward-line 1))
1366 (when tags-apropos-verbose (princ "\n")))
1368 (defun etags-tags-table-files ()
1369 (let ((files nil)
1370 beg)
1371 (goto-char (point-min))
1372 (while (search-forward "\f\n" nil t)
1373 (setq beg (point))
1374 (end-of-line)
1375 (skip-chars-backward "^," beg)
1376 (or (looking-at "include$")
1377 (setq files (cons (buffer-substring beg (1- (point))) files))))
1378 (nreverse files)))
1380 (defun etags-tags-included-tables ()
1381 (let ((files nil)
1382 beg)
1383 (goto-char (point-min))
1384 (while (search-forward "\f\n" nil t)
1385 (setq beg (point))
1386 (end-of-line)
1387 (skip-chars-backward "^," beg)
1388 (if (looking-at "include$")
1389 ;; Expand in the default-directory of the tags table buffer.
1390 (setq files (cons (expand-file-name (buffer-substring beg (1- (point))))
1391 files))))
1392 (nreverse files)))
1394 ;; Empty tags file support.
1396 ;; Recognize an empty file and give it local values of the tags table format
1397 ;; variables which do nothing.
1398 (defun tags-recognize-empty-tags-table ()
1399 (and (zerop (buffer-size))
1400 (mapc (lambda (sym) (set (make-local-variable sym) 'ignore))
1401 '(tags-table-files-function
1402 tags-completion-table-function
1403 find-tag-regexp-search-function
1404 find-tag-search-function
1405 tags-apropos-function
1406 tags-included-tables-function))
1407 (set (make-local-variable 'verify-tags-table-function)
1408 (lambda () (zerop (buffer-size))))))
1410 ;; Match qualifier functions for tagnames.
1411 ;; XXX these functions assume etags file format.
1413 ;; This might be a neat idea, but it's too hairy at the moment.
1414 ;;(defmacro tags-with-syntax (&rest body)
1415 ;; `(let ((current (current-buffer))
1416 ;; (otable (syntax-table))
1417 ;; (buffer (find-file-noselect (file-of-tag)))
1418 ;; table)
1419 ;; (unwind-protect
1420 ;; (progn
1421 ;; (set-buffer buffer)
1422 ;; (setq table (syntax-table))
1423 ;; (set-buffer current)
1424 ;; (set-syntax-table table)
1425 ;; ,@body)
1426 ;; (set-syntax-table otable))))
1427 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
1429 ;; t if point is at a tag line that matches TAG exactly.
1430 ;; point should be just after a string that matches TAG.
1431 (defun tag-exact-match-p (tag)
1432 ;; The match is really exact if there is an explicit tag name.
1433 (or (and (eq (char-after (point)) ?\001)
1434 (eq (char-after (- (point) (length tag) 1)) ?\177))
1435 ;; We are not on the explicit tag name, but perhaps it follows.
1436 (looking-at (concat "[^\177\n]*\177" (regexp-quote tag) "\001"))))
1438 ;; t if point is at a tag line that matches TAG as a symbol.
1439 ;; point should be just after a string that matches TAG.
1440 (defun tag-symbol-match-p (tag)
1441 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1442 (save-excursion
1443 (backward-char (1+ (length tag)))
1444 (and (looking-at "\\Sw") (looking-at "\\S_")))))
1446 ;; t if point is at a tag line that matches TAG as a word.
1447 ;; point should be just after a string that matches TAG.
1448 (defun tag-word-match-p (tag)
1449 (and (looking-at "\\b.*\177")
1450 (save-excursion (backward-char (length tag))
1451 (looking-at "\\b"))))
1453 (defun tag-exact-file-name-match-p (tag)
1454 (and (looking-at ",")
1455 (save-excursion (backward-char (+ 2 (length tag)))
1456 (looking-at "\f\n"))))
1457 (defun tag-partial-file-name-match-p (tag)
1458 (and (looking-at ".*,")
1459 (save-excursion (beginning-of-line)
1460 (backward-char 2)
1461 (looking-at "\f\n"))))
1463 ;; t if point is in a tag line with a tag containing TAG as a substring.
1464 (defun tag-any-match-p (tag)
1465 (looking-at ".*\177"))
1467 ;; t if point is at a tag line that matches RE as a regexp.
1468 (defun tag-re-match-p (re)
1469 (save-excursion
1470 (beginning-of-line)
1471 (let ((bol (point)))
1472 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t)
1473 (re-search-backward re bol t)))))
1475 (defcustom tags-loop-revert-buffers nil
1476 "*Non-nil means tags-scanning loops should offer to reread changed files.
1477 These loops normally read each file into Emacs, but when a file
1478 is already visited, they use the existing buffer.
1479 When this flag is non-nil, they offer to revert the existing buffer
1480 in the case where the file has changed since you visited it."
1481 :type 'boolean
1482 :group 'etags)
1484 ;;;###autoload
1485 (defun next-file (&optional initialize novisit)
1486 "Select next file among files in current tags table.
1488 A first argument of t (prefix arg, if interactive) initializes to the
1489 beginning of the list of files in the tags table. If the argument is
1490 neither nil nor t, it is evalled to initialize the list of files.
1492 Non-nil second argument NOVISIT means use a temporary buffer
1493 to save time and avoid uninteresting warnings.
1495 Value is nil if the file was already visited;
1496 if the file was newly read in, the value is the filename."
1497 ;; Make the interactive arg t if there was any prefix arg.
1498 (interactive (list (if current-prefix-arg t)))
1499 (cond ((not initialize)
1500 ;; Not the first run.
1502 ((eq initialize t)
1503 ;; Initialize the list from the tags table.
1504 (save-excursion
1505 ;; Visit the tags table buffer to get its list of files.
1506 (visit-tags-table-buffer)
1507 ;; Copy the list so we can setcdr below, and expand the file
1508 ;; names while we are at it, in this buffer's default directory.
1509 (setq next-file-list (mapcar 'expand-file-name (tags-table-files)))
1510 ;; Iterate over all the tags table files, collecting
1511 ;; a complete list of referenced file names.
1512 (while (visit-tags-table-buffer t)
1513 ;; Find the tail of the working list and chain on the new
1514 ;; sublist for this tags table.
1515 (let ((tail next-file-list))
1516 (while (cdr tail)
1517 (setq tail (cdr tail)))
1518 ;; Use a copy so the next loop iteration will not modify the
1519 ;; list later returned by (tags-table-files).
1520 (if tail
1521 (setcdr tail (mapcar 'expand-file-name (tags-table-files)))
1522 (setq next-file-list (mapcar 'expand-file-name
1523 (tags-table-files))))))))
1525 ;; Initialize the list by evalling the argument.
1526 (setq next-file-list (eval initialize))))
1527 (unless next-file-list
1528 (and novisit
1529 (get-buffer " *next-file*")
1530 (kill-buffer " *next-file*"))
1531 (error "All files processed"))
1532 (let* ((next (car next-file-list))
1533 (buffer (get-file-buffer next))
1534 (new (not buffer)))
1535 ;; Advance the list before trying to find the file.
1536 ;; If we get an error finding the file, don't get stuck on it.
1537 (setq next-file-list (cdr next-file-list))
1538 ;; Optionally offer to revert buffers
1539 ;; if the files have changed on disk.
1540 (and buffer tags-loop-revert-buffers
1541 (not (verify-visited-file-modtime buffer))
1542 (with-current-buffer buffer
1543 (revert-buffer t)))
1544 (if (not (and new novisit))
1545 (set-buffer (find-file-noselect next novisit))
1546 ;; Like find-file, but avoids random warning messages.
1547 (set-buffer (get-buffer-create " *next-file*"))
1548 (kill-all-local-variables)
1549 (erase-buffer)
1550 (setq new next)
1551 (insert-file-contents new nil))
1552 new))
1554 (defvar tags-loop-operate nil
1555 "Form for `tags-loop-continue' to eval to change one file.")
1557 (defvar tags-loop-scan
1558 '(error "%s"
1559 (substitute-command-keys
1560 "No \\[tags-search] or \\[tags-query-replace] in progress"))
1561 "Form for `tags-loop-continue' to eval to scan one file.
1562 If it returns non-nil, this file needs processing by evalling
1563 \`tags-loop-operate'. Otherwise, move on to the next file.")
1565 (defun tags-loop-eval (form)
1566 "Evaluate FORM and return its result.
1567 Bind `case-fold-search' during the evaluation, depending on the value of
1568 `tags-case-fold-search'."
1569 (let ((case-fold-search (if (memq tags-case-fold-search '(t nil))
1570 tags-case-fold-search
1571 case-fold-search)))
1572 (eval form)))
1575 ;;;###autoload
1576 (defun tags-loop-continue (&optional first-time)
1577 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1578 Used noninteractively with non-nil argument to begin such a command (the
1579 argument is passed to `next-file', which see).
1581 Two variables control the processing we do on each file: the value of
1582 `tags-loop-scan' is a form to be executed on each file to see if it is
1583 interesting (it returns non-nil if so) and `tags-loop-operate' is a form to
1584 evaluate to operate on an interesting file. If the latter evaluates to
1585 nil, we exit; otherwise we scan the next file."
1586 (interactive)
1587 (let (new
1588 ;; Non-nil means we have finished one file
1589 ;; and should not scan it again.
1590 file-finished
1591 (messaged nil))
1592 (while
1593 (progn
1594 ;; Scan files quickly for the first or next interesting one.
1595 (while (or first-time file-finished
1596 (save-restriction
1597 (widen)
1598 (not (tags-loop-eval tags-loop-scan))))
1599 (setq file-finished nil)
1600 (setq new (next-file first-time t))
1601 ;; If NEW is non-nil, we got a temp buffer,
1602 ;; and NEW is the file name.
1603 (if (or messaged
1604 (and (not first-time)
1605 (> baud-rate search-slow-speed)
1606 (setq messaged t)))
1607 (message "Scanning file %s..." (or new buffer-file-name)))
1608 (setq first-time nil)
1609 (goto-char (point-min)))
1611 ;; If we visited it in a temp buffer, visit it now for real.
1612 (if new
1613 (let ((pos (point)))
1614 (erase-buffer)
1615 (set-buffer (find-file-noselect new))
1616 (setq new nil) ;No longer in a temp buffer.
1617 (widen)
1618 (goto-char pos)))
1620 (switch-to-buffer (current-buffer))
1622 ;; Now operate on the file.
1623 ;; If value is non-nil, continue to scan the next file.
1624 (tags-loop-eval tags-loop-operate))
1625 (setq file-finished t))
1626 (and messaged
1627 (null tags-loop-operate)
1628 (message "Scanning file %s...found" buffer-file-name))))
1629 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
1631 ;;;###autoload
1632 (defun tags-search (regexp &optional file-list-form)
1633 "Search through all files listed in tags table for match for REGEXP.
1634 Stops when a match is found.
1635 To continue searching for next match, use command \\[tags-loop-continue].
1637 See documentation of variable `tags-file-name'."
1638 (interactive "sTags search (regexp): ")
1639 (if (and (equal regexp "")
1640 (eq (car tags-loop-scan) 're-search-forward)
1641 (null tags-loop-operate))
1642 ;; Continue last tags-search as if by M-,.
1643 (tags-loop-continue nil)
1644 (setq tags-loop-scan
1645 (list 're-search-forward (list 'quote regexp) nil t)
1646 tags-loop-operate nil)
1647 (tags-loop-continue (or file-list-form t))))
1649 ;;;###autoload
1650 (defun tags-query-replace (from to &optional delimited file-list-form start end)
1651 "`Query-replace-regexp' FROM with TO through all files listed in tags table.
1652 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1653 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
1654 with the command \\[tags-loop-continue].
1656 See documentation of variable `tags-file-name'."
1657 (interactive (query-replace-read-args "Tags query replace (regexp)" t))
1658 (setq tags-loop-scan (list 'prog1
1659 (list 'if (list 're-search-forward
1660 (list 'quote from) nil t)
1661 ;; When we find a match, move back
1662 ;; to the beginning of it so perform-replace
1663 ;; will see it.
1664 '(goto-char (match-beginning 0))))
1665 tags-loop-operate (list 'perform-replace
1666 (list 'quote from) (list 'quote to) nil nil
1667 t t (list 'quote delimited)))
1668 (tags-loop-continue (or file-list-form t)))
1670 (defun tags-complete-tags-table-file (string predicate what)
1671 (save-excursion
1672 ;; If we need to ask for the tag table, allow that.
1673 (let ((enable-recursive-minibuffers t))
1674 (visit-tags-table-buffer))
1675 (if (eq what t)
1676 (all-completions string (mapcar 'list (tags-table-files))
1677 predicate)
1678 (try-completion string (mapcar 'list (tags-table-files))
1679 predicate))))
1681 ;;;###autoload
1682 (defun list-tags (file &optional next-match)
1683 "Display list of tags in file FILE.
1684 This searches only the first table in the list, and no included tables.
1685 FILE should be as it appeared in the `etags' command, usually without a
1686 directory specification."
1687 (interactive (list (completing-read "List tags in file: "
1688 'tags-complete-tags-table-file
1689 nil t nil)))
1690 (with-output-to-temp-buffer "*Tags List*"
1691 (princ "Tags in file `")
1692 (tags-with-face 'highlight (princ file))
1693 (princ "':\n\n")
1694 (save-excursion
1695 (let ((first-time t)
1696 (gotany nil))
1697 (while (visit-tags-table-buffer (not first-time))
1698 (setq first-time nil)
1699 (if (funcall list-tags-function file)
1700 (setq gotany t)))
1701 (or gotany
1702 (error "File %s not in current tags tables" file)))))
1703 (with-current-buffer "*Tags List*"
1704 (setq buffer-read-only t)
1705 (apropos-mode)))
1707 ;;;###autoload
1708 (defun tags-apropos (regexp)
1709 "Display list of all tags in tags table REGEXP matches."
1710 (interactive "sTags apropos (regexp): ")
1711 (with-output-to-temp-buffer "*Tags List*"
1712 (princ "Click mouse-2 to follow tags.\n\nTags matching regexp `")
1713 (tags-with-face 'highlight (princ regexp))
1714 (princ "':\n\n")
1715 (save-excursion
1716 (let ((first-time t))
1717 (while (visit-tags-table-buffer (not first-time))
1718 (setq first-time nil)
1719 (funcall tags-apropos-function regexp))))
1720 (etags-tags-apropos-additional regexp))
1721 (with-current-buffer "*Tags List*"
1722 (setq buffer-read-only t)
1723 (apropos-mode)))
1725 ;;; XXX Kludge interface.
1727 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1728 ;;;###autoload
1729 (defun select-tags-table ()
1730 "Select a tags table file from a menu of those you have already used.
1731 The list of tags tables to select from is stored in `tags-table-set-list';
1732 see the doc of that variable if you want to add names to the list."
1733 (interactive)
1734 (pop-to-buffer "*Tags Table List*")
1735 (setq buffer-read-only nil)
1736 (erase-buffer)
1737 (let ((set-list tags-table-set-list)
1738 (desired-point nil))
1739 (when tags-table-list
1740 (setq desired-point (point-marker))
1741 (princ tags-table-list (current-buffer))
1742 (insert "\C-m")
1743 (prin1 (car tags-table-list) (current-buffer)) ;invisible
1744 (insert "\n"))
1745 (while set-list
1746 (unless (eq (car set-list) tags-table-list)
1747 (princ (car set-list) (current-buffer))
1748 (insert "\C-m")
1749 (prin1 (car (car set-list)) (current-buffer)) ;invisible
1750 (insert "\n"))
1751 (setq set-list (cdr set-list)))
1752 (when tags-file-name
1753 (or desired-point
1754 (setq desired-point (point-marker)))
1755 (insert tags-file-name "\C-m")
1756 (prin1 tags-file-name (current-buffer)) ;invisible
1757 (insert "\n"))
1758 (setq set-list (delete tags-file-name
1759 (apply 'nconc (cons (copy-sequence tags-table-list)
1760 (mapcar 'copy-sequence
1761 tags-table-set-list)))))
1762 (while set-list
1763 (insert (car set-list) "\C-m")
1764 (prin1 (car set-list) (current-buffer)) ;invisible
1765 (insert "\n")
1766 (setq set-list (delete (car set-list) set-list)))
1767 (goto-char 1)
1768 (insert-before-markers
1769 "Type `t' to select a tags table or set of tags tables:\n\n")
1770 (if desired-point
1771 (goto-char desired-point))
1772 (set-window-start (selected-window) 1 t))
1773 (set-buffer-modified-p nil)
1774 (select-tags-table-mode))
1776 (defvar select-tags-table-mode-map)
1777 (let ((map (make-sparse-keymap)))
1778 (define-key map "t" 'select-tags-table-select)
1779 (define-key map " " 'next-line)
1780 (define-key map "\^?" 'previous-line)
1781 (define-key map "n" 'next-line)
1782 (define-key map "p" 'previous-line)
1783 (define-key map "q" 'select-tags-table-quit)
1784 (setq select-tags-table-mode-map map))
1786 (defun select-tags-table-mode ()
1787 "Major mode for choosing a current tags table among those already loaded.
1789 \\{select-tags-table-mode-map}"
1790 (interactive)
1791 (kill-all-local-variables)
1792 (setq buffer-read-only t
1793 major-mode 'select-tags-table-mode
1794 mode-name "Select Tags Table")
1795 (use-local-map select-tags-table-mode-map)
1796 (setq selective-display t
1797 selective-display-ellipses nil))
1799 (defun select-tags-table-select ()
1800 "Select the tags table named on this line."
1801 (interactive)
1802 (search-forward "\C-m")
1803 (let ((name (read (current-buffer))))
1804 (visit-tags-table name)
1805 (select-tags-table-quit)
1806 (message "Tags table now %s" name)))
1808 (defun select-tags-table-quit ()
1809 "Kill the buffer and delete the selected window."
1810 (interactive)
1811 (quit-window t (selected-window)))
1813 ;;; Note, there is another definition of this function in bindings.el.
1814 ;;;###autoload
1815 (defun complete-tag ()
1816 "Perform tags completion on the text around point.
1817 Completes to the set of names listed in the current tags table.
1818 The string to complete is chosen in the same way as the default
1819 for \\[find-tag] (which see)."
1820 (interactive)
1821 (or tags-table-list
1822 tags-file-name
1823 (error "%s"
1824 (substitute-command-keys
1825 "No tags table loaded; try \\[visit-tags-table]")))
1826 (let ((pattern (funcall (or find-tag-default-function
1827 (get major-mode 'find-tag-default-function)
1828 'find-tag-default)))
1830 completion)
1831 (or pattern
1832 (error "Nothing to complete"))
1833 (search-backward pattern)
1834 (setq beg (point))
1835 (forward-char (length pattern))
1836 (setq completion (tags-complete-tag pattern nil nil))
1837 (cond ((eq completion t))
1838 ((null completion)
1839 (message "Can't find completion for \"%s\"" pattern)
1840 (ding))
1841 ((not (string= pattern completion))
1842 (delete-region beg (point))
1843 (insert completion))
1845 (message "Making completion list...")
1846 (with-output-to-temp-buffer "*Completions*"
1847 (display-completion-list
1848 (all-completions pattern 'tags-complete-tag nil)))
1849 (message "Making completion list...%s" "done")))))
1851 (dolist (x '("^No tags table in use; use .* to select one$"
1852 "^There is no default tag$"
1853 "^No previous tag locations$"
1854 "^File .* is not a valid tags table$"
1855 "^No \\(more \\|\\)tags \\(matching\\|containing\\) "
1856 "^Rerun etags: `.*' not found in "
1857 "^All files processed$"
1858 "^No .* or .* in progress$"
1859 "^File .* not in current tags tables$"
1860 "^No tags table loaded"
1861 "^Nothing to complete$"))
1862 (add-to-list 'debug-ignored-errors x))
1864 (provide 'etags)
1866 ;;; etags.el ends here