From 6bfc70707773f902c5500639fcb39dc25a2e610a Mon Sep 17 00:00:00 2001 From: Simon Willshire Date: Thu, 31 Oct 2019 14:04:25 +1300 Subject: [PATCH 1/2] Add functionality for Home and End (Begin, End of Line) - Added associated rpc, with and without selection - Added defaults to enable during normal, visual and insert modes - Fixed spelling mistakes: cute => cut, quite => quit --- src/input_controller/actions/mod.rs | 24 ++++++++++----- src/input_controller/actions/rpc.rs | 38 ++++++++++++++++++++++-- src/input_controller/keyboard/mod.rs | 4 +++ src/input_controller/keyboard/termion.rs | 4 +-- src/input_controller/mode_actions.rs | 22 ++++++++++---- 5 files changed, 74 insertions(+), 18 deletions(-) diff --git a/src/input_controller/actions/mod.rs b/src/input_controller/actions/mod.rs index 4ce428c..63cfcff 100644 --- a/src/input_controller/actions/mod.rs +++ b/src/input_controller/actions/mod.rs @@ -18,7 +18,7 @@ pub enum Response { #[derive(Debug, Copy, Clone, Eq, PartialEq)] pub enum Action { WriteToFile, - Quite, + Quit, SwitchToInsertMode, SwitchToVisualMode, @@ -29,9 +29,12 @@ pub enum Action { MoveDown, MoveLeft, MoveRight, - MoveWordRight, MoveWordLeft, + MoveToLeftEndOfLine, + MoveToRightEndOfLine, + MoveToLeftEndOfLineAndSelect, + MoveToRightEndOfLineAndSelect, PageUp, PageDown, @@ -68,7 +71,7 @@ impl Action { ) -> Response { match self { Action::WriteToFile => rpc::write_to_file(view_id, front_event_writer), - Action::Quite => rpc::quite(view_id, core), + Action::Quit => rpc::quit(view_id, core), Action::SwitchToInsertMode => Response::SwitchToInsertMode, Action::SwitchToVisualMode => Response::SwitchToVisualMode, @@ -85,6 +88,11 @@ impl Action { Action::PageUp => rpc::page_up(view_id, core), Action::PageDown => rpc::page_down(view_id, core), + Action::MoveToLeftEndOfLine => rpc::move_to_left_end_of_line(view_id, core), + Action::MoveToRightEndOfLine => rpc::move_to_right_end_of_line(view_id, core), + Action::MoveToLeftEndOfLineAndSelect => rpc::move_to_left_end_of_line_and_select(view_id, core), + Action::MoveToRightEndOfLineAndSelect => rpc::move_to_right_end_of_line_and_select(view_id, core), + Action::MoveUpAndSelect => rpc::move_up_and_select(view_id, core), Action::MoveDownAndSelect => rpc::move_down_and_select(view_id, core), @@ -94,8 +102,8 @@ impl Action { Action::MoveWordLeftAndSelect => rpc::move_word_left_and_select(view_id, core), Action::YankSelection => rpc::yank_selection(view_id, core), - Action::DeleteSelection => rpc::cute_selection(view_id, core), - Action::DeleteSelectionAndPaste => rpc::cute_selection_and_paste(view_id, core), + Action::DeleteSelection => rpc::cut_selection(view_id, core), + Action::DeleteSelectionAndPaste => rpc::cut_selection_and_paste(view_id, core), Action::Paste => rpc::paste(view_id, core), @@ -111,7 +119,7 @@ impl Action { pub fn from_description(desc: &str) -> Option { match desc { "write_to_file" => Some(Action::WriteToFile), - "quit" => Some(Action::Quite), + "quit" => Some(Action::Quit), "switch_to_insert_mode" => Some(Action::SwitchToInsertMode), "switch_to_visual_mode" => Some(Action::SwitchToVisualMode), @@ -125,6 +133,8 @@ impl Action { "page_up" => Some(Action::PageUp), "page_down" => Some(Action::PageDown), + "move_to_left_end_of_line" => Some(Action::MoveToLeftEndOfLine), + "move_to_right_end_of_line" => Some(Action::MoveToRightEndOfLine), "move_up_and_select" => Some(Action::MoveUpAndSelect), "move_down_and_select" => Some(Action::MoveDownAndSelect), "move_left_and_select" => Some(Action::MoveLeftAndSelect), @@ -132,7 +142,7 @@ impl Action { "yank_selection" => Some(Action::YankSelection), "delete_selection" => Some(Action::DeleteSelection), - "delete_selection_and_past" => Some(Action::DeleteSelectionAndPaste), + "delete_selection_and_paste" => Some(Action::DeleteSelectionAndPaste), "paste" => Some(Action::Paste), diff --git a/src/input_controller/actions/rpc.rs b/src/input_controller/actions/rpc.rs index ee0b0ba..530dfb6 100644 --- a/src/input_controller/actions/rpc.rs +++ b/src/input_controller/actions/rpc.rs @@ -24,7 +24,7 @@ pub fn insert_keystroke(view_id: &str, key: KeyStroke, core: &dyn Peer) -> Respo Response::Continue } -pub fn quite(view_id: &str, core: &dyn Peer) -> Response { +pub fn quit(view_id: &str, core: &dyn Peer) -> Response { core.send_rpc_notification("close_view", &json!({ "view_id": view_id })); core.send_rpc_notification("exit", &json!({})); @@ -92,6 +92,38 @@ pub fn page_down(view_id: &str, core: &dyn Peer) -> Response { Response::Continue } +pub fn move_to_left_end_of_line(view_id: &str, core: &dyn Peer) -> Response { + core.send_rpc_notification( + "edit", + &json!({ "method": "move_to_left_end_of_line", "view_id": view_id }), + ); + Response::Continue +} + +pub fn move_to_right_end_of_line(view_id: &str, core: &dyn Peer) -> Response { + core.send_rpc_notification( + "edit", + &json!({ "method": "move_to_right_end_of_line", "view_id": view_id }), + ); + Response::Continue +} + +pub fn move_to_left_end_of_line_and_select(view_id: &str, core: &dyn Peer) -> Response { + core.send_rpc_notification( + "edit", + &json!({ "method": "move_to_left_end_of_line_and_modify_selection", "view_id": view_id }), + ); + Response::Continue +} + +pub fn move_to_right_end_of_line_and_select(view_id: &str, core: &dyn Peer) -> Response { + core.send_rpc_notification( + "edit", + &json!({ "method": "move_to_right_end_of_line_and_modify_selection", "view_id": view_id }), + ); + Response::Continue +} + pub fn move_word_right(view_id: &str, core: &dyn Peer) -> Response { core.send_rpc_notification( "edit", @@ -182,7 +214,7 @@ pub fn yank_selection(view_id: &str, core: &dyn Peer) -> Response { Response::SwitchToNormalMode } -pub fn cute_selection(view_id: &str, core: &dyn Peer) -> Response { +pub fn cut_selection(view_id: &str, core: &dyn Peer) -> Response { let cut_res = core.send_rpc_request("edit", &json!({ "method": "cut", "view_id": view_id})); if cut_res.is_err() { error!("failed to cut the selection: {:?}", cut_res); @@ -200,7 +232,7 @@ pub fn cute_selection(view_id: &str, core: &dyn Peer) -> Response { Response::SwitchToNormalMode } -pub fn cute_selection_and_paste(view_id: &str, core: &dyn Peer) -> Response { +pub fn cut_selection_and_paste(view_id: &str, core: &dyn Peer) -> Response { let cut_res = core.send_rpc_request("edit", &json!({ "method": "cut", "view_id": view_id})); if cut_res.is_err() { error!("failed to cut the selection: {:?}", cut_res); diff --git a/src/input_controller/keyboard/mod.rs b/src/input_controller/keyboard/mod.rs index 6d381df..5b42ed1 100644 --- a/src/input_controller/keyboard/mod.rs +++ b/src/input_controller/keyboard/mod.rs @@ -21,6 +21,8 @@ pub enum KeyStroke { KeyBackSpace, KeyDelete, KeySpace, + KeyHome, + KeyEnd, } impl KeyStroke { @@ -37,6 +39,8 @@ impl KeyStroke { "" => Some(KeyStroke::KeyRight), "" => Some(KeyStroke::KeyPreviousPage), "" => Some(KeyStroke::KeyNextPage), + "" => Some(KeyStroke::KeyHome), + "" => Some(KeyStroke::KeyEnd), "" => Some(KeyStroke::KeyBackSpace), "" => Some(KeyStroke::KeyDelete), "" => Some(KeyStroke::KeySpace), diff --git a/src/input_controller/keyboard/termion.rs b/src/input_controller/keyboard/termion.rs index 88bb436..bbe75c1 100644 --- a/src/input_controller/keyboard/termion.rs +++ b/src/input_controller/keyboard/termion.rs @@ -27,10 +27,10 @@ impl Keyboard for TermionKeyboard { Key::Right => Some(KeyStroke::KeyRight), Key::Up => Some(KeyStroke::KeyUp), Key::Down => Some(KeyStroke::KeyDown), - Key::Home => None, - Key::End => None, Key::PageUp => Some(KeyStroke::KeyPreviousPage), Key::PageDown => Some(KeyStroke::KeyNextPage), + Key::Home => Some(KeyStroke::KeyHome), + Key::End => Some(KeyStroke::KeyEnd), Key::Delete => Some(KeyStroke::KeyDelete), Key::Insert => None, Key::F(n) => Some(KeyStroke::KeyF(n)), diff --git a/src/input_controller/mode_actions.rs b/src/input_controller/mode_actions.rs index ff98fd8..12b4e72 100644 --- a/src/input_controller/mode_actions.rs +++ b/src/input_controller/mode_actions.rs @@ -46,13 +46,15 @@ pub mod defaults { lazy_static! { pub static ref DEFAULT_NORMAL_MODE_ACTIONS: HashMap = { - let mut actions = HashMap::with_capacity(12); + let mut actions = HashMap::with_capacity(21); // The classic arrow keys. actions.insert(KeyStroke::KeyUp, Action::MoveUp); actions.insert(KeyStroke::KeyDown, Action::MoveDown); actions.insert(KeyStroke::KeyLeft, Action::MoveLeft); actions.insert(KeyStroke::KeyRight, Action::MoveRight); + actions.insert(KeyStroke::KeyHome, Action::MoveToLeftEndOfLine); + actions.insert(KeyStroke::KeyEnd, Action::MoveToRightEndOfLine); // The "vim like" keys. actions.insert(KeyStroke::Char('k'), Action::MoveUp); @@ -61,7 +63,7 @@ pub mod defaults { actions.insert(KeyStroke::Char('l'), Action::MoveRight); actions.insert(KeyStroke::Char('p'), Action::Paste); - actions.insert(KeyStroke::Char('q'), Action::Quite); + actions.insert(KeyStroke::Char('q'), Action::Quit); actions.insert(KeyStroke::Char('i'), Action::SwitchToInsertMode); actions.insert(KeyStroke::Char('v'), Action::SwitchToVisualMode); actions.insert(KeyStroke::KeySpace, Action::SwitchToActionMode); @@ -82,11 +84,11 @@ pub mod defaults { let mut actions = HashMap::with_capacity(3); actions.insert(KeyStroke::KeyEscape, Action::SwitchToNormalMode); - actions.insert(KeyStroke::Char('q'), Action::Quite); + actions.insert(KeyStroke::Char('q'), Action::Quit); actions.insert(KeyStroke::Char('w'), Action::WriteToFile); actions - }; + }; pub static ref DEFAULT_INSERT_MODE_ACTIONS: HashMap = { let mut actions = HashMap::with_capacity(12); @@ -100,11 +102,14 @@ pub mod defaults { actions.insert(KeyStroke::KeyDown, Action::MoveDown); actions.insert(KeyStroke::KeyLeft, Action::MoveLeft); actions.insert(KeyStroke::KeyRight, Action::MoveRight); + actions.insert(KeyStroke::KeyPreviousPage, Action::PageUp); actions.insert(KeyStroke::KeyNextPage, Action::PageDown); + actions.insert(KeyStroke::KeyHome, Action::MoveToLeftEndOfLine); + actions.insert(KeyStroke::KeyEnd, Action::MoveToRightEndOfLine); actions - }; + }; pub static ref DEFAULT_VISUAL_MODE_ACTIONS: HashMap = { let mut actions = HashMap::with_capacity(1); @@ -120,6 +125,11 @@ pub mod defaults { actions.insert(KeyStroke::KeyLeft, Action::MoveLeftAndSelect); actions.insert(KeyStroke::KeyRight, Action::MoveRightAndSelect); + actions.insert(KeyStroke::KeyPreviousPage, Action::PageUp); + actions.insert(KeyStroke::KeyNextPage, Action::PageDown); + actions.insert(KeyStroke::KeyHome, Action::MoveToLeftEndOfLineAndSelect); + actions.insert(KeyStroke::KeyEnd, Action::MoveToRightEndOfLineAndSelect); + // The "vim like" keys. actions.insert(KeyStroke::Char('k'), Action::MoveUpAndSelect); actions.insert(KeyStroke::Char('j'), Action::MoveDownAndSelect); @@ -132,6 +142,6 @@ pub mod defaults { actions.insert(KeyStroke::Char('X'), Action::DeleteSelection); actions - }; + }; } } From a15c78edb026867977e6b43a5f70113c038621de Mon Sep 17 00:00:00 2001 From: Simon Willshire Date: Thu, 31 Oct 2019 14:16:18 +1300 Subject: [PATCH 2/2] Update readme with Home / End actions --- README.md | 22 ++++++++++++++-------- 1 file changed, 14 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index 56fe500..6385865 100644 --- a/README.md +++ b/README.md @@ -58,6 +58,8 @@ and switch into the other modes. | W | Move the cursor to the previous word | | \ | Move the cursor one page above | | \ | Move the cursor one page below | +| \ | Move the cursor to beginning of line | +| \ | Move the cursor to the end of the line | | v | Switch to Visual Mode | | i | Switch to Insert Mode | | \ | Switch to Action Mode | @@ -73,14 +75,16 @@ and switch into the other modes. The Insert mode is used to insert some content. -| **Key** | **Description** | -|:------------:|:------------------------------:| -| ←↑→↓ | Move the cursor | -| \ | Move the cursor one page above | -| \ | Move the cursor one page below | -| \ | Switch to the normal mode | -| \ | Remove a character backward | -| \ | Remove a character forward | +| **Key** | **Description** | +|:------------:|:--------------------------------------:| +| ←↑→↓ | Move the cursor | +| \ | Move the cursor one page above | +| \ | Move the cursor one page below | +| \ | Move the cursor to beginning of line | +| \ | Move the cursor to the end of the line | +| \ | Switch to the normal mode | +| \ | Remove a character backward | +| \ | Remove a character forward | #### Visual mode @@ -93,6 +97,8 @@ The Visual mode is used to select some text and manipulate it. | hjkl | Move the cursor | | w | Move the cursor to the next word | | W | Move the cursor to the previous word | +| \ | Move the cursor to beginning of line | +| \ | Move the cursor to the end of the line | | \ | Switch to Normal Mode | | \ | Switch to Action Mode | | y | Yank the selection |