Cider debug -- how to evaluate stuff while debugging - clojure

Cider debug instructions tell me I can press e to evaluate something while debugging. This gives me a little one-line space in Emacs mini-buffer at the bottom.
Is there a way to switch to the full REPL while in the middle of
debugging a function, with access to all the locals, etc.? Currently
the REPL is hung/frozen while debugging. I'm thinking of something in
the style of how PyCharm or Matlab allow full REPL while in the middle of something.

It does appear that the jacked-in REPL is tied up during debugging.
But there are a few options available through the debugger that may
give you nearly as much as you'd get out of the REPL. A handy one is
to inject a new value for the result you're about to produce.
So you're actually changing the data on-the-fly.
You can inspect the full list of local vars with l. Then see more
about a var with inspect and specifying which.
You can also eval to enter an arbitrary expression just like
you would in the REPL (as you've mentioned). That seems to be a
single-line full REPL, with history, editing, etc. Is there something
you'd want to do in the REPL that you can't do with e or discover
with l or p?

One thing I find really frustrating is that I can't edit a function while the debugger is stoped at the said function, then edit it and re run it with the initial arguments. In cider, if you try to edit a function being debugged, emacs will open the bebugger in a new buffer with the original code. Alternatively, you have the e command that evals things in the minibuffer, which I don't think is a great experience. The closest I came to this is the following:
Imagine you have some function that crashes and you need to debug:
(defn some-fn
[complex-data more-data]
; block of code with some bug
)
I'll create atoms in the namespace and set the value inside the given function:
(def c (atom nil))
(def d (atom nil))
(defn some-fn
[complex-data more-data]
(reset! c complex-data)
(reset! d more-data)
; block of code with some bug
)
Then I'l just iterate on some-fn using the args I now have available in the namespace.
(some-fn #c #d)
I think it's a much better approach than using the eval command and the minibuffer from the cider debugger.

Related

What is the easiest way to load a clojure library all the time?

A question from an utter newcomer to clojure: What if I want to be able to start a clojure REPL from anywhere, for example because I just want to compute an exponent? How can I set up my system to do this? (I've deleted earlier links to not-quite-answers because they were cluttering up the question.) The Pomegranate documentation linked by #Jared314's answer below helped me see that I can do this:
~$ lein repl
...
user=> (use '[cemerick.pomegranate :only (add-dependencies)])
nil
user=> (add-dependencies :coordinates '[[org.clojure/math.numeric-tower "0.0.2"]])
{[org.clojure/clojure "1.3.0"] nil, [org.clojure/math.numeric-tower "0.0.2"] #{[org.clojure/clojure "1.3.0"]}}
user=> (use 'clojure.math.numeric-tower)
nil
user=> (expt 2 3)
8
Yay!
Now how can I make this happen every time I start the REPL, no matter what subdirectory I'm in?
I think I'm just ignorant of basic clojure setup. Sorry about that.
Second major edit:
I've figured out that if I use raw clojure without lein, I can execute commands on startup of the repl. For example, if the file .clojurerc contains the text (print "Yow!\n"), I can do this:
~$ java -cp /usr/local/lib/clojure-1.5.1/clojure-1.5.1.jar clojure.main -i .clojurerc -r
Clojure 1.5.1
Yow!
user=>
Can I do something like this with lein? Or maybe better yet, load clojure.math.numeric-tower automatically in clojure without using lein (since for simple command line experimentation, lein's startup is slower than starting clojure directly).
(It may seem as if I'm not trying to solve this on my own, but I that's not so. I have been doing web searches and experimenting, but I keep hitting brick walls. I'm starting to feel as if clojure is only intended for full-blown programming projects. I had assumed that it could be good for add-hoc experiments and calculations (as lisps traditionally are but Java is not). I'm not trying to incite arguments. I'm just frustrated. There ought to be a simple, well-known formula for doing what I'm trying to do.)
When you want external dependencies you will need either a new project, lein new testproject1, the lein-oneoff plugin, Pomegranate, a Leiningen profile :dependencies entry, or some specific IDE feature. (I know at least LightTable allows external dependencies in their Instarepl, so I assume you can do it in Emacs and CCW.)
It might be best to start with creating a new test project so you can see the project.clj layout. But, if you just want a one-off library in a repl, take a look at the instructions for Pomegranate's add-classpath command. Pomegranate is accessible by default in the lein repl, so their example should work without anything extra.
Edit:
From your updated question, it sounds like you want a persistent repl dependency. You can add [org.clojure/math.numeric-tower "0.0.2"] to your ~/.lein/profiles.clj profile file, under the :repl profile.
{:user {}
:repl {:dependencies [[org.clojure/math.numeric-tower "0.0.2"]]
:repl-options {:init (use 'clojure.math.numeric-tower)}}}
Then when you run lein repl:
(expt 2 3) ;=> 8
Looking at this question a few months later, I realized that I settled on a different solution, but never posted it. I now leave references to math.numeric-tower out of both .clojurerc and .lein/project.clj, since I don't need it for most Leiningen projects. And when I want to use Clojure for quick calculations, I start Clojure via a shell script without Leiningen to avoid slow startup. This is what's in the shell script:
#!/bin/sh
jars=/usr/local/lib/clojure-1.5.1/clojure-1.5.1.jar:/usr/local/lib/clojure-1.5.1/math.numeric-tower-0.0.2.jar
while [ "$1" != "${1%.jar}" ]; do # while param is a jar file
jars="$jars:$1"
shift
done
jars="$jars:."
exec rlwrap java -cp "$jars" clojure.main -e "(use 'clojure.math.numeric-tower)" "$#" -r
The middle part allows you to add jars on the command line, but in practice I don't find that useful, given that I use Leiningen for ongoing projects. Someone else might find that part of the script useful, though. rlwrap is a utility that gives you command line history, though it's not as good as what Leiningen provides, for my purposes.

LightTable IDE legacy REPL mode?

I found
Light Table
is a great tool to try Clojure language.
It has a handy feature called Instarepl -
;; Anything you type in here will be executed immediately with the results shown on the right.
However, I can't find a way to use a legacy REPL mode which I can run a small code such as
(take 100 (iterate inc 1))
Typing the code, sure the Instarepl immediately eval and show
(1 2 3 4 5 ...)
, but does not exactly shows the result.
Please guide. Thanks.
Light Table uses leining for the project management, so you can very easily keep repl open in another window connected to the same project to get the traditional REPL expierence. This lets you switch between the two quickly. Light Table is evolving rapidly and who knows if standalone repl mode has been added since I last looked or will be added soon.
Well, on a second try, I found out that clicking the animated-blue-block-icon shows a bottom pane (console), into which for example println outputs. This was what I needed.

How to compile ClojureScript inside Clojure

I want to compile ClojureScript inside Clojure and am having some problems. I would like to do something like this:
(def x '(map (fn [n] (* n n n)) [1 2 3 4]))
(cljs->js x)
where cljs->js returns JavaScript code. I guess Himera does something similar (first reading ClojureScript from a string), but I don't know enough about ClojureScript to figure it out.
Is there are simple solution to this?
Have you look at the Himera code? Here is where the code sent by the UI is compiled, which basically calls the cljs.compiler from the clojurescript project. Note that Himera is probably a lot more complex than what you are asking for, probably you just need to get the "compilation" function working
once you have the clojurescript dependencies sorted out (which is it's own question) then you can just call the clojurescript emit function. this is used in the Clutch project (couchdb for clojure+clojurescript). it basically looks like this:
(js/emit (aget doc "_id") nil)

How can you dump contents of Clojure REPL to a file?

So I have been working on a Clojure tutorial and it's pretty fun so far. Unfortunately, every time I close my REPL out, I lose all of the defn and def that I created in the previous session.
So, to save time in the future, is it possible to have the Clojure REPL save everything I've typed to a file so I can dig out what I need for future uses?
I think most people work by using their IDE to send fragments of code to the REPL, rather than doing a lot of direct hacking in the REPL. This way you have a better idea of the current state of the program.
If you use Emacs and SLIME, you can save the REPL buffer to a file just like saving any other buffer.
If you are using a Unix-like operating system you can start your REPL using rlwrap. This would give you Ctrl-R for accessing commands from history in an easy way, as well as storing the history itself.
You'd just have to install rlwrap and prepend it to the line where you start REPL, e.g. rlwrap lein repl
Couple of tips:
I do a lot of coding in the REPL, but as soon as I write something that works then I copy/paste it into a source file. Then I can just run or reload that source file whenever I want to get a REPL environment back to a fully configured state.
Some REPLs (e.g. the Eclipse Counterclockwise REPL that I use) keep history from previous sessions so you can up-arrow to the earlier REPL commands you want to repeat.
This is actually a harder problem than it seems because a dump like this must take into account symbol and vars dependencies and output to file in the correct order of symbols. That said since code is data one should be able to write a function that does that.

Is there a colored REPL for Clojure?

I'd like to get a colored REPL for clojure code, similar to what you can do with IRB for Ruby.
Are there any libraries or settings for user.clj that provide automatic coloring of the REPL?
Example IRB:
Rather late to the party, here, but you can get this from using the Leiningen plugin Ultra (which also has support for colorized stacktraces and pretty-printed test output), or by adding Whidbey to your list of Leiningen plugins instead.
Sample Ultra REPL:
I do not know of any way to have the basic Clojure REPL, as started by something like java -cp clojure.jar clojure.main, do syntax highlighting. If, however, you use Emacs & SLIME (the development environment of choice of a great part of the Clojure community!), then you can have the SLIME REPL highlight syntax like clojure-mode does.
First, you'll have to lift some code from the clojure-mode function (defined towards the top of clojure-mode.el):
;;; all code in this function lifted from the clojure-mode function
;;; from clojure-mode.el
(defun clojure-font-lock-setup ()
(interactive)
(set (make-local-variable 'lisp-indent-function)
'clojure-indent-function)
(set (make-local-variable 'lisp-doc-string-elt-property)
'clojure-doc-string-elt)
(set (make-local-variable 'font-lock-multiline) t)
(add-to-list 'font-lock-extend-region-functions
'clojure-font-lock-extend-region-def t)
(when clojure-mode-font-lock-comment-sexp
(add-to-list 'font-lock-extend-region-functions
'clojure-font-lock-extend-region-comment t)
(make-local-variable 'clojure-font-lock-keywords)
(add-to-list 'clojure-font-lock-keywords
'clojure-font-lock-mark-comment t)
(set (make-local-variable 'open-paren-in-column-0-is-defun-start) nil))
(setq font-lock-defaults
'(clojure-font-lock-keywords ; keywords
nil nil
(("+-*/.<>=!?$%_&~^:#" . "w")) ; syntax alist
nil
(font-lock-mark-block-function . mark-defun)
(font-lock-syntactic-face-function
. lisp-font-lock-syntactic-face-function))))
Then add it to the slime-repl-mode-hook:
(add-hook 'slime-repl-mode-hook
(lambda ()
(font-lock-mode nil)
(clojure-font-lock-setup)
(font-lock-mode t)))
Et voilà, next time you connect to the SLIME REPL you'll have clojure-mode syntax highlighting available. If you use SLIME for Common Lisp too, you'll want to tweak this so it doesn't try to do Clojure highlighting with CL. Also, this is just a first approximation; one thing it sort of breaks is prompt highlighting (the namespace> thing will not be highlighted anymore). I'm not a proficient font-lock hacker by any stretch of the imagination, though, so I'll leave it at that. :-)
If you just want to color the prompt and you are using Leiningen (which you should), you can use :repl-options and ANSI escape sequences:
:repl-options {:prompt (fn [ns]
(str "\033[1;32m"
ns "=>"
"\033[0m "))}
References:
GitHub – technomancy/leiningen – sample.project.clj
roguejs.com – “Console colors in node.js”, posted 2011
Install Emacs 24
Install Emacs Starter Kit v2
M-x package-install -> starter-kit-lisp
Add to init.el: (add-hook 'slime-repl-mode-hook 'clojure-mode-font-lock-setup)
Install Swank for Clojure
Open your Clojure project and M-x clojure-jack-in
Some coloring have REPL in VimClojure.
Try out Light Table Playground by Chris Granger. It is the first part of more ambitous multi language IDE.
It has a color Clojure REPL that does real time evaluations and display for entire blocks of code.
http://www.chris-granger.com/lighttable/
You can see a higher level view of the project here:
http://www.kickstarter.com/projects/ibdknox/light-table
CIDER users can obtain a coloured REPL by adding the following to their config:
(setq cider-repl-use-clojure-font-lock t)
The Eclipse Counterclockwise REPL provides full syntax colouring (including rainbow bracket colouring).
I believe it uses nREPL under the hood.
If you're an Intellij user, the excellent Cursive is a great choice, and provides pretty colours that you can customise:
To get your REPL output colorized try repl-color
You can try LightTable, it lets you select the functions and run instantly.