This commit is contained in:
sxyazi 2023-07-25 00:59:25 +08:00
parent 5f2992c864
commit 2a5bce4dc6
No known key found for this signature in database
4 changed files with 26 additions and 21 deletions

View file

@ -125,8 +125,10 @@ keymap = [
{ on = [ "h" ], exec = "move -1" },
{ on = [ "l" ], exec = "move 1" },
{ on = [ "H" ], exec = "move -999" },
{ on = [ "L" ], exec = "move 999" },
{ on = [ "0" ], exec = "move -999" },
{ on = [ "$" ], exec = "move 999" },
{ on = [ "I" ], exec = "move -999", "insert" },
{ on = [ "A" ], exec = "move 999", "insert --append" },
{ on = [ "<Left>" ], exec = "move -1" },
{ on = [ "<Right>" ], exec = "move 1" },
@ -135,9 +137,9 @@ keymap = [
{ on = [ "w" ], exec = "forward" },
{ on = [ "e" ], exec = "forward --end-of-word" },
{ on = [ "d" ], exec = "delete" },
{ on = [ "c" ], exec = "delete --insert" },
{ on = [ "x" ], exec = [ "delete", "move 1 --in-operating" ] },
{ on = [ "d" ], exec = "delete --cut" },
{ on = [ "c" ], exec = "delete --cut --insert" },
{ on = [ "x" ], exec = [ "delete --cut", "move 1 --in-operating" ] },
{ on = [ "y" ], exec = [ "yank" ] },
{ on = [ "p" ], exec = [ "paste" ] },

View file

@ -208,21 +208,19 @@ impl Input {
self.move_(-1)
}
pub fn delete(&mut self, insert: bool) -> bool {
pub fn delete(&mut self, cut: bool, insert: bool) -> bool {
match self.snap().op {
InputOp::None => {
self.snap_mut().op = InputOp::Delete(insert, self.snap().cursor);
self.snap_mut().op = InputOp::Delete(cut, insert, self.snap().cursor);
false
}
InputOp::Select(start) => {
self.snap_mut().op = InputOp::Delete(insert, start);
self.snap_mut().op = InputOp::Delete(cut, insert, start);
return self.handle_op(self.snap().cursor, true).then(|| self.move_(0)).is_some();
}
InputOp::Delete(..) => {
self.move_(-(self.snap().len() as isize));
self.snap_mut().value.clear();
self.snap_mut().mode = if insert { InputMode::Insert } else { InputMode::Normal };
true
self.snap_mut().op = InputOp::Delete(cut, insert, 0);
return self.move_(self.snap().len() as isize);
}
_ => false,
}
@ -249,7 +247,7 @@ impl Input {
pub fn paste(&mut self, before: bool) -> bool {
if let Some(start) = self.snap().op.start() {
self.snap_mut().op = InputOp::Delete(false, start);
self.snap_mut().op = InputOp::Delete(false, false, start);
self.handle_op(self.snap().cursor, true);
}
@ -275,11 +273,15 @@ impl Input {
InputOp::None | InputOp::Select(_) => {
snap.cursor = cursor;
}
InputOp::Delete(insert, _) => {
InputOp::Delete(cut, insert, _) => {
let range = snap.op.range(cursor, include).unwrap();
let Range { start, end } = snap.idx(range.start)..snap.idx(range.end);
snap.value.drain(start.unwrap()..end.unwrap());
let drain = snap.value.drain(start.unwrap()..end.unwrap()).collect::<String>();
if cut {
futures::executor::block_on(async { external::clipboard_set(&drain).await.ok() });
}
snap.op = InputOp::None;
snap.mode = if insert { InputMode::Insert } else { InputMode::Normal };
snap.cursor = range.start;
@ -290,9 +292,7 @@ impl Input {
let yanked = &snap.value[start.unwrap()..end.unwrap()];
snap.op = InputOp::None;
futures::executor::block_on(async {
external::clipboard_set(yanked).await.ok();
});
futures::executor::block_on(async { external::clipboard_set(yanked).await.ok() });
}
};

View file

@ -5,7 +5,8 @@ pub enum InputOp {
#[default]
None,
Select(usize),
Delete(bool, usize),
// cut, insert, start
Delete(bool, bool, usize),
Yank(usize),
}
@ -15,7 +16,7 @@ impl InputOp {
match self {
InputOp::None => None,
InputOp::Select(s) => Some(*s),
InputOp::Delete(_, s) => Some(*s),
InputOp::Delete(.., s) => Some(*s),
InputOp::Yank(s) => Some(*s),
}
}

View file

@ -192,7 +192,9 @@ impl Executor {
"backward" => cx.input.backward(),
"forward" => cx.input.forward(exec.named.contains_key("end-of-word")),
"delete" => cx.input.delete(exec.named.contains_key("insert")),
"delete" => {
cx.input.delete(exec.named.contains_key("cut"), exec.named.contains_key("insert"))
}
"yank" => cx.input.yank(),
"paste" => cx.input.paste(exec.named.contains_key("before")),