1 ;;; etags.el --- etags facility for Emacs
3 ;; Copyright (C) 1985, 1986, 1988, 1989, 1992, 1993, 1994
4 ;; Free Software Foundation, Inc.
6 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 (defvar tags-file-name nil
29 "*File name of tags table.
30 To switch to a new tags table, setting this variable is sufficient.
31 If you set this variable, do not also set `tags-table-list'.
32 Use the `etags' program to make a tags table file.")
33 ;; Make M-x set-variable tags-file-name like M-x visit-tags-table.
34 ;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ")
37 ;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
38 (defvar tags-table-list nil
39 "*List of file names of tags tables to search.
40 An element that is a directory means the file \"TAGS\" in that directory.
41 To switch to a new list of tags tables, setting this variable is sufficient.
42 If you set this variable, do not also set `tags-file-name'.
43 Use the `etags' program to make a tags table file.")
46 (defvar tags-add-tables 'ask-user
47 "*Control whether to add a new tags table to the current list.
48 t means do; nil means don't (always start a new list).
49 Any other value means ask the user whether to add a new tags table
50 to the current list (as opposed to starting a new list).")
52 (defvar tags-table-list-pointer nil
53 "Pointer into `tags-table-list' where the current state of searching is.
54 Might instead point into a list of included tags tables.
55 Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
57 (defvar tags-table-list-started-at nil
58 "Pointer into `tags-table-list', where the current search started.")
60 (defvar tags-table-parent-pointer-list nil
61 "Saved state of the tags table that included this one.
62 Each element is (POINTER . STARTED-AT), giving the values of
63 `tags-table-list-pointer' and `tags-table-list-started-at' from
64 before we moved into the current table.")
66 (defvar tags-table-set-list nil
67 "List of sets of tags table which have been used together in the past.
68 Each element is a list of strings which are file names.")
71 (defvar find-tag-hook nil
72 "*Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'.
73 The value in the buffer in which \\[find-tag] is done is used,
74 not the value in the buffer \\[find-tag] goes to.")
77 (defvar find-tag-default-function nil
78 "*A function of no arguments used by \\[find-tag] to pick a default tag.
79 If nil, and the symbol that is the value of `major-mode'
80 has a `find-tag-default-function' property (see `put'), that is used.
81 Otherwise, `find-tag-default' is used.")
83 (defvar default-tags-table-function nil
84 "If non-nil, a function to choose a default tags file for a buffer.
85 This function receives no arguments and should return the default
86 tags table file to use for the current buffer.")
88 (defvar tags-location-stack nil
89 "List of markers which are locations visited by \\[find-tag].
90 Pop back to the last location with \\[negative-argument] \\[find-tag].")
93 ;; These variables are local in tags table buffers.
95 (defvar tag-lines-already-matched nil
96 "List of positions of beginnings of lines within the tags table
97 that are already matched.")
99 (defvar tags-table-files nil
100 "List of file names covered by current tags table.
101 nil means it has not yet been computed; use `tags-table-files' to do so.")
103 (defvar tags-completion-table nil
104 "Alist of tag names defined in current tags table.")
106 (defvar tags-included-tables nil
107 "List of tags tables included by the current tags table.")
109 (defvar next-file-list nil
110 "List of files for \\[next-file] to process.")
112 ;; Hooks for file formats.
114 (defvar tags-table-format-hooks '(etags-recognize-tags-table
115 recognize-empty-tags-table)
116 "List of functions to be called in a tags table buffer to identify
117 the type of tags table. The functions are called in order, with no arguments,
118 until one returns non-nil. The function should make buffer-local bindings
119 of the format-parsing tags function variables if successful.")
121 (defvar file-of-tag-function nil
122 "Function to do the work of `file-of-tag' (which see).")
123 (defvar tags-table-files-function nil
124 "Function to do the work of `tags-table-files' (which see).")
125 (defvar tags-completion-table-function nil
126 "Function to build the tags-completion-table.")
127 (defvar snarf-tag-function nil
128 "Function to get info about a matched tag for `goto-tag-location-function'.")
129 (defvar goto-tag-location-function nil
130 "Function of to go to the location in the buffer specified by a tag.
131 One argument, the tag info returned by `snarf-tag-function'.")
132 (defvar find-tag-regexp-search-function nil
133 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
134 (defvar find-tag-regexp-tag-order nil
135 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
136 (defvar find-tag-regexp-next-line-after-failure-p nil
137 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
138 (defvar find-tag-search-function nil
139 "Search function passed to `find-tag-in-order' for finding a tag.")
140 (defvar find-tag-tag-order nil
141 "Tag order passed to `find-tag-in-order' for finding a tag.")
142 (defvar find-tag-next-line-after-failure-p nil
143 "Flag passed to `find-tag-in-order' for finding a tag.")
144 (defvar list-tags-function nil
145 "Function to do the work of `list-tags' (which see).")
146 (defvar tags-apropos-function nil
147 "Function to do the work of `tags-apropos' (which see).")
148 (defvar tags-included-tables-function nil
149 "Function to do the work of `tags-included-tables' (which see).")
150 (defvar verify-tags-table-function nil
151 "Function to return t iff the current buffer contains a valid
152 \(already initialized\) tags file.")
154 ;; Initialize the tags table in the current buffer.
155 ;; Returns non-nil iff it is a valid tags table. On
156 ;; non-nil return, the tags table state variable are
157 ;; made buffer-local and initialized to nil.
158 (defun initialize-new-tags-table ()
159 (set (make-local-variable 'tag-lines-already-matched) nil)
160 (set (make-local-variable 'tags-table-files) nil)
161 (set (make-local-variable 'tags-completion-table) nil)
162 (set (make-local-variable 'tags-included-tables) nil)
163 ;; Value is t if we have found a valid tags table buffer.
164 (let ((hooks tags-table-format-hooks))
166 (not (funcall (car hooks))))
167 (setq hooks (cdr hooks)))
171 (defun visit-tags-table (file &optional local)
172 "Tell tags commands to use tags table file FILE.
173 FILE should be the name of a file created with the `etags' program.
174 A directory name is ok too; it means file TAGS in that directory.
176 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
177 With a prefix arg, set the buffer-local value instead.
178 When you find a tag with \\[find-tag], the buffer it finds the tag
179 in is given a local value of this variable which is the name of the tags
180 file the tag was in."
181 (interactive (list (read-file-name "Visit tags table: (default TAGS) "
183 (expand-file-name "TAGS"
187 ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
188 ;; initialize a buffer for FILE and set tags-file-name to the
189 ;; fully-expanded name.
190 (let ((tags-file-name file))
192 (or (visit-tags-table-buffer 'same)
193 (signal 'file-error (list "Visiting tags table"
194 "file does not exist"
196 ;; Set FILE to the expanded name.
197 (setq file tags-file-name)))
199 ;; Set the local value of tags-file-name.
200 (set (make-local-variable 'tags-file-name) file)
201 ;; Set the global value of tags-file-name.
202 (setq-default tags-file-name file)))
204 ;; Move tags-table-list-pointer along and set tags-file-name.
205 ;; If NO-INCLUDES is non-nil, ignore included tags tables.
206 ;; Returns nil when out of tables.
207 (defun tags-next-table (&optional no-includes)
208 ;; Do we have any included tables?
209 (if (and (not no-includes)
210 (visit-tags-table-buffer 'same)
211 (tags-included-tables))
213 ;; Move into the included tags tables.
214 (setq tags-table-parent-pointer-list
215 ;; Save the current state of what table we are in.
216 (cons (cons tags-table-list-pointer tags-table-list-started-at)
217 tags-table-parent-pointer-list)
218 ;; Start the pointer in the list of included tables.
219 tags-table-list-pointer tags-included-tables
220 tags-table-list-started-at tags-included-tables)
222 ;; No included tables. Go to the next table in the list.
223 (setq tags-table-list-pointer
224 (cdr tags-table-list-pointer))
225 (or tags-table-list-pointer
227 (setq tags-table-list-pointer tags-table-list))
229 (if (eq tags-table-list-pointer tags-table-list-started-at)
230 ;; We have come full circle. No more tables.
231 (if tags-table-parent-pointer-list
232 ;; Pop back to the tags table which includes this one.
234 ;; Restore the state variables.
235 (setq tags-table-list-pointer
236 (car (car tags-table-parent-pointer-list))
237 tags-table-list-started-at
238 (cdr (car tags-table-parent-pointer-list))
239 tags-table-parent-pointer-list
240 (cdr tags-table-parent-pointer-list))
241 ;; Recurse to skip to the next table after the parent.
243 ;; All out of tags tables.
244 (setq tags-table-list-pointer nil))))
246 (and tags-table-list-pointer
247 ;; Set tags-file-name to the fully-expanded name.
249 (tags-expand-table-name (car tags-table-list-pointer)))))
251 ;; Expand tags table name FILE into a complete file name.
252 (defun tags-expand-table-name (file)
253 (setq file (expand-file-name file))
254 (if (file-directory-p file)
255 (expand-file-name "TAGS" file)
258 ;; Return the cdr of LIST (default: tags-table-list) whose car
259 ;; is equal to FILE after tags-expand-table-name on both sides.
260 (defun tags-table-list-member (file &optional list)
262 (setq list tags-table-list))
263 (setq file (tags-expand-table-name file))
265 (not (string= file (tags-expand-table-name (car list)))))
266 (setq list (cdr list)))
269 ;; Local var in visit-tags-table-buffer-cont
270 ;; which is set by tags-table-including.
271 (defvar visit-tags-table-buffer-cont)
273 ;; Subroutine of visit-tags-table-buffer. Frobs its local vars.
274 ;; Search TABLES for one that has tags for THIS-FILE. Recurses on
275 ;; included tables. Returns the tail of TABLES (or of an inner
276 ;; included list) whose car is a table listing THIS-FILE. If
277 ;; CORE-ONLY is non-nil, check only tags tables that are already in
278 ;; buffers--don't visit any new files.
279 (defun tags-table-including (this-file tables core-only &optional recursing)
281 ;; Loop over TABLES, looking for one containing tags for THIS-FILE.
282 (while (and (not found)
284 (let ((tags-file-name (tags-expand-table-name (car tables))))
285 (if (or (get-file-buffer tags-file-name)
287 (file-exists-p tags-file-name)))
289 ;; Select the tags table buffer and get the file list up to date.
290 (visit-tags-table-buffer 'same)
292 (setq tags-table-files
293 (funcall tags-table-files-function)))
295 (cond ((member this-file tags-table-files)
299 ((tags-included-tables)
300 ;; This table has included tables. Check them.
301 (let ((old tags-table-parent-pointer-list))
305 ;; At top level (not in an included tags
306 ;; table), set the list to nil so we can
307 ;; collect just the elts from this run.
308 (setq tags-table-parent-pointer-list nil))
310 ;; Recurse on the list of included tables.
311 (tags-table-including this-file
316 ;; One of them lists THIS-FILE.
317 ;; Set the table list state variables to move
318 ;; us inside the list of included tables.
319 (setq tags-table-parent-pointer-list
321 (cons tags-table-list-pointer
322 tags-table-list-started-at)
323 tags-table-parent-pointer-list)
324 tags-table-list-pointer found
325 tags-table-list-started-at found
326 ;; Set a local variable of
327 ;; our caller, visit-tags-table-buffer.
328 ;; Set it so we won't frob lists later.
329 visit-tags-table-buffer-cont
332 ;; tags-table-parent-pointer-list now describes
333 ;; the path of included tables taken by recursive
334 ;; invocations of this function. The recursive
335 ;; calls have consed onto the front of the list,
336 ;; so it is now outermost first. We want it
337 ;; innermost first, so reverse it. Then append
338 ;; the old list (from before we were called the
339 ;; outermost time), to get the complete current
340 ;; state of included tables.
341 (setq tags-table-parent-pointer-list
343 tags-table-parent-pointer-list)
345 (setq tables (cdr tables)))
348 (defun visit-tags-table-buffer (&optional cont)
349 "Select the buffer containing the current tags table.
350 If optional arg is t, visit the next table in `tags-table-list'.
351 If optional arg is the atom `same', don't look for a new table;
352 just select the buffer visiting `tags-file-name'.
353 If arg is nil or absent, choose a first buffer from information in
354 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
355 Returns t if it visits a tags table, or nil if there are no more in the list."
357 ;; Set tags-file-name to the tags table file we want to visit.
358 (let ((visit-tags-table-buffer-cont cont))
359 (cond ((eq visit-tags-table-buffer-cont 'same)
360 ;; Use the ambient value of tags-file-name.
362 (error (substitute-command-keys
363 (concat "No tags table in use! "
364 "Use \\[visit-tags-table] to select one.")))))
366 (visit-tags-table-buffer-cont
367 ;; Find the next table.
368 (if (tags-next-table)
369 ;; Skip over nonexistent files.
371 (while (and (setq file
372 (tags-expand-table-name tags-file-name))
373 (not (or (get-file-buffer file)
374 (file-exists-p file))))
375 (tags-next-table)))))
378 ;; Pick a table out of our hat.
381 ;; First, try a local variable.
382 (cdr (assq 'tags-file-name (buffer-local-variables)))
383 ;; Second, try a user-specified function to guess.
384 (and default-tags-table-function
385 (funcall default-tags-table-function))
386 ;; Third, look for a tags table that contains
387 ;; tags for the current buffer's file.
388 ;; If one is found, the lists will be frobnicated,
389 ;; and VISIT-TAGS-TABLE-BUFFER-CONT
390 ;; will be set non-nil so we don't do it below.
392 ;; First check only tables already in buffers.
393 (save-excursion (tags-table-including buffer-file-name
396 ;; Since that didn't find any, now do the
397 ;; expensive version: reading new files.
398 (save-excursion (tags-table-including buffer-file-name
401 ;; Fourth, use the user variable tags-file-name, if it is not
402 ;; already in tags-table-list.
404 (not (tags-table-list-member tags-file-name))
406 ;; Fifth, use the user variable giving the table list.
407 ;; Find the first element of the list that actually exists.
408 (let ((list tags-table-list)
411 (setq file (tags-expand-table-name (car list)))
412 (not (get-file-buffer file))
413 (not (file-exists-p file)))
414 (setq list (cdr list)))
416 ;; Finally, prompt the user for a file name.
418 (read-file-name "Visit tags table: (default TAGS) "
423 ;; Expand the table name into a full file name.
424 (setq tags-file-name (tags-expand-table-name tags-file-name))
426 (if (and (eq visit-tags-table-buffer-cont t)
427 (null tags-table-list-pointer))
428 ;; All out of tables.
431 ;; Verify that tags-file-name is a valid tags table.
432 (if (if (get-file-buffer tags-file-name)
433 ;; The file is already in a buffer. Check for the visited file
434 ;; having changed since we last used it.
436 (set-buffer (get-file-buffer tags-file-name))
437 (setq win (or verify-tags-table-function
438 (initialize-new-tags-table)))
439 (if (or (verify-visited-file-modtime (current-buffer))
441 "Tags file has changed, read new contents? ")))
442 (and win (funcall verify-tags-table-function))
444 (initialize-new-tags-table)))
445 (set-buffer (find-file-noselect tags-file-name))
446 (or (string= tags-file-name buffer-file-name)
447 ;; find-file-noselect has changed the file name.
448 ;; Propagate the change to tags-file-name and tags-table-list.
449 (let ((tail (member tags-file-name tags-table-list)))
451 (setcar tail buffer-file-name))
452 (setq tags-file-name buffer-file-name)))
453 (initialize-new-tags-table))
455 ;; We have a valid tags table.
457 ;; Bury the tags table buffer so it
458 ;; doesn't get in the user's way.
459 (bury-buffer (current-buffer))
461 (if (memq visit-tags-table-buffer-cont '(same nil))
462 ;; Look in the list for the table we chose.
463 (let ((elt (tags-table-list-member tags-file-name)))
465 ;; The table is not in the current set.
466 ;; Try to find it in another previously used set.
467 (let ((sets tags-table-set-list))
470 (tags-table-list-member
471 tags-file-name (car sets)))))
472 (setq sets (cdr sets)))
474 ;; Found in some other set. Switch to that set.
476 (or (memq tags-table-list tags-table-set-list)
477 ;; Save the current list.
478 (setq tags-table-set-list
479 (cons tags-table-list
480 tags-table-set-list)))
481 (setq tags-table-list (car sets)))
483 ;; Not found in any existing set.
484 (if (and tags-table-list
485 (or (eq t tags-add-tables)
488 (concat "Keep current list of "
489 "tags tables also? ")))))
490 ;; Add it to the current list.
491 (setq tags-table-list (cons tags-file-name
493 ;; Make a fresh list, and store the old one.
494 (message "Starting a new list of tags tables")
495 (or (memq tags-table-list tags-table-set-list)
496 (setq tags-table-set-list
497 (cons tags-table-list
498 tags-table-set-list)))
499 (setq tags-table-list (list tags-file-name)))
500 (setq elt tags-table-list))))
502 (or visit-tags-table-buffer-cont
503 ;; Set the tags table list state variables to point
504 ;; at the table we want to use first.
505 (setq tags-table-list-started-at elt
506 tags-table-list-pointer elt))))
508 ;; Return of t says the tags table is valid.
511 ;; The buffer was not valid. Don't use it again.
512 (let ((file tags-file-name))
513 (kill-local-variable 'tags-file-name)
514 (if (eq file tags-file-name)
515 (setq tags-file-name nil)))
516 (error "File %s is not a valid tags table" buffer-file-name)))))
518 (defun file-of-tag ()
519 "Return the file name of the file whose tags point is within.
520 Assumes the tags table is the current buffer.
521 File name returned is relative to tags table file's directory."
522 (funcall file-of-tag-function))
525 (defun tags-table-files ()
526 "Return a list of files in the current tags table.
527 Assumes the tags table is the current buffer.
528 File names returned are absolute."
530 (setq tags-table-files
531 (funcall tags-table-files-function))))
533 (defun tags-included-tables ()
534 "Return a list of tags tables included by the current table.
535 Assumes the tags table is the current buffer."
536 (or tags-included-tables
537 (setq tags-included-tables (funcall tags-included-tables-function))))
539 ;; Build tags-completion-table on demand. The single current tags table
540 ;; and its included tags tables (and their included tables, etc.) have
541 ;; their tags included in the completion table.
542 (defun tags-completion-table ()
543 (or tags-completion-table
546 (message "Making tags completion table for %s..." buffer-file-name)
547 (let ((included (tags-included-tables))
548 (table (funcall tags-completion-table-function)))
550 ;; Iterate over the list of included tables, and combine each
551 ;; included table's completion obarray to the parent obarray.
554 (let ((tags-file-name (car included)))
555 (visit-tags-table-buffer 'same))
556 ;; Recurse in that buffer to compute its completion table.
557 (if (tags-completion-table)
558 ;; Combine the tables.
561 (intern (symbol-name sym) table)))
562 tags-completion-table))
563 (setq included (cdr included))))
564 (setq tags-completion-table table))
565 (message "Making tags completion table for %s...done"
567 (quit (message "Tags completion table construction aborted.")
568 (setq tags-completion-table nil)))))
570 ;; Completion function for tags. Does normal try-completion,
571 ;; but builds tags-completion-table on demand.
572 (defun tags-complete-tag (string predicate what)
574 ;; If we need to ask for the tag table, allow that.
575 (let ((enable-recursive-minibuffers t))
576 (visit-tags-table-buffer))
578 (all-completions string (tags-completion-table) predicate)
579 (try-completion string (tags-completion-table) predicate))))
581 ;; Return a default tag to search for, based on the text at point.
582 (defun find-tag-default ()
584 (while (looking-at "\\sw\\|\\s_")
586 (if (or (re-search-backward "\\sw\\|\\s_"
587 (save-excursion (beginning-of-line) (point))
589 (re-search-forward "\\(\\sw\\|\\s_\\)+"
590 (save-excursion (end-of-line) (point))
592 (progn (goto-char (match-end 0))
593 (buffer-substring (point)
594 (progn (forward-sexp -1)
595 (while (looking-at "\\s'")
600 ;; Read a tag name from the minibuffer with defaulting and completion.
601 (defun find-tag-tag (string)
602 (let* ((default (funcall (or find-tag-default-function
603 (get major-mode 'find-tag-default-function)
605 (spec (completing-read (if default
606 (format "%s(default %s) " string default)
608 'tags-complete-tag)))
610 (or default (error "There is no default tag"))
614 "Last tag found by \\[find-tag].")
616 ;; Get interactive args for find-tag{-noselect,-other-window,-regexp}.
617 (defun find-tag-interactive (prompt &optional no-default)
618 (if current-prefix-arg
619 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
624 (find-tag-tag prompt)))))
627 (defun find-tag-noselect (tagname &optional next-p regexp-p)
628 "Find tag (in current tags table) whose name contains TAGNAME.
629 Returns the buffer containing the tag's definition and moves its point there,
630 but does not select the buffer.
631 The default for TAGNAME is the expression in the buffer near point.
633 If second arg NEXT-P is t (interactively, with prefix arg), search for
634 another tag that matches the last tagname or regexp used. When there are
635 multiple matches for a tag, more exact matches are found first. If NEXT-P
636 is the atom `-' (interactively, with prefix arg that is a negative number
637 or just \\[negative-argument]), pop back to the previous tag gone to.
639 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
641 See documentation of variable `tags-file-name'."
642 (interactive (find-tag-interactive "Find tag: "))
644 ;; Save the current buffer's value of `find-tag-hook' before selecting the
645 ;; tags table buffer.
646 (let ((local-find-tag-hook find-tag-hook))
648 ;; Pop back to a previous location.
649 (if (null tags-location-stack)
650 (error "No previous tag locations")
651 (let ((marker (car tags-location-stack)))
653 (setq tags-location-stack (cdr tags-location-stack))
655 ;; Move to the saved location.
656 (set-buffer (marker-buffer marker))
657 (goto-char (marker-position marker))
658 ;; Kill that marker so it doesn't slow down editing.
659 (set-marker marker nil nil)
660 ;; Run the user's hook. Do we really want to do this for pop?
661 (run-hooks 'local-find-tag-hook))))
663 ;; Find the same table we last used.
664 (visit-tags-table-buffer 'same)
665 ;; Pick a table to use.
666 (visit-tags-table-buffer)
667 ;; Record TAGNAME for a future call with NEXT-P non-nil.
668 (setq last-tag tagname))
670 ;; Record the location so we can pop back to it later.
673 (setq tags-location-stack
674 (cons (let ((marker (make-marker)))
677 ;; find-tag-in-order does the real work.
679 (if next-p last-tag tagname)
681 find-tag-regexp-search-function
682 find-tag-search-function)
684 find-tag-regexp-tag-order
687 find-tag-regexp-next-line-after-failure-p
688 find-tag-next-line-after-failure-p)
689 (if regexp-p "matching" "containing")
691 (set-marker marker (point))))
692 tags-location-stack))))
693 (run-hooks 'local-find-tag-hook)))))
696 (defun find-tag (tagname &optional next-p regexp-p)
697 "Find tag (in current tags table) whose name contains TAGNAME.
698 Select the buffer containing the tag's definition, and move point there.
699 The default for TAGNAME is the expression in the buffer around or before point.
701 If second arg NEXT-P is t (interactively, with prefix arg), search for
702 another tag that matches the last tagname or regexp used. When there are
703 multiple matches for a tag, more exact matches are found first. If NEXT-P
704 is the atom `-' (interactively, with prefix arg that is a negative number
705 or just \\[negative-argument]), pop back to the previous tag gone to.
707 See documentation of variable `tags-file-name'."
708 (interactive (find-tag-interactive "Find tag: "))
709 (switch-to-buffer (find-tag-noselect tagname next-p regexp-p)))
710 ;;;###autoload (define-key esc-map "." 'find-tag)
713 (defun find-tag-other-window (tagname &optional next-p regexp-p)
714 "Find tag (in current tags table) whose name contains TAGNAME.
715 Select the buffer containing the tag's definition in another window, and
716 move point there. The default for TAGNAME is the expression in the buffer
717 around or before point.
719 If second arg NEXT-P is t (interactively, with prefix arg), search for
720 another tag that matches the last tagname or regexp used. When there are
721 multiple matches for a tag, more exact matches are found first. If NEXT-P
722 is negative (interactively, with prefix arg that is a negative number or
723 just \\[negative-argument]), pop back to the previous tag gone to.
725 See documentation of variable `tags-file-name'."
726 (interactive (find-tag-interactive "Find tag other window: "))
728 ;; This hair is to deal with the case where the tag is found in the
729 ;; selected window's buffer; without the hair, point is moved in both
730 ;; windows. To prevent this, we save the selected window's point before
731 ;; doing find-tag-noselect, and restore it after.
732 (let* ((window-point (window-point (selected-window)))
733 (tagbuf (find-tag-noselect tagname next-p regexp-p))
734 (tagpoint (progn (set-buffer tagbuf) (point))))
735 (set-window-point (prog1
737 (switch-to-buffer-other-window tagbuf)
738 ;; We have to set this new window's point; it
739 ;; might already have been displaying a
740 ;; different portion of tagbuf, in which case
741 ;; switch-to-buffer-other-window doesn't set
742 ;; the window's point from the buffer.
743 (set-window-point (selected-window) tagpoint))
745 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
748 (defun find-tag-other-frame (tagname &optional next-p)
749 "Find tag (in current tags table) whose name contains TAGNAME.
750 Select the buffer containing the tag's definition in another frame, and
751 move point there. The default for TAGNAME is the expression in the buffer
752 around or before point.
754 If second arg NEXT-P is t (interactively, with prefix arg), search for
755 another tag that matches the last tagname or regexp used. When there are
756 multiple matches for a tag, more exact matches are found first. If NEXT-P
757 is negative (interactively, with prefix arg that is a negative number or
758 just \\[negative-argument]), pop back to the previous tag gone to.
760 See documentation of variable `tags-file-name'."
761 (interactive (find-tag-interactive "Find tag other frame: "))
762 (let ((pop-up-frames t))
763 (find-tag-other-window tagname next-p)))
764 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
767 (defun find-tag-regexp (regexp &optional next-p other-window)
768 "Find tag (in current tags table) whose name matches REGEXP.
769 Select the buffer containing the tag's definition and move point there.
771 If second arg NEXT-P is t (interactively, with prefix arg), search for
772 another tag that matches the last tagname or regexp used. When there are
773 multiple matches for a tag, more exact matches are found first. If NEXT-P
774 is negative (interactively, with prefix arg that is a negative number or
775 just \\[negative-argument]), pop back to the previous tag gone to.
777 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
779 See documentation of variable `tags-file-name'."
780 (interactive (find-tag-interactive "Find tag regexp: " t))
781 ;; We go through find-tag-other-window to do all the display hair there.
782 (funcall (if other-window 'find-tag-other-window 'find-tag)
785 ;; Internal tag finding function.
787 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
788 ;; any member of the function list ORDER (third arg). If ORDER is nil,
789 ;; use saved state to continue a previous search.
791 ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
794 ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
795 ;; point should be moved to the next line.
797 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
798 ;; beginning of tags file, and perform inner loop: for each naive match for
799 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
800 ;; qualifier-func. If it qualifies, go to the specified line in the
801 ;; specified source file and return. Qualified matches are remembered to
802 ;; avoid repetition. State is saved so that the loop can be continued.
804 (defun find-tag-in-order (pattern
807 next-line-after-failure-p
810 (let (file ;name of file containing tag
811 tag-info ;where to find the tag in FILE
812 tags-table-file ;name of tags file
818 (or first-search ;find-tag-noselect has already done it.
819 (visit-tags-table-buffer 'same))
821 ;; Get a qualified match.
822 (catch 'qualified-match-found
824 ;; Iterate over the list of tags tables.
825 (while (or first-table
826 (visit-tags-table-buffer t))
829 (setq tag-lines-already-matched nil))
831 (and first-search first-table
832 ;; Start at beginning of tags file.
833 (goto-char (point-min)))
834 (setq first-table nil)
836 (setq tags-table-file buffer-file-name)
837 ;; Iterate over the list of ordering predicates.
839 (while (funcall search-forward-func pattern nil t)
840 ;; Naive match found. Qualify the match.
841 (and (funcall (car order) pattern)
842 ;; Make sure it is not a previous qualified match.
843 ;; Use of `memq' depends on numbers being eq.
844 (not (memq (save-excursion (beginning-of-line) (point))
845 tag-lines-already-matched))
846 (throw 'qualified-match-found nil))
847 (if next-line-after-failure-p
849 ;; Try the next flavor of match.
850 (setq order (cdr order))
851 (goto-char (point-min)))
852 (setq order tag-order))
853 ;; We throw out on match, so only get here if there were no matches.
854 (error "No %stags %s %s" (if first-search "" "more ")
857 ;; Found a tag; extract location info.
859 (setq tag-lines-already-matched (cons (point)
860 tag-lines-already-matched))
861 ;; Expand the filename, using the tags table buffer's default-directory.
862 (setq file (expand-file-name (file-of-tag))
863 tag-info (funcall snarf-tag-function))
865 ;; Get the local value in the tags table buffer before switching buffers.
866 (setq goto-func goto-tag-location-function)
868 ;; Find the right line in the specified file.
869 (set-buffer (find-file-noselect file))
872 (funcall goto-func tag-info)
874 ;; Give this buffer a local value of tags-file-name.
875 ;; The next time visit-tags-table-buffer is called,
876 ;; it will use the same tags table that found a match in this buffer.
877 (make-local-variable 'tags-file-name)
878 (setq tags-file-name tags-table-file)
880 ;; Return the buffer where the tag was found.
883 ;; `etags' TAGS file format support.
885 ;; If the current buffer is a valid etags TAGS file, give it local values of
886 ;; the tags table format variables, and return non-nil.
887 (defun etags-recognize-tags-table ()
888 (and (etags-verify-tags-table)
889 ;; It is annoying to flash messages on the screen briefly,
890 ;; and this message is not useful. -- rms
891 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
892 (mapcar (function (lambda (elt)
893 (set (make-local-variable (car elt)) (cdr elt))))
894 '((file-of-tag-function . etags-file-of-tag)
895 (tags-table-files-function . etags-tags-table-files)
896 (tags-completion-table-function . etags-tags-completion-table)
897 (snarf-tag-function . etags-snarf-tag)
898 (goto-tag-location-function . etags-goto-tag-location)
899 (find-tag-regexp-search-function . re-search-forward)
900 (find-tag-regexp-tag-order . (tag-re-match-p))
901 (find-tag-regexp-next-line-after-failure-p . t)
902 (find-tag-search-function . search-forward)
903 (find-tag-tag-order . (tag-exact-match-p tag-word-match-p
905 (find-tag-next-line-after-failure-p . nil)
906 (list-tags-function . etags-list-tags)
907 (tags-apropos-function . etags-tags-apropos)
908 (tags-included-tables-function . etags-tags-included-tables)
909 (verify-tags-table-function . etags-verify-tags-table)
912 ;; Return non-nil iff the current buffer is a valid etags TAGS file.
913 (defun etags-verify-tags-table ()
914 ;; Use eq instead of = in case char-after returns nil.
915 (eq (char-after 1) ?\f))
917 (defun etags-file-of-tag ()
919 (search-backward "\f\n")
921 (buffer-substring (point)
922 (progn (skip-chars-forward "^,") (point)))))
924 (defun etags-tags-completion-table ()
925 (let ((table (make-vector 511 0)))
927 (goto-char (point-min))
928 ;; This monster regexp matches an etags tag line.
929 ;; \1 is the string to match;
930 ;; \2 is not interesting;
931 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
932 ;; \4 is not interesting;
933 ;; \5 is the explicitly-specified tag name.
934 ;; \6 is the line to start searching at;
935 ;; \7 is the char to start searching at.
936 (while (re-search-forward
937 "^\\(\\(.+[^-a-zA-Z0-9_$]+\\)?\\([-a-zA-Z0-9_$]+\\)\
938 \[^-a-zA-Z0-9_$]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
939 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
941 (intern (if (match-beginning 5)
942 ;; There is an explicit tag name.
943 (buffer-substring (match-beginning 5) (match-end 5))
944 ;; No explicit tag name. Best guess.
945 (buffer-substring (match-beginning 3) (match-end 3)))
949 (defun etags-snarf-tag ()
950 (let (tag-text line startpos)
951 (search-forward "\177")
952 (setq tag-text (buffer-substring (1- (point))
953 (save-excursion (beginning-of-line)
955 ;; Skip explicit tag name if present.
956 (search-forward "\001" (save-excursion (forward-line 1) (point)) t)
957 (if (looking-at "[0-9]")
958 (setq line (string-to-int (buffer-substring
960 (progn (skip-chars-forward "0-9")
963 (if (looking-at "[0-9]")
964 (setq startpos (string-to-int (buffer-substring
966 (progn (skip-chars-forward "0-9")
968 ;; Leave point on the next line of the tags file.
970 (cons tag-text (cons line startpos))))
972 ;; TAG-INFO is a cons (TEXT LINE . POSITION) where TEXT is the initial part
973 ;; of a line containing the tag and POSITION is the character position of
974 ;; TEXT within the file (starting from 1); LINE is the line number. Either
975 ;; LINE or POSITION may be nil; POSITION is used if present. If the tag
976 ;; isn't exactly at the given position then look around that position using
977 ;; a search window which expands until it hits the start of file.
978 (defun etags-goto-tag-location (tag-info)
979 (let ((startpos (cdr (cdr tag-info)))
980 ;; This constant is 1/2 the initial search window.
981 ;; There is no sense in making it too small,
982 ;; since just going around the loop once probably
983 ;; costs about as much as searching 2000 chars.
986 (pat (concat (if (eq selective-display t)
988 (regexp-quote (car tag-info)))))
989 ;; If no char pos was given, try the given line number.
991 (if (car (cdr tag-info))
992 (setq startpos (progn (goto-line (car (cdr tag-info)))
995 (setq startpos (point-min)))
996 ;; First see if the tag is right at the specified location.
998 (setq found (looking-at pat))
999 (while (and (not found)
1001 (goto-char (- startpos offset))
1004 (re-search-forward pat (+ startpos offset) t)
1005 offset (* 3 offset))) ; expand search window
1007 (re-search-forward pat nil t)
1008 (error "Rerun etags: `%s' not found in %s"
1009 pat buffer-file-name)))
1010 ;; Position point at the right place
1011 ;; if the search string matched an extra Ctrl-m at the beginning.
1012 (and (eq selective-display t)
1015 (beginning-of-line))
1017 (defun etags-list-tags (file)
1019 (if (not (search-forward (concat "\f\n" file ",") nil t))
1022 (while (not (or (eobp) (looking-at "\f")))
1023 (let ((tag (buffer-substring (point)
1024 (progn (skip-chars-forward "^\177")
1026 (princ (if (looking-at "[^\n]+\001")
1027 ;; There is an explicit tag name; use that.
1028 (buffer-substring (point)
1029 (progn (skip-chars-forward "^\001")
1036 (defun etags-tags-apropos (string)
1038 (while (re-search-forward string nil t)
1040 (princ (buffer-substring (point)
1041 (progn (skip-chars-forward "^\177")
1046 (defun etags-tags-table-files ()
1049 (goto-char (point-min))
1050 (while (search-forward "\f\n" nil t)
1052 (skip-chars-forward "^,\n")
1053 (or (looking-at ",include$")
1054 ;; Expand in the default-directory of the tags table buffer.
1055 (setq files (cons (expand-file-name (buffer-substring beg (point)))
1059 (defun etags-tags-included-tables ()
1062 (goto-char (point-min))
1063 (while (search-forward "\f\n" nil t)
1065 (skip-chars-forward "^,\n")
1066 (if (looking-at ",include$")
1067 ;; Expand in the default-directory of the tags table buffer.
1068 (setq files (cons (expand-file-name (buffer-substring beg (point)))
1072 ;; Empty tags file support.
1074 ;; Recognize an empty file and give it local values of the tags table format
1075 ;; variables which do nothing.
1076 (defun recognize-empty-tags-table ()
1077 (and (zerop (buffer-size))
1078 (mapcar (function (lambda (sym)
1079 (set (make-local-variable sym) 'ignore)))
1080 '(tags-table-files-function
1081 tags-completion-table-function
1082 find-tag-regexp-search-function
1083 find-tag-search-function
1084 tags-apropos-function
1085 tags-included-tables-function))
1086 (set (make-local-variable 'verify-tags-table-function)
1087 (function (lambda ()
1088 (zerop (buffer-size)))))))
1090 ;;; Match qualifier functions for tagnames.
1091 ;;; XXX these functions assume etags file format.
1093 ;; This might be a neat idea, but it's too hairy at the moment.
1094 ;;(defmacro tags-with-syntax (&rest body)
1095 ;; (` (let ((current (current-buffer))
1096 ;; (otable (syntax-table))
1097 ;; (buffer (find-file-noselect (file-of-tag)))
1101 ;; (set-buffer buffer)
1102 ;; (setq table (syntax-table))
1103 ;; (set-buffer current)
1104 ;; (set-syntax-table table)
1106 ;; (set-syntax-table otable)))))
1107 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
1109 ;; t if point is at a tag line that matches TAG "exactly".
1110 ;; point should be just after a string that matches TAG.
1111 (defun tag-exact-match-p (tag)
1112 ;; The match is really exact if there is an explicit tag name.
1113 (or (looking-at (concat "[^\177]*\177" (regexp-quote tag) "\001"))
1114 ;; We also call it "exact" if it is surrounded by symbol boundaries.
1115 ;; This is needed because etags does not always generate explicit names.
1116 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1118 (backward-char (1+ (length tag)))
1119 (and (looking-at "\\Sw") (looking-at "\\S_"))))))
1121 ;; t if point is at a tag line that matches TAG as a word.
1122 ;; point should be just after a string that matches TAG.
1123 (defun tag-word-match-p (tag)
1124 (and (looking-at "\\b.*\177")
1125 (save-excursion (backward-char (1+ (length tag)))
1126 (looking-at "\\b"))))
1128 ;; t if point is in a tag line with a tag containing TAG as a substring.
1129 (defun tag-any-match-p (tag)
1130 (looking-at ".*\177"))
1132 ;; t if point is at a tag line that matches RE as a regexp.
1133 (defun tag-re-match-p (re)
1136 (let ((bol (point)))
1137 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t)
1138 (re-search-backward re bol t)))))
1141 (defun next-file (&optional initialize novisit)
1142 "Select next file among files in current tags table.
1144 A first argument of t (prefix arg, if interactive) initializes to the
1145 beginning of the list of files in the tags table. If the argument is
1146 neither nil nor t, it is evalled to initialize the list of files.
1148 Non-nil second argument NOVISIT means use a temporary buffer
1149 to save time and avoid uninteresting warnings.
1151 Value is nil if the file was already visited;
1152 if the file was newly read in, the value is the filename."
1154 (cond ((not initialize)
1155 ;; Not the first run.
1158 ;; Initialize the list from the tags table.
1160 ;; Visit the tags table buffer to get its list of files.
1161 (visit-tags-table-buffer)
1162 (setq next-file-list (tags-table-files))))
1164 ;; Initialize the list by evalling the argument.
1165 (setq next-file-list (eval initialize))))
1168 ;; Get the files from the next tags table.
1169 ;; When doing (visit-tags-table-buffer t),
1170 ;; the tags table buffer must be current.
1171 (if (and (visit-tags-table-buffer 'same)
1172 (visit-tags-table-buffer t))
1173 (setq next-file-list (tags-table-files))
1175 (get-buffer " *next-file*")
1176 (kill-buffer " *next-file*"))
1177 (error "All files processed."))))
1178 (let ((new (not (get-file-buffer (car next-file-list)))))
1179 (if (not (and new novisit))
1180 (set-buffer (find-file-noselect (car next-file-list) novisit))
1181 ;; Like find-file, but avoids random warning messages.
1182 (set-buffer (get-buffer-create " *next-file*"))
1183 (kill-all-local-variables)
1185 (setq new (car next-file-list))
1186 (insert-file-contents new nil))
1187 (setq next-file-list (cdr next-file-list))
1190 (defvar tags-loop-operate nil
1191 "Form for `tags-loop-continue' to eval to change one file.")
1193 (defvar tags-loop-scan
1194 '(error (substitute-command-keys
1195 "No \\[tags-search] or \\[tags-query-replace] in progress."))
1196 "Form for `tags-loop-continue' to eval to scan one file.
1197 If it returns non-nil, this file needs processing by evalling
1198 \`tags-loop-operate'. Otherwise, move on to the next file.")
1201 (defun tags-loop-continue (&optional first-time)
1202 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1203 Used noninteractively with non-nil argument to begin such a command (the
1204 argument is passed to `next-file', which see).
1205 Two variables control the processing we do on each file:
1206 the value of `tags-loop-scan' is a form to be executed on each file
1207 to see if it is interesting (it returns non-nil if so)
1208 and `tags-loop-operate' is a form to execute to operate on an interesting file
1209 If the latter returns non-nil, we exit; otherwise we scan the next file."
1215 ;; Scan files quickly for the first or next interesting one.
1216 (while (or first-time
1219 (not (eval tags-loop-scan))))
1220 (setq new (next-file first-time t))
1221 ;; If NEW is non-nil, we got a temp buffer,
1222 ;; and NEW is the file name.
1224 (and (not first-time)
1225 (> baud-rate search-slow-speed)
1227 (message "Scanning file %s..." (or new buffer-file-name)))
1228 (setq first-time nil)
1229 (goto-char (point-min)))
1231 ;; If we visited it in a temp buffer, visit it now for real.
1233 (let ((pos (point)))
1235 (set-buffer (find-file-noselect new))
1239 (switch-to-buffer (current-buffer))
1241 ;; Now operate on the file.
1242 ;; If value is non-nil, continue to scan the next file.
1243 (eval tags-loop-operate)))
1245 (null tags-loop-operate)
1246 (message "Scanning file %s...found" buffer-file-name))))
1247 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
1250 (defun tags-search (regexp &optional file-list-form)
1251 "Search through all files listed in tags table for match for REGEXP.
1252 Stops when a match is found.
1253 To continue searching for next match, use command \\[tags-loop-continue].
1255 See documentation of variable `tags-file-name'."
1256 (interactive "sTags search (regexp): ")
1257 (if (and (equal regexp "")
1258 (eq (car tags-loop-scan) 're-search-forward)
1259 (null tags-loop-operate))
1260 ;; Continue last tags-search as if by M-,.
1261 (tags-loop-continue nil)
1262 (setq tags-loop-scan
1263 (list 're-search-forward regexp nil t)
1264 tags-loop-operate nil)
1265 (tags-loop-continue (or file-list-form t))))
1268 (defun tags-query-replace (from to &optional delimited file-list-form)
1269 "Query-replace-regexp FROM with TO through all files listed in tags table.
1270 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1271 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
1272 with the command \\[tags-loop-continue].
1274 See documentation of variable `tags-file-name'."
1276 "sTags query replace (regexp): \nsTags query replace %s by: \nP")
1277 (setq tags-loop-scan (list 'prog1
1278 (list 'if (list 're-search-forward from nil t)
1279 ;; When we find a match, move back
1280 ;; to the beginning of it so perform-replace
1282 '(goto-char (match-beginning 0))))
1283 tags-loop-operate (list 'perform-replace from to t t delimited))
1284 (tags-loop-continue (or file-list-form t)))
1287 (defun list-tags (file)
1288 "Display list of tags in file FILE.
1289 FILE should not contain a directory specification."
1290 (interactive (list (completing-read "List tags in file: "
1292 (visit-tags-table-buffer)
1294 (mapcar 'file-name-nondirectory
1295 (tags-table-files))))
1297 (with-output-to-temp-buffer "*Tags List*"
1298 (princ "Tags in file ")
1302 (let ((first-time t)
1304 (while (visit-tags-table-buffer (not first-time))
1305 (setq first-time nil)
1306 (if (funcall list-tags-function file)
1309 (error "File %s not in current tags tables" file))))))
1312 (defun tags-apropos (regexp)
1313 "Display list of all tags in tags table REGEXP matches."
1314 (interactive "sTags apropos (regexp): ")
1315 (with-output-to-temp-buffer "*Tags List*"
1316 (princ "Tags matching regexp ")
1320 (let ((first-time t))
1321 (while (visit-tags-table-buffer (not first-time))
1322 (setq first-time nil)
1323 (funcall tags-apropos-function regexp))))))
1325 ;;; XXX Kludge interface.
1327 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1329 (defun select-tags-table ()
1330 "Select a tags table file from a menu of those you have already used.
1331 The list of tags tables to select from is stored in `tags-table-file-list';
1332 see the doc of that variable if you want to add names to the list."
1334 (pop-to-buffer "*Tags Table List*")
1335 (setq buffer-read-only nil)
1337 (setq selective-display t
1338 selective-display-ellipses nil)
1339 (let ((set-list tags-table-set-list)
1340 (desired-point nil))
1343 (setq desired-point (point-marker))
1344 (princ tags-table-list (current-buffer))
1346 (prin1 (car tags-table-list) (current-buffer)) ;invisible
1349 (if (eq (car set-list) tags-table-list)
1350 ;; Already printed it.
1352 (princ (car set-list) (current-buffer))
1354 (prin1 (car (car set-list)) (current-buffer)) ;invisible
1356 (setq set-list (cdr set-list)))
1360 (setq desired-point (point-marker)))
1361 (insert tags-file-name "\C-m")
1362 (prin1 tags-file-name (current-buffer)) ;invisible
1364 (setq set-list (delete tags-file-name
1365 (apply 'nconc (cons tags-table-list
1366 (mapcar 'copy-sequence
1367 tags-table-set-list)))))
1369 (insert (car set-list) "\C-m")
1370 (prin1 (car set-list) (current-buffer)) ;invisible
1372 (setq set-list (delete (car set-list) set-list)))
1374 (insert-before-markers
1375 "Type `t' to select a tags table or set of tags tables:\n\n")
1377 (goto-char desired-point))
1378 (set-window-start (selected-window) 1 t))
1379 (set-buffer-modified-p nil)
1380 (setq buffer-read-only t
1381 mode-name "Select Tags Table")
1382 (let ((map (make-sparse-keymap)))
1383 (define-key map "t" 'select-tags-table-select)
1384 (define-key map " " 'next-line)
1385 (define-key map "\^?" 'previous-line)
1386 (define-key map "n" 'next-line)
1387 (define-key map "p" 'previous-line)
1388 (define-key map "q" 'select-tags-table-quit)
1389 (use-local-map map)))
1391 (defun select-tags-table-select ()
1392 "Select the tags table named on this line."
1394 (search-forward "\C-m")
1395 (let ((name (read (current-buffer))))
1396 (visit-tags-table name)
1397 (select-tags-table-quit)
1398 (message "Tags table now %s" name)))
1400 (defun select-tags-table-quit ()
1401 "Kill the buffer and delete the selected window."
1403 (kill-buffer (current-buffer))
1408 (defun complete-tag ()
1409 "Perform tags completion on the text around point.
1410 Completes to the set of names listed in the current tags table.
1411 The string to complete is chosen in the same way as the default
1412 for \\[find-tag] (which see)."
1416 (error (substitute-command-keys
1417 "No tags table loaded. Try \\[visit-tags-table].")))
1418 (let ((pattern (funcall (or find-tag-default-function
1419 (get major-mode 'find-tag-default-function)
1420 'find-tag-default)))
1424 (error "Nothing to complete"))
1425 (search-backward pattern)
1427 (forward-char (length pattern))
1428 (setq completion (try-completion pattern 'tags-complete-tag nil))
1429 (cond ((eq completion t))
1431 (message "Can't find completion for \"%s\"" pattern)
1433 ((not (string= pattern completion))
1434 (delete-region beg (point))
1435 (insert completion))
1437 (message "Making completion list...")
1438 (with-output-to-temp-buffer " *Completions*"
1439 (display-completion-list
1440 (all-completions pattern 'tags-complete-tag nil)))
1441 (message "Making completion list...%s" "done")))))
1443 ;;;###autoload (define-key esc-map "\t" 'complete-tag)
1447 ;;; etags.el ends here