;; and you can set the macro counter format with C-x C-k C-f.
;; The following key bindings are performed:
-;;
+;;
;; Normal While defining macro
;; --------------------------- ------------------------------
;; f3 Define macro Insert current counter value
;; counter value (default 0) (default increment: 1)
;;
;; C-u f3 APPENDs to last macro
-;;
-;; f4 Call last macro End macro
+;;
+;; f4 Call last macro End macro
;; Prefix arg specifies number
;; of times to execute macro.
;;
;; C-u f4 Swap last and head of macro ring.
-;;
+;;
;; S-mouse-3 Set point at click and End macro and execute macro at
;; execute last macro. click.
(defvar kmacro-keymap
(let ((map (make-sparse-keymap)))
- (define-key map "\C-s" 'kmacro-start-macro)
+ (define-key map "s" 'kmacro-start-macro)
(define-key map "\C-k" 'kmacro-end-or-call-macro-repeat)
(define-key map "\C-e" 'kmacro-edit-macro-repeat)
(define-key map "\r" 'kmacro-edit-macro)
(define-key map "\C-f" 'kmacro-set-format)
(define-key map "\C-c" 'kmacro-set-counter)
(define-key map "\C-t" 'kmacro-swap-ring)
- (define-key map "\C-b" 'kmacro-bind-to-key)
+ (define-key map "b" 'kmacro-bind-to-key)
(define-key map "\C-d" 'kmacro-delete-ring-head)
;; Compatibility bindings
(define-key map "q" 'kbd-macro-query)
Non-nil arg RAW means just return raw first element."
(unless (kmacro-ring-empty-p)
(kmacro-pop-ring1 raw)))
-
+
(defun kmacro-ring-length ()
"Return length of macro ring, including pseudo head."
(m (format-kbd-macro macro))
(l (length m))
(z (and nil trunc (> l x))))
- (message (format "%s: %s%s" (or descr "Macro")
+ (message (format "%s: %s%s" (or descr "Macro")
(if z (substring m 0 (1- x)) m) (if z "..." ""))))
(message (or empty "No keyboard macros defined"))))
(kmacro-display (car (car kmacro-ring)) "2nd macro")))
-
+
(defun kmacro-cycle-ring-next (&optional arg)
"Move to next keyboard macro in keyboard macro ring.
Displays the selected macro in the echo area."
;;; Traditional bindings:
-
+
;;;###autoload
(defun kmacro-start-macro (arg)
"Record subsequent keyboard input, defining a keyboard macro.
The commands are recorded even as they are executed.
Use \\[kmacro-end-macro] to finish recording and make the macro available.
-Use \\[call-last-kbd-macro] to execute the macro.
+Use \\[kmacro-end-and-call-macro] to execute the macro.
Use \\[name-last-kbd-macro] to give it a permanent name.
Non-nil arg (prefix arg) means append to last macro defined;
(unless append
(if last-kbd-macro
(let ((len (length kmacro-ring)))
- (setq kmacro-ring
+ (setq kmacro-ring
(cons
(list last-kbd-macro kmacro-counter kmacro-counter-format-start)
kmacro-ring))
kmacro-last-counter kmacro-counter
kmacro-counter-format-start kmacro-counter-format))
- (start-kbd-macro append
+ (start-kbd-macro append
(and append
(if kmacro-execute-before-append
(> (car arg) 4)
(if end-macro
(kmacro-end-macro arg)
(call-last-kbd-macro arg #'kmacro-loop-setup-function))
+ (when (consp arg)
+ (setq arg (car arg)))
(when (and (or (null arg) (> arg 0))
(setq repeat-key
- (if (eq kmacro-call-repeat-key t) repeat-key kmacro-call-repeat-key)))
+ (if (eq kmacro-call-repeat-key t)
+ repeat-key
+ kmacro-call-repeat-key)))
(setq repeat-key-str (format-kbd-macro (vector repeat-key) nil))
(while repeat-key
- (message "Repeat macro %swith `%s'..."
+ (message "(Type %s to repeat macro%s)"
+ repeat-key-str
(if (and kmacro-call-repeat-with-arg
arg (> arg 1))
- (format "%d times " arg) "")
- repeat-key-str)
+ (format " %d times" arg) ""))
(if (equal repeat-key (read-event))
(progn
(clear-this-command-keys t)
- (call-last-kbd-macro (and kmacro-call-repeat-with-arg arg) #'kmacro-loop-setup-function)
+ (call-last-kbd-macro (and kmacro-call-repeat-with-arg arg)
+ #'kmacro-loop-setup-function)
(setq last-input-event nil))
(setq repeat-key nil)))
(when last-input-event
With numeric prefix ARG, repeat macro that many times.
With \\[universal-argument], call second macro in macro ring."
(interactive "P")
- (cond
+ (cond
(defining-kbd-macro
(if kmacro-call-repeat-key
(kmacro-call-macro arg no-repeat t)
;;;###autoload
(defun kmacro-end-and-call-macro (arg &optional no-repeat)
"Call last keyboard macro, ending it first if currently being defined.
-With numeric prefix ARG, repeat macro that many times."
+With numeric prefix ARG, repeat macro that many times.
+Zero argument means repeat until there is an error.
+
+To give a macro a permanent name, so you can call it
+even after defining other macros, use \\[name-last-kbd-macro]."
(interactive "P")
(if defining-kbd-macro
(kmacro-end-macro nil))
(defvar kmacro-step-edit-map (make-sparse-keymap)
"Keymap that defines the responses to questions in `kmacro-step-edit-macro'.
This keymap is an extension to the `query-replace-map', allowing the
-following additional answers: `insert-1', `insert', `append-1',
-`append', `replace-1', `replace', `act-repeat', `skip-rest',
-`skip-keep'.")
+following additional answers: `insert', `insert-1', `replace', `replace-1',
+`append', `append-end', `act-repeat', `skip-end', `skip-keep'.")
;; query-replace-map answers include: `act', `skip', `act-and-show',
;; `exit', `act-and-exit', `edit', `delete-and-edit', `recenter',
(define-key kmacro-step-edit-map "f" 'skip-keep)
(define-key kmacro-step-edit-map "q" 'quit)
(define-key kmacro-step-edit-map "d" 'skip)
+(define-key kmacro-step-edit-map "\C-d" 'skip)
(define-key kmacro-step-edit-map "i" 'insert)
(define-key kmacro-step-edit-map "I" 'insert-1)
(define-key kmacro-step-edit-map "r" 'replace)
(define-key kmacro-step-edit-map "R" 'replace-1)
(define-key kmacro-step-edit-map "a" 'append)
-(define-key kmacro-step-edit-map "A" 'append-1)
+(define-key kmacro-step-edit-map "A" 'append-end)
(defvar kmacro-step-edit-prefix-commands
'(universal-argument universal-argument-more universal-argument-minus
(format "Macro: %s%s%s%s%s\n"
(format-kbd-macro kmacro-step-edit-new-macro 1)
(if (and kmacro-step-edit-new-macro (> (length kmacro-step-edit-new-macro) 0)) " " "")
- (propertize (if keys (format-kbd-macro keys)
+ (propertize (if keys (format-kbd-macro keys)
(if kmacro-step-edit-appending "<APPEND>" "<INSERT>")) 'face 'region)
(if future " " "")
(if future (format-kbd-macro future) ""))
curmsg))
(t ""))
(if keys
- (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
+ (format "%s\n%s%s %S [yn iIaArR C-k kq!] "
(propertize "\
--------------Step Edit Keyboard Macro [?: help]---------------" 'face 'mode-line)
(if kmacro-step-edit-help "\
Step: y/SPC: execute next, d/n/DEL: skip next, f: skip but keep
TAB: execute while same, ?: toggle help
- Edit: i: insert, a: append, r: replace, I/A/R: one sequence
+ Edit: i: insert, r: replace, a: append, A: append at end,
+ I/R: insert/replace with one sequence,
End: !/c: execute rest, C-k: skip rest and save, q/C-g: quit
----------------------------------------------------------------
" "")
(propertize "Next command:" 'face 'bold)
this-command)
- (propertize
+ (propertize
(format "Type key sequence%s to insert and execute%s: "
(if (numberp kmacro-step-edit-inserting) "" "s")
- (if (numberp kmacro-step-edit-inserting) "" "[end with C-j]"))
+ (if (numberp kmacro-step-edit-inserting) "" " (end with C-j)"))
'face 'bold))))))
(defun kmacro-step-edit-query ()
(clear-this-command-keys) ;; recent-keys actually
(let (unread-command-events)
(quoted-insert (prefix-numeric-value current-prefix-arg))
- (setq kmacro-step-edit-new-macro
+ (setq kmacro-step-edit-new-macro
(vconcat kmacro-step-edit-new-macro (recent-keys)))
(when unread-command-events
(setq kmacro-step-edit-new-macro
((eq act 'skip-rest)
(setq kmacro-step-edit-active 'ignore)
nil)
- ((eq act 'automatic)
+ ((memq act '(automatic exit))
(setq kmacro-step-edit-active nil)
(setq act t)
t)
(setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
kmacro-step-edit-appending t))
nil)
- ((member act '(append-1 append))
- (setq kmacro-step-edit-inserting (if (eq act 'append-1) 1 t))
+ ((eq act 'append)
+ (setq kmacro-step-edit-inserting t)
(if (= executing-macro-index (length executing-kbd-macro))
(setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
kmacro-step-edit-appending t))
t)
+ ((eq act 'append-end)
+ (if (= executing-macro-index (length executing-kbd-macro))
+ (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
+ kmacro-step-edit-inserting t
+ kmacro-step-edit-appending t)
+ (setq kmacro-step-edit-active 'append-end))
+ (setq act t)
+ t)
((eq act 'help)
(setq executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
(setq kmacro-step-edit-help (not kmacro-step-edit-help))
(if (> executing-macro-index (or kmacro-step-edit-prefix-index kmacro-step-edit-key-index))
(setq kmacro-step-edit-new-macro
(vconcat kmacro-step-edit-new-macro
- (substring executing-kbd-macro
+ (substring executing-kbd-macro
(or kmacro-step-edit-prefix-index kmacro-step-edit-key-index)
(if (eq act t) nil executing-macro-index)))
kmacro-step-edit-prefix-index nil))
(cond
((eq kmacro-step-edit-active 'ignore)
(setq this-command 'ignore))
+ ((eq kmacro-step-edit-active 'append-end)
+ (if (= executing-macro-index (length executing-kbd-macro))
+ (setq executing-kbd-macro (vconcat executing-kbd-macro [nil])
+ kmacro-step-edit-inserting t
+ kmacro-step-edit-appending t
+ kmacro-step-edit-active t)))
((/= kmacro-step-edit-num-input-keys num-input-keys)
(if kmacro-step-edit-inserting
(kmacro-step-edit-insert)
(add-hook 'pre-command-hook 'kmacro-step-edit-pre-command nil nil)
(if kmacro-step-edit-key-index
(setq executing-macro-index kmacro-step-edit-key-index)
- (setq kmacro-step-edit-key-index executing-macro-index))))
+ (setq kmacro-step-edit-key-index executing-macro-index))))
(defun kmacro-step-edit-macro ()
(add-hook 'post-command-hook 'kmacro-step-edit-post-command t)
(add-hook 'minibuffer-setup-hook 'kmacro-step-edit-minibuf-setup t)
(call-last-kbd-macro nil nil)
- (if kmacro-step-edit-replace
- (setq last-kbd-macro kmacro-step-edit-new-macro))))
+ (when (and kmacro-step-edit-replace
+ kmacro-step-edit-new-macro
+ (not (equal last-kbd-macro kmacro-step-edit-new-macro)))
+ (kmacro-push-ring)
+ (setq last-kbd-macro kmacro-step-edit-new-macro))))
(provide 'kmacro)
;;; kmacro.el ends here