1
votes

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 "M-x" 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 "M-x". 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 "M-x" 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 "M-x"? Thanks!

3

3 Answers

1
votes

If you actually did this:

M-x name-last-kbd-macro RET start-conway-project
M-x 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 M-x, or to which you can bind a key sequence:

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

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 . . . 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
1
votes

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 M-x to invoke the command, you can use C-x p c F (all bookmark commands are on prefix key C-x 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.)