Skip to content

Commit

Permalink
Use em_delete in key_delete
Browse files Browse the repository at this point in the history
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 <Del> 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.
  • Loading branch information
etiennebarrie committed Mar 24, 2023
1 parent 83588f4 commit c0d4086
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 2 deletions.
6 changes: 4 additions & 2 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down Expand Up @@ -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)
Expand Down
11 changes: 11 additions & 0 deletions test/reline/test_key_actor_emacs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c0d4086

Please sign in to comment.