]> code.delx.au - gnu-emacs/blob - lisp/progmodes/etags.el
(c++-mode): Doc fix.
[gnu-emacs] / lisp / progmodes / etags.el
1 ;;; etags.el --- etags facility for Emacs
2
3 ;; Copyright (C) 1985, 1986, 1988, 1989, 1992, 1993, 1994, 1995
4 ;; Free Software Foundation, Inc.
5
6 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
7 ;; Keywords: tools
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
13 ;; the Free Software Foundation; either version 2, or (at your option)
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
25 ;;; Code:
26
27 ;;;###autoload
28 (defvar tags-file-name nil
29 "*File name of tags table.
30 To switch to a new tags table, setting this variable is sufficient.
31 If you set this variable, do not also set `tags-table-list'.
32 Use the `etags' program to make a tags table file.")
33 ;; Make M-x set-variable tags-file-name like M-x visit-tags-table.
34 ;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ")
35
36 ;;;###autoload
37 ;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
38 (defvar tags-table-list nil
39 "*List of file names of tags tables to search.
40 An element that is a directory means the file \"TAGS\" in that directory.
41 To switch to a new list of tags tables, setting this variable is sufficient.
42 If you set this variable, do not also set `tags-file-name'.
43 Use the `etags' program to make a tags table file.")
44
45 ;;;###autoload
46 (defvar tags-add-tables 'ask-user
47 "*Control whether to add a new tags table to the current list.
48 t means do; nil means don't (always start a new list).
49 Any other value means ask the user whether to add a new tags table
50 to the current list (as opposed to starting a new list).")
51
52 (defvar tags-table-computed-list nil
53 "List of tags tables to search, computed from `tags-table-list'.
54 This includes tables implicitly included by other tables. The list is not
55 always complete: the included tables of a table are not known until that
56 table is read into core. An element that is `t' is a placeholder
57 indicating that the preceding element is a table that has not been read
58 into core and might contain included tables to search.
59 See `tags-table-check-computed-list'.")
60
61 (defvar tags-table-computed-list-for nil
62 "Value of `tags-table-list' that `tags-table-computed-list' corresponds to.
63 If `tags-table-list' changes, `tags-table-computed-list' is thrown away and
64 recomputed; see `tags-table-check-computed-list'.")
65
66 (defvar tags-table-list-pointer nil
67 "Pointer into `tags-table-computed-list' for the current state of searching.
68 Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
69
70 (defvar tags-table-list-started-at nil
71 "Pointer into `tags-table-computed-list', where the current search started.")
72
73 (defvar tags-table-set-list nil
74 "List of sets of tags table which have been used together in the past.
75 Each element is a list of strings which are file names.")
76
77 ;;;###autoload
78 (defvar find-tag-hook nil
79 "*Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'.
80 The value in the buffer in which \\[find-tag] is done is used,
81 not the value in the buffer \\[find-tag] goes to.")
82
83 ;;;###autoload
84 (defvar find-tag-default-function nil
85 "*A function of no arguments used by \\[find-tag] to pick a default tag.
86 If nil, and the symbol that is the value of `major-mode'
87 has a `find-tag-default-function' property (see `put'), that is used.
88 Otherwise, `find-tag-default' is used.")
89
90 (defvar default-tags-table-function nil
91 "If non-nil, a function to choose a default tags file for a buffer.
92 This function receives no arguments and should return the default
93 tags table file to use for the current buffer.")
94
95 (defvar tags-location-stack nil
96 "List of markers which are locations visited by \\[find-tag].
97 Pop back to the last location with \\[negative-argument] \\[find-tag].")
98 \f
99 ;; Tags table state.
100 ;; These variables are local in tags table buffers.
101
102 (defvar tag-lines-already-matched nil
103 "List of positions of beginnings of lines within the tags table
104 that are already matched.")
105
106 (defvar tags-table-files nil
107 "List of file names covered by current tags table.
108 nil means it has not yet been computed; use `tags-table-files' to do so.")
109
110 (defvar tags-completion-table nil
111 "Alist of tag names defined in current tags table.")
112
113 (defvar tags-included-tables nil
114 "List of tags tables included by the current tags table.")
115
116 (defvar next-file-list nil
117 "List of files for \\[next-file] to process.")
118 \f
119 ;; Hooks for file formats.
120
121 (defvar tags-table-format-hooks '(etags-recognize-tags-table
122 recognize-empty-tags-table)
123 "List of functions to be called in a tags table buffer to identify
124 the type of tags table. The functions are called in order, with no arguments,
125 until one returns non-nil. The function should make buffer-local bindings
126 of the format-parsing tags function variables if successful.")
127
128 (defvar file-of-tag-function nil
129 "Function to do the work of `file-of-tag' (which see).")
130 (defvar tags-table-files-function nil
131 "Function to do the work of `tags-table-files' (which see).")
132 (defvar tags-completion-table-function nil
133 "Function to build the tags-completion-table.")
134 (defvar snarf-tag-function nil
135 "Function to get info about a matched tag for `goto-tag-location-function'.")
136 (defvar goto-tag-location-function nil
137 "Function of to go to the location in the buffer specified by a tag.
138 One argument, the tag info returned by `snarf-tag-function'.")
139 (defvar find-tag-regexp-search-function nil
140 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
141 (defvar find-tag-regexp-tag-order nil
142 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
143 (defvar find-tag-regexp-next-line-after-failure-p nil
144 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
145 (defvar find-tag-search-function nil
146 "Search function passed to `find-tag-in-order' for finding a tag.")
147 (defvar find-tag-tag-order nil
148 "Tag order passed to `find-tag-in-order' for finding a tag.")
149 (defvar find-tag-next-line-after-failure-p nil
150 "Flag passed to `find-tag-in-order' for finding a tag.")
151 (defvar list-tags-function nil
152 "Function to do the work of `list-tags' (which see).")
153 (defvar tags-apropos-function nil
154 "Function to do the work of `tags-apropos' (which see).")
155 (defvar tags-included-tables-function nil
156 "Function to do the work of `tags-included-tables' (which see).")
157 (defvar verify-tags-table-function nil
158 "Function to return t iff the current buffer contains a valid
159 \(already initialized\) tags file.")
160 \f
161 ;; Initialize the tags table in the current buffer.
162 ;; Returns non-nil iff it is a valid tags table. On
163 ;; non-nil return, the tags table state variable are
164 ;; made buffer-local and initialized to nil.
165 (defun initialize-new-tags-table ()
166 (set (make-local-variable 'tag-lines-already-matched) nil)
167 (set (make-local-variable 'tags-table-files) nil)
168 (set (make-local-variable 'tags-completion-table) nil)
169 (set (make-local-variable 'tags-included-tables) nil)
170 ;; Value is t if we have found a valid tags table buffer.
171 (let ((hooks tags-table-format-hooks))
172 (while (and hooks
173 (not (funcall (car hooks))))
174 (setq hooks (cdr hooks)))
175 hooks))
176
177 ;;;###autoload
178 (defun visit-tags-table (file &optional local)
179 "Tell tags commands to use tags table file FILE.
180 FILE should be the name of a file created with the `etags' program.
181 A directory name is ok too; it means file TAGS in that directory.
182
183 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
184 With a prefix arg, set the buffer-local value instead.
185 When you find a tag with \\[find-tag], the buffer it finds the tag
186 in is given a local value of this variable which is the name of the tags
187 file the tag was in."
188 (interactive (list (read-file-name "Visit tags table: (default TAGS) "
189 default-directory
190 (expand-file-name "TAGS"
191 default-directory)
192 t)
193 current-prefix-arg))
194 (or (stringp file) (signal 'wrong-type-argument (list 'stringp file)))
195 ;; Bind tags-file-name so we can control below whether the local or
196 ;; global value gets set. Calling visit-tags-table-buffer will
197 ;; initialize a buffer for the file and set tags-file-name to the
198 ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
199 ;; initialize a buffer for FILE and set tags-file-name to the
200 ;; fully-expanded name.
201 (let ((tags-file-name file))
202 (save-excursion
203 (or (visit-tags-table-buffer file)
204 (signal 'file-error (list "Visiting tags table"
205 "file does not exist"
206 file)))
207 ;; Set FILE to the expanded name.
208 (setq file tags-file-name)))
209 (if local
210 ;; Set the local value of tags-file-name.
211 (set (make-local-variable 'tags-file-name) file)
212 ;; Set the global value of tags-file-name.
213 (setq-default tags-file-name file)))
214
215 (defun tags-table-check-computed-list ()
216 "Compute `tags-table-computed-list' from `tags-table-list' if necessary."
217 (let ((expanded-list (mapcar 'tags-expand-table-name tags-table-list)))
218 (or (equal tags-table-computed-list-for expanded-list)
219 ;; The list (or default-directory) has changed since last computed.
220 (let* ((compute-for (mapcar 'copy-sequence expanded-list))
221 (tables (copy-sequence compute-for)) ;Mutated in the loop.
222 (computed nil)
223 table-buffer)
224
225 (while tables
226 (setq computed (cons (car tables) computed)
227 table-buffer (get-file-buffer (car tables)))
228 (if (and table-buffer
229 ;; There is a buffer visiting the file. Now make sure
230 ;; it is initialized as a tag table buffer.
231 (save-excursion
232 (tags-verify-table (buffer-file-name table-buffer))))
233 (save-excursion
234 (set-buffer table-buffer)
235 (if (tags-included-tables)
236 ;; Insert the included tables into the list we
237 ;; are processing.
238 (setcdr tables (nconc (mapcar 'tags-expand-table-name
239 (tags-included-tables))
240 (cdr tables)))))
241 ;; This table is not in core yet. Insert a placeholder
242 ;; saying we must read it into core to check for included
243 ;; tables before searching the next table in the list.
244 (setq computed (cons t computed)))
245 (setq tables (cdr tables)))
246
247 ;; Record the tags-table-list value (and the context of the
248 ;; current directory) we computed from.
249 (setq tags-table-computed-list-for compute-for
250 tags-table-computed-list (nreverse computed))))))
251
252 ;; Extend `tags-table-computed-list' to remove the first `t' placeholder.
253 ;; An element of the list that is `t' is a placeholder indicating that the
254 ;; preceding element is a table that has not been read into core and might
255 ;; contain included tables to search. On return, the first placeholder
256 ;; element will be gone and the element before it read into core and its
257 ;; included tables inserted into the list.
258 (defun tags-table-extend-computed-list ()
259 (let ((list tags-table-computed-list))
260 (while (not (eq (nth 1 list) t))
261 (setq list (cdr list)))
262 (save-excursion
263 (if (tags-verify-table (car list))
264 ;; We are now in the buffer visiting (car LIST). Extract its
265 ;; list of included tables and insert it into the computed list.
266 (let ((tables (tags-included-tables))
267 (computed nil)
268 table-buffer)
269 (while tables
270 (setq computed (cons (car tables) computed)
271 table-buffer (get-file-buffer (car tables)))
272 (if table-buffer
273 (save-excursion
274 (set-buffer table-buffer)
275 (if (tags-included-tables)
276 ;; Insert the included tables into the list we
277 ;; are processing.
278 (setcdr tables (append (tags-included-tables)
279 tables))))
280 ;; This table is not in core yet. Insert a placeholder
281 ;; saying we must read it into core to check for included
282 ;; tables before searching the next table in the list.
283 (setq computed (cons t computed)))
284 (setq tables (cdr tables)))
285 (setq computed (nreverse computed))
286 ;; COMPUTED now contains the list of included tables (and
287 ;; tables included by them, etc.). Now splice this into the
288 ;; current list.
289 (setcdr list (nconc computed (cdr (cdr list)))))
290 ;; It was not a valid table, so just remove the following placeholder.
291 (setcdr list (cdr (cdr list)))))))
292
293 ;; Expand tags table name FILE into a complete file name.
294 (defun tags-expand-table-name (file)
295 (setq file (expand-file-name file))
296 (if (file-directory-p file)
297 (expand-file-name "TAGS" file)
298 file))
299
300 ;; Like member, but comparison is done after tags-expand-table-name on both
301 ;; sides and elements of LIST that are t are skipped.
302 (defun tags-table-list-member (file list)
303 (setq file (tags-expand-table-name file))
304 (while (and list
305 (or (eq (car list) t)
306 (not (string= file (tags-expand-table-name (car list))))))
307 (setq list (cdr list)))
308 list)
309
310 (defun tags-verify-table (file)
311 "Read FILE into a buffer and verify that it is a valid tags table.
312 Sets the current buffer to one visiting FILE (if it exists).
313 Returns non-nil iff it is a valid table."
314 (if (get-file-buffer file)
315 ;; The file is already in a buffer. Check for the visited file
316 ;; having changed since we last used it.
317 (let (win)
318 (set-buffer (get-file-buffer file))
319 (setq win (or verify-tags-table-function (initialize-new-tags-table)))
320 (if (or (verify-visited-file-modtime (current-buffer))
321 (not (yes-or-no-p
322 (format "Tags file %s has changed, read new contents? "
323 file))))
324 (and win (funcall verify-tags-table-function))
325 (revert-buffer t t)
326 (initialize-new-tags-table)))
327 (and (file-exists-p file)
328 (progn
329 (set-buffer (find-file-noselect file))
330 (or (string= file buffer-file-name)
331 ;; find-file-noselect has changed the file name.
332 ;; Propagate the change to tags-file-name and tags-table-list.
333 (let ((tail (member file tags-table-list)))
334 (if tail
335 (setcar tail buffer-file-name))
336 (if (eq file tags-file-name)
337 (setq tags-file-name buffer-file-name))))
338 (initialize-new-tags-table)))))
339
340 ;; Subroutine of visit-tags-table-buffer. Search the current tags tables
341 ;; for one that has tags for THIS-FILE (or that includes a table that
342 ;; does). Return the name of the first table table listing THIS-FILE; if
343 ;; the table is one included by another table, it is the master table that
344 ;; we return. If CORE-ONLY is non-nil, check only tags tables that are
345 ;; already in buffers--don't visit any new files.
346 (defun tags-table-including (this-file core-only)
347 (let ((tables tags-table-computed-list)
348 (found nil))
349 ;; Loop over the list, looking for a table containing tags for THIS-FILE.
350 (while (and (not found)
351 tables)
352
353 (if core-only
354 ;; Skip tables not in core.
355 (while (eq (nth 1 tables) t)
356 (setq tables (cdr (cdr tables))))
357 (if (eq (nth 1 tables) t)
358 ;; This table has not been read into core yet. Read it in now.
359 (tags-table-extend-computed-list)))
360
361 (if tables
362 ;; Select the tags table buffer and get the file list up to date.
363 (let ((tags-file-name (car tables)))
364 (visit-tags-table-buffer 'same)
365 (if (member this-file (mapcar 'expand-file-name
366 (tags-table-files)))
367 ;; Found it.
368 (setq found tables))))
369 (setq tables (cdr tables)))
370 (if found
371 ;; Now determine if the table we found was one included by another
372 ;; table, not explicitly listed. We do this by checking each
373 ;; element of the computed list to see if it appears in the user's
374 ;; explicit list; the last element we will check is FOUND itself.
375 ;; Then we return the last one which did in fact appear in
376 ;; tags-table-list.
377 (let ((could-be nil)
378 (elt tags-table-computed-list))
379 (while (not (eq elt (cdr found)))
380 (if (tags-table-list-member (car elt) tags-table-list)
381 ;; This table appears in the user's list, so it could be
382 ;; the one which includes the table we found.
383 (setq could-be (car elt)))
384 (setq elt (cdr elt))
385 (if (eq t (car elt))
386 (setq elt (cdr elt))))
387 ;; The last element we found in the computed list before FOUND
388 ;; that appears in the user's list will be the table that
389 ;; included the one we found.
390 could-be))))
391
392 ;; Subroutine of visit-tags-table-buffer. Move tags-table-list-pointer
393 ;; along and set tags-file-name. Returns nil when out of tables.
394 (defun tags-next-table ()
395 ;; If there is a placeholder element next, compute the list to replace it.
396 (while (eq (nth 1 tags-table-list-pointer) t)
397 (tags-table-extend-computed-list))
398
399 ;; Go to the next table in the list.
400 (setq tags-table-list-pointer (cdr tags-table-list-pointer))
401 (or tags-table-list-pointer
402 ;; Wrap around.
403 (setq tags-table-list-pointer tags-table-computed-list))
404
405 (if (eq tags-table-list-pointer tags-table-list-started-at)
406 ;; We have come full circle. No more tables.
407 (setq tags-table-list-pointer nil)
408 ;; Set tags-file-name to the name from the list. It is already expanded.
409 (setq tags-file-name (car tags-table-list-pointer))))
410
411 (defun visit-tags-table-buffer (&optional cont)
412 "Select the buffer containing the current tags table.
413 If optional arg is a string, visit that file as a tags table.
414 If optional arg is t, visit the next table in `tags-table-list'.
415 If optional arg is the atom `same', don't look for a new table;
416 just select the buffer visiting `tags-file-name'.
417 If arg is nil or absent, choose a first buffer from information in
418 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
419 Returns t if it visits a tags table, or nil if there are no more in the list."
420
421 ;; Set tags-file-name to the tags table file we want to visit.
422 (cond ((eq cont 'same)
423 ;; Use the ambient value of tags-file-name.
424 (or tags-file-name
425 (error (substitute-command-keys
426 (concat "No tags table in use! "
427 "Use \\[visit-tags-table] to select one.")))))
428
429 ((eq t cont)
430 ;; Find the next table.
431 (if (tags-next-table)
432 ;; Skip over nonexistent files.
433 (while (and (not (or (get-file-buffer tags-file-name)
434 (file-exists-p tags-file-name)))
435 (tags-next-table)))))
436
437 (t
438 ;; Pick a table out of our hat.
439 (tags-table-check-computed-list) ;Get it up to date, we might use it.
440 (setq tags-file-name
441 (or
442 ;; If passed a string, use that.
443 (if (stringp cont)
444 (prog1 cont
445 (setq cont nil)))
446 ;; First, try a local variable.
447 (cdr (assq 'tags-file-name (buffer-local-variables)))
448 ;; Second, try a user-specified function to guess.
449 (and default-tags-table-function
450 (funcall default-tags-table-function))
451 ;; Third, look for a tags table that contains tags for the
452 ;; current buffer's file. If one is found, the lists will
453 ;; be frobnicated, and CONT will be set non-nil so we don't
454 ;; do it below.
455 (and buffer-file-name
456 (or
457 ;; First check only tables already in buffers.
458 (tags-table-including buffer-file-name t)
459 ;; Since that didn't find any, now do the
460 ;; expensive version: reading new files.
461 (tags-table-including buffer-file-name nil)))
462 ;; Fourth, use the user variable tags-file-name, if it is
463 ;; not already in the current list.
464 (and tags-file-name
465 (not (tags-table-list-member tags-file-name
466 tags-table-computed-list))
467 tags-file-name)
468 ;; Fifth, use the user variable giving the table list.
469 ;; Find the first element of the list that actually exists.
470 (let ((list tags-table-list)
471 file)
472 (while (and list
473 (setq file (tags-expand-table-name (car list)))
474 (not (get-file-buffer file))
475 (not (file-exists-p file)))
476 (setq list (cdr list)))
477 (car list))
478 ;; Finally, prompt the user for a file name.
479 (expand-file-name
480 (read-file-name "Visit tags table: (default TAGS) "
481 default-directory
482 "TAGS"
483 t))))))
484
485 ;; Expand the table name into a full file name.
486 (setq tags-file-name (tags-expand-table-name tags-file-name))
487
488 (if (and (eq cont t)
489 (null tags-table-list-pointer))
490 ;; All out of tables.
491 nil
492
493 ;; Verify that tags-file-name names a valid tags table.
494 ;; Bind another variable with the value of tags-file-name
495 ;; before we switch buffers, in case tags-file-name is buffer-local.
496 (let ((curbuf (current-buffer))
497 (local-tags-file-name tags-file-name))
498 (if (tags-verify-table local-tags-file-name)
499
500 ;; We have a valid tags table.
501 (progn
502 ;; Bury the tags table buffer so it
503 ;; doesn't get in the user's way.
504 (bury-buffer (current-buffer))
505
506 ;; If this was a new table selection (CONT is nil), make
507 ;; sure tags-table-list includes the chosen table, and
508 ;; update the list pointer variables.
509 (or cont
510 ;; Look in the list for the table we chose.
511 (let ((found (tags-table-list-member
512 local-tags-file-name
513 tags-table-computed-list)))
514 (if found
515 ;; There it is. Just switch to it.
516 (setq tags-table-list-pointer found
517 tags-table-list-started-at found)
518
519 ;; The table is not in the current set.
520 ;; Try to find it in another previously used set.
521 (let ((sets tags-table-set-list))
522 (while (and sets
523 (not (tags-table-list-member
524 local-tags-file-name
525 (car sets))))
526 (setq sets (cdr sets)))
527 (if sets
528 ;; Found in some other set. Switch to that set.
529 (progn
530 (or (memq tags-table-list tags-table-set-list)
531 ;; Save the current list.
532 (setq tags-table-set-list
533 (cons tags-table-list
534 tags-table-set-list)))
535 (setq tags-table-list (car sets)))
536
537 ;; Not found in any existing set.
538 (if (and tags-table-list
539 (or (eq t tags-add-tables)
540 (and tags-add-tables
541 (y-or-n-p
542 (concat "Keep current list of "
543 "tags tables also? ")))))
544 ;; Add it to the current list.
545 (setq tags-table-list (cons local-tags-file-name
546 tags-table-list))
547
548 ;; Make a fresh list, and store the old one.
549 (message "Starting a new list of tags tables")
550 (or (null tags-table-list)
551 (memq tags-table-list tags-table-set-list)
552 (setq tags-table-set-list
553 (cons tags-table-list
554 tags-table-set-list)))
555 (setq tags-table-list (list local-tags-file-name))))
556
557 ;; Recompute tags-table-computed-list.
558 (tags-table-check-computed-list)
559 ;; Set the tags table list state variables to start
560 ;; over from tags-table-computed-list.
561 (setq tags-table-list-started-at tags-table-computed-list
562 tags-table-list-pointer
563 tags-table-computed-list)))))
564
565 ;; Return of t says the tags table is valid.
566 t)
567
568 ;; The buffer was not valid. Don't use it again.
569 (set-buffer curbuf)
570 (kill-local-variable 'tags-file-name)
571 (if (eq local-tags-file-name tags-file-name)
572 (setq tags-file-name nil))
573 (error "File %s is not a valid tags table" local-tags-file-name)))))
574
575 (defun tags-reset-tags-tables ()
576 "Reset tags state to cancel effect of any previous \\[visit-tags-table]
577 or \\[find-tag]."
578 (interactive)
579 (setq tags-file-name nil
580 tags-location-stack nil
581 tags-table-list nil
582 tags-table-computed-list nil
583 tags-table-computed-list-for nil
584 tags-table-list-pointer nil
585 tags-table-list-started-at nil
586 tags-table-set-list nil))
587 \f
588 (defun file-of-tag ()
589 "Return the file name of the file whose tags point is within.
590 Assumes the tags table is the current buffer.
591 File name returned is relative to tags table file's directory."
592 (funcall file-of-tag-function))
593
594 ;;;###autoload
595 (defun tags-table-files ()
596 "Return a list of files in the current tags table.
597 Assumes the tags table is the current buffer. The file names are returned
598 as they appeared in the `etags' command that created the table, usually
599 without directory names."
600 (or tags-table-files
601 (setq tags-table-files
602 (funcall tags-table-files-function))))
603
604 (defun tags-included-tables ()
605 "Return a list of tags tables included by the current table.
606 Assumes the tags table is the current buffer."
607 (or tags-included-tables
608 (setq tags-included-tables (funcall tags-included-tables-function))))
609 \f
610 ;; Build tags-completion-table on demand. The single current tags table
611 ;; and its included tags tables (and their included tables, etc.) have
612 ;; their tags included in the completion table.
613 (defun tags-completion-table ()
614 (or tags-completion-table
615 (condition-case ()
616 (prog2
617 (message "Making tags completion table for %s..." buffer-file-name)
618 (let ((included (tags-included-tables))
619 (table (funcall tags-completion-table-function)))
620 (save-excursion
621 ;; Iterate over the list of included tables, and combine each
622 ;; included table's completion obarray to the parent obarray.
623 (while included
624 ;; Visit the buffer.
625 (let ((tags-file-name (car included)))
626 (visit-tags-table-buffer 'same))
627 ;; Recurse in that buffer to compute its completion table.
628 (if (tags-completion-table)
629 ;; Combine the tables.
630 (mapatoms (function
631 (lambda (sym)
632 (intern (symbol-name sym) table)))
633 tags-completion-table))
634 (setq included (cdr included))))
635 (setq tags-completion-table table))
636 (message "Making tags completion table for %s...done"
637 buffer-file-name))
638 (quit (message "Tags completion table construction aborted.")
639 (setq tags-completion-table nil)))))
640
641 ;; Completion function for tags. Does normal try-completion,
642 ;; but builds tags-completion-table on demand.
643 (defun tags-complete-tag (string predicate what)
644 (save-excursion
645 ;; If we need to ask for the tag table, allow that.
646 (let ((enable-recursive-minibuffers t))
647 (visit-tags-table-buffer))
648 (if (eq what t)
649 (all-completions string (tags-completion-table) predicate)
650 (try-completion string (tags-completion-table) predicate))))
651 \f
652 ;; Return a default tag to search for, based on the text at point.
653 (defun find-tag-default ()
654 (save-excursion
655 (while (looking-at "\\sw\\|\\s_")
656 (forward-char 1))
657 (if (or (re-search-backward "\\sw\\|\\s_"
658 (save-excursion (beginning-of-line) (point))
659 t)
660 (re-search-forward "\\(\\sw\\|\\s_\\)+"
661 (save-excursion (end-of-line) (point))
662 t))
663 (progn (goto-char (match-end 0))
664 (buffer-substring (point)
665 (progn (forward-sexp -1)
666 (while (looking-at "\\s'")
667 (forward-char 1))
668 (point))))
669 nil)))
670
671 ;; Read a tag name from the minibuffer with defaulting and completion.
672 (defun find-tag-tag (string)
673 (let* ((default (funcall (or find-tag-default-function
674 (get major-mode 'find-tag-default-function)
675 'find-tag-default)))
676 (spec (completing-read (if default
677 (format "%s(default %s) " string default)
678 string)
679 'tags-complete-tag)))
680 (if (equal spec "")
681 (or default (error "There is no default tag"))
682 spec)))
683
684 (defvar last-tag nil
685 "Last tag found by \\[find-tag].")
686
687 ;; Get interactive args for find-tag{-noselect,-other-window,-regexp}.
688 (defun find-tag-interactive (prompt &optional no-default)
689 (if current-prefix-arg
690 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
691 '-
692 t))
693 (list (if no-default
694 (read-string prompt)
695 (find-tag-tag prompt)))))
696
697 (defvar find-tag-history nil)
698
699 ;;;###autoload
700 (defun find-tag-noselect (tagname &optional next-p regexp-p)
701 "Find tag (in current tags table) whose name contains TAGNAME.
702 Returns the buffer containing the tag's definition and moves its point there,
703 but does not select the buffer.
704 The default for TAGNAME is the expression in the buffer near point.
705
706 If second arg NEXT-P is t (interactively, with prefix arg), search for
707 another tag that matches the last tagname or regexp used. When there are
708 multiple matches for a tag, more exact matches are found first. If NEXT-P
709 is the atom `-' (interactively, with prefix arg that is a negative number
710 or just \\[negative-argument]), pop back to the previous tag gone to.
711
712 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
713
714 See documentation of variable `tags-file-name'."
715 (interactive (find-tag-interactive "Find tag: "))
716
717 (setq find-tag-history (cons tagname find-tag-history))
718 ;; Save the current buffer's value of `find-tag-hook' before selecting the
719 ;; tags table buffer.
720 (let ((local-find-tag-hook find-tag-hook))
721 (if (eq '- next-p)
722 ;; Pop back to a previous location.
723 (if (null tags-location-stack)
724 (error "No previous tag locations")
725 (let ((marker (car tags-location-stack)))
726 ;; Pop the stack.
727 (setq tags-location-stack (cdr tags-location-stack))
728 (prog1
729 ;; Move to the saved location.
730 (set-buffer (marker-buffer marker))
731 (goto-char (marker-position marker))
732 ;; Kill that marker so it doesn't slow down editing.
733 (set-marker marker nil nil)
734 ;; Run the user's hook. Do we really want to do this for pop?
735 (run-hooks 'local-find-tag-hook))))
736 (if next-p
737 ;; Find the same table we last used.
738 (visit-tags-table-buffer 'same)
739 ;; Pick a table to use.
740 (visit-tags-table-buffer)
741 ;; Record TAGNAME for a future call with NEXT-P non-nil.
742 (setq last-tag tagname))
743 ;; Record the location so we can pop back to it later.
744 (let ((marker (make-marker)))
745 (save-excursion
746 (set-buffer
747 ;; find-tag-in-order does the real work.
748 (find-tag-in-order
749 (if next-p last-tag tagname)
750 (if regexp-p
751 find-tag-regexp-search-function
752 find-tag-search-function)
753 (if regexp-p
754 find-tag-regexp-tag-order
755 find-tag-tag-order)
756 (if regexp-p
757 find-tag-regexp-next-line-after-failure-p
758 find-tag-next-line-after-failure-p)
759 (if regexp-p "matching" "containing")
760 (not next-p)))
761 (set-marker marker (point))
762 (run-hooks 'local-find-tag-hook)
763 (setq tags-location-stack
764 (cons marker tags-location-stack))
765 (current-buffer))))))
766
767 ;;;###autoload
768 (defun find-tag (tagname &optional next-p regexp-p)
769 "Find tag (in current tags table) whose name contains TAGNAME.
770 Select the buffer containing the tag's definition, and move point there.
771 The default for TAGNAME is the expression in the buffer around or before point.
772
773 If second arg NEXT-P is t (interactively, with prefix arg), search for
774 another tag that matches the last tagname or regexp used. When there are
775 multiple matches for a tag, more exact matches are found first. If NEXT-P
776 is the atom `-' (interactively, with prefix arg that is a negative number
777 or just \\[negative-argument]), pop back to the previous tag gone to.
778
779 See documentation of variable `tags-file-name'."
780 (interactive (find-tag-interactive "Find tag: "))
781 (switch-to-buffer (find-tag-noselect tagname next-p regexp-p)))
782 ;;;###autoload (define-key esc-map "." 'find-tag)
783
784 ;;;###autoload
785 (defun find-tag-other-window (tagname &optional next-p regexp-p)
786 "Find tag (in current tags table) whose name contains TAGNAME.
787 Select the buffer containing the tag's definition in another window, and
788 move point there. The default for TAGNAME is the expression in the buffer
789 around or before point.
790
791 If second arg NEXT-P is t (interactively, with prefix arg), search for
792 another tag that matches the last tagname or regexp used. When there are
793 multiple matches for a tag, more exact matches are found first. If NEXT-P
794 is negative (interactively, with prefix arg that is a negative number or
795 just \\[negative-argument]), pop back to the previous tag gone to.
796
797 See documentation of variable `tags-file-name'."
798 (interactive (find-tag-interactive "Find tag other window: "))
799
800 ;; This hair is to deal with the case where the tag is found in the
801 ;; selected window's buffer; without the hair, point is moved in both
802 ;; windows. To prevent this, we save the selected window's point before
803 ;; doing find-tag-noselect, and restore it after.
804 (let* ((window-point (window-point (selected-window)))
805 (tagbuf (find-tag-noselect tagname next-p regexp-p))
806 (tagpoint (progn (set-buffer tagbuf) (point))))
807 (set-window-point (prog1
808 (selected-window)
809 (switch-to-buffer-other-window tagbuf)
810 ;; We have to set this new window's point; it
811 ;; might already have been displaying a
812 ;; different portion of tagbuf, in which case
813 ;; switch-to-buffer-other-window doesn't set
814 ;; the window's point from the buffer.
815 (set-window-point (selected-window) tagpoint))
816 window-point)))
817 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
818
819 ;;;###autoload
820 (defun find-tag-other-frame (tagname &optional next-p)
821 "Find tag (in current tags table) whose name contains TAGNAME.
822 Select the buffer containing the tag's definition in another frame, and
823 move point there. The default for TAGNAME is the expression in the buffer
824 around or before point.
825
826 If second arg NEXT-P is t (interactively, with prefix arg), search for
827 another tag that matches the last tagname or regexp used. When there are
828 multiple matches for a tag, more exact matches are found first. If NEXT-P
829 is negative (interactively, with prefix arg that is a negative number or
830 just \\[negative-argument]), pop back to the previous tag gone to.
831
832 See documentation of variable `tags-file-name'."
833 (interactive (find-tag-interactive "Find tag other frame: "))
834 (let ((pop-up-frames t))
835 (find-tag-other-window tagname next-p)))
836 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
837
838 ;;;###autoload
839 (defun find-tag-regexp (regexp &optional next-p other-window)
840 "Find tag (in current tags table) whose name matches REGEXP.
841 Select the buffer containing the tag's definition and move point there.
842
843 If second arg NEXT-P is t (interactively, with prefix arg), search for
844 another tag that matches the last tagname or regexp used. When there are
845 multiple matches for a tag, more exact matches are found first. If NEXT-P
846 is negative (interactively, with prefix arg that is a negative number or
847 just \\[negative-argument]), pop back to the previous tag gone to.
848
849 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
850
851 See documentation of variable `tags-file-name'."
852 (interactive (find-tag-interactive "Find tag regexp: " t))
853 ;; We go through find-tag-other-window to do all the display hair there.
854 (funcall (if other-window 'find-tag-other-window 'find-tag)
855 regexp next-p t))
856 ;;;###autoload (define-key esc-map [?\C-.] 'find-tag-regexp)
857 \f
858 ;; Internal tag finding function.
859
860 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
861 ;; any member of the function list ORDER (third arg). If ORDER is nil,
862 ;; use saved state to continue a previous search.
863
864 ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
865 ;; an error message.
866
867 ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
868 ;; point should be moved to the next line.
869
870 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
871 ;; beginning of tags file, and perform inner loop: for each naive match for
872 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
873 ;; qualifier-func. If it qualifies, go to the specified line in the
874 ;; specified source file and return. Qualified matches are remembered to
875 ;; avoid repetition. State is saved so that the loop can be continued.
876
877 (defun find-tag-in-order (pattern
878 search-forward-func
879 order
880 next-line-after-failure-p
881 matching
882 first-search)
883 (let (file ;name of file containing tag
884 tag-info ;where to find the tag in FILE
885 (first-table t)
886 (tag-order order)
887 goto-func
888 )
889 (save-excursion
890 (or first-search ;find-tag-noselect has already done it.
891 (visit-tags-table-buffer 'same))
892
893 ;; Get a qualified match.
894 (catch 'qualified-match-found
895
896 ;; Iterate over the list of tags tables.
897 (while (or first-table
898 (visit-tags-table-buffer t))
899
900 (if first-search
901 (setq tag-lines-already-matched nil))
902
903 (and first-search first-table
904 ;; Start at beginning of tags file.
905 (goto-char (point-min)))
906
907 (setq first-table nil)
908
909 ;; Iterate over the list of ordering predicates.
910 (while order
911 (while (funcall search-forward-func pattern nil t)
912 ;; Naive match found. Qualify the match.
913 (and (funcall (car order) pattern)
914 ;; Make sure it is not a previous qualified match.
915 ;; Use of `memq' depends on numbers being eq.
916 (not (memq (save-excursion (beginning-of-line) (point))
917 tag-lines-already-matched))
918 (throw 'qualified-match-found nil))
919 (if next-line-after-failure-p
920 (forward-line 1)))
921 ;; Try the next flavor of match.
922 (setq order (cdr order))
923 (goto-char (point-min)))
924 (setq order tag-order))
925 ;; We throw out on match, so only get here if there were no matches.
926 (error "No %stags %s %s" (if first-search "" "more ")
927 matching pattern))
928
929 ;; Found a tag; extract location info.
930 (beginning-of-line)
931 (setq tag-lines-already-matched (cons (point)
932 tag-lines-already-matched))
933 ;; Expand the filename, using the tags table buffer's default-directory.
934 (setq file (expand-file-name (file-of-tag))
935 tag-info (funcall snarf-tag-function))
936
937 ;; Get the local value in the tags table buffer before switching buffers.
938 (setq goto-func goto-tag-location-function)
939
940 ;; Find the right line in the specified file.
941 (set-buffer (find-file-noselect file))
942 (widen)
943 (push-mark)
944 (funcall goto-func tag-info)
945
946 ;; Return the buffer where the tag was found.
947 (current-buffer))))
948 \f
949 ;; `etags' TAGS file format support.
950
951 ;; If the current buffer is a valid etags TAGS file, give it local values of
952 ;; the tags table format variables, and return non-nil.
953 (defun etags-recognize-tags-table ()
954 (and (etags-verify-tags-table)
955 ;; It is annoying to flash messages on the screen briefly,
956 ;; and this message is not useful. -- rms
957 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
958 (mapcar (function (lambda (elt)
959 (set (make-local-variable (car elt)) (cdr elt))))
960 '((file-of-tag-function . etags-file-of-tag)
961 (tags-table-files-function . etags-tags-table-files)
962 (tags-completion-table-function . etags-tags-completion-table)
963 (snarf-tag-function . etags-snarf-tag)
964 (goto-tag-location-function . etags-goto-tag-location)
965 (find-tag-regexp-search-function . re-search-forward)
966 (find-tag-regexp-tag-order . (tag-re-match-p))
967 (find-tag-regexp-next-line-after-failure-p . t)
968 (find-tag-search-function . search-forward)
969 (find-tag-tag-order . (tag-exact-file-name-match-p
970 tag-exact-match-p
971 tag-symbol-match-p
972 tag-word-match-p
973 tag-any-match-p))
974 (find-tag-next-line-after-failure-p . nil)
975 (list-tags-function . etags-list-tags)
976 (tags-apropos-function . etags-tags-apropos)
977 (tags-included-tables-function . etags-tags-included-tables)
978 (verify-tags-table-function . etags-verify-tags-table)
979 ))))
980
981 ;; Return non-nil iff the current buffer is a valid etags TAGS file.
982 (defun etags-verify-tags-table ()
983 ;; Use eq instead of = in case char-after returns nil.
984 (eq (char-after 1) ?\f))
985
986 (defun etags-file-of-tag ()
987 (save-excursion
988 (if (looking-at "./")
989 (re-search-forward "\\([^\n]+\\),[0-9]*\n")
990 (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n"))
991 (buffer-substring (match-beginning 1) (match-end 1))))
992
993
994 (defun etags-tags-completion-table ()
995 (let ((table (make-vector 511 0)))
996 (save-excursion
997 (goto-char (point-min))
998 ;; This monster regexp matches an etags tag line.
999 ;; \1 is the string to match;
1000 ;; \2 is not interesting;
1001 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
1002 ;; \4 is not interesting;
1003 ;; \5 is the explicitly-specified tag name.
1004 ;; \6 is the line to start searching at;
1005 ;; \7 is the char to start searching at.
1006 (while (re-search-forward
1007 "^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
1008 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
1009 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
1010 nil t)
1011 (intern (if (match-beginning 5)
1012 ;; There is an explicit tag name.
1013 (buffer-substring (match-beginning 5) (match-end 5))
1014 ;; No explicit tag name. Best guess.
1015 (buffer-substring (match-beginning 3) (match-end 3)))
1016 table)))
1017 table))
1018
1019 (defun etags-snarf-tag ()
1020 (let (tag-text line startpos)
1021 (if (save-excursion
1022 (forward-line -1)
1023 (looking-at "\f\n"))
1024 ;; The match was for a source file name, not any tag within a file.
1025 ;; Give text of t, meaning to go exactly to the location we specify,
1026 ;; the beginning of the file.
1027 (setq tag-text t
1028 line nil
1029 startpos 1)
1030
1031 ;; Find the end of the tag and record the whole tag text.
1032 (search-forward "\177")
1033 (setq tag-text (buffer-substring (1- (point))
1034 (save-excursion (beginning-of-line)
1035 (point))))
1036 ;; Skip explicit tag name if present.
1037 (search-forward "\001" (save-excursion (forward-line 1) (point)) t)
1038 (if (looking-at "[0-9]")
1039 (setq line (string-to-int (buffer-substring
1040 (point)
1041 (progn (skip-chars-forward "0-9")
1042 (point))))))
1043 (search-forward ",")
1044 (if (looking-at "[0-9]")
1045 (setq startpos (string-to-int (buffer-substring
1046 (point)
1047 (progn (skip-chars-forward "0-9")
1048 (point)))))))
1049 ;; Leave point on the next line of the tags file.
1050 (forward-line 1)
1051 (cons tag-text (cons line startpos))))
1052
1053 ;; TAG-INFO is a cons (TEXT LINE . POSITION) where TEXT is the initial part
1054 ;; of a line containing the tag and POSITION is the character position of
1055 ;; TEXT within the file (starting from 1); LINE is the line number. If
1056 ;; TEXT is t, it means the tag refers to exactly LINE or POSITION
1057 ;; (whichever is present, LINE having preference, no searching. Either
1058 ;; LINE or POSITION may be nil; POSITION is used if present. If the tag
1059 ;; isn't exactly at the given position then look around that position using
1060 ;; a search window which expands until it hits the start of file.
1061 (defun etags-goto-tag-location (tag-info)
1062 (let ((startpos (cdr (cdr tag-info)))
1063 offset found pat)
1064 (if (eq (car tag-info) t)
1065 ;; Direct file tag.
1066 (cond (line (goto-line line))
1067 (position (goto-char position))
1068 (t (error "etags.el BUG: bogus direct file tag")))
1069 ;; This constant is 1/2 the initial search window.
1070 ;; There is no sense in making it too small,
1071 ;; since just going around the loop once probably
1072 ;; costs about as much as searching 2000 chars.
1073 (setq offset 1000
1074 found nil
1075 pat (concat (if (eq selective-display t)
1076 "\\(^\\|\^m\\)" "^")
1077 (regexp-quote (car tag-info))))
1078 ;; The character position in the tags table is 0-origin.
1079 ;; Convert it to a 1-origin Emacs character position.
1080 (if startpos (setq startpos (1+ startpos)))
1081 ;; If no char pos was given, try the given line number.
1082 (or startpos
1083 (if (car (cdr tag-info))
1084 (setq startpos (progn (goto-line (car (cdr tag-info)))
1085 (point)))))
1086 (or startpos
1087 (setq startpos (point-min)))
1088 ;; First see if the tag is right at the specified location.
1089 (goto-char startpos)
1090 (setq found (looking-at pat))
1091 (while (and (not found)
1092 (progn
1093 (goto-char (- startpos offset))
1094 (not (bobp))))
1095 (setq found
1096 (re-search-forward pat (+ startpos offset) t)
1097 offset (* 3 offset))) ; expand search window
1098 (or found
1099 (re-search-forward pat nil t)
1100 (error "Rerun etags: `%s' not found in %s"
1101 pat buffer-file-name)))
1102 ;; Position point at the right place
1103 ;; if the search string matched an extra Ctrl-m at the beginning.
1104 (and (eq selective-display t)
1105 (looking-at "\^m")
1106 (forward-char 1))
1107 (beginning-of-line)))
1108
1109 (defun etags-list-tags (file)
1110 (goto-char 1)
1111 (if (not (search-forward (concat "\f\n" file ",") nil t))
1112 nil
1113 (forward-line 1)
1114 (while (not (or (eobp) (looking-at "\f")))
1115 (let ((tag (buffer-substring (point)
1116 (progn (skip-chars-forward "^\177")
1117 (point)))))
1118 (princ (if (looking-at "[^\n]+\001")
1119 ;; There is an explicit tag name; use that.
1120 (buffer-substring (point)
1121 (progn (skip-chars-forward "^\001")
1122 (point)))
1123 tag)))
1124 (terpri)
1125 (forward-line 1))
1126 t))
1127
1128 (defun etags-tags-apropos (string)
1129 (goto-char 1)
1130 (while (re-search-forward string nil t)
1131 (beginning-of-line)
1132 (princ (buffer-substring (point)
1133 (progn (skip-chars-forward "^\177")
1134 (point))))
1135 (terpri)
1136 (forward-line 1)))
1137
1138 (defun etags-tags-table-files ()
1139 (let ((files nil)
1140 beg)
1141 (goto-char (point-min))
1142 (while (search-forward "\f\n" nil t)
1143 (setq beg (point))
1144 (end-of-line)
1145 (skip-chars-backward "^," beg)
1146 (or (looking-at "include$")
1147 (setq files (cons (buffer-substring beg (1- (point))) files))))
1148 (nreverse files)))
1149
1150 (defun etags-tags-included-tables ()
1151 (let ((files nil)
1152 beg)
1153 (goto-char (point-min))
1154 (while (search-forward "\f\n" nil t)
1155 (setq beg (point))
1156 (end-of-line)
1157 (skip-chars-backward "^," beg)
1158 (if (looking-at "include$")
1159 ;; Expand in the default-directory of the tags table buffer.
1160 (setq files (cons (expand-file-name (buffer-substring beg (1- (point))))
1161 files))))
1162 (nreverse files)))
1163 \f
1164 ;; Empty tags file support.
1165
1166 ;; Recognize an empty file and give it local values of the tags table format
1167 ;; variables which do nothing.
1168 (defun recognize-empty-tags-table ()
1169 (and (zerop (buffer-size))
1170 (mapcar (function (lambda (sym)
1171 (set (make-local-variable sym) 'ignore)))
1172 '(tags-table-files-function
1173 tags-completion-table-function
1174 find-tag-regexp-search-function
1175 find-tag-search-function
1176 tags-apropos-function
1177 tags-included-tables-function))
1178 (set (make-local-variable 'verify-tags-table-function)
1179 (function (lambda ()
1180 (zerop (buffer-size)))))))
1181 \f
1182 ;;; Match qualifier functions for tagnames.
1183 ;;; XXX these functions assume etags file format.
1184
1185 ;; This might be a neat idea, but it's too hairy at the moment.
1186 ;;(defmacro tags-with-syntax (&rest body)
1187 ;; (` (let ((current (current-buffer))
1188 ;; (otable (syntax-table))
1189 ;; (buffer (find-file-noselect (file-of-tag)))
1190 ;; table)
1191 ;; (unwind-protect
1192 ;; (progn
1193 ;; (set-buffer buffer)
1194 ;; (setq table (syntax-table))
1195 ;; (set-buffer current)
1196 ;; (set-syntax-table table)
1197 ;; (,@ body))
1198 ;; (set-syntax-table otable)))))
1199 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
1200
1201 ;; t if point is at a tag line that matches TAG exactly.
1202 ;; point should be just after a string that matches TAG.
1203 (defun tag-exact-match-p (tag)
1204 ;; The match is really exact if there is an explicit tag name.
1205 (or (and (eq (char-after (point)) ?\001)
1206 (eq (char-after (- (point) (length tag) 1)) ?\177))
1207 ;; We are not on the explicit tag name, but perhaps it follows.
1208 (looking-at (concat "[^\177\n]*\177" (regexp-quote tag) "\001"))))
1209
1210 ;; t if point is at a tag line that matches TAG as a symbol.
1211 ;; point should be just after a string that matches TAG.
1212 (defun tag-symbol-match-p (tag)
1213 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1214 (save-excursion
1215 (backward-char (1+ (length tag)))
1216 (and (looking-at "\\Sw") (looking-at "\\S_")))))
1217
1218 ;; t if point is at a tag line that matches TAG as a word.
1219 ;; point should be just after a string that matches TAG.
1220 (defun tag-word-match-p (tag)
1221 (and (looking-at "\\b.*\177")
1222 (save-excursion (backward-char (1+ (length tag)))
1223 (looking-at "\\b"))))
1224
1225 (defun tag-exact-file-name-match-p (tag)
1226 (and (looking-at ",")
1227 (save-excursion (backward-char (1+ (length tag)))
1228 (looking-at "\f\n"))))
1229
1230 ;; t if point is in a tag line with a tag containing TAG as a substring.
1231 (defun tag-any-match-p (tag)
1232 (looking-at ".*\177"))
1233
1234 ;; t if point is at a tag line that matches RE as a regexp.
1235 (defun tag-re-match-p (re)
1236 (save-excursion
1237 (beginning-of-line)
1238 (let ((bol (point)))
1239 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t)
1240 (re-search-backward re bol t)))))
1241 \f
1242 ;;;###autoload
1243 (defun next-file (&optional initialize novisit)
1244 "Select next file among files in current tags table.
1245
1246 A first argument of t (prefix arg, if interactive) initializes to the
1247 beginning of the list of files in the tags table. If the argument is
1248 neither nil nor t, it is evalled to initialize the list of files.
1249
1250 Non-nil second argument NOVISIT means use a temporary buffer
1251 to save time and avoid uninteresting warnings.
1252
1253 Value is nil if the file was already visited;
1254 if the file was newly read in, the value is the filename."
1255 ;; Make the interactive arg t if there was any prefix arg.
1256 (interactive (list (if current-prefix-arg t)))
1257 (cond ((not initialize)
1258 ;; Not the first run.
1259 )
1260 ((eq initialize t)
1261 ;; Initialize the list from the tags table.
1262 (save-excursion
1263 ;; Visit the tags table buffer to get its list of files.
1264 (visit-tags-table-buffer)
1265 ;; Copy the list so we can setcdr below, and expand the file
1266 ;; names while we are at it, in this buffer's default directory.
1267 (setq next-file-list (mapcar 'expand-file-name (tags-table-files)))
1268 ;; Iterate over all the tags table files, collecting
1269 ;; a complete list of referenced file names.
1270 (while (visit-tags-table-buffer t)
1271 ;; Find the tail of the working list and chain on the new
1272 ;; sublist for this tags table.
1273 (let ((tail next-file-list))
1274 (while (cdr tail)
1275 (setq tail (cdr tail)))
1276 ;; Use a copy so the next loop iteration will not modify the
1277 ;; list later returned by (tags-table-files).
1278 (if tail
1279 (setcdr tail (mapcar 'expand-file-name (tags-table-files)))
1280 (setq next-file-list (mapcar 'expand-file-name
1281 (tags-table-files))))))))
1282 (t
1283 ;; Initialize the list by evalling the argument.
1284 (setq next-file-list (eval initialize))))
1285 (if next-file-list
1286 ()
1287 (and novisit
1288 (get-buffer " *next-file*")
1289 (kill-buffer " *next-file*"))
1290 (error "All files processed."))
1291 (let* ((next (car next-file-list))
1292 (new (not (get-file-buffer next))))
1293 ;; Advance the list before trying to find the file.
1294 ;; If we get an error finding the file, don't get stuck on it.
1295 (setq next-file-list (cdr next-file-list))
1296 (if (not (and new novisit))
1297 (set-buffer (find-file-noselect next novisit))
1298 ;; Like find-file, but avoids random warning messages.
1299 (set-buffer (get-buffer-create " *next-file*"))
1300 (kill-all-local-variables)
1301 (erase-buffer)
1302 (setq new next)
1303 (insert-file-contents new nil))
1304 new))
1305
1306 (defvar tags-loop-operate nil
1307 "Form for `tags-loop-continue' to eval to change one file.")
1308
1309 (defvar tags-loop-scan
1310 '(error (substitute-command-keys
1311 "No \\[tags-search] or \\[tags-query-replace] in progress."))
1312 "Form for `tags-loop-continue' to eval to scan one file.
1313 If it returns non-nil, this file needs processing by evalling
1314 \`tags-loop-operate'. Otherwise, move on to the next file.")
1315
1316 ;;;###autoload
1317 (defun tags-loop-continue (&optional first-time)
1318 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1319 Used noninteractively with non-nil argument to begin such a command (the
1320 argument is passed to `next-file', which see).
1321
1322 Two variables control the processing we do on each file: the value of
1323 `tags-loop-scan' is a form to be executed on each file to see if it is
1324 interesting (it returns non-nil if so) and `tags-loop-operate' is a form to
1325 evaluate to operate on an interesting file. If the latter evaluates to
1326 nil, we exit; otherwise we scan the next file."
1327 (interactive)
1328 (let (new
1329 (messaged nil))
1330 (while
1331 (progn
1332 ;; Scan files quickly for the first or next interesting one.
1333 (while (or first-time
1334 (save-restriction
1335 (widen)
1336 (not (eval tags-loop-scan))))
1337 (setq new (next-file first-time t))
1338 ;; If NEW is non-nil, we got a temp buffer,
1339 ;; and NEW is the file name.
1340 (if (or messaged
1341 (and (not first-time)
1342 (> baud-rate search-slow-speed)
1343 (setq messaged t)))
1344 (message "Scanning file %s..." (or new buffer-file-name)))
1345 (setq first-time nil)
1346 (goto-char (point-min)))
1347
1348 ;; If we visited it in a temp buffer, visit it now for real.
1349 (if new
1350 (let ((pos (point)))
1351 (erase-buffer)
1352 (set-buffer (find-file-noselect new))
1353 (setq new nil) ;No longer in a temp buffer.
1354 (widen)
1355 (goto-char pos)))
1356
1357 (switch-to-buffer (current-buffer))
1358
1359 ;; Now operate on the file.
1360 ;; If value is non-nil, continue to scan the next file.
1361 (eval tags-loop-operate)))
1362 (and messaged
1363 (null tags-loop-operate)
1364 (message "Scanning file %s...found" buffer-file-name))))
1365 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
1366
1367 ;;;###autoload
1368 (defun tags-search (regexp &optional file-list-form)
1369 "Search through all files listed in tags table for match for REGEXP.
1370 Stops when a match is found.
1371 To continue searching for next match, use command \\[tags-loop-continue].
1372
1373 See documentation of variable `tags-file-name'."
1374 (interactive "sTags search (regexp): ")
1375 (if (and (equal regexp "")
1376 (eq (car tags-loop-scan) 're-search-forward)
1377 (null tags-loop-operate))
1378 ;; Continue last tags-search as if by M-,.
1379 (tags-loop-continue nil)
1380 (setq tags-loop-scan
1381 (list 're-search-forward (list 'quote regexp) nil t)
1382 tags-loop-operate nil)
1383 (tags-loop-continue (or file-list-form t))))
1384
1385 ;;;###autoload
1386 (defun tags-query-replace (from to &optional delimited file-list-form)
1387 "Query-replace-regexp FROM with TO through all files listed in tags table.
1388 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1389 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
1390 with the command \\[tags-loop-continue].
1391
1392 See documentation of variable `tags-file-name'."
1393 (interactive (query-replace-read-args "Tags query replace (regexp)" t))
1394 (setq tags-loop-scan (list 'prog1
1395 (list 'if (list 're-search-forward
1396 (list 'quote from) nil t)
1397 ;; When we find a match, move back
1398 ;; to the beginning of it so perform-replace
1399 ;; will see it.
1400 '(goto-char (match-beginning 0))))
1401 tags-loop-operate (list 'perform-replace
1402 (list 'quote from) (list 'quote to)
1403 t t (list 'quote delimited)))
1404 (tags-loop-continue (or file-list-form t)))
1405 \f
1406 (defun tags-complete-tags-table-file (string predicate what)
1407 (save-excursion
1408 ;; If we need to ask for the tag table, allow that.
1409 (let ((enable-recursive-minibuffers t))
1410 (visit-tags-table-buffer))
1411 (if (eq what t)
1412 (all-completions string (mapcar 'list (tags-table-files))
1413 predicate)
1414 (try-completion string (mapcar 'list (tags-table-files))
1415 predicate))))
1416
1417 ;;;###autoload
1418 (defun list-tags (file &optional next-match)
1419 "Display list of tags in file FILE.
1420 This searches only the first table in the list, and no included tables.
1421 FILE should be as it appeared in the `etags' command, usually without a
1422 directory specification."
1423 (interactive (list (completing-read "List tags in file: "
1424 'tags-complete-tags-table-file
1425 nil t nil)))
1426 (with-output-to-temp-buffer "*Tags List*"
1427 (princ "Tags in file ")
1428 (princ file)
1429 (terpri)
1430 (save-excursion
1431 (let ((first-time t)
1432 (gotany nil))
1433 (while (visit-tags-table-buffer (not first-time))
1434 (setq first-time nil)
1435 (if (funcall list-tags-function file)
1436 (setq gotany t)))
1437 (or gotany
1438 (error "File %s not in current tags tables" file))))))
1439
1440 ;;;###autoload
1441 (defun tags-apropos (regexp)
1442 "Display list of all tags in tags table REGEXP matches."
1443 (interactive "sTags apropos (regexp): ")
1444 (with-output-to-temp-buffer "*Tags List*"
1445 (princ "Tags matching regexp ")
1446 (prin1 regexp)
1447 (terpri)
1448 (save-excursion
1449 (let ((first-time t))
1450 (while (visit-tags-table-buffer (not first-time))
1451 (setq first-time nil)
1452 (funcall tags-apropos-function regexp))))))
1453 \f
1454 ;;; XXX Kludge interface.
1455
1456 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1457 ;;;###autoload
1458 (defun select-tags-table ()
1459 "Select a tags table file from a menu of those you have already used.
1460 The list of tags tables to select from is stored in `tags-table-set-list';
1461 see the doc of that variable if you want to add names to the list."
1462 (interactive)
1463 (pop-to-buffer "*Tags Table List*")
1464 (setq buffer-read-only nil)
1465 (erase-buffer)
1466 (let ((set-list tags-table-set-list)
1467 (desired-point nil))
1468 (if tags-table-list
1469 (progn
1470 (setq desired-point (point-marker))
1471 (princ tags-table-list (current-buffer))
1472 (insert "\C-m")
1473 (prin1 (car tags-table-list) (current-buffer)) ;invisible
1474 (insert "\n")))
1475 (while set-list
1476 (if (eq (car set-list) tags-table-list)
1477 ;; Already printed it.
1478 ()
1479 (princ (car set-list) (current-buffer))
1480 (insert "\C-m")
1481 (prin1 (car (car set-list)) (current-buffer)) ;invisible
1482 (insert "\n"))
1483 (setq set-list (cdr set-list)))
1484 (if tags-file-name
1485 (progn
1486 (or desired-point
1487 (setq desired-point (point-marker)))
1488 (insert tags-file-name "\C-m")
1489 (prin1 tags-file-name (current-buffer)) ;invisible
1490 (insert "\n")))
1491 (setq set-list (delete tags-file-name
1492 (apply 'nconc (cons (copy-sequence tags-table-list)
1493 (mapcar 'copy-sequence
1494 tags-table-set-list)))))
1495 (while set-list
1496 (insert (car set-list) "\C-m")
1497 (prin1 (car set-list) (current-buffer)) ;invisible
1498 (insert "\n")
1499 (setq set-list (delete (car set-list) set-list)))
1500 (goto-char 1)
1501 (insert-before-markers
1502 "Type `t' to select a tags table or set of tags tables:\n\n")
1503 (if desired-point
1504 (goto-char desired-point))
1505 (set-window-start (selected-window) 1 t))
1506 (set-buffer-modified-p nil)
1507 (select-tags-table-mode))
1508
1509 (defvar select-tags-table-mode-map)
1510 (let ((map (make-sparse-keymap)))
1511 (define-key map "t" 'select-tags-table-select)
1512 (define-key map " " 'next-line)
1513 (define-key map "\^?" 'previous-line)
1514 (define-key map "n" 'next-line)
1515 (define-key map "p" 'previous-line)
1516 (define-key map "q" 'select-tags-table-quit)
1517 (setq select-tags-table-mode-map map))
1518
1519 (defun select-tags-table-mode ()
1520 "Major mode for choosing a current tags table among those already loaded.
1521
1522 \\{select-tags-table-mode-map}"
1523 (interactive)
1524 (kill-all-local-variables)
1525 (setq buffer-read-only t
1526 major-mode 'select-tags-table-mode
1527 mode-name "Select Tags Table")
1528 (use-local-map select-tags-table-mode-map)
1529 (setq selective-display t
1530 selective-display-ellipses nil))
1531
1532 (defun select-tags-table-select ()
1533 "Select the tags table named on this line."
1534 (interactive)
1535 (search-forward "\C-m")
1536 (let ((name (read (current-buffer))))
1537 (visit-tags-table name)
1538 (select-tags-table-quit)
1539 (message "Tags table now %s" name)))
1540
1541 (defun select-tags-table-quit ()
1542 "Kill the buffer and delete the selected window."
1543 (interactive)
1544 (kill-buffer (current-buffer))
1545 (or (one-window-p)
1546 (delete-window)))
1547 \f
1548 ;;;###autoload
1549 (defun complete-tag ()
1550 "Perform tags completion on the text around point.
1551 Completes to the set of names listed in the current tags table.
1552 The string to complete is chosen in the same way as the default
1553 for \\[find-tag] (which see)."
1554 (interactive)
1555 (or tags-table-list
1556 tags-file-name
1557 (error (substitute-command-keys
1558 "No tags table loaded. Try \\[visit-tags-table].")))
1559 (let ((pattern (funcall (or find-tag-default-function
1560 (get major-mode 'find-tag-default-function)
1561 'find-tag-default)))
1562 beg
1563 completion)
1564 (or pattern
1565 (error "Nothing to complete"))
1566 (search-backward pattern)
1567 (setq beg (point))
1568 (forward-char (length pattern))
1569 (setq completion (try-completion pattern 'tags-complete-tag nil))
1570 (cond ((eq completion t))
1571 ((null completion)
1572 (message "Can't find completion for \"%s\"" pattern)
1573 (ding))
1574 ((not (string= pattern completion))
1575 (delete-region beg (point))
1576 (insert completion))
1577 (t
1578 (message "Making completion list...")
1579 (with-output-to-temp-buffer "*Completions*"
1580 (display-completion-list
1581 (all-completions pattern 'tags-complete-tag nil)))
1582 (message "Making completion list...%s" "done")))))
1583
1584 ;;;###autoload (define-key esc-map "\t" 'complete-tag)
1585 \f
1586 (provide 'etags)
1587
1588 ;;; etags.el ends here