简体   繁体   中英

How to name a action sequence and invoke it in M-x interface in Emacs

I am using nrepl in Emacs 24 as Clojure IDE. When I want to write some codes in a Clojure project(after starting Emacs), I have to repeat the following commands:

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

I want to save all above actions as a command and invoke it in "Mx" interface. Following this instruction I record my actions and save it as "last-kbd-macro" in ~/.emacs file:

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

Now the following variable is added to my ~/.emacs file:

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

And after restarting Emacs, I can use F4 to invoke this macro. However, what I want is defining my own command and invoke it in "Mx". So I modify above definition as:

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

But when I use "Mx" and input "start-conway-project", there is a "[no match]" sign and it doesn't work.

What is the "emacs" way to define some actions as a command (macro or something else) and invoke it with "Mx"? Thanks!

Here are my notes regarding macro usage. The first paragraph is a step-by-step how to create and then manually copy into your .emacs file -- there are at least three ways to invoke starting a macro, and an equal number of ways to stop recording a macro. The second paragraph is a function that does it for you.

However, I recommending creating a lisp function instead of using a macro. The macro looks something like fset . . . fset . . . and then you can create a keyboard shortcut that looks something like: (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

If you actually did this:

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

Then the code you would (should!) have been given was not

(setq last-kbd-macro ...

but:

(fset 'start-conway-project ...

The former sets a variable; the latter sets a function, which you can call with Mx , or to which you can bind a key sequence:

(global-set-key (kbd "C-c s") 'start-conway-project)

Here's another approach:

With Bookmark+ you can use command bmkp-make-function-bookmark with a prefix argument to define a bookmark that does what the last named keyboard macro does. In effect, this makes your keyboard macro persistent and lets you use ordinary bookmark "jumping" to enact it.

When you invoke bmkp-make-function-bookmark with a prefix arg you are prompted for the name of the bookmark to be created. Besides using Mx to invoke the command, you can use Cx pc F (all bookmark commands are on prefix key Cx p , which is bound to keymap bookmark-map ).

(Just FYI, related command bmkp-wrap-bookmark-with-last-kbd-macro adds the code of the last keyboard macro to a bookmark.)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM