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