diff --git a/src/edit.rs b/src/edit.rs index 840445fe85..0507aff64b 100644 --- a/src/edit.rs +++ b/src/edit.rs @@ -513,20 +513,22 @@ impl<'out, 'prompt, H: Helper> State<'out, 'prompt, H> { } /// Moves the cursor to the same column in the line above - pub fn edit_move_line_up(&mut self, n: RepeatCount) -> Result<()> { + pub fn edit_move_line_up(&mut self, n: RepeatCount) -> Result { if self.line.move_to_line_up(n) { - self.move_cursor() + self.move_cursor()?; + Ok(true) } else { - Ok(()) + Ok(false) } } /// Moves the cursor to the same column in the line above - pub fn edit_move_line_down(&mut self, n: RepeatCount) -> Result<()> { + pub fn edit_move_line_down(&mut self, n: RepeatCount) -> Result { if self.line.move_to_line_down(n) { - self.move_cursor() + self.move_cursor()?; + Ok(true) } else { - Ok(()) + Ok(false) } } diff --git a/src/keymap.rs b/src/keymap.rs index 98a2ee4dd1..3e1d26f97f 100644 --- a/src/keymap.rs +++ b/src/keymap.rs @@ -88,6 +88,12 @@ pub enum Cmd { Yank(RepeatCount, Anchor), /// yank-pop YankPop, + /// moves cursor to the line above or switches to prev history entry if + /// the cursor is already on the first line + LineUpOrPreviousHistory, + /// moves cursor to the line below or switches to next history entry if + /// the cursor is already on the last line + LineDownOrNextHistory, } impl Cmd { @@ -891,8 +897,8 @@ impl InputState { } KeyPress::Ctrl('J') | KeyPress::Enter => Cmd::AcceptLine, - KeyPress::Down => Cmd::NextHistory, - KeyPress::Up => Cmd::PreviousHistory, + KeyPress::Down => Cmd::LineDownOrNextHistory, + KeyPress::Up => Cmd::LineUpOrPreviousHistory, KeyPress::Ctrl('R') => Cmd::ReverseSearchHistory, KeyPress::Ctrl('S') => Cmd::ForwardSearchHistory, // most terminals override Ctrl+S to suspend execution KeyPress::Ctrl('T') => Cmd::TransposeChars, diff --git a/src/lib.rs b/src/lib.rs index 97fc41573a..cc775e7383 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -536,6 +536,16 @@ fn readline_edit( // Fetch the previous command from the history list. s.edit_history_next(true)? } + Cmd::LineUpOrPreviousHistory => { + if !s.edit_move_line_up(1)? { + s.edit_history_next(true)? + } + } + Cmd::LineDownOrNextHistory => { + if !s.edit_move_line_down(1)? { + s.edit_history_next(false)? + } + } Cmd::HistorySearchBackward => s.edit_history_search(Direction::Reverse)?, Cmd::HistorySearchForward => s.edit_history_search(Direction::Forward)?, Cmd::TransposeChars => { @@ -603,10 +613,10 @@ fn readline_edit( s.edit_move_to_next_word(at, word_def, n)? } Cmd::Move(Movement::LineUp(n)) => { - s.edit_move_line_up(n)? + s.edit_move_line_up(n)?; } Cmd::Move(Movement::LineDown(n)) => { - s.edit_move_line_down(n)? + s.edit_move_line_down(n)?; } Cmd::DowncaseWord => { // lowercase word after point