1 ;; etags.el --- etags facility for Emacs
3 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
6 ;; Copyright (C) 1985, 1986, 1988, 1989, 1991, 1992
7 ;; Free Software Foundation, Inc.
9 ;; This file is part of GNU Emacs.
11 ;; GNU Emacs is free software; you can redistribute it and/or modify
12 ;; it under the terms of the GNU General Public License as published by
13 ;; the Free Software Foundation; either version 2, or (at your option)
16 ;; GNU Emacs is distributed in the hope that it will be useful,
17 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
18 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 ;; GNU General Public License for more details.
21 ;; You should have received a copy of the GNU General Public License
22 ;; along with GNU Emacs; see the file COPYING. If not, write to
23 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
28 (defvar tags-file-name nil
"\
29 *File name of tags table.
30 To switch to a new tags table, setting this variable is sufficient.
31 Use the `etags' program to make a tags table file.")
34 (defvar tags-table-list nil
35 "*List of names of tags table files which are currently being searched.
36 An element of nil means to look for a file \"TAGS\" in the current directory.
37 Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
39 (defvar tags-table-list-pointer nil
40 "Pointer into `tags-table-list', or into a list of included tags tables,
41 where the current state of searching is. Use `visit-tags-table-buffer' to
42 cycle through tags tables in this list.")
44 (defvar tags-table-parent-pointer-list nil
45 "List of values to restore into `tags-table-list-pointer' when it hits nil.")
47 (defvar tags-table-set-list nil
48 "List of sets of tags table which have been used together in the past.
49 Each element is a list of strings which are file names.")
52 (defvar find-tag-hook nil
53 "*Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'.
54 The value in the buffer in which \\[find-tag] is done is used,
55 not the value in the buffer \\[find-tag] goes to.")
58 (defvar find-tag-default-function nil
59 "*If non-nil, a function of no arguments used by \\[find-tag] to pick a
60 default tag. If nil, and the symbol that is the value of `major-mode'
61 has a `find-tag-default-function' property (see `put'), that is used.
62 Otherwise, `find-tag-default' is used.")
65 (defvar default-tags-table-function nil
66 "*If non-nil, a function of no arguments to choose a default tags file
67 for a particular buffer.")
70 ;; These variables are local in tags table buffers.
72 (defvar tag-lines-already-matched nil
73 "List of positions of beginnings of lines within the tags table
74 that are already matched.")
76 (defvar tags-table-files nil
77 "List of file names covered by current tags table.
78 nil means it has not yet been computed; use `tags-table-files' to do so.")
80 (defvar tags-completion-table nil
81 "Alist of tag names defined in current tags table.")
83 (defvar tags-included-tables nil
84 "List of tags tables included by the current tags table.")
86 (defvar next-file-list nil
87 "List of files for \\[next-file] to process.")
89 ;; Hooks for file formats.
91 (defvar tags-table-format-hooks
'(etags-recognize-tags-table
92 recognize-empty-tags-table
93 ctags-recognize-tags-table
)
94 "List of functions to be called in a tags table buffer to identify
95 the type of tags table. The functions are called in order, with no arguments,
96 until one returns non-nil. The function should make buffer-local bindings
97 of the format-parsing tags function variables if successful.")
99 (defvar file-of-tag-function nil
100 "Function to do the work of `file-of-tag' (which see).")
101 (defvar tags-table-files-function nil
102 "Function to do the work of `tags-table-files' (which see).")
103 (defvar tags-completion-table-function nil
104 "Function to build the tags-completion-table.")
105 (defvar snarf-tag-function nil
106 "Function to get info about a matched tag for `goto-tag-location-function'.")
107 (defvar goto-tag-location-function nil
108 "Function of to go to the location in the buffer specified by a tag.
109 One argument, the tag info returned by `snarf-tag-function'.")
110 (defvar find-tag-regexp-search-function nil
111 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
112 (defvar find-tag-regexp-tag-order nil
113 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
114 (defvar find-tag-regexp-next-line-after-failure-p nil
115 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
116 (defvar find-tag-search-function nil
117 "Search function passed to `find-tag-in-order' for finding a tag.")
118 (defvar find-tag-tag-order nil
119 "Tag order passed to `find-tag-in-order' for finding a tag.")
120 (defvar find-tag-next-line-after-failure-p nil
121 "Flag passed to `find-tag-in-order' for finding a tag.")
122 (defvar list-tags-function nil
123 "Function to do the work of `list-tags' (which see).")
124 (defvar tags-apropos-function nil
125 "Function to do the work of `tags-apropos' (which see).")
126 (defvar tags-included-tables-function nil
127 "Function to do the work of `tags-included-tables' (which see).")
128 (defvar verify-tags-table-function nil
129 "Function to return t iff the current buffer vontains a valid
130 \(already initialized\) tags file.")
132 (defun initialize-new-tags-table ()
133 "Initialize the tags table in the current buffer.
134 Returns non-nil iff it is a valid tags table."
135 (make-local-variable 'tag-lines-already-matched
)
136 (make-local-variable 'tags-table-files
)
137 (make-local-variable 'tags-completion-table
)
138 (make-local-variable 'tags-included-tables
)
139 (setq tags-table-files nil
140 tag-lines-already-matched nil
141 tags-completion-table nil
142 tags-included-tables nil
)
143 ;; Value is t if we have found a valid tags table buffer.
144 (let ((hooks tags-table-format-hooks
))
146 (not (funcall (car hooks
))))
147 (setq hooks
(cdr hooks
)))
151 (defun visit-tags-table (file &optional local
)
152 "Tell tags commands to use tags table file FILE.
153 FILE should be the name of a file created with the `etags' program.
154 A directory name is ok too; it means file TAGS in that directory.
156 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
157 With a prefix arg, set the buffer-local value instead.
158 When you find a tag with \\[find-tag], the buffer it finds the tag
159 in is given a local value of this variable which is the name of the tags
160 file the tag was in."
161 (interactive (list (read-file-name "Visit tags table: (default TAGS) "
163 (expand-file-name "TAGS"
167 (setq file
(expand-file-name file
))
168 (if (file-directory-p file
)
169 (setq file
(expand-file-name "TAGS" file
)))
171 (setq tags-file-name file
)
172 (kill-local-variable 'tags-file-name
)
173 (setq-default tags-file-name file
))
175 (visit-tags-file t
)))
177 (defun visit-tags-table-buffer (&optional cont
)
178 "Select the buffer containing the current tags table.
179 If optional arg is t, visit the next table in `tags-table-list'.
180 If optional arg is the atom `reset', reset to the head of `tags-table-list'.
181 If optional arg is the atom `same', don't look for a new table;
182 just select the buffer.
183 If arg is nil or absent, choose a buffer from information in
184 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
185 Returns t if it visits a tags table, or nil if there are no more in the list."
187 (let ((tags-file-name (car tags-table-list-pointer
)))
188 (if (null tags-file-name
)
190 (visit-tags-file nil
)
192 (let ((put-in-list t
))
193 (if (cond ((eq cont
'reset
)
194 (setq tags-table-list-pointer tags-table-list
198 (setq tags-table-list-pointer
(cdr tags-table-list-pointer
))
199 (if (tags-included-tables)
201 ;; Move into the included tags tables.
202 (if tags-table-list-pointer
203 (setq tags-table-parent-pointer-list
204 (cons tags-table-list-pointer
205 tags-table-parent-pointer-list
)))
206 (setq tags-table-list-pointer tags-included-tables
)))
207 (or tags-table-list-pointer
208 ;; Pop back to the tags table after the one which includes
210 (setq tags-table-list-pointer
211 (car tags-table-parent-pointer-list
)
212 tags-table-parent-pointer-list
213 (cdr tags-table-parent-pointer-list
)))
214 (setq put-in-list nil
)
215 (null tags-table-list-pointer
)))
216 ;; No more tags table files in the list.
220 (and tags-table-list-pointer
221 (or (car tags-table-list-pointer
)
222 ;; nil means look for TAGS in current directory.
224 (expand-file-name "TAGS"
226 (expand-file-name "TAGS"
227 default-directory
))))
228 (cdr (assq 'tags-file-name
(buffer-local-variables))))
229 (and default-tags-table-function
230 (funcall default-tags-table-function
))
231 (car tags-table-list-pointer
)
234 (read-file-name "Visit tags table: (default TAGS) "
236 (expand-file-name "TAGS" default-directory
)
238 (if (file-directory-p tags-file-name
)
239 (setq tags-file-name
(expand-file-name "TAGS" tags-file-name
)))
240 (visit-tags-file put-in-list
)
243 ;; Visit tags-file-name and check that it's a valid tags table.
244 ;; On return, tags-table-list and tags-table-list-pointer
245 ;; point to tags-file-name.
246 (defun visit-tags-file (put-in-list)
247 ;; FILE is never changed, but we don't just use tags-file-name
248 ;; directly because we don't want to get its buffer-local value
249 ;; in the buffer we switch to.
250 (let ((file tags-file-name
))
251 (if (if (get-file-buffer file
)
253 (set-buffer (get-file-buffer file
))
254 (setq win
(or verify-tags-table-function
255 (initialize-new-tags-table)))
256 (if (or (verify-visited-file-modtime (current-buffer))
258 "Tags file has changed, read new contents? ")))
259 (and win
(funcall verify-tags-table-function
))
261 (initialize-new-tags-table)))
262 (set-buffer (find-file-noselect file
))
263 (initialize-new-tags-table))
266 (not (equal file
(car tags-table-list-pointer
))))
268 ;; Bury the tags table buffer so it
269 ;; doesn't get in the user's way.
270 (bury-buffer (current-buffer))
271 ;; Look for this file in the current list of tags files.
272 (if (setq elt
(member file tags-table-list
))
273 (if (eq elt tags-table-list
)
274 ;; Already at the head of the list.
276 ;; Rotate this element to the head of the search list.
277 (setq tags-table-list-pointer
(nconc elt tags-table-list
))
278 (while (not (eq (cdr tags-table-list
) elt
))
279 (setq tags-table-list
(cdr tags-table-list
)))
280 (setcdr tags-table-list nil
)
281 (setq tags-table-list tags-table-list-pointer
))
282 ;; The table is not in the current set.
283 ;; Try to find it in another previously used set.
284 (let ((sets tags-table-set-list
))
286 (not (setq elt
(member file
288 (setq sets
(cdr sets
)))
291 ;; Found in some other set. Switch to that set, making
292 ;; the selected tags table the head of the search list.
293 (or (memq tags-table-list tags-table-set-list
)
294 ;; Save the current list.
295 (setq tags-table-set-list
296 (cons tags-table-list tags-table-set-list
)))
297 (setq tags-table-list
(car sets
))
298 (if (eq elt tags-table-list
)
299 ;; Already at the head of the list
301 ;; Rotate this element to the head of the list.
302 (setq tags-table-list-pointer
303 (nconc elt tags-table-list
))
304 (while (not (eq (cdr tags-table-list
) elt
))
305 (setq tags-table-list
(cdr tags-table-list
)))
306 (setcdr tags-table-list nil
)
307 (setq tags-table-list tags-table-list-pointer
)
308 (setcar sets tags-table-list
)))
309 ;; Not found in any current set.
310 (if (and tags-table-list
313 " to current list of tags tables? ")))
314 ;; Add it to the current list.
315 (setq tags-table-list
316 (cons file tags-table-list
))
317 ;; Make a fresh list, and store the old one.
318 (or (memq tags-table-list tags-table-set-list
)
319 (setq tags-table-set-list
320 (cons tags-table-list tags-table-set-list
)))
321 (setq tags-table-list
(cons file nil
)))
322 (setq tags-table-list-pointer tags-table-list
))))))
324 ;; The buffer was not valid. Don't use it again.
325 (kill-local-variable 'tags-file-name
)
326 (setq tags-file-name nil
)
327 (error "File %s is not a valid tags table" buffer-file-name
))))
329 (defun file-of-tag ()
330 "Return the file name of the file whose tags point is within.
331 Assumes the tags table is the current buffer.
332 File name returned is relative to tags table file's directory."
333 (funcall file-of-tag-function
))
336 (defun tags-table-files ()
337 "Return a list of files in the current tags table.
338 File names returned are absolute."
340 (visit-tags-table-buffer)
342 (setq tags-table-files
343 (funcall tags-table-files-function
)))))
345 (defun tags-included-tables ()
346 "Return a list of tags tables included by the current table."
347 (or tags-included-tables
348 (setq tags-included-tables
(funcall tags-included-tables-function
))))
350 ;; Build tags-completion-table on demand. The single current tags table
351 ;; and its included tags tables (and their included tables, etc.) have
352 ;; their tags included in the completion table.
353 (defun tags-completion-table ()
354 (or tags-completion-table
357 (message "Making tags completion table for %s..." buffer-file-name
)
358 (let ((included (tags-included-tables))
359 (table (funcall tags-completion-table-function
)))
362 (let ((tags-file-name (car included
)))
363 (visit-tags-file nil
))
364 (if (tags-completion-table)
367 (intern (symbol-name sym
) table
)))
368 tags-completion-table
))
369 (setq included
(cdr included
))))
370 (setq tags-completion-table table
))
371 (message "Making tags completion table for %s...done"
373 (quit (message "Tags completion table construction aborted.")
374 (setq tags-completion-table nil
)))))
376 ;; Completion function for tags. Does normal try-completion,
377 ;; but builds tags-completion-table on demand.
378 (defun tags-complete-tag (string predicate what
)
380 (visit-tags-table-buffer)
382 (all-completions string
(tags-completion-table) predicate
)
383 (try-completion string
(tags-completion-table) predicate
))))
385 ;; Return a default tag to search for, based on the text at point.
386 (defun find-tag-default ()
388 (while (looking-at "\\sw\\|\\s_")
390 (if (or (re-search-backward "\\sw\\|\\s_"
391 (save-excursion (beginning-of-line) (point))
393 (re-search-forward "\\(\\sw\\|\\s_\\)+"
394 (save-excursion (end-of-line) (point))
396 (progn (goto-char (match-end 0))
397 (buffer-substring (point)
398 (progn (forward-sexp -
1)
399 (while (looking-at "\\s'")
404 ;; Read a tag name from the minibuffer with defaulting and completion.
405 (defun find-tag-tag (string)
406 (let* ((default (funcall (or find-tag-default-function
407 (get major-mode
'find-tag-default-function
)
409 (spec (completing-read (if default
410 (format "%s(default %s) " string default
)
412 'tags-complete-tag
)))
413 (list (if (equal spec
"")
414 (or default
(error "There is no default tag"))
418 (defun find-tag-noselect (tagname &optional next-p regexp-p
)
419 "Find tag (in current tags table) whose name contains TAGNAME.
420 Returns the buffer containing the tag's definition moves its point there,
421 but does not select the buffer.
422 The default for TAGNAME is the expression in the buffer near point.
424 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
425 for another tag that matches the last tagname or regexp used. When there
426 are multiple matches for a tag, more exact matches are found first.
428 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
430 See documentation of variable `tags-file-name'."
431 (interactive (if current-prefix-arg
433 (find-tag-tag "Find tag: ")))
434 (let ((local-find-tag-hook find-tag-hook
))
436 (visit-tags-table-buffer 'reset
))
437 (find-tag-in-order tagname
439 find-tag-regexp-search-function
440 find-tag-search-function
)
442 find-tag-regexp-tag-order
445 find-tag-regexp-next-line-after-failure-p
446 find-tag-next-line-after-failure-p
)
447 (if regexp-p
"matching" "containing")
449 (run-hooks 'local-find-tag-hook
)))
452 (defun find-tag (tagname &optional next-p
)
453 "Find tag (in current tags table) whose name contains TAGNAME.
454 Select the buffer containing the tag's definition, and move point there.
455 The default for TAGNAME is the expression in the buffer around or before point.
457 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
458 for another tag that matches the last tagname used. When there are
459 multiple matches, more exact matches are found first.
461 See documentation of variable `tags-file-name'."
462 (interactive (if current-prefix-arg
464 (find-tag-tag "Find tag other window: ")))
465 (switch-to-buffer (find-tag-noselect tagname next-p
)))
466 ;;;###autoload (define-key esc-map "." 'find-tag)
469 (defun find-tag-other-window (tagname &optional next-p
)
470 "Find tag (in current tags table) whose name contains TAGNAME.
471 Select the buffer containing the tag's definition
472 in another window, and move point there.
473 The default for TAGNAME is the expression in the buffer around or before point.
475 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
476 for another tag that matches the last tagname used. When there are
477 multiple matches, more exact matches are found first.
479 See documentation of variable `tags-file-name'."
480 (interactive (if current-prefix-arg
482 (find-tag-tag "Find tag other window: ")))
483 (switch-to-buffer-other-window (find-tag-noselect tagname next-p
)))
484 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
487 (defun find-tag-other-frame (tagname &optional next-p
)
488 "Find tag (in current tag table) whose name contains TAGNAME.
489 Selects the buffer that the tag is contained in in another frame
490 and puts point at its definition.
491 If TAGNAME is a null string, the expression in the buffer
492 around or before point is used as the tag name.
493 If second arg NEXT-P is non-nil (interactively, with prefix arg),
494 searches for the next tag in the tag table
495 that matches the tagname used in the previous find-tag.
497 See documentation of variable `tags-file-name'."
498 (interactive (if current-prefix-arg
500 (find-tag-tag "Find tag other window: ")))
501 (let ((pop-up-frames t
))
502 (find-tag-other-window tagname next-p
)))
503 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
506 (defun find-tag-regexp (regexp &optional next-p other-window
)
507 "Find tag (in current tags table) whose name matches REGEXP.
508 Select the buffer containing the tag's definition and move point there.
510 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
511 for another tag that matches the last tagname used.
513 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
515 See documentation of variable `tags-file-name'."
516 (interactive (if current-prefix-arg
518 (read-string "Find tag regexp: ")))
519 (funcall (if other-window
'switch-to-buffer-other-window
'switch-to-buffer
)
520 (find-tag-noselect regexp next-p t
)))
522 ;; Internal tag finding function.
524 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
525 ;; any member of the function list ORDER (third arg). If ORDER is nil,
526 ;; use saved state to continue a previous search.
528 ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
531 ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
532 ;; point should be moved to the next line.
534 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
535 ;; beginning of tags file, and perform inner loop: for each naive match for
536 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
537 ;; qualifier-func. If it qualifies, go to the specified line in the
538 ;; specified source file and return. Qualified matches are remembered to
539 ;; avoid repetition. State is saved so that the loop can be continued.
541 (defun find-tag-in-order (pattern search-forward-func order
542 next-line-after-failure-p matching
544 (let (file ;name of file containing tag
545 tag-info
;where to find the tag in FILE
546 tags-table-file
;name of tags file
553 (visit-tags-table-buffer))
554 ;; Get a qualified match.
555 (catch 'qualified-match-found
556 (while (or first-table
557 (visit-tags-table-buffer t
))
560 (setq tag-lines-already-matched nil
))
563 (setq first-table nil
)
564 ;; Start at beginning of tags file.
565 (goto-char (point-min)))
567 (setq tags-table-file buffer-file-name
)
569 (while (funcall search-forward-func pattern nil t
)
570 ;; Naive match found. Qualify the match.
571 (and (funcall (car order
) pattern
)
572 ;; Make sure it is not a previous qualified match.
573 ;; Use of `memq' depends on numbers being eq.
574 (not (memq (save-excursion (beginning-of-line) (point))
575 tag-lines-already-matched
))
576 (throw 'qualified-match-found nil
))
577 (if next-line-after-failure-p
579 ;; Try the next flavor of match.
580 (setq order
(cdr order
))
581 (goto-char (point-min)))
582 (setq order tag-order
))
583 ;; We throw out on match, so only get here if there were no matches.
584 (error "No %stags %s %s" (if first-search
"" "more ")
587 ;; Found a tag; extract location info.
589 (setq tag-lines-already-matched
(cons (point)
590 tag-lines-already-matched
))
591 ;; Expand the filename, using the tags table buffer's default-directory.
592 (setq file
(expand-file-name (file-of-tag))
593 tag-info
(funcall snarf-tag-function
))
595 ;; Get the local value in the tags table buffer.
596 (setq goto-func goto-tag-location-function
)
598 ;; Find the right line in the specified file.
599 (set-buffer (find-file-noselect file
))
602 (funcall goto-func tag-info
)
604 ;; Give this buffer a local value of tags-file-name.
605 ;; The next time visit-tags-table-buffer is called,
606 ;; it will use the same tags table that found a match in this buffer.
607 (make-local-variable 'tags-file-name
)
608 (setq tags-file-name tags-table-file
)
610 ;; Return the buffer where the tag was found.
613 ;; `etags' TAGS file format support.
615 (defun etags-recognize-tags-table ()
616 (and (eq (char-after 1) ?
\f)
617 (message "%s is an `etags' TAGS file" buffer-file-name
)
618 (mapcar (function (lambda (elt)
619 (make-local-variable (car elt
))
620 (set (car elt
) (cdr elt
))))
621 '((file-of-tag-function . etags-file-of-tag
)
622 (tags-table-files-function . etags-tags-table-files
)
623 (tags-completion-table-function . etags-tags-completion-table
)
624 (snarf-tag-function . etags-snarf-tag
)
625 (goto-tag-location-function . etags-goto-tag-location
)
626 (find-tag-regexp-search-function . re-search-forward
)
627 (find-tag-regexp-tag-order .
(tag-re-match-p))
628 (find-tag-regexp-next-line-after-failuire-p . t
)
629 (find-tag-search-function . search-forward
)
630 (find-tag-tag-order .
(tag-exact-match-p tag-word-match-p
632 (find-tag-next-line-after-failure-p . nil
)
633 (list-tags-function . etags-list-tags
)
634 (tags-apropos-function . etags-tags-apropos
)
635 (tags-included-tables-function . etags-tags-included-tables
)
636 (verify-tags-table-function . etags-verify-tags-table
)
639 (defun etags-verify-tags-table ()
640 (= (char-after 1) ?
\f))
642 (defun etags-file-of-tag ()
644 (search-backward "\f\n")
646 (buffer-substring (point)
647 (progn (skip-chars-forward "^,") (point)))))
649 (defun etags-tags-completion-table ()
650 (let ((table (make-vector 511 0)))
652 (goto-char (point-min))
653 (while (search-forward "\177" nil t
)
654 ;; Handle multiple \177's on a line.
656 (skip-chars-backward "^-A-Za-z0-9_$\n") ;sym syntax? XXX
658 (intern (buffer-substring
661 (skip-chars-backward "-A-Za-z0-9_$")
663 ;; `::' in the middle of a C++ tag.
664 (and (= (preceding-char) ?
:)
665 (= (char-after (- (point) 2)) ?
:)
674 (defun etags-snarf-tag ()
675 (let (tag-text startpos
)
676 (search-forward "\177")
677 (setq tag-text
(buffer-substring (1- (point))
678 (save-excursion (beginning-of-line)
681 (setq startpos
(string-to-int (buffer-substring
683 (progn (skip-chars-forward "0-9")
685 ;; Leave point on the next line of the tags file.
687 (cons tag-text startpos
)))
689 (defun etags-goto-tag-location (tag-info)
690 (let ((startpos (cdr tag-info
))
691 ;; This constant is 1/2 the initial search window.
692 ;; There is no sense in making it too small,
693 ;; since just going around the loop once probably
694 ;; costs about as much as searching 2000 chars.
697 (pat (concat "^" (regexp-quote (car tag-info
)))))
699 (setq startpos
(point-min)))
700 (while (and (not found
)
702 (goto-char (- startpos offset
))
705 (re-search-forward pat
(+ startpos offset
) t
)
706 offset
(* 3 offset
))) ; expand search window
708 (re-search-forward pat nil t
)
709 (error "`%s' not found in %s; time to rerun etags"
710 pat buffer-file-name
)))
713 (defun etags-list-tags (file)
715 (if (not (search-forward (concat "\f\n" file
",") nil t
))
718 (while (not (or (eobp) (looking-at "\f")))
719 (princ (buffer-substring (point)
720 (progn (skip-chars-forward "^\177")
725 (defun etags-tags-apropos (string)
727 (while (re-search-forward string nil t
)
729 (princ (buffer-substring (point)
730 (progn (skip-chars-forward "^\177")
735 (defun etags-tags-table-files ()
738 (goto-char (point-min))
739 (while (search-forward "\f\n" nil t
)
741 (skip-chars-forward "^,\n")
742 (or (looking-at ",include$")
743 ;; Expand in the default-directory of the tags table buffer.
744 (setq files
(cons (expand-file-name (buffer-substring beg
(point)))
748 (defun etags-tags-included-tables ()
751 (goto-char (point-min))
752 (while (search-forward "\f\n" nil t
)
754 (skip-chars-forward "^,\n")
755 (if (looking-at ",include$")
756 ;; Expand in the default-directory of the tags table buffer.
757 (setq files
(cons (expand-file-name (buffer-substring beg
(point)))
761 ;; Empty tags file support.
763 (defun recognize-empty-tags-table ()
764 (and (zerop (buffer-size))
765 (mapcar (function (lambda (sym)
766 (make-local-variable sym
)
768 '(tags-table-files-function
769 tags-completion-table-function
770 find-tag-regexp-search-function
771 find-tag-search-function
772 tags-apropos-function
773 tags-included-tables-function
))
774 (set (make-local-variable 'verify-tags-table-function
)
776 (zerop (buffer-size)))))))
778 ;;; Match qualifier functions for tagnames.
780 ;; t if point is at a tag line that matches TAG "exactly".
781 ;; point should be just after a string that matches TAG.
782 (defun tag-exact-match-p (tag)
783 (and (looking-at "[ \t();,]?.*\177")
784 (let ((c (char-after (- (point) (length tag
)))))
785 (or (= c ?
\n) (= c ?\
) (= c ?
\t)))))
787 ;; t if point is at a tag line that matches TAG as a word.
788 ;; point should be just after a string that matches TAG.
789 (defun tag-word-match-p (tag)
790 (and (looking-at "\\b.*\177")
791 (save-excursion (backward-char (1+ (length tag
)))
792 (looking-at "\\b"))))
794 ;; t if point is in a tag line with a tag containing TAG as a substring.
795 (defun tag-any-match-p (tag)
796 (looking-at ".*\177"))
798 ;; t if point is at a tag line that matches RE as a regexp.
799 (defun tag-re-match-p (re)
803 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t
)
804 (re-search-backward re bol t
)))))
807 (defun next-file (&optional initialize novisit
)
808 "Select next file among files in current tags table.
809 Non-nil first argument (prefix arg, if interactive)
810 initializes to the beginning of the list of files in the tags table.
812 Non-nil second argument NOVISIT means use a temporary buffer
813 to save time and avoid uninteresting warnings.
815 Value is nil if the file was already visited;
816 if the file was newly read in, the value is the filename."
820 (visit-tags-table-buffer 'reset
)
821 (setq next-file-list
(tags-table-files))))
824 ;; When doing (visit-tag-table-buffer t),
825 ;; the tags table buffer must be current.
826 (if (and (visit-tags-table-buffer 'same
)
827 (visit-tags-table-buffer t
))
828 (setq next-file-list
(tags-table-files))
830 (get-buffer " *next-file*")
831 (kill-buffer " *next-file*"))
832 (error "All files processed."))))
833 (let ((new (not (get-file-buffer (car next-file-list
)))))
834 (if (not (and new novisit
))
835 (set-buffer (find-file-noselect (car next-file-list
) novisit
))
836 ;; Like find-file, but avoids random warning messages.
837 (set-buffer (get-buffer-create " *next-file*"))
838 (kill-all-local-variables)
840 (setq new
(car next-file-list
))
841 (insert-file-contents new nil
))
842 (setq next-file-list
(cdr next-file-list
))
845 (defvar tags-loop-operate nil
846 "Form for `tags-loop-continue' to eval to change one file.")
848 (defvar tags-loop-scan nil
849 "Form for `tags-loop-continue' to eval to scan one file.
850 If it returns non-nil, this file needs processing by evalling
851 \`tags-loop-operate'. Otherwise, move on to the next file.")
854 (defun tags-loop-continue (&optional first-time
)
855 "Continue last \\[tags-search] or \\[tags-query-replace] command.
856 Used noninteractively with non-nil argument to begin such a command.
857 Two variables control the processing we do on each file:
858 the value of `tags-loop-scan' is a form to be executed on each file
859 to see if it is interesting (it returns non-nil if so)
860 and `tags-loop-operate' is a form to execute to operate on an interesting file
861 If the latter returns non-nil, we exit; otherwise we scan the next file."
867 ;; Scan files quickly for the first or next interesting one.
868 (while (or first-time
871 (not (eval tags-loop-scan
))))
872 (setq new
(next-file first-time t
))
873 ;; If NEW is non-nil, we got a temp buffer,
874 ;; and NEW is the file name.
876 (and (not first-time
)
877 (> baud-rate search-slow-speed
)
879 (message "Scanning file %s..." (or new buffer-file-name
)))
880 (setq first-time nil
)
881 (goto-char (point-min)))
883 ;; If we visited it in a temp buffer, visit it now for real.
887 (set-buffer (find-file-noselect new
))
891 (switch-to-buffer (current-buffer))
893 ;; Now operate on the file.
894 ;; If value is non-nil, continue to scan the next file.
895 (eval tags-loop-operate
)))
897 (null tags-loop-operate
)
898 (message "Scanning file %s...found" buffer-file-name
))))
900 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
903 (defun tags-search (regexp)
904 "Search through all files listed in tags table for match for REGEXP.
905 Stops when a match is found.
906 To continue searching for next match, use command \\[tags-loop-continue].
908 See documentation of variable `tags-file-name'."
909 (interactive "sTags search (regexp): ")
910 (if (and (equal regexp
"")
911 (eq (car tags-loop-scan
) 're-search-forward
)
912 (eq tags-loop-operate t
))
913 ;; Continue last tags-search as if by M-,.
914 (tags-loop-continue nil
)
916 (list 're-search-forward regexp nil t
)
917 tags-loop-operate nil
)
918 (tags-loop-continue t
)))
921 (defun tags-query-replace (from to
&optional delimited
)
922 "Query-replace-regexp FROM with TO through all files listed in tags table.
923 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
924 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
925 with the command \\[tags-loop-continue].
927 See documentation of variable `tags-file-name'."
929 "sTags query replace (regexp): \nsTags query replace %s by: \nP")
930 (setq tags-loop-scan
(list 'prog1
931 (list 'if
(list 're-search-forward form nil t
)
932 ;; When we find a match, move back
933 ;; to the beginning of it so perform-replace
935 '(goto-char (match-beginning 0))))
936 tags-loop-operate
(list 'perform-replace from to t t delimited
))
937 (tags-loop-continue t
))
940 (defun list-tags (file)
941 "Display list of tags in file FILE.
942 FILE should not contain a directory specification
943 unless it has one in the tags table."
944 (interactive (list (completing-read "List tags in file: " nil
945 'tags-table-files t nil
)))
946 (with-output-to-temp-buffer "*Tags List*"
947 (princ "Tags in file ")
953 (while (visit-tags-table-buffer (if first-time
'reset t
))
954 (if (funcall list-tags-function file
)
957 (error "File %s not in current tags tables"))))))
960 (defun tags-apropos (regexp)
961 "Display list of all tags in tags table REGEXP matches."
962 (interactive "sTags apropos (regexp): ")
963 (with-output-to-temp-buffer "*Tags List*"
964 (princ "Tags matching regexp ")
968 (let ((first-time nil
))
969 (while (visit-tags-table-buffer (if first-time
'reset t
))
970 (funcall tags-apropos-function
))))))
972 ;;; XXX Kludge interface.
974 ;; XXX If a file is in multiple tables, selection may get the wrong one.
976 (defun select-tags-table ()
977 "Select a tags table file from a menu of those you have already used.
978 The list of tags tables to select from is stored in `tags-table-file-list';
979 see the doc of that variable if you want to add names to the list."
981 (pop-to-buffer "*Tags Table List*")
982 (setq buffer-read-only nil
)
984 (setq selective-display t
985 selective-display-ellipses nil
)
986 (let ((set-list tags-table-set-list
)
990 (setq desired-point
(point-marker))
991 (princ tags-table-list
(current-buffer))
993 (prin1 (car tags-table-list
) (current-buffer)) ;invisible
996 (if (eq (car set-list
) tags-table-list
)
997 ;; Already printed it.
999 (princ (car set-list
) (current-buffer))
1001 (prin1 (car (car set-list
)) (current-buffer)) ;invisible
1003 (setq set-list
(cdr set-list
)))
1007 (setq desired-point
(point-marker)))
1008 (insert tags-file-name
"\C-m")
1009 (prin1 tags-file-name
(current-buffer)) ;invisible
1011 (setq set-list
(delete tags-file-name
1012 (apply 'nconc
(cons tags-table-list
1013 (mapcar 'copy-sequence
1014 tags-table-set-list
)))))
1016 (insert (car set-list
) "\C-m")
1017 (prin1 (car set-list
) (current-buffer)) ;invisible
1019 (setq set-list
(delete (car set-list
) set-list
)))
1021 (insert-before-markers
1022 "Type `t' to select a tags table or set of tags tables:\n\n")
1024 (goto-char desired-point
))
1025 (set-window-start (selected-window) 1 t
))
1026 (set-buffer-modified-p nil
)
1027 (setq buffer-read-only t
1028 mode-name
"Select Tags Table")
1029 (let ((map (make-sparse-keymap)))
1030 (define-key map
"t" 'select-tags-table-select
)
1031 (define-key map
" " 'next-line
)
1032 (define-key map
"\^?" 'previous-line
)
1033 (define-key map
"n" 'next-line
)
1034 (define-key map
"p" 'previous-line
)
1035 (define-key map
"q" 'select-tags-table-quit
)
1036 (use-local-map map
)))
1038 (defun select-tags-table-select ()
1039 "Select the tags table named on this line."
1041 (search-forward "\C-m")
1042 (let ((name (read (current-buffer))))
1043 (visit-tags-table name
)
1044 (select-tags-table-quit)
1045 (message "Tags table now %s" name
)))
1047 (defun select-tags-table-quit ()
1048 "Kill the buffer and delete the selected window."
1050 (kill-buffer (current-buffer))
1055 (defun complete-tag ()
1056 "Perform tags completion on the text around point.
1057 Completes to the set of names listed in the current tags table.
1058 The string to complete is chosen in the same way as the default
1059 for \\[find-tag] (which see). See also `visit-tags-table-buffer'."
1061 (let ((pattern (funcall (or find-tag-default-function
1062 (get major-mode
'find-tag-default-function
)
1063 'find-tag-default
)))
1067 (error "Nothing to complete"))
1068 (search-backward pattern
)
1070 (forward-char (length pattern
))
1071 (setq completion
(try-completion pattern
'tags-complete-tag nil
))
1072 (cond ((eq completion t
))
1074 (message "Can't find completion for \"%s\"" pattern
)
1076 ((not (string= pattern completion
))
1077 (delete-region beg
(point))
1078 (insert completion
))
1080 (message "Making completion list...")
1081 (with-output-to-temp-buffer " *Completions*"
1082 (display-completion-list
1083 (all-completions pattern
'tags-complete-tag nil
)))
1084 (message "Making completion list...%s" "done")))))
1085 ;;;###autoload (define-key esc-map "?" 'complete-tag) ;? XXX
1089 ;;; etags.el ends here