From 594387656902ba0f61f6447b698791f7a045d03b Mon Sep 17 00:00:00 2001 From: Joe Bellus Date: Mon, 17 Oct 2022 15:11:29 -0400 Subject: [PATCH] Added yank editor commands Yank, yank line, and paste were added to normal mode keybinds --- abacus-ui/README.org | 4 +++ abacus-ui/src/editor.rs | 56 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 58 insertions(+), 2 deletions(-) diff --git a/abacus-ui/README.org b/abacus-ui/README.org index e1db715..67f7569 100644 --- a/abacus-ui/README.org +++ b/abacus-ui/README.org @@ -35,6 +35,10 @@ In normal mode the cursor is a block and functional keybinds can be used for mov | dd | Delete the current line | | cw | Change word forward | | cb | Change word backward | +| y | Yank selection | +| Y | Yank line | +| p | Paste line at cursor | +| P | Paste line below | ** Insert mode In insert mode the cursor is a line and text can be edited. To return to normal mode use the ESC key. diff --git a/abacus-ui/src/editor.rs b/abacus-ui/src/editor.rs index 2fb6a80..9a35092 100644 --- a/abacus-ui/src/editor.rs +++ b/abacus-ui/src/editor.rs @@ -38,6 +38,10 @@ mod keymap { pub const SELECT_MODE: &str = "v"; pub const CHANGE_WORD_FORWARD: &str = "cw"; pub const CHANGE_WORD_BACKWARD: &str = "cb"; + pub const YANK: &str = "y"; + pub const YANK_LINE: &str = "Y"; + pub const PASTE: &str = "p"; + pub const PASTE_LINE: &str = "P"; #[derive(Debug, Default)] pub struct KeyList { @@ -67,6 +71,10 @@ mod keymap { SELECT_MODE, CHANGE_WORD_FORWARD, CHANGE_WORD_BACKWARD, + YANK, + YANK_LINE, + PASTE, + PASTE_LINE, ], } } @@ -153,7 +161,7 @@ impl AbacusEditor { } if data.mode == EditMode::Normal { - let char_rect = if data.content.len_chars() == 0 { + let char_rect = if data.cursor_pos == 0 { let rects = layout.rects_for_range(0..1); rects.first().cloned() } else if data.current_char() == Some('\n') @@ -362,7 +370,9 @@ impl Widget for AbacusEditor { data.word_scan_backward(); } Some(keymap::SELECT_MODE) => { - data.selection_pos = Some(data.cursor_pos); + data.cursor_to_end_of_line(); + data.start_selection(); + data.cursor_to_start_of_line(); } Some(keymap::CHANGE_WORD_FORWARD) => { data.delete_word_forward(); @@ -372,6 +382,48 @@ impl Widget for AbacusEditor { data.delete_word_backward(); data.insert_mode(); } + Some(keymap::YANK) => { + if data.selection_pos.is_some() { + let mut cb: clipboard::ClipboardContext = + clipboard::ClipboardProvider::new().unwrap(); + if let Some(slice) = data.content.get_slice(data.select_range()) + { + let _ = cb.set_contents(slice.to_string()); + } + } + data.deselect(); + } + Some(keymap::YANK_LINE) => { + data.cursor_to_start_of_line(); + data.start_selection(); + data.cursor_to_end_of_line(); + if data.selection_pos.is_some() { + let mut cb: clipboard::ClipboardContext = + clipboard::ClipboardProvider::new().unwrap(); + if let Some(slice) = data.content.get_slice(data.select_range()) + { + let _ = cb.set_contents(slice.to_string()); + } + } + data.deselect(); + } + Some(keymap::PASTE) => { + let mut cb: clipboard::ClipboardContext = + clipboard::ClipboardProvider::new().unwrap(); + data.push_str(&cb.get_contents().unwrap_or_default()); + ctx.request_layout(); + } + Some(keymap::PASTE_LINE) => { + if data.selection_pos.is_some() { + data.delete_char_back(); + } + let mut cb: clipboard::ClipboardContext = + clipboard::ClipboardProvider::new().unwrap(); + data.cursor_to_end_of_line(); + data.push_str("\n"); + data.push_str(&cb.get_contents().unwrap_or_default()); + ctx.request_layout(); + } _ => {} } }