Commit | Line | Data |
---|---|---|
e5167999 ER |
1 | ;; etags.el --- etags facility for Emacs |
2 | ||
bf349f36 RM |
3 | ;; Copyright (C) 1985, 1986, 1988, 1989, 1992, 1993 |
4 | ;; Free Software Foundation, Inc. | |
ff1f0fa6 | 5 | |
3a801d0c ER |
6 | ;; Author: Roland McGrath <roland@gnu.ai.mit.edu> |
7 | ;; Keywords: tools | |
8 | ||
ff1f0fa6 JB |
9 | ;; This file is part of GNU Emacs. |
10 | ||
11 | ;; GNU Emacs is free software; you can redistribute it and/or modify | |
12 | ;; it under the terms of the GNU General Public License as published by | |
daa37602 | 13 | ;; the Free Software Foundation; either version 2, or (at your option) |
ff1f0fa6 JB |
14 | ;; any later version. |
15 | ||
16 | ;; GNU Emacs is distributed in the hope that it will be useful, | |
17 | ;; but WITHOUT ANY WARRANTY; without even the implied warranty of | |
18 | ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
19 | ;; GNU General Public License for more details. | |
20 | ||
21 | ;; You should have received a copy of the GNU General Public License | |
22 | ;; along with GNU Emacs; see the file COPYING. If not, write to | |
23 | ;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA. | |
24 | ||
e5167999 ER |
25 | ;;; Code: |
26 | ||
c086701a | 27 | ;;;###autoload |
b6176f64 RM |
28 | (defvar tags-file-name nil |
29 | "*File name of tags table. | |
9708f7fc | 30 | To switch to a new tags table, setting this variable is sufficient. |
b6176f64 | 31 | If you set this variable, do not also set `tags-table-list'. |
9708f7fc | 32 | Use the `etags' program to make a tags table file.") |
b6176f64 | 33 | ;; Make M-x set-variable tags-file-name like M-x visit-tags-table. |
9ef8b0d6 | 34 | ;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ") |
9708f7fc RM |
35 | |
36 | ;;;###autoload | |
b6176f64 | 37 | ;; Use `visit-tags-table-buffer' to cycle through tags tables in this list. |
9708f7fc | 38 | (defvar tags-table-list nil |
b6176f64 RM |
39 | "*List of file names of tags tables to search. |
40 | An element that is a directory means the file \"TAGS\" in that directory. | |
41 | To switch to a new list of tags tables, setting this variable is sufficient. | |
42 | If you set this variable, do not also set `tags-file-name'. | |
43 | Use the `etags' program to make a tags table file.") | |
9708f7fc RM |
44 | |
45 | (defvar tags-table-list-pointer nil | |
47f3c459 RM |
46 | "Pointer into `tags-table-list' where the current state of searching is. |
47 | Might instead point into a list of included tags tables. | |
48 | Use `visit-tags-table-buffer' to cycle through tags tables in this list.") | |
49 | ||
50 | (defvar tags-table-list-started-at nil | |
51 | "Pointer into `tags-table-list', where the current search started.") | |
9708f7fc RM |
52 | |
53 | (defvar tags-table-parent-pointer-list nil | |
47f3c459 RM |
54 | "Saved state of the tags table that included this one. |
55 | Each element is (POINTER . STARTED-AT), giving the values of | |
56 | `tags-table-list-pointer' and `tags-table-list-started-at' from | |
57 | before we moved into the current table.") | |
9708f7fc RM |
58 | |
59 | (defvar tags-table-set-list nil | |
60 | "List of sets of tags table which have been used together in the past. | |
61 | Each element is a list of strings which are file names.") | |
62 | ||
63 | ;;;###autoload | |
64 | (defvar find-tag-hook nil | |
65 | "*Hook to be run by \\[find-tag] after finding a tag. See `run-hooks'. | |
66 | The value in the buffer in which \\[find-tag] is done is used, | |
67 | not the value in the buffer \\[find-tag] goes to.") | |
68 | ||
69 | ;;;###autoload | |
70 | (defvar find-tag-default-function nil | |
47f3c459 RM |
71 | "*A function of no arguments used by \\[find-tag] to pick a default tag. |
72 | If nil, and the symbol that is the value of `major-mode' | |
9708f7fc RM |
73 | has a `find-tag-default-function' property (see `put'), that is used. |
74 | Otherwise, `find-tag-default' is used.") | |
75 | ||
76 | ;;;###autoload | |
77 | (defvar default-tags-table-function nil | |
78 | "*If non-nil, a function of no arguments to choose a default tags file | |
79 | for a particular buffer.") | |
80 | \f | |
81 | ;; Tags table state. | |
82 | ;; These variables are local in tags table buffers. | |
ff1f0fa6 | 83 | |
9708f7fc RM |
84 | (defvar tag-lines-already-matched nil |
85 | "List of positions of beginnings of lines within the tags table | |
86 | that are already matched.") | |
ff1f0fa6 | 87 | |
9708f7fc RM |
88 | (defvar tags-table-files nil |
89 | "List of file names covered by current tags table. | |
90 | nil means it has not yet been computed; use `tags-table-files' to do so.") | |
91 | ||
92 | (defvar tags-completion-table nil | |
93 | "Alist of tag names defined in current tags table.") | |
94 | ||
95 | (defvar tags-included-tables nil | |
96 | "List of tags tables included by the current tags table.") | |
97 | ||
98 | (defvar next-file-list nil | |
99 | "List of files for \\[next-file] to process.") | |
100 | \f | |
101 | ;; Hooks for file formats. | |
102 | ||
103 | (defvar tags-table-format-hooks '(etags-recognize-tags-table | |
9ef8b0d6 | 104 | recognize-empty-tags-table) |
9708f7fc RM |
105 | "List of functions to be called in a tags table buffer to identify |
106 | the type of tags table. The functions are called in order, with no arguments, | |
107 | until one returns non-nil. The function should make buffer-local bindings | |
108 | of the format-parsing tags function variables if successful.") | |
109 | ||
110 | (defvar file-of-tag-function nil | |
111 | "Function to do the work of `file-of-tag' (which see).") | |
112 | (defvar tags-table-files-function nil | |
113 | "Function to do the work of `tags-table-files' (which see).") | |
114 | (defvar tags-completion-table-function nil | |
115 | "Function to build the tags-completion-table.") | |
116 | (defvar snarf-tag-function nil | |
117 | "Function to get info about a matched tag for `goto-tag-location-function'.") | |
118 | (defvar goto-tag-location-function nil | |
119 | "Function of to go to the location in the buffer specified by a tag. | |
120 | One argument, the tag info returned by `snarf-tag-function'.") | |
121 | (defvar find-tag-regexp-search-function nil | |
122 | "Search function passed to `find-tag-in-order' for finding a regexp tag.") | |
123 | (defvar find-tag-regexp-tag-order nil | |
124 | "Tag order passed to `find-tag-in-order' for finding a regexp tag.") | |
125 | (defvar find-tag-regexp-next-line-after-failure-p nil | |
126 | "Flag passed to `find-tag-in-order' for finding a regexp tag.") | |
127 | (defvar find-tag-search-function nil | |
128 | "Search function passed to `find-tag-in-order' for finding a tag.") | |
129 | (defvar find-tag-tag-order nil | |
130 | "Tag order passed to `find-tag-in-order' for finding a tag.") | |
131 | (defvar find-tag-next-line-after-failure-p nil | |
132 | "Flag passed to `find-tag-in-order' for finding a tag.") | |
133 | (defvar list-tags-function nil | |
134 | "Function to do the work of `list-tags' (which see).") | |
135 | (defvar tags-apropos-function nil | |
136 | "Function to do the work of `tags-apropos' (which see).") | |
137 | (defvar tags-included-tables-function nil | |
138 | "Function to do the work of `tags-included-tables' (which see).") | |
139 | (defvar verify-tags-table-function nil | |
140 | "Function to return t iff the current buffer vontains a valid | |
141 | \(already initialized\) tags file.") | |
142 | \f | |
b6176f64 RM |
143 | ;; Initialize the tags table in the current buffer. |
144 | ;; Returns non-nil iff it is a valid tags table. On | |
145 | ;; non-nil return, the tags table state variable are | |
146 | ;; made buffer-local and initialized to nil. | |
9708f7fc | 147 | (defun initialize-new-tags-table () |
b6176f64 RM |
148 | (set (make-local-variable 'tag-lines-already-matched) nil) |
149 | (set (make-local-variable 'tags-table-files) nil) | |
150 | (set (make-local-variable 'tags-completion-table) nil) | |
151 | (set (make-local-variable 'tags-included-tables) nil) | |
9708f7fc RM |
152 | ;; Value is t if we have found a valid tags table buffer. |
153 | (let ((hooks tags-table-format-hooks)) | |
154 | (while (and hooks | |
155 | (not (funcall (car hooks)))) | |
156 | (setq hooks (cdr hooks))) | |
157 | hooks)) | |
ff1f0fa6 | 158 | |
c086701a | 159 | ;;;###autoload |
9708f7fc RM |
160 | (defun visit-tags-table (file &optional local) |
161 | "Tell tags commands to use tags table file FILE. | |
ff1f0fa6 | 162 | FILE should be the name of a file created with the `etags' program. |
9708f7fc RM |
163 | A directory name is ok too; it means file TAGS in that directory. |
164 | ||
165 | Normally \\[visit-tags-table] sets the global value of `tags-file-name'. | |
166 | With a prefix arg, set the buffer-local value instead. | |
167 | When you find a tag with \\[find-tag], the buffer it finds the tag | |
168 | in is given a local value of this variable which is the name of the tags | |
169 | file the tag was in." | |
ff1f0fa6 JB |
170 | (interactive (list (read-file-name "Visit tags table: (default TAGS) " |
171 | default-directory | |
9708f7fc RM |
172 | (expand-file-name "TAGS" |
173 | default-directory) | |
174 | t) | |
175 | current-prefix-arg)) | |
b6176f64 RM |
176 | ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will |
177 | ;; initialize a buffer for FILE and set tags-file-name to the | |
178 | ;; fully-expanded name. | |
9806213d RM |
179 | (let ((tags-file-name file)) |
180 | (save-excursion | |
47f3c459 | 181 | (or (visit-tags-table-buffer 'same) |
9806213d RM |
182 | (signal 'file-error (list "Visiting tags table" |
183 | "file does not exist" | |
184 | file))) | |
b6176f64 | 185 | ;; Set FILE to the expanded name. |
9806213d | 186 | (setq file tags-file-name))) |
9708f7fc | 187 | (if local |
b6176f64 | 188 | ;; Set the local value of tags-file-name. |
47f3c459 | 189 | (set (make-local-variable 'tags-file-name) file) |
b6176f64 | 190 | ;; Set the global value of tags-file-name. |
9806213d RM |
191 | (setq-default tags-file-name file))) |
192 | ||
193 | ;; Move tags-table-list-pointer along and set tags-file-name. | |
b6176f64 | 194 | ;; If NO-INCLUDES is non-nil, ignore included tags tables. |
9806213d | 195 | ;; Returns nil when out of tables. |
b6176f64 RM |
196 | (defun tags-next-table (&optional no-includes) |
197 | ;; Do we have any included tables? | |
198 | (if (and (not no-includes) | |
199 | (visit-tags-table-buffer 'same) | |
200 | (tags-included-tables)) | |
201 | ||
202 | ;; Move into the included tags tables. | |
203 | (setq tags-table-parent-pointer-list | |
204 | ;; Save the current state of what table we are in. | |
205 | (cons (cons tags-table-list-pointer tags-table-list-started-at) | |
206 | tags-table-parent-pointer-list) | |
207 | ;; Start the pointer in the list of included tables. | |
208 | tags-table-list-pointer tags-included-tables | |
209 | tags-table-list-started-at tags-included-tables) | |
210 | ||
211 | ;; No included tables. Go to the next table in the list. | |
212 | (setq tags-table-list-pointer | |
213 | (cdr tags-table-list-pointer)) | |
214 | (or tags-table-list-pointer | |
215 | ;; Wrap around. | |
216 | (setq tags-table-list-pointer tags-table-list)) | |
217 | ||
218 | (if (eq tags-table-list-pointer tags-table-list-started-at) | |
219 | ;; We have come full circle. No more tables. | |
220 | (if tags-table-parent-pointer-list | |
221 | ;; Pop back to the tags table which includes this one. | |
222 | (progn | |
223 | ;; Restore the state variables. | |
224 | (setq tags-table-list-pointer | |
225 | (car (car tags-table-parent-pointer-list)) | |
226 | tags-table-list-started-at | |
227 | (cdr (car tags-table-parent-pointer-list)) | |
228 | tags-table-parent-pointer-list | |
229 | (cdr tags-table-parent-pointer-list)) | |
230 | ;; Recurse to skip to the next table after the parent. | |
231 | (tags-next-table t)) | |
232 | ;; All out of tags tables. | |
233 | (setq tags-table-list-pointer nil)))) | |
234 | ||
235 | (and tags-table-list-pointer | |
236 | ;; Set tags-file-name to the fully-expanded name. | |
237 | (setq tags-file-name | |
238 | (tags-expand-table-name (car tags-table-list-pointer))))) | |
239 | ||
240 | ;; Expand tags table name FILE into a complete file name. | |
47f3c459 | 241 | (defun tags-expand-table-name (file) |
47f3c459 RM |
242 | (setq file (expand-file-name file)) |
243 | (if (file-directory-p file) | |
244 | (expand-file-name "TAGS" file) | |
245 | file)) | |
246 | ||
b6176f64 RM |
247 | ;; Return the cdr of LIST (default: tags-table-list) whose car |
248 | ;; is equal to FILE after tags-expand-table-name on both sides. | |
47f3c459 RM |
249 | (defun tags-table-list-member (file &optional list) |
250 | (or list | |
251 | (setq list tags-table-list)) | |
252 | (setq file (tags-expand-table-name file)) | |
253 | (while (and list | |
254 | (not (string= file (tags-expand-table-name (car list))))) | |
255 | (setq list (cdr list))) | |
256 | list) | |
257 | ||
258 | ;; Subroutine of visit-tags-table-buffer. Frobs its local vars. | |
b6176f64 RM |
259 | ;; Search TABLES for one that has tags for THIS-FILE. Recurses |
260 | ;; on included tables. Returns the tail of TABLES (or of an | |
261 | ;; inner included list) whose car is a table listing THIS-FILE. | |
47f3c459 RM |
262 | (defun tags-table-including (this-file tables &optional recursing) |
263 | (let ((found nil)) | |
b6176f64 | 264 | ;; Loop over TABLES, looking for one containing tags for THIS-FILE. |
47f3c459 RM |
265 | (while (and (not found) |
266 | tables) | |
267 | (let ((tags-file-name (tags-expand-table-name (car tables)))) | |
268 | (if (or (get-file-buffer tags-file-name) | |
b6176f64 | 269 | (file-exists-p tags-file-name)) ;XXX check all in core first. |
47f3c459 RM |
270 | (progn |
271 | ;; Select the tags table buffer and get the file list up to date. | |
272 | (visit-tags-table-buffer 'same) | |
273 | (or tags-table-files | |
274 | (setq tags-table-files | |
275 | (funcall tags-table-files-function))) | |
276 | ||
277 | (cond ((member this-file tags-table-files) | |
278 | ;; Found it. | |
279 | (setq found tables)) | |
280 | ||
281 | ((tags-included-tables) | |
b6176f64 | 282 | ;; This table has included tables. Check them. |
47f3c459 RM |
283 | (let ((old tags-table-parent-pointer-list)) |
284 | (unwind-protect | |
285 | (progn | |
286 | (or recursing | |
287 | ;; At top level (not in an included tags | |
288 | ;; table), set the list to nil so we can | |
289 | ;; collect just the elts from this run. | |
290 | (setq tags-table-parent-pointer-list nil)) | |
291 | (setq found | |
b6176f64 | 292 | ;; Recurse on the list of included tables. |
47f3c459 RM |
293 | (tags-table-including this-file |
294 | tags-included-tables | |
295 | t)) | |
296 | (if found | |
b6176f64 RM |
297 | ;; One of them lists THIS-FILE. |
298 | ;; Set the table list state variables to move | |
299 | ;; us inside the list of included tables. | |
300 | (setq tags-table-parent-pointer-list | |
301 | (cons | |
302 | (cons tags-table-list-pointer | |
303 | tags-table-list-started-at) | |
304 | tags-table-parent-pointer-list) | |
305 | tags-table-list-pointer found | |
306 | tags-table-list-started-at found | |
307 | ;; CONT is a local variable of | |
308 | ;; our caller, visit-tags-table-buffer. | |
309 | ;; Set it so we won't frob lists later. | |
310 | cont 'included))) | |
47f3c459 | 311 | (or recursing |
b6176f64 RM |
312 | ;; tags-table-parent-pointer-list now describes |
313 | ;; the path of included tables taken by recursive | |
314 | ;; invocations of this function. The recursive | |
315 | ;; calls have consed onto the front of the list, | |
316 | ;; so it is now outermost first. We want it | |
317 | ;; innermost first, so reverse it. Then append | |
318 | ;; the old list (from before we were called the | |
319 | ;; outermost time), to get the complete current | |
320 | ;; state of included tables. | |
47f3c459 RM |
321 | (setq tags-table-parent-pointer-list |
322 | (nconc (nreverse | |
323 | tags-table-parent-pointer-list) | |
324 | old)))))))))) | |
325 | (setq tables (cdr tables))) | |
326 | found)) | |
9708f7fc RM |
327 | |
328 | (defun visit-tags-table-buffer (&optional cont) | |
329 | "Select the buffer containing the current tags table. | |
330 | If optional arg is t, visit the next table in `tags-table-list'. | |
9708f7fc | 331 | If optional arg is the atom `same', don't look for a new table; |
b6176f64 | 332 | just select the buffer visiting `tags-file-name'. |
47f3c459 | 333 | If arg is nil or absent, choose a first buffer from information in |
b6176f64 | 334 | `tags-file-name', `tags-table-list', `tags-table-list-pointer'. |
9708f7fc | 335 | Returns t if it visits a tags table, or nil if there are no more in the list." |
b6176f64 RM |
336 | |
337 | ;; Set tags-file-name to the tags table file we want to visit. | |
338 | (cond ((eq cont 'same) | |
bf349f36 RM |
339 | ;; Use the ambient value of tags-file-name. |
340 | (or tags-file-name | |
341 | (error (substitute-command-keys | |
342 | (concat "No tags table in use! " | |
343 | "Use \\[visit-tags-table] to select one.")))) | |
344 | ;; Set CONT to nil so the code below will make sure tags-file-name | |
345 | ;; is in tags-table-list. | |
b6176f64 | 346 | (setq cont nil)) |
47f3c459 RM |
347 | |
348 | (cont | |
b6176f64 | 349 | ;; Find the next table. |
47f3c459 RM |
350 | (if (tags-next-table) |
351 | ;; Skip over nonexistent files. | |
352 | (while (and (let ((file (tags-expand-table-name tags-file-name))) | |
353 | (not (or (get-file-buffer file) | |
354 | (file-exists-p file)))) | |
355 | (tags-next-table))))) | |
356 | ||
357 | (t | |
b6176f64 | 358 | ;; Pick a table out of our hat. |
47f3c459 | 359 | (setq tags-file-name |
b6176f64 RM |
360 | (or |
361 | ;; First, try a local variable. | |
362 | (cdr (assq 'tags-file-name (buffer-local-variables))) | |
363 | ;; Second, try a user-specified function to guess. | |
364 | (and default-tags-table-function | |
365 | (funcall default-tags-table-function)) | |
366 | ;; Third, look for a tags table that contains | |
367 | ;; tags for the current buffer's file. | |
368 | ;; If one is found, the lists will be frobnicated, | |
369 | ;; and CONT will be set non-nil so we don't do it below. | |
370 | (car (save-excursion (tags-table-including buffer-file-name | |
371 | tags-table-list))) | |
372 | ;; Fourth, use the user variable tags-file-name, if it is not | |
373 | ;; already in tags-table-list. | |
4d997d08 RM |
374 | (and tags-file-name |
375 | (not (tags-table-list-member tags-file-name)) | |
b6176f64 RM |
376 | tags-file-name) |
377 | ;; Fifth, use the user variable giving the table list. | |
21ccfb5c RM |
378 | ;; Find the first element of the list that actually exists. |
379 | (let ((list tags-table-list) | |
380 | file) | |
381 | (while (and list | |
382 | (setq file (tags-expand-table-name (car list))) | |
383 | (not (get-file-buffer file)) | |
384 | (not (file-exists-p file))) | |
385 | (setq list (cdr list))) | |
386 | (car list)) | |
b6176f64 RM |
387 | ;; Finally, prompt the user for a file name. |
388 | (expand-file-name | |
389 | (read-file-name "Visit tags table: (default TAGS) " | |
390 | default-directory | |
391 | "TAGS" | |
392 | t)))))) | |
393 | ||
394 | ;; Expand the table name into a full file name. | |
47f3c459 RM |
395 | (setq tags-file-name (tags-expand-table-name tags-file-name)) |
396 | ||
614517af | 397 | (if (and (eq cont t) (null tags-table-list-pointer)) |
47f3c459 RM |
398 | ;; All out of tables. |
399 | nil | |
400 | ||
b6176f64 | 401 | ;; Verify that tags-file-name is a valid tags table. |
47f3c459 | 402 | (if (if (get-file-buffer tags-file-name) |
b6176f64 RM |
403 | ;; The file is already in a buffer. Check for the visited file |
404 | ;; having changed since we last used it. | |
47f3c459 RM |
405 | (let (win) |
406 | (set-buffer (get-file-buffer tags-file-name)) | |
407 | (setq win (or verify-tags-table-function | |
408 | (initialize-new-tags-table))) | |
409 | (if (or (verify-visited-file-modtime (current-buffer)) | |
410 | (not (yes-or-no-p | |
411 | "Tags file has changed, read new contents? "))) | |
412 | (and win (funcall verify-tags-table-function)) | |
413 | (revert-buffer t t) | |
414 | (initialize-new-tags-table))) | |
415 | (set-buffer (find-file-noselect tags-file-name)) | |
416 | (or (string= tags-file-name buffer-file-name) | |
417 | ;; find-file-noselect has changed the file name. | |
b6176f64 | 418 | ;; Propagate the change to tags-file-name and tags-table-list. |
2ae6a918 | 419 | (let ((tail (member tags-file-name tags-table-list))) |
47f3c459 RM |
420 | (if tail |
421 | (setcar tail buffer-file-name)) | |
422 | (setq tags-file-name buffer-file-name))) | |
423 | (initialize-new-tags-table)) | |
424 | ||
425 | ;; We have a valid tags table. | |
426 | (progn | |
427 | ;; Bury the tags table buffer so it | |
428 | ;; doesn't get in the user's way. | |
429 | (bury-buffer (current-buffer)) | |
9708f7fc | 430 | |
47f3c459 RM |
431 | (if cont |
432 | ;; No list frobbing required. | |
433 | nil | |
434 | ||
435 | ;; Look in the list for the table we chose. | |
436 | (let ((elt (tags-table-list-member tags-file-name))) | |
437 | (or elt | |
438 | ;; The table is not in the current set. | |
439 | ;; Try to find it in another previously used set. | |
440 | (let ((sets tags-table-set-list)) | |
441 | (while (and sets | |
442 | (not (setq elt (tags-table-list-member | |
443 | tags-file-name (car sets))))) | |
444 | (setq sets (cdr sets))) | |
445 | (if sets | |
b6176f64 | 446 | ;; Found in some other set. Switch to that set. |
47f3c459 | 447 | (progn |
9806213d | 448 | (or (memq tags-table-list tags-table-set-list) |
47f3c459 | 449 | ;; Save the current list. |
9806213d RM |
450 | (setq tags-table-set-list |
451 | (cons tags-table-list | |
452 | tags-table-set-list))) | |
47f3c459 RM |
453 | (setq tags-table-list (car sets))) |
454 | ||
455 | ;; Not found in any existing set. | |
456 | (if (and tags-table-list | |
457 | (y-or-n-p (concat "Add " tags-file-name | |
458 | " to current list" | |
459 | " of tags tables? "))) | |
460 | ;; Add it to the current list. | |
461 | (setq tags-table-list (cons tags-file-name | |
462 | tags-table-list)) | |
463 | ;; Make a fresh list, and store the old one. | |
464 | (or (memq tags-table-list tags-table-set-list) | |
465 | (setq tags-table-set-list | |
466 | (cons tags-table-list tags-table-set-list))) | |
467 | (setq tags-table-list (list tags-file-name))) | |
468 | (setq elt tags-table-list)))) | |
469 | ||
b6176f64 RM |
470 | ;; Set the tags table list state variables to point at the table |
471 | ;; we want to use first. | |
47f3c459 | 472 | (setq tags-table-list-started-at elt |
614517af RM |
473 | tags-table-list-pointer elt))) |
474 | ||
475 | ;; Return of t says the tags table is valid. | |
476 | t) | |
47f3c459 RM |
477 | |
478 | ;; The buffer was not valid. Don't use it again. | |
b6176f64 RM |
479 | (let ((file tags-file-name)) |
480 | (kill-local-variable 'tags-file-name) | |
481 | (if (eq file tags-file-name) | |
482 | (setq tags-file-name nil))) | |
47f3c459 | 483 | (error "File %s is not a valid tags table" buffer-file-name)))) |
a128c7a0 | 484 | \f |
ff1f0fa6 JB |
485 | (defun file-of-tag () |
486 | "Return the file name of the file whose tags point is within. | |
9708f7fc RM |
487 | Assumes the tags table is the current buffer. |
488 | File name returned is relative to tags table file's directory." | |
489 | (funcall file-of-tag-function)) | |
ff1f0fa6 | 490 | |
c086701a | 491 | ;;;###autoload |
9708f7fc RM |
492 | (defun tags-table-files () |
493 | "Return a list of files in the current tags table. | |
b6176f64 | 494 | Assumes the tags table is the current buffer. |
ff1f0fa6 | 495 | File names returned are absolute." |
a128c7a0 RM |
496 | (or tags-table-files |
497 | (setq tags-table-files | |
498 | (funcall tags-table-files-function)))) | |
9708f7fc RM |
499 | |
500 | (defun tags-included-tables () | |
b6176f64 RM |
501 | "Return a list of tags tables included by the current table. |
502 | Assumes the tags table is the current buffer." | |
9708f7fc RM |
503 | (or tags-included-tables |
504 | (setq tags-included-tables (funcall tags-included-tables-function)))) | |
505 | \f | |
506 | ;; Build tags-completion-table on demand. The single current tags table | |
507 | ;; and its included tags tables (and their included tables, etc.) have | |
508 | ;; their tags included in the completion table. | |
509 | (defun tags-completion-table () | |
510 | (or tags-completion-table | |
511 | (condition-case () | |
512 | (prog2 | |
513 | (message "Making tags completion table for %s..." buffer-file-name) | |
514 | (let ((included (tags-included-tables)) | |
515 | (table (funcall tags-completion-table-function))) | |
516 | (save-excursion | |
b6176f64 RM |
517 | ;; Iterate over the list of included tables, and combine each |
518 | ;; included table's completion obarray to the parent obarray. | |
9708f7fc | 519 | (while included |
b6176f64 | 520 | ;; Visit the buffer. |
9708f7fc | 521 | (let ((tags-file-name (car included))) |
47f3c459 | 522 | (visit-tags-table-buffer 'same)) |
b6176f64 | 523 | ;; Recurse in that buffer to compute its completion table. |
9708f7fc | 524 | (if (tags-completion-table) |
b6176f64 | 525 | ;; Combine the tables. |
9708f7fc RM |
526 | (mapatoms (function |
527 | (lambda (sym) | |
528 | (intern (symbol-name sym) table))) | |
529 | tags-completion-table)) | |
530 | (setq included (cdr included)))) | |
531 | (setq tags-completion-table table)) | |
532 | (message "Making tags completion table for %s...done" | |
533 | buffer-file-name)) | |
534 | (quit (message "Tags completion table construction aborted.") | |
535 | (setq tags-completion-table nil))))) | |
536 | ||
537 | ;; Completion function for tags. Does normal try-completion, | |
538 | ;; but builds tags-completion-table on demand. | |
539 | (defun tags-complete-tag (string predicate what) | |
540 | (save-excursion | |
541 | (visit-tags-table-buffer) | |
542 | (if (eq what t) | |
543 | (all-completions string (tags-completion-table) predicate) | |
544 | (try-completion string (tags-completion-table) predicate)))) | |
a128c7a0 | 545 | \f |
ff1f0fa6 JB |
546 | ;; Return a default tag to search for, based on the text at point. |
547 | (defun find-tag-default () | |
548 | (save-excursion | |
549 | (while (looking-at "\\sw\\|\\s_") | |
550 | (forward-char 1)) | |
9708f7fc RM |
551 | (if (or (re-search-backward "\\sw\\|\\s_" |
552 | (save-excursion (beginning-of-line) (point)) | |
553 | t) | |
554 | (re-search-forward "\\(\\sw\\|\\s_\\)+" | |
555 | (save-excursion (end-of-line) (point)) | |
556 | t)) | |
557 | (progn (goto-char (match-end 0)) | |
ff1f0fa6 JB |
558 | (buffer-substring (point) |
559 | (progn (forward-sexp -1) | |
560 | (while (looking-at "\\s'") | |
561 | (forward-char 1)) | |
562 | (point)))) | |
563 | nil))) | |
564 | ||
9708f7fc | 565 | ;; Read a tag name from the minibuffer with defaulting and completion. |
ff1f0fa6 | 566 | (defun find-tag-tag (string) |
9708f7fc RM |
567 | (let* ((default (funcall (or find-tag-default-function |
568 | (get major-mode 'find-tag-default-function) | |
569 | 'find-tag-default))) | |
570 | (spec (completing-read (if default | |
571 | (format "%s(default %s) " string default) | |
572 | string) | |
573 | 'tags-complete-tag))) | |
b6176f64 RM |
574 | (if (equal spec "") |
575 | (or default (error "There is no default tag")) | |
576 | spec))) | |
ff1f0fa6 | 577 | |
21800cb8 RM |
578 | (defvar last-tag nil |
579 | "Last tag found by \\[find-tag].") | |
580 | ||
c086701a | 581 | ;;;###autoload |
9708f7fc RM |
582 | (defun find-tag-noselect (tagname &optional next-p regexp-p) |
583 | "Find tag (in current tags table) whose name contains TAGNAME. | |
f90a6155 | 584 | Returns the buffer containing the tag's definition and moves its point there, |
9708f7fc RM |
585 | but does not select the buffer. |
586 | The default for TAGNAME is the expression in the buffer near point. | |
587 | ||
588 | If second arg NEXT-P is non-nil (interactively, with prefix arg), search | |
589 | for another tag that matches the last tagname or regexp used. When there | |
590 | are multiple matches for a tag, more exact matches are found first. | |
ff1f0fa6 | 591 | |
9708f7fc RM |
592 | If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp. |
593 | ||
594 | See documentation of variable `tags-file-name'." | |
ff1f0fa6 JB |
595 | (interactive (if current-prefix-arg |
596 | '(nil t) | |
b6176f64 RM |
597 | (list (find-tag-tag "Find tag: ")))) |
598 | ;; Save the current buffer's value of `find-tag-hook' before selecting the | |
599 | ;; tags table buffer. | |
9708f7fc | 600 | (let ((local-find-tag-hook find-tag-hook)) |
21800cb8 | 601 | (if next-p |
b6176f64 | 602 | ;; Find the same table we last used. |
ab13123a | 603 | (visit-tags-table-buffer 'same) |
b6176f64 RM |
604 | ;; Pick a table to use. |
605 | (visit-tags-table-buffer) | |
606 | ;; Record TAGNAME for a future call with NEXT-P non-nil. | |
607 | (setq last-tag tagname)) | |
21800cb8 | 608 | (prog1 |
b6176f64 | 609 | ;; find-tag-in-order does the real work. |
21800cb8 RM |
610 | (find-tag-in-order (if next-p last-tag tagname) |
611 | (if regexp-p | |
612 | find-tag-regexp-search-function | |
613 | find-tag-search-function) | |
614 | (if regexp-p | |
615 | find-tag-regexp-tag-order | |
616 | find-tag-tag-order) | |
617 | (if regexp-p | |
618 | find-tag-regexp-next-line-after-failure-p | |
619 | find-tag-next-line-after-failure-p) | |
620 | (if regexp-p "matching" "containing") | |
621 | (not next-p)) | |
622 | (run-hooks 'local-find-tag-hook)))) | |
ff1f0fa6 | 623 | |
c086701a | 624 | ;;;###autoload |
9708f7fc RM |
625 | (defun find-tag (tagname &optional next-p) |
626 | "Find tag (in current tags table) whose name contains TAGNAME. | |
627 | Select the buffer containing the tag's definition, and move point there. | |
628 | The default for TAGNAME is the expression in the buffer around or before point. | |
c086701a | 629 | |
9708f7fc RM |
630 | If second arg NEXT-P is non-nil (interactively, with prefix arg), search |
631 | for another tag that matches the last tagname used. When there are | |
632 | multiple matches, more exact matches are found first. | |
ff1f0fa6 | 633 | |
9708f7fc | 634 | See documentation of variable `tags-file-name'." |
ff1f0fa6 JB |
635 | (interactive (if current-prefix-arg |
636 | '(nil t) | |
b6176f64 | 637 | (list (find-tag-tag "Find tag: ")))) |
9708f7fc RM |
638 | (switch-to-buffer (find-tag-noselect tagname next-p))) |
639 | ;;;###autoload (define-key esc-map "." 'find-tag) | |
ff1f0fa6 | 640 | |
daa37602 | 641 | ;;;###autoload |
9708f7fc RM |
642 | (defun find-tag-other-window (tagname &optional next-p) |
643 | "Find tag (in current tags table) whose name contains TAGNAME. | |
644 | Select the buffer containing the tag's definition | |
645 | in another window, and move point there. | |
646 | The default for TAGNAME is the expression in the buffer around or before point. | |
647 | ||
648 | If second arg NEXT-P is non-nil (interactively, with prefix arg), search | |
649 | for another tag that matches the last tagname used. When there are | |
650 | multiple matches, more exact matches are found first. | |
651 | ||
652 | See documentation of variable `tags-file-name'." | |
653 | (interactive (if current-prefix-arg | |
654 | '(nil t) | |
b6176f64 RM |
655 | (list (find-tag-tag "Find tag other window: ")))) |
656 | ;; This hair is to deal with the case where the tag is found in the | |
657 | ;; selected window's buffer; without the hair, point is moved in both | |
658 | ;; windows. To prevent this, we save the selected window's point before | |
659 | ;; doing find-tag-noselect, and restore it after. | |
660 | (let* ((window-point (window-point (selected-window))) | |
661 | (tagbuf (find-tag-noselect tagname next-p))) | |
662 | (set-window-point (prog1 | |
663 | (selected-window) | |
664 | (switch-to-buffer-other-window tagbuf)) | |
665 | window-point))) | |
9708f7fc RM |
666 | ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window) |
667 | ||
29add8b9 | 668 | ;;;###autoload |
9708f7fc | 669 | (defun find-tag-other-frame (tagname &optional next-p) |
daa37602 JB |
670 | "Find tag (in current tag table) whose name contains TAGNAME. |
671 | Selects the buffer that the tag is contained in in another frame | |
672 | and puts point at its definition. | |
673 | If TAGNAME is a null string, the expression in the buffer | |
674 | around or before point is used as the tag name. | |
9708f7fc | 675 | If second arg NEXT-P is non-nil (interactively, with prefix arg), |
daa37602 JB |
676 | searches for the next tag in the tag table |
677 | that matches the tagname used in the previous find-tag. | |
678 | ||
9708f7fc | 679 | See documentation of variable `tags-file-name'." |
daa37602 JB |
680 | (interactive (if current-prefix-arg |
681 | '(nil t) | |
b6176f64 | 682 | (list (find-tag-tag "Find tag other window: ")))) |
9708f7fc RM |
683 | (let ((pop-up-frames t)) |
684 | (find-tag-other-window tagname next-p))) | |
685 | ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame) | |
686 | ||
daa37602 | 687 | ;;;###autoload |
9708f7fc RM |
688 | (defun find-tag-regexp (regexp &optional next-p other-window) |
689 | "Find tag (in current tags table) whose name matches REGEXP. | |
690 | Select the buffer containing the tag's definition and move point there. | |
daa37602 | 691 | |
9708f7fc RM |
692 | If second arg NEXT-P is non-nil (interactively, with prefix arg), search |
693 | for another tag that matches the last tagname used. | |
694 | ||
695 | If third arg OTHER-WINDOW is non-nil, select the buffer in another window. | |
696 | ||
697 | See documentation of variable `tags-file-name'." | |
698 | (interactive (if current-prefix-arg | |
699 | '(nil t) | |
b6176f64 | 700 | (list (read-string "Find tag regexp: ")))) |
9708f7fc RM |
701 | (funcall (if other-window 'switch-to-buffer-other-window 'switch-to-buffer) |
702 | (find-tag-noselect regexp next-p t))) | |
703 | \f | |
704 | ;; Internal tag finding function. | |
705 | ||
706 | ;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to | |
707 | ;; any member of the function list ORDER (third arg). If ORDER is nil, | |
708 | ;; use saved state to continue a previous search. | |
709 | ||
710 | ;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in | |
711 | ;; an error message. | |
712 | ||
713 | ;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match, | |
714 | ;; point should be moved to the next line. | |
715 | ||
716 | ;; Algorithm is as follows. For each qualifier-func in ORDER, go to | |
717 | ;; beginning of tags file, and perform inner loop: for each naive match for | |
718 | ;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using | |
719 | ;; qualifier-func. If it qualifies, go to the specified line in the | |
720 | ;; specified source file and return. Qualified matches are remembered to | |
721 | ;; avoid repetition. State is saved so that the loop can be continued. | |
722 | ||
f90a6155 JB |
723 | (defun find-tag-in-order (pattern |
724 | search-forward-func | |
725 | order | |
726 | next-line-after-failure-p | |
727 | matching | |
728 | first-search) | |
9708f7fc RM |
729 | (let (file ;name of file containing tag |
730 | tag-info ;where to find the tag in FILE | |
731 | tags-table-file ;name of tags file | |
732 | (first-table t) | |
733 | (tag-order order) | |
734 | goto-func | |
735 | ) | |
736 | (save-excursion | |
47f3c459 RM |
737 | (or first-search ;find-tag-noselect has already done it. |
738 | (visit-tags-table-buffer 'same)) | |
739 | ||
9708f7fc RM |
740 | ;; Get a qualified match. |
741 | (catch 'qualified-match-found | |
47f3c459 | 742 | |
b6176f64 | 743 | ;; Iterate over the list of tags tables. |
9708f7fc RM |
744 | (while (or first-table |
745 | (visit-tags-table-buffer t)) | |
746 | ||
747 | (if first-search | |
748 | (setq tag-lines-already-matched nil)) | |
749 | ||
6218e8c6 RM |
750 | (and first-search first-table |
751 | ;; Start at beginning of tags file. | |
752 | (goto-char (point-min))) | |
753 | (setq first-table nil) | |
9708f7fc RM |
754 | |
755 | (setq tags-table-file buffer-file-name) | |
b6176f64 | 756 | ;; Iterate over the list of ordering predicates. |
9708f7fc RM |
757 | (while order |
758 | (while (funcall search-forward-func pattern nil t) | |
759 | ;; Naive match found. Qualify the match. | |
760 | (and (funcall (car order) pattern) | |
761 | ;; Make sure it is not a previous qualified match. | |
762 | ;; Use of `memq' depends on numbers being eq. | |
763 | (not (memq (save-excursion (beginning-of-line) (point)) | |
764 | tag-lines-already-matched)) | |
765 | (throw 'qualified-match-found nil)) | |
766 | (if next-line-after-failure-p | |
767 | (forward-line 1))) | |
768 | ;; Try the next flavor of match. | |
769 | (setq order (cdr order)) | |
770 | (goto-char (point-min))) | |
771 | (setq order tag-order)) | |
772 | ;; We throw out on match, so only get here if there were no matches. | |
773 | (error "No %stags %s %s" (if first-search "" "more ") | |
774 | matching pattern)) | |
775 | ||
776 | ;; Found a tag; extract location info. | |
777 | (beginning-of-line) | |
778 | (setq tag-lines-already-matched (cons (point) | |
779 | tag-lines-already-matched)) | |
780 | ;; Expand the filename, using the tags table buffer's default-directory. | |
781 | (setq file (expand-file-name (file-of-tag)) | |
782 | tag-info (funcall snarf-tag-function)) | |
783 | ||
b6176f64 | 784 | ;; Get the local value in the tags table buffer before switching buffers. |
9708f7fc RM |
785 | (setq goto-func goto-tag-location-function) |
786 | ||
787 | ;; Find the right line in the specified file. | |
788 | (set-buffer (find-file-noselect file)) | |
789 | (widen) | |
790 | (push-mark) | |
791 | (funcall goto-func tag-info) | |
792 | ||
793 | ;; Give this buffer a local value of tags-file-name. | |
794 | ;; The next time visit-tags-table-buffer is called, | |
795 | ;; it will use the same tags table that found a match in this buffer. | |
796 | (make-local-variable 'tags-file-name) | |
797 | (setq tags-file-name tags-table-file) | |
798 | ||
799 | ;; Return the buffer where the tag was found. | |
800 | (current-buffer)))) | |
801 | \f | |
802 | ;; `etags' TAGS file format support. | |
803 | ||
b6176f64 RM |
804 | ;; If the current buffer is a valid etags TAGS file, give it local values of |
805 | ;; the tags table format variables, and return non-nil. | |
9708f7fc | 806 | (defun etags-recognize-tags-table () |
b6176f64 | 807 | (and (etags-verify-tags-table) |
9508896e JB |
808 | ;; It is annoying to flash messages on the screen briefly, |
809 | ;; and this message is not useful. -- rms | |
810 | ;; (message "%s is an `etags' TAGS file" buffer-file-name) | |
9708f7fc | 811 | (mapcar (function (lambda (elt) |
b6176f64 | 812 | (set (make-local-variable (car elt)) (cdr elt)))) |
9708f7fc RM |
813 | '((file-of-tag-function . etags-file-of-tag) |
814 | (tags-table-files-function . etags-tags-table-files) | |
815 | (tags-completion-table-function . etags-tags-completion-table) | |
816 | (snarf-tag-function . etags-snarf-tag) | |
817 | (goto-tag-location-function . etags-goto-tag-location) | |
818 | (find-tag-regexp-search-function . re-search-forward) | |
819 | (find-tag-regexp-tag-order . (tag-re-match-p)) | |
820 | (find-tag-regexp-next-line-after-failuire-p . t) | |
821 | (find-tag-search-function . search-forward) | |
822 | (find-tag-tag-order . (tag-exact-match-p tag-word-match-p | |
823 | tag-any-match-p)) | |
824 | (find-tag-next-line-after-failure-p . nil) | |
825 | (list-tags-function . etags-list-tags) | |
826 | (tags-apropos-function . etags-tags-apropos) | |
827 | (tags-included-tables-function . etags-tags-included-tables) | |
828 | (verify-tags-table-function . etags-verify-tags-table) | |
829 | )))) | |
830 | ||
b6176f64 | 831 | ;; Return non-nil iff the current buffer is a valid etags TAGS file. |
9708f7fc | 832 | (defun etags-verify-tags-table () |
e4fc4f58 RM |
833 | ;; Use eq instead of = in case char-after returns nil. |
834 | (eq (char-after 1) ?\f)) | |
9708f7fc RM |
835 | |
836 | (defun etags-file-of-tag () | |
837 | (save-excursion | |
838 | (search-backward "\f\n") | |
839 | (forward-char 2) | |
840 | (buffer-substring (point) | |
841 | (progn (skip-chars-forward "^,") (point))))) | |
842 | ||
843 | (defun etags-tags-completion-table () | |
844 | (let ((table (make-vector 511 0))) | |
845 | (save-excursion | |
846 | (goto-char (point-min)) | |
847 | (while (search-forward "\177" nil t) | |
848 | ;; Handle multiple \177's on a line. | |
849 | (save-excursion | |
850 | (skip-chars-backward "^-A-Za-z0-9_$\n") ;sym syntax? XXX | |
851 | (or (bolp) | |
852 | (intern (buffer-substring | |
853 | (point) | |
854 | (progn | |
855 | (skip-chars-backward "-A-Za-z0-9_$") | |
856 | ;; ??? New | |
857 | ;; `::' in the middle of a C++ tag. | |
858 | (and (= (preceding-char) ?:) | |
859 | (= (char-after (- (point) 2)) ?:) | |
860 | (progn | |
861 | (backward-char 2) | |
862 | (skip-chars-backward | |
863 | "-A-Za-z0-9_$"))) | |
864 | (point))) | |
865 | table))))) | |
866 | table)) | |
867 | ||
868 | (defun etags-snarf-tag () | |
869 | (let (tag-text startpos) | |
870 | (search-forward "\177") | |
871 | (setq tag-text (buffer-substring (1- (point)) | |
872 | (save-excursion (beginning-of-line) | |
873 | (point)))) | |
874 | (search-forward ",") | |
875 | (setq startpos (string-to-int (buffer-substring | |
876 | (point) | |
877 | (progn (skip-chars-forward "0-9") | |
878 | (point))))) | |
879 | ;; Leave point on the next line of the tags file. | |
880 | (forward-line 1) | |
881 | (cons tag-text startpos))) | |
882 | ||
883 | (defun etags-goto-tag-location (tag-info) | |
884 | (let ((startpos (cdr tag-info)) | |
885 | ;; This constant is 1/2 the initial search window. | |
886 | ;; There is no sense in making it too small, | |
887 | ;; since just going around the loop once probably | |
888 | ;; costs about as much as searching 2000 chars. | |
889 | (offset 1000) | |
890 | (found nil) | |
891 | (pat (concat "^" (regexp-quote (car tag-info))))) | |
892 | (or startpos | |
893 | (setq startpos (point-min))) | |
894 | (while (and (not found) | |
895 | (progn | |
896 | (goto-char (- startpos offset)) | |
897 | (not (bobp)))) | |
898 | (setq found | |
899 | (re-search-forward pat (+ startpos offset) t) | |
900 | offset (* 3 offset))) ; expand search window | |
901 | (or found | |
902 | (re-search-forward pat nil t) | |
903 | (error "`%s' not found in %s; time to rerun etags" | |
904 | pat buffer-file-name))) | |
905 | (beginning-of-line)) | |
906 | ||
907 | (defun etags-list-tags (file) | |
908 | (goto-char 1) | |
909 | (if (not (search-forward (concat "\f\n" file ",") nil t)) | |
910 | nil | |
911 | (forward-line 1) | |
912 | (while (not (or (eobp) (looking-at "\f"))) | |
913 | (princ (buffer-substring (point) | |
914 | (progn (skip-chars-forward "^\177") | |
915 | (point)))) | |
916 | (terpri) | |
917 | (forward-line 1)))) | |
918 | ||
919 | (defun etags-tags-apropos (string) | |
920 | (goto-char 1) | |
921 | (while (re-search-forward string nil t) | |
922 | (beginning-of-line) | |
923 | (princ (buffer-substring (point) | |
924 | (progn (skip-chars-forward "^\177") | |
925 | (point)))) | |
926 | (terpri) | |
927 | (forward-line 1))) | |
928 | ||
929 | (defun etags-tags-table-files () | |
930 | (let ((files nil) | |
931 | beg) | |
932 | (goto-char (point-min)) | |
933 | (while (search-forward "\f\n" nil t) | |
934 | (setq beg (point)) | |
935 | (skip-chars-forward "^,\n") | |
936 | (or (looking-at ",include$") | |
937 | ;; Expand in the default-directory of the tags table buffer. | |
938 | (setq files (cons (expand-file-name (buffer-substring beg (point))) | |
939 | files)))) | |
940 | (nreverse files))) | |
941 | ||
942 | (defun etags-tags-included-tables () | |
943 | (let ((files nil) | |
944 | beg) | |
945 | (goto-char (point-min)) | |
946 | (while (search-forward "\f\n" nil t) | |
947 | (setq beg (point)) | |
948 | (skip-chars-forward "^,\n") | |
949 | (if (looking-at ",include$") | |
950 | ;; Expand in the default-directory of the tags table buffer. | |
951 | (setq files (cons (expand-file-name (buffer-substring beg (point))) | |
952 | files)))) | |
953 | (nreverse files))) | |
954 | \f | |
955 | ;; Empty tags file support. | |
956 | ||
b6176f64 RM |
957 | ;; Recognize an empty file and give it local values of the tags table format |
958 | ;; variables which do nothing. | |
9708f7fc RM |
959 | (defun recognize-empty-tags-table () |
960 | (and (zerop (buffer-size)) | |
961 | (mapcar (function (lambda (sym) | |
b6176f64 | 962 | (set (make-local-variable sym) 'ignore))) |
9708f7fc RM |
963 | '(tags-table-files-function |
964 | tags-completion-table-function | |
965 | find-tag-regexp-search-function | |
966 | find-tag-search-function | |
967 | tags-apropos-function | |
968 | tags-included-tables-function)) | |
969 | (set (make-local-variable 'verify-tags-table-function) | |
970 | (function (lambda () | |
971 | (zerop (buffer-size))))))) | |
972 | \f | |
973 | ;;; Match qualifier functions for tagnames. | |
b6176f64 | 974 | ;;; XXX these functions assume etags file format. |
9708f7fc | 975 | |
6218e8c6 RM |
976 | ;; This might be a neat idea, but it's too hairy at the moment. |
977 | ;;(defmacro tags-with-syntax (&rest body) | |
978 | ;; (` (let ((current (current-buffer)) | |
979 | ;; (otable (syntax-table)) | |
980 | ;; (buffer (find-file-noselect (file-of-tag))) | |
981 | ;; table) | |
982 | ;; (unwind-protect | |
983 | ;; (progn | |
984 | ;; (set-buffer buffer) | |
985 | ;; (setq table (syntax-table)) | |
986 | ;; (set-buffer current) | |
987 | ;; (set-syntax-table table) | |
988 | ;; (,@ body)) | |
989 | ;; (set-syntax-table otable))))) | |
990 | ;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form)) | |
8a4c10dc | 991 | |
9708f7fc RM |
992 | ;; t if point is at a tag line that matches TAG "exactly". |
993 | ;; point should be just after a string that matches TAG. | |
6218e8c6 RM |
994 | (defun tag-exact-match-p (tag) |
995 | (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177") ;not a symbol char | |
996 | (save-excursion | |
997 | (backward-char (1+ (length tag))) | |
998 | (and (looking-at "\\Sw") (looking-at "\\S_"))))) | |
9708f7fc RM |
999 | |
1000 | ;; t if point is at a tag line that matches TAG as a word. | |
1001 | ;; point should be just after a string that matches TAG. | |
1002 | (defun tag-word-match-p (tag) | |
1003 | (and (looking-at "\\b.*\177") | |
1004 | (save-excursion (backward-char (1+ (length tag))) | |
1005 | (looking-at "\\b")))) | |
1006 | ||
1007 | ;; t if point is in a tag line with a tag containing TAG as a substring. | |
1008 | (defun tag-any-match-p (tag) | |
1009 | (looking-at ".*\177")) | |
ff1f0fa6 | 1010 | |
9708f7fc RM |
1011 | ;; t if point is at a tag line that matches RE as a regexp. |
1012 | (defun tag-re-match-p (re) | |
1013 | (save-excursion | |
1014 | (beginning-of-line) | |
1015 | (let ((bol (point))) | |
1016 | (and (search-forward "\177" (save-excursion (end-of-line) (point)) t) | |
1017 | (re-search-backward re bol t))))) | |
1018 | \f | |
c086701a | 1019 | ;;;###autoload |
9708f7fc RM |
1020 | (defun next-file (&optional initialize novisit) |
1021 | "Select next file among files in current tags table. | |
1022 | Non-nil first argument (prefix arg, if interactive) | |
1023 | initializes to the beginning of the list of files in the tags table. | |
1024 | ||
1025 | Non-nil second argument NOVISIT means use a temporary buffer | |
1026 | to save time and avoid uninteresting warnings. | |
1027 | ||
1028 | Value is nil if the file was already visited; | |
1029 | if the file was newly read in, the value is the filename." | |
ff1f0fa6 | 1030 | (interactive "P") |
9708f7fc RM |
1031 | (and initialize |
1032 | (save-excursion | |
b6176f64 | 1033 | ;; Visit the tags table buffer to get its list of files. |
47f3c459 | 1034 | (visit-tags-table-buffer) |
9708f7fc | 1035 | (setq next-file-list (tags-table-files)))) |
ff1f0fa6 | 1036 | (or next-file-list |
9708f7fc | 1037 | (save-excursion |
a128c7a0 | 1038 | ;; Get the files from the next tags table. |
974f8dd6 | 1039 | ;; When doing (visit-tags-table-buffer t), |
9708f7fc RM |
1040 | ;; the tags table buffer must be current. |
1041 | (if (and (visit-tags-table-buffer 'same) | |
1042 | (visit-tags-table-buffer t)) | |
1043 | (setq next-file-list (tags-table-files)) | |
1044 | (and novisit | |
1045 | (get-buffer " *next-file*") | |
1046 | (kill-buffer " *next-file*")) | |
1047 | (error "All files processed.")))) | |
1048 | (let ((new (not (get-file-buffer (car next-file-list))))) | |
1049 | (if (not (and new novisit)) | |
1050 | (set-buffer (find-file-noselect (car next-file-list) novisit)) | |
1051 | ;; Like find-file, but avoids random warning messages. | |
1052 | (set-buffer (get-buffer-create " *next-file*")) | |
1053 | (kill-all-local-variables) | |
1054 | (erase-buffer) | |
1055 | (setq new (car next-file-list)) | |
1056 | (insert-file-contents new nil)) | |
1057 | (setq next-file-list (cdr next-file-list)) | |
1058 | new)) | |
ff1f0fa6 | 1059 | |
9708f7fc RM |
1060 | (defvar tags-loop-operate nil |
1061 | "Form for `tags-loop-continue' to eval to change one file.") | |
1062 | ||
1063 | (defvar tags-loop-scan nil | |
1064 | "Form for `tags-loop-continue' to eval to scan one file. | |
1065 | If it returns non-nil, this file needs processing by evalling | |
1066 | \`tags-loop-operate'. Otherwise, move on to the next file.") | |
ff1f0fa6 | 1067 | |
c086701a | 1068 | ;;;###autoload |
ff1f0fa6 JB |
1069 | (defun tags-loop-continue (&optional first-time) |
1070 | "Continue last \\[tags-search] or \\[tags-query-replace] command. | |
9708f7fc RM |
1071 | Used noninteractively with non-nil argument to begin such a command. |
1072 | Two variables control the processing we do on each file: | |
1073 | the value of `tags-loop-scan' is a form to be executed on each file | |
1074 | to see if it is interesting (it returns non-nil if so) | |
1075 | and `tags-loop-operate' is a form to execute to operate on an interesting file | |
1076 | If the latter returns non-nil, we exit; otherwise we scan the next file." | |
ff1f0fa6 | 1077 | (interactive) |
9708f7fc RM |
1078 | (let (new |
1079 | (messaged nil)) | |
1080 | (while | |
1081 | (progn | |
1082 | ;; Scan files quickly for the first or next interesting one. | |
1083 | (while (or first-time | |
1084 | (save-restriction | |
1085 | (widen) | |
1086 | (not (eval tags-loop-scan)))) | |
1087 | (setq new (next-file first-time t)) | |
1088 | ;; If NEW is non-nil, we got a temp buffer, | |
1089 | ;; and NEW is the file name. | |
1090 | (if (or messaged | |
1091 | (and (not first-time) | |
1092 | (> baud-rate search-slow-speed) | |
1093 | (setq messaged t))) | |
1094 | (message "Scanning file %s..." (or new buffer-file-name))) | |
1095 | (setq first-time nil) | |
1096 | (goto-char (point-min))) | |
1097 | ||
1098 | ;; If we visited it in a temp buffer, visit it now for real. | |
1099 | (if new | |
1100 | (let ((pos (point))) | |
1101 | (erase-buffer) | |
1102 | (set-buffer (find-file-noselect new)) | |
1103 | (widen) | |
1104 | (goto-char pos))) | |
1105 | ||
1106 | (switch-to-buffer (current-buffer)) | |
1107 | ||
1108 | ;; Now operate on the file. | |
1109 | ;; If value is non-nil, continue to scan the next file. | |
1110 | (eval tags-loop-operate))) | |
1111 | (and messaged | |
1112 | (null tags-loop-operate) | |
1113 | (message "Scanning file %s...found" buffer-file-name)))) | |
9708f7fc | 1114 | ;;;###autoload (define-key esc-map "," 'tags-loop-continue) |
ff1f0fa6 | 1115 | |
c086701a | 1116 | ;;;###autoload |
ff1f0fa6 | 1117 | (defun tags-search (regexp) |
9708f7fc | 1118 | "Search through all files listed in tags table for match for REGEXP. |
ff1f0fa6 JB |
1119 | Stops when a match is found. |
1120 | To continue searching for next match, use command \\[tags-loop-continue]. | |
1121 | ||
9708f7fc | 1122 | See documentation of variable `tags-file-name'." |
ff1f0fa6 JB |
1123 | (interactive "sTags search (regexp): ") |
1124 | (if (and (equal regexp "") | |
9708f7fc | 1125 | (eq (car tags-loop-scan) 're-search-forward) |
b6176f64 | 1126 | (null tags-loop-operate)) |
9708f7fc | 1127 | ;; Continue last tags-search as if by M-,. |
ff1f0fa6 | 1128 | (tags-loop-continue nil) |
9708f7fc RM |
1129 | (setq tags-loop-scan |
1130 | (list 're-search-forward regexp nil t) | |
1131 | tags-loop-operate nil) | |
ff1f0fa6 JB |
1132 | (tags-loop-continue t))) |
1133 | ||
c086701a | 1134 | ;;;###autoload |
ff1f0fa6 | 1135 | (defun tags-query-replace (from to &optional delimited) |
9708f7fc | 1136 | "Query-replace-regexp FROM with TO through all files listed in tags table. |
ff1f0fa6 | 1137 | Third arg DELIMITED (prefix arg) means replace only word-delimited matches. |
9708f7fc | 1138 | If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace |
ff1f0fa6 JB |
1139 | with the command \\[tags-loop-continue]. |
1140 | ||
9708f7fc RM |
1141 | See documentation of variable `tags-file-name'." |
1142 | (interactive | |
1143 | "sTags query replace (regexp): \nsTags query replace %s by: \nP") | |
29add8b9 RM |
1144 | (setq tags-loop-scan (list 'prog1 |
1145 | (list 'if (list 're-search-forward form nil t) | |
1146 | ;; When we find a match, move back | |
1147 | ;; to the beginning of it so perform-replace | |
1148 | ;; will see it. | |
1149 | '(goto-char (match-beginning 0)))) | |
9708f7fc | 1150 | tags-loop-operate (list 'perform-replace from to t t delimited)) |
ff1f0fa6 | 1151 | (tags-loop-continue t)) |
9708f7fc | 1152 | \f |
c086701a | 1153 | ;;;###autoload |
9708f7fc | 1154 | (defun list-tags (file) |
ff1f0fa6 | 1155 | "Display list of tags in file FILE. |
9708f7fc RM |
1156 | FILE should not contain a directory specification |
1157 | unless it has one in the tags table." | |
1158 | (interactive (list (completing-read "List tags in file: " nil | |
1159 | 'tags-table-files t nil))) | |
ff1f0fa6 JB |
1160 | (with-output-to-temp-buffer "*Tags List*" |
1161 | (princ "Tags in file ") | |
9708f7fc | 1162 | (princ file) |
ff1f0fa6 JB |
1163 | (terpri) |
1164 | (save-excursion | |
9708f7fc RM |
1165 | (let ((first-time t) |
1166 | (gotany nil)) | |
47f3c459 | 1167 | (while (visit-tags-table-buffer (not first-time)) |
9708f7fc RM |
1168 | (if (funcall list-tags-function file) |
1169 | (setq gotany t))) | |
1170 | (or gotany | |
1171 | (error "File %s not in current tags tables")))))) | |
ff1f0fa6 | 1172 | |
c086701a | 1173 | ;;;###autoload |
9708f7fc RM |
1174 | (defun tags-apropos (regexp) |
1175 | "Display list of all tags in tags table REGEXP matches." | |
1176 | (interactive "sTags apropos (regexp): ") | |
ff1f0fa6 JB |
1177 | (with-output-to-temp-buffer "*Tags List*" |
1178 | (princ "Tags matching regexp ") | |
9708f7fc | 1179 | (prin1 regexp) |
ff1f0fa6 JB |
1180 | (terpri) |
1181 | (save-excursion | |
47f3c459 RM |
1182 | (let ((first-time t)) |
1183 | (while (visit-tags-table-buffer (not first-time)) | |
1184 | (setq first-time nil) | |
a32a25f4 | 1185 | (funcall tags-apropos-function regexp)))))) |
9708f7fc RM |
1186 | \f |
1187 | ;;; XXX Kludge interface. | |
aa228418 | 1188 | |
9708f7fc | 1189 | ;; XXX If a file is in multiple tables, selection may get the wrong one. |
29add8b9 | 1190 | ;;;###autoload |
9708f7fc RM |
1191 | (defun select-tags-table () |
1192 | "Select a tags table file from a menu of those you have already used. | |
1193 | The list of tags tables to select from is stored in `tags-table-file-list'; | |
1194 | see the doc of that variable if you want to add names to the list." | |
1195 | (interactive) | |
1196 | (pop-to-buffer "*Tags Table List*") | |
1197 | (setq buffer-read-only nil) | |
1198 | (erase-buffer) | |
1199 | (setq selective-display t | |
1200 | selective-display-ellipses nil) | |
1201 | (let ((set-list tags-table-set-list) | |
1202 | (desired-point nil)) | |
1203 | (if tags-table-list | |
1204 | (progn | |
1205 | (setq desired-point (point-marker)) | |
1206 | (princ tags-table-list (current-buffer)) | |
1207 | (insert "\C-m") | |
1208 | (prin1 (car tags-table-list) (current-buffer)) ;invisible | |
1209 | (insert "\n"))) | |
1210 | (while set-list | |
1211 | (if (eq (car set-list) tags-table-list) | |
1212 | ;; Already printed it. | |
1213 | () | |
1214 | (princ (car set-list) (current-buffer)) | |
1215 | (insert "\C-m") | |
1216 | (prin1 (car (car set-list)) (current-buffer)) ;invisible | |
1217 | (insert "\n")) | |
1218 | (setq set-list (cdr set-list))) | |
1219 | (if tags-file-name | |
1220 | (progn | |
1221 | (or desired-point | |
1222 | (setq desired-point (point-marker))) | |
1223 | (insert tags-file-name "\C-m") | |
1224 | (prin1 tags-file-name (current-buffer)) ;invisible | |
1225 | (insert "\n"))) | |
1226 | (setq set-list (delete tags-file-name | |
1227 | (apply 'nconc (cons tags-table-list | |
1228 | (mapcar 'copy-sequence | |
1229 | tags-table-set-list))))) | |
1230 | (while set-list | |
1231 | (insert (car set-list) "\C-m") | |
1232 | (prin1 (car set-list) (current-buffer)) ;invisible | |
1233 | (insert "\n") | |
1234 | (setq set-list (delete (car set-list) set-list))) | |
1235 | (goto-char 1) | |
1236 | (insert-before-markers | |
1237 | "Type `t' to select a tags table or set of tags tables:\n\n") | |
1238 | (if desired-point | |
1239 | (goto-char desired-point)) | |
1240 | (set-window-start (selected-window) 1 t)) | |
1241 | (set-buffer-modified-p nil) | |
1242 | (setq buffer-read-only t | |
1243 | mode-name "Select Tags Table") | |
1244 | (let ((map (make-sparse-keymap))) | |
1245 | (define-key map "t" 'select-tags-table-select) | |
1246 | (define-key map " " 'next-line) | |
1247 | (define-key map "\^?" 'previous-line) | |
1248 | (define-key map "n" 'next-line) | |
1249 | (define-key map "p" 'previous-line) | |
1250 | (define-key map "q" 'select-tags-table-quit) | |
1251 | (use-local-map map))) | |
1252 | ||
1253 | (defun select-tags-table-select () | |
1254 | "Select the tags table named on this line." | |
1255 | (interactive) | |
1256 | (search-forward "\C-m") | |
1257 | (let ((name (read (current-buffer)))) | |
1258 | (visit-tags-table name) | |
1259 | (select-tags-table-quit) | |
1260 | (message "Tags table now %s" name))) | |
49116ac0 | 1261 | |
9708f7fc RM |
1262 | (defun select-tags-table-quit () |
1263 | "Kill the buffer and delete the selected window." | |
1264 | (interactive) | |
1265 | (kill-buffer (current-buffer)) | |
1266 | (or (one-window-p) | |
1267 | (delete-window))) | |
1268 | \f | |
1269 | ;;;###autoload | |
1270 | (defun complete-tag () | |
1271 | "Perform tags completion on the text around point. | |
1272 | Completes to the set of names listed in the current tags table. | |
1273 | The string to complete is chosen in the same way as the default | |
ab13123a | 1274 | for \\[find-tag] (which see)." |
9708f7fc | 1275 | (interactive) |
ab13123a RM |
1276 | (or tags-table-list |
1277 | tags-file-name | |
1278 | (error (substitute-command-keys | |
1279 | "No tags table loaded. Try \\[visit-tags-table]."))) | |
9708f7fc RM |
1280 | (let ((pattern (funcall (or find-tag-default-function |
1281 | (get major-mode 'find-tag-default-function) | |
1282 | 'find-tag-default))) | |
1283 | beg | |
1284 | completion) | |
1285 | (or pattern | |
1286 | (error "Nothing to complete")) | |
1287 | (search-backward pattern) | |
1288 | (setq beg (point)) | |
1289 | (forward-char (length pattern)) | |
1290 | (setq completion (try-completion pattern 'tags-complete-tag nil)) | |
1291 | (cond ((eq completion t)) | |
1292 | ((null completion) | |
1293 | (message "Can't find completion for \"%s\"" pattern) | |
1294 | (ding)) | |
1295 | ((not (string= pattern completion)) | |
1296 | (delete-region beg (point)) | |
1297 | (insert completion)) | |
1298 | (t | |
1299 | (message "Making completion list...") | |
1300 | (with-output-to-temp-buffer " *Completions*" | |
1301 | (display-completion-list | |
1302 | (all-completions pattern 'tags-complete-tag nil))) | |
1303 | (message "Making completion list...%s" "done"))))) | |
6218e8c6 RM |
1304 | |
1305 | ;;;###autoload (define-key esc-map "\t" 'complete-tag) | |
9708f7fc RM |
1306 | \f |
1307 | (provide 'etags) | |
e5167999 ER |
1308 | |
1309 | ;;; etags.el ends here |