Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Convert Emacs macro into Elisp

Tags:

emacs

elisp

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.

like image 743
Sahas Avatar asked Nov 11 '09 19:11

Sahas


People also ask

Does Emacs Lisp have macros?

14 Macros. Macros enable you to define new control constructs and other language features. A macro is defined much like a function, but instead of telling how to compute a value, it tells how to compute another Lisp expression which will in turn compute the value.

How do I create a macro in Emacs?

Defining a Macro In order to start recording Emacs keyboard macros you only have to press the <f3> key on your keyboard. The next series of keyboard input commands you use will be recorded in memory. When you have finished the repeatable sequence of keys, press the <f4> key.


2 Answers

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.

like image 63
Trey Jackson Avatar answered Sep 21 '22 23:09

Trey Jackson


I made a package that allows pretty much exactly this at https://github.com/Silex/elmacro

It has some quirks but it works pretty well... for example, the following macro:

F3 C-e M-b M-u C-a C-n F4

Generates the following elisp:

(defun upcase-last-word ()
  "Change me!"
  (interactive)
  (move-end-of-line 1)
  (backward-word 1)
  (upcase-word 1)
  (move-beginning-of-line 1)
  (next-line 1 1))
like image 27
Silex Avatar answered Sep 18 '22 23:09

Silex