In clojure cider repl, when some code goes wrong, I have to C-b k, kill the cider-error buffer, C-x o switch back to the repl buffer. Is there a shortcut/function to jump back to the repl buffer directly?
Just press q in the *cider-error* buffer and you'll be back in the REPL.
Related
I am new to Clojure, I wish to create a command line in clojure.
I am using lein, The app is simply waiting for user to type something and when press enter, it will print the line.
I cannot seems to make Clojure wait forever with lein run
Is there any other way?
Here is my code.
(defn -main [& args]
(read-line)
)
so when I type something and press enter, the whole code stops,
I want to take the input of user typing and process it continuously.
I mean each time user press enter, he/she should be able to continue to next line and program will run forever.
You need to loop for the user inputs then and provide some means to break the loop (yet, ctrl-c also works). E.g.
(loop []
(let [input (read-line)]
(if (= input "quit")
(println "bye")
(do
(println "You said: " input)
(recur)))))
How can I get readline-like (or rlwrap-like) functionality from my REPL when I use the repl function from clojure.main?
The background to this is that I'm utilizing and customizing the break function from The Joy of Clojure, First Edition. I'm using it from inside the lein repl REPL. When my "breakpoint" kicks in, the readline-like functionality of Leiningen's REPL is gone, which is kind of inconvenient. My muscle memory makes me hit ↑ followed quickly by Enter. Before I can stop myself, I've got this in my terminal:
debug=> ^[[A
CompilerException java.lang.RuntimeException: Unable to resolve symbol: in this context, compiling:(/tmp/form-init13211381000659590518.clj:1:1)
And now my REPL is stuck and I have to kill the terminal or the process to get out. I'd like very much if I could either get readline working in this second-level REPL or at least prevent this common issue from derailing my debug sessions.
You should use rebel readline, a new repl for clojure developed by bhauman the same guy who brought is figwheel.
https://github.com/bhauman/rebel-readline
It has rlwrap features, syntax highlighting and multi line code editing all in the terminal.
I'm not sure the rlwrap utility would help there, because the inner REPL is held by the outer one. So the input is being controlled by Java code, not the rlwrap tool.
You are causing an exception since you input a wrong value. I remember, the clojure.main/repl function might take an additional argument to handle exceptions. Probably, you could handle it somehow and just print a string "wrong input value" instead. Take a look at the documentation for REPL.
Also, you may implement your own REPL for debugging. Long ago, I used to write some kind of it, here what I've got:
(defn repl []
(let [input (read)]
(if (= input 'q)
nil
(do
(try
(let [result (eval input)]
(println result))
(catch Exception e
(println e)))
(recur)))))
That function just prompts for a proper Clojure expression in an endless loop, evaluates it and prints the result. In case of a wrong input, it prints the error and carries on. To leave the REPL, input q.
Example:
(repl)
(+ 1 2) ;; 2
fsdf8 9_fsd ;; prints a stack trace
q ;; returns nil and exit
Try Emacs with Cider as your repl. When you (break) you'll be bumped out of the Cider repl and into the Emacs Minibuffer, where your standard emacs editing shortcuts (upon which readline is modeled) continue to apply.
I'm puzzled by the following behavior:
(do (println "Say hi.") (println (read-line)))
I would expect the message "Say hi." to appear in the console before the program blocks and waits for input. Instead the program blocks right away and only outputs "Say hi." after the user has responded. What's going on here and how is this program written properly?
If you look at the source of println you'll see it calls prn. When you look at the source of prn you'll see it flushes *out* on a newline when *flush-on-newline* is bound to true.
What is the value of *flush-on-newline* in your REPL?
This is probably a buffering issue. Try issuing a (flush) before the readline call.
How does one read a single keystroke from the terminal (not Swing) in Clojure?
I have tried a few things including various versions of the JLine library, but have not gotten it working (see example below).
I will happily accept a working, Unix-only (Mac, Linux, ...) example. Ideally I'd like to know how to switch buffering off for both stdin and stdout.
Here's something close:
;; project.clj dependencies:
;; [[org.clojure/clojure "1.4.0"]
;; [jline/jline "2.8"]])
(ns slosh.core
(:import [jline.console ConsoleReader])
(:gen-class))
(defn -main []
(println "start")
(let [cr (ConsoleReader.)]
(.readCharacter cr)
(println "done")))
This prints "start" but does not respond to any input except control-C.
I'm not sure how you are running this, but if you are using lein run, you will run into problems. Try using lein trampoline run.
I would link Single character console input in java/clojure but I don't seem to have enough Internet Points to do that.
Maybe also have a look at clojure-lanterna.
I'm trying to exit the REPL.
I use (. System exit 0) or (System/exit 0), but that causes an error:
Exception in thread "Thread-3" java.lang.RuntimeException: java.lang.IndexOutOfBoundsException
Is there another way to exit the REPL? How I can resolve this error?
You can send the 'end-of-file' character.
You can just press ctrl-d (*nix) or ctrl-z (Windows) to exit the REPL.
My answer is now 10 years old and was given a context of less understanding (although I think I shared the same confusion as the original asker so it kind of works).
(System/exit 0) does indeed exit the whole JVM - that might be what you want "to exit the REPL" but not necessarily. Clojure and it's REPL are designed to run in a multi-threaded environment and you can even have multiple REPLs connected to the same process. Obviously exiting the JVM is not what you want if you want to exit a REPL in an otherwise continuing process.
Original answer below:
It looks like you have a different problem in your code.
The way to exit the repl is:(System/exit 0)
The alternative syntax (. System exit 0) also works.
You can test this from a clean repl started with:
java -cp clojure.jar clojure.main -r
The exception you get would seem to indicate an error in some indexed lookup before your code gets to the intended exit point, apparently on a different thread.
The problem with (System/exit 0) is that it kills the whole JVM. The real question is how to programmatically exit just the current repl, and return to whatever function launched that repl.
Here is a convenient hack:
(clojure.main/repl
; Exit the repl whenever the user enters "exit" at the prompt.
:read (fn [request-prompt request-exit]
(let [form (clojure.main/repl-read request-prompt request-exit)]
(if (= 'exit form) request-exit form))))
clojure.main/repl repeatedly calls a a reader, by default repl-read, to get one form at a time. One of the arguments to the reader is a special sentinel object that the reader is supposed to return when there are no more forms to be read. The default reader, repl-read, returns the sentinel value only on EOF. At the repl-read prompt, you do not have access to the sentinel object, so you cannot return it to tell the evaluator that you have finished entering forms. By installing your own reader, you can check for a particular form -- e.g., the symbol exit -- and return the sentinel object whenever this form is read, thus indicating to the evaluator that you are ready to exit the repl, without actually killing the entire VM.
i just wanted to exit my REPL and landed here.
This seems to be a question that comes to everyones mind when starting to do first steps in the Clojure REPL. And of course I did not read the start-up message. The answer for my Clojure 1.7.0 is (exit) or (quit) or Control-d as stated in other replies.
nREPL server started on port 49276 on host 127.0.0.1 - nrepl://127.0.0.1:49276
REPL-y 0.3.7, nREPL 0.2.10
Clojure 1.7.0
Java HotSpot(TM) 64-Bit Server VM 1.8.0_72-b15
Docs: (doc function-name-here)
(find-doc "part-of-name-here")
Source: (source function-name-here)
Javadoc: (javadoc java-object-or-class-here)
Exit: Control+D or (exit) or (quit)
Results: Stored in vars *1, *2, *3, an exception in *e
user=> (exit)
Bye for now!
You may use the following key combination to exit Cider REPL in emacs: C-c C-q
If you install "lein" then it's exit or quit like this:
$ lein repl
...
user=>exit
Bye for now!
On Mac, use CTRL + C twice, see:
to exit the repl:
If you're running the repl from the command line then (as Sean mentions) ctrl-d
if you're running it from slime then Alt-x slime-quit-lisp should do it.
if you're running it from eclipse then i'm not sure there is a clean way to exit use the little red button.
today I have the answer, this is what I need
(import ('java.lang.management ManagementFactory)
(use 'clojure.contrib.shell)
(defn process-pid [] (let [m-name (.getName (ManagementFactory/getRuntimeMXBean))] (first (.split m-name "#"))))
(defn exit (sh "kill" (process-pid))
In Windows (tested on Windows 7 x64), using the standard command shell (cmd.exe), the ctrl-Z (followed by the enter key) character appears to be the end-of-file indicator, it dropped me out of the REPL back to the command prompt. I suspect that this will also work equivalently in Windows PowerShell, would someone who is familiar with it please test and confirm...
Note also that this is for the stock Clojure REPL - if you are running the Datomic shell (which appears to be an instance of a Java Beanshell), enter "quit();"...
HTH.
On Clojure 1.10.2 installed on Linux
(. System exit 0)