Executing >1 line of Clojure code in the REPL from Emacs - emacs

I'm following the clojure tutorial here and it shows me how to execute one line of clojure code in the repl by invoking C-x C-e at the end of the line. Now how would I execute more than 1 line of clojure code...for example...if I was in a clojure file with lots of code but I only wanted to execute a subset in the REPL....any way to do that?
eg.
...
...
(defn hello-world
[]
(printf "Hello World")
)
(hello-world)
;; I'd like to execute the previous 5 lines with a key combination
...
...
```

The shortcut C-c C-c executes the entire sexp your on, the top-level form.
see all shortcuts for cider here

Select all the sexps you want to evaluate.
M-x cider-eval-regionto evaluate all those statements in order.

Related

SLIME on Emacs with paredit in repl - how to prevent execution of incomplete but balanced expressions?

I use paredit on emacs with SLIME's repl. This means that at any point during my typing on the repl, my s-expressions are balanced.
However, they may not be complete, and I might want to continue typing inside them in another line, as follows:
CL-USER> (defun print-hello ()
)
When I start a new line by pressing the enter key, however, the SLIME repl executes my incomplete expression. I want it to wait for me to complete the expression, as follows:
CL-USER> (defun print-hello ()
(format t "Hello, world"))
How do I make this happen please?
For that situations, when writing long s-expressions in REPL I think that the best way is to use the slime scratch buffer. you can edit it and after that execute with
C-j
No problem pressing enter inside the buffer, I'm using sly but the capture could be like this:
(defun print-hello ()
(format t "Hello, world"))
; => PRINT-HELLO
Also another alternative is working without the last parent :-(
or as suggested in a comment by #jkiisky, type the expression and add in the middle of the s expression C-j
CL-USER> (defun
)
Related to your question, lispy provides integration with SLIME.
I typically never type anything into the REPL buffer. Instead, I edit all code in place in the source file, and use e to eval the current sexp.
lispy is also a super-set of paredit, if compatibility is your concern.

emacs kbd macro - record key incorrectly

According to this post, I have defined eval-and-replace in my .emacs file.
However when I tried to eval and replace the following list of sexp using a macro, it will mess up on my mac. Whereas on my Linux machine or just do eval-and-replace with a single sexp, it will work.
(+ 1 2)
(+ 1 2)
(+ 1 2)
(+ 1 2)
(+ 1 2)
So starting from the beginning of each sexp, the macro I defined is the following: C-e C-c e <down> C-a.
But when I ran the macro stepper the actual macro is C-e C-e C-c C-c ee <down> C-a. I am wondering how come it will record some commands twice.
And I also got an error from the macro stepper Error in post-command-hook (kmacro-step-edit-post-command): (void variable kmacro-step-edit-active
According to the wiki post-command-hook is related to the previous command. But I have no clues about how to resolve this.
After commenting out some parts in my .emacs file, I have found that there is a bug in the flash-paren.el file, which flashes the paren while defining a kbd macro but not executing a macro.
By adding (not (defining-kbd-macro)) in flash-paren-check function solves the problem.
So it is not a hardware problem.

emacs `racket-mode` REPL not loading recognizing procedures

I just installed racket-mode in my emacs 24.3, when I run the REPL via racket-repl command, the REPL starts correctly, but some of the racket procedures/functions aren't recognized. i.e
> (class object%)
; class: undefined;
; cannot reference undefined identifier
> (enter! "test.rkt")
; enter!: undefined;
; cannot reference undefined identifier

The current value of the racket-racket-program variable is set to Racket.exe. On the other hand if I just run Racket.exe from the windows command line, then the REPL works as expected.
Any help with this is greatly appreciated.
When you do a racket-repl, that REPL initially opens with the equivalent of #lang racket/base. At the prompt you could type (require racket) to get the bigger language, including the class stuff like object%.
(There's an open issue about this. Feel free to chime in there.)
Instead of typing (enter! "test.rkt") at the prompt, try ,run test.rkt.
In fact, the easiest way to do this is with an Emacs buffer open on test.rkt. Then you can press C-c C-k a.k.a. M-x racket-run.
(Note that this leaves point in the test.rkt buffer. If you prefer point to go to the REPL, instead you can use M-x racket-run-and-switch-to-repl, bound by default to F5 like in DrRacket.)
TL;DR the most common use pattern with racket-mode is:
Visit a .rkt file in a buffer.
"Run" it with C-c C-c (or F5).
Explore in the REPL interactively as you wish.
Goto 2.
If your file contains #lang racket then the REPL picks up what language to use.
If no such line is present my guess is that racket/base is used -- and object% is not defined in racket/base.
Try entering this program:
#lang racket
(+ 1 2)
Then start the REPL and try your snippet again.

Convert Emacs macro into Elisp

Is there a way to convert an emacs macro into elisp, not like what M-x insert-kbd-macro does, the actual activity becoming elisp statements.
Thanks for your help.
Nope, sorry. There is no trivial way to convert an emacs macro into elisp.
Update: There's been some work on Emacs to start down this path. See this thread as a starting point. It's still not possible (June 2010), but there's activity.
The first reason I can think of is dealing with interactive commands and translating keystrokes into proper arguments for functions.
Think of the following sequence:
C-x b .em TAB RET
This begins the command to switch to a buffer, types three characters, uses TAB completion to complete it and RET to accept. The equivalent lisp for the end result (in an emacs session where the TAB completion is unique) is:
(switch-to-buffer ".emacs")
Thinking of completion, there are also interactions with expansion of all types (dabbrev, hippie-expand, etc.).
A starting point can be M-x edit-last-kbd-macro which (in my case) shows this:
;; Keyboard Macro Editor. Press C-c C-c to finish; press C-x k RET to cancel.
;; Original keys: C-x b .em <tab> RET
Command: last-kbd-macro
Key: none
Macro:
C-x b ;; switch-to-buffer
.em ;; self-insert-command * 3
<tab> ;; pabbrev-expand-maybe
RET ;; newline-and-indent
Which at least gives you some of the function names. But you'll see that RET is labeled as 'newline-and-indent which is incorrect because at the time of the macro execution, the minibuffer is active and the binding is in fact 'minibuffer-complete-and-exit. Similarly, the proper binding for TAB is 'minibuffer-complete.
I made a package that allows pretty much exactly this at https://github.com/Silex/elmacro
It has some quirks but it works pretty well... for example, the following macro:
F3 C-e M-b M-u C-a C-n F4
Generates the following elisp:
(defun upcase-last-word ()
"Change me!"
(interactive)
(move-end-of-line 1)
(backward-word 1)
(upcase-word 1)
(move-beginning-of-line 1)
(next-line 1 1))

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