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