Skip to content

Commit

Permalink
Run OhMyREPL keybindings in a fixed world age
Browse files Browse the repository at this point in the history
Hopefully this should avoid most invalidations caused by loading other
packages.

Also remove the NEW_KEYBINDINGS global as it's hard to follow where this
is added to.
  • Loading branch information
c42f committed May 18, 2023
1 parent 0d60955 commit 2647d6a
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 35 deletions.
1 change: 0 additions & 1 deletion src/BracketInserter.jl
Original file line number Diff line number Diff line change
Expand Up @@ -161,5 +161,4 @@ function insert_into_keymap!(D::Dict)
end
end

insert_into_keymap!(OhMyREPL.Prompt.NEW_KEYBINDINGS)
end # module
37 changes: 17 additions & 20 deletions src/OhMyREPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -90,43 +90,40 @@ const ENABLE_FZF = Ref(true)
enable_fzf(v::Bool) = ENABLE_FZF[] = v

using Pkg
function reinsert_after_pkg()
repl = Base.active_repl
function reinsert_after_pkg(repl, world_age)
mirepl = isdefined(repl,:mi) ? repl.mi : repl
main_mode = mirepl.interface.modes[1]
m = first(methods(main_mode.keymap_dict[']']))
if m.module == Pkg.REPLMode
Prompt.insert_keybindings()
Prompt.insert_keybindings(repl, world_age)
end
end

function setup_repl(repl, world_age)
if !isdefined(repl, :interface)
repl.interface = REPL.setup_interface(repl)
end
Prompt.insert_keybindings(repl, world_age)
@async begin
sleep(0.25)
reinsert_after_pkg(repl, world_age)
end
update_interface(repl.interface)
end

function __init__()
options = Base.JLOptions()
world_age = Base.get_world_counter()
# command-line
if (options.isinteractive != 1) && options.commands != C_NULL
return
end

if isdefined(Base, :active_repl)
if !isdefined(Base.active_repl, :interface)
Base.active_repl.interface = REPL.setup_interface(Base.active_repl)
end
Prompt.insert_keybindings()
@async begin
sleep(0.25)
reinsert_after_pkg()
end
setup_repl(Base.active_repl, world_age)
else
atreplinit() do repl
if !isdefined(repl, :interface)
repl.interface = REPL.setup_interface(repl)
end
Prompt.insert_keybindings()
@async begin
sleep(0.25)
reinsert_after_pkg()
end
update_interface(repl.interface)
setup_repl(Base.active_repl, world_age)
end
end

Expand Down
39 changes: 25 additions & 14 deletions src/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,14 @@ function rewrite_with_ANSI(s, cursormove::Bool = false)
flush(terminal(s))
end

# Wrap the function `f` so that it's always invoked in the given `world_age`
function fix_world_age(f, world_age)
function (args...; kws...)
Base.invoke_in_world(world_age, f, args...; kws...)
end
end

function create_keybindings()
function create_keybindings(prefix_hist_prompt, world_age)
D = Dict{Any, Any}()
D['\b'] = (s, data, c) -> if LineEdit.edit_backspace(s, true)
rewrite_with_ANSI(s)
Expand Down Expand Up @@ -268,26 +274,31 @@ function create_keybindings()
LineEdit.enter_search(s, p, true)
end
end
return D
end
NEW_KEYBINDINGS = create_keybindings()

function insert_keybindings(repl = Base.active_repl)
mirepl = isdefined(repl,:mi) ? repl.mi : repl
main_mode = mirepl.interface.modes[1]
p = mirepl.interface.modes[5]

NEW_KEYBINDINGS["\e[A"] = (s,o...)-> begin
LineEdit.edit_move_up(buffer(s)) || LineEdit.enter_prefix_search(s, p, true)
# Up Arrow
D["\e[A"] = (s,o...)-> begin
LineEdit.edit_move_up(buffer(s)) || LineEdit.enter_prefix_search(s, prefix_hist_prompt, true)
Prompt.rewrite_with_ANSI(s)
end
# Down Arrow
NEW_KEYBINDINGS["\e[B"] = (s,o...)-> begin
LineEdit.edit_move_down(buffer(s)) || LineEdit.enter_prefix_search(s, p, false)
D["\e[B"] = (s,o...)-> begin
LineEdit.edit_move_down(buffer(s)) || LineEdit.enter_prefix_search(s, prefix_hist_prompt, false)
Prompt.rewrite_with_ANSI(s)
end

main_mode.keymap_dict = LineEdit.keymap(Dict{Any, Any}[NEW_KEYBINDINGS, main_mode.keymap_dict])
OhMyREPL.BracketInserter.insert_into_keymap!(D)

return Dict(k=>fix_world_age(f, world_age) for (k,f) in D)
end

function insert_keybindings(repl, world_age)
mirepl = isdefined(repl,:mi) ? repl.mi : repl
main_mode = mirepl.interface.modes[1]
p = mirepl.interface.modes[5]

keybinds = create_keybindings(p, world_age)

main_mode.keymap_dict = LineEdit.keymap(Dict{Any, Any}[keybinds, main_mode.keymap_dict])
end

function _commit_line(s, data, c)
Expand Down

0 comments on commit 2647d6a

Please sign in to comment.