emacs-elpa-diffs
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[elpa] master 5b3676f 095/110: avy.el (avy-next): New command to go to t


From: Oleh Krehel
Subject: [elpa] master 5b3676f 095/110: avy.el (avy-next): New command to go to the next candidate after avy-read
Date: Sat, 11 May 2019 10:15:52 -0400 (EDT)

branch: master
commit 5b3676f1a4212b43de33e8c7a8f3a2de933068c7
Author: Oleh Krehel <address@hidden>
Commit: Oleh Krehel <address@hidden>

    avy.el (avy-next): New command to go to the next candidate after avy-read
    
    Example config:
    
        (defhydra hydra-avy-cycle ()
          ("j" avy-next "next")
          ("k" avy-prev "prev")
          ("q" nil "quit"))
    
        (global-set-key (kbd "C-M-'") 'hydra-avy-cycle/body)
    
    After e.g. `avy-goto-char' or `avy-goto-char-timer', use the above
    hydra to cycle between the last candidates.
    
    Fixes #254
---
 avy.el | 35 +++++++++++++++++++++++++++++++++++
 1 file changed, 35 insertions(+)

diff --git a/avy.el b/avy.el
index 560401a..7d949ea 100644
--- a/avy.el
+++ b/avy.el
@@ -787,6 +787,40 @@ Set `avy-style' according to COMMMAND as well."
                                    #'avy--remove-leading-chars))))
              (avy--done))))))
 
+(defvar avy-last-candidates nil
+  "Store the last candidate list.")
+
+(defun avy--last-candidates-cycle (advancer)
+  (let* ((avy-last-candidates
+          (cl-remove-if-not
+           (lambda (x) (equal (cdr x) (selected-window)))
+           avy-last-candidates))
+         (min-dist
+          (apply #'min
+                 (mapcar (lambda (x) (abs (- (caar x) (point)))) 
avy-last-candidates)))
+         (pos
+          (cl-position-if
+           (lambda (x)
+             (= (- (caar x) (point)) min-dist))
+           avy-last-candidates)))
+    (funcall advancer pos avy-last-candidates)))
+
+(defun avy-prev ()
+  "Go to the previous candidate of the last `avy-read'."
+  (interactive)
+  (avy--last-candidates-cycle
+   (lambda (pos lst)
+     (when (> pos 0)
+       (goto-char (caar (nth (1- pos) lst)))))))
+
+(defun avy-next ()
+  "Go to the next candidate of the last `avy-read'."
+  (interactive)
+  (avy--last-candidates-cycle
+   (lambda (pos lst)
+     (when (< pos (1- (length lst)))
+       (goto-char (caar (nth (1+ pos) lst)))))))
+
 (defun avy--process (candidates &optional overlay-fn)
   "Select one of CANDIDATES using `avy-read'.
 Use OVERLAY-FN to visualize the decision overlay."
@@ -796,6 +830,7 @@ Use OVERLAY-FN to visualize the decision overlay."
     (setq candidates
           (mapcar (lambda (x) (cons x (selected-window)))
                   candidates)))
+  (setq avy-last-candidates (copy-sequence candidates))
   (let ((original-cands (copy-sequence candidates))
         (res (avy--process-1 candidates overlay-fn)))
     (cond



reply via email to

[Prev in Thread] Current Thread [Next in Thread]