]> code.delx.au - gnu-emacs/blob - test/manual/etags/el-src/emacs/lisp/progmodes/etags.el
Merge from origin/emacs-25
[gnu-emacs] / test / manual / etags / el-src / emacs / lisp / progmodes / etags.el
1 ;;; etags.el --- etags facility for Emacs -*- lexical-binding: t -*-
2
3 ;; Copyright (C) 1985-1986, 1988-1989, 1992-1996, 1998, 2000-2016 Free
4 ;; Software Foundation, Inc.
5
6 ;; Author: Roland McGrath <roland@gnu.org>
7 ;; Maintainer: emacs-devel@gnu.org
8 ;; Keywords: tools
9
10 ;; This file is part of GNU Emacs.
11
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.
16
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.
21
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/>.
24
25 ;;; Commentary:
26
27 ;;; Code:
28
29 (require 'ring)
30 (require 'button)
31 (require 'xref)
32
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)
42
43 (defgroup etags nil "Tags tables."
44 :group 'tools)
45
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")
56
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))
67
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)
76
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.
85
86
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)))
97
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)
102
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'.")
111
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'.")
116
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.")
120
121 (defvar tags-table-list-started-at nil
122 "Pointer into `tags-table-computed-list', where the current search started.")
123
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.")
127
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)
135
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))
144
145 (define-obsolete-variable-alias 'find-tag-marker-ring-length
146 'xref-marker-ring-length "25.1")
147
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")
153
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")
159
160 (defcustom tags-apropos-additional-actions nil
161 "Specify additional actions for `tags-apropos'.
162
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.
171
172 Example value:
173
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")
182
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")
188
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.")
193
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].")
197 \f
198 ;; Tags table state.
199 ;; These variables are local in tags table buffers.
200
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.")
205
206 (defvar tags-completion-table nil
207 "Obarray of tag names defined in current tags table.")
208
209 (defvar tags-included-tables nil
210 "List of tags tables included by the current tags table.")
211
212 (defvar next-file-list nil
213 "List of files for \\[next-file] to process.")
214 \f
215 ;; Hooks for file formats.
216
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.")
223
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.")
259 \f
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.
271
272 ;; Value is t if we have found a valid tags table buffer.
273 (run-hook-with-args-until-success 'tags-table-format-functions))
274
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))
283
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.
289
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)
299 t)
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)))
320
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)
330
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)))
354
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))))))
359
360 (defun tags-table-extend-computed-list ()
361 "Extend `tags-table-computed-list' to remove the first t placeholder.
362
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)))))))
399
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))
406
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)
420
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)))))
463
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)
482
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)))
490
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))))
521
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))
529
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))
535
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))))
541
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."
552
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)))))
568 (t
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
572 (or
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
587 (or
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))))))
615
616 ;; Expand the table name into a full file name.
617 (setq tags-file-name (tags-expand-table-name tags-file-name))
618
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)
626
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))
632
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)
645
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)))
663
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))
674
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))))
690
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)))))
698
699 ;; Return of t says the tags table is valid.
700 t)
701
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)))))
711
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))
730 \f
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))
738
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))))
748
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))))
754 \f
755 (defun tags-completion-table ()
756 "Build `tags-completion-table' on demand.
757 The tags included in the completion table are those in the current
758 tags table and its (recursively) included tags tables."
759 (or tags-completion-table
760 ;; No cached value for this buffer.
761 (condition-case ()
762 (let (current-table combined-table)
763 (message "Making tags completion table for %s..." buffer-file-name)
764 (save-excursion
765 ;; Iterate over the current list of tags tables.
766 (while (visit-tags-table-buffer (and combined-table t))
767 ;; Find possible completions in this table.
768 (setq current-table (funcall tags-completion-table-function))
769 ;; Merge this buffer's completions into the combined table.
770 (if combined-table
771 (mapatoms
772 (lambda (sym) (intern (symbol-name sym) combined-table))
773 current-table)
774 (setq combined-table current-table))))
775 (message "Making tags completion table for %s...done"
776 buffer-file-name)
777 ;; Cache the result in a buffer-local variable.
778 (setq tags-completion-table combined-table))
779 (quit (message "Tags completion table construction aborted.")
780 (setq tags-completion-table nil)))))
781
782 ;;;###autoload
783 (defun tags-lazy-completion-table ()
784 (let ((buf (current-buffer)))
785 (lambda (string pred action)
786 (with-current-buffer buf
787 (save-excursion
788 ;; If we need to ask for the tag table, allow that.
789 (let ((enable-recursive-minibuffers t))
790 (visit-tags-table-buffer))
791 (complete-with-action action (tags-completion-table) string pred))))))
792
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))))
798
799 (defun tags-completion-at-point-function ()
800 "Using tags, return a completion table for the text around point.
801 If no tags table is loaded, do nothing and return nil."
802 (when (or tags-table-list tags-file-name)
803 (let ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
804 tags-case-fold-search
805 case-fold-search))
806 (pattern (funcall (or find-tag-default-function
807 (get major-mode 'find-tag-default-function)
808 'find-tag-default)))
809 beg)
810 (when pattern
811 (save-excursion
812 (forward-char (1- (length pattern)))
813 (search-backward pattern)
814 (setq beg (point))
815 (forward-char (length pattern))
816 (list beg (point) (tags-lazy-completion-table) :exclusive 'no))))))
817 \f
818 (defun find-tag-tag (string)
819 "Read a tag name, with defaulting and completion."
820 (let* ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
821 tags-case-fold-search
822 case-fold-search))
823 (default (funcall (or find-tag-default-function
824 (get major-mode 'find-tag-default-function)
825 'find-tag-default)))
826 (spec (completing-read (if default
827 (format "%s (default %s): "
828 (substring string 0 (string-match "[ :]+\\'" string))
829 default)
830 string)
831 (tags-lazy-completion-table)
832 nil nil nil nil default)))
833 (if (equal spec "")
834 (or default (user-error "There is no default tag"))
835 spec)))
836
837 (defvar last-tag nil
838 "Last tag found by \\[find-tag].")
839
840 (defun find-tag-interactive (prompt &optional no-default)
841 "Get interactive arguments for tag functions.
842 The functions using this are `find-tag-noselect',
843 `find-tag-other-window', and `find-tag-regexp'."
844 (if (and current-prefix-arg last-tag)
845 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
846 '-
847 t))
848 (list (if no-default
849 (read-string prompt)
850 (find-tag-tag prompt)))))
851
852 (defvar find-tag-history nil) ; Doc string?
853
854 ;; Dynamic bondage:
855 (defvar etags-case-fold-search)
856 (defvar etags-syntax-table)
857 (defvar local-find-tag-hook)
858
859 ;;;###autoload
860 (defun find-tag-noselect (tagname &optional next-p regexp-p)
861 "Find tag (in current tags table) whose name contains TAGNAME.
862 Returns the buffer containing the tag's definition and moves its point there,
863 but does not select the buffer.
864 The default for TAGNAME is the expression in the buffer near point.
865
866 If second arg NEXT-P is t (interactively, with prefix arg), search for
867 another tag that matches the last tagname or regexp used. When there are
868 multiple matches for a tag, more exact matches are found first. If NEXT-P
869 is the atom `-' (interactively, with prefix arg that is a negative number
870 or just \\[negative-argument]), pop back to the previous tag gone to.
871
872 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
873
874 A marker representing the point when this command is invoked is pushed
875 onto a ring and may be popped back to with \\[pop-tag-mark].
876 Contrast this with the ring of marks gone to by the command.
877
878 See documentation of variable `tags-file-name'."
879 (interactive (find-tag-interactive "Find tag: "))
880
881 (setq find-tag-history (cons tagname find-tag-history))
882 ;; Save the current buffer's value of `find-tag-hook' before
883 ;; selecting the tags table buffer. For the same reason, save value
884 ;; of `tags-file-name' in case it has a buffer-local value.
885 (let ((local-find-tag-hook find-tag-hook))
886 (if (eq '- next-p)
887 ;; Pop back to a previous location.
888 (if (ring-empty-p tags-location-ring)
889 (user-error "No previous tag locations")
890 (let ((marker (ring-remove tags-location-ring 0)))
891 (prog1
892 ;; Move to the saved location.
893 (set-buffer (or (marker-buffer marker)
894 (error "The marked buffer has been deleted")))
895 (goto-char (marker-position marker))
896 ;; Kill that marker so it doesn't slow down editing.
897 (set-marker marker nil nil)
898 ;; Run the user's hook. Do we really want to do this for pop?
899 (run-hooks 'local-find-tag-hook))))
900 ;; Record whence we came.
901 (xref-push-marker-stack)
902 (if (and next-p last-tag)
903 ;; Find the same table we last used.
904 (visit-tags-table-buffer 'same)
905 ;; Pick a table to use.
906 (visit-tags-table-buffer)
907 ;; Record TAGNAME for a future call with NEXT-P non-nil.
908 (setq last-tag tagname))
909 ;; Record the location so we can pop back to it later.
910 (let ((marker (make-marker)))
911 (with-current-buffer
912 ;; find-tag-in-order does the real work.
913 (find-tag-in-order
914 (if (and next-p last-tag) last-tag tagname)
915 (if regexp-p
916 find-tag-regexp-search-function
917 find-tag-search-function)
918 (if regexp-p
919 find-tag-regexp-tag-order
920 find-tag-tag-order)
921 (if regexp-p
922 find-tag-regexp-next-line-after-failure-p
923 find-tag-next-line-after-failure-p)
924 (if regexp-p "matching" "containing")
925 (or (not next-p) (not last-tag)))
926 (set-marker marker (point))
927 (run-hooks 'local-find-tag-hook)
928 (ring-insert tags-location-ring marker)
929 (current-buffer))))))
930
931 ;;;###autoload
932 (defun find-tag (tagname &optional next-p regexp-p)
933 "Find tag (in current tags table) whose name contains TAGNAME.
934 Select the buffer containing the tag's definition, and move point there.
935 The default for TAGNAME is the expression in the buffer around or before point.
936
937 If second arg NEXT-P is t (interactively, with prefix arg), search for
938 another tag that matches the last tagname or regexp used. When there are
939 multiple matches for a tag, more exact matches are found first. If NEXT-P
940 is the atom `-' (interactively, with prefix arg that is a negative number
941 or just \\[negative-argument]), pop back to the previous tag gone to.
942
943 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
944
945 A marker representing the point when this command is invoked is pushed
946 onto a ring and may be popped back to with \\[pop-tag-mark].
947 Contrast this with the ring of marks gone to by the command.
948
949 See documentation of variable `tags-file-name'."
950 (interactive (find-tag-interactive "Find tag: "))
951 (let* ((buf (find-tag-noselect tagname next-p regexp-p))
952 (pos (with-current-buffer buf (point))))
953 (condition-case nil
954 (switch-to-buffer buf)
955 (error (pop-to-buffer buf)))
956 (goto-char pos)))
957
958 ;;;###autoload
959 (defun find-tag-other-window (tagname &optional next-p regexp-p)
960 "Find tag (in current tags table) whose name contains TAGNAME.
961 Select the buffer containing the tag's definition in another window, and
962 move point there. The default for TAGNAME is the expression in the buffer
963 around or before point.
964
965 If second arg NEXT-P is t (interactively, with prefix arg), search for
966 another tag that matches the last tagname or regexp used. When there are
967 multiple matches for a tag, more exact matches are found first. If NEXT-P
968 is negative (interactively, with prefix arg that is a negative number or
969 just \\[negative-argument]), pop back to the previous tag gone to.
970
971 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
972
973 A marker representing the point when this command is invoked is pushed
974 onto a ring and may be popped back to with \\[pop-tag-mark].
975 Contrast this with the ring of marks gone to by the command.
976
977 See documentation of variable `tags-file-name'."
978 (declare (obsolete xref-find-definitions-other-window "25.1"))
979 (interactive (find-tag-interactive "Find tag other window: "))
980
981 ;; This hair is to deal with the case where the tag is found in the
982 ;; selected window's buffer; without the hair, point is moved in both
983 ;; windows. To prevent this, we save the selected window's point before
984 ;; doing find-tag-noselect, and restore it after.
985 (let* ((window-point (window-point))
986 (tagbuf (find-tag-noselect tagname next-p regexp-p))
987 (tagpoint (progn (set-buffer tagbuf) (point))))
988 (set-window-point (prog1
989 (selected-window)
990 (switch-to-buffer-other-window tagbuf)
991 ;; We have to set this new window's point; it
992 ;; might already have been displaying a
993 ;; different portion of tagbuf, in which case
994 ;; switch-to-buffer-other-window doesn't set
995 ;; the window's point from the buffer.
996 (set-window-point (selected-window) tagpoint))
997 window-point)))
998
999 ;;;###autoload
1000 (defun find-tag-other-frame (tagname &optional next-p)
1001 "Find tag (in current tags table) whose name contains TAGNAME.
1002 Select the buffer containing the tag's definition in another frame, and
1003 move point there. The default for TAGNAME is the expression in the buffer
1004 around or before point.
1005
1006 If second arg NEXT-P is t (interactively, with prefix arg), search for
1007 another tag that matches the last tagname or regexp used. When there are
1008 multiple matches for a tag, more exact matches are found first. If NEXT-P
1009 is negative (interactively, with prefix arg that is a negative number or
1010 just \\[negative-argument]), pop back to the previous tag gone to.
1011
1012 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
1013
1014 A marker representing the point when this command is invoked is pushed
1015 onto a ring and may be popped back to with \\[pop-tag-mark].
1016 Contrast this with the ring of marks gone to by the command.
1017
1018 See documentation of variable `tags-file-name'."
1019 (declare (obsolete xref-find-definitions-other-frame "25.1"))
1020 (interactive (find-tag-interactive "Find tag other frame: "))
1021 (let ((pop-up-frames t))
1022 (find-tag-other-window tagname next-p)))
1023
1024 ;;;###autoload
1025 (defun find-tag-regexp (regexp &optional next-p other-window)
1026 "Find tag (in current tags table) whose name matches REGEXP.
1027 Select the buffer containing the tag's definition and move point there.
1028
1029 If second arg NEXT-P is t (interactively, with prefix arg), search for
1030 another tag that matches the last tagname or regexp used. When there are
1031 multiple matches for a tag, more exact matches are found first. If NEXT-P
1032 is negative (interactively, with prefix arg that is a negative number or
1033 just \\[negative-argument]), pop back to the previous tag gone to.
1034
1035 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
1036
1037 A marker representing the point when this command is invoked is pushed
1038 onto a ring and may be popped back to with \\[pop-tag-mark].
1039 Contrast this with the ring of marks gone to by the command.
1040
1041 See documentation of variable `tags-file-name'."
1042 (declare (obsolete xref-find-apropos "25.1"))
1043 (interactive (find-tag-interactive "Find tag regexp: " t))
1044 ;; We go through find-tag-other-window to do all the display hair there.
1045 (funcall (if other-window 'find-tag-other-window 'find-tag)
1046 regexp next-p t))
1047
1048 ;;;###autoload
1049 (defalias 'pop-tag-mark 'xref-pop-marker-stack)
1050
1051 \f
1052 (defvar tag-lines-already-matched nil
1053 "Matches remembered between calls.") ; Doc string: calls to what?
1054
1055 (defun find-tag-in-order (pattern
1056 search-forward-func
1057 order
1058 next-line-after-failure-p
1059 matching
1060 first-search)
1061 "Internal tag-finding function.
1062 PATTERN is a string to pass to arg SEARCH-FORWARD-FUNC, and to any
1063 member of the function list ORDER. If ORDER is nil, use saved state
1064 to continue a previous search.
1065
1066 Arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
1067 point should be moved to the next line.
1068
1069 Arg MATCHING is a string, an English `-ing' word, to be used in an
1070 error message."
1071 ;; Algorithm is as follows:
1072 ;; For each qualifier-func in ORDER, go to beginning of tags file, and
1073 ;; perform inner loop: for each naive match for PATTERN found using
1074 ;; SEARCH-FORWARD-FUNC, qualify the naive match using qualifier-func. If
1075 ;; it qualifies, go to the specified line in the specified source file
1076 ;; and return. Qualified matches are remembered to avoid repetition.
1077 ;; State is saved so that the loop can be continued.
1078 (let (file ;name of file containing tag
1079 tag-info ;where to find the tag in FILE
1080 (first-table t)
1081 (tag-order order)
1082 (match-marker (make-marker))
1083 goto-func
1084 (case-fold-search (if (memq tags-case-fold-search '(nil t))
1085 tags-case-fold-search
1086 case-fold-search))
1087 )
1088 (save-excursion
1089
1090 (if first-search
1091 ;; This is the start of a search for a fresh tag.
1092 ;; Clear the list of tags matched by the previous search.
1093 ;; find-tag-noselect has already put us in the first tags table
1094 ;; buffer before we got called.
1095 (setq tag-lines-already-matched nil)
1096 ;; Continuing to search for the tag specified last time.
1097 ;; tag-lines-already-matched lists locations matched in previous
1098 ;; calls so we don't visit the same tag twice if it matches twice
1099 ;; during two passes with different qualification predicates.
1100 ;; Switch to the current tags table buffer.
1101 (visit-tags-table-buffer 'same))
1102
1103 ;; Get a qualified match.
1104 (catch 'qualified-match-found
1105
1106 ;; Iterate over the list of tags tables.
1107 (while (or first-table
1108 (visit-tags-table-buffer t))
1109
1110 (and first-search first-table
1111 ;; Start at beginning of tags file.
1112 (goto-char (point-min)))
1113
1114 (setq first-table nil)
1115
1116 ;; Iterate over the list of ordering predicates.
1117 (while order
1118 (while (funcall search-forward-func pattern nil t)
1119 ;; Naive match found. Qualify the match.
1120 (and (funcall (car order) pattern)
1121 ;; Make sure it is not a previous qualified match.
1122 (not (member (set-marker match-marker (point-at-bol))
1123 tag-lines-already-matched))
1124 (throw 'qualified-match-found nil))
1125 (if next-line-after-failure-p
1126 (forward-line 1)))
1127 ;; Try the next flavor of match.
1128 (setq order (cdr order))
1129 (goto-char (point-min)))
1130 (setq order tag-order))
1131 ;; We throw out on match, so only get here if there were no matches.
1132 ;; Clear out the markers we use to avoid duplicate matches so they
1133 ;; don't slow down editing and are immediately available for GC.
1134 (while tag-lines-already-matched
1135 (set-marker (car tag-lines-already-matched) nil nil)
1136 (setq tag-lines-already-matched (cdr tag-lines-already-matched)))
1137 (set-marker match-marker nil nil)
1138 (user-error "No %stags %s %s" (if first-search "" "more ")
1139 matching pattern))
1140
1141 ;; Found a tag; extract location info.
1142 (beginning-of-line)
1143 (setq tag-lines-already-matched (cons match-marker
1144 tag-lines-already-matched))
1145 ;; Expand the filename, using the tags table buffer's default-directory.
1146 ;; We should be able to search for file-name backwards in file-of-tag:
1147 ;; the beginning-of-line is ok except when positioned on a "file-name" tag.
1148 (setq file (expand-file-name
1149 (if (memq (car order) '(tag-exact-file-name-match-p
1150 tag-file-name-match-p
1151 tag-partial-file-name-match-p))
1152 (save-excursion (forward-line 1)
1153 (file-of-tag))
1154 (file-of-tag)))
1155 tag-info (funcall snarf-tag-function))
1156
1157 ;; Get the local value in the tags table buffer before switching buffers.
1158 (setq goto-func goto-tag-location-function)
1159 (tag-find-file-of-tag-noselect file)
1160 (widen)
1161 (push-mark)
1162 (funcall goto-func tag-info)
1163
1164 ;; Return the buffer where the tag was found.
1165 (current-buffer))))
1166
1167 (defun tag-find-file-of-tag-noselect (file)
1168 "Find the right line in the specified FILE."
1169 ;; If interested in compressed-files, search files with extensions.
1170 ;; Otherwise, search only the real file.
1171 (let* ((buffer-search-extensions (if auto-compression-mode
1172 tags-compression-info-list
1173 '("")))
1174 the-buffer
1175 (file-search-extensions buffer-search-extensions))
1176 ;; search a buffer visiting the file with each possible extension
1177 ;; Note: there is a small inefficiency in find-buffer-visiting :
1178 ;; truename is computed even if not needed. Not too sure about this
1179 ;; but I suspect truename computation accesses the disk.
1180 ;; It is maybe a good idea to optimize this find-buffer-visiting.
1181 ;; An alternative would be to use only get-file-buffer
1182 ;; but this looks less "sure" to find the buffer for the file.
1183 (while (and (not the-buffer) buffer-search-extensions)
1184 (setq the-buffer (find-buffer-visiting (concat file (car buffer-search-extensions))))
1185 (setq buffer-search-extensions (cdr buffer-search-extensions)))
1186 ;; if found a buffer but file modified, ensure we re-read !
1187 (if (and the-buffer (not (verify-visited-file-modtime the-buffer)))
1188 (find-file-noselect (buffer-file-name the-buffer)))
1189 ;; if no buffer found, search for files with possible extensions on disk
1190 (while (and (not the-buffer) file-search-extensions)
1191 (if (not (file-exists-p (concat file (car file-search-extensions))))
1192 (setq file-search-extensions (cdr file-search-extensions))
1193 (setq the-buffer (find-file-noselect (concat file (car file-search-extensions))))))
1194 (if (not the-buffer)
1195 (if auto-compression-mode
1196 (error "File %s (with or without extensions %s) not found" file tags-compression-info-list)
1197 (error "File %s not found" file))
1198 (set-buffer the-buffer))))
1199
1200 (defun tag-find-file-of-tag (file) ; Doc string?
1201 (let ((buf (tag-find-file-of-tag-noselect file)))
1202 (condition-case nil
1203 (switch-to-buffer buf)
1204 (error (pop-to-buffer buf)))))
1205 \f
1206 ;; `etags' TAGS file format support.
1207
1208 (defun etags-recognize-tags-table ()
1209 "If `etags-verify-tags-table', make buffer-local format variables.
1210 If current buffer is a valid etags TAGS file, then give it
1211 buffer-local values of tags table format variables."
1212 (and (etags-verify-tags-table)
1213 ;; It is annoying to flash messages on the screen briefly,
1214 ;; and this message is not useful. -- rms
1215 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
1216 (mapc (lambda (elt) (set (make-local-variable (car elt)) (cdr elt)))
1217 '((file-of-tag-function . etags-file-of-tag)
1218 (tags-table-files-function . etags-tags-table-files)
1219 (tags-completion-table-function . etags-tags-completion-table)
1220 (snarf-tag-function . etags-snarf-tag)
1221 (goto-tag-location-function . etags-goto-tag-location)
1222 (find-tag-regexp-search-function . re-search-forward)
1223 (find-tag-regexp-tag-order . (tag-re-match-p))
1224 (find-tag-regexp-next-line-after-failure-p . t)
1225 (find-tag-search-function . search-forward)
1226 (find-tag-tag-order . (tag-exact-file-name-match-p
1227 tag-file-name-match-p
1228 tag-exact-match-p
1229 tag-implicit-name-match-p
1230 tag-symbol-match-p
1231 tag-word-match-p
1232 tag-partial-file-name-match-p
1233 tag-any-match-p))
1234 (find-tag-next-line-after-failure-p . nil)
1235 (list-tags-function . etags-list-tags)
1236 (tags-apropos-function . etags-tags-apropos)
1237 (tags-included-tables-function . etags-tags-included-tables)
1238 (verify-tags-table-function . etags-verify-tags-table)
1239 ))))
1240
1241 (defun etags-verify-tags-table ()
1242 "Return non-nil if the current buffer is a valid etags TAGS file."
1243 ;; Use eq instead of = in case char-after returns nil.
1244 (eq (char-after (point-min)) ?\f))
1245
1246 (defun etags-file-of-tag (&optional relative) ; Doc string?
1247 (save-excursion
1248 (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n")
1249 (let ((str (convert-standard-filename
1250 (buffer-substring (match-beginning 1) (match-end 1)))))
1251 (if relative
1252 str
1253 (expand-file-name str (file-truename default-directory))))))
1254
1255
1256 (defun etags-tags-completion-table () ; Doc string?
1257 (let ((table (make-vector 511 0))
1258 (progress-reporter
1259 (make-progress-reporter
1260 (format "Making tags completion table for %s..." buffer-file-name)
1261 (point-min) (point-max))))
1262 (save-excursion
1263 (goto-char (point-min))
1264 ;; This monster regexp matches an etags tag line.
1265 ;; \1 is the string to match;
1266 ;; \2 is not interesting;
1267 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
1268 ;; \4 is not interesting;
1269 ;; \5 is the explicitly-specified tag name.
1270 ;; \6 is the line to start searching at;
1271 ;; \7 is the char to start searching at.
1272 (while (re-search-forward
1273 "^\\(\\([^\177]+[^-a-zA-Z0-9_+*$:\177]+\\)?\
1274 \\([-a-zA-Z0-9_+*$?:]+\\)[^-a-zA-Z0-9_+*$?:\177]*\\)\177\
1275 \\(\\([^\n\001]+\\)\001\\)?\\([0-9]+\\)?,\\([0-9]+\\)?\n"
1276 nil t)
1277 (intern (prog1 (if (match-beginning 5)
1278 ;; There is an explicit tag name.
1279 (buffer-substring (match-beginning 5) (match-end 5))
1280 ;; No explicit tag name. Best guess.
1281 (buffer-substring (match-beginning 3) (match-end 3)))
1282 (progress-reporter-update progress-reporter (point)))
1283 table)))
1284 table))
1285
1286 (defun etags-snarf-tag (&optional use-explicit) ; Doc string?
1287 (let (tag-text line startpos explicit-start)
1288 (if (save-excursion
1289 (forward-line -1)
1290 (looking-at "\f\n"))
1291 ;; The match was for a source file name, not any tag within a file.
1292 ;; Give text of t, meaning to go exactly to the location we specify,
1293 ;; the beginning of the file.
1294 (setq tag-text t
1295 line nil
1296 startpos (point-min))
1297
1298 ;; Find the end of the tag and record the whole tag text.
1299 (search-forward "\177")
1300 (setq tag-text (buffer-substring (1- (point)) (point-at-bol)))
1301 ;; If use-explicit is non nil and explicit tag is present, use it as part of
1302 ;; return value. Else just skip it.
1303 (setq explicit-start (point))
1304 (when (and (search-forward "\001" (point-at-bol 2) t)
1305 use-explicit)
1306 (setq tag-text (buffer-substring explicit-start (1- (point)))))
1307
1308
1309 (if (looking-at "[0-9]")
1310 (setq line (string-to-number (buffer-substring
1311 (point)
1312 (progn (skip-chars-forward "0-9")
1313 (point))))))
1314 (search-forward ",")
1315 (if (looking-at "[0-9]")
1316 (setq startpos (string-to-number (buffer-substring
1317 (point)
1318 (progn (skip-chars-forward "0-9")
1319 (point)))))))
1320 ;; Leave point on the next line of the tags file.
1321 (forward-line 1)
1322 (cons tag-text (cons line startpos))))
1323
1324 (defun etags-goto-tag-location (tag-info)
1325 "Go to location of tag specified by TAG-INFO.
1326 TAG-INFO is a cons (TEXT LINE . POSITION).
1327 TEXT is the initial part of a line containing the tag.
1328 LINE is the line number.
1329 POSITION is the (one-based) char position of TEXT within the file.
1330
1331 If TEXT is t, it means the tag refers to exactly LINE or POSITION,
1332 whichever is present, LINE having preference, no searching.
1333 Either LINE or POSITION can be nil. POSITION is used if present.
1334
1335 If the tag isn't exactly at the given position, then look near that
1336 position using a search window that expands progressively until it
1337 hits the start of file."
1338 (let ((startpos (cdr (cdr tag-info)))
1339 (line (car (cdr tag-info)))
1340 offset found pat)
1341 (if (eq (car tag-info) t)
1342 ;; Direct file tag.
1343 (cond (line (progn (goto-char (point-min))
1344 (forward-line (1- line))))
1345 (startpos (goto-char startpos))
1346 (t (error "etags.el BUG: bogus direct file tag")))
1347 ;; This constant is 1/2 the initial search window.
1348 ;; There is no sense in making it too small,
1349 ;; since just going around the loop once probably
1350 ;; costs about as much as searching 2000 chars.
1351 (setq offset 1000
1352 found nil
1353 pat (concat (if (eq selective-display t)
1354 "\\(^\\|\^m\\)" "^")
1355 (regexp-quote (car tag-info))))
1356 ;; The character position in the tags table is 0-origin.
1357 ;; Convert it to a 1-origin Emacs character position.
1358 (if startpos (setq startpos (1+ startpos)))
1359 ;; If no char pos was given, try the given line number.
1360 (or startpos
1361 (if line
1362 (setq startpos (progn (goto-char (point-min))
1363 (forward-line (1- line))
1364 (point)))))
1365 (or startpos
1366 (setq startpos (point-min)))
1367 ;; First see if the tag is right at the specified location.
1368 (goto-char startpos)
1369 (setq found (looking-at pat))
1370 (while (and (not found)
1371 (progn
1372 (goto-char (- startpos offset))
1373 (not (bobp))))
1374 (setq found
1375 (re-search-forward pat (+ startpos offset) t)
1376 offset (* 3 offset))) ; expand search window
1377 (or found
1378 (re-search-forward pat nil t)
1379 (user-error "Rerun etags: `%s' not found in %s"
1380 pat buffer-file-name)))
1381 ;; Position point at the right place
1382 ;; if the search string matched an extra Ctrl-m at the beginning.
1383 (and (eq selective-display t)
1384 (looking-at "\^m")
1385 (forward-char 1))
1386 (beginning-of-line)))
1387
1388 (defun etags-list-tags (file) ; Doc string?
1389 (goto-char (point-min))
1390 (when (re-search-forward (concat "\f\n" "\\(" file "\\)" ",") nil t)
1391 (let ((path (save-excursion (forward-line 1) (file-of-tag)))
1392 ;; Get the local value in the tags table
1393 ;; buffer before switching buffers.
1394 (goto-func goto-tag-location-function)
1395 tag tag-info pt)
1396 (forward-line 1)
1397 (while (not (or (eobp) (looking-at "\f")))
1398 ;; We used to use explicit tags when available, but the current goto-func
1399 ;; can only handle implicit tags.
1400 (setq tag-info (save-excursion (funcall snarf-tag-function nil))
1401 tag (car tag-info)
1402 pt (with-current-buffer standard-output (point)))
1403 (princ tag)
1404 (when (= (aref tag 0) ?\() (princ " ...)"))
1405 (with-current-buffer standard-output
1406 (make-text-button pt (point)
1407 'tag-info tag-info
1408 'file-path path
1409 'goto-func goto-func
1410 'action (lambda (button)
1411 (let ((tag-info (button-get button 'tag-info))
1412 (goto-func (button-get button 'goto-func)))
1413 (tag-find-file-of-tag (button-get button 'file-path))
1414 (widen)
1415 (funcall goto-func tag-info)))
1416 'follow-link t
1417 'face tags-tag-face
1418 'type 'button))
1419 (terpri)
1420 (forward-line 1))
1421 t)))
1422
1423 (defmacro tags-with-face (face &rest body)
1424 "Execute BODY, give output to `standard-output' face FACE."
1425 (let ((pp (make-symbol "start")))
1426 `(let ((,pp (with-current-buffer standard-output (point))))
1427 ,@body
1428 (put-text-property ,pp (with-current-buffer standard-output (point))
1429 'face ,face standard-output))))
1430
1431 (defun etags-tags-apropos-additional (regexp)
1432 "Display tags matching REGEXP from `tags-apropos-additional-actions'."
1433 (with-current-buffer standard-output
1434 (dolist (oba tags-apropos-additional-actions)
1435 (princ "\n\n")
1436 (tags-with-face 'highlight (princ (car oba)))
1437 (princ":\n\n")
1438 (let* ((beg (point))
1439 (symbs (car (cddr oba)))
1440 (ins-symb (lambda (sy)
1441 (let ((sn (symbol-name sy)))
1442 (when (string-match regexp sn)
1443 (make-text-button (point)
1444 (progn (princ sy) (point))
1445 'action-internal(cadr oba)
1446 'action (lambda (button) (funcall
1447 (button-get button 'action-internal)
1448 (button-get button 'item)))
1449 'item sn
1450 'face tags-tag-face
1451 'follow-link t
1452 'type 'button)
1453 (terpri))))))
1454 (when (symbolp symbs)
1455 (if (boundp symbs)
1456 (setq symbs (symbol-value symbs))
1457 (insert "symbol `" (symbol-name symbs) "' has no value\n")
1458 (setq symbs nil)))
1459 (if (vectorp symbs)
1460 (mapatoms ins-symb symbs)
1461 (dolist (sy symbs)
1462 (funcall ins-symb (car sy))))
1463 (sort-lines nil beg (point))))))
1464
1465 (defun etags-tags-apropos (string) ; Doc string?
1466 (when tags-apropos-verbose
1467 (princ "Tags in file `")
1468 (tags-with-face 'highlight (princ buffer-file-name))
1469 (princ "':\n\n"))
1470 (goto-char (point-min))
1471 (let ((progress-reporter (make-progress-reporter
1472 (format "Making tags apropos buffer for `%s'..."
1473 string)
1474 (point-min) (point-max))))
1475 (while (re-search-forward string nil t)
1476 (progress-reporter-update progress-reporter (point))
1477 (beginning-of-line)
1478
1479 (let* ( ;; Get the local value in the tags table
1480 ;; buffer before switching buffers.
1481 (goto-func goto-tag-location-function)
1482 (tag-info (save-excursion (funcall snarf-tag-function)))
1483 (tag (if (eq t (car tag-info)) nil (car tag-info)))
1484 (file-path (save-excursion (if tag (file-of-tag)
1485 (save-excursion (forward-line 1)
1486 (file-of-tag)))))
1487 (file-label (if tag (file-of-tag t)
1488 (save-excursion (forward-line 1)
1489 (file-of-tag t))))
1490 (pt (with-current-buffer standard-output (point))))
1491 (if tag
1492 (progn
1493 (princ (format "[%s]: " file-label))
1494 (princ tag)
1495 (when (= (aref tag 0) ?\() (princ " ...)"))
1496 (with-current-buffer standard-output
1497 (make-text-button pt (point)
1498 'tag-info tag-info
1499 'file-path file-path
1500 'goto-func goto-func
1501 'action (lambda (button)
1502 (let ((tag-info (button-get button 'tag-info))
1503 (goto-func (button-get button 'goto-func)))
1504 (tag-find-file-of-tag (button-get button 'file-path))
1505 (widen)
1506 (funcall goto-func tag-info)))
1507 'follow-link t
1508 'face tags-tag-face
1509 'type 'button)))
1510 (princ (format "- %s" file-label))
1511 (with-current-buffer standard-output
1512 (make-text-button pt (point)
1513 'file-path file-path
1514 'action (lambda (button)
1515 (tag-find-file-of-tag (button-get button 'file-path))
1516 ;; Get the local value in the tags table
1517 ;; buffer before switching buffers.
1518 (goto-char (point-min)))
1519 'follow-link t
1520 'face tags-tag-face
1521 'type 'button))))
1522 (terpri)
1523 (forward-line 1))
1524 (message nil))
1525 (when tags-apropos-verbose (princ "\n")))
1526
1527 (defun etags-tags-table-files () ; Doc string?
1528 (let ((files nil)
1529 beg)
1530 (goto-char (point-min))
1531 (while (search-forward "\f\n" nil t)
1532 (setq beg (point))
1533 (end-of-line)
1534 (skip-chars-backward "^," beg)
1535 (or (looking-at "include$")
1536 (push (convert-standard-filename
1537 (buffer-substring beg (1- (point))))
1538 files)))
1539 (nreverse files)))
1540
1541 ;; FIXME? Should this save-excursion?
1542 (defun etags-tags-included-tables () ; Doc string?
1543 (let ((files nil)
1544 beg)
1545 (goto-char (point-min))
1546 (while (search-forward "\f\n" nil t)
1547 (setq beg (point))
1548 (end-of-line)
1549 (skip-chars-backward "^," beg)
1550 (when (looking-at "include$")
1551 ;; Expand in the default-directory of the tags table buffer.
1552 (push (expand-file-name (convert-standard-filename
1553 (buffer-substring beg (1- (point)))))
1554 files)))
1555 (nreverse files)))
1556 \f
1557 ;; Empty tags file support.
1558
1559 (defun tags-recognize-empty-tags-table ()
1560 "Return non-nil if current buffer is empty.
1561 If empty, make buffer-local values of the tags table format variables
1562 that do nothing."
1563 (and (zerop (buffer-size))
1564 (mapc (lambda (sym) (set (make-local-variable sym) 'ignore))
1565 '(tags-table-files-function
1566 tags-completion-table-function
1567 find-tag-regexp-search-function
1568 find-tag-search-function
1569 tags-apropos-function
1570 tags-included-tables-function))
1571 (set (make-local-variable 'verify-tags-table-function)
1572 (lambda () (zerop (buffer-size))))))
1573 \f
1574 ;; Match qualifier functions for tagnames.
1575 ;; These functions assume the etags file format defined in etc/ETAGS.EBNF.
1576
1577 ;; This might be a neat idea, but it's too hairy at the moment.
1578 ;;(defmacro tags-with-syntax (&rest body)
1579 ;; `(with-syntax-table
1580 ;; (with-current-buffer (find-file-noselect (file-of-tag))
1581 ;; (syntax-table))
1582 ;; ,@body))
1583 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
1584
1585 ;; exact file name match, i.e. searched tag must match complete file
1586 ;; name including directories parts if there are some.
1587 (defun tag-exact-file-name-match-p (tag)
1588 "Return non-nil if TAG matches complete file name.
1589 Any directory part of the file name is also matched."
1590 (and (looking-at ",[0-9\n]")
1591 (save-excursion (backward-char (+ 2 (length tag)))
1592 (looking-at "\f\n"))))
1593
1594 ;; file name match as above, but searched tag must match the file
1595 ;; name not including the directories if there are some.
1596 (defun tag-file-name-match-p (tag)
1597 "Return non-nil if TAG matches file name, excluding directory part."
1598 (and (looking-at ",[0-9\n]")
1599 (save-excursion (backward-char (1+ (length tag)))
1600 (looking-at "/"))))
1601
1602 ;; this / to detect we are after a directory separator is ok for unix,
1603 ;; is there a variable that contains the regexp for directory separator
1604 ;; on whatever operating system ?
1605 ;; Looks like ms-win will lose here :).
1606
1607 ;; t if point is at a tag line that matches TAG exactly.
1608 ;; point should be just after a string that matches TAG.
1609 (defun tag-exact-match-p (tag)
1610 "Return non-nil if current tag line matches TAG exactly.
1611 Point should be just after a string that matches TAG."
1612 ;; The match is really exact if there is an explicit tag name.
1613 (or (and (eq (char-after (point)) ?\001)
1614 (eq (char-after (- (point) (length tag) 1)) ?\177))
1615 ;; We are not on the explicit tag name, but perhaps it follows.
1616 (looking-at (concat "[^\177\n]*\177" (regexp-quote tag) "\001"))))
1617
1618 ;; t if point is at a tag line that has an implicit name.
1619 ;; point should be just after a string that matches TAG.
1620 (defun tag-implicit-name-match-p (tag)
1621 "Return non-nil if current tag line has an implicit name.
1622 Point should be just after a string that matches TAG."
1623 ;; Look at the comment of the make_tag function in lib-src/etags.c for
1624 ;; a textual description of the four rules.
1625 (and (string-match "^[^ \t()=,;]+$" tag) ;rule #1
1626 (looking-at "[ \t()=,;]?\177") ;rules #2 and #4
1627 (save-excursion
1628 (backward-char (1+ (length tag)))
1629 (looking-at "[\n \t()=,;]")))) ;rule #3
1630
1631 ;; t if point is at a tag line that matches TAG as a symbol.
1632 ;; point should be just after a string that matches TAG.
1633 (defun tag-symbol-match-p (tag)
1634 "Return non-nil if current tag line matches TAG as a symbol.
1635 Point should be just after a string that matches TAG."
1636 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1637 (save-excursion
1638 (backward-char (1+ (length tag)))
1639 (and (looking-at "\\Sw") (looking-at "\\S_")))))
1640
1641 ;; t if point is at a tag line that matches TAG as a word.
1642 ;; point should be just after a string that matches TAG.
1643 (defun tag-word-match-p (tag)
1644 "Return non-nil if current tag line matches TAG as a word.
1645 Point should be just after a string that matches TAG."
1646 (and (looking-at "\\b.*\177")
1647 (save-excursion (backward-char (length tag))
1648 (looking-at "\\b"))))
1649
1650 ;; partial file name match, i.e. searched tag must match a substring
1651 ;; of the file name (potentially including a directory separator).
1652 (defun tag-partial-file-name-match-p (_tag)
1653 "Return non-nil if current tag matches file name.
1654 This is a substring match, and it can include directory separators.
1655 Point should be just after a string that matches TAG."
1656 (and (looking-at ".*,[0-9\n]")
1657 (save-excursion (beginning-of-line)
1658 (backward-char 2)
1659 (looking-at "\f\n"))))
1660
1661 ;; t if point is in a tag line with a tag containing TAG as a substring.
1662 (defun tag-any-match-p (_tag)
1663 "Return non-nil if current tag line contains TAG as a substring."
1664 (looking-at ".*\177"))
1665
1666 ;; t if point is at a tag line that matches RE as a regexp.
1667 (defun tag-re-match-p (re)
1668 "Return non-nil if current tag line matches regexp RE."
1669 (save-excursion
1670 (beginning-of-line)
1671 (let ((bol (point)))
1672 (and (search-forward "\177" (line-end-position) t)
1673 (re-search-backward re bol t)))))
1674 \f
1675 (defcustom tags-loop-revert-buffers nil
1676 "Non-nil means tags-scanning loops should offer to reread changed files.
1677 These loops normally read each file into Emacs, but when a file
1678 is already visited, they use the existing buffer.
1679 When this flag is non-nil, they offer to revert the existing buffer
1680 in the case where the file has changed since you visited it."
1681 :type 'boolean
1682 :group 'etags)
1683
1684 ;;;###autoload
1685 (defun next-file (&optional initialize novisit)
1686 "Select next file among files in current tags table.
1687
1688 A first argument of t (prefix arg, if interactive) initializes to the
1689 beginning of the list of files in the tags table. If the argument is
1690 neither nil nor t, it is evalled to initialize the list of files.
1691
1692 Non-nil second argument NOVISIT means use a temporary buffer
1693 to save time and avoid uninteresting warnings.
1694
1695 Value is nil if the file was already visited;
1696 if the file was newly read in, the value is the filename."
1697 ;; Make the interactive arg t if there was any prefix arg.
1698 (interactive (list (if current-prefix-arg t)))
1699 (cond ((not initialize)
1700 ;; Not the first run.
1701 )
1702 ((eq initialize t)
1703 ;; Initialize the list from the tags table.
1704 (save-excursion
1705 ;; Visit the tags table buffer to get its list of files.
1706 (visit-tags-table-buffer)
1707 ;; Copy the list so we can setcdr below, and expand the file
1708 ;; names while we are at it, in this buffer's default directory.
1709 (setq next-file-list (mapcar 'expand-file-name (tags-table-files)))
1710 ;; Iterate over all the tags table files, collecting
1711 ;; a complete list of referenced file names.
1712 (while (visit-tags-table-buffer t)
1713 ;; Find the tail of the working list and chain on the new
1714 ;; sublist for this tags table.
1715 (let ((tail next-file-list))
1716 (while (cdr tail)
1717 (setq tail (cdr tail)))
1718 ;; Use a copy so the next loop iteration will not modify the
1719 ;; list later returned by (tags-table-files).
1720 (if tail
1721 (setcdr tail (mapcar 'expand-file-name (tags-table-files)))
1722 (setq next-file-list (mapcar 'expand-file-name
1723 (tags-table-files))))))))
1724 (t
1725 ;; Initialize the list by evalling the argument.
1726 (setq next-file-list (eval initialize))))
1727 (unless next-file-list
1728 (and novisit
1729 (get-buffer " *next-file*")
1730 (kill-buffer " *next-file*"))
1731 (user-error "All files processed"))
1732 (let* ((next (car next-file-list))
1733 (buffer (get-file-buffer next))
1734 (new (not buffer)))
1735 ;; Advance the list before trying to find the file.
1736 ;; If we get an error finding the file, don't get stuck on it.
1737 (setq next-file-list (cdr next-file-list))
1738 ;; Optionally offer to revert buffers
1739 ;; if the files have changed on disk.
1740 (and buffer tags-loop-revert-buffers
1741 (not (verify-visited-file-modtime buffer))
1742 (y-or-n-p
1743 (format
1744 (if (buffer-modified-p buffer)
1745 "File %s changed on disk. Discard your edits? "
1746 "File %s changed on disk. Reread from disk? ")
1747 next))
1748 (with-current-buffer buffer
1749 (revert-buffer t t)))
1750 (if (not (and new novisit))
1751 (find-file next novisit)
1752 ;; Like find-file, but avoids random warning messages.
1753 (switch-to-buffer (get-buffer-create " *next-file*"))
1754 (kill-all-local-variables)
1755 (erase-buffer)
1756 (setq new next)
1757 (insert-file-contents new nil))
1758 new))
1759
1760 (defvar tags-loop-operate nil
1761 "Form for `tags-loop-continue' to eval to change one file.")
1762
1763 (defvar tags-loop-scan
1764 '(user-error "%s"
1765 (substitute-command-keys
1766 "No \\[tags-search] or \\[tags-query-replace] in progress"))
1767 "Form for `tags-loop-continue' to eval to scan one file.
1768 If it returns non-nil, this file needs processing by evalling
1769 `tags-loop-operate'. Otherwise, move on to the next file.")
1770
1771 (defun tags-loop-eval (form)
1772 "Evaluate FORM and return its result.
1773 Bind `case-fold-search' during the evaluation, depending on the value of
1774 `tags-case-fold-search'."
1775 (let ((case-fold-search (if (memq tags-case-fold-search '(t nil))
1776 tags-case-fold-search
1777 case-fold-search)))
1778 (eval form)))
1779
1780
1781 ;;;###autoload
1782 (defun tags-loop-continue (&optional first-time)
1783 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1784 Used noninteractively with non-nil argument to begin such a command (the
1785 argument is passed to `next-file', which see).
1786
1787 Two variables control the processing we do on each file: the value of
1788 `tags-loop-scan' is a form to be executed on each file to see if it is
1789 interesting (it returns non-nil if so) and `tags-loop-operate' is a form to
1790 evaluate to operate on an interesting file. If the latter evaluates to
1791 nil, we exit; otherwise we scan the next file."
1792 (declare (obsolete "use `xref-find-definitions' interface instead." "25.1"))
1793 (interactive)
1794 (let (new
1795 ;; Non-nil means we have finished one file
1796 ;; and should not scan it again.
1797 file-finished
1798 original-point
1799 (messaged nil))
1800 (while
1801 (progn
1802 ;; Scan files quickly for the first or next interesting one.
1803 ;; This starts at point in the current buffer.
1804 (while (or first-time file-finished
1805 (save-restriction
1806 (widen)
1807 (not (tags-loop-eval tags-loop-scan))))
1808 ;; If nothing was found in the previous file, and
1809 ;; that file isn't in a temp buffer, restore point to
1810 ;; where it was.
1811 (when original-point
1812 (goto-char original-point))
1813
1814 (setq file-finished nil)
1815 (setq new (next-file first-time t))
1816
1817 ;; If NEW is non-nil, we got a temp buffer,
1818 ;; and NEW is the file name.
1819 (when (or messaged
1820 (and (not first-time)
1821 (> baud-rate search-slow-speed)
1822 (setq messaged t)))
1823 (message "Scanning file %s..." (or new buffer-file-name)))
1824
1825 (setq first-time nil)
1826 (setq original-point (if new nil (point)))
1827 (goto-char (point-min)))
1828
1829 ;; If we visited it in a temp buffer, visit it now for real.
1830 (if new
1831 (let ((pos (point)))
1832 (erase-buffer)
1833 (set-buffer (find-file-noselect new))
1834 (setq new nil) ;No longer in a temp buffer.
1835 (widen)
1836 (goto-char pos))
1837 (push-mark original-point t))
1838
1839 (switch-to-buffer (current-buffer))
1840
1841 ;; Now operate on the file.
1842 ;; If value is non-nil, continue to scan the next file.
1843 (tags-loop-eval tags-loop-operate))
1844 (setq file-finished t))
1845 (and messaged
1846 (null tags-loop-operate)
1847 (message "Scanning file %s...found" buffer-file-name))))
1848
1849 ;;;###autoload
1850 (defun tags-search (regexp &optional file-list-form)
1851 "Search through all files listed in tags table for match for REGEXP.
1852 Stops when a match is found.
1853 To continue searching for next match, use command \\[tags-loop-continue].
1854
1855 If FILE-LIST-FORM is non-nil, it should be a form that, when
1856 evaluated, will return a list of file names. The search will be
1857 restricted to these files.
1858
1859 Also see the documentation of the `tags-file-name' variable."
1860 (interactive "sTags search (regexp): ")
1861 (if (and (equal regexp "")
1862 (eq (car tags-loop-scan) 're-search-forward)
1863 (null tags-loop-operate))
1864 ;; Continue last tags-search as if by M-,.
1865 (tags-loop-continue nil)
1866 (setq tags-loop-scan `(re-search-forward ',regexp nil t)
1867 tags-loop-operate nil)
1868 (tags-loop-continue (or file-list-form t))))
1869
1870 ;;;###autoload
1871 (defun tags-query-replace (from to &optional delimited file-list-form)
1872 "Do `query-replace-regexp' of FROM with TO on all files listed in tags table.
1873 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1874 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
1875 with the command \\[tags-loop-continue].
1876 Fourth arg FILE-LIST-FORM non-nil means initialize the replacement loop.
1877 Fifth and sixth arguments START and END are accepted, for compatibility
1878 with `query-replace-regexp', and ignored.
1879
1880 If FILE-LIST-FORM is non-nil, it is a form to evaluate to
1881 produce the list of files to search.
1882
1883 See also the documentation of the variable `tags-file-name'."
1884 (interactive (query-replace-read-args "Tags query replace (regexp)" t t))
1885 (setq tags-loop-scan `(let ,(unless (equal from (downcase from))
1886 '((case-fold-search nil)))
1887 (if (re-search-forward ',from nil t)
1888 ;; When we find a match, move back
1889 ;; to the beginning of it so perform-replace
1890 ;; will see it.
1891 (goto-char (match-beginning 0))))
1892 tags-loop-operate `(perform-replace ',from ',to t t ',delimited
1893 nil multi-query-replace-map))
1894 (tags-loop-continue (or file-list-form t)))
1895 \f
1896 (defun tags-complete-tags-table-file (string predicate what) ; Doc string?
1897 (save-excursion
1898 ;; If we need to ask for the tag table, allow that.
1899 (let ((enable-recursive-minibuffers t))
1900 (visit-tags-table-buffer))
1901 (if (eq what t)
1902 (all-completions string (tags-table-files) predicate)
1903 (try-completion string (tags-table-files) predicate))))
1904
1905 ;;;###autoload
1906 (defun list-tags (file &optional _next-match)
1907 "Display list of tags in file FILE.
1908 This searches only the first table in the list, and no included tables.
1909 FILE should be as it appeared in the `etags' command, usually without a
1910 directory specification."
1911 (interactive (list (completing-read "List tags in file: "
1912 'tags-complete-tags-table-file
1913 nil t nil)))
1914 (with-output-to-temp-buffer "*Tags List*"
1915 (princ "Tags in file `")
1916 (tags-with-face 'highlight (princ file))
1917 (princ "':\n\n")
1918 (save-excursion
1919 (let ((first-time t)
1920 (gotany nil))
1921 (while (visit-tags-table-buffer (not first-time))
1922 (setq first-time nil)
1923 (if (funcall list-tags-function file)
1924 (setq gotany t)))
1925 (or gotany
1926 (user-error "File %s not in current tags tables" file)))))
1927 (with-current-buffer "*Tags List*"
1928 (require 'apropos)
1929 (with-no-warnings
1930 (apropos-mode))
1931 (setq buffer-read-only t)))
1932
1933 ;;;###autoload
1934 (defun tags-apropos (regexp)
1935 "Display list of all tags in tags table REGEXP matches."
1936 (declare (obsolete xref-find-apropos "25.1"))
1937 (interactive "sTags apropos (regexp): ")
1938 (with-output-to-temp-buffer "*Tags List*"
1939 (princ "Click mouse-2 to follow tags.\n\nTags matching regexp `")
1940 (tags-with-face 'highlight (princ regexp))
1941 (princ "':\n\n")
1942 (save-excursion
1943 (let ((first-time t))
1944 (while (visit-tags-table-buffer (not first-time))
1945 (setq first-time nil)
1946 (funcall tags-apropos-function regexp))))
1947 (etags-tags-apropos-additional regexp))
1948 (with-current-buffer "*Tags List*"
1949 (eval-and-compile (require 'apropos))
1950 (apropos-mode)
1951 ;; apropos-mode is derived from fundamental-mode and it kills
1952 ;; all local variables.
1953 (setq buffer-read-only t)))
1954 \f
1955 ;; XXX Kludge interface.
1956
1957 (define-button-type 'tags-select-tags-table
1958 'action 'select-tags-table-select
1959 'follow-link t
1960 'help-echo "RET, t or mouse-2: select tags table")
1961
1962 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1963 ;;;###autoload
1964 (defun select-tags-table ()
1965 "Select a tags table file from a menu of those you have already used.
1966 The list of tags tables to select from is stored in `tags-table-set-list';
1967 see the doc of that variable if you want to add names to the list."
1968 (interactive)
1969 (pop-to-buffer "*Tags Table List*")
1970 (setq buffer-read-only nil
1971 buffer-undo-list t)
1972 (erase-buffer)
1973 (let ((set-list tags-table-set-list)
1974 (desired-point nil)
1975 b)
1976 (when tags-table-list
1977 (setq desired-point (point-marker))
1978 (setq b (point))
1979 (princ (mapcar 'abbreviate-file-name tags-table-list) (current-buffer))
1980 (make-text-button b (point) 'type 'tags-select-tags-table
1981 'etags-table (car tags-table-list))
1982 (insert "\n"))
1983 (while set-list
1984 (unless (eq (car set-list) tags-table-list)
1985 (setq b (point))
1986 (princ (mapcar 'abbreviate-file-name (car set-list)) (current-buffer))
1987 (make-text-button b (point) 'type 'tags-select-tags-table
1988 'etags-table (car (car set-list)))
1989 (insert "\n"))
1990 (setq set-list (cdr set-list)))
1991 (when tags-file-name
1992 (or desired-point
1993 (setq desired-point (point-marker)))
1994 (setq b (point))
1995 (insert (abbreviate-file-name tags-file-name))
1996 (make-text-button b (point) 'type 'tags-select-tags-table
1997 'etags-table tags-file-name)
1998 (insert "\n"))
1999 (setq set-list (delete tags-file-name
2000 (apply 'nconc (cons (copy-sequence tags-table-list)
2001 (mapcar 'copy-sequence
2002 tags-table-set-list)))))
2003 (while set-list
2004 (setq b (point))
2005 (insert (abbreviate-file-name (car set-list)))
2006 (make-text-button b (point) 'type 'tags-select-tags-table
2007 'etags-table (car set-list))
2008 (insert "\n")
2009 (setq set-list (delete (car set-list) set-list)))
2010 (goto-char (point-min))
2011 (insert-before-markers
2012 "Type `t' to select a tags table or set of tags tables:\n\n")
2013 (if desired-point
2014 (goto-char desired-point))
2015 (set-window-start (selected-window) 1 t))
2016 (set-buffer-modified-p nil)
2017 (select-tags-table-mode))
2018
2019 (defvar select-tags-table-mode-map ; Doc string?
2020 (let ((map (make-sparse-keymap)))
2021 (set-keymap-parent map button-buffer-map)
2022 (define-key map "t" 'push-button)
2023 (define-key map " " 'next-line)
2024 (define-key map "\^?" 'previous-line)
2025 (define-key map "n" 'next-line)
2026 (define-key map "p" 'previous-line)
2027 (define-key map "q" 'select-tags-table-quit)
2028 map))
2029
2030 (define-derived-mode select-tags-table-mode special-mode "Select Tags Table"
2031 "Major mode for choosing a current tags table among those already loaded."
2032 (setq buffer-read-only t))
2033
2034 (defun select-tags-table-select (button)
2035 "Select the tags table named on this line."
2036 (interactive (list (or (button-at (line-beginning-position))
2037 (error "No tags table on current line"))))
2038 (let ((name (button-get button 'etags-table)))
2039 (visit-tags-table name)
2040 (select-tags-table-quit)
2041 (message "Tags table now %s" name)))
2042
2043 (defun select-tags-table-quit ()
2044 "Kill the buffer and delete the selected window."
2045 (interactive)
2046 (quit-window t (selected-window)))
2047 \f
2048 ;;;###autoload
2049 (defun complete-tag ()
2050 "Perform tags completion on the text around point.
2051 Completes to the set of names listed in the current tags table.
2052 The string to complete is chosen in the same way as the default
2053 for \\[find-tag] (which see)."
2054 (interactive)
2055 (or tags-table-list
2056 tags-file-name
2057 (user-error "%s"
2058 (substitute-command-keys
2059 "No tags table loaded; try \\[visit-tags-table]")))
2060 (let ((comp-data (tags-completion-at-point-function)))
2061 (if (null comp-data)
2062 (user-error "Nothing to complete")
2063 (completion-in-region (car comp-data) (cadr comp-data)
2064 (nth 2 comp-data)
2065 (plist-get (nthcdr 3 comp-data) :predicate)))))
2066
2067 \f
2068 ;;; Xref backend
2069
2070 ;; Stop searching if we find more than xref-limit matches, as the xref
2071 ;; infrastructure is not designed to handle very long lists.
2072 ;; Switching to some kind of lazy list might be better, but hopefully
2073 ;; we hit the limit rarely.
2074 (defconst etags--xref-limit 1000)
2075
2076 (defvar etags-xref-find-definitions-tag-order '(tag-exact-match-p
2077 tag-implicit-name-match-p
2078 tag-symbol-match-p)
2079 "Tag order used in `etags-xref-find' to look for definitions.")
2080
2081 ;;;###autoload
2082 (defun etags-xref-find (action id)
2083 (pcase action
2084 (`definitions (etags--xref-find-definitions id))
2085 (`references
2086 (let ((dirs (if tags-table-list
2087 (mapcar #'file-name-directory tags-table-list)
2088 ;; If no tags files are loaded, prompt for the dir.
2089 (list (read-directory-name "In directory: " nil nil t)))))
2090 (cl-mapcan
2091 (lambda (dir)
2092 (xref-collect-references id dir))
2093 dirs)))
2094 (`apropos (etags--xref-find-definitions id t))))
2095
2096 (defun etags--xref-find-definitions (pattern &optional regexp?)
2097 ;; This emulates the behavior of `find-tag-in-order' but instead of
2098 ;; returning one match at a time all matches are returned as list.
2099 ;; NOTE: find-tag-tag-order is typically a buffer-local variable.
2100 (let* ((xrefs '())
2101 (first-time t)
2102 (search-fun (if regexp? #'re-search-forward #'search-forward))
2103 (marks (make-hash-table :test 'equal))
2104 (case-fold-search (if (memq tags-case-fold-search '(nil t))
2105 tags-case-fold-search
2106 case-fold-search)))
2107 (save-excursion
2108 (while (visit-tags-table-buffer (not first-time))
2109 (setq first-time nil)
2110 (dolist (order-fun (cond (regexp? find-tag-regexp-tag-order)
2111 (t etags-xref-find-definitions-tag-order)))
2112 (goto-char (point-min))
2113 (while (and (funcall search-fun pattern nil t)
2114 (< (hash-table-count marks) etags--xref-limit))
2115 (when (funcall order-fun pattern)
2116 (beginning-of-line)
2117 (pcase-let* ((tag-info (etags-snarf-tag))
2118 (`(,hint ,line . _) tag-info))
2119 (unless (eq hint t) ; hint==t if we are in a filename line
2120 (let* ((file (file-of-tag))
2121 (mark-key (cons file line)))
2122 (unless (gethash mark-key marks)
2123 (let ((loc (xref-make-etags-location
2124 tag-info (expand-file-name file))))
2125 (push (xref-make hint loc) xrefs)
2126 (puthash mark-key t marks)))))))))))
2127 (nreverse xrefs)))
2128
2129 (defclass xref-etags-location (xref-location)
2130 ((tag-info :type list :initarg :tag-info)
2131 (file :type string :initarg :file
2132 :reader xref-location-group))
2133 :documentation "Location of an etags tag.")
2134
2135 (defun xref-make-etags-location (tag-info file)
2136 (make-instance 'xref-etags-location :tag-info tag-info
2137 :file (expand-file-name file)))
2138
2139 (cl-defmethod xref-location-marker ((l xref-etags-location))
2140 (with-slots (tag-info file) l
2141 (let ((buffer (find-file-noselect file)))
2142 (with-current-buffer buffer
2143 (etags-goto-tag-location tag-info)
2144 (point-marker)))))
2145
2146 (cl-defmethod xref-location-line ((l xref-etags-location))
2147 (with-slots (tag-info) l
2148 (nth 1 tag-info)))
2149
2150 \f
2151 (provide 'etags)
2152
2153 ;;; etags.el ends here