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