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