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