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
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/>.
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."
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'."
52 :type '(choice (const :tag "Case-sensitive" nil)
53 (const :tag "Case-insensitive" t)
54 (other :tag "Use default" default))
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."
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)
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.
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)."
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."
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.")
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."
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."
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'."
154 (defcustom tags-apropos-verbose nil
155 "If non-nil, print the name of the tags file in the *Tags List* buffer."
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.
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))"
178 :type '(repeat (list (string :tag "Title")
180 (sexp :tag "Tags to search")))
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."
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].")
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
272 ;; Value is t if we have found a valid tags table buffer.
273 (run-hook-with-args-until-success 'tags-table-format-functions))
276 (defun tags-table-mode ()
277 "Major mode for tags table file buffers."
279 (setq major-mode 'tags-table-mode ;FIXME: Use define-derived-mode.
280 mode-name "Tags Table"
282 (initialize-new-tags-table))
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): "
297 (expand-file-name "TAGS"
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))
309 (or (visit-tags-table-buffer file)
310 (signal 'file-error (list "Visiting tags table"
311 "No such file or directory"
313 ;; Set FILE to the expanded name.
314 (setq file tags-file-name)))
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.
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.
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.
343 (if (tags-included-tables)
344 ;; Insert the included tables into the list we
346 (setcdr tables (nconc (mapcar 'tags-expand-table-name
347 (tags-included-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)))
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))
378 (setq computed (cons (car tables) computed)
379 table-buffer (get-file-buffer (car tables)))
381 (with-current-buffer table-buffer
382 (if (tags-included-tables)
383 ;; Insert the included tables into the list we
385 (setcdr tables (append (tags-included-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
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)
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))
416 (or (eq (car list) t)
417 (not (string= file (tags-expand-table-name (car list))))))
418 (setq list (cdr 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.
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)
438 (if (string-match (car tail) buffer-file-name)
440 (setq tail (cdr tail)))
442 tags-revert-without-query
444 (format "Tags file %s has changed, read new contents? "
446 (and verify-tags-table-function
447 (funcall verify-tags-table-function))
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.
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)
479 ;; Loop over the list, looking for a table containing tags for THIS-FILE.
480 (while (and (not found)
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)))
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
498 (setq found tables))))
499 (setq tables (cdr tables)))
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
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)))
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.
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
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))))
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.
558 (substitute-command-keys
559 (concat "No tags table in use; "
560 "use \\[visit-tags-table] to select one")))))
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.
573 ;; If passed a string, use that.
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
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.
596 (not (tags-table-list-member tags-file-name
597 tags-table-computed-list))
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)
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)))
609 ;; Finally, prompt the user for a file name.
611 (read-file-name "Visit tags table (default TAGS): "
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.
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.
637 ;; Look in the list for the table we chose.
638 (let ((found (tags-table-list-member
640 tags-table-computed-list)))
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))
650 (not (tags-table-list-member
653 (setq sets (cdr sets)))
655 ;; Found in some other set. Switch to that set.
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)
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
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.
686 (let ((buffer (find-buffer-visiting table)))
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.
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]."
715 ;; Clear out the markers we are throwing away.
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))
721 (xref-clear-marker-stack)
722 (setq tags-file-name nil
723 tags-location-ring (make-ring xref-marker-ring-length)
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
737 (funcall file-of-tag-function relative))
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."
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.
762 (let (current-table combined-table)
763 (message "Making tags completion table for %s..." buffer-file-name)
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.
772 (lambda (sym) (intern (symbol-name sym) combined-table))
774 (setq combined-table current-table))))
775 (message "Making tags completion table for %s...done"
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)))))
783 (defun tags-lazy-completion-table ()
784 (let ((buf (current-buffer)))
785 (lambda (string pred action)
786 (with-current-buffer buf
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
806 (pattern (funcall (or find-tag-default-function
807 (get major-mode 'find-tag-default-function)
808 #'find-tag-default)))
812 (forward-char (1- (length pattern)))
813 ;; The find-tag function might be overly optimistic.
814 (when (search-backward pattern nil t)
816 (forward-char (length pattern))
817 (list beg (point) (tags-lazy-completion-table) :exclusive 'no)))))))
819 (defun find-tag-tag (string)
820 "Read a tag name, with defaulting and completion."
821 (let* ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
822 tags-case-fold-search
824 (default (funcall (or find-tag-default-function
825 (get major-mode 'find-tag-default-function)
827 (spec (completing-read (if default
828 (format "%s (default %s): "
829 (substring string 0 (string-match "[ :]+\\'" string))
832 (tags-lazy-completion-table)
833 nil nil nil nil default)))
835 (or default (user-error "There is no default tag"))
839 "Last tag found by \\[find-tag].")
841 (defun find-tag-interactive (prompt &optional no-default)
842 "Get interactive arguments for tag functions.
843 The functions using this are `find-tag-noselect',
844 `find-tag-other-window', and `find-tag-regexp'."
845 (if (and current-prefix-arg last-tag)
846 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
851 (find-tag-tag prompt)))))
853 (defvar find-tag-history nil) ; Doc string?
856 (defvar etags-case-fold-search)
857 (defvar etags-syntax-table)
858 (defvar local-find-tag-hook)
861 (defun find-tag-noselect (tagname &optional next-p regexp-p)
862 "Find tag (in current tags table) whose name contains TAGNAME.
863 Returns the buffer containing the tag's definition and moves its point there,
864 but does not select the buffer.
865 The default for TAGNAME is the expression in the buffer near point.
867 If second arg NEXT-P is t (interactively, with prefix arg), search for
868 another tag that matches the last tagname or regexp used. When there are
869 multiple matches for a tag, more exact matches are found first. If NEXT-P
870 is the atom `-' (interactively, with prefix arg that is a negative number
871 or just \\[negative-argument]), pop back to the previous tag gone to.
873 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
875 A marker representing the point when this command is invoked is pushed
876 onto a ring and may be popped back to with \\[pop-tag-mark].
877 Contrast this with the ring of marks gone to by the command.
879 See documentation of variable `tags-file-name'."
880 (interactive (find-tag-interactive "Find tag: "))
882 (setq find-tag-history (cons tagname find-tag-history))
883 ;; Save the current buffer's value of `find-tag-hook' before
884 ;; selecting the tags table buffer. For the same reason, save value
885 ;; of `tags-file-name' in case it has a buffer-local value.
886 (let ((local-find-tag-hook find-tag-hook))
888 ;; Pop back to a previous location.
889 (if (ring-empty-p tags-location-ring)
890 (user-error "No previous tag locations")
891 (let ((marker (ring-remove tags-location-ring 0)))
893 ;; Move to the saved location.
894 (set-buffer (or (marker-buffer marker)
895 (error "The marked buffer has been deleted")))
896 (goto-char (marker-position marker))
897 ;; Kill that marker so it doesn't slow down editing.
898 (set-marker marker nil nil)
899 ;; Run the user's hook. Do we really want to do this for pop?
900 (run-hooks 'local-find-tag-hook))))
901 ;; Record whence we came.
902 (xref-push-marker-stack)
903 (if (and next-p last-tag)
904 ;; Find the same table we last used.
905 (visit-tags-table-buffer 'same)
906 ;; Pick a table to use.
907 (visit-tags-table-buffer)
908 ;; Record TAGNAME for a future call with NEXT-P non-nil.
909 (setq last-tag tagname))
910 ;; Record the location so we can pop back to it later.
911 (let ((marker (make-marker)))
913 ;; find-tag-in-order does the real work.
915 (if (and next-p last-tag) last-tag tagname)
917 find-tag-regexp-search-function
918 find-tag-search-function)
920 find-tag-regexp-tag-order
923 find-tag-regexp-next-line-after-failure-p
924 find-tag-next-line-after-failure-p)
925 (if regexp-p "matching" "containing")
926 (or (not next-p) (not last-tag)))
927 (set-marker marker (point))
928 (run-hooks 'local-find-tag-hook)
929 (ring-insert tags-location-ring marker)
930 (current-buffer))))))
933 (defun find-tag (tagname &optional next-p regexp-p)
934 "Find tag (in current tags table) whose name contains TAGNAME.
935 Select the buffer containing the tag's definition, and move point there.
936 The default for TAGNAME is the expression in the buffer around or before point.
938 If second arg NEXT-P is t (interactively, with prefix arg), search for
939 another tag that matches the last tagname or regexp used. When there are
940 multiple matches for a tag, more exact matches are found first. If NEXT-P
941 is the atom `-' (interactively, with prefix arg that is a negative number
942 or just \\[negative-argument]), pop back to the previous tag gone to.
944 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
946 A marker representing the point when this command is invoked is pushed
947 onto a ring and may be popped back to with \\[pop-tag-mark].
948 Contrast this with the ring of marks gone to by the command.
950 See documentation of variable `tags-file-name'."
951 (declare (obsolete xref-find-definitions "25.1"))
952 (interactive (find-tag-interactive "Find tag: "))
953 (let* ((buf (find-tag-noselect tagname next-p regexp-p))
954 (pos (with-current-buffer buf (point))))
956 (switch-to-buffer buf)
957 (error (pop-to-buffer buf)))
961 (defun find-tag-other-window (tagname &optional next-p regexp-p)
962 "Find tag (in current tags table) whose name contains TAGNAME.
963 Select the buffer containing the tag's definition in another window, and
964 move point there. The default for TAGNAME is the expression in the buffer
965 around or before point.
967 If second arg NEXT-P is t (interactively, with prefix arg), search for
968 another tag that matches the last tagname or regexp used. When there are
969 multiple matches for a tag, more exact matches are found first. If NEXT-P
970 is negative (interactively, with prefix arg that is a negative number or
971 just \\[negative-argument]), pop back to the previous tag gone to.
973 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
975 A marker representing the point when this command is invoked is pushed
976 onto a ring and may be popped back to with \\[pop-tag-mark].
977 Contrast this with the ring of marks gone to by the command.
979 See documentation of variable `tags-file-name'."
980 (declare (obsolete xref-find-definitions-other-window "25.1"))
981 (interactive (find-tag-interactive "Find tag other window: "))
983 ;; This hair is to deal with the case where the tag is found in the
984 ;; selected window's buffer; without the hair, point is moved in both
985 ;; windows. To prevent this, we save the selected window's point before
986 ;; doing find-tag-noselect, and restore it after.
987 (let* ((window-point (window-point))
988 (tagbuf (find-tag-noselect tagname next-p regexp-p))
989 (tagpoint (progn (set-buffer tagbuf) (point))))
990 (set-window-point (prog1
992 (switch-to-buffer-other-window tagbuf)
993 ;; We have to set this new window's point; it
994 ;; might already have been displaying a
995 ;; different portion of tagbuf, in which case
996 ;; switch-to-buffer-other-window doesn't set
997 ;; the window's point from the buffer.
998 (set-window-point (selected-window) tagpoint))
1002 (defun find-tag-other-frame (tagname &optional next-p)
1003 "Find tag (in current tags table) whose name contains TAGNAME.
1004 Select the buffer containing the tag's definition in another frame, and
1005 move point there. The default for TAGNAME is the expression in the buffer
1006 around or before point.
1008 If second arg NEXT-P is t (interactively, with prefix arg), search for
1009 another tag that matches the last tagname or regexp used. When there are
1010 multiple matches for a tag, more exact matches are found first. If NEXT-P
1011 is negative (interactively, with prefix arg that is a negative number or
1012 just \\[negative-argument]), pop back to the previous tag gone to.
1014 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
1016 A marker representing the point when this command is invoked is pushed
1017 onto a ring and may be popped back to with \\[pop-tag-mark].
1018 Contrast this with the ring of marks gone to by the command.
1020 See documentation of variable `tags-file-name'."
1021 (declare (obsolete xref-find-definitions-other-frame "25.1"))
1022 (interactive (find-tag-interactive "Find tag other frame: "))
1023 (let ((pop-up-frames t))
1024 (find-tag-other-window tagname next-p)))
1027 (defun find-tag-regexp (regexp &optional next-p other-window)
1028 "Find tag (in current tags table) whose name matches REGEXP.
1029 Select the buffer containing the tag's definition and move point there.
1031 If second arg NEXT-P is t (interactively, with prefix arg), search for
1032 another tag that matches the last tagname or regexp used. When there are
1033 multiple matches for a tag, more exact matches are found first. If NEXT-P
1034 is negative (interactively, with prefix arg that is a negative number or
1035 just \\[negative-argument]), pop back to the previous tag gone to.
1037 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
1039 A marker representing the point when this command is invoked is pushed
1040 onto a ring and may be popped back to with \\[pop-tag-mark].
1041 Contrast this with the ring of marks gone to by the command.
1043 See documentation of variable `tags-file-name'."
1044 (declare (obsolete xref-find-apropos "25.1"))
1045 (interactive (find-tag-interactive "Find tag regexp: " t))
1046 ;; We go through find-tag-other-window to do all the display hair there.
1047 (funcall (if other-window 'find-tag-other-window 'find-tag)
1051 (defalias 'pop-tag-mark 'xref-pop-marker-stack)
1054 (defvar tag-lines-already-matched nil
1055 "Matches remembered between calls.") ; Doc string: calls to what?
1057 (defun find-tag-in-order (pattern
1060 next-line-after-failure-p
1063 "Internal tag-finding function.
1064 PATTERN is a string to pass to arg SEARCH-FORWARD-FUNC, and to any
1065 member of the function list ORDER. If ORDER is nil, use saved state
1066 to continue a previous search.
1068 Arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
1069 point should be moved to the next line.
1071 Arg MATCHING is a string, an English `-ing' word, to be used in an
1073 ;; Algorithm is as follows:
1074 ;; For each qualifier-func in ORDER, go to beginning of tags file, and
1075 ;; perform inner loop: for each naive match for PATTERN found using
1076 ;; SEARCH-FORWARD-FUNC, qualify the naive match using qualifier-func. If
1077 ;; it qualifies, go to the specified line in the specified source file
1078 ;; and return. Qualified matches are remembered to avoid repetition.
1079 ;; State is saved so that the loop can be continued.
1080 (let (file ;name of file containing tag
1081 tag-info ;where to find the tag in FILE
1084 (match-marker (make-marker))
1086 (case-fold-search (if (memq tags-case-fold-search '(nil t))
1087 tags-case-fold-search
1093 ;; This is the start of a search for a fresh tag.
1094 ;; Clear the list of tags matched by the previous search.
1095 ;; find-tag-noselect has already put us in the first tags table
1096 ;; buffer before we got called.
1097 (setq tag-lines-already-matched nil)
1098 ;; Continuing to search for the tag specified last time.
1099 ;; tag-lines-already-matched lists locations matched in previous
1100 ;; calls so we don't visit the same tag twice if it matches twice
1101 ;; during two passes with different qualification predicates.
1102 ;; Switch to the current tags table buffer.
1103 (visit-tags-table-buffer 'same))
1105 ;; Get a qualified match.
1106 (catch 'qualified-match-found
1108 ;; Iterate over the list of tags tables.
1109 (while (or first-table
1110 (visit-tags-table-buffer t))
1112 (and first-search first-table
1113 ;; Start at beginning of tags file.
1114 (goto-char (point-min)))
1116 (setq first-table nil)
1118 ;; Iterate over the list of ordering predicates.
1120 (while (funcall search-forward-func pattern nil t)
1121 ;; Naive match found. Qualify the match.
1122 (and (funcall (car order) pattern)
1123 ;; Make sure it is not a previous qualified match.
1124 (not (member (set-marker match-marker (point-at-bol))
1125 tag-lines-already-matched))
1126 (throw 'qualified-match-found nil))
1127 (if next-line-after-failure-p
1129 ;; Try the next flavor of match.
1130 (setq order (cdr order))
1131 (goto-char (point-min)))
1132 (setq order tag-order))
1133 ;; We throw out on match, so only get here if there were no matches.
1134 ;; Clear out the markers we use to avoid duplicate matches so they
1135 ;; don't slow down editing and are immediately available for GC.
1136 (while tag-lines-already-matched
1137 (set-marker (car tag-lines-already-matched) nil nil)
1138 (setq tag-lines-already-matched (cdr tag-lines-already-matched)))
1139 (set-marker match-marker nil nil)
1140 (user-error "No %stags %s %s" (if first-search "" "more ")
1143 ;; Found a tag; extract location info.
1145 (setq tag-lines-already-matched (cons match-marker
1146 tag-lines-already-matched))
1147 ;; Expand the filename, using the tags table buffer's default-directory.
1148 ;; We should be able to search for file-name backwards in file-of-tag:
1149 ;; the beginning-of-line is ok except when positioned on a "file-name" tag.
1150 (setq file (expand-file-name
1151 (if (memq (car order) '(tag-exact-file-name-match-p
1152 tag-file-name-match-p
1153 tag-partial-file-name-match-p))
1154 (save-excursion (forward-line 1)
1157 tag-info (funcall snarf-tag-function))
1159 ;; Get the local value in the tags table buffer before switching buffers.
1160 (setq goto-func goto-tag-location-function)
1161 (tag-find-file-of-tag-noselect file)
1164 (funcall goto-func tag-info)
1166 ;; Return the buffer where the tag was found.
1169 (defun tag-find-file-of-tag-noselect (file)
1170 "Find the right line in the specified FILE."
1171 ;; If interested in compressed-files, search files with extensions.
1172 ;; Otherwise, search only the real file.
1173 (let* ((buffer-search-extensions (if auto-compression-mode
1174 tags-compression-info-list
1177 (file-search-extensions buffer-search-extensions))
1178 ;; search a buffer visiting the file with each possible extension
1179 ;; Note: there is a small inefficiency in find-buffer-visiting :
1180 ;; truename is computed even if not needed. Not too sure about this
1181 ;; but I suspect truename computation accesses the disk.
1182 ;; It is maybe a good idea to optimize this find-buffer-visiting.
1183 ;; An alternative would be to use only get-file-buffer
1184 ;; but this looks less "sure" to find the buffer for the file.
1185 (while (and (not the-buffer) buffer-search-extensions)
1186 (setq the-buffer (find-buffer-visiting (concat file (car buffer-search-extensions))))
1187 (setq buffer-search-extensions (cdr buffer-search-extensions)))
1188 ;; if found a buffer but file modified, ensure we re-read !
1189 (if (and the-buffer (not (verify-visited-file-modtime the-buffer)))
1190 (find-file-noselect (buffer-file-name the-buffer)))
1191 ;; if no buffer found, search for files with possible extensions on disk
1192 (while (and (not the-buffer) file-search-extensions)
1193 (if (not (file-exists-p (concat file (car file-search-extensions))))
1194 (setq file-search-extensions (cdr file-search-extensions))
1195 (setq the-buffer (find-file-noselect (concat file (car file-search-extensions))))))
1196 (if (not the-buffer)
1197 (if auto-compression-mode
1198 (error "File %s (with or without extensions %s) not found" file tags-compression-info-list)
1199 (error "File %s not found" file))
1200 (set-buffer the-buffer))))
1202 (defun tag-find-file-of-tag (file) ; Doc string?
1203 (let ((buf (tag-find-file-of-tag-noselect file)))
1205 (switch-to-buffer buf)
1206 (error (pop-to-buffer buf)))))
1208 ;; `etags' TAGS file format support.
1210 (defun etags-recognize-tags-table ()
1211 "If `etags-verify-tags-table', make buffer-local format variables.
1212 If current buffer is a valid etags TAGS file, then give it
1213 buffer-local values of tags table format variables."
1214 (and (etags-verify-tags-table)
1215 ;; It is annoying to flash messages on the screen briefly,
1216 ;; and this message is not useful. -- rms
1217 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
1218 (mapc (lambda (elt) (set (make-local-variable (car elt)) (cdr elt)))
1219 '((file-of-tag-function . etags-file-of-tag)
1220 (tags-table-files-function . etags-tags-table-files)
1221 (tags-completion-table-function . etags-tags-completion-table)
1222 (snarf-tag-function . etags-snarf-tag)
1223 (goto-tag-location-function . etags-goto-tag-location)
1224 (find-tag-regexp-search-function . re-search-forward)
1225 (find-tag-regexp-tag-order . (tag-re-match-p))
1226 (find-tag-regexp-next-line-after-failure-p . t)
1227 (find-tag-search-function . search-forward)
1228 (find-tag-tag-order . (tag-exact-file-name-match-p
1229 tag-file-name-match-p
1231 tag-implicit-name-match-p
1234 tag-partial-file-name-match-p
1236 (find-tag-next-line-after-failure-p . nil)
1237 (list-tags-function . etags-list-tags)
1238 (tags-apropos-function . etags-tags-apropos)
1239 (tags-included-tables-function . etags-tags-included-tables)
1240 (verify-tags-table-function . etags-verify-tags-table)
1243 (defun etags-verify-tags-table ()
1244 "Return non-nil if the current buffer is a valid etags TAGS file."
1245 ;; Use eq instead of = in case char-after returns nil.
1246 (eq (char-after (point-min)) ?\f))
1248 (defun etags-file-of-tag (&optional relative) ; Doc string?
1250 (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n")
1251 (let ((str (convert-standard-filename
1252 (buffer-substring (match-beginning 1) (match-end 1)))))
1255 (expand-file-name str (file-truename default-directory))))))
1258 (defun etags-tags-completion-table () ; Doc string?
1259 (let ((table (make-vector 511 0))
1261 (make-progress-reporter
1262 (format "Making tags completion table for %s..." buffer-file-name)
1263 (point-min) (point-max))))
1265 (goto-char (point-min))
1266 ;; This monster regexp matches an etags tag line.
1267 ;; \1 is the string to match;
1268 ;; \2 is not interesting;
1269 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
1270 ;; \4 is not interesting;
1271 ;; \5 is the explicitly-specified tag name.
1272 ;; \6 is the line to start searching at;
1273 ;; \7 is the char to start searching at.
1274 (while (re-search-forward
1275 "^\\(\\([^\177]+[^-a-zA-Z0-9_+*$:\177]+\\)?\
1276 \\([-a-zA-Z0-9_+*$?:]+\\)[^-a-zA-Z0-9_+*$?:\177]*\\)\177\
1277 \\(\\([^\n\001]+\\)\001\\)?\\([0-9]+\\)?,\\([0-9]+\\)?\n"
1279 (intern (prog1 (if (match-beginning 5)
1280 ;; There is an explicit tag name.
1281 (buffer-substring (match-beginning 5) (match-end 5))
1282 ;; No explicit tag name. Best guess.
1283 (buffer-substring (match-beginning 3) (match-end 3)))
1284 (progress-reporter-update progress-reporter (point)))
1288 (defun etags-snarf-tag (&optional use-explicit) ; Doc string?
1289 (let (tag-text line startpos explicit-start)
1292 (looking-at "\f\n"))
1293 ;; The match was for a source file name, not any tag within a file.
1294 ;; Give text of t, meaning to go exactly to the location we specify,
1295 ;; the beginning of the file.
1298 startpos (point-min))
1300 ;; Find the end of the tag and record the whole tag text.
1301 (search-forward "\177")
1302 (setq tag-text (buffer-substring (1- (point)) (point-at-bol)))
1303 ;; If use-explicit is non nil and explicit tag is present, use it as part of
1304 ;; return value. Else just skip it.
1305 (setq explicit-start (point))
1306 (when (and (search-forward "\001" (point-at-bol 2) t)
1308 (setq tag-text (buffer-substring explicit-start (1- (point)))))
1311 (if (looking-at "[0-9]")
1312 (setq line (string-to-number (buffer-substring
1314 (progn (skip-chars-forward "0-9")
1316 (search-forward ",")
1317 (if (looking-at "[0-9]")
1318 (setq startpos (string-to-number (buffer-substring
1320 (progn (skip-chars-forward "0-9")
1322 ;; Leave point on the next line of the tags file.
1324 (cons tag-text (cons line startpos))))
1326 (defun etags-goto-tag-location (tag-info)
1327 "Go to location of tag specified by TAG-INFO.
1328 TAG-INFO is a cons (TEXT LINE . POSITION).
1329 TEXT is the initial part of a line containing the tag.
1330 LINE is the line number.
1331 POSITION is the (one-based) char position of TEXT within the file.
1333 If TEXT is t, it means the tag refers to exactly LINE or POSITION,
1334 whichever is present, LINE having preference, no searching.
1335 Either LINE or POSITION can be nil. POSITION is used if present.
1337 If the tag isn't exactly at the given position, then look near that
1338 position using a search window that expands progressively until it
1339 hits the start of file."
1340 (let ((startpos (cdr (cdr tag-info)))
1341 (line (car (cdr tag-info)))
1343 (if (eq (car tag-info) t)
1345 (cond (line (progn (goto-char (point-min))
1346 (forward-line (1- line))))
1347 (startpos (goto-char startpos))
1348 (t (error "etags.el BUG: bogus direct file tag")))
1349 ;; This constant is 1/2 the initial search window.
1350 ;; There is no sense in making it too small,
1351 ;; since just going around the loop once probably
1352 ;; costs about as much as searching 2000 chars.
1355 pat (concat (if (eq selective-display t)
1356 "\\(^\\|\^m\\)" "^")
1357 (regexp-quote (car tag-info))))
1358 ;; The character position in the tags table is 0-origin.
1359 ;; Convert it to a 1-origin Emacs character position.
1360 (if startpos (setq startpos (1+ startpos)))
1361 ;; If no char pos was given, try the given line number.
1364 (setq startpos (progn (goto-char (point-min))
1365 (forward-line (1- line))
1368 (setq startpos (point-min)))
1369 ;; First see if the tag is right at the specified location.
1370 (goto-char startpos)
1371 (setq found (looking-at pat))
1372 (while (and (not found)
1374 (goto-char (- startpos offset))
1377 (re-search-forward pat (+ startpos offset) t)
1378 offset (* 3 offset))) ; expand search window
1380 (re-search-forward pat nil t)
1381 (user-error "Rerun etags: `%s' not found in %s"
1382 pat buffer-file-name)))
1383 ;; Position point at the right place
1384 ;; if the search string matched an extra Ctrl-m at the beginning.
1385 (and (eq selective-display t)
1388 (beginning-of-line)))
1390 (defun etags-list-tags (file) ; Doc string?
1391 (goto-char (point-min))
1392 (when (re-search-forward (concat "\f\n" "\\(" file "\\)" ",") nil t)
1393 (let ((path (save-excursion (forward-line 1) (file-of-tag)))
1394 ;; Get the local value in the tags table
1395 ;; buffer before switching buffers.
1396 (goto-func goto-tag-location-function)
1399 (while (not (or (eobp) (looking-at "\f")))
1400 ;; We used to use explicit tags when available, but the current goto-func
1401 ;; can only handle implicit tags.
1402 (setq tag-info (save-excursion (funcall snarf-tag-function nil))
1404 pt (with-current-buffer standard-output (point)))
1406 (when (= (aref tag 0) ?\() (princ " ...)"))
1407 (with-current-buffer standard-output
1408 (make-text-button pt (point)
1411 'goto-func goto-func
1412 'action (lambda (button)
1413 (let ((tag-info (button-get button 'tag-info))
1414 (goto-func (button-get button 'goto-func)))
1415 (tag-find-file-of-tag (button-get button 'file-path))
1417 (funcall goto-func tag-info)))
1425 (defmacro tags-with-face (face &rest body)
1426 "Execute BODY, give output to `standard-output' face FACE."
1427 (let ((pp (make-symbol "start")))
1428 `(let ((,pp (with-current-buffer standard-output (point))))
1430 (put-text-property ,pp (with-current-buffer standard-output (point))
1431 'face ,face standard-output))))
1433 (defun etags-tags-apropos-additional (regexp)
1434 "Display tags matching REGEXP from `tags-apropos-additional-actions'."
1435 (with-current-buffer standard-output
1436 (dolist (oba tags-apropos-additional-actions)
1438 (tags-with-face 'highlight (princ (car oba)))
1440 (let* ((beg (point))
1441 (symbs (car (cddr oba)))
1442 (ins-symb (lambda (sy)
1443 (let ((sn (symbol-name sy)))
1444 (when (string-match regexp sn)
1445 (make-text-button (point)
1446 (progn (princ sy) (point))
1447 'action-internal(cadr oba)
1448 'action (lambda (button) (funcall
1449 (button-get button 'action-internal)
1450 (button-get button 'item)))
1456 (when (symbolp symbs)
1458 (setq symbs (symbol-value symbs))
1459 (insert "symbol `" (symbol-name symbs) "' has no value\n")
1462 (mapatoms ins-symb symbs)
1464 (funcall ins-symb (car sy))))
1465 (sort-lines nil beg (point))))))
1467 (defun etags-tags-apropos (string) ; Doc string?
1468 (when tags-apropos-verbose
1469 (princ "Tags in file `")
1470 (tags-with-face 'highlight (princ buffer-file-name))
1472 (goto-char (point-min))
1473 (let ((progress-reporter (make-progress-reporter
1474 (format "Making tags apropos buffer for `%s'..."
1476 (point-min) (point-max))))
1477 (while (re-search-forward string nil t)
1478 (progress-reporter-update progress-reporter (point))
1481 (let* ( ;; Get the local value in the tags table
1482 ;; buffer before switching buffers.
1483 (goto-func goto-tag-location-function)
1484 (tag-info (save-excursion (funcall snarf-tag-function)))
1485 (tag (if (eq t (car tag-info)) nil (car tag-info)))
1486 (file-path (save-excursion (if tag (file-of-tag)
1487 (save-excursion (forward-line 1)
1489 (file-label (if tag (file-of-tag t)
1490 (save-excursion (forward-line 1)
1492 (pt (with-current-buffer standard-output (point))))
1495 (princ (format "[%s]: " file-label))
1497 (when (= (aref tag 0) ?\() (princ " ...)"))
1498 (with-current-buffer standard-output
1499 (make-text-button pt (point)
1501 'file-path file-path
1502 'goto-func goto-func
1503 'action (lambda (button)
1504 (let ((tag-info (button-get button 'tag-info))
1505 (goto-func (button-get button 'goto-func)))
1506 (tag-find-file-of-tag (button-get button 'file-path))
1508 (funcall goto-func tag-info)))
1512 (princ (format "- %s" file-label))
1513 (with-current-buffer standard-output
1514 (make-text-button pt (point)
1515 'file-path file-path
1516 'action (lambda (button)
1517 (tag-find-file-of-tag (button-get button 'file-path))
1518 ;; Get the local value in the tags table
1519 ;; buffer before switching buffers.
1520 (goto-char (point-min)))
1527 (when tags-apropos-verbose (princ "\n")))
1529 (defun etags-tags-table-files () ; Doc string?
1532 (goto-char (point-min))
1533 (while (search-forward "\f\n" nil t)
1536 (skip-chars-backward "^," beg)
1537 (or (looking-at "include$")
1538 (push (convert-standard-filename
1539 (buffer-substring beg (1- (point))))
1543 ;; FIXME? Should this save-excursion?
1544 (defun etags-tags-included-tables () ; Doc string?
1547 (goto-char (point-min))
1548 (while (search-forward "\f\n" nil t)
1551 (skip-chars-backward "^," beg)
1552 (when (looking-at "include$")
1553 ;; Expand in the default-directory of the tags table buffer.
1554 (push (expand-file-name (convert-standard-filename
1555 (buffer-substring beg (1- (point)))))
1559 ;; Empty tags file support.
1561 (defun tags-recognize-empty-tags-table ()
1562 "Return non-nil if current buffer is empty.
1563 If empty, make buffer-local values of the tags table format variables
1565 (and (zerop (buffer-size))
1566 (mapc (lambda (sym) (set (make-local-variable sym) 'ignore))
1567 '(tags-table-files-function
1568 tags-completion-table-function
1569 find-tag-regexp-search-function
1570 find-tag-search-function
1571 tags-apropos-function
1572 tags-included-tables-function))
1573 (set (make-local-variable 'verify-tags-table-function)
1574 (lambda () (zerop (buffer-size))))))
1576 ;; Match qualifier functions for tagnames.
1577 ;; These functions assume the etags file format defined in etc/ETAGS.EBNF.
1579 ;; This might be a neat idea, but it's too hairy at the moment.
1580 ;;(defmacro tags-with-syntax (&rest body)
1581 ;; `(with-syntax-table
1582 ;; (with-current-buffer (find-file-noselect (file-of-tag))
1585 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
1587 ;; exact file name match, i.e. searched tag must match complete file
1588 ;; name including directories parts if there are some.
1589 (defun tag-exact-file-name-match-p (tag)
1590 "Return non-nil if TAG matches complete file name.
1591 Any directory part of the file name is also matched."
1592 (and (looking-at ",[0-9\n]")
1593 (save-excursion (backward-char (+ 2 (length tag)))
1594 (looking-at "\f\n"))))
1596 ;; file name match as above, but searched tag must match the file
1597 ;; name not including the directories if there are some.
1598 (defun tag-file-name-match-p (tag)
1599 "Return non-nil if TAG matches file name, excluding directory part."
1600 (and (looking-at ",[0-9\n]")
1601 (save-excursion (backward-char (1+ (length tag)))
1604 ;; this / to detect we are after a directory separator is ok for unix,
1605 ;; is there a variable that contains the regexp for directory separator
1606 ;; on whatever operating system ?
1607 ;; Looks like ms-win will lose here :).
1609 ;; t if point is at a tag line that matches TAG exactly.
1610 ;; point should be just after a string that matches TAG.
1611 (defun tag-exact-match-p (tag)
1612 "Return non-nil if current tag line matches TAG exactly.
1613 Point should be just after a string that matches TAG."
1614 ;; The match is really exact if there is an explicit tag name.
1615 (or (and (eq (char-after (point)) ?\001)
1616 (eq (char-after (- (point) (length tag) 1)) ?\177))
1617 ;; We are not on the explicit tag name, but perhaps it follows.
1618 (looking-at (concat "[^\177\n]*\177" (regexp-quote tag) "\001"))))
1620 ;; t if point is at a tag line that has an implicit name.
1621 ;; point should be just after a string that matches TAG.
1622 (defun tag-implicit-name-match-p (tag)
1623 "Return non-nil if current tag line has an implicit name.
1624 Point should be just after a string that matches TAG."
1625 ;; Look at the comment of the make_tag function in lib-src/etags.c for
1626 ;; a textual description of the four rules.
1627 (and (string-match "^[^ \t()=,;]+$" tag) ;rule #1
1628 (looking-at "[ \t()=,;]?\177") ;rules #2 and #4
1630 (backward-char (1+ (length tag)))
1631 (looking-at "[\n \t()=,;]")))) ;rule #3
1633 ;; t if point is at a tag line that matches TAG as a symbol.
1634 ;; point should be just after a string that matches TAG.
1635 (defun tag-symbol-match-p (tag)
1636 "Return non-nil if current tag line matches TAG as a symbol.
1637 Point should be just after a string that matches TAG."
1638 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1640 (backward-char (1+ (length tag)))
1641 (and (looking-at "\\Sw") (looking-at "\\S_")))))
1643 ;; t if point is at a tag line that matches TAG as a word.
1644 ;; point should be just after a string that matches TAG.
1645 (defun tag-word-match-p (tag)
1646 "Return non-nil if current tag line matches TAG as a word.
1647 Point should be just after a string that matches TAG."
1648 (and (looking-at "\\b.*\177")
1649 (save-excursion (backward-char (length tag))
1650 (looking-at "\\b"))))
1652 ;; partial file name match, i.e. searched tag must match a substring
1653 ;; of the file name (potentially including a directory separator).
1654 (defun tag-partial-file-name-match-p (_tag)
1655 "Return non-nil if current tag matches file name.
1656 This is a substring match, and it can include directory separators.
1657 Point should be just after a string that matches TAG."
1658 (and (looking-at ".*,[0-9\n]")
1659 (save-excursion (beginning-of-line)
1661 (looking-at "\f\n"))))
1663 ;; t if point is in a tag line with a tag containing TAG as a substring.
1664 (defun tag-any-match-p (_tag)
1665 "Return non-nil if current tag line contains TAG as a substring."
1666 (looking-at ".*\177"))
1668 ;; t if point is at a tag line that matches RE as a regexp.
1669 (defun tag-re-match-p (re)
1670 "Return non-nil if current tag line matches regexp RE."
1673 (let ((bol (point)))
1674 (and (search-forward "\177" (line-end-position) t)
1675 (re-search-backward re bol t)))))
1677 (defcustom tags-loop-revert-buffers nil
1678 "Non-nil means tags-scanning loops should offer to reread changed files.
1679 These loops normally read each file into Emacs, but when a file
1680 is already visited, they use the existing buffer.
1681 When this flag is non-nil, they offer to revert the existing buffer
1682 in the case where the file has changed since you visited it."
1687 (defun next-file (&optional initialize novisit)
1688 "Select next file among files in current tags table.
1690 A first argument of t (prefix arg, if interactive) initializes to the
1691 beginning of the list of files in the tags table. If the argument is
1692 neither nil nor t, it is evalled to initialize the list of files.
1694 Non-nil second argument NOVISIT means use a temporary buffer
1695 to save time and avoid uninteresting warnings.
1697 Value is nil if the file was already visited;
1698 if the file was newly read in, the value is the filename."
1699 ;; Make the interactive arg t if there was any prefix arg.
1700 (interactive (list (if current-prefix-arg t)))
1701 (cond ((not initialize)
1702 ;; Not the first run.
1705 ;; Initialize the list from the tags table.
1707 ;; Visit the tags table buffer to get its list of files.
1708 (visit-tags-table-buffer)
1709 ;; Copy the list so we can setcdr below, and expand the file
1710 ;; names while we are at it, in this buffer's default directory.
1711 (setq next-file-list (mapcar 'expand-file-name (tags-table-files)))
1712 ;; Iterate over all the tags table files, collecting
1713 ;; a complete list of referenced file names.
1714 (while (visit-tags-table-buffer t)
1715 ;; Find the tail of the working list and chain on the new
1716 ;; sublist for this tags table.
1717 (let ((tail next-file-list))
1719 (setq tail (cdr tail)))
1720 ;; Use a copy so the next loop iteration will not modify the
1721 ;; list later returned by (tags-table-files).
1723 (setcdr tail (mapcar 'expand-file-name (tags-table-files)))
1724 (setq next-file-list (mapcar 'expand-file-name
1725 (tags-table-files))))))))
1727 ;; Initialize the list by evalling the argument.
1728 (setq next-file-list (eval initialize))))
1729 (unless next-file-list
1731 (get-buffer " *next-file*")
1732 (kill-buffer " *next-file*"))
1733 (user-error "All files processed"))
1734 (let* ((next (car next-file-list))
1735 (buffer (get-file-buffer next))
1737 ;; Advance the list before trying to find the file.
1738 ;; If we get an error finding the file, don't get stuck on it.
1739 (setq next-file-list (cdr next-file-list))
1740 ;; Optionally offer to revert buffers
1741 ;; if the files have changed on disk.
1742 (and buffer tags-loop-revert-buffers
1743 (not (verify-visited-file-modtime buffer))
1746 (if (buffer-modified-p buffer)
1747 "File %s changed on disk. Discard your edits? "
1748 "File %s changed on disk. Reread from disk? ")
1750 (with-current-buffer buffer
1751 (revert-buffer t t)))
1752 (if (not (and new novisit))
1753 (find-file next novisit)
1754 ;; Like find-file, but avoids random warning messages.
1755 (switch-to-buffer (get-buffer-create " *next-file*"))
1756 (kill-all-local-variables)
1759 (insert-file-contents new nil))
1762 (defvar tags-loop-operate nil
1763 "Form for `tags-loop-continue' to eval to change one file.")
1765 (defvar tags-loop-scan
1767 (substitute-command-keys
1768 "No \\[tags-search] or \\[tags-query-replace] in progress"))
1769 "Form for `tags-loop-continue' to eval to scan one file.
1770 If it returns non-nil, this file needs processing by evalling
1771 \`tags-loop-operate'. Otherwise, move on to the next file.")
1773 (defun tags-loop-eval (form)
1774 "Evaluate FORM and return its result.
1775 Bind `case-fold-search' during the evaluation, depending on the value of
1776 `tags-case-fold-search'."
1777 (let ((case-fold-search (if (memq tags-case-fold-search '(t nil))
1778 tags-case-fold-search
1784 (defun tags-loop-continue (&optional first-time)
1785 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1786 Used noninteractively with non-nil argument to begin such a command (the
1787 argument is passed to `next-file', which see).
1789 Two variables control the processing we do on each file: the value of
1790 `tags-loop-scan' is a form to be executed on each file to see if it is
1791 interesting (it returns non-nil if so) and `tags-loop-operate' is a form to
1792 evaluate to operate on an interesting file. If the latter evaluates to
1793 nil, we exit; otherwise we scan the next file."
1794 (declare (obsolete "use `xref-find-definitions' interface instead." "25.1"))
1797 ;; Non-nil means we have finished one file
1798 ;; and should not scan it again.
1804 ;; Scan files quickly for the first or next interesting one.
1805 ;; This starts at point in the current buffer.
1806 (while (or first-time file-finished
1809 (not (tags-loop-eval tags-loop-scan))))
1810 ;; If nothing was found in the previous file, and
1811 ;; that file isn't in a temp buffer, restore point to
1813 (when original-point
1814 (goto-char original-point))
1816 (setq file-finished nil)
1817 (setq new (next-file first-time t))
1819 ;; If NEW is non-nil, we got a temp buffer,
1820 ;; and NEW is the file name.
1822 (and (not first-time)
1823 (> baud-rate search-slow-speed)
1825 (message "Scanning file %s..." (or new buffer-file-name)))
1827 (setq first-time nil)
1828 (setq original-point (if new nil (point)))
1829 (goto-char (point-min)))
1831 ;; If we visited it in a temp buffer, visit it now for real.
1833 (let ((pos (point)))
1835 (set-buffer (find-file-noselect new))
1836 (setq new nil) ;No longer in a temp buffer.
1839 (push-mark original-point t))
1841 (switch-to-buffer (current-buffer))
1843 ;; Now operate on the file.
1844 ;; If value is non-nil, continue to scan the next file.
1845 (tags-loop-eval tags-loop-operate))
1846 (setq file-finished t))
1848 (null tags-loop-operate)
1849 (message "Scanning file %s...found" buffer-file-name))))
1852 (defun tags-search (regexp &optional file-list-form)
1853 "Search through all files listed in tags table for match for REGEXP.
1854 Stops when a match is found.
1855 To continue searching for next match, use command \\[tags-loop-continue].
1857 If FILE-LIST-FORM is non-nil, it should be a form that, when
1858 evaluated, will return a list of file names. The search will be
1859 restricted to these files.
1861 Also see the documentation of the `tags-file-name' variable."
1862 (interactive "sTags search (regexp): ")
1863 (if (and (equal regexp "")
1864 (eq (car tags-loop-scan) 're-search-forward)
1865 (null tags-loop-operate))
1866 ;; Continue last tags-search as if by M-,.
1867 (tags-loop-continue nil)
1868 (setq tags-loop-scan `(re-search-forward ',regexp nil t)
1869 tags-loop-operate nil)
1870 (tags-loop-continue (or file-list-form t))))
1873 (defun tags-query-replace (from to &optional delimited file-list-form)
1874 "Do `query-replace-regexp' of FROM with TO on all files listed in tags table.
1875 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1876 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
1877 with the command \\[tags-loop-continue].
1878 Fourth arg FILE-LIST-FORM non-nil means initialize the replacement loop.
1879 Fifth and sixth arguments START and END are accepted, for compatibility
1880 with `query-replace-regexp', and ignored.
1882 If FILE-LIST-FORM is non-nil, it is a form to evaluate to
1883 produce the list of files to search.
1885 See also the documentation of the variable `tags-file-name'."
1886 (interactive (query-replace-read-args "Tags query replace (regexp)" t t))
1887 (setq tags-loop-scan `(let ,(unless (equal from (downcase from))
1888 '((case-fold-search nil)))
1889 (if (re-search-forward ',from nil t)
1890 ;; When we find a match, move back
1891 ;; to the beginning of it so perform-replace
1893 (goto-char (match-beginning 0))))
1894 tags-loop-operate `(perform-replace ',from ',to t t ',delimited
1895 nil multi-query-replace-map))
1896 (tags-loop-continue (or file-list-form t)))
1898 (defun tags-complete-tags-table-file (string predicate what) ; Doc string?
1900 ;; If we need to ask for the tag table, allow that.
1901 (let ((enable-recursive-minibuffers t))
1902 (visit-tags-table-buffer))
1904 (all-completions string (tags-table-files) predicate)
1905 (try-completion string (tags-table-files) predicate))))
1908 (defun list-tags (file &optional _next-match)
1909 "Display list of tags in file FILE.
1910 This searches only the first table in the list, and no included tables.
1911 FILE should be as it appeared in the `etags' command, usually without a
1912 directory specification."
1913 (interactive (list (completing-read "List tags in file: "
1914 'tags-complete-tags-table-file
1916 (with-output-to-temp-buffer "*Tags List*"
1917 (princ "Tags in file `")
1918 (tags-with-face 'highlight (princ file))
1921 (let ((first-time t)
1923 (while (visit-tags-table-buffer (not first-time))
1924 (setq first-time nil)
1925 (if (funcall list-tags-function file)
1928 (user-error "File %s not in current tags tables" file)))))
1929 (with-current-buffer "*Tags List*"
1933 (setq buffer-read-only t)))
1936 (defun tags-apropos (regexp)
1937 "Display list of all tags in tags table REGEXP matches."
1938 (declare (obsolete xref-find-apropos "25.1"))
1939 (interactive "sTags apropos (regexp): ")
1940 (with-output-to-temp-buffer "*Tags List*"
1941 (princ "Click mouse-2 to follow tags.\n\nTags matching regexp `")
1942 (tags-with-face 'highlight (princ regexp))
1945 (let ((first-time t))
1946 (while (visit-tags-table-buffer (not first-time))
1947 (setq first-time nil)
1948 (funcall tags-apropos-function regexp))))
1949 (etags-tags-apropos-additional regexp))
1950 (with-current-buffer "*Tags List*"
1951 (eval-and-compile (require 'apropos))
1953 ;; apropos-mode is derived from fundamental-mode and it kills
1954 ;; all local variables.
1955 (setq buffer-read-only t)))
1957 ;; XXX Kludge interface.
1959 (define-button-type 'tags-select-tags-table
1960 'action 'select-tags-table-select
1962 'help-echo "RET, t or mouse-2: select tags table")
1964 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1966 (defun select-tags-table ()
1967 "Select a tags table file from a menu of those you have already used.
1968 The list of tags tables to select from is stored in `tags-table-set-list';
1969 see the doc of that variable if you want to add names to the list."
1971 (pop-to-buffer "*Tags Table List*")
1972 (setq buffer-read-only nil
1975 (let ((set-list tags-table-set-list)
1978 (when tags-table-list
1979 (setq desired-point (point-marker))
1981 (princ (mapcar 'abbreviate-file-name tags-table-list) (current-buffer))
1982 (make-text-button b (point) 'type 'tags-select-tags-table
1983 'etags-table (car tags-table-list))
1986 (unless (eq (car set-list) tags-table-list)
1988 (princ (mapcar 'abbreviate-file-name (car set-list)) (current-buffer))
1989 (make-text-button b (point) 'type 'tags-select-tags-table
1990 'etags-table (car (car set-list)))
1992 (setq set-list (cdr set-list)))
1993 (when tags-file-name
1995 (setq desired-point (point-marker)))
1997 (insert (abbreviate-file-name tags-file-name))
1998 (make-text-button b (point) 'type 'tags-select-tags-table
1999 'etags-table tags-file-name)
2001 (setq set-list (delete tags-file-name
2002 (apply 'nconc (cons (copy-sequence tags-table-list)
2003 (mapcar 'copy-sequence
2004 tags-table-set-list)))))
2007 (insert (abbreviate-file-name (car set-list)))
2008 (make-text-button b (point) 'type 'tags-select-tags-table
2009 'etags-table (car set-list))
2011 (setq set-list (delete (car set-list) set-list)))
2012 (goto-char (point-min))
2013 (insert-before-markers
2014 "Type `t' to select a tags table or set of tags tables:\n\n")
2016 (goto-char desired-point))
2017 (set-window-start (selected-window) 1 t))
2018 (set-buffer-modified-p nil)
2019 (select-tags-table-mode))
2021 (defvar select-tags-table-mode-map ; Doc string?
2022 (let ((map (make-sparse-keymap)))
2023 (set-keymap-parent map button-buffer-map)
2024 (define-key map "t" 'push-button)
2025 (define-key map " " 'next-line)
2026 (define-key map "\^?" 'previous-line)
2027 (define-key map "n" 'next-line)
2028 (define-key map "p" 'previous-line)
2029 (define-key map "q" 'select-tags-table-quit)
2032 (define-derived-mode select-tags-table-mode special-mode "Select Tags Table"
2033 "Major mode for choosing a current tags table among those already loaded."
2034 (setq buffer-read-only t))
2036 (defun select-tags-table-select (button)
2037 "Select the tags table named on this line."
2038 (interactive (list (or (button-at (line-beginning-position))
2039 (error "No tags table on current line"))))
2040 (let ((name (button-get button 'etags-table)))
2041 (visit-tags-table name)
2042 (select-tags-table-quit)
2043 (message "Tags table now %s" name)))
2045 (defun select-tags-table-quit ()
2046 "Kill the buffer and delete the selected window."
2048 (quit-window t (selected-window)))
2051 (defun complete-tag ()
2052 "Perform tags completion on the text around point.
2053 Completes to the set of names listed in the current tags table.
2054 The string to complete is chosen in the same way as the default
2055 for \\[find-tag] (which see)."
2060 (substitute-command-keys
2061 "No tags table loaded; try \\[visit-tags-table]")))
2062 (let ((comp-data (tags-completion-at-point-function)))
2063 (if (null comp-data)
2064 (user-error "Nothing to complete")
2065 (completion-in-region (car comp-data) (cadr comp-data)
2067 (plist-get (nthcdr 3 comp-data) :predicate)))))
2072 ;; Stop searching if we find more than xref-limit matches, as the xref
2073 ;; infrastructure is not designed to handle very long lists.
2074 ;; Switching to some kind of lazy list might be better, but hopefully
2075 ;; we hit the limit rarely.
2076 (defconst etags--xref-limit 1000)
2078 (defvar etags-xref-find-definitions-tag-order '(tag-exact-match-p
2079 tag-implicit-name-match-p)
2080 "Tag order used in `etags-xref-find' to look for definitions.")
2083 (defun etags-xref-find (action id)
2085 (`definitions (etags--xref-find-definitions id))
2086 (`references (etags--xref-find-matches id 'symbol))
2087 (`matches (etags--xref-find-matches id 'regexp))
2088 (`apropos (etags--xref-find-definitions id t))))
2090 (defun etags--xref-find-matches (input kind)
2091 (let ((dirs (if tags-table-list
2092 (mapcar #'file-name-directory tags-table-list)
2093 ;; If no tags files are loaded, prompt for the dir.
2094 (list (read-directory-name "In directory: " nil nil t)))))
2097 (xref-collect-matches input dir kind))
2100 (defun etags--xref-find-definitions (pattern &optional regexp?)
2101 ;; This emulates the behaviour of `find-tag-in-order' but instead of
2102 ;; returning one match at a time all matches are returned as list.
2103 ;; NOTE: find-tag-tag-order is typically a buffer-local variable.
2106 (search-fun (if regexp? #'re-search-forward #'search-forward))
2107 (marks (make-hash-table :test 'equal))
2108 (case-fold-search (if (memq tags-case-fold-search '(nil t))
2109 tags-case-fold-search
2112 (while (visit-tags-table-buffer (not first-time))
2113 (setq first-time nil)
2114 (dolist (order-fun (cond (regexp? find-tag-regexp-tag-order)
2115 (t etags-xref-find-definitions-tag-order)))
2116 (goto-char (point-min))
2117 (while (and (funcall search-fun pattern nil t)
2118 (< (hash-table-count marks) etags--xref-limit))
2119 (when (funcall order-fun pattern)
2121 (pcase-let* ((tag-info (etags-snarf-tag))
2122 (`(,hint ,line . _) tag-info))
2123 (unless (eq hint t) ; hint==t if we are in a filename line
2124 (let* ((file (file-of-tag))
2125 (mark-key (cons file line)))
2126 (unless (gethash mark-key marks)
2127 (let ((loc (xref-make-etags-location
2128 tag-info (expand-file-name file))))
2129 (push (xref-make hint loc) xrefs)
2130 (puthash mark-key t marks)))))))))))
2133 (defclass xref-etags-location (xref-location)
2134 ((tag-info :type list :initarg :tag-info)
2135 (file :type string :initarg :file
2136 :reader xref-location-group))
2137 :documentation "Location of an etags tag.")
2139 (defun xref-make-etags-location (tag-info file)
2140 (make-instance 'xref-etags-location :tag-info tag-info
2141 :file (expand-file-name file)))
2143 (cl-defmethod xref-location-marker ((l xref-etags-location))
2144 (with-slots (tag-info file) l
2145 (let ((buffer (find-file-noselect file)))
2146 (with-current-buffer buffer
2147 (etags-goto-tag-location tag-info)
2150 (cl-defmethod xref-location-line ((l xref-etags-location))
2151 (with-slots (tag-info) l
2157 ;;; etags.el ends here