From 9ab6acac48a2b6cdc7b4c4c5b9a473aed97a761f Mon Sep 17 00:00:00 2001 From: Rafael Fourquet Date: Fri, 25 Aug 2017 08:56:34 +0200 Subject: [PATCH] REPL: implement Alt-{u,c,l} to change the case of the next word (#23379) Fixes part of #8447. --- base/repl/LineEdit.jl | 22 ++++++++++++++++++++++ doc/src/manual/interacting-with-julia.md | 3 +++ test/lineedit.jl | 12 ++++++++++++ 3 files changed, 37 insertions(+) diff --git a/base/repl/LineEdit.jl b/base/repl/LineEdit.jl index 89e3bbb367b53..f29956839b0da 100644 --- a/base/repl/LineEdit.jl +++ b/base/repl/LineEdit.jl @@ -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) @@ -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( diff --git a/doc/src/manual/interacting-with-julia.md b/doc/src/manual/interacting-with-julia.md index c8ba66d6236f5..03653d2cfeaf8 100644 --- a/doc/src/manual/interacting-with-julia.md +++ b/doc/src/manual/interacting-with-julia.md @@ -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 | diff --git a/test/lineedit.jl b/test/lineedit.jl index 71aed1ffe6421..8bbf881d7adb4 100644 --- a/test/lineedit.jl +++ b/test/lineedit.jl @@ -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