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