Clojure - start a REPL without a project.clj - emacs

I started using Clojure with leiningen (and now boot).
Now I sometimes want to get quickly to a Clojure{Script} CIDER REPL in Emacs to execute just a few instructions. I don't want to create a project.clj file for that, since I just want a throwaway REPL.
Is there a way to get a Clojure REPL, for instance in the *scratch* buffer ?

well, you can execute M-x cider-jack-in anywhere you want, even with no project.clj in path. This works for me.

You can just type lein repl in the friendly console / shell / terminal right next to you -- no project.clj required. This will start a REPL as expected, to which you can then connect from Emacs via M-x cider-connect (which in recent versions will handily suggest host and port to connect to).
M-x cider-jack-in basically does the same thing (i.e. lein repl) behind the scenes.
I'm not a boot user, but according to the boot wiki for leiningen users it should be possible to call boot repl -s.

Use M-x cider-jack-in, and if you don't want it to warn you that you're running cider-jack-in without a Clojure project, add the following to your emacs.d/init.el:
(setq cider-allow-jack-in-without-project t)

Related

print output of execution in cider repl

I am wondering if there is a way to have the output of the current execution in emacs cider when using cider-connect.
For instance :
I run lein repl on a project directory
then connect to it in emacs using cider-connect.
Now let's say that I have (println "cider is amazing by the way") in the code of one of my ring handlers, this will only be printed in the console I ran lein repl when a request is made.
How can I have this output also in my nrepl buffer ?
Sorry to say, I suspect that you cannot do this in the way you describe. The output is going strait to the console of that device and not through anything related to nrepl on the way. This also makes sense if you consider that nrepl is often not even running on the same computer. (the "n" in nrepl is for "network").
Perhaps you can arrange for that output to be teed to a file where you can get at it? Then you could start a thread on your nrepl buffer that cated that file. Or have a buffer in emacs that watches the remote file.
If you start leiningen via cider-jack-in you should get the output in the repl buffer.
One way to use this with a remote setup could be to call C-u M-x cider-jack-in and use a specialized server command that will essentially trigger the lein repl command (say over ssh) on the remote machine -- I haven't checked whether this will actually work, but I don't see any reason why it shouldn't. If you can access the code over tramp, it shouldn't matter much whether the code is remote or not.

What's wrong with my lein/nrepl/emacs setup?

