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