Convert Emacs macro into Elisp

半腔热情 提交于 2019-11-27 06:36:59

问题


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.


回答1:


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.




回答2:


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))



回答3:


I don't know whether these commands were introduced more recently than the above comments, but you can do it this way:

Give the keyboard macro a name with 'kmacro-name-last-macro', then run 'insert-kbd-macro' which asks for that name and inserts a function definition at mark.

See: 17.5 Naming and Saving Keyboard Macros



来源:https://stackoverflow.com/questions/1717569/convert-emacs-macro-into-elisp

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!