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