Skip to content

Commit

Permalink
More robust initialization; julia -e "using IPython" -i works now
Browse files Browse the repository at this point in the history
  • Loading branch information
tkf committed Nov 1, 2018
1 parent a0cec45 commit cfbe21b
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 14 deletions.
2 changes: 1 addition & 1 deletion src/core.jl
Original file line number Diff line number Diff line change
Expand Up @@ -18,5 +18,5 @@ end

function __init__()
pushfirst!(PyVector(pyimport("sys")["path"]), @__DIR__)
init_repl_if_not()
afterreplinit(init_repl)
end
42 changes: 32 additions & 10 deletions src/julia_repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -5,21 +5,43 @@ else
using Base: REPL, LineEdit
end

# Register keybind '.' in Julia REPL:
"""
afterreplinit(f)
function init_repl_if_not(; _init_repl=init_repl)
active_repl = try
Base.active_repl
catch err
err isa UndefVarError || rethrow()
return
Like `atreplinit` but triggers `f` even after REPL is initialized when
it is called.
"""
function afterreplinit(f)
# See: https://github.com/JuliaLang/Pkg.jl/blob/v1.0.2/src/Pkg.jl#L338
function wrapper(repl)
if isinteractive() && repl isa REPL.LineEditREPL
f(repl)
end
end
if isdefined(Base, :active_repl)
wrapper(Base.active_repl)
else
atreplinit() do repl
@async begin
wait_repl_interface(repl)
wrapper(repl)
end
end
end
end

if isinteractive() && typeof(active_repl) != REPL.BasicREPL
_init_repl(active_repl)
function wait_repl_interface(repl)
for _ in 1:20
try
repl.interface.modes[1].keymap_dict
return
catch
end
sleep(0.05)
end
end
# See: https://github.com/JuliaInterop/RCall.jl/blob/master/src/setup.jl

# Register keybind '.' in Julia REPL:

function init_repl(repl)
start = function(s, _...)
Expand Down
6 changes: 3 additions & 3 deletions test/test_julia_repl.jl
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
module TestJuliaREPL

include("preamble.jl")
using IPython: init_repl, init_repl_if_not, REPL
using IPython: init_repl, afterreplinit, REPL

@static if VERSION >= v"0.7.0-"
using REPL: TextTerminal
Expand All @@ -10,8 +10,8 @@ else
end


@testset "init_repl_if_not" begin
repl = init_repl_if_not(; _init_repl=identity)
@testset "afterreplinit" begin
repl = afterreplinit(identity)
if isinteractive()
@test repl === Base.active_repl
else
Expand Down

0 comments on commit cfbe21b

Please sign in to comment.