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