Skip to content

Commit

Permalink
REPL: jump to first/last history entries
Browse files Browse the repository at this point in the history
  • Loading branch information
rfourquet committed Jul 16, 2017
1 parent a2f4a05 commit 4f317ce
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 9 deletions.
6 changes: 5 additions & 1 deletion base/repl/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -588,6 +588,8 @@ end

history_prev(::EmptyHistoryProvider) = ("", false)
history_next(::EmptyHistoryProvider) = ("", false)
history_first(::EmptyHistoryProvider) = ("", false)
history_last(::EmptyHistoryProvider) = ("", false)
history_search(::EmptyHistoryProvider, args...) = false
add_history(::EmptyHistoryProvider, s) = nothing
add_history(s::PromptState) = add_history(mode(s).hist, s)
Expand Down Expand Up @@ -1443,7 +1445,9 @@ const history_keymap = AnyDict(
# Page Up
"\e[5~" => (s,o...)->(history_prev(s, mode(s).hist)),
# Page Down
"\e[6~" => (s,o...)->(history_next(s, mode(s).hist))
"\e[6~" => (s,o...)->(history_next(s, mode(s).hist)),
"\e<" => (s,o...)->(history_first(s, mode(s).hist)),
"\e>" => (s,o...)->(history_last(s, mode(s).hist)),
)

const prefix_history_keymap = merge!(
Expand Down
28 changes: 20 additions & 8 deletions base/repl/REPL.jl
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ import ..LineEdit:
history_next_prefix,
history_prev,
history_prev_prefix,
history_first,
history_last,
history_search,
accept_result,
terminal
Expand Down Expand Up @@ -463,44 +465,54 @@ function LineEdit.accept_result(s, p::LineEdit.HistoryPrompt{REPLHistoryProvider
end

function history_prev(s::LineEdit.MIState, hist::REPLHistoryProvider,
save_idx::Int = hist.cur_idx)
num::Int=1, save_idx::Int = hist.cur_idx)
num <= 0 && history_next(s, hist, -num, save_idx)
hist.last_idx = -1
m = history_move(s, hist, hist.cur_idx-1, save_idx)
m = history_move(s, hist, hist.cur_idx-num, save_idx)
if m === :ok
LineEdit.move_input_start(s)
LineEdit.reset_key_repeats(s) do
LineEdit.move_line_end(s)
end
LineEdit.refresh_line(s)
elseif m === :skip
hist.cur_idx -= 1
history_prev(s, hist, save_idx)
history_prev(s, hist, num+1, save_idx)
else
Terminals.beep(LineEdit.terminal(s))
end
end

function history_next(s::LineEdit.MIState, hist::REPLHistoryProvider,
save_idx::Int = hist.cur_idx)
num::Int=1, save_idx::Int = hist.cur_idx)
if num == 0
Terminals.beep(LineEdit.terminal(s))
return
end
num < 0 && history_prev(s, hist, -num, save_idx)
cur_idx = hist.cur_idx
max_idx = length(hist.history) + 1
if cur_idx == max_idx && 0 < hist.last_idx
# issue #6312
cur_idx = hist.last_idx
hist.last_idx = -1
end
m = history_move(s, hist, cur_idx+1, save_idx)
m = history_move(s, hist, cur_idx+num, save_idx)
if m === :ok
LineEdit.move_input_end(s)
LineEdit.refresh_line(s)
elseif m === :skip
hist.cur_idx += 1
history_next(s, hist, save_idx)
history_next(s, hist, num+1, save_idx)
else
Terminals.beep(LineEdit.terminal(s))
end
end

history_first(s::LineEdit.MIState, hist::REPLHistoryProvider) =
history_prev(s, hist, hist.cur_idx - 1)

history_last(s::LineEdit.MIState, hist::REPLHistoryProvider) =
history_next(s, hist, length(hist.history) - hist.cur_idx + 1)

function history_move_prefix(s::LineEdit.PrefixSearchState,
hist::REPLHistoryProvider,
prefix::AbstractString,
Expand Down
14 changes: 14 additions & 0 deletions test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -336,6 +336,20 @@ begin
@test LineEdit.mode(s) == repl_mode
@test buffercontents(LineEdit.buffer(s)) == "wip"
@test position(LineEdit.buffer(s)) == 3
LineEdit.history_next(s, hp)
@test buffercontents(LineEdit.buffer(s)) == "wip"
LineEdit.history_prev(s, hp, 2)
@test LineEdit.mode(s) == shell_mode
@test buffercontents(LineEdit.buffer(s)) == "ls"
LineEdit.history_first(s, hp)
@test LineEdit.mode(s) == repl_mode
@test buffercontents(LineEdit.buffer(s)) == "é"
LineEdit.history_next(s, hp, 6)
@test LineEdit.mode(s) == shell_mode
@test buffercontents(LineEdit.buffer(s)) == "ls"
LineEdit.history_last(s, hp)
@test buffercontents(LineEdit.buffer(s)) == "wip"
@test position(LineEdit.buffer(s)) == 3
LineEdit.move_line_start(s)
@test position(LineEdit.buffer(s)) == 0

Expand Down

0 comments on commit 4f317ce

Please sign in to comment.