Skip to content

Commit

Permalink
REPL: implement Alt-{u,c,l} to change the case of the next word
Browse files Browse the repository at this point in the history
Fixes part of #8447.
  • Loading branch information
rfourquet committed Aug 24, 2017
1 parent 0f4baee commit ef66c23
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 0 deletions.
22 changes: 22 additions & 0 deletions base/repl/LineEdit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -636,6 +636,25 @@ function edit_transpose_words(buf::IOBuffer, mode=:emacs)
end


edit_upper_case(s) = edit_replace_word_right(s, uppercase)
edit_lower_case(s) = edit_replace_word_right(s, lowercase)
edit_title_case(s) = edit_replace_word_right(s, ucfirst)

edit_replace_word_right(s, replace::Function) =
edit_replace_word_right(buffer(s), replace) && refresh_line(s)

function edit_replace_word_right(buf::IOBuffer, replace::Function)
# put the cursor at the beginning of the next word
skipchars(buf, is_non_word_char)
b = position(buf)
char_move_word_right(buf)
e = position(buf)
e == b && return false
newstr = replace(String(buf.data[b+1:e]))
splice_buffer!(buf, b:e-1, newstr)
true
end

edit_clear(buf::IOBuffer) = truncate(buf, 0)

function edit_clear(s::MIState)
Expand Down Expand Up @@ -1531,6 +1550,9 @@ AnyDict(
end,
"^T" => (s,o...)->edit_transpose_chars(s),
"\et" => (s,o...)->edit_transpose_words(s),
"\eu" => (s,o...)->edit_upper_case(s),
"\el" => (s,o...)->edit_lower_case(s),
"\ec" => (s,o...)->edit_title_case(s),
)

const history_keymap = AnyDict(
Expand Down
3 changes: 3 additions & 0 deletions doc/src/manual/interacting-with-julia.md
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,9 @@ to do so).
| `^K` | "Kill" to end of line, placing the text in a buffer |
| `^Y` | "Yank" insert the text from the kill buffer |
| `^T` | Transpose the characters about the cursor |
| `meta-u` | Change the next word to uppercase |
| `meta-c` | Change the next word to titlecase |
| `meta-l` | Change the next word to lowercase |
| `^Q` | Write a number in REPL and press `^Q` to open editor at corresponding stackframe or method |


Expand Down
12 changes: 12 additions & 0 deletions test/lineedit.jl
Original file line number Diff line number Diff line change
Expand Up @@ -574,3 +574,15 @@ end
@test bufferdata(s) == "begin\n" * ' '^(i-6) * "\n x"
end
end

@testset "change case on the right" begin
buf = IOBuffer()
LineEdit.edit_insert(buf, "aa bb CC")
seekstart(buf)
LineEdit.edit_upper_case(buf)
LineEdit.edit_title_case(buf)
@test String(take!(copy(buf))) == "AA Bb CC"
@test position(buf) == 5
LineEdit.edit_lower_case(buf)
@test String(take!(copy(buf))) == "AA Bb cc"
end

0 comments on commit ef66c23

Please sign in to comment.