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