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