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