2005-09-24 Emilio C. Lopes <eclig@gmx.net>
[emacs.git] / lisp / progmodes / etags.el
blobea87dce591f1f95dd2e4ee2b609f51a227472c6b
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
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 2, or (at your option)
16 ;; 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; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
28 ;;; Commentary:
30 ;;; Code:
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 iff current buffer contains valid tags file.")
261 ;; Initialize the tags table in the current buffer.
262 ;; Returns non-nil iff it is a valid tags table. On
263 ;; non-nil return, the tags table state variable are
264 ;; made buffer-local and initialized to nil.
265 (defun initialize-new-tags-table ()
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 visit-tags-table (file &optional local)
278 "Tell tags commands to use tags table file FILE.
279 FILE should be the name of a file created with the `etags' program.
280 A directory name is ok too; it means file TAGS in that directory.
282 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
283 With a prefix arg, set the buffer-local value instead.
284 When you find a tag with \\[find-tag], the buffer it finds the tag
285 in is given a local value of this variable which is the name of the tags
286 file the tag was in."
287 (interactive (list (read-file-name "Visit tags table (default TAGS): "
288 default-directory
289 (expand-file-name "TAGS"
290 default-directory)
292 current-prefix-arg))
293 (or (stringp file) (signal 'wrong-type-argument (list 'stringp file)))
294 ;; Bind tags-file-name so we can control below whether the local or
295 ;; global value gets set. Calling visit-tags-table-buffer will
296 ;; initialize a buffer for the file and set tags-file-name to the
297 ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
298 ;; initialize a buffer for FILE and set tags-file-name to the
299 ;; fully-expanded name.
300 (let ((tags-file-name file))
301 (save-excursion
302 (or (visit-tags-table-buffer file)
303 (signal 'file-error (list "Visiting tags table"
304 "file does not exist"
305 file)))
306 ;; Set FILE to the expanded name.
307 (setq file tags-file-name)))
308 (if local
309 ;; Set the local value of tags-file-name.
310 (set (make-local-variable 'tags-file-name) file)
311 ;; Set the global value of tags-file-name.
312 (setq-default tags-file-name file)))
314 (defun tags-table-check-computed-list ()
315 "Compute `tags-table-computed-list' from `tags-table-list' if necessary."
316 (let ((expanded-list (mapcar 'tags-expand-table-name tags-table-list)))
317 (or (equal tags-table-computed-list-for expanded-list)
318 ;; The list (or default-directory) has changed since last computed.
319 (let* ((compute-for (mapcar 'copy-sequence expanded-list))
320 (tables (copy-sequence compute-for)) ;Mutated in the loop.
321 (computed nil)
322 table-buffer)
324 (while tables
325 (setq computed (cons (car tables) computed)
326 table-buffer (get-file-buffer (car tables)))
327 (if (and table-buffer
328 ;; There is a buffer visiting the file. Now make sure
329 ;; it is initialized as a tag table buffer.
330 (save-excursion
331 (tags-verify-table (buffer-file-name table-buffer))))
332 (save-excursion
333 (set-buffer table-buffer)
334 (if (tags-included-tables)
335 ;; Insert the included tables into the list we
336 ;; are processing.
337 (setcdr tables (nconc (mapcar 'tags-expand-table-name
338 (tags-included-tables))
339 (cdr tables)))))
340 ;; This table is not in core yet. Insert a placeholder
341 ;; saying we must read it into core to check for included
342 ;; tables before searching the next table in the list.
343 (setq computed (cons t computed)))
344 (setq tables (cdr tables)))
346 ;; Record the tags-table-list value (and the context of the
347 ;; current directory) we computed from.
348 (setq tags-table-computed-list-for compute-for
349 tags-table-computed-list (nreverse computed))))))
351 ;; Extend `tags-table-computed-list' to remove the first `t' placeholder.
352 ;; An element of the list that is `t' is a placeholder indicating that the
353 ;; preceding element is a table that has not been read into core and might
354 ;; contain included tables to search. On return, the first placeholder
355 ;; element will be gone and the element before it read into core and its
356 ;; included tables inserted into the list.
357 (defun tags-table-extend-computed-list ()
358 (let ((list tags-table-computed-list))
359 (while (not (eq (nth 1 list) t))
360 (setq list (cdr list)))
361 (save-excursion
362 (if (tags-verify-table (car list))
363 ;; We are now in the buffer visiting (car LIST). Extract its
364 ;; list of included tables and insert it into the computed list.
365 (let ((tables (tags-included-tables))
366 (computed nil)
367 table-buffer)
368 (while tables
369 (setq computed (cons (car tables) computed)
370 table-buffer (get-file-buffer (car tables)))
371 (if table-buffer
372 (save-excursion
373 (set-buffer table-buffer)
374 (if (tags-included-tables)
375 ;; Insert the included tables into the list we
376 ;; are processing.
377 (setcdr tables (append (tags-included-tables)
378 tables))))
379 ;; This table is not in core yet. Insert a placeholder
380 ;; saying we must read it into core to check for included
381 ;; tables before searching the next table in the list.
382 (setq computed (cons t computed)))
383 (setq tables (cdr tables)))
384 (setq computed (nreverse computed))
385 ;; COMPUTED now contains the list of included tables (and
386 ;; tables included by them, etc.). Now splice this into the
387 ;; current list.
388 (setcdr list (nconc computed (cdr (cdr list)))))
389 ;; It was not a valid table, so just remove the following placeholder.
390 (setcdr list (cdr (cdr list)))))))
392 ;; Expand tags table name FILE into a complete file name.
393 (defun tags-expand-table-name (file)
394 (setq file (expand-file-name file))
395 (if (file-directory-p file)
396 (expand-file-name "TAGS" file)
397 file))
399 ;; Like member, but comparison is done after tags-expand-table-name on both
400 ;; sides and elements of LIST that are t are skipped.
401 (defun tags-table-list-member (file list)
402 (setq file (tags-expand-table-name file))
403 (while (and list
404 (or (eq (car list) t)
405 (not (string= file (tags-expand-table-name (car list))))))
406 (setq list (cdr list)))
407 list)
409 (defun tags-verify-table (file)
410 "Read FILE into a buffer and verify that it is a valid tags table.
411 Sets the current buffer to one visiting FILE (if it exists).
412 Returns non-nil iff it is a valid table."
413 (if (get-file-buffer file)
414 ;; The file is already in a buffer. Check for the visited file
415 ;; having changed since we last used it.
416 (let (win)
417 (set-buffer (get-file-buffer file))
418 (setq win (or verify-tags-table-function (initialize-new-tags-table)))
419 (if (or (verify-visited-file-modtime (current-buffer))
420 ;; Decide whether to revert the file.
421 ;; revert-without-query can say to revert
422 ;; or the user can say to revert.
423 (not (or (let ((tail revert-without-query)
424 (found nil))
425 (while tail
426 (if (string-match (car tail) buffer-file-name)
427 (setq found t))
428 (setq tail (cdr tail)))
429 found)
430 tags-revert-without-query
431 (yes-or-no-p
432 (format "Tags file %s has changed, read new contents? "
433 file)))))
434 (and verify-tags-table-function
435 (funcall verify-tags-table-function))
436 (revert-buffer t t)
437 (initialize-new-tags-table)))
438 (and (file-exists-p file)
439 (progn
440 (set-buffer (find-file-noselect file))
441 (or (string= file buffer-file-name)
442 ;; find-file-noselect has changed the file name.
443 ;; Propagate the change to tags-file-name and tags-table-list.
444 (let ((tail (member file tags-table-list)))
445 (if tail
446 (setcar tail buffer-file-name))
447 (if (eq file tags-file-name)
448 (setq tags-file-name buffer-file-name))))
449 (initialize-new-tags-table)))))
451 ;; Subroutine of visit-tags-table-buffer. Search the current tags tables
452 ;; for one that has tags for THIS-FILE (or that includes a table that
453 ;; does). Return the name of the first table table listing THIS-FILE; if
454 ;; the table is one included by another table, it is the master table that
455 ;; we return. If CORE-ONLY is non-nil, check only tags tables that are
456 ;; already in buffers--don't visit any new files.
457 (defun tags-table-including (this-file core-only)
458 (let ((tables tags-table-computed-list)
459 (found nil))
460 ;; Loop over the list, looking for a table containing tags for THIS-FILE.
461 (while (and (not found)
462 tables)
464 (if core-only
465 ;; Skip tables not in core.
466 (while (eq (nth 1 tables) t)
467 (setq tables (cdr (cdr tables))))
468 (if (eq (nth 1 tables) t)
469 ;; This table has not been read into core yet. Read it in now.
470 (tags-table-extend-computed-list)))
472 (if tables
473 ;; Select the tags table buffer and get the file list up to date.
474 (let ((tags-file-name (car tables)))
475 (visit-tags-table-buffer 'same)
476 (if (member this-file (mapcar 'expand-file-name
477 (tags-table-files)))
478 ;; Found it.
479 (setq found tables))))
480 (setq tables (cdr tables)))
481 (if found
482 ;; Now determine if the table we found was one included by another
483 ;; table, not explicitly listed. We do this by checking each
484 ;; element of the computed list to see if it appears in the user's
485 ;; explicit list; the last element we will check is FOUND itself.
486 ;; Then we return the last one which did in fact appear in
487 ;; tags-table-list.
488 (let ((could-be nil)
489 (elt tags-table-computed-list))
490 (while (not (eq elt (cdr found)))
491 (if (tags-table-list-member (car elt) tags-table-list)
492 ;; This table appears in the user's list, so it could be
493 ;; the one which includes the table we found.
494 (setq could-be (car elt)))
495 (setq elt (cdr elt))
496 (if (eq t (car elt))
497 (setq elt (cdr elt))))
498 ;; The last element we found in the computed list before FOUND
499 ;; that appears in the user's list will be the table that
500 ;; included the one we found.
501 could-be))))
503 ;; Subroutine of visit-tags-table-buffer. Move tags-table-list-pointer
504 ;; along and set tags-file-name. Returns nil when out of tables.
505 (defun tags-next-table ()
506 ;; If there is a placeholder element next, compute the list to replace it.
507 (while (eq (nth 1 tags-table-list-pointer) t)
508 (tags-table-extend-computed-list))
510 ;; Go to the next table in the list.
511 (setq tags-table-list-pointer (cdr tags-table-list-pointer))
512 (or tags-table-list-pointer
513 ;; Wrap around.
514 (setq tags-table-list-pointer tags-table-computed-list))
516 (if (eq tags-table-list-pointer tags-table-list-started-at)
517 ;; We have come full circle. No more tables.
518 (setq tags-table-list-pointer nil)
519 ;; Set tags-file-name to the name from the list. It is already expanded.
520 (setq tags-file-name (car tags-table-list-pointer))))
522 ;;;###autoload
523 (defun visit-tags-table-buffer (&optional cont)
524 "Select the buffer containing the current tags table.
525 If optional arg is a string, visit that file as a tags table.
526 If optional arg is t, visit the next table in `tags-table-list'.
527 If optional arg is the atom `same', don't look for a new table;
528 just select the buffer visiting `tags-file-name'.
529 If arg is nil or absent, choose a first buffer from information in
530 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
531 Returns t if it visits a tags table, or nil if there are no more in the list."
533 ;; Set tags-file-name to the tags table file we want to visit.
534 (cond ((eq cont 'same)
535 ;; Use the ambient value of tags-file-name.
536 (or tags-file-name
537 (error "%s"
538 (substitute-command-keys
539 (concat "No tags table in use; "
540 "use \\[visit-tags-table] to select one")))))
542 ((eq t cont)
543 ;; Find the next table.
544 (if (tags-next-table)
545 ;; Skip over nonexistent files.
546 (while (and (not (or (get-file-buffer tags-file-name)
547 (file-exists-p tags-file-name)))
548 (tags-next-table)))))
551 ;; Pick a table out of our hat.
552 (tags-table-check-computed-list) ;Get it up to date, we might use it.
553 (setq tags-file-name
555 ;; If passed a string, use that.
556 (if (stringp cont)
557 (prog1 cont
558 (setq cont nil)))
559 ;; First, try a local variable.
560 (cdr (assq 'tags-file-name (buffer-local-variables)))
561 ;; Second, try a user-specified function to guess.
562 (and default-tags-table-function
563 (funcall default-tags-table-function))
564 ;; Third, look for a tags table that contains tags for the
565 ;; current buffer's file. If one is found, the lists will
566 ;; be frobnicated, and CONT will be set non-nil so we don't
567 ;; do it below.
568 (and buffer-file-name
570 ;; First check only tables already in buffers.
571 (tags-table-including buffer-file-name t)
572 ;; Since that didn't find any, now do the
573 ;; expensive version: reading new files.
574 (tags-table-including buffer-file-name nil)))
575 ;; Fourth, use the user variable tags-file-name, if it is
576 ;; not already in the current list.
577 (and tags-file-name
578 (not (tags-table-list-member tags-file-name
579 tags-table-computed-list))
580 tags-file-name)
581 ;; Fifth, use the user variable giving the table list.
582 ;; Find the first element of the list that actually exists.
583 (let ((list tags-table-list)
584 file)
585 (while (and list
586 (setq file (tags-expand-table-name (car list)))
587 (not (get-file-buffer file))
588 (not (file-exists-p file)))
589 (setq list (cdr list)))
590 (car list))
591 ;; Finally, prompt the user for a file name.
592 (expand-file-name
593 (read-file-name "Visit tags table (default TAGS): "
594 default-directory
595 "TAGS"
596 t))))))
598 ;; Expand the table name into a full file name.
599 (setq tags-file-name (tags-expand-table-name tags-file-name))
601 (unless (and (eq cont t) (null tags-table-list-pointer))
602 ;; Verify that tags-file-name names a valid tags table.
603 ;; Bind another variable with the value of tags-file-name
604 ;; before we switch buffers, in case tags-file-name is buffer-local.
605 (let ((curbuf (current-buffer))
606 (local-tags-file-name tags-file-name))
607 (if (tags-verify-table local-tags-file-name)
609 ;; We have a valid tags table.
610 (progn
611 ;; Bury the tags table buffer so it
612 ;; doesn't get in the user's way.
613 (bury-buffer (current-buffer))
615 ;; If this was a new table selection (CONT is nil), make
616 ;; sure tags-table-list includes the chosen table, and
617 ;; update the list pointer variables.
618 (or cont
619 ;; Look in the list for the table we chose.
620 (let ((found (tags-table-list-member
621 local-tags-file-name
622 tags-table-computed-list)))
623 (if found
624 ;; There it is. Just switch to it.
625 (setq tags-table-list-pointer found
626 tags-table-list-started-at found)
628 ;; The table is not in the current set.
629 ;; Try to find it in another previously used set.
630 (let ((sets tags-table-set-list))
631 (while (and sets
632 (not (tags-table-list-member
633 local-tags-file-name
634 (car sets))))
635 (setq sets (cdr sets)))
636 (if sets
637 ;; Found in some other set. Switch to that set.
638 (progn
639 (or (memq tags-table-list tags-table-set-list)
640 ;; Save the current list.
641 (setq tags-table-set-list
642 (cons tags-table-list
643 tags-table-set-list)))
644 (setq tags-table-list (car sets)))
646 ;; Not found in any existing set.
647 (if (and tags-table-list
648 (or (eq t tags-add-tables)
649 (and tags-add-tables
650 (y-or-n-p
651 (concat "Keep current list of "
652 "tags tables also? ")))))
653 ;; Add it to the current list.
654 (setq tags-table-list (cons local-tags-file-name
655 tags-table-list))
657 ;; Make a fresh list, and store the old one.
658 (message "Starting a new list of tags tables")
659 (or (null tags-table-list)
660 (memq tags-table-list tags-table-set-list)
661 (setq tags-table-set-list
662 (cons tags-table-list
663 tags-table-set-list)))
664 ;; Clear out buffers holding old tables.
665 (dolist (table tags-table-list)
666 ;; The list can contain items `t'.
667 (if (stringp table)
668 (let ((buffer (find-buffer-visiting table)))
669 (if buffer
670 (kill-buffer buffer)))))
671 (setq tags-table-list (list local-tags-file-name))))
673 ;; Recompute tags-table-computed-list.
674 (tags-table-check-computed-list)
675 ;; Set the tags table list state variables to start
676 ;; over from tags-table-computed-list.
677 (setq tags-table-list-started-at tags-table-computed-list
678 tags-table-list-pointer
679 tags-table-computed-list)))))
681 ;; Return of t says the tags table is valid.
684 ;; The buffer was not valid. Don't use it again.
685 (set-buffer curbuf)
686 (kill-local-variable 'tags-file-name)
687 (if (eq local-tags-file-name tags-file-name)
688 (setq tags-file-name nil))
689 (error "File %s is not a valid tags table" local-tags-file-name)))))
691 (defun tags-reset-tags-tables ()
692 "Reset tags state to cancel effect of any previous \\[visit-tags-table] or \\[find-tag]."
693 (interactive)
694 ;; Clear out the markers we are throwing away.
695 (let ((i 0))
696 (while (< i find-tag-marker-ring-length)
697 (if (aref (cddr tags-location-ring) i)
698 (set-marker (aref (cddr tags-location-ring) i) nil))
699 (if (aref (cddr find-tag-marker-ring) i)
700 (set-marker (aref (cddr find-tag-marker-ring) i) nil))
701 (setq i (1+ i))))
702 (setq tags-file-name nil
703 tags-location-ring (make-ring find-tag-marker-ring-length)
704 find-tag-marker-ring (make-ring find-tag-marker-ring-length)
705 tags-table-list nil
706 tags-table-computed-list nil
707 tags-table-computed-list-for nil
708 tags-table-list-pointer nil
709 tags-table-list-started-at nil
710 tags-table-set-list nil))
712 (defun file-of-tag (&optional relative)
713 "Return the file name of the file whose tags point is within.
714 Assumes the tags table is the current buffer.
715 If RELATIVE is non-nil, file name returned is relative to tags
716 table file's directory. If RELATIVE is nil, file name returned
717 is complete."
718 (funcall file-of-tag-function relative))
720 ;;;###autoload
721 (defun tags-table-files ()
722 "Return a list of files in the current tags table.
723 Assumes the tags table is the current buffer. The file names are returned
724 as they appeared in the `etags' command that created the table, usually
725 without directory names."
726 (or tags-table-files
727 (setq tags-table-files
728 (funcall tags-table-files-function))))
730 (defun tags-included-tables ()
731 "Return a list of tags tables included by the current table.
732 Assumes the tags table is the current buffer."
733 (or tags-included-tables
734 (setq tags-included-tables (funcall tags-included-tables-function))))
736 ;; Build tags-completion-table on demand. The single current tags table
737 ;; and its included tags tables (and their included tables, etc.) have
738 ;; their tags included in the completion table.
739 (defun tags-completion-table ()
740 (or tags-completion-table
741 (condition-case ()
742 (prog2
743 (message "Making tags completion table for %s..." buffer-file-name)
744 (let ((included (tags-included-tables))
745 (table (funcall tags-completion-table-function)))
746 (save-excursion
747 ;; Iterate over the list of included tables, and combine each
748 ;; included table's completion obarray to the parent obarray.
749 (while included
750 ;; Visit the buffer.
751 (let ((tags-file-name (car included)))
752 (visit-tags-table-buffer 'same))
753 ;; Recurse in that buffer to compute its completion table.
754 (if (tags-completion-table)
755 ;; Combine the tables.
756 (mapatoms (lambda (sym) (intern (symbol-name sym) table))
757 tags-completion-table))
758 (setq included (cdr included))))
759 (setq tags-completion-table table))
760 (message "Making tags completion table for %s...done"
761 buffer-file-name))
762 (quit (message "Tags completion table construction aborted.")
763 (setq tags-completion-table nil)))))
765 ;; Completion function for tags. Does normal try-completion,
766 ;; but builds tags-completion-table on demand.
767 (defun tags-complete-tag (string predicate what)
768 (save-excursion
769 ;; If we need to ask for the tag table, allow that.
770 (let ((enable-recursive-minibuffers t))
771 (visit-tags-table-buffer))
772 (if (eq what t)
773 (all-completions string (tags-completion-table) predicate)
774 (try-completion string (tags-completion-table) predicate))))
776 ;; Read a tag name from the minibuffer with defaulting and completion.
777 (defun find-tag-tag (string)
778 (let* ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
779 tags-case-fold-search
780 case-fold-search))
781 (default (funcall (or find-tag-default-function
782 (get major-mode 'find-tag-default-function)
783 'find-tag-default)))
784 (spec (completing-read (if default
785 (format "%s (default %s): "
786 (substring string 0 (string-match "[ :]+\\'" string))
787 default)
788 string)
789 'tags-complete-tag
790 nil nil nil nil default)))
791 (if (equal spec "")
792 (or default (error "There is no default tag"))
793 spec)))
795 (defvar last-tag nil
796 "Last tag found by \\[find-tag].")
798 ;; Get interactive args for find-tag{-noselect,-other-window,-regexp}.
799 (defun find-tag-interactive (prompt &optional no-default)
800 (if (and current-prefix-arg last-tag)
801 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
804 (list (if no-default
805 (read-string prompt)
806 (find-tag-tag prompt)))))
808 (defvar find-tag-history nil)
810 ;; Dynamic bondage:
811 (eval-when-compile
812 (defvar etags-case-fold-search)
813 (defvar etags-syntax-table))
815 ;;;###autoload
816 (defun find-tag-noselect (tagname &optional next-p regexp-p)
817 "Find tag (in current tags table) whose name contains TAGNAME.
818 Returns the buffer containing the tag's definition and moves its point there,
819 but does not select the buffer.
820 The default for TAGNAME is the expression in the buffer near point.
822 If second arg NEXT-P is t (interactively, with prefix arg), search for
823 another tag that matches the last tagname or regexp used. When there are
824 multiple matches for a tag, more exact matches are found first. If NEXT-P
825 is the atom `-' (interactively, with prefix arg that is a negative number
826 or just \\[negative-argument]), pop back to the previous tag gone to.
828 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
830 A marker representing the point when this command is invoked is pushed
831 onto a ring and may be popped back to with \\[pop-tag-mark].
832 Contrast this with the ring of marks gone to by the command.
834 See documentation of variable `tags-file-name'."
835 (interactive (find-tag-interactive "Find tag: "))
837 (setq find-tag-history (cons tagname find-tag-history))
838 ;; Save the current buffer's value of `find-tag-hook' before
839 ;; selecting the tags table buffer. For the same reason, save value
840 ;; of `tags-file-name' in case it has a buffer-local value.
841 (let ((local-find-tag-hook find-tag-hook))
842 (if (eq '- next-p)
843 ;; Pop back to a previous location.
844 (if (ring-empty-p tags-location-ring)
845 (error "No previous tag locations")
846 (let ((marker (ring-remove tags-location-ring 0)))
847 (prog1
848 ;; Move to the saved location.
849 (set-buffer (or (marker-buffer marker)
850 (error "The marked buffer has been deleted")))
851 (goto-char (marker-position marker))
852 ;; Kill that marker so it doesn't slow down editing.
853 (set-marker marker nil nil)
854 ;; Run the user's hook. Do we really want to do this for pop?
855 (run-hooks 'local-find-tag-hook))))
856 ;; Record whence we came.
857 (ring-insert find-tag-marker-ring (point-marker))
858 (if (and next-p last-tag)
859 ;; Find the same table we last used.
860 (visit-tags-table-buffer 'same)
861 ;; Pick a table to use.
862 (visit-tags-table-buffer)
863 ;; Record TAGNAME for a future call with NEXT-P non-nil.
864 (setq last-tag tagname))
865 ;; Record the location so we can pop back to it later.
866 (let ((marker (make-marker)))
867 (save-excursion
868 (set-buffer
869 ;; find-tag-in-order does the real work.
870 (find-tag-in-order
871 (if (and next-p last-tag) last-tag tagname)
872 (if regexp-p
873 find-tag-regexp-search-function
874 find-tag-search-function)
875 (if regexp-p
876 find-tag-regexp-tag-order
877 find-tag-tag-order)
878 (if regexp-p
879 find-tag-regexp-next-line-after-failure-p
880 find-tag-next-line-after-failure-p)
881 (if regexp-p "matching" "containing")
882 (or (not next-p) (not last-tag))))
883 (set-marker marker (point))
884 (run-hooks 'local-find-tag-hook)
885 (ring-insert tags-location-ring marker)
886 (current-buffer))))))
888 ;;;###autoload
889 (defun find-tag (tagname &optional next-p regexp-p)
890 "Find tag (in current tags table) whose name contains TAGNAME.
891 Select the buffer containing the tag's definition, and move point there.
892 The default for TAGNAME is the expression in the buffer around or before point.
894 If second arg NEXT-P is t (interactively, with prefix arg), search for
895 another tag that matches the last tagname or regexp used. When there are
896 multiple matches for a tag, more exact matches are found first. If NEXT-P
897 is the atom `-' (interactively, with prefix arg that is a negative number
898 or just \\[negative-argument]), pop back to the previous tag gone to.
900 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
902 A marker representing the point when this command is invoked is pushed
903 onto a ring and may be popped back to with \\[pop-tag-mark].
904 Contrast this with the ring of marks gone to by the command.
906 See documentation of variable `tags-file-name'."
907 (interactive (find-tag-interactive "Find tag: "))
908 (let* ((buf (find-tag-noselect tagname next-p regexp-p))
909 (pos (with-current-buffer buf (point))))
910 (condition-case nil
911 (switch-to-buffer buf)
912 (error (pop-to-buffer buf)))
913 (goto-char pos)))
914 ;;;###autoload (define-key esc-map "." 'find-tag)
916 ;;;###autoload
917 (defun find-tag-other-window (tagname &optional next-p regexp-p)
918 "Find tag (in current tags table) whose name contains TAGNAME.
919 Select the buffer containing the tag's definition in another window, and
920 move point there. The default for TAGNAME is the expression in the buffer
921 around or before point.
923 If second arg NEXT-P is t (interactively, with prefix arg), search for
924 another tag that matches the last tagname or regexp used. When there are
925 multiple matches for a tag, more exact matches are found first. If NEXT-P
926 is negative (interactively, with prefix arg that is a negative number or
927 just \\[negative-argument]), pop back to the previous tag gone to.
929 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
931 A marker representing the point when this command is invoked is pushed
932 onto a ring and may be popped back to with \\[pop-tag-mark].
933 Contrast this with the ring of marks gone to by the command.
935 See documentation of variable `tags-file-name'."
936 (interactive (find-tag-interactive "Find tag other window: "))
938 ;; This hair is to deal with the case where the tag is found in the
939 ;; selected window's buffer; without the hair, point is moved in both
940 ;; windows. To prevent this, we save the selected window's point before
941 ;; doing find-tag-noselect, and restore it after.
942 (let* ((window-point (window-point (selected-window)))
943 (tagbuf (find-tag-noselect tagname next-p regexp-p))
944 (tagpoint (progn (set-buffer tagbuf) (point))))
945 (set-window-point (prog1
946 (selected-window)
947 (switch-to-buffer-other-window tagbuf)
948 ;; We have to set this new window's point; it
949 ;; might already have been displaying a
950 ;; different portion of tagbuf, in which case
951 ;; switch-to-buffer-other-window doesn't set
952 ;; the window's point from the buffer.
953 (set-window-point (selected-window) tagpoint))
954 window-point)))
955 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
957 ;;;###autoload
958 (defun find-tag-other-frame (tagname &optional next-p)
959 "Find tag (in current tags table) whose name contains TAGNAME.
960 Select the buffer containing the tag's definition in another frame, and
961 move point there. The default for TAGNAME is the expression in the buffer
962 around or before point.
964 If second arg NEXT-P is t (interactively, with prefix arg), search for
965 another tag that matches the last tagname or regexp used. When there are
966 multiple matches for a tag, more exact matches are found first. If NEXT-P
967 is negative (interactively, with prefix arg that is a negative number or
968 just \\[negative-argument]), pop back to the previous tag gone to.
970 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
972 A marker representing the point when this command is invoked is pushed
973 onto a ring and may be popped back to with \\[pop-tag-mark].
974 Contrast this with the ring of marks gone to by the command.
976 See documentation of variable `tags-file-name'."
977 (interactive (find-tag-interactive "Find tag other frame: "))
978 (let ((pop-up-frames t))
979 (find-tag-other-window tagname next-p)))
980 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
982 ;;;###autoload
983 (defun find-tag-regexp (regexp &optional next-p other-window)
984 "Find tag (in current tags table) whose name matches REGEXP.
985 Select the buffer containing the tag's definition and move point there.
987 If second arg NEXT-P is t (interactively, with prefix arg), search for
988 another tag that matches the last tagname or regexp used. When there are
989 multiple matches for a tag, more exact matches are found first. If NEXT-P
990 is negative (interactively, with prefix arg that is a negative number or
991 just \\[negative-argument]), pop back to the previous tag gone to.
993 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
995 A marker representing the point when this command is invoked is pushed
996 onto a ring and may be popped back to with \\[pop-tag-mark].
997 Contrast this with the ring of marks gone to by the command.
999 See documentation of variable `tags-file-name'."
1000 (interactive (find-tag-interactive "Find tag regexp: " t))
1001 ;; We go through find-tag-other-window to do all the display hair there.
1002 (funcall (if other-window 'find-tag-other-window 'find-tag)
1003 regexp next-p t))
1004 ;;;###autoload (define-key esc-map [?\C-.] 'find-tag-regexp)
1006 ;;;###autoload (define-key esc-map "*" 'pop-tag-mark)
1008 ;;;###autoload
1009 (defun pop-tag-mark ()
1010 "Pop back to where \\[find-tag] was last invoked.
1012 This is distinct from invoking \\[find-tag] with a negative argument
1013 since that pops a stack of markers at which tags were found, not from
1014 where they were found."
1015 (interactive)
1016 (if (ring-empty-p find-tag-marker-ring)
1017 (error "No previous locations for find-tag invocation"))
1018 (let ((marker (ring-remove find-tag-marker-ring 0)))
1019 (switch-to-buffer (or (marker-buffer marker)
1020 (error "The marked buffer has been deleted")))
1021 (goto-char (marker-position marker))
1022 (set-marker marker nil nil)))
1024 ;; Internal tag finding function.
1026 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
1027 ;; any member of the function list ORDER (third arg). If ORDER is nil,
1028 ;; use saved state to continue a previous search.
1030 ;; Fourth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
1031 ;; point should be moved to the next line.
1033 ;; Fifth arg MATCHING is a string, an English '-ing' word, to be used in
1034 ;; an error message.
1036 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
1037 ;; beginning of tags file, and perform inner loop: for each naive match for
1038 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
1039 ;; qualifier-func. If it qualifies, go to the specified line in the
1040 ;; specified source file and return. Qualified matches are remembered to
1041 ;; avoid repetition. State is saved so that the loop can be continued.
1043 (defvar tag-lines-already-matched nil) ;matches remembered here between calls
1045 (defun find-tag-in-order (pattern
1046 search-forward-func
1047 order
1048 next-line-after-failure-p
1049 matching
1050 first-search)
1051 (let (file ;name of file containing tag
1052 tag-info ;where to find the tag in FILE
1053 (first-table t)
1054 (tag-order order)
1055 (match-marker (make-marker))
1056 goto-func
1057 (case-fold-search (if (memq tags-case-fold-search '(nil t))
1058 tags-case-fold-search
1059 case-fold-search))
1061 (save-excursion
1063 (if first-search
1064 ;; This is the start of a search for a fresh tag.
1065 ;; Clear the list of tags matched by the previous search.
1066 ;; find-tag-noselect has already put us in the first tags table
1067 ;; buffer before we got called.
1068 (setq tag-lines-already-matched nil)
1069 ;; Continuing to search for the tag specified last time.
1070 ;; tag-lines-already-matched lists locations matched in previous
1071 ;; calls so we don't visit the same tag twice if it matches twice
1072 ;; during two passes with different qualification predicates.
1073 ;; Switch to the current tags table buffer.
1074 (visit-tags-table-buffer 'same))
1076 ;; Get a qualified match.
1077 (catch 'qualified-match-found
1079 ;; Iterate over the list of tags tables.
1080 (while (or first-table
1081 (visit-tags-table-buffer t))
1083 (and first-search first-table
1084 ;; Start at beginning of tags file.
1085 (goto-char (point-min)))
1087 (setq first-table nil)
1089 ;; Iterate over the list of ordering predicates.
1090 (while order
1091 (while (funcall search-forward-func pattern nil t)
1092 ;; Naive match found. Qualify the match.
1093 (and (funcall (car order) pattern)
1094 ;; Make sure it is not a previous qualified match.
1095 (not (member (set-marker match-marker (save-excursion
1096 (beginning-of-line)
1097 (point)))
1098 tag-lines-already-matched))
1099 (throw 'qualified-match-found nil))
1100 (if next-line-after-failure-p
1101 (forward-line 1)))
1102 ;; Try the next flavor of match.
1103 (setq order (cdr order))
1104 (goto-char (point-min)))
1105 (setq order tag-order))
1106 ;; We throw out on match, so only get here if there were no matches.
1107 ;; Clear out the markers we use to avoid duplicate matches so they
1108 ;; don't slow down editting and are immediately available for GC.
1109 (while tag-lines-already-matched
1110 (set-marker (car tag-lines-already-matched) nil nil)
1111 (setq tag-lines-already-matched (cdr tag-lines-already-matched)))
1112 (set-marker match-marker nil nil)
1113 (error "No %stags %s %s" (if first-search "" "more ")
1114 matching pattern))
1116 ;; Found a tag; extract location info.
1117 (beginning-of-line)
1118 (setq tag-lines-already-matched (cons match-marker
1119 tag-lines-already-matched))
1120 ;; Expand the filename, using the tags table buffer's default-directory.
1121 ;; We should be able to search for file-name backwards in file-of-tag:
1122 ;; the beginning-of-line is ok except when positioned on a "file-name" tag.
1123 (setq file (expand-file-name
1124 (if (memq (car order) '(tag-exact-file-name-match-p
1125 tag-file-name-match-p
1126 tag-partial-file-name-match-p))
1127 (save-excursion (next-line 1)
1128 (file-of-tag))
1129 (file-of-tag)))
1130 tag-info (funcall snarf-tag-function))
1132 ;; Get the local value in the tags table buffer before switching buffers.
1133 (setq goto-func goto-tag-location-function)
1134 (tag-find-file-of-tag-noselect file)
1135 (widen)
1136 (push-mark)
1137 (funcall goto-func tag-info)
1139 ;; Return the buffer where the tag was found.
1140 (current-buffer))))
1142 (defun tag-find-file-of-tag-noselect (file)
1143 ;; Find the right line in the specified file.
1144 ;; If we are interested in compressed-files,
1145 ;; we search files with extensions.
1146 ;; otherwise only the real file.
1147 (let* ((buffer-search-extensions (if (featurep 'jka-compr)
1148 tags-compression-info-list
1149 '("")))
1150 the-buffer
1151 (file-search-extensions buffer-search-extensions))
1152 ;; search a buffer visiting the file with each possible extension
1153 ;; Note: there is a small inefficiency in find-buffer-visiting :
1154 ;; truename is computed even if not needed. Not too sure about this
1155 ;; but I suspect truename computation accesses the disk.
1156 ;; It is maybe a good idea to optimise this find-buffer-visiting.
1157 ;; An alternative would be to use only get-file-buffer
1158 ;; but this looks less "sure" to find the buffer for the file.
1159 (while (and (not the-buffer) buffer-search-extensions)
1160 (setq the-buffer (find-buffer-visiting (concat file (car buffer-search-extensions))))
1161 (setq buffer-search-extensions (cdr buffer-search-extensions)))
1162 ;; if found a buffer but file modified, ensure we re-read !
1163 (if (and the-buffer (not (verify-visited-file-modtime the-buffer)))
1164 (find-file-noselect (buffer-file-name the-buffer)))
1165 ;; if no buffer found, search for files with possible extensions on disk
1166 (while (and (not the-buffer) file-search-extensions)
1167 (if (not (file-exists-p (concat file (car file-search-extensions))))
1168 (setq file-search-extensions (cdr file-search-extensions))
1169 (setq the-buffer (find-file-noselect (concat file (car file-search-extensions))))))
1170 (if (not the-buffer)
1171 (if (featurep 'jka-compr)
1172 (error "File %s (with or without extensions %s) not found" file tags-compression-info-list)
1173 (error "File %s not found" file))
1174 (set-buffer the-buffer))))
1176 (defun tag-find-file-of-tag (file)
1177 (let ((buf (tag-find-file-of-tag-noselect file)))
1178 (condition-case nil
1179 (switch-to-buffer buf)
1180 (error (pop-to-buffer buf)))))
1182 ;; `etags' TAGS file format support.
1184 ;; If the current buffer is a valid etags TAGS file, give it local values of
1185 ;; the tags table format variables, and return non-nil.
1186 (defun etags-recognize-tags-table ()
1187 (and (etags-verify-tags-table)
1188 ;; It is annoying to flash messages on the screen briefly,
1189 ;; and this message is not useful. -- rms
1190 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
1191 (mapc (lambda (elt) (set (make-local-variable (car elt)) (cdr elt)))
1192 '((file-of-tag-function . etags-file-of-tag)
1193 (tags-table-files-function . etags-tags-table-files)
1194 (tags-completion-table-function . etags-tags-completion-table)
1195 (snarf-tag-function . etags-snarf-tag)
1196 (goto-tag-location-function . etags-goto-tag-location)
1197 (find-tag-regexp-search-function . re-search-forward)
1198 (find-tag-regexp-tag-order . (tag-re-match-p))
1199 (find-tag-regexp-next-line-after-failure-p . t)
1200 (find-tag-search-function . search-forward)
1201 (find-tag-tag-order . (tag-exact-file-name-match-p
1202 tag-file-name-match-p
1203 tag-exact-match-p
1204 tag-implicit-name-match-p
1205 tag-symbol-match-p
1206 tag-word-match-p
1207 tag-partial-file-name-match-p
1208 tag-any-match-p))
1209 (find-tag-next-line-after-failure-p . nil)
1210 (list-tags-function . etags-list-tags)
1211 (tags-apropos-function . etags-tags-apropos)
1212 (tags-included-tables-function . etags-tags-included-tables)
1213 (verify-tags-table-function . etags-verify-tags-table)
1214 ))))
1216 ;; Return non-nil iff the current buffer is a valid etags TAGS file.
1217 (defun etags-verify-tags-table ()
1218 ;; Use eq instead of = in case char-after returns nil.
1219 (eq (char-after (point-min)) ?\f))
1221 (defun etags-file-of-tag (&optional relative)
1222 (save-excursion
1223 (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n")
1224 (let ((str (buffer-substring (match-beginning 1) (match-end 1))))
1225 (if relative
1227 (expand-file-name str
1228 (file-truename default-directory))))))
1231 (defun etags-tags-completion-table ()
1232 (let ((table (make-vector 511 0))
1233 (progress-reporter
1234 (make-progress-reporter
1235 (format "Making tags completion table for %s..." buffer-file-name)
1236 (point-min) (point-max))))
1237 (save-excursion
1238 (goto-char (point-min))
1239 ;; This monster regexp matches an etags tag line.
1240 ;; \1 is the string to match;
1241 ;; \2 is not interesting;
1242 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
1243 ;; \4 is not interesting;
1244 ;; \5 is the explicitly-specified tag name.
1245 ;; \6 is the line to start searching at;
1246 ;; \7 is the char to start searching at.
1247 (while (re-search-forward
1248 "^\\(\\([^\177]+[^-a-zA-Z0-9_+*$:\177]+\\)?\
1249 \\([-a-zA-Z0-9_+*$?:]+\\)[^-a-zA-Z0-9_+*$?:\177]*\\)\177\
1250 \\(\\([^\n\001]+\\)\001\\)?\\([0-9]+\\)?,\\([0-9]+\\)?\n"
1251 nil t)
1252 (intern (prog1 (if (match-beginning 5)
1253 ;; There is an explicit tag name.
1254 (buffer-substring (match-beginning 5) (match-end 5))
1255 ;; No explicit tag name. Best guess.
1256 (buffer-substring (match-beginning 3) (match-end 3)))
1257 (progress-reporter-update progress-reporter (point)))
1258 table)))
1259 table))
1261 (defun etags-snarf-tag (&optional use-explicit)
1262 (let (tag-text line startpos explicit-start)
1263 (if (save-excursion
1264 (forward-line -1)
1265 (looking-at "\f\n"))
1266 ;; The match was for a source file name, not any tag within a file.
1267 ;; Give text of t, meaning to go exactly to the location we specify,
1268 ;; the beginning of the file.
1269 (setq tag-text t
1270 line nil
1271 startpos (point-min))
1273 ;; Find the end of the tag and record the whole tag text.
1274 (search-forward "\177")
1275 (setq tag-text (buffer-substring (1- (point))
1276 (save-excursion (beginning-of-line)
1277 (point))))
1278 ;; If use-explicit is non nil and explicit tag is present, use it as part of
1279 ;; return value. Else just skip it.
1280 (setq explicit-start (point))
1281 (when (and (search-forward "\001" (save-excursion (forward-line 1) (point)) t)
1282 use-explicit)
1283 (setq tag-text (buffer-substring explicit-start (1- (point)))))
1286 (if (looking-at "[0-9]")
1287 (setq line (string-to-number (buffer-substring
1288 (point)
1289 (progn (skip-chars-forward "0-9")
1290 (point))))))
1291 (search-forward ",")
1292 (if (looking-at "[0-9]")
1293 (setq startpos (string-to-number (buffer-substring
1294 (point)
1295 (progn (skip-chars-forward "0-9")
1296 (point)))))))
1297 ;; Leave point on the next line of the tags file.
1298 (forward-line 1)
1299 (cons tag-text (cons line startpos))))
1301 ;; TAG-INFO is a cons (TEXT LINE . POSITION) where TEXT is the initial part
1302 ;; of a line containing the tag and POSITION is the character position of
1303 ;; TEXT within the file (starting from 1); LINE is the line number. If
1304 ;; TEXT is t, it means the tag refers to exactly LINE or POSITION
1305 ;; (whichever is present, LINE having preference, no searching. Either
1306 ;; LINE or POSITION may be nil; POSITION is used if present. If the tag
1307 ;; isn't exactly at the given position then look around that position using
1308 ;; a search window which expands until it hits the start of file.
1309 (defun etags-goto-tag-location (tag-info)
1310 (let ((startpos (cdr (cdr tag-info)))
1311 (line (car (cdr tag-info)))
1312 offset found pat)
1313 (if (eq (car tag-info) t)
1314 ;; Direct file tag.
1315 (cond (line (goto-line line))
1316 (startpos (goto-char startpos))
1317 (t (error "etags.el BUG: bogus direct file tag")))
1318 ;; This constant is 1/2 the initial search window.
1319 ;; There is no sense in making it too small,
1320 ;; since just going around the loop once probably
1321 ;; costs about as much as searching 2000 chars.
1322 (setq offset 1000
1323 found nil
1324 pat (concat (if (eq selective-display t)
1325 "\\(^\\|\^m\\)" "^")
1326 (regexp-quote (car tag-info))))
1327 ;; The character position in the tags table is 0-origin.
1328 ;; Convert it to a 1-origin Emacs character position.
1329 (if startpos (setq startpos (1+ startpos)))
1330 ;; If no char pos was given, try the given line number.
1331 (or startpos
1332 (if line
1333 (setq startpos (progn (goto-line line)
1334 (point)))))
1335 (or startpos
1336 (setq startpos (point-min)))
1337 ;; First see if the tag is right at the specified location.
1338 (goto-char startpos)
1339 (setq found (looking-at pat))
1340 (while (and (not found)
1341 (progn
1342 (goto-char (- startpos offset))
1343 (not (bobp))))
1344 (setq found
1345 (re-search-forward pat (+ startpos offset) t)
1346 offset (* 3 offset))) ; expand search window
1347 (or found
1348 (re-search-forward pat nil t)
1349 (error "Rerun etags: `%s' not found in %s"
1350 pat buffer-file-name)))
1351 ;; Position point at the right place
1352 ;; if the search string matched an extra Ctrl-m at the beginning.
1353 (and (eq selective-display t)
1354 (looking-at "\^m")
1355 (forward-char 1))
1356 (beginning-of-line)))
1358 (defun etags-list-tags (file)
1359 (goto-char (point-min))
1360 (when (re-search-forward (concat "\f\n" "\\(" file "\\)" ",") nil t)
1361 (let ((path (save-excursion (forward-line 1) (file-of-tag)))
1362 ;; Get the local value in the tags table
1363 ;; buffer before switching buffers.
1364 (goto-func goto-tag-location-function)
1365 tag tag-info pt)
1366 (forward-line 1)
1367 (while (not (or (eobp) (looking-at "\f")))
1368 (setq tag-info (save-excursion (funcall snarf-tag-function t))
1369 tag (car tag-info)
1370 pt (with-current-buffer standard-output (point)))
1371 (princ tag)
1372 (when (= (aref tag 0) ?\() (princ " ...)"))
1373 (with-current-buffer standard-output
1374 (make-text-button pt (point)
1375 'tag-info tag-info
1376 'file-path path
1377 'goto-func goto-func
1378 'action (lambda (button)
1379 (let ((tag-info (button-get button 'tag-info))
1380 (goto-func (button-get button 'goto-func)))
1381 (tag-find-file-of-tag (button-get button 'file-path))
1382 (widen)
1383 (funcall goto-func tag-info)))
1384 'face 'tags-tag-face
1385 'type 'button))
1386 (terpri)
1387 (forward-line 1))
1388 t)))
1390 (defmacro tags-with-face (face &rest body)
1391 "Execute BODY, give output to `standard-output' face FACE."
1392 (let ((pp (make-symbol "start")))
1393 `(let ((,pp (with-current-buffer standard-output (point))))
1394 ,@body
1395 (put-text-property ,pp (with-current-buffer standard-output (point))
1396 'face ,face standard-output))))
1398 (defun etags-tags-apropos-additional (regexp)
1399 "Display tags matching REGEXP from `tags-apropos-additional-actions'."
1400 (with-current-buffer standard-output
1401 (dolist (oba tags-apropos-additional-actions)
1402 (princ "\n\n")
1403 (tags-with-face 'highlight (princ (car oba)))
1404 (princ":\n\n")
1405 (let* ((beg (point))
1406 (symbs (car (cddr oba)))
1407 (ins-symb (lambda (sy)
1408 (let ((sn (symbol-name sy)))
1409 (when (string-match regexp sn)
1410 (make-text-button (point)
1411 (progn (princ sy) (point))
1412 'action-internal(cadr oba)
1413 'action (lambda (button) (funcall
1414 (button-get button 'action-internal)
1415 (button-get button 'item)))
1416 'item sn
1417 'face tags-tag-face
1418 'type 'button)
1419 (terpri))))))
1420 (when (symbolp symbs)
1421 (if (boundp symbs)
1422 (setq symbs (symbol-value symbs))
1423 (insert "symbol `" (symbol-name symbs) "' has no value\n")
1424 (setq symbs nil)))
1425 (if (vectorp symbs)
1426 (mapatoms ins-symb symbs)
1427 (dolist (sy symbs)
1428 (funcall ins-symb (car sy))))
1429 (sort-lines nil beg (point))))))
1431 (defun etags-tags-apropos (string)
1432 (when tags-apropos-verbose
1433 (princ "Tags in file `")
1434 (tags-with-face 'highlight (princ buffer-file-name))
1435 (princ "':\n\n"))
1436 (goto-char (point-min))
1437 (let ((progress-reporter (make-progress-reporter
1438 (format "Making tags apropos buffer for `%s'..."
1439 string)
1440 (point-min) (point-max))))
1441 (while (re-search-forward string nil t)
1442 (progress-reporter-update progress-reporter (point))
1443 (beginning-of-line)
1445 (let* ( ;; Get the local value in the tags table
1446 ;; buffer before switching buffers.
1447 (goto-func goto-tag-location-function)
1448 (tag-info (save-excursion (funcall snarf-tag-function)))
1449 (tag (if (eq t (car tag-info)) nil (car tag-info)))
1450 (file-path (save-excursion (if tag (file-of-tag)
1451 (save-excursion (next-line 1)
1452 (file-of-tag)))))
1453 (file-label (if tag (file-of-tag t)
1454 (save-excursion (next-line 1)
1455 (file-of-tag t))))
1456 (pt (with-current-buffer standard-output (point))))
1457 (if tag
1458 (progn
1459 (princ (format "[%s]: " file-label))
1460 (princ tag)
1461 (when (= (aref tag 0) ?\() (princ " ...)"))
1462 (with-current-buffer standard-output
1463 (make-text-button pt (point)
1464 'tag-info tag-info
1465 'file-path file-path
1466 'goto-func goto-func
1467 'action (lambda (button)
1468 (let ((tag-info (button-get button 'tag-info))
1469 (goto-func (button-get button 'goto-func)))
1470 (tag-find-file-of-tag (button-get button 'file-path))
1471 (widen)
1472 (funcall goto-func tag-info)))
1473 'face 'tags-tag-face
1474 'type 'button)))
1475 (princ (format "- %s" file-label))
1476 (with-current-buffer standard-output
1477 (make-text-button pt (point)
1478 'file-path file-path
1479 'action (lambda (button)
1480 (tag-find-file-of-tag (button-get button 'file-path))
1481 ;; Get the local value in the tags table
1482 ;; buffer before switching buffers.
1483 (goto-char (point-min)))
1484 'face 'tags-tag-face
1485 'type 'button))
1487 (terpri)
1488 (forward-line 1))
1489 (message nil))
1490 (when tags-apropos-verbose (princ "\n")))
1492 (defun etags-tags-table-files ()
1493 (let ((files nil)
1494 beg)
1495 (goto-char (point-min))
1496 (while (search-forward "\f\n" nil t)
1497 (setq beg (point))
1498 (end-of-line)
1499 (skip-chars-backward "^," beg)
1500 (or (looking-at "include$")
1501 (setq files (cons (buffer-substring beg (1- (point))) files))))
1502 (nreverse files)))
1504 (defun etags-tags-included-tables ()
1505 (let ((files nil)
1506 beg)
1507 (goto-char (point-min))
1508 (while (search-forward "\f\n" nil t)
1509 (setq beg (point))
1510 (end-of-line)
1511 (skip-chars-backward "^," beg)
1512 (if (looking-at "include$")
1513 ;; Expand in the default-directory of the tags table buffer.
1514 (setq files (cons (expand-file-name (buffer-substring beg (1- (point))))
1515 files))))
1516 (nreverse files)))
1518 ;; Empty tags file support.
1520 ;; Recognize an empty file and give it local values of the tags table format
1521 ;; variables which do nothing.
1522 (defun tags-recognize-empty-tags-table ()
1523 (and (zerop (buffer-size))
1524 (mapc (lambda (sym) (set (make-local-variable sym) 'ignore))
1525 '(tags-table-files-function
1526 tags-completion-table-function
1527 find-tag-regexp-search-function
1528 find-tag-search-function
1529 tags-apropos-function
1530 tags-included-tables-function))
1531 (set (make-local-variable 'verify-tags-table-function)
1532 (lambda () (zerop (buffer-size))))))
1534 ;; Match qualifier functions for tagnames.
1535 ;; These functions assume the etags file format defined in etc/ETAGS.EBNF.
1537 ;; This might be a neat idea, but it's too hairy at the moment.
1538 ;;(defmacro tags-with-syntax (&rest body)
1539 ;; `(let ((current (current-buffer))
1540 ;; (otable (syntax-table))
1541 ;; (buffer (find-file-noselect (file-of-tag)))
1542 ;; table)
1543 ;; (unwind-protect
1544 ;; (progn
1545 ;; (set-buffer buffer)
1546 ;; (setq table (syntax-table))
1547 ;; (set-buffer current)
1548 ;; (set-syntax-table table)
1549 ;; ,@body)
1550 ;; (set-syntax-table otable))))
1551 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
1553 ;; exact file name match, i.e. searched tag must match complete file
1554 ;; name including directories parts if there are some.
1555 (defun tag-exact-file-name-match-p (tag)
1556 (and (looking-at ",[0-9\n]")
1557 (save-excursion (backward-char (+ 2 (length tag)))
1558 (looking-at "\f\n"))))
1559 ;; file name match as above, but searched tag must match the file
1560 ;; name not including the directories if there are some.
1561 (defun tag-file-name-match-p (tag)
1562 (and (looking-at ",[0-9\n]")
1563 (save-excursion (backward-char (1+ (length tag)))
1564 (looking-at "/"))))
1565 ;; this / to detect we are after a directory separator is ok for unix,
1566 ;; is there a variable that contains the regexp for directory separator
1567 ;; on whatever operating system ?
1568 ;; Looks like ms-win will lose here :).
1570 ;; t if point is at a tag line that matches TAG exactly.
1571 ;; point should be just after a string that matches TAG.
1572 (defun tag-exact-match-p (tag)
1573 ;; The match is really exact if there is an explicit tag name.
1574 (or (and (eq (char-after (point)) ?\001)
1575 (eq (char-after (- (point) (length tag) 1)) ?\177))
1576 ;; We are not on the explicit tag name, but perhaps it follows.
1577 (looking-at (concat "[^\177\n]*\177" (regexp-quote tag) "\001"))))
1579 ;; t if point is at a tag line that has an implicit name.
1580 ;; point should be just after a string that matches TAG.
1581 (defun tag-implicit-name-match-p (tag)
1582 ;; Look at the comment of the make_tag function in lib-src/etags.c for
1583 ;; a textual description of the four rules.
1584 (and (string-match "^[^ \t()=,;]+$" tag) ;rule #1
1585 (looking-at "[ \t()=,;]?\177") ;rules #2 and #4
1586 (save-excursion
1587 (backward-char (1+ (length tag)))
1588 (looking-at "[\n \t()=,;]")))) ;rule #3
1590 ;; t if point is at a tag line that matches TAG as a symbol.
1591 ;; point should be just after a string that matches TAG.
1592 (defun tag-symbol-match-p (tag)
1593 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1594 (save-excursion
1595 (backward-char (1+ (length tag)))
1596 (and (looking-at "\\Sw") (looking-at "\\S_")))))
1598 ;; t if point is at a tag line that matches TAG as a word.
1599 ;; point should be just after a string that matches TAG.
1600 (defun tag-word-match-p (tag)
1601 (and (looking-at "\\b.*\177")
1602 (save-excursion (backward-char (length tag))
1603 (looking-at "\\b"))))
1605 ;; partial file name match, i.e. searched tag must match a substring
1606 ;; of the file name (potentially including a directory separator).
1607 (defun tag-partial-file-name-match-p (tag)
1608 (and (looking-at ".*,[0-9\n]")
1609 (save-excursion (beginning-of-line)
1610 (backward-char 2)
1611 (looking-at "\f\n"))))
1613 ;; t if point is in a tag line with a tag containing TAG as a substring.
1614 (defun tag-any-match-p (tag)
1615 (looking-at ".*\177"))
1617 ;; t if point is at a tag line that matches RE as a regexp.
1618 (defun tag-re-match-p (re)
1619 (save-excursion
1620 (beginning-of-line)
1621 (let ((bol (point)))
1622 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t)
1623 (re-search-backward re bol t)))))
1625 (defcustom tags-loop-revert-buffers nil
1626 "*Non-nil means tags-scanning loops should offer to reread changed files.
1627 These loops normally read each file into Emacs, but when a file
1628 is already visited, they use the existing buffer.
1629 When this flag is non-nil, they offer to revert the existing buffer
1630 in the case where the file has changed since you visited it."
1631 :type 'boolean
1632 :group 'etags)
1634 ;;;###autoload
1635 (defun next-file (&optional initialize novisit)
1636 "Select next file among files in current tags table.
1638 A first argument of t (prefix arg, if interactive) initializes to the
1639 beginning of the list of files in the tags table. If the argument is
1640 neither nil nor t, it is evalled to initialize the list of files.
1642 Non-nil second argument NOVISIT means use a temporary buffer
1643 to save time and avoid uninteresting warnings.
1645 Value is nil if the file was already visited;
1646 if the file was newly read in, the value is the filename."
1647 ;; Make the interactive arg t if there was any prefix arg.
1648 (interactive (list (if current-prefix-arg t)))
1649 (cond ((not initialize)
1650 ;; Not the first run.
1652 ((eq initialize t)
1653 ;; Initialize the list from the tags table.
1654 (save-excursion
1655 ;; Visit the tags table buffer to get its list of files.
1656 (visit-tags-table-buffer)
1657 ;; Copy the list so we can setcdr below, and expand the file
1658 ;; names while we are at it, in this buffer's default directory.
1659 (setq next-file-list (mapcar 'expand-file-name (tags-table-files)))
1660 ;; Iterate over all the tags table files, collecting
1661 ;; a complete list of referenced file names.
1662 (while (visit-tags-table-buffer t)
1663 ;; Find the tail of the working list and chain on the new
1664 ;; sublist for this tags table.
1665 (let ((tail next-file-list))
1666 (while (cdr tail)
1667 (setq tail (cdr tail)))
1668 ;; Use a copy so the next loop iteration will not modify the
1669 ;; list later returned by (tags-table-files).
1670 (if tail
1671 (setcdr tail (mapcar 'expand-file-name (tags-table-files)))
1672 (setq next-file-list (mapcar 'expand-file-name
1673 (tags-table-files))))))))
1675 ;; Initialize the list by evalling the argument.
1676 (setq next-file-list (eval initialize))))
1677 (unless next-file-list
1678 (and novisit
1679 (get-buffer " *next-file*")
1680 (kill-buffer " *next-file*"))
1681 (error "All files processed"))
1682 (let* ((next (car next-file-list))
1683 (buffer (get-file-buffer next))
1684 (new (not buffer)))
1685 ;; Advance the list before trying to find the file.
1686 ;; If we get an error finding the file, don't get stuck on it.
1687 (setq next-file-list (cdr next-file-list))
1688 ;; Optionally offer to revert buffers
1689 ;; if the files have changed on disk.
1690 (and buffer tags-loop-revert-buffers
1691 (not (verify-visited-file-modtime buffer))
1692 (with-current-buffer buffer
1693 (revert-buffer t)))
1694 (if (not (and new novisit))
1695 (set-buffer (find-file-noselect next novisit))
1696 ;; Like find-file, but avoids random warning messages.
1697 (set-buffer (get-buffer-create " *next-file*"))
1698 (kill-all-local-variables)
1699 (erase-buffer)
1700 (setq new next)
1701 (insert-file-contents new nil))
1702 new))
1704 (defvar tags-loop-operate nil
1705 "Form for `tags-loop-continue' to eval to change one file.")
1707 (defvar tags-loop-scan
1708 '(error "%s"
1709 (substitute-command-keys
1710 "No \\[tags-search] or \\[tags-query-replace] in progress"))
1711 "Form for `tags-loop-continue' to eval to scan one file.
1712 If it returns non-nil, this file needs processing by evalling
1713 \`tags-loop-operate'. Otherwise, move on to the next file.")
1715 (defun tags-loop-eval (form)
1716 "Evaluate FORM and return its result.
1717 Bind `case-fold-search' during the evaluation, depending on the value of
1718 `tags-case-fold-search'."
1719 (let ((case-fold-search (if (memq tags-case-fold-search '(t nil))
1720 tags-case-fold-search
1721 case-fold-search)))
1722 (eval form)))
1725 ;;;###autoload
1726 (defun tags-loop-continue (&optional first-time)
1727 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1728 Used noninteractively with non-nil argument to begin such a command (the
1729 argument is passed to `next-file', which see).
1731 Two variables control the processing we do on each file: the value of
1732 `tags-loop-scan' is a form to be executed on each file to see if it is
1733 interesting (it returns non-nil if so) and `tags-loop-operate' is a form to
1734 evaluate to operate on an interesting file. If the latter evaluates to
1735 nil, we exit; otherwise we scan the next file."
1736 (interactive)
1737 (let (new
1738 ;; Non-nil means we have finished one file
1739 ;; and should not scan it again.
1740 file-finished
1741 original-point
1742 (messaged nil))
1743 (while
1744 (progn
1745 ;; Scan files quickly for the first or next interesting one.
1746 ;; This starts at point in the current buffer.
1747 (while (or first-time file-finished
1748 (save-restriction
1749 (widen)
1750 (not (tags-loop-eval tags-loop-scan))))
1751 ;; If nothing was found in the previous file, and
1752 ;; that file isn't in a temp buffer, restore point to
1753 ;; where it was.
1754 (when original-point
1755 (goto-char original-point))
1757 (setq file-finished nil)
1758 (setq new (next-file first-time t))
1760 ;; If NEW is non-nil, we got a temp buffer,
1761 ;; and NEW is the file name.
1762 (when (or messaged
1763 (and (not first-time)
1764 (> baud-rate search-slow-speed)
1765 (setq messaged t)))
1766 (message "Scanning file %s..." (or new buffer-file-name)))
1768 (setq first-time nil)
1769 (setq original-point (if new nil (point)))
1770 (goto-char (point-min)))
1772 ;; If we visited it in a temp buffer, visit it now for real.
1773 (if new
1774 (let ((pos (point)))
1775 (erase-buffer)
1776 (set-buffer (find-file-noselect new))
1777 (setq new nil) ;No longer in a temp buffer.
1778 (widen)
1779 (goto-char pos))
1780 (push-mark original-point t))
1782 (switch-to-buffer (current-buffer))
1784 ;; Now operate on the file.
1785 ;; If value is non-nil, continue to scan the next file.
1786 (tags-loop-eval tags-loop-operate))
1787 (setq file-finished t))
1788 (and messaged
1789 (null tags-loop-operate)
1790 (message "Scanning file %s...found" buffer-file-name))))
1791 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
1793 ;;;###autoload
1794 (defun tags-search (regexp &optional file-list-form)
1795 "Search through all files listed in tags table for match for REGEXP.
1796 Stops when a match is found.
1797 To continue searching for next match, use command \\[tags-loop-continue].
1799 See documentation of variable `tags-file-name'."
1800 (interactive "sTags search (regexp): ")
1801 (if (and (equal regexp "")
1802 (eq (car tags-loop-scan) 're-search-forward)
1803 (null tags-loop-operate))
1804 ;; Continue last tags-search as if by M-,.
1805 (tags-loop-continue nil)
1806 (setq tags-loop-scan `(re-search-forward ',regexp nil t)
1807 tags-loop-operate nil)
1808 (tags-loop-continue (or file-list-form t))))
1810 ;;;###autoload
1811 (defun tags-query-replace (from to &optional delimited file-list-form start end)
1812 "Do `query-replace-regexp' of FROM with TO on all files listed in tags table.
1813 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1814 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
1815 with the command \\[tags-loop-continue].
1817 See documentation of variable `tags-file-name'."
1818 (interactive (query-replace-read-args "Tags query replace (regexp)" t t))
1819 (setq tags-loop-scan `(let ,(unless (equal from (downcase from))
1820 '((case-fold-search nil)))
1821 (if (re-search-forward ',from nil t)
1822 ;; When we find a match, move back
1823 ;; to the beginning of it so perform-replace
1824 ;; will see it.
1825 (goto-char (match-beginning 0))))
1826 tags-loop-operate `(perform-replace ',from ',to t t ',delimited))
1827 (tags-loop-continue (or file-list-form t)))
1829 (defun tags-complete-tags-table-file (string predicate what)
1830 (save-excursion
1831 ;; If we need to ask for the tag table, allow that.
1832 (let ((enable-recursive-minibuffers t))
1833 (visit-tags-table-buffer))
1834 (if (eq what t)
1835 (all-completions string (tags-table-files) predicate)
1836 (try-completion string (tags-table-files) predicate))))
1838 ;;;###autoload
1839 (defun list-tags (file &optional next-match)
1840 "Display list of tags in file FILE.
1841 This searches only the first table in the list, and no included tables.
1842 FILE should be as it appeared in the `etags' command, usually without a
1843 directory specification."
1844 (interactive (list (completing-read "List tags in file: "
1845 'tags-complete-tags-table-file
1846 nil t nil)))
1847 (with-output-to-temp-buffer "*Tags List*"
1848 (princ "Tags in file `")
1849 (tags-with-face 'highlight (princ file))
1850 (princ "':\n\n")
1851 (save-excursion
1852 (let ((first-time t)
1853 (gotany nil))
1854 (while (visit-tags-table-buffer (not first-time))
1855 (setq first-time nil)
1856 (if (funcall list-tags-function file)
1857 (setq gotany t)))
1858 (or gotany
1859 (error "File %s not in current tags tables" file)))))
1860 (with-current-buffer "*Tags List*"
1861 (require 'apropos)
1862 (with-no-warnings
1863 (apropos-mode))
1864 (setq buffer-read-only t)))
1866 ;;;###autoload
1867 (defun tags-apropos (regexp)
1868 "Display list of all tags in tags table REGEXP matches."
1869 (interactive "sTags apropos (regexp): ")
1870 (with-output-to-temp-buffer "*Tags List*"
1871 (princ "Click mouse-2 to follow tags.\n\nTags matching regexp `")
1872 (tags-with-face 'highlight (princ regexp))
1873 (princ "':\n\n")
1874 (save-excursion
1875 (let ((first-time t))
1876 (while (visit-tags-table-buffer (not first-time))
1877 (setq first-time nil)
1878 (funcall tags-apropos-function regexp))))
1879 (etags-tags-apropos-additional regexp))
1880 (with-current-buffer "*Tags List*"
1881 (require 'apropos)
1882 (apropos-mode)
1883 ;; apropos-mode is derived from fundamental-mode and it kills
1884 ;; all local variables.
1885 (setq buffer-read-only t)))
1887 ;; XXX Kludge interface.
1889 (define-button-type 'tags-select-tags-table
1890 'action (lambda (button) (select-tags-table-select))
1891 'help-echo "RET, t or mouse-2: select tags table")
1893 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1894 ;;;###autoload
1895 (defun select-tags-table ()
1896 "Select a tags table file from a menu of those you have already used.
1897 The list of tags tables to select from is stored in `tags-table-set-list';
1898 see the doc of that variable if you want to add names to the list."
1899 (interactive)
1900 (pop-to-buffer "*Tags Table List*")
1901 (setq buffer-read-only nil)
1902 (erase-buffer)
1903 (let ((set-list tags-table-set-list)
1904 (desired-point nil)
1906 (when tags-table-list
1907 (setq desired-point (point-marker))
1908 (setq b (point))
1909 (princ tags-table-list (current-buffer))
1910 (make-text-button b (point) 'type 'tags-select-tags-table)
1911 (insert "\C-m")
1912 (prin1 (car tags-table-list) (current-buffer)) ;invisible
1913 (insert "\n"))
1914 (while set-list
1915 (unless (eq (car set-list) tags-table-list)
1916 (setq b (point))
1917 (princ (car set-list) (current-buffer))
1918 (make-text-button b (point) 'type 'tags-select-tags-table)
1919 (insert "\C-m")
1920 (prin1 (car (car set-list)) (current-buffer)) ;invisible
1921 (insert "\n"))
1922 (setq set-list (cdr set-list)))
1923 (when tags-file-name
1924 (or desired-point
1925 (setq desired-point (point-marker)))
1926 (setq b (point))
1927 (insert tags-file-name)
1928 (make-text-button b (point) 'type 'tags-select-tags-table)
1929 (insert "\C-m")
1930 (prin1 tags-file-name (current-buffer)) ;invisible
1931 (insert "\n"))
1932 (setq set-list (delete tags-file-name
1933 (apply 'nconc (cons (copy-sequence tags-table-list)
1934 (mapcar 'copy-sequence
1935 tags-table-set-list)))))
1936 (while set-list
1937 (setq b (point))
1938 (insert (car set-list))
1939 (make-text-button b (point) 'type 'tags-select-tags-table)
1940 (insert "\C-m")
1941 (prin1 (car set-list) (current-buffer)) ;invisible
1942 (insert "\n")
1943 (setq set-list (delete (car set-list) set-list)))
1944 (goto-char (point-min))
1945 (insert-before-markers
1946 "Type `t' to select a tags table or set of tags tables:\n\n")
1947 (if desired-point
1948 (goto-char desired-point))
1949 (set-window-start (selected-window) 1 t))
1950 (set-buffer-modified-p nil)
1951 (select-tags-table-mode))
1953 (defvar select-tags-table-mode-map
1954 (let ((map (copy-keymap button-buffer-map)))
1955 (define-key map "t" 'push-button)
1956 (define-key map " " 'next-line)
1957 (define-key map "\^?" 'previous-line)
1958 (define-key map "n" 'next-line)
1959 (define-key map "p" 'previous-line)
1960 (define-key map "q" 'select-tags-table-quit)
1961 map))
1963 (defun select-tags-table-mode ()
1964 "Major mode for choosing a current tags table among those already loaded.
1966 \\{select-tags-table-mode-map}"
1967 (interactive)
1968 (kill-all-local-variables)
1969 (setq buffer-read-only t
1970 major-mode 'select-tags-table-mode
1971 mode-name "Select Tags Table")
1972 (use-local-map select-tags-table-mode-map)
1973 (setq selective-display t
1974 selective-display-ellipses nil))
1976 (defun select-tags-table-select ()
1977 "Select the tags table named on this line."
1978 (interactive)
1979 (search-forward "\C-m")
1980 (let ((name (read (current-buffer))))
1981 (visit-tags-table name)
1982 (select-tags-table-quit)
1983 (message "Tags table now %s" name)))
1985 (defun select-tags-table-quit ()
1986 "Kill the buffer and delete the selected window."
1987 (interactive)
1988 (quit-window t (selected-window)))
1990 ;; Note, there is another definition of this function in bindings.el.
1991 ;;;###autoload
1992 (defun complete-tag ()
1993 "Perform tags completion on the text around point.
1994 Completes to the set of names listed in the current tags table.
1995 The string to complete is chosen in the same way as the default
1996 for \\[find-tag] (which see)."
1997 (interactive)
1998 (or tags-table-list
1999 tags-file-name
2000 (error "%s"
2001 (substitute-command-keys
2002 "No tags table loaded; try \\[visit-tags-table]")))
2003 (let ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
2004 tags-case-fold-search
2005 case-fold-search))
2006 (pattern (funcall (or find-tag-default-function
2007 (get major-mode 'find-tag-default-function)
2008 'find-tag-default)))
2010 completion)
2011 (or pattern
2012 (error "Nothing to complete"))
2013 (search-backward pattern)
2014 (setq beg (point))
2015 (forward-char (length pattern))
2016 (setq completion (tags-complete-tag pattern nil nil))
2017 (cond ((eq completion t))
2018 ((null completion)
2019 (message "Can't find completion for \"%s\"" pattern)
2020 (ding))
2021 ((not (string= pattern completion))
2022 (delete-region beg (point))
2023 (insert completion))
2025 (message "Making completion list...")
2026 (with-output-to-temp-buffer "*Completions*"
2027 (display-completion-list
2028 (all-completions pattern 'tags-complete-tag nil)))
2029 (message "Making completion list...%s" "done")))))
2031 (dolist (x '("^No tags table in use; use .* to select one$"
2032 "^There is no default tag$"
2033 "^No previous tag locations$"
2034 "^File .* is not a valid tags table$"
2035 "^No \\(more \\|\\)tags \\(matching\\|containing\\) "
2036 "^Rerun etags: `.*' not found in "
2037 "^All files processed$"
2038 "^No .* or .* in progress$"
2039 "^File .* not in current tags tables$"
2040 "^No tags table loaded"
2041 "^Nothing to complete$"))
2042 (add-to-list 'debug-ignored-errors x))
2044 (provide 'etags)
2046 ;;; arch-tag: b897c2b5-08f3-4837-b2d3-0e7d6db1b63e
2047 ;;; etags.el ends here