Scala REPL in Emacs - scala

I like to do my Scala development in Emacs, and from time to time, I use the REPL to test out snippets of code or to load and test some code I've just written. I'm using Scala 2.9.1, and I've noticed that when I open the REPL in a terminal buffer, things are substantially broken. In particular, I am unable to remove any characters from the current line, and cannot move the cursor backward on the current line. This is highly frustrating as any time I mistype something, I have to begin the command anew. I was wondering if anyone else is having this problem using the Scala REPL under Emacs, and if anyone has a potential solution.

I find that rlwrap (readline wrapper) plays well with emacs' ansi-term and scala. Just call "rlwrap scala", and you'll have the usual bash line editing, as well as history, working properly. As a bonus, your history will span multiple invocations of scala, so you won't lose everything you've typed after exiting the Scala REPL.
If rlwrap doesn't work for you, just switching to line-mode in ansi-term (C-x C-j by default) will allow you to use emacs-style editing on the line, but without the shell niceties like history and completion.
Ensime has already been mentioned, so I'll just second that as a great option if you don't mind the setup involved.

You may install ENSIME and follow instructions. I don't know about your way to invoke REPL, but REPL called from ENSIME works (it allows moving the cursor backward and deleting symbols, I've checked).

You could try using the ammonite repl, amm. I can confirm tab completion works in the emacs ansi-term. This is true for its default mode (based on stty) or you can use JLine3 with amm. Note that the Dotty (tentative Scala 3) repl, dotr, is also based on amm and also has working tab-completion in ansi-term.

Related

Use mit-scheme with REPL and editor together

I'm going through SICP course and as recommended installed mit-scheme. I want to use the REPL together with a scheme file. The reason is because I can add scheme code in the file and then run the commands in REPL. What I have works, but the problem is every time I edit the file, I have to quit terminal and reload the file for REPL to see changes.
Is there a way to reload the file easily or some other way for REPL to see changes from the file?
This my setup:
I installed mit-scheme with brew install mit-scheme
I have a local file named code.scm
In terminal, I load the file with mit-scheme --load /Users/name/Desktop/code.scm
Terminal now starts the REPL and everything works. The problem is that if I add new code to the file code.scm, I have to quit terminal and call this again: mit-scheme --load /Users/name/Desktop/code.scm
System details:
macOS Catalina - 10.15.6
Default Mac Terminal app - Version 2.10
MIT/GNU Scheme running under OS X
The text editor I use is Atom - 1.50.0
Question Edit #1 (Based on answer below)
I tried following instructions but this is complicated.
This is what I did:
Run mit-scheme < /Users/Desktop/code.scm
After this I ran mit-scheme --edit to open Edwin. I tried to use the code inside of the code.scm file but it doesn't recognize it. This is the code in code.scm file:
This is what I want to be able to do:
Notice in this picture, I can type a command, press enter and it automatically runs command. However, I want to be able to call (fib 5) and it references the function in code.scm file.
Could someone explain step by step how to do this? It's confusing looking at documentation for scheme websites.
There's actually a built-in load procedure available in the MIT Scheme REPL.
Evaluating
(load "path/to/file.scm")
causes the Scheme file located at path/to/file.scm to be evaluated at the top level (note that the double quotes around the file name are required).
And, as it turns out, this same function can be used to reload a file.
With this in mind, a possible "workflow" might look like this:
Create new source file
Evaluate (load "path/to/file.scm") in the REPL
Edit source file
Evaluate (load "path/to/file.scm") in the REPL
...etc.
Unfortunately, I don't think there is a built-in "reload" procedure.
But...if you find yourself reloading a lot (as I imagine you will), you can always quickly write your own at the beginning of a hacking session:
(define (reload)
(load "path/to/file.scm"))
And then just call (reload) whenever you make a change to your source file.
If you're interesting in using Emacs, I'd say it's worth a shot.
There's a bit of a learning curve, but it's not as steep as it looks up front :)
Also, I cannot recommend the Racket programming language(s) enough.
There is an incredibly straightforward way to set it up for SICP, and it's a much more forgiving environment than Emacs.
Let me know if you are interested and want any help getting started.
You should use emacs with xscheme.el. It works much better.
If you continue as you do, you can also do mit-scheme < code.scm or mit-scheme --edit code.scm and you will also get a *repl* buffer inside edwin.
I recommend you the emacs way, however.
A (load "/path/to/file") command should be available to you within MIT Scheme and the Edwin editor it comes with. However, I would actually recommend that you use Emacs, and use Geiser within that to access both the REPL and to help with scheme file editing. It also makes dealing with various Scheme REPLs such as Chez Scheme, Racket, MIT Scheme, Guile, Chicken, Gambit and Chibi Scheme fairly effortless. The same (load "/path/to/file") command would then be available to you within the REPL running under Geiser, within Emacs, but is generally much more powerful and seamless than when using the "naked" REPL. Emacs is very well tuned to use with Scheme and LISP. Highly recommended.
Evaluate entire buffer: press M-o (M is Alt on Windows). When in source file window, press it. It will evaluate the entire buffer i.e. (re)load the entire source file (without even saving it first). I found it by googling "mit scheme edwin tutorial". Edwin is kind of Emacs itself.
This page says: "C-c C-s when done in a scheme-mode buffer [i.e. Scheme source file window], will switch to the Scheme interaction buffer [i.e. REPL]". i.e. you press C-x C-s to save file, M-o to evaluate (i.e. load), C-c C-s to switch to the REPL.
If you've split your screen with C-x 2 between a source file buffer and the REPL ("interactions buffer"), you can switch between them by pressing C-x o (for "go to the other window").

