Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

In ed_search_[prev|next]_history, make the cursor come to the end of the line when there is no search substr #714

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions lib/reline/line_editor.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1770,7 +1770,7 @@ def finish
history_range = 0...(@history_pointer || Reline::HISTORY.size)
h_pointer, line_index = search_history(substr, history_range.reverse_each)
return unless h_pointer
move_history(h_pointer, line: line_index || :start, cursor: @byte_pointer)
move_history(h_pointer, line: line_index || :start, cursor: substr.empty? ? :end : @byte_pointer)
arg -= 1
ed_search_prev_history(key, arg: arg) if arg > 0
end
Expand All @@ -1784,7 +1784,7 @@ def finish
h_pointer, line_index = search_history(substr, history_range)
return if h_pointer.nil? and not substr.empty?

move_history(h_pointer, line: line_index || :start, cursor: @byte_pointer)
move_history(h_pointer, line: line_index || :start, cursor: substr.empty? ? :end : @byte_pointer)
arg -= 1
ed_search_next_history(key, arg: arg) if arg > 0
end
Expand Down
21 changes: 12 additions & 9 deletions test/reline/test_key_actor_emacs.rb
Original file line number Diff line number Diff line change
Expand Up @@ -1243,13 +1243,14 @@ def test_ed_search_prev_history_with_empty
])
# The ed_search_prev_history doesn't have default binding
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_line_around_cursor('', '12345')
assert_line_around_cursor('12345', '')
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_line_around_cursor('', '12aaa')
assert_line_around_cursor('12345', '')
Copy link
Member

@tompng tompng Jun 2, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This part should be ('12aaa', '')
#618 might be harder than I first thought.

I checked GNU Readline.

ruby -rreadline -rreline -e "raise if Readline==Reline; loop{p Readline.readline 'prompt> ', true}"
prompt> █
ed_search_prev_history, search with empty string
prompt> 12345█
(A) ed_search_prev_history, search with empty string
prompt> 12aaa█
prompt> █
ed_search_prev_history, search with empty string
prompt> 12345█
Move cursor left and right. Continuous search_xxx_history is reset.
prompt> 12345█
(B) ed_search_prev_history, search with "12345", search fails
prompt> 12345█

There is a difference between (A) and (B). Same input, same cursor position, but there is a hidden state. We need to introduce a new state (instance variable) to achieve this.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Adding new instance variable for many def ed_[key_action_name] will make maintainability poor, so I think we should introduce a general-purpose feature for passing state to next action.

One idea is:

def input_key
  ...
  # process_key() or normal_char() is called here
  ...
  @prev_action_state, @next_action_state = @next_action_state, nil
end

Key action method can read @prev_action_state.
If the action want to pass a state to next action, set @next_action_state to something like [state_type, state_value].

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for your clear comment. To replicate the behavior of Readline, it is best to include information about the before and after states of the current operation. I will give it a try

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

fixed in 7ddd20d

3.times { input_key_by_symbol(:ed_prev_char) }
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_line_around_cursor('', '12356')
assert_line_around_cursor('12', 'aaa')
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_line_around_cursor('', '12356')
assert_line_around_cursor('12', '356')
end

def test_ed_search_prev_history_without_match
Expand Down Expand Up @@ -1292,15 +1293,17 @@ def test_ed_search_next_history_with_empty
])
# The ed_search_prev_history and ed_search_next_history doesn't have default binding
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_line_around_cursor('', '12345')
assert_line_around_cursor('12345', '')
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_line_around_cursor('', '12aaa')
assert_line_around_cursor('12345', '')
3.times { input_key_by_symbol(:ed_prev_char) }
@line_editor.__send__(:ed_search_prev_history, "\C-p".ord)
assert_line_around_cursor('', '12356')
assert_line_around_cursor('12', 'aaa')
@line_editor.__send__(:ed_search_next_history, "\C-n".ord)
assert_line_around_cursor('', '12aaa')
assert_line_around_cursor('12', '345')
@line_editor.__send__(:ed_search_next_history, "\C-n".ord)
assert_line_around_cursor('', '12345')
assert_line_around_cursor('12', '345')
2.times { input_key_by_symbol(:ed_prev_char) }
@line_editor.__send__(:ed_search_next_history, "\C-n".ord)
assert_line_around_cursor('', '')
end
Expand Down
Loading