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