Merge from trunk
[emacs.git] / lisp / progmodes / etags.el
blobc4883458e41dac05786aa074e273feefd16dafc3
1 ;;; etags.el --- etags facility for Emacs
3 ;; Copyright (C) 1985-1986, 1988-1989, 1992-1996, 1998, 2000-2011
4 ;; Free Software Foundation, Inc.
6 ;; Author: Roland McGrath <roland@gnu.org>
7 ;; Maintainer: FSF
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 (eval-when-compile
30 (require 'cl))
31 (require 'ring)
32 (require 'button)
34 ;;;###autoload
35 (defvar tags-file-name nil
36 "*File name of tags table.
37 To switch to a new tags table, setting this variable is sufficient.
38 If you set this variable, do not also set `tags-table-list'.
39 Use the `etags' program to make a tags table file.")
40 ;; Make M-x set-variable tags-file-name like M-x visit-tags-table.
41 ;;;###autoload (put 'tags-file-name 'variable-interactive (purecopy "fVisit tags table: "))
42 ;;;###autoload (put 'tags-file-name 'safe-local-variable 'stringp)
44 (defgroup etags nil "Tags tables."
45 :group 'tools)
47 ;;;###autoload
48 (defcustom tags-case-fold-search 'default
49 "*Whether tags operations should be case-sensitive.
50 A value of t means case-insensitive, a value of nil means case-sensitive.
51 Any other value means use the setting of `case-fold-search'."
52 :group 'etags
53 :type '(choice (const :tag "Case-sensitive" nil)
54 (const :tag "Case-insensitive" t)
55 (other :tag "Use default" default))
56 :version "21.1")
58 ;;;###autoload
59 ;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
60 (defcustom tags-table-list nil
61 "*List of file names of tags tables to search.
62 An element that is a directory means the file \"TAGS\" in that directory.
63 To switch to a new list of tags tables, setting this variable is sufficient.
64 If you set this variable, do not also set `tags-file-name'.
65 Use the `etags' program to make a tags table file."
66 :group 'etags
67 :type '(repeat file))
69 ;;;###autoload
70 (defcustom tags-compression-info-list
71 (purecopy '("" ".Z" ".bz2" ".gz" ".xz" ".tgz"))
72 "*List of extensions tried by etags when jka-compr is used.
73 An empty string means search the non-compressed file.
74 These extensions will be tried only if jka-compr was activated
75 \(i.e. via customize of `auto-compression-mode' or by calling the function
76 `auto-compression-mode')."
77 :version "24.1" ; added xz
78 :type '(repeat string)
79 :group 'etags)
81 ;; !!! tags-compression-info-list should probably be replaced by access
82 ;; to directory list and matching jka-compr-compression-info-list. Currently,
83 ;; this implementation forces each modification of
84 ;; jka-compr-compression-info-list to be reflected in this var.
85 ;; An alternative could be to say that introducing a special
86 ;; element in this list (e.g. t) means : try at this point
87 ;; using directory listing and regexp matching using
88 ;; jka-compr-compression-info-list.
91 ;;;###autoload
92 (defcustom tags-add-tables 'ask-user
93 "*Control whether to add a new tags table to the current list.
94 t means do; nil means don't (always start a new list).
95 Any other value means ask the user whether to add a new tags table
96 to the current list (as opposed to starting a new list)."
97 :group 'etags
98 :type '(choice (const :tag "Do" t)
99 (const :tag "Don't" nil)
100 (other :tag "Ask" ask-user)))
102 (defcustom tags-revert-without-query nil
103 "*Non-nil means reread a TAGS table without querying, if it has changed."
104 :group 'etags
105 :type 'boolean)
107 (defvar tags-table-computed-list nil
108 "List of tags tables to search, computed from `tags-table-list'.
109 This includes tables implicitly included by other tables. The list is not
110 always complete: the included tables of a table are not known until that
111 table is read into core. An element that is t is a placeholder
112 indicating that the preceding element is a table that has not been read
113 into core and might contain included tables to search.
114 See `tags-table-check-computed-list'.")
116 (defvar tags-table-computed-list-for nil
117 "Value of `tags-table-list' that `tags-table-computed-list' corresponds to.
118 If `tags-table-list' changes, `tags-table-computed-list' is thrown away and
119 recomputed; see `tags-table-check-computed-list'.")
121 (defvar tags-table-list-pointer nil
122 "Pointer into `tags-table-computed-list' for the current state of searching.
123 Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
125 (defvar tags-table-list-started-at nil
126 "Pointer into `tags-table-computed-list', where the current search started.")
128 (defvar tags-table-set-list nil
129 "List of sets of tags table which have been used together in the past.
130 Each element is a list of strings which are file names.")
132 ;;;###autoload
133 (defcustom find-tag-hook nil
134 "*Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'.
135 The value in the buffer in which \\[find-tag] is done is used,
136 not the value in the buffer \\[find-tag] goes to."
137 :group 'etags
138 :type 'hook)
140 ;;;###autoload
141 (defcustom find-tag-default-function nil
142 "*A function of no arguments used by \\[find-tag] to pick a default tag.
143 If nil, and the symbol that is the value of `major-mode'
144 has a `find-tag-default-function' property (see `put'), that is used.
145 Otherwise, `find-tag-default' is used."
146 :group 'etags
147 :type '(choice (const nil) function))
149 (defcustom find-tag-marker-ring-length 16
150 "*Length of marker rings `find-tag-marker-ring' and `tags-location-ring'."
151 :group 'etags
152 :type 'integer
153 :version "20.3")
155 (defcustom tags-tag-face 'default
156 "*Face for tags in the output of `tags-apropos'."
157 :group 'etags
158 :type 'face
159 :version "21.1")
161 (defcustom tags-apropos-verbose nil
162 "If non-nil, print the name of the tags file in the *Tags List* buffer."
163 :group 'etags
164 :type 'boolean
165 :version "21.1")
167 (defcustom tags-apropos-additional-actions nil
168 "Specify additional actions for `tags-apropos'.
170 If non-nil, value should be a list of triples (TITLE FUNCTION
171 TO-SEARCH). For each triple, `tags-apropos' processes TO-SEARCH and
172 lists tags from it. TO-SEARCH should be an alist, obarray, or symbol.
173 If it is a symbol, the symbol's value is used.
174 TITLE, a string, is a title used to label the additional list of tags.
175 FUNCTION is a function to call when a symbol is selected in the
176 *Tags List* buffer. It will be called with one argument SYMBOL which
177 is the symbol being selected.
179 Example value:
181 '((\"Emacs Lisp\" Info-goto-emacs-command-node obarray)
182 (\"Common Lisp\" common-lisp-hyperspec common-lisp-hyperspec-obarray)
183 (\"SCWM\" scwm-documentation scwm-obarray))"
184 :group 'etags
185 :type '(repeat (list (string :tag "Title")
186 function
187 (sexp :tag "Tags to search")))
188 :version "21.1")
190 (defvar find-tag-marker-ring (make-ring find-tag-marker-ring-length)
191 "Ring of markers which are locations from which \\[find-tag] was invoked.")
193 (defvar default-tags-table-function nil
194 "If non-nil, a function to choose a default tags file for a buffer.
195 This function receives no arguments and should return the default
196 tags table file to use for the current buffer.")
198 (defvar tags-location-ring (make-ring find-tag-marker-ring-length)
199 "Ring of markers which are locations visited by \\[find-tag].
200 Pop back to the last location with \\[negative-argument] \\[find-tag].")
202 ;; Tags table state.
203 ;; These variables are local in tags table buffers.
205 (defvar tags-table-files nil
206 "List of file names covered by current tags table.
207 nil means it has not yet been computed; use `tags-table-files' to do so.")
209 (defvar tags-completion-table nil
210 "Obarray of tag names defined in current tags table.")
212 (defvar tags-included-tables nil
213 "List of tags tables included by the current tags table.")
215 (defvar next-file-list nil
216 "List of files for \\[next-file] to process.")
218 ;; Hooks for file formats.
220 (defvar tags-table-format-functions '(etags-recognize-tags-table
221 tags-recognize-empty-tags-table)
222 "Hook to be called in a tags table buffer to identify the type of tags table.
223 The functions are called in order, with no arguments,
224 until one returns non-nil. The function should make buffer-local bindings
225 of the format-parsing tags function variables if successful.")
227 (defvar file-of-tag-function nil
228 "Function to do the work of `file-of-tag' (which see).
229 One optional argument, a boolean specifying to return complete path (nil) or
230 relative path (non-nil).")
231 (defvar tags-table-files-function nil
232 "Function to do the work of `tags-table-files' (which see).")
233 (defvar tags-completion-table-function nil
234 "Function to build the `tags-completion-table'.")
235 (defvar snarf-tag-function nil
236 "Function to get info about a matched tag for `goto-tag-location-function'.
237 One optional argument, specifying to use explicit tag (non-nil) or not (nil).
238 The default is nil.")
239 (defvar goto-tag-location-function nil
240 "Function of to go to the location in the buffer specified by a tag.
241 One argument, the tag info returned by `snarf-tag-function'.")
242 (defvar find-tag-regexp-search-function nil
243 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
244 (defvar find-tag-regexp-tag-order nil
245 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
246 (defvar find-tag-regexp-next-line-after-failure-p nil
247 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
248 (defvar find-tag-search-function nil
249 "Search function passed to `find-tag-in-order' for finding a tag.")
250 (defvar find-tag-tag-order nil
251 "Tag order passed to `find-tag-in-order' for finding a tag.")
252 (defvar find-tag-next-line-after-failure-p nil
253 "Flag passed to `find-tag-in-order' for finding a tag.")
254 (defvar list-tags-function nil
255 "Function to do the work of `list-tags' (which see).")
256 (defvar tags-apropos-function nil
257 "Function to do the work of `tags-apropos' (which see).")
258 (defvar tags-included-tables-function nil
259 "Function to do the work of `tags-included-tables' (which see).")
260 (defvar verify-tags-table-function nil
261 "Function to return t if current buffer contains valid tags file.")
263 (defun initialize-new-tags-table ()
264 "Initialize the tags table in the current buffer.
265 Return non-nil if it is a valid tags table, and
266 in that case, also make the tags table state variables
267 buffer-local and set them to nil."
268 (set (make-local-variable 'tags-table-files) nil)
269 (set (make-local-variable 'tags-completion-table) nil)
270 (set (make-local-variable 'tags-included-tables) nil)
271 ;; We used to initialize find-tag-marker-ring and tags-location-ring
272 ;; here, to new empty rings. But that is wrong, because those
273 ;; are global.
275 ;; Value is t if we have found a valid tags table buffer.
276 (run-hook-with-args-until-success 'tags-table-format-functions))
278 ;;;###autoload
279 (defun tags-table-mode ()
280 "Major mode for tags table file buffers."
281 (interactive)
282 (setq major-mode 'tags-table-mode ;FIXME: Use define-derived-mode.
283 mode-name "Tags Table"
284 buffer-undo-list t)
285 (initialize-new-tags-table))
287 ;;;###autoload
288 (defun visit-tags-table (file &optional local)
289 "Tell tags commands to use tags table file FILE.
290 FILE should be the name of a file created with the `etags' program.
291 A directory name is ok too; it means file TAGS in that directory.
293 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
294 With a prefix arg, set the buffer-local value instead.
295 When you find a tag with \\[find-tag], the buffer it finds the tag
296 in is given a local value of this variable which is the name of the tags
297 file the tag was in."
298 (interactive (list (read-file-name "Visit tags table (default TAGS): "
299 default-directory
300 (expand-file-name "TAGS"
301 default-directory)
303 current-prefix-arg))
304 (or (stringp file) (signal 'wrong-type-argument (list 'stringp file)))
305 ;; Bind tags-file-name so we can control below whether the local or
306 ;; global value gets set.
307 ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
308 ;; initialize a buffer for FILE and set tags-file-name to the
309 ;; fully-expanded name.
310 (let ((tags-file-name file))
311 (save-excursion
312 (or (visit-tags-table-buffer file)
313 (signal 'file-error (list "Visiting tags table"
314 "file does not exist"
315 file)))
316 ;; Set FILE to the expanded name.
317 (setq file tags-file-name)))
318 (if local
319 ;; Set the local value of tags-file-name.
320 (set (make-local-variable 'tags-file-name) file)
321 ;; Set the global value of tags-file-name.
322 (setq-default tags-file-name file)))
324 (defun tags-table-check-computed-list ()
325 "Compute `tags-table-computed-list' from `tags-table-list' if necessary."
326 (let ((expanded-list (mapcar 'tags-expand-table-name tags-table-list)))
327 (or (equal tags-table-computed-list-for expanded-list)
328 ;; The list (or default-directory) has changed since last computed.
329 (let* ((compute-for (mapcar 'copy-sequence expanded-list))
330 (tables (copy-sequence compute-for)) ;Mutated in the loop.
331 (computed nil)
332 table-buffer)
334 (while tables
335 (setq computed (cons (car tables) computed)
336 table-buffer (get-file-buffer (car tables)))
337 (if (and table-buffer
338 ;; There is a buffer visiting the file. Now make sure
339 ;; it is initialized as a tag table buffer.
340 (save-excursion
341 (tags-verify-table (buffer-file-name table-buffer))))
342 (with-current-buffer table-buffer
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 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 (error "%s"
558 (substitute-command-keys
559 (concat "No tags table in use; "
560 "use \\[visit-tags-table] to select one")))))
562 ((eq t cont)
563 ;; Find the next table.
564 (if (tags-next-table)
565 ;; Skip over nonexistent files.
566 (while (and (not (or (get-file-buffer tags-file-name)
567 (file-exists-p tags-file-name)))
568 (tags-next-table)))))
571 ;; Pick a table out of our hat.
572 (tags-table-check-computed-list) ;Get it up to date, we might use it.
573 (setq tags-file-name
575 ;; If passed a string, use that.
576 (if (stringp cont)
577 (prog1 cont
578 (setq cont nil)))
579 ;; First, try a local variable.
580 (cdr (assq 'tags-file-name (buffer-local-variables)))
581 ;; Second, try a user-specified function to guess.
582 (and default-tags-table-function
583 (funcall default-tags-table-function))
584 ;; Third, look for a tags table that contains tags for the
585 ;; current buffer's file. If one is found, the lists will
586 ;; be frobnicated, and CONT will be set non-nil so we don't
587 ;; do it below.
588 (and buffer-file-name
590 ;; First check only tables already in buffers.
591 (tags-table-including buffer-file-name t)
592 ;; Since that didn't find any, now do the
593 ;; expensive version: reading new files.
594 (tags-table-including buffer-file-name nil)))
595 ;; Fourth, use the user variable tags-file-name, if it is
596 ;; not already in the current list.
597 (and tags-file-name
598 (not (tags-table-list-member tags-file-name
599 tags-table-computed-list))
600 tags-file-name)
601 ;; Fifth, use the user variable giving the table list.
602 ;; Find the first element of the list that actually exists.
603 (let ((list tags-table-list)
604 file)
605 (while (and list
606 (setq file (tags-expand-table-name (car list)))
607 (not (get-file-buffer file))
608 (not (file-exists-p file)))
609 (setq list (cdr list)))
610 (car list))
611 ;; Finally, prompt the user for a file name.
612 (expand-file-name
613 (read-file-name "Visit tags table (default TAGS): "
614 default-directory
615 "TAGS"
616 t))))))
618 ;; Expand the table name into a full file name.
619 (setq tags-file-name (tags-expand-table-name tags-file-name))
621 (unless (and (eq cont t) (null tags-table-list-pointer))
622 ;; Verify that tags-file-name names a valid tags table.
623 ;; Bind another variable with the value of tags-file-name
624 ;; before we switch buffers, in case tags-file-name is buffer-local.
625 (let ((curbuf (current-buffer))
626 (local-tags-file-name tags-file-name))
627 (if (tags-verify-table local-tags-file-name)
629 ;; We have a valid tags table.
630 (progn
631 ;; Bury the tags table buffer so it
632 ;; doesn't get in the user's way.
633 (bury-buffer (current-buffer))
635 ;; If this was a new table selection (CONT is nil), make
636 ;; sure tags-table-list includes the chosen table, and
637 ;; update the list pointer variables.
638 (or cont
639 ;; Look in the list for the table we chose.
640 (let ((found (tags-table-list-member
641 local-tags-file-name
642 tags-table-computed-list)))
643 (if found
644 ;; There it is. Just switch to it.
645 (setq tags-table-list-pointer found
646 tags-table-list-started-at found)
648 ;; The table is not in the current set.
649 ;; Try to find it in another previously used set.
650 (let ((sets tags-table-set-list))
651 (while (and sets
652 (not (tags-table-list-member
653 local-tags-file-name
654 (car sets))))
655 (setq sets (cdr sets)))
656 (if sets
657 ;; Found in some other set. Switch to that set.
658 (progn
659 (or (memq tags-table-list tags-table-set-list)
660 ;; Save the current list.
661 (setq tags-table-set-list
662 (cons tags-table-list
663 tags-table-set-list)))
664 (setq tags-table-list (car sets)))
666 ;; Not found in any existing set.
667 (if (and tags-table-list
668 (or (eq t tags-add-tables)
669 (and tags-add-tables
670 (y-or-n-p
671 (concat "Keep current list of "
672 "tags tables also? ")))))
673 ;; Add it to the current list.
674 (setq tags-table-list (cons local-tags-file-name
675 tags-table-list))
677 ;; Make a fresh list, and store the old one.
678 (message "Starting a new list of tags tables")
679 (or (null tags-table-list)
680 (memq tags-table-list tags-table-set-list)
681 (setq tags-table-set-list
682 (cons tags-table-list
683 tags-table-set-list)))
684 ;; Clear out buffers holding old tables.
685 (dolist (table tags-table-list)
686 ;; The list can contain items t.
687 (if (stringp table)
688 (let ((buffer (find-buffer-visiting table)))
689 (if buffer
690 (kill-buffer buffer)))))
691 (setq tags-table-list (list local-tags-file-name))))
693 ;; Recompute tags-table-computed-list.
694 (tags-table-check-computed-list)
695 ;; Set the tags table list state variables to start
696 ;; over from tags-table-computed-list.
697 (setq tags-table-list-started-at tags-table-computed-list
698 tags-table-list-pointer
699 tags-table-computed-list)))))
701 ;; Return of t says the tags table is valid.
704 ;; The buffer was not valid. Don't use it again.
705 (set-buffer curbuf)
706 (kill-local-variable 'tags-file-name)
707 (if (eq local-tags-file-name tags-file-name)
708 (setq tags-file-name nil))
709 (error "File %s is not a valid tags table" local-tags-file-name)))))
711 (defun tags-reset-tags-tables ()
712 "Reset tags state to cancel effect of any previous \\[visit-tags-table] or \\[find-tag]."
713 (interactive)
714 ;; Clear out the markers we are throwing away.
715 (let ((i 0))
716 (while (< i find-tag-marker-ring-length)
717 (if (aref (cddr tags-location-ring) i)
718 (set-marker (aref (cddr tags-location-ring) i) nil))
719 (if (aref (cddr find-tag-marker-ring) i)
720 (set-marker (aref (cddr find-tag-marker-ring) i) nil))
721 (setq i (1+ i))))
722 (setq tags-file-name nil
723 tags-location-ring (make-ring find-tag-marker-ring-length)
724 find-tag-marker-ring (make-ring find-tag-marker-ring-length)
725 tags-table-list nil
726 tags-table-computed-list nil
727 tags-table-computed-list-for nil
728 tags-table-list-pointer nil
729 tags-table-list-started-at nil
730 tags-table-set-list nil))
732 (defun file-of-tag (&optional relative)
733 "Return the file name of the file whose tags point is within.
734 Assumes the tags table is the current buffer.
735 If RELATIVE is non-nil, file name returned is relative to tags
736 table file's directory. If RELATIVE is nil, file name returned
737 is complete."
738 (funcall file-of-tag-function relative))
740 ;;;###autoload
741 (defun tags-table-files ()
742 "Return a list of files in the current tags table.
743 Assumes the tags table is the current buffer. The file names are returned
744 as they appeared in the `etags' command that created the table, usually
745 without directory names."
746 (or tags-table-files
747 (setq tags-table-files
748 (funcall tags-table-files-function))))
750 (defun tags-included-tables ()
751 "Return a list of tags tables included by the current table.
752 Assumes the tags table is the current buffer."
753 (or tags-included-tables
754 (setq tags-included-tables (funcall tags-included-tables-function))))
756 (defun tags-completion-table ()
757 "Build `tags-completion-table' on demand.
758 The tags included in the completion table are those in the current
759 tags table and its (recursively) included tags tables."
760 (or tags-completion-table
761 ;; No cached value for this buffer.
762 (condition-case ()
763 (let (current-table combined-table)
764 (message "Making tags completion table for %s..." buffer-file-name)
765 (save-excursion
766 ;; Iterate over the current list of tags tables.
767 (while (visit-tags-table-buffer (and combined-table t))
768 ;; Find possible completions in this table.
769 (setq current-table (funcall tags-completion-table-function))
770 ;; Merge this buffer's completions into the combined table.
771 (if combined-table
772 (mapatoms
773 (lambda (sym) (intern (symbol-name sym) combined-table))
774 current-table)
775 (setq combined-table current-table))))
776 (message "Making tags completion table for %s...done"
777 buffer-file-name)
778 ;; Cache the result in a buffer-local variable.
779 (setq tags-completion-table combined-table))
780 (quit (message "Tags completion table construction aborted.")
781 (setq tags-completion-table nil)))))
783 (defun tags-lazy-completion-table ()
784 (lexical-let ((buf (current-buffer)))
785 (lambda (string pred action)
786 (with-current-buffer buf
787 (save-excursion
788 ;; If we need to ask for the tag table, allow that.
789 (let ((enable-recursive-minibuffers t))
790 (visit-tags-table-buffer))
791 (complete-with-action action (tags-completion-table) string pred))))))
793 ;;;###autoload (defun tags-completion-at-point-function ()
794 ;;;###autoload (if (or tags-table-list tags-file-name)
795 ;;;###autoload (progn
796 ;;;###autoload (load "etags")
797 ;;;###autoload (tags-completion-at-point-function))))
799 (defun tags-completion-at-point-function ()
800 "Using tags, return a completion table for the text around point.
801 If no tags table is loaded, do nothing and return nil."
802 (when (or tags-table-list tags-file-name)
803 (let ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
804 tags-case-fold-search
805 case-fold-search))
806 (pattern (funcall (or find-tag-default-function
807 (get major-mode 'find-tag-default-function)
808 'find-tag-default)))
809 beg)
810 (when pattern
811 (save-excursion
812 (search-backward pattern) ;FIXME: will fail if we're inside pattern.
813 (setq beg (point))
814 (forward-char (length pattern))
815 (list beg (point) (tags-lazy-completion-table) :exclusive 'no))))))
817 (defun find-tag-tag (string)
818 "Read a tag name, with defaulting and completion."
819 (let* ((completion-ignore-case (if (memq tags-case-fold-search '(t nil))
820 tags-case-fold-search
821 case-fold-search))
822 (default (funcall (or find-tag-default-function
823 (get major-mode 'find-tag-default-function)
824 'find-tag-default)))
825 (spec (completing-read (if default
826 (format "%s (default %s): "
827 (substring string 0 (string-match "[ :]+\\'" string))
828 default)
829 string)
830 (tags-lazy-completion-table)
831 nil nil nil nil default)))
832 (if (equal spec "")
833 (or default (error "There is no default tag"))
834 spec)))
836 (defvar last-tag nil
837 "Last tag found by \\[find-tag].")
839 (defun find-tag-interactive (prompt &optional no-default)
840 "Get interactive arguments for tag functions.
841 The functions using this are `find-tag-noselect',
842 `find-tag-other-window', and `find-tag-regexp'."
843 (if (and current-prefix-arg last-tag)
844 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
847 (list (if no-default
848 (read-string prompt)
849 (find-tag-tag prompt)))))
851 (defvar find-tag-history nil) ; Doc string?
853 ;; Dynamic bondage:
854 (defvar etags-case-fold-search)
855 (defvar etags-syntax-table)
856 (defvar local-find-tag-hook)
858 ;;;###autoload
859 (defun find-tag-noselect (tagname &optional next-p regexp-p)
860 "Find tag (in current tags table) whose name contains TAGNAME.
861 Returns the buffer containing the tag's definition and moves its point there,
862 but does not select the buffer.
863 The default for TAGNAME is the expression in the buffer near point.
865 If second arg NEXT-P is t (interactively, with prefix arg), search for
866 another tag that matches the last tagname or regexp used. When there are
867 multiple matches for a tag, more exact matches are found first. If NEXT-P
868 is the atom `-' (interactively, with prefix arg that is a negative number
869 or just \\[negative-argument]), pop back to the previous tag gone to.
871 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
873 A marker representing the point when this command is invoked is pushed
874 onto a ring and may be popped back to with \\[pop-tag-mark].
875 Contrast this with the ring of marks gone to by the command.
877 See documentation of variable `tags-file-name'."
878 (interactive (find-tag-interactive "Find tag: "))
880 (setq find-tag-history (cons tagname find-tag-history))
881 ;; Save the current buffer's value of `find-tag-hook' before
882 ;; selecting the tags table buffer. For the same reason, save value
883 ;; of `tags-file-name' in case it has a buffer-local value.
884 (let ((local-find-tag-hook find-tag-hook))
885 (if (eq '- next-p)
886 ;; Pop back to a previous location.
887 (if (ring-empty-p tags-location-ring)
888 (error "No previous tag locations")
889 (let ((marker (ring-remove tags-location-ring 0)))
890 (prog1
891 ;; Move to the saved location.
892 (set-buffer (or (marker-buffer marker)
893 (error "The marked buffer has been deleted")))
894 (goto-char (marker-position marker))
895 ;; Kill that marker so it doesn't slow down editing.
896 (set-marker marker nil nil)
897 ;; Run the user's hook. Do we really want to do this for pop?
898 (run-hooks 'local-find-tag-hook))))
899 ;; Record whence we came.
900 (ring-insert find-tag-marker-ring (point-marker))
901 (if (and next-p last-tag)
902 ;; Find the same table we last used.
903 (visit-tags-table-buffer 'same)
904 ;; Pick a table to use.
905 (visit-tags-table-buffer)
906 ;; Record TAGNAME for a future call with NEXT-P non-nil.
907 (setq last-tag tagname))
908 ;; Record the location so we can pop back to it later.
909 (let ((marker (make-marker)))
910 (with-current-buffer
911 ;; find-tag-in-order does the real work.
912 (find-tag-in-order
913 (if (and next-p last-tag) last-tag tagname)
914 (if regexp-p
915 find-tag-regexp-search-function
916 find-tag-search-function)
917 (if regexp-p
918 find-tag-regexp-tag-order
919 find-tag-tag-order)
920 (if regexp-p
921 find-tag-regexp-next-line-after-failure-p
922 find-tag-next-line-after-failure-p)
923 (if regexp-p "matching" "containing")
924 (or (not next-p) (not last-tag)))
925 (set-marker marker (point))
926 (run-hooks 'local-find-tag-hook)
927 (ring-insert tags-location-ring marker)
928 (current-buffer))))))
930 ;;;###autoload
931 (defun find-tag (tagname &optional next-p regexp-p)
932 "Find tag (in current tags table) whose name contains TAGNAME.
933 Select the buffer containing the tag's definition, and move point there.
934 The default for TAGNAME is the expression in the buffer around or before point.
936 If second arg NEXT-P is t (interactively, with prefix arg), search for
937 another tag that matches the last tagname or regexp used. When there are
938 multiple matches for a tag, more exact matches are found first. If NEXT-P
939 is the atom `-' (interactively, with prefix arg that is a negative number
940 or just \\[negative-argument]), pop back to the previous tag gone to.
942 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
944 A marker representing the point when this command is invoked is pushed
945 onto a ring and may be popped back to with \\[pop-tag-mark].
946 Contrast this with the ring of marks gone to by the command.
948 See documentation of variable `tags-file-name'."
949 (interactive (find-tag-interactive "Find tag: "))
950 (let* ((buf (find-tag-noselect tagname next-p regexp-p))
951 (pos (with-current-buffer buf (point))))
952 (condition-case nil
953 (switch-to-buffer buf)
954 (error (pop-to-buffer buf)))
955 (goto-char pos)))
956 ;;;###autoload (define-key esc-map "." 'find-tag)
958 ;;;###autoload
959 (defun find-tag-other-window (tagname &optional next-p regexp-p other-frame)
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.
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.
971 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
973 Third arg OTHER-FRAME non-nil find tag on another frame.
975 A marker representing the point when this command is invoked is pushed
976 onto a ring and may be popped back to with \\[pop-tag-mark].
977 Contrast this with the ring of marks gone to by the command.
979 See documentation of variable `tags-file-name'."
980 (interactive (find-tag-interactive "Find tag other window: "))
982 ;; This hair is to deal with the case where the tag is found in the
983 ;; selected window's buffer; without the hair, point is moved in both
984 ;; windows. To prevent this, we save the selected window's point before
985 ;; doing find-tag-noselect, and restore it after.
986 (let* ((window-point (window-point (selected-window)))
987 (tagbuf (find-tag-noselect tagname next-p regexp-p))
988 (tagpoint (progn (set-buffer tagbuf) (point))))
989 (set-window-point (prog1
990 (selected-window)
991 (if other-frame
992 (pop-to-buffer-other-frame tagbuf)
993 (pop-to-buffer-other-window tagbuf))
994 ;; We have to set this new window's point; it
995 ;; might already have been displaying a
996 ;; different portion of tagbuf, in which case
997 ;; switch-to-buffer-other-window doesn't set
998 ;; the window's point from the buffer.
999 (set-window-point (selected-window) tagpoint))
1000 window-point)))
1001 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
1003 ;;;###autoload
1004 (defun find-tag-other-frame (tagname &optional next-p)
1005 "Find tag (in current tags table) whose name contains TAGNAME.
1006 Select the buffer containing the tag's definition in another frame, and
1007 move point there. The default for TAGNAME is the expression in the buffer
1008 around or before point.
1010 If second arg NEXT-P is t (interactively, with prefix arg), search for
1011 another tag that matches the last tagname or regexp used. When there are
1012 multiple matches for a tag, more exact matches are found first. If NEXT-P
1013 is negative (interactively, with prefix arg that is a negative number or
1014 just \\[negative-argument]), pop back to the previous tag gone to.
1016 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
1018 A marker representing the point when this command is invoked is pushed
1019 onto a ring and may be popped back to with \\[pop-tag-mark].
1020 Contrast this with the ring of marks gone to by the command.
1022 See documentation of variable `tags-file-name'."
1023 (interactive (find-tag-interactive "Find tag other frame: "))
1024 (find-tag-other-window tagname next-p t))
1025 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
1027 ;;;###autoload
1028 (defun find-tag-regexp (regexp &optional next-p other-window)
1029 "Find tag (in current tags table) whose name matches REGEXP.
1030 Select the buffer containing the tag's definition and move point there.
1032 If second arg NEXT-P is t (interactively, with prefix arg), search for
1033 another tag that matches the last tagname or regexp used. When there are
1034 multiple matches for a tag, more exact matches are found first. If NEXT-P
1035 is negative (interactively, with prefix arg that is a negative number or
1036 just \\[negative-argument]), pop back to the previous tag gone to.
1038 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
1040 A marker representing the point when this command is invoked is pushed
1041 onto a ring and may be popped back to with \\[pop-tag-mark].
1042 Contrast this with the ring of marks gone to by the command.
1044 See documentation of variable `tags-file-name'."
1045 (interactive (find-tag-interactive "Find tag regexp: " t))
1046 ;; We go through find-tag-other-window to do all the display hair there.
1047 (funcall (if other-window 'find-tag-other-window 'find-tag)
1048 regexp next-p t))
1049 ;;;###autoload (define-key esc-map [?\C-.] 'find-tag-regexp)
1051 ;;;###autoload (define-key esc-map "*" 'pop-tag-mark)
1053 ;;;###autoload
1054 (defun pop-tag-mark ()
1055 "Pop back to where \\[find-tag] was last invoked.
1057 This is distinct from invoking \\[find-tag] with a negative argument
1058 since that pops a stack of markers at which tags were found, not from
1059 where they were found."
1060 (interactive)
1061 (if (ring-empty-p find-tag-marker-ring)
1062 (error "No previous locations for find-tag invocation"))
1063 (let ((marker (ring-remove find-tag-marker-ring 0)))
1064 (switch-to-buffer (or (marker-buffer marker)
1065 (error "The marked buffer has been deleted")))
1066 (goto-char (marker-position marker))
1067 (set-marker marker nil nil)))
1069 (defvar tag-lines-already-matched nil
1070 "Matches remembered between calls.") ; Doc string: calls to what?
1072 (defun find-tag-in-order (pattern
1073 search-forward-func
1074 order
1075 next-line-after-failure-p
1076 matching
1077 first-search)
1078 "Internal tag-finding function.
1079 PATTERN is a string to pass to arg SEARCH-FORWARD-FUNC, and to any
1080 member of the function list ORDER. If ORDER is nil, use saved state
1081 to continue a previous search.
1083 Arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
1084 point should be moved to the next line.
1086 Arg MATCHING is a string, an English `-ing' word, to be used in an
1087 error message."
1088 ;; Algorithm is as follows:
1089 ;; For each qualifier-func in ORDER, go to beginning of tags file, and
1090 ;; perform inner loop: for each naive match for PATTERN found using
1091 ;; SEARCH-FORWARD-FUNC, qualify the naive match using qualifier-func. If
1092 ;; it qualifies, go to the specified line in the specified source file
1093 ;; and return. Qualified matches are remembered to avoid repetition.
1094 ;; State is saved so that the loop can be continued.
1095 (let (file ;name of file containing tag
1096 tag-info ;where to find the tag in FILE
1097 (first-table t)
1098 (tag-order order)
1099 (match-marker (make-marker))
1100 goto-func
1101 (case-fold-search (if (memq tags-case-fold-search '(nil t))
1102 tags-case-fold-search
1103 case-fold-search))
1105 (save-excursion
1107 (if first-search
1108 ;; This is the start of a search for a fresh tag.
1109 ;; Clear the list of tags matched by the previous search.
1110 ;; find-tag-noselect has already put us in the first tags table
1111 ;; buffer before we got called.
1112 (setq tag-lines-already-matched nil)
1113 ;; Continuing to search for the tag specified last time.
1114 ;; tag-lines-already-matched lists locations matched in previous
1115 ;; calls so we don't visit the same tag twice if it matches twice
1116 ;; during two passes with different qualification predicates.
1117 ;; Switch to the current tags table buffer.
1118 (visit-tags-table-buffer 'same))
1120 ;; Get a qualified match.
1121 (catch 'qualified-match-found
1123 ;; Iterate over the list of tags tables.
1124 (while (or first-table
1125 (visit-tags-table-buffer t))
1127 (and first-search first-table
1128 ;; Start at beginning of tags file.
1129 (goto-char (point-min)))
1131 (setq first-table nil)
1133 ;; Iterate over the list of ordering predicates.
1134 (while order
1135 (while (funcall search-forward-func pattern nil t)
1136 ;; Naive match found. Qualify the match.
1137 (and (funcall (car order) pattern)
1138 ;; Make sure it is not a previous qualified match.
1139 (not (member (set-marker match-marker (point-at-bol))
1140 tag-lines-already-matched))
1141 (throw 'qualified-match-found nil))
1142 (if next-line-after-failure-p
1143 (forward-line 1)))
1144 ;; Try the next flavor of match.
1145 (setq order (cdr order))
1146 (goto-char (point-min)))
1147 (setq order tag-order))
1148 ;; We throw out on match, so only get here if there were no matches.
1149 ;; Clear out the markers we use to avoid duplicate matches so they
1150 ;; don't slow down editting and are immediately available for GC.
1151 (while tag-lines-already-matched
1152 (set-marker (car tag-lines-already-matched) nil nil)
1153 (setq tag-lines-already-matched (cdr tag-lines-already-matched)))
1154 (set-marker match-marker nil nil)
1155 (error "No %stags %s %s" (if first-search "" "more ")
1156 matching pattern))
1158 ;; Found a tag; extract location info.
1159 (beginning-of-line)
1160 (setq tag-lines-already-matched (cons match-marker
1161 tag-lines-already-matched))
1162 ;; Expand the filename, using the tags table buffer's default-directory.
1163 ;; We should be able to search for file-name backwards in file-of-tag:
1164 ;; the beginning-of-line is ok except when positioned on a "file-name" tag.
1165 (setq file (expand-file-name
1166 (if (memq (car order) '(tag-exact-file-name-match-p
1167 tag-file-name-match-p
1168 tag-partial-file-name-match-p))
1169 (save-excursion (forward-line 1)
1170 (file-of-tag))
1171 (file-of-tag)))
1172 tag-info (funcall snarf-tag-function))
1174 ;; Get the local value in the tags table buffer before switching buffers.
1175 (setq goto-func goto-tag-location-function)
1176 (tag-find-file-of-tag-noselect file)
1177 (widen)
1178 (push-mark)
1179 (funcall goto-func tag-info)
1181 ;; Return the buffer where the tag was found.
1182 (current-buffer))))
1184 (defun tag-find-file-of-tag-noselect (file)
1185 "Find the right line in the specified FILE."
1186 ;; If interested in compressed-files, search files with extensions.
1187 ;; Otherwise, search only the real file.
1188 (let* ((buffer-search-extensions (if (featurep 'jka-compr)
1189 tags-compression-info-list
1190 '("")))
1191 the-buffer
1192 (file-search-extensions buffer-search-extensions))
1193 ;; search a buffer visiting the file with each possible extension
1194 ;; Note: there is a small inefficiency in find-buffer-visiting :
1195 ;; truename is computed even if not needed. Not too sure about this
1196 ;; but I suspect truename computation accesses the disk.
1197 ;; It is maybe a good idea to optimise this find-buffer-visiting.
1198 ;; An alternative would be to use only get-file-buffer
1199 ;; but this looks less "sure" to find the buffer for the file.
1200 (while (and (not the-buffer) buffer-search-extensions)
1201 (setq the-buffer (find-buffer-visiting (concat file (car buffer-search-extensions))))
1202 (setq buffer-search-extensions (cdr buffer-search-extensions)))
1203 ;; if found a buffer but file modified, ensure we re-read !
1204 (if (and the-buffer (not (verify-visited-file-modtime the-buffer)))
1205 (find-file-noselect (buffer-file-name the-buffer)))
1206 ;; if no buffer found, search for files with possible extensions on disk
1207 (while (and (not the-buffer) file-search-extensions)
1208 (if (not (file-exists-p (concat file (car file-search-extensions))))
1209 (setq file-search-extensions (cdr file-search-extensions))
1210 (setq the-buffer (find-file-noselect (concat file (car file-search-extensions))))))
1211 (if (not the-buffer)
1212 (if (featurep 'jka-compr)
1213 (error "File %s (with or without extensions %s) not found" file tags-compression-info-list)
1214 (error "File %s not found" file))
1215 (set-buffer the-buffer))))
1217 (defun tag-find-file-of-tag (file) ; Doc string?
1218 (let ((buf (tag-find-file-of-tag-noselect file)))
1219 (condition-case nil
1220 (switch-to-buffer buf)
1221 (error (pop-to-buffer buf)))))
1223 ;; `etags' TAGS file format support.
1225 (defun etags-recognize-tags-table ()
1226 "If `etags-verify-tags-table', make buffer-local format variables.
1227 If current buffer is a valid etags TAGS file, then give it
1228 buffer-local values of tags table format variables."
1229 (and (etags-verify-tags-table)
1230 ;; It is annoying to flash messages on the screen briefly,
1231 ;; and this message is not useful. -- rms
1232 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
1233 (mapc (lambda (elt) (set (make-local-variable (car elt)) (cdr elt)))
1234 '((file-of-tag-function . etags-file-of-tag)
1235 (tags-table-files-function . etags-tags-table-files)
1236 (tags-completion-table-function . etags-tags-completion-table)
1237 (snarf-tag-function . etags-snarf-tag)
1238 (goto-tag-location-function . etags-goto-tag-location)
1239 (find-tag-regexp-search-function . re-search-forward)
1240 (find-tag-regexp-tag-order . (tag-re-match-p))
1241 (find-tag-regexp-next-line-after-failure-p . t)
1242 (find-tag-search-function . search-forward)
1243 (find-tag-tag-order . (tag-exact-file-name-match-p
1244 tag-file-name-match-p
1245 tag-exact-match-p
1246 tag-implicit-name-match-p
1247 tag-symbol-match-p
1248 tag-word-match-p
1249 tag-partial-file-name-match-p
1250 tag-any-match-p))
1251 (find-tag-next-line-after-failure-p . nil)
1252 (list-tags-function . etags-list-tags)
1253 (tags-apropos-function . etags-tags-apropos)
1254 (tags-included-tables-function . etags-tags-included-tables)
1255 (verify-tags-table-function . etags-verify-tags-table)
1256 ))))
1258 (defun etags-verify-tags-table ()
1259 "Return non-nil if the current buffer is a valid etags TAGS file."
1260 ;; Use eq instead of = in case char-after returns nil.
1261 (eq (char-after (point-min)) ?\f))
1263 (defun etags-file-of-tag (&optional relative) ; Doc string?
1264 (save-excursion
1265 (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n")
1266 (let ((str (buffer-substring (match-beginning 1) (match-end 1))))
1267 (if relative
1269 (expand-file-name str
1270 (file-truename default-directory))))))
1273 (defun etags-tags-completion-table () ; Doc string?
1274 (let ((table (make-vector 511 0))
1275 (progress-reporter
1276 (make-progress-reporter
1277 (format "Making tags completion table for %s..." buffer-file-name)
1278 (point-min) (point-max))))
1279 (save-excursion
1280 (goto-char (point-min))
1281 ;; This monster regexp matches an etags tag line.
1282 ;; \1 is the string to match;
1283 ;; \2 is not interesting;
1284 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
1285 ;; \4 is not interesting;
1286 ;; \5 is the explicitly-specified tag name.
1287 ;; \6 is the line to start searching at;
1288 ;; \7 is the char to start searching at.
1289 (while (re-search-forward
1290 "^\\(\\([^\177]+[^-a-zA-Z0-9_+*$:\177]+\\)?\
1291 \\([-a-zA-Z0-9_+*$?:]+\\)[^-a-zA-Z0-9_+*$?:\177]*\\)\177\
1292 \\(\\([^\n\001]+\\)\001\\)?\\([0-9]+\\)?,\\([0-9]+\\)?\n"
1293 nil t)
1294 (intern (prog1 (if (match-beginning 5)
1295 ;; There is an explicit tag name.
1296 (buffer-substring (match-beginning 5) (match-end 5))
1297 ;; No explicit tag name. Best guess.
1298 (buffer-substring (match-beginning 3) (match-end 3)))
1299 (progress-reporter-update progress-reporter (point)))
1300 table)))
1301 table))
1303 (defun etags-snarf-tag (&optional use-explicit) ; Doc string?
1304 (let (tag-text line startpos explicit-start)
1305 (if (save-excursion
1306 (forward-line -1)
1307 (looking-at "\f\n"))
1308 ;; The match was for a source file name, not any tag within a file.
1309 ;; Give text of t, meaning to go exactly to the location we specify,
1310 ;; the beginning of the file.
1311 (setq tag-text t
1312 line nil
1313 startpos (point-min))
1315 ;; Find the end of the tag and record the whole tag text.
1316 (search-forward "\177")
1317 (setq tag-text (buffer-substring (1- (point)) (point-at-bol)))
1318 ;; If use-explicit is non nil and explicit tag is present, use it as part of
1319 ;; return value. Else just skip it.
1320 (setq explicit-start (point))
1321 (when (and (search-forward "\001" (point-at-bol 2) t)
1322 use-explicit)
1323 (setq tag-text (buffer-substring explicit-start (1- (point)))))
1326 (if (looking-at "[0-9]")
1327 (setq line (string-to-number (buffer-substring
1328 (point)
1329 (progn (skip-chars-forward "0-9")
1330 (point))))))
1331 (search-forward ",")
1332 (if (looking-at "[0-9]")
1333 (setq startpos (string-to-number (buffer-substring
1334 (point)
1335 (progn (skip-chars-forward "0-9")
1336 (point)))))))
1337 ;; Leave point on the next line of the tags file.
1338 (forward-line 1)
1339 (cons tag-text (cons line startpos))))
1341 (defun etags-goto-tag-location (tag-info)
1342 "Go to location of tag specified by TAG-INFO.
1343 TAG-INFO is a cons (TEXT LINE . POSITION).
1344 TEXT is the initial part of a line containing the tag.
1345 LINE is the line number.
1346 POSITION is the (one-based) char position of TEXT within the file.
1348 If TEXT is t, it means the tag refers to exactly LINE or POSITION,
1349 whichever is present, LINE having preference, no searching.
1350 Either LINE or POSITION can be nil. POSITION is used if present.
1352 If the tag isn't exactly at the given position, then look near that
1353 position using a search window that expands progressively until it
1354 hits the start of file."
1355 (let ((startpos (cdr (cdr tag-info)))
1356 (line (car (cdr tag-info)))
1357 offset found pat)
1358 (if (eq (car tag-info) t)
1359 ;; Direct file tag.
1360 (cond (line (progn (goto-char (point-min))
1361 (forward-line (1- line))))
1362 (startpos (goto-char startpos))
1363 (t (error "etags.el BUG: bogus direct file tag")))
1364 ;; This constant is 1/2 the initial search window.
1365 ;; There is no sense in making it too small,
1366 ;; since just going around the loop once probably
1367 ;; costs about as much as searching 2000 chars.
1368 (setq offset 1000
1369 found nil
1370 pat (concat (if (eq selective-display t)
1371 "\\(^\\|\^m\\)" "^")
1372 (regexp-quote (car tag-info))))
1373 ;; The character position in the tags table is 0-origin.
1374 ;; Convert it to a 1-origin Emacs character position.
1375 (if startpos (setq startpos (1+ startpos)))
1376 ;; If no char pos was given, try the given line number.
1377 (or startpos
1378 (if line
1379 (setq startpos (progn (goto-char (point-min))
1380 (forward-line (1- line))
1381 (point)))))
1382 (or startpos
1383 (setq startpos (point-min)))
1384 ;; First see if the tag is right at the specified location.
1385 (goto-char startpos)
1386 (setq found (looking-at pat))
1387 (while (and (not found)
1388 (progn
1389 (goto-char (- startpos offset))
1390 (not (bobp))))
1391 (setq found
1392 (re-search-forward pat (+ startpos offset) t)
1393 offset (* 3 offset))) ; expand search window
1394 (or found
1395 (re-search-forward pat nil t)
1396 (error "Rerun etags: `%s' not found in %s"
1397 pat buffer-file-name)))
1398 ;; Position point at the right place
1399 ;; if the search string matched an extra Ctrl-m at the beginning.
1400 (and (eq selective-display t)
1401 (looking-at "\^m")
1402 (forward-char 1))
1403 (beginning-of-line)))
1405 (defun etags-list-tags (file) ; Doc string?
1406 (goto-char (point-min))
1407 (when (re-search-forward (concat "\f\n" "\\(" file "\\)" ",") nil t)
1408 (let ((path (save-excursion (forward-line 1) (file-of-tag)))
1409 ;; Get the local value in the tags table
1410 ;; buffer before switching buffers.
1411 (goto-func goto-tag-location-function)
1412 tag tag-info pt)
1413 (forward-line 1)
1414 (while (not (or (eobp) (looking-at "\f")))
1415 (setq tag-info (save-excursion (funcall snarf-tag-function t))
1416 tag (car tag-info)
1417 pt (with-current-buffer standard-output (point)))
1418 (princ tag)
1419 (when (= (aref tag 0) ?\() (princ " ...)"))
1420 (with-current-buffer standard-output
1421 (make-text-button pt (point)
1422 'tag-info tag-info
1423 'file-path path
1424 'goto-func goto-func
1425 'action (lambda (button)
1426 (let ((tag-info (button-get button 'tag-info))
1427 (goto-func (button-get button 'goto-func)))
1428 (tag-find-file-of-tag (button-get button 'file-path))
1429 (widen)
1430 (funcall goto-func tag-info)))
1431 'follow-link t
1432 'face tags-tag-face
1433 'type 'button))
1434 (terpri)
1435 (forward-line 1))
1436 t)))
1438 (defmacro tags-with-face (face &rest body)
1439 "Execute BODY, give output to `standard-output' face FACE."
1440 (let ((pp (make-symbol "start")))
1441 `(let ((,pp (with-current-buffer standard-output (point))))
1442 ,@body
1443 (put-text-property ,pp (with-current-buffer standard-output (point))
1444 'face ,face standard-output))))
1446 (defun etags-tags-apropos-additional (regexp)
1447 "Display tags matching REGEXP from `tags-apropos-additional-actions'."
1448 (with-current-buffer standard-output
1449 (dolist (oba tags-apropos-additional-actions)
1450 (princ "\n\n")
1451 (tags-with-face 'highlight (princ (car oba)))
1452 (princ":\n\n")
1453 (let* ((beg (point))
1454 (symbs (car (cddr oba)))
1455 (ins-symb (lambda (sy)
1456 (let ((sn (symbol-name sy)))
1457 (when (string-match regexp sn)
1458 (make-text-button (point)
1459 (progn (princ sy) (point))
1460 'action-internal(cadr oba)
1461 'action (lambda (button) (funcall
1462 (button-get button 'action-internal)
1463 (button-get button 'item)))
1464 'item sn
1465 'face tags-tag-face
1466 'follow-link t
1467 'type 'button)
1468 (terpri))))))
1469 (when (symbolp symbs)
1470 (if (boundp symbs)
1471 (setq symbs (symbol-value symbs))
1472 (insert "symbol `" (symbol-name symbs) "' has no value\n")
1473 (setq symbs nil)))
1474 (if (vectorp symbs)
1475 (mapatoms ins-symb symbs)
1476 (dolist (sy symbs)
1477 (funcall ins-symb (car sy))))
1478 (sort-lines nil beg (point))))))
1480 (defun etags-tags-apropos (string) ; Doc string?
1481 (when tags-apropos-verbose
1482 (princ "Tags in file `")
1483 (tags-with-face 'highlight (princ buffer-file-name))
1484 (princ "':\n\n"))
1485 (goto-char (point-min))
1486 (let ((progress-reporter (make-progress-reporter
1487 (format "Making tags apropos buffer for `%s'..."
1488 string)
1489 (point-min) (point-max))))
1490 (while (re-search-forward string nil t)
1491 (progress-reporter-update progress-reporter (point))
1492 (beginning-of-line)
1494 (let* ( ;; Get the local value in the tags table
1495 ;; buffer before switching buffers.
1496 (goto-func goto-tag-location-function)
1497 (tag-info (save-excursion (funcall snarf-tag-function)))
1498 (tag (if (eq t (car tag-info)) nil (car tag-info)))
1499 (file-path (save-excursion (if tag (file-of-tag)
1500 (save-excursion (forward-line 1)
1501 (file-of-tag)))))
1502 (file-label (if tag (file-of-tag t)
1503 (save-excursion (forward-line 1)
1504 (file-of-tag t))))
1505 (pt (with-current-buffer standard-output (point))))
1506 (if tag
1507 (progn
1508 (princ (format "[%s]: " file-label))
1509 (princ tag)
1510 (when (= (aref tag 0) ?\() (princ " ...)"))
1511 (with-current-buffer standard-output
1512 (make-text-button pt (point)
1513 'tag-info tag-info
1514 'file-path file-path
1515 'goto-func goto-func
1516 'action (lambda (button)
1517 (let ((tag-info (button-get button 'tag-info))
1518 (goto-func (button-get button 'goto-func)))
1519 (tag-find-file-of-tag (button-get button 'file-path))
1520 (widen)
1521 (funcall goto-func tag-info)))
1522 'follow-link t
1523 'face tags-tag-face
1524 'type 'button)))
1525 (princ (format "- %s" file-label))
1526 (with-current-buffer standard-output
1527 (make-text-button pt (point)
1528 'file-path file-path
1529 'action (lambda (button)
1530 (tag-find-file-of-tag (button-get button 'file-path))
1531 ;; Get the local value in the tags table
1532 ;; buffer before switching buffers.
1533 (goto-char (point-min)))
1534 'follow-link t
1535 'face tags-tag-face
1536 'type 'button))))
1537 (terpri)
1538 (forward-line 1))
1539 (message nil))
1540 (when tags-apropos-verbose (princ "\n")))
1542 (defun etags-tags-table-files () ; 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 (or (looking-at "include$")
1551 (setq files (cons (buffer-substring beg (1- (point))) files))))
1552 (nreverse files)))
1554 (defun etags-tags-included-tables () ; Doc string?
1555 (let ((files nil)
1556 beg)
1557 (goto-char (point-min))
1558 (while (search-forward "\f\n" nil t)
1559 (setq beg (point))
1560 (end-of-line)
1561 (skip-chars-backward "^," beg)
1562 (if (looking-at "include$")
1563 ;; Expand in the default-directory of the tags table buffer.
1564 (setq files (cons (expand-file-name (buffer-substring beg (1- (point))))
1565 files))))
1566 (nreverse files)))
1568 ;; Empty tags file support.
1570 (defun tags-recognize-empty-tags-table ()
1571 "Return non-nil if current buffer is empty.
1572 If empty, make buffer-local values of the tags table format variables
1573 that do nothing."
1574 (and (zerop (buffer-size))
1575 (mapc (lambda (sym) (set (make-local-variable sym) 'ignore))
1576 '(tags-table-files-function
1577 tags-completion-table-function
1578 find-tag-regexp-search-function
1579 find-tag-search-function
1580 tags-apropos-function
1581 tags-included-tables-function))
1582 (set (make-local-variable 'verify-tags-table-function)
1583 (lambda () (zerop (buffer-size))))))
1585 ;; Match qualifier functions for tagnames.
1586 ;; These functions assume the etags file format defined in etc/ETAGS.EBNF.
1588 ;; This might be a neat idea, but it's too hairy at the moment.
1589 ;;(defmacro tags-with-syntax (&rest body)
1590 ;; `(with-syntax-table
1591 ;; (with-current-buffer (find-file-noselect (file-of-tag))
1592 ;; (syntax-table))
1593 ;; ,@body))
1594 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
1596 ;; exact file name match, i.e. searched tag must match complete file
1597 ;; name including directories parts if there are some.
1598 (defun tag-exact-file-name-match-p (tag)
1599 "Return non-nil if TAG matches complete file name.
1600 Any directory part of the file name is also matched."
1601 (and (looking-at ",[0-9\n]")
1602 (save-excursion (backward-char (+ 2 (length tag)))
1603 (looking-at "\f\n"))))
1605 ;; file name match as above, but searched tag must match the file
1606 ;; name not including the directories if there are some.
1607 (defun tag-file-name-match-p (tag)
1608 "Return non-nil if TAG matches file name, excluding directory part."
1609 (and (looking-at ",[0-9\n]")
1610 (save-excursion (backward-char (1+ (length tag)))
1611 (looking-at "/"))))
1613 ;; this / to detect we are after a directory separator is ok for unix,
1614 ;; is there a variable that contains the regexp for directory separator
1615 ;; on whatever operating system ?
1616 ;; Looks like ms-win will lose here :).
1618 ;; t if point is at a tag line that matches TAG exactly.
1619 ;; point should be just after a string that matches TAG.
1620 (defun tag-exact-match-p (tag)
1621 "Return non-nil if current tag line matches TAG exactly.
1622 Point should be just after a string that matches TAG."
1623 ;; The match is really exact if there is an explicit tag name.
1624 (or (and (eq (char-after (point)) ?\001)
1625 (eq (char-after (- (point) (length tag) 1)) ?\177))
1626 ;; We are not on the explicit tag name, but perhaps it follows.
1627 (looking-at (concat "[^\177\n]*\177" (regexp-quote tag) "\001"))))
1629 ;; t if point is at a tag line that has an implicit name.
1630 ;; point should be just after a string that matches TAG.
1631 (defun tag-implicit-name-match-p (tag)
1632 "Return non-nil if current tag line has an implicit name.
1633 Point should be just after a string that matches TAG."
1634 ;; Look at the comment of the make_tag function in lib-src/etags.c for
1635 ;; a textual description of the four rules.
1636 (and (string-match "^[^ \t()=,;]+$" tag) ;rule #1
1637 (looking-at "[ \t()=,;]?\177") ;rules #2 and #4
1638 (save-excursion
1639 (backward-char (1+ (length tag)))
1640 (looking-at "[\n \t()=,;]")))) ;rule #3
1642 ;; t if point is at a tag line that matches TAG as a symbol.
1643 ;; point should be just after a string that matches TAG.
1644 (defun tag-symbol-match-p (tag)
1645 "Return non-nil if current tag line matches TAG as a symbol.
1646 Point should be just after a string that matches TAG."
1647 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1648 (save-excursion
1649 (backward-char (1+ (length tag)))
1650 (and (looking-at "\\Sw") (looking-at "\\S_")))))
1652 ;; t if point is at a tag line that matches TAG as a word.
1653 ;; point should be just after a string that matches TAG.
1654 (defun tag-word-match-p (tag)
1655 "Return non-nil if current tag line matches TAG as a word.
1656 Point should be just after a string that matches TAG."
1657 (and (looking-at "\\b.*\177")
1658 (save-excursion (backward-char (length tag))
1659 (looking-at "\\b"))))
1661 ;; partial file name match, i.e. searched tag must match a substring
1662 ;; of the file name (potentially including a directory separator).
1663 (defun tag-partial-file-name-match-p (_tag)
1664 "Return non-nil if current tag matches file name.
1665 This is a substring match, and it can include directory separators.
1666 Point should be just after a string that matches TAG."
1667 (and (looking-at ".*,[0-9\n]")
1668 (save-excursion (beginning-of-line)
1669 (backward-char 2)
1670 (looking-at "\f\n"))))
1672 ;; t if point is in a tag line with a tag containing TAG as a substring.
1673 (defun tag-any-match-p (_tag)
1674 "Return non-nil if current tag line contains TAG as a substring."
1675 (looking-at ".*\177"))
1677 ;; t if point is at a tag line that matches RE as a regexp.
1678 (defun tag-re-match-p (re)
1679 "Return non-nil if current tag line matches regexp RE."
1680 (save-excursion
1681 (beginning-of-line)
1682 (let ((bol (point)))
1683 (and (search-forward "\177" (line-end-position) t)
1684 (re-search-backward re bol t)))))
1686 (defcustom tags-loop-revert-buffers nil
1687 "*Non-nil means tags-scanning loops should offer to reread changed files.
1688 These loops normally read each file into Emacs, but when a file
1689 is already visited, they use the existing buffer.
1690 When this flag is non-nil, they offer to revert the existing buffer
1691 in the case where the file has changed since you visited it."
1692 :type 'boolean
1693 :group 'etags)
1695 ;;;###autoload
1696 (defun next-file (&optional initialize novisit)
1697 "Select next file among files in current tags table.
1699 A first argument of t (prefix arg, if interactive) initializes to the
1700 beginning of the list of files in the tags table. If the argument is
1701 neither nil nor t, it is evalled to initialize the list of files.
1703 Non-nil second argument NOVISIT means use a temporary buffer
1704 to save time and avoid uninteresting warnings.
1706 Value is nil if the file was already visited;
1707 if the file was newly read in, the value is the filename."
1708 ;; Make the interactive arg t if there was any prefix arg.
1709 (interactive (list (if current-prefix-arg t)))
1710 (cond ((not initialize)
1711 ;; Not the first run.
1713 ((eq initialize t)
1714 ;; Initialize the list from the tags table.
1715 (save-excursion
1716 ;; Visit the tags table buffer to get its list of files.
1717 (visit-tags-table-buffer)
1718 ;; Copy the list so we can setcdr below, and expand the file
1719 ;; names while we are at it, in this buffer's default directory.
1720 (setq next-file-list (mapcar 'expand-file-name (tags-table-files)))
1721 ;; Iterate over all the tags table files, collecting
1722 ;; a complete list of referenced file names.
1723 (while (visit-tags-table-buffer t)
1724 ;; Find the tail of the working list and chain on the new
1725 ;; sublist for this tags table.
1726 (let ((tail next-file-list))
1727 (while (cdr tail)
1728 (setq tail (cdr tail)))
1729 ;; Use a copy so the next loop iteration will not modify the
1730 ;; list later returned by (tags-table-files).
1731 (if tail
1732 (setcdr tail (mapcar 'expand-file-name (tags-table-files)))
1733 (setq next-file-list (mapcar 'expand-file-name
1734 (tags-table-files))))))))
1736 ;; Initialize the list by evalling the argument.
1737 (setq next-file-list (eval initialize))))
1738 (unless next-file-list
1739 (and novisit
1740 (get-buffer " *next-file*")
1741 (kill-buffer " *next-file*"))
1742 (error "All files processed"))
1743 (let* ((next (car next-file-list))
1744 (buffer (get-file-buffer next))
1745 (new (not buffer)))
1746 ;; Advance the list before trying to find the file.
1747 ;; If we get an error finding the file, don't get stuck on it.
1748 (setq next-file-list (cdr next-file-list))
1749 ;; Optionally offer to revert buffers
1750 ;; if the files have changed on disk.
1751 (and buffer tags-loop-revert-buffers
1752 (not (verify-visited-file-modtime buffer))
1753 (y-or-n-p
1754 (format
1755 (if (buffer-modified-p buffer)
1756 "File %s changed on disk. Discard your edits? "
1757 "File %s changed on disk. Reread from disk? ")
1758 next))
1759 (with-current-buffer buffer
1760 (revert-buffer t t)))
1761 (if (not (and new novisit))
1762 (find-file next novisit)
1763 ;; Like find-file, but avoids random warning messages.
1764 (switch-to-buffer (get-buffer-create " *next-file*"))
1765 (kill-all-local-variables)
1766 (erase-buffer)
1767 (setq new next)
1768 (insert-file-contents new nil))
1769 new))
1771 (defvar tags-loop-operate nil
1772 "Form for `tags-loop-continue' to eval to change one file.")
1774 (defvar tags-loop-scan
1775 '(error "%s"
1776 (substitute-command-keys
1777 "No \\[tags-search] or \\[tags-query-replace] in progress"))
1778 "Form for `tags-loop-continue' to eval to scan one file.
1779 If it returns non-nil, this file needs processing by evalling
1780 \`tags-loop-operate'. Otherwise, move on to the next file.")
1782 (defun tags-loop-eval (form)
1783 "Evaluate FORM and return its result.
1784 Bind `case-fold-search' during the evaluation, depending on the value of
1785 `tags-case-fold-search'."
1786 (let ((case-fold-search (if (memq tags-case-fold-search '(t nil))
1787 tags-case-fold-search
1788 case-fold-search)))
1789 (eval form)))
1792 ;;;###autoload
1793 (defun tags-loop-continue (&optional first-time)
1794 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1795 Used noninteractively with non-nil argument to begin such a command (the
1796 argument is passed to `next-file', which see).
1798 Two variables control the processing we do on each file: the value of
1799 `tags-loop-scan' is a form to be executed on each file to see if it is
1800 interesting (it returns non-nil if so) and `tags-loop-operate' is a form to
1801 evaluate to operate on an interesting file. If the latter evaluates to
1802 nil, we exit; otherwise we scan the next file."
1803 (interactive)
1804 (let (new
1805 ;; Non-nil means we have finished one file
1806 ;; and should not scan it again.
1807 file-finished
1808 original-point
1809 (messaged nil))
1810 (while
1811 (progn
1812 ;; Scan files quickly for the first or next interesting one.
1813 ;; This starts at point in the current buffer.
1814 (while (or first-time file-finished
1815 (save-restriction
1816 (widen)
1817 (not (tags-loop-eval tags-loop-scan))))
1818 ;; If nothing was found in the previous file, and
1819 ;; that file isn't in a temp buffer, restore point to
1820 ;; where it was.
1821 (when original-point
1822 (goto-char original-point))
1824 (setq file-finished nil)
1825 (setq new (next-file first-time t))
1827 ;; If NEW is non-nil, we got a temp buffer,
1828 ;; and NEW is the file name.
1829 (when (or messaged
1830 (and (not first-time)
1831 (> baud-rate search-slow-speed)
1832 (setq messaged t)))
1833 (message "Scanning file %s..." (or new buffer-file-name)))
1835 (setq first-time nil)
1836 (setq original-point (if new nil (point)))
1837 (goto-char (point-min)))
1839 ;; If we visited it in a temp buffer, visit it now for real.
1840 (if new
1841 (let ((pos (point)))
1842 (erase-buffer)
1843 (set-buffer (find-file-noselect new))
1844 (setq new nil) ;No longer in a temp buffer.
1845 (widen)
1846 (goto-char pos))
1847 (push-mark original-point t))
1849 (switch-to-buffer (current-buffer))
1851 ;; Now operate on the file.
1852 ;; If value is non-nil, continue to scan the next file.
1853 (tags-loop-eval tags-loop-operate))
1854 (setq file-finished t))
1855 (and messaged
1856 (null tags-loop-operate)
1857 (message "Scanning file %s...found" buffer-file-name))))
1858 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
1860 ;;;###autoload
1861 (defun tags-search (regexp &optional file-list-form)
1862 "Search through all files listed in tags table for match for REGEXP.
1863 Stops when a match is found.
1864 To continue searching for next match, use command \\[tags-loop-continue].
1866 See documentation of variable `tags-file-name'."
1867 (interactive "sTags search (regexp): ")
1868 (if (and (equal regexp "")
1869 (eq (car tags-loop-scan) 're-search-forward)
1870 (null tags-loop-operate))
1871 ;; Continue last tags-search as if by M-,.
1872 (tags-loop-continue nil)
1873 (setq tags-loop-scan `(re-search-forward ',regexp nil t)
1874 tags-loop-operate nil)
1875 (tags-loop-continue (or file-list-form t))))
1877 ;;;###autoload
1878 (defun tags-query-replace (from to &optional delimited file-list-form)
1879 "Do `query-replace-regexp' of FROM with TO on all files listed in tags table.
1880 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1881 If you exit (\\[keyboard-quit], RET or q), you can resume the query replace
1882 with the command \\[tags-loop-continue].
1883 Fourth arg FILE-LIST-FORM non-nil means initialize the replacement loop.
1884 Fifth and sixth arguments START and END are accepted, for compatibility
1885 with `query-replace-regexp', and ignored.
1887 If FILE-LIST-FORM is non-nil, it is a form to evaluate to
1888 produce the list of files to search.
1890 See also the documentation of the variable `tags-file-name'."
1891 (interactive (query-replace-read-args "Tags query replace (regexp)" t t))
1892 (setq tags-loop-scan `(let ,(unless (equal from (downcase from))
1893 '((case-fold-search nil)))
1894 (if (re-search-forward ',from nil t)
1895 ;; When we find a match, move back
1896 ;; to the beginning of it so perform-replace
1897 ;; will see it.
1898 (goto-char (match-beginning 0))))
1899 tags-loop-operate `(perform-replace ',from ',to t t ',delimited
1900 nil multi-query-replace-map))
1901 (tags-loop-continue (or file-list-form t)))
1903 (defun tags-complete-tags-table-file (string predicate what) ; Doc string?
1904 (save-excursion
1905 ;; If we need to ask for the tag table, allow that.
1906 (let ((enable-recursive-minibuffers t))
1907 (visit-tags-table-buffer))
1908 (if (eq what t)
1909 (all-completions string (tags-table-files) predicate)
1910 (try-completion string (tags-table-files) predicate))))
1912 ;;;###autoload
1913 (defun list-tags (file &optional _next-match)
1914 "Display list of tags in file FILE.
1915 This searches only the first table in the list, and no included tables.
1916 FILE should be as it appeared in the `etags' command, usually without a
1917 directory specification."
1918 (interactive (list (completing-read "List tags in file: "
1919 'tags-complete-tags-table-file
1920 nil t nil)))
1921 (with-output-to-temp-buffer "*Tags List*"
1922 (princ "Tags in file `")
1923 (tags-with-face 'highlight (princ file))
1924 (princ "':\n\n")
1925 (save-excursion
1926 (let ((first-time t)
1927 (gotany nil))
1928 (while (visit-tags-table-buffer (not first-time))
1929 (setq first-time nil)
1930 (if (funcall list-tags-function file)
1931 (setq gotany t)))
1932 (or gotany
1933 (error "File %s not in current tags tables" file)))))
1934 (with-current-buffer "*Tags List*"
1935 (require 'apropos)
1936 (with-no-warnings
1937 (apropos-mode))
1938 (setq buffer-read-only t)))
1940 ;;;###autoload
1941 (defun tags-apropos (regexp)
1942 "Display list of all tags in tags table REGEXP matches."
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 (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 (error "Nothing to complete")
2069 (apply 'completion-in-region comp-data))))
2071 (dolist (x '("^No tags table in use; use .* to select one$"
2072 "^There is no default tag$"
2073 "^No previous tag locations$"
2074 "^File .* is not a valid tags table$"
2075 "^No \\(more \\|\\)tags \\(matching\\|containing\\) "
2076 "^Rerun etags: `.*' not found in "
2077 "^All files processed$"
2078 "^No .* or .* in progress$"
2079 "^File .* not in current tags tables$"
2080 "^No tags table loaded"
2081 "^Nothing to complete$"))
2082 (add-to-list 'debug-ignored-errors x))
2084 (provide 'etags)
2086 ;;; etags.el ends here