How to automate loading leiningen repl from within Emacs? - 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.

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)

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 is the correct way to use emacs/cider while developing a compojure/ring-based application?

What is the correct workflow/pathway of usage of emacs/cider while developing
a compojure/ring-based clojure application?
I feel that I can "attach" to my running compojure/ring-process,
change its code, read/change its data, but I can't understand how do I do it right?
What is the correct way?
What I do?
lein new compojure my-project
cd my-project
lein ring server-headless
The development server runs now. If I change files in the projects they will be automatically reloaded. That is good. But what I'd like to have is that I attach
direct to the process and change its functions for example.
I understand that it is possible, but I can't understand how.
I don't know about correct but I'll throw in my 2 cents.
I start my ring project using immutant which starts a REPL at a specified port. I start cider with M-x cider and connect to the previously specified port. From there I can modify things from the REPL.
I've also seen other people start jetty from inside the REPL though I've never tried this.
There are two main ways of doing what you want. None of them are specific to ring servers, or even to webservers, they'll apply to any Clojure Project.
Both of the methods below should give you a fully functional REPL, with complete control to redefine the functions in your running server, and full CIDER functionality (like being able to debug web-requests to the server).
As usually with CIDER, you can reload changed files with C-c C-k, which will redefine any functions you've changed. There are plenty of other keys for more fine-tuned evaluations as well.
M-x cider-jack-in (or C-c M-j)
As documented on the manual this starts a process with your project and connects a REPL to it. This won't call any functions for you (CIDER doesn't do that), but you can easily start your webserver by calling the corresponding function in the REPL. If the function in question is the -main function, you can do M-x cider-run to call it (bind that to a key if you'd like).
M-x cider-connect
Also as documented on the manual, you can start your webserver from the terminal like you normally would, and then call M-x cider-connect to open a REPL in it. (This is what I used to do a while back).
A bit late to the party. But as I have just to deal with the same issue and found this unanswered question. The answer could be found at:
lein ring server with nrepl doesn't honour cider-nrepl
Basically, use the plugin version 0.9.2 of lein-ring and add to the :ring configuration on project.clj :nrepl {:start? true} and it works

clojure nrepl in emacs saving state - always evaluating entire file on nrepl-jack-in

I'm using emacs live to set up my clojure environment. 1) What command would cause nrepl to try and evaluate the entire file upon jack-in? 2) Is there some place where session state is saved?
For some unknown reason emacs is now trying to evaluate the entire clojure file when I execute M-x nrepl-jack-in. It used to not do that. I have an error in my file and, since the execution fails, I can't start nrepl with this file. This is not the first time I've run into some semantics that change. I've tried restarting emacs but that doesn't help.
Thanks
nrepl's jack-in behavior is dependent on the location of the file backing the buffer you are visiting when you invoke the jack-in command.
To get a vanilla no-project repl, be sure to run nrepl-jack-in from a buffer where there is no Clojure project. To run a repl in a specific project via jack-in, run it while visiting a file in the top level of that project (project.clj is a prime cantidate).

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.