Function: insert-kbd-macro
insert-kbd-macro is an autoloaded, interactive and byte-compiled
function defined in macros.el.gz.
Signature
(insert-kbd-macro MACRONAME &optional KEYS)
Documentation
Insert in buffer the definition of kbd macro MACRONAME, as Lisp code.
MACRONAME should be a symbol; if none is given, the function inserts
the definition of last-kdb-macro.
Optional second arg KEYS means also record the keys it is on
(this is the prefix argument, when calling interactively).
This Lisp code will, when executed, define the kbd macro with the same definition it has now. If you say to record the keys, the Lisp code will also rebind those keys to the macro. Only global key bindings are recorded since executing this Lisp code always makes global bindings.
To save a kbd macro, visit a file of Lisp code such as your ~/.emacs,
use this command, and then save the file.
Probably introduced at or before Emacs version 18.
Key Bindings
Source Code
;; Defined in /usr/src/emacs/lisp/macros.el.gz
;;;###autoload
(defun insert-kbd-macro (macroname &optional keys)
"Insert in buffer the definition of kbd macro MACRONAME, as Lisp code.
MACRONAME should be a symbol; if none is given, the function inserts
the definition of `last-kdb-macro'.
Optional second arg KEYS means also record the keys it is on
\(this is the prefix argument, when calling interactively).
This Lisp code will, when executed, define the kbd macro with the same
definition it has now. If you say to record the keys, the Lisp code
will also rebind those keys to the macro. Only global key bindings
are recorded since executing this Lisp code always makes global
bindings.
To save a kbd macro, visit a file of Lisp code such as your `~/.emacs',
use this command, and then save the file."
(interactive (list (intern (completing-read "Insert kbd macro (name): "
obarray
#'kmacro-keyboard-macro-p
t))
current-prefix-arg))
(if (string= (symbol-name macroname) "")
(pp `(setq last-kbd-macro
(key-parse ,(key-description last-kbd-macro)))
(current-buffer))
(let ((definition (symbol-function macroname)))
(when (or (stringp definition) (vectorp definition))
(setq definition (kmacro (kmacro--to-vector definition))))
;; Prefer `defalias' over `fset' since it additionally keeps
;; track of the file where the users added it, and it interacts
;; better with `advice-add' (and hence things like ELP).
(let ((counter (kmacro--counter definition))
(format (kmacro--format definition)))
(pp `(defalias ',macroname
(kmacro ,(key-description (kmacro--keys definition))
;; FIXME: Do we really want to store the counter?
. ,(unless (and (equal counter 0) (equal format "%d"))
`(,counter ,format))))
(current-buffer)))
(when keys
(let ((keys (or (where-is-internal definition '(keymap))
(where-is-internal macroname '(keymap)))))
(dolist (key keys)
(pp `(keymap-global-set ,(key-description key) #',macroname)
(current-buffer))))))))