Backspace esc char in Clojure's console - clojure

I'm learning Clojure.
To practice I started rewriting Java app I've written for my son to solve inequalities (like 3 + 2 ? 7).
In java I did (note using backslash escape chars)
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.Scanner;
public class JavaTest {
public static void main(String[] args) throws Exception {
System.out.print("3 + 2 ? 7\b\b\b");
System.out.flush();
BufferedReader reader = new BufferedReader(new InputStreamReader(System.in));
reader.readLine();
}
}
and see expected result: cursor placed on the question sign position and when I write a char it overwrites the question sign. correct output from java
But when I do the same (I believe so) in Clojure
(print "3 + 2 ? 7\b\b\b")
(flush)
(let [reader (java.io.BufferedReader. *in*)]
(.readLine reader))
I see that cursor is placed at the correct position for milliseconds and then is moved at the end of the line. And when I put a char, it is placed at very end of the string I printed. incorrect output from clojure
As a workaround I could use clojure-lanterna library, as suggested here, but I'd like to solve such a simple thing without any libraries (as I actually did in Java).
Any ideas on this?
Or maybe someone can explain the reason of the behavior. I tried to find it in Clojure sources, but without success.
Thanks in advance!

Use clojure instead of clj.
clj wraps the ordinary Clojure REPL in rlwrap, which provides a more ergonomic command-line editing experience; presumably here it is messing with the cursor in some way.

You can achieve the above behavior by calling the native Java methods:
(.println System/out "jjjabcdef 99999 777" )
(.println System/out "jjjabcdef 99999 \b\b\b 777" )
=>
jjjabcdef 99999 777
jjjabcdef 999 777

Related

How to run an interactive CLI program from within Clojure?

