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