@@ -20,6 +20,8 @@ pub struct Prompt {
2020 cursor: usize,
2121 completion: Vec<Completion>,
2222 selection: Option<usize>,
23+ history_register: Option<char>,
24+ history_pos: Option<usize>,
2325 completion_fn: Box<dyn FnMut(&str) -> Vec<Completion>>,
2426 callback_fn: Box<dyn FnMut(&mut Context, &str, PromptEvent)>,
2527 pub doc_fn: Box<dyn Fn(&str) -> Option<&'static str>>,
@@ -54,6 +56,7 @@ pub enum Movement {
5456impl Prompt {
5557 pub fn new(
5658 prompt: String,
59+ history_register: Option<char>,
5760 mut completion_fn: impl FnMut(&str) -> Vec<Completion> + 'static,
5861 callback_fn: impl FnMut(&mut Context, &str, PromptEvent) + 'static,
5962 ) -> Self {
@@ -63,6 +66,8 @@ impl Prompt {
6366 cursor: 0,
6467 completion: completion_fn(""),
6568 selection: None,
69+ history_register,
70+ history_pos: None,
6671 completion_fn: Box::new(completion_fn),
6772 callback_fn: Box::new(callback_fn),
6873 doc_fn: Box::new(|_| None),
@@ -226,6 +231,28 @@ impl Prompt {
226231 self.exit_selection();
227232 }
228233
234+ pub fn change_history(&mut self, register: &[String], direction: CompletionDirection) {
235+ if register.is_empty() {
236+ return;
237+ }
238+
239+ let end = register.len().saturating_sub(1);
240+
241+ let index = match direction {
242+ CompletionDirection::Forward => self.history_pos.map_or(0, |i| i + 1),
243+ CompletionDirection::Backward => {
244+ self.history_pos.unwrap_or(register.len()).saturating_sub(1)
245+ }
246+ }
247+ .min(end);
248+
249+ self.line = register[index].clone();
250+
251+ self.history_pos = Some(index);
252+
253+ self.move_end();
254+ }
255+
229256 pub fn change_completion_selection(&mut self, direction: CompletionDirection) {
230257 if self.completion.is_empty() {
231258 return;
@@ -468,9 +495,40 @@ impl Component for Prompt {
468495 self.exit_selection();
469496 } else {
470497 (self.callback_fn)(cx, &self.line, PromptEvent::Validate);
498+
499+ if let Some(register) = self.history_register {
500+ // store in history
501+ let register = cx.editor.registers.get_mut(register);
502+ register.push(self.line.clone());
503+ }
471504 return close_fn;
472505 }
473506 }
507+ KeyEvent {
508+ code: KeyCode::Char('p'),
509+ modifiers: KeyModifiers::CONTROL,
510+ }
511+ | KeyEvent {
512+ code: KeyCode::Up, ..
513+ } => {
514+ if let Some(register) = self.history_register {
515+ let register = cx.editor.registers.get_mut(register);
516+ self.change_history(register.read(), CompletionDirection::Backward);
517+ }
518+ }
519+ KeyEvent {
520+ code: KeyCode::Char('n'),
521+ modifiers: KeyModifiers::CONTROL,
522+ }
523+ | KeyEvent {
524+ code: KeyCode::Down,
525+ ..
526+ } => {
527+ if let Some(register) = self.history_register {
528+ let register = cx.editor.registers.get_mut(register);
529+ self.change_history(register.read(), CompletionDirection::Forward);
530+ }
531+ }
474532 KeyEvent {
475533 code: KeyCode::Tab, ..
476534 } => self.change_completion_selection(CompletionDirection::Forward),