]> code.delx.au - gnu-emacs/blob - lisp/progmodes/etags.el
(visit-tags-table-buffer): When trying to pick table, call
[gnu-emacs] / lisp / progmodes / etags.el
1 ;; etags.el --- etags facility for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1988, 1989, 1992 Free Software Foundation, Inc.
4
5 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
6 ;; Keywords: tools
7
8 ;; This file is part of GNU Emacs.
9
10 ;; GNU Emacs is free software; you can redistribute it and/or modify
11 ;; it under the terms of the GNU General Public License as published by
12 ;; the Free Software Foundation; either version 2, or (at your option)
13 ;; any later version.
14
15 ;; GNU Emacs is distributed in the hope that it will be useful,
16 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
17 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 ;; GNU General Public License for more details.
19
20 ;; You should have received a copy of the GNU General Public License
21 ;; along with GNU Emacs; see the file COPYING. If not, write to
22 ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.
23
24 ;;; Code:
25
26 ;;;###autoload
27 (defvar tags-file-name nil "\
28 *File name of tags table.
29 To switch to a new tags table, setting this variable is sufficient.
30 Use the `etags' program to make a tags table file.")
31 ;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ")
32
33 ;;;###autoload
34 (defvar tags-table-list nil
35 "*List of names of tags table files which are currently being searched.
36 Elements that are directories mean the file \"TAGS\" in that directory.
37 An element of nil means to look for a file \"TAGS\" in the current directory.
38 Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
39
40 (defvar tags-table-list-pointer nil
41 "Pointer into `tags-table-list' where the current state of searching is.
42 Might instead point into a list of included tags tables.
43 Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
44
45 (defvar tags-table-list-started-at nil
46 "Pointer into `tags-table-list', where the current search started.")
47
48 (defvar tags-table-parent-pointer-list nil
49 "Saved state of the tags table that included this one.
50 Each element is (POINTER . STARTED-AT), giving the values of
51 `tags-table-list-pointer' and `tags-table-list-started-at' from
52 before we moved into the current table.")
53
54 (defvar tags-table-set-list nil
55 "List of sets of tags table which have been used together in the past.
56 Each element is a list of strings which are file names.")
57
58 ;;;###autoload
59 (defvar find-tag-hook nil
60 "*Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'.
61 The value in the buffer in which \\[find-tag] is done is used,
62 not the value in the buffer \\[find-tag] goes to.")
63
64 ;;;###autoload
65 (defvar find-tag-default-function nil
66 "*A function of no arguments used by \\[find-tag] to pick a default tag.
67 If nil, and the symbol that is the value of `major-mode'
68 has a `find-tag-default-function' property (see `put'), that is used.
69 Otherwise, `find-tag-default' is used.")
70
71 ;;;###autoload
72 (defvar default-tags-table-function nil
73 "*If non-nil, a function of no arguments to choose a default tags file
74 for a particular buffer.")
75 \f
76 ;; Tags table state.
77 ;; These variables are local in tags table buffers.
78
79 (defvar tag-lines-already-matched nil
80 "List of positions of beginnings of lines within the tags table
81 that are already matched.")
82
83 (defvar tags-table-files nil
84 "List of file names covered by current tags table.
85 nil means it has not yet been computed; use `tags-table-files' to do so.")
86
87 (defvar tags-completion-table nil
88 "Alist of tag names defined in current tags table.")
89
90 (defvar tags-included-tables nil
91 "List of tags tables included by the current tags table.")
92
93 (defvar next-file-list nil
94 "List of files for \\[next-file] to process.")
95 \f
96 ;; Hooks for file formats.
97
98 (defvar tags-table-format-hooks '(etags-recognize-tags-table
99 recognize-empty-tags-table)
100 "List of functions to be called in a tags table buffer to identify
101 the type of tags table. The functions are called in order, with no arguments,
102 until one returns non-nil. The function should make buffer-local bindings
103 of the format-parsing tags function variables if successful.")
104
105 (defvar file-of-tag-function nil
106 "Function to do the work of `file-of-tag' (which see).")
107 (defvar tags-table-files-function nil
108 "Function to do the work of `tags-table-files' (which see).")
109 (defvar tags-completion-table-function nil
110 "Function to build the tags-completion-table.")
111 (defvar snarf-tag-function nil
112 "Function to get info about a matched tag for `goto-tag-location-function'.")
113 (defvar goto-tag-location-function nil
114 "Function of to go to the location in the buffer specified by a tag.
115 One argument, the tag info returned by `snarf-tag-function'.")
116 (defvar find-tag-regexp-search-function nil
117 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
118 (defvar find-tag-regexp-tag-order nil
119 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
120 (defvar find-tag-regexp-next-line-after-failure-p nil
121 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
122 (defvar find-tag-search-function nil
123 "Search function passed to `find-tag-in-order' for finding a tag.")
124 (defvar find-tag-tag-order nil
125 "Tag order passed to `find-tag-in-order' for finding a tag.")
126 (defvar find-tag-next-line-after-failure-p nil
127 "Flag passed to `find-tag-in-order' for finding a tag.")
128 (defvar list-tags-function nil
129 "Function to do the work of `list-tags' (which see).")
130 (defvar tags-apropos-function nil
131 "Function to do the work of `tags-apropos' (which see).")
132 (defvar tags-included-tables-function nil
133 "Function to do the work of `tags-included-tables' (which see).")
134 (defvar verify-tags-table-function nil
135 "Function to return t iff the current buffer vontains a valid
136 \(already initialized\) tags file.")
137 \f
138 (defun initialize-new-tags-table ()
139 "Initialize the tags table in the current buffer.
140 Returns non-nil iff it is a valid tags table."
141 (make-local-variable 'tag-lines-already-matched)
142 (make-local-variable 'tags-table-files)
143 (make-local-variable 'tags-completion-table)
144 (make-local-variable 'tags-included-tables)
145 (setq tags-table-files nil
146 tag-lines-already-matched nil
147 tags-completion-table nil
148 tags-included-tables nil)
149 ;; Value is t if we have found a valid tags table buffer.
150 (let ((hooks tags-table-format-hooks))
151 (while (and hooks
152 (not (funcall (car hooks))))
153 (setq hooks (cdr hooks)))
154 hooks))
155
156 ;;;###autoload
157 (defun visit-tags-table (file &optional local)
158 "Tell tags commands to use tags table file FILE.
159 FILE should be the name of a file created with the `etags' program.
160 A directory name is ok too; it means file TAGS in that directory.
161
162 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
163 With a prefix arg, set the buffer-local value instead.
164 When you find a tag with \\[find-tag], the buffer it finds the tag
165 in is given a local value of this variable which is the name of the tags
166 file the tag was in."
167 (interactive (list (read-file-name "Visit tags table: (default TAGS) "
168 default-directory
169 (expand-file-name "TAGS"
170 default-directory)
171 t)
172 current-prefix-arg))
173 (let ((tags-file-name file))
174 (save-excursion
175 (or (visit-tags-table-buffer 'same)
176 (signal 'file-error (list "Visiting tags table"
177 "file does not exist"
178 file)))
179 (setq file tags-file-name)))
180 (if local
181 (set (make-local-variable 'tags-file-name) file)
182 (setq-default tags-file-name file)))
183
184 ;; Move tags-table-list-pointer along and set tags-file-name.
185 ;; Returns nil when out of tables.
186 (defun tags-next-table (&optional reset no-includes)
187 (if reset
188 (setq tags-table-list-pointer tags-table-list)
189
190 (if (and (not no-includes)
191 (visit-tags-table-buffer 'same)
192 (tags-included-tables))
193 ;; Move into the included tags tables.
194 (setq tags-table-parent-pointer-list
195 (cons (cons tags-table-list-pointer tags-table-list-started-at)
196 tags-table-parent-pointer-list)
197 tags-table-list-pointer tags-included-tables
198 tags-table-list-started-at tags-included-tables)
199
200 ;; Go to the next table in the list.
201 (setq tags-table-list-pointer
202 (cdr tags-table-list-pointer))
203 (or tags-table-list-pointer
204 ;; Wrap around.
205 (setq tags-table-list-pointer tags-table-list))
206
207 (if (eq tags-table-list-pointer tags-table-list-started-at)
208 ;; We have come full circle.
209 (if tags-table-parent-pointer-list
210 ;; Pop back to the tags table which includes this one.
211 (progn
212 (setq tags-table-list-pointer
213 (car (car tags-table-parent-pointer-list))
214 tags-table-list-started-at
215 (cdr (car tags-table-parent-pointer-list))
216 tags-table-parent-pointer-list
217 (cdr tags-table-parent-pointer-list))
218 (tags-next-table nil t))
219 ;; All out of tags tables.
220 (setq tags-table-list-pointer nil))))
221
222 (and tags-table-list-pointer
223 (setq tags-file-name
224 (tags-expand-table-name (car tags-table-list-pointer))))))
225
226 (defun tags-expand-table-name (file)
227 (or file
228 ;; nil means look for TAGS in current directory.
229 (setq file default-directory))
230 (setq file (expand-file-name file))
231 (if (file-directory-p file)
232 (expand-file-name "TAGS" file)
233 file))
234
235 (defun tags-table-list-member (file &optional list)
236 (or list
237 (setq list tags-table-list))
238 (setq file (tags-expand-table-name file))
239 (while (and list
240 (not (string= file (tags-expand-table-name (car list)))))
241 (setq list (cdr list)))
242 list)
243
244 ;; Subroutine of visit-tags-table-buffer. Frobs its local vars.
245 ;; Search TABLES for one that has tags for THIS-FILE.
246 ;; Recurses on included tables.
247 (defun tags-table-including (this-file tables &optional recursing)
248 (let ((found nil))
249 (while (and (not found)
250 tables)
251 (let ((tags-file-name (tags-expand-table-name (car tables))))
252 (if (or (get-file-buffer tags-file-name)
253 (file-exists-p tags-file-name))
254 (progn
255 ;; Select the tags table buffer and get the file list up to date.
256 (visit-tags-table-buffer 'same)
257 (or tags-table-files
258 (setq tags-table-files
259 (funcall tags-table-files-function)))
260
261 (cond ((member this-file tags-table-files)
262 ;; Found it.
263 (setq found tables))
264
265 ((tags-included-tables)
266 (let ((old tags-table-parent-pointer-list))
267 (unwind-protect
268 (progn
269 (or recursing
270 ;; At top level (not in an included tags
271 ;; table), set the list to nil so we can
272 ;; collect just the elts from this run.
273 (setq tags-table-parent-pointer-list nil))
274 (setq found
275 (tags-table-including this-file
276 tags-included-tables
277 t))
278 (if found
279 (progn
280 (setq tags-table-parent-pointer-list
281 (cons
282 (cons tags-table-list-pointer
283 tags-table-list-started-at)
284 tags-table-parent-pointer-list)
285 tags-table-list-pointer found
286 tags-table-list-started-at found
287 ;; Don't frob lists later.
288 cont 'included))))
289 (or recursing
290 ;; Recursive calls have consed onto the front
291 ;; of the list, so it is now outermost first.
292 ;; We want it innermost first.
293 (setq tags-table-parent-pointer-list
294 (nconc (nreverse
295 tags-table-parent-pointer-list)
296 old))))))))))
297 (setq tables (cdr tables)))
298 found))
299
300 (defun visit-tags-table-buffer (&optional cont)
301 "Select the buffer containing the current tags table.
302 If optional arg is t, visit the next table in `tags-table-list'.
303 If optional arg is the atom `same', don't look for a new table;
304 just select the buffer.
305 If arg is nil or absent, choose a first buffer from information in
306 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
307 Returns t if it visits a tags table, or nil if there are no more in the list."
308 (cond ((eq cont 'same))
309
310 (cont
311 (if (tags-next-table)
312 ;; Skip over nonexistent files.
313 (while (and (let ((file (tags-expand-table-name tags-file-name)))
314 (not (or (get-file-buffer file)
315 (file-exists-p file))))
316 (tags-next-table)))))
317
318 (t
319 (setq tags-file-name
320 (or (cdr (assq 'tags-file-name (buffer-local-variables)))
321 (and default-tags-table-function
322 (funcall default-tags-table-function))
323 ;; Look for a tags table that contains
324 ;; tags for the current buffer's file.
325 ;; If one is found, the lists will be frobnicated,
326 ;; and CONT will be set non-nil so we don't do it below.
327 (let ((found (save-excursion
328 (tags-table-including buffer-file-name
329 tags-table-list))))
330 (and found
331 ;; Expand it so it won't be nil.
332 (tags-expand-table-name (car found))))
333 (tags-expand-table-name (car tags-table-list))
334 (tags-expand-table-name tags-file-name)
335 (expand-file-name
336 (read-file-name "Visit tags table: (default TAGS) "
337 default-directory
338 "TAGS"
339 t))))))
340
341 (setq tags-file-name (tags-expand-table-name tags-file-name))
342
343 (if (and (eq cont t) (null tags-table-list-pointer))
344 ;; All out of tables.
345 nil
346
347 (if (if (get-file-buffer tags-file-name)
348 (let (win)
349 (set-buffer (get-file-buffer tags-file-name))
350 (setq win (or verify-tags-table-function
351 (initialize-new-tags-table)))
352 (if (or (verify-visited-file-modtime (current-buffer))
353 (not (yes-or-no-p
354 "Tags file has changed, read new contents? ")))
355 (and win (funcall verify-tags-table-function))
356 (revert-buffer t t)
357 (initialize-new-tags-table)))
358 (set-buffer (find-file-noselect tags-file-name))
359 (or (string= tags-file-name buffer-file-name)
360 ;; find-file-noselect has changed the file name.
361 ;; Propagate change to tags-file-name and tags-table-list.
362 (let ((tail (member file tags-table-list)))
363 (if tail
364 (setcar tail buffer-file-name))
365 (setq tags-file-name buffer-file-name)))
366 (initialize-new-tags-table))
367
368 ;; We have a valid tags table.
369 (progn
370 ;; Bury the tags table buffer so it
371 ;; doesn't get in the user's way.
372 (bury-buffer (current-buffer))
373
374 (if cont
375 ;; No list frobbing required.
376 nil
377
378 ;; Look in the list for the table we chose.
379 (let ((elt (tags-table-list-member tags-file-name)))
380 (or elt
381 ;; The table is not in the current set.
382 ;; Try to find it in another previously used set.
383 (let ((sets tags-table-set-list))
384 (while (and sets
385 (not (setq elt (tags-table-list-member
386 tags-file-name (car sets)))))
387 (setq sets (cdr sets)))
388 (if sets
389 (progn
390 ;; Found in some other set. Switch to that set.
391 (or (memq tags-table-list tags-table-set-list)
392 ;; Save the current list.
393 (setq tags-table-set-list
394 (cons tags-table-list
395 tags-table-set-list)))
396 (setq tags-table-list (car sets)))
397
398 ;; Not found in any existing set.
399 (if (and tags-table-list
400 (y-or-n-p (concat "Add " tags-file-name
401 " to current list"
402 " of tags tables? ")))
403 ;; Add it to the current list.
404 (setq tags-table-list (cons tags-file-name
405 tags-table-list))
406 ;; Make a fresh list, and store the old one.
407 (or (memq tags-table-list tags-table-set-list)
408 (setq tags-table-set-list
409 (cons tags-table-list tags-table-set-list)))
410 (setq tags-table-list (list tags-file-name)))
411 (setq elt tags-table-list))))
412
413 (setq tags-table-list-started-at elt
414 tags-table-list-pointer elt)))
415
416 ;; Return of t says the tags table is valid.
417 t)
418
419 ;; The buffer was not valid. Don't use it again.
420 (kill-local-variable 'tags-file-name)
421 (setq tags-file-name nil)
422 (error "File %s is not a valid tags table" buffer-file-name))))
423 \f
424 (defun file-of-tag ()
425 "Return the file name of the file whose tags point is within.
426 Assumes the tags table is the current buffer.
427 File name returned is relative to tags table file's directory."
428 (funcall file-of-tag-function))
429
430 ;;;###autoload
431 (defun tags-table-files ()
432 "Return a list of files in the current tags table.
433 File names returned are absolute."
434 (or tags-table-files
435 (setq tags-table-files
436 (funcall tags-table-files-function))))
437
438 (defun tags-included-tables ()
439 "Return a list of tags tables included by the current table."
440 (or tags-included-tables
441 (setq tags-included-tables (funcall tags-included-tables-function))))
442 \f
443 ;; Build tags-completion-table on demand. The single current tags table
444 ;; and its included tags tables (and their included tables, etc.) have
445 ;; their tags included in the completion table.
446 (defun tags-completion-table ()
447 (or tags-completion-table
448 (condition-case ()
449 (prog2
450 (message "Making tags completion table for %s..." buffer-file-name)
451 (let ((included (tags-included-tables))
452 (table (funcall tags-completion-table-function)))
453 (save-excursion
454 (while included
455 (let ((tags-file-name (car included)))
456 (visit-tags-table-buffer 'same))
457 (if (tags-completion-table)
458 (mapatoms (function
459 (lambda (sym)
460 (intern (symbol-name sym) table)))
461 tags-completion-table))
462 (setq included (cdr included))))
463 (setq tags-completion-table table))
464 (message "Making tags completion table for %s...done"
465 buffer-file-name))
466 (quit (message "Tags completion table construction aborted.")
467 (setq tags-completion-table nil)))))
468
469 ;; Completion function for tags. Does normal try-completion,
470 ;; but builds tags-completion-table on demand.
471 (defun tags-complete-tag (string predicate what)
472 (save-excursion
473 (visit-tags-table-buffer)
474 (if (eq what t)
475 (all-completions string (tags-completion-table) predicate)
476 (try-completion string (tags-completion-table) predicate))))
477 \f
478 ;; Return a default tag to search for, based on the text at point.
479 (defun find-tag-default ()
480 (save-excursion
481 (while (looking-at "\\sw\\|\\s_")
482 (forward-char 1))
483 (if (or (re-search-backward "\\sw\\|\\s_"
484 (save-excursion (beginning-of-line) (point))
485 t)
486 (re-search-forward "\\(\\sw\\|\\s_\\)+"
487 (save-excursion (end-of-line) (point))
488 t))
489 (progn (goto-char (match-end 0))
490 (buffer-substring (point)
491 (progn (forward-sexp -1)
492 (while (looking-at "\\s'")
493 (forward-char 1))
494 (point))))
495 nil)))
496
497 ;; Read a tag name from the minibuffer with defaulting and completion.
498 (defun find-tag-tag (string)
499 (let* ((default (funcall (or find-tag-default-function
500 (get major-mode 'find-tag-default-function)
501 'find-tag-default)))
502 (spec (completing-read (if default
503 (format "%s(default %s) " string default)
504 string)
505 'tags-complete-tag)))
506 (list (if (equal spec "")
507 (or default (error "There is no default tag"))
508 spec))))
509
510 (defvar last-tag nil
511 "Last tag found by \\[find-tag].")
512
513 ;;;###autoload
514 (defun find-tag-noselect (tagname &optional next-p regexp-p)
515 "Find tag (in current tags table) whose name contains TAGNAME.
516 Returns the buffer containing the tag's definition moves its point there,
517 but does not select the buffer.
518 The default for TAGNAME is the expression in the buffer near point.
519
520 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
521 for another tag that matches the last tagname or regexp used. When there
522 are multiple matches for a tag, more exact matches are found first.
523
524 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
525
526 See documentation of variable `tags-file-name'."
527 (interactive (if current-prefix-arg
528 '(nil t)
529 (find-tag-tag "Find tag: ")))
530 (let ((local-find-tag-hook find-tag-hook))
531 (if next-p
532 (visit-tags-table-buffer 'same)
533 (setq last-tag tagname)
534 (visit-tags-table-buffer))
535 (prog1
536 (find-tag-in-order (if next-p last-tag tagname)
537 (if regexp-p
538 find-tag-regexp-search-function
539 find-tag-search-function)
540 (if regexp-p
541 find-tag-regexp-tag-order
542 find-tag-tag-order)
543 (if regexp-p
544 find-tag-regexp-next-line-after-failure-p
545 find-tag-next-line-after-failure-p)
546 (if regexp-p "matching" "containing")
547 (not next-p))
548 (run-hooks 'local-find-tag-hook))))
549
550 ;;;###autoload
551 (defun find-tag (tagname &optional next-p)
552 "Find tag (in current tags table) whose name contains TAGNAME.
553 Select the buffer containing the tag's definition, and move point there.
554 The default for TAGNAME is the expression in the buffer around or before point.
555
556 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
557 for another tag that matches the last tagname used. When there are
558 multiple matches, more exact matches are found first.
559
560 See documentation of variable `tags-file-name'."
561 (interactive (if current-prefix-arg
562 '(nil t)
563 (find-tag-tag "Find tag: ")))
564 (switch-to-buffer (find-tag-noselect tagname next-p)))
565 ;;;###autoload (define-key esc-map "." 'find-tag)
566
567 ;;;###autoload
568 (defun find-tag-other-window (tagname &optional next-p)
569 "Find tag (in current tags table) whose name contains TAGNAME.
570 Select the buffer containing the tag's definition
571 in another window, and move point there.
572 The default for TAGNAME is the expression in the buffer around or before point.
573
574 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
575 for another tag that matches the last tagname used. When there are
576 multiple matches, more exact matches are found first.
577
578 See documentation of variable `tags-file-name'."
579 (interactive (if current-prefix-arg
580 '(nil t)
581 (find-tag-tag "Find tag other window: ")))
582 (switch-to-buffer-other-window (find-tag-noselect tagname next-p)))
583 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
584
585 ;;;###autoload
586 (defun find-tag-other-frame (tagname &optional next-p)
587 "Find tag (in current tag table) whose name contains TAGNAME.
588 Selects the buffer that the tag is contained in in another frame
589 and puts point at its definition.
590 If TAGNAME is a null string, the expression in the buffer
591 around or before point is used as the tag name.
592 If second arg NEXT-P is non-nil (interactively, with prefix arg),
593 searches for the next tag in the tag table
594 that matches the tagname used in the previous find-tag.
595
596 See documentation of variable `tags-file-name'."
597 (interactive (if current-prefix-arg
598 '(nil t)
599 (find-tag-tag "Find tag other window: ")))
600 (let ((pop-up-frames t))
601 (find-tag-other-window tagname next-p)))
602 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
603
604 ;;;###autoload
605 (defun find-tag-regexp (regexp &optional next-p other-window)
606 "Find tag (in current tags table) whose name matches REGEXP.
607 Select the buffer containing the tag's definition and move point there.
608
609 If second arg NEXT-P is non-nil (interactively, with prefix arg), search
610 for another tag that matches the last tagname used.
611
612 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
613
614 See documentation of variable `tags-file-name'."
615 (interactive (if current-prefix-arg
616 '(nil t)
617 (read-string "Find tag regexp: ")))
618 (funcall (if other-window 'switch-to-buffer-other-window 'switch-to-buffer)
619 (find-tag-noselect regexp next-p t)))
620 \f
621 ;; Internal tag finding function.
622
623 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
624 ;; any member of the function list ORDER (third arg). If ORDER is nil,
625 ;; use saved state to continue a previous search.
626
627 ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
628 ;; an error message.
629
630 ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
631 ;; point should be moved to the next line.
632
633 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
634 ;; beginning of tags file, and perform inner loop: for each naive match for
635 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
636 ;; qualifier-func. If it qualifies, go to the specified line in the
637 ;; specified source file and return. Qualified matches are remembered to
638 ;; avoid repetition. State is saved so that the loop can be continued.
639
640 (defun find-tag-in-order (pattern search-forward-func order
641 next-line-after-failure-p matching
642 first-search)
643 (let (file ;name of file containing tag
644 tag-info ;where to find the tag in FILE
645 tags-table-file ;name of tags file
646 (first-table t)
647 (tag-order order)
648 goto-func
649 )
650 (save-excursion
651 (or first-search ;find-tag-noselect has already done it.
652 (visit-tags-table-buffer 'same))
653
654 ;; Get a qualified match.
655 (catch 'qualified-match-found
656
657 (while (or first-table
658 (visit-tags-table-buffer t))
659
660 (if first-search
661 (setq tag-lines-already-matched nil))
662
663 (and first-search first-table
664 ;; Start at beginning of tags file.
665 (goto-char (point-min)))
666 (setq first-table nil)
667
668 (setq tags-table-file buffer-file-name)
669 (while order
670 (while (funcall search-forward-func pattern nil t)
671 ;; Naive match found. Qualify the match.
672 (and (funcall (car order) pattern)
673 ;; Make sure it is not a previous qualified match.
674 ;; Use of `memq' depends on numbers being eq.
675 (not (memq (save-excursion (beginning-of-line) (point))
676 tag-lines-already-matched))
677 (throw 'qualified-match-found nil))
678 (if next-line-after-failure-p
679 (forward-line 1)))
680 ;; Try the next flavor of match.
681 (setq order (cdr order))
682 (goto-char (point-min)))
683 (setq order tag-order))
684 ;; We throw out on match, so only get here if there were no matches.
685 (error "No %stags %s %s" (if first-search "" "more ")
686 matching pattern))
687
688 ;; Found a tag; extract location info.
689 (beginning-of-line)
690 (setq tag-lines-already-matched (cons (point)
691 tag-lines-already-matched))
692 ;; Expand the filename, using the tags table buffer's default-directory.
693 (setq file (expand-file-name (file-of-tag))
694 tag-info (funcall snarf-tag-function))
695
696 ;; Get the local value in the tags table buffer.
697 (setq goto-func goto-tag-location-function)
698
699 ;; Find the right line in the specified file.
700 (set-buffer (find-file-noselect file))
701 (widen)
702 (push-mark)
703 (funcall goto-func tag-info)
704
705 ;; Give this buffer a local value of tags-file-name.
706 ;; The next time visit-tags-table-buffer is called,
707 ;; it will use the same tags table that found a match in this buffer.
708 (make-local-variable 'tags-file-name)
709 (setq tags-file-name tags-table-file)
710
711 ;; Return the buffer where the tag was found.
712 (current-buffer))))
713 \f
714 ;; `etags' TAGS file format support.
715
716 (defun etags-recognize-tags-table ()
717 (and (eq (char-after 1) ?\f)
718 ;; It is annoying to flash messages on the screen briefly,
719 ;; and this message is not useful. -- rms
720 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
721 (mapcar (function (lambda (elt)
722 (make-local-variable (car elt))
723 (set (car elt) (cdr elt))))
724 '((file-of-tag-function . etags-file-of-tag)
725 (tags-table-files-function . etags-tags-table-files)
726 (tags-completion-table-function . etags-tags-completion-table)
727 (snarf-tag-function . etags-snarf-tag)
728 (goto-tag-location-function . etags-goto-tag-location)
729 (find-tag-regexp-search-function . re-search-forward)
730 (find-tag-regexp-tag-order . (tag-re-match-p))
731 (find-tag-regexp-next-line-after-failuire-p . t)
732 (find-tag-search-function . search-forward)
733 (find-tag-tag-order . (tag-exact-match-p tag-word-match-p
734 tag-any-match-p))
735 (find-tag-next-line-after-failure-p . nil)
736 (list-tags-function . etags-list-tags)
737 (tags-apropos-function . etags-tags-apropos)
738 (tags-included-tables-function . etags-tags-included-tables)
739 (verify-tags-table-function . etags-verify-tags-table)
740 ))))
741
742 (defun etags-verify-tags-table ()
743 (= (char-after 1) ?\f))
744
745 (defun etags-file-of-tag ()
746 (save-excursion
747 (search-backward "\f\n")
748 (forward-char 2)
749 (buffer-substring (point)
750 (progn (skip-chars-forward "^,") (point)))))
751
752 (defun etags-tags-completion-table ()
753 (let ((table (make-vector 511 0)))
754 (save-excursion
755 (goto-char (point-min))
756 (while (search-forward "\177" nil t)
757 ;; Handle multiple \177's on a line.
758 (save-excursion
759 (skip-chars-backward "^-A-Za-z0-9_$\n") ;sym syntax? XXX
760 (or (bolp)
761 (intern (buffer-substring
762 (point)
763 (progn
764 (skip-chars-backward "-A-Za-z0-9_$")
765 ;; ??? New
766 ;; `::' in the middle of a C++ tag.
767 (and (= (preceding-char) ?:)
768 (= (char-after (- (point) 2)) ?:)
769 (progn
770 (backward-char 2)
771 (skip-chars-backward
772 "-A-Za-z0-9_$")))
773 (point)))
774 table)))))
775 table))
776
777 (defun etags-snarf-tag ()
778 (let (tag-text startpos)
779 (search-forward "\177")
780 (setq tag-text (buffer-substring (1- (point))
781 (save-excursion (beginning-of-line)
782 (point))))
783 (search-forward ",")
784 (setq startpos (string-to-int (buffer-substring
785 (point)
786 (progn (skip-chars-forward "0-9")
787 (point)))))
788 ;; Leave point on the next line of the tags file.
789 (forward-line 1)
790 (cons tag-text startpos)))
791
792 (defun etags-goto-tag-location (tag-info)
793 (let ((startpos (cdr tag-info))
794 ;; This constant is 1/2 the initial search window.
795 ;; There is no sense in making it too small,
796 ;; since just going around the loop once probably
797 ;; costs about as much as searching 2000 chars.
798 (offset 1000)
799 (found nil)
800 (pat (concat "^" (regexp-quote (car tag-info)))))
801 (or startpos
802 (setq startpos (point-min)))
803 (while (and (not found)
804 (progn
805 (goto-char (- startpos offset))
806 (not (bobp))))
807 (setq found
808 (re-search-forward pat (+ startpos offset) t)
809 offset (* 3 offset))) ; expand search window
810 (or found
811 (re-search-forward pat nil t)
812 (error "`%s' not found in %s; time to rerun etags"
813 pat buffer-file-name)))
814 (beginning-of-line))
815
816 (defun etags-list-tags (file)
817 (goto-char 1)
818 (if (not (search-forward (concat "\f\n" file ",") nil t))
819 nil
820 (forward-line 1)
821 (while (not (or (eobp) (looking-at "\f")))
822 (princ (buffer-substring (point)
823 (progn (skip-chars-forward "^\177")
824 (point))))
825 (terpri)
826 (forward-line 1))))
827
828 (defun etags-tags-apropos (string)
829 (goto-char 1)
830 (while (re-search-forward string nil t)
831 (beginning-of-line)
832 (princ (buffer-substring (point)
833 (progn (skip-chars-forward "^\177")
834 (point))))
835 (terpri)
836 (forward-line 1)))
837
838 (defun etags-tags-table-files ()
839 (let ((files nil)
840 beg)
841 (goto-char (point-min))
842 (while (search-forward "\f\n" nil t)
843 (setq beg (point))
844 (skip-chars-forward "^,\n")
845 (or (looking-at ",include$")
846 ;; Expand in the default-directory of the tags table buffer.
847 (setq files (cons (expand-file-name (buffer-substring beg (point)))
848 files))))
849 (nreverse files)))
850
851 (defun etags-tags-included-tables ()
852 (let ((files nil)
853 beg)
854 (goto-char (point-min))
855 (while (search-forward "\f\n" nil t)
856 (setq beg (point))
857 (skip-chars-forward "^,\n")
858 (if (looking-at ",include$")
859 ;; Expand in the default-directory of the tags table buffer.
860 (setq files (cons (expand-file-name (buffer-substring beg (point)))
861 files))))
862 (nreverse files)))
863 \f
864 ;; Empty tags file support.
865
866 (defun recognize-empty-tags-table ()
867 (and (zerop (buffer-size))
868 (mapcar (function (lambda (sym)
869 (make-local-variable sym)
870 (set sym 'ignore)))
871 '(tags-table-files-function
872 tags-completion-table-function
873 find-tag-regexp-search-function
874 find-tag-search-function
875 tags-apropos-function
876 tags-included-tables-function))
877 (set (make-local-variable 'verify-tags-table-function)
878 (function (lambda ()
879 (zerop (buffer-size)))))))
880 \f
881 ;;; Match qualifier functions for tagnames.
882
883 ;; This might be a neat idea, but it's too hairy at the moment.
884 ;;(defmacro tags-with-syntax (&rest body)
885 ;; (` (let ((current (current-buffer))
886 ;; (otable (syntax-table))
887 ;; (buffer (find-file-noselect (file-of-tag)))
888 ;; table)
889 ;; (unwind-protect
890 ;; (progn
891 ;; (set-buffer buffer)
892 ;; (setq table (syntax-table))
893 ;; (set-buffer current)
894 ;; (set-syntax-table table)
895 ;; (,@ body))
896 ;; (set-syntax-table otable)))))
897 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
898
899 ;; t if point is at a tag line that matches TAG "exactly".
900 ;; point should be just after a string that matches TAG.
901 (defun tag-exact-match-p (tag)
902 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177") ;not a symbol char
903 (save-excursion
904 (backward-char (1+ (length tag)))
905 (and (looking-at "\\Sw") (looking-at "\\S_")))))
906
907 ;; t if point is at a tag line that matches TAG as a word.
908 ;; point should be just after a string that matches TAG.
909 (defun tag-word-match-p (tag)
910 (and (looking-at "\\b.*\177")
911 (save-excursion (backward-char (1+ (length tag)))
912 (looking-at "\\b"))))
913
914 ;; t if point is in a tag line with a tag containing TAG as a substring.
915 (defun tag-any-match-p (tag)
916 (looking-at ".*\177"))
917
918 ;; t if point is at a tag line that matches RE as a regexp.
919 (defun tag-re-match-p (re)
920 (save-excursion
921 (beginning-of-line)
922 (let ((bol (point)))
923 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t)
924 (re-search-backward re bol t)))))
925 \f
926 ;;;###autoload
927 (defun next-file (&optional initialize novisit)
928 "Select next file among files in current tags table.
929 Non-nil first argument (prefix arg, if interactive)
930 initializes to the beginning of the list of files in the tags table.
931
932 Non-nil second argument NOVISIT means use a temporary buffer
933 to save time and avoid uninteresting warnings.
934
935 Value is nil if the file was already visited;
936 if the file was newly read in, the value is the filename."
937 (interactive "P")
938 (and initialize
939 (save-excursion
940 (visit-tags-table-buffer)
941 (setq next-file-list (tags-table-files))))
942 (or next-file-list
943 (save-excursion
944 ;; Get the files from the next tags table.
945 ;; When doing (visit-tags-table-buffer t),
946 ;; the tags table buffer must be current.
947 (if (and (visit-tags-table-buffer 'same)
948 (visit-tags-table-buffer t))
949 (setq next-file-list (tags-table-files))
950 (and novisit
951 (get-buffer " *next-file*")
952 (kill-buffer " *next-file*"))
953 (error "All files processed."))))
954 (let ((new (not (get-file-buffer (car next-file-list)))))
955 (if (not (and new novisit))
956 (set-buffer (find-file-noselect (car next-file-list) novisit))
957 ;; Like find-file, but avoids random warning messages.
958 (set-buffer (get-buffer-create " *next-file*"))
959 (kill-all-local-variables)
960 (erase-buffer)
961 (setq new (car next-file-list))
962 (insert-file-contents new nil))
963 (setq next-file-list (cdr next-file-list))
964 new))
965
966 (defvar tags-loop-operate nil
967 "Form for `tags-loop-continue' to eval to change one file.")
968
969 (defvar tags-loop-scan nil
970 "Form for `tags-loop-continue' to eval to scan one file.
971 If it returns non-nil, this file needs processing by evalling
972 \`tags-loop-operate'. Otherwise, move on to the next file.")
973
974 ;;;###autoload
975 (defun tags-loop-continue (&optional first-time)
976 "Continue last \\[tags-search] or \\[tags-query-replace] command.
977 Used noninteractively with non-nil argument to begin such a command.
978 Two variables control the processing we do on each file:
979 the value of `tags-loop-scan' is a form to be executed on each file
980 to see if it is interesting (it returns non-nil if so)
981 and `tags-loop-operate' is a form to execute to operate on an interesting file
982 If the latter returns non-nil, we exit; otherwise we scan the next file."
983 (interactive)
984 (let (new
985 (messaged nil))
986 (while
987 (progn
988 ;; Scan files quickly for the first or next interesting one.
989 (while (or first-time
990 (save-restriction
991 (widen)
992 (not (eval tags-loop-scan))))
993 (setq new (next-file first-time t))
994 ;; If NEW is non-nil, we got a temp buffer,
995 ;; and NEW is the file name.
996 (if (or messaged
997 (and (not first-time)
998 (> baud-rate search-slow-speed)
999 (setq messaged t)))
1000 (message "Scanning file %s..." (or new buffer-file-name)))
1001 (setq first-time nil)
1002 (goto-char (point-min)))
1003
1004 ;; If we visited it in a temp buffer, visit it now for real.
1005 (if new
1006 (let ((pos (point)))
1007 (erase-buffer)
1008 (set-buffer (find-file-noselect new))
1009 (widen)
1010 (goto-char pos)))
1011
1012 (switch-to-buffer (current-buffer))
1013
1014 ;; Now operate on the file.
1015 ;; If value is non-nil, continue to scan the next file.
1016 (eval tags-loop-operate)))
1017 (and messaged
1018 (null tags-loop-operate)
1019 (message "Scanning file %s...found" buffer-file-name))))
1020
1021 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
1022
1023 ;;;###autoload
1024 (defun tags-search (regexp)
1025 "Search through all files listed in tags table for match for REGEXP.
1026 Stops when a match is found.
1027 To continue searching for next match, use command \\[tags-loop-continue].
1028
1029 See documentation of variable `tags-file-name'."
1030 (interactive "sTags search (regexp): ")
1031 (if (and (equal regexp "")
1032 (eq (car tags-loop-scan) 're-search-forward)
1033 (eq tags-loop-operate t))
1034 ;; Continue last tags-search as if by M-,.
1035 (tags-loop-continue nil)
1036 (setq tags-loop-scan
1037 (list 're-search-forward regexp nil t)
1038 tags-loop-operate nil)
1039 (tags-loop-continue t)))
1040
1041 ;;;###autoload
1042 (defun tags-query-replace (from to &optional delimited)
1043 "Query-replace-regexp FROM with TO through all files listed in tags table.
1044 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1045 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
1046 with the command \\[tags-loop-continue].
1047
1048 See documentation of variable `tags-file-name'."
1049 (interactive
1050 "sTags query replace (regexp): \nsTags query replace %s by: \nP")
1051 (setq tags-loop-scan (list 'prog1
1052 (list 'if (list 're-search-forward form nil t)
1053 ;; When we find a match, move back
1054 ;; to the beginning of it so perform-replace
1055 ;; will see it.
1056 '(goto-char (match-beginning 0))))
1057 tags-loop-operate (list 'perform-replace from to t t delimited))
1058 (tags-loop-continue t))
1059 \f
1060 ;;;###autoload
1061 (defun list-tags (file)
1062 "Display list of tags in file FILE.
1063 FILE should not contain a directory specification
1064 unless it has one in the tags table."
1065 (interactive (list (completing-read "List tags in file: " nil
1066 'tags-table-files t nil)))
1067 (with-output-to-temp-buffer "*Tags List*"
1068 (princ "Tags in file ")
1069 (princ file)
1070 (terpri)
1071 (save-excursion
1072 (let ((first-time t)
1073 (gotany nil))
1074 (while (visit-tags-table-buffer (not first-time))
1075 (if (funcall list-tags-function file)
1076 (setq gotany t)))
1077 (or gotany
1078 (error "File %s not in current tags tables"))))))
1079
1080 ;;;###autoload
1081 (defun tags-apropos (regexp)
1082 "Display list of all tags in tags table REGEXP matches."
1083 (interactive "sTags apropos (regexp): ")
1084 (with-output-to-temp-buffer "*Tags List*"
1085 (princ "Tags matching regexp ")
1086 (prin1 regexp)
1087 (terpri)
1088 (save-excursion
1089 (let ((first-time t))
1090 (while (visit-tags-table-buffer (not first-time))
1091 (setq first-time nil)
1092 (funcall tags-apropos-function regexp))))))
1093 \f
1094 ;;; XXX Kludge interface.
1095
1096 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1097 ;;;###autoload
1098 (defun select-tags-table ()
1099 "Select a tags table file from a menu of those you have already used.
1100 The list of tags tables to select from is stored in `tags-table-file-list';
1101 see the doc of that variable if you want to add names to the list."
1102 (interactive)
1103 (pop-to-buffer "*Tags Table List*")
1104 (setq buffer-read-only nil)
1105 (erase-buffer)
1106 (setq selective-display t
1107 selective-display-ellipses nil)
1108 (let ((set-list tags-table-set-list)
1109 (desired-point nil))
1110 (if tags-table-list
1111 (progn
1112 (setq desired-point (point-marker))
1113 (princ tags-table-list (current-buffer))
1114 (insert "\C-m")
1115 (prin1 (car tags-table-list) (current-buffer)) ;invisible
1116 (insert "\n")))
1117 (while set-list
1118 (if (eq (car set-list) tags-table-list)
1119 ;; Already printed it.
1120 ()
1121 (princ (car set-list) (current-buffer))
1122 (insert "\C-m")
1123 (prin1 (car (car set-list)) (current-buffer)) ;invisible
1124 (insert "\n"))
1125 (setq set-list (cdr set-list)))
1126 (if tags-file-name
1127 (progn
1128 (or desired-point
1129 (setq desired-point (point-marker)))
1130 (insert tags-file-name "\C-m")
1131 (prin1 tags-file-name (current-buffer)) ;invisible
1132 (insert "\n")))
1133 (setq set-list (delete tags-file-name
1134 (apply 'nconc (cons tags-table-list
1135 (mapcar 'copy-sequence
1136 tags-table-set-list)))))
1137 (while set-list
1138 (insert (car set-list) "\C-m")
1139 (prin1 (car set-list) (current-buffer)) ;invisible
1140 (insert "\n")
1141 (setq set-list (delete (car set-list) set-list)))
1142 (goto-char 1)
1143 (insert-before-markers
1144 "Type `t' to select a tags table or set of tags tables:\n\n")
1145 (if desired-point
1146 (goto-char desired-point))
1147 (set-window-start (selected-window) 1 t))
1148 (set-buffer-modified-p nil)
1149 (setq buffer-read-only t
1150 mode-name "Select Tags Table")
1151 (let ((map (make-sparse-keymap)))
1152 (define-key map "t" 'select-tags-table-select)
1153 (define-key map " " 'next-line)
1154 (define-key map "\^?" 'previous-line)
1155 (define-key map "n" 'next-line)
1156 (define-key map "p" 'previous-line)
1157 (define-key map "q" 'select-tags-table-quit)
1158 (use-local-map map)))
1159
1160 (defun select-tags-table-select ()
1161 "Select the tags table named on this line."
1162 (interactive)
1163 (search-forward "\C-m")
1164 (let ((name (read (current-buffer))))
1165 (visit-tags-table name)
1166 (select-tags-table-quit)
1167 (message "Tags table now %s" name)))
1168
1169 (defun select-tags-table-quit ()
1170 "Kill the buffer and delete the selected window."
1171 (interactive)
1172 (kill-buffer (current-buffer))
1173 (or (one-window-p)
1174 (delete-window)))
1175 \f
1176 ;;;###autoload
1177 (defun complete-tag ()
1178 "Perform tags completion on the text around point.
1179 Completes to the set of names listed in the current tags table.
1180 The string to complete is chosen in the same way as the default
1181 for \\[find-tag] (which see)."
1182 (interactive)
1183 (or tags-table-list
1184 tags-file-name
1185 (error (substitute-command-keys
1186 "No tags table loaded. Try \\[visit-tags-table].")))
1187 (let ((pattern (funcall (or find-tag-default-function
1188 (get major-mode 'find-tag-default-function)
1189 'find-tag-default)))
1190 beg
1191 completion)
1192 (or pattern
1193 (error "Nothing to complete"))
1194 (search-backward pattern)
1195 (setq beg (point))
1196 (forward-char (length pattern))
1197 (setq completion (try-completion pattern 'tags-complete-tag nil))
1198 (cond ((eq completion t))
1199 ((null completion)
1200 (message "Can't find completion for \"%s\"" pattern)
1201 (ding))
1202 ((not (string= pattern completion))
1203 (delete-region beg (point))
1204 (insert completion))
1205 (t
1206 (message "Making completion list...")
1207 (with-output-to-temp-buffer " *Completions*"
1208 (display-completion-list
1209 (all-completions pattern 'tags-complete-tag nil)))
1210 (message "Making completion list...%s" "done")))))
1211
1212 ;;;###autoload (define-key esc-map "\t" 'complete-tag)
1213 \f
1214 (provide 'etags)
1215
1216 ;;; etags.el ends here