tags:

views:

231

answers:

1

Hi,

Is there a way to convert an emacs macro into elisp, not like what M-x insert-kbd-macro does, the actual activity becoming elisp statements.

Thanks for your help.

+14  A: 

Nope, sorry. There is no trivial way to convert an emacs macro into elisp.

Update: There's been some work on Emacs to start down this path. See this thread as a starting point. It's still not possible (June 2010), but there's activity.

The first reason I can think of is dealing with interactive commands and translating keystrokes into proper arguments for functions.

Think of the following sequence:

C-x b .em TAB RET

This begins the command to switch to a buffer, types three characters, uses TAB completion to complete it and RET to accept. The equivalent lisp for the end result (in an emacs session where the TAB completion is unique) is:

(switch-to-buffer ".emacs")

Thinking of completion, there are also interactions with expansion of all types (dabbrev, hippie-expand, etc.).

A starting point can be M-x edit-last-kbd-macro which (in my case) shows this:

;; Keyboard Macro Editor.  Press C-c C-c to finish; press C-x k RET to cancel.
;; Original keys: C-x b .em <tab> RET

Command: last-kbd-macro
Key: none

Macro:

C-x b       ;; switch-to-buffer
.em         ;; self-insert-command * 3
<tab>       ;; pabbrev-expand-maybe
RET         ;; newline-and-indent

Which at least gives you some of the function names. But you'll see that RET is labeled as 'newline-and-indent which is incorrect because at the time of the macro execution, the minibuffer is active and the binding is in fact 'minibuffer-complete-and-exit. Similarly, the proper binding for TAB is 'minibuffer-complete.

Trey Jackson
Thanks for the update, Trey. Looks interesting.
Sahasranaman MS