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