Open new python shell on C-c C-c in python-mode.el

I have a small GTK python application that imports a package (Twisted) that may not be loaded twice.
If I run my application in emacs with python-mode.el and press C-c C-c, the application gets executed in a python shell window.
If I now close the application, the python shell stays up and running. If I now press C-c C-c again, emacs "reuses" the old python process and thus I run into problems because I'm installing a Twisted reactor twice.
Is it possible to have python-mode.el open a new shell window each time I execute a buffer?
python-mode.el comes with a command py-execute-buffer-dedicated,
opening a new and reserved process for it
In python.el, a new inferior process is launched in a new buffer if the python-buffer variable is set to nil. Therefore, it's possible to advise the python-send-buffer function to reset that variable to nil after every invocation, thereby forcing a new Python process to be executed for every subsequent python-send-buffer command. Something like the following should work:
(defadvice
python-send-buffer
(after python-send-buffer-new-proc activate)
(setq python-buffer nil))
(ad-activate python-send-buffer)
I know that your post was asking for help with python-mode.el, but I thought it might be helpful to mention this anyway, as I'd surprised if python-mode.el doesn't use a similar mechanism. If I have time, I'll try to look into it.
Edit: the python-mode.el package uses the command py-shell to initiate a new inferior Python process. I found a mailing list posting in which a user provides an ad hoc function that appears to do what you need.
By the way, it might be worth considering that trying to alter the default behavior of python-mode isn't the best approach to this problem. I don't know what your code does, and I'm not particularly familiar with Twisted, but it seems to me that experiencing major errors when evaluating your code a second time within the same session could be a sign of a more fundamental design problem. I fail to see how it could be a matter of multiple imports of the same module being the issue, as Python modules are only loaded once, with successive import statements having no effect (for that, an explicit reload or execfile() is required). If I'm completely off-base here, I apologize, but I felt this possibility might merit mention.

Is there a fast way for connect clojure swank and slime for a fast repl in emacs?

I've a question:
When I use emacs with clojure and elein (leiningen extension) I write my code inside a file, then I need connect to swang, I type "elein swank" and open the conexion...2 step) I open a repl...3) I type slime-connect (and press y two times) 4) this step is really annoying: I need use my file or change the namespace...
so far so good..the problem is if inside my file there is a little mistake (maybe a parentheses) now I insult my code and I've repeat all steps...again!!
for me this is really annoying, I really like emacs, I've used this for long time and is the best editor, but comparing this to netbeans (I try this today..It's nice but its repl suck...) with netbeans I only need ONE click for do all these steps...
I can press "load file" and this load my file inside repl....seriously!!
and if I press refer alias/file in NS it open the repl and change the namespace...so good
now..my question is if is possible create a command inside emacs than make everything..maybe and it would be really nice..a command than open a repl and load my file or my ns...would be great...is it possible?...has someone do it??..thanks
thanks a lot a have a good day!!
elein-swank should automatically connect to the swank backend after starting it for you. There was a recently-fixed bug which prevented this for some swank-clojure versions, so you might want to update your elein.el to the latest version.
You can use elein-reswank to restart the backend and reconnect to it if necessary.
Once it's running, C-c C-k will compile and load a .clj file in the backend. C-c C-z will flip you to the REPL from any clojure source buffer.
Hopefully those tips will help to streamline your emacs/slime experience -- it's really a nice working environment, so stick with it if you can! :-)
You might want to try out swank-clojure if you aren't already.
With swank-clojure you just M-x clojure-jack-in and it loads the REPL. If you call it from within a project.clj file, it makes all the namespaces of your project available. It takes a few seconds to start, but after that it's very easy and you don't have to reload.
Why you need to reload everything if you made one typo? You just need to run lein swank once and connect to swank using slime-connect... And then you can load and evaluate your code as you want. To (re-)load your file you can use slime-load-file command, that is bound to C-c C-l...
You can also to look to M-x clojure-jack-in command from fresh clojure-mode...
P.S. I personally run swank sessions for a whole day (and sometime several days), without leaving it, writing new code, evaluating it, etc.

