Is there a way to go back to a keyboard macro from elisp? - emacs

I have been creating macros and saving them for later use with the following process.
Create keyboard macro
Edit the macro: C-x C-k C-e
Name the macro: M-x name-last-kbd-macro
Create an .el file then insert the macro definition: M-x insert-kbd-macro
With the lisp code saved for later, I'll load it up in my .emacs file. Perhaps it is my unfamiliarity with elisp, but the elisp code which is generated seems quite unwieldy. I much prefer the syntax in the macro editor, and I'd like to make updates and refinements to the code that I've created, so I ask:
Is there a way to take my elisp code back to a reasonable rendition in the macro editor?

You can use C-x C-k e M-x followed by the name of the macro you want to edit. (Or C-x C-k e (edit-kbd-macro) followed by the key sequence a macro is bound to; M-x here is just a kind of mnemonic, as the help for the command explains).
Maybe it's also helpful to know that all the keyboard macros you've run are saved in a global ring, so you can cycle backwards and forwards through them using C-x C-k C-n and C-x C-k C-p.
BTW, it's not just unfamiliarity with Elisp that makes those macro definitions hard to read; insert-kbd-macro basically just writes out the macro as a vector of key sequences. Elisp written by humans is a much pleasanter language ;-)

Related

Does emacs support multiple macros in a same buffer just as vim?

I'm new in emacs. I know you can use C-x ( to start recording a macro. Use C-x ) to stop recording and use C-x e or F4 to play it. However, I didn't find a way to record multiple macros in the same buffer. I want to know whether emacs support this feature?
Emacs maintains a keyboard macro ring, which stores all the defined macros. You can cycle through the ring by
C-x C-k C-n next macro (kmacro-cycle-ring-next)
C-x C-k C-p previous macro (kmacro-cycle-ring-previous)
You can see which macro will be used in your mini-buffer.
Perhaps you want to read this manual:
https://www.gnu.org/software/emacs/manual/html_node/emacs/Keyboard-Macro-Ring.html#Keyboard-Macro-Ring

Highlight julia code in emacs

