The sequence you specify does simplify the keystroke representation. What you're left with is essentially the set of keystrokes you typed.
It's already been asked if you can convert an emacs macro into elisp.
Perhaps you're asking if it could be made more human readable?
If so, then you're in luck.
You can use the kbd
macro to convert the printed representation of keystrokes into the equivalent vector of keystrokes.
For example, the sequence which results in a query-replace
of 3
with tj
looks like: M-% 3 RET tj RET !
Well, you can manually set that up with:
(fset 'my-hand-crafted-kbd-macro (kbd "M-% 3 RET tj RET !"))
And this piece of elisp code should generate the above for you if you have named your macro 'my-hand-crafted-kbd-macro
:
(defun insert-pretty-kbd-macro (macro-name)
(interactive (list (intern (completing-read
"Insert kbd macro (name): "
obarray
(lambda (elt)
(and (fboundp elt)
(or (stringp (symbol-function elt))
(vectorp (symbol-function elt))
(get elt 'kmacro))))
t))))
(interactive)
(insert "(fset '")
(prin1 macro-name (current-buffer))
(insert "\n (kbd \"")
(insert (key-description (symbol-function macro-name)))
(insert "\"))\n"))
To get this to happen automatically when you finish a keyboard macro C-x ), you can use this advice:
(defadvice kmacro-end-macro (after kmacro-end-macro-name-it-and-insert activate)
"user wants to insert elisp to reproduce the last kbd macro"
(let ((name (read-string "Name for this kbd macro: ")))
(insert "(fset '")
(insert name)
(insert "\n (kbd \"")
(insert (key-description last-kbd-macro))
(insert "\"))\n")))