diff --git a/lisp/kmacro.el b/lisp/kmacro.el index 94d8794bd23..e7c3f75efd0 100644 --- a/lisp/kmacro.el +++ b/lisp/kmacro.el @@ -183,10 +183,18 @@ kmacro-keymap "C-l" #'kmacro-call-ring-2nd-repeat ;; macro counter - "C-f" #'kmacro-set-format - "C-c" #'kmacro-set-counter - "C-i" #'kmacro-insert-counter - "C-a" #'kmacro-add-counter + "C-f" #'kmacro-set-format + "C-c" #'kmacro-set-counter + "C-i" #'kmacro-insert-counter + "C-a" #'kmacro-add-counter + "C-r l" #'kmacro-reg-load-counter + "C-r s" #'kmacro-reg-save-counter + "C-r a =" #'kmacro-reg-add-counter-equal + "C-r a <" #'kmacro-reg-add-counter-less + "C-r a >" #'kmacro-reg-add-counter-greater + "C-q =" #'kmacro-quit-counter-equal + "C-q <" #'kmacro-quit-counter-less + "C-q >" #'kmacro-quit-counter-greater ;; macro editing "C-e" #'kmacro-edit-macro-repeat @@ -347,6 +355,89 @@ kmacro-add-counter (kmacro-display-counter))) +(defun kmacro-reg-load-counter (register) + "Load the value of a register into `kmacro-counter'" + (interactive + (list (register-read-with-preview "Load register to counter: "))) + (let ((register-val (get-register register))) + (when (numberp register-val) + (setq kmacro-counter register-val)))) + + +(defun kmacro-reg-save-counter (register) + "Save the value of `kmacro-counter' to a register" + (interactive + (list (register-read-with-preview "Save counter to register: "))) + (set-register register kmacro-counter)) + + +(defun kmacro-reg-add-counter-equal (&optional arg) + "Increment counter by ARG if it is equal to register value" + (interactive "p") + (let + ((register (register-read-with-preview "Compare counter to register: "))) + (kmacro-reg-add-counter '= register arg))) + + +(defun kmacro-reg-add-counter-less (&optional arg) + "Increment counter by ARG if it is less than register value" + (interactive "p") + (let + ((register (register-read-with-preview "Compare counter to register: "))) + (kmacro-reg-add-counter '< register arg))) + + +(defun kmacro-reg-add-counter-greater (&optional arg) + "Increment counter by ARG if it is greater than register value" + (interactive "p") + (let + ((register (register-read-with-preview "Compare counter to register: "))) + (kmacro-reg-add-counter '> register arg))) + + +(defun kmacro-reg-add-counter (func register &optional arg) + "Increment the counter by ARG if (FUNC kmacro-counter REGISTER-VALUE) +is true. +With no ARG, ARG is set to 1" + (let ((register-val (get-register register)) + (arg (if (null arg) 1 arg))) + (when (apply func (list kmacro-counter register-val)) + (setq current-prefix-arg nil) + (kmacro-add-counter arg)))) + + +(defun kmacro-quit-counter-equal (&optional arg) + "Quit the keyboard macro if the counter is equal to ARG" + (interactive "P") + (kmacro-quit-counter '= arg)) + + +(defun kmacro-quit-counter-less (&optional arg) + "Quit the keyboard macro if the counter is less than ARG" + (interactive "P") + (kmacro-quit-counter '< arg)) + + +(defun kmacro-quit-counter-greater (&optional arg) + "Quit the keyboard macro if the counter is greater than ARG" + (interactive "P") + (kmacro-quit-counter '> arg)) + + +(defun kmacro-quit-counter (func &optional arg) + "Quit the keyboard macro if (FUNC kmacro-counter ARG) is true. +With \\[universal-argument] or no ARG, ARG is set to 0" + (when kmacro-initial-counter-value + (setq kmacro-counter kmacro-initial-counter-value + kmacro-initial-counter-value nil)) + (let ((arg + (cond ((or (consp arg) (null arg)) 0) + ((eq '- arg) -1) + (t arg)))) + (when (apply func (list kmacro-counter arg)) + (keyboard-quit)))) + + (defun kmacro-loop-setup-function () "Function called prior to each iteration of macro." ;; Restore macro counter format to initial format, so it is ok to change