Enabling autocomplete in sbcl

I have recently started to learn lisp, and have mainly bin using clisp and vim. I wanted to try sbcl as well, since this is often recommended as one of the best, free lisp compilers. There is one thing, though, which makes sbcl more difficult to use for me: I can not get autocomplete in the REPL, which I do have in clisp. That is, when I start sbcl and type the following (as an example),
* (requi<tab>
where <tab> is the literal tab character, I do not get a list of completions, but rather a verbose tab character. In clisp, <tab> will complete the previous line to * (require.
As I am fairly new to lisp, the autocomplete functionality in clisp is really handy, so it would really be very convenient if anyone could explain how to get it in sbcl as well.
One way of getting an autocompleting repl in SBCL is to use linedit, from http://common-lisp.net/project/linedit/. A second is to use rlwrap, a readline wrapper, with a suitable completions file. (I think it's fair to say that neither of these is as commonly used as emacs, which of course also provides a completing REPL with a number of other useful features.)
I don't know of a way to get an auto-completing REPL in SBCL, but I find that interfacing with my lisp environment from within SLIME is quite handy and at that point, you can use C-c C-i for auto-completion.
Actually it's completely possible. Yes, You want to work with Common Lisp via Emacs and Slime (I prefer SLY). But it's another wall beginners hit.
You want to just play from the REPL?
The following instructions allow autocomplete in SBCL with rlwrap.
https://www.cliki.net/CMUCL%20Hints
1) install rlwrap
2) create shell alias, for example putting text like
alias rs="rlwrap sbcl"
into your ~/.bashrc (or ~/.profile or whatever).
(or you can continue calling sbcl rlwrapped via "rlwrap sbcl")
3)Edi Weitz created a completion list file that is now gone from his website, so i'm linking to the Internet Archive. save this wordlist into a file "sbcl"
https://web.archive.org/web/20031207221537/http://weitz.de/files/cmucl_completions
4)You can try putting the file according to the instructions on Cliki, this will only apply for the user you are logged in under. I wanted it to work for all users, so I put the "sbcl" file into my rlwrap completion directory, which is in
/usr/share/rlwrap/completions/
So now I have a file /usr/share/rlwrap/completions/sbcl
That contains the words.
5)Create / adjust
~/.inputrc file add the line
TAB: complete
5) Done, now in a new terminal (or after reloading .bashrc)
I can launch SBCL via rlwrap with the alias "rs"
start typing (def (or whatever) and hit TAB, and get auto-completion suggestions.
Beginner Bonus - if you want to edit lisp in the terminal, from the REPL, in say, vim with parinfer, try magic-ed, which will allow you to edit files from the repl. Configuring SBCL to use ED is esoteric. This solves that issue for You.
https://github.com/sanel/magic-ed
With tab auto-completion and convenient way to edit lisp from the terminal, one can start learning Common Lisp in the terminal.
If you want to use sbcl, emacs and slime, follow this Modern Common Lisp on Linux tutorial. The tutorial mentions installing quicklisp and especially (ql:quickload "quicklisp-slime-helper"), an elispscript which getting slime to do autocomplete and more.

What program can help a REPL shell remember and search history?

I'm playing with Paul Graham's arc, and it's getting really annoying that the up arrow inserts ^[[A instead of the previous command, and ^R doesn't work as in shell. I vaguely remember there being a simple way to run Arc's REPL in a program which will remember the input history - does anyone know what it is?
Perhaps you're thinking of rlwrap?
I like rlwrap too, but other options include:
Emacs modes:
shell
term
comint
Slime (works with Common Lisps and Clojure, probably not with Arc)
ssfe (the frontend part of sirc)
jline (especially if the REPL is written in Java, which arc is not)
nex3's arc on github comes with a script arc.sh which calls rlwrap by default, and an emacs mode to use the history with alt+P and alt+N.