Mercurial > hg > xemacs-beta
annotate lisp/etags.el @ 5402:308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
author | Mats Lidell <matsl@xemacs.org> |
---|---|
date | Thu, 14 Oct 2010 17:15:20 +0200 |
parents | 0d43872986b6 |
children | ac37a5f7e5be |
rev | line source |
---|---|
428 | 1 ;;; etags.el --- etags facility for Emacs |
2 | |
1584 | 3 ;; Copyright 1985, 1986, 1988, 1990, 1997, 2003 Free Software Foundation, Inc. |
428 | 4 |
5 ;; Author: Their Name is Legion (see list below) | |
6 ;; Maintainer: XEmacs Development Team | |
7 ;; Keywords: tools | |
8 | |
9 ;; This file is part of XEmacs. | |
10 | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
11 ;; XEmacs is free software: you can redistribute it and/or modify it |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
12 ;; under the terms of the GNU General Public License as published by the |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
13 ;; Free Software Foundation, either version 3 of the License, or (at your |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
14 ;; option) any later version. |
428 | 15 |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
16 ;; XEmacs is distributed in the hope that it will be useful, but WITHOUT |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
17 ;; ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
18 ;; FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License |
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
19 ;; for more details. |
428 | 20 |
21 ;; You should have received a copy of the GNU General Public License | |
5402
308d34e9f07d
Changed bulk of GPLv2 or later files identified by script
Mats Lidell <matsl@xemacs.org>
parents:
5264
diff
changeset
|
22 ;; along with XEmacs. If not, see <http://www.gnu.org/licenses/>. |
428 | 23 |
24 ;;; Synched up with: Not synched with FSF. | |
25 | |
26 ;;; Commentary: | |
27 | |
28 ;; This file is completely different from FSF's etags.el. It appears | |
29 ;; that an early version of this file (tags.el) has been rewritten by | |
30 ;; two different people; we got one, FSF got the other. Various | |
31 ;; people have said that our version is better and faster. | |
32 | |
33 ;; TODO: | |
34 ;; - DOCUMENT! | |
35 | |
36 ;; Derived from the original lisp/tags.el. | |
37 | |
38 ;; Ideas and code from the work of the following people: | |
39 ;; Andy Norman <ange@hplb.hpl.hp.com>, author of ange-tags.el | |
40 ;; Ramana Rao <rao@arisia.xerox.com> | |
41 ;; John Sturdy <jcgs@harlqn.co.uk>, author of tags-helper.el | |
42 ;; Henry Kautz <kautz@allegra.att.com>, author of tag-completion.el | |
43 ;; Dan LaLiberte <liberte@cs.uiuc.edu>, author of local-tags.el | |
44 ;; Tom Dietterich <tgd@turing.cs.orst.edu>, author of quest.el | |
45 ;; The author(s) of lisp/simple.el | |
46 ;; Duke Briscoe <briscoe@cs.yale.edu> | |
47 ;; Lynn Slater <lrs@indetech.com>, author of location.el | |
48 ;; Shinichirou Sugou <shin@sgtp.apple.juice.or.jp> | |
49 ;; an unidentified anonymous elisp hacker | |
50 ;; Kyle Jones <kyle_jones@wonderworks.com> | |
51 ;; added "Exact match, then inexact" code | |
52 ;; added support for include directive. | |
53 ;; Hrvoje Niksic <hniksic@xemacs.org> | |
54 ;; various changes. | |
55 | |
56 | |
57 ;;; User variables. | |
58 | |
59 (defgroup etags nil | |
60 "Etags facility for Emacs. | |
61 Using etags, you can create tag tables for any number of files, and | |
62 easily access the symbols in those files, using the `\\[find-tag]' | |
63 command." | |
64 :prefix "tags-" | |
65 :group 'tools) | |
66 | |
67 | |
68 (defcustom tags-build-completion-table 'ask | |
69 "*If this variable is nil, then tags completion is disabled. | |
70 If it is t, then things which prompt for tags will do so with completion | |
71 across all known tags. | |
72 If it is the symbol `ask', you will be asked whether each tags table | |
73 should be added to the completion list as it is read in. (With the | |
74 exception that for very small tags tables, you will not be asked, | |
75 since they can be parsed quickly.)" | |
76 :type '(choice (const :tag "Disabled" nil) | |
77 (const :tag "Complete All" t) | |
78 (const :tag "Ask" ask)) | |
79 :group 'etags) | |
80 | |
81 (defcustom tags-always-exact nil | |
82 "*If this variable is non-nil, then tags always looks for exact matches. | |
83 If it is nil (the default), tags will first go through exact matches, | |
84 then through the non-exact ones." | |
85 :type 'boolean | |
86 :group 'etags) | |
87 | |
88 (defcustom tag-table-alist nil | |
89 "*A list which determines which tags files are active for a buffer. | |
90 This is not really an association list, in that all elements are | |
91 checked. The CAR of each element of this list is a pattern against | |
92 which the buffer's file name is compared; if it matches, then the CDR | |
93 of the list should be the name of the tags table to use. If more than | |
94 one element of this list matches the buffer's file name, then all of | |
95 the associated tags tables will be used. Earlier ones will be | |
96 searched first. | |
97 | |
98 If the CAR of elements of this list are strings, then they are treated | |
99 as regular-expressions against which the file is compared (like the | |
100 auto-mode-alist). If they are not strings, then they are evaluated. | |
101 If they evaluate to non-nil, then the current buffer is considered to | |
102 match. | |
103 | |
104 If the CDR of the elements of this list are strings, then they are | |
105 assumed to name a TAGS file. If they name a directory, then the string | |
106 \"TAGS\" is appended to them to get the file name. If they are not | |
107 strings, then they are evaluated, and must return an appropriate string. | |
108 | |
109 For example: | |
110 (setq tag-table-alist | |
111 '((\"/usr/src/public/perl/\" . \"/usr/src/public/perl/perl-3.0/\") | |
112 (\"\\\\.el$\" . \"/usr/local/emacs/src/\") | |
113 (\"/jbw/gnu/\" . \"/usr15/degree/stud/jbw/gnu/\") | |
114 (\"\" . \"/usr/local/emacs/src/\") | |
115 )) | |
116 | |
117 This means that anything in the /usr/src/public/perl/ directory should use | |
118 the TAGS file /usr/src/public/perl/perl-3.0/TAGS; and file ending in .el should | |
119 use the TAGS file /usr/local/emacs/src/TAGS; and anything in or below the | |
120 directory /jbw/gnu/ should use the TAGS file /usr15/degree/stud/jbw/gnu/TAGS. | |
121 A file called something like \"/usr/jbw/foo.el\" would use both the TAGS files | |
122 /usr/local/emacs/src/TAGS and /usr15/degree/stud/jbw/gnu/TAGS (in that order) | |
123 because it matches both patterns. | |
124 | |
125 If the buffer-local variable `buffer-tag-table' is set, then it names a tags | |
126 table that is searched before all others when find-tag is executed from this | |
127 buffer. | |
128 | |
129 If there is a file called \"TAGS\" in the same directory as the file in | |
130 question, then that tags file will always be used as well (after the | |
131 `buffer-tag-table' but before the tables specified by this list.) | |
132 | |
133 If the variable tags-file-name is set, then the tags file it names will apply | |
134 to all buffers (for backwards compatibility.) It is searched first." | |
135 :type '(repeat (cons :format "%v" | |
136 (choice :value "" | |
137 (regexp :tag "Buffer regexp") | |
138 sexp) | |
139 (choice :value "" | |
140 (string :tag "Tag file or directory") | |
141 sexp))) | |
142 :group 'etags) | |
143 | |
144 (defvar buffer-tag-table nil | |
145 "*The additional name of one TAGS table to be used for this buffer. | |
146 You can set this with `\\[set-buffer-tag-table]'. See the documentation | |
147 for the variable `tag-table-alist' for more information.") | |
148 (make-variable-buffer-local 'buffer-tag-table) | |
149 | |
150 (defvar tags-file-name nil | |
151 "The name of the tags-table used by all buffers. | |
152 This is for backwards compatibility, and is largely supplanted by the | |
153 variable tag-table-alist.") | |
154 | |
155 (defcustom tags-auto-read-changed-tag-files nil | |
156 "*If non-nil, always re-read changed TAGS file without prompting. | |
157 If nil, prompt whether to re-read the changed TAGS file." | |
158 :type 'boolean | |
159 :group 'etags) | |
160 | |
161 (defcustom make-tags-files-invisible nil | |
162 "*If non-nil, TAGS-files will not show up in buffer-lists or be | |
163 selectable (or deletable.)" | |
164 :type 'boolean | |
165 :group 'etags) | |
166 | |
167 (defcustom tags-search-nuke-uninteresting-buffers t | |
168 "*If non-nil, keep newly-visited files if they contain the search target. | |
169 This affects the `tags-search' and `tags-query-replace' commands." | |
170 :type 'boolean | |
171 :group 'etags) | |
172 | |
502 | 173 (defcustom tags-check-parent-directories-for-tag-files t |
174 "*If non-nil, look for TAGS files in all parent directories." | |
175 :type 'boolean | |
176 :group 'etags) | |
177 | |
2067 | 178 (defcustom tags-exuberant-ctags-optimization-p nil |
179 "*If this variable is nil (the default), then exact tag search is able | |
180 to find tag names in the name part of the tagtable (enclosed by ^?..^A) | |
181 and in the sourceline part of the tagtable ( enclosed by ^..^?). | |
182 This is needed by xemacs etags as not every tag has a name field. | |
183 It is slower for large tables and less precise than the other option. | |
184 | |
185 If it is non-nil, then exact tag will only search tag names in the name | |
186 part (enclosed by ^?..^A). This is faster and more precise than the other | |
187 option. This is only usable with exuberant etags, as it has a name field | |
188 entry for every tag." | |
189 :type 'boolean | |
190 :group 'etags) | |
428 | 191 |
192 ;; Buffer tag tables. | |
193 | |
194 (defun buffer-tag-table-list () | |
195 "Returns a list (ordered) of the tags tables which should be used for | |
196 the current buffer." | |
197 (let (result) | |
198 ;; Explicitly set buffer-tag-table | |
199 (when buffer-tag-table | |
200 (push buffer-tag-table result)) | |
201 ;; Current directory | |
202 (when (file-readable-p (concat default-directory "TAGS")) | |
203 (push (concat default-directory "TAGS") result)) | |
502 | 204 ;; Parent directories |
205 (when tags-check-parent-directories-for-tag-files | |
206 (let ((cur default-directory)) | |
593 | 207 ;; Fuck! Shouldn't there be a more obvious portable way |
208 ;; to determine if we're the root? Shouldn't we have a | |
209 ;; proper path manipulation API? Do you know how many | |
210 ;; god-damn bugs are lurking out there because of Unix/ | |
211 ;; Windows differences? And how much code is littered | |
212 ;; with stuff such as 10 lines down from here? | |
213 (while (not (and (equal (file-name-as-directory cur) cur) | |
214 (equal (directory-file-name cur) cur))) | |
215 (setq cur (expand-file-name ".." cur)) | |
502 | 216 (let ((parent-tag-file (expand-file-name "TAGS" cur))) |
217 (when (file-readable-p parent-tag-file) | |
218 (push parent-tag-file result)))))) | |
428 | 219 ;; tag-table-alist |
442 | 220 (let* ((key (or buffer-file-name |
221 (concat default-directory (buffer-name)))) | |
222 (key (if (eq system-type 'windows-nt) | |
223 (replace-in-string key "\\\\" "/") | |
224 key)) | |
225 expression) | |
428 | 226 (dolist (item tag-table-alist) |
227 (setq expression (car item)) | |
228 ;; If the car of the alist item is a string, apply it as a regexp | |
229 ;; to the buffer-file-name. Otherwise, evaluate it. If the | |
230 ;; regexp matches, or the expression evaluates non-nil, then this | |
231 ;; item in tag-table-alist applies to this buffer. | |
232 (when (if (stringp expression) | |
233 (string-match expression key) | |
234 (ignore-errors | |
235 (eval expression))) | |
236 ;; Now evaluate the cdr of the alist item to get the name of | |
237 ;; the tag table file. | |
238 (setq expression (ignore-errors | |
239 (eval (cdr item)))) | |
240 (if (stringp expression) | |
241 (push expression result) | |
242 (error "Expression in tag-table-alist evaluated to non-string"))))) | |
243 (setq result | |
244 (mapcar | |
245 (lambda (name) | |
246 (when (file-directory-p name) | |
247 (setq name (concat (file-name-as-directory name) "TAGS"))) | |
248 (and (file-readable-p name) | |
249 ;; get-tag-table-buffer has side-effects | |
250 (symbol-value-in-buffer 'buffer-file-name | |
251 (get-tag-table-buffer name)))) | |
252 result)) | |
253 (setq result (delq nil result)) | |
254 ;; If no TAGS file has been found, ask the user explicitly. | |
255 ;; #### tags-file-name is *evil*. | |
256 (or result tags-file-name | |
257 (call-interactively 'visit-tags-table)) | |
258 (when tags-file-name | |
259 (setq result (nconc result (list tags-file-name)))) | |
260 (or result (error "Buffer has no associated tag tables")) | |
261 (delete-duplicates (nreverse result) :test 'equal))) | |
262 | |
263 ;;;###autoload | |
264 (defun visit-tags-table (file) | |
265 "Tell tags commands to use tags table file FILE when all else fails. | |
266 FILE should be the name of a file created with the `etags' program. | |
267 A directory name is ok too; it means file TAGS in that directory." | |
268 (interactive (list (read-file-name "Visit tags table: (default TAGS) " | |
269 default-directory | |
270 (expand-file-name "TAGS" default-directory) | |
271 t))) | |
272 (if (string-equal file "") | |
273 (setq tags-file-name nil) | |
274 (setq file (expand-file-name file)) | |
275 (when (file-directory-p file) | |
276 (setq file (expand-file-name "TAGS" file))) | |
277 ;; It used to be that, if a user pressed RET by mistake, the bogus | |
278 ;; `tags-file-name' would remain, causing the error at | |
279 ;; `buffer-tag-table'. | |
280 (when (file-exists-p file) | |
281 (setq tags-file-name file)))) | |
282 | |
283 (defun set-buffer-tag-table (file) | |
284 "In addition to the tags tables specified by the variable `tag-table-alist', | |
285 each buffer can have one additional table. This command sets that. | |
286 See the documentation for the variable `tag-table-alist' for more information." | |
287 (interactive | |
288 (list | |
289 (read-file-name "Visit tags table: (directory sufficient) " | |
290 nil default-directory t))) | |
291 (or file (error "No TAGS file name supplied")) | |
292 (setq file (expand-file-name file)) | |
293 (when (file-directory-p file) | |
294 (setq file (expand-file-name "TAGS" file))) | |
295 (or (file-exists-p file) (error "TAGS file missing: %s" file)) | |
296 (setq buffer-tag-table file)) | |
297 | |
298 | |
299 ;; Manipulating the tag table buffer | |
300 | |
301 (defconst tag-table-completion-status nil | |
302 "Indicates whether a completion table has been built. | |
303 Either nil, t, or `disabled'.") | |
304 (make-variable-buffer-local 'tag-table-completion-status) | |
305 | |
306 (defconst tag-table-files nil | |
307 "If the current buffer is a TAGS table, this holds a list of the files | |
308 referenced by this file, or nil if that hasn't been computed yet.") | |
309 (make-variable-buffer-local 'tag-table-files) | |
310 | |
311 (defun get-tag-table-buffer (tag-table) | |
312 "Returns a buffer visiting the given TAGS table. | |
313 If appropriate, reverting the buffer, and possibly build a completion-table." | |
314 (or (stringp tag-table) | |
315 (error "Bad tags file name supplied: %s" tag-table)) | |
316 ;; Remove symbolic links from name. | |
317 (setq tag-table (symlink-expand-file-name tag-table)) | |
318 (let (buf build-completion check-name) | |
319 (setq buf (get-file-buffer tag-table)) | |
320 (unless buf | |
321 (if (file-readable-p tag-table) | |
322 (setq buf (find-file-noselect tag-table) | |
323 check-name t) | |
324 (error "No such tags file: %s" tag-table))) | |
325 (with-current-buffer buf | |
326 ;; Make the TAGS buffer invisible. | |
327 (when (and check-name | |
328 make-tags-files-invisible | |
329 (string-match "\\`[^ ]" (buffer-name))) | |
330 (rename-buffer (generate-new-buffer-name | |
331 (concat " " (buffer-name))))) | |
332 (or (verify-visited-file-modtime buf) | |
333 (cond ((or tags-auto-read-changed-tag-files | |
334 (yes-or-no-p | |
335 (format "Tags file %s has changed, read new contents? " | |
336 tag-table))) | |
337 (when tags-auto-read-changed-tag-files | |
338 (message "Tags file %s has changed, reading new contents..." | |
339 tag-table)) | |
340 (revert-buffer t t) | |
341 (when (eq tag-table-completion-status t) | |
342 (setq tag-table-completion-status nil)) | |
343 (setq tag-table-files nil)))) | |
344 (or (eq (char-after 1) ?\f) | |
345 (error "File %s not a valid tags file" tag-table)) | |
346 (or (memq tag-table-completion-status '(t disabled)) | |
347 (setq build-completion t)) | |
348 (when build-completion | |
349 (if (ecase tags-build-completion-table | |
350 ((nil) nil) | |
351 ((t) t) | |
352 ((ask) | |
353 ;; don't bother asking for small ones | |
354 (or (< (buffer-size) 20000) | |
355 (y-or-n-p | |
356 (format "Build tag completion table for %s? " | |
357 tag-table))))) | |
358 ;; The user wants to build the table: | |
359 (condition-case nil | |
360 (progn | |
2067 | 361 (if tags-exuberant-ctags-optimization-p |
362 (add-to-tag-completion-table-exuberant-ctags) | |
363 (add-to-tag-completion-table)) | |
428 | 364 (setq tag-table-completion-status t)) |
365 ;; Allow user to C-g out correctly | |
366 (quit | |
367 (message "Tags completion table construction aborted") | |
368 (setq tag-table-completion-status nil | |
369 quit-flag t) | |
370 t)) | |
371 ;; The table is verboten. | |
372 (setq tag-table-completion-status 'disabled)))) | |
373 buf)) | |
374 | |
375 (defun file-of-tag () | |
376 "Return the file name of the file whose tags point is within. | |
377 Assumes the tag table is the current buffer. | |
378 File name returned is relative to tag table file's directory." | |
379 (let ((opoint (point)) | |
380 prev size) | |
381 (save-excursion | |
382 (goto-char (point-min)) | |
383 (while (< (point) opoint) | |
384 (forward-line 1) | |
385 (end-of-line) | |
386 (skip-chars-backward "^,\n") | |
387 (setq prev (point) | |
388 size (read (current-buffer))) | |
389 (goto-char prev) | |
390 (forward-line 1) | |
391 ;; New include syntax | |
392 ;; filename,include | |
393 ;; tacked on to the end of a tag file means use filename | |
394 ;; as a tag file before giving up. | |
395 ;; Skip it here. | |
396 (unless (eq size 'include) | |
397 (forward-char size))) | |
398 (goto-char (1- prev)) | |
399 (buffer-substring (point) (point-at-bol))))) | |
400 | |
401 (defun tag-table-include-files () | |
402 "Return all file names associated with `include' directives in a tag buffer." | |
403 ;; New include syntax | |
404 ;; filename,include | |
405 ;; tacked on to the end of a tag file means use filename as a | |
406 ;; tag file before giving up. | |
407 (let ((files nil)) | |
408 (save-excursion | |
409 (goto-char (point-min)) | |
410 (while (re-search-forward "\f\n\\(.*\\),include$" nil t) | |
411 (push (match-string 1) files))) | |
412 files)) | |
413 | |
414 (defun tag-table-files (tag-table) | |
415 "Returns a list of the files referenced by the named TAGS table." | |
416 (with-current-buffer (get-tag-table-buffer tag-table) | |
417 (unless tag-table-files | |
418 (let (files prev size) | |
419 (goto-char (point-min)) | |
420 (while (not (eobp)) | |
421 (forward-line 1) | |
422 (end-of-line) | |
423 (skip-chars-backward "^,\n") | |
424 (setq prev (point) | |
425 size (read (current-buffer))) | |
426 (goto-char prev) | |
427 (push (expand-file-name (buffer-substring (1- (point)) | |
428 (point-at-bol)) | |
429 default-directory) | |
430 files) | |
431 (forward-line 1) | |
432 (forward-char size)) | |
433 (setq tag-table-files (nreverse files)))) | |
434 tag-table-files)) | |
435 | |
436 ;; #### should this be on previous page? | |
437 (defun buffer-tag-table-files () | |
438 "Returns a list of all files referenced by all TAGS tables that | |
439 this buffer uses." | |
5264
0d43872986b6
Change (apply 'nconc (mapcar ...)) to (mapcan ...); warn about first form.
Aidan Kehoe <kehoea@parhasard.net>
parents:
4755
diff
changeset
|
440 (mapcan #'tag-table-files (buffer-tag-table-list))) |
428 | 441 |
442 | |
443 ;; Building the completion table | |
444 | |
445 ;; Test cases for building completion table; must handle these properly: | |
446 ;; Lisp_Int, XSETINT, current_column 60,2282 | |
447 ;; Lisp_Int, XSETINT, point>NumCharacters ? 0 : CharAt(363,9935 | |
448 ;; Lisp_Int, XSETINT, point<=FirstCharacter ? 0 : CharAt(366,10108 | |
449 ;; point<=FirstCharacter || CharAt(378,10630 | |
450 ;; point>NumCharacters || CharAt(382,10825 | |
451 ;; DEFUN ("x-set-foreground-color", Fx_set_foreground_color,191,4562 | |
452 ;; DEFUN ("x-set-foreground-color", Fx_set_foreground_color,191,4562 | |
453 ;; DEFUN ("*", Ftimes,1172,32079 | |
454 ;; DEFUN ("/=", Fneq,1035,28839 | |
455 ;; defun_internal 4199,101362 | |
456 ;; int pure[PURESIZE / sizeof 53,1564 | |
457 ;; char staticvec1[NSTATICS * sizeof 667,17608 | |
458 ;; Date: 04 May 87 23:53:11 PDT 26,1077 | |
459 ;; #define anymacroname(324,4344 | |
460 ;; (define-key ctl-x-map 311,11784 | |
461 ;; (define-abbrev-table 'c-mode-abbrev-table 24,1016 | |
462 ;; static char *skip_white(116,3443 | |
463 ;; static foo 348,11643 | |
464 ;; (defun texinfo-insert-@code 91,3358 | |
465 ;; (defvar texinfo-kindex)29,1105 | |
466 ;; (defun texinfo-format-\. 548,18376 | |
467 ;; (defvar sm::menu-kludge-y 621,22726 | |
468 ;; (defvar *mouse-drag-window* 103,3642 | |
469 ;; (defun simula-back-level(317,11263 | |
470 ;; } DPxAC,380,14024 | |
471 ;; } BM_QCB;69,2990 | |
472 ;; #define MTOS_DONE\t | |
473 | |
474 ;; "^[^ ]+ +\\([^ ]+\\) " | |
475 | |
476 ;; void *find_cactus_segment(116,2444 | |
477 ;; void *find_pdb_segment(162,3688 | |
478 ;; void init_dclpool(410,10739 | |
479 ;; WORD insert_draw_command(342,8881 | |
480 ;; void *req_pdbmem(579,15574 | |
481 | |
482 (defvar tag-completion-table (make-vector 511 0)) | |
483 | |
484 (defvar tag-symbol) | |
485 (defvar tag-table-symbol) | |
486 (defvar tag-symbol-tables) | |
487 (defvar buffer-tag-table-list) | |
488 | |
489 (defmacro intern-tag-symbol (tag) | |
490 `(progn | |
491 (setq tag-symbol (intern ,tag tag-completion-table) | |
492 tag-symbol-tables (and (boundp tag-symbol) | |
493 (symbol-value tag-symbol))) | |
494 (or (memq tag-table-symbol tag-symbol-tables) | |
495 (set tag-symbol (cons tag-table-symbol tag-symbol-tables))))) | |
496 | |
1584 | 497 ;; Can't use "\\s-" in these patterns because that will include newline |
498 ;; \2 matches an explicit name. | |
499 (defconst tags-explicit-name-pattern "\177\\(\\([^\n\001]+\\)\001\\)?") | |
500 ;; \1 matches Lisp-name, \2 matches C-name, \5 (from | |
501 ;; tags-explicit-name-pattern) matches explicit name. | |
428 | 502 (defconst tags-DEFUN-pattern |
1584 | 503 (concat "DEFUN[ \t]*(\"\\([^\"]+\\)\",[ \t]*\\(\\(\\sw\\|\\s_\\)+\\)," |
504 tags-explicit-name-pattern)) | |
505 ;; \1 matches an array name. Explicit names unused? | |
428 | 506 (defconst tags-array-pattern ".*[ \t]+\\([^ \[]+\\)\\[") |
1584 | 507 ;; \2 matches a Lispish name, \5 (from tags-explicit-name-pattern) matches |
508 ;; explicit name. | |
428 | 509 (defconst tags-def-pattern |
1584 | 510 (concat "\\(.*[ \t]+\\)?\\**\\(\\(\\sw\\|\\s_\\)+\\)[ ();,\t]*" |
511 ;; "\\(.*[ \t]+\\)?\\(\\(\\sw\\|\\s_\\)+\\)[ ()]*" | |
512 ;; "\\(\\sw\\|\\s_\\)+[ ()]*" | |
513 tags-explicit-name-pattern) | |
428 | 514 ) |
1584 | 515 ;; \1 matches Schemish name, \4 (from tags-explicit-name-pattern) matches |
516 ;; explicit name | |
517 (defconst tags-schemish-pattern | |
518 (concat "\\s-*(\\s-*def\\sw*\\s-*(?\\s-*\\(\\(\\sw\\|\\s_\\|:\\)+\\))?\\s-*" | |
519 tags-explicit-name-pattern)) | |
428 | 520 (defconst tags-file-pattern "^\f\n\\(.+\\),[0-9]+\n") |
521 | |
2067 | 522 (defun add-to-tag-completion-table-exuberant-ctags () |
523 "Sucks the current buffer (a TAGS table) into the completion-table. | |
524 This is a version which is optimized for exuberant etags and will not | |
525 work with xemacs etags." | |
526 (message "Adding %s to tags completion table..." buffer-file-name) | |
527 (goto-char (point-min)) | |
528 (let ((tag-table-symbol (intern buffer-file-name tag-completion-table)) | |
529 ;; tag-table-symbol is used by intern-tag-symbol | |
530 name tag-symbol | |
531 tag-symbol-tables | |
532 (case-fold-search nil)) | |
533 (while (re-search-forward tags-explicit-name-pattern nil t) | |
534 ;; no need to check the mode here | |
535 (setq name (match-string 2)) | |
536 (intern-tag-symbol name))) | |
537 (message "Adding %s to tags completion table...done" buffer-file-name)) | |
538 | |
539 | |
428 | 540 ;; #### Should make it work with the `include' directive! |
541 (defun add-to-tag-completion-table () | |
542 "Sucks the current buffer (a TAGS table) into the completion-table." | |
543 (message "Adding %s to tags completion table..." buffer-file-name) | |
544 (goto-char (point-min)) | |
545 (let ((tag-table-symbol (intern buffer-file-name tag-completion-table)) | |
546 ;; tag-table-symbol is used by intern-tag-symbol | |
1584 | 547 filename file-type name name2 name3 tag-symbol |
428 | 548 tag-symbol-tables |
549 (case-fold-search nil)) | |
550 ;; Loop over the files mentioned in the TAGS file for each file, | |
551 ;; try to find its major-mode, then process tags appropriately. | |
552 (while (looking-at tags-file-pattern) | |
553 (goto-char (match-end 0)) | |
554 (setq filename (file-name-sans-versions (match-string 1)) | |
555 ;; We used to check auto-mode-alist for the proper | |
556 ;; file-type. This was way too slow, as it had to process | |
557 ;; an enormous amount of regexps for each time. Now we | |
558 ;; use the shotgun approach with only two regexps. | |
559 file-type (cond ((string-match "\\.\\([cC]\\|cc\\|cxx\\)\\'" | |
560 filename) | |
561 'c-mode) | |
562 ((string-match "\\.\\(el\\|cl\\|lisp\\)\\'" | |
563 filename) | |
564 'lisp-mode) | |
565 ((string-match "\\.scm\\'" filename) | |
566 'scheme-mode) | |
567 (t nil))) | |
442 | 568 (defvar c-mode-syntax-table) |
428 | 569 (set-syntax-table (cond ((and (eq file-type 'c-mode) |
570 c-mode-syntax-table) | |
571 c-mode-syntax-table) | |
572 ((eq file-type 'lisp-mode) | |
573 lisp-mode-syntax-table) | |
574 (t (standard-syntax-table)))) | |
575 ;; Clear loop variables. | |
1584 | 576 (setq name nil name2 nil name3 nil) |
428 | 577 (lmessage 'progress "%s..." filename) |
578 ;; Loop over the individual tag lines. | |
579 (while (not (or (eobp) (eq (char-after) ?\f))) | |
580 (cond ((and (eq file-type 'c-mode) | |
581 (looking-at "DEFUN[ \t]")) | |
582 ;; DEFUN | |
583 (or (looking-at tags-DEFUN-pattern) | |
584 (error "DEFUN doesn't fit pattern")) | |
585 (setq name (match-string 1) | |
1584 | 586 name2 (match-string 2) |
587 name3 (match-string 5))) | |
588 ;;((looking-at "\\s-") | |
428 | 589 ;; skip probably bogus entry: |
590 ;;) | |
591 ((and (eq file-type 'c-mode) | |
592 (looking-at ".*\\[")) | |
593 ;; Array | |
594 (cond ((not (looking-at tags-array-pattern)) | |
595 (message "array definition doesn't fit pattern") | |
596 (setq name nil)) | |
597 (t | |
598 (setq name (match-string 1))))) | |
599 ((and (eq file-type 'scheme-mode) | |
1584 | 600 (looking-at tags-schemish-pattern)) |
428 | 601 ;; Something Schemish (is this really necessary??) |
1584 | 602 (setq name (match-string 1) |
603 name2 (match-string 4))) | |
428 | 604 ((looking-at tags-def-pattern) |
605 ;; ??? | |
1584 | 606 (setq name (match-string 2) |
607 name2 (match-string 5)))) | |
428 | 608 ;; add the tags we found to the completion table |
609 (and name (intern-tag-symbol name)) | |
610 (and name2 (intern-tag-symbol name2)) | |
1584 | 611 (and name3 (intern-tag-symbol name3)) |
428 | 612 (forward-line 1))) |
613 (or (eobp) (error "Bad TAGS file"))) | |
614 (message "Adding %s to tags completion table...done" buffer-file-name)) | |
615 | |
616 | |
617 ;; Interactive find-tag | |
618 | |
619 (defvar find-tag-default-hook nil | |
620 "Function to call to create a default tag. | |
621 Make it buffer-local in a mode hook. The function is called with no | |
622 arguments.") | |
623 | |
624 (defvar find-tag-hook nil | |
625 "*Function to call after a tag is found. | |
626 Make it buffer-local in a mode hook. The function is called with no | |
627 arguments.") | |
628 | |
629 ;; Return a default tag to search for, based on the text at point. | |
630 (defun find-tag-default () | |
631 (or (and (not (memq find-tag-default-hook '(nil find-tag-default))) | |
793 | 632 (with-trapping-errors |
4755
c1784fd59d7d
Fix syntax of some uses of condition-case and with-trapping-errors.
Stephen J. Turnbull <stephen@xemacs.org>
parents:
2067
diff
changeset
|
633 :operation 'find-tag-default-hook |
793 | 634 :error-form nil |
635 (funcall find-tag-default-hook))) | |
428 | 636 (symbol-near-point))) |
637 | |
638 ;; This function depends on the following symbols being bound properly: | |
639 ;; buffer-tag-table-list, | |
640 ;; tag-symbol-tables (value irrelevant, bound outside for efficiency) | |
641 (defun tag-completion-predicate (tag-symbol) | |
642 (and (boundp tag-symbol) | |
643 (setq tag-symbol-tables (symbol-value tag-symbol)) | |
644 (catch 'found | |
645 (while tag-symbol-tables | |
646 (when (memq (car tag-symbol-tables) buffer-tag-table-list) | |
647 (throw 'found t)) | |
648 (setq tag-symbol-tables (cdr tag-symbol-tables)))))) | |
649 | |
650 (defun buffer-tag-table-symbol-list () | |
651 (mapcar (lambda (table-name) | |
652 (intern table-name tag-completion-table)) | |
653 (buffer-tag-table-list))) | |
654 | |
655 (defvar find-tag-history nil "History list for find-tag-tag.") | |
656 | |
657 (defun find-tag-tag (prompt) | |
658 (let* ((default (find-tag-default)) | |
659 (buffer-tag-table-list (buffer-tag-table-symbol-list)) | |
660 tag-symbol-tables tag-name) | |
661 (setq tag-name | |
662 (completing-read | |
663 (if default | |
664 (format "%s(default %s) " prompt default) | |
665 prompt) | |
666 tag-completion-table 'tag-completion-predicate nil nil | |
440 | 667 'find-tag-history default)) |
668 tag-name)) | |
428 | 669 |
670 (defvar last-tag-data nil | |
671 "Information for continuing a tag search. | |
672 Is of the form (TAG POINT MATCHING-EXACT TAG-TABLE TAG-TABLE ...).") | |
673 | |
674 (defvar tags-loop-operate nil | |
675 "Form for `tags-loop-continue' to eval to change one file.") | |
676 | |
677 (defvar tags-loop-scan | |
678 '(error "%s" (substitute-command-keys | |
679 "No \\[tags-search] or \\[tags-query-replace] in progress.")) | |
680 "Form for `tags-loop-continue' to eval to scan one file. | |
681 If it returns non-nil, this file needs processing by evalling | |
682 \`tags-loop-operate'. Otherwise, move on to the next file.") | |
683 | |
684 (autoload 'get-symbol-syntax-table "symbol-syntax") | |
685 | |
686 (defun find-tag-internal (tagname) | |
487 | 687 |
428 | 688 (let ((next (null tagname)) |
689 (tmpnext (null tagname)) | |
690 ;; If tagname is a list: (TAGNAME), this indicates | |
691 ;; requiring an exact symbol match. | |
692 (exact (or tags-always-exact (consp tagname))) | |
693 (normal-syntax-table (syntax-table)) | |
694 (exact-syntax-table (get-symbol-syntax-table (syntax-table))) | |
695 tag-table-currently-matching-exact | |
696 tag-target exact-tagname | |
487 | 697 tag-tables tag-table-point file linebeg line startpos buf |
428 | 698 offset found pat syn-tab) |
699 (when (consp tagname) | |
700 (setq tagname (car tagname))) | |
701 (cond (next | |
702 (setq tagname (car last-tag-data)) | |
703 (setq tag-table-currently-matching-exact | |
704 (car (cdr (cdr last-tag-data))))) | |
705 (t | |
706 (setq tag-table-currently-matching-exact t))) | |
707 ;; \_ in the tagname is used to indicate a symbol boundary. | |
2067 | 708 (if tags-exuberant-ctags-optimization-p |
709 (setq exact-tagname (format "\C-?%s\C-a" tagname)) | |
710 (setq exact-tagname (format "\C-?%s\C-a\\|\ | |
711 \\_%s.?\C-?[0-9]*,[0-9]*$" tagname tagname)) | |
712 ) | |
428 | 713 (while (string-match "\\\\_" exact-tagname) |
714 (aset exact-tagname (1- (match-end 0)) ?b)) | |
715 (save-excursion | |
716 (catch 'found | |
717 ;; Loop searching for exact matches and then inexact matches. | |
718 (while (not (eq tag-table-currently-matching-exact 'neither)) | |
719 (cond (tmpnext | |
720 (setq tag-tables (cdr (cdr (cdr last-tag-data))) | |
721 tag-table-point (car (cdr last-tag-data))) | |
722 ;; Start from the beginning of the table list on the | |
723 ;; next iteration of the loop. | |
724 (setq tmpnext nil)) | |
725 (t | |
726 (setq tag-tables (buffer-tag-table-list) | |
727 tag-table-point 1))) | |
728 (if tag-table-currently-matching-exact | |
729 (setq tag-target exact-tagname | |
730 syn-tab exact-syntax-table) | |
731 (setq tag-target tagname | |
732 syn-tab normal-syntax-table)) | |
733 (with-search-caps-disable-folding tag-target t | |
734 (while tag-tables | |
735 (set-buffer (get-tag-table-buffer (car tag-tables))) | |
736 (bury-buffer (current-buffer)) | |
737 (goto-char (or tag-table-point (point-min))) | |
738 (setq tag-table-point nil) | |
739 (letf (((syntax-table) syn-tab) | |
740 (case-fold-search nil)) | |
741 ;; #### should there be support for non-regexp | |
742 ;; tag searches? | |
743 (while (re-search-forward tag-target nil t) | |
744 (and (save-match-data | |
442 | 745 (save-excursion |
746 (goto-char (match-beginning 0)) | |
747 (looking-at "[^\n\C-?]*\C-?"))) | |
428 | 748 ;; If we're looking for inexact matches, skip |
749 ;; exact matches since we've visited them | |
750 ;; already. | |
751 (or tag-table-currently-matching-exact | |
752 (letf (((syntax-table) exact-syntax-table)) | |
753 (save-excursion | |
754 (goto-char (match-beginning 0)) | |
755 (not (looking-at exact-tagname))))) | |
756 (throw 'found t)))) | |
757 (setq tag-tables | |
758 (nconc (tag-table-include-files) (cdr tag-tables))))) | |
759 (if (and (not exact) (eq tag-table-currently-matching-exact t)) | |
760 (setq tag-table-currently-matching-exact nil) | |
761 (setq tag-table-currently-matching-exact 'neither))) | |
762 (error "No %sentries %s %s" | |
763 (if next "more " "") | |
764 (if exact "matching" "containing") | |
765 tagname)) | |
442 | 766 (beginning-of-line) |
487 | 767 |
768 ;; from here down, synched with FSF 20.7 | |
769 ;; etags-snarf-tag and etags-goto-tag-location. --ben | |
770 | |
771 (if (save-excursion | |
772 (forward-line -1) | |
773 (looking-at "\f\n")) | |
774 (progn | |
775 ;; The match was for a source file name, not any tag | |
776 ;; within a file. Give text of t, meaning to go exactly | |
777 ;; to the location we specify, the beginning of the file. | |
778 (setq linebeg t | |
779 line nil | |
780 startpos 1) | |
781 (setq file | |
782 (expand-file-name (file-of-tag) | |
783 ;; In XEmacs, this needs to be | |
784 ;; relative to: | |
785 (or (file-name-directory (car tag-tables)) | |
786 "./")))) | |
787 (search-forward "\C-?") | |
788 (setq file | |
789 (expand-file-name (file-of-tag) | |
790 ;; In XEmacs, this needs to be | |
791 ;; relative to: | |
792 (or (file-name-directory (car tag-tables)) | |
793 "./"))) | |
794 (setq linebeg (buffer-substring (1- (point)) (point-at-bol))) | |
795 ;; Skip explicit tag name if present. | |
796 (search-forward "\001" (save-excursion (forward-line 1) (point)) t) | |
797 (if (looking-at "[0-9]") | |
798 (setq line (string-to-int (buffer-substring | |
799 (point) | |
800 (progn (skip-chars-forward "0-9") | |
801 (point)))))) | |
802 (search-forward ",") | |
803 (if (looking-at "[0-9]") | |
804 (setq startpos (string-to-int (buffer-substring | |
805 (point) | |
806 (progn (skip-chars-forward "0-9") | |
807 (point))))))) | |
808 ;; Leave point on the next line of the tags file. | |
809 (forward-line 1) | |
428 | 810 (setq last-tag-data |
811 (nconc (list tagname (point) tag-table-currently-matching-exact) | |
812 tag-tables)) | |
813 (setq buf (find-file-noselect file)) | |
487 | 814 |
815 ;; LINEBEG is the initial part of a line containing the tag and | |
816 ;; STARTPOS is the character position of LINEBEG within the file | |
817 ;; (starting from 1); LINE is the line number. If LINEBEG is t, | |
818 ;; it means the tag refers to exactly LINE or STARTPOS | |
819 ;; (whichever is present, LINE having preference, no searching). | |
820 ;; Either LINE or STARTPOS may be nil; STARTPOS is used if | |
821 ;; present. If the tag isn't exactly at the given position then | |
822 ;; look around that position using a search window which expands | |
823 ;; until it hits the start of file. | |
824 | |
428 | 825 (with-current-buffer buf |
826 (save-excursion | |
827 (save-restriction | |
828 (widen) | |
487 | 829 (if (eq linebeg t) |
830 ;; Direct file tag. | |
831 (cond (line (goto-line line)) | |
832 (startpos (goto-char startpos)) | |
833 (t (error "etags.el BUG: bogus direct file tag"))) | |
834 ;; Here we search for PAT in the range [STARTPOS - OFFSET, | |
835 ;; STARTPOS + OFFSET], with increasing values of OFFSET. | |
836 ;; | |
837 ;; We used to set the initial offset to 1000, but the | |
838 ;; actual sources show that finer-grained control is | |
839 ;; needed (e.g. two `hash_string's in src/symbols.c.) So, | |
840 ;; I changed 1000 to 100, and (* 3 offset) to (* 5 offset). | |
841 (setq offset 100) | |
842 (setq pat (concat (if (eq selective-display t) | |
843 "\\(^\\|\^m\\)" "^") | |
844 (regexp-quote linebeg))) | |
845 | |
846 ;; The character position in the tags table is 0-origin. | |
847 ;; Convert it to a 1-origin Emacs character position. | |
848 (if startpos (setq startpos (1+ startpos))) | |
849 ;; If no char pos was given, try the given line number. | |
850 (or startpos | |
851 (if line | |
852 (setq startpos (progn (goto-line line) | |
853 (point))))) | |
854 (or startpos | |
855 (setq startpos (point-min))) | |
856 ;; First see if the tag is right at the specified location. | |
857 (goto-char startpos) | |
858 (setq found (looking-at pat)) | |
859 (while (and (not found) | |
860 (progn | |
861 (goto-char (- startpos offset)) | |
862 (not (bobp)))) | |
863 (setq found | |
864 (re-search-forward pat (+ startpos offset) t) | |
865 offset (* 5 offset))) ; expand search window | |
866 ;; Finally, try finding it anywhere in the buffer. | |
867 (or found | |
868 (re-search-forward pat nil t) | |
869 (error "Rerun etags: `%s' not found in %s" | |
870 pat file)))) | |
871 ;; Position point at the right place | |
872 ;; if the search string matched an extra Ctrl-m at the beginning. | |
873 (and (eq selective-display t) | |
874 (looking-at "\^m") | |
875 (forward-char 1)) | |
876 (beginning-of-line) | |
877 (setq startpos (point)))) | |
428 | 878 (cons buf startpos)))) |
879 | |
880 ;;;###autoload | |
442 | 881 (defun find-tag-at-point (tagname &optional other-window) |
882 "*Find tag whose name contains TAGNAME. | |
883 Identical to `find-tag' but does not prompt for tag when called interactively; | |
884 instead, uses tag around or before point." | |
885 (interactive (if current-prefix-arg | |
886 '(nil nil) | |
887 (list (find-tag-default) nil))) | |
888 (find-tag tagname other-window)) | |
889 | |
890 ;;;###autoload | |
428 | 891 (defun find-tag (tagname &optional other-window) |
892 "*Find tag whose name contains TAGNAME. | |
893 Selects the buffer that the tag is contained in | |
894 and puts point at its definition. | |
895 If TAGNAME is a null string, the expression in the buffer | |
896 around or before point is used as the tag name. | |
897 If called interactively with a numeric argument, searches for the next tag | |
898 in the tag table that matches the tagname used in the previous find-tag. | |
899 If second arg OTHER-WINDOW is non-nil, uses another window to display | |
900 the tag. | |
901 | |
902 This version of this function supports multiple active tags tables, | |
903 and completion. | |
904 | |
905 Variables of note: | |
906 | |
907 tag-table-alist controls which tables apply to which buffers | |
908 tags-file-name a default tags table | |
909 tags-build-completion-table controls completion behavior | |
910 buffer-tag-table another way of specifying a buffer-local table | |
911 make-tags-files-invisible whether tags tables should be very hidden | |
912 tag-mark-stack-max how many tags-based hops to remember" | |
913 (interactive (if current-prefix-arg | |
914 '(nil nil) | |
915 (list (find-tag-tag "Find tag: ") nil))) | |
916 (let* ((local-find-tag-hook find-tag-hook) | |
917 (next (null tagname)) | |
918 (result (find-tag-internal tagname)) | |
919 (tag-buf (car result)) | |
920 (tag-point (cdr result))) | |
921 ;; Push old position on the tags mark stack. | |
922 (if (or (not next) | |
923 (not (memq last-command | |
924 '(find-tag find-tag-other-window tags-loop-continue)))) | |
925 (push-tag-mark)) | |
926 (if other-window | |
442 | 927 (pop-to-buffer tag-buf t) |
428 | 928 (switch-to-buffer tag-buf)) |
929 (widen) | |
930 (push-mark) | |
931 (goto-char tag-point) | |
932 (if find-tag-hook | |
933 (run-hooks 'find-tag-hook) | |
934 (if local-find-tag-hook | |
935 (run-hooks 'local-find-tag-hook)))) | |
936 (setq tags-loop-scan (list 'find-tag nil nil) | |
937 tags-loop-operate nil) | |
938 ;; Return t in case used as the tags-loop-scan. | |
939 t) | |
940 | |
941 ;;;###autoload | |
942 (defun find-tag-other-window (tagname &optional next) | |
442 | 943 "*Find tag whose name contains TAGNAME, in another window. |
428 | 944 Selects the buffer that the tag is contained in in another window |
945 and puts point at its definition. | |
946 If TAGNAME is a null string, the expression in the buffer | |
947 around or before point is used as the tag name. | |
948 If second arg NEXT is non-nil (interactively, with prefix arg), | |
949 searches for the next tag in the tag table | |
950 that matches the tagname used in the previous find-tag. | |
951 | |
952 This version of this function supports multiple active tags tables, | |
953 and completion. | |
954 | |
955 Variables of note: | |
956 | |
957 tag-table-alist controls which tables apply to which buffers | |
958 tags-file-name a default tags table | |
959 tags-build-completion-table controls completion behavior | |
960 buffer-tag-table another way of specifying a buffer-local table | |
961 make-tags-files-invisible whether tags tables should be very hidden | |
962 tag-mark-stack-max how many tags-based hops to remember" | |
963 (interactive (if current-prefix-arg | |
964 '(nil t) | |
965 (list (find-tag-tag "Find tag other window: ")))) | |
966 (if next | |
967 (find-tag nil t) | |
968 (find-tag tagname t))) | |
969 | |
970 | |
971 ;; Completion on tags in the buffer. | |
972 | |
973 (defun complete-symbol (&optional table predicate prettify) | |
974 (let* ((end (point)) | |
975 (beg (save-excursion | |
976 (backward-sexp 1) | |
977 ;;(while (= (char-syntax (following-char)) ?\') | |
978 ;; (forward-char 1)) | |
979 (skip-syntax-forward "'") | |
980 (point))) | |
981 (pattern (buffer-substring beg end)) | |
982 (table (or table obarray)) | |
983 (completion (try-completion pattern table predicate))) | |
984 (cond ((eq completion t)) | |
985 ((null completion) | |
986 (error "Can't find completion for \"%s\"" pattern)) | |
987 ((not (string-equal pattern completion)) | |
988 (delete-region beg end) | |
989 (insert completion)) | |
990 (t | |
991 (message "Making completion list...") | |
992 (let ((list (all-completions pattern table predicate))) | |
993 (if prettify | |
994 (setq list (funcall prettify list))) | |
995 (with-output-to-temp-buffer "*Help*" | |
996 (display-completion-list list))) | |
997 (message "Making completion list...%s" "done"))))) | |
998 | |
999 ;;;###autoload | |
1000 (defun tag-complete-symbol () | |
1001 "The function used to do tags-completion (using 'tag-completion-predicate)." | |
1002 (interactive) | |
1003 (let* ((buffer-tag-table-list (buffer-tag-table-symbol-list)) | |
1004 tag-symbol-tables) | |
1005 (complete-symbol tag-completion-table 'tag-completion-predicate))) | |
1006 | |
1007 | |
1008 ;; Applying a command to files mentioned in tag tables | |
1009 | |
1010 (defvar next-file-list nil | |
1011 "List of files for next-file to process.") | |
1012 | |
1013 ;;;###autoload | |
1014 (defun next-file (&optional initialize novisit) | |
1015 "Select next file among files in current tag table(s). | |
1016 | |
1017 A first argument of t (prefix arg, if interactive) initializes to the | |
1018 beginning of the list of files in the (first) tags table. If the argument | |
1019 is neither nil nor t, it is evalled to initialize the list of files. | |
1020 | |
1021 Non-nil second argument NOVISIT means use a temporary buffer | |
1022 to save time and avoid uninteresting warnings. | |
1023 | |
1024 Value is nil if the file was already visited; | |
1025 if the file was newly read in, the value is the filename." | |
1026 (interactive "P") | |
1027 (cond ((not initialize) | |
1028 ;; Not the first run. | |
1029 ) | |
1030 ((eq initialize t) | |
1031 ;; Initialize the list from the tags table. | |
1032 (setq next-file-list (buffer-tag-table-files))) | |
1033 (t | |
1034 ;; Initialize the list by evalling the argument. | |
1035 (setq next-file-list (eval initialize)))) | |
1036 (when (null next-file-list) | |
1037 (and novisit | |
1038 (get-buffer " *next-file*") | |
1039 (kill-buffer " *next-file*")) | |
1040 (error "All files processed")) | |
1041 (let* ((file (car next-file-list)) | |
1042 (buf (get-file-buffer file)) | |
1043 (new (not buf))) | |
1044 (pop next-file-list) | |
1045 | |
1046 (if (not (and new novisit)) | |
1047 (switch-to-buffer (find-file-noselect file novisit) t) | |
1048 ;; Like find-file, but avoids random junk. | |
1049 (set-buffer (get-buffer-create " *next-file*")) | |
1050 (kill-all-local-variables) | |
1051 (erase-buffer) | |
1052 (insert-file-contents file nil)) | |
1053 (widen) | |
1054 (when (> (point) (point-min)) | |
1055 (push-mark nil t) | |
1056 (goto-char (point-min))) | |
1057 (and new file))) | |
1058 | |
1059 ;;;###autoload | |
1060 (defun tags-loop-continue (&optional first-time) | |
1061 "Continue last \\[tags-search] or \\[tags-query-replace] command. | |
1062 Used noninteractively with non-nil argument to begin such a command (the | |
1063 argument is passed to `next-file', which see). | |
1064 Two variables control the processing we do on each file: | |
1065 the value of `tags-loop-scan' is a form to be executed on each file | |
1066 to see if it is interesting (it returns non-nil if so) | |
1067 and `tags-loop-operate' is a form to execute to operate on an interesting file | |
1068 If the latter returns non-nil, we exit; otherwise we scan the next file." | |
1069 (interactive) | |
1070 (let ((messaged nil) | |
1071 (more-files-p t) | |
1072 new) | |
1073 (while more-files-p | |
1074 ;; Scan files quickly for the first or next interesting one. | |
1075 (while (or first-time | |
1076 (save-restriction | |
1077 (widen) | |
1078 (not (eval tags-loop-scan)))) | |
1079 (setq new (next-file first-time | |
1080 tags-search-nuke-uninteresting-buffers)) | |
1081 ;; If NEW is non-nil, we got a temp buffer, | |
1082 ;; and NEW is the file name. | |
1083 (if (or messaged | |
1084 (and (not first-time) | |
1085 (> (device-baud-rate) search-slow-speed) | |
1086 (setq messaged t))) | |
1087 (lmessage 'progress | |
1088 "Scanning file %s..." (or new buffer-file-name))) | |
1089 (setq first-time nil) | |
1090 (goto-char (point-min))) | |
1091 | |
1092 ;; If we visited it in a temp buffer, visit it now for real. | |
1093 (if (and new tags-search-nuke-uninteresting-buffers) | |
1094 (let ((pos (point))) | |
1095 (erase-buffer) | |
1096 (set-buffer (find-file-noselect new)) | |
1097 (widen) | |
1098 (goto-char pos))) | |
1099 | |
1100 (switch-to-buffer (current-buffer)) | |
1101 | |
1102 ;; Now operate on the file. | |
1103 ;; If value is non-nil, continue to scan the next file. | |
1104 (setq more-files-p (eval tags-loop-operate))) | |
1105 (and messaged | |
1106 (null tags-loop-operate) | |
1107 (message "Scanning file %s...found" buffer-file-name)))) | |
1108 | |
1109 | |
1110 ;;;###autoload | |
1111 (defun tags-search (regexp &optional file-list-form) | |
1112 "Search through all files listed in tags table for match for REGEXP. | |
1113 Stops when a match is found. | |
1114 To continue searching for next match, use command \\[tags-loop-continue]. | |
1115 | |
1116 See documentation of variable `tag-table-alist'." | |
1117 (interactive "sTags search (regexp): ") | |
1118 (if (and (equal regexp "") | |
1119 (eq (car tags-loop-scan) 'with-search-caps-disable-folding) | |
1120 (null tags-loop-operate)) | |
1121 ;; Continue last tags-search as if by `M-,'. | |
1122 (tags-loop-continue nil) | |
1123 (setq tags-loop-scan `(with-search-caps-disable-folding ,regexp t | |
1124 (re-search-forward ,regexp nil t)) | |
1125 tags-loop-operate nil) | |
1126 (tags-loop-continue (or file-list-form t)))) | |
1127 | |
1128 ;;;###autoload | |
1129 (defun tags-query-replace (from to &optional delimited file-list-form) | |
1130 "Query-replace-regexp FROM with TO through all files listed in tags table. | |
1131 Third arg DELIMITED (prefix arg) means replace only word-delimited matches. | |
1132 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace | |
1133 with the command \\[tags-loop-continue]. | |
1134 | |
1135 See documentation of variable `tag-table-alist'." | |
1136 (interactive | |
1137 "sTags query replace (regexp): \nsTags query replace %s by: \nP") | |
1138 (setq tags-loop-scan `(with-search-caps-disable-folding ,from t | |
1139 (if (re-search-forward ,from nil t) | |
1140 ;; When we find a match, move back | |
1141 ;; to the beginning of it so perform-replace | |
1142 ;; will see it. | |
1143 (progn (goto-char (match-beginning 0)) t))) | |
1144 tags-loop-operate (list 'perform-replace from to t t | |
1145 (not (null delimited)))) | |
1146 (tags-loop-continue (or file-list-form t))) | |
1147 | |
1148 ;; Miscellaneous | |
1149 | |
1150 ;;;###autoload | |
1151 (defun list-tags (file) | |
1152 "Display list of tags in FILE." | |
1153 (interactive (list (read-file-name | |
1154 (if (buffer-file-name) | |
1155 (format "List tags (in file, %s by default): " | |
1156 (file-name-nondirectory (buffer-file-name))) | |
1157 "List tags (in file): ") | |
1158 nil (buffer-file-name) t))) | |
1159 (find-file-noselect file) | |
1160 (with-output-to-temp-buffer "*Tags List*" | |
1161 (princ "Tags in file ") | |
1162 (princ file) | |
1163 (terpri) | |
1164 (save-excursion | |
1165 (dolist (tags-file (with-current-buffer (get-file-buffer file) | |
1166 (buffer-tag-table-list))) | |
1167 ;; We don't want completions getting in the way. | |
1168 (let ((tags-build-completion-table nil)) | |
1169 (set-buffer (get-tag-table-buffer tags-file))) | |
1170 (goto-char (point-min)) | |
1171 (when | |
1172 (search-forward (concat "\f\n" (file-name-nondirectory file) ",") | |
1173 nil t) | |
1174 (forward-line 1) | |
1175 (while (not (or (eobp) (looking-at "\f"))) | |
1176 (princ (buffer-substring (point) | |
1177 (progn (skip-chars-forward "^\C-?") | |
1178 (point)))) | |
1179 (terpri) | |
1180 (forward-line 1))))))) | |
1181 | |
1182 ;;;###autoload | |
1183 (defun tags-apropos (string) | |
1184 "Display list of all tags in tag table REGEXP matches." | |
1185 (interactive "sTag apropos (regexp): ") | |
1186 (with-output-to-temp-buffer "*Tags List*" | |
1187 (princ "Tags matching regexp ") | |
1188 (prin1 string) | |
1189 (terpri) | |
1190 (save-excursion | |
1191 (visit-tags-table-buffer) | |
1192 (goto-char 1) | |
1193 (while (re-search-forward string nil t) | |
1194 (beginning-of-line) | |
1195 (princ (buffer-substring (point) | |
1196 (progn (skip-chars-forward "^\C-?") | |
1197 (point)))) | |
1198 (terpri) | |
1199 (forward-line 1))))) | |
1200 | |
1201 ;; #### copied from tags.el. This function is *very* big in FSF. | |
1202 (defun visit-tags-table-buffer () | |
1203 "Select the buffer containing the current tag table." | |
1204 (or tags-file-name | |
1205 (call-interactively 'visit-tags-table)) | |
1206 (set-buffer (or (get-file-buffer tags-file-name) | |
1207 (progn | |
1208 (setq tag-table-files nil) | |
1209 (find-file-noselect tags-file-name)))) | |
1210 (or (verify-visited-file-modtime (get-file-buffer tags-file-name)) | |
1211 (cond ((yes-or-no-p "Tags file has changed, read new contents? ") | |
1212 (revert-buffer t t) | |
1213 (setq tag-table-files nil)))) | |
1214 (or (eq (char-after 1) ?\^L) | |
1215 (error "File %s not a valid tag table" tags-file-name))) | |
1216 | |
1217 | |
1218 ;; Sample uses of find-tag-hook and find-tag-default-hook | |
1219 | |
1220 ;; This is wrong. We should either make this behavior default and | |
1221 ;; back it up, or not use it at all. For now, I've commented it out. | |
1222 ;; --hniksic | |
1223 | |
1224 ;; Example buffer-local tag finding | |
1225 | |
1226 ;(add-hook 'emacs-lisp-mode-hook 'setup-emacs-lisp-default-tag-hook) | |
1227 | |
1228 ;(defun setup-emacs-lisp-default-tag-hook () | |
1229 ; (cond ((eq major-mode 'emacs-lisp-mode) | |
1230 ; (make-variable-buffer-local 'find-tag-default-hook) | |
1231 ; (setq find-tag-default-hook 'emacs-lisp-default-tag)))) | |
1232 ;;; Run it once immediately | |
1233 ;(setup-emacs-lisp-default-tag-hook) | |
1234 ;(when (get-buffer "*scratch*") | |
1235 ; (with-current-buffer "*scratch*" | |
1236 ; (setup-emacs-lisp-default-tag-hook))) | |
1237 | |
1238 ;(defun emacs-lisp-default-tag () | |
1239 ; "Function to return a default tag for Emacs-Lisp mode." | |
1240 ; (let ((tag (or (variable-at-point) | |
1241 ; (function-at-point)))) | |
1242 ; (if tag (symbol-name tag)))) | |
1243 | |
1244 | |
1245 ;; Display short info on tag in minibuffer | |
1246 | |
1247 ;; Don't pollute `M-?' -- we may need it for more important stuff. --hniksic | |
1248 ;(if (null (lookup-key esc-map "?")) | |
1249 ; (define-key esc-map "?" 'display-tag-info)) | |
1250 | |
1251 (defun display-tag-info (tagname) | |
1252 "Prints a description of the first tag matching TAGNAME in the echo area. | |
1253 If this is an elisp function, prints something like \"(defun foo (x y z)\". | |
1254 That is, is prints the first line of the definition of the form. | |
1255 If this is a C-defined elisp function, it does something more clever." | |
1256 (interactive (if current-prefix-arg | |
1257 '(nil) | |
1258 (list (find-tag-tag "Display tag info: ")))) | |
1259 (let* ((results (find-tag-internal tagname)) | |
1260 (tag-buf (car results)) | |
1261 (tag-point (cdr results)) | |
1262 info lname min max fname args) | |
1263 (with-current-buffer tag-buf | |
1264 (save-excursion | |
1265 (save-restriction | |
1266 (widen) | |
1267 (goto-char tag-point) | |
1268 (cond ((let ((case-fold-search nil)) | |
1269 (looking-at "^DEFUN[ \t]")) | |
1270 (forward-sexp 1) | |
1271 (down-list 1) | |
1272 (setq lname (read (current-buffer)) | |
1273 fname (buffer-substring | |
1274 (progn (forward-sexp 1) (point)) | |
1275 (progn (backward-sexp 1) (point))) | |
1276 min (buffer-substring | |
1277 (progn (forward-sexp 3) (point)) | |
1278 (progn (backward-sexp 1) (point))) | |
1279 max (buffer-substring | |
1280 (progn (forward-sexp 2) (point)) | |
1281 (progn (backward-sexp 1) (point)))) | |
1282 (backward-up-list 1) | |
1283 (setq args (buffer-substring | |
1284 (progn (forward-sexp 2) (point)) | |
1285 (progn (backward-sexp 1) (point)))) | |
1286 (setq info (format "Elisp: %s, C: %s %s, #args: %s" | |
1287 lname | |
1288 fname args | |
1289 (if (string-equal min max) | |
1290 min | |
1291 (format "from %s to %s" min max))))) | |
1292 (t | |
1293 (setq info | |
1294 (buffer-substring | |
1295 (progn (beginning-of-line) (point)) | |
1296 (progn (end-of-line) (point))))))))) | |
1297 (message "%s" info)) | |
1298 (setq tags-loop-scan '(display-tag-info nil) | |
1299 tags-loop-operate nil) | |
1300 ;; Always return non-nil | |
1301 t) | |
1302 | |
1303 | |
1304 ;; Tag mark stack. | |
1305 | |
1306 (defvar tag-mark-stack1 nil) | |
1307 (defvar tag-mark-stack2 nil) | |
1308 | |
1309 (defcustom tag-mark-stack-max 16 | |
1310 "*The maximum number of elements kept on the mark-stack used | |
1311 by tags-search. See also the commands `\\[push-tag-mark]' and | |
1312 and `\\[pop-tag-mark]'." | |
1313 :type 'integer | |
1314 :group 'etags) | |
1315 | |
1316 (defun push-mark-on-stack (stack-symbol &optional max-size) | |
1317 (let ((stack (symbol-value stack-symbol))) | |
1318 (push (point-marker) stack) | |
1319 (cond ((and max-size | |
1320 (> (length stack) max-size)) | |
1321 (set-marker (car (nthcdr max-size stack)) nil) | |
1322 (setcdr (nthcdr (1- max-size) stack) nil))) | |
1323 (set stack-symbol stack))) | |
1324 | |
1325 (defun pop-mark-from-stack (stack-symbol1 stack-symbol2 &optional max-size) | |
1326 (let* ((stack (or (symbol-value stack-symbol1) | |
1327 (error "No more tag marks on stack"))) | |
1328 (marker (car stack)) | |
1329 (m-buf (marker-buffer marker))) | |
1330 (set stack-symbol1 (cdr stack)) | |
1331 (or m-buf | |
1332 (error "Marker has no buffer")) | |
1333 (or (buffer-live-p m-buf) | |
1334 (error "Buffer has been killed")) | |
1335 (push-mark-on-stack stack-symbol2 max-size) | |
1336 (switch-to-buffer m-buf) | |
1337 (widen) | |
1338 (goto-char marker))) | |
1339 | |
1340 (defun push-tag-mark () | |
1341 (push-mark-on-stack 'tag-mark-stack1 tag-mark-stack-max)) | |
1342 | |
1343 ;;;###autoload (define-key esc-map "*" 'pop-tag-mark) | |
1344 | |
1345 ;;;###autoload | |
1346 (defun pop-tag-mark (arg) | |
1347 "Go to last tag position. | |
1348 `find-tag' maintains a mark-stack seperate from the \\[set-mark-command] mark-stack. | |
1349 This function pops (and moves to) the tag at the top of this stack." | |
1350 (interactive "P") | |
1351 (if (not arg) | |
1352 (pop-mark-from-stack | |
1353 'tag-mark-stack1 'tag-mark-stack2 tag-mark-stack-max) | |
1354 (pop-mark-from-stack | |
1355 'tag-mark-stack2 'tag-mark-stack1 tag-mark-stack-max))) | |
1356 | |
1357 | |
1358 (provide 'etags) | |
1359 (provide 'tags) | |
1360 | |
1361 ;;; etags.el ends here |