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