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