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

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

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

Jim Blandy's avatar
Jim Blandy committed
9 10 11 12
;; 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
13
;; the Free Software Foundation; either version 2, or (at your option)
Jim Blandy's avatar
Jim Blandy committed
14 15 16 17 18 19 20 21 22 23 24
;; 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
25 26
;;; Code:

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

;;;###autoload
37
;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
Roland McGrath's avatar
Roland McGrath committed
38
(defvar tags-table-list nil
39 40 41 42 43
  "*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
44

45 46
;;;###autoload
(defvar tags-add-tables 'ask-user
47 48 49
  "*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
50 51
to the current list (as opposed to starting a new list).")

Roland McGrath's avatar
Roland McGrath committed
52
(defvar tags-table-list-pointer nil
Roland McGrath's avatar
Roland McGrath committed
53 54 55 56 57 58
  "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
59 60

(defvar tags-table-parent-pointer-list nil
Roland McGrath's avatar
Roland McGrath committed
61
  "Saved state of the tags table that included this one.
62 63 64
Each element is (LIST POINTER STARTED-AT), giving the values of
 `tags-table-list', `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
65 66 67 68 69 70 71 72 73 74 75 76 77

(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
78 79
  "*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
80 81 82 83
has a `find-tag-default-function' property (see `put'), that is used.
Otherwise, `find-tag-default' is used.")

(defvar default-tags-table-function nil
84 85 86
  "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.")
87 88 89 90

(defvar tags-location-stack nil
  "List of markers which are locations visited by \\[find-tag].
Pop back to the last location with \\[negative-argument] \\[find-tag].")
Roland McGrath's avatar
Roland McGrath committed
91 92 93

;; Tags table state.
;; These variables are local in tags table buffers.
Jim Blandy's avatar
Jim Blandy committed
94

Roland McGrath's avatar
Roland McGrath committed
95 96 97
(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
98

Roland McGrath's avatar
Roland McGrath committed
99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
(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
115
				  recognize-empty-tags-table)
Roland McGrath's avatar
Roland McGrath committed
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 142 143 144 145 146 147 148 149 150
  "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
151
  "Function to return t iff the current buffer contains a valid
Roland McGrath's avatar
Roland McGrath committed
152 153
\(already initialized\) tags file.")

154 155 156 157
;; 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
158
(defun initialize-new-tags-table ()
159 160 161 162
  (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
163 164 165 166 167 168
  ;; 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
169

Jim Blandy's avatar
Jim Blandy committed
170
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
171 172
(defun visit-tags-table (file &optional local)
  "Tell tags commands to use tags table file FILE.
Jim Blandy's avatar
Jim Blandy committed
173
FILE should be the name of a file created with the `etags' program.
Roland McGrath's avatar
Roland McGrath committed
174 175 176 177 178 179 180
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
181 182
  (interactive (list (read-file-name "Visit tags table: (default TAGS) "
				     default-directory
Roland McGrath's avatar
Roland McGrath committed
183 184 185 186
				     (expand-file-name "TAGS"
						       default-directory)
				     t)
		     current-prefix-arg))
187 188 189
  ;; 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
190 191
  (let ((tags-file-name file))
    (save-excursion
Roland McGrath's avatar
Roland McGrath committed
192
      (or (visit-tags-table-buffer 'same)
Roland McGrath's avatar
Roland McGrath committed
193 194 195
	  (signal 'file-error (list "Visiting tags table"
				    "file does not exist"
				    file)))
196
      ;; Set FILE to the expanded name.
Roland McGrath's avatar
Roland McGrath committed
197
      (setq file tags-file-name)))
Roland McGrath's avatar
Roland McGrath committed
198
  (if local
199
      ;; Set the local value of tags-file-name.
Roland McGrath's avatar
Roland McGrath committed
200
      (set (make-local-variable 'tags-file-name) file)
201
    ;; Set the global value of tags-file-name.
Roland McGrath's avatar
Roland McGrath committed
202 203 204
    (setq-default tags-file-name file)))

;; Move tags-table-list-pointer along and set tags-file-name.
205
;; If NO-INCLUDES is non-nil, ignore included tags tables.
Roland McGrath's avatar
Roland McGrath committed
206
;; Returns nil when out of tables.
207 208 209 210 211 212 213 214 215
(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.
216 217 218
	    (cons (list tags-table-list
			tags-table-list-pointer
			tags-table-list-started-at)
219 220
		  tags-table-parent-pointer-list)
	    ;; Start the pointer in the list of included tables.
221
	    tags-table-list tags-included-tables
222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237
	    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.
238 239 240 241
	      (setq tags-table-list
		    (nth 0 (car tags-table-parent-pointer-list))
		    tags-table-list-pointer
		    (nth 1 (car tags-table-parent-pointer-list))
242
		    tags-table-list-started-at
243
		    (nth 2 (car tags-table-parent-pointer-list))
244 245 246 247 248 249 250 251 252 253 254 255 256
		    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
257 258 259 260 261 262
(defun tags-expand-table-name (file)
  (setq file (expand-file-name file))
  (if (file-directory-p file)
      (expand-file-name "TAGS" file)
    file))

263 264 265 266 267 268 269 270
;; Search for FILE in LIST (default: tags-table-list); also search
;; tables that are already in core for FILE being included by them.  Return t
;; if we find it, nil if not.  Comparison is done after tags-expand-table-name
;; on both sides.  If MOVE-TO is non-nil, update tags-table-list and the list
;; pointers to point to the table found.  In recursive calls, MOVE-TO is a list
;; value for tags-table-parent-pointer-list describing the position of the
;; caller's search.
(defun tags-find-table-in-list (file move-to &optional list)
Roland McGrath's avatar
Roland McGrath committed
271 272 273
  (or list
      (setq list tags-table-list))
  (setq file (tags-expand-table-name file))
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 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325
  (let (;; Set up the MOVE-TO argument used for the recursive calls we will do
	;; for included tables.  This is a list value for
	;; tags-table-parent-pointer-list describing the included tables we are
	;; descending; we cons our position onto the list from our recursive
	;; caller (which is searching a list that contains the table whose
	;; included tables we are searching).  The atom `in-progress' is a
	;; placeholder; when a recursive call locates FILE, we replace
	;; 'in-progress with the tail of LIST whose car contained FILE.
	(recursing-move-to (if move-to
			       (cons (list list 'in-progress 'in-progress)
				     (if (eq move-to t) nil move-to))))
	this-file)
    (while (and (consp list)		; We set LIST to t when we locate FILE.
		(not (string= file
			      (setq this-file
				    (tags-expand-table-name (car list))))))
      (if (get-file-buffer this-file)
	  ;; This table is already in core.  Visit it and recurse to check
	  ;; its included tables.
	  (save-excursion
	    (let ((tags-file-name this-file)
		  found)
	      (visit-tags-table-buffer 'same)
	      (if (tags-find-table-in-list file recursing-move-to
					   (tags-included-tables))
		  (progn
		    ;; We found FILE in the included table.
		    (if move-to
			(progn
			  ;; The recursive call has already frobbed the list
			  ;; pointers.  It set tags-table-parent-pointer-list
			  ;; to a list including RECURSING-MOVE-TO.  Now we
			  ;; must mutate that cons so its list pointers show
			  ;; the position where we found this included table.
			  (setcar (cdr (car recursing-move-to)) list)
			  (setcar (cdr (cdr (car recursing-move-to))) list)
			  ;; Don't do further list frobnication below.
			  (setq move-to nil)))
		    (setq list t))))))
      (if (consp list)
	  (setq list (cdr list))))
    (and list move-to
	 (progn
	   ;; We have located FILE in the list.
	   ;; Now frobnicate the list pointers to point to it.
	   (setq tags-table-list-started-at list
		 tags-table-list-pointer list)
	   (if (consp move-to)
	       ;; We are in a recursive call.  MOVE-TO is the value for
	       ;; tags-table-parent-pointer-list that describes the tables
	       ;; descended by the caller (and its callers, recursively).
	       (setq tags-table-parent-pointer-list move-to)))))
Roland McGrath's avatar
Roland McGrath committed
326 327
  list)

328
;; Local var in visit-tags-table-buffer
329 330 331
;; which is set by tags-table-including.
(defvar visit-tags-table-buffer-cont)

Roland McGrath's avatar
Roland McGrath committed
332
;; Subroutine of visit-tags-table-buffer.  Frobs its local vars.
333 334 335 336 337 338
;; 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.  If
;; CORE-ONLY is non-nil, check only tags tables that are already in
;; buffers--don't visit any new files.
(defun tags-table-including (this-file tables core-only &optional recursing)
339 340
  (let ((starting-tables tables)
	(found nil))
341
    ;; Loop over TABLES, looking for one containing tags for THIS-FILE.
Roland McGrath's avatar
Roland McGrath committed
342 343 344 345
    (while (and (not found)
		tables)
      (let ((tags-file-name (tags-expand-table-name (car tables))))
	(if (or (get-file-buffer tags-file-name)
346 347
		(and (not core-only)
		     (file-exists-p tags-file-name)))
Roland McGrath's avatar
Roland McGrath committed
348 349 350 351 352 353 354 355 356 357 358 359
	    (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)
360
		     ;; This table has included tables.  Check them.
Roland McGrath's avatar
Roland McGrath committed
361 362 363 364 365 366 367 368 369
		     (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
370
				   ;; Recurse on the list of included tables.
Roland McGrath's avatar
Roland McGrath committed
371 372
				   (tags-table-including this-file
							 tags-included-tables
373
							 core-only
Roland McGrath's avatar
Roland McGrath committed
374 375
							 t))
			     (if found
376 377 378 379 380
				 ;; 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
381 382
					(list tags-table-list
					      tags-table-list-pointer
383 384
					      tags-table-list-started-at)
					tags-table-parent-pointer-list)
385
				       tags-table-list starting-tables
386 387
				       tags-table-list-pointer found
				       tags-table-list-started-at found
388
				       ;; Set a local variable of
389 390
				       ;; our caller, visit-tags-table-buffer.
				       ;; Set it so we won't frob lists later.
391 392
				       visit-tags-table-buffer-cont
				       'included)))
Roland McGrath's avatar
Roland McGrath committed
393
			 (or recursing
394 395 396 397 398 399 400 401 402
			     ;; 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
403 404 405 406 407 408
			     (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
409 410 411 412 413

(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;
414
 just select the buffer visiting `tags-file-name'.
Roland McGrath's avatar
Roland McGrath committed
415
If arg is nil or absent, choose a first buffer from information in
416
 `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
Roland McGrath's avatar
Roland McGrath committed
417
Returns t if it visits a tags table, or nil if there are no more in the list."
418 419

  ;; Set tags-file-name to the tags table file we want to visit.
420 421 422 423 424 425
  (let ((visit-tags-table-buffer-cont cont))
    (cond ((eq visit-tags-table-buffer-cont 'same)
	   ;; Use the ambient value of tags-file-name.
	   (or tags-file-name
	       (error (substitute-command-keys
		       (concat "No tags table in use!  "
426
			       "Use \\[visit-tags-table] to select one.")))))
427 428 429 430 431

	  (visit-tags-table-buffer-cont
	   ;; Find the next table.
	   (if (tags-next-table)
	       ;; Skip over nonexistent files.
432 433 434
	       (let (file)
		 (while (and (setq file
				   (tags-expand-table-name tags-file-name))
435 436
			     (not (or (get-file-buffer file)
				      (file-exists-p file))))
437
		   (tags-next-table)))))
438 439

	  (t
440 441 442 443 444 445 446 447 448
	   ;; We are visiting a table anew, so throw away the previous
	   ;; context of what included tables we were inside of.
	   (while tags-table-parent-pointer-list
	     ;; Set the pointer as if we had iterated through all the
	     ;; tables in the list.
	     (setq tags-table-list-pointer tags-table-list-started-at)
	     ;; Fetching the next table will pop the included-table state.
	     (tags-next-table))

449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464
	   ;; Pick a table out of our hat.
	   (setq tags-file-name
		 (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 VISIT-TAGS-TABLE-BUFFER-CONT
		  ;; will be set non-nil so we don't do it below.
		  (car (or 
			;; First check only tables already in buffers.
			(save-excursion (tags-table-including buffer-file-name
465 466
							      tags-table-list
							      t))
467 468 469 470 471
			;; Since that didn't find any, now do the
			;; expensive version: reading new files.
			(save-excursion (tags-table-including buffer-file-name
							      tags-table-list
							      nil))))
472 473
		  ;; Fourth, use the user variable tags-file-name, if it is
		  ;; not already in tags-table-list.
474
		  (and tags-file-name
475
		       (not (tags-find-table-in-list tags-file-name nil))
476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496
		       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))

497 498
    (if (and (eq visit-tags-table-buffer-cont t)
	     (null tags-table-list-pointer))
499 500 501 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
	;; All out of tables.
	nil

      ;; Verify that tags-file-name is a valid tags table.
      (if (if (get-file-buffer tags-file-name)
	      ;; 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 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.
		;; Propagate the change to tags-file-name and tags-table-list.
		(let ((tail (member tags-file-name 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))

532 533 534 535
	    ;; 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 visit-tags-table-buffer-cont
536
		;; Look in the list for the table we chose.
537 538 539 540 541 542 543 544 545 546 547 548
		;; This updates the list pointers if it finds the table.
		(or (tags-find-table-in-list tags-file-name t)
		    ;; 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-find-table-in-list tags-file-name
								t (car sets))))
			(setq sets (cdr sets)))
		      (if sets
			  ;; Found in some other set.  Switch to that set.
			  (progn
549
			    (or (memq tags-table-list tags-table-set-list)
550
				;; Save the current list.
551 552 553
				(setq tags-table-set-list
				      (cons tags-table-list
					    tags-table-set-list)))
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
			    ;; The list pointers are already up to date;
			    ;; we need only set tags-table-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 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)
			      (setq tags-table-set-list
				    (cons tags-table-list
					  tags-table-set-list)))
			  (setq tags-table-list (list tags-file-name)))

			;; Set the tags table list state variables to point
			;; at the table we want to use first.
			(setq tags-table-list-started-at tags-table-list
			      tags-table-list-pointer tags-table-list)))))
581 582 583 584 585 586 587 588 589 590

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

	;; The buffer was not valid.  Don't use it again.
	(let ((file tags-file-name))
	  (kill-local-variable 'tags-file-name)
	  (if (eq file tags-file-name)
	      (setq tags-file-name nil)))
	(error "File %s is not a valid tags table" buffer-file-name)))))
591

Jim Blandy's avatar
Jim Blandy committed
592 593
(defun file-of-tag ()
  "Return the file name of the file whose tags point is within.
Roland McGrath's avatar
Roland McGrath committed
594 595 596
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
597

Jim Blandy's avatar
Jim Blandy committed
598
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
599 600
(defun tags-table-files ()
  "Return a list of files in the current tags table.
601
Assumes the tags table is the current buffer.
Jim Blandy's avatar
Jim Blandy committed
602
File names returned are absolute."
603 604 605
  (or tags-table-files
      (setq tags-table-files
	    (funcall tags-table-files-function))))
Roland McGrath's avatar
Roland McGrath committed
606 607

(defun tags-included-tables ()
608 609
  "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
610 611 612 613 614 615 616 617 618 619 620 621 622 623
  (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
624 625
	       ;; 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
626
	       (while included
627
		 ;; Visit the buffer.
Roland McGrath's avatar
Roland McGrath committed
628
		 (let ((tags-file-name (car included)))
Roland McGrath's avatar
Roland McGrath committed
629
		   (visit-tags-table-buffer 'same))
630
		 ;; Recurse in that buffer to compute its completion table.
Roland McGrath's avatar
Roland McGrath committed
631
		 (if (tags-completion-table)
632
		     ;; Combine the tables.
Roland McGrath's avatar
Roland McGrath committed
633 634 635 636 637 638 639 640 641 642 643 644 645 646 647
		     (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
648 649 650
    ;; 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
651 652 653
    (if (eq what t)
	(all-completions string (tags-completion-table) predicate)
      (try-completion string (tags-completion-table) predicate))))
654

Jim Blandy's avatar
Jim Blandy committed
655 656 657 658 659
;; 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
660 661 662 663 664 665 666
    (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
667 668 669 670 671 672 673
	       (buffer-substring (point)
				 (progn (forward-sexp -1)
					(while (looking-at "\\s'")
					  (forward-char 1))
					(point))))
      nil)))

Roland McGrath's avatar
Roland McGrath committed
674
;; Read a tag name from the minibuffer with defaulting and completion.
Jim Blandy's avatar
Jim Blandy committed
675
(defun find-tag-tag (string)
Roland McGrath's avatar
Roland McGrath committed
676 677 678 679 680 681 682
  (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)))
683 684 685
    (if (equal spec "")
	(or default (error "There is no default tag"))
      spec)))
Jim Blandy's avatar
Jim Blandy committed
686

Roland McGrath's avatar
Roland McGrath committed
687 688 689
(defvar last-tag nil
  "Last tag found by \\[find-tag].")

690 691 692 693 694 695 696 697 698 699
;; 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)))))

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

707 708 709 710 711
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
712

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

See documentation of variable `tags-file-name'."
716 717
  (interactive (find-tag-interactive "Find tag: "))

718 719
  ;; Save the current buffer's value of `find-tag-hook' before selecting the
  ;; tags table buffer.
Roland McGrath's avatar
Roland McGrath committed
720
  (let ((local-find-tag-hook find-tag-hook))
721 722 723 724 725 726 727 728 729 730 731
    (if (eq '- next-p)
	;; Pop back to a previous location.
	(if (null tags-location-stack)
	    (error "No previous tag locations")
	  (let ((marker (car tags-location-stack)))
	    ;; Pop the stack.
	    (setq tags-location-stack (cdr tags-location-stack))
	    (prog1
		;; Move to the saved location.
		(set-buffer (marker-buffer marker))
	      (goto-char (marker-position marker))
732
	      ;; Kill that marker so it doesn't slow down editing.
733 734 735 736 737 738 739 740 741 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
	      (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))))
      (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))
      (prog1
	  ;; Record the location so we can pop back to it later.
	  (marker-buffer
	   (car
	    (setq tags-location-stack
		  (cons (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
				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)))
			    (set-marker marker (point))))
			tags-location-stack))))
	(run-hooks 'local-find-tag-hook)))))
Jim Blandy's avatar
Jim Blandy committed
768

Jim Blandy's avatar
Jim Blandy committed
769
;;;###autoload
770
(defun find-tag (tagname &optional next-p regexp-p)
Roland McGrath's avatar
Roland McGrath committed
771 772 773
  "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
774

775 776 777 778 779
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
780

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

Jim Blandy's avatar
Jim Blandy committed
786
;;;###autoload
787
(defun find-tag-other-window (tagname &optional next-p regexp-p)
Roland McGrath's avatar
Roland McGrath committed
788
  "Find tag (in current tags table) whose name contains TAGNAME.
789 790 791
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
792

793 794 795 796 797
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
798 799

See documentation of variable `tags-file-name'."
800 801
  (interactive (find-tag-interactive "Find tag other window: "))

802 803 804 805 806
  ;; 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)))
807
	 (tagbuf (find-tag-noselect tagname next-p regexp-p))
808
	 (tagpoint (progn (set-buffer tagbuf) (point))))
809 810
    (set-window-point (prog1
			  (selected-window)
811 812 813 814 815 816 817
			(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))
818
		      window-point)))
Roland McGrath's avatar
Roland McGrath committed
819 820
;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)

Roland McGrath's avatar
Roland McGrath committed
821
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
822
(defun find-tag-other-frame (tagname &optional next-p)
823 824 825 826 827 828 829 830 831 832
  "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
833

Roland McGrath's avatar
Roland McGrath committed
834
See documentation of variable `tags-file-name'."
835
  (interactive (find-tag-interactive "Find tag other frame: "))
Roland McGrath's avatar
Roland McGrath committed
836 837 838 839
  (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
840
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
841 842 843
(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
844

845 846 847 848 849
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
850 851 852 853

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

See documentation of variable `tags-file-name'."
854 855 856 857
  (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))
Roland McGrath's avatar
Roland McGrath committed
858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877

;; 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.

878 879 880 881 882 883
(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
884 885 886 887 888 889 890 891
  (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
892 893 894
      (or first-search			;find-tag-noselect has already done it.
	  (visit-tags-table-buffer 'same))

Roland McGrath's avatar
Roland McGrath committed
895 896
      ;; Get a qualified match.
      (catch 'qualified-match-found
Roland McGrath's avatar
Roland McGrath committed
897

898
	;; Iterate over the list of tags tables.
Roland McGrath's avatar
Roland McGrath committed
899 900 901 902 903 904
	(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
905 906 907 908
	  (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
909 910

	  (setq tags-table-file buffer-file-name)
911
	  ;; Iterate over the list of ordering predicates.
Roland McGrath's avatar
Roland McGrath committed
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
	  (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))

939
      ;; Get the local value in the tags table buffer before switching buffers.
Roland McGrath's avatar
Roland McGrath committed
940 941 942 943 944 945 946 947 948 949 950 951 952 953 954 955 956 957 958
      (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.

959 960
;; 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
961
(defun etags-recognize-tags-table ()
962
  (and (etags-verify-tags-table)
Jim Blandy's avatar
Jim Blandy committed
963 964 965
       ;; 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
966
       (mapcar (function (lambda (elt)
967
			   (set (make-local-variable (car elt)) (cdr elt))))
Roland McGrath's avatar
Roland McGrath committed
968 969 970 971 972 973 974
	       '((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))
975
		 (find-tag-regexp-next-line-after-failure-p . t)
Roland McGrath's avatar
Roland McGrath committed
976 977 978 979 980 981 982 983 984 985
		 (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)
		 ))))

986
;; Return non-nil iff the current buffer is a valid etags TAGS file.
Roland McGrath's avatar
Roland McGrath committed
987
(defun etags-verify-tags-table ()
988 989
  ;; Use eq instead of = in case char-after returns nil.
  (eq (char-after 1) ?\f))
Roland McGrath's avatar
Roland McGrath committed
990 991 992 993 994 995 996 997 998 999 1000 1001

(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))
1002 1003 1004 1005
      ;; 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
1006 1007 1008 1009
      ;;   \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.
1010
      (while (re-search-forward
1011
	      "^\\(\\(.+[^-a-zA-Z0-9_$]+\\)?\\([-a-zA-Z0-9_$]+\\)\
1012 1013
\[^-a-zA-Z0-9_$]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\
\\([0-9]+\\)?,\\([0-9]+\\)?\n"
1014
	      nil t)
1015
	(intern	(if (match-beginning 5)
1016
		    ;; There is an explicit tag name.
1017
		    (buffer-substring (match-beginning 5) (match-end 5))
1018 1019 1020
		  ;; No explicit tag name.  Best guess.
		  (buffer-substring (match-beginning 3) (match-end 3)))
		table)))
Roland McGrath's avatar
Roland McGrath committed
1021 1022 1023
    table))

(defun etags-snarf-tag ()
1024
  (let (tag-text line startpos)
Roland McGrath's avatar
Roland McGrath committed
1025 1026 1027 1028
    (search-forward "\177")
    (setq tag-text (buffer-substring (1- (point))
				     (save-excursion (beginning-of-line)
						     (point))))
1029 1030
    ;; Skip explicit tag name if present.
    (search-forward "\001" (save-excursion (forward-line 1) (point)) t)
1031 1032
    (if (looking-at "[0-9]")
	(setq line (string-to-int (buffer-substring
Roland McGrath's avatar
Roland McGrath committed
1033 1034
				   (point)
				   (progn (skip-chars-forward "0-9")
1035 1036 1037 1038 1039 1040 1041
					  (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
1042 1043
    ;; Leave point on the next line of the tags file.
    (forward-line 1)
1044 1045 1046 1047 1048 1049 1050 1051
    (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
;; TEXT within the file (starting from 1); LINE is the line number.  Either
;; 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
1052
(defun etags-goto-tag-location (tag-info)
1053
  (let ((startpos (cdr (cdr tag-info)))
Roland McGrath's avatar
Roland McGrath committed
1054 1055 1056 1057 1058 1059
	;; 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)
1060 1061 1062
	(pat (concat (if (eq selective-display t)
			 "\\(^\\|\^m\\)" "^")
		     (regexp-quote (car tag-info)))))
1063 1064 1065 1066 1067
    ;; If no char pos was given, try the given line number.
    (or startpos
	(if (car (cdr tag-info))
	    (setq startpos (progn (goto-line (car (cdr tag-info)))
				  (point)))))
Roland McGrath's avatar
Roland McGrath committed
1068 1069
    (or startpos
	(setq startpos (point-min)))
1070 1071 1072
    ;; First see if the tag is right at the specified location.
    (goto-char startpos)
    (setq found (looking-at pat))
Roland McGrath's avatar
Roland McGrath committed
1073 1074 1075 1076 1077 1078 1079 1080 1081
    (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)
1082
	(error "Rerun etags: `%s' not found in %s"
Roland McGrath's avatar
Roland McGrath committed
1083
	       pat buffer-file-name)))
1084 1085 1086 1087 1088
  ;; 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))
Roland McGrath's avatar
Roland McGrath committed
1089 1090 1091 1092 1093 1094 1095 1096
  (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")))
1097 1098 1099 1100 1101 1102 1103 1104 1105
      (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.
		   (buffer-substring (point)
				     (progn (skip-chars-forward "^\001")
					    (point)))
		 tag)))
Roland McGrath's avatar
Roland McGrath committed
1106
      (terpri)
1107 1108
      (forward-line 1))
    t))
Roland McGrath's avatar
Roland McGrath committed
1109 1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124 1125 1126 1127 1128 1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143 1144 1145 1146 1147

(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.

1148 1149
;; 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
1150 1151 1152
(defun recognize-empty-tags-table ()
  (and (zerop (buffer-size))
       (mapcar (function (lambda (sym)
1153
			   (set (make-local-variable sym) 'ignore)))
Roland McGrath's avatar
Roland McGrath committed
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164
	       '(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.
1165
;;; XXX these functions assume etags file format.
Roland McGrath's avatar
Roland McGrath committed
1166

Roland McGrath's avatar
Roland McGrath committed
1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181
;; 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
1182

Roland McGrath's avatar
Roland McGrath committed
1183 1184
;; 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
1185
(defun tag-exact-match-p (tag)
1186 1187 1188 1189 1190 1191 1192 1193
  ;; The match is really exact if there is an explicit tag name.
  (or (looking-at (concat "[^\177]*\177" (regexp-quote tag) "\001"))
      ;; We also call it "exact" if it is surrounded by symbol boundaries.
      ;; This is needed because etags does not always generate explicit names.
      (and (looking-at "\\Sw.*\177") (looking-at "\\S_.*\177")
	   (save-excursion
	     (backward-char (1+ (length tag)))
	     (and (looking-at "\\Sw") (looking-at "\\S_"))))))
Roland McGrath's avatar
Roland McGrath committed
1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204

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

Roland McGrath's avatar
Roland McGrath committed
1206 1207 1208 1209 1210 1211 1212 1213
;; 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
1214
;;;###autoload
Roland McGrath's avatar
Roland McGrath committed
1215 1216
(defun next-file (&optional initialize novisit)
  "Select next file among files in current tags table.
1217