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