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