Skip to content

Commit

Permalink
REPL: implement "insert last word from previous history entry"
Browse files Browse the repository at this point in the history
Bind this shell-like feature to the usual "meta-.".
  • Loading branch information
rfourquet committed Nov 4, 2019
1 parent ffdee15 commit d9a4873
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 0 deletions.
1 change: 1 addition & 0 deletions stdlib/REPL/docs/src/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -174,6 +174,7 @@ to do so), or pressing Esc and then the key.
| `^Q` | Write a number in REPL and press `^Q` to open editor at corresponding stackframe or method |
| `meta-Left Arrow` | indent the current line on the left |
| `meta-Right Arrow` | indent the current line on the right |
| `meta-.` | insert last word from previous history entry |


### Customizing keybindings
Expand Down
20 changes: 20 additions & 0 deletions stdlib/REPL/src/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -1942,6 +1942,25 @@ function move_line_end(buf::IOBuffer)
nothing
end

edit_insert_last_word(s::MIState) =
edit_insert(s, get_last_word(IOBuffer(mode(s).hist.history[end])))

function get_last_word(buf::IOBuffer)
move_line_end(buf)
char_move_word_left(buf)
posbeg = position(buf)
char_move_word_right(buf)
posend = position(buf)
buf = take!(buf)
word = String(buf[posbeg+1:posend])
rest = String(buf[posend+1:end])
lp, rp, lb, rb = count.(.==(('(', ')', '[', ']')), rest)
special = any(in.(('\'', '"', '`'), rest))
!special && lp == rp && lb == rb ?
word *= rest :
word
end

function commit_line(s)
cancel_beep(s)
move_input_end(s)
Expand Down Expand Up @@ -2080,6 +2099,7 @@ AnyDict(
"\eOc" => "\ef",
# Meta Enter
"\e\r" => (s,o...)->edit_insert_newline(s),
"\e." => (s,o...)->edit_insert_last_word(s),
"\e\n" => "\e\r",
"^_" => (s,o...)->edit_undo!(s),
"\e_" => (s,o...)->edit_redo!(s),
Expand Down
15 changes: 15 additions & 0 deletions stdlib/REPL/test/lineedit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -878,5 +878,20 @@ end
buf.mark = 0
seek(buf, 1)
@test transform!(transpose_lines_down_reg!, buf) == ("l2\nl3\nl1", 4, 3)
end

@testset "edit_insert_last_word" begin
get_last_word(str::String) = LineEdit.get_last_word(IOBuffer(str))
@test get_last_word("1+2") == "2"
@test get_last_word("1+23") == "23"
@test get_last_word("1+2") == "2"
@test get_last_word(""" "a" * "b" """) == "b"
@test get_last_word(""" "a" * 'b' """) == "b"
@test get_last_word(""" "a" * `b` """) == "b"
@test get_last_word("g()") == "g()"
@test get_last_word("g(1, 2)") == "2"
@test get_last_word("g(1, f())") == "f"
@test get_last_word("a[1]") == "1"
@test get_last_word("a[b[]]") == "b"
@test get_last_word("a[]") == "a[]"
end
5 changes: 5 additions & 0 deletions stdlib/REPL/test/repl.jl
Original file line number Diff line number Diff line change
Expand Up @@ -415,6 +415,11 @@ for prompt = ["TestΠ", () -> randstring(rand(1:10))]
s = LineEdit.init_state(repl.t, repl.interface)
LineEdit.edit_insert(s, "wip")

# LineEdit functions related to history
LineEdit.edit_insert_last_word(s)
@test buffercontents(LineEdit.buffer(s)) == "wip2"
LineEdit.edit_backspace(s) # remove the "2"

# Test that navigating history skips invalid modes
# (in both directions)
LineEdit.history_prev(s, hp)
Expand Down

0 comments on commit d9a4873

Please sign in to comment.