Newbie Clojure programmer here. NREPL in Emacs isn't working for me.
Aquamacs 2.5 (Emacs 23.4.1)
nrepl.el 0.1.8
Leiningen 2.2.0 on Java 1.6.0_51 Java HotSpot(TM) 64-Bit Server VM
My project is just the lein-generated Hello World.
Running "lein repl" in a shell works, but nrepl.el doesn't work.
Rather than winding up in the project's namespace in the repl,
I just get the default toplevel in the "user" namespace.
Should see something like:
my$ lein repl
nREPL server started on port 57347
REPL-y 0.2.0
Clojure 1.5.1
Hello Project
my.core=>
But instead see:
; nREPL 0.1.8-preview
user> (in-ns my.core)
CompilerException java.lang.ClassNotFoundException: my.core, compiling:(NO_SOURCE_PATH:1:1)
user>
In a clean nrepl.el scenario, I see two java processes going and they look plausible. One has my project on the -classpath and is implementing the actual repl (server), while the other is the client side java (-D MyProjectPath -m leiningen.core.main repl :headless).
I get the same lossage whether I get there by m-x nrepl-jack-in or starting in the shell and then m-x nrepl. I get a repl that doesn't know my project.
I wonder if this stuff works for anyone, or if I have something installed or set up wrong? Lots of "0.xxx" versions of things going on here...
Looks like you are missing a quote before your namespace, try the following:
(in-ns 'my.core)
Note the ' before my.
NRepl always starts in the top-level user namespace. Use M-x nrepl-set-ns, or C-c M-n in a Clojure buffer, to set the namespace of NRepl to the one of the buffer.
In your specific case,
visit src/my/core.clj, press C-c M-j to start NRepl,
switch back to the src/my/core.clj buffer,
press C-u C-c C-z to set the NRepl namespace to my.core and switch back to the NRepl buffer.
C-u C-c C-z is a shortcut for C-c M-n followed by C-c C-z to switch to the NRepl buffer.

Emacs with Slime and Swank for non-leiningen projects

I found this website which explains how to use emacs with leiningen, swank, and slime. Is there a way to use slime + swank in non-leiningen projects i.e. how can I connect to slime/swank repl to run a ad-hoc Clojure script while I write it as demonstrated here?
You need to have swank-clojure.jar in CLASSPATH and your script should have following code:
(require 'swank.swank)
(swank.swank/start-repl 4005)
to start swank process on port 4005 (or some other)...
P.S. You can look onto Incanter's swank script, that pass this code in environment variable, and later it evaluated as part of boostrap script

How to automate loading leiningen repl from within Emacs?

I'm using Emacs, integrated with Leiningen's swank. Each time I need to start working with .clj file I have to:
Run emacs pack/my-ns.clj from console.
Type M-x lein-swank.
Compile buffer.
In Clojure REPL change namespace, i.e. type (in-ns 'pack.my-ns).
How can I automate this process to just run from console something like emacs-clj pack/my-ns.clj and get my environment ready to use?
I don't have an answer to the question you posed, but you should be able to replace your 4th step with C-c M-p while in your Clojure file and then emacs should prompt you as to what namespace you want to be in with the namespace of the file auto-detected so you can simply hit enter. C-c M-p maps to slime-repl-set-package.
Update:
Just stumbled across this. Should be able to take what is said in that answer and modify it so upon initial connection to swank files get compiled. Could also set it up to automatically switch to namespace and make the repl buffer active.
Swank should automatically load the namespace pointed to by :repl-init-script in project.clj when it starts, so if you want to set that to your starting namespace, that should get you started.

Clojure 1.2 is released! How to get swank-clojure to use this?

I just set up a new emacs installation, installed ELPA and installed swank-clojure with using ELPA. Although Clojure 1.2 is released, when I hit "M-x slime", it still downloaded Clojure 1.1 for me instead of 1.2
How do I get it to use 1.2 now that it's available? Do I have to do it manually? Do I have to wait for slime/swank to be updated to use 1.2?
Update: Thanks for the recommendations for using leiningen, but what I'm after is for emacs to just launch a REPL itself. Is this problem happening because the swank-clojure package in ELPA is not being updated?
With Leiningen 1.3, it's easy to create a standalone swank session:
$ lein install swank-clojure 1.3.0-SNAPSHOT
$ ~/.lein/bin/swank-clojure
You can add ~/.lein/bin to your $PATH to make this easier.
Then inside Emacs:
M-x slime-connect
Launching swank from inside Emacs can be done with M-x lein-swank if you are inside a project directory. However, auto-download and install of Clojure and other dependencies via swank-clojure.el is error-prone and deprecated.
I believe that with the sort of setup that you describe, M-x slime will put everything in ~/.clojure and ~/.swank-clojure on the classpath. You can customise this by setting the swank-clojure-classpath variable (I have it customised to use ~/.clojure only; that's where I put the basic set of jars useful for launching one-off experimental REPLs.
As soon as you need more stuff on the classpath, the above is inadequate. There are two options for managing those more complex cases:
Option 1: launch Swank, then connect
The most straightforward approach is to use Leiningen. Put this in your project.clj:
(defproject repl-base "1.0.0-SNAPSHOT"
:description "A project to start Swank in."
:dependencies [[org.clojure/clojure "1.2.0"]
[org.clojure/clojure-contrib "1.2.0"]]
:dev-dependencies [[swank-clojure "1.2.1"]])
Then use lein swank in your project's directory to start a swank server and use M-x slime-connect to connect to it from within Emacs.
As another possibility, David Edgar Liebke's cljr will apparently launch stand-alone Swank instances for you; consult the README for details.
Option 2: launch Swank from within Emacs
Now, to be entirely honest, I tend to start Swank from within Emacs myself -- it's maybe a bit trickier to setup (and thus possibly not advisable in the beginning), but quite convenient later on. See my answer to an older question for one version of a function I use to launch Clojure-specific Swank instances complete with proper classpath configuration for Leiningen-style projects.
I use leiningen to connect emacs slime to the clojure instance. In a project you always end up with a couple of dependencies, which leiningen nicely places on the classpath.
It uses a small clojure file to describe the dependencies, e.g. :
(defproject myprojecy "0.1.0-SNAPSHOT"
:description "A project."
:dependencies [[org.clojure/clojure "1.2.0-beta1"]
[org.clojure/clojure-contrib "1.2.0-beta1"]]
:dev-dependencies [[swank-clojure "1.2.1"]])
You can see that changing versions is just a couple of keystrokes in an editor. Leiningen downloads the dependencies from the 'net which keeps the projects really smal and fast to version control.
running lein swank starts the program and the repl in swank :
ptimac:cljhack pti$ lein swank
user=> Connection opened on local port 4005
#<ServerSocket ServerSocket[addr=localhost/127.0.0.1,port=0,localport=4005]>
you can now connect in emacs using M-x slime-connect and accept the defaults.
You just need to set the swank-clojure-classpath variable before you hit M-x slime
Say if you have the clojure.jar, clojure-contrib.jar and swank-clojure.jar under c:\jars
In the scratch buffer paste this
(setq swank-clojure-classpath '("c:/jars/clojure.jar" "c:/jars/cloure-contrib.jar"
"c:/jars/swank-clojure.jar"))
Hit C-x C-e and the M-x slime , that should do it.
For details checkout
https://github.com/jochu/swank-clojure/blob/master/swank-clojure.el#L32-34
http://en.wikibooks.org/wiki/Clojure_Programming/FAQ#Where_does_swank-clojure_.28SLIME.29_look_for_Clojure.27s_jars.3F