From 9191300d574a1a7d4adc3eb86f0381a6e836c887 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Mattias=20Engdeg=C3=A5rd?= <mattiase@acm.org>
Date: Tue, 19 Oct 2021 13:26:19 +0200
Subject: [PATCH] Add xref forward history (bug#38797)

Make it possible to go forward as well as back in the Xref history.
The new `xref-forward-history` is bound to `C-M-,`.

* lisp/progmodes/etags.el (find-tag-marker-ring-length): Update.
(tags-location-ring-length): New.
(find-tag-marker-ring): Remove.
(tags-location-ring, tags-reset-tags-tables):
Use `tags-location-ring-length` instead of `xref-marker-ring-length`.
* lisp/progmodes/xref.el (xref-marker-ring-length, xref-marker-ring)
(xref-set-marker-ring-length):
Make obsolete.
(xref--history, xref-forward-history): New.
(xref-push-marker-stack, xref-pop-marker-stack)
(xref-clear-marker-stack, xref-marker-stack-empty-p):
Use `xref--history`.
* doc/emacs/maintaining.texi (Looking Up Identifiers): Document.
* etc/NEWS: Announce.
---
 doc/emacs/maintaining.texi | 13 +++++--
 etc/NEWS                   |  5 +++
 lisp/progmodes/etags.el    | 11 +++---
 lisp/progmodes/xref.el     | 78 ++++++++++++++++++++++----------------
 4 files changed, 66 insertions(+), 41 deletions(-)

diff --git a/doc/emacs/maintaining.texi b/doc/emacs/maintaining.texi
index 5b66031b8a..46b4d8181f 100644
--- a/doc/emacs/maintaining.texi
+++ b/doc/emacs/maintaining.texi
@@ -2140,6 +2140,9 @@ Looking Up Identifiers
 @item M-,
 Go back to where you previously invoked @kbd{M-.} and friends
 (@code{xref-pop-marker-stack}).
+@item C-M-,
+Go forward to where you previously invoked @kbd{M-,}
+(@code{xref-forward-history}).
 @item M-x xref-etags-mode
 Switch @code{xref} to use the @code{etags} backend.
 @end table
@@ -2205,14 +2208,16 @@ Looking Up Identifiers
 
 @kindex M-,
 @findex xref-pop-marker-stack
-@vindex xref-marker-ring-length
   To go back to places @emph{from where} you've displayed the definition,
 use @kbd{M-,} (@code{xref-pop-marker-stack}).  It jumps back to the
 point of the last invocation of @kbd{M-.}.  Thus you can find and
 examine the definition of something with @kbd{M-.} and then return to
-where you were with @kbd{M-,}.  @kbd{M-,} allows you to retrace your
-steps to a depth determined by the variable
-@code{xref-marker-ring-length}, which defaults to 16.
+where you were with @kbd{M-,}.
+
+@kindex C-M-,
+@findex xref-forward-history
+  Go forward to a place from where you previously went back using @kbd{M-,}.
+This is useful if you find that you went back too far.
 
 @findex xref-etags-mode
   Some major modes install @code{xref} support facilities that might
diff --git a/etc/NEWS b/etc/NEWS
index 9f0a4ac4ce..21f9a0f75f 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -153,6 +153,11 @@ it with new 'term-{faint,italic,slow-blink,fast-blink}' faces.
 *** 'project-find-file' and 'project-or-external-find-file' now accept
 a prefix argument which is interpreted to mean "include all files".
 
++++
+*** New command 'xref-forward-history'.
+It is bound to 'C-M-,' and jumps to the location where 'xref-pop-marker-stack'
+was invoked previously.
+
 ** File notifications
 
 +++
diff --git a/lisp/progmodes/etags.el b/lisp/progmodes/etags.el
index d2ce813daa..518c4ec81b 100644
--- a/lisp/progmodes/etags.el
+++ b/lisp/progmodes/etags.el
@@ -145,7 +145,9 @@ find-tag-default-function
   :type '(choice (const nil) function))
 
 (define-obsolete-variable-alias 'find-tag-marker-ring-length
-  'xref-marker-ring-length "25.1")
+  'tags-location-ring-length "29.1")
+
+(defvar tags-location-ring-length 16)
 
 (defcustom tags-tag-face 'default
   "Face for tags in the output of `tags-apropos'."
@@ -180,7 +182,6 @@ tags-apropos-additional-actions
 		       (sexp :tag "Tags to search")))
   :version "21.1")
 
