1

我在 Emacs 24 中使用 nrepl 作为 Clojure IDE。当我想在 Clojure 项目中编写一些代码时(在启动 Emacs 之后),我必须重复以下命令:

M-x cd
docs/clojurefiles/conways-game-of-life
M-x nrepl-jack-in
C-x 4 f src/conways_game_of_life/core.clj

我想将以上所有操作保存为命令并在“Mx”界面中调用它。按照这个说明,我记录了我的操作并将其保存为 ~/.emacs 文件中的“last-kbd-macro”:

F3
M-x cd
docs/clojurefiles/conways-game-of-life
M-x nrepl-jack-in
C-x 4 f src/conways_game_of_life/core.clj
F4
M-x name-last-kbd-macro<RET> start-conway-project
M-x insert-kbd-macro<RET> start-conway-project

现在将以下变量添加到我的 ~/.emacs 文件中:

(setq last-kbd-macro
[?\M-x ?c ?d return ?d ?o ?c ?s ?/ ?c ?l ?o ?j ?u ?r ?e ?f ?i ?l ?e ?s ?/ ?c ?o ?n ?w ?a ?y ?s ?- ?g ?a ?m ?e ?- ?o ?f ?- ?l ?i ?f ?e return ?\M-x ?n ?r ?e ?p ?l ?- ?j ?a ?c ?k ?- ?i ?n return ?\C-x ?4 ?f ?s ?r ?c ?/ ?c ?o ?n ?w ?a ?y ?s ?_ ?g ?a ?m ?e ?_ ?o ?f ?_ ?l ?i ?f ?e ?/ ?c ?o ?r ?e ?. ?c ?l ?j return])

并且在重启 Emacs 之后,我可以使用 F4 来调用这个宏。但是,我想要的是定义我自己的命令并在“Mx”中调用它。所以我将上面的定义修改为:

(setq start-conway-project
[?\M-x ?c ?d return ?d ?o ?c ?s ?/ ?c ?l ?o ?j ?u ?r ?e ?f ?i ?l ?e ?s ?/ ?c ?o ?n ?w ?a ?y ?s ?- ?g ?a ?m ?e ?- ?o ?f ?- ?l ?i ?f ?e return ?\M-x ?n ?r ?e ?p ?l ?- ?j ?a ?c ?k ?- ?i ?n return ?\C-x ?4 ?f ?s ?r ?c ?/ ?c ?o ?n ?w ?a ?y ?s ?_ ?g ?a ?m ?e ?_ ?o ?f ?_ ?l ?i ?f ?e ?/ ?c ?o ?r ?e ?. ?c ?l ?j return])

但是当我使用“Mx”并输入“start-conway-project”时,有一个“[no match]”标志,它不起作用。

将某些操作定义为命令(宏或其他内容)并使用“Mx”调用它的“emacs”方式是什么?谢谢!

4

3 回答 3

1

如果你真的这样做了:

Mx name-last-kbd-macro RET start-conway-project
Mx insert-kbd-macro RET start-conway-project

那么你会(应该!)已经给出的代码不是

(setq last-kbd-macro ...

但:

(fset 'start-conway-project ...

前者设置一个变量;后者设置一个函数,您可以使用 调用该函数,M-x或者您可以绑定一个键序列:

(global-set-key (kbd "C-c s") 'start-conway-project)
于 2013-10-16T05:11:51.023 回答
1

这是我关于宏使用的笔记。第一段是如何创建然后手动复制到.emacs文件中的分步说明——至少有三种方法可以调用启动宏,以及相同数量的方法来停止录制宏。第二段是为您执行此操作的功能。

但是,我建议创建一个 lisp 函数而不是使用宏。宏看起来像fset . . .,然后您可以创建一个键盘快捷键,看起来像: (global-set-key (kbd "<f5>") 'chad)

;; Record Macro:  C-x (  |  F3  |  M-x kmacro-start-macro
;; Stop Recording:  C-x )  |  F4  |  M-x save-macro
;;   M-x name-last-kbd-macro RET my-silly-macro
;;   M-x insert-kbd-macro RET my-silly-macro RET
;;   The macro will be inserted into the active buffer, which can then be copied to the init.el

(defun save-macro (name)
    "save a macro. Take a name as argument
     and save the last defined macro under
     this name at the end of your .emacs"
     (interactive "SName of the macro :")  ; ask for the name of the macro
     (kmacro-name-last-macro name)         ; use this name for the macro
     (find-file user-init-file)            ; open ~/.emacs or other user init file
     (goto-char (point-max))               ; go to the end of the .emacs
     (newline)                             ; insert a newline
     (insert-kbd-macro name)               ; copy the macro
     (newline)                             ; insert a newline
     (switch-to-buffer nil))               ; return to the initial buffer
于 2013-10-16T05:09:09.917 回答
1

这是另一种方法:

使用Bookmark+,您可以使用bmkp-make-function-bookmark带有前缀参数的命令来定义一个书签,该书签执行最后命名的键盘宏所做的事情。实际上,这使您的键盘宏持久化,并允许您使用普通的书签“跳跃”来执行它。

当您bmkp-make-function-bookmark使用前缀 arg 调用时,系统会提示您输入要创建的书签的名称。除了M-x用来调用命令外,还可以使用C-x p c F(所有书签命令都在前缀 key 上C-x p,它绑定到 keymap bookmark-map)。

(仅供参考,相关命令bmkp-wrap-bookmark-with-last-kbd-macro将最后一个键盘宏的代码添加到书签。)

于 2013-10-17T22:20:21.770 回答