diff options
author | aycabta <aycabta@gmail.com> | 2019-06-04 06:39:02 +0900 |
---|---|---|
committer | aycabta <aycabta@gmail.com> | 2019-06-04 07:23:36 +0900 |
commit | 4b7213a85a6700657b825f8f127ce83a3070bf1d (patch) | |
tree | a67ac0525591eefb8a467ee2cf5a731ddcfa2dc2 /lib/reline/unicode.rb | |
parent | c9b74f9fd95113df903fc34cc1d6ec3fb3160c85 (diff) | |
download | ruby-4b7213a85a6700657b825f8f127ce83a3070bf1d.tar.gz |
Implement transpose-words
Diffstat (limited to 'lib/reline/unicode.rb')
-rw-r--r-- | lib/reline/unicode.rb | 101 |
1 files changed, 101 insertions, 0 deletions
diff --git a/lib/reline/unicode.rb b/lib/reline/unicode.rb index 5523d4fa31..4b30f044f3 100644 --- a/lib/reline/unicode.rb +++ b/lib/reline/unicode.rb @@ -188,6 +188,107 @@ class Reline::Unicode [byte_size, width] end + def self.ed_transpose_words(line, byte_pointer) + right_word_start = nil + size = get_next_mbchar_size(line, byte_pointer) + mbchar = line.byteslice(byte_pointer, size) + if size.zero? + # ' aaa bbb [cursor]' + byte_size = 0 + while 0 < (byte_pointer + byte_size) + size = get_prev_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size - size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ + byte_size -= size + end + while 0 < (byte_pointer + byte_size) + size = get_prev_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size - size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ + byte_size -= size + end + right_word_start = byte_pointer + byte_size + byte_size = 0 + while line.bytesize > (byte_pointer + byte_size) + size = get_next_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ + byte_size += size + end + after_start = byte_pointer + byte_size + elsif mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ + # ' aaa bb[cursor]b' + byte_size = 0 + while 0 < (byte_pointer + byte_size) + size = get_prev_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size - size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ + byte_size -= size + end + right_word_start = byte_pointer + byte_size + byte_size = 0 + while line.bytesize > (byte_pointer + byte_size) + size = get_next_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ + byte_size += size + end + after_start = byte_pointer + byte_size + else + byte_size = 0 + while (line.bytesize - 1) > (byte_pointer + byte_size) + size = get_next_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ + byte_size += size + end + if (byte_pointer + byte_size) == (line.bytesize - 1) + # ' aaa bbb [cursor] ' + after_start = line.bytesize + while 0 < (byte_pointer + byte_size) + size = get_prev_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size - size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ + byte_size -= size + end + while 0 < (byte_pointer + byte_size) + size = get_prev_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size - size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ + byte_size -= size + end + right_word_start = byte_pointer + byte_size + else + # ' aaa [cursor] bbb ' + right_word_start = byte_pointer + byte_size + while line.bytesize > (byte_pointer + byte_size) + size = get_next_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ + byte_size += size + end + after_start = byte_pointer + byte_size + end + end + byte_size = right_word_start - byte_pointer + while 0 < (byte_pointer + byte_size) + size = get_prev_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size - size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\p{Word}/ + byte_size -= size + end + middle_start = byte_pointer + byte_size + byte_size = middle_start - byte_pointer + while 0 < (byte_pointer + byte_size) + size = get_prev_mbchar_size(line, byte_pointer + byte_size) + mbchar = line.byteslice(byte_pointer + byte_size - size, size) + break if mbchar.encode(Encoding::UTF_8) =~ /\P{Word}/ + byte_size -= size + end + left_word_start = byte_pointer + byte_size + [left_word_start, middle_start, right_word_start, after_start] + end + def self.vi_big_forward_word(line, byte_pointer) width = 0 byte_size = 0 |