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