Skip to content
This repository has been archived by the owner on Jun 15, 2021. It is now read-only.

Add functionality for Home and End (Begin, End of Line) #16

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 14 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,8 @@ and switch into the other modes.
| W | Move the cursor to the previous word |
| \<PgUp> | Move the cursor one page above |
| \<PgDn> | Move the cursor one page below |
| \<Home> | Move the cursor to beginning of line |
| \<End> | Move the cursor to the end of the line |
| v | Switch to Visual Mode |
| i | Switch to Insert Mode |
| \<Space> | Switch to Action Mode |
Expand All @@ -75,14 +77,16 @@ and switch into the other modes.

The Insert mode is used to insert some content.

| **Key** | **Description** |
|:------------:|:------------------------------:|
| ←↑→↓ | Move the cursor |
| \<PgUp> | Move the cursor one page above |
| \<PgDn> | Move the cursor one page below |
| \<Esc> | Switch to the normal mode |
| \<Backspace> | Remove a character backward |
| \<Del> | Remove a character forward |
| **Key** | **Description** |
|:------------:|:--------------------------------------:|
| ←↑→↓ | Move the cursor |
| \<PgUp> | Move the cursor one page above |
| \<PgDn> | Move the cursor one page below |
| \<Home> | Move the cursor to beginning of line |
| \<End> | Move the cursor to the end of the line |
| \<Esc> | Switch to the normal mode |
| \<Backspace> | Remove a character backward |
| \<Del> | Remove a character forward |


#### Visual mode
Expand All @@ -95,6 +99,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 |
| \<Home> | Move the cursor to beginning of line |
| \<End> | Move the cursor to the end of the line |
| \<Esc> | Switch to Normal Mode |
| q | Switch to Normal Mode |
| \<Space> | Switch to Action Mode |
Expand Down
24 changes: 17 additions & 7 deletions src/input_controller/actions/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ pub enum Response {
#[derive(Debug, Copy, Clone, Eq, PartialEq)]
pub enum Action {
WriteToFile,
Quite,
Quit,

SwitchToInsertMode,
SwitchToVisualMode,
Expand All @@ -29,9 +29,12 @@ pub enum Action {
MoveDown,
MoveLeft,
MoveRight,

MoveWordRight,
MoveWordLeft,
MoveToLeftEndOfLine,
MoveToRightEndOfLine,
MoveToLeftEndOfLineAndSelect,
MoveToRightEndOfLineAndSelect,

PageUp,
PageDown,
Expand Down Expand Up @@ -71,7 +74,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,
Expand All @@ -88,6 +91,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),
Expand All @@ -97,8 +105,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),

Expand All @@ -117,7 +125,7 @@ impl Action {
pub fn from_description(desc: &str) -> Option<Action> {
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),
Expand All @@ -131,14 +139,16 @@ 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),
"move_right_and_select" => Some(Action::MoveRightAndSelect),

"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),

Expand Down
38 changes: 35 additions & 3 deletions src/input_controller/actions/rpc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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!({}));

Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -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);
Expand All @@ -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);
Expand Down
4 changes: 4 additions & 0 deletions src/input_controller/keyboard/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ pub enum KeyStroke {
KeyBackSpace,
KeyDelete,
KeySpace,
KeyHome,
KeyEnd,
}

impl KeyStroke {
Expand All @@ -37,6 +39,8 @@ impl KeyStroke {
"<key_right>" => Some(KeyStroke::KeyRight),
"<page_up>" => Some(KeyStroke::KeyPreviousPage),
"<page_down>" => Some(KeyStroke::KeyNextPage),
"<home>" => Some(KeyStroke::KeyHome),
"<end>" => Some(KeyStroke::KeyEnd),
"<backspace>" => Some(KeyStroke::KeyBackSpace),
"<del>" => Some(KeyStroke::KeyDelete),
"<space>" => Some(KeyStroke::KeySpace),
Expand Down
4 changes: 2 additions & 2 deletions src/input_controller/keyboard/termion.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,10 +27,10 @@ impl<R: Read> Keyboard for TermionKeyboard<R> {
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)),
Expand Down
22 changes: 16 additions & 6 deletions src/input_controller/mode_actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -46,13 +46,15 @@ pub mod defaults {

lazy_static! {
pub static ref DEFAULT_NORMAL_MODE_ACTIONS: HashMap<KeyStroke, Action> = {
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);
Expand All @@ -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);
Expand All @@ -85,11 +87,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<KeyStroke, Action> = {
let mut actions = HashMap::with_capacity(12);
Expand All @@ -103,11 +105,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<KeyStroke, Action> = {
let mut actions = HashMap::with_capacity(1);
Expand All @@ -124,6 +129,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);
Expand All @@ -134,6 +144,6 @@ pub mod defaults {
actions.insert(KeyStroke::Char('W'), Action::MoveWordLeftAndSelect);

actions
};
};
}
}