Skip to content

Commit

Permalink
Bind clipboard commands, fix bug with PasteSwap
Browse files Browse the repository at this point in the history
  • Loading branch information
justinpombrio committed May 5, 2024
1 parent 44c63fa commit 2b1e301
Show file tree
Hide file tree
Showing 4 changed files with 28 additions and 7 deletions.
5 changes: 5 additions & 0 deletions scripts/init.rhai
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,11 @@ tree_keymap.bind_key("r", "Redo", || s::redo());
tree_keymap.bind_key("m", "SaveBookmark", || s::save_bookmark('a'));
tree_keymap.bind_key("'", "GoToBookmark", || s::goto_bookmark('a'));

tree_keymap.bind_key("y", "Copy", || s::copy());
tree_keymap.bind_key("d", "Cut", || s::cut());
tree_keymap.bind_key("p", "Paste", || s::paste());
tree_keymap.bind_key("P", "PasteSwap", || s::paste_swap());

tree_keymap.bind_key("I", "Insert", || {
s::open_menu("candidate_node_selection");
let construct = s::block();
Expand Down
4 changes: 3 additions & 1 deletion src/engine/doc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,7 @@ fn execute_tree_ed(
Replace(new_node) => {
let old_node = cursor.left_node(s).ok_or(EditError::NoNodeHere)?;
if old_node.swap(s, new_node) {
*cursor = Location::after(s, new_node);
Ok(vec![(*cursor, Replace(old_node).into())])
} else {
Err(EditError::CannotPlaceNode)
Expand Down Expand Up @@ -345,8 +346,9 @@ fn execute_clipboard(
}
PasteSwap => {
let clip_node = clipboard.pop().ok_or(EditError::EmptyClipboard)?;
let doc_node = cursor.left_node(s).ok_or(EditError::NoNodeHere)?;
let doc_node = cursor.right_node(s).ok_or(EditError::NoNodeHere)?;
if doc_node.swap(s, clip_node) {
*cursor = Location::after(s, clip_node);
clipboard.push(doc_node.deep_copy(s));
Ok(vec![(*cursor, TreeEdCommand::Replace(doc_node).into())])
} else {
Expand Down
5 changes: 1 addition & 4 deletions src/engine/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,11 +6,8 @@ mod engine;
use partial_pretty_printer as ppp;
use std::default::Default;

// TODO remove allow(unused)
#[allow(unused)]
pub use command::{
BookmarkCommand, ClipboardCommand, Command, EdCommand, NavCommand, TextEdCommand,
TextNavCommand, TreeEdCommand, TreeNavCommand,
BookmarkCommand, ClipboardCommand, TextEdCommand, TextNavCommand, TreeEdCommand, TreeNavCommand,
};
pub use doc_set::{DocDisplayLabel, DocName};
pub use engine::Engine;
Expand Down
21 changes: 19 additions & 2 deletions src/runtime.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use crate::engine::{
BookmarkCommand, DocDisplayLabel, DocName, Engine, Settings, TextEdCommand, TextNavCommand,
TreeEdCommand, TreeNavCommand,
BookmarkCommand, ClipboardCommand, DocDisplayLabel, DocName, Engine, Settings, TextEdCommand,
TextNavCommand, TreeEdCommand, TreeNavCommand,
};
use crate::frontends::{Event, Frontend, Key};
use crate::keymap::{KeyLookupResult, KeyProg, Keymap, Layer, LayerManager, MenuSelectionCmd};
Expand Down Expand Up @@ -352,6 +352,15 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
self.engine.execute(TreeEdCommand::Insert(node))
}

/*************
* Clipboard *
*************/

pub fn cut(&mut self) -> Result<(), SynlessError> {
self.engine.execute(ClipboardCommand::Copy)?;
self.engine.execute(TreeEdCommand::Backspace)
}

/***********
* Private *
***********/
Expand Down Expand Up @@ -603,6 +612,14 @@ impl<F: Frontend<Style = Style> + 'static> Runtime<F> {
register!(module, rt, BookmarkCommand::Save(ch: char) as save_bookmark);
register!(module, rt, BookmarkCommand::Goto(ch: char) as goto_bookmark);

// Clipboard
register!(module, rt.cut()?);
register!(module, rt, ClipboardCommand::Copy as copy);
register!(module, rt, ClipboardCommand::Paste as paste);
register!(module, rt, ClipboardCommand::PasteSwap as paste_swap);
register!(module, rt, ClipboardCommand::Dup as dup_clipboard);
register!(module, rt, ClipboardCommand::Pop as pop_clipboard);

// Editing: Meta
register!(module, rt.undo()?);
register!(module, rt.redo()?);
Expand Down

0 comments on commit 2b1e301

Please sign in to comment.