branch: elpa/magit
commit 085baa2ac96df90bdb2a1bedd3598df19919d731
Author: Jonas Bernoulli <jo...@bernoul.li>
Commit: Jonas Bernoulli <jo...@bernoul.li>

    magit-{previous,next}-reference: New commands
    
    Closes #5308.
---
 CHANGELOG          |  4 ++++
 lisp/magit-log.el  | 53 +++++++++++++++++++++++++++++++++++++++++++++++++++++
 lisp/magit-mode.el |  1 +
 3 files changed, 58 insertions(+)

diff --git a/CHANGELOG b/CHANGELOG
index 0369e9ca5f..bf6b6b040f 100644
--- a/CHANGELOG
+++ b/CHANGELOG
@@ -4,6 +4,10 @@
 - Added new option ~magit-format-file-function,~ and two functions to
   optionally prefix file names with icons, with the help of either
   ~all-the-icons~ or ~nerd-icons~.  #5308
+
+- Added new commands ~magit-previous-reference~ and ~magit-next-reference~,
+  with entry point ~C-c C-r~.  Enable ~repeat-mode~ to keep navigating with
+  ~p~ and ~n~.  #5310
   
 Bugfixes:
 
diff --git a/lisp/magit-log.el b/lisp/magit-log.el
index bb23609aaf..13bb65d2a6 100644
--- a/lisp/magit-log.el
+++ b/lisp/magit-log.el
@@ -1008,6 +1008,59 @@ of the current repository first; creating it if 
necessary."
          (transient-args 'magit-shortlog)))
   (magit-git-shortlog rev-or-range args))
 
+;;;; Movement Commands
+
+(defvar magit-reference-movement-faces
+  '(magit-tag
+    magit-branch-remote
+    magit-branch-remote-head
+    magit-branch-local
+    magit-branch-current
+    magit-branch-upstream
+    magit-branch-warning
+    magit-head
+    magit-refname
+    magit-refname-stash
+    magit-refname-wip
+    magit-refname-pullreq))
+
+(defvar-keymap magit-reference-navigation-repeat-map
+  :repeat t
+  "p" #'magit-previous-reference
+  "n" #'magit-next-reference
+  "r" #'magit-next-reference)
+
+(defun magit-previous-reference ()
+  "Move to the previous Git reference appearing in the current buffer.
+
+Move to the previous location that uses a face appearing in
+`magit-reference-movement-faces'.  If `repeat-mode' is enabled,
+this command and its counterpart can be repeated using \
+\\<magit-reference-navigation-repeat-map>\
+\\[magit-previous-reference] and \\[magit-next-reference]."
+  (interactive)
+  (magit-next-reference t))
+
+(defun magit-next-reference (&optional previous)
+  "Move to the next Git reference appearing in the current buffer.
+
+Move to the previous location that uses a face appearing in
+`magit-reference-movement-faces'.  If `repeat-mode' is enabled,
+this command and its counterpart can be repeated using \
+\\<magit-reference-navigation-repeat-map>\
+\\[magit-previous-reference] and \\[magit-next-reference]."
+  (interactive)
+  (catch 'found
+    (let ((pos (point)))
+      (while (and (not (eobp))
+                  (setq pos (if previous
+                                (previous-single-property-change pos 'face)
+                              (next-single-property-change pos 'face))))
+       (when (cl-intersection (ensure-list (get-text-property pos 'face))
+                               magit-reference-movement-faces)
+         (throw 'found (goto-char pos))))
+      (message "No more references"))))
+
 ;;; Log Mode
 
 (defvar magit-log-disable-graph-hack-args
diff --git a/lisp/magit-mode.el b/lisp/magit-mode.el
index a980c44edc..b79084280d 100644
--- a/lisp/magit-mode.el
+++ b/lisp/magit-mode.el
@@ -419,6 +419,7 @@ recommended value."
   "!" 'magit-run
   ">" 'magit-sparse-checkout
   "C-c C-c" 'magit-dispatch
+  "C-c C-r" 'magit-next-reference
   "C-c C-e" 'magit-edit-thing
   "C-c C-o" 'magit-browse-thing
   "C-c C-w" 'magit-copy-thing

Reply via email to