From 8e97956bc1d2c90d908dee23384a757074e02e1d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Barri=C3=A9?= Date: Fri, 24 Mar 2023 13:35:24 +0100 Subject: [PATCH 1/3] Test existing behavior Typing Ctrl-D ends editing but typing does not. Also renamed a test that is not testing ed_delete_next_char but key_delete. --- test/reline/test_key_actor_emacs.rb | 16 +++++++++++++++- 1 file changed, 15 insertions(+), 1 deletion(-) diff --git a/test/reline/test_key_actor_emacs.rb b/test/reline/test_key_actor_emacs.rb index 40b26e5058..17dace7672 100644 --- a/test/reline/test_key_actor_emacs.rb +++ b/test/reline/test_key_actor_emacs.rb @@ -428,6 +428,12 @@ def test_em_delete_for_mbchar_by_plural_code_points assert_line("き\u3099") end + def test_em_delete_ends_editing + input_keys("\C-d") # quit from inputing + assert_line(nil) + assert(@line_editor.finished?) + end + def test_ed_clear_screen refute(@line_editor.instance_variable_get(:@cleared)) input_keys("\C-l", false) @@ -449,7 +455,7 @@ def test_ed_clear_screen_with_inputed assert_line('abc') end - def test_ed_delete_next_char + def test_key_delete input_keys('abc') assert_cursor(3) assert_cursor_max(3) @@ -459,6 +465,14 @@ def test_ed_delete_next_char assert_line('abc') end + def test_key_delete_does_not_end_editing + @line_editor.input_key(Reline::Key.new(:key_delete, :key_delete, false)) + assert_cursor(0) + assert_cursor_max(0) + assert_line('') + refute(@line_editor.finished?) + end + def test_em_next_word assert_byte_pointer_size('') assert_cursor(0) From 83588f4ab3052cd76a192841698fd382781a8b55 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Barri=C3=A9?= Date: Tue, 17 Jan 2023 15:44:52 +0100 Subject: [PATCH 2/3] Check if line empty first in em_delete By distributivity of AND over OR, we can factor out this condition. This will make the next commit simpler. --- lib/reline/line_editor.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index bd2f2c858a..12fec7383f 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -2651,7 +2651,7 @@ def finish alias_method :kill_whole_line, :em_kill_line private def em_delete(key) - if (not @is_multiline and @line.empty?) or (@is_multiline and @line.empty? and @buffer_of_lines.size == 1) + if @line.empty? and (not @is_multiline or @buffer_of_lines.size == 1) @line = nil if @buffer_of_lines.size > 1 scroll_down(@highest_in_all - @first_line_started_from) From c0d4086ceac8501d65159e0318ec38c97e959761 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Barri=C3=A9?= Date: Thu, 22 Dec 2022 23:48:44 +0100 Subject: [PATCH 3/3] Use em_delete in key_delete When the editing mode is emacs, use `em_delete` in `key_delete`. We need to add a condition though to `em_delete`, because it implements both `delete-char` and `end-of-file`. We only want the `end-of-file` behavior is the key is really Ctrl-D. This matches the behavior of the key with readline, i.e. deleting the next character if there is one, but not moving the cursor, while not finishing the editing if there are no characters. --- lib/reline/line_editor.rb | 6 ++++-- test/reline/test_key_actor_emacs.rb | 11 +++++++++++ 2 files changed, 15 insertions(+), 2 deletions(-) diff --git a/lib/reline/line_editor.rb b/lib/reline/line_editor.rb index 12fec7383f..166866aa6c 100644 --- a/lib/reline/line_editor.rb +++ b/lib/reline/line_editor.rb @@ -1944,8 +1944,10 @@ def finish end private def key_delete(key) - if @config.editing_mode_is?(:vi_insert, :emacs) + if @config.editing_mode_is?(:vi_insert) ed_delete_next_char(key) + elsif @config.editing_mode_is?(:emacs) + em_delete(key) end end @@ -2651,7 +2653,7 @@ def finish alias_method :kill_whole_line, :em_kill_line private def em_delete(key) - if @line.empty? and (not @is_multiline or @buffer_of_lines.size == 1) + if @line.empty? and (not @is_multiline or @buffer_of_lines.size == 1) and key == "\C-d".ord @line = nil if @buffer_of_lines.size > 1 scroll_down(@highest_in_all - @first_line_started_from) diff --git a/test/reline/test_key_actor_emacs.rb b/test/reline/test_key_actor_emacs.rb index 17dace7672..18a2448539 100644 --- a/test/reline/test_key_actor_emacs.rb +++ b/test/reline/test_key_actor_emacs.rb @@ -473,6 +473,17 @@ def test_key_delete_does_not_end_editing refute(@line_editor.finished?) end + def test_key_delete_preserves_cursor + input_keys('abc') + input_keys("\C-b", false) + assert_cursor(2) + assert_cursor_max(3) + @line_editor.input_key(Reline::Key.new(:key_delete, :key_delete, false)) + assert_cursor(2) + assert_cursor_max(2) + assert_line('ab') + end + def test_em_next_word assert_byte_pointer_size('') assert_cursor(0)