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