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