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