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