Skip to content

Commit cfbe21b

Browse files
committed
More robust initialization; julia -e "using IPython" -i works now
1 parent a0cec45 commit cfbe21b

File tree

3 files changed

+36
-14
lines changed

3 files changed

+36
-14
lines changed

src/core.jl

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,5 +18,5 @@ end
1818

1919
function __init__()
2020
pushfirst!(PyVector(pyimport("sys")["path"]), @__DIR__)
21-
init_repl_if_not()
21+
afterreplinit(init_repl)
2222
end

src/julia_repl.jl

Lines changed: 32 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,43 @@ else
55
using Base: REPL, LineEdit
66
end
77

8-
# Register keybind '.' in Julia REPL:
8+
"""
9+
afterreplinit(f)
910
10-
function init_repl_if_not(; _init_repl=init_repl)
11-
active_repl = try
12-
Base.active_repl
13-
catch err
14-
err isa UndefVarError || rethrow()
15-
return
11+
Like `atreplinit` but triggers `f` even after REPL is initialized when
12+
it is called.
13+
"""
14+
function afterreplinit(f)
15+
# See: https://github.com/JuliaLang/Pkg.jl/blob/v1.0.2/src/Pkg.jl#L338
16+
function wrapper(repl)
17+
if isinteractive() && repl isa REPL.LineEditREPL
18+
f(repl)
19+
end
20+
end
21+
if isdefined(Base, :active_repl)
22+
wrapper(Base.active_repl)
23+
else
24+
atreplinit() do repl
25+
@async begin
26+
wait_repl_interface(repl)
27+
wrapper(repl)
28+
end
29+
end
1630
end
31+
end
1732

18-
if isinteractive() && typeof(active_repl) != REPL.BasicREPL
19-
_init_repl(active_repl)
33+
function wait_repl_interface(repl)
34+
for _ in 1:20
35+
try
36+
repl.interface.modes[1].keymap_dict
37+
return
38+
catch
39+
end
40+
sleep(0.05)
2041
end
2142
end
22-
# See: https://github.com/JuliaInterop/RCall.jl/blob/master/src/setup.jl
43+
44+
# Register keybind '.' in Julia REPL:
2345

2446
function init_repl(repl)
2547
start = function(s, _...)

test/test_julia_repl.jl

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
module TestJuliaREPL
22

33
include("preamble.jl")
4-
using IPython: init_repl, init_repl_if_not, REPL
4+
using IPython: init_repl, afterreplinit, REPL
55

66
@static if VERSION >= v"0.7.0-"
77
using REPL: TextTerminal
@@ -10,8 +10,8 @@ else
1010
end
1111

1212

13-
@testset "init_repl_if_not" begin
14-
repl = init_repl_if_not(; _init_repl=identity)
13+
@testset "afterreplinit" begin
14+
repl = afterreplinit(identity)
1515
if isinteractive()
1616
@test repl === Base.active_repl
1717
else

0 commit comments

Comments
 (0)