Handle paste events in editor

Only on non-windows platforms though, since crossterm doesn't support
pasting on windows.
This commit is contained in:
Joscha 2022-08-10 23:59:40 +02:00
parent 5ad9f0f3e7
commit f7e7003788
3 changed files with 15 additions and 2 deletions

View file

@ -17,6 +17,7 @@ Procedure when bumping the version number:
### Added ### Added
- New messages are now marked as unseen - New messages are now marked as unseen
- Sub-trees can now be folded - Sub-trees can now be folded
- Support for pasting text into editors
- More readline-esque editor key bindings - More readline-esque editor key bindings
- Key bindings to move to prev/next sibling - Key bindings to move to prev/next sibling
- Key binding to center cursor on screen - Key binding to center cursor on screen

View file

@ -78,6 +78,7 @@ pub fn handle_editor_event(
// Editing // Editing
key!(Char ch) if char_filter(*ch) => editor.insert_char(terminal.frame(), *ch), key!(Char ch) if char_filter(*ch) => editor.insert_char(terminal.frame(), *ch),
key!(Paste str) if str.chars().all(char_filter) => editor.insert_str(terminal.frame(), str),
key!(Ctrl + 'h') | key!(Backspace) => editor.backspace(terminal.frame()), key!(Ctrl + 'h') | key!(Backspace) => editor.backspace(terminal.frame()),
key!(Ctrl + 'd') | key!(Delete) => editor.delete(), key!(Ctrl + 'd') | key!(Delete) => editor.delete(),
key!(Ctrl + 'l') => editor.clear(), key!(Ctrl + 'l') => editor.clear(),

View file

@ -182,8 +182,15 @@ impl InnerEditorState {
/// accordingly. /// accordingly.
fn insert_char(&mut self, frame: &mut Frame, ch: char) { fn insert_char(&mut self, frame: &mut Frame, ch: char) {
self.text.insert(self.idx, ch); self.text.insert(self.idx, ch);
self.idx += 1; self.idx += ch.len_utf8();
self.move_cursor_to_grapheme_boundary(); self.record_cursor_col(frame);
}
/// Insert a string at the current cursor position and move the cursor
/// accordingly.
fn insert_str(&mut self, frame: &mut Frame, str: &str) {
self.text.insert_str(self.idx, str);
self.idx += str.len();
self.record_cursor_col(frame); self.record_cursor_col(frame);
} }
@ -347,6 +354,10 @@ impl EditorState {
self.0.lock().insert_char(frame, ch); self.0.lock().insert_char(frame, ch);
} }
pub fn insert_str(&self, frame: &mut Frame, str: &str) {
self.0.lock().insert_str(frame, str);
}
/// Delete the grapheme before the cursor position. /// Delete the grapheme before the cursor position.
pub fn backspace(&self, frame: &mut Frame) { pub fn backspace(&self, frame: &mut Frame) {
self.0.lock().backspace(frame); self.0.lock().backspace(frame);