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