I'd like to run an interactive CLI program from within Clojure (e.g., vim) and be able to interact with it.
In bash and other programming languages, I can do that with
vim > `tty`
I tried to do the same in Clojure:
(require '[clojure.java.shell :as shell])
(shell/sh "vim > `tty`")
but it just opens vim without giving me tty.
Background: I'm developing a Clojure CLI tool which parses emails and lets a user edit the parsed data before saving them on the disk. It works the following way:
Read a file with email content and parse it. Each email is stored as a separate file.
Show a user the parsed data and let the user edit the data in vim. Internally I create a temporary file with the parsed data, but I don't mind doing it another way if that would solve my issue.
After a user finished editing the parsed data (they might decide to keep it as it is) append the data to a file on a disk. So all parsed data are saved to the same file.
Go to 1st step if there are any files with emails left.
This code relies on Clojure Java interop to make use of Java's ProcessBuilder class.
(defn -main
[]
;use doseq instead of for because for is lazily evaluated
(doseq [i [1 2 3]]
;extract current directory from system variable
(let [file-name (str "test" i ".txt")
working-directory (trim-newline (:out (sh "printenv" "PWD")))]
(spit file-name "")
;this is where fun begins. We use ProcessBuilder to forward commands to terminal
;we pass a list of commands and their arguments to its constructor
(let [process-builder (java.lang.ProcessBuilder. (list "vim" (str working-directory "/" file-name)))
;inherit is a configuration constant
inherit (java.lang.ProcessBuilder$Redirect/INHERIT)]
;we configure input, output and error redirection
(.redirectOutput process-builder inherit)
(.redirectError process-builder inherit)
(.redirectInput process-builder inherit)
;waitFor used to block execution until vim is closed
(.waitFor (.start process-builder))
)
;additional processing here
)
)
;not necessary but script tends to hang for around 30 seconds at end of its execution
;so this command is used to terminate it instantly
(System/exit 0)
)

How can I get readline/rlwrap-like functionality when using clojure.main/repl?

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.

Support for ANSI escape sequence cursor moving

I'm making a simple ASCII animation, and I need the ability to place the cursor at an arbitrary point in the console screen.
While searching, I found this blog that shows this can be achieved by doing:
(print (str (char 27) "[2J")) ; clear screen
(print (str (char 27) "[;H")) ; move cursor to the top left corner of the screen
, which uses ANSI escape sequences.
The first line works as expected, but unfortunately, I haven't been able to find a console that allows for the second line to move the cursor.
After looking up how ANSI escape sequences work, I wrote up this function to ease it's use:
(defn move-cursor-to [x y]
(print (str (char 27) "[" y ";" x "H")))
But when I run (move-cursor-to 10 10), the output is wrong in every "console" I've tried:
IntelliJ/Cursive's REPL ignores it outright; printing nothing.
IntelliJ's Terminal prints the escape character as a ?, and literally prints the rest (?[10;10H)
The Window's 10 command prompt prints something similar to IntelliJ's Terminal, except the ? it prints is inside a box.
Am I doing something wrong? Is there a way to get this to work in the standard Windows 10 command prompt?
I wrote this to fill in the blanks in the meantime:
(defn move-cursor-to [x y]
(let [r #(apply str (repeat % %2))]
(print (str (r y \newline)
(r x \space)))))
but this is a poor solution. It requires clearing the screen prior to use, which for anything beyond a simple animation is unacceptable.
There is an easier way!
There is a much easier way to do this. Have a look at
the clojure-lanterna library.
This library will allow you to address an arbitrary location on a screen. It can
either use a terminal emulator or it can create a swing based window.
Another advantage of using this library is that it also incorporates support for
a virtual window or virtual screen, which can make your output appear to be
much smoother and reduces potential flicker.
The library also has support for ANSI colour codes and a few other nice
features.
Cursive only implements a limited subset of ANSI commands. In particular, most of the caret movement commands don't work. Feel free to file an issue for this, but fixing it is likely to be low priority since it's quite tricky to do in a REPL output pane.

How do I interactively read the input text for this Emacs function?

I am new to Emacs functions. Today is my first attempt to create a function.
I know that count-matches will tell me how many times a regex appears in the rest of the buffer, but most of the time I need to count from the beginning of the buffer. So I tried this:
(defun count-matches-for-whole-buffer (text-to-count)
"Opens the ~/.emacs.d/init.el file"
(interactive "sText-to-count:")
(beginning-of-buffer)
(count-matches text-to-count))
I put this in ~/.emacs.d/init.el and then do "eval-buffer" on that buffer.
So now I have access to this function. And if I run it, it will ask me for text to search for.
But the function only gets as far as this line:
beginning-of-buffer
I never get the count. Why is that?
Two things.
You should use (goto-char (point-min)) instead of beginning-of-buffer.
count-matches will not display messages when called from lisp code unless you provide a parameter indicating so.
Try this code:
(defun count-matches-for-whole-buffer (text-to-count)
(interactive "sText-to-count:")
(count-matches text-to-count (point-min) (point-max) t))

How to get emacs tempo mode working with abbrevs for C/C++?

I've been experimenting with emacs tempo mode and it seems likely to save me lots of typing (always a good thing), but I haven't gotten it to work exactly the way I want it. On the wiki, there is an example for elisp similar to what I want to do which works as expected. Here is the complete .emacs that I tested it on:
(require 'tempo)
(setq tempo-interactive t)
(tempo-define-template "lambda"
'(> "(lambda (" p ")" n> r> ")">)
nil
"Insert a template for an anonymous procedure")
(define-abbrev lisp-mode-abbrev-table "lambda" "" 'tempo-template-lambda)
This allows me to type "lambda" followed by a space and have it automatically insert
(lambda ( )
)
In my buffer with the point on the first closing parenthesis.
However, replacing the last two sexp's with the following code (stolen from Joachim Baumann via Sebastien Varrette and modified by me):
(tempo-define-template "c-include"
'("#include <" r ".h>" > n)
nil
"Insert a #include <> statement")
(define-abbrev c-mode-abbrev-table "c-include" "" 'tempo-template-lambda)
Will not cause the template to be inserted after typing "c-include" followed by a space. This is on emacs 22.2.1 running under Ubuntu 9.04. Does anybody have any idea why this might be the case before I go digging deeper into the tempo code and/or (god forbid) the C-mode code?
The last argument to your define-abbrev should be 'tempo-template-c-include . Also, I'm not sure you can have a dash in there, i.e. it might have to be cinclude instead of c-include:
(define-abbrev c-mode-abbrev-table "cinclude" "" 'tempo-template-c-include)
An alternative to tempo is yasnippet, which I found to be easier to set up interesting expansions.