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