Commit 073e011c authored by AdminXVII's avatar AdminXVII

Finish bubbling up display

parent b56b3ca9
......@@ -130,6 +130,9 @@ impl Context {
if keymap.handle_key(c.unwrap(), &mut ed, handler)? {
break;
}
ed.display()?;
ed.flush()?;
}
Ok(ed.into())
......
use std::io::{self, Write};
use std::io::Write;
use termion::event::Key;
use crate::CursorPosition;
......@@ -22,99 +22,49 @@ impl Emacs {
Self::default()
}
fn handle_ctrl_key<'a, W: Write>(&mut self, c: char, ed: &mut Editor<'a, W>) -> io::Result<()> {
fn handle_ctrl_key<'a, W: Write>(&mut self, c: char, ed: &mut Editor<'a, W>) {
match c {
'l' => {
ed.clear();
ed.display()
}
'a' => {
ed.move_cursor_to_start_of_line();
ed.display()
}
'e' => {
ed.move_cursor_to_end_of_line();
ed.display()
}
'b' => {
ed.move_cursor_left(1);
ed.display()
}
'f' => {
ed.move_cursor_right(1);
ed.display()
}
'd' => {
ed.delete_after_cursor();
ed.display()
}
'p' => {
ed.move_up();
ed.display()
}
'n' => {
ed.move_down();
ed.display()
}
'u' => {
ed.delete_all_before_cursor();
ed.display()
}
'k' => {
ed.delete_all_after_cursor();
ed.display()
}
'w' => {
ed.delete_word_before_cursor(true);
ed.display()
}
'l' => ed.clear(),
'a' => ed.move_cursor_to_start_of_line(),
'e' => ed.move_cursor_to_end_of_line(),
'b' => ed.move_cursor_left(1),
'f' => ed.move_cursor_right(1),
'd' => ed.delete_after_cursor(),
'p' => ed.move_up(),
'n' => ed.move_down(),
'u' => ed.delete_all_before_cursor(),
'k' => ed.delete_all_after_cursor(),
'w' => ed.delete_word_before_cursor(true),
'x' => {
ed.undo();
ed.display()
}
_ => Ok(()),
_ => (),
}
}
fn handle_alt_key<'a, W: Write>(&mut self, c: char, ed: &mut Editor<'a, W>) -> io::Result<()> {
fn handle_alt_key<'a, W: Write>(&mut self, c: char, ed: &mut Editor<'a, W>) {
match c {
'<' => {
ed.move_to_start_of_history();
ed.display()
}
'>' => {
ed.move_to_end_of_history();
ed.display()
}
'\x7F' => {
ed.delete_word_before_cursor(true);
ed.display()
}
'f' => {
emacs_move_word(ed, EmacsMoveDir::Right);
ed.display()
}
'b' => {
emacs_move_word(ed, EmacsMoveDir::Left);
ed.display()
}
'<' => ed.move_to_start_of_history(),
'>' => ed.move_to_end_of_history(),
'\x7F' => ed.delete_word_before_cursor(true),
'f' => emacs_move_word(ed, EmacsMoveDir::Right),
'b' => emacs_move_word(ed, EmacsMoveDir::Left),
'r' => {
ed.revert();
ed.display()
}
'.' => self.handle_last_arg_fetch(ed),
_ => Ok(()),
_ => (),
}
}
fn handle_last_arg_fetch<'a, W: Write>(&mut self, ed: &mut Editor<'a, W>) -> io::Result<()> {
fn handle_last_arg_fetch<'a, W: Write>(&mut self, ed: &mut Editor<'a, W>) {
// Empty history means no last arg to fetch.
if ed.context().history.is_empty() {
return Ok(());
return;
}
let history_index = match self.last_arg_fetch_index {
Some(0) => return Ok(()),
Some(0) => return,
Some(x) => x - 1,
None => ed
.current_history_location()
......@@ -138,63 +88,29 @@ impl Emacs {
// Edit the index in case the user does a last arg fetch again.
self.last_arg_fetch_index = Some(history_index);
ed.display()
}
}
impl KeyMap for Emacs {
fn handle_key_core<'a, W: Write>(
&mut self,
key: Key,
ed: &mut Editor<'a, W>,
) -> io::Result<()> {
fn handle_key_core<'a, W: Write>(&mut self, key: Key, ed: &mut Editor<'a, W>) {
match key {
Key::Alt('.') => {}
_ => self.last_arg_fetch_index = None,
}
match key {
Key::Char(c) => {
ed.insert_after_cursor(c);
ed.display()
}
Key::Char(c) => ed.insert_after_cursor(c),
Key::Alt(c) => self.handle_alt_key(c, ed),
Key::Ctrl(c) => self.handle_ctrl_key(c, ed),
Key::Left => {
ed.move_cursor_left(1);
ed.display()
}
Key::Right => {
ed.move_cursor_right(1);
ed.display()
}
Key::Up => {
ed.move_up();
ed.display()
}
Key::Down => {
ed.move_down();
ed.display()
}
Key::Home => {
ed.move_cursor_to_start_of_line();
ed.display()
}
Key::End => {
ed.move_cursor_to_end_of_line();
ed.display()
}
Key::Backspace => {
ed.delete_before_cursor();
ed.display()
}
Key::Delete => {
ed.delete_after_cursor();
ed.display()
}
Key::Null => Ok(()),
_ => Ok(()),
Key::Left => ed.move_cursor_left(1),
Key::Right => ed.move_cursor_right(1),
Key::Up => ed.move_up(),
Key::Down => ed.move_down(),
Key::Home => ed.move_cursor_to_start_of_line(),
Key::End => ed.move_cursor_to_end_of_line(),
Key::Backspace => ed.delete_before_cursor(),
Key::Delete => ed.delete_after_cursor(),
_ => (),
}
}
}
......
......@@ -5,11 +5,7 @@ use std::io::{self, ErrorKind, Write};
use termion::event::Key;
pub trait KeyMap: Default {
fn handle_key_core<'a, W: Write>(
&mut self,
key: Key,
editor: &mut Editor<'a, W>,
) -> io::Result<()>;
fn handle_key_core<'a, W: Write>(&mut self, key: Key, editor: &mut Editor<'a, W>);
fn init<'a, W: Write>(&mut self, _editor: &mut Editor<'a, W>) {}
......@@ -23,8 +19,6 @@ pub trait KeyMap: Default {
handler.on_event(Event::new(editor, EventKind::BeforeKey(key)));
let is_empty = editor.current_buffer().is_empty();
if key == Key::Ctrl('h') {
// XXX: Might need to change this when remappable keybindings are added.
key = Key::Backspace;
......@@ -36,7 +30,7 @@ pub trait KeyMap: Default {
return Err(io::Error::new(ErrorKind::Interrupted, "ctrl-c"));
}
// if the current buffer is empty, treat ctrl-d as eof
Key::Ctrl('d') if is_empty => {
Key::Ctrl('d') if editor.current_buffer().is_empty() => {
editor.handle_newline();
return Err(io::Error::new(ErrorKind::UnexpectedEof, "ctrl-d"));
}
......@@ -54,16 +48,13 @@ pub trait KeyMap: Default {
editor.accept_autosuggestion()
}
_ => {
self.handle_key_core(key, editor)?;
self.handle_key_core(key, editor);
editor.skip_completions_hint();
}
};
handler.on_event(Event::new(editor, EventKind::AfterKey(key)));
editor.display()?;
editor.flush()?;
Ok(done)
}
}
......@@ -85,13 +76,7 @@ mod tests {
struct TestKeyMap;
impl KeyMap for TestKeyMap {
fn handle_key_core<'a, W: Write>(
&mut self,
_: Key,
_: &mut Editor<'a, W>,
) -> io::Result<()> {
Ok(())
}
fn handle_key_core<'a, W: Write>(&mut self, _: Key, _: &mut Editor<'a, W>) {}
}
struct EmptyCompleter;
......
This diff is collapsed.
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