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