From f7e70037887bede2ed6ce87e5e4b61eb903cc420 Mon Sep 17 00:00:00 2001 From: Joscha Date: Wed, 10 Aug 2022 23:59:40 +0200 Subject: [PATCH] Handle paste events in editor Only on non-windows platforms though, since crossterm doesn't support pasting on windows. --- CHANGELOG.md | 1 + src/ui/util.rs | 1 + src/ui/widgets/editor.rs | 15 +++++++++++++-- 3 files changed, 15 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0e16d9c..064c0c0 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -17,6 +17,7 @@ Procedure when bumping the version number: ### Added - New messages are now marked as unseen - Sub-trees can now be folded +- Support for pasting text into editors - More readline-esque editor key bindings - Key bindings to move to prev/next sibling - Key binding to center cursor on screen diff --git a/src/ui/util.rs b/src/ui/util.rs index 2d56380..ff6b7d8 100644 --- a/src/ui/util.rs +++ b/src/ui/util.rs @@ -78,6 +78,7 @@ pub fn handle_editor_event( // Editing 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 + 'd') | key!(Delete) => editor.delete(), key!(Ctrl + 'l') => editor.clear(), diff --git a/src/ui/widgets/editor.rs b/src/ui/widgets/editor.rs index cae62ca..af8ac93 100644 --- a/src/ui/widgets/editor.rs +++ b/src/ui/widgets/editor.rs @@ -182,8 +182,15 @@ impl InnerEditorState { /// accordingly. fn insert_char(&mut self, frame: &mut Frame, ch: char) { self.text.insert(self.idx, ch); - self.idx += 1; - self.move_cursor_to_grapheme_boundary(); + self.idx += ch.len_utf8(); + 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); } @@ -347,6 +354,10 @@ impl EditorState { 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. pub fn backspace(&self, frame: &mut Frame) { self.0.lock().backspace(frame);