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