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