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))
(insert "(fset '"))
(prin1 macroname (current-buffer))
(insert "\n ")
(if (stringp definition)
(let ((beg (point)) end)
(prin1 definition (current-buffer))
(setq end (point-marker))
(goto-char beg)
(while (< (point) end)
(let ((char (following-char)))
(cond ((= char 0)
(delete-region (point) (1+ (point)))
(insert "\\C-@"))
((< char 27)
(delete-region (point) (1+ (point)))
(insert "\\C-" (+ 96 char)))
((= char ?\C-\\)
(delete-region (point) (1+ (point)))
(insert "\\C-\\\\"))
((< char 32)
(delete-region (point) (1+ (point)))
(insert "\\C-" (+ 64 char)))
((< char 127)
(forward-char 1))
((= char 127)
(delete-region (point) (1+ (point)))
(insert "\\C-?"))
((= char 128)
(delete-region (point) (1+ (point)))
(insert "\\M-\\C-@"))
((= char (aref "\M-\C-\\" 0))
(delete-region (point) (1+ (point)))
(insert "\\M-\\C-\\\\"))
((< char 155)
(delete-region (point) (1+ (point)))
(insert "\\M-\\C-" (- char 32)))
((< char 160)
(delete-region (point) (1+ (point)))
(insert "\\M-\\C-" (- char 64)))
((= char (aref "\M-\\" 0))
(delete-region (point) (1+ (point)))
(insert "\\M-\\\\"))
((< char 255)
(delete-region (point) (1+ (point)))
(insert "\\M-" (- char 128)))
((= char 255)
(delete-region (point) (1+ (point)))
(insert "\\M-\\C-?"))))))
(if (vectorp definition)
(macros--insert-vector-macro definition)
(pcase (kmacro-extract-lambda definition)
(`(,vecdef ,counter ,format)
(insert "(kmacro-lambda-form ")
(macros--insert-vector-macro vecdef)
(insert " ")
(prin1 counter (current-buffer))
(insert " ")
(prin1 format (current-buffer))
(insert ")"))
(_ (prin1 definition (current-buffer))))))
(insert ")\n")
(if keys
(let ((keys (or (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)))))))