-(defvaralias 'find-tag-marker-ring 'xref--marker-ring)
 (make-obsolete-variable
  'find-tag-marker-ring
  "use `xref-push-marker-stack' or `xref-pop-marker-stack' instead."
@@ -191,7 +192,7 @@ default-tags-table-function
 This function receives no arguments and should return the default
 tags table file to use for the current buffer.")
 
-(defvar tags-location-ring (make-ring xref-marker-ring-length)
+(defvar tags-location-ring (make-ring tags-location-ring-length)
   "Ring of markers which are locations visited by \\[find-tag].
 Pop back to the last location with \\[negative-argument] \\[find-tag].")
 
@@ -731,13 +732,13 @@ tags-reset-tags-tables
   (interactive)
   ;; Clear out the markers we are throwing away.
   (let ((i 0))
-    (while (< i xref-marker-ring-length)
+    (while (< i tags-location-ring-length)
       (if (aref (cddr tags-location-ring) i)
 	  (set-marker (aref (cddr tags-location-ring) i) nil))
       (setq i (1+ i))))
   (xref-clear-marker-stack)
   (setq tags-file-name nil
-	tags-location-ring (make-ring xref-marker-ring-length)
+	tags-location-ring (make-ring tags-location-ring-length)
 	tags-table-list nil
 	tags-table-computed-list nil
 	tags-table-computed-list-for nil
diff --git a/lisp/progmodes/xref.el b/lisp/progmodes/xref.el
index 52a4e0c543..502cab022c 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -334,15 +334,7 @@ xref--search-property
           (t (goto-char start) nil))))
 
 
-;;; Marker stack  (M-. pushes, M-, pops)
-
-(defcustom xref-marker-ring-length 16
-  "Length of the xref marker ring.
-If this variable is not set through Customize, you must call
-`xref-set-marker-ring-length' for changes to take effect."
-  :type 'integer
-  :initialize #'custom-initialize-default
-  :set #'xref-set-marker-ring-length)
+(make-obsolete-variable 'xref-marker-ring-length nil "29.1")
 
 (defcustom xref-prompt-for-identifier '(not xref-find-definitions
                                             xref-find-definitions-other-window
@@ -413,29 +405,47 @@ xref-auto-jump-to-first-xref
   :version "28.1"
   :package-version '(xref . "1.2.0"))
 
-(defvar xref--marker-ring (make-ring xref-marker-ring-length)
-  "Ring of markers to implement the marker stack.")
+(defvar xref--history (cons nil nil)
+  "(HISTORY-STACK . FUTURE-STACK) of markers to visited Xref locations.")
+
+(make-obsolete-variable 'xref-marker-ring nil "29.1")
 
-(defun xref-set-marker-ring-length (var val)
-  "Set `xref-marker-ring-length'.
-VAR is the symbol `xref-marker-ring-length' and VAL is the new
-value."
-  (set-default var val)
-  (if (ring-p xref--marker-ring)
-      (ring-resize xref--marker-ring val)))
+(defun xref-set-marker-ring-length (_var _val)
+  (declare (obsolete nil "29.1"))
+  nil)
 
 (defun xref-push-marker-stack (&optional m)
-  "Add point M (defaults to `point-marker') to the marker stack."
-  (ring-insert xref--marker-ring (or m (point-marker))))
+  "Add point M (defaults to `point-marker') to the marker stack.
+The future stack is erased."
+  (push (or m (point-marker)) (car xref--history))
+  (dolist (mk (cdr xref--history))
+    (set-marker mk nil nil))
+  (setcdr xref--history nil))
 
 ;;;###autoload
 (defun xref-pop-marker-stack ()
-  "Pop back to where \\[xref-find-definitions] was last invoked."
+  "Go back to the previous position in xref history.
+To undo, use \\[xref-forward-history]."
+  (interactive)
+  (if (null (car xref--history))
+      (user-error "At start of xref history")
+    (let ((marker (pop (car xref--history))))
+      (push (point-marker) (cdr xref--history))
+      (switch-to-buffer (or (marker-buffer marker)
+                            (user-error "The marked buffer has been deleted")))
+      (goto-char (marker-position marker))
+      (set-marker marker nil nil)
+      (run-hooks 'xref-after-return-hook))))
+;; FIXME: rename to `xref-back-history'.
+
+;;;###autoload
+(defun xref-forward-history ()
+  "Got to the point where a previous \\[xref-pop-marker-stack] was invoked."
   (interactive)
-  (let ((ring xref--marker-ring))
-    (when (ring-empty-p ring)
-      (user-error "Marker stack is empty"))
-    (let ((marker (ring-remove ring 0)))
+  (if (null (cdr xref--history))
+      (user-error "At end of xref history")
+    (let ((marker (pop (cdr xref--history))))
+      (push (point-marker) (car xref--history))
       (switch-to-buffer (or (marker-buffer marker)
                             (user-error "The marked buffer has been deleted")))
       (goto-char (marker-position marker))
@@ -458,16 +468,19 @@ xref-pulse-momentarily
 
 ;; etags.el needs this
 (defun xref-clear-marker-stack ()
-  "Discard all markers from the marker stack."
-  (let ((ring xref--marker-ring))
-    (while (not (ring-empty-p ring))
-      (let ((marker (ring-remove ring)))
-        (set-marker marker nil nil)))))
+  "Discard all markers from the xref history."
+  (dolist (l (list (car xref--history) (cdr xref--history)))
+    (dolist (m l)
+      (set-marker m nil nil)))
+  (setq xref--history (cons nil nil))
+  nil)
 
 ;;;###autoload
 (defun xref-marker-stack-empty-p ()
-  "Return t if the marker stack is empty; nil otherwise."
-  (ring-empty-p xref--marker-ring))
+  "Whether the xref back-history is empty."
+  (null (car xref--history)))
+;; FIXME: rename this to `xref--back-history-empty-p' and add the
+;; corresponding forward history function.
 
 
 
@@ -1487,6 +1500,7 @@ xref-apropos-regexp
 ;;;###autoload (define-key esc-map "," #'xref-pop-marker-stack)
 ;;;###autoload (define-key esc-map "?" #'xref-find-references)
 ;;;###autoload (define-key esc-map [?\C-.] #'xref-find-apropos)
+;;;###autoload (define-key esc-map [?\C-,] #'xref-forward-history)
 ;;;###autoload (define-key ctl-x-4-map "." #'xref-find-definitions-other-window)
 ;;;###autoload (define-key ctl-x-5-map "." #'xref-find-definitions-other-frame)
 
-- 
2.21.1 (Apple Git-122.3)

