Skip to content

Enable multiple REPLs#186

Open
zauster wants to merge 3 commits intogcv:masterfrom
zauster:feature/multiple-repls
Open

Enable multiple REPLs#186
zauster wants to merge 3 commits intogcv:masterfrom
zauster:feature/multiple-repls

Conversation

@zauster
Copy link
Copy Markdown

@zauster zauster commented Oct 2, 2025

Hey,

Coming from R and ESS, I use multiple REPLs quite frequently. So I set
out to do it in Julia as well. This is my first Emacs Lisp PR: So
while the code works (I have been using it for the past week), the
code could definitely need more elegance.

Added functions

  • julia-snail--port-in-use-p
  • julia-snail--find-free-port
  • julia-snail-switch-or-create-repl

When calling julia-snail in a Julia buffer, it now does the following:

  • If there is no REPL yet, create one (as before)
  • If there is an REPL, let the user choose to create a new REPL or switch to an existing one

When a buffer has already been associated with a REPL and you want to connect it to
a new or other one, call julia-snail-switch-or-create-repl.

What does not work:

  • Renaming the REPL buffer after it is created: then the user has to
    manually update the julia-snail-repl-buffer variable too
  • Exiting an REPL still leaves the REPL name in the list of REPLs

At the moment, I simply record any new REPL buffers (and their ports)
in a list when they are created. Obviously, this is error-prone, as
this approach fails when a REPL buffer is exited or renamed. So what
would still be nice to have is a way to get the list of active REPL
(and their ports) at a given time. Do you happen to know this could be
done?

@zauster zauster mentioned this pull request Oct 2, 2025
@zauster zauster force-pushed the feature/multiple-repls branch from a09e8d8 to da04a97 Compare October 6, 2025 10:03
@gcv
Copy link
Copy Markdown
Owner

gcv commented Oct 8, 2025

Thank you for all this work. I think your approach of keeping track of the running REPLs and their ports is reasonable, but probably needs to be refined by hooking into the events that occur when a REPL buffer is killed or renamed.

I’m concerned that the way you check for open ports by calling ss is very portable. Not sure what the right approach is.

Broadly speaking, you implemented an automatic port choosing mechanism, whereas I tried to avoid doing all that work and forced port selection to be manually done by the user. :) This is a good feature, but I won’t have time for a while to properly review or work on it. Let’s keep this open, and I’ll get to it when I can.

@zauster
Copy link
Copy Markdown
Author

zauster commented Oct 8, 2025

I agree, the port-checking function is quite far from being portable. There is no guarantee that ss is even installed on a Linux system. I didn't find an Elisp-exclusive way to check for occupied ports. AI suggested to use the make-network-process Elisp function (if the connection is refused, that port is already in use), but that seemed a bit over-the-top for me...

@zauster zauster force-pushed the feature/multiple-repls branch from da04a97 to 37ef0fb Compare October 13, 2025 09:27
@rossviljoen
Copy link
Copy Markdown

rossviljoen commented Dec 8, 2025

Another approach to finding a free port is to use make-network-process with :service 0 (I think :service t would work as well?). This finds a random free port, which you can then extract from the network process. This is used in emacs' own jsonrpc code:

https://github.com/emacs-mirror/emacs/blob/24297f6d9a2fb3bf464f25bdf80c85b13aa04a36/lisp/jsonrpc.el#L1154

The downside is of course that the chosen port is much less predictable than your approach

Added functions
- julia-snail--port-in-use-p
- julia-snail--find-free-port
- julia-snail-multiple-repls
@zauster zauster force-pushed the feature/multiple-repls branch from c13e3c5 to 3de465b Compare December 15, 2025 07:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants