on master help sign in
Make editor remember the latest search register (#5244)
3a162e2Jonathan LEI
helix-term/src/commands.rs+21−5
@@ -1895,7 +1895,9 @@ fn searcher(cx: &mut Context, direction: Direction) {
1895 .collect()
1895 .collect()
1896 },
1896 },
1897 move |editor, regex, event| {
1897 move |editor, regex, event| {
1898+ if event == PromptEvent::Validate {
1899+ editor.registers.last_search_register = reg;
1898− if !matches!(event, PromptEvent::Update | PromptEvent::Validate) {
1900+ } else if event != PromptEvent::Update {
1899 return;
1901 return;
1900 }
1902 }
1901 search_impl(
1903 search_impl(
@@ -1914,7 +1916,9 @@ fn searcher(cx: &mut Context, direction: Direction) {
1914
1916
1915fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Direction) {
1917fn search_next_or_prev_impl(cx: &mut Context, movement: Movement, direction: Direction) {
1916 let count = cx.count();
1918 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);
1918 let config = cx.editor.config();
1922 let config = cx.editor.config();
1919 let scrolloff = config.scrolloff;
1923 let scrolloff = config.scrolloff;
1920 if let Some(query) = cx.editor.registers.first(register, cx.editor) {
1924 if let Some(query) = cx.editor.registers.first(register, cx.editor) {
@@ -1982,13 +1986,21 @@ fn search_selection(cx: &mut Context) {
1982
1986
1983 let msg = format!("register '{}' set to '{}'", register, &regex);
1987 let msg = format!("register '{}' set to '{}'", register, &regex);
1984 match cx.editor.registers.push(register, regex) {
1988 match cx.editor.registers.push(register, regex) {
1989+ Ok(_) => {
1990+ cx.editor.registers.last_search_register = register;
1985− Ok(_) => cx.editor.set_status(msg),
1991+ cx.editor.set_status(msg)
1992+ }
1986 Err(err) => cx.editor.set_error(err.to_string()),
1993 Err(err) => cx.editor.set_error(err.to_string()),
1987 }
1994 }
1988}
1995}
1989
1996
1990fn make_search_word_bounded(cx: &mut Context) {
1997fn make_search_word_bounded(cx: &mut Context) {
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.
1991− let register = cx.register.unwrap_or('/');
2001+ let register = cx
2002+ .register
2003+ .unwrap_or(cx.editor.registers.last_search_register);
1992 let regex = match cx.editor.registers.first(register, cx.editor) {
2004 let regex = match cx.editor.registers.first(register, cx.editor) {
1993 Some(regex) => regex,
2005 Some(regex) => regex,
1994 None => return,
2006 None => return,
@@ -2014,7 +2026,10 @@ fn make_search_word_bounded(cx: &mut Context) {
2014
2026
2015 let msg = format!("register '{}' set to '{}'", register, &new_regex);
2027 let msg = format!("register '{}' set to '{}'", register, &new_regex);
2016 match cx.editor.registers.push(register, new_regex) {
2028 match cx.editor.registers.push(register, new_regex) {
2029+ Ok(_) => {
2030+ cx.editor.registers.last_search_register = register;
2017− Ok(_) => cx.editor.set_status(msg),
2031+ cx.editor.set_status(msg)
2032+ }
2018 Err(err) => cx.editor.set_error(err.to_string()),
2033 Err(err) => cx.editor.set_error(err.to_string()),
2019 }
2034 }
2020}
2035}
@@ -2078,6 +2093,7 @@ fn global_search(cx: &mut Context) {
2078 if event != PromptEvent::Validate {
2093 if event != PromptEvent::Validate {
2079 return;
2094 return;
2080 }
2095 }
2096+ editor.registers.last_search_register = reg;
2081
2097
2082 let documents: Vec<_> = editor
2098 let documents: Vec<_> = editor
2083 .documents()
2099 .documents()
helix-view/src/register.rs+2
@@ -29,6 +29,7 @@ pub struct Registers {
29 /// efficiently prepend new values in `Registers::push`.
29 /// efficiently prepend new values in `Registers::push`.
30 inner: HashMap<char, Vec<String>>,
30 inner: HashMap<char, Vec<String>>,
31 clipboard_provider: Box<dyn ClipboardProvider>,
31 clipboard_provider: Box<dyn ClipboardProvider>,
32+ pub last_search_register: char,
32}
33}
33
34
34impl Default for Registers {
35impl Default for Registers {
@@ -36,6 +37,7 @@ impl Default for Registers {
36 Self {
37 Self {
37 inner: Default::default(),
38 inner: Default::default(),
38 clipboard_provider: get_clipboard_provider(),
39 clipboard_provider: get_clipboard_provider(),
40+ last_search_register: '/',
39 }
41 }
40 }
42 }
41}
43}