Commit 406f331e authored by MovingtoMars's avatar MovingtoMars

right arrow key only accepts autosuggestions if at end of line

parent 92275477
......@@ -513,6 +513,15 @@ impl<'a, W: Write> Editor<'a, W> {
self.print_current_buffer(false)
}
pub fn cursor_is_at_end_of_line(&self) -> bool {
let num_chars = cur_buf!(self).num_chars();
if self.no_eol {
self.cursor == num_chars - 1
} else {
self.cursor == num_chars
}
}
/// Returns a reference to the current buffer being edited.
/// This may be the new buffer or a buffer from history.
pub fn current_buffer(&self) -> &Buffer {
......
......@@ -30,7 +30,11 @@ pub trait KeyMap<'a, W: Write, T>: From<T> {
try!(self.editor_mut().handle_newline());
done = true;
}
Key::Ctrl('f') | Key::Right if self.editor().is_currently_showing_autosuggestion() => {
Key::Ctrl('f') if self.editor().is_currently_showing_autosuggestion() => {
try!(self.editor_mut().accept_autosuggestion());
}
Key::Right if self.editor().is_currently_showing_autosuggestion() &&
self.editor().cursor_is_at_end_of_line() => {
try!(self.editor_mut().accept_autosuggestion());
}
_ => {
......
......@@ -3,6 +3,7 @@ extern crate termion;
use std::mem::replace;
use std::env::{args, current_dir};
use std::io;
use liner::{Context, CursorPosition, Event, EventKind, FilenameCompleter};
......@@ -22,32 +23,65 @@ fn main() {
loop {
let res = con.read_line("[prompt]$ ",
&mut |Event { editor, kind }| {
if let EventKind::BeforeComplete = kind {
let (_, pos) = editor.get_words_and_cursor_position();
// Figure out of we are completing a command (the first word) or a filename.
let filename = match pos {
CursorPosition::InWord(i) => i > 0,
CursorPosition::InSpace(Some(_), _) => true,
CursorPosition::InSpace(None, _) => false,
CursorPosition::OnWordLeftEdge(i) => i >= 1,
CursorPosition::OnWordRightEdge(i) => i >= 1,
};
if filename {
let completer = FilenameCompleter::new(Some(current_dir().unwrap()));
replace(&mut editor.context().completer, Some(Box::new(completer)));
} else {
replace(&mut editor.context().completer, None);
&mut |Event { editor, kind }| {
if let EventKind::BeforeComplete = kind {
let (_, pos) = editor.get_words_and_cursor_position();
// Figure out of we are completing a command (the first word) or a filename.
let filename = match pos {
CursorPosition::InWord(i) => i > 0,
CursorPosition::InSpace(Some(_), _) => true,
CursorPosition::InSpace(None, _) => false,
CursorPosition::OnWordLeftEdge(i) => i >= 1,
CursorPosition::OnWordRightEdge(i) => i >= 1,
};
if filename {
let completer = FilenameCompleter::new(Some(current_dir().unwrap()));
replace(&mut editor.context().completer, Some(Box::new(completer)));
} else {
replace(&mut editor.context().completer, None);
}
}
});
match res {
Ok(res) => {
match res.as_str() {
"emacs" => {
con.key_bindings = liner::KeyBindings::Emacs;
println!("emacs mode");
}
"vi" => {
con.key_bindings = liner::KeyBindings::Vi;
println!("vi mode");
}
"exit" | "" => {
println!("exiting...");
break;
}
_ => {}
}
})
.unwrap();
if res.is_empty() {
break;
if res.is_empty() {
break;
}
con.history.push(res.into()).unwrap();
}
Err(e) => {
match e.kind() {
// ctrl-c pressed
io::ErrorKind::Interrupted => {}
// ctrl-d pressed
io::ErrorKind::UnexpectedEof => {
println!("exiting...");
break;
}
_ => panic!("error: {:?}", e),
}
}
}
con.history.push(res.into()).unwrap();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment