Here’s a DedicatedKeys approach to keyboard macros. Bind some key to ted-macro-dwim; this is the key that you use to start recording, to stop recording, and also to execute. You might want to bind some other key to ted-macro-clear if you do this. I’m currently using F8 and M-F8 for these. Perhaps C-F8 or S-F8 could be bound to name-last-kbd-macro.
(defun ted-macro-dwim (arg)
"DWIM keyboard macro recording and executing."
(interactive "P")
(if defining-kbd-macro
(if arg
(end-kbd-macro arg)
(end-kbd-macro))
(if last-kbd-macro
(call-last-kbd-macro arg)
(start-kbd-macro arg))))(defun ted-macro-clear () "Clear out the last keyboard macro." (interactive) (setq last-kbd-macro nil) (message "Last keyboard macro cleared."))
This is not actually quite ideal of what I’d call “dwim”. Better to use one key to start/end a macro and another dedicated key that always execute the last macro.
(defun start-or-end-macro (arg)
(interactive "P")
(if defining-kbd-macro
(if arg
(end-kbd-macro arg)
(end-kbd-macro))
(start-kbd-macro arg)))(global-set-key '[f2] 'call-last-kbd-macro) (global-set-key '[(control f2)] 'start-or-end-macro)