all messages for Emacs-related lists mirrored at yhetil.org
 help / color / mirror / code / Atom feed
From: "Mattias Engdegård" <mattiase@acm.org>
To: Dmitry Gutov <dgutov@yandex.ru>
Cc: yyoncho <yyoncho@gmail.com>, 38797@debbugs.gnu.org
Subject: bug#38797: 27.0.50; Feature request: provide the opposite of xref-pop-marker-stack
Date: Sun, 24 Oct 2021 15:24:31 +0200	[thread overview]
Message-ID: <072CF8C3-2ADC-4610-A7E1-94C795A22618@acm.org> (raw)
In-Reply-To: <0c88bcef-f07d-5a38-fa7d-b0ddd048cb48@yandex.ru>

[-- Attachment #1: Type: text/plain, Size: 1795 bytes --]

24 okt. 2021 kl. 03.40 skrev Dmitry Gutov <dgutov@yandex.ru>:

> - The removal of
> 
>  (defvaralias 'find-tag-marker-ring 'xref--marker-ring)
> 
> and the subsequent initialization of the aliased variable to a new ring will likely break any real code code which is still depending on that variable. Because now it is nil by default, and not a ring. Not sure what's the best solution, perhaps it's just time to remove it.

Right; I've resurrected find-tag-marker-ring as a zombie variable. Code manipulating it will have no effect but at least won't crash, which is probably a good trade-off.

>  (make-obsolete-variable 'xref-marker-ring-length nil "29.1")
> 
> is similarly pointless because the variable definition has been deleted. Declaring it obsolete does not help the fact that it is unbound now.

Correct -- resurrected as a zombie variable here too.

> * xref-go-back and xref-go-forward?

Good suggestions and now used. (I've still not added xref-go-back as an alias but that should probably be done.)

> - The slots in the docstring of xref--history (should we call it xref--location-history?) could be called (BACK-STACK . FORWARD-STACK).

Yes, done.

> - xref-set-marker-ring-length's fate is changed accordingly to what we choose to do with xref-marker-ring-length.

We could make it set the pointless xref-marker-ring-length but I didn't bother. I doubt anyone is using that function.

> - The FIXME about proposed xref--back-history-empty-p doesn't make much sense if the introduced function name is going to be private

Sorry, that wasn't the intention. I've added xref-back-history-empty-p for use in the new menu entry.
(I'll let someone else add it to the context menu because that seemed much more complicated.)

Revised patch attached.


[-- Attachment #2: 0001-Add-xref-forward-history-bug-38797.patch --]
[-- Type: application/octet-stream, Size: 11099 bytes --]

From 6984b9ed0880a31e70d297f74799e47ee2903d97 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-go-forward` is bound to `C-M-,`.

* lisp/progmodes/etags.el (find-tag-marker-ring-length): Update.
(tags-location-ring-length): New.
(find-tag-marker-ring): Keep as dummy.
(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-go-forward, xref-forward-history-empty-p): New.
(xref-push-marker-stack, xref-pop-marker-stack)
(xref-clear-marker-stack, xref-marker-stack-empty-p):
Use `xref--history`.
* lisp/menu-bar.el (menu-bar-goto-menu): Add Forward entry.
* doc/emacs/maintaining.texi (Looking Up Identifiers): Document.
* etc/NEWS: Announce.
---
 doc/emacs/maintaining.texi | 13 ++++--
 etc/NEWS                   |  5 +++
 lisp/menu-bar.el           |  6 +++
 lisp/progmodes/etags.el    | 13 +++---
 lisp/progmodes/xref.el     | 83 +++++++++++++++++++++++---------------
 5 files changed, 79 insertions(+), 41 deletions(-)

diff --git a/doc/emacs/maintaining.texi b/doc/emacs/maintaining.texi
index 5b66031b8a..e28ec5fb43 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-go-forward}).
 @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-go-forward
+  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 294181635e..e0c2aead94 100644
--- a/etc/NEWS
+++ b/etc/NEWS
@@ -176,6 +176,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-go-forward'.
+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/menu-bar.el b/lisp/menu-bar.el
index fafc99eb95..d1bb8d9677 100644
--- a/lisp/menu-bar.el
+++ b/lisp/menu-bar.el
@@ -419,6 +419,12 @@ menu-bar-goto-menu
                                 (not (xref-marker-stack-empty-p)))
                   :help "Back to the position of the last search"))
 
+    (bindings--define-key menu [xref-forward]
+      '(menu-item "Forward" xref-go-forward
+                  :visible (and (featurep 'xref)
+                                (not (xref-forward-history-empty-p)))
+                  :help "Forward to the position gone Back from"))
+
     (bindings--define-key menu [xref-apropos]
       '(menu-item "Find Apropos..." xref-find-apropos
                   :help "Find function/variables whose names match regexp"))
diff --git a/lisp/progmodes/etags.el b/lisp/progmodes/etags.el
index d2ce813daa..318121fbb5 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 "25.1")
+
+(defvar tags-location-ring-length 16)
 
 (defcustom tags-tag-face 'default
   "Face for tags in the output of `tags-apropos'."
@@ -180,7 +182,8 @@ tags-apropos-additional-actions
 		       (sexp :tag "Tags to search")))
   :version "21.1")
 
-(defvaralias 'find-tag-marker-ring 'xref--marker-ring)
+;; Obsolete variable kept for compatibility. We don't use it in any way.
+(defvar find-tag-marker-ring (make-ring 16))
 (make-obsolete-variable
  'find-tag-marker-ring
  "use `xref-push-marker-stack' or `xref-pop-marker-stack' instead."
@@ -191,7 +194,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].")
 \f
@@ -731,13 +734,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 a198ae349e..66ac89e72f 100644
--- a/lisp/progmodes/xref.el
+++ b/lisp/progmodes/xref.el
@@ -341,15 +341,9 @@ xref--search-property
           (t (goto-char start) nil))))
 
 \f
