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