etags.el 64.2 KB
Newer Older
1
;;; etags.el --- etags facility for Emacs
2
;; Copyright (C) 1985, 1986, 1988, 1989, 1992, 1993, 1994, 1995, 1996, 1998
3
;;	Free Software Foundation, Inc.
Jim Blandy's avatar
Jim Blandy committed
4

Karl Heuer's avatar
Karl Heuer committed
5
;; Author: Roland McGrath <roland@gnu.org>
Eric S. Raymond's avatar
Eric S. Raymond committed
6 7
;; 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
;; 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
Erik Naggum's avatar
Erik Naggum committed
21 22 23
;; along with GNU Emacs; see the file COPYING.  If not, write to the
;; Free Software Foundation, Inc., 59 Temple Place - Suite 330,
;; Boston, MA 02111-1307, USA.
Jim Blandy's avatar
Jim Blandy committed
24

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

27 28
(require 'ring)

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

38
(defgroup etags nil "Tags tables"
Richard M. Stallman's avatar
Richard M. Stallman committed
39 40
  :group 'tools)

Roland McGrath's avatar
Roland McGrath committed
41
;;;###autoload
42
;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
Richard M. Stallman's avatar
Richard M. Stallman committed
43
(defcustom tags-table-list nil
44 45 46 47
  "*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'.
Richard M. Stallman's avatar
Richard M. Stallman committed
48 49 50
Use the `etags' program to make a tags table file."
  :group 'etags
  :type '(repeat file))
Roland McGrath's avatar
Roland McGrath committed
51

52
;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
53
(defcustom tags-add-tables 'ask-user
54 55 56
  "*Control whether to add a new tags table to the current list.
t means do; nil means don't (always start a new list).
Any other value means ask the user whether to add a new tags table
Richard M. Stallman's avatar
Richard M. Stallman committed
57 58 59 60
to the current list (as opposed to starting a new list)."
  :group 'etags
  :type '(choice (const :tag "Do" t)
		 (const :tag "Don't" nil)
61
		 (other :tag "Ask" ask-user)))
Richard M. Stallman's avatar
Richard M. Stallman committed
62 63 64 65 66

(defcustom tags-revert-without-query nil
  "*Non-nil means reread a TAGS table without querying, if it has changed."
  :group 'etags
  :type 'boolean)
67

68 69 70 71 72 73 74 75 76 77 78 79 80 81
(defvar tags-table-computed-list nil
  "List of tags tables to search, computed from `tags-table-list'.
This includes tables implicitly included by other tables.  The list is not
always complete: the included tables of a table are not known until that
table is read into core.  An element that is `t' is a placeholder
indicating that the preceding element is a table that has not been read
into core and might contain included tables to search.
See `tags-table-check-computed-list'.")

(defvar tags-table-computed-list-for nil
  "Value of `tags-table-list' that `tags-table-computed-list' corresponds to.
If `tags-table-list' changes, `tags-table-computed-list' is thrown away and
recomputed; see `tags-table-check-computed-list'.")

Roland McGrath's avatar
Roland McGrath committed
82
(defvar tags-table-list-pointer nil
83
  "Pointer into `tags-table-computed-list' for the current state of searching.
Roland McGrath's avatar
Roland McGrath committed
84 85 86
Use `visit-tags-table-buffer' to cycle through tags tables in this list.")

(defvar tags-table-list-started-at nil
87
  "Pointer into `tags-table-computed-list', where the current search started.")
Roland McGrath's avatar
Roland McGrath committed
88 89 90 91 92 93

(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
Richard M. Stallman's avatar
Richard M. Stallman committed
94
(defcustom find-tag-hook nil
Roland McGrath's avatar
Roland McGrath committed
95 96
  "*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,
Richard M. Stallman's avatar
Richard M. Stallman committed
97 98 99
not the value in the buffer \\[find-tag] goes to."
  :group 'etags
  :type 'hook)
Roland McGrath's avatar
Roland McGrath committed
100 101

;;;###autoload
Richard M. Stallman's avatar
Richard M. Stallman committed
102
(defcustom find-tag-default-function nil
Roland McGrath's avatar
Roland McGrath committed
103 104
  "*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
105
has a `find-tag-default-function' property (see `put'), that is used.
Richard M. Stallman's avatar
Richard M. Stallman committed
106 107 108
Otherwise, `find-tag-default' is used."
  :group 'etags
  :type 'function)
Roland McGrath's avatar
Roland McGrath committed
109

110 111 112
(defcustom find-tag-marker-ring-length 16
  "*Length of marker rings `find-tag-marker-ring' and `tags-location-ring'."
  :group 'etags
Dan Nicolaescu's avatar
Dan Nicolaescu committed
113 114
  :type 'integer
  :version "20.3")
115 116 117 118

(defvar find-tag-marker-ring (make-ring find-tag-marker-ring-length)
  "Ring of markers which are locations from which \\[find-tag] was invoked.")

Roland McGrath's avatar
Roland McGrath committed
119
(defvar default-tags-table-function nil
120 121 122
  "If non-nil, a function to choose a default tags file for a buffer.
This function receives no arguments and should return the default
tags table file to use for the current buffer.")
123

124 125
(defvar tags-location-ring (make-ring find-tag-marker-ring-length)
  "Ring of markers which are locations visited by \\[find-tag].
126
Pop back to the last location with \\[negative-argument] \\[find-tag].")
Roland McGrath's avatar
Roland McGrath committed
127 128 129

;; Tags table state.
;; These variables are local in tags table buffers.
Jim Blandy's avatar
Jim Blandy committed
130

Roland McGrath's avatar
Roland McGrath committed
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146
(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
147
				  recognize-empty-tags-table)
148 149
  "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,
Roland McGrath's avatar
Roland McGrath committed
150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182
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
183
  "Function to return t iff current buffer contains valid tags file.")
Roland McGrath's avatar
Roland McGrath committed
184

185 186 187 188
;; 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
189
(defun initialize-new-tags-table ()
190 191 192
  (set (make-local-variable 'tags-table-files) nil)
  (set (make-local-variable 'tags-completion-table) nil)
  (set (make-local-variable 'tags-included-tables) nil)
193 194 195 196
  ;; We used to initialize find-tag-marker-ring and tags-location-ring
  ;; here, to new empty rings.  But that is wrong, because those
  ;; are global.

Roland McGrath's avatar
Roland McGrath committed
197 198 199 200 201 202
  ;; 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
203

Jim Blandy's avatar
Jim Blandy committed
204
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
205 206
(defun visit-tags-table (file &optional local)
  "Tell tags commands to use tags table file FILE.
Jim Blandy's avatar
Jim Blandy committed
207
FILE should be the name of a file created with the `etags' program.
Roland McGrath's avatar
Roland McGrath committed
208 209 210 211 212 213 214
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
215 216
  (interactive (list (read-file-name "Visit tags table: (default TAGS) "
				     default-directory
Roland McGrath's avatar
Roland McGrath committed
217 218 219 220
				     (expand-file-name "TAGS"
						       default-directory)
				     t)
		     current-prefix-arg))
221 222 223 224
  (or (stringp file) (signal 'wrong-type-argument (list 'stringp file)))
  ;; Bind tags-file-name so we can control below whether the local or
  ;; global value gets set.  Calling visit-tags-table-buffer will
  ;; initialize a buffer for the file and set tags-file-name to the
225 226
  ;; 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
227
  ;; fully-expanded name.
Roland McGrath's avatar
Roland McGrath committed
228 229
  (let ((tags-file-name file))
    (save-excursion
230
      (or (visit-tags-table-buffer file)
Roland McGrath's avatar
Roland McGrath committed
231 232 233
	  (signal 'file-error (list "Visiting tags table"
				    "file does not exist"
				    file)))
234
      ;; Set FILE to the expanded name.
Roland McGrath's avatar
Roland McGrath committed
235
      (setq file tags-file-name)))
Roland McGrath's avatar
Roland McGrath committed
236
  (if local
237
      ;; Set the local value of tags-file-name.
Roland McGrath's avatar
Roland McGrath committed
238
      (set (make-local-variable 'tags-file-name) file)
239
    ;; Set the global value of tags-file-name.
Roland McGrath's avatar
Roland McGrath committed
240 241
    (setq-default tags-file-name file)))

242 243 244 245 246 247 248 249 250 251 252 253 254
(defun tags-table-check-computed-list ()
  "Compute `tags-table-computed-list' from `tags-table-list' if necessary."
  (let ((expanded-list (mapcar 'tags-expand-table-name tags-table-list)))
    (or (equal tags-table-computed-list-for expanded-list)
	;; The list (or default-directory) has changed since last computed.
	(let* ((compute-for (mapcar 'copy-sequence expanded-list))
	       (tables (copy-sequence compute-for)) ;Mutated in the loop.
	       (computed nil)
	       table-buffer)

	  (while tables
	    (setq computed (cons (car tables) computed)
		  table-buffer (get-file-buffer (car tables)))
255
	    (if (and table-buffer
256 257 258 259
		     ;; There is a buffer visiting the file.  Now make sure
		     ;; it is initialized as a tag table buffer.
		     (save-excursion
		       (tags-verify-table (buffer-file-name table-buffer))))
260 261 262 263 264
		(save-excursion
		  (set-buffer table-buffer)
		  (if (tags-included-tables)
		      ;; Insert the included tables into the list we
		      ;; are processing.
265 266 267
		      (setcdr tables (nconc (mapcar 'tags-expand-table-name
						    (tags-included-tables))
					    (cdr tables)))))
268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309
	      ;; This table is not in core yet.  Insert a placeholder
	      ;; saying we must read it into core to check for included
	      ;; tables before searching the next table in the list.
	      (setq computed (cons t computed)))
	    (setq tables (cdr tables)))

	  ;; Record the tags-table-list value (and the context of the
	  ;; current directory) we computed from.
	  (setq tags-table-computed-list-for compute-for
		tags-table-computed-list (nreverse computed))))))

;; Extend `tags-table-computed-list' to remove the first `t' placeholder.
;; An element of the list that is `t' is a placeholder indicating that the
;; preceding element is a table that has not been read into core and might
;; contain included tables to search.  On return, the first placeholder
;; element will be gone and the element before it read into core and its
;; included tables inserted into the list.
(defun tags-table-extend-computed-list ()
  (let ((list tags-table-computed-list))
    (while (not (eq (nth 1 list) t))
      (setq list (cdr list)))
    (save-excursion
      (if (tags-verify-table (car list))
	  ;; We are now in the buffer visiting (car LIST).  Extract its
	  ;; list of included tables and insert it into the computed list.
	  (let ((tables (tags-included-tables))
		(computed nil)
		table-buffer)
	    (while tables
	      (setq computed (cons (car tables) computed)
		    table-buffer (get-file-buffer (car tables)))
	      (if table-buffer
		  (save-excursion
		    (set-buffer table-buffer)
		    (if (tags-included-tables)
			;; Insert the included tables into the list we
			;; are processing.
			(setcdr tables (append (tags-included-tables)
					       tables))))
		;; This table is not in core yet.  Insert a placeholder
		;; saying we must read it into core to check for included
		;; tables before searching the next table in the list.
310 311
		(setq computed (cons t computed)))
	      (setq tables (cdr tables)))
312 313 314 315 316 317 318
	    (setq computed (nreverse computed))
	    ;; COMPUTED now contains the list of included tables (and
	    ;; tables included by them, etc.).  Now splice this into the
	    ;; current list.
	    (setcdr list (nconc computed (cdr (cdr list)))))
	;; It was not a valid table, so just remove the following placeholder.
	(setcdr list (cdr (cdr list)))))))
319 320

;; Expand tags table name FILE into a complete file name.
Roland McGrath's avatar
Roland McGrath committed
321 322 323 324 325 326
(defun tags-expand-table-name (file)
  (setq file (expand-file-name file))
  (if (file-directory-p file)
      (expand-file-name "TAGS" file)
    file))

327 328 329
;; Like member, but comparison is done after tags-expand-table-name on both
;; sides and elements of LIST that are t are skipped.
(defun tags-table-list-member (file list)
Roland McGrath's avatar
Roland McGrath committed
330
  (setq file (tags-expand-table-name file))
331 332 333 334
  (while (and list
	      (or (eq (car list) t)
		  (not (string= file (tags-expand-table-name (car list))))))
    (setq list (cdr list)))
Roland McGrath's avatar
Roland McGrath committed
335 336
  list)

337 338 339 340 341 342 343 344 345 346 347
(defun tags-verify-table (file)
  "Read FILE into a buffer and verify that it is a valid tags table.
Sets the current buffer to one visiting FILE (if it exists).
Returns non-nil iff it is a valid table."
  (if (get-file-buffer file)
      ;; The file is already in a buffer.  Check for the visited file
      ;; having changed since we last used it.
      (let (win)
	(set-buffer (get-file-buffer file))
	(setq win (or verify-tags-table-function (initialize-new-tags-table)))
	(if (or (verify-visited-file-modtime (current-buffer))
348 349 350 351 352 353 354 355 356 357
		;; Decide whether to revert the file.
		;; revert-without-query can say to revert
		;; or the user can say to revert.
		(not (or (let ((tail revert-without-query)
			       (found nil))
			   (while tail
			     (if (string-match (car tail) buffer-file-name)
				 (setq found t))
			     (setq tail (cdr tail)))
			   found)
Richard M. Stallman's avatar
Richard M. Stallman committed
358
			 tags-revert-without-query
359 360 361 362 363
			 (yes-or-no-p
			  (format "Tags file %s has changed, read new contents? "
				  file)))))
	    (and verify-tags-table-function
		 (funcall verify-tags-table-function))
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380
	  (revert-buffer t t)
	  (initialize-new-tags-table)))
    (and (file-exists-p file)
	 (progn
	   (set-buffer (find-file-noselect file))
	   (or (string= file buffer-file-name)
	       ;; find-file-noselect has changed the file name.
	       ;; Propagate the change to tags-file-name and tags-table-list.
	       (let ((tail (member file tags-table-list)))
		 (if tail
		     (setcar tail buffer-file-name))
		 (if (eq file tags-file-name)
		     (setq tags-file-name buffer-file-name))))
	   (initialize-new-tags-table)))))

;; Subroutine of visit-tags-table-buffer.  Search the current tags tables
;; for one that has tags for THIS-FILE (or that includes a table that
381 382 383 384
;; does).  Return the name of the first table table listing THIS-FILE; if
;; the table is one included by another table, it is the master table that
;; we return.  If CORE-ONLY is non-nil, check only tags tables that are
;; already in buffers--don't visit any new files.
385 386
(defun tags-table-including (this-file core-only)
  (let ((tables tags-table-computed-list)
387
	(found nil))
388
    ;; Loop over the list, looking for a table containing tags for THIS-FILE.
Roland McGrath's avatar
Roland McGrath committed
389 390
    (while (and (not found)
		tables)
391 392 393 394 395 396 397 398

      (if core-only
	  ;; Skip tables not in core.
	  (while (eq (nth 1 tables) t)
	    (setq tables (cdr (cdr tables))))
	(if (eq (nth 1 tables) t)
	    ;; This table has not been read into core yet.  Read it in now.
	    (tags-table-extend-computed-list)))
399

400 401 402 403
      (if tables
	  ;; Select the tags table buffer and get the file list up to date.
	  (let ((tags-file-name (car tables)))
	    (visit-tags-table-buffer 'same)
404 405
	    (if (member this-file (mapcar 'expand-file-name
					  (tags-table-files)))
406 407
		;; Found it.
		(setq found tables))))
Roland McGrath's avatar
Roland McGrath committed
408
      (setq tables (cdr tables)))
409 410
    (if found
	;; Now determine if the table we found was one included by another
411 412 413 414 415
	;; table, not explicitly listed.  We do this by checking each
	;; element of the computed list to see if it appears in the user's
	;; explicit list; the last element we will check is FOUND itself.
	;; Then we return the last one which did in fact appear in
	;; tags-table-list.
416 417 418 419 420 421
	(let ((could-be nil)
	      (elt tags-table-computed-list))
	  (while (not (eq elt (cdr found)))
	    (if (tags-table-list-member (car elt) tags-table-list)
		;; This table appears in the user's list, so it could be
		;; the one which includes the table we found.
422 423 424 425
		(setq could-be (car elt)))
	    (setq elt (cdr elt))
	    (if (eq t (car elt))
		(setq elt (cdr elt))))
426 427
	  ;; The last element we found in the computed list before FOUND
	  ;; that appears in the user's list will be the table that
428
	  ;; included the one we found.
429
	  could-be))))
Roland McGrath's avatar
Roland McGrath committed
430

431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449
;; Subroutine of visit-tags-table-buffer.  Move tags-table-list-pointer
;; along and set tags-file-name.  Returns nil when out of tables.
(defun tags-next-table ()
  ;; If there is a placeholder element next, compute the list to replace it.
  (while (eq (nth 1 tags-table-list-pointer) t)
    (tags-table-extend-computed-list))

  ;; 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-computed-list))

  (if (eq tags-table-list-pointer tags-table-list-started-at)
      ;; We have come full circle.  No more tables.
      (setq tags-table-list-pointer nil)
    ;; Set tags-file-name to the name from the list.  It is already expanded.
    (setq tags-file-name (car tags-table-list-pointer))))

Roland McGrath's avatar
Roland McGrath committed
450 451
(defun visit-tags-table-buffer (&optional cont)
  "Select the buffer containing the current tags table.
452
If optional arg is a string, visit that file as a tags table.
Roland McGrath's avatar
Roland McGrath committed
453 454
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;
455
 just select the buffer visiting `tags-file-name'.
Roland McGrath's avatar
Roland McGrath committed
456
If arg is nil or absent, choose a first buffer from information in
457
 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
Roland McGrath's avatar
Roland McGrath committed
458
Returns t if it visits a tags table, or nil if there are no more in the list."
459 460

  ;; Set tags-file-name to the tags table file we want to visit.
461 462 463
  (cond ((eq cont 'same)
	 ;; Use the ambient value of tags-file-name.
	 (or tags-file-name
464 465
	     (error "%s"
		    (substitute-command-keys
466 467
		     (concat "No tags table in use; "
			     "use \\[visit-tags-table] to select one")))))
468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495

	((eq t cont)
	 ;; Find the next table.
	 (if (tags-next-table)
	     ;; Skip over nonexistent files.
	     (while (and (not (or (get-file-buffer tags-file-name)
				  (file-exists-p tags-file-name)))
			 (tags-next-table)))))

	(t
	 ;; Pick a table out of our hat.
	 (tags-table-check-computed-list) ;Get it up to date, we might use it.
	 (setq tags-file-name
	       (or
		;; If passed a string, use that.
		(if (stringp cont)
		    (prog1 cont
		      (setq cont nil)))
		;; 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.
		(and buffer-file-name
496
		     (or
497 498 499 500 501
		      ;; First check only tables already in buffers.
		      (tags-table-including buffer-file-name t)
		      ;; Since that didn't find any, now do the
		      ;; expensive version: reading new files.
		      (tags-table-including buffer-file-name nil)))
502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533
		;; Fourth, use the user variable tags-file-name, if it is
		;; not already in the current list.
		(and tags-file-name
		     (not (tags-table-list-member tags-file-name
						  tags-table-computed-list))
		     tags-file-name)
		;; Fifth, use the user variable giving the table list.
		;; Find the first element of the list that actually exists.
		(let ((list tags-table-list)
		      file)
		  (while (and list
			      (setq file (tags-expand-table-name (car list)))
			      (not (get-file-buffer file))
			      (not (file-exists-p file)))
		    (setq list (cdr list)))
		  (car 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.
  (setq tags-file-name (tags-expand-table-name tags-file-name))

  (if (and (eq cont t)
	   (null tags-table-list-pointer))
      ;; All out of tables.
      nil

    ;; Verify that tags-file-name names a valid tags table.
534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591
    ;; Bind another variable with the value of tags-file-name
    ;; before we switch buffers, in case tags-file-name is buffer-local.
    (let ((curbuf (current-buffer))
	  (local-tags-file-name tags-file-name))
      (if (tags-verify-table local-tags-file-name)

	  ;; 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))

	    ;; If this was a new table selection (CONT is nil), make
	    ;; sure tags-table-list includes the chosen table, and
	    ;; update the list pointer variables.
	    (or cont
		;; Look in the list for the table we chose.
		(let ((found (tags-table-list-member
			      local-tags-file-name
			      tags-table-computed-list)))
		  (if found
		      ;; There it is.  Just switch to it.
		      (setq tags-table-list-pointer found
			    tags-table-list-started-at found)

		    ;; 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 (tags-table-list-member
					local-tags-file-name
					(car sets))))
			(setq sets (cdr sets)))
		      (if sets
			  ;; Found in some other set.  Switch to that set.
			  (progn
			    (or (memq tags-table-list tags-table-set-list)
				;; Save the current list.
				(setq tags-table-set-list
				      (cons tags-table-list
					    tags-table-set-list)))
			    (setq tags-table-list (car sets)))

			;; Not found in any existing set.
			(if (and tags-table-list
				 (or (eq t tags-add-tables)
				     (and tags-add-tables
					  (y-or-n-p
					   (concat "Keep current list of "
						   "tags tables also? ")))))
			    ;; Add it to the current list.
			    (setq tags-table-list (cons local-tags-file-name
							tags-table-list))

			  ;; Make a fresh list, and store the old one.
			  (message "Starting a new list of tags tables")
			  (or (null tags-table-list)
			      (memq tags-table-list tags-table-set-list)
592 593 594
			      (setq tags-table-set-list
				    (cons tags-table-list
					  tags-table-set-list)))
595 596 597 598 599 600 601 602 603 604 605 606 607 608 609
			  (setq tags-table-list (list local-tags-file-name))))

		      ;; Recompute tags-table-computed-list.
		      (tags-table-check-computed-list)
		      ;; Set the tags table list state variables to start
		      ;; over from tags-table-computed-list.
		      (setq tags-table-list-started-at tags-table-computed-list
			    tags-table-list-pointer
			    tags-table-computed-list)))))

	    ;; Return of t says the tags table is valid.
	    t)

	;; The buffer was not valid.  Don't use it again.
	(set-buffer curbuf)
610
	(kill-local-variable 'tags-file-name)
611
	(if (eq local-tags-file-name tags-file-name)
612
	    (setq tags-file-name nil))
613
	(error "File %s is not a valid tags table" local-tags-file-name)))))
614 615

(defun tags-reset-tags-tables ()
616
  "Reset tags state to cancel effect of any previous \\[visit-tags-table] or \\[find-tag]."
617
  (interactive)
618 619 620 621 622 623 624 625
  ;; Clear out the markers we are throwing away.
  (let ((i 0))
    (while (< i find-tag-marker-ring-length)
      (if (aref (cddr tags-location-ring) i)
	  (set-marker (aref (cddr tags-location-ring) i) nil))
      (if (aref (cddr find-tag-marker-ring) i)
	  (set-marker (aref (cddr find-tag-marker-ring) i) nil))
      (setq i (1+ i))))
626
  (setq tags-file-name nil
627 628
	tags-location-ring (make-ring find-tag-marker-ring-length)
	find-tag-marker-ring (make-ring find-tag-marker-ring-length)
629 630 631 632 633 634
	tags-table-list nil
	tags-table-computed-list nil
	tags-table-computed-list-for nil
	tags-table-list-pointer nil
	tags-table-list-started-at nil
	tags-table-set-list nil))
635

Jim Blandy's avatar
Jim Blandy committed
636 637
(defun file-of-tag ()
  "Return the file name of the file whose tags point is within.
Roland McGrath's avatar
Roland McGrath committed
638 639 640
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
641

Jim Blandy's avatar
Jim Blandy committed
642
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
643 644
(defun tags-table-files ()
  "Return a list of files in the current tags table.
645 646 647
Assumes the tags table is the current buffer.  The file names are returned
as they appeared in the `etags' command that created the table, usually
without directory names."
648 649 650
  (or tags-table-files
      (setq tags-table-files
	    (funcall tags-table-files-function))))
Roland McGrath's avatar
Roland McGrath committed
651 652

(defun tags-included-tables ()
653 654
  "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
655 656 657 658 659 660 661 662 663 664 665 666 667 668
  (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
669 670
	       ;; 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
671
	       (while included
672
		 ;; Visit the buffer.
Roland McGrath's avatar
Roland McGrath committed
673
		 (let ((tags-file-name (car included)))
Roland McGrath's avatar
Roland McGrath committed
674
		   (visit-tags-table-buffer 'same))
675
		 ;; Recurse in that buffer to compute its completion table.
Roland McGrath's avatar
Roland McGrath committed
676
		 (if (tags-completion-table)
677
		     ;; Combine the tables.
Roland McGrath's avatar
Roland McGrath committed
678 679 680 681 682 683 684 685 686 687 688 689 690 691 692
		     (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
693 694 695
    ;; If we need to ask for the tag table, allow that.
    (let ((enable-recursive-minibuffers t))
      (visit-tags-table-buffer))
Roland McGrath's avatar
Roland McGrath committed
696 697 698
    (if (eq what t)
	(all-completions string (tags-completion-table) predicate)
      (try-completion string (tags-completion-table) predicate))))
699

Jim Blandy's avatar
Jim Blandy committed
700 701 702 703 704
;; 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
705 706 707 708 709 710 711
    (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
712 713 714 715 716 717 718
	       (buffer-substring (point)
				 (progn (forward-sexp -1)
					(while (looking-at "\\s'")
					  (forward-char 1))
					(point))))
      nil)))

Roland McGrath's avatar
Roland McGrath committed
719
;; Read a tag name from the minibuffer with defaulting and completion.
Jim Blandy's avatar
Jim Blandy committed
720
(defun find-tag-tag (string)
Roland McGrath's avatar
Roland McGrath committed
721 722 723 724 725 726
  (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)
727 728
				'tags-complete-tag
				nil nil nil nil default)))
729 730 731
    (if (equal spec "")
	(or default (error "There is no default tag"))
      spec)))
Jim Blandy's avatar
Jim Blandy committed
732

Roland McGrath's avatar
Roland McGrath committed
733 734 735
(defvar last-tag nil
  "Last tag found by \\[find-tag].")

736 737 738 739 740 741 742 743 744 745
;; Get interactive args for find-tag{-noselect,-other-window,-regexp}.
(defun find-tag-interactive (prompt &optional no-default)
  (if current-prefix-arg
      (list nil (if (< (prefix-numeric-value current-prefix-arg) 0)
		    '-
		  t))
    (list (if no-default
	      (read-string prompt)
	    (find-tag-tag prompt)))))

746 747
(defvar find-tag-history nil)

Jim Blandy's avatar
Jim Blandy committed
748
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
749 750
(defun find-tag-noselect (tagname &optional next-p regexp-p)
  "Find tag (in current tags table) whose name contains TAGNAME.
751
Returns the buffer containing the tag's definition and moves its point there,
Roland McGrath's avatar
Roland McGrath committed
752 753 754
but does not select the buffer.
The default for TAGNAME is the expression in the buffer near point.

755 756 757 758 759
If second arg NEXT-P is t (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.  If NEXT-P
is the atom `-' (interactively, with prefix arg that is a negative number
or just \\[negative-argument]), pop back to the previous tag gone to.
Jim Blandy's avatar
Jim Blandy committed
760

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

763 764 765 766
A marker representing the point when this command is onvoked is pushed
onto a ring and may be popped back to with \\[pop-tag-mark].
Contrast this with the ring of marks gone to by the command.

Roland McGrath's avatar
Roland McGrath committed
767
See documentation of variable `tags-file-name'."
768 769
  (interactive (find-tag-interactive "Find tag: "))

770
  (setq find-tag-history (cons tagname find-tag-history))
771 772
  ;; Save the current buffer's value of `find-tag-hook' before selecting the
  ;; tags table buffer.
773
  (let ((local-find-tag-hook find-tag-hook))
774 775
    (if (eq '- next-p)
	;; Pop back to a previous location.
776
	(if (ring-empty-p tags-location-ring)
777
	    (error "No previous tag locations")
778
	  (let ((marker (ring-remove tags-location-ring 0)))
779 780
	    (prog1
		;; Move to the saved location.
781 782
		(set-buffer (or (marker-buffer marker)
                                (error "The marked buffer has been deleted")))
783
	      (goto-char (marker-position marker))
784
	      ;; Kill that marker so it doesn't slow down editing.
785 786 787
	      (set-marker marker nil nil)
	      ;; Run the user's hook.  Do we really want to do this for pop?
	      (run-hooks 'local-find-tag-hook))))
788 789
      ;; Record whence we came.
      (ring-insert find-tag-marker-ring (point-marker))
790 791 792 793 794 795 796
      (if next-p
	  ;; Find the same table we last used.
	  (visit-tags-table-buffer 'same)
	;; Pick a table to use.
	(visit-tags-table-buffer)
	;; Record TAGNAME for a future call with NEXT-P non-nil.
	(setq last-tag tagname))
797 798 799 800 801 802 803 804 805 806 807 808
      ;; Record the location so we can pop back to it later.
      (let ((marker (make-marker)))
	(save-excursion
	  (set-buffer
	   ;; find-tag-in-order does the real work.
	   (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
809
	      find-tag-tag-order)
810 811 812 813 814 815 816
	    (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)))
	  (set-marker marker (point))
	  (run-hooks 'local-find-tag-hook)
817
	  (ring-insert tags-location-ring marker)
818
	  (current-buffer))))))
Jim Blandy's avatar
Jim Blandy committed
819

Jim Blandy's avatar
Jim Blandy committed
820
;;;###autoload
821
(defun find-tag (tagname &optional next-p regexp-p)
Roland McGrath's avatar
Roland McGrath committed
822 823 824
  "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
825

826 827 828 829 830
If second arg NEXT-P is t (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.  If NEXT-P
is the atom `-' (interactively, with prefix arg that is a negative number
or just \\[negative-argument]), pop back to the previous tag gone to.
Jim Blandy's avatar
Jim Blandy committed
831

832 833 834 835 836 837
If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.

A marker representing the point when this command is onvoked is pushed
onto a ring and may be popped back to with \\[pop-tag-mark].
Contrast this with the ring of marks gone to by the command.

Roland McGrath's avatar
Roland McGrath committed
838
See documentation of variable `tags-file-name'."
839 840
  (interactive (find-tag-interactive "Find tag: "))
  (switch-to-buffer (find-tag-noselect tagname next-p regexp-p)))
Roland McGrath's avatar
Roland McGrath committed
841
;;;###autoload (define-key esc-map "." 'find-tag)
Jim Blandy's avatar
Jim Blandy committed
842

Jim Blandy's avatar
Jim Blandy committed
843
;;;###autoload
844
(defun find-tag-other-window (tagname &optional next-p regexp-p)
Roland McGrath's avatar
Roland McGrath committed
845
  "Find tag (in current tags table) whose name contains TAGNAME.
846 847 848
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.
Roland McGrath's avatar
Roland McGrath committed
849

850 851 852 853 854
If second arg NEXT-P is t (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.  If NEXT-P
is negative (interactively, with prefix arg that is a negative number or
just \\[negative-argument]), pop back to the previous tag gone to.
Roland McGrath's avatar
Roland McGrath committed
855

856 857 858 859 860 861
If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.

A marker representing the point when this command is onvoked is pushed
onto a ring and may be popped back to with \\[pop-tag-mark].
Contrast this with the ring of marks gone to by the command.

Roland McGrath's avatar
Roland McGrath committed
862
See documentation of variable `tags-file-name'."
863 864
  (interactive (find-tag-interactive "Find tag other window: "))

865 866 867 868 869
  ;; 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)))
870
	 (tagbuf (find-tag-noselect tagname next-p regexp-p))
871
	 (tagpoint (progn (set-buffer tagbuf) (point))))
872 873
    (set-window-point (prog1
			  (selected-window)
874 875 876 877 878 879 880
			(switch-to-buffer-other-window tagbuf)
			;; We have to set this new window's point; it
			;; might already have been displaying a
			;; different portion of tagbuf, in which case
			;; switch-to-buffer-other-window doesn't set
			;; the window's point from the buffer.
			(set-window-point (selected-window) tagpoint))
881
		      window-point)))
Roland McGrath's avatar
Roland McGrath committed
882 883
;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)

Roland McGrath's avatar
Roland McGrath committed
884
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
885
(defun find-tag-other-frame (tagname &optional next-p)
886 887 888 889 890 891 892 893 894 895
  "Find tag (in current tags table) whose name contains TAGNAME.
Select the buffer containing the tag's definition in another frame, and
move point there.  The default for TAGNAME is the expression in the buffer
around or before point.

If second arg NEXT-P is t (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.  If NEXT-P
is negative (interactively, with prefix arg that is a negative number or
just \\[negative-argument]), pop back to the previous tag gone to.
Jim Blandy's avatar
Jim Blandy committed
896

897 898 899 900 901 902
If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.

A marker representing the point when this command is onvoked is pushed
onto a ring and may be popped back to with \\[pop-tag-mark].
Contrast this with the ring of marks gone to by the command.

Roland McGrath's avatar
Roland McGrath committed
903
See documentation of variable `tags-file-name'."
904
  (interactive (find-tag-interactive "Find tag other frame: "))
Roland McGrath's avatar
Roland McGrath committed
905 906 907 908
  (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
909
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
910 911 912
(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
913

914 915 916 917 918
If second arg NEXT-P is t (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.  If NEXT-P
is negative (interactively, with prefix arg that is a negative number or
just \\[negative-argument]), pop back to the previous tag gone to.
Roland McGrath's avatar
Roland McGrath committed
919 920 921

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

922 923 924 925
A marker representing the point when this command is onvoked is pushed
onto a ring and may be popped back to with \\[pop-tag-mark].
Contrast this with the ring of marks gone to by the command.

Roland McGrath's avatar
Roland McGrath committed
926
See documentation of variable `tags-file-name'."
927 928 929 930
  (interactive (find-tag-interactive "Find tag regexp: " t))
  ;; We go through find-tag-other-window to do all the display hair there.
  (funcall (if other-window 'find-tag-other-window 'find-tag)
	   regexp next-p t))
931
;;;###autoload (define-key esc-map [?\C-.] 'find-tag-regexp)
932 933 934 935 936 937 938 939 940 941 942 943 944 945 946 947 948 949

;;;###autoload (define-key esc-map "*" 'pop-tag-mark)

;;;###autoload
(defun pop-tag-mark ()
  "Pop back to where \\[find-tag] was last invoked.

This is distinct from invoking \\[find-tag] with a negative argument
since that pops a stack of markers at which tags were found, not from
where they were found."
  (interactive)
  (if (ring-empty-p find-tag-marker-ring)
      (error "No previous locations for find-tag invocation"))
  (let ((marker (ring-remove find-tag-marker-ring 0)))
    (switch-to-buffer (or (marker-buffer marker)
                          (error "The marked buffer has been deleted")))
    (goto-char (marker-position marker))
    (set-marker marker nil nil)))
Roland McGrath's avatar
Roland McGrath committed
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969

;; 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.

970 971
(defvar tag-lines-already-matched nil)	;matches remembered here between calls

972 973 974 975 976 977
(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
978 979 980 981
  (let (file				;name of file containing tag
	tag-info			;where to find the tag in FILE
	(first-table t)
	(tag-order order)
982
	(match-marker (make-marker))
Roland McGrath's avatar
Roland McGrath committed
983 984 985
	goto-func
	)
    (save-excursion
986 987 988 989 990 991 992 993 994 995 996 997 998

      (if first-search
	  ;; This is the start of a search for a fresh tag.
	  ;; Clear the list of tags matched by the previous search.
	  ;; find-tag-noselect has already put us in the first tags table
	  ;; buffer before we got called.
	  (setq tag-lines-already-matched nil)
	;; Continuing to search for the tag specified last time.
	;; tag-lines-already-matched lists locations matched in previous
	;; calls so we don't visit the same tag twice if it matches twice
	;; during two passes with different qualification predicates.
	;; Switch to the current tags table buffer.
	(visit-tags-table-buffer 'same))
Roland McGrath's avatar
Roland McGrath committed
999

Roland McGrath's avatar
Roland McGrath committed
1000
      ;; Get a qualified match.
1001
      (catch 'qualified-match-found
Roland McGrath's avatar
Roland McGrath committed
1002

1003
	;; Iterate over the list of tags tables.
Roland McGrath's avatar
Roland McGrath committed
1004 1005 1006
	(while (or first-table
		   (visit-tags-table-buffer t))

Roland McGrath's avatar
Roland McGrath committed
1007 1008 1009
	  (and first-search first-table
	       ;; Start at beginning of tags file.
	       (goto-char (point-min)))
1010

Roland McGrath's avatar
Roland McGrath committed
1011
	  (setq first-table nil)
Roland McGrath's avatar
Roland McGrath committed
1012

1013
	  ;; Iterate over the list of ordering predicates.
Roland McGrath's avatar
Roland McGrath committed
1014 1015 1016 1017 1018
	  (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.
1019 1020 1021 1022
		   (not (member (set-marker match-marker (save-excursion
							   (beginning-of-line)
							   (point)))
				tag-lines-already-matched))
Roland McGrath's avatar
Roland McGrath committed
1023 1024 1025 1026 1027 1028 1029 1030
		   (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.
1031 1032 1033 1034 1035 1036
	;; Clear out the markers we use to avoid duplicate matches so they
	;; don't slow down editting and are immediately available for GC.
	(while tag-lines-already-matched
	  (set-marker (car tag-lines-already-matched) nil nil)
	  (setq tag-lines-already-matched (cdr tag-lines-already-matched)))
	(set-marker match-marker nil nil)
Roland McGrath's avatar
Roland McGrath committed
1037
	(error "No %stags %s %s" (if first-search "" "more ")
1038 1039
	       matching pattern))

Roland McGrath's avatar
Roland McGrath committed
1040 1041
      ;; Found a tag; extract location info.
      (beginning-of-line)
1042
      (setq tag-lines-already-matched (cons match-marker
Roland McGrath's avatar
Roland McGrath committed
1043 1044 1045 1046 1047
					    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))

1048
      ;; Get the local value in the tags table buffer before switching buffers.
Roland McGrath's avatar
Roland McGrath committed
1049 1050 1051 1052 1053 1054
      (setq goto-func goto-tag-location-function)

      ;; Find the right line in the specified file.
      (set-buffer (find-file-noselect file))
      (widen)
      (push-mark)
1055 1056
      (funcall goto-func tag-info)

Roland McGrath's avatar
Roland McGrath committed
1057 1058 1059 1060 1061
      ;; Return the buffer where the tag was found.
      (current-buffer))))

;; `etags' TAGS file format support.

1062 1063
;; 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
1064
(defun etags-recognize-tags-table ()
1065
  (and (etags-verify-tags-table)
Jim Blandy's avatar
Jim Blandy committed
1066 1067 1068
       ;; 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
1069
       (mapcar (function (lambda (elt)
1070
			   (set (make-local-variable (car elt)) (cdr elt))))
Roland McGrath's avatar
Roland McGrath committed
1071 1072 1073 1074 1075 1076 1077
	       '((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))
1078
		 (find-tag-regexp-next-line-after-failure-p . t)
Roland McGrath's avatar
Roland McGrath committed
1079
		 (find-tag-search-function . search-forward)
1080
		 (find-tag-tag-order . (tag-exact-file-name-match-p
1081
					tag-exact-match-p
1082 1083 1084
					tag-symbol-match-p
					tag-word-match-p
					tag-any-match-p))
Roland McGrath's avatar
Roland McGrath committed
1085 1086 1087 1088 1089 1090 1091
		 (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)
		 ))))

1092
;; Return non-nil iff the current buffer is a valid etags TAGS file.
Roland McGrath's avatar
Roland McGrath committed
1093
(defun etags-verify-tags-table ()
1094 1095
  ;; Use eq instead of = in case char-after returns nil.
  (eq (char-after 1) ?\f))
Roland McGrath's avatar
Roland McGrath committed
1096 1097 1098

(defun etags-file-of-tag ()
  (save-excursion
1099
    (re-search-backward "\f\n\\([^\n]+\\),[0-9]*\n")
1100 1101
    (expand-file-name (buffer-substring (match-beginning 1) (match-end 1))
		      (file-truename default-directory))))
Roland McGrath's avatar
Roland McGrath committed
1102

1103

Roland McGrath's avatar
Roland McGrath committed
1104 1105 1106 1107
(defun etags-tags-completion-table ()
  (let ((table (make-vector 511 0)))
    (save-excursion
      (goto-char (point-min))
1108 1109 1110 1111
      ;; This monster regexp matches an etags tag line.
      ;;   \1 is the string to match;
      ;;   \2 is not interesting;
      ;;   \3 is the guessed tag name; XXX guess should be better eg DEFUN
1112 1113 1114 1115
      ;;   \4 is not interesting;
      ;;   \5 is the explicitly-specified tag name.
      ;;   \6 is the line to start searching at;
      ;;   \7 is the char to start searching at.
1116
      (while (re-search-forward
1117 1118
	      "^\\(\\([^\177]+[^-a-zA-Z0-9_$\177]+\\)?\\([-a-zA-Z0-9_$?:]+\\)\
\[^-a-zA-Z0-9_$?:\177]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
1119
\\([0-9]+\\)?,\\([0-9]+\\)?\n"
1120
	      nil t)
1121
	(intern	(if (match-beginning 5)
1122
		    ;; There is an explicit tag name.
1123
		    (buffer-substring (match-beginning 5) (match-end 5))
1124 1125 1126
		  ;; No explicit tag name.  Best guess.
		  (buffer-substring (match-beginning 3) (match-end 3)))
		table)))
Roland McGrath's avatar
Roland McGrath committed
1127 1128 1129
    table))

(defun etags-snarf-tag ()
1130
  (let (tag-text line startpos)
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147 1148 1149 1150 1151 1152 1153 1154 1155 1156 1157 1158
    (if (save-excursion
	  (forward-line -1)
	  (looking-at "\f\n"))
	;; The match was for a source file name, not any tag within a file.
	;; Give text of t, meaning to go exactly to the location we specify,
	;; the beginning of the file.
	(setq tag-text t
	      line nil
	      startpos 1)

      ;; Find the end of the tag and record the whole tag text.
      (search-forward "\177")
      (setq tag-text (buffer-substring (1- (point))
				       (save-excursion (beginning-of-line)
						       (point))))
      ;; Skip explicit tag name if present.
      (search-forward "\001" (save-excursion (forward-line 1) (point)) t)
      (if (looking-at "[0-9]")
	  (setq line (string-to-int (buffer-substring
				     (point)
				     (progn (skip-chars-forward "0-9")
					    (point))))))
      (search-forward ",")
      (if (looking-at "[0-9]")
	  (setq startpos (string-to-int (buffer-substring
					 (point)
					 (progn (skip-chars-forward "0-9")
						(point)))))))
Roland McGrath's avatar
Roland McGrath committed
1159 1160
    ;; Leave point on the next line of the tags file.
    (forward-line 1)
1161 1162 1163 1164
    (cons tag-text (cons line startpos))))

;; TAG-INFO is a cons (TEXT LINE . POSITION) where TEXT is the initial part
;; of a line containing the tag and POSITION is the character position of
1165 1166 1167
;; TEXT within the file (starting from 1); LINE is the line number.  If
;; TEXT is t, it means the tag refers to exactly LINE or POSITION
;; (whichever is present, LINE having preference, no searching.  Either
1168 1169 1170
;; LINE or POSITION may be nil; POSITION is used if present.  If the tag
;; isn't exactly at the given position then look around that position using
;; a search window which expands until it hits the start of file.
Roland McGrath's avatar
Roland McGrath committed
1171
(defun etags-goto-tag-location (tag-info)
1172
  (let ((startpos (cdr (cdr tag-info)))
1173
	(line (car (cdr tag-info)))
1174 1175 1176 1177
	offset found pat)
    (if (eq (car tag-info) t)
	;; Direct file tag.
	(cond (line (goto-line line))
1178
	      (startpos (goto-char startpos))
1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189 1190 1191 1192 1193
	      (t (error "etags.el BUG: bogus direct file tag")))
      ;; 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.
      (setq offset 1000
	    found nil
	    pat (concat (if (eq selective-display t)
			    "\\(^\\|\^m\\)" "^")
			(regexp-quote (car tag-info))))
      ;; The character position in the tags table is 0-origin.
      ;; Convert it to a 1-origin Emacs character position.
      (if startpos (setq startpos (1+ startpos)))
      ;; If no char pos was given, try the given line number.
      (or startpos
1194 1195
	  (if line
	      (setq startpos (progn (goto-line line)
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216 1217 1218
				    (point)))))
      (or startpos
	  (setq startpos (point-min)))
      ;; First see if the tag is right at the specified location.
      (goto-char startpos)
      (setq found (looking-at pat))
      (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 "Rerun etags: `%s' not found in %s"
		 pat buffer-file-name)))
    ;; Position point at the right place
    ;; if the search string matched an extra Ctrl-m at the beginning.
    (and (eq selective-display t)
	 (looking-at "\^m")
	 (forward-char 1))
    (beginning-of-line)))
Roland McGrath's avatar
Roland McGrath committed
1219 1220 1221 1222 1223 1224 1225

(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")))
1226 1227 1228 1229 1230
      (let ((tag (buffer-substring (point)
				   (progn (skip-chars-forward "^\177")
					  (point)))))
	(princ (if (looking-at "[^\n]+\001")
		   ;; There is an explicit tag name; use that.
1231
		   (buffer-substring (1+ (point)) ;skip \177
1232 1233 1234
				     (progn (skip-chars-forward "^\001")
					    (point)))
		 tag)))
Roland McGrath's avatar
Roland McGrath committed
1235
      (terpri)