(next-file): If arg is neither t nor nil, then eval it to return initial
[bpt/emacs.git] / lisp / progmodes / etags.el
index 1f97188..452d9ad 100644 (file)
@@ -1,4 +1,4 @@
-;; etags.el --- etags facility for Emacs
+;;; etags.el --- etags facility for Emacs
 
 ;; Copyright (C) 1985, 1986, 1988, 1989, 1992, 1993
 ;;     Free Software Foundation, Inc.
@@ -77,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.
@@ -137,7 +141,7 @@ 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.
@@ -255,18 +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.  Returns the tail of TABLES (or of an
-;; inner included list) whose car is a table listing THIS-FILE.
-(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)) ;XXX check all in core first.
+               (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)
@@ -292,6 +303,7 @@ file the tag was in."
                                   ;; Recurse on the list of included tables.
                                   (tags-table-including this-file
                                                         tags-included-tables
+                                                        core-only
                                                         t))
                             (if found
                                 ;; One of them lists THIS-FILE.
@@ -304,10 +316,11 @@ file the tag was in."
                                        tags-table-parent-pointer-list)
                                       tags-table-list-pointer found
                                       tags-table-list-started-at found
-                                      ;; CONT is a local variable of
+                                      ;; Set a local variable of
                                       ;; our caller, visit-tags-table-buffer.
                                       ;; Set it so we won't frob lists later.
-                                      cont 'included)))
+                                      visit-tags-table-buffer-cont
+                                      'included)))
                         (or recursing
                             ;; tags-table-parent-pointer-list now describes
                             ;; the path of included tables taken by recursive
@@ -335,152 +348,163 @@ If arg is nil or absent, choose a first buffer from information in
 Returns t if it visits a tags table, or nil if there are no more in the list."
 
   ;; Set tags-file-name to the tags table file we want to visit.
-  (cond ((eq cont 'same)
-        ;; Use the ambient value of tags-file-name.
-        (or tags-file-name
-            (error (substitute-command-keys
-                    (concat "No tags table in use!  "
-                            "Use \\[visit-tags-table] to select one."))))
-        ;; Set CONT to nil so the code below will make sure tags-file-name
-        ;; is in tags-table-list.
-        (setq cont nil))
-
-       (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)))))
-
-       (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 CONT will be set non-nil so we don't do it below.
-               (car (save-excursion (tags-table-including buffer-file-name
-                                                          tags-table-list)))
-               ;; Fourth, use the user variable tags-file-name, if it is not
-               ;; already in tags-table-list.
-               (and 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 cont t) (null tags-table-list-pointer))
-      ;; All out of tables.
-      nil
+  (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)))))
 
-    ;; 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 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
+         (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))))
-
-             ;; 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))))
+                                   (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.
@@ -578,6 +602,16 @@ Assumes the tags table is the current buffer."
 (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.
@@ -585,101 +619,135 @@ 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)
-                (list (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
-       ;; 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
-       ;; 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))
-      (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)
-                (list (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)
-                (list (find-tag-tag "Find tag other window: "))))
+  (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)))
+        (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))
+                       (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)
-                (list (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)
@@ -689,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)
-                (list (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.
 
@@ -817,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))
@@ -844,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 ()
@@ -871,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)
@@ -888,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)
@@ -902,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)
@@ -1019,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.
@@ -1028,11 +1108,18 @@ 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 the tags table buffer to get its list of files.
-        (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.
@@ -1070,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)
@@ -1116,7 +1204,7 @@ If the latter returns non-nil, we exit; otherwise we scan the next file."
 ;;;###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].
@@ -1131,10 +1219,10 @@ See documentation of variable `tags-file-name'."
     (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
@@ -1150,7 +1238,7 @@ See documentation of variable `tags-file-name'."
                                   ;; 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)