Convert Emacs macro into Elisp

前端 未结 2 1187
轻奢々
轻奢々 2020-12-11 00:45

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 fo

相关标签:
2条回答
  • 2020-12-11 01:34

    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))
    
    0 讨论(0)
  • 2020-12-11 01:36

    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.

    0 讨论(0)
提交回复
热议问题