(tcl-hilit): New function from "Chris Alfeld" <calfeld@math.utah.edu>
[bpt/emacs.git] / 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-file-order nil
140 "Function which checks for complete and correct match, for file name as tag.")
141 (defvar find-tag-regexp-search-function nil
142 "Search function passed to `find-tag-in-order' for finding a regexp tag.")
143 (defvar find-tag-regexp-tag-order nil
144 "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
145 (defvar find-tag-regexp-next-line-after-failure-p nil
146 "Flag passed to `find-tag-in-order' for finding a regexp tag.")
147 (defvar find-tag-search-function nil
148 "Search function passed to `find-tag-in-order' for finding a tag.")
149 (defvar find-tag-tag-order nil
150 "Tag order passed to `find-tag-in-order' for finding a tag.")
151 (defvar find-tag-next-line-after-failure-p nil
152 "Flag passed to `find-tag-in-order' for finding a tag.")
153 (defvar list-tags-function nil
154 "Function to do the work of `list-tags' (which see).")
155 (defvar tags-apropos-function nil
156 "Function to do the work of `tags-apropos' (which see).")
157 (defvar tags-included-tables-function nil
158 "Function to do the work of `tags-included-tables' (which see).")
159 (defvar verify-tags-table-function nil
160 "Function to return t iff the current buffer contains a valid
161 \(already initialized\) tags file.")
162 \f
163 ;; Initialize the tags table in the current buffer.
164 ;; Returns non-nil iff it is a valid tags table. On
165 ;; non-nil return, the tags table state variable are
166 ;; made buffer-local and initialized to nil.
167 (defun initialize-new-tags-table ()
168 (set (make-local-variable 'tag-lines-already-matched) nil)
169 (set (make-local-variable 'tags-table-files) nil)
170 (set (make-local-variable 'tags-completion-table) nil)
171 (set (make-local-variable 'tags-included-tables) nil)
172 ;; Value is t if we have found a valid tags table buffer.
173 (let ((hooks tags-table-format-hooks))
174 (while (and hooks
175 (not (funcall (car hooks))))
176 (setq hooks (cdr hooks)))
177 hooks))
178
179 ;;;###autoload
180 (defun visit-tags-table (file &optional local)
181 "Tell tags commands to use tags table file FILE.
182 FILE should be the name of a file created with the `etags' program.
183 A directory name is ok too; it means file TAGS in that directory.
184
185 Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
186 With a prefix arg, set the buffer-local value instead.
187 When you find a tag with \\[find-tag], the buffer it finds the tag
188 in is given a local value of this variable which is the name of the tags
189 file the tag was in."
190 (interactive (list (read-file-name "Visit tags table: (default TAGS) "
191 default-directory
192 (expand-file-name "TAGS"
193 default-directory)
194 t)
195 current-prefix-arg))
196 (or (stringp file) (signal 'wrong-type-argument (list 'stringp file)))
197 ;; Bind tags-file-name so we can control below whether the local or
198 ;; global value gets set. Calling visit-tags-table-buffer will
199 ;; initialize a buffer for the file and set tags-file-name to the
200 ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
201 ;; initialize a buffer for FILE and set tags-file-name to the
202 ;; fully-expanded name.
203 (let ((tags-file-name file))
204 (save-excursion
205 (or (visit-tags-table-buffer file)
206 (signal 'file-error (list "Visiting tags table"
207 "file does not exist"
208 file)))
209 ;; Set FILE to the expanded name.
210 (setq file tags-file-name)))
211 (if local
212 ;; Set the local value of tags-file-name.
213 (set (make-local-variable 'tags-file-name) file)
214 ;; Set the global value of tags-file-name.
215 (setq-default tags-file-name file)))
216
217 (defun tags-table-check-computed-list ()
218 "Compute `tags-table-computed-list' from `tags-table-list' if necessary."
219 (let ((expanded-list (mapcar 'tags-expand-table-name tags-table-list)))
220 (or (equal tags-table-computed-list-for expanded-list)
221 ;; The list (or default-directory) has changed since last computed.
222 (let* ((compute-for (mapcar 'copy-sequence expanded-list))
223 (tables (copy-sequence compute-for)) ;Mutated in the loop.
224 (computed nil)
225 table-buffer)
226
227 (while tables
228 (setq computed (cons (car tables) computed)
229 table-buffer (get-file-buffer (car tables)))
230 (if (and table-buffer
231 ;; There is a buffer visiting the file. Now make sure
232 ;; it is initialized as a tag table buffer.
233 (save-excursion
234 (tags-verify-table (buffer-file-name table-buffer))))
235 (save-excursion
236 (set-buffer table-buffer)
237 (if (tags-included-tables)
238 ;; Insert the included tables into the list we
239 ;; are processing.
240 (setcdr tables (append (tags-included-tables)
241 (cdr tables)))))
242 ;; This table is not in core yet. Insert a placeholder
243 ;; saying we must read it into core to check for included
244 ;; tables before searching the next table in the list.
245 (setq computed (cons t computed)))
246 (setq tables (cdr tables)))
247
248 ;; Record the tags-table-list value (and the context of the
249 ;; current directory) we computed from.
250 (setq tags-table-computed-list-for compute-for
251 tags-table-computed-list (nreverse computed))))))
252
253 ;; Extend `tags-table-computed-list' to remove the first `t' placeholder.
254 ;; An element of the list that is `t' is a placeholder indicating that the
255 ;; preceding element is a table that has not been read into core and might
256 ;; contain included tables to search. On return, the first placeholder
257 ;; element will be gone and the element before it read into core and its
258 ;; included tables inserted into the list.
259 (defun tags-table-extend-computed-list ()
260 (let ((list tags-table-computed-list))
261 (while (not (eq (nth 1 list) t))
262 (setq list (cdr list)))
263 (save-excursion
264 (if (tags-verify-table (car list))
265 ;; We are now in the buffer visiting (car LIST). Extract its
266 ;; list of included tables and insert it into the computed list.
267 (let ((tables (tags-included-tables))
268 (computed nil)
269 table-buffer)
270 (while tables
271 (setq computed (cons (car tables) computed)
272 table-buffer (get-file-buffer (car tables)))
273 (if table-buffer
274 (save-excursion
275 (set-buffer table-buffer)
276 (if (tags-included-tables)
277 ;; Insert the included tables into the list we
278 ;; are processing.
279 (setcdr tables (append (tags-included-tables)
280 tables))))
281 ;; This table is not in core yet. Insert a placeholder
282 ;; saying we must read it into core to check for included
283 ;; tables before searching the next table in the list.
284 (setq computed (cons t computed)))
285 (setq tables (cdr tables)))
286 (setq computed (nreverse computed))
287 ;; COMPUTED now contains the list of included tables (and
288 ;; tables included by them, etc.). Now splice this into the
289 ;; current list.
290 (setcdr list (nconc computed (cdr (cdr list)))))
291 ;; It was not a valid table, so just remove the following placeholder.
292 (setcdr list (cdr (cdr list)))))))
293
294 ;; Expand tags table name FILE into a complete file name.
295 (defun tags-expand-table-name (file)
296 (setq file (expand-file-name file))
297 (if (file-directory-p file)
298 (expand-file-name "TAGS" file)
299 file))
300
301 ;; Like member, but comparison is done after tags-expand-table-name on both
302 ;; sides and elements of LIST that are t are skipped.
303 (defun tags-table-list-member (file list)
304 (setq file (tags-expand-table-name file))
305 (while (and list
306 (or (eq (car list) t)
307 (not (string= file (tags-expand-table-name (car list))))))
308 (setq list (cdr list)))
309 list)
310
311 (defun tags-verify-table (file)
312 "Read FILE into a buffer and verify that it is a valid tags table.
313 Sets the current buffer to one visiting FILE (if it exists).
314 Returns non-nil iff it is a valid table."
315 (if (get-file-buffer file)
316 ;; The file is already in a buffer. Check for the visited file
317 ;; having changed since we last used it.
318 (let (win)
319 (set-buffer (get-file-buffer file))
320 (setq win (or verify-tags-table-function (initialize-new-tags-table)))
321 (if (or (verify-visited-file-modtime (current-buffer))
322 (not (yes-or-no-p
323 (format "Tags file %s has changed, read new contents? "
324 file))))
325 (and win (funcall verify-tags-table-function))
326 (revert-buffer t t)
327 (initialize-new-tags-table)))
328 (and (file-exists-p file)
329 (progn
330 (set-buffer (find-file-noselect file))
331 (or (string= file buffer-file-name)
332 ;; find-file-noselect has changed the file name.
333 ;; Propagate the change to tags-file-name and tags-table-list.
334 (let ((tail (member file tags-table-list)))
335 (if tail
336 (setcar tail buffer-file-name))
337 (if (eq file tags-file-name)
338 (setq tags-file-name buffer-file-name))))
339 (initialize-new-tags-table)))))
340
341 ;; Subroutine of visit-tags-table-buffer. Search the current tags tables
342 ;; for one that has tags for THIS-FILE (or that includes a table that
343 ;; does). Return the name of the first table table listing THIS-FILE; if
344 ;; the table is one included by another table, it is the master table that
345 ;; we return. If CORE-ONLY is non-nil, check only tags tables that are
346 ;; already in buffers--don't visit any new files.
347 (defun tags-table-including (this-file core-only)
348 (let ((tables tags-table-computed-list)
349 (found nil))
350 ;; Loop over the list, looking for a table containing tags for THIS-FILE.
351 (while (and (not found)
352 tables)
353
354 (if core-only
355 ;; Skip tables not in core.
356 (while (eq (nth 1 tables) t)
357 (setq tables (cdr (cdr tables))))
358 (if (eq (nth 1 tables) t)
359 ;; This table has not been read into core yet. Read it in now.
360 (tags-table-extend-computed-list)))
361
362 (if tables
363 ;; Select the tags table buffer and get the file list up to date.
364 (let ((tags-file-name (car tables)))
365 (visit-tags-table-buffer 'same)
366 (if (member this-file (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.
598 File names returned are absolute."
599 (or tags-table-files
600 (setq tags-table-files
601 (funcall tags-table-files-function))))
602
603 (defun tags-included-tables ()
604 "Return a list of tags tables included by the current table.
605 Assumes the tags table is the current buffer."
606 (or tags-included-tables
607 (setq tags-included-tables (funcall tags-included-tables-function))))
608 \f
609 ;; Build tags-completion-table on demand. The single current tags table
610 ;; and its included tags tables (and their included tables, etc.) have
611 ;; their tags included in the completion table.
612 (defun tags-completion-table ()
613 (or tags-completion-table
614 (condition-case ()
615 (prog2
616 (message "Making tags completion table for %s..." buffer-file-name)
617 (let ((included (tags-included-tables))
618 (table (funcall tags-completion-table-function)))
619 (save-excursion
620 ;; Iterate over the list of included tables, and combine each
621 ;; included table's completion obarray to the parent obarray.
622 (while included
623 ;; Visit the buffer.
624 (let ((tags-file-name (car included)))
625 (visit-tags-table-buffer 'same))
626 ;; Recurse in that buffer to compute its completion table.
627 (if (tags-completion-table)
628 ;; Combine the tables.
629 (mapatoms (function
630 (lambda (sym)
631 (intern (symbol-name sym) table)))
632 tags-completion-table))
633 (setq included (cdr included))))
634 (setq tags-completion-table table))
635 (message "Making tags completion table for %s...done"
636 buffer-file-name))
637 (quit (message "Tags completion table construction aborted.")
638 (setq tags-completion-table nil)))))
639
640 ;; Completion function for tags. Does normal try-completion,
641 ;; but builds tags-completion-table on demand.
642 (defun tags-complete-tag (string predicate what)
643 (save-excursion
644 ;; If we need to ask for the tag table, allow that.
645 (let ((enable-recursive-minibuffers t))
646 (visit-tags-table-buffer))
647 (if (eq what t)
648 (all-completions string (tags-completion-table) predicate)
649 (try-completion string (tags-completion-table) predicate))))
650 \f
651 ;; Return a default tag to search for, based on the text at point.
652 (defun find-tag-default ()
653 (save-excursion
654 (while (looking-at "\\sw\\|\\s_")
655 (forward-char 1))
656 (if (or (re-search-backward "\\sw\\|\\s_"
657 (save-excursion (beginning-of-line) (point))
658 t)
659 (re-search-forward "\\(\\sw\\|\\s_\\)+"
660 (save-excursion (end-of-line) (point))
661 t))
662 (progn (goto-char (match-end 0))
663 (buffer-substring (point)
664 (progn (forward-sexp -1)
665 (while (looking-at "\\s'")
666 (forward-char 1))
667 (point))))
668 nil)))
669
670 ;; Read a tag name from the minibuffer with defaulting and completion.
671 (defun find-tag-tag (string)
672 (let* ((default (funcall (or find-tag-default-function
673 (get major-mode 'find-tag-default-function)
674 'find-tag-default)))
675 (spec (completing-read (if default
676 (format "%s(default %s) " string default)
677 string)
678 'tags-complete-tag)))
679 (if (equal spec "")
680 (or default (error "There is no default tag"))
681 spec)))
682
683 (defvar last-tag nil
684 "Last tag found by \\[find-tag].")
685
686 ;; Get interactive args for find-tag{-noselect,-other-window,-regexp}.
687 (defun find-tag-interactive (prompt &optional no-default)
688 (if current-prefix-arg
689 (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
690 '-
691 t))
692 (list (if no-default
693 (read-string prompt)
694 (find-tag-tag prompt)))))
695
696 (defvar find-tag-history nil)
697
698 ;;;###autoload
699 (defun find-tag-noselect (tagname &optional next-p regexp-p)
700 "Find tag (in current tags table) whose name contains TAGNAME.
701 Returns the buffer containing the tag's definition and moves its point there,
702 but does not select the buffer.
703 The default for TAGNAME is the expression in the buffer near point.
704
705 If second arg NEXT-P is t (interactively, with prefix arg), search for
706 another tag that matches the last tagname or regexp used. When there are
707 multiple matches for a tag, more exact matches are found first. If NEXT-P
708 is the atom `-' (interactively, with prefix arg that is a negative number
709 or just \\[negative-argument]), pop back to the previous tag gone to.
710
711 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
712
713 See documentation of variable `tags-file-name'."
714 (interactive (find-tag-interactive "Find tag: "))
715
716 (setq find-tag-history (cons tagname find-tag-history))
717 ;; Save the current buffer's value of `find-tag-hook' before selecting the
718 ;; tags table buffer.
719 (let ((local-find-tag-hook find-tag-hook)
720 (search-tag))
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 (setq search-tag (if next-p last-tag tagname))
747 (set-buffer
748 ;; find-tag-in-order does the real work.
749 (find-tag-in-order
750 (if next-p last-tag tagname)
751 (if regexp-p
752 find-tag-regexp-search-function
753 find-tag-search-function)
754 (if regexp-p
755 find-tag-regexp-tag-order
756 (if (string-match "\\b.*\\.\\w*" search-tag)
757 find-tag-file-order
758 find-tag-tag-order))
759 (if regexp-p
760 find-tag-regexp-next-line-after-failure-p
761 find-tag-next-line-after-failure-p)
762 (if regexp-p "matching" "containing")
763 (not next-p)))
764 (set-marker marker (point))
765 (run-hooks 'local-find-tag-hook)
766 (setq tags-location-stack
767 (cons marker tags-location-stack))
768 (current-buffer))))))
769
770 ;;;###autoload
771 (defun find-tag (tagname &optional next-p regexp-p)
772 "Find tag (in current tags table) whose name contains TAGNAME.
773 Select the buffer containing the tag's definition, and move point there.
774 The default for TAGNAME is the expression in the buffer around or before point.
775
776 If second arg NEXT-P is t (interactively, with prefix arg), search for
777 another tag that matches the last tagname or regexp used. When there are
778 multiple matches for a tag, more exact matches are found first. If NEXT-P
779 is the atom `-' (interactively, with prefix arg that is a negative number
780 or just \\[negative-argument]), pop back to the previous tag gone to.
781
782 See documentation of variable `tags-file-name'."
783 (interactive (find-tag-interactive "Find tag: "))
784 (switch-to-buffer (find-tag-noselect tagname next-p regexp-p)))
785 ;;;###autoload (define-key esc-map "." 'find-tag)
786
787 ;;;###autoload
788 (defun find-tag-other-window (tagname &optional next-p regexp-p)
789 "Find tag (in current tags table) whose name contains TAGNAME.
790 Select the buffer containing the tag's definition in another window, and
791 move point there. The default for TAGNAME is the expression in the buffer
792 around or before point.
793
794 If second arg NEXT-P is t (interactively, with prefix arg), search for
795 another tag that matches the last tagname or regexp used. When there are
796 multiple matches for a tag, more exact matches are found first. If NEXT-P
797 is negative (interactively, with prefix arg that is a negative number or
798 just \\[negative-argument]), pop back to the previous tag gone to.
799
800 See documentation of variable `tags-file-name'."
801 (interactive (find-tag-interactive "Find tag other window: "))
802
803 ;; This hair is to deal with the case where the tag is found in the
804 ;; selected window's buffer; without the hair, point is moved in both
805 ;; windows. To prevent this, we save the selected window's point before
806 ;; doing find-tag-noselect, and restore it after.
807 (let* ((window-point (window-point (selected-window)))
808 (tagbuf (find-tag-noselect tagname next-p regexp-p))
809 (tagpoint (progn (set-buffer tagbuf) (point))))
810 (set-window-point (prog1
811 (selected-window)
812 (switch-to-buffer-other-window tagbuf)
813 ;; We have to set this new window's point; it
814 ;; might already have been displaying a
815 ;; different portion of tagbuf, in which case
816 ;; switch-to-buffer-other-window doesn't set
817 ;; the window's point from the buffer.
818 (set-window-point (selected-window) tagpoint))
819 window-point)))
820 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
821
822 ;;;###autoload
823 (defun find-tag-other-frame (tagname &optional next-p)
824 "Find tag (in current tags table) whose name contains TAGNAME.
825 Select the buffer containing the tag's definition in another frame, and
826 move point there. The default for TAGNAME is the expression in the buffer
827 around or before point.
828
829 If second arg NEXT-P is t (interactively, with prefix arg), search for
830 another tag that matches the last tagname or regexp used. When there are
831 multiple matches for a tag, more exact matches are found first. If NEXT-P
832 is negative (interactively, with prefix arg that is a negative number or
833 just \\[negative-argument]), pop back to the previous tag gone to.
834
835 See documentation of variable `tags-file-name'."
836 (interactive (find-tag-interactive "Find tag other frame: "))
837 (let ((pop-up-frames t))
838 (find-tag-other-window tagname next-p)))
839 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
840
841 ;;;###autoload
842 (defun find-tag-regexp (regexp &optional next-p other-window)
843 "Find tag (in current tags table) whose name matches REGEXP.
844 Select the buffer containing the tag's definition and move point there.
845
846 If second arg NEXT-P is t (interactively, with prefix arg), search for
847 another tag that matches the last tagname or regexp used. When there are
848 multiple matches for a tag, more exact matches are found first. If NEXT-P
849 is negative (interactively, with prefix arg that is a negative number or
850 just \\[negative-argument]), pop back to the previous tag gone to.
851
852 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
853
854 See documentation of variable `tags-file-name'."
855 (interactive (find-tag-interactive "Find tag regexp: " t))
856 ;; We go through find-tag-other-window to do all the display hair there.
857 (funcall (if other-window 'find-tag-other-window 'find-tag)
858 regexp next-p t))
859 ;;;###autoload (define-key esc-map [?\C-.] 'find-tag-regexp)
860 \f
861 ;; Internal tag finding function.
862
863 ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
864 ;; any member of the function list ORDER (third arg). If ORDER is nil,
865 ;; use saved state to continue a previous search.
866
867 ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
868 ;; an error message.
869
870 ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
871 ;; point should be moved to the next line.
872
873 ;; Algorithm is as follows. For each qualifier-func in ORDER, go to
874 ;; beginning of tags file, and perform inner loop: for each naive match for
875 ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
876 ;; qualifier-func. If it qualifies, go to the specified line in the
877 ;; specified source file and return. Qualified matches are remembered to
878 ;; avoid repetition. State is saved so that the loop can be continued.
879
880 (defun find-tag-in-order (pattern
881 search-forward-func
882 order
883 next-line-after-failure-p
884 matching
885 first-search)
886 (let (file ;name of file containing tag
887 tag-info ;where to find the tag in FILE
888 tags-table-file ;name of tags file
889 (first-table t)
890 (tag-order order)
891 goto-func
892 match-type
893 )
894 (save-excursion
895 (or first-search ;find-tag-noselect has already done it.
896 (visit-tags-table-buffer 'same))
897
898 ;; Get a qualified match.
899 (setq match-type
900 (catch 'qualified-match-found
901
902 ;; Iterate over the list of tags tables.
903 (while (or first-table
904 (visit-tags-table-buffer t))
905
906 (if first-search
907 (setq tag-lines-already-matched nil))
908
909 (and first-search first-table
910 ;; Start at beginning of tags file.
911 (goto-char (point-min)))
912 (or first-table
913 (goto-char (point-min)))
914
915 (setq first-table nil)
916
917 (setq tags-table-file buffer-file-name)
918 ;; Iterate over the list of ordering predicates.
919 (while order
920 (while (funcall search-forward-func pattern nil t)
921 ;; Naive match found. Qualify the match.
922 (and (funcall (car order) pattern)
923 ;; Make sure it is not a previous qualified match.
924 ;; Use of `memq' depends on numbers being eq.
925 (not (memq (save-excursion (beginning-of-line) (point))
926 tag-lines-already-matched))
927 (throw 'qualified-match-found nil))
928 (if next-line-after-failure-p
929 (forward-line 1)))
930 ;; Try the next flavor of match.
931 (setq order (cdr order))
932 (goto-char (point-min)))
933 (setq order tag-order))
934 ;; We throw out on match, so only get here if there were no matches.
935 (error "No %stags %s %s" (if first-search "" "more ")
936 matching pattern)))
937
938 ;; Found a tag; extract location info.
939 (beginning-of-line)
940 (setq tag-lines-already-matched (cons (point)
941 tag-lines-already-matched))
942 ;; Expand the filename, using the tags table buffer's default-directory.
943 (setq file (expand-file-name (file-of-tag))
944 tag-info (funcall snarf-tag-function))
945
946 ;; Get the local value in the tags table buffer before switching buffers.
947 (setq goto-func goto-tag-location-function)
948
949 ;; Find the right line in the specified file.
950 (set-buffer (find-file-noselect file))
951 (widen)
952 (push-mark)
953 (if (eq match-type 'tag-filename-match-p)
954 (goto-char (point-min))
955 (funcall goto-func tag-info))
956
957 ;; Return the buffer where the tag was found.
958 (current-buffer))))
959 \f
960 ;; `etags' TAGS file format support.
961
962 ;; If the current buffer is a valid etags TAGS file, give it local values of
963 ;; the tags table format variables, and return non-nil.
964 (defun etags-recognize-tags-table ()
965 (and (etags-verify-tags-table)
966 ;; It is annoying to flash messages on the screen briefly,
967 ;; and this message is not useful. -- rms
968 ;; (message "%s is an `etags' TAGS file" buffer-file-name)
969 (mapcar (function (lambda (elt)
970 (set (make-local-variable (car elt)) (cdr elt))))
971 '((file-of-tag-function . etags-file-of-tag)
972 (tags-table-files-function . etags-tags-table-files)
973 (tags-completion-table-function . etags-tags-completion-table)
974 (snarf-tag-function . etags-snarf-tag)
975 (goto-tag-location-function . etags-goto-tag-location)
976 (find-tag-regexp-search-function . re-search-forward)
977 (find-tag-regexp-tag-order . (tag-re-match-p))
978 (find-tag-regexp-next-line-after-failure-p . t)
979 (find-tag-search-function . search-forward)
980 (find-tag-tag-order . (tag-filename-match-p
981 tag-exact-match-p
982 tag-symbol-match-p
983 tag-word-match-p
984 tag-any-match-p))
985 (find-tag-file-order . (tag-filename-match-p))
986 (find-tag-next-line-after-failure-p . nil)
987 (list-tags-function . etags-list-tags)
988 (tags-apropos-function . etags-tags-apropos)
989 (tags-included-tables-function . etags-tags-included-tables)
990 (verify-tags-table-function . etags-verify-tags-table)
991 ))))
992
993 ;; Return non-nil iff the current buffer is a valid etags TAGS file.
994 (defun etags-verify-tags-table ()
995 ;; Use eq instead of = in case char-after returns nil.
996 (eq (char-after 1) ?\f))
997
998 (defun etags-file-of-tag ()
999 (save-excursion
1000 (if (looking-at "./")
1001 (re-search-forward "\\([^\n]+\\),[0-9]*\n")
1002 (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n"))
1003 (buffer-substring (match-beginning 1) (match-end 1))))
1004
1005
1006 (defun etags-tags-completion-table ()
1007 (let ((table (make-vector 511 0)))
1008 (save-excursion
1009 (goto-char (point-min))
1010 ;; This monster regexp matches an etags tag line.
1011 ;; \1 is the string to match;
1012 ;; \2 is not interesting;
1013 ;; \3 is the guessed tag name; XXX guess should be better eg DEFUN
1014 ;; \4 is not interesting;
1015 ;; \5 is the explicitly-specified tag name.
1016 ;; \6 is the line to start searching at;
1017 ;; \7 is the char to start searching at.
1018 (while (re-search-forward
1019 "^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
1020 \[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
1021 \\([0-9]+\\)?,\\([0-9]+\\)?\n"
1022 nil t)
1023 (intern (if (match-beginning 5)
1024 ;; There is an explicit tag name.
1025 (buffer-substring (match-beginning 5) (match-end 5))
1026 ;; No explicit tag name. Best guess.
1027 (buffer-substring (match-beginning 3) (match-end 3)))
1028 table)))
1029 table))
1030
1031 (defun etags-snarf-tag ()
1032 (let (tag-text line startpos)
1033 (search-forward "\177")
1034 (setq tag-text (buffer-substring (1- (point))
1035 (save-excursion (beginning-of-line)
1036 (point))))
1037 ;; Skip explicit tag name if present.
1038 (search-forward "\001" (save-excursion (forward-line 1) (point)) t)
1039 (if (looking-at "[0-9]")
1040 (setq line (string-to-int (buffer-substring
1041 (point)
1042 (progn (skip-chars-forward "0-9")
1043 (point))))))
1044 (search-forward ",")
1045 (if (looking-at "[0-9]")
1046 (setq startpos (string-to-int (buffer-substring
1047 (point)
1048 (progn (skip-chars-forward "0-9")
1049 (point))))))
1050 ;; Leave point on the next line of the tags file.
1051 (forward-line 1)
1052 (cons tag-text (cons line startpos))))
1053
1054 ;; TAG-INFO is a cons (TEXT LINE . POSITION) where TEXT is the initial part
1055 ;; of a line containing the tag and POSITION is the character position of
1056 ;; TEXT within the file (starting from 1); LINE is the line number. Either
1057 ;; LINE or POSITION may be nil; POSITION is used if present. If the tag
1058 ;; isn't exactly at the given position then look around that position using
1059 ;; a search window which expands until it hits the start of file.
1060 (defun etags-goto-tag-location (tag-info)
1061 (let ((startpos (cdr (cdr tag-info)))
1062 ;; This constant is 1/2 the initial search window.
1063 ;; There is no sense in making it too small,
1064 ;; since just going around the loop once probably
1065 ;; costs about as much as searching 2000 chars.
1066 (offset 1000)
1067 (found nil)
1068 (pat (concat (if (eq selective-display t)
1069 "\\(^\\|\^m\\)" "^")
1070 (regexp-quote (car tag-info)))))
1071 ;; The character position in the tags table is 0-origin.
1072 ;; Convert it to a 1-origin Emacs character position.
1073 (if startpos (setq startpos (1+ startpos)))
1074 ;; If no char pos was given, try the given line number.
1075 (or startpos
1076 (if (car (cdr tag-info))
1077 (setq startpos (progn (goto-line (car (cdr tag-info)))
1078 (point)))))
1079 (or startpos
1080 (setq startpos (point-min)))
1081 ;; First see if the tag is right at the specified location.
1082 (goto-char startpos)
1083 (setq found (looking-at pat))
1084 (while (and (not found)
1085 (progn
1086 (goto-char (- startpos offset))
1087 (not (bobp))))
1088 (setq found
1089 (re-search-forward pat (+ startpos offset) t)
1090 offset (* 3 offset))) ; expand search window
1091 (or found
1092 (re-search-forward pat nil t)
1093 (error "Rerun etags: `%s' not found in %s"
1094 pat buffer-file-name)))
1095 ;; Position point at the right place
1096 ;; if the search string matched an extra Ctrl-m at the beginning.
1097 (and (eq selective-display t)
1098 (looking-at "\^m")
1099 (forward-char 1))
1100 (beginning-of-line))
1101
1102 (defun etags-list-tags (file)
1103 (goto-char 1)
1104 (if (not (search-forward (concat "\f\n" file ",") nil t))
1105 nil
1106 (forward-line 1)
1107 (while (not (or (eobp) (looking-at "\f")))
1108 (let ((tag (buffer-substring (point)
1109 (progn (skip-chars-forward "^\177")
1110 (point)))))
1111 (princ (if (looking-at "[^\n]+\001")
1112 ;; There is an explicit tag name; use that.
1113 (buffer-substring (point)
1114 (progn (skip-chars-forward "^\001")
1115 (point)))
1116 tag)))
1117 (terpri)
1118 (forward-line 1))
1119 t))
1120
1121 (defun etags-tags-apropos (string)
1122 (goto-char 1)
1123 (while (re-search-forward string nil t)
1124 (beginning-of-line)
1125 (princ (buffer-substring (point)
1126 (progn (skip-chars-forward "^\177")
1127 (point))))
1128 (terpri)
1129 (forward-line 1)))
1130
1131 (defun etags-tags-table-files ()
1132 (let ((files nil)
1133 beg)
1134 (goto-char (point-min))
1135 (while (search-forward "\f\n" nil t)
1136 (setq beg (point))
1137 (end-of-line)
1138 (skip-chars-backward "^," beg)
1139 (or (looking-at "include$")
1140 ;; Expand in the default-directory of the tags table buffer.
1141 (setq files (cons (expand-file-name (buffer-substring beg (1- (point))))
1142 files))))
1143 (nreverse files)))
1144
1145 (defun etags-tags-included-tables ()
1146 (let ((files nil)
1147 beg)
1148 (goto-char (point-min))
1149 (while (search-forward "\f\n" nil t)
1150 (setq beg (point))
1151 (end-of-line)
1152 (skip-chars-backward "^," beg)
1153 (if (looking-at "include$")
1154 ;; Expand in the default-directory of the tags table buffer.
1155 (setq files (cons (expand-file-name (buffer-substring beg (1- (point))))
1156 files))))
1157 (nreverse files)))
1158 \f
1159 ;; Empty tags file support.
1160
1161 ;; Recognize an empty file and give it local values of the tags table format
1162 ;; variables which do nothing.
1163 (defun recognize-empty-tags-table ()
1164 (and (zerop (buffer-size))
1165 (mapcar (function (lambda (sym)
1166 (set (make-local-variable sym) 'ignore)))
1167 '(tags-table-files-function
1168 tags-completion-table-function
1169 find-tag-regexp-search-function
1170 find-tag-search-function
1171 tags-apropos-function
1172 tags-included-tables-function))
1173 (set (make-local-variable 'verify-tags-table-function)
1174 (function (lambda ()
1175 (zerop (buffer-size)))))))
1176 \f
1177 ;;; Match qualifier functions for tagnames.
1178 ;;; XXX these functions assume etags file format.
1179
1180 ;; This might be a neat idea, but it's too hairy at the moment.
1181 ;;(defmacro tags-with-syntax (&rest body)
1182 ;; (` (let ((current (current-buffer))
1183 ;; (otable (syntax-table))
1184 ;; (buffer (find-file-noselect (file-of-tag)))
1185 ;; table)
1186 ;; (unwind-protect
1187 ;; (progn
1188 ;; (set-buffer buffer)
1189 ;; (setq table (syntax-table))
1190 ;; (set-buffer current)
1191 ;; (set-syntax-table table)
1192 ;; (,@ body))
1193 ;; (set-syntax-table otable)))))
1194 ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
1195
1196 ;; t if point is at a tag line that matches TAG exactly.
1197 ;; point should be just after a string that matches TAG.
1198 (defun tag-exact-match-p (tag)
1199 ;; The match is really exact if there is an explicit tag name.
1200 (or (and (eq (char-after (point)) ?\001)
1201 (eq (char-after (- (point) (length tag) 1)) ?\177))
1202 ;; We are not on the explicit tag name, but perhaps it follows.
1203 (looking-at (concat "[^\177\n]*\177" (regexp-quote tag) "\001"))))
1204
1205 ;; t if point is at a tag line that matches TAG as a symbol.
1206 ;; point should be just after a string that matches TAG.
1207 (defun tag-symbol-match-p (tag)
1208 (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
1209 (save-excursion
1210 (backward-char (1+ (length tag)))
1211 (and (looking-at "\\Sw") (looking-at "\\S_")))))
1212
1213 ;; t if point is at a tag line that matches TAG as a word.
1214 ;; point should be just after a string that matches TAG.
1215 (defun tag-word-match-p (tag)
1216 (and (looking-at "\\b.*\177")
1217 (save-excursion (backward-char (1+ (length tag)))
1218 (looking-at "\\b"))))
1219
1220 (defun tag-filename-match-p (tag)
1221 (and (looking-at ",")
1222 (save-excursion (backward-char (1+ (length tag)))
1223 (looking-at "\\b"))))
1224
1225 ;; t if point is in a tag line with a tag containing TAG as a substring.
1226 (defun tag-any-match-p (tag)
1227 (looking-at ".*\177"))
1228
1229 ;; t if point is at a tag line that matches RE as a regexp.
1230 (defun tag-re-match-p (re)
1231 (save-excursion
1232 (beginning-of-line)
1233 (let ((bol (point)))
1234 (and (search-forward "\177" (save-excursion (end-of-line) (point)) t)
1235 (re-search-backward re bol t)))))
1236 \f
1237 ;;;###autoload
1238 (defun next-file (&optional initialize novisit)
1239 "Select next file among files in current tags table.
1240
1241 A first argument of t (prefix arg, if interactive) initializes to the
1242 beginning of the list of files in the tags table. If the argument is
1243 neither nil nor t, it is evalled to initialize the list of files.
1244
1245 Non-nil second argument NOVISIT means use a temporary buffer
1246 to save time and avoid uninteresting warnings.
1247
1248 Value is nil if the file was already visited;
1249 if the file was newly read in, the value is the filename."
1250 (interactive "P")
1251 (cond ((not initialize)
1252 ;; Not the first run.
1253 )
1254 ((eq initialize t)
1255 ;; Initialize the list from the tags table.
1256 (save-excursion
1257 ;; Visit the tags table buffer to get its list of files.
1258 (visit-tags-table-buffer)
1259 ;; Copy the list so we can setcdr below.
1260 (setq next-file-list (copy-sequence (tags-table-files)))
1261 ;; Iterate over all the tags table files, collecting
1262 ;; a complete list of referenced file names.
1263 (while (visit-tags-table-buffer t)
1264 ;; Find the tail of the working list and chain on the new
1265 ;; sublist for this tags table.
1266 (let ((tail next-file-list))
1267 (while (cdr tail)
1268 (setq tail (cdr tail)))
1269 ;; Use a copy so the next loop iteration will not modify the
1270 ;; list later returned by (tags-table-files).
1271 (if tail
1272 (setcdr tail (copy-sequence (tags-table-files)))
1273 (setq next-file-list (copy-sequence (tags-table-files))))))))
1274 (t
1275 ;; Initialize the list by evalling the argument.
1276 (setq next-file-list (eval initialize))))
1277 (if next-file-list
1278 ()
1279 (and novisit
1280 (get-buffer " *next-file*")
1281 (kill-buffer " *next-file*"))
1282 (error "All files processed."))
1283 (let ((new (not (get-file-buffer (car next-file-list)))))
1284 (if (not (and new novisit))
1285 (set-buffer (find-file-noselect (car next-file-list) novisit))
1286 ;; Like find-file, but avoids random warning messages.
1287 (set-buffer (get-buffer-create " *next-file*"))
1288 (kill-all-local-variables)
1289 (erase-buffer)
1290 (setq new (car next-file-list))
1291 (insert-file-contents new nil))
1292 (setq next-file-list (cdr next-file-list))
1293 new))
1294
1295 (defvar tags-loop-operate nil
1296 "Form for `tags-loop-continue' to eval to change one file.")
1297
1298 (defvar tags-loop-scan
1299 '(error (substitute-command-keys
1300 "No \\[tags-search] or \\[tags-query-replace] in progress."))
1301 "Form for `tags-loop-continue' to eval to scan one file.
1302 If it returns non-nil, this file needs processing by evalling
1303 \`tags-loop-operate'. Otherwise, move on to the next file.")
1304
1305 ;;;###autoload
1306 (defun tags-loop-continue (&optional first-time)
1307 "Continue last \\[tags-search] or \\[tags-query-replace] command.
1308 Used noninteractively with non-nil argument to begin such a command (the
1309 argument is passed to `next-file', which see).
1310
1311 Two variables control the processing we do on each file: the value of
1312 `tags-loop-scan' is a form to be executed on each file to see if it is
1313 interesting (it returns non-nil if so) and `tags-loop-operate' is a form to
1314 evaluate to operate on an interesting file. If the latter evaluates to
1315 nil, we exit; otherwise we scan the next file."
1316 (interactive)
1317 (let (new
1318 (messaged nil))
1319 (while
1320 (progn
1321 ;; Scan files quickly for the first or next interesting one.
1322 (while (or first-time
1323 (save-restriction
1324 (widen)
1325 (not (eval tags-loop-scan))))
1326 (setq new (next-file first-time t))
1327 ;; If NEW is non-nil, we got a temp buffer,
1328 ;; and NEW is the file name.
1329 (if (or messaged
1330 (and (not first-time)
1331 (> baud-rate search-slow-speed)
1332 (setq messaged t)))
1333 (message "Scanning file %s..." (or new buffer-file-name)))
1334 (setq first-time nil)
1335 (goto-char (point-min)))
1336
1337 ;; If we visited it in a temp buffer, visit it now for real.
1338 (if new
1339 (let ((pos (point)))
1340 (erase-buffer)
1341 (set-buffer (find-file-noselect new))
1342 (setq new nil) ;No longer in a temp buffer.
1343 (widen)
1344 (goto-char pos)))
1345
1346 (switch-to-buffer (current-buffer))
1347
1348 ;; Now operate on the file.
1349 ;; If value is non-nil, continue to scan the next file.
1350 (eval tags-loop-operate)))
1351 (and messaged
1352 (null tags-loop-operate)
1353 (message "Scanning file %s...found" buffer-file-name))))
1354 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
1355
1356 ;;;###autoload
1357 (defun tags-search (regexp &optional file-list-form)
1358 "Search through all files listed in tags table for match for REGEXP.
1359 Stops when a match is found.
1360 To continue searching for next match, use command \\[tags-loop-continue].
1361
1362 See documentation of variable `tags-file-name'."
1363 (interactive "sTags search (regexp): ")
1364 (if (and (equal regexp "")
1365 (eq (car tags-loop-scan) 're-search-forward)
1366 (null tags-loop-operate))
1367 ;; Continue last tags-search as if by M-,.
1368 (tags-loop-continue nil)
1369 (setq tags-loop-scan
1370 (list 're-search-forward (list 'quote regexp) nil t)
1371 tags-loop-operate nil)
1372 (tags-loop-continue (or file-list-form t))))
1373
1374 ;;;###autoload
1375 (defun tags-query-replace (from to &optional delimited file-list-form)
1376 "Query-replace-regexp FROM with TO through all files listed in tags table.
1377 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
1378 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
1379 with the command \\[tags-loop-continue].
1380
1381 See documentation of variable `tags-file-name'."
1382 (interactive (query-replace-read-args "Tags query replace (regexp)" t))
1383 (setq tags-loop-scan (list 'prog1
1384 (list 'if (list 're-search-forward
1385 (list 'quote from) nil t)
1386 ;; When we find a match, move back
1387 ;; to the beginning of it so perform-replace
1388 ;; will see it.
1389 '(goto-char (match-beginning 0))))
1390 tags-loop-operate (list 'perform-replace
1391 (list 'quote from) (list 'quote to)
1392 t t (list 'quote delimited)))
1393 (tags-loop-continue (or file-list-form t)))
1394 \f
1395 ;;;###autoload
1396 (defun list-tags (filename &optional next-match)
1397 "Gives the list of functions available in file \"filename\"
1398 Searches only in \"tags-file-name\"."
1399 (interactive "sFunctions in File: ")
1400 (let (file-list)
1401 (setq file-list (tags-locate-file-in-tags-table filename
1402 (if next-match next-match nil)))
1403 (if file-list
1404 (tags-list-functions-in-file (nth 1 (car file-list))
1405 (nth 2 (car file-list)))
1406 (message (format "%s not found in tags table" filename)))))
1407
1408 ;;;###autoload
1409 (defun tags-apropos (regexp)
1410 "Display list of all tags in tags table REGEXP matches."
1411 (interactive "sTags apropos (regexp): ")
1412 (with-output-to-temp-buffer "*Tags List*"
1413 (princ "Tags matching regexp ")
1414 (prin1 regexp)
1415 (terpri)
1416 (save-excursion
1417 (let ((first-time t))
1418 (while (visit-tags-table-buffer (not first-time))
1419 (setq first-time nil)
1420 (funcall tags-apropos-function regexp))))))
1421 \f
1422 ;;; XXX Kludge interface.
1423
1424 ;; XXX If a file is in multiple tables, selection may get the wrong one.
1425 ;;;###autoload
1426 (defun select-tags-table ()
1427 "Select a tags table file from a menu of those you have already used.
1428 The list of tags tables to select from is stored in `tags-table-set-list';
1429 see the doc of that variable if you want to add names to the list."
1430 (interactive)
1431 (pop-to-buffer "*Tags Table List*")
1432 (setq buffer-read-only nil)
1433 (erase-buffer)
1434 (let ((set-list tags-table-set-list)
1435 (desired-point nil))
1436 (if tags-table-list
1437 (progn
1438 (setq desired-point (point-marker))
1439 (princ tags-table-list (current-buffer))
1440 (insert "\C-m")
1441 (prin1 (car tags-table-list) (current-buffer)) ;invisible
1442 (insert "\n")))
1443 (while set-list
1444 (if (eq (car set-list) tags-table-list)
1445 ;; Already printed it.
1446 ()
1447 (princ (car set-list) (current-buffer))
1448 (insert "\C-m")
1449 (prin1 (car (car set-list)) (current-buffer)) ;invisible
1450 (insert "\n"))
1451 (setq set-list (cdr set-list)))
1452 (if tags-file-name
1453 (progn
1454 (or desired-point
1455 (setq desired-point (point-marker)))
1456 (insert tags-file-name "\C-m")
1457 (prin1 tags-file-name (current-buffer)) ;invisible
1458 (insert "\n")))
1459 (setq set-list (delete tags-file-name
1460 (apply 'nconc (cons tags-table-list
1461 (mapcar 'copy-sequence
1462 tags-table-set-list)))))
1463 (while set-list
1464 (insert (car set-list) "\C-m")
1465 (prin1 (car set-list) (current-buffer)) ;invisible
1466 (insert "\n")
1467 (setq set-list (delete (car set-list) set-list)))
1468 (goto-char 1)
1469 (insert-before-markers
1470 "Type `t' to select a tags table or set of tags tables:\n\n")
1471 (if desired-point
1472 (goto-char desired-point))
1473 (set-window-start (selected-window) 1 t))
1474 (set-buffer-modified-p nil)
1475 (select-tags-table-mode))
1476
1477 (defvar select-tags-table-mode-map)
1478 (let ((map (make-sparse-keymap)))
1479 (define-key map "t" 'select-tags-table-select)
1480 (define-key map " " 'next-line)
1481 (define-key map "\^?" 'previous-line)
1482 (define-key map "n" 'next-line)
1483 (define-key map "p" 'previous-line)
1484 (define-key map "q" 'select-tags-table-quit)
1485 (setq select-tags-table-mode-map map))
1486
1487 (defun select-tags-table-mode ()
1488 "Major mode for choosing a current tags table among those already loaded.
1489
1490 \\{select-tags-table-mode-map}"
1491 (interactive)
1492 (kill-all-local-variables)
1493 (setq buffer-read-only t
1494 major-mode 'select-tags-table-mode
1495 mode-name "Select Tags Table")
1496 (use-local-map select-tags-table-mode-map)
1497 (setq selective-display t
1498 selective-display-ellipses nil))
1499
1500 (defun select-tags-table-select ()
1501 "Select the tags table named on this line."
1502 (interactive)
1503 (search-forward "\C-m")
1504 (let ((name (read (current-buffer))))
1505 (visit-tags-table name)
1506 (select-tags-table-quit)
1507 (message "Tags table now %s" name)))
1508
1509 (defun select-tags-table-quit ()
1510 "Kill the buffer and delete the selected window."
1511 (interactive)
1512 (kill-buffer (current-buffer))
1513 (or (one-window-p)
1514 (delete-window)))
1515 \f
1516 ;;;###autoload
1517 (defun complete-tag ()
1518 "Perform tags completion on the text around point.
1519 Completes to the set of names listed in the current tags table.
1520 The string to complete is chosen in the same way as the default
1521 for \\[find-tag] (which see)."
1522 (interactive)
1523 (or tags-table-list
1524 tags-file-name
1525 (error (substitute-command-keys
1526 "No tags table loaded. Try \\[visit-tags-table].")))
1527 (let ((pattern (funcall (or find-tag-default-function
1528 (get major-mode 'find-tag-default-function)
1529 'find-tag-default)))
1530 beg
1531 completion)
1532 (or pattern
1533 (error "Nothing to complete"))
1534 (search-backward pattern)
1535 (setq beg (point))
1536 (forward-char (length pattern))
1537 (setq completion (try-completion pattern 'tags-complete-tag nil))
1538 (cond ((eq completion t))
1539 ((null completion)
1540 (message "Can't find completion for \"%s\"" pattern)
1541 (ding))
1542 ((not (string= pattern completion))
1543 (delete-region beg (point))
1544 (insert completion))
1545 (t
1546 (message "Making completion list...")
1547 (with-output-to-temp-buffer "*Completions*"
1548 (display-completion-list
1549 (all-completions pattern 'tags-complete-tag nil)))
1550 (message "Making completion list...%s" "done")))))
1551
1552 ;;;###autoload (define-key esc-map "\t" 'complete-tag)
1553 \f
1554 (defun tags-list-functions-in-file (pos tag-file)
1555 "Lists the functions for the given file. Backend for `list-tags'."
1556 (let ((tag-buf (find-file-noselect tag-file))
1557 (result-buf (get-buffer-create "*Tags Function List*"))
1558 function
1559 beg
1560 map)
1561 (save-excursion
1562 (set-buffer result-buf)
1563 (erase-buffer)
1564 (set-buffer tag-buf)
1565 (goto-char pos)
1566 (forward-line 1)
1567 (beginning-of-line)
1568 ; C-l marks end of information of a file in TAGS.
1569 (while (and (not (looking-at "^\C-l")) (not (eobp)))
1570 ; skip mere #defines, typedefs and struct definitions
1571 (if (not (or (looking-at "^#define\\s-+[a-zA-Z0-9_]+\\s-+")
1572 (looking-at "^typedef\\s-+")
1573 (looking-at "^\\s-*}")))
1574 (progn
1575 (setq beg (point))
1576 (skip-chars-forward "^\C-?(")
1577 (setq function (buffer-substring beg (point)))
1578 (save-excursion
1579 (set-buffer result-buf)
1580 (insert (concat function "\n")))))
1581 (forward-line 1)
1582 (beginning-of-line)))
1583 (switch-to-buffer "*Tags Function List*")
1584 (goto-char 1)
1585 (set-buffer-modified-p nil)
1586 (setq buffer-read-only t)))
1587
1588 (defun tags-locate-file-in-tags-table (filename first-search)
1589 "This function is used to locate `filename' in `tags-table-list'.
1590 Its internally used by the functions `find-file-from-tags' and
1591 `tags-list-tags-in-file'. If `first-search' is t, search continues from where
1592 it left off last time. Else, its a fresh search."
1593 (let (tag-list current-tags-buffer beg file found-file-list next-tag-file)
1594 (setq tag-list tags-table-list)
1595 (catch 'found-file
1596 (setq found-file-list nil
1597 next-tag-file nil)
1598 (while tag-list
1599 (setq current-tags-buffer (find-file-noselect (car tag-list)))
1600 (save-excursion
1601 (set-buffer current-tags-buffer)
1602 (if (or next-tag-file
1603 (not first-search))
1604 (goto-char (point-min)))
1605 (if (search-forward filename nil t)
1606 (if (tag-filename-match-p filename)
1607 (progn
1608 (beginning-of-line)
1609 (setq beg (point))
1610 (skip-chars-forward "^,")
1611 (or (looking-at ",include$")
1612 (setq file (expand-file-name (buffer-substring beg
1613 (point)))))
1614 (if (string-match filename (file-name-nondirectory file))
1615 (progn
1616 (setq found-file-list (cons (list file (point)
1617 (buffer-file-name))
1618 found-file-list))
1619 (throw 'found-file found-file-list))))))
1620 (setq tag-list (cdr tag-list))
1621 (setq next-tag-file 't)))
1622 (throw 'found-file found-file-list))))
1623
1624 (provide 'etags)
1625
1626 ;;; etags.el ends here