I would like to write julialang code in emacs. Is there an easy way to do it like, for example, when you do python code (all these reserved words in colors and indentation)?
Here is what I have done so far:
I created the file
~/.emacs.d/init.el
with the line (after cloning https://github.com/emacs-ess/ESS/wiki/Julia):
(load "/path-to-ESS/ESS/lisp/ess-site.el")
julia is in my path also. Still, emacs does not recognize julia code
I think that this may help. It seems to be emacs support for Julia.
emacs-ess / ESS
Julia
Overview
ESS support for julia language, among other things, includes
font-lock, indentation, sending code to sub-process, interactive
documentation, imenu, completion and eldoc.
Usage
Start
To start julia type M-x julia RET. You can start multiple julia
processes if you so desire.
To autostart or switch form script to julia subprocess use C-c C-z. To
switch from process buffer to the most recent script buffer also use
C-c C-z. To associate a buffer with a different julia process press
C-c C-s.
Evaluation
To send chunks of code from your script use C-c C-c, C-M-x, C-RET etc.
To load the whole file: C-c C-l. See evaluating code section in ESS
manual. (tip: look up the documentation string of these commands with
for example C-h k C-c C-c)
Help
To access documentation for any help topic or object use C-c C-d C-d.
To call apropos: C-c C-d C-a. To look up for a topic in julia standard
library reference: C-c C-d C-r. To look up a topic in the julia
manual: C-c C-d m. To search julia website: C-c C-d C-w.
To see all help commands defined in ess-doc-map: C-c C-d C-h. See Help
section in ESS documentation.

How to bind a key to kill repl and yank in a buffer on emacs

You can assume that I'm in repl using the slime mode.
How can I make a function key (for example, f4), to do this:
kill the last history item (the ones that you get with C-up or C-down);
move to the upper buffer;
yank, Save buffer to file;
move back to the repl.
Please, make it a step by step guide, because I'm a complete beginner to Emacs and Lisp.
The easiest way to make what you ask would be using emacs macros.
Why?
Because you have just said exactly what you want to do.
And macros save the sequence of keys you typed.
You can do it in emacs for one time, and save the sequence of pressed keys.
So, start recording a macro (when you are in the repl buffer) using F3 or C-x (, then make something like M-p C-a C-k C-u - C-x o C-y C-x o(i just translated your request to key sequence), then type F4 or C-x ). To execute macro, press F4 again, or C-x e.
You can interrupt recording a macro if you made a mess with C-g. The reverse is applied, if you made a mess and error message is send, your macro recording(sometimes frustrating) or evaluating(and this is feature, since you can make macro that will work good by just holding F4) would be interrupted.
If you want to use this macro later, you can name it with M-x name-last-kbd-macro. This will allow you to use as a command, typing M-x <your macro name> (<your macro name> - name of your macro). This will not save it for future sessions, just name it.
To save your named macro, use M-x insert-kbd-macro when you are in your .emacs file. This will insert elisp code at current point, executing which you will get your macro binded to your command name(and it will be executed every time you start emacs).
To bind it to some key, rather start it every time from M-x, insert this in your .emacs file: (global-set-key [f12] '<your-macro-name>). You can read more about setting comands to keys there and there.
The bad thing about macro is that you will undo every step, not the whole macro in one time(but someone may bring solution here, if he have one). If you want to make something more serious, using conditions or iterations, you have to forward your path to elisp. Just C-h k everything around. Help keys like C-h f, C-h a, C-h b will also come in use.

Simple Emacs keybindings

I have two operations that I do all the time in Emacs:
Create a new buffer and paste the clipboard. C-S-n
Close the current buffer. C-S-w
Switch to the last viewed buffer. C-TAB
I feel like a keyboard acrobat when doing the first two operations. I think it would be worth trying some custom key bindings and macros.
A few questions about this customization:
How would I make a macro for #1?
Are these good key bindings? (I know this is a bit subjective, but they might be used by something popular that I don't use.)
Has anyone found a C-TAB macro that will act like Alt+Tab in Linux/Windows? Specifically, I want to have a stack of buffers according to the last viewed timestamp (most recent on top). I want to continue cycling through the stack until I let go of the Ctrl key. When the Ctrl key is released, I want the current buffer to get an updated position on the stack.
Have you tried using vertically or horizontally split windows for this (via C-x 3 or C-x 2)? It seems like it would give you fewer steps - even if you implement something like you're talking about.
I find split windows really speed up copy and pasting operations. I use the arrow keys on my num pad to switch among windows (windmove-left/-right/-up/-down), so it's only one key to press and you go to the window you want.
I guess this is a little different from what you're asking for, but it sounds like it might help speed things along a bit.
C-x left and C-x right cycle through buffers, but you have to hit it multiple times, you can't just keep the key pressed down.
For creating a macro for #1, you just start a macro, hit the keys you usually do to create a new buffer, and stop the macro.
So it would be something like:
C-x ( C-x b NEW RET C-x )
You can then save NEW to a file once you're done pasting, so you can use the macro again to create a new buffer. C-x e to try out the macro. If it works you can save it into your init.el file. This is done with:
M-x name-last-kbd-macro
Then you'll get a prompt to enter the name of your choice. This is only good for the current session. Then you save the named macro to your initialization file. First you open your .emacs or init.el file. Then you place point where you want the macro definition to go, then you type:
M-x insert-kbd-macro
Now you can run your macro using its name via M-x <macroname> . You can bind your macro to keys too (in your .emacs or init.el file):
(global-set-key (kbd "C-c a") '<macroname>)
For example this is how your init.el would look after creating a macro that opens a new buffer called NEW that is not associated with a file and binding this macro to C-c n:
;; Creates a new unassociated buffer called NEW
(fset 'new-buffer "\C-xbNEW\C-m");
;; Shortcut for new-buffer
(global-set-key (kbd "C-c n") 'new-buffer)
You can also throw in the paste, buffer close, and buffer switching operations. I guess you'd have to save the buffer to a file manually.
Some resources
Information about macros on EmacsWiki
Possibly useful: Swap text between buffers
start by invoking start-kbd-macro, finish by with end-kbd-macro. Afterwards you may immediately test the new macro with call-last-kbd-macro. If you're happy with the result you might want to save the macro.
Emacs generally doesn't use C-S keybindings and they are easy to use, so I'd call them good. They might cause problems if you're using the terminal version of Emacs, but I assume that's not the case with you.
I use this simple snippet:
(global-set-key (kbd "<C-tab>") 'bury-buffer)
bury-buffer basically makes the current buffer the last in the buffer-list so you'll be able to cycle buffers in a predictable order.
I wouldn't make a macro for that but write a function like someone else posted on this page. Instead of (cua-paste nil) you could also use (yank). I'm not sure which one's better and why.
I don't like them that much. For things that I use often I'd like to do as little finger acrobatics as possible, so that would mean modifier+key instead of modifier1+modifier2+key.. or use a function key if you don't feel tied to the homerow.
no comment

How to save all functions I entered in LispBox/Slime?

Situation: I entered several functions while working with REPL in Emacs.
Problem: There is junk like "; Evaluation aborted" when I'm simply saving buffer.
What I want: clear descriptions of all the functions I entered in their latest revision.
Can I do that? Thanks.
I don't get it. Are you entering definitions at the REPL and expecting to recover them later? Just save a source file as you would in any other language. Use C-x 2 to split your Emacs window in two. Open a source file in one of them C-x C-f foo.lisp. Use C-c C-k, C-c C-r and friends (see SLIME menu) to compile / evaluate regions of your source code in the REPL.
I've looked for something like this in the past and have been unable to find it. You're best off writing all your definitions in a separate buffer and using SLIME's extensive evaluation/compilation functions (C-c C-k loads an entire file, C-x C-e evaluates the last expression, C-c C-r evaluates a region, etc.), only directly entering into the REPL things you don't want to save.
Um, C-x o or C-x b to get to the SLIME REPL buffer, then C-x w or C-x C-s to save it to a file. All the SLIME/CL stuff is a reader comment; you can either write a reader hack to reload the file treating the prompts as comments, or you can go through the file yourself to capture the pieces you want to save.
I agree that the best work flow method is to write your code in a separate buffer and evaluate in that, rather than enter the functions in the repl.
Assuming you have gone the repl way, I guess, C. Martin's solution to save the repl log and manually go through it are your only options.
If you entered the functions and vars into a separate package, you could go through the symbols in the package to help you decide what you want to keep.
E.g. to see all symbols created in the cl-user package:
(let ((p (find-package :cl-user)))
(loop
for s being the symbols in p
when (eq p (symbol-package s))
do (format t "~a~%" s)))