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