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.
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.
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))
(let (definition)
(if (string= (symbol-name macroname) "")
(progn
(setq macroname 'last-kbd-macro definition last-kbd-macro)
(insert "(setq "))
(setq definition (symbol-function macroname))
;; 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).
(insert "(defalias '"))
(prin1 macroname (current-buffer))
(insert "\n ")
(when (or (stringp definition) (vectorp definition))
(setq definition (kmacro (kmacro--to-vector definition))))
(if (kmacro-p definition)
(let ((vecdef (kmacro--keys definition))
(counter (kmacro--counter definition))
(format (kmacro--format definition)))
(insert "(kmacro ")
(prin1 (key-description vecdef) (current-buffer))
;; FIXME: Do we really want to store the counter?
(unless (and (equal counter 0) (equal format "%d"))
(insert " ")
(prin1 counter (current-buffer))
(insert " ")
(prin1 format (current-buffer)))
(insert ")"))
;; FIXME: Shouldn't this signal an error?
(prin1 definition (current-buffer)))
(insert ")\n")
(if keys
(let ((keys (or (and (symbol-function macroname)
(where-is-internal (symbol-function macroname)
'(keymap)))
(where-is-internal macroname '(keymap)))))
(while keys
(insert "(global-set-key ")
(prin1 (car keys) (current-buffer))
(insert " '")
(prin1 macroname (current-buffer))
(insert ")\n")
(setq keys (cdr keys)))))))