I usually define new keybindings in emacs using
"C-x (" --> (command kmacro-start-macro) "C-x )" --> (kmacro-end-macro) (name-last-kbd-macro) (insert-kbd-macro)But the code it generates looks different than the code I read and modify in various major / minor modes and other emacs utilities.
Is there an emacs tool that simplifies keystroke representation ? Ideally one that translates from ahk keystroke notation to any sane emacs keystroke notation.
Edit: some other reading to clear my confusion: keybinding guide by Jari Aalto.
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")))