From 8f18ef77cbdc6914f241fc3360076ab86bd4c266 Mon Sep 17 00:00:00 2001 From: gwenn <gtreguier@gmail.com> Date: Sun, 24 Jul 2016 12:42:31 +0200 Subject: [PATCH] Add moving to the first/last entry in history. --- README.md | 16 +++++++++------- src/lib.rs | 45 +++++++++++++++++++++++++++++++++++++++++---- 2 files changed, 50 insertions(+), 11 deletions(-) diff --git a/README.md b/README.md index aaaafd9e..841e38ee 100644 --- a/README.md +++ b/README.md @@ -82,7 +82,7 @@ Ctrl-D | (if line *is* empty) End of File Ctrl-E, End | Move cursor to end of line Ctrl-F, Right| Move cursor one character right Ctrl-H, BackSpace | Delete character before cursor -Ctrl-I, Tab | Next completion +Ctrl-I, Tab | Next completion Ctrl-J, Ctrl-M, Enter | Finish the line entry Ctrl-K | Delete from cursor to end of line Ctrl-L | Clear screen @@ -94,14 +94,16 @@ Ctrl-U | Delete from start of line to cursor Ctrl-V | Insert any special character without perfoming its associated action Ctrl-W | Delete word leading up to cursor (using white space as a word boundary) Ctrl-Y | Paste from Yank buffer (Meta-Y to paste next yank instead) +Meta-< | Move to first entry in history +Meta-> | Move to last entry in history Meta-B, Alt-Left | Move cursor to previous word -Meta-C | Capitalize the current word -Meta-D | Delete forwards one word +Meta-C | Capitalize the current word +Meta-D | Delete forwards one word Meta-F, Alt-Right | Move cursor to next word -Meta-L | Lower-case the next word -Meta-T | Transpose words -Meta-U | Upper-case the next word -Meta-Y | See Ctrl-Y +Meta-L | Lower-case the next word +Meta-T | Transpose words +Meta-U | Upper-case the next word +Meta-Y | See Ctrl-Y Meta-BackSpace | Kill from the start of the current word, or, if between words, to the start of the previous word ## ToDo diff --git a/src/lib.rs b/src/lib.rs index bd065a11..eb3e2cc3 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -690,7 +690,34 @@ fn edit_history_next(s: &mut State, history: &History, prev: bool) -> Result<()> } else { // Restore current edited line s.snapshot(); - }; + } + s.refresh_line() +} + +/// Substitute the currently edited line with the first/last history entry. +fn edit_history(s: &mut State, history: &History, first: bool) -> Result<()> { + if history.is_empty() { + return Ok(()); + } + if s.history_index == history.len() { + if first { + // Save the current edited line before to overwrite it + s.snapshot(); + } else { + return Ok(()); + } + } else if s.history_index == 0 && first { + return Ok(()); + } + if first { + s.history_index = 0; + let buf = history.get(s.history_index).unwrap(); + s.line.update(buf, buf.len()); + } else { + s.history_index = history.len(); + // Restore current edited line + s.snapshot(); + } s.refresh_line() } @@ -897,9 +924,10 @@ impl<R: Read> RawReader<R> { // TODO ESC-N (n): search history forward not interactively // TODO ESC-P (p): search history backward not interactively // TODO ESC-R (r): Undo all changes made to this line. - // TODO ESC-<: move to first entry in history - // TODO ESC->: move to last entry in history match seq1 { + '\x08' => Ok(KeyPress::Meta('\x08')), // Backspace + '<' => Ok(KeyPress::Meta('<')), + '>' => Ok(KeyPress::Meta('>')), 'b' | 'B' => Ok(KeyPress::Meta('B')), 'c' | 'C' => Ok(KeyPress::Meta('C')), 'd' | 'D' => Ok(KeyPress::Meta('D')), @@ -908,7 +936,6 @@ impl<R: Read> RawReader<R> { 't' | 'T' => Ok(KeyPress::Meta('T')), 'u' | 'U' => Ok(KeyPress::Meta('U')), 'y' | 'Y' => Ok(KeyPress::Meta('Y')), - '\x08' => Ok(KeyPress::Meta('\x08')), // Backspace '\x7f' => Ok(KeyPress::Meta('\x7f')), // Delete _ => { // writeln!(io::stderr(), "key: {:?}, seq1: {:?}", KeyPress::Esc, seq1).unwrap(); @@ -1214,6 +1241,16 @@ fn readline_edit(prompt: &str, kill_ring.kill(&text, false) } } + KeyPress::Meta('<') => { + // move to first entry in history + kill_ring.reset(); + try!(edit_history(&mut s, history, true)) + } + KeyPress::Meta('>') => { + // move to last entry in history + kill_ring.reset(); + try!(edit_history(&mut s, history, false)) + } KeyPress::Meta('B') => { // move backwards one word kill_ring.reset(); -- GitLab