branch: externals/ivy-hydra
commit 316d4dda5792762d45083b7227722db9cee789b3
Author: okamsn <[email protected]>
Commit: Oleh Krehel <[email protected]>

    counsel-kmacro: Create actions for cycling the ring and setting a saved 
counter value.
    
    When cycling the keyboard macro ring, Emacs saves the current counter value 
with
    the current macro definition, so that the value is restored when cycling 
back.
    
    `counsel-kmacro' currently allows the user to run any defined macro with the
    current counter value, and does not switch the counter value when running
    different macros.  `counsel-kmacro-action-set-saved-starting-counter' 
allows the
    user to change the counter value saved the the macro in `kmacro-ring'. This 
is
    not the same as setting the current counter value.
    
    `counsel-kmacro-action-cycle-ring-to-macro' allows the user to cycle the 
ring to
    the selected macro, which uses `kmacro-cycle-ring-previous'.
    
    Fixes #2502
---
 counsel.el | 50 ++++++++++++++++++++++++++++++++++++++++++++++++--
 1 file changed, 48 insertions(+), 2 deletions(-)

diff --git a/counsel.el b/counsel.el
index e9b2c1d..16e86cf 100644
--- a/counsel.el
+++ b/counsel.el
@@ -5529,6 +5529,50 @@ This will apply to the next macro a user defines."
          (format (nth 2 actual-kmacro)))
     (kmacro-set-format format)))
 
+(defun counsel-kmacro-action-cycle-ring-to-macro (x)
+  "Cycle `kmacro-ring' until `last-kbd-macro' is the selected macro.
+This is convenient when using \\[kmacro-end-or-call-macro] to call macros.
+Note that cycling the ring changes the starting value of the current macro
+to changes the current macro counter."
+  (let ((actual-kmacro (cdr x)))
+    (unless (equal last-kbd-macro
+                   (if (listp last-kbd-macro)
+                       last-kbd-macro
+                     (car actual-kmacro)))
+      (while (not (equal actual-kmacro
+                         (car kmacro-ring)))
+        (kmacro-cycle-ring-previous))
+      ;; Once selected macro is at the head of the ring,
+      ;; cycle one last time.
+      (kmacro-cycle-ring-previous))))
+
+(defun counsel-kmacro-action-set-saved-starting-counter (x)
+  "Set the starting counter value of the chosen macro.
+
+By default, sets to current value of the counter. It has no
+effect when selecting the current macro.
+
+Normally, when cycling keyboard macro ring with \\[kmacro-cycle-ring-previous]
+or \\[kmacro-cycle-ring-next], the current value of the macro counter is
+included with the current macro definition. Then, when cycling
+back, that counter value is restored.  This function is meant to
+achieve something similar when cycling macros in the context of
+using `counsel-kmacro', which does not use different counter
+values when running different macros."
+  (let ((actual-kmacro (cdr x))
+        (default-kmacro-counter-string (number-to-string kmacro-counter)))
+    (setq kmacro-ring (mapcar (lambda (this-macro-in-ring)
+                                (if (equal this-macro-in-ring actual-kmacro)
+                                    (list (car this-macro-in-ring)
+                                          (read-from-minibuffer (concat "Set 
initial counter for macro (default: "
+                                                                        
default-kmacro-counter-string
+                                                                        "): ")
+                                                                nil nil t nil
+                                                                
default-kmacro-counter-string)
+                                          (caddr this-macro-in-ring))
+                                  this-macro-in-ring))
+                              kmacro-ring))))
+
 (defun counsel-kmacro-action-execute-after-prompt (x)
   "Execute an existing keyboard macro, prompting for a starting counter value, 
a
 counter format, and the number of times to execute the macro.
@@ -5567,10 +5611,12 @@ counter value and iteration amount."
 
 (ivy-set-actions
  'counsel-kmacro
- '(("c" counsel-kmacro-action-copy-initial-counter-value "copy initial counter 
value")
+ '(("c" counsel-kmacro-action-cycle-ring-to-macro "cycle to")
    ("d" counsel-kmacro-action-delete-kmacro "delete")
+   ("e" counsel-kmacro-action-execute-after-prompt "execute after prompt")
    ("f" counsel-kmacro-action-copy-counter-format-for-new-macro "copy counter 
format for new macro")
-   ("e" counsel-kmacro-action-execute-after-prompt "execute after prompt")))
+   ("s" counsel-kmacro-action-set-saved-starting-counter "set this counter 
value")
+   ("v" counsel-kmacro-action-copy-initial-counter-value "copy initial counter 
value")))
 
 ;;** `counsel-geiser-doc-look-up-manual'
 (declare-function geiser-doc-manual-for-symbol "ext:geiser-doc")

Reply via email to