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