From 544355ed3d215861d77d47d0fee5813a7f33dbfe Mon Sep 17 00:00:00 2001 From: Jonathan LEI Date: Mon, 14 Aug 2023 15:10:43 +0000 Subject: [PATCH] Make editor remember the latest search register --- helix-term/src/commands.rs | 22 +++++++++++++++++----- helix-view/src/editor.rs | 2 ++ 2 files changed, 19 insertions(+), 5 deletions(-) diff --git a/helix-term/src/commands.rs b/helix-term/src/commands.rs index 61c647d0b8216..3d18edd8128c7 100644 --- a/helix-term/src/commands.rs +++ b/helix-term/src/commands.rs @@ -1895,7 +1895,9 @@ fn searcher(cx: &mut Context, direction: Direction) { .collect() }, move |editor, regex, event| { - if !matches!(event, PromptEvent::Update | PromptEvent::Validate) { + if event == PromptEvent::Validate { + editor.last_search_register = reg; + } else if event != PromptEvent::Update { return; } search_impl( @@ -1914,7 +1916,7 @@ fn searcher(cx: &mut Context, direction: Direction) { fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Direction) { let count = cx.count(); - let register = cx.register.unwrap_or('/'); + let register = cx.register.unwrap_or(cx.editor.last_search_register); let config = cx.editor.config(); let scrolloff = config.scrolloff; if let Some(query) = cx.editor.registers.first(register, cx.editor) { @@ -1982,13 +1984,19 @@ fn search_selection(cx: &mut Context) { let msg = format!("register '{}' set to '{}'", register, ®ex); match cx.editor.registers.push(register, regex) { - Ok(_) => cx.editor.set_status(msg), + Ok(_) => { + cx.editor.last_search_register = register; + cx.editor.set_status(msg) + } Err(err) => cx.editor.set_error(err.to_string()), } } fn make_search_word_bounded(cx: &mut Context) { - let register = cx.register.unwrap_or('/'); + // Defaults to the active search register instead `/` to be more ergonomic assuming most people + // would use this command following `search_selection`. This avoids selecting the register + // twice. + let register = cx.register.unwrap_or(cx.editor.last_search_register); let regex = match cx.editor.registers.first(register, cx.editor) { Some(regex) => regex, None => return, @@ -2014,7 +2022,10 @@ fn make_search_word_bounded(cx: &mut Context) { let msg = format!("register '{}' set to '{}'", register, &new_regex); match cx.editor.registers.push(register, new_regex) { - Ok(_) => cx.editor.set_status(msg), + Ok(_) => { + cx.editor.last_search_register = register; + cx.editor.set_status(msg) + } Err(err) => cx.editor.set_error(err.to_string()), } } @@ -2078,6 +2089,7 @@ fn global_search(cx: &mut Context) { if event != PromptEvent::Validate { return; } + editor.last_search_register = reg; let documents: Vec<_> = editor .documents() diff --git a/helix-view/src/editor.rs b/helix-view/src/editor.rs index 66542e898e230..57e3fe8e7b50e 100644 --- a/helix-view/src/editor.rs +++ b/helix-view/src/editor.rs @@ -886,6 +886,7 @@ pub struct Editor { pub count: Option, pub selected_register: Option, + pub last_search_register: char, pub registers: Registers, pub macro_recording: Option<(char, Vec)>, pub macro_replaying: Vec, @@ -1036,6 +1037,7 @@ impl Editor { write_count: 0, count: None, selected_register: None, + last_search_register: '/', macro_recording: None, macro_replaying: Vec::new(), theme: theme_loader.default(),