on master help sign in
Make editor remember the latest search register (#5244)
commit3a162e2bef0b8a5ae1045d8b018447c9ed9fde61 authorJonathan LEI <me@xjonathan.dev> dateTue 15 Aug 2023, 07:59 (3 years ago) parent085706e
2 files changed+23−5
helix-term/src/commands.rs+21−5 helix-view/src/register.rs+2
helix-term/src/commands.rs+21−5
@@ -1895,7 +1895,9 @@ fn searcher(cx: &mut Context, direction: Direction) {
18951895 .collect()
18961896 },
18971897 move |editor, regex, event| {
1898− if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
1898+ if event == PromptEvent::Validate {
1899+ editor.registers.last_search_register = reg;
1900+ } else if event != PromptEvent::Update {
18991901 return;
19001902 }
19011903 search_impl(
@@ -1914,7 +1916,9 @@ fn searcher(cx: &mut Context, direction: Direction) {
19141916
19151917fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Direction) {
19161918 let count = cx.count();
1917− let register = cx.register.unwrap_or('/');
1919+ let register = cx
1920+ .register
1921+ .unwrap_or(cx.editor.registers.last_search_register);
19181922 let config = cx.editor.config();
19191923 let scrolloff = config.scrolloff;
19201924 if let Some(query) = cx.editor.registers.first(register, cx.editor) {
@@ -1982,13 +1986,21 @@ fn search_selection(cx: &mut Context) {
19821986
19831987 let msg = format!("register '{}' set to '{}'", register, &regex);
19841988 match cx.editor.registers.push(register, regex) {
1985− Ok(_) => cx.editor.set_status(msg),
1989+ Ok(_) => {
1990+ cx.editor.registers.last_search_register = register;
1991+ cx.editor.set_status(msg)
1992+ }
19861993 Err(err) => cx.editor.set_error(err.to_string()),
19871994 }
19881995}
19891996
19901997fn make_search_word_bounded(cx: &mut Context) {
1991− let register = cx.register.unwrap_or('/');
1998+ // Defaults to the active search register instead `/` to be more ergonomic assuming most people
1999+ // would use this command following `search_selection`. This avoids selecting the register
2000+ // twice.
2001+ let register = cx
2002+ .register
2003+ .unwrap_or(cx.editor.registers.last_search_register);
19922004 let regex = match cx.editor.registers.first(register, cx.editor) {
19932005 Some(regex) => regex,
19942006 None => return,
@@ -2014,7 +2026,10 @@ fn make_search_word_bounded(cx: &mut Context) {
20142026
20152027 let msg = format!("register '{}' set to '{}'", register, &new_regex);
20162028 match cx.editor.registers.push(register, new_regex) {
2017− Ok(_) => cx.editor.set_status(msg),
2029+ Ok(_) => {
2030+ cx.editor.registers.last_search_register = register;
2031+ cx.editor.set_status(msg)
2032+ }
20182033 Err(err) => cx.editor.set_error(err.to_string()),
20192034 }
20202035}
@@ -2078,6 +2093,7 @@ fn global_search(cx: &mut Context) {
20782093 if event != PromptEvent::Validate {
20792094 return;
20802095 }
2096+ editor.registers.last_search_register = reg;
20812097
20822098 let documents: Vec<_> = editor
20832099 .documents()
helix-view/src/register.rs+2
@@ -29,6 +29,7 @@ pub struct Registers {
2929 /// efficiently prepend new values in `Registers::push`.
3030 inner: HashMap<char, Vec<String>>,
3131 clipboard_provider: Box<dyn ClipboardProvider>,
32+ pub last_search_register: char,
3233}
3334
3435impl Default for Registers {
@@ -36,6 +37,7 @@ impl Default for Registers {
3637 Self {
3738 inner: Default::default(),
3839 clipboard_provider: get_clipboard_provider(),
40+ last_search_register: '/',
3941 }
4042 }
4143}