How to change REPL mode on startup?

652 views
Skip to first unread message

lapeyre....@gmail.com

unread,
Apr 15, 2016, 9:16:53 AM4/15/16
to julia-users
I have a REPL mode for an application: https://github.com/jlapeyre/SJulia.jl/blob/master/src/sjulia_repl.jl

I start julia in a terminal,  load the package with 'using SJulia', and then press '=' to enter the alternative mode.  This works fine.

I would like to do this in one step. e.g.  enter 'sjulia' in a terminal and then start in the alternative mode. Problems are:

1. I have to execute a julia function to start the REPL mode 'after' the REPL starts and I see the julia prompt. Otherwise there is no active REPL. Using .juliarc.jl or julia -e apparently loads and runs code before starting the REPL.  I would like the alternative REPL mode to be created without entering anything at the julia prompt.

2. Once I create the alternative REPL, I am still in the standard julia REPL mode. To switch to the alternative mode, I enter '='. I would like to enter this mode automatically. There is a function Base.LineEdit.transition that seems to do this. But, it takes a data structure (of type Base.LineEdit.MIState' ) as an argument that I don't know how to get at.  I can probably find it buried somewhere in Base.active_repl, but, I have not been able to so far.

-John

Keno Fischer

unread,
Apr 15, 2016, 12:07:00 PM4/15/16
to julia...@googlegroups.com
You can call REPL.setup_inferface yourself and add your own REPL mode. You can also look at https://github.com/JuliaLang/julia/blob/master/base/client.jl to see how the active_repl gets created.

lapeyre....@gmail.com

unread,
Apr 19, 2016, 6:06:43 PM4/19/16
to julia-users

Thanks. I got it working. I copied much of the code in client.jl. In the end I wrote the new mode directly into a copy of REPL.setup_interface.  There is probably an easier way to get the REPL to start in the new mode rather than julia, but this was faster for me.  There were many details, eg to get warn and error colors working. For me, it would have been far easier to use a hook after the REPL is created, but before it runs. But, I guess there is probably not much demand for that.

I also copied four functions, changing only `Base.parse_input_line(line)' in each (to support dumb terminals, etc.),  to include a macro to post-process the AST.  Again, I don't know if there is any demand for adding a general facility for this.

John

Rafael Fourquet

unread,
Apr 19, 2016, 11:54:22 PM4/19/16
to julia...@googlegroups.com
> Again, I don't know if there is any demand for adding a general
> facility for this.

If you have in mind to make a PR, I would be a client for such a
facility. IIUC, this would e.g. allow me to change automatically the
prompt by calling a function from .juliarc.jl ? (which I do manually
now with "Base.active_repl.interface.modes[1].prompt = ...").

lapeyre....@gmail.com

unread,
Apr 20, 2016, 10:31:17 AM4/20/16
to julia-users
Yes, AFAIK it is not possible to modify the REPL in .juliarc.jl because it is executed before the REPL runs. A tedious workaround is to use .juliarc.jl to create a REPL and then exit() upon completion before the usual REPL is started. I hadn't thought about a PR, but I can take a look at it.
John

lapeyre....@gmail.com

unread,
Apr 20, 2016, 11:57:38 AM4/20/16
to julia-users
You can try this branch https://github.com/jlapeyre/julia/tree/gjl/replhooks

In .juliarc.jl, you can include code like this:

if isdefined(Base, :atreplrun)
    Base.atreplrun( (repl)->repl.interface.modes[1].prompt = "newprompt>" )
end

to install a hook. Call atreplrun repeatedly to push hooks, all of which are run (more or less) just before the loop is entered.


John


On Wednesday, April 20, 2016 at 5:54:22 AM UTC+2, Rafael Fourquet wrote:

Ismael Venegas Castelló

unread,
Apr 22, 2016, 7:29:23 PM4/22/16
to julia-users
You could achieve this behaviour by using an `@async` function and checking until `isdefined(Base,:active_repl)`, like so:

function setup()
    @async while true
        if isdefined(Base,:active_repl)
            # enable LipsREPL
            LispREPL.initrepl(Base.active_repl)

            # enable sticky shell mode
            sticky_shell_mode(true)

            # enable CxxREPL
            !(v"0.4" <= VERSION < v"0.5-") && @eval using Cxx

            break
        else
            sleep(0.25)
        end
    end
end

My current full `.juliarc.jl`:

Then I would try using `REPL.setup_inferface` as Keno mentions inside that async loop and have it execute and change to your repl as soon as `Base.active_repl` is instantiated.

Rafael Fourquet

unread,
May 15, 2016, 4:31:58 AM5/15/16
to julia...@googlegroups.com
John, I tried your branch which works as expected, thank you. I found
that there has been a PR at
https://github.com/JuliaLang/julia/pull/13545 related to REPL hooks,
not sure how much this overlaps with your solution. In any case, I
hope to see this functionality merged.

lapeyre....@gmail.com

unread,
May 16, 2016, 6:47:06 AM5/16/16
to julia-users
Reply all
Reply to author
Forward
0 new messages