将先前定义的宏添加到 Emacs 中的宏环中

发布于 2024-09-17 19:39:14 字数 413 浏览 7 评论 0原文

我一直在使用 kmacro 命令(例如 kmacro-name-last-macro)来保存键盘宏。问题是,在我保存宏并将其添加到我的 .emacs 文件中后,我遇到了错误,并希望使用 kmacro-step-edit-macro 编辑宏。如果我命名的宏不再位于宏环中(默认 kmacro-ring-max 为 8),我将无法在该宏上使用任何编辑或宏环命令。在得知 name-last-kbd-macro 会保存更容易编辑的符号形式后,我后悔使用 kmacro-name-last-macro 并想知道这是为什么新的默认值。

有没有办法将先前定义的宏添加到宏环中,以便我可以使用 kmacro-step-edit-macro 对其进行编辑?

I've been using kmacro commands such as kmacro-name-last-macro to save keyboard macros. The problem is that after I have saved a macro and even added it to my .emacs file, I come across an error and want to edit the macro using kmacro-step-edit-macro. If my named macro is no longer in the macro ring (the default kmacro-ring-max is 8) I can't use any of the editing or macro ring commands on that macro. After learning that name-last-kbd-macro will save the symbol form which is easier to edit, I regret using kmacro-name-last-macro and wonder why it is the new default.

Is there are way to add a previously defined macro to the macro ring so I can edit it with kmacro-step-edit-macro?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

雨后咖啡店 2024-09-24 19:39:14

是的,有一种方法可以将先前定义的宏添加到宏环中,以便您可以使用 kmacro-step-edit-macro 对其进行编辑:

假设您已使用 name-last-kbd-macro< 命名了键盘宏 tata /code>,并为 tata 完成了 insert-kbd-macro。例如:

(fset 'tata
   [return return ?f ?o ?o return])

您可以将此宏定义存储到 .emacs 中以供以后使用。在新的 emacs 会话中,您可以使用以下 lisp 代码将宏放回到 kmacro-ring 中:

(kmacro-push-ring (list 'tata 0 "%d"))
(kmacro-pop-ring)

之后,您可以对其执行 kmacro-step-edit-macro

如果您使用 kmacro-name-last-macro 命名宏,而不是 name-last-kbd-macro,则调用 insert-kbd-macro< /code> 将为您的宏插入不同的定义,使用 lambda 函数而不是向量或字符串(以便能够存储当前计数器),例如:

(fset 'tata
   (lambda (&optional arg) "Keyboard macro." (interactive "p")
   (kmacro-exec-ring-item
      (quote ([return return 102 111 111 return] 0 "%d")) arg)))

在本例中,kmacro-step-edit-宏 会引发错误,因为这不是向量或字符串。要解决此问题,您可以:

  • 将 lambda 函数转换为经典向量宏定义(例如上面 tata 的顶部定义)。通常总是可以进行这种转换。

  • 或者定义一个调用您的 lambda 函数宏的宏,例如: (fset 'foo [?\Mx ?t ?a ?t ?a return]) 然后您可以放置​​此 foo如前所述,宏进入 kmacro 环。但在这种情况下,您可能会在宏执行结束时产生一些副作用。

Yes, there is a way to add a previously defined macro to the macro ring so you can edit it with kmacro-step-edit-macro :

Imagine you have named a keyboard macro tata using name-last-kbd-macro, and done a insert-kbd-macro for tata. For example :

(fset 'tata
   [return return ?f ?o ?o return])

You can store this macro definition into your .emacs for later use. On a new emacs session, you can use the following lisp code to put back your macro into your kmacro-ring :

(kmacro-push-ring (list 'tata 0 "%d"))
(kmacro-pop-ring)

After that, you can do a kmacro-step-edit-macro on it.

If you have named your macro using kmacro-name-last-macro instead of name-last-kbd-macro, the call to insert-kbd-macro will insert a different definition for your macro, using a lambda function instead of a vector or a string (to be able to store the current counter), for example :

(fset 'tata
   (lambda (&optional arg) "Keyboard macro." (interactive "p")
   (kmacro-exec-ring-item
      (quote ([return return 102 111 111 return] 0 "%d")) arg)))

In this case, kmacro-step-edit-macro raises an error as this is not a vector or a string. To solve this problem you can :

  • either transform your lambda function to a classic vector macro definition (like, for example, the top definition of tata above). It is normally always possible to do this kind of transformation.

  • or define a macro that calls your lambda function macro, for example : (fset 'foo [?\M-x ?t ?a ?t ?a return]) And then you can place this foo macro into the kmacro ring as said before. But in this case, you could have some side-effects at the end of the macro execution.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文