Skip to content

Commit 8f331ed

Browse files
authored
Change quoted_insert and bracketed_paste to a single key input (#792)
1 parent cdd7288 commit 8f331ed

File tree

7 files changed

+74
-46
lines changed

7 files changed

+74
-46
lines changed

lib/reline.rb

Lines changed: 7 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -343,13 +343,14 @@ def readline(prompt = '', add_hist = false)
343343
read_io(config.keyseq_timeout) { |inputs|
344344
line_editor.set_pasting_state(io_gate.in_pasting?)
345345
inputs.each do |key|
346-
if key.method_symbol == :bracketed_paste_start
347-
text = io_gate.read_bracketed_paste
348-
line_editor.insert_multiline_text(text)
349-
line_editor.scroll_into_view
350-
else
351-
line_editor.update(key)
346+
case key.method_symbol
347+
when :bracketed_paste_start
348+
# io_gate is Reline::ANSI because the key :bracketed_paste_start is only assigned in Reline::ANSI
349+
key = Reline::Key.new(io_gate.read_bracketed_paste, :insert_multiline_text)
350+
when :quoted_insert, :ed_quoted_insert
351+
key = Reline::Key.new(io_gate.read_single_char(config.keyseq_timeout), :insert_raw_char)
352352
end
353+
line_editor.update(key)
353354
end
354355
}
355356
if line_editor.finished?

lib/reline/io.rb

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,20 @@ def win?
3535
def reset_color_sequence
3636
self.class::RESET_COLOR
3737
end
38+
39+
# Read a single encoding valid character from the input.
40+
def read_single_char(keyseq_timeout)
41+
buffer = String.new(encoding: Encoding::ASCII_8BIT)
42+
loop do
43+
timeout = buffer.empty? ? Float::INFINITY : keyseq_timeout
44+
c = getc(timeout)
45+
return unless c
46+
47+
buffer << c
48+
encoded = buffer.dup.force_encoding(encoding)
49+
return encoded if encoded.valid_encoding?
50+
end
51+
end
3852
end
3953
end
4054

lib/reline/key_actor/vi_insert.rb

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -97,25 +97,25 @@ module Reline::KeyActor
9797
# 47 /
9898
:ed_insert,
9999
# 48 0
100-
:ed_insert,
100+
:ed_digit,
101101
# 49 1
102-
:ed_insert,
102+
:ed_digit,
103103
# 50 2
104-
:ed_insert,
104+
:ed_digit,
105105
# 51 3
106-
:ed_insert,
106+
:ed_digit,
107107
# 52 4
108-
:ed_insert,
108+
:ed_digit,
109109
# 53 5
110-
:ed_insert,
110+
:ed_digit,
111111
# 54 6
112-
:ed_insert,
112+
:ed_digit,
113113
# 55 7
114-
:ed_insert,
114+
:ed_digit,
115115
# 56 8
116-
:ed_insert,
116+
:ed_digit,
117117
# 57 9
118-
:ed_insert,
118+
:ed_digit,
119119
# 58 :
120120
:ed_insert,
121121
# 59 ;

lib/reline/line_editor.rb

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,7 @@ def wrap_method_call(method_symbol, method_obj, key, with_operator = false)
973973
@drop_terminate_spaces = false
974974
end
975975

976+
ARGUMENT_DIGIT_METHODS = %i[ed_digit vi_zero ed_argument_digit]
976977
VI_WAITING_ACCEPT_METHODS = %i[vi_change_meta vi_delete_meta vi_yank ed_insert ed_argument_digit]
977978

978979
private def process_key(key, method_symbol)
@@ -1004,7 +1005,7 @@ def wrap_method_call(method_symbol, method_obj, key, with_operator = false)
10041005
method_obj = method(method_symbol)
10051006
end
10061007
if @vi_arg
1007-
if key.match?(/\A\d\z/)
1008+
if ARGUMENT_DIGIT_METHODS.include?(method_symbol)
10081009
ed_argument_digit(key)
10091010
else
10101011
if argumentable?(method_obj)
@@ -1015,9 +1016,7 @@ def wrap_method_call(method_symbol, method_obj, key, with_operator = false)
10151016
wrap_method_call(method_symbol, method_obj, key)
10161017
end
10171018
@kill_ring.process
1018-
if @vi_arg
1019-
@vi_arg = nil
1020-
end
1019+
@vi_arg = nil
10211020
end
10221021
elsif method_obj
10231022
if method_symbol == :ed_argument_digit
@@ -1227,15 +1226,13 @@ def confirm_multiline_termination
12271226
end
12281227

12291228
def insert_multiline_text(text)
1230-
save_old_buffer
12311229
pre = @buffer_of_lines[@line_index].byteslice(0, @byte_pointer)
12321230
post = @buffer_of_lines[@line_index].byteslice(@byte_pointer..)
12331231
lines = (pre + Reline::Unicode.safe_encode(text, encoding).gsub(/\r\n?/, "\n") + post).split("\n", -1)
12341232
lines << '' if lines.empty?
12351233
@buffer_of_lines[@line_index, 1] = lines
12361234
@line_index += lines.size - 1
12371235
@byte_pointer = @buffer_of_lines[@line_index].bytesize - post.bytesize
1238-
push_input_lines
12391236
end
12401237

12411238
def insert_text(text)
@@ -1419,20 +1416,16 @@ def finish
14191416
alias_method :ed_digit, :ed_insert
14201417
alias_method :self_insert, :ed_insert
14211418

1422-
private def ed_quoted_insert(str, arg: 1)
1423-
@waiting_proc = proc { |key|
1424-
arg.times do
1425-
if key == "\C-j" or key == "\C-m"
1426-
key_newline(key)
1427-
elsif key != "\0"
1428-
# Ignore NUL.
1429-
ed_insert(key)
1430-
end
1419+
private def insert_raw_char(str, arg: 1)
1420+
arg.times do
1421+
if str == "\C-j" or str == "\C-m"
1422+
key_newline(str)
1423+
elsif str != "\0"
1424+
# Ignore NUL.
1425+
ed_insert(str)
14311426
end
1432-
@waiting_proc = nil
1433-
}
1427+
end
14341428
end
1435-
alias_method :quoted_insert, :ed_quoted_insert
14361429

14371430
private def ed_next_char(key, arg: 1)
14381431
byte_size = Reline::Unicode.get_next_mbchar_size(current_line, @byte_pointer)

test/reline/helper.rb

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -108,9 +108,9 @@ class Reline::TestCase < Test::Unit::TestCase
108108
input
109109
end
110110

111-
def input_key_by_symbol(method_symbol, csi: false)
112-
dummy_char = csi ? "\e[A" : "\C-a"
113-
@line_editor.input_key(Reline::Key.new(dummy_char, method_symbol, false))
111+
def input_key_by_symbol(method_symbol, char: nil, csi: false)
112+
char ||= csi ? "\e[A" : "\C-a"
113+
@line_editor.input_key(Reline::Key.new(char, method_symbol, false))
114114
end
115115

116116
def input_keys(input, convert = true)

test/reline/test_key_actor_emacs.rb

Lines changed: 21 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -138,11 +138,25 @@ def test_em_delete_prev_char_for_mbchar_by_plural_code_points
138138
assert_line_around_cursor("か\u3099", '')
139139
end
140140

141+
def test_bracketed_paste_insert
142+
set_line_around_cursor('A', 'Z')
143+
input_key_by_symbol(:insert_multiline_text, char: "abc\n\C-abc")
144+
assert_whole_lines(['Aabc', "\C-abcZ"])
145+
assert_line_around_cursor("\C-abc", 'Z')
146+
end
147+
141148
def test_ed_quoted_insert
142-
input_keys("ab\C-v\C-acd")
143-
assert_line_around_cursor("ab\C-acd", '')
144-
input_keys("\C-q\C-b")
145-
assert_line_around_cursor("ab\C-acd\C-b", '')
149+
set_line_around_cursor('A', 'Z')
150+
input_key_by_symbol(:insert_raw_char, char: "\C-a")
151+
assert_line_around_cursor("A\C-a", 'Z')
152+
end
153+
154+
def test_ed_quoted_insert_with_vi_arg
155+
input_keys("a\C-[3")
156+
input_key_by_symbol(:insert_raw_char, char: "\C-a")
157+
input_keys("b\C-[4")
158+
input_key_by_symbol(:insert_raw_char, char: '1')
159+
assert_line_around_cursor("a\C-a\C-a\C-ab1111", '')
146160
end
147161

148162
def test_ed_kill_line
@@ -1474,7 +1488,9 @@ def test_ed_search_prev_next_history_in_multibyte
14741488
end
14751489

14761490
def test_ignore_NUL_by_ed_quoted_insert
1477-
input_keys(%Q{"\C-v\C-@"}, false)
1491+
input_keys('"')
1492+
input_key_by_symbol(:insert_raw_char, char: 0.chr)
1493+
input_keys('"')
14781494
assert_line_around_cursor('""', '')
14791495
end
14801496

test/reline/test_key_actor_vi.rb

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,13 +344,17 @@ def test_vi_end_big_word
344344
end
345345

346346
def test_ed_quoted_insert
347-
input_keys("ab\C-v\C-acd")
348-
assert_line_around_cursor("ab\C-acd", '')
347+
input_keys('ab')
348+
input_key_by_symbol(:insert_raw_char, char: "\C-a")
349+
assert_line_around_cursor("ab\C-a", '')
349350
end
350351

351352
def test_ed_quoted_insert_with_vi_arg
352-
input_keys("ab\C-[3\C-v\C-aacd")
353-
assert_line_around_cursor("a\C-a\C-a\C-abcd", '')
353+
input_keys("ab\C-[3")
354+
input_key_by_symbol(:insert_raw_char, char: "\C-a")
355+
input_keys('4')
356+
input_key_by_symbol(:insert_raw_char, char: '1')
357+
assert_line_around_cursor("a\C-a\C-a\C-a1111", 'b')
354358
end
355359

356360
def test_vi_replace_char

0 commit comments

Comments
 (0)