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