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