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