etags.el 46.4 KB
Newer Older
Eric S. Raymond's avatar
Eric S. Raymond committed
1 2
;; etags.el --- etags facility for Emacs

3
;; Copyright (C) 1985, 1986, 1988, 1989, 1992 Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4

Eric S. Raymond's avatar
Eric S. Raymond committed
5 6 7
;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
;; Keywords: tools

Jim Blandy's avatar
Jim Blandy committed
8 9 10 11
;; This file is part of GNU Emacs.

;; GNU Emacs is free software; you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
Jim Blandy's avatar
Jim Blandy committed
12
;; the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
13 14 15 16 17 18 19 20 21 22 23
;; any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs; see the file COPYING.  If not, write to
;; the Free Software Foundation, 675 Mass Ave, Cambridge, MA 02139, USA.

Eric S. Raymond's avatar
Eric S. Raymond committed
24 25
;;; Code:

Jim Blandy's avatar
Jim Blandy committed
26
;;;###autoload
27 28
(defvar tags-file-name nil
  "*File name of tags table.
Roland McGrath's avatar
Roland McGrath committed
29
To switch to a new tags table, setting this variable is sufficient.
30
If you set this variable, do not also set `tags-table-list'.
Roland McGrath's avatar
Roland McGrath committed
31
Use the `etags' program to make a tags table file.")
32
;; Make M-x set-variable tags-file-name like M-x visit-tags-table.
Roland McGrath's avatar
Roland McGrath committed
33
;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ")
Roland McGrath's avatar
Roland McGrath committed
34 35

;;;###autoload
36
;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
Roland McGrath's avatar
Roland McGrath committed
37
(defvar tags-table-list nil
38 39 40 41 42
  "*List of file names of tags tables to search.
An element that is a directory means the file \"TAGS\" in that directory.
To switch to a new list of tags tables, setting this variable is sufficient.
If you set this variable, do not also set `tags-file-name'.
Use the `etags' program to make a tags table file.")
Roland McGrath's avatar
Roland McGrath committed
43 44

(defvar tags-table-list-pointer nil
Roland McGrath's avatar
Roland McGrath committed
45 46 47 48 49 50
  "Pointer into `tags-table-list' where the current state of searching is.
Might instead point into a list of included tags tables.
Use `visit-tags-table-buffer' to cycle through tags tables in this list.")

(defvar tags-table-list-started-at nil
  "Pointer into `tags-table-list', where the current search started.")
Roland McGrath's avatar
Roland McGrath committed
51 52

(defvar tags-table-parent-pointer-list nil
Roland McGrath's avatar
Roland McGrath committed
53 54 55 56
  "Saved state of the tags table that included this one.
Each element is (POINTER . STARTED-AT), giving the values of
 `tags-table-list-pointer' and `tags-table-list-started-at' from
 before we moved into the current table.")
Roland McGrath's avatar
Roland McGrath committed
57 58 59 60 61 62 63 64 65 66 67 68 69

(defvar tags-table-set-list nil
  "List of sets of tags table which have been used together in the past.
Each element is a list of strings which are file names.")

;;;###autoload
(defvar find-tag-hook nil
  "*Hook to be run by \\[find-tag] after finding a tag.  See `run-hooks'.
The value in the buffer in which \\[find-tag] is done is used,
not the value in the buffer \\[find-tag] goes to.")

;;;###autoload
(defvar find-tag-default-function nil
Roland McGrath's avatar
Roland McGrath committed
70 71
  "*A function of no arguments used by \\[find-tag] to pick a default tag.
If nil, and the symbol that is the value of `major-mode'
Roland McGrath's avatar
Roland McGrath committed
72 73 74 75 76 77 78 79 80 81
has a `find-tag-default-function' property (see `put'), that is used.
Otherwise, `find-tag-default' is used.")

;;;###autoload
(defvar default-tags-table-function nil
  "*If non-nil, a function of no arguments to choose a default tags file
for a particular buffer.")

;; Tags table state.
;; These variables are local in tags table buffers.
Jim Blandy's avatar
Jim Blandy committed
82

Roland McGrath's avatar
Roland McGrath committed
83 84 85
(defvar tag-lines-already-matched nil
  "List of positions of beginnings of lines within the tags table
that are already matched.")
Jim Blandy's avatar
Jim Blandy committed
86

Roland McGrath's avatar
Roland McGrath committed
87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102
(defvar tags-table-files nil
  "List of file names covered by current tags table.
nil means it has not yet been computed; use `tags-table-files' to do so.")

(defvar tags-completion-table nil
  "Alist of tag names defined in current tags table.")

(defvar tags-included-tables nil
  "List of tags tables included by the current tags table.")

(defvar next-file-list nil
  "List of files for \\[next-file] to process.")

;; Hooks for file formats.

(defvar tags-table-format-hooks '(etags-recognize-tags-table
Roland McGrath's avatar
Roland McGrath committed
103
				  recognize-empty-tags-table)
Roland McGrath's avatar
Roland McGrath committed
104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  "List of functions to be called in a tags table buffer to identify
the type of tags table.  The functions are called in order, with no arguments,
until one returns non-nil.  The function should make buffer-local bindings
of the format-parsing tags function variables if successful.")

(defvar file-of-tag-function nil
  "Function to do the work of `file-of-tag' (which see).")
(defvar tags-table-files-function nil
  "Function to do the work of `tags-table-files' (which see).")
(defvar tags-completion-table-function nil
  "Function to build the tags-completion-table.")
(defvar snarf-tag-function nil
  "Function to get info about a matched tag for `goto-tag-location-function'.")
(defvar goto-tag-location-function nil
  "Function of to go to the location in the buffer specified by a tag.
One argument, the tag info returned by `snarf-tag-function'.")
(defvar find-tag-regexp-search-function nil
  "Search function passed to `find-tag-in-order' for finding a regexp tag.")
(defvar find-tag-regexp-tag-order nil
  "Tag order passed to `find-tag-in-order' for finding a regexp tag.")
(defvar find-tag-regexp-next-line-after-failure-p nil
  "Flag passed to `find-tag-in-order' for finding a regexp tag.")
(defvar find-tag-search-function nil
  "Search function passed to `find-tag-in-order' for finding a tag.")
(defvar find-tag-tag-order nil
  "Tag order passed to `find-tag-in-order' for finding a tag.")
(defvar find-tag-next-line-after-failure-p nil
  "Flag passed to `find-tag-in-order' for finding a tag.")
(defvar list-tags-function nil
  "Function to do the work of `list-tags' (which see).")
(defvar tags-apropos-function nil
  "Function to do the work of `tags-apropos' (which see).")
(defvar tags-included-tables-function nil
  "Function to do the work of `tags-included-tables' (which see).")
(defvar verify-tags-table-function nil
  "Function to return t iff the current buffer vontains a valid
\(already initialized\) tags file.")

142 143 144 145
;; Initialize the tags table in the current buffer.
;; Returns non-nil iff it is a valid tags table.  On
;; non-nil return, the tags table state variable are
;; made buffer-local and initialized to nil.
Roland McGrath's avatar
Roland McGrath committed
146
(defun initialize-new-tags-table ()
147 148 149 150
  (set (make-local-variable 'tag-lines-already-matched) nil)
  (set (make-local-variable 'tags-table-files) nil)
  (set (make-local-variable 'tags-completion-table) nil)
  (set (make-local-variable 'tags-included-tables) nil)
Roland McGrath's avatar
Roland McGrath committed
151 152 153 154 155 156
  ;; Value is t if we have found a valid tags table buffer.
  (let ((hooks tags-table-format-hooks))
    (while (and hooks
		(not (funcall (car hooks))))
      (setq hooks (cdr hooks)))
    hooks))
Jim Blandy's avatar
Jim Blandy committed
157

Jim Blandy's avatar
Jim Blandy committed
158
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
159 160
(defun visit-tags-table (file &optional local)
  "Tell tags commands to use tags table file FILE.
Jim Blandy's avatar
Jim Blandy committed
161
FILE should be the name of a file created with the `etags' program.
Roland McGrath's avatar
Roland McGrath committed
162 163 164 165 166 167 168
A directory name is ok too; it means file TAGS in that directory.

Normally \\[visit-tags-table] sets the global value of `tags-file-name'.
With a prefix arg, set the buffer-local value instead.
When you find a tag with \\[find-tag], the buffer it finds the tag
in is given a local value of this variable which is the name of the tags
file the tag was in."
Jim Blandy's avatar
Jim Blandy committed
169 170
  (interactive (list (read-file-name "Visit tags table: (default TAGS) "
				     default-directory
Roland McGrath's avatar
Roland McGrath committed
171 172 173 174
				     (expand-file-name "TAGS"
						       default-directory)
				     t)
		     current-prefix-arg))
175 176 177
  ;; Calling visit-tags-table-buffer with tags-file-name set to FILE will
  ;; initialize a buffer for FILE and set tags-file-name to the
  ;; fully-expanded name.
Roland McGrath's avatar
Roland McGrath committed
178 179
  (let ((tags-file-name file))
    (save-excursion
Roland McGrath's avatar
Roland McGrath committed
180
      (or (visit-tags-table-buffer 'same)
Roland McGrath's avatar
Roland McGrath committed
181 182 183
	  (signal 'file-error (list "Visiting tags table"
				    "file does not exist"
				    file)))
184
      ;; Set FILE to the expanded name.
Roland McGrath's avatar
Roland McGrath committed
185
      (setq file tags-file-name)))
Roland McGrath's avatar
Roland McGrath committed
186
  (if local
187
      ;; Set the local value of tags-file-name.
Roland McGrath's avatar
Roland McGrath committed
188
      (set (make-local-variable 'tags-file-name) file)
189
    ;; Set the global value of tags-file-name.
Roland McGrath's avatar
Roland McGrath committed
190 191 192
    (setq-default tags-file-name file)))

;; Move tags-table-list-pointer along and set tags-file-name.
193
;; If NO-INCLUDES is non-nil, ignore included tags tables.
Roland McGrath's avatar
Roland McGrath committed
194
;; Returns nil when out of tables.
195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239
(defun tags-next-table (&optional no-includes)
  ;; Do we have any included tables?
  (if (and (not no-includes)
	   (visit-tags-table-buffer 'same)
	   (tags-included-tables))

      ;; Move into the included tags tables.
      (setq tags-table-parent-pointer-list
	    ;; Save the current state of what table we are in.
	    (cons (cons tags-table-list-pointer tags-table-list-started-at)
		  tags-table-parent-pointer-list)
	    ;; Start the pointer in the list of included tables.
	    tags-table-list-pointer tags-included-tables
	    tags-table-list-started-at tags-included-tables)

    ;; No included tables.  Go to the next table in the list.
    (setq tags-table-list-pointer
	  (cdr tags-table-list-pointer))
    (or tags-table-list-pointer
	;; Wrap around.
	(setq tags-table-list-pointer tags-table-list))

    (if (eq tags-table-list-pointer tags-table-list-started-at)
	;; We have come full circle.  No more tables.
	(if tags-table-parent-pointer-list
	    ;; Pop back to the tags table which includes this one.
	    (progn
	      ;; Restore the state variables.
	      (setq tags-table-list-pointer
		    (car (car tags-table-parent-pointer-list))
		    tags-table-list-started-at
		    (cdr (car tags-table-parent-pointer-list))
		    tags-table-parent-pointer-list
		    (cdr tags-table-parent-pointer-list))
	      ;; Recurse to skip to the next table after the parent.
	      (tags-next-table t))
	  ;; All out of tags tables.
	  (setq tags-table-list-pointer nil))))

  (and tags-table-list-pointer
       ;; Set tags-file-name to the fully-expanded name.
       (setq tags-file-name
	     (tags-expand-table-name (car tags-table-list-pointer)))))

;; Expand tags table name FILE into a complete file name.
Roland McGrath's avatar
Roland McGrath committed
240 241 242 243 244 245
(defun tags-expand-table-name (file)
  (setq file (expand-file-name file))
  (if (file-directory-p file)
      (expand-file-name "TAGS" file)
    file))

246 247
;; Return the cdr of LIST (default: tags-table-list) whose car
;; is equal to FILE after tags-expand-table-name on both sides.
Roland McGrath's avatar
Roland McGrath committed
248 249 250 251 252 253 254 255 256 257
(defun tags-table-list-member (file &optional list)
  (or list
      (setq list tags-table-list))
  (setq file (tags-expand-table-name file))
  (while (and list
	      (not (string= file (tags-expand-table-name (car list)))))
    (setq list (cdr list)))
  list)

;; Subroutine of visit-tags-table-buffer.  Frobs its local vars.
258 259 260
;; Search TABLES for one that has tags for THIS-FILE.  Recurses
;; on included tables.  Returns the tail of TABLES (or of an
;; inner included list) whose car is a table listing THIS-FILE.
Roland McGrath's avatar
Roland McGrath committed
261 262
(defun tags-table-including (this-file tables &optional recursing)
  (let ((found nil))
263
    ;; Loop over TABLES, looking for one containing tags for THIS-FILE.
Roland McGrath's avatar
Roland McGrath committed
264 265 266 267
    (while (and (not found)
		tables)
      (let ((tags-file-name (tags-expand-table-name (car tables))))
	(if (or (get-file-buffer tags-file-name)
268
		(file-exists-p tags-file-name))	;XXX check all in core first.
Roland McGrath's avatar
Roland McGrath committed
269 270 271 272 273 274 275 276 277 278 279 280
	    (progn
	      ;; Select the tags table buffer and get the file list up to date.
	      (visit-tags-table-buffer 'same)
	      (or tags-table-files
		  (setq tags-table-files
			(funcall tags-table-files-function)))

	      (cond ((member this-file tags-table-files)
		     ;; Found it.
		     (setq found tables))

		    ((tags-included-tables)
281
		     ;; This table has included tables.  Check them.
Roland McGrath's avatar
Roland McGrath committed
282 283 284 285 286 287 288 289 290
		     (let ((old tags-table-parent-pointer-list))
		       (unwind-protect
			   (progn
			     (or recursing
				 ;; At top level (not in an included tags
				 ;; table), set the list to nil so we can
				 ;; collect just the elts from this run.
				 (setq tags-table-parent-pointer-list nil))
			     (setq found
291
				   ;; Recurse on the list of included tables.
Roland McGrath's avatar
Roland McGrath committed
292 293 294 295
				   (tags-table-including this-file
							 tags-included-tables
							 t))
			     (if found
296 297 298 299 300 301 302 303 304 305 306 307 308 309
				 ;; One of them lists THIS-FILE.
				 ;; Set the table list state variables to move
				 ;; us inside the list of included tables.
				 (setq tags-table-parent-pointer-list
				       (cons
					(cons tags-table-list-pointer
					      tags-table-list-started-at)
					tags-table-parent-pointer-list)
				       tags-table-list-pointer found
				       tags-table-list-started-at found
				       ;; CONT is a local variable of
				       ;; our caller, visit-tags-table-buffer.
				       ;; Set it so we won't frob lists later.
				       cont 'included)))
Roland McGrath's avatar
Roland McGrath committed
310
			 (or recursing
311 312 313 314 315 316 317 318 319
			     ;; tags-table-parent-pointer-list now describes
			     ;; the path of included tables taken by recursive
			     ;; invocations of this function.  The recursive
			     ;; calls have consed onto the front of the list,
			     ;; so it is now outermost first.  We want it
			     ;; innermost first, so reverse it.  Then append
			     ;; the old list (from before we were called the
			     ;; outermost time), to get the complete current
			     ;; state of included tables.
Roland McGrath's avatar
Roland McGrath committed
320 321 322 323 324 325
			     (setq tags-table-parent-pointer-list
				   (nconc (nreverse
					   tags-table-parent-pointer-list)
					  old))))))))))
      (setq tables (cdr tables)))
    found))
Roland McGrath's avatar
Roland McGrath committed
326 327 328 329 330

(defun visit-tags-table-buffer (&optional cont)
  "Select the buffer containing the current tags table.
If optional arg is t, visit the next table in `tags-table-list'.
If optional arg is the atom `same', don't look for a new table;
331
 just select the buffer visiting `tags-file-name'.
Roland McGrath's avatar
Roland McGrath committed
332
If arg is nil or absent, choose a first buffer from information in
333
 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
Roland McGrath's avatar
Roland McGrath committed
334
Returns t if it visits a tags table, or nil if there are no more in the list."
335 336 337 338 339 340

  ;; Set tags-file-name to the tags table file we want to visit.
  (cond ((eq cont 'same)
	 ;; Use the ambient value of tags-file-name.  Set CONT to nil so the
	 ;; code below will make sure tags-file-name is in tags-table-list.
	 (setq cont nil))
Roland McGrath's avatar
Roland McGrath committed
341 342

	(cont
343
	 ;; Find the next table.
Roland McGrath's avatar
Roland McGrath committed
344 345 346 347 348 349 350 351
	 (if (tags-next-table)
	     ;; Skip over nonexistent files.
	     (while (and (let ((file (tags-expand-table-name tags-file-name)))
			   (not (or (get-file-buffer file)
				    (file-exists-p file))))
			 (tags-next-table)))))

	(t
352
	 ;; Pick a table out of our hat.
Roland McGrath's avatar
Roland McGrath committed
353
	 (setq tags-file-name
354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379
	       (or
		;; First, try a local variable.
		(cdr (assq 'tags-file-name (buffer-local-variables)))
		;; Second, try a user-specified function to guess.
		(and default-tags-table-function
		     (funcall default-tags-table-function))
		;; Third, look for a tags table that contains
		;; tags for the current buffer's file.
		;; If one is found, the lists will be frobnicated,
		;; and CONT will be set non-nil so we don't do it below.
		(car (save-excursion (tags-table-including buffer-file-name
							   tags-table-list)))
		;; Fourth, use the user variable tags-file-name, if it is not
		;; already in tags-table-list.
		(and (not (tags-table-list-member tags-file-name))
		     tags-file-name)
		;; Fifth, use the user variable giving the table list.
		(car tags-table-list)
		;; Finally, prompt the user for a file name.
		(expand-file-name
		 (read-file-name "Visit tags table: (default TAGS) "
				 default-directory
				 "TAGS"
				 t))))))

  ;; Expand the table name into a full file name.
Roland McGrath's avatar
Roland McGrath committed
380 381
  (setq tags-file-name (tags-expand-table-name tags-file-name))

Roland McGrath's avatar
Roland McGrath committed
382
  (if (and (eq cont t) (null tags-table-list-pointer))
Roland McGrath's avatar
Roland McGrath committed
383 384 385
      ;; All out of tables.
      nil

386
    ;; Verify that tags-file-name is a valid tags table.
Roland McGrath's avatar
Roland McGrath committed
387
    (if (if (get-file-buffer tags-file-name)
388 389
	    ;; The file is already in a buffer.  Check for the visited file
	    ;; having changed since we last used it.
Roland McGrath's avatar
Roland McGrath committed
390 391 392 393 394 395 396 397 398 399 400 401 402
	    (let (win)
	      (set-buffer (get-file-buffer tags-file-name))
	      (setq win (or verify-tags-table-function
			    (initialize-new-tags-table)))
	      (if (or (verify-visited-file-modtime (current-buffer))
		      (not (yes-or-no-p
			    "Tags file has changed, read new contents? ")))
		  (and win (funcall verify-tags-table-function))
		(revert-buffer t t)
		(initialize-new-tags-table)))
	  (set-buffer (find-file-noselect tags-file-name))
	  (or (string= tags-file-name buffer-file-name)
	      ;; find-file-noselect has changed the file name.
403
	      ;; Propagate the change to tags-file-name and tags-table-list.
Roland McGrath's avatar
Roland McGrath committed
404 405 406 407 408 409 410 411 412 413 414
	      (let ((tail (member file tags-table-list)))
		(if tail
		    (setcar tail buffer-file-name))
		(setq tags-file-name buffer-file-name)))
	  (initialize-new-tags-table))

	;; We have a valid tags table.
	(progn
	  ;; Bury the tags table buffer so it
	  ;; doesn't get in the user's way.
	  (bury-buffer (current-buffer))
Roland McGrath's avatar
Roland McGrath committed
415
	
Roland McGrath's avatar
Roland McGrath committed
416 417 418 419 420 421 422 423 424 425 426 427 428 429 430
	  (if cont
	      ;; No list frobbing required.
	      nil

	    ;; Look in the list for the table we chose.
	    (let ((elt (tags-table-list-member tags-file-name)))
	      (or elt
		  ;; The table is not in the current set.
		  ;; Try to find it in another previously used set.
		  (let ((sets tags-table-set-list))
		    (while (and sets
				(not (setq elt (tags-table-list-member
						tags-file-name (car sets)))))
		      (setq sets (cdr sets)))
		    (if sets
431
			;; Found in some other set.  Switch to that set.
Roland McGrath's avatar
Roland McGrath committed
432
			(progn
Roland McGrath's avatar
Roland McGrath committed
433
			  (or (memq tags-table-list tags-table-set-list)
Roland McGrath's avatar
Roland McGrath committed
434
			      ;; Save the current list.
Roland McGrath's avatar
Roland McGrath committed
435 436 437
			      (setq tags-table-set-list
				    (cons tags-table-list
					  tags-table-set-list)))
Roland McGrath's avatar
Roland McGrath committed
438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454
			  (setq tags-table-list (car sets)))

		      ;; Not found in any existing set.
		      (if (and tags-table-list
			       (y-or-n-p (concat "Add " tags-file-name
						 " to current list"
						 " of tags tables? ")))
			  ;; Add it to the current list.
			  (setq tags-table-list (cons tags-file-name
						      tags-table-list))
			;; Make a fresh list, and store the old one.
			(or (memq tags-table-list tags-table-set-list)
			    (setq tags-table-set-list
				  (cons tags-table-list tags-table-set-list)))
			(setq tags-table-list (list tags-file-name)))
		      (setq elt tags-table-list))))

455 456
	      ;; Set the tags table list state variables to point at the table
	      ;; we want to use first.
Roland McGrath's avatar
Roland McGrath committed
457
	      (setq tags-table-list-started-at elt
Roland McGrath's avatar
Roland McGrath committed
458 459 460 461
		    tags-table-list-pointer elt)))

	  ;; Return of t says the tags table is valid.
	  t)
Roland McGrath's avatar
Roland McGrath committed
462 463

      ;; The buffer was not valid.  Don't use it again.
464 465 466 467
      (let ((file tags-file-name))
	(kill-local-variable 'tags-file-name)
	(if (eq file tags-file-name)
	    (setq tags-file-name nil)))
Roland McGrath's avatar
Roland McGrath committed
468
      (error "File %s is not a valid tags table" buffer-file-name))))
469

Jim Blandy's avatar
Jim Blandy committed
470 471
(defun file-of-tag ()
  "Return the file name of the file whose tags point is within.
Roland McGrath's avatar
Roland McGrath committed
472 473 474
Assumes the tags table is the current buffer.
File name returned is relative to tags table file's directory."
  (funcall file-of-tag-function))
Jim Blandy's avatar
Jim Blandy committed
475

Jim Blandy's avatar
Jim Blandy committed
476
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
477 478
(defun tags-table-files ()
  "Return a list of files in the current tags table.
479
Assumes the tags table is the current buffer.
Jim Blandy's avatar
Jim Blandy committed
480
File names returned are absolute."
481 482 483
  (or tags-table-files
      (setq tags-table-files
	    (funcall tags-table-files-function))))
Roland McGrath's avatar
Roland McGrath committed
484 485

(defun tags-included-tables ()
486 487
  "Return a list of tags tables included by the current table.
Assumes the tags table is the current buffer."
Roland McGrath's avatar
Roland McGrath committed
488 489 490 491 492 493 494 495 496 497 498 499 500 501
  (or tags-included-tables
      (setq tags-included-tables (funcall tags-included-tables-function))))

;; Build tags-completion-table on demand.  The single current tags table
;; and its included tags tables (and their included tables, etc.) have
;; their tags included in the completion table.
(defun tags-completion-table ()
  (or tags-completion-table
      (condition-case ()
	  (prog2
	   (message "Making tags completion table for %s..." buffer-file-name)
	   (let ((included (tags-included-tables))
		 (table (funcall tags-completion-table-function)))
	     (save-excursion
502 503
	       ;; Iterate over the list of included tables, and combine each
	       ;; included table's completion obarray to the parent obarray.
Roland McGrath's avatar
Roland McGrath committed
504
	       (while included
505
		 ;; Visit the buffer.
Roland McGrath's avatar
Roland McGrath committed
506
		 (let ((tags-file-name (car included)))
Roland McGrath's avatar
Roland McGrath committed
507
		   (visit-tags-table-buffer 'same))
508
		 ;; Recurse in that buffer to compute its completion table.
Roland McGrath's avatar
Roland McGrath committed
509
		 (if (tags-completion-table)
510
		     ;; Combine the tables.
Roland McGrath's avatar
Roland McGrath committed
511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529
		     (mapatoms (function
				(lambda (sym)
				  (intern (symbol-name sym) table)))
			       tags-completion-table))
		 (setq included (cdr included))))
	     (setq tags-completion-table table))
	   (message "Making tags completion table for %s...done"
		    buffer-file-name))
	(quit (message "Tags completion table construction aborted.")
	      (setq tags-completion-table nil)))))

;; Completion function for tags.  Does normal try-completion,
;; but builds tags-completion-table on demand.
(defun tags-complete-tag (string predicate what)
  (save-excursion
    (visit-tags-table-buffer)
    (if (eq what t)
	(all-completions string (tags-completion-table) predicate)
      (try-completion string (tags-completion-table) predicate))))
530

Jim Blandy's avatar
Jim Blandy committed
531 532 533 534 535
;; Return a default tag to search for, based on the text at point.
(defun find-tag-default ()
  (save-excursion
    (while (looking-at "\\sw\\|\\s_")
      (forward-char 1))
Roland McGrath's avatar
Roland McGrath committed
536 537 538 539 540 541 542
    (if (or (re-search-backward "\\sw\\|\\s_"
				(save-excursion (beginning-of-line) (point))
				t)
	    (re-search-forward "\\(\\sw\\|\\s_\\)+"
			       (save-excursion (end-of-line) (point))
			       t))
	(progn (goto-char (match-end 0))
Jim Blandy's avatar
Jim Blandy committed
543 544 545 546 547 548 549
	       (buffer-substring (point)
				 (progn (forward-sexp -1)
					(while (looking-at "\\s'")
					  (forward-char 1))
					(point))))
      nil)))

Roland McGrath's avatar
Roland McGrath committed
550
;; Read a tag name from the minibuffer with defaulting and completion.
Jim Blandy's avatar
Jim Blandy committed
551
(defun find-tag-tag (string)
Roland McGrath's avatar
Roland McGrath committed
552 553 554 555 556 557 558
  (let* ((default (funcall (or find-tag-default-function
			       (get major-mode 'find-tag-default-function)
			       'find-tag-default)))
	 (spec (completing-read (if default
				    (format "%s(default %s) " string default)
				  string)
				'tags-complete-tag)))
559 560 561
    (if (equal spec "")
	(or default (error "There is no default tag"))
      spec)))
Jim Blandy's avatar
Jim Blandy committed
562

Roland McGrath's avatar
Roland McGrath committed
563 564 565
(defvar last-tag nil
  "Last tag found by \\[find-tag].")

Jim Blandy's avatar
Jim Blandy committed
566
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
567 568
(defun find-tag-noselect (tagname &optional next-p regexp-p)
  "Find tag (in current tags table) whose name contains TAGNAME.
569
Returns the buffer containing the tag's definition and moves its point there,
Roland McGrath's avatar
Roland McGrath committed
570 571 572 573 574 575
but does not select the buffer.
The default for TAGNAME is the expression in the buffer near point.

If second arg NEXT-P is non-nil (interactively, with prefix arg), search
for another tag that matches the last tagname or regexp used.  When there
are multiple matches for a tag, more exact matches are found first.
Jim Blandy's avatar
Jim Blandy committed
576

Roland McGrath's avatar
Roland McGrath committed
577 578 579
If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.

See documentation of variable `tags-file-name'."
Jim Blandy's avatar
Jim Blandy committed
580 581
  (interactive (if current-prefix-arg
		   '(nil t)
582 583 584
		 (list (find-tag-tag "Find tag: "))))
  ;; Save the current buffer's value of `find-tag-hook' before selecting the
  ;; tags table buffer.
Roland McGrath's avatar
Roland McGrath committed
585
  (let ((local-find-tag-hook find-tag-hook))
Roland McGrath's avatar
Roland McGrath committed
586
    (if next-p
587
	;; Find the same table we last used.
Roland McGrath's avatar
Roland McGrath committed
588
	(visit-tags-table-buffer 'same)
589 590 591 592
      ;; Pick a table to use.
      (visit-tags-table-buffer)
      ;; Record TAGNAME for a future call with NEXT-P non-nil.
      (setq last-tag tagname))
Roland McGrath's avatar
Roland McGrath committed
593
    (prog1
594
	;; find-tag-in-order does the real work.
Roland McGrath's avatar
Roland McGrath committed
595 596 597 598 599 600 601 602 603 604 605 606 607
	(find-tag-in-order (if next-p last-tag tagname)
			   (if regexp-p
			       find-tag-regexp-search-function
			     find-tag-search-function)
			   (if regexp-p
			       find-tag-regexp-tag-order
			     find-tag-tag-order)
			   (if regexp-p
			       find-tag-regexp-next-line-after-failure-p
			     find-tag-next-line-after-failure-p)
			   (if regexp-p "matching" "containing")
			   (not next-p))
      (run-hooks 'local-find-tag-hook))))
Jim Blandy's avatar
Jim Blandy committed
608

Jim Blandy's avatar
Jim Blandy committed
609
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
610 611 612 613
(defun find-tag (tagname &optional next-p)
  "Find tag (in current tags table) whose name contains TAGNAME.
Select the buffer containing the tag's definition, and move point there.
The default for TAGNAME is the expression in the buffer around or before point.
Jim Blandy's avatar
Jim Blandy committed
614

Roland McGrath's avatar
Roland McGrath committed
615 616 617
If second arg NEXT-P is non-nil (interactively, with prefix arg), search
for another tag that matches the last tagname used.  When there are
multiple matches, more exact matches are found first.
Jim Blandy's avatar
Jim Blandy committed
618

Roland McGrath's avatar
Roland McGrath committed
619
See documentation of variable `tags-file-name'."
Jim Blandy's avatar
Jim Blandy committed
620 621
  (interactive (if current-prefix-arg
		   '(nil t)
622
		 (list (find-tag-tag "Find tag: "))))
Roland McGrath's avatar
Roland McGrath committed
623 624
  (switch-to-buffer (find-tag-noselect tagname next-p)))
;;;###autoload (define-key esc-map "." 'find-tag)
Jim Blandy's avatar
Jim Blandy committed
625

Jim Blandy's avatar
Jim Blandy committed
626
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
627 628 629 630 631 632 633 634 635 636 637 638 639
(defun find-tag-other-window (tagname &optional next-p)
  "Find tag (in current tags table) whose name contains TAGNAME.
Select the buffer containing the tag's definition
in another window, and move point there.
The default for TAGNAME is the expression in the buffer around or before point.

If second arg NEXT-P is non-nil (interactively, with prefix arg), search
for another tag that matches the last tagname used.  When there are
multiple matches, more exact matches are found first.

See documentation of variable `tags-file-name'."
  (interactive (if current-prefix-arg
		   '(nil t)
640 641 642 643 644 645 646 647 648 649 650
		 (list (find-tag-tag "Find tag other window: "))))
  ;; This hair is to deal with the case where the tag is found in the
  ;; selected window's buffer; without the hair, point is moved in both
  ;; windows.  To prevent this, we save the selected window's point before
  ;; doing find-tag-noselect, and restore it after.
  (let* ((window-point (window-point (selected-window)))
	 (tagbuf (find-tag-noselect tagname next-p)))
    (set-window-point (prog1
			  (selected-window)
			(switch-to-buffer-other-window tagbuf))
		      window-point)))
Roland McGrath's avatar
Roland McGrath committed
651 652
;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)

Roland McGrath's avatar
Roland McGrath committed
653
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
654
(defun find-tag-other-frame (tagname &optional next-p)
Jim Blandy's avatar
Jim Blandy committed
655 656 657 658 659
  "Find tag (in current tag table) whose name contains TAGNAME.
 Selects the buffer that the tag is contained in in another frame
and puts point at its definition.
 If TAGNAME is a null string, the expression in the buffer
around or before point is used as the tag name.
Roland McGrath's avatar
Roland McGrath committed
660
 If second arg NEXT-P is non-nil (interactively, with prefix arg),
Jim Blandy's avatar
Jim Blandy committed
661 662 663
searches for the next tag in the tag table
that matches the tagname used in the previous find-tag.

Roland McGrath's avatar
Roland McGrath committed
664
See documentation of variable `tags-file-name'."
Jim Blandy's avatar
Jim Blandy committed
665 666
  (interactive (if current-prefix-arg
		   '(nil t)
667
		 (list (find-tag-tag "Find tag other window: "))))
Roland McGrath's avatar
Roland McGrath committed
668 669 670 671
  (let ((pop-up-frames t))
    (find-tag-other-window tagname next-p)))
;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)

Jim Blandy's avatar
Jim Blandy committed
672
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
673 674 675
(defun find-tag-regexp (regexp &optional next-p other-window)
  "Find tag (in current tags table) whose name matches REGEXP.
Select the buffer containing the tag's definition and move point there.
Jim Blandy's avatar
Jim Blandy committed
676

Roland McGrath's avatar
Roland McGrath committed
677 678 679 680 681 682 683 684
If second arg NEXT-P is non-nil (interactively, with prefix arg), search
for another tag that matches the last tagname used.

If third arg OTHER-WINDOW is non-nil, select the buffer in another window.

See documentation of variable `tags-file-name'."
  (interactive (if current-prefix-arg
		   '(nil t)
685
		 (list (read-string "Find tag regexp: "))))
Roland McGrath's avatar
Roland McGrath committed
686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707
  (funcall (if other-window 'switch-to-buffer-other-window 'switch-to-buffer)
	   (find-tag-noselect regexp next-p t)))

;; Internal tag finding function.

;; PATTERN is a string to pass to second arg SEARCH-FORWARD-FUNC, and to
;; any member of the function list ORDER (third arg).  If ORDER is nil,
;; use saved state to continue a previous search.

;; Fourth arg MATCHING is a string, an English '-ing' word, to be used in
;; an error message.

;; Fifth arg NEXT-LINE-AFTER-FAILURE-P is non-nil if after a failed match,
;; point should be moved to the next line.

;; Algorithm is as follows.  For each qualifier-func in ORDER, go to
;; beginning of tags file, and perform inner loop: for each naive match for
;; PATTERN found using SEARCH-FORWARD-FUNC, qualify the naive match using
;; qualifier-func.  If it qualifies, go to the specified line in the
;; specified source file and return.  Qualified matches are remembered to
;; avoid repetition.  State is saved so that the loop can be continued.

708 709 710 711 712 713
(defun find-tag-in-order (pattern
			  search-forward-func
			  order
			  next-line-after-failure-p
			  matching
			  first-search)
Roland McGrath's avatar
Roland McGrath committed
714 715 716 717 718 719 720 721
  (let (file				;name of file containing tag
	tag-info			;where to find the tag in FILE
	tags-table-file			;name of tags file
	(first-table t)
	(tag-order order)
	goto-func
	)
    (save-excursion
Roland McGrath's avatar
Roland McGrath committed
722 723 724
      (or first-search			;find-tag-noselect has already done it.
	  (visit-tags-table-buffer 'same))

Roland McGrath's avatar
Roland McGrath committed
725 726
      ;; Get a qualified match.
      (catch 'qualified-match-found
Roland McGrath's avatar
Roland McGrath committed
727

728
	;; Iterate over the list of tags tables.
Roland McGrath's avatar
Roland McGrath committed
729 730 731 732 733 734
	(while (or first-table
		   (visit-tags-table-buffer t))

	  (if first-search
	      (setq tag-lines-already-matched nil))

Roland McGrath's avatar
Roland McGrath committed
735 736 737 738
	  (and first-search first-table
	       ;; Start at beginning of tags file.
	       (goto-char (point-min)))
	  (setq first-table nil)
Roland McGrath's avatar
Roland McGrath committed
739 740

	  (setq tags-table-file buffer-file-name)
741
	  ;; Iterate over the list of ordering predicates.
Roland McGrath's avatar
Roland McGrath committed
742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768
	  (while order
	    (while (funcall search-forward-func pattern nil t)
	      ;; Naive match found.  Qualify the match.
	      (and (funcall (car order) pattern)
		   ;; Make sure it is not a previous qualified match.
		   ;; Use of `memq' depends on numbers being eq.
		   (not (memq (save-excursion (beginning-of-line) (point))
			      tag-lines-already-matched))
		   (throw 'qualified-match-found nil))
	      (if next-line-after-failure-p
		  (forward-line 1)))
	    ;; Try the next flavor of match.
	    (setq order (cdr order))
	    (goto-char (point-min)))
	  (setq order tag-order))
	;; We throw out on match, so only get here if there were no matches.
	(error "No %stags %s %s" (if first-search "" "more ")
	       matching pattern))
      
      ;; Found a tag; extract location info.
      (beginning-of-line)
      (setq tag-lines-already-matched (cons (point)
					    tag-lines-already-matched))
      ;; Expand the filename, using the tags table buffer's default-directory.
      (setq file (expand-file-name (file-of-tag))
	    tag-info (funcall snarf-tag-function))

769
      ;; Get the local value in the tags table buffer before switching buffers.
Roland McGrath's avatar
Roland McGrath committed
770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788
      (setq goto-func goto-tag-location-function)

      ;; Find the right line in the specified file.
      (set-buffer (find-file-noselect file))
      (widen)
      (push-mark)
      (funcall goto-func tag-info)
      
      ;; Give this buffer a local value of tags-file-name.
      ;; The next time visit-tags-table-buffer is called,
      ;; it will use the same tags table that found a match in this buffer.
      (make-local-variable 'tags-file-name)
      (setq tags-file-name tags-table-file)
      
      ;; Return the buffer where the tag was found.
      (current-buffer))))

;; `etags' TAGS file format support.

789 790
;; If the current buffer is a valid etags TAGS file, give it local values of
;; the tags table format variables, and return non-nil.
Roland McGrath's avatar
Roland McGrath committed
791
(defun etags-recognize-tags-table ()
792
  (and (etags-verify-tags-table)
Jim Blandy's avatar
Jim Blandy committed
793 794 795
       ;; It is annoying to flash messages on the screen briefly,
       ;; and this message is not useful.  -- rms
       ;; (message "%s is an `etags' TAGS file" buffer-file-name)
Roland McGrath's avatar
Roland McGrath committed
796
       (mapcar (function (lambda (elt)
797
			   (set (make-local-variable (car elt)) (cdr elt))))
Roland McGrath's avatar
Roland McGrath committed
798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815
	       '((file-of-tag-function . etags-file-of-tag)
		 (tags-table-files-function . etags-tags-table-files)
		 (tags-completion-table-function . etags-tags-completion-table)
		 (snarf-tag-function . etags-snarf-tag)
		 (goto-tag-location-function . etags-goto-tag-location)
		 (find-tag-regexp-search-function . re-search-forward)
		 (find-tag-regexp-tag-order . (tag-re-match-p))
		 (find-tag-regexp-next-line-after-failuire-p . t)
		 (find-tag-search-function . search-forward)
		 (find-tag-tag-order . (tag-exact-match-p tag-word-match-p
							  tag-any-match-p))
		 (find-tag-next-line-after-failure-p . nil)
		 (list-tags-function . etags-list-tags)
		 (tags-apropos-function . etags-tags-apropos)
		 (tags-included-tables-function . etags-tags-included-tables)
		 (verify-tags-table-function . etags-verify-tags-table)
		 ))))

816
;; Return non-nil iff the current buffer is a valid etags TAGS file.
Roland McGrath's avatar
Roland McGrath committed
817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940
(defun etags-verify-tags-table ()
  (= (char-after 1) ?\f))

(defun etags-file-of-tag ()
  (save-excursion
    (search-backward "\f\n")
    (forward-char 2)
    (buffer-substring (point)
		      (progn (skip-chars-forward "^,") (point)))))

(defun etags-tags-completion-table ()
  (let ((table (make-vector 511 0)))
    (save-excursion
      (goto-char (point-min))
      (while (search-forward "\177" nil t)
	;; Handle multiple \177's on a line.
	(save-excursion
	  (skip-chars-backward "^-A-Za-z0-9_$\n") ;sym syntax? XXX
	  (or (bolp)
	      (intern (buffer-substring
		       (point)
		       (progn
			 (skip-chars-backward "-A-Za-z0-9_$")
			 ;; ??? New
			 ;; `::' in the middle of a C++ tag.
			 (and (= (preceding-char) ?:)
			      (= (char-after (- (point) 2)) ?:)
			      (progn
				(backward-char 2)
				(skip-chars-backward
				 "-A-Za-z0-9_$")))
			 (point)))
		      table)))))
    table))

(defun etags-snarf-tag ()
  (let (tag-text startpos)
    (search-forward "\177")
    (setq tag-text (buffer-substring (1- (point))
				     (save-excursion (beginning-of-line)
						     (point))))
    (search-forward ",")
    (setq startpos (string-to-int (buffer-substring
				   (point)
				   (progn (skip-chars-forward "0-9")
					  (point)))))
    ;; Leave point on the next line of the tags file.
    (forward-line 1)
    (cons tag-text startpos)))

(defun etags-goto-tag-location (tag-info)
  (let ((startpos (cdr tag-info))
	;; This constant is 1/2 the initial search window.
	;; There is no sense in making it too small,
	;; since just going around the loop once probably
	;; costs about as much as searching 2000 chars.
	(offset 1000)
	(found nil)
	(pat (concat "^" (regexp-quote (car tag-info)))))
    (or startpos
	(setq startpos (point-min)))
    (while (and (not found)
		(progn
		  (goto-char (- startpos offset))
		  (not (bobp))))
      (setq found
	    (re-search-forward pat (+ startpos offset) t)
	    offset (* 3 offset)))	; expand search window
    (or found
	(re-search-forward pat nil t)
	(error "`%s' not found in %s; time to rerun etags"
	       pat buffer-file-name)))
  (beginning-of-line))

(defun etags-list-tags (file)
  (goto-char 1)
  (if (not (search-forward (concat "\f\n" file ",") nil t))
      nil
    (forward-line 1)
    (while (not (or (eobp) (looking-at "\f")))
      (princ (buffer-substring (point)
			       (progn (skip-chars-forward "^\177")
				      (point))))
      (terpri)
      (forward-line 1))))

(defun etags-tags-apropos (string)
  (goto-char 1)
  (while (re-search-forward string nil t)
    (beginning-of-line)
    (princ (buffer-substring (point)
			     (progn (skip-chars-forward "^\177")
				    (point))))
    (terpri)
    (forward-line 1)))

(defun etags-tags-table-files ()
  (let ((files nil)
	beg)
    (goto-char (point-min))
    (while (search-forward "\f\n" nil t)
      (setq beg (point))
      (skip-chars-forward "^,\n")
      (or (looking-at ",include$")
	  ;; Expand in the default-directory of the tags table buffer.
	  (setq files (cons (expand-file-name (buffer-substring beg (point)))
			    files))))
    (nreverse files)))

(defun etags-tags-included-tables ()
  (let ((files nil)
	beg)
    (goto-char (point-min))
    (while (search-forward "\f\n" nil t)
      (setq beg (point))
      (skip-chars-forward "^,\n")
      (if (looking-at ",include$")
	  ;; Expand in the default-directory of the tags table buffer.
	  (setq files (cons (expand-file-name (buffer-substring beg (point)))
			    files))))
    (nreverse files)))

;; Empty tags file support.

941 942
;; Recognize an empty file and give it local values of the tags table format
;; variables which do nothing.
Roland McGrath's avatar
Roland McGrath committed
943 944 945
(defun recognize-empty-tags-table ()
  (and (zerop (buffer-size))
       (mapcar (function (lambda (sym)
946
			   (set (make-local-variable sym) 'ignore)))
Roland McGrath's avatar
Roland McGrath committed
947 948 949 950 951 952 953 954 955 956 957
	       '(tags-table-files-function
		 tags-completion-table-function
		 find-tag-regexp-search-function
		 find-tag-search-function
		 tags-apropos-function
		 tags-included-tables-function))
       (set (make-local-variable 'verify-tags-table-function)
	    (function (lambda ()
			(zerop (buffer-size)))))))

;;; Match qualifier functions for tagnames.
958
;;; XXX these functions assume etags file format.
Roland McGrath's avatar
Roland McGrath committed
959

Roland McGrath's avatar
Roland McGrath committed
960 961 962 963 964 965 966 967 968 969 970 971 972 973 974
;; This might be a neat idea, but it's too hairy at the moment.
;;(defmacro tags-with-syntax (&rest body)
;;  (` (let ((current (current-buffer))
;;	   (otable (syntax-table))
;;	   (buffer (find-file-noselect (file-of-tag)))
;;	   table)
;;       (unwind-protect
;;	   (progn
;;	     (set-buffer buffer)
;;	     (setq table (syntax-table))
;;	     (set-buffer current)
;;	     (set-syntax-table table)
;;	     (,@ body))
;;	 (set-syntax-table otable)))))
;;(put 'tags-with-syntax 'edebug-form-spec '(&rest form))
Roland McGrath's avatar
Roland McGrath committed
975

Roland McGrath's avatar
Roland McGrath committed
976 977
;; t if point is at a tag line that matches TAG "exactly".
;; point should be just after a string that matches TAG.
Roland McGrath's avatar
Roland McGrath committed
978 979 980 981 982
(defun tag-exact-match-p (tag)
  (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177") ;not a symbol char
       (save-excursion
	 (backward-char (1+ (length tag)))
	 (and (looking-at "\\Sw") (looking-at "\\S_")))))
Roland McGrath's avatar
Roland McGrath committed
983 984 985 986 987 988 989 990 991 992 993

;; t if point is at a tag line that matches TAG as a word.
;; point should be just after a string that matches TAG.
(defun tag-word-match-p (tag)
  (and (looking-at "\\b.*\177")
       (save-excursion (backward-char (1+ (length tag)))
		       (looking-at "\\b"))))

;; t if point is in a tag line with a tag containing TAG as a substring.
(defun tag-any-match-p (tag)
  (looking-at ".*\177"))
Jim Blandy's avatar
Jim Blandy committed
994

Roland McGrath's avatar
Roland McGrath committed
995 996 997 998 999 1000 1001 1002
;; t if point is at a tag line that matches RE as a regexp.
(defun tag-re-match-p (re)
  (save-excursion
    (beginning-of-line)
    (let ((bol (point)))
      (and (search-forward "\177" (save-excursion (end-of-line) (point)) t)
	   (re-search-backward re bol t)))))

Jim Blandy's avatar
Jim Blandy committed
1003
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
1004 1005 1006 1007 1008 1009 1010 1011 1012 1013
(defun next-file (&optional initialize novisit)
  "Select next file among files in current tags table.
Non-nil first argument (prefix arg, if interactive)
initializes to the beginning of the list of files in the tags table.

Non-nil second argument NOVISIT means use a temporary buffer
 to save time and avoid uninteresting warnings.

Value is nil if the file was already visited;
if the file was newly read in, the value is the filename."
Jim Blandy's avatar
Jim Blandy committed
1014
  (interactive "P")
Roland McGrath's avatar
Roland McGrath committed
1015 1016
  (and initialize
       (save-excursion
1017
	 ;; Visit the tags table buffer to get its list of files.
Roland McGrath's avatar
Roland McGrath committed
1018
	 (visit-tags-table-buffer)
Roland McGrath's avatar
Roland McGrath committed
1019
	 (setq next-file-list (tags-table-files))))
Jim Blandy's avatar
Jim Blandy committed
1020
  (or next-file-list
Roland McGrath's avatar
Roland McGrath committed
1021
      (save-excursion
1022
	;; Get the files from the next tags table.
Roland McGrath's avatar
Roland McGrath committed
1023
	;; When doing (visit-tags-table-buffer t),
Roland McGrath's avatar
Roland McGrath committed
1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042
	;; the tags table buffer must be current.
	(if (and (visit-tags-table-buffer 'same)
		 (visit-tags-table-buffer t))
	    (setq next-file-list (tags-table-files))
	  (and novisit
	       (get-buffer " *next-file*")
	       (kill-buffer " *next-file*"))
	  (error "All files processed."))))
  (let ((new (not (get-file-buffer (car next-file-list)))))
    (if (not (and new novisit))
	(set-buffer (find-file-noselect (car next-file-list) novisit))
      ;; Like find-file, but avoids random warning messages.
      (set-buffer (get-buffer-create " *next-file*"))
      (kill-all-local-variables)
      (erase-buffer)
      (setq new (car next-file-list))
      (insert-file-contents new nil))
    (setq next-file-list (cdr next-file-list))
    new))
Jim Blandy's avatar
Jim Blandy committed
1043

Roland McGrath's avatar
Roland McGrath committed
1044 1045 1046 1047 1048 1049 1050
(defvar tags-loop-operate nil
  "Form for `tags-loop-continue' to eval to change one file.")

(defvar tags-loop-scan nil
  "Form for `tags-loop-continue' to eval to scan one file.
If it returns non-nil, this file needs processing by evalling
\`tags-loop-operate'.  Otherwise, move on to the next file.")
Jim Blandy's avatar
Jim Blandy committed
1051

Jim Blandy's avatar
Jim Blandy committed
1052
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
1053 1054
(defun tags-loop-continue (&optional first-time)
  "Continue last \\[tags-search] or \\[tags-query-replace] command.
Roland McGrath's avatar
Roland McGrath committed
1055 1056 1057 1058 1059 1060
Used noninteractively with non-nil argument to begin such a command.
Two variables control the processing we do on each file:
the value of `tags-loop-scan' is a form to be executed on each file
to see if it is interesting (it returns non-nil if so)
and `tags-loop-operate' is a form to execute to operate on an interesting file
If the latter returns non-nil, we exit; otherwise we scan the next file."
Jim Blandy's avatar
Jim Blandy committed
1061
  (interactive)
Roland McGrath's avatar
Roland McGrath committed
1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098
  (let (new
	(messaged nil))
    (while
	(progn
	  ;; Scan files quickly for the first or next interesting one.
	  (while (or first-time
		     (save-restriction
		       (widen)
		       (not (eval tags-loop-scan))))
	    (setq new (next-file first-time t))
	    ;; If NEW is non-nil, we got a temp buffer,
	    ;; and NEW is the file name.
	    (if (or messaged
		    (and (not first-time)
			 (> baud-rate search-slow-speed)
			 (setq messaged t)))
		(message "Scanning file %s..." (or new buffer-file-name)))
	    (setq first-time nil)
	    (goto-char (point-min)))

	  ;; If we visited it in a temp buffer, visit it now for real.
	  (if new
	      (let ((pos (point)))
		(erase-buffer)
		(set-buffer (find-file-noselect new))
		(widen)
		(goto-char pos)))

	  (switch-to-buffer (current-buffer))

	  ;; Now operate on the file.
	  ;; If value is non-nil, continue to scan the next file.
	  (eval tags-loop-operate)))
    (and messaged
	 (null tags-loop-operate)
	 (message "Scanning file %s...found" buffer-file-name))))
;;;###autoload (define-key esc-map "," 'tags-loop-continue)
Jim Blandy's avatar
Jim Blandy committed
1099

Jim Blandy's avatar
Jim Blandy committed
1100
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
1101
(defun tags-search (regexp)
Roland McGrath's avatar
Roland McGrath committed
1102
  "Search through all files listed in tags table for match for REGEXP.
Jim Blandy's avatar
Jim Blandy committed
1103 1104 1105
Stops when a match is found.
To continue searching for next match, use command \\[tags-loop-continue].

Roland McGrath's avatar
Roland McGrath committed
1106
See documentation of variable `tags-file-name'."
Jim Blandy's avatar
Jim Blandy committed
1107 1108
  (interactive "sTags search (regexp): ")
  (if (and (equal regexp "")
Roland McGrath's avatar
Roland McGrath committed
1109
	   (eq (car tags-loop-scan) 're-search-forward)
1110
	   (null tags-loop-operate))
Roland McGrath's avatar
Roland McGrath committed
1111
      ;; Continue last tags-search as if by M-,.
Jim Blandy's avatar
Jim Blandy committed
1112
      (tags-loop-continue nil)
Roland McGrath's avatar
Roland McGrath committed
1113 1114 1115
    (setq tags-loop-scan
	  (list 're-search-forward regexp nil t)
	  tags-loop-operate nil)
Jim Blandy's avatar
Jim Blandy committed
1116 1117
    (tags-loop-continue t)))

Jim Blandy's avatar
Jim Blandy committed
1118
;;;###autoload
Jim Blandy's avatar
Jim Blandy committed
1119
(defun tags-query-replace (from to &optional delimited)
Roland McGrath's avatar
Roland McGrath committed
1120
  "Query-replace-regexp FROM with TO through all files listed in tags table.
Jim Blandy's avatar
Jim Blandy committed
1121
Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
Roland McGrath's avatar
Roland McGrath committed
1122
If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
Jim Blandy's avatar
Jim Blandy committed
1123 1124
with the command \\[tags-loop-continue].

Roland McGrath's avatar
Roland McGrath committed
1125 1126 1127
See documentation of variable `tags-file-name'."
  (interactive
   "sTags query replace (regexp): \nsTags query replace %s by: \nP")
Roland McGrath's avatar
Roland McGrath committed
1128 1129 1130 1131 1132 1133
  (setq tags-loop-scan (list 'prog1
			     (list 'if (list 're-search-forward form nil t)
				   ;; When we find a match, move back
				   ;; to the beginning of it so perform-replace
				   ;; will see it.
				   '(goto-char (match-beginning 0))))
Roland McGrath's avatar
Roland McGrath committed
1134
	tags-loop-operate (list 'perform-replace from to t t delimited))
Jim Blandy's avatar
Jim Blandy committed
1135
  (tags-loop-continue t))
Roland McGrath's avatar
Roland McGrath committed
1136

Jim Blandy's avatar
Jim Blandy committed
1137
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
1138
(defun list-tags (file)
Jim Blandy's avatar
Jim Blandy committed
1139
  "Display list of tags in file FILE.
Roland McGrath's avatar
Roland McGrath committed
1140 1141 1142 1143
FILE should not contain a directory specification
unless it has one in the tags table."
  (interactive (list (completing-read "List tags in file: " nil
				      'tags-table-files t nil)))
Jim Blandy's avatar
Jim Blandy committed
1144 1145
  (with-output-to-temp-buffer "*Tags List*"
    (princ "Tags in file ")
Roland McGrath's avatar
Roland McGrath committed
1146
    (princ file)
Jim Blandy's avatar
Jim Blandy committed
1147 1148
    (terpri)
    (save-excursion
Roland McGrath's avatar
Roland McGrath committed
1149 1150
      (let ((first-time t)
	    (gotany nil))
Roland McGrath's avatar
Roland McGrath committed
1151
	(while (visit-tags-table-buffer (not first-time))
Roland McGrath's avatar
Roland McGrath committed
1152 1153 1154 1155
	  (if (funcall list-tags-function file)
	      (setq gotany t)))
	(or gotany
	    (error "File %s not in current tags tables"))))))
Jim Blandy's avatar
Jim Blandy committed
1156

Jim Blandy's avatar
Jim Blandy committed
1157
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
1158 1159 1160
(defun tags-apropos (regexp)
  "Display list of all tags in tags table REGEXP matches."
  (interactive "sTags apropos (regexp): ")
Jim Blandy's avatar
Jim Blandy committed
1161 1162
  (with-output-to-temp-buffer "*Tags List*"
    (princ "Tags matching regexp ")
Roland McGrath's avatar
Roland McGrath committed
1163
    (prin1 regexp)
Jim Blandy's avatar
Jim Blandy committed
1164 1165
    (terpri)
    (save-excursion
Roland McGrath's avatar
Roland McGrath committed
1166 1167 1168
      (let ((first-time t))
	(while (visit-tags-table-buffer (not first-time))
	  (setq first-time nil)
Roland McGrath's avatar
Roland McGrath committed
1169
	  (funcall tags-apropos-function regexp))))))
Roland McGrath's avatar
Roland McGrath committed
1170 1171

;;; XXX Kludge interface.
Jim Blandy's avatar
Jim Blandy committed
1172

Roland McGrath's avatar
Roland McGrath committed
1173
;; XXX If a file is in multiple tables, selection may get the wrong one.
Roland McGrath's avatar
Roland McGrath committed
1174
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 1240 1241 1242 1243 1244
(defun select-tags-table ()
  "Select a tags table file from a menu of those you have already used.
The list of tags tables to select from is stored in `tags-table-file-list';
see the doc of that variable if you want to add names to the list."
  (interactive)
  (pop-to-buffer "*Tags Table List*")
  (setq buffer-read-only nil)
  (erase-buffer)
  (setq selective-display t
	selective-display-ellipses nil)
  (let ((set-list tags-table-set-list)
	(desired-point nil))
    (if tags-table-list
	(progn
	  (setq desired-point (point-marker))
	  (princ tags-table-list (current-buffer))
	  (insert "\C-m")
	  (prin1 (car tags-table-list) (current-buffer)) ;invisible
	  (insert "\n")))
    (while set-list
      (if (eq (car set-list) tags-table-list)
	  ;; Already printed it.
	  ()
	(princ (car set-list) (current-buffer))
	(insert "\C-m")
	(prin1 (car (car set-list)) (current-buffer)) ;invisible
	(insert "\n"))
      (setq set-list (cdr set-list)))
    (if tags-file-name
	(progn
	  (or desired-point
	      (setq desired-point (point-marker)))
	  (insert tags-file-name "\C-m")
	  (prin1 tags-file-name (current-buffer)) ;invisible
	  (insert "\n")))
    (setq set-list (delete tags-file-name
			   (apply 'nconc (cons tags-table-list
					       (mapcar 'copy-sequence
						       tags-table-set-list)))))
    (while set-list
      (insert (car set-list) "\C-m")
      (prin1 (car set-list) (current-buffer)) ;invisible
      (insert "\n")
      (setq set-list (delete (car set-list) set-list)))
    (goto-char 1)
    (insert-before-markers
     "Type `t' to select a tags table or set of tags tables:\n\n")
    (if desired-point
	(goto-char desired-point))
    (set-window-start (selected-window) 1 t))
  (set-buffer-modified-p nil)
  (setq buffer-read-only t
	mode-name "Select Tags Table")
  (let ((map (make-sparse-keymap)))
    (define-key map "t" 'select-tags-table-select)
    (define-key map " " 'next-line)
    (define-key map "\^?" 'previous-line)
    (define-key map "n" 'next-line)
    (define-key map "p" 'previous-line)
    (define-key map "q" 'select-tags-table-quit)
    (use-local-map map)))
  
(defun select-tags-table-select ()
  "Select the tags table named on this line."
  (interactive)
  (search-forward "\C-m")
  (let ((name (read (current-buffer))))
    (visit-tags-table name)
    (select-tags-table-quit)
    (message "Tags table now %s" name)))
Jim Blandy's avatar
Jim Blandy committed
1245

Roland McGrath's avatar
Roland McGrath committed
1246 1247 1248 1249 1250 1251