Fix unnecessary undo in backward history search after returning to the present

Currently, if we return to the present, fish thinks that it still has a transient edit. This results in an unnecessary undo when performing a backward search. For example:
1. Type ': '.
2. Do a backward token search.
3. Do a forward token search.
4. Do another backward token search - this will result in the undo of ': '.
This commit is contained in:
kerty
2025-02-02 23:49:28 +03:00
committed by Johannes Altmanninger
parent c1c23269d2
commit 5e4f801ad5
2 changed files with 24 additions and 27 deletions

View File

@@ -1894,30 +1894,28 @@ fn replace_current_token(&mut self, new_token: WString) {
/// Apply the history search to the command line.
fn update_command_line_from_history_search(&mut self) {
let new_text = if self.history_search.is_at_present() {
self.history_search.search_string()
} else {
self.history_search.current_result()
}
.to_owned();
assert!(self.history_search.active());
if self.command_line_has_transient_edit {
self.undo(EditableLineTag::Commandline);
self.command_line_has_transient_edit = false;
}
if self.history_search.by_token() {
self.replace_current_token(new_text);
} else {
assert!(self.history_search.by_line() || self.history_search.by_prefix());
self.replace_substring(
EditableLineTag::Commandline,
0..self.command_line.len(),
new_text,
);
if self.history_search.by_prefix() {
self.command_line
.set_position(self.history_search.search_string().len());
if !self.history_search.is_at_present() {
let new_text = self.history_search.current_result().to_owned();
if self.history_search.by_token() {
self.replace_current_token(new_text);
} else {
self.replace_substring(
EditableLineTag::Commandline,
0..self.command_line.len(),
new_text,
);
if self.history_search.by_prefix() {
self.command_line
.set_position(self.history_search.search_string().len());
}
}
self.command_line_has_transient_edit = true;
}
self.command_line_has_transient_edit = true;
self.update_buff_pos(EditableLineTag::Commandline, None);
}
@@ -3034,12 +3032,10 @@ fn handle_readline_command(&mut self, c: ReadlineCmd) {
})
}
if !found && !was_active_before {
self.history_search.reset();
} else if found
|| (dir == SearchDirection::Forward && self.history_search.is_at_present())
{
if found {
self.update_command_line_from_history_search();
} else if !was_active_before {
self.history_search.reset();
}
}
rl::HistoryPager => {

View File

@@ -4,11 +4,12 @@
isolated-tmux-start
isolated-tmux send-keys ': 1' Enter
isolated-tmux send-keys ': ' M-Up M-Down M-Up M-Up M-Up Enter
isolated-tmux send-keys C-l 'echo still alive' Enter
isolated-tmux send-keys ': ' M-Up M-Down M-Up M-Up M-Up M-Down Enter
isolated-tmux send-keys 'echo still alive' Enter
tmux-sleep
# isolated-tmux capture-pane -p | tail -2
isolated-tmux capture-pane -p
# CHECK: prompt 0> : 1
# CHECK: prompt 1> : 1
# CHECK: prompt 2> echo still alive
# CHECK: still alive
# CHECK: prompt 3>