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