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