more work on vi stuff
This commit is contained in:
@@ -1,16 +1,36 @@
|
||||
use std::iter::Peekable;
|
||||
use std::str::Chars;
|
||||
|
||||
use nix::NixPath;
|
||||
|
||||
use super::keys::{KeyEvent as E, KeyCode as K, ModKeys as M};
|
||||
use super::linebuf::TermChar;
|
||||
use super::vicmd::{Anchor, Bound, Dest, Direction, Motion, MotionBuilder, TextObj, To, Verb, VerbBuilder, ViCmd, Word};
|
||||
use super::vicmd::{Anchor, Bound, Dest, Direction, Motion, MotionBuilder, MotionCmd, RegisterName, TextObj, To, Verb, VerbBuilder, VerbCmd, ViCmd, Word};
|
||||
use crate::prelude::*;
|
||||
|
||||
pub struct CmdReplay {
|
||||
cmds: Vec<ViCmd>,
|
||||
repeat: u16
|
||||
#[derive(Debug,Clone)]
|
||||
pub enum CmdReplay {
|
||||
ModeReplay { cmds: Vec<ViCmd>, repeat: u16 },
|
||||
Single(ViCmd),
|
||||
Motion(Motion)
|
||||
}
|
||||
|
||||
impl CmdReplay {
|
||||
pub fn new(cmds: Vec<ViCmd>, repeat: u16) -> Self {
|
||||
Self { cmds, repeat }
|
||||
pub fn mode(cmds: Vec<ViCmd>, repeat: u16) -> Self {
|
||||
Self::ModeReplay { cmds, repeat }
|
||||
}
|
||||
pub fn single(cmd: ViCmd) -> Self {
|
||||
Self::Single(cmd)
|
||||
}
|
||||
pub fn motion(motion: Motion) -> Self {
|
||||
Self::Motion(motion)
|
||||
}
|
||||
}
|
||||
|
||||
pub enum CmdState {
|
||||
Pending,
|
||||
Complete,
|
||||
Invalid
|
||||
}
|
||||
|
||||
pub trait ViMode {
|
||||
@@ -18,6 +38,7 @@ pub trait ViMode {
|
||||
fn is_repeatable(&self) -> bool;
|
||||
fn as_replay(&self) -> Option<CmdReplay>;
|
||||
fn cursor_style(&self) -> String;
|
||||
fn pending_seq(&self) -> Option<String>;
|
||||
}
|
||||
|
||||
#[derive(Default,Debug)]
|
||||
@@ -53,36 +74,36 @@ impl ViMode for ViInsert {
|
||||
match key {
|
||||
E(K::Grapheme(ch), M::NONE) => {
|
||||
let ch = TermChar::from(ch);
|
||||
self.pending_cmd.set_verb(Verb::InsertChar(ch));
|
||||
self.pending_cmd.set_motion(Motion::ForwardChar);
|
||||
self.pending_cmd.set_verb(VerbCmd(1,Verb::InsertChar(ch)));
|
||||
self.pending_cmd.set_motion(MotionCmd(1,Motion::ForwardChar));
|
||||
self.register_and_return()
|
||||
}
|
||||
E(K::Char(ch), M::NONE) => {
|
||||
self.pending_cmd.set_verb(Verb::InsertChar(TermChar::from(ch)));
|
||||
self.pending_cmd.set_motion(Motion::ForwardChar);
|
||||
self.pending_cmd.set_verb(VerbCmd(1,Verb::InsertChar(TermChar::from(ch))));
|
||||
self.pending_cmd.set_motion(MotionCmd(1,Motion::ForwardChar));
|
||||
self.register_and_return()
|
||||
}
|
||||
E(K::Char('H'), M::CTRL) |
|
||||
E(K::Backspace, M::NONE) => {
|
||||
self.pending_cmd.set_verb(Verb::Delete);
|
||||
self.pending_cmd.set_motion(Motion::BackwardChar);
|
||||
self.pending_cmd.set_verb(VerbCmd(1,Verb::Delete));
|
||||
self.pending_cmd.set_motion(MotionCmd(1,Motion::BackwardChar));
|
||||
self.register_and_return()
|
||||
}
|
||||
|
||||
E(K::BackTab, M::NONE) => {
|
||||
self.pending_cmd.set_verb(Verb::CompleteBackward);
|
||||
self.pending_cmd.set_verb(VerbCmd(1,Verb::CompleteBackward));
|
||||
self.register_and_return()
|
||||
}
|
||||
|
||||
E(K::Char('I'), M::CTRL) |
|
||||
E(K::Tab, M::NONE) => {
|
||||
self.pending_cmd.set_verb(Verb::Complete);
|
||||
self.pending_cmd.set_verb(VerbCmd(1,Verb::Complete));
|
||||
self.register_and_return()
|
||||
}
|
||||
|
||||
E(K::Esc, M::NONE) => {
|
||||
self.pending_cmd.set_verb(Verb::NormalMode);
|
||||
self.pending_cmd.set_motion(Motion::BackwardChar);
|
||||
self.pending_cmd.set_verb(VerbCmd(1,Verb::NormalMode));
|
||||
self.pending_cmd.set_motion(MotionCmd(1,Motion::BackwardChar));
|
||||
self.register_and_return()
|
||||
}
|
||||
_ => common_cmds(key)
|
||||
@@ -94,230 +115,416 @@ impl ViMode for ViInsert {
|
||||
}
|
||||
|
||||
fn as_replay(&self) -> Option<CmdReplay> {
|
||||
Some(CmdReplay::new(self.cmds.clone(), self.repeat_count))
|
||||
Some(CmdReplay::mode(self.cmds.clone(), self.repeat_count))
|
||||
}
|
||||
|
||||
fn cursor_style(&self) -> String {
|
||||
"\x1b[6 q".to_string()
|
||||
}
|
||||
fn pending_seq(&self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Default,Debug)]
|
||||
pub struct ViNormal {
|
||||
pending_cmd: ViCmd,
|
||||
pending_seq: String,
|
||||
}
|
||||
|
||||
impl ViNormal {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
pub fn take_cmd(&mut self) -> ViCmd {
|
||||
std::mem::take(&mut self.pending_cmd)
|
||||
}
|
||||
pub fn clear_cmd(&mut self) {
|
||||
self.pending_cmd = ViCmd::new();
|
||||
self.pending_seq = String::new();
|
||||
}
|
||||
fn handle_pending_builder(&mut self, key: E) -> Option<ViCmd> {
|
||||
if self.pending_cmd.wants_register {
|
||||
if let E(K::Char(ch @ ('a'..='z' | 'A'..='Z')), M::NONE) = key {
|
||||
self.pending_cmd.set_register(ch);
|
||||
return None
|
||||
} else {
|
||||
self.clear_cmd();
|
||||
return None
|
||||
}
|
||||
} else if let Some(Verb::Builder(_)) = &self.pending_cmd.verb {
|
||||
todo!() // Don't have any verb builders yet, but might later
|
||||
} else if let Some(Motion::Builder(builder)) = self.pending_cmd.motion.clone() {
|
||||
match builder {
|
||||
MotionBuilder::CharSearch(direction, dest, _) => {
|
||||
if let E(K::Char(ch), M::NONE) = key {
|
||||
self.pending_cmd.set_motion(Motion::CharSearch(
|
||||
direction.unwrap(),
|
||||
dest.unwrap(),
|
||||
ch.into(),
|
||||
));
|
||||
return Some(self.take_cmd());
|
||||
} else {
|
||||
self.clear_cmd();
|
||||
return None;
|
||||
}
|
||||
}
|
||||
MotionBuilder::TextObj(_, bound) => {
|
||||
if let Some(bound) = bound {
|
||||
if let E(K::Char(ch), M::NONE) = key {
|
||||
let obj = match ch {
|
||||
'w' => TextObj::Word(Word::Normal),
|
||||
'W' => TextObj::Word(Word::Big),
|
||||
'(' | ')' => TextObj::Paren,
|
||||
'[' | ']' => TextObj::Bracket,
|
||||
'{' | '}' => TextObj::Brace,
|
||||
'<' | '>' => TextObj::Angle,
|
||||
'"' => TextObj::DoubleQuote,
|
||||
'\'' => TextObj::SingleQuote,
|
||||
'`' => TextObj::BacktickQuote,
|
||||
_ => TextObj::Custom(ch),
|
||||
};
|
||||
self.pending_cmd.set_motion(Motion::TextObj(obj, bound));
|
||||
return Some(self.take_cmd());
|
||||
} else {
|
||||
self.clear_cmd();
|
||||
return None;
|
||||
}
|
||||
} else if let E(K::Char(ch), M::NONE) = key {
|
||||
let bound = match ch {
|
||||
'i' => Bound::Inside,
|
||||
'a' => Bound::Around,
|
||||
_ => {
|
||||
self.clear_cmd();
|
||||
return None;
|
||||
}
|
||||
};
|
||||
self.pending_cmd.set_motion(Motion::Builder(MotionBuilder::TextObj(None, Some(bound))));
|
||||
return None;
|
||||
} else {
|
||||
self.clear_cmd();
|
||||
return None;
|
||||
}
|
||||
}
|
||||
pub fn take_cmd(&mut self) -> String {
|
||||
std::mem::take(&mut self.pending_seq)
|
||||
}
|
||||
fn validate_combination(&self, verb: Option<&Verb>, motion: Option<&Motion>) -> CmdState {
|
||||
if verb.is_none() {
|
||||
match motion {
|
||||
Some(Motion::TextObj(_,_)) => return CmdState::Invalid,
|
||||
Some(_) => return CmdState::Complete,
|
||||
None => return CmdState::Pending
|
||||
}
|
||||
}
|
||||
if verb.is_some() && motion.is_none() {
|
||||
match verb.unwrap() {
|
||||
Verb::Put(_) |
|
||||
Verb::DeleteChar(_) => CmdState::Complete,
|
||||
_ => CmdState::Pending
|
||||
}
|
||||
} else {
|
||||
CmdState::Complete
|
||||
}
|
||||
}
|
||||
pub fn parse_count(&self, chars: &mut Peekable<Chars<'_>>) -> Option<usize> {
|
||||
let mut count = String::new();
|
||||
let Some(_digit @ '1'..='9') = chars.peek() else {
|
||||
return None
|
||||
};
|
||||
count.push(chars.next().unwrap());
|
||||
while let Some(_digit @ '0'..='9') = chars.peek() {
|
||||
count.push(chars.next().unwrap());
|
||||
}
|
||||
if !count.is_empty() {
|
||||
count.parse::<usize>().ok()
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
/// End the parse and clear the pending sequence
|
||||
pub fn quit_parse(&mut self) -> Option<ViCmd> {
|
||||
flog!(WARN, "exiting parse early with sequence: {}",self.pending_seq);
|
||||
self.clear_cmd();
|
||||
None
|
||||
}
|
||||
pub fn try_parse(&mut self, ch: char) -> Option<ViCmd> {
|
||||
self.pending_seq.push(ch);
|
||||
flog!(DEBUG, self.pending_seq);
|
||||
let mut chars = self.pending_seq.chars().peekable();
|
||||
|
||||
let register = 'reg_parse: {
|
||||
let mut chars_clone = chars.clone();
|
||||
let count = self.parse_count(&mut chars_clone);
|
||||
|
||||
let Some('"') = chars_clone.next() else {
|
||||
break 'reg_parse RegisterName::default()
|
||||
};
|
||||
|
||||
let Some(reg_name) = chars_clone.next() else {
|
||||
return None // Pending register name
|
||||
};
|
||||
match reg_name {
|
||||
'a'..='z' |
|
||||
'A'..='Z' => { /* proceed */ }
|
||||
_ => return self.quit_parse()
|
||||
}
|
||||
|
||||
chars = chars_clone;
|
||||
RegisterName::new(Some(reg_name), count)
|
||||
};
|
||||
|
||||
let verb = 'verb_parse: {
|
||||
let mut chars_clone = chars.clone();
|
||||
let count = self.parse_count(&mut chars_clone).unwrap_or(1);
|
||||
|
||||
let Some(ch) = chars_clone.next() else {
|
||||
break 'verb_parse None
|
||||
};
|
||||
flog!(DEBUG, "parsing verb char '{}'",ch);
|
||||
match ch {
|
||||
'.' => {
|
||||
return Some(
|
||||
ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::RepeatLast)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
}
|
||||
)
|
||||
}
|
||||
'x' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::DeleteChar(Anchor::After)));
|
||||
}
|
||||
'X' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::DeleteChar(Anchor::Before)));
|
||||
}
|
||||
'p' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Put(Anchor::After)));
|
||||
}
|
||||
'P' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Put(Anchor::Before)));
|
||||
}
|
||||
'r' => {
|
||||
let Some(ch) = chars_clone.next() else {
|
||||
return None
|
||||
};
|
||||
return Some(
|
||||
ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::ReplaceChar(ch))),
|
||||
motion: Some(MotionCmd(count, Motion::ForwardChar)),
|
||||
raw_seq: self.take_cmd()
|
||||
}
|
||||
)
|
||||
}
|
||||
'u' => {
|
||||
return Some(
|
||||
ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::Undo)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd()
|
||||
}
|
||||
)
|
||||
}
|
||||
'o' => {
|
||||
return Some(
|
||||
ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertModeLineBreak(Anchor::After))),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd()
|
||||
}
|
||||
)
|
||||
}
|
||||
'O' => {
|
||||
return Some(
|
||||
ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertModeLineBreak(Anchor::Before))),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd()
|
||||
}
|
||||
)
|
||||
}
|
||||
'a' => {
|
||||
return Some(
|
||||
ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||
motion: Some(MotionCmd(1, Motion::ForwardChar)),
|
||||
raw_seq: self.take_cmd()
|
||||
}
|
||||
)
|
||||
}
|
||||
'A' => {
|
||||
return Some(
|
||||
ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||
motion: Some(MotionCmd(1, Motion::EndOfLine)),
|
||||
raw_seq: self.take_cmd()
|
||||
}
|
||||
)
|
||||
}
|
||||
'i' => {
|
||||
return Some(
|
||||
ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd()
|
||||
}
|
||||
)
|
||||
}
|
||||
'I' => {
|
||||
return Some(
|
||||
ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||
motion: Some(MotionCmd(1, Motion::BeginningOfFirstWord)),
|
||||
raw_seq: self.take_cmd()
|
||||
}
|
||||
)
|
||||
}
|
||||
'y' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Yank))
|
||||
}
|
||||
'd' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Delete))
|
||||
}
|
||||
'c' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Change))
|
||||
}
|
||||
'=' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Equalize))
|
||||
}
|
||||
_ => break 'verb_parse None
|
||||
}
|
||||
};
|
||||
|
||||
let motion = 'motion_parse: {
|
||||
let mut chars_clone = chars.clone();
|
||||
let count = self.parse_count(&mut chars_clone).unwrap_or(1);
|
||||
|
||||
let Some(ch) = chars_clone.next() else {
|
||||
break 'motion_parse None
|
||||
};
|
||||
flog!(DEBUG, "parsing motion char '{}'",ch);
|
||||
match (ch, &verb) {
|
||||
('d', Some(VerbCmd(_,Verb::Delete))) |
|
||||
('c', Some(VerbCmd(_,Verb::Change))) |
|
||||
('y', Some(VerbCmd(_,Verb::Yank))) |
|
||||
('>', Some(VerbCmd(_,Verb::Indent))) |
|
||||
('<', Some(VerbCmd(_,Verb::Dedent))) => break 'motion_parse Some(MotionCmd(count, Motion::WholeLine)),
|
||||
_ => {}
|
||||
}
|
||||
match ch {
|
||||
'g' => {
|
||||
if let Some(ch) = chars_clone.peek() {
|
||||
match ch {
|
||||
'g' => {
|
||||
chars_clone.next();
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::BeginningOfBuffer))
|
||||
}
|
||||
'e' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::BackwardWord(To::End, Word::Normal)));
|
||||
}
|
||||
'E' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::BackwardWord(To::End, Word::Big)));
|
||||
}
|
||||
_ => return self.quit_parse()
|
||||
}
|
||||
} else {
|
||||
break 'motion_parse None
|
||||
}
|
||||
}
|
||||
'|' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(1, Motion::ToColumn(count)));
|
||||
}
|
||||
'0' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::BeginningOfLine));
|
||||
}
|
||||
'$' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::EndOfLine));
|
||||
}
|
||||
'k' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::LineUp));
|
||||
}
|
||||
'j' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::LineDown));
|
||||
}
|
||||
'h' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::BackwardChar));
|
||||
}
|
||||
'l' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ForwardChar));
|
||||
}
|
||||
'w' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ForwardWord(To::Start, Word::Normal)));
|
||||
}
|
||||
'W' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ForwardWord(To::Start, Word::Big)));
|
||||
}
|
||||
'e' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ForwardWord(To::End, Word::Normal)));
|
||||
}
|
||||
'E' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ForwardWord(To::End, Word::Big)));
|
||||
}
|
||||
'b' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::BackwardWord(To::Start, Word::Normal)));
|
||||
}
|
||||
'B' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::BackwardWord(To::Start, Word::Big)));
|
||||
}
|
||||
ch if ch == 'i' || ch == 'a' => {
|
||||
let bound = match ch {
|
||||
'i' => Bound::Inside,
|
||||
'a' => Bound::Around,
|
||||
_ => unreachable!()
|
||||
};
|
||||
if chars_clone.peek().is_none() {
|
||||
break 'motion_parse None
|
||||
}
|
||||
let obj = match chars_clone.next().unwrap() {
|
||||
'w' => TextObj::Word(Word::Normal),
|
||||
'W' => TextObj::Word(Word::Big),
|
||||
'"' => TextObj::DoubleQuote,
|
||||
'\'' => TextObj::SingleQuote,
|
||||
'(' | ')' | 'b' => TextObj::Paren,
|
||||
'{' | '}' | 'B' => TextObj::Brace,
|
||||
'[' | ']' => TextObj::Bracket,
|
||||
'<' | '>' => TextObj::Angle,
|
||||
_ => return self.quit_parse()
|
||||
};
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::TextObj(obj, bound)))
|
||||
}
|
||||
_ => return self.quit_parse(),
|
||||
}
|
||||
};
|
||||
|
||||
if chars.peek().is_some() {
|
||||
flog!(WARN, "Unused characters in Vi command parse!");
|
||||
flog!(WARN, "{:?}",chars)
|
||||
}
|
||||
|
||||
let verb_ref = verb.as_ref().map(|v| &v.1);
|
||||
let motion_ref = motion.as_ref().map(|m| &m.1);
|
||||
|
||||
match self.validate_combination(verb_ref, motion_ref) {
|
||||
CmdState::Complete => {
|
||||
let cmd = Some(
|
||||
ViCmd {
|
||||
register,
|
||||
verb,
|
||||
motion,
|
||||
raw_seq: std::mem::take(&mut self.pending_seq)
|
||||
}
|
||||
);
|
||||
flog!(DEBUG, cmd);
|
||||
cmd
|
||||
}
|
||||
CmdState::Pending => {
|
||||
flog!(DEBUG, "pending sequence: {}", self.pending_seq);
|
||||
None
|
||||
}
|
||||
CmdState::Invalid => {
|
||||
flog!(DEBUG, "invalid sequence: {}",self.pending_seq);
|
||||
self.pending_seq.clear();
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ViMode for ViNormal {
|
||||
fn handle_key(&mut self, key: E) -> Option<ViCmd> {
|
||||
if let E(K::Char(ch),M::NONE) = key {
|
||||
self.pending_cmd.append_seq_char(ch);
|
||||
}
|
||||
if self.pending_cmd.is_building() {
|
||||
return self.handle_pending_builder(key)
|
||||
}
|
||||
flog!(DEBUG, key);
|
||||
match key {
|
||||
E(K::Char(digit @ '0'..='9'), M::NONE) => self.pending_cmd.append_digit(digit),
|
||||
E(K::Char('"'),M::NONE) => {
|
||||
if self.pending_cmd.is_empty() {
|
||||
if self.pending_cmd.register().name().is_none() {
|
||||
self.pending_cmd.wants_register = true;
|
||||
} else {
|
||||
self.clear_cmd();
|
||||
E(K::Char(ch), M::NONE) => self.try_parse(ch),
|
||||
E(K::Char('R'), M::CTRL) => {
|
||||
let mut chars = self.pending_seq.chars().peekable();
|
||||
let count = self.parse_count(&mut chars).unwrap_or(1);
|
||||
Some(
|
||||
ViCmd {
|
||||
register: RegisterName::default(),
|
||||
verb: Some(VerbCmd(count,Verb::Redo)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd()
|
||||
}
|
||||
} else {
|
||||
)
|
||||
}
|
||||
E(K::Esc, M::NONE) => {
|
||||
self.clear_cmd();
|
||||
None
|
||||
}
|
||||
_ => {
|
||||
if let Some(cmd) = common_cmds(key) {
|
||||
self.clear_cmd();
|
||||
}
|
||||
return None
|
||||
}
|
||||
E(K::Char('i'),M::NONE) if self.pending_cmd.verb().is_some() => {
|
||||
self.pending_cmd.set_motion(Motion::Builder(MotionBuilder::TextObj(None, Some(Bound::Inside))));
|
||||
}
|
||||
E(K::Char('a'),M::NONE) if self.pending_cmd.verb().is_some() => {
|
||||
self.pending_cmd.set_motion(Motion::Builder(MotionBuilder::TextObj(None, Some(Bound::Around))));
|
||||
}
|
||||
E(K::Char('h'),M::NONE) => self.pending_cmd.set_motion(Motion::BackwardChar),
|
||||
E(K::Char('j'),M::NONE) => self.pending_cmd.set_motion(Motion::LineDown),
|
||||
E(K::Char('k'),M::NONE) => self.pending_cmd.set_motion(Motion::LineUp),
|
||||
E(K::Char('l'),M::NONE) => self.pending_cmd.set_motion(Motion::ForwardChar),
|
||||
E(K::Char('w'),M::NONE) => self.pending_cmd.set_motion(Motion::ForwardWord(To::Start, Word::Normal)),
|
||||
E(K::Char('W'),M::NONE) => self.pending_cmd.set_motion(Motion::ForwardWord(To::Start, Word::Big)),
|
||||
E(K::Char('e'),M::NONE) => self.pending_cmd.set_motion(Motion::ForwardWord(To::End, Word::Normal)),
|
||||
E(K::Char('E'),M::NONE) => self.pending_cmd.set_motion(Motion::ForwardWord(To::End, Word::Big)),
|
||||
E(K::Char('b'),M::NONE) => self.pending_cmd.set_motion(Motion::BackwardWord(Word::Normal)),
|
||||
E(K::Char('B'),M::NONE) => self.pending_cmd.set_motion(Motion::BackwardWord(Word::Big)),
|
||||
E(K::Char('x'),M::NONE) => self.pending_cmd.set_verb(Verb::DeleteChar(Anchor::After)),
|
||||
E(K::Char('X'),M::NONE) => self.pending_cmd.set_verb(Verb::DeleteChar(Anchor::Before)),
|
||||
E(K::Char('d'),M::NONE) => {
|
||||
if self.pending_cmd.verb().is_none() {
|
||||
self.pending_cmd.set_verb(Verb::Delete)
|
||||
} else if let Some(verb) = self.pending_cmd.verb() {
|
||||
if verb == &Verb::Delete {
|
||||
self.pending_cmd.set_motion(Motion::WholeLine);
|
||||
} else {
|
||||
self.clear_cmd();
|
||||
}
|
||||
Some(cmd)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
E(K::Char('c'),M::NONE) => {
|
||||
if self.pending_cmd.verb().is_none() {
|
||||
self.pending_cmd.set_verb(Verb::Change)
|
||||
} else if let Some(verb) = self.pending_cmd.verb() {
|
||||
if verb == &Verb::Change {
|
||||
self.pending_cmd.set_motion(Motion::WholeLine);
|
||||
} else {
|
||||
self.clear_cmd();
|
||||
}
|
||||
}
|
||||
}
|
||||
E(K::Char('y'),M::NONE) => {
|
||||
if self.pending_cmd.verb().is_none() {
|
||||
self.pending_cmd.set_verb(Verb::Yank)
|
||||
} else if let Some(verb) = self.pending_cmd.verb() {
|
||||
if verb == &Verb::Yank {
|
||||
self.pending_cmd.set_motion(Motion::WholeLine);
|
||||
} else {
|
||||
self.clear_cmd();
|
||||
}
|
||||
}
|
||||
}
|
||||
E(K::Char('p'),M::NONE) => self.pending_cmd.set_verb(Verb::Put(Anchor::After)),
|
||||
E(K::Char('P'),M::NONE) => self.pending_cmd.set_verb(Verb::Put(Anchor::Before)),
|
||||
E(K::Char('D'),M::NONE) => {
|
||||
self.pending_cmd.set_verb(Verb::Delete);
|
||||
self.pending_cmd.set_motion(Motion::EndOfLine);
|
||||
}
|
||||
E(K::Char('f'),M::NONE) => {
|
||||
let builder = MotionBuilder::CharSearch(
|
||||
Some(Direction::Forward),
|
||||
Some(Dest::On),
|
||||
None
|
||||
);
|
||||
self.pending_cmd.set_motion(Motion::Builder(builder));
|
||||
}
|
||||
E(K::Char('F'),M::NONE) => {
|
||||
let builder = MotionBuilder::CharSearch(
|
||||
Some(Direction::Backward),
|
||||
Some(Dest::On),
|
||||
None
|
||||
);
|
||||
self.pending_cmd.set_motion(Motion::Builder(builder));
|
||||
}
|
||||
E(K::Char('t'),M::NONE) => {
|
||||
let builder = MotionBuilder::CharSearch(
|
||||
Some(Direction::Forward),
|
||||
Some(Dest::Before),
|
||||
None
|
||||
);
|
||||
self.pending_cmd.set_motion(Motion::Builder(builder));
|
||||
}
|
||||
E(K::Char('T'),M::NONE) => {
|
||||
let builder = MotionBuilder::CharSearch(
|
||||
Some(Direction::Backward),
|
||||
Some(Dest::Before),
|
||||
None
|
||||
);
|
||||
self.pending_cmd.set_motion(Motion::Builder(builder));
|
||||
}
|
||||
E(K::Char('i'),M::NONE) => {
|
||||
self.pending_cmd.set_verb(Verb::InsertMode);
|
||||
}
|
||||
E(K::Char('I'),M::NONE) => {
|
||||
self.pending_cmd.set_verb(Verb::InsertMode);
|
||||
self.pending_cmd.set_motion(Motion::BeginningOfFirstWord);
|
||||
}
|
||||
E(K::Char('a'),M::NONE) => {
|
||||
self.pending_cmd.set_verb(Verb::InsertMode);
|
||||
self.pending_cmd.set_motion(Motion::ForwardChar);
|
||||
}
|
||||
E(K::Char('A'),M::NONE) => {
|
||||
self.pending_cmd.set_verb(Verb::InsertMode);
|
||||
self.pending_cmd.set_motion(Motion::EndOfLine);
|
||||
}
|
||||
_ => return common_cmds(key)
|
||||
}
|
||||
if self.pending_cmd.is_complete() {
|
||||
Some(self.take_cmd())
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
|
||||
@@ -332,28 +539,26 @@ impl ViMode for ViNormal {
|
||||
fn cursor_style(&self) -> String {
|
||||
"\x1b[2 q".to_string()
|
||||
}
|
||||
|
||||
fn pending_seq(&self) -> Option<String> {
|
||||
Some(self.pending_seq.clone())
|
||||
}
|
||||
}
|
||||
|
||||
pub fn common_cmds(key: E) -> Option<ViCmd> {
|
||||
let mut pending_cmd = ViCmd::new();
|
||||
match key {
|
||||
E(K::Home, M::NONE) => pending_cmd.set_motion(Motion::BeginningOfLine),
|
||||
E(K::End, M::NONE) => pending_cmd.set_motion(Motion::EndOfLine),
|
||||
E(K::Left, M::NONE) => pending_cmd.set_motion(Motion::BackwardChar),
|
||||
E(K::Right, M::NONE) => pending_cmd.set_motion(Motion::ForwardChar),
|
||||
E(K::Up, M::NONE) => pending_cmd.set_motion(Motion::LineUp),
|
||||
E(K::Down, M::NONE) => pending_cmd.set_motion(Motion::LineDown),
|
||||
E(K::Enter, M::NONE) => pending_cmd.set_verb(Verb::AcceptLine),
|
||||
E(K::Char('D'), M::CTRL) => pending_cmd.set_verb(Verb::EndOfFile),
|
||||
E(K::Home, M::NONE) => pending_cmd.set_motion(MotionCmd(1,Motion::BeginningOfLine)),
|
||||
E(K::End, M::NONE) => pending_cmd.set_motion(MotionCmd(1,Motion::EndOfLine)),
|
||||
E(K::Left, M::NONE) => pending_cmd.set_motion(MotionCmd(1,Motion::BackwardChar)),
|
||||
E(K::Right, M::NONE) => pending_cmd.set_motion(MotionCmd(1,Motion::ForwardChar)),
|
||||
E(K::Up, M::NONE) => pending_cmd.set_motion(MotionCmd(1,Motion::LineUp)),
|
||||
E(K::Down, M::NONE) => pending_cmd.set_motion(MotionCmd(1,Motion::LineDown)),
|
||||
E(K::Enter, M::NONE) => pending_cmd.set_verb(VerbCmd(1,Verb::AcceptLine)),
|
||||
E(K::Char('D'), M::CTRL) => pending_cmd.set_verb(VerbCmd(1,Verb::EndOfFile)),
|
||||
E(K::Delete, M::NONE) => pending_cmd.set_verb(VerbCmd(1,Verb::DeleteChar(Anchor::After))),
|
||||
E(K::Backspace, M::NONE) |
|
||||
E(K::Char('H'), M::CTRL) => {
|
||||
pending_cmd.set_verb(Verb::Delete);
|
||||
pending_cmd.set_motion(Motion::BackwardChar);
|
||||
}
|
||||
E(K::Delete, M::NONE) => {
|
||||
pending_cmd.set_verb(Verb::Delete);
|
||||
pending_cmd.set_motion(Motion::ForwardChar);
|
||||
}
|
||||
E(K::Char('H'), M::CTRL) => pending_cmd.set_verb(VerbCmd(1,Verb::DeleteChar(Anchor::Before))),
|
||||
_ => return None
|
||||
}
|
||||
Some(pending_cmd)
|
||||
|
||||
Reference in New Issue
Block a user