etags.el fix for bug#13412
[bpt/emacs.git] / lisp / progmodes / etags.el
CommitLineData
e95a67dc 1;;; etags.el --- etags facility for Emacs -*- lexical-binding: t -*-
8c8f9bc1 2
ab422c4d
PE
3;; Copyright (C) 1985-1986, 1988-1989, 1992-1996, 1998, 2000-2013 Free
4;; Software Foundation, Inc.
ff1f0fa6 5
5762abec 6;; Author: Roland McGrath <roland@gnu.org>
d6b8c85b 7;; Maintainer: FSF
3a801d0c
ER
8;; Keywords: tools
9
ff1f0fa6
JB
10;; This file is part of GNU Emacs.
11
b1fc2b50 12;; GNU Emacs is free software: you can redistribute it and/or modify
ff1f0fa6 13;; it under the terms of the GNU General Public License as published by
b1fc2b50
GM
14;; the Free Software Foundation, either version 3 of the License, or
15;; (at your option) any later version.
ff1f0fa6
JB
16
17;; GNU Emacs is distributed in the hope that it will be useful,
18;; but WITHOUT ANY WARRANTY; without even the implied warranty of
19;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20;; GNU General Public License for more details.
21
22;; You should have received a copy of the GNU General Public License
b1fc2b50 23;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
ff1f0fa6 24
6010664f
SM
25;;; Commentary:
26
e5167999
ER
27;;; Code:
28
b533f3c5 29(require 'ring)
89228b63 30(require 'button)
b533f3c5 31
c086701a 32;;;###autoload
b6176f64 33(defvar tags-file-name nil
fb7ada5f 34 "File name of tags table.
9708f7fc 35To switch to a new tags table, setting this variable is sufficient.
b6176f64 36If you set this variable, do not also set `tags-table-list'.
9708f7fc 37Use the `etags' program to make a tags table file.")
b6176f64 38;; Make M-x set-variable tags-file-name like M-x visit-tags-table.
1e8780b1 39;;;###autoload (put 'tags-file-name 'variable-interactive (purecopy "fVisit tags table: "))
6f1a6faf 40;;;###autoload (put 'tags-file-name 'safe-local-variable 'stringp)
9708f7fc 41
99730b0e 42(defgroup etags nil "Tags tables."
99783bde
RS
43 :group 'tools)
44
ece6e35a
GM
45;;;###autoload
46(defcustom tags-case-fold-search 'default
fb7ada5f 47 "Whether tags operations should be case-sensitive.
ece6e35a
GM
48A value of t means case-insensitive, a value of nil means case-sensitive.
49Any 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")
55
9708f7fc 56;;;###autoload
b6176f64 57;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
99783bde 58(defcustom tags-table-list nil
fb7ada5f 59 "List of file names of tags tables to search.
b6176f64
RM
60An element that is a directory means the file \"TAGS\" in that directory.
61To switch to a new list of tags tables, setting this variable is sufficient.
62If you set this variable, do not also set `tags-file-name'.
99783bde
RS
63Use the `etags' program to make a tags table file."
64 :group 'etags
65 :type '(repeat file))
9708f7fc 66
ddc76b00 67;;;###autoload
4c964351
UM
68(defcustom tags-compression-info-list
69 (purecopy '("" ".Z" ".bz2" ".gz" ".xz" ".tgz"))
220740a3
GM
70 "List of extensions tried by etags when `auto-compression-mode' is on.
71An empty string means search the non-compressed file."
4c964351 72 :version "24.1" ; added xz
1ea98518 73 :type '(repeat string)
ddc76b00
FP
74 :group 'etags)
75
1ea98518
SM
76;; !!! tags-compression-info-list should probably be replaced by access
77;; to directory list and matching jka-compr-compression-info-list. Currently,
78;; this implementation forces each modification of
79;; jka-compr-compression-info-list to be reflected in this var.
80;; An alternative could be to say that introducing a special
81;; element in this list (e.g. t) means : try at this point
82;; using directory listing and regexp matching using
83;; jka-compr-compression-info-list.
ddc76b00
FP
84
85
33633b28 86;;;###autoload
99783bde 87(defcustom tags-add-tables 'ask-user
fb7ada5f 88 "Control whether to add a new tags table to the current list.
0c37b824
RS
89t means do; nil means don't (always start a new list).
90Any other value means ask the user whether to add a new tags table
99783bde
RS
91to the current list (as opposed to starting a new list)."
92 :group 'etags
93 :type '(choice (const :tag "Do" t)
94 (const :tag "Don't" nil)
add3e8b3 95 (other :tag "Ask" ask-user)))
99783bde
RS
96
97(defcustom tags-revert-without-query nil
fb7ada5f 98 "Non-nil means reread a TAGS table without querying, if it has changed."
99783bde
RS
99 :group 'etags
100 :type 'boolean)
5f8cdaf2 101
a3d358c6
RM
102(defvar tags-table-computed-list nil
103 "List of tags tables to search, computed from `tags-table-list'.
104This includes tables implicitly included by other tables. The list is not
105always complete: the included tables of a table are not known until that
6010664f 106table is read into core. An element that is t is a placeholder
a3d358c6
RM
107indicating that the preceding element is a table that has not been read
108into core and might contain included tables to search.
109See `tags-table-check-computed-list'.")
110
111(defvar tags-table-computed-list-for nil
112 "Value of `tags-table-list' that `tags-table-computed-list' corresponds to.
113If `tags-table-list' changes, `tags-table-computed-list' is thrown away and
114recomputed; see `tags-table-check-computed-list'.")
115
9708f7fc 116(defvar tags-table-list-pointer nil
a3d358c6 117 "Pointer into `tags-table-computed-list' for the current state of searching.
47f3c459
RM
118Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
119
120(defvar tags-table-list-started-at nil
a3d358c6 121 "Pointer into `tags-table-computed-list', where the current search started.")
9708f7fc
RM
122
123(defvar tags-table-set-list nil
124 "List of sets of tags table which have been used together in the past.
125Each element is a list of strings which are file names.")
126
127;;;###autoload
99783bde 128(defcustom find-tag-hook nil
fb7ada5f 129 "Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'.
9708f7fc 130The value in the buffer in which \\[find-tag] is done is used,
99783bde
RS
131not the value in the buffer \\[find-tag] goes to."
132 :group 'etags
133 :type 'hook)
9708f7fc
RM
134
135;;;###autoload
99783bde 136(defcustom find-tag-default-function nil
fb7ada5f 137 "A function of no arguments used by \\[find-tag] to pick a default tag.
47f3c459 138If nil, and the symbol that is the value of `major-mode'
9708f7fc 139has a `find-tag-default-function' property (see `put'), that is used.
99783bde
RS
140Otherwise, `find-tag-default' is used."
141 :group 'etags
d639db36 142 :type '(choice (const nil) function))
9708f7fc 143
b533f3c5 144(defcustom find-tag-marker-ring-length 16
fb7ada5f 145 "Length of marker rings `find-tag-marker-ring' and `tags-location-ring'."
b533f3c5 146 :group 'etags
cd32a7ba
DN
147 :type 'integer
148 :version "20.3")
b533f3c5 149
7e7b42b2 150(defcustom tags-tag-face 'default
fb7ada5f 151 "Face for tags in the output of `tags-apropos'."
7e7b42b2
GM
152 :group 'etags
153 :type 'face
154 :version "21.1")
155
156(defcustom tags-apropos-verbose nil
157 "If non-nil, print the name of the tags file in the *Tags List* buffer."
158 :group 'etags
159 :type 'boolean
160 :version "21.1")
161
162(defcustom tags-apropos-additional-actions nil
163 "Specify additional actions for `tags-apropos'.
164
165If non-nil, value should be a list of triples (TITLE FUNCTION
166TO-SEARCH). For each triple, `tags-apropos' processes TO-SEARCH and
167lists tags from it. TO-SEARCH should be an alist, obarray, or symbol.
168If it is a symbol, the symbol's value is used.
6010664f 169TITLE, a string, is a title used to label the additional list of tags.
7e7b42b2
GM
170FUNCTION is a function to call when a symbol is selected in the
171*Tags List* buffer. It will be called with one argument SYMBOL which
172is the symbol being selected.
173
174Example value:
175
176 '((\"Emacs Lisp\" Info-goto-emacs-command-node obarray)
177 (\"Common Lisp\" common-lisp-hyperspec common-lisp-hyperspec-obarray)
178 (\"SCWM\" scwm-documentation scwm-obarray))"
179 :group 'etags
61520f26
DL
180 :type '(repeat (list (string :tag "Title")
181 function
182 (sexp :tag "Tags to search")))
7e7b42b2
GM
183 :version "21.1")
184
b533f3c5
RS
185(defvar find-tag-marker-ring (make-ring find-tag-marker-ring-length)
186 "Ring of markers which are locations from which \\[find-tag] was invoked.")
187
9708f7fc 188(defvar default-tags-table-function nil
5f8cdaf2
RS
189 "If non-nil, a function to choose a default tags file for a buffer.
190This function receives no arguments and should return the default
191tags table file to use for the current buffer.")
d74e816f 192
b533f3c5
RS
193(defvar tags-location-ring (make-ring find-tag-marker-ring-length)
194 "Ring of markers which are locations visited by \\[find-tag].
d74e816f 195Pop back to the last location with \\[negative-argument] \\[find-tag].")
9708f7fc
RM
196\f
197;; Tags table state.
198;; These variables are local in tags table buffers.
ff1f0fa6 199
9708f7fc
RM
200(defvar tags-table-files nil
201 "List of file names covered by current tags table.
202nil means it has not yet been computed; use `tags-table-files' to do so.")
203
204(defvar tags-completion-table nil
7e7b42b2 205 "Obarray of tag names defined in current tags table.")
9708f7fc
RM
206
207(defvar tags-included-tables nil
208 "List of tags tables included by the current tags table.")
209
210(defvar next-file-list nil
211 "List of files for \\[next-file] to process.")
212\f
213;; Hooks for file formats.
214
d6b8c85b
SM
215(defvar tags-table-format-functions '(etags-recognize-tags-table
216 tags-recognize-empty-tags-table)
6010664f 217 "Hook to be called in a tags table buffer to identify the type of tags table.
b533f3c5 218The functions are called in order, with no arguments,
9708f7fc
RM
219until one returns non-nil. The function should make buffer-local bindings
220of the format-parsing tags function variables if successful.")
221
222(defvar file-of-tag-function nil
79e01623
JB
223 "Function to do the work of `file-of-tag' (which see).
224One optional argument, a boolean specifying to return complete path (nil) or
225relative path (non-nil).")
9708f7fc
RM
226(defvar tags-table-files-function nil
227 "Function to do the work of `tags-table-files' (which see).")
228(defvar tags-completion-table-function nil
6010664f 229 "Function to build the `tags-completion-table'.")
9708f7fc 230(defvar snarf-tag-function nil
79e01623
JB
231 "Function to get info about a matched tag for `goto-tag-location-function'.
232One optional argument, specifying to use explicit tag (non-nil) or not (nil).
233The default is nil.")
9708f7fc
RM
234(defvar goto-tag-location-function nil
235 "Function of to go to the location in the buffer specified by a tag.
236One argument, the tag info returned by `snarf-tag-function'.")
237(defvar find-tag-regexp-search-function nil
238 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
239(defvar find-tag-regexp-tag-order nil
240 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
241(defvar find-tag-regexp-next-line-after-failure-p nil
242 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
243(defvar find-tag-search-function nil
244 "Search function passed to `find-tag-in-order' for finding a tag.")
245(defvar find-tag-tag-order nil
246 "Tag order passed to `find-tag-in-order' for finding a tag.")
247(defvar find-tag-next-line-after-failure-p nil
248 "Flag passed to `find-tag-in-order' for finding a tag.")
249(defvar list-tags-function nil
250 "Function to do the work of `list-tags' (which see).")
251(defvar tags-apropos-function nil
252 "Function to do the work of `tags-apropos' (which see).")
253(defvar tags-included-tables-function nil
254 "Function to do the work of `tags-included-tables' (which see).")
255(defvar verify-tags-table-function nil
e7f767c2 256 "Function to return t if current buffer contains valid tags file.")
9708f7fc
RM
257\f
258(defun initialize-new-tags-table ()
cba9bdd2
RS
259 "Initialize the tags table in the current buffer.
260Return non-nil if it is a valid tags table, and
e02f48d7 261in that case, also make the tags table state variables
cba9bdd2 262buffer-local and set them to nil."
b6176f64
RM
263 (set (make-local-variable 'tags-table-files) nil)
264 (set (make-local-variable 'tags-completion-table) nil)
265 (set (make-local-variable 'tags-included-tables) nil)
931f525c
RS
266 ;; We used to initialize find-tag-marker-ring and tags-location-ring
267 ;; here, to new empty rings. But that is wrong, because those
268 ;; are global.
269
9708f7fc 270 ;; Value is t if we have found a valid tags table buffer.
d6b8c85b 271 (run-hook-with-args-until-success 'tags-table-format-functions))
ff1f0fa6 272
0f518b6b
JL
273;;;###autoload
274(defun tags-table-mode ()
275 "Major mode for tags table file buffers."
276 (interactive)
175069ef 277 (setq major-mode 'tags-table-mode ;FIXME: Use define-derived-mode.
5ec19a11 278 mode-name "Tags Table"
8e004a44 279 buffer-undo-list t)
0f518b6b
JL
280 (initialize-new-tags-table))
281
c086701a 282;;;###autoload
9708f7fc
RM
283(defun visit-tags-table (file &optional local)
284 "Tell tags commands to use tags table file FILE.
ff1f0fa6 285FILE should be the name of a file created with the `etags' program.
9708f7fc
RM
286A directory name is ok too; it means file TAGS in that directory.
287
288Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
289With a prefix arg, set the buffer-local value instead.
290When you find a tag with \\[find-tag], the buffer it finds the tag
291in is given a local value of this variable which is the name of the tags
292file the tag was in."
5b76833f 293 (interactive (list (read-file-name "Visit tags table (default TAGS): "
ff1f0fa6 294 default-directory
9708f7fc
RM
295 (expand-file-name "TAGS"
296 default-directory)
297 t)
298 current-prefix-arg))
aab936ad
RM
299 (or (stringp file) (signal 'wrong-type-argument (list 'stringp file)))
300 ;; Bind tags-file-name so we can control below whether the local or
dcaaec23 301 ;; global value gets set.
5e882a6a
RS
302 ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
303 ;; initialize a buffer for FILE and set tags-file-name to the
b6176f64 304 ;; fully-expanded name.
9806213d
RM
305 (let ((tags-file-name file))
306 (save-excursion
aab936ad 307 (or (visit-tags-table-buffer file)
9806213d
RM
308 (signal 'file-error (list "Visiting tags table"
309 "file does not exist"
310 file)))
b6176f64 311 ;; Set FILE to the expanded name.
9806213d 312 (setq file tags-file-name)))
9708f7fc 313 (if local
b6176f64 314 ;; Set the local value of tags-file-name.
47f3c459 315 (set (make-local-variable 'tags-file-name) file)
b6176f64 316 ;; Set the global value of tags-file-name.
9806213d
RM
317 (setq-default tags-file-name file)))
318
a3d358c6
RM
319(defun tags-table-check-computed-list ()
320 "Compute `tags-table-computed-list' from `tags-table-list' if necessary."
321 (let ((expanded-list (mapcar 'tags-expand-table-name tags-table-list)))
322 (or (equal tags-table-computed-list-for expanded-list)
323 ;; The list (or default-directory) has changed since last computed.
324 (let* ((compute-for (mapcar 'copy-sequence expanded-list))
325 (tables (copy-sequence compute-for)) ;Mutated in the loop.
326 (computed nil)
327 table-buffer)
328
329 (while tables
330 (setq computed (cons (car tables) computed)
331 table-buffer (get-file-buffer (car tables)))
83287e5b 332 (if (and table-buffer
c49a777a
RM
333 ;; There is a buffer visiting the file. Now make sure
334 ;; it is initialized as a tag table buffer.
335 (save-excursion
336 (tags-verify-table (buffer-file-name table-buffer))))
dcaaec23 337 (with-current-buffer table-buffer
19503d5a
GM
338 ;; Needed so long as etags-tags-included-tables
339 ;; does not save-excursion.
340 (save-excursion
341 (if (tags-included-tables)
342 ;; Insert the included tables into the list we
343 ;; are processing.
344 (setcdr tables (nconc (mapcar 'tags-expand-table-name
345 (tags-included-tables))
346 (cdr tables))))))
a3d358c6
RM
347 ;; This table is not in core yet. Insert a placeholder
348 ;; saying we must read it into core to check for included
349 ;; tables before searching the next table in the list.
350 (setq computed (cons t computed)))
351 (setq tables (cdr tables)))
352
353 ;; Record the tags-table-list value (and the context of the
354 ;; current directory) we computed from.
355 (setq tags-table-computed-list-for compute-for
356 tags-table-computed-list (nreverse computed))))))
357
a3d358c6 358(defun tags-table-extend-computed-list ()
cba9bdd2
RS
359 "Extend `tags-table-computed-list' to remove the first t placeholder.
360
361An element of the list that is t is a placeholder indicating that the
362preceding element is a table that has not been read in and might
363contain included tables to search. This function reads in the first
364such table and puts its included tables into the list."
a3d358c6
RM
365 (let ((list tags-table-computed-list))
366 (while (not (eq (nth 1 list) t))
367 (setq list (cdr list)))
368 (save-excursion
369 (if (tags-verify-table (car list))
370 ;; We are now in the buffer visiting (car LIST). Extract its
371 ;; list of included tables and insert it into the computed list.
372 (let ((tables (tags-included-tables))
373 (computed nil)
374 table-buffer)
375 (while tables
376 (setq computed (cons (car tables) computed)
377 table-buffer (get-file-buffer (car tables)))
378 (if table-buffer
dcaaec23 379 (with-current-buffer table-buffer
a3d358c6
RM
380 (if (tags-included-tables)
381 ;; Insert the included tables into the list we
382 ;; are processing.
383 (setcdr tables (append (tags-included-tables)
384 tables))))
385 ;; This table is not in core yet. Insert a placeholder
386 ;; saying we must read it into core to check for included
387 ;; tables before searching the next table in the list.
e99045bb
RM
388 (setq computed (cons t computed)))
389 (setq tables (cdr tables)))
a3d358c6
RM
390 (setq computed (nreverse computed))
391 ;; COMPUTED now contains the list of included tables (and
392 ;; tables included by them, etc.). Now splice this into the
393 ;; current list.
394 (setcdr list (nconc computed (cdr (cdr list)))))
395 ;; It was not a valid table, so just remove the following placeholder.
396 (setcdr list (cdr (cdr list)))))))
b6176f64 397
47f3c459 398(defun tags-expand-table-name (file)
cba9bdd2 399 "Expand tags table name FILE into a complete file name."
47f3c459
RM
400 (setq file (expand-file-name file))
401 (if (file-directory-p file)
402 (expand-file-name "TAGS" file)
403 file))
404
a3d358c6
RM
405;; Like member, but comparison is done after tags-expand-table-name on both
406;; sides and elements of LIST that are t are skipped.
407(defun tags-table-list-member (file list)
cba9bdd2
RS
408 "Like (member FILE LIST) after applying `tags-expand-table-name'.
409More precisely, apply `tags-expand-table-name' to FILE
410and each element of LIST, returning the link whose car is the first match.
411If an element of LIST is t, ignore it."
47f3c459 412 (setq file (tags-expand-table-name file))
a3d358c6
RM
413 (while (and list
414 (or (eq (car list) t)
415 (not (string= file (tags-expand-table-name (car list))))))
416 (setq list (cdr list)))
47f3c459
RM
417 list)
418
a3d358c6
RM
419(defun tags-verify-table (file)
420 "Read FILE into a buffer and verify that it is a valid tags table.
421Sets the current buffer to one visiting FILE (if it exists).
e7f767c2 422Returns non-nil if it is a valid table."
a3d358c6
RM
423 (if (get-file-buffer file)
424 ;; The file is already in a buffer. Check for the visited file
425 ;; having changed since we last used it.
8c0bf8b3 426 (progn
a3d358c6 427 (set-buffer (get-file-buffer file))
8c0bf8b3 428 (or verify-tags-table-function (tags-table-mode))
a3d358c6 429 (if (or (verify-visited-file-modtime (current-buffer))
350ce4cf
RS
430 ;; Decide whether to revert the file.
431 ;; revert-without-query can say to revert
432 ;; or the user can say to revert.
433 (not (or (let ((tail revert-without-query)
434 (found nil))
435 (while tail
436 (if (string-match (car tail) buffer-file-name)
437 (setq found t))
438 (setq tail (cdr tail)))
439 found)
99783bde 440 tags-revert-without-query
350ce4cf
RS
441 (yes-or-no-p
442 (format "Tags file %s has changed, read new contents? "
443 file)))))
444 (and verify-tags-table-function
445 (funcall verify-tags-table-function))
a3d358c6 446 (revert-buffer t t)
0f518b6b 447 (tags-table-mode)))
dcaaec23
SM
448 (when (file-exists-p file)
449 (let* ((buf (find-file-noselect file))
450 (newfile (buffer-file-name buf)))
451 (unless (string= file newfile)
452 ;; find-file-noselect has changed the file name.
453 ;; Propagate the change to tags-file-name and tags-table-list.
454 (let ((tail (member file tags-table-list)))
455 (if tail (setcar tail newfile)))
456 (if (eq file tags-file-name) (setq tags-file-name newfile)))
457 ;; Only change buffer now that we're done using potentially
458 ;; buffer-local variables.
459 (set-buffer buf)
460 (tags-table-mode)))))
a3d358c6
RM
461
462;; Subroutine of visit-tags-table-buffer. Search the current tags tables
463;; for one that has tags for THIS-FILE (or that includes a table that
5a5fa834 464;; does). Return the name of the first table listing THIS-FILE; if
c3dea9ba
RM
465;; the table is one included by another table, it is the master table that
466;; we return. If CORE-ONLY is non-nil, check only tags tables that are
467;; already in buffers--don't visit any new files.
a3d358c6 468(defun tags-table-including (this-file core-only)
cba9bdd2
RS
469 "Search current tags tables for tags for THIS-FILE.
470Subroutine of `visit-tags-table-buffer'.
471Looks for a tags table that has such tags or that includes a table
472that has them. Returns the name of the first such table.
473Non-nil CORE-ONLY means check only tags tables that are already in
40b1a3a9 474buffers. If CORE-ONLY is nil, it is ignored."
a3d358c6 475 (let ((tables tags-table-computed-list)
f06df563 476 (found nil))
a3d358c6 477 ;; Loop over the list, looking for a table containing tags for THIS-FILE.
47f3c459
RM
478 (while (and (not found)
479 tables)
9d591df8
RM
480
481 (if core-only
482 ;; Skip tables not in core.
483 (while (eq (nth 1 tables) t)
484 (setq tables (cdr (cdr tables))))
485 (if (eq (nth 1 tables) t)
486 ;; This table has not been read into core yet. Read it in now.
487 (tags-table-extend-computed-list)))
a3d358c6 488
3cc3f703
RM
489 (if tables
490 ;; Select the tags table buffer and get the file list up to date.
491 (let ((tags-file-name (car tables)))
492 (visit-tags-table-buffer 'same)
83287e5b
RM
493 (if (member this-file (mapcar 'expand-file-name
494 (tags-table-files)))
3cc3f703
RM
495 ;; Found it.
496 (setq found tables))))
47f3c459 497 (setq tables (cdr tables)))
3cc3f703
RM
498 (if found
499 ;; Now determine if the table we found was one included by another
82f75cca
RM
500 ;; table, not explicitly listed. We do this by checking each
501 ;; element of the computed list to see if it appears in the user's
502 ;; explicit list; the last element we will check is FOUND itself.
503 ;; Then we return the last one which did in fact appear in
504 ;; tags-table-list.
3cc3f703
RM
505 (let ((could-be nil)
506 (elt tags-table-computed-list))
507 (while (not (eq elt (cdr found)))
508 (if (tags-table-list-member (car elt) tags-table-list)
509 ;; This table appears in the user's list, so it could be
510 ;; the one which includes the table we found.
82f75cca
RM
511 (setq could-be (car elt)))
512 (setq elt (cdr elt))
513 (if (eq t (car elt))
514 (setq elt (cdr elt))))
ec623f1b
RM
515 ;; The last element we found in the computed list before FOUND
516 ;; that appears in the user's list will be the table that
83287e5b 517 ;; included the one we found.
82f75cca 518 could-be))))
9708f7fc 519
a3d358c6 520(defun tags-next-table ()
cba9bdd2
RS
521 "Move `tags-table-list-pointer' along and set `tags-file-name'.
522Subroutine of `visit-tags-table-buffer'.\
523Returns nil when out of tables."
a3d358c6
RM
524 ;; If there is a placeholder element next, compute the list to replace it.
525 (while (eq (nth 1 tags-table-list-pointer) t)
526 (tags-table-extend-computed-list))
527
528 ;; Go to the next table in the list.
529 (setq tags-table-list-pointer (cdr tags-table-list-pointer))
530 (or tags-table-list-pointer
531 ;; Wrap around.
532 (setq tags-table-list-pointer tags-table-computed-list))
533
534 (if (eq tags-table-list-pointer tags-table-list-started-at)
535 ;; We have come full circle. No more tables.
536 (setq tags-table-list-pointer nil)
537 ;; Set tags-file-name to the name from the list. It is already expanded.
538 (setq tags-file-name (car tags-table-list-pointer))))
539
97534f32 540;;;###autoload
9708f7fc
RM
541(defun visit-tags-table-buffer (&optional cont)
542 "Select the buffer containing the current tags table.
aab936ad 543If optional arg is a string, visit that file as a tags table.
9708f7fc 544If optional arg is t, visit the next table in `tags-table-list'.
9708f7fc 545If optional arg is the atom `same', don't look for a new table;
b6176f64 546 just select the buffer visiting `tags-file-name'.
47f3c459 547If arg is nil or absent, choose a first buffer from information in
b6176f64 548 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
9708f7fc 549Returns t if it visits a tags table, or nil if there are no more in the list."
b6176f64
RM
550
551 ;; Set tags-file-name to the tags table file we want to visit.
a3d358c6
RM
552 (cond ((eq cont 'same)
553 ;; Use the ambient value of tags-file-name.
554 (or tags-file-name
71873e2b
SM
555 (user-error "%s"
556 (substitute-command-keys
557 (concat "No tags table in use; "
558 "use \\[visit-tags-table] to select one")))))
a3d358c6
RM
559 ((eq t cont)
560 ;; Find the next table.
561 (if (tags-next-table)
562 ;; Skip over nonexistent files.
563 (while (and (not (or (get-file-buffer tags-file-name)
564 (file-exists-p tags-file-name)))
565 (tags-next-table)))))
a3d358c6
RM
566 (t
567 ;; Pick a table out of our hat.
568 (tags-table-check-computed-list) ;Get it up to date, we might use it.
569 (setq tags-file-name
570 (or
571 ;; If passed a string, use that.
572 (if (stringp cont)
573 (prog1 cont
574 (setq cont nil)))
575 ;; First, try a local variable.
576 (cdr (assq 'tags-file-name (buffer-local-variables)))
577 ;; Second, try a user-specified function to guess.
578 (and default-tags-table-function
579 (funcall default-tags-table-function))
580 ;; Third, look for a tags table that contains tags for the
581 ;; current buffer's file. If one is found, the lists will
582 ;; be frobnicated, and CONT will be set non-nil so we don't
583 ;; do it below.
584 (and buffer-file-name
83287e5b 585 (or
c3dea9ba
RM
586 ;; First check only tables already in buffers.
587 (tags-table-including buffer-file-name t)
588 ;; Since that didn't find any, now do the
589 ;; expensive version: reading new files.
590 (tags-table-including buffer-file-name nil)))
a3d358c6
RM
591 ;; Fourth, use the user variable tags-file-name, if it is
592 ;; not already in the current list.
593 (and tags-file-name
594 (not (tags-table-list-member tags-file-name
595 tags-table-computed-list))
596 tags-file-name)
597 ;; Fifth, use the user variable giving the table list.
598 ;; Find the first element of the list that actually exists.
599 (let ((list tags-table-list)
600 file)
601 (while (and list
602 (setq file (tags-expand-table-name (car list)))
603 (not (get-file-buffer file))
604 (not (file-exists-p file)))
605 (setq list (cdr list)))
606 (car list))
607 ;; Finally, prompt the user for a file name.
608 (expand-file-name
5b76833f 609 (read-file-name "Visit tags table (default TAGS): "
a3d358c6
RM
610 default-directory
611 "TAGS"
612 t))))))
613
614 ;; Expand the table name into a full file name.
615 (setq tags-file-name (tags-expand-table-name tags-file-name))
616
7e7b42b2 617 (unless (and (eq cont t) (null tags-table-list-pointer))
a3d358c6 618 ;; Verify that tags-file-name names a valid tags table.
229b7986
RM
619 ;; Bind another variable with the value of tags-file-name
620 ;; before we switch buffers, in case tags-file-name is buffer-local.
621 (let ((curbuf (current-buffer))
622 (local-tags-file-name tags-file-name))
623 (if (tags-verify-table local-tags-file-name)
624
625 ;; We have a valid tags table.
626 (progn
627 ;; Bury the tags table buffer so it
628 ;; doesn't get in the user's way.
629 (bury-buffer (current-buffer))
630
631 ;; If this was a new table selection (CONT is nil), make
632 ;; sure tags-table-list includes the chosen table, and
633 ;; update the list pointer variables.
634 (or cont
635 ;; Look in the list for the table we chose.
636 (let ((found (tags-table-list-member
637 local-tags-file-name
638 tags-table-computed-list)))
639 (if found
640 ;; There it is. Just switch to it.
641 (setq tags-table-list-pointer found
642 tags-table-list-started-at found)
643
644 ;; The table is not in the current set.
645 ;; Try to find it in another previously used set.
646 (let ((sets tags-table-set-list))
647 (while (and sets
648 (not (tags-table-list-member
649 local-tags-file-name
650 (car sets))))
651 (setq sets (cdr sets)))
652 (if sets
653 ;; Found in some other set. Switch to that set.
654 (progn
655 (or (memq tags-table-list tags-table-set-list)
656 ;; Save the current list.
657 (setq tags-table-set-list
658 (cons tags-table-list
659 tags-table-set-list)))
660 (setq tags-table-list (car sets)))
661
662 ;; Not found in any existing set.
663 (if (and tags-table-list
664 (or (eq t tags-add-tables)
665 (and tags-add-tables
666 (y-or-n-p
667 (concat "Keep current list of "
668 "tags tables also? ")))))
669 ;; Add it to the current list.
670 (setq tags-table-list (cons local-tags-file-name
671 tags-table-list))
672
673 ;; Make a fresh list, and store the old one.
674 (message "Starting a new list of tags tables")
675 (or (null tags-table-list)
676 (memq tags-table-list tags-table-set-list)
f06df563
RM
677 (setq tags-table-set-list
678 (cons tags-table-list
679 tags-table-set-list)))
dd2cedb9
DL
680 ;; Clear out buffers holding old tables.
681 (dolist (table tags-table-list)
cba9bdd2 682 ;; The list can contain items t.
84d51f9b
DL
683 (if (stringp table)
684 (let ((buffer (find-buffer-visiting table)))
dd2cedb9 685 (if buffer
84d51f9b 686 (kill-buffer buffer)))))
229b7986
RM
687 (setq tags-table-list (list local-tags-file-name))))
688
689 ;; Recompute tags-table-computed-list.
690 (tags-table-check-computed-list)
691 ;; Set the tags table list state variables to start
692 ;; over from tags-table-computed-list.
693 (setq tags-table-list-started-at tags-table-computed-list
694 tags-table-list-pointer
695 tags-table-computed-list)))))
696
697 ;; Return of t says the tags table is valid.
698 t)
699
700 ;; The buffer was not valid. Don't use it again.
701 (set-buffer curbuf)
a3d358c6 702 (kill-local-variable 'tags-file-name)
229b7986 703 (if (eq local-tags-file-name tags-file-name)
e98cc0af 704 (setq tags-file-name nil))
71873e2b
SM
705 (user-error "File %s is not a valid tags table"
706 local-tags-file-name)))))
c6987f0b
RM
707
708(defun tags-reset-tags-tables ()
b533f3c5 709 "Reset tags state to cancel effect of any previous \\[visit-tags-table] or \\[find-tag]."
c6987f0b 710 (interactive)
466886a2
KH
711 ;; Clear out the markers we are throwing away.
712 (let ((i 0))
713 (while (< i find-tag-marker-ring-length)
714 (if (aref (cddr tags-location-ring) i)
715 (set-marker (aref (cddr tags-location-ring) i) nil))
716 (if (aref (cddr find-tag-marker-ring) i)
717 (set-marker (aref (cddr find-tag-marker-ring) i) nil))
718 (setq i (1+ i))))
c6987f0b 719 (setq tags-file-name nil
466886a2
KH
720 tags-location-ring (make-ring find-tag-marker-ring-length)
721 find-tag-marker-ring (make-ring find-tag-marker-ring-length)
c6987f0b
RM
722 tags-table-list nil
723 tags-table-computed-list nil
724 tags-table-computed-list-for nil
725 tags-table-list-pointer nil
726 tags-table-list-started-at nil
727 tags-table-set-list nil))
a128c7a0 728\f
79e01623 729(defun file-of-tag (&optional relative)
ff1f0fa6 730 "Return the file name of the file whose tags point is within.
9708f7fc 731Assumes the tags table is the current buffer.
79e01623
JB
732If RELATIVE is non-nil, file name returned is relative to tags
733table file's directory. If RELATIVE is nil, file name returned
734is complete."
735 (funcall file-of-tag-function relative))
ff1f0fa6 736
c086701a 737;;;###autoload
9708f7fc
RM
738(defun tags-table-files ()
739 "Return a list of files in the current tags table.
83287e5b
RM
740Assumes the tags table is the current buffer. The file names are returned
741as they appeared in the `etags' command that created the table, usually
742without directory names."
a128c7a0
RM
743 (or tags-table-files
744 (setq tags-table-files
745 (funcall tags-table-files-function))))
9708f7fc
RM
746
747(defun tags-included-tables ()
b6176f64
RM
748 "Return a list of tags tables included by the current table.
749Assumes the tags table is the current buffer."
9708f7fc
RM
750 (or tags-included-tables
751 (setq tags-included-tables (funcall tags-included-tables-function))))
752\f
9708f7fc 753(defun tags-completion-table ()
cba9bdd2
RS
754 "Build `tags-completion-table' on demand.
755The tags included in the completion table are those in the current
756tags table and its (recursively) included tags tables."
9708f7fc 757 (or tags-completion-table
f37de644 758 ;; No cached value for this buffer.
9708f7fc 759 (condition-case ()
f37de644
FP
760 (let (current-table combined-table)
761 (message "Making tags completion table for %s..." buffer-file-name)
762 (save-excursion
763 ;; Iterate over the current list of tags tables.
764 (while (visit-tags-table-buffer (and combined-table t))
765 ;; Find possible completions in this table.
766 (setq current-table (funcall tags-completion-table-function))
767 ;; Merge this buffer's completions into the combined table.
768 (if combined-table
769 (mapatoms
770 (lambda (sym) (intern (symbol-name sym) combined-table))
771 current-table)
772 (setq combined-table current-table))))
773 (message "Making tags completion table for %s...done"
774 buffer-file-name)
f0d732ae 775 ;; Cache the result in a buffer-local variable.
f37de644 776 (setq tags-completion-table combined-table))
9708f7fc
RM
777 (quit (message "Tags completion table construction aborted.")
778 (setq tags-completion-table nil)))))
779
b5507bc1 780(defun tags-lazy-completion-table ()
e95a67dc 781 (let ((buf (current-buffer)))
b5507bc1
SM
782 (lambda (string pred action)
783 (with-current-buffer buf
784 (save-excursion
785 ;; If we need to ask for the tag table, allow that.
786 (let ((enable-recursive-minibuffers t))
787 (visit-tags-table-buffer))
788 (complete-with-action action (tags-completion-table) string pred))))))
8c0bf8b3
SM
789
790;;;###autoload (defun tags-completion-at-point-function ()
791;;;###autoload (if (or tags-table-list tags-file-name)
792;;;###autoload (progn
793;;;###autoload (load "etags")
794;;;###autoload (tags-completion-at-point-function))))
795
796(defun tags-completion-at-point-function ()
797 "Using tags, return a completion table for the text around point.
798If no tags table is loaded, do nothing and return nil."
799 (when (or tags-table-list tags-file-name)
800 (let ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
801 tags-case-fold-search
802 case-fold-search))
803 (pattern (funcall (or find-tag-default-function
804 (get major-mode 'find-tag-default-function)
805 'find-tag-default)))
806 beg)
807 (when pattern
808 (save-excursion
af67c9d7
SM
809 (forward-char (1- (length pattern)))
810 (search-backward pattern)
811 (setq beg (point))
812 (forward-char (length pattern))
813 (list beg (point) (tags-lazy-completion-table) :exclusive 'no))))))
a128c7a0 814\f
ff1f0fa6 815(defun find-tag-tag (string)
cba9bdd2 816 "Read a tag name, with defaulting and completion."
8a294d90
FP
817 (let* ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
818 tags-case-fold-search
819 case-fold-search))
820 (default (funcall (or find-tag-default-function
9708f7fc
RM
821 (get major-mode 'find-tag-default-function)
822 'find-tag-default)))
823 (spec (completing-read (if default
ebc6b37c
SM
824 (format "%s (default %s): "
825 (substring string 0 (string-match "[ :]+\\'" string))
826 default)
9708f7fc 827 string)
b5507bc1 828 (tags-lazy-completion-table)
ab685b6b 829 nil nil nil nil default)))
b6176f64 830 (if (equal spec "")
71873e2b 831 (or default (user-error "There is no default tag"))
b6176f64 832 spec)))
ff1f0fa6 833
21800cb8
RM
834(defvar last-tag nil
835 "Last tag found by \\[find-tag].")
836
d74e816f 837(defun find-tag-interactive (prompt &optional no-default)
cba9bdd2
RS
838 "Get interactive arguments for tag functions.
839The functions using this are `find-tag-noselect',
840`find-tag-other-window', and `find-tag-regexp'."
926861fb 841 (if (and current-prefix-arg last-tag)
d74e816f
RM
842 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
843 '-
844 t))
845 (list (if no-default
846 (read-string prompt)
847 (find-tag-tag prompt)))))
848
cba9bdd2 849(defvar find-tag-history nil) ; Doc string?
c5507689 850
dd2cedb9 851;; Dynamic bondage:
0d56ae89
GM
852(defvar etags-case-fold-search)
853(defvar etags-syntax-table)
e02f48d7 854(defvar local-find-tag-hook)
dd2cedb9 855
c086701a 856;;;###autoload
9708f7fc
RM
857(defun find-tag-noselect (tagname &optional next-p regexp-p)
858 "Find tag (in current tags table) whose name contains TAGNAME.
f90a6155 859Returns the buffer containing the tag's definition and moves its point there,
9708f7fc
RM
860but does not select the buffer.
861The default for TAGNAME is the expression in the buffer near point.
862
d74e816f
RM
863If second arg NEXT-P is t (interactively, with prefix arg), search for
864another tag that matches the last tagname or regexp used. When there are
865multiple matches for a tag, more exact matches are found first. If NEXT-P
866is the atom `-' (interactively, with prefix arg that is a negative number
867or just \\[negative-argument]), pop back to the previous tag gone to.
ff1f0fa6 868
9708f7fc
RM
869If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
870
a70ea557 871A marker representing the point when this command is invoked is pushed
b533f3c5
RS
872onto a ring and may be popped back to with \\[pop-tag-mark].
873Contrast this with the ring of marks gone to by the command.
874
9708f7fc 875See documentation of variable `tags-file-name'."
d74e816f
RM
876 (interactive (find-tag-interactive "Find tag: "))
877
c5507689 878 (setq find-tag-history (cons tagname find-tag-history))
e1cf67b6
GM
879 ;; Save the current buffer's value of `find-tag-hook' before
880 ;; selecting the tags table buffer. For the same reason, save value
881 ;; of `tags-file-name' in case it has a buffer-local value.
dc0274bd 882 (let ((local-find-tag-hook find-tag-hook))
d74e816f
RM
883 (if (eq '- next-p)
884 ;; Pop back to a previous location.
b533f3c5 885 (if (ring-empty-p tags-location-ring)
71873e2b 886 (user-error "No previous tag locations")
b533f3c5 887 (let ((marker (ring-remove tags-location-ring 0)))
d74e816f
RM
888 (prog1
889 ;; Move to the saved location.
b533f3c5
RS
890 (set-buffer (or (marker-buffer marker)
891 (error "The marked buffer has been deleted")))
d74e816f 892 (goto-char (marker-position marker))
eb8c3be9 893 ;; Kill that marker so it doesn't slow down editing.
d74e816f
RM
894 (set-marker marker nil nil)
895 ;; Run the user's hook. Do we really want to do this for pop?
896 (run-hooks 'local-find-tag-hook))))
b533f3c5
RS
897 ;; Record whence we came.
898 (ring-insert find-tag-marker-ring (point-marker))
926861fb 899 (if (and next-p last-tag)
d74e816f
RM
900 ;; Find the same table we last used.
901 (visit-tags-table-buffer 'same)
902 ;; Pick a table to use.
903 (visit-tags-table-buffer)
904 ;; Record TAGNAME for a future call with NEXT-P non-nil.
905 (setq last-tag tagname))
4cc32db6
RS
906 ;; Record the location so we can pop back to it later.
907 (let ((marker (make-marker)))
dcaaec23
SM
908 (with-current-buffer
909 ;; find-tag-in-order does the real work.
910 (find-tag-in-order
911 (if (and next-p last-tag) last-tag tagname)
912 (if regexp-p
913 find-tag-regexp-search-function
914 find-tag-search-function)
915 (if regexp-p
916 find-tag-regexp-tag-order
917 find-tag-tag-order)
918 (if regexp-p
919 find-tag-regexp-next-line-after-failure-p
920 find-tag-next-line-after-failure-p)
921 (if regexp-p "matching" "containing")
922 (or (not next-p) (not last-tag)))
4cc32db6
RS
923 (set-marker marker (point))
924 (run-hooks 'local-find-tag-hook)
b533f3c5 925 (ring-insert tags-location-ring marker)
4cc32db6 926 (current-buffer))))))
ff1f0fa6 927
c086701a 928;;;###autoload
d74e816f 929(defun find-tag (tagname &optional next-p regexp-p)
9708f7fc
RM
930 "Find tag (in current tags table) whose name contains TAGNAME.
931Select the buffer containing the tag's definition, and move point there.
932The default for TAGNAME is the expression in the buffer around or before point.
c086701a 933
d74e816f
RM
934If second arg NEXT-P is t (interactively, with prefix arg), search for
935another tag that matches the last tagname or regexp used. When there are
936multiple matches for a tag, more exact matches are found first. If NEXT-P
937is the atom `-' (interactively, with prefix arg that is a negative number
938or just \\[negative-argument]), pop back to the previous tag gone to.
ff1f0fa6 939
b533f3c5
RS
940If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
941
a70ea557 942A marker representing the point when this command is invoked is pushed
b533f3c5
RS
943onto a ring and may be popped back to with \\[pop-tag-mark].
944Contrast this with the ring of marks gone to by the command.
945
9708f7fc 946See documentation of variable `tags-file-name'."
d74e816f 947 (interactive (find-tag-interactive "Find tag: "))
ebc6b37c
SM
948 (let* ((buf (find-tag-noselect tagname next-p regexp-p))
949 (pos (with-current-buffer buf (point))))
6010664f
SM
950 (condition-case nil
951 (switch-to-buffer buf)
ebc6b37c
SM
952 (error (pop-to-buffer buf)))
953 (goto-char pos)))
9708f7fc 954;;;###autoload (define-key esc-map "." 'find-tag)
ff1f0fa6 955
daa37602 956;;;###autoload
d74e816f 957(defun find-tag-other-window (tagname &optional next-p regexp-p)
9708f7fc 958 "Find tag (in current tags table) whose name contains TAGNAME.
d74e816f
RM
959Select the buffer containing the tag's definition in another window, and
960move point there. The default for TAGNAME is the expression in the buffer
961around or before point.
9708f7fc 962
d74e816f
RM
963If second arg NEXT-P is t (interactively, with prefix arg), search for
964another tag that matches the last tagname or regexp used. When there are
965multiple matches for a tag, more exact matches are found first. If NEXT-P
966is negative (interactively, with prefix arg that is a negative number or
967just \\[negative-argument]), pop back to the previous tag gone to.
9708f7fc 968
b533f3c5
RS
969If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
970
a70ea557 971A marker representing the point when this command is invoked is pushed
b533f3c5
RS
972onto a ring and may be popped back to with \\[pop-tag-mark].
973Contrast this with the ring of marks gone to by the command.
974
9708f7fc 975See documentation of variable `tags-file-name'."
d74e816f
RM
976 (interactive (find-tag-interactive "Find tag other window: "))
977
b6176f64
RM
978 ;; This hair is to deal with the case where the tag is found in the
979 ;; selected window's buffer; without the hair, point is moved in both
980 ;; windows. To prevent this, we save the selected window's point before
981 ;; doing find-tag-noselect, and restore it after.
982 (let* ((window-point (window-point (selected-window)))
d74e816f 983 (tagbuf (find-tag-noselect tagname next-p regexp-p))
49693298 984 (tagpoint (progn (set-buffer tagbuf) (point))))
b6176f64
RM
985 (set-window-point (prog1
986 (selected-window)
49693298
JB
987 (switch-to-buffer-other-window tagbuf)
988 ;; We have to set this new window's point; it
989 ;; might already have been displaying a
990 ;; different portion of tagbuf, in which case
991 ;; switch-to-buffer-other-window doesn't set
992 ;; the window's point from the buffer.
993 (set-window-point (selected-window) tagpoint))
b6176f64 994 window-point)))
9708f7fc
RM
995;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
996
29add8b9 997;;;###autoload
9708f7fc 998(defun find-tag-other-frame (tagname &optional next-p)
d74e816f
RM
999 "Find tag (in current tags table) whose name contains TAGNAME.
1000Select the buffer containing the tag's definition in another frame, and
1001move point there. The default for TAGNAME is the expression in the buffer
1002around or before point.
1003
1004If second arg NEXT-P is t (interactively, with prefix arg), search for
1005another tag that matches the last tagname or regexp used. When there are
1006multiple matches for a tag, more exact matches are found first. If NEXT-P
1007is negative (interactively, with prefix arg that is a negative number or
1008just \\[negative-argument]), pop back to the previous tag gone to.
daa37602 1009
b533f3c5
RS
1010If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
1011
a70ea557 1012A marker representing the point when this command is invoked is pushed
b533f3c5
RS
1013onto a ring and may be popped back to with \\[pop-tag-mark].
1014Contrast this with the ring of marks gone to by the command.
1015
9708f7fc 1016See documentation of variable `tags-file-name'."
d74e816f 1017 (interactive (find-tag-interactive "Find tag other frame: "))
9708f7fc
RM
1018 (let ((pop-up-frames t))
1019 (find-tag-other-window tagname next-p)))
1020;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
1021
daa37602 1022;;;###autoload
9708f7fc
RM
1023(defun find-tag-regexp (regexp &optional next-p other-window)
1024 "Find tag (in current tags table) whose name matches REGEXP.
1025Select the buffer containing the tag's definition and move point there.
daa37602 1026
d74e816f
RM
1027If second arg NEXT-P is t (interactively, with prefix arg), search for
1028another tag that matches the last tagname or regexp used. When there are
1029multiple matches for a tag, more exact matches are found first. If NEXT-P
1030is negative (interactively, with prefix arg that is a negative number or
1031just \\[negative-argument]), pop back to the previous tag gone to.
9708f7fc
RM
1032
1033If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
1034
a70ea557 1035A marker representing the point when this command is invoked is pushed
b533f3c5
RS
1036onto a ring and may be popped back to with \\[pop-tag-mark].
1037Contrast this with the ring of marks gone to by the command.
1038
9708f7fc 1039See documentation of variable `tags-file-name'."
d74e816f
RM
1040 (interactive (find-tag-interactive "Find tag regexp: " t))
1041 ;; We go through find-tag-other-window to do all the display hair there.
1042 (funcall (if other-window 'find-tag-other-window 'find-tag)
1043 regexp next-p t))
a6125773 1044;;;###autoload (define-key esc-map [?\C-.] 'find-tag-regexp)
b533f3c5
RS
1045
1046;;;###autoload (define-key esc-map "*" 'pop-tag-mark)
1047
1048;;;###autoload
1049(defun pop-tag-mark ()
1050 "Pop back to where \\[find-tag] was last invoked.
1051
1052This is distinct from invoking \\[find-tag] with a negative argument
1053since that pops a stack of markers at which tags were found, not from
1054where they were found."
1055 (interactive)
1056 (if (ring-empty-p find-tag-marker-ring)
1057 (error "No previous locations for find-tag invocation"))
1058 (let ((marker (ring-remove find-tag-marker-ring 0)))
1059 (switch-to-buffer (or (marker-buffer marker)
1060 (error "The marked buffer has been deleted")))
1061 (goto-char (marker-position marker))
1062 (set-marker marker nil nil)))
9708f7fc 1063\f
cba9bdd2
RS
1064(defvar tag-lines-already-matched nil
1065 "Matches remembered between calls.") ; Doc string: calls to what?
b7089f3c 1066
f90a6155
JB
1067(defun find-tag-in-order (pattern
1068 search-forward-func
1069 order
1070 next-line-after-failure-p
1071 matching
1072 first-search)
cba9bdd2
RS
1073 "Internal tag-finding function.
1074PATTERN is a string to pass to arg SEARCH-FORWARD-FUNC, and to any
1075member of the function list ORDER. If ORDER is nil, use saved state
1076to continue a previous search.
1077
1078Arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
1079point should be moved to the next line.
1080
1081Arg MATCHING is a string, an English `-ing' word, to be used in an
1082error message."
1083;; Algorithm is as follows:
1084;; For each qualifier-func in ORDER, go to beginning of tags file, and
1085;; perform inner loop: for each naive match for PATTERN found using
1086;; SEARCH-FORWARD-FUNC, qualify the naive match using qualifier-func. If
1087;; it qualifies, go to the specified line in the specified source file
1088;; and return. Qualified matches are remembered to avoid repetition.
1089;; State is saved so that the loop can be continued.
9708f7fc
RM
1090 (let (file ;name of file containing tag
1091 tag-info ;where to find the tag in FILE
9708f7fc
RM
1092 (first-table t)
1093 (tag-order order)
b7089f3c 1094 (match-marker (make-marker))
9708f7fc 1095 goto-func
ece6e35a
GM
1096 (case-fold-search (if (memq tags-case-fold-search '(nil t))
1097 tags-case-fold-search
1098 case-fold-search))
9708f7fc
RM
1099 )
1100 (save-excursion
b7089f3c
RM
1101
1102 (if first-search
1103 ;; This is the start of a search for a fresh tag.
1104 ;; Clear the list of tags matched by the previous search.
1105 ;; find-tag-noselect has already put us in the first tags table
1106 ;; buffer before we got called.
1107 (setq tag-lines-already-matched nil)
1108 ;; Continuing to search for the tag specified last time.
1109 ;; tag-lines-already-matched lists locations matched in previous
1110 ;; calls so we don't visit the same tag twice if it matches twice
1111 ;; during two passes with different qualification predicates.
1112 ;; Switch to the current tags table buffer.
1113 (visit-tags-table-buffer 'same))
47f3c459 1114
9708f7fc 1115 ;; Get a qualified match.
83287e5b 1116 (catch 'qualified-match-found
47f3c459 1117
b6176f64 1118 ;; Iterate over the list of tags tables.
9708f7fc
RM
1119 (while (or first-table
1120 (visit-tags-table-buffer t))
1121
6218e8c6
RM
1122 (and first-search first-table
1123 ;; Start at beginning of tags file.
1124 (goto-char (point-min)))
5e882a6a 1125
6218e8c6 1126 (setq first-table nil)
9708f7fc 1127
b6176f64 1128 ;; Iterate over the list of ordering predicates.
9708f7fc
RM
1129 (while order
1130 (while (funcall search-forward-func pattern nil t)
1131 ;; Naive match found. Qualify the match.
1132 (and (funcall (car order) pattern)
1133 ;; Make sure it is not a previous qualified match.
9b026d9f 1134 (not (member (set-marker match-marker (point-at-bol))
b7089f3c 1135 tag-lines-already-matched))
9708f7fc
RM
1136 (throw 'qualified-match-found nil))
1137 (if next-line-after-failure-p
1138 (forward-line 1)))
1139 ;; Try the next flavor of match.
1140 (setq order (cdr order))
1141 (goto-char (point-min)))
1142 (setq order tag-order))
1143 ;; We throw out on match, so only get here if there were no matches.
b7089f3c 1144 ;; Clear out the markers we use to avoid duplicate matches so they
4c36be58 1145 ;; don't slow down editing and are immediately available for GC.
b7089f3c
RM
1146 (while tag-lines-already-matched
1147 (set-marker (car tag-lines-already-matched) nil nil)
1148 (setq tag-lines-already-matched (cdr tag-lines-already-matched)))
1149 (set-marker match-marker nil nil)
71873e2b
SM
1150 (user-error "No %stags %s %s" (if first-search "" "more ")
1151 matching pattern))
83287e5b 1152
9708f7fc
RM
1153 ;; Found a tag; extract location info.
1154 (beginning-of-line)
b7089f3c 1155 (setq tag-lines-already-matched (cons match-marker
9708f7fc
RM
1156 tag-lines-already-matched))
1157 ;; Expand the filename, using the tags table buffer's default-directory.
fb7775eb 1158 ;; We should be able to search for file-name backwards in file-of-tag:
ddc76b00 1159 ;; the beginning-of-line is ok except when positioned on a "file-name" tag.
fb7775eb 1160 (setq file (expand-file-name
ddc76b00
FP
1161 (if (memq (car order) '(tag-exact-file-name-match-p
1162 tag-file-name-match-p
1163 tag-partial-file-name-match-p))
7caf6803 1164 (save-excursion (forward-line 1)
fb7775eb
GM
1165 (file-of-tag))
1166 (file-of-tag)))
9708f7fc
RM
1167 tag-info (funcall snarf-tag-function))
1168
b6176f64 1169 ;; Get the local value in the tags table buffer before switching buffers.
9708f7fc 1170 (setq goto-func goto-tag-location-function)
84406262 1171 (tag-find-file-of-tag-noselect file)
9708f7fc
RM
1172 (widen)
1173 (push-mark)
83287e5b
RM
1174 (funcall goto-func tag-info)
1175
9708f7fc
RM
1176 ;; Return the buffer where the tag was found.
1177 (current-buffer))))
79e01623 1178
84406262 1179(defun tag-find-file-of-tag-noselect (file)
cba9bdd2
RS
1180 "Find the right line in the specified FILE."
1181 ;; If interested in compressed-files, search files with extensions.
1182 ;; Otherwise, search only the real file.
220740a3 1183 (let* ((buffer-search-extensions (if auto-compression-mode
79e01623
JB
1184 tags-compression-info-list
1185 '("")))
1186 the-buffer
1187 (file-search-extensions buffer-search-extensions))
1188 ;; search a buffer visiting the file with each possible extension
1189 ;; Note: there is a small inefficiency in find-buffer-visiting :
1190 ;; truename is computed even if not needed. Not too sure about this
1191 ;; but I suspect truename computation accesses the disk.
e1dbe924 1192 ;; It is maybe a good idea to optimize this find-buffer-visiting.
79e01623
JB
1193 ;; An alternative would be to use only get-file-buffer
1194 ;; but this looks less "sure" to find the buffer for the file.
1195 (while (and (not the-buffer) buffer-search-extensions)
1196 (setq the-buffer (find-buffer-visiting (concat file (car buffer-search-extensions))))
1197 (setq buffer-search-extensions (cdr buffer-search-extensions)))
1198 ;; if found a buffer but file modified, ensure we re-read !
1199 (if (and the-buffer (not (verify-visited-file-modtime the-buffer)))
1200 (find-file-noselect (buffer-file-name the-buffer)))
1201 ;; if no buffer found, search for files with possible extensions on disk
1202 (while (and (not the-buffer) file-search-extensions)
1203 (if (not (file-exists-p (concat file (car file-search-extensions))))
1204 (setq file-search-extensions (cdr file-search-extensions))
1205 (setq the-buffer (find-file-noselect (concat file (car file-search-extensions))))))
1206 (if (not the-buffer)
220740a3 1207 (if auto-compression-mode
79e01623
JB
1208 (error "File %s (with or without extensions %s) not found" file tags-compression-info-list)
1209 (error "File %s not found" file))
1210 (set-buffer the-buffer))))
1211
cba9bdd2 1212(defun tag-find-file-of-tag (file) ; Doc string?
84406262 1213 (let ((buf (tag-find-file-of-tag-noselect file)))
79e01623
JB
1214 (condition-case nil
1215 (switch-to-buffer buf)
1216 (error (pop-to-buffer buf)))))
9708f7fc
RM
1217\f
1218;; `etags' TAGS file format support.
1219
1220(defun etags-recognize-tags-table ()
cba9bdd2
RS
1221 "If `etags-verify-tags-table', make buffer-local format variables.
1222If current buffer is a valid etags TAGS file, then give it
1223buffer-local values of tags table format variables."
b6176f64 1224 (and (etags-verify-tags-table)
9508896e
JB
1225 ;; It is annoying to flash messages on the screen briefly,
1226 ;; and this message is not useful. -- rms
1227 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
dd2cedb9
DL
1228 (mapc (lambda (elt) (set (make-local-variable (car elt)) (cdr elt)))
1229 '((file-of-tag-function . etags-file-of-tag)
1230 (tags-table-files-function . etags-tags-table-files)
1231 (tags-completion-table-function . etags-tags-completion-table)
1232 (snarf-tag-function . etags-snarf-tag)
1233 (goto-tag-location-function . etags-goto-tag-location)
1234 (find-tag-regexp-search-function . re-search-forward)
1235 (find-tag-regexp-tag-order . (tag-re-match-p))
1236 (find-tag-regexp-next-line-after-failure-p . t)
1237 (find-tag-search-function . search-forward)
1238 (find-tag-tag-order . (tag-exact-file-name-match-p
ddc76b00 1239 tag-file-name-match-p
dd2cedb9 1240 tag-exact-match-p
d30ffe0e 1241 tag-implicit-name-match-p
dd2cedb9
DL
1242 tag-symbol-match-p
1243 tag-word-match-p
fb7775eb 1244 tag-partial-file-name-match-p
dd2cedb9
DL
1245 tag-any-match-p))
1246 (find-tag-next-line-after-failure-p . nil)
1247 (list-tags-function . etags-list-tags)
1248 (tags-apropos-function . etags-tags-apropos)
1249 (tags-included-tables-function . etags-tags-included-tables)
1250 (verify-tags-table-function . etags-verify-tags-table)
1251 ))))
9708f7fc
RM
1252
1253(defun etags-verify-tags-table ()
e7f767c2 1254 "Return non-nil if the current buffer is a valid etags TAGS file."
e4fc4f58 1255 ;; Use eq instead of = in case char-after returns nil.
a1906d51 1256 (eq (char-after (point-min)) ?\f))
9708f7fc 1257
cba9bdd2 1258(defun etags-file-of-tag (&optional relative) ; Doc string?
9708f7fc 1259 (save-excursion
5e0b7560 1260 (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n")
bfa4f190
SS
1261 (let ((str (convert-standard-filename
1262 (buffer-substring (match-beginning 1) (match-end 1)))))
79e01623
JB
1263 (if relative
1264 str
bfa4f190 1265 (expand-file-name str (file-truename default-directory))))))
9708f7fc 1266
aa27fbb4 1267
cba9bdd2 1268(defun etags-tags-completion-table () ; Doc string?
46de1c5a 1269 (let ((table (make-vector 511 0))
4f124fb5
EZ
1270 (progress-reporter
1271 (make-progress-reporter
1272 (format "Making tags completion table for %s..." buffer-file-name)
1273 (point-min) (point-max))))
9708f7fc
RM
1274 (save-excursion
1275 (goto-char (point-min))
4a92b718
RM
1276 ;; This monster regexp matches an etags tag line.
1277 ;; \1 is the string to match;
1278 ;; \2 is not interesting;
1279 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
eb6a920f
RM
1280 ;; \4 is not interesting;
1281 ;; \5 is the explicitly-specified tag name.
1282 ;; \6 is the line to start searching at;
1283 ;; \7 is the char to start searching at.
4a92b718 1284 (while (re-search-forward
a7885816 1285 "^\\(\\([^\177]+[^-a-zA-Z0-9_+*$:\177]+\\)?\
7e7b42b2
GM
1286\\([-a-zA-Z0-9_+*$?:]+\\)[^-a-zA-Z0-9_+*$?:\177]*\\)\177\
1287\\(\\([^\n\001]+\\)\001\\)?\\([0-9]+\\)?,\\([0-9]+\\)?\n"
4a92b718 1288 nil t)
46de1c5a
MY
1289 (intern (prog1 (if (match-beginning 5)
1290 ;; There is an explicit tag name.
1291 (buffer-substring (match-beginning 5) (match-end 5))
1292 ;; No explicit tag name. Best guess.
1293 (buffer-substring (match-beginning 3) (match-end 3)))
4f124fb5 1294 (progress-reporter-update progress-reporter (point)))
4a92b718 1295 table)))
9708f7fc
RM
1296 table))
1297
cba9bdd2 1298(defun etags-snarf-tag (&optional use-explicit) ; Doc string?
79e01623 1299 (let (tag-text line startpos explicit-start)
83287e5b
RM
1300 (if (save-excursion
1301 (forward-line -1)
1302 (looking-at "\f\n"))
1303 ;; The match was for a source file name, not any tag within a file.
1304 ;; Give text of t, meaning to go exactly to the location we specify,
1305 ;; the beginning of the file.
1306 (setq tag-text t
1307 line nil
a1906d51 1308 startpos (point-min))
83287e5b
RM
1309
1310 ;; Find the end of the tag and record the whole tag text.
1311 (search-forward "\177")
9b026d9f 1312 (setq tag-text (buffer-substring (1- (point)) (point-at-bol)))
79e01623
JB
1313 ;; If use-explicit is non nil and explicit tag is present, use it as part of
1314 ;; return value. Else just skip it.
1315 (setq explicit-start (point))
9b026d9f 1316 (when (and (search-forward "\001" (point-at-bol 2) t)
79e01623
JB
1317 use-explicit)
1318 (setq tag-text (buffer-substring explicit-start (1- (point)))))
1319
1320
83287e5b 1321 (if (looking-at "[0-9]")
027a4b6b
JB
1322 (setq line (string-to-number (buffer-substring
1323 (point)
1324 (progn (skip-chars-forward "0-9")
1325 (point))))))
83287e5b
RM
1326 (search-forward ",")
1327 (if (looking-at "[0-9]")
027a4b6b
JB
1328 (setq startpos (string-to-number (buffer-substring
1329 (point)
1330 (progn (skip-chars-forward "0-9")
1331 (point)))))))
9708f7fc
RM
1332 ;; Leave point on the next line of the tags file.
1333 (forward-line 1)
e1dec509
RM
1334 (cons tag-text (cons line startpos))))
1335
9708f7fc 1336(defun etags-goto-tag-location (tag-info)
cba9bdd2
RS
1337 "Go to location of tag specified by TAG-INFO.
1338TAG-INFO is a cons (TEXT LINE . POSITION).
1339TEXT is the initial part of a line containing the tag.
1340LINE is the line number.
1341POSITION is the (one-based) char position of TEXT within the file.
1342
1343If TEXT is t, it means the tag refers to exactly LINE or POSITION,
1344whichever is present, LINE having preference, no searching.
1345Either LINE or POSITION can be nil. POSITION is used if present.
1346
1347If the tag isn't exactly at the given position, then look near that
1348position using a search window that expands progressively until it
1349hits the start of file."
e1dec509 1350 (let ((startpos (cdr (cdr tag-info)))
a0f09378 1351 (line (car (cdr tag-info)))
83287e5b
RM
1352 offset found pat)
1353 (if (eq (car tag-info) t)
1354 ;; Direct file tag.
d80619fa
GM
1355 (cond (line (progn (goto-char (point-min))
1356 (forward-line (1- line))))
a0f09378 1357 (startpos (goto-char startpos))
83287e5b
RM
1358 (t (error "etags.el BUG: bogus direct file tag")))
1359 ;; This constant is 1/2 the initial search window.
1360 ;; There is no sense in making it too small,
1361 ;; since just going around the loop once probably
1362 ;; costs about as much as searching 2000 chars.
1363 (setq offset 1000
1364 found nil
1365 pat (concat (if (eq selective-display t)
1366 "\\(^\\|\^m\\)" "^")
1367 (regexp-quote (car tag-info))))
1368 ;; The character position in the tags table is 0-origin.
1369 ;; Convert it to a 1-origin Emacs character position.
1370 (if startpos (setq startpos (1+ startpos)))
1371 ;; If no char pos was given, try the given line number.
1372 (or startpos
a0f09378 1373 (if line
d80619fa
GM
1374 (setq startpos (progn (goto-char (point-min))
1375 (forward-line (1- line))
83287e5b
RM
1376 (point)))))
1377 (or startpos
1378 (setq startpos (point-min)))
1379 ;; First see if the tag is right at the specified location.
1380 (goto-char startpos)
1381 (setq found (looking-at pat))
1382 (while (and (not found)
1383 (progn
1384 (goto-char (- startpos offset))
1385 (not (bobp))))
1386 (setq found
1387 (re-search-forward pat (+ startpos offset) t)
1388 offset (* 3 offset))) ; expand search window
1389 (or found
1390 (re-search-forward pat nil t)
71873e2b
SM
1391 (user-error "Rerun etags: `%s' not found in %s"
1392 pat buffer-file-name)))
83287e5b
RM
1393 ;; Position point at the right place
1394 ;; if the search string matched an extra Ctrl-m at the beginning.
1395 (and (eq selective-display t)
1396 (looking-at "\^m")
1397 (forward-char 1))
1398 (beginning-of-line)))
9708f7fc 1399
cba9bdd2 1400(defun etags-list-tags (file) ; Doc string?
a1906d51 1401 (goto-char (point-min))
79e01623
JB
1402 (when (re-search-forward (concat "\f\n" "\\(" file "\\)" ",") nil t)
1403 (let ((path (save-excursion (forward-line 1) (file-of-tag)))
1404 ;; Get the local value in the tags table
1405 ;; buffer before switching buffers.
1406 (goto-func goto-tag-location-function)
1407 tag tag-info pt)
9708f7fc
RM
1408 (forward-line 1)
1409 (while (not (or (eobp) (looking-at "\f")))
126f3d39
LW
1410 ;; We used to use explicit tags when available, but the current goto-func
1411 ;; can only handle implicit tags.
1412 (setq tag-info (save-excursion (funcall snarf-tag-function nil))
79e01623
JB
1413 tag (car tag-info)
1414 pt (with-current-buffer standard-output (point)))
1415 (princ tag)
1416 (when (= (aref tag 0) ?\() (princ " ...)"))
1417 (with-current-buffer standard-output
1418 (make-text-button pt (point)
1419 'tag-info tag-info
1420 'file-path path
1421 'goto-func goto-func
1422 'action (lambda (button)
1423 (let ((tag-info (button-get button 'tag-info))
1424 (goto-func (button-get button 'goto-func)))
84406262 1425 (tag-find-file-of-tag (button-get button 'file-path))
79e01623
JB
1426 (widen)
1427 (funcall goto-func tag-info)))
26581f0e
CY
1428 'follow-link t
1429 'face tags-tag-face
79e01623 1430 'type 'button))
9708f7fc 1431 (terpri)
274d013d 1432 (forward-line 1))
79e01623 1433 t)))
9708f7fc 1434
7e7b42b2
GM
1435(defmacro tags-with-face (face &rest body)
1436 "Execute BODY, give output to `standard-output' face FACE."
06adf6b1 1437 (let ((pp (make-symbol "start")))
286c138d 1438 `(let ((,pp (with-current-buffer standard-output (point))))
7e7b42b2 1439 ,@body
286c138d 1440 (put-text-property ,pp (with-current-buffer standard-output (point))
7e7b42b2
GM
1441 'face ,face standard-output))))
1442
1443(defun etags-tags-apropos-additional (regexp)
1444 "Display tags matching REGEXP from `tags-apropos-additional-actions'."
1445 (with-current-buffer standard-output
1446 (dolist (oba tags-apropos-additional-actions)
1447 (princ "\n\n")
1448 (tags-with-face 'highlight (princ (car oba)))
1449 (princ":\n\n")
79e01623 1450 (let* ((beg (point))
7e7b42b2
GM
1451 (symbs (car (cddr oba)))
1452 (ins-symb (lambda (sy)
1453 (let ((sn (symbol-name sy)))
1454 (when (string-match regexp sn)
79e01623
JB
1455 (make-text-button (point)
1456 (progn (princ sy) (point))
1457 'action-internal(cadr oba)
1458 'action (lambda (button) (funcall
1459 (button-get button 'action-internal)
1460 (button-get button 'item)))
1461 'item sn
1462 'face tags-tag-face
26581f0e 1463 'follow-link t
79e01623 1464 'type 'button)
7e7b42b2
GM
1465 (terpri))))))
1466 (when (symbolp symbs)
1467 (if (boundp symbs)
1468 (setq symbs (symbol-value symbs))
1469 (insert "symbol `" (symbol-name symbs) "' has no value\n")
1470 (setq symbs nil)))
1471 (if (vectorp symbs)
1472 (mapatoms ins-symb symbs)
1473 (dolist (sy symbs)
1474 (funcall ins-symb (car sy))))
1475 (sort-lines nil beg (point))))))
1476
cba9bdd2 1477(defun etags-tags-apropos (string) ; Doc string?
7e7b42b2
GM
1478 (when tags-apropos-verbose
1479 (princ "Tags in file `")
1480 (tags-with-face 'highlight (princ buffer-file-name))
1481 (princ "':\n\n"))
a1906d51 1482 (goto-char (point-min))
4f124fb5
EZ
1483 (let ((progress-reporter (make-progress-reporter
1484 (format "Making tags apropos buffer for `%s'..."
1485 string)
1486 (point-min) (point-max))))
31f98a93 1487 (while (re-search-forward string nil t)
4f124fb5 1488 (progress-reporter-update progress-reporter (point))
31f98a93
MY
1489 (beginning-of-line)
1490
1491 (let* ( ;; Get the local value in the tags table
1492 ;; buffer before switching buffers.
1493 (goto-func goto-tag-location-function)
1494 (tag-info (save-excursion (funcall snarf-tag-function)))
1495 (tag (if (eq t (car tag-info)) nil (car tag-info)))
1496 (file-path (save-excursion (if tag (file-of-tag)
7caf6803 1497 (save-excursion (forward-line 1)
31f98a93
MY
1498 (file-of-tag)))))
1499 (file-label (if tag (file-of-tag t)
7caf6803 1500 (save-excursion (forward-line 1)
31f98a93
MY
1501 (file-of-tag t))))
1502 (pt (with-current-buffer standard-output (point))))
1503 (if tag
1504 (progn
1505 (princ (format "[%s]: " file-label))
1506 (princ tag)
1507 (when (= (aref tag 0) ?\() (princ " ...)"))
1508 (with-current-buffer standard-output
1509 (make-text-button pt (point)
1510 'tag-info tag-info
1511 'file-path file-path
1512 'goto-func goto-func
1513 'action (lambda (button)
1514 (let ((tag-info (button-get button 'tag-info))
1515 (goto-func (button-get button 'goto-func)))
1516 (tag-find-file-of-tag (button-get button 'file-path))
1517 (widen)
1518 (funcall goto-func tag-info)))
26581f0e
CY
1519 'follow-link t
1520 'face tags-tag-face
31f98a93
MY
1521 'type 'button)))
1522 (princ (format "- %s" file-label))
1523 (with-current-buffer standard-output
1524 (make-text-button pt (point)
1525 'file-path file-path
1526 'action (lambda (button)
1527 (tag-find-file-of-tag (button-get button 'file-path))
1528 ;; Get the local value in the tags table
1529 ;; buffer before switching buffers.
1530 (goto-char (point-min)))
26581f0e
CY
1531 'follow-link t
1532 'face tags-tag-face
1533 'type 'button))))
31f98a93
MY
1534 (terpri)
1535 (forward-line 1))
1536 (message nil))
7e7b42b2 1537 (when tags-apropos-verbose (princ "\n")))
9708f7fc 1538
cba9bdd2 1539(defun etags-tags-table-files () ; Doc string?
9708f7fc
RM
1540 (let ((files nil)
1541 beg)
1542 (goto-char (point-min))
1543 (while (search-forward "\f\n" nil t)
1544 (setq beg (point))
610c25c1
RS
1545 (end-of-line)
1546 (skip-chars-backward "^," beg)
1547 (or (looking-at "include$")
bfa4f190
SS
1548 (push (convert-standard-filename
1549 (buffer-substring beg (1- (point))))
1550 files)))
9708f7fc
RM
1551 (nreverse files)))
1552
19503d5a 1553;; FIXME? Should this save-excursion?
cba9bdd2 1554(defun etags-tags-included-tables () ; Doc string?
9708f7fc
RM
1555 (let ((files nil)
1556 beg)
1557 (goto-char (point-min))
1558 (while (search-forward "\f\n" nil t)
1559 (setq beg (point))
610c25c1
RS
1560 (end-of-line)
1561 (skip-chars-backward "^," beg)
bfa4f190
SS
1562 (when (looking-at "include$")
1563 ;; Expand in the default-directory of the tags table buffer.
1564 (push (expand-file-name (convert-standard-filename
1565 (buffer-substring beg (1- (point)))))
1566 files)))
9708f7fc
RM
1567 (nreverse files)))
1568\f
1569;; Empty tags file support.
1570
7e7b42b2 1571(defun tags-recognize-empty-tags-table ()
cba9bdd2
RS
1572 "Return non-nil if current buffer is empty.
1573If empty, make buffer-local values of the tags table format variables
1574that do nothing."
9708f7fc 1575 (and (zerop (buffer-size))
dd2cedb9
DL
1576 (mapc (lambda (sym) (set (make-local-variable sym) 'ignore))
1577 '(tags-table-files-function
1578 tags-completion-table-function
1579 find-tag-regexp-search-function
1580 find-tag-search-function
1581 tags-apropos-function
1582 tags-included-tables-function))
9708f7fc 1583 (set (make-local-variable 'verify-tags-table-function)
7e7b42b2 1584 (lambda () (zerop (buffer-size))))))
9708f7fc 1585\f
7e7b42b2 1586;; Match qualifier functions for tagnames.
2aa9d1be 1587;; These functions assume the etags file format defined in etc/ETAGS.EBNF.
9708f7fc 1588
6218e8c6
RM
1589;; This might be a neat idea, but it's too hairy at the moment.
1590;;(defmacro tags-with-syntax (&rest body)
dcaaec23
SM
1591;; `(with-syntax-table
1592;; (with-current-buffer (find-file-noselect (file-of-tag))
1593;; (syntax-table))
1594;; ,@body))
6218e8c6 1595;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
8a4c10dc 1596
2aa9d1be
FP
1597;; exact file name match, i.e. searched tag must match complete file
1598;; name including directories parts if there are some.
1599(defun tag-exact-file-name-match-p (tag)
cba9bdd2
RS
1600 "Return non-nil if TAG matches complete file name.
1601Any directory part of the file name is also matched."
2aa9d1be
FP
1602 (and (looking-at ",[0-9\n]")
1603 (save-excursion (backward-char (+ 2 (length tag)))
1604 (looking-at "\f\n"))))
cba9bdd2 1605
2aa9d1be
FP
1606;; file name match as above, but searched tag must match the file
1607;; name not including the directories if there are some.
1608(defun tag-file-name-match-p (tag)
cba9bdd2 1609 "Return non-nil if TAG matches file name, excluding directory part."
2aa9d1be
FP
1610 (and (looking-at ",[0-9\n]")
1611 (save-excursion (backward-char (1+ (length tag)))
1612 (looking-at "/"))))
cba9bdd2 1613
2aa9d1be
FP
1614;; this / to detect we are after a directory separator is ok for unix,
1615;; is there a variable that contains the regexp for directory separator
1616;; on whatever operating system ?
1617;; Looks like ms-win will lose here :).
1618
eb21e7ae 1619;; t if point is at a tag line that matches TAG exactly.
9708f7fc 1620;; point should be just after a string that matches TAG.
6218e8c6 1621(defun tag-exact-match-p (tag)
cba9bdd2
RS
1622 "Return non-nil if current tag line matches TAG exactly.
1623Point should be just after a string that matches TAG."
add3312f 1624 ;; The match is really exact if there is an explicit tag name.
63aeffd5 1625 (or (and (eq (char-after (point)) ?\001)
40ce9268 1626 (eq (char-after (- (point) (length tag) 1)) ?\177))
63aeffd5 1627 ;; We are not on the explicit tag name, but perhaps it follows.
eb21e7ae
RM
1628 (looking-at (concat "[^\177\n]*\177" (regexp-quote tag) "\001"))))
1629
d30ffe0e
FP
1630;; t if point is at a tag line that has an implicit name.
1631;; point should be just after a string that matches TAG.
1632(defun tag-implicit-name-match-p (tag)
cba9bdd2
RS
1633 "Return non-nil if current tag line has an implicit name.
1634Point should be just after a string that matches TAG."
d30ffe0e
FP
1635 ;; Look at the comment of the make_tag function in lib-src/etags.c for
1636 ;; a textual description of the four rules.
1637 (and (string-match "^[^ \t()=,;]+$" tag) ;rule #1
1638 (looking-at "[ \t()=,;]?\177") ;rules #2 and #4
1639 (save-excursion
1640 (backward-char (1+ (length tag)))
1641 (looking-at "[\n \t()=,;]")))) ;rule #3
1642
eb21e7ae
RM
1643;; t if point is at a tag line that matches TAG as a symbol.
1644;; point should be just after a string that matches TAG.
1645(defun tag-symbol-match-p (tag)
cba9bdd2
RS
1646 "Return non-nil if current tag line matches TAG as a symbol.
1647Point should be just after a string that matches TAG."
eb21e7ae
RM
1648 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1649 (save-excursion
1650 (backward-char (1+ (length tag)))
1651 (and (looking-at "\\Sw") (looking-at "\\S_")))))
9708f7fc
RM
1652
1653;; t if point is at a tag line that matches TAG as a word.
1654;; point should be just after a string that matches TAG.
1655(defun tag-word-match-p (tag)
cba9bdd2
RS
1656 "Return non-nil if current tag line matches TAG as a word.
1657Point should be just after a string that matches TAG."
9708f7fc 1658 (and (looking-at "\\b.*\177")
a0f09378 1659 (save-excursion (backward-char (length tag))
9708f7fc
RM
1660 (looking-at "\\b"))))
1661
1ea98518
SM
1662;; partial file name match, i.e. searched tag must match a substring
1663;; of the file name (potentially including a directory separator).
e02f48d7 1664(defun tag-partial-file-name-match-p (_tag)
cba9bdd2
RS
1665 "Return non-nil if current tag matches file name.
1666This is a substring match, and it can include directory separators.
1667Point should be just after a string that matches TAG."
1f21ea94 1668 (and (looking-at ".*,[0-9\n]")
fb7775eb
GM
1669 (save-excursion (beginning-of-line)
1670 (backward-char 2)
1671 (looking-at "\f\n"))))
5e882a6a 1672
9708f7fc 1673;; t if point is in a tag line with a tag containing TAG as a substring.
e02f48d7 1674(defun tag-any-match-p (_tag)
cba9bdd2 1675 "Return non-nil if current tag line contains TAG as a substring."
9708f7fc 1676 (looking-at ".*\177"))
ff1f0fa6 1677
9708f7fc
RM
1678;; t if point is at a tag line that matches RE as a regexp.
1679(defun tag-re-match-p (re)
cba9bdd2 1680 "Return non-nil if current tag line matches regexp RE."
9708f7fc
RM
1681 (save-excursion
1682 (beginning-of-line)
1683 (let ((bol (point)))
5ed619e0 1684 (and (search-forward "\177" (line-end-position) t)
9708f7fc
RM
1685 (re-search-backward re bol t)))))
1686\f
6613bf7a 1687(defcustom tags-loop-revert-buffers nil
fb7ada5f 1688 "Non-nil means tags-scanning loops should offer to reread changed files.
6613bf7a
CY
1689These loops normally read each file into Emacs, but when a file
1690is already visited, they use the existing buffer.
1691When this flag is non-nil, they offer to revert the existing buffer
1692in the case where the file has changed since you visited it."
d5792fb2
RS
1693 :type 'boolean
1694 :group 'etags)
1695
c086701a 1696;;;###autoload
9708f7fc
RM
1697(defun next-file (&optional initialize novisit)
1698 "Select next file among files in current tags table.
4f1388fd
RM
1699
1700A first argument of t (prefix arg, if interactive) initializes to the
1701beginning of the list of files in the tags table. If the argument is
1702neither nil nor t, it is evalled to initialize the list of files.
9708f7fc
RM
1703
1704Non-nil second argument NOVISIT means use a temporary buffer
1705 to save time and avoid uninteresting warnings.
1706
1707Value is nil if the file was already visited;
1708if the file was newly read in, the value is the filename."
c20032c4
RS
1709 ;; Make the interactive arg t if there was any prefix arg.
1710 (interactive (list (if current-prefix-arg t)))
4f1388fd
RM
1711 (cond ((not initialize)
1712 ;; Not the first run.
1713 )
1714 ((eq initialize t)
1715 ;; Initialize the list from the tags table.
1716 (save-excursion
1717 ;; Visit the tags table buffer to get its list of files.
1718 (visit-tags-table-buffer)
83287e5b
RM
1719 ;; Copy the list so we can setcdr below, and expand the file
1720 ;; names while we are at it, in this buffer's default directory.
1721 (setq next-file-list (mapcar 'expand-file-name (tags-table-files)))
5c9e49a9
RM
1722 ;; Iterate over all the tags table files, collecting
1723 ;; a complete list of referenced file names.
1724 (while (visit-tags-table-buffer t)
1725 ;; Find the tail of the working list and chain on the new
1726 ;; sublist for this tags table.
1727 (let ((tail next-file-list))
1728 (while (cdr tail)
1729 (setq tail (cdr tail)))
1730 ;; Use a copy so the next loop iteration will not modify the
1731 ;; list later returned by (tags-table-files).
2f14fde6 1732 (if tail
83287e5b
RM
1733 (setcdr tail (mapcar 'expand-file-name (tags-table-files)))
1734 (setq next-file-list (mapcar 'expand-file-name
1735 (tags-table-files))))))))
4f1388fd
RM
1736 (t
1737 ;; Initialize the list by evalling the argument.
1738 (setq next-file-list (eval initialize))))
7e7b42b2 1739 (unless next-file-list
5c9e49a9
RM
1740 (and novisit
1741 (get-buffer " *next-file*")
1742 (kill-buffer " *next-file*"))
71873e2b 1743 (user-error "All files processed"))
f042d383 1744 (let* ((next (car next-file-list))
d5792fb2
RS
1745 (buffer (get-file-buffer next))
1746 (new (not buffer)))
f042d383
RM
1747 ;; Advance the list before trying to find the file.
1748 ;; If we get an error finding the file, don't get stuck on it.
1749 (setq next-file-list (cdr next-file-list))
d5792fb2
RS
1750 ;; Optionally offer to revert buffers
1751 ;; if the files have changed on disk.
1752 (and buffer tags-loop-revert-buffers
1753 (not (verify-visited-file-modtime buffer))
6613bf7a
CY
1754 (y-or-n-p
1755 (format
1756 (if (buffer-modified-p buffer)
1757 "File %s changed on disk. Discard your edits? "
1758 "File %s changed on disk. Reread from disk? ")
1759 next))
d5792fb2 1760 (with-current-buffer buffer
6613bf7a 1761 (revert-buffer t t)))
9708f7fc 1762 (if (not (and new novisit))
7031be6d 1763 (find-file next novisit)
9708f7fc 1764 ;; Like find-file, but avoids random warning messages.
7031be6d 1765 (switch-to-buffer (get-buffer-create " *next-file*"))
9708f7fc
RM
1766 (kill-all-local-variables)
1767 (erase-buffer)
f042d383 1768 (setq new next)
9708f7fc 1769 (insert-file-contents new nil))
9708f7fc 1770 new))
ff1f0fa6 1771
9708f7fc
RM
1772(defvar tags-loop-operate nil
1773 "Form for `tags-loop-continue' to eval to change one file.")
1774
0f6b9c32 1775(defvar tags-loop-scan
71873e2b
SM
1776 '(user-error "%s"
1777 (substitute-command-keys
1778 "No \\[tags-search] or \\[tags-query-replace] in progress"))
9708f7fc
RM
1779 "Form for `tags-loop-continue' to eval to scan one file.
1780If it returns non-nil, this file needs processing by evalling
1781\`tags-loop-operate'. Otherwise, move on to the next file.")
ff1f0fa6 1782
ece6e35a
GM
1783(defun tags-loop-eval (form)
1784 "Evaluate FORM and return its result.
1785Bind `case-fold-search' during the evaluation, depending on the value of
1786`tags-case-fold-search'."
1787 (let ((case-fold-search (if (memq tags-case-fold-search '(t nil))
1788 tags-case-fold-search
1789 case-fold-search)))
1790 (eval form)))
ddc76b00 1791
ece6e35a 1792
c086701a 1793;;;###autoload
ff1f0fa6
JB
1794(defun tags-loop-continue (&optional first-time)
1795 "Continue last \\[tags-search] or \\[tags-query-replace] command.
4f1388fd
RM
1796Used noninteractively with non-nil argument to begin such a command (the
1797argument is passed to `next-file', which see).
78809db7
RM
1798
1799Two variables control the processing we do on each file: the value of
1800`tags-loop-scan' is a form to be executed on each file to see if it is
1801interesting (it returns non-nil if so) and `tags-loop-operate' is a form to
1802evaluate to operate on an interesting file. If the latter evaluates to
1803nil, we exit; otherwise we scan the next file."
ff1f0fa6 1804 (interactive)
9708f7fc 1805 (let (new
565c8985
RS
1806 ;; Non-nil means we have finished one file
1807 ;; and should not scan it again.
1808 file-finished
04528cda 1809 original-point
9708f7fc
RM
1810 (messaged nil))
1811 (while
1812 (progn
1813 ;; Scan files quickly for the first or next interesting one.
04528cda 1814 ;; This starts at point in the current buffer.
565c8985 1815 (while (or first-time file-finished
9708f7fc
RM
1816 (save-restriction
1817 (widen)
ece6e35a 1818 (not (tags-loop-eval tags-loop-scan))))
04528cda
GM
1819 ;; If nothing was found in the previous file, and
1820 ;; that file isn't in a temp buffer, restore point to
1821 ;; where it was.
1822 (when original-point
1823 (goto-char original-point))
1824
565c8985 1825 (setq file-finished nil)
9708f7fc 1826 (setq new (next-file first-time t))
04528cda 1827
9708f7fc
RM
1828 ;; If NEW is non-nil, we got a temp buffer,
1829 ;; and NEW is the file name.
04528cda
GM
1830 (when (or messaged
1831 (and (not first-time)
1832 (> baud-rate search-slow-speed)
1833 (setq messaged t)))
1834 (message "Scanning file %s..." (or new buffer-file-name)))
1835
9708f7fc 1836 (setq first-time nil)
04528cda 1837 (setq original-point (if new nil (point)))
9708f7fc
RM
1838 (goto-char (point-min)))
1839
1840 ;; If we visited it in a temp buffer, visit it now for real.
1841 (if new
1842 (let ((pos (point)))
1843 (erase-buffer)
1844 (set-buffer (find-file-noselect new))
78809db7 1845 (setq new nil) ;No longer in a temp buffer.
9708f7fc 1846 (widen)
04528cda
GM
1847 (goto-char pos))
1848 (push-mark original-point t))
9708f7fc
RM
1849
1850 (switch-to-buffer (current-buffer))
1851
1852 ;; Now operate on the file.
1853 ;; If value is non-nil, continue to scan the next file.
ece6e35a 1854 (tags-loop-eval tags-loop-operate))
565c8985 1855 (setq file-finished t))
9708f7fc
RM
1856 (and messaged
1857 (null tags-loop-operate)
1858 (message "Scanning file %s...found" buffer-file-name))))
9708f7fc 1859;;;###autoload (define-key esc-map "," 'tags-loop-continue)
ff1f0fa6 1860
c086701a 1861;;;###autoload
4f1388fd 1862(defun tags-search (regexp &optional file-list-form)
9708f7fc 1863 "Search through all files listed in tags table for match for REGEXP.
ff1f0fa6
JB
1864Stops when a match is found.
1865To continue searching for next match, use command \\[tags-loop-continue].
1866
a762e966 1867If FILE-LIST-FORM is non-nil, it should be a form that, when
343c3b5a
LMI
1868evaluated, will return a list of file names. The search will be
1869restricted to these files.
1870
acc650b8 1871Also see the documentation of the `tags-file-name' variable."
ff1f0fa6
JB
1872 (interactive "sTags search (regexp): ")
1873 (if (and (equal regexp "")
9708f7fc 1874 (eq (car tags-loop-scan) 're-search-forward)
b6176f64 1875 (null tags-loop-operate))
9708f7fc 1876 ;; Continue last tags-search as if by M-,.
ff1f0fa6 1877 (tags-loop-continue nil)
06adf6b1 1878 (setq tags-loop-scan `(re-search-forward ',regexp nil t)
9708f7fc 1879 tags-loop-operate nil)
4f1388fd 1880 (tags-loop-continue (or file-list-form t))))
ff1f0fa6 1881
c086701a 1882;;;###autoload
c575e658 1883(defun tags-query-replace (from to &optional delimited file-list-form)
056f8a11 1884 "Do `query-replace-regexp' of FROM with TO on all files listed in tags table.
ff1f0fa6 1885Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
056f8a11 1886If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
ff1f0fa6 1887with the command \\[tags-loop-continue].
405b8be3
EZ
1888Fourth arg FILE-LIST-FORM non-nil means initialize the replacement loop.
1889Fifth and sixth arguments START and END are accepted, for compatibility
1890with `query-replace-regexp', and ignored.
ff1f0fa6 1891
c575e658
RS
1892If FILE-LIST-FORM is non-nil, it is a form to evaluate to
1893produce the list of files to search.
1894
1895See also the documentation of the variable `tags-file-name'."
17b9c33c 1896 (interactive (query-replace-read-args "Tags query replace (regexp)" t t))
06adf6b1
SM
1897 (setq tags-loop-scan `(let ,(unless (equal from (downcase from))
1898 '((case-fold-search nil)))
1899 (if (re-search-forward ',from nil t)
1900 ;; When we find a match, move back
1901 ;; to the beginning of it so perform-replace
1902 ;; will see it.
1903 (goto-char (match-beginning 0))))
64b98d12
JL
1904 tags-loop-operate `(perform-replace ',from ',to t t ',delimited
1905 nil multi-query-replace-map))
4f1388fd 1906 (tags-loop-continue (or file-list-form t)))
9708f7fc 1907\f
cba9bdd2 1908(defun tags-complete-tags-table-file (string predicate what) ; Doc string?
83287e5b
RM
1909 (save-excursion
1910 ;; If we need to ask for the tag table, allow that.
1911 (let ((enable-recursive-minibuffers t))
1912 (visit-tags-table-buffer))
1913 (if (eq what t)
725349c8
SM
1914 (all-completions string (tags-table-files) predicate)
1915 (try-completion string (tags-table-files) predicate))))
83287e5b 1916
c086701a 1917;;;###autoload
e02f48d7 1918(defun list-tags (file &optional _next-match)
83287e5b
RM
1919 "Display list of tags in file FILE.
1920This searches only the first table in the list, and no included tables.
1921FILE should be as it appeared in the `etags' command, usually without a
1922directory specification."
1923 (interactive (list (completing-read "List tags in file: "
1924 'tags-complete-tags-table-file
1925 nil t nil)))
1926 (with-output-to-temp-buffer "*Tags List*"
7e7b42b2
GM
1927 (princ "Tags in file `")
1928 (tags-with-face 'highlight (princ file))
1929 (princ "':\n\n")
83287e5b
RM
1930 (save-excursion
1931 (let ((first-time t)
1932 (gotany nil))
1933 (while (visit-tags-table-buffer (not first-time))
1934 (setq first-time nil)
1935 (if (funcall list-tags-function file)
1936 (setq gotany t)))
1937 (or gotany
71873e2b 1938 (user-error "File %s not in current tags tables" file)))))
7e7b42b2 1939 (with-current-buffer "*Tags List*"
a6e7bdf1 1940 (require 'apropos)
6ef254ec
RS
1941 (with-no-warnings
1942 (apropos-mode))
79e01623 1943 (setq buffer-read-only t)))
ff1f0fa6 1944
c086701a 1945;;;###autoload
9708f7fc
RM
1946(defun tags-apropos (regexp)
1947 "Display list of all tags in tags table REGEXP matches."
1948 (interactive "sTags apropos (regexp): ")
ff1f0fa6 1949 (with-output-to-temp-buffer "*Tags List*"
7e7b42b2
GM
1950 (princ "Click mouse-2 to follow tags.\n\nTags matching regexp `")
1951 (tags-with-face 'highlight (princ regexp))
1952 (princ "':\n\n")
ff1f0fa6 1953 (save-excursion
47f3c459
RM
1954 (let ((first-time t))
1955 (while (visit-tags-table-buffer (not first-time))
1956 (setq first-time nil)
7e7b42b2
GM
1957 (funcall tags-apropos-function regexp))))
1958 (etags-tags-apropos-additional regexp))
1959 (with-current-buffer "*Tags List*"
7caf6803 1960 (eval-and-compile (require 'apropos))
89228b63
JB
1961 (apropos-mode)
1962 ;; apropos-mode is derived from fundamental-mode and it kills
1963 ;; all local variables.
1964 (setq buffer-read-only t)))
9708f7fc 1965\f
1ea98518 1966;; XXX Kludge interface.
aa228418 1967
36278af3 1968(define-button-type 'tags-select-tags-table
29878150 1969 'action 'select-tags-table-select
26581f0e 1970 'follow-link t
36278af3
MY
1971 'help-echo "RET, t or mouse-2: select tags table")
1972
9708f7fc 1973;; XXX If a file is in multiple tables, selection may get the wrong one.
29add8b9 1974;;;###autoload
9708f7fc
RM
1975(defun select-tags-table ()
1976 "Select a tags table file from a menu of those you have already used.
168e43e7 1977The list of tags tables to select from is stored in `tags-table-set-list';
9708f7fc
RM
1978see the doc of that variable if you want to add names to the list."
1979 (interactive)
1980 (pop-to-buffer "*Tags Table List*")
9099b373
GM
1981 (setq buffer-read-only nil
1982 buffer-undo-list t)
9708f7fc 1983 (erase-buffer)
9708f7fc 1984 (let ((set-list tags-table-set-list)
36278af3
MY
1985 (desired-point nil)
1986 b)
7e7b42b2 1987 (when tags-table-list
29878150
SM
1988 (setq desired-point (point-marker))
1989 (setq b (point))
1990 (princ (mapcar 'abbreviate-file-name tags-table-list) (current-buffer))
1991 (make-text-button b (point) 'type 'tags-select-tags-table
1992 'etags-table (car tags-table-list))
7e7b42b2 1993 (insert "\n"))
9708f7fc 1994 (while set-list
7e7b42b2 1995 (unless (eq (car set-list) tags-table-list)
36278af3 1996 (setq b (point))
29878150
SM
1997 (princ (mapcar 'abbreviate-file-name (car set-list)) (current-buffer))
1998 (make-text-button b (point) 'type 'tags-select-tags-table
1999 'etags-table (car (car set-list)))
9708f7fc
RM
2000 (insert "\n"))
2001 (setq set-list (cdr set-list)))
7e7b42b2 2002 (when tags-file-name
29878150
SM
2003 (or desired-point
2004 (setq desired-point (point-marker)))
2005 (setq b (point))
2006 (insert (abbreviate-file-name tags-file-name))
2007 (make-text-button b (point) 'type 'tags-select-tags-table
2008 'etags-table tags-file-name)
7e7b42b2 2009 (insert "\n"))
9708f7fc 2010 (setq set-list (delete tags-file-name
9993b561 2011 (apply 'nconc (cons (copy-sequence tags-table-list)
9708f7fc
RM
2012 (mapcar 'copy-sequence
2013 tags-table-set-list)))))
2014 (while set-list
36278af3 2015 (setq b (point))
29878150
SM
2016 (insert (abbreviate-file-name (car set-list)))
2017 (make-text-button b (point) 'type 'tags-select-tags-table
2018 'etags-table (car set-list))
9708f7fc
RM
2019 (insert "\n")
2020 (setq set-list (delete (car set-list) set-list)))
a1906d51 2021 (goto-char (point-min))
9708f7fc
RM
2022 (insert-before-markers
2023 "Type `t' to select a tags table or set of tags tables:\n\n")
2024 (if desired-point
2025 (goto-char desired-point))
2026 (set-window-start (selected-window) 1 t))
2027 (set-buffer-modified-p nil)
ef90db45
RS
2028 (select-tags-table-mode))
2029
cba9bdd2 2030(defvar select-tags-table-mode-map ; Doc string?
29878150
SM
2031 (let ((map (make-sparse-keymap)))
2032 (set-keymap-parent map button-buffer-map)
36278af3
MY
2033 (define-key map "t" 'push-button)
2034 (define-key map " " 'next-line)
2035 (define-key map "\^?" 'previous-line)
2036 (define-key map "n" 'next-line)
2037 (define-key map "p" 'previous-line)
2038 (define-key map "q" 'select-tags-table-quit)
2039 map))
ef90db45 2040
175069ef
SM
2041(define-derived-mode select-tags-table-mode special-mode "Select Tags Table"
2042 "Major mode for choosing a current tags table among those already loaded."
29878150
SM
2043 (setq buffer-read-only t))
2044
2045(defun select-tags-table-select (button)
9708f7fc 2046 "Select the tags table named on this line."
29878150
SM
2047 (interactive (list (or (button-at (line-beginning-position))
2048 (error "No tags table on current line"))))
2049 (let ((name (button-get button 'etags-table)))
9708f7fc
RM
2050 (visit-tags-table name)
2051 (select-tags-table-quit)
2052 (message "Tags table now %s" name)))
49116ac0 2053
9708f7fc
RM
2054(defun select-tags-table-quit ()
2055 "Kill the buffer and delete the selected window."
2056 (interactive)
7591cf05 2057 (quit-window t (selected-window)))
9708f7fc 2058\f
3a07ffce
CY
2059;;;###autoload
2060(defun complete-tag ()
2061 "Perform tags completion on the text around point.
2062Completes to the set of names listed in the current tags table.
2063The string to complete is chosen in the same way as the default
2064for \\[find-tag] (which see)."
2065 (interactive)
2066 (or tags-table-list
2067 tags-file-name
71873e2b
SM
2068 (user-error "%s"
2069 (substitute-command-keys
2070 "No tags table loaded; try \\[visit-tags-table]")))
8c0bf8b3
SM
2071 (let ((comp-data (tags-completion-at-point-function)))
2072 (if (null comp-data)
71873e2b 2073 (user-error "Nothing to complete")
8f1383f7
GM
2074 (completion-in-region (car comp-data) (cadr comp-data)
2075 (nth 2 comp-data)
2076 (plist-get (nthcdr 3 comp-data) :predicate)))))
9708f7fc
RM
2077\f
2078(provide 'etags)
e5167999
ER
2079
2080;;; etags.el ends here