-;;; 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)
+;; Dummy variable retained for compatibility.
+(defvar xref-marker-ring-length 16)
+(make-obsolete-variable 'xref-marker-ring-length nil "29.1")
 
 (defcustom xref-prompt-for-identifier '(not xref-find-definitions
                                             xref-find-definitions-other-window
@@ -420,29 +414,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)
+  "(BACKWARD-STACK . FORWARD-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-go-forward]."
   (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 (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-go-back'.
+
+;;;###autoload
+(defun xref-go-forward ()
+  "Got to the point where a previous \\[xref-pop-marker-stack] was invoked."
+  (interactive)
+  (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))
@@ -465,17 +477,23 @@ 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'.
 
+;;;###autoload
+(defun xref-forward-history-empty-p ()
+  "Whether the xref forward-history is empty."
+  (null (cdr xref--history)))
 \f
 
 (defun xref--goto-char (pos)
@@ -1494,6 +1512,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-go-forward)
 ;;;###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)


  parent reply	other threads:[~2021-10-24 13:24 UTC|newest]

Thread overview: 23+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2019-12-29 18:11 bug#38797: 27.0.50; Feature request: provide the opposite of xref-pop-marker-stack yyoncho
2020-01-23 14:13 ` Dmitry Gutov
2021-10-20 19:12 ` Mattias Engdegård
2021-10-24  1:40   ` Dmitry Gutov
2021-10-24  7:54     ` Juri Linkov
2021-10-24 12:39       ` Dmitry Gutov
2021-10-24 19:10         ` Juri Linkov
2021-10-24 23:22           ` Dmitry Gutov
2021-10-25  7:44             ` Juri Linkov
2021-10-25 15:18               ` Dmitry Gutov
2021-10-25 17:07                 ` Juri Linkov
2021-10-24 13:24     ` Mattias Engdegård [this message]
2021-10-25  2:13       ` Dmitry Gutov
2021-10-25 15:49         ` Mattias Engdegård
2021-10-25 17:05           ` Juri Linkov
2021-10-25 18:53             ` Mattias Engdegård
2021-10-25 19:38             ` Dmitry Gutov
2021-10-25 21:29           ` Glenn Morris
2021-10-26  8:06             ` Mattias Engdegård
2021-11-08 18:23           ` Juri Linkov
2021-11-09  8:30             ` Juri Linkov
2021-11-09 13:08               ` Mattias Engdegård
2021-11-09 13:09                 ` Dmitry Gutov

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=072CF8C3-2ADC-4610-A7E1-94C795A22618@acm.org \
    --to=mattiase@acm.org \
    --cc=38797@debbugs.gnu.org \
    --cc=dgutov@yandex.ru \
    --cc=yyoncho@gmail.com \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
Code repositories for project(s) associated with this external index

	https://git.savannah.gnu.org/cgit/emacs.git
	https://git.savannah.gnu.org/cgit/emacs/org-mode.git

This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.