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