(next-file): If arg is neither t nor nil, then eval it to return initial
[bpt/emacs.git] / lisp / progmodes / etags.el
index 1bc13f5..452d9ad 100644 (file)
@@ -1,6 +1,7 @@
-;; etags.el --- etags facility for Emacs
+;;; etags.el --- etags facility for Emacs
 
-;; Copyright (C) 1985, 1986, 1988, 1989, 1992 Free Software Foundation, Inc.
+;; Copyright (C) 1985, 1986, 1988, 1989, 1992, 1993
+;;     Free Software Foundation, Inc.
 
 ;; Author: Roland McGrath <roland@gnu.ai.mit.edu>
 ;; Keywords: tools
 ;;; Code:
 
 ;;;###autoload
-(defvar tags-file-name nil "\
-*File name of tags table.
+(defvar tags-file-name nil
+  "*File name of tags table.
 To switch to a new tags table, setting this variable is sufficient.
+If you set this variable, do not also set `tags-table-list'.
 Use the `etags' program to make a tags table file.")
+;; Make M-x set-variable tags-file-name like M-x visit-tags-table.
 ;;;###autoload (put 'tags-file-name 'variable-interactive "fVisit tags table: ")
 
 ;;;###autoload
+;; Use `visit-tags-table-buffer' to cycle through tags tables in this list.
 (defvar tags-table-list nil
-  "*List of names of tags table files which are currently being searched.
-Elements that are directories mean the file \"TAGS\" in that directory.
-An element of nil means to look for a file \"TAGS\" in the current directory.
-Use `visit-tags-table-buffer' to cycle through tags tables in this list.")
+  "*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.")
 
 (defvar tags-table-list-pointer nil
   "Pointer into `tags-table-list' where the current state of searching is.
@@ -72,6 +77,10 @@ Otherwise, `find-tag-default' is used.")
 (defvar default-tags-table-function nil
   "*If non-nil, a function of no arguments to choose a default tags file
 for a particular buffer.")
+
+(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].")
 \f
 ;; Tags table state.
 ;; These variables are local in tags table buffers.
@@ -132,20 +141,18 @@ One argument, the tag info returned by `snarf-tag-function'.")
 (defvar tags-included-tables-function nil
   "Function to do the work of `tags-included-tables' (which see).")
 (defvar verify-tags-table-function nil
-  "Function to return t iff the current buffer vontains a valid
+  "Function to return t iff the current buffer contains a valid
 \(already initialized\) tags file.")
 \f
+;; 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.
 (defun initialize-new-tags-table ()
-  "Initialize the tags table in the current buffer.
-Returns non-nil iff it is a valid tags table."
-  (make-local-variable 'tag-lines-already-matched)
-  (make-local-variable 'tags-table-files)
-  (make-local-variable 'tags-completion-table)
-  (make-local-variable 'tags-included-tables)
-  (setq tags-table-files nil
-       tag-lines-already-matched nil
-       tags-completion-table nil
-       tags-included-tables nil)
+  (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)
   ;; Value is t if we have found a valid tags table buffer.
   (let ((hooks tags-table-format-hooks))
     (while (and hooks
@@ -170,68 +177,79 @@ file the tag was in."
                                                       default-directory)
                                     t)
                     current-prefix-arg))
+  ;; 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.
   (let ((tags-file-name file))
     (save-excursion
       (or (visit-tags-table-buffer 'same)
          (signal 'file-error (list "Visiting tags table"
                                    "file does not exist"
                                    file)))
+      ;; Set FILE to the expanded name.
       (setq file tags-file-name)))
   (if local
+      ;; Set the local value of tags-file-name.
       (set (make-local-variable 'tags-file-name) file)
+    ;; Set the global value of tags-file-name.
     (setq-default tags-file-name file)))
 
 ;; Move tags-table-list-pointer along and set tags-file-name.
+;; If NO-INCLUDES is non-nil, ignore included tags tables.
 ;; Returns nil when out of tables.
-(defun tags-next-table (&optional reset no-includes)
-  (if reset
-      (setq tags-table-list-pointer tags-table-list)
-
-    (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
-             (cons (cons tags-table-list-pointer tags-table-list-started-at)
-                   tags-table-parent-pointer-list)
-             tags-table-list-pointer tags-included-tables
-             tags-table-list-started-at tags-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.
-         (if tags-table-parent-pointer-list
-             ;; Pop back to the tags table which includes this one.
-             (progn
-               (setq tags-table-list-pointer
-                     (car (car tags-table-parent-pointer-list))
-                     tags-table-list-started-at
-                     (cdr (car tags-table-parent-pointer-list))
-                     tags-table-parent-pointer-list
-                     (cdr tags-table-parent-pointer-list))
-               (tags-next-table nil t))
-           ;; All out of tags tables.
-           (setq tags-table-list-pointer nil))))
-
-    (and tags-table-list-pointer
-        (setq tags-file-name
-              (tags-expand-table-name (car tags-table-list-pointer))))))
-
+(defun tags-next-table (&optional no-includes)
+  ;; Do we have any included tables?
+  (if (and (not no-includes)
+          (visit-tags-table-buffer 'same)
+          (tags-included-tables))
+
+      ;; Move into the included tags tables.
+      (setq tags-table-parent-pointer-list
+           ;; Save the current state of what table we are in.
+           (cons (cons tags-table-list-pointer tags-table-list-started-at)
+                 tags-table-parent-pointer-list)
+           ;; Start the pointer in the list of included tables.
+           tags-table-list-pointer tags-included-tables
+           tags-table-list-started-at tags-included-tables)
+
+    ;; No included tables.  Go to the next table in the list.
+    (setq tags-table-list-pointer
+         (cdr tags-table-list-pointer))
+    (or tags-table-list-pointer
+       ;; Wrap around.
+       (setq tags-table-list-pointer tags-table-list))
+
+    (if (eq tags-table-list-pointer tags-table-list-started-at)
+       ;; We have come full circle.  No more tables.
+       (if tags-table-parent-pointer-list
+           ;; Pop back to the tags table which includes this one.
+           (progn
+             ;; Restore the state variables.
+             (setq tags-table-list-pointer
+                   (car (car tags-table-parent-pointer-list))
+                   tags-table-list-started-at
+                   (cdr (car tags-table-parent-pointer-list))
+                   tags-table-parent-pointer-list
+                   (cdr tags-table-parent-pointer-list))
+             ;; Recurse to skip to the next table after the parent.
+             (tags-next-table t))
+         ;; All out of tags tables.
+         (setq tags-table-list-pointer nil))))
+
+  (and tags-table-list-pointer
+       ;; Set tags-file-name to the fully-expanded name.
+       (setq tags-file-name
+            (tags-expand-table-name (car tags-table-list-pointer)))))
+
+;; Expand tags table name FILE into a complete file name.
 (defun tags-expand-table-name (file)
-  (or file
-      ;; nil means look for TAGS in current directory.
-      (setq file default-directory))
   (setq file (expand-file-name file))
   (if (file-directory-p file)
       (expand-file-name "TAGS" file)
     file))
 
+;; Return the cdr of LIST (default: tags-table-list) whose car
+;; is equal to FILE after tags-expand-table-name on both sides.
 (defun tags-table-list-member (file &optional list)
   (or list
       (setq list tags-table-list))
@@ -241,16 +259,25 @@ file the tag was in."
     (setq list (cdr list)))
   list)
 
+;; Local var in visit-tags-table-buffer-cont
+;; which is set by tags-table-including.
+(defvar visit-tags-table-buffer-cont)
+
 ;; Subroutine of visit-tags-table-buffer.  Frobs its local vars.
-;; Search TABLES for one that has tags for THIS-FILE.
-;; Recurses on included tables.
-(defun tags-table-including (this-file tables &optional recursing)
+;; 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)
   (let ((found nil))
+    ;; Loop over TABLES, looking for one containing tags for THIS-FILE.
     (while (and (not found)
                tables)
       (let ((tags-file-name (tags-expand-table-name (car tables))))
        (if (or (get-file-buffer tags-file-name)
-               (file-exists-p tags-file-name))
+               (and (not core-only)
+                    (file-exists-p tags-file-name)))
            (progn
              ;; Select the tags table buffer and get the file list up to date.
              (visit-tags-table-buffer 'same)
@@ -263,6 +290,7 @@ file the tag was in."
                     (setq found tables))
 
                    ((tags-included-tables)
+                    ;; This table has included tables.  Check them.
                     (let ((old tags-table-parent-pointer-list))
                       (unwind-protect
                           (progn
@@ -272,24 +300,37 @@ file the tag was in."
                                 ;; collect just the elts from this run.
                                 (setq tags-table-parent-pointer-list nil))
                             (setq found
+                                  ;; Recurse on the list of included tables.
                                   (tags-table-including this-file
                                                         tags-included-tables
+                                                        core-only
                                                         t))
                             (if found
-                                (progn
-                                  (setq tags-table-parent-pointer-list
-                                        (cons
-                                         (cons tags-table-list-pointer
-                                               tags-table-list-started-at)
-                                         tags-table-parent-pointer-list)
-                                        tags-table-list-pointer found
-                                        tags-table-list-started-at found
-                                        ;; Don't frob lists later.
-                                        cont 'included))))
+                                ;; One of them lists THIS-FILE.
+                                ;; Set the table list state variables to move
+                                ;; us inside the list of included tables.
+                                (setq tags-table-parent-pointer-list
+                                      (cons
+                                       (cons tags-table-list-pointer
+                                             tags-table-list-started-at)
+                                       tags-table-parent-pointer-list)
+                                      tags-table-list-pointer found
+                                      tags-table-list-started-at found
+                                      ;; Set a local variable of
+                                      ;; our caller, visit-tags-table-buffer.
+                                      ;; Set it so we won't frob lists later.
+                                      visit-tags-table-buffer-cont
+                                      'included)))
                         (or recursing
-                            ;; Recursive calls have consed onto the front
-                            ;; of the list, so it is now outermost first.
-                            ;; We want it innermost first.
+                            ;; 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.
                             (setq tags-table-parent-pointer-list
                                   (nconc (nreverse
                                           tags-table-parent-pointer-list)
@@ -301,123 +342,170 @@ file the tag was in."
   "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;
- just select the buffer.
+ just select the buffer visiting `tags-file-name'.
 If arg is nil or absent, choose a first buffer from information in
-`tags-file-name', `tags-table-list', `tags-table-list-pointer'.
+ `tags-file-name', `tags-table-list', `tags-table-list-pointer'.
 Returns t if it visits a tags table, or nil if there are no more in the list."
-  (cond ((eq cont 'same)
-        ;; We don't need to look for a setting for tags-file-name,
-        ;; but other than that we should behave just as if passed nil.
-        (setq cont nil))
-
-       (cont
-        (if (tags-next-table)
-            ;; Skip over nonexistent files.
-            (while (and (let ((file (tags-expand-table-name tags-file-name)))
-                          (not (or (get-file-buffer file)
-                                   (file-exists-p file))))
-                        (tags-next-table)))))
 
-       (t
-        (setq tags-file-name
-              (or (cdr (assq 'tags-file-name (buffer-local-variables)))
-                  (and default-tags-table-function
-                       (funcall default-tags-table-function))
-                  ;; 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.
-                  (save-excursion
-                    (car (tags-table-including buffer-file-name
-                                               tags-table-list)))
-                  (car tags-table-list)
-                  tags-file-name
-                  (expand-file-name
-                   (read-file-name "Visit tags table: (default TAGS) "
-                                   default-directory
-                                   "TAGS"
-                                   t))))))
-
-  (setq tags-file-name (tags-expand-table-name tags-file-name))
-
-  (if (and cont (null tags-table-list-pointer))
-      ;; All out of tables.
-      nil
+  ;; Set tags-file-name to the tags table file we want to visit.
+  (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!  "
+                              "Use \\[visit-tags-table] to select one."))))
+          ;; Set VISIT-TAGS-TABLE-BUFFER-CONT to nil
+          ;; so the code below will make sure tags-file-name
+          ;; is in tags-table-list.
+          (setq visit-tags-table-buffer-cont nil))
+
+         (visit-tags-table-buffer-cont
+          ;; Find the next table.
+          (if (tags-next-table)
+              ;; Skip over nonexistent files.
+              (while (and (let ((file (tags-expand-table-name tags-file-name)))
+                            (not (or (get-file-buffer file)
+                                     (file-exists-p file))))
+                          (tags-next-table)))))
 
-    (if (if (get-file-buffer tags-file-name)
-           (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 change to tags-file-name and tags-table-list.
-             (let ((tail (member file tags-table-list)))
-               (if tail
-                   (setcar tail buffer-file-name))
-               (setq tags-file-name buffer-file-name)))
-         (initialize-new-tags-table))
-
-       ;; We have a valid tags table.
-       (progn
-         ;; Bury the tags table buffer so it
-         ;; doesn't get in the user's way.
-         (bury-buffer (current-buffer))
-       
-         (if cont
-             ;; No list frobbing required.
-             nil
-
-           ;; Look in the list for the table we chose.
-           (let ((elt (tags-table-list-member tags-file-name)))
-             (or elt
-                 ;; The table is not in the current set.
-                 ;; Try to find it in another previously used set.
-                 (let ((sets tags-table-set-list))
-                   (while (and sets
-                               (not (setq elt (tags-table-list-member
-                                               tags-file-name (car sets)))))
-                     (setq sets (cdr sets)))
-                   (if sets
-                       (progn
+         (t
+          ;; 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
+                                                                tags-table-list
+                                                                t))
+                       ;; 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))))
+                 ;; Fourth, use the user variable tags-file-name, if it is not
+                 ;; already in tags-table-list.
+                 (and tags-file-name
+                      (not (tags-table-list-member tags-file-name))
+                      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 visit-tags-table-buffer-cont t) (null tags-table-list-pointer))
+       ;; 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))
+
+           (if visit-tags-table-buffer-cont
+               ;; No list frobbing required.
+               nil
+
+             ;; Look in the list for the table we chose.
+             (let ((elt (tags-table-list-member tags-file-name)))
+               (or elt
+                   ;; The table is not in the current set.
+                   ;; Try to find it in another previously used set.
+                   (let ((sets tags-table-set-list))
+                     (while (and sets
+                                 (not (setq elt (tags-table-list-member
+                                                 tags-file-name (car sets)))))
+                       (setq sets (cdr sets)))
+                     (if sets
                          ;; 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
+                                (y-or-n-p (concat "Add " tags-file-name
+                                                  " to current list"
+                                                  " of tags tables? ")))
+                           ;; Add it to the current list.
+                           (setq tags-table-list (cons tags-file-name
+                                                       tags-table-list))
+                         ;; Make a fresh list, and store the old one.
                          (or (memq tags-table-list tags-table-set-list)
-                             ;; 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
-                              (y-or-n-p (concat "Add " tags-file-name
-                                                " to current list"
-                                                " of tags tables? ")))
-                         ;; Add it to the current list.
-                         (setq tags-table-list (cons tags-file-name
-                                                     tags-table-list))
-                       ;; Make a fresh list, and store the old one.
-                       (or (memq tags-table-list tags-table-set-list)
-                           (setq tags-table-set-list
-                                 (cons tags-table-list tags-table-set-list)))
-                       (setq tags-table-list (list tags-file-name)))
-                     (setq elt tags-table-list))))
-
-             (setq tags-table-list-started-at elt
-                   tags-table-list-pointer elt))))
-
-      ;; The buffer was not valid.  Don't use it again.
-      (kill-local-variable 'tags-file-name)
-      (setq tags-file-name nil)
-      (error "File %s is not a valid tags table" buffer-file-name))))
-
+                                   (cons tags-table-list tags-table-set-list)))
+                         (setq tags-table-list (list tags-file-name)))
+                       (setq elt tags-table-list))))
+
+               ;; Set the tags table list state variables to point at the table
+               ;; we want to use first.
+               (setq tags-table-list-started-at elt
+                     tags-table-list-pointer elt)))
+
+           ;; 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)))))
+\f
 (defun file-of-tag ()
   "Return the file name of the file whose tags point is within.
 Assumes the tags table is the current buffer.
@@ -427,15 +515,15 @@ File name returned is relative to tags table file's directory."
 ;;;###autoload
 (defun tags-table-files ()
   "Return a list of files in the current tags table.
+Assumes the tags table is the current buffer.
 File names returned are absolute."
-  (save-excursion
-    (visit-tags-table-buffer)
-    (or tags-table-files
-       (setq tags-table-files
-             (funcall tags-table-files-function)))))
+  (or tags-table-files
+      (setq tags-table-files
+           (funcall tags-table-files-function))))
 
 (defun tags-included-tables ()
-  "Return a list of tags tables included by the current table."
+  "Return a list of tags tables included by the current table.
+Assumes the tags table is the current buffer."
   (or tags-included-tables
       (setq tags-included-tables (funcall tags-included-tables-function))))
 \f
@@ -450,10 +538,15 @@ File names returned are absolute."
           (let ((included (tags-included-tables))
                 (table (funcall tags-completion-table-function)))
             (save-excursion
+              ;; Iterate over the list of included tables, and combine each
+              ;; included table's completion obarray to the parent obarray.
               (while included
+                ;; Visit the buffer.
                 (let ((tags-file-name (car included)))
                   (visit-tags-table-buffer 'same))
+                ;; Recurse in that buffer to compute its completion table.
                 (if (tags-completion-table)
+                    ;; Combine the tables.
                     (mapatoms (function
                                (lambda (sym)
                                  (intern (symbol-name sym) table)))
@@ -473,7 +566,7 @@ File names returned are absolute."
     (if (eq what t)
        (all-completions string (tags-completion-table) predicate)
       (try-completion string (tags-completion-table) predicate))))
-
+\f
 ;; Return a default tag to search for, based on the text at point.
 (defun find-tag-default ()
   (save-excursion
@@ -502,100 +595,159 @@ File names returned are absolute."
                                    (format "%s(default %s) " string default)
                                  string)
                                'tags-complete-tag)))
-    (list (if (equal spec "")
-             (or default (error "There is no default tag"))
-           spec))))
+    (if (equal spec "")
+       (or default (error "There is no default tag"))
+      spec)))
 
 (defvar last-tag nil
   "Last tag found by \\[find-tag].")
 
+;; 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)))))
+
 ;;;###autoload
 (defun find-tag-noselect (tagname &optional next-p regexp-p)
   "Find tag (in current tags table) whose name contains TAGNAME.
-Returns the buffer containing the tag's definition moves its point there,
+Returns the buffer containing the tag's definition and moves its point there,
 but does not select the buffer.
 The default for TAGNAME is the expression in the buffer near point.
 
-If second arg NEXT-P is non-nil (interactively, with prefix arg), search
-for another tag that matches the last tagname or regexp used.  When there
-are multiple matches for a tag, more exact matches are found first.
+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.
 
 If third arg REGEXP-P is non-nil, treat TAGNAME as a regexp.
 
 See documentation of variable `tags-file-name'."
-  (interactive (if current-prefix-arg
-                  '(nil t)
-                (find-tag-tag "Find tag: ")))
+  (interactive (find-tag-interactive "Find tag: "))
+
+  ;; Save the current buffer's value of `find-tag-hook' before selecting the
+  ;; tags table buffer.
   (let ((local-find-tag-hook find-tag-hook))
-    (if next-p
-       (visit-tags-table-buffer 'same)
-      (setq last-tag tagname)
-      (visit-tags-table-buffer))
-    (prog1
-       (find-tag-in-order (if next-p last-tag tagname)
-                          (if regexp-p
-                              find-tag-regexp-search-function
-                            find-tag-search-function)
-                          (if regexp-p
-                              find-tag-regexp-tag-order
-                            find-tag-tag-order)
-                          (if regexp-p
-                              find-tag-regexp-next-line-after-failure-p
-                            find-tag-next-line-after-failure-p)
-                          (if regexp-p "matching" "containing")
-                          (not next-p))
-      (run-hooks 'local-find-tag-hook))))
+    (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))
+             ;; Kill that marker so it doesn't slow down editing.
+             (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)))))
 
 ;;;###autoload
-(defun find-tag (tagname &optional next-p)
+(defun find-tag (tagname &optional next-p regexp-p)
   "Find tag (in current tags table) whose name contains TAGNAME.
 Select the buffer containing the tag's definition, and move point there.
 The default for TAGNAME is the expression in the buffer around or before point.
 
-If second arg NEXT-P is non-nil (interactively, with prefix arg), search
-for another tag that matches the last tagname used.  When there are
-multiple matches, more exact matches are found first.
+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.
 
 See documentation of variable `tags-file-name'."
-  (interactive (if current-prefix-arg
-                  '(nil t)
-                (find-tag-tag "Find tag: ")))
-  (switch-to-buffer (find-tag-noselect tagname next-p)))
+  (interactive (find-tag-interactive "Find tag: "))
+  (switch-to-buffer (find-tag-noselect tagname next-p regexp-p)))
 ;;;###autoload (define-key esc-map "." 'find-tag)
 
 ;;;###autoload
-(defun find-tag-other-window (tagname &optional next-p)
+(defun find-tag-other-window (tagname &optional next-p regexp-p)
   "Find tag (in current tags table) whose name contains TAGNAME.
-Select the buffer containing the tag's definition
-in another window, and move point there.
-The default for TAGNAME is the expression in the buffer around or before point.
+Select the buffer containing the tag's definition in another window, and
+move point there.  The default for TAGNAME is the expression in the buffer
+around or before point.
 
-If second arg NEXT-P is non-nil (interactively, with prefix arg), search
-for another tag that matches the last tagname used.  When there are
-multiple matches, more exact matches are found first.
+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.
 
 See documentation of variable `tags-file-name'."
-  (interactive (if current-prefix-arg
-                  '(nil t)
-                (find-tag-tag "Find tag other window: ")))
-  (switch-to-buffer-other-window (find-tag-noselect tagname next-p)))
+  (interactive (find-tag-interactive "Find tag other window: "))
+
+  ;; This hair is to deal with the case where the tag is found in the
+  ;; selected window's buffer; without the hair, point is moved in both
+  ;; windows.  To prevent this, we save the selected window's point before
+  ;; doing find-tag-noselect, and restore it after.
+  (let* ((window-point (window-point (selected-window)))
+        (tagbuf (find-tag-noselect tagname next-p regexp-p))
+        (tagpoint (progn (set-buffer tagbuf) (point))))
+    (set-window-point (prog1
+                         (selected-window)
+                       (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))
+                     window-point)))
 ;;;###autoload (define-key ctl-x-4-map "." 'find-tag-other-window)
 
 ;;;###autoload
 (defun find-tag-other-frame (tagname &optional next-p)
-  "Find tag (in current tag table) whose name contains TAGNAME.
- Selects the buffer that the tag is contained in in another frame
-and puts point at its definition.
- If TAGNAME is a null string, the expression in the buffer
-around or before point is used as the tag name.
- If second arg NEXT-P is non-nil (interactively, with prefix arg),
-searches for the next tag in the tag table
-that matches the tagname used in the previous find-tag.
+  "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.
 
 See documentation of variable `tags-file-name'."
-  (interactive (if current-prefix-arg
-                  '(nil t)
-                  (find-tag-tag "Find tag other window: ")))
+  (interactive (find-tag-interactive "Find tag other frame: "))
   (let ((pop-up-frames t))
     (find-tag-other-window tagname next-p)))
 ;;;###autoload (define-key ctl-x-5-map "." 'find-tag-other-frame)
@@ -605,17 +757,19 @@ See documentation of variable `tags-file-name'."
   "Find tag (in current tags table) whose name matches REGEXP.
 Select the buffer containing the tag's definition and move point there.
 
-If second arg NEXT-P is non-nil (interactively, with prefix arg), search
-for another tag that matches the last tagname used.
+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.
 
 If third arg OTHER-WINDOW is non-nil, select the buffer in another window.
 
 See documentation of variable `tags-file-name'."
-  (interactive (if current-prefix-arg
-                  '(nil t)
-                (read-string "Find tag regexp: ")))
-  (funcall (if other-window 'switch-to-buffer-other-window 'switch-to-buffer)
-          (find-tag-noselect regexp next-p t)))
+  (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))
 \f
 ;; Internal tag finding function.
 
@@ -636,9 +790,12 @@ See documentation of variable `tags-file-name'."
 ;; specified source file and return.  Qualified matches are remembered to
 ;; avoid repetition.  State is saved so that the loop can be continued.
 
-(defun find-tag-in-order (pattern search-forward-func order
-                                 next-line-after-failure-p matching
-                                 first-search)
+(defun find-tag-in-order (pattern
+                         search-forward-func
+                         order
+                         next-line-after-failure-p
+                         matching
+                         first-search)
   (let (file                           ;name of file containing tag
        tag-info                        ;where to find the tag in FILE
        tags-table-file                 ;name of tags file
@@ -653,6 +810,7 @@ See documentation of variable `tags-file-name'."
       ;; Get a qualified match.
       (catch 'qualified-match-found
 
+       ;; Iterate over the list of tags tables.
        (while (or first-table
                   (visit-tags-table-buffer t))
 
@@ -665,6 +823,7 @@ See documentation of variable `tags-file-name'."
          (setq first-table nil)
 
          (setq tags-table-file buffer-file-name)
+         ;; Iterate over the list of ordering predicates.
          (while order
            (while (funcall search-forward-func pattern nil t)
              ;; Naive match found.  Qualify the match.
@@ -692,7 +851,7 @@ See documentation of variable `tags-file-name'."
       (setq file (expand-file-name (file-of-tag))
            tag-info (funcall snarf-tag-function))
 
-      ;; Get the local value in the tags table buffer.
+      ;; Get the local value in the tags table buffer before switching buffers.
       (setq goto-func goto-tag-location-function)
 
       ;; Find the right line in the specified file.
@@ -712,14 +871,15 @@ See documentation of variable `tags-file-name'."
 \f
 ;; `etags' TAGS file format support.
 
+;; If the current buffer is a valid etags TAGS file, give it local values of
+;; the tags table format variables, and return non-nil.
 (defun etags-recognize-tags-table ()
-  (and (eq (char-after 1) ?\f)
+  (and (etags-verify-tags-table)
        ;; 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)
        (mapcar (function (lambda (elt)
-                          (make-local-variable (car elt))
-                          (set (car elt) (cdr elt))))
+                          (set (make-local-variable (car elt)) (cdr elt))))
               '((file-of-tag-function . etags-file-of-tag)
                 (tags-table-files-function . etags-tags-table-files)
                 (tags-completion-table-function . etags-tags-completion-table)
@@ -727,7 +887,7 @@ See documentation of variable `tags-file-name'."
                 (goto-tag-location-function . etags-goto-tag-location)
                 (find-tag-regexp-search-function . re-search-forward)
                 (find-tag-regexp-tag-order . (tag-re-match-p))
-                (find-tag-regexp-next-line-after-failuire-p . t)
+                (find-tag-regexp-next-line-after-failure-p . t)
                 (find-tag-search-function . search-forward)
                 (find-tag-tag-order . (tag-exact-match-p tag-word-match-p
                                                          tag-any-match-p))
@@ -738,8 +898,10 @@ See documentation of variable `tags-file-name'."
                 (verify-tags-table-function . etags-verify-tags-table)
                 ))))
 
+;; Return non-nil iff the current buffer is a valid etags TAGS file.
 (defun etags-verify-tags-table ()
-  (= (char-after 1) ?\f))
+  ;; Use eq instead of = in case char-after returns nil.
+  (eq (char-after 1) ?\f))
 
 (defun etags-file-of-tag ()
   (save-excursion
@@ -752,25 +914,24 @@ See documentation of variable `tags-file-name'."
   (let ((table (make-vector 511 0)))
     (save-excursion
       (goto-char (point-min))
-      (while (search-forward "\177" nil t)
-       ;; Handle multiple \177's on a line.
-       (save-excursion
-         (skip-chars-backward "^-A-Za-z0-9_$\n") ;sym syntax? XXX
-         (or (bolp)
-             (intern (buffer-substring
-                      (point)
-                      (progn
-                        (skip-chars-backward "-A-Za-z0-9_$")
-                        ;; ??? New
-                        ;; `::' in the middle of a C++ tag.
-                        (and (= (preceding-char) ?:)
-                             (= (char-after (- (point) 2)) ?:)
-                             (progn
-                               (backward-char 2)
-                               (skip-chars-backward
-                                "-A-Za-z0-9_$")))
-                        (point)))
-                     table)))))
+      ;; 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
+      ;;   \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.
+      (while (re-search-forward
+             "^\\(\\(.+[^-a-zA-Z0-9_$]+\\)?\\([-a-zA-Z0-9_$]+\\)\
+\[^-a-zA-Z0-9_$]*\\)\177\\(\\([^\n\001]+\\)\001\\)?\\([0-9]+\\),\\([0-9]+\\)\n"
+             nil t)
+       (intern (if (match-beginning 5)
+                   ;; There is an explicit tag name.
+                   (buffer-substring (match-beginning 5) (match-end 5))
+                 ;; No explicit tag name.  Best guess.
+                 (buffer-substring (match-beginning 3) (match-end 3)))
+               table)))
     table))
 
 (defun etags-snarf-tag ()
@@ -779,6 +940,8 @@ See documentation of variable `tags-file-name'."
     (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)
     (search-forward ",")
     (setq startpos (string-to-int (buffer-substring
                                   (point)
@@ -796,7 +959,9 @@ See documentation of variable `tags-file-name'."
        ;; costs about as much as searching 2000 chars.
        (offset 1000)
        (found nil)
-       (pat (concat "^" (regexp-quote (car tag-info)))))
+       (pat (concat (if (eq selective-display t)
+                        "\\(^\\|\^m\\)" "^")
+                    (regexp-quote (car tag-info)))))
     (or startpos
        (setq startpos (point-min)))
     (while (and (not found)
@@ -810,6 +975,11 @@ See documentation of variable `tags-file-name'."
        (re-search-forward pat nil t)
        (error "`%s' not found in %s; time to rerun etags"
               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))
 
 (defun etags-list-tags (file)
@@ -862,11 +1032,12 @@ See documentation of variable `tags-file-name'."
 \f
 ;; Empty tags file support.
 
+;; Recognize an empty file and give it local values of the tags table format
+;; variables which do nothing.
 (defun recognize-empty-tags-table ()
   (and (zerop (buffer-size))
        (mapcar (function (lambda (sym)
-                          (make-local-variable sym)
-                          (set sym 'ignore)))
+                          (set (make-local-variable sym) 'ignore)))
               '(tags-table-files-function
                 tags-completion-table-function
                 find-tag-regexp-search-function
@@ -878,6 +1049,7 @@ See documentation of variable `tags-file-name'."
                        (zerop (buffer-size)))))))
 \f
 ;;; Match qualifier functions for tagnames.
+;;; XXX these functions assume etags file format.
 
 ;; This might be a neat idea, but it's too hairy at the moment.
 ;;(defmacro tags-with-syntax (&rest body)
@@ -925,8 +1097,10 @@ See documentation of variable `tags-file-name'."
 ;;;###autoload
 (defun next-file (&optional initialize novisit)
   "Select next file among files in current tags table.
-Non-nil first argument (prefix arg, if interactive)
-initializes to the beginning of the list of files in the tags table.
+
+A first argument of t (prefix arg, if interactive) initializes to the
+beginning of the list of files in the tags table.  If the argument is
+neither nil nor t, it is evalled to initialize the list of files.
 
 Non-nil second argument NOVISIT means use a temporary buffer
  to save time and avoid uninteresting warnings.
@@ -934,12 +1108,21 @@ Non-nil second argument NOVISIT means use a temporary buffer
 Value is nil if the file was already visited;
 if the file was newly read in, the value is the filename."
   (interactive "P")
-  (and initialize
-       (save-excursion
-        (visit-tags-table-buffer)
-        (setq next-file-list (tags-table-files))))
+  (cond ((not initialize)
+        ;; Not the first run.
+        )
+       ((eq initialize t)
+        ;; Initialize the list from the tags table.
+        (save-excursion
+          ;; Visit the tags table buffer to get its list of files.
+          (visit-tags-table-buffer)
+          (setq next-file-list (tags-table-files))))
+       (t
+        ;; Initialize the list by evalling the argument.
+        (setq next-file-list (eval initialize))))
   (or next-file-list
       (save-excursion
+       ;; Get the files from the next tags table.
        ;; When doing (visit-tags-table-buffer t),
        ;; the tags table buffer must be current.
        (if (and (visit-tags-table-buffer 'same)
@@ -964,7 +1147,9 @@ if the file was newly read in, the value is the filename."
 (defvar tags-loop-operate nil
   "Form for `tags-loop-continue' to eval to change one file.")
 
-(defvar tags-loop-scan nil
+(defvar tags-loop-scan
+  '(error (substitute-command-keys
+          "No \\[tags-search] or \\[tags-query-replace] in progress."))
   "Form for `tags-loop-continue' to eval to scan one file.
 If it returns non-nil, this file needs processing by evalling
 \`tags-loop-operate'.  Otherwise, move on to the next file.")
@@ -972,7 +1157,8 @@ If it returns non-nil, this file needs processing by evalling
 ;;;###autoload
 (defun tags-loop-continue (&optional first-time)
   "Continue last \\[tags-search] or \\[tags-query-replace] command.
-Used noninteractively with non-nil argument to begin such a command.
+Used noninteractively with non-nil argument to begin such a command (the
+argument is passed to `next-file', which see).
 Two variables control the processing we do on each file:
 the value of `tags-loop-scan' is a form to be executed on each file
 to see if it is interesting (it returns non-nil if so)
@@ -1015,11 +1201,10 @@ If the latter returns non-nil, we exit; otherwise we scan the next file."
     (and messaged
         (null tags-loop-operate)
         (message "Scanning file %s...found" buffer-file-name))))
-    
 ;;;###autoload (define-key esc-map "," 'tags-loop-continue)
 
 ;;;###autoload
-(defun tags-search (regexp)
+(defun tags-search (regexp &optional file-list-form)
   "Search through all files listed in tags table for match for REGEXP.
 Stops when a match is found.
 To continue searching for next match, use command \\[tags-loop-continue].
@@ -1028,16 +1213,16 @@ See documentation of variable `tags-file-name'."
   (interactive "sTags search (regexp): ")
   (if (and (equal regexp "")
           (eq (car tags-loop-scan) 're-search-forward)
-          (eq tags-loop-operate t))
+          (null tags-loop-operate))
       ;; Continue last tags-search as if by M-,.
       (tags-loop-continue nil)
     (setq tags-loop-scan
          (list 're-search-forward regexp nil t)
          tags-loop-operate nil)
-    (tags-loop-continue t)))
+    (tags-loop-continue (or file-list-form t))))
 
 ;;;###autoload
-(defun tags-query-replace (from to &optional delimited)
+(defun tags-query-replace (from to &optional delimited file-list-form)
   "Query-replace-regexp FROM with TO through all files listed in tags table.
 Third arg DELIMITED (prefix arg) means replace only word-delimited matches.
 If you exit (\\[keyboard-quit] or ESC), you can resume the query-replace
@@ -1047,13 +1232,13 @@ See documentation of variable `tags-file-name'."
   (interactive
    "sTags query replace (regexp): \nsTags query replace %s by: \nP")
   (setq tags-loop-scan (list 'prog1
-                            (list 'if (list 're-search-forward form nil t)
+                            (list 'if (list 're-search-forward from nil t)
                                   ;; When we find a match, move back
                                   ;; to the beginning of it so perform-replace
                                   ;; will see it.
                                   '(goto-char (match-beginning 0))))
        tags-loop-operate (list 'perform-replace from to t t delimited))
-  (tags-loop-continue t))
+  (tags-loop-continue (or file-list-form t)))
 \f
 ;;;###autoload
 (defun list-tags (file)