Cannot eval Clojure buffer in inferior REPL - emacs

I'm trying to work with Clojure using inferior lisp mode. My setup:
(setq inferior-lisp-program "lein repl")
(run-lisp) ;; Clojure REPL appears then...
When I evaluate short forms using lisp-eval-region, there are not any errors. But, when I try to eval a long region or even entire buffer, I face the following strange behavior:
The input text sent to the REPL buffer is full of rubbish symbols:
(doseq [addr addresses][53G[38G[54G
For unknown reason, some symbols are cut and so cannot be found:
(u/validat (:entity_id obj)[61G[46G[62G Whereas in the code I have (u/validate-access!.
More comprehensive dump is here.
ps: I've been working with cider for a long time, just wondering are there any alternatives.

The "rubbish symbols" are ANSI escape codes for putting the cursor on a certain column. Your REPL process is outputting these, and Emacs's process buffer doesn't know how to handle them.
Notice that in a lein repl when you type a closing parenthesis your cursor will briefly jump back to the opening one, that's what those escape codes do.
You're right that inf-lisp should work, Rich Hickey used it for a long time, maybe he still does. I would try using it with the plain built-in Clojure REPL, instead of with Leiningen's, since the latter really expects an actual terminal on the other end.
(setq inferior-lisp-program "lein run -m clojure.main")
Also consider using inf-clojure, it's very similar to inf-lisp, but is better suited for use with Clojure.

Related

Emacs slime management, user input and multi eval and print

I'm on ubuntu 19.
Using emacs, slime and sbcl to practice some lisp.
Currently I have one buffer in slime mode in one window and the slime-description in the other window.
When I want to execute a line, I write it on the buffer and press C-c C-p.
But when I try to do the same for the line
(defvar *name* (read))
to set the the name var with the user input, nothing is happening.
Why ?
Also I would like to execute the whole script and not one line at a time, how do I do that ?
'Nothing is happening' because read is waiting for you to type something at the REPL. If you look at the REPL you will be confused because the form you are evaluating is not displayed, so all you see is ... nothing, but you need to type something at it. Further, it's not clear from your description what buffers you have displayed, but I suspect the REPL is not one of them, which is going to make things even worse.
I don't know how other people use SLIME, but what I do is to have at least the REPL (the thing you get after typing M-x-slime in one window, and a file I am working on in another. You can then interact with the REPL just by typing at it, and send code to the running lisp from the file with C-M-x or any of the other commands (in particular things like C-c C-k which compiles & loads the file.
However you almost never want a file you are compiling or loading to include anything which causes read to be called at compilation or load time: the results are going to be mysterious to put it mildly: the system will just stop with no prompt waiting for you to type something. It makes much more sense to do that in the REPL:
CL-USER> (defvar *name* (read))
(here is the data I am typing in)
*NAME*
Indeed, even when you go to some lengths to make calls to read non-mysterious in files being loaded, you have to go to yet further lengths to make them safe. Consider this file, toxin.lisp:
(defvar *my-thing*
(progn
(format *query-io* "~&thing? ")
(finish-output *query-io*)
(read *query-io*)))
Now:
$ lisp
[...]
(load "toxin" :verbose t)
;Loading #P"toxin"...
thing? #.(quit)
Of course there are much worse things I could have said than that to the Lisp.

Python Interpretor in Emacs, removing the input reprinting

I am quite new to Emacs.
When running Emacs' python interpretor, it does
>>> print(24)
print(24)
24
Is there a way I can prevent the re-printing of my input and make it as below?
>>> print(24)
24
Thank you so much :)
The trick here is that the buffer you're running the python process in doesn't have comint-process-echoes set.
There are a couple of other questions that are relevant to your problem.
How to turn off the echoing
How to set emacs so it always turns off echoing
But the basic gist is you need to customize the value of comint-process-echoes. If you are new to emacs, you might not know that most customizations are done using emacs lisp, where setting a variable looks something like this:
(setq variable-name new-value)
In this case, the variable we want is comint-process-echoes so the lisp we want to evaluate is:
(setq comint-process-echoes t)
Where t is lisp-speak for "true."
So, to borrow the advice of the first link above, to actually tell emacs to evaluate this lisp code, use the M-: (meta+colon) command. From the python shell buffer, type meta+colon, then type (setq comint-process-echoes t) then hit return. Your problem should be solved.

Emacs Macro to Start in Shell Mode and Run a Command

I have a confession: I don't know Lisp. Despite that fact, with a bit of help from some co-workers, I managed to write an emacs macro/script which:
switched to shell mode (ie. M-x shell-mode)
disabled truncating lines (ie. M-x toggle-truncate-lines)
started a database console (ie. "mysql")
I was then able to start emacs with that macro using the --script option, and suddenly I had a way to start mysql in a much friendlier environment with a single command :-)
But here's the problem: I changed jobs and left that script behind. Now I'd very much like to re-create that script at my new job, but I no longer have any emacs experts to help me write it like I did at the old job.
Now, I really hate SO posts where someone basically says "please write my code for me", so I don't want to do that. However, if any emacs macro experts could at least give me some pointers (like "here's how you invoke a M-x command in a macro"), or point me to an emacs-macro-writing guide, or otherwise "teach me to fish" on this issue, I would greatly appreciate it.
... and if someone just happened to have a similar script already lying around that they wanted to post, I certainly wouldn't complain ;-)
Most emacs commands (i.e., M-x toggle-truncate-lines) can be translated directly to elisp by wrapping them in parentheses:
(toggle-truncate-lines)
The rumours are true, in lisp you just scatter parentheses around and they make magic.
Now in this case, you can do better. Toggling makes sense for an interactive function, but in a program you don't really want to toggle truncate-lines, you want to turn on truncate-lines. Its the same thing if truncate-lines was turned off to begin with, but you don't know when your program will be run next. Anyways, in Emacs, features are often controlled by a variable. In this case, the variable is truncate-lines, and to turn that feature on, you set the variable to t (which means true).
To do this, use:
(setq truncate-lines t)
We use setq instead of = for assignment, because they made lisp before = had been invented.
For the real scoop you should take a look at Robert Chassel's excellent "An introduction to to Programming in Emacs Lisp". It comes built-in with your emacs, you can get to it with C-h i m Emacs Lisp Intro.
A good way (I think) to start writing elisp functions is to record keyboard macros, and then to analyse them using edit-kbd-macro
For example, if you start recording a keyboard macro using f3, then do interactively all the things you want and terminate the macro using f4, you can see the underlying emacs-lisp commands using M-xedit-kbd-macrof4 (this last f4 is the key binding you'd have used to execute the keyboard macro)
<<shell>> ;; shell
<<toggle-truncate-lines>> ;; toggle-truncate-lines
mysql ;; self-insert-command * 5
RET ;; comint-send-input
Now you can write a script using these functions, looking up the documentation (e.g. C-h ftoggle-truncate-lines) to see if you should call them with special arguments in non-interactive mode.
You should also replace self-insert-command by calls to insert.
This should give you something like the following script, which you can call using emacs --load myscript.el
(shell)
(toggle-truncate-lines 1)
(insert "mysql")
(comint-send-input)
Of course, this might not work as expected the first time, so you might have to eval (setq debug-on-error t) to get debugging information.
What version of Emacs are you using?
In Emacs 24, I have M-x sql-mysql, which does everything you ask and has font-locking.

Slime autodoc while in custom REPL

I'm using SLIME and EMACS for Common LISP, with the SBCL compiler. The autodoc feature of SLIME, where function arguments are shown in the minibuffer, works fine.
But when I execute a custom REPL like the following:
(defun game-repl ()
(let ((cmd (game-read)))
(unless (eq (car cmd) 'quit)
(game-print (game-eval cmd))
(game-repl))))
The autodoc feature doesn't work anymore. Not in LISP buffers, and not in my custom REPL. Probably because the SBCL process is busy with my REPL (waiting for input) and can't communicate with SLIME.
After I start another SBCL process with C-u M-x slime, the autodoc feature works again, but only in LISP buffers.
So, is there a way to get the SLIME autodoc in my custom REPL?
I think you're correct in concluding that the swank backend (in your sbcl process) is busy. IIRC slime has both synchronous and asynchronous commands, and your game-repl would be a synchronous command that wouldn't allow the asynchronous documentation commands to get through to the backend -- in contrast, while composing a regular command in the slime REPL, the backend is idle, so doc queries can get through.
But please forgive me for also wondering whether what you're doing in this particular case makes sense -- the purpose of a custom REPL is presumably one or both of:
Expose a limited or synthetic command set
Provide non-standard control/syntax structures
and in either case, input to the custom REPL might not be equivalent to regular code that slime could auto-doc for you.
Might an option be to provide a limited "game" namespace in which you could play around in the regular slime REPL, and then also provide a separate production-oriented REPL with a reader which would only allow access to symbols in that namespace? (There's a discussion of common-lisp sandboxing here.)

how to write scheme program fast in emacs

(define (cube guess x)
(if (good-enough? guess x)
guess
(improve guess x)))
I'm using emacs+Racket, but when I write in Racket,it doesn't auto-complete.
I also can't write the Anti-brackets in the same line,like this
(define (cube guess x) ). I want to use the 'return' key to make the anti-brackets next line, however the scheme interpreter will compute the expression,then it will be wrong.
then if we write the code in the scheme-mode buffer,it may be some bother, we have to
select the region,then compute in another buffer
Anyone tell me some better ways? sorry for my poor English!
It looks to me like you're using an interactive interpreter, and when you hit the "return" key in the middle of a line, it sends the expression to be evaluated rather than allowing you to edit it further. Is this correct? If so, I would encourage you to take a look at Neil Van Dyke's "Quack" package, which (IIRC) is designed to allow you to edit Racket code using emacs.
If you're not married to emacs, then of course I would also suggest trying to use DrRacket.
It sounds like you're using the scheme interpreter from within Emacs. This is a good start for writing small functions, but you really want to use a REPL (Read-Eval-Print Loop) workflow. Thankfully, Emacs has a ready-made scheme REPL built-in, and has been mentioned elsewhere, there are additional modes (like Quack) that enhance the experience.
In the REPL model, you can freely type expressions in the interpreter if you want to try them out, but most of your coding should take place in the file you're writing. From within that buffer, if you have a scheme interpreter running (M-x run-scheme), then you can send sexps to the interpreter for evaluation without copying manually with C-c C-e. You can use C-M-x to do the same thing.
You can compile the entire file with C-c C-k, and if you have several expression you want to send together, grab them in a region and use C-c C-r to send the region to the interpreter.
There are several other commands that make transferring your code to interpreter easy; you can read more about them in your REPL session by pressing C-h m to describe the keybindings for your current mode.
What does this code even do ? Are you missing the "if" ? That could be part of the reason the interpreter isn't working.?
(if (good-enough? guess x) guess (improve guess x))
Sorry if I just don't understand what you are trying to achieve.