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

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.

Related

Clojure - start a REPL without a project.clj

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)

Emacs, Clojure, lein and nrepl: *nrepl* buffer shows up blank

I created a new project with lein. I open core.clj in emacs. I make sure to run M-x clojure-mode, and M-x nrepl-enable-on-existing-clojure-buffers.
Then I run M-x nrepl-jack-in and in the mini-buffer I get
Starting nREPL server...
followed by a message such as:
Connected. You're bound to be unhappy if you optimize everything.
-Donald Knuth
I see that the buffer name is *nrepl*, but the buffer does not contain a Clojure Repl and instead is completely blank.
If I type anything (meaning anything at all,) I get:
Wrong type argument: integer-or-marker-p, nil
If I switch back to my core.clj buffer, and hit C-c C-l,I get the namespaced name of the last function in my buffer in the minibuffer as a result. And if I put my cursor at the end of a function definition and hit C-x C-e, I get:
CompilerException java.lang.RuntimeException: Unable to resolve
symbol: x in this context, compiling:(NO_SOURCE_PATH:1:1)
I tried making a new lein directory with no dependencies using lein new project-name and tried the same steps as above and got the same results.
What else can I check and/or what am I doing wrong?
EDIT: Additional Information
When I type something into the empty *nrepl* buffer and try to press C-x C-e on what I typed, in the mini buffer I get the message:
No Lisp subprocess; see variable `inferior-lisp-buffer'
Also, I am running:
GNU Emacs 24.3.1
Leiningen 2.1.2 on Java 1.6.0_27 OpenJDK 64-Bit Server VM
I just fixed this exact issue on my own setup. Move your .emacs.d to a backup location, and make a backup copy of your .emacs. Make a new .emacs with only the following lines:
(require 'package)
(add-to-list 'package-archives
'("marmalade" .
"http://marmalade-repo.org/packages/"))
(package-initialize)
(require 'clojure-mode)
(require 'nrepl)
Once you install nrepl and clojure-mode via running M-x package-list-packages and installing their respective entries, you will be able to use nrepl.
Add your custom .emacs back in one logical unit at a time, and you should be able to figure out where the conflict was and eliminate it. Be suspicious of anything related to slime / swank.

Unable to evaluate form in nrepl + emacs

I have installed the nrepl, clojure-mode and starter-kit-lisp packages. I am able to start the nrepl by opening my .clj file and then M-x nrepl-jack-in.
However when I evaluate any form in my .clj file by C-x C-e, I get the following message:
inferior-lisp-proc: No Lisp subprocess; see variable `inferior-lisp-buffer'
How do I get it right?
I have had this problem a number of times. In all cases, it has been because while nrepl has started, the nrepl minor mode has not been enabled in the clj buffer. As a result, any evaluation attempts fail with this error.
There should be a "nrepl" menu item. If there isn't, this may be your problem. M-x nrepl-enable-on-existing-buffers should fix it.
I had this problem for a while, and now it appears to have gone away; you might want to try installing clojure-mode and nrepl afresh.
Are you sure M-x nrepl-jack-in worked? If it did, you should be prompted with a repl in emacs, like so:
nrepl requires leiningen 2, so make sure that you use the right version in your project and that the nrepl plugin is properly installed.
Also make sure that the major mode when editing your .clj file is clojure-mode.
You might want to post a screenshot to make it easier to spot the problem.

Why clojure starts with 'M-x slime' for my Aquamacs without any setup?

I have Aquamacs running on my Mac.
I installed the ESK for Aquamacs and it installs ELPA at ~/.emacs.d/elpa. With ELPA, I installed clojure-mode/clojure-est-mode/slime/slime-repl/swank-clojure.
The thing is that when I run 'M-x slime', clojure is run. Why is this? I'm curious as I don't have any setup for slime in my .emacs and even in /Users/smcho/Library/Preferences/Aquamacs Emacs/{Preferences.el, customizations.el}. I mean, I don't have (setq inferior-lisp-program "clojure") or something.
Or, does it have to do with my 'swank-clojure' installation? I see something like this at the inferior-lisp buffer.
(require 'swank.swank)
(swank.swank/ignore-protocol-version nil)
...
#ServerSocket ServerSocket[addr=0.0.0.0/0.0.0.0,port=0,localport=51847]
That's because of swank-clojure.el, a deprecated elisp program. Don't use it, as per my post here, on your next question. It may have bugs and thus isn't suitable to use. Use M-x slime-connect instead, after starting swank up with lein swank.
As Isaac said, swank-clojure.el sets 'slime-lisp-implementations' for clojure (in short, a way to start java with swank-clojure in classpath so that swank can be started). Then slime connects to this port and you get the repl.
The problem with this approach is that, you will need to setup classpath upfront. This gets tedious for each and every project you want to work on. So, best way is to use M-x swank-clojure-project or lein swank and then M-x slime-connect. I prefer swank-clojure-project.

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