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