feat: add --big parameter to forward and backward commands

This commit is contained in:
darcy 2025-01-03 11:57:14 +11:00 committed by sxyazi
parent e0cbe78377
commit 90ccc63244
No known key found for this signature in database
3 changed files with 40 additions and 11 deletions

View file

@ -249,11 +249,14 @@ keymap = [
{ on = "<C-f>", run = "move 1", desc = "Move forward a character" },
# Word-wise movement
{ on = "b", run = "backward", desc = "Move back to the start of the current or previous word" },
{ on = "w", run = "forward", desc = "Move forward to the start of the next word" },
{ on = "e", run = "forward --end-of-word", desc = "Move forward to the end of the current or next word" },
{ on = "<A-b>", run = "backward", desc = "Move back to the start of the current or previous word" },
{ on = "<A-f>", run = "forward --end-of-word", desc = "Move forward to the end of the current or next word" },
{ on = "b", run = "backward", desc = "Move back to the start of the current or previous word" },
{ on = "w", run = "forward", desc = "Move forward to the start of the next word" },
{ on = "e", run = "forward --end-of-word", desc = "Move forward to the end of the current or next word" },
{ on = "B", run = "backward --big", desc = "Move back to the start of the current or previous WORD" },
{ on = "W", run = "forward --big", desc = "Move forward to the start of the next WORD" },
{ on = "E", run = "forward --big --end-of-word", desc = "Move forward to the end of the current or next WORD" },
{ on = "<A-b>", run = "backward", desc = "Move back to the start of the current or previous word" },
{ on = "<A-f>", run = "forward --end-of-word", desc = "Move forward to the end of the current or next word" },
# Line-wise movement
{ on = "0", run = "move bol", desc = "Move to the BOL" },

View file

@ -2,8 +2,18 @@ use yazi_shared::{CharKind, event::CmdCow};
use crate::input::Input;
struct Opt {
big: bool,
}
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { big: c.bool("big") }
}
}
impl Input {
pub fn backward(&mut self, _: CmdCow) {
#[yazi_codegen::command]
pub fn backward(&mut self, opt: Opt) {
let snap = self.snap();
if snap.cursor == 0 {
return self.move_(0);
@ -14,7 +24,12 @@ impl Input {
let mut prev = CharKind::new(it.next().unwrap().1);
for (i, c) in it {
let c = CharKind::new(c);
if prev != CharKind::Space && prev != c {
let new_char_kind = if opt.big {
(c == CharKind::Space) != (prev == CharKind::Space)
} else {
c != prev
};
if prev != CharKind::Space && new_char_kind {
return self.move_(-(i as isize));
}
prev = c;

View file

@ -4,10 +4,16 @@ use crate::input::{Input, op::InputOp};
struct Opt {
end_of_word: bool,
big: bool,
}
impl From<CmdCow> for Opt {
fn from(c: CmdCow) -> Self { Self { end_of_word: c.bool("end-of-word") } }
fn from(c: CmdCow) -> Self {
Self {
end_of_word: c.bool("end-of-word"),
big: c.bool("big"),
}
}
}
impl Input {
@ -22,10 +28,15 @@ impl Input {
for (i, c) in it {
let c = CharKind::new(c);
let b = if opt.end_of_word {
prev != CharKind::Space && prev != c && i != 1
let new_char_kind = if opt.big {
(c == CharKind::Space) != (prev == CharKind::Space)
} else {
c != CharKind::Space && c != prev
c != prev
};
let b = if opt.end_of_word {
prev != CharKind::Space && new_char_kind && i != 1
} else {
c != CharKind::Space && new_char_kind
};
if b && !matches!(snap.op, InputOp::None | InputOp::Select(_)) {
return self.move_(i as isize);