Added ex mode to line editor, a 'keymap' builtin, and a zsh-like widget system using ':!<shellcmd>' ex mode commands
This commit is contained in:
381
src/readline/vimode/ex.rs
Normal file
381
src/readline/vimode/ex.rs
Normal file
@@ -0,0 +1,381 @@
|
||||
use std::iter::Peekable;
|
||||
use std::path::PathBuf;
|
||||
use std::str::Chars;
|
||||
|
||||
use itertools::Itertools;
|
||||
|
||||
use crate::bitflags;
|
||||
use crate::libsh::error::{ShErr, ShErrKind, ShResult};
|
||||
use crate::readline::keys::KeyEvent;
|
||||
use crate::readline::linebuf::LineBuf;
|
||||
use crate::readline::vicmd::{
|
||||
Anchor, CmdFlags, Motion, MotionCmd, ReadSrc, RegisterName, To, Val, Verb, VerbCmd,
|
||||
ViCmd, WriteDest,
|
||||
};
|
||||
use crate::readline::vimode::{ModeReport, ViInsert, ViMode};
|
||||
use crate::state::write_meta;
|
||||
|
||||
bitflags! {
|
||||
#[derive(Debug,Clone,Copy,PartialEq,Eq)]
|
||||
pub struct SubFlags: u16 {
|
||||
const GLOBAL = 1 << 0; // g
|
||||
const CONFIRM = 1 << 1; // c (probably not implemented)
|
||||
const IGNORE_CASE = 1 << 2; // i
|
||||
const NO_IGNORE_CASE = 1 << 3; // I
|
||||
const SHOW_COUNT = 1 << 4; // n
|
||||
const PRINT_RESULT = 1 << 5; // p
|
||||
const PRINT_NUMBERED = 1 << 6; // #
|
||||
const PRINT_LEFT_ALIGN = 1 << 7; // l
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
struct ExEditor {
|
||||
buf: LineBuf,
|
||||
mode: ViInsert
|
||||
}
|
||||
|
||||
impl ExEditor {
|
||||
pub fn clear(&mut self) {
|
||||
*self = Self::default()
|
||||
}
|
||||
pub fn handle_key(&mut self, key: KeyEvent) -> ShResult<()> {
|
||||
let Some(cmd) = self.mode.handle_key(key) else {
|
||||
return Ok(())
|
||||
};
|
||||
self.buf.exec_cmd(cmd)
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct ViEx {
|
||||
pending_cmd: ExEditor,
|
||||
}
|
||||
|
||||
impl ViEx {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
|
||||
impl ViMode for ViEx {
|
||||
// Ex mode can return errors, so we use this fallible method instead of the normal one
|
||||
fn handle_key_fallible(&mut self, key: KeyEvent) -> ShResult<Option<ViCmd>> {
|
||||
use crate::readline::keys::{KeyEvent as E, KeyCode as C, ModKeys as M};
|
||||
log::debug!("[ViEx] handle_key_fallible: key={:?}", key);
|
||||
match key {
|
||||
E(C::Char('\r'), M::NONE) |
|
||||
E(C::Enter, M::NONE) => {
|
||||
let input = self.pending_cmd.buf.as_str();
|
||||
log::debug!("[ViEx] Enter pressed, pending_cmd={:?}", input);
|
||||
match parse_ex_cmd(input) {
|
||||
Ok(cmd) => {
|
||||
log::debug!("[ViEx] parse_ex_cmd Ok: {:?}", cmd);
|
||||
Ok(cmd)
|
||||
}
|
||||
Err(e) => {
|
||||
log::debug!("[ViEx] parse_ex_cmd Err: {:?}", e);
|
||||
let msg = e.unwrap_or(format!("Not an editor command: {}", input));
|
||||
write_meta(|m| m.post_system_message(msg.clone()));
|
||||
Err(ShErr::simple(ShErrKind::ParseErr, msg))
|
||||
}
|
||||
}
|
||||
}
|
||||
E(C::Char('C'), M::CTRL) => {
|
||||
log::debug!("[ViEx] Ctrl-C, clearing");
|
||||
self.pending_cmd.clear();
|
||||
Ok(None)
|
||||
}
|
||||
E(C::Esc, M::NONE) => {
|
||||
log::debug!("[ViEx] Esc, returning to normal mode");
|
||||
Ok(Some(ViCmd {
|
||||
register: RegisterName::default(),
|
||||
verb: Some(VerbCmd(1, Verb::NormalMode)),
|
||||
motion: None,
|
||||
flags: CmdFlags::empty(),
|
||||
raw_seq: "".into(),
|
||||
}))
|
||||
}
|
||||
_ => {
|
||||
log::debug!("[ViEx] forwarding key to ExEditor");
|
||||
self.pending_cmd.handle_key(key).map(|_| None)
|
||||
}
|
||||
}
|
||||
}
|
||||
fn handle_key(&mut self, key: KeyEvent) -> Option<ViCmd> {
|
||||
let result = self.handle_key_fallible(key);
|
||||
log::debug!("[ViEx] handle_key result: {:?}", result);
|
||||
result.ok().flatten()
|
||||
}
|
||||
fn is_repeatable(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn as_replay(&self) -> Option<super::CmdReplay> {
|
||||
None
|
||||
}
|
||||
|
||||
fn cursor_style(&self) -> String {
|
||||
"\x1b[3 q".to_string()
|
||||
}
|
||||
|
||||
fn pending_seq(&self) -> Option<String> {
|
||||
Some(self.pending_cmd.buf.as_str().to_string())
|
||||
}
|
||||
|
||||
fn pending_cursor(&self) -> Option<usize> {
|
||||
Some(self.pending_cmd.buf.cursor.get())
|
||||
}
|
||||
|
||||
fn move_cursor_on_undo(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn clamp_cursor(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn hist_scroll_start_pos(&self) -> Option<To> {
|
||||
None
|
||||
}
|
||||
|
||||
fn report_mode(&self) -> super::ModeReport {
|
||||
ModeReport::Ex
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_ex_cmd(raw: &str) -> Result<Option<ViCmd>,Option<String>> {
|
||||
let raw = raw.trim();
|
||||
if raw.is_empty() {
|
||||
return Ok(None)
|
||||
}
|
||||
let mut chars = raw.chars().peekable();
|
||||
let (verb, motion) = {
|
||||
if chars.peek() == Some(&'g') {
|
||||
let mut cmd_name = String::new();
|
||||
while let Some(ch) = chars.peek() {
|
||||
if ch.is_alphanumeric() {
|
||||
cmd_name.push(*ch);
|
||||
chars.next();
|
||||
} else {
|
||||
break
|
||||
}
|
||||
}
|
||||
if !"global".starts_with(&cmd_name) {
|
||||
return Err(None)
|
||||
}
|
||||
let Some(result) = parse_global(&mut chars)? else { return Ok(None) };
|
||||
(Some(VerbCmd(1,result.1)), Some(MotionCmd(1,result.0)))
|
||||
} else {
|
||||
(parse_ex_command(&mut chars)?.map(|v| VerbCmd(1, v)), None)
|
||||
}
|
||||
};
|
||||
|
||||
Ok(Some(ViCmd {
|
||||
register: RegisterName::default(),
|
||||
verb,
|
||||
motion,
|
||||
raw_seq: raw.to_string(),
|
||||
flags: CmdFlags::EXIT_CUR_MODE,
|
||||
}))
|
||||
}
|
||||
|
||||
/// Unescape shell command arguments
|
||||
fn unescape_shell_cmd(cmd: &str) -> String {
|
||||
// The pest grammar uses double quotes for vicut commands
|
||||
// So shell commands need to escape double quotes
|
||||
// We will be removing a single layer of escaping from double quotes
|
||||
let mut result = String::new();
|
||||
let mut chars = cmd.chars().peekable();
|
||||
while let Some(ch) = chars.next() {
|
||||
if ch == '\\' {
|
||||
if let Some(&'"') = chars.peek() {
|
||||
chars.next();
|
||||
result.push('"');
|
||||
} else {
|
||||
result.push(ch);
|
||||
}
|
||||
} else {
|
||||
result.push(ch);
|
||||
}
|
||||
}
|
||||
result
|
||||
}
|
||||
|
||||
fn parse_ex_command(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>,Option<String>> {
|
||||
let mut cmd_name = String::new();
|
||||
|
||||
while let Some(ch) = chars.peek() {
|
||||
if ch == &'!' {
|
||||
cmd_name.push(*ch);
|
||||
chars.next();
|
||||
break
|
||||
} else if !ch.is_alphanumeric() {
|
||||
break
|
||||
}
|
||||
cmd_name.push(*ch);
|
||||
chars.next();
|
||||
}
|
||||
|
||||
match cmd_name.as_str() {
|
||||
"!" => {
|
||||
let cmd = chars.collect::<String>();
|
||||
let cmd = unescape_shell_cmd(&cmd);
|
||||
Ok(Some(Verb::ShellCmd(cmd)))
|
||||
}
|
||||
"normal!" => parse_normal(chars),
|
||||
_ if "delete".starts_with(&cmd_name) => Ok(Some(Verb::Delete)),
|
||||
_ if "yank".starts_with(&cmd_name) => Ok(Some(Verb::Yank)),
|
||||
_ if "put".starts_with(&cmd_name) => Ok(Some(Verb::Put(Anchor::After))),
|
||||
_ if "read".starts_with(&cmd_name) => parse_read(chars),
|
||||
_ if "write".starts_with(&cmd_name) => parse_write(chars),
|
||||
_ if "substitute".starts_with(&cmd_name) => parse_substitute(chars),
|
||||
_ => Err(None)
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_normal(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>,Option<String>> {
|
||||
chars.peeking_take_while(|c| c.is_whitespace()).for_each(drop);
|
||||
|
||||
let seq: String = chars.collect();
|
||||
Ok(Some(Verb::Normal(seq)))
|
||||
}
|
||||
|
||||
fn parse_read(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>,Option<String>> {
|
||||
chars.peeking_take_while(|c| c.is_whitespace()).for_each(drop);
|
||||
|
||||
let is_shell_read = if chars.peek() == Some(&'!') { chars.next(); true } else { false };
|
||||
let arg: String = chars.collect();
|
||||
|
||||
if arg.trim().is_empty() {
|
||||
return Err(Some("Expected file path or shell command after ':r'".into()))
|
||||
}
|
||||
|
||||
if is_shell_read {
|
||||
Ok(Some(Verb::Read(ReadSrc::Cmd(arg))))
|
||||
} else {
|
||||
let arg_path = get_path(arg.trim());
|
||||
Ok(Some(Verb::Read(ReadSrc::File(arg_path))))
|
||||
}
|
||||
}
|
||||
|
||||
fn get_path(path: &str) -> PathBuf {
|
||||
if let Some(stripped) = path.strip_prefix("~/")
|
||||
&& let Some(home) = std::env::var_os("HOME") {
|
||||
return PathBuf::from(home).join(stripped)
|
||||
}
|
||||
if path == "~"
|
||||
&& let Some(home) = std::env::var_os("HOME") {
|
||||
return PathBuf::from(home)
|
||||
}
|
||||
PathBuf::from(path)
|
||||
}
|
||||
|
||||
fn parse_write(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>,Option<String>> {
|
||||
chars.peeking_take_while(|c| c.is_whitespace()).for_each(drop);
|
||||
|
||||
let is_shell_write = chars.peek() == Some(&'!');
|
||||
if is_shell_write {
|
||||
chars.next(); // consume '!'
|
||||
let arg: String = chars.collect();
|
||||
return Ok(Some(Verb::Write(WriteDest::Cmd(arg))));
|
||||
}
|
||||
|
||||
// Check for >>
|
||||
let mut append_check = chars.clone();
|
||||
let is_file_append = append_check.next() == Some('>') && append_check.next() == Some('>');
|
||||
if is_file_append {
|
||||
*chars = append_check;
|
||||
}
|
||||
|
||||
let arg: String = chars.collect();
|
||||
let arg_path = get_path(arg.trim());
|
||||
|
||||
let dest = if is_file_append {
|
||||
WriteDest::FileAppend(arg_path)
|
||||
} else {
|
||||
WriteDest::File(arg_path)
|
||||
};
|
||||
|
||||
Ok(Some(Verb::Write(dest)))
|
||||
}
|
||||
|
||||
fn parse_global(chars: &mut Peekable<Chars<'_>>) -> Result<Option<(Motion,Verb)>,Option<String>> {
|
||||
let is_negated = if chars.peek() == Some(&'!') { chars.next(); true } else { false };
|
||||
|
||||
chars.peeking_take_while(|c| c.is_whitespace()).for_each(drop); // Ignore whitespace
|
||||
|
||||
let Some(delimiter) = chars.next() else {
|
||||
return Ok(Some((Motion::Null,Verb::RepeatGlobal)))
|
||||
};
|
||||
if delimiter.is_alphanumeric() {
|
||||
return Err(None)
|
||||
}
|
||||
let global_pat = parse_pattern(chars, delimiter)?;
|
||||
let Some(command) = parse_ex_command(chars)? else {
|
||||
return Err(Some("Expected a command after global pattern".into()))
|
||||
};
|
||||
if is_negated {
|
||||
Ok(Some((Motion::NotGlobal(Val::new_str(global_pat)), command)))
|
||||
} else {
|
||||
Ok(Some((Motion::Global(Val::new_str(global_pat)), command)))
|
||||
}
|
||||
}
|
||||
|
||||
fn parse_substitute(chars: &mut Peekable<Chars<'_>>) -> Result<Option<Verb>,Option<String>> {
|
||||
while chars.peek().is_some_and(|c| c.is_whitespace()) { chars.next(); } // Ignore whitespace
|
||||
|
||||
let Some(delimiter) = chars.next() else {
|
||||
return Ok(Some(Verb::RepeatSubstitute))
|
||||
};
|
||||
if delimiter.is_alphanumeric() {
|
||||
return Err(None)
|
||||
}
|
||||
let old_pat = parse_pattern(chars, delimiter)?;
|
||||
let new_pat = parse_pattern(chars, delimiter)?;
|
||||
let mut flags = SubFlags::empty();
|
||||
while let Some(ch) = chars.next() {
|
||||
match ch {
|
||||
'g' => flags |= SubFlags::GLOBAL,
|
||||
'i' => flags |= SubFlags::IGNORE_CASE,
|
||||
'I' => flags |= SubFlags::NO_IGNORE_CASE,
|
||||
'n' => flags |= SubFlags::SHOW_COUNT,
|
||||
_ => return Err(None)
|
||||
}
|
||||
}
|
||||
Ok(Some(Verb::Substitute(old_pat, new_pat, flags)))
|
||||
}
|
||||
|
||||
fn parse_pattern(chars: &mut Peekable<Chars<'_>>, delimiter: char) -> Result<String,Option<String>> {
|
||||
let mut pat = String::new();
|
||||
let mut closed = false;
|
||||
while let Some(ch) = chars.next() {
|
||||
match ch {
|
||||
'\\' => {
|
||||
if chars.peek().is_some_and(|c| *c == delimiter) {
|
||||
// We escaped the delimiter, so we consume the escape char and continue
|
||||
pat.push(chars.next().unwrap());
|
||||
continue
|
||||
} else {
|
||||
// The escape char is probably for the regex in the pattern
|
||||
pat.push(ch);
|
||||
if let Some(esc_ch) = chars.next() {
|
||||
pat.push(esc_ch)
|
||||
}
|
||||
}
|
||||
}
|
||||
_ if ch == delimiter => {
|
||||
closed = true;
|
||||
break
|
||||
}
|
||||
_ => pat.push(ch)
|
||||
}
|
||||
}
|
||||
if !closed {
|
||||
Err(Some("Unclosed pattern in ex command".into()))
|
||||
} else {
|
||||
Ok(pat)
|
||||
}
|
||||
}
|
||||
124
src/readline/vimode/insert.rs
Normal file
124
src/readline/vimode/insert.rs
Normal file
@@ -0,0 +1,124 @@
|
||||
use super::{common_cmds, CmdReplay, ModeReport, ViMode};
|
||||
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
||||
use crate::readline::vicmd::{
|
||||
Direction, Motion, MotionCmd, To, Verb, VerbCmd, ViCmd, Word,
|
||||
};
|
||||
|
||||
#[derive(Default, Clone, Debug)]
|
||||
pub struct ViInsert {
|
||||
cmds: Vec<ViCmd>,
|
||||
pending_cmd: ViCmd,
|
||||
repeat_count: u16,
|
||||
}
|
||||
|
||||
impl ViInsert {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
pub fn with_count(mut self, repeat_count: u16) -> Self {
|
||||
self.repeat_count = repeat_count;
|
||||
self
|
||||
}
|
||||
pub fn register_and_return(&mut self) -> Option<ViCmd> {
|
||||
let mut cmd = self.take_cmd();
|
||||
cmd.normalize_counts();
|
||||
self.register_cmd(&cmd);
|
||||
Some(cmd)
|
||||
}
|
||||
pub fn ctrl_w_is_undo(&self) -> bool {
|
||||
let insert_count = self
|
||||
.cmds
|
||||
.iter()
|
||||
.filter(|cmd: &&ViCmd| matches!(cmd.verb(), Some(VerbCmd(1, Verb::InsertChar(_)))))
|
||||
.count();
|
||||
let backspace_count = self
|
||||
.cmds
|
||||
.iter()
|
||||
.filter(|cmd: &&ViCmd| matches!(cmd.verb(), Some(VerbCmd(1, Verb::Delete))))
|
||||
.count();
|
||||
insert_count > backspace_count
|
||||
}
|
||||
pub fn register_cmd(&mut self, cmd: &ViCmd) {
|
||||
self.cmds.push(cmd.clone())
|
||||
}
|
||||
pub fn take_cmd(&mut self) -> ViCmd {
|
||||
std::mem::take(&mut self.pending_cmd)
|
||||
}
|
||||
}
|
||||
|
||||
impl ViMode for ViInsert {
|
||||
fn handle_key(&mut self, key: E) -> Option<ViCmd> {
|
||||
match key {
|
||||
E(K::Char(ch), M::NONE) => {
|
||||
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('W'), M::CTRL) => {
|
||||
self.pending_cmd.set_verb(VerbCmd(1, Verb::Delete));
|
||||
self.pending_cmd.set_motion(MotionCmd(
|
||||
1,
|
||||
Motion::WordMotion(To::Start, Word::Normal, Direction::Backward),
|
||||
));
|
||||
self.register_and_return()
|
||||
}
|
||||
E(K::Char('H'), M::CTRL) | E(K::Backspace, M::NONE) => {
|
||||
self.pending_cmd.set_verb(VerbCmd(1, Verb::Delete));
|
||||
self
|
||||
.pending_cmd
|
||||
.set_motion(MotionCmd(1, Motion::BackwardCharForced));
|
||||
self.register_and_return()
|
||||
}
|
||||
|
||||
E(K::BackTab, M::NONE) => {
|
||||
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(VerbCmd(1, Verb::Complete));
|
||||
self.register_and_return()
|
||||
}
|
||||
|
||||
E(K::Esc, M::NONE) => {
|
||||
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),
|
||||
}
|
||||
}
|
||||
|
||||
fn is_repeatable(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn as_replay(&self) -> Option<CmdReplay> {
|
||||
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
|
||||
}
|
||||
fn move_cursor_on_undo(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn clamp_cursor(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn hist_scroll_start_pos(&self) -> Option<To> {
|
||||
Some(To::End)
|
||||
}
|
||||
fn report_mode(&self) -> ModeReport {
|
||||
ModeReport::Insert
|
||||
}
|
||||
}
|
||||
103
src/readline/vimode/mod.rs
Normal file
103
src/readline/vimode/mod.rs
Normal file
@@ -0,0 +1,103 @@
|
||||
use unicode_segmentation::UnicodeSegmentation;
|
||||
|
||||
use crate::libsh::error::ShResult;
|
||||
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
||||
use crate::readline::vicmd::{
|
||||
Motion, MotionCmd, To, Verb, VerbCmd, ViCmd,
|
||||
};
|
||||
|
||||
pub mod insert;
|
||||
pub mod normal;
|
||||
pub mod replace;
|
||||
pub mod visual;
|
||||
pub mod ex;
|
||||
|
||||
pub use ex::ViEx;
|
||||
pub use insert::ViInsert;
|
||||
pub use normal::ViNormal;
|
||||
pub use replace::ViReplace;
|
||||
pub use visual::ViVisual;
|
||||
|
||||
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
|
||||
pub enum ModeReport {
|
||||
Insert,
|
||||
Normal,
|
||||
Ex,
|
||||
Visual,
|
||||
Replace,
|
||||
Unknown,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
pub enum CmdReplay {
|
||||
ModeReplay { cmds: Vec<ViCmd>, repeat: u16 },
|
||||
Single(ViCmd),
|
||||
Motion(Motion),
|
||||
}
|
||||
|
||||
impl CmdReplay {
|
||||
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 {
|
||||
fn handle_key_fallible(&mut self, key: E) -> ShResult<Option<ViCmd>> { Ok(self.handle_key(key)) }
|
||||
fn handle_key(&mut self, key: E) -> Option<ViCmd>;
|
||||
fn is_repeatable(&self) -> bool;
|
||||
fn as_replay(&self) -> Option<CmdReplay>;
|
||||
fn cursor_style(&self) -> String;
|
||||
fn pending_seq(&self) -> Option<String>;
|
||||
fn pending_cursor(&self) -> Option<usize> { None }
|
||||
fn move_cursor_on_undo(&self) -> bool;
|
||||
fn clamp_cursor(&self) -> bool;
|
||||
fn hist_scroll_start_pos(&self) -> Option<To>;
|
||||
fn report_mode(&self) -> ModeReport;
|
||||
fn cmds_from_raw(&mut self, raw: &str) -> Vec<ViCmd> {
|
||||
let mut cmds = vec![];
|
||||
for ch in raw.graphemes(true) {
|
||||
let key = E::new(ch, M::NONE);
|
||||
let Some(cmd) = self.handle_key(key) else {
|
||||
continue;
|
||||
};
|
||||
cmds.push(cmd)
|
||||
}
|
||||
cmds
|
||||
}
|
||||
}
|
||||
|
||||
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(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::AcceptLineOrNewline)),
|
||||
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::Delete));
|
||||
pending_cmd.set_motion(MotionCmd(1, Motion::ForwardChar));
|
||||
}
|
||||
E(K::Backspace, M::NONE) | E(K::Char('H'), M::CTRL) => {
|
||||
pending_cmd.set_verb(VerbCmd(1, Verb::Delete));
|
||||
pending_cmd.set_motion(MotionCmd(1, Motion::BackwardChar));
|
||||
}
|
||||
_ => return None,
|
||||
}
|
||||
Some(pending_cmd)
|
||||
}
|
||||
849
src/readline/vimode/normal.rs
Normal file
849
src/readline/vimode/normal.rs
Normal file
@@ -0,0 +1,849 @@
|
||||
use std::iter::Peekable;
|
||||
use std::str::Chars;
|
||||
|
||||
use super::{common_cmds, CmdReplay, CmdState, ModeReport, ViMode};
|
||||
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
||||
use crate::readline::vicmd::{
|
||||
Anchor, Bound, CmdFlags, Dest, Direction, Motion, MotionCmd, RegisterName, TextObj, To, Verb,
|
||||
VerbCmd, ViCmd, Word,
|
||||
};
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct ViNormal {
|
||||
pending_seq: String,
|
||||
pending_flags: CmdFlags,
|
||||
}
|
||||
|
||||
impl ViNormal {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
pub fn clear_cmd(&mut self) {
|
||||
self.pending_seq = String::new();
|
||||
}
|
||||
pub fn take_cmd(&mut self) -> String {
|
||||
std::mem::take(&mut self.pending_seq)
|
||||
}
|
||||
pub fn flags(&self) -> CmdFlags {
|
||||
self.pending_flags
|
||||
}
|
||||
#[allow(clippy::unnecessary_unwrap)]
|
||||
fn validate_combination(&self, verb: Option<&Verb>, motion: Option<&Motion>) -> CmdState {
|
||||
if verb.is_none() {
|
||||
match motion {
|
||||
Some(Motion::TextObj(obj)) => {
|
||||
return match obj {
|
||||
TextObj::Sentence(_) | TextObj::Paragraph(_) => CmdState::Complete,
|
||||
_ => CmdState::Invalid,
|
||||
};
|
||||
}
|
||||
Some(_) => return CmdState::Complete,
|
||||
None => return CmdState::Pending,
|
||||
}
|
||||
}
|
||||
if verb.is_some() && motion.is_none() {
|
||||
match verb.unwrap() {
|
||||
Verb::Put(_) => 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> {
|
||||
self.clear_cmd();
|
||||
None
|
||||
}
|
||||
pub fn try_parse(&mut self, ch: char) -> Option<ViCmd> {
|
||||
self.pending_seq.push(ch);
|
||||
let mut chars = self.pending_seq.chars().peekable();
|
||||
|
||||
/*
|
||||
* Parse the register
|
||||
*
|
||||
* Registers can be any letter a-z or A-Z.
|
||||
* While uncommon, it is possible to give a count to a register name.
|
||||
*/
|
||||
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)
|
||||
};
|
||||
|
||||
/*
|
||||
* We will now parse the verb
|
||||
* If we hit an invalid sequence, we will call 'return self.quit_parse()'
|
||||
* self.quit_parse() will clear the pending command and return None
|
||||
*
|
||||
* If we hit an incomplete sequence, we will simply return None.
|
||||
* returning None leaves the pending sequence where it is
|
||||
*
|
||||
* Note that we do use a label here for the block and 'return' values from
|
||||
* this scope using "break 'verb_parse <value>"
|
||||
*/
|
||||
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;
|
||||
};
|
||||
match ch {
|
||||
'g' => {
|
||||
if let Some(ch) = chars_clone.peek() {
|
||||
match ch {
|
||||
'v' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::VisualModeSelectLast)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'~' => {
|
||||
chars_clone.next();
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::ToggleCaseRange));
|
||||
}
|
||||
'u' => {
|
||||
chars_clone.next();
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::ToLower));
|
||||
}
|
||||
'U' => {
|
||||
chars_clone.next();
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::ToUpper));
|
||||
}
|
||||
'?' => {
|
||||
chars_clone.next();
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Rot13));
|
||||
}
|
||||
_ => break 'verb_parse None,
|
||||
}
|
||||
} else {
|
||||
break 'verb_parse None;
|
||||
}
|
||||
}
|
||||
'.' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::RepeatLast)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'x' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::Delete)),
|
||||
motion: Some(MotionCmd(1, Motion::ForwardCharForced)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'X' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::Delete)),
|
||||
motion: Some(MotionCmd(1, Motion::BackwardChar)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
's' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::Change)),
|
||||
motion: Some(MotionCmd(1, Motion::ForwardChar)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'S' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::Change)),
|
||||
motion: Some(MotionCmd(1, Motion::WholeLineExclusive)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'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)));
|
||||
}
|
||||
'>' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Indent));
|
||||
}
|
||||
'<' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Dedent));
|
||||
}
|
||||
'r' => {
|
||||
let ch = chars_clone.next()?;
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::ReplaceCharInplace(ch, count as u16))),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'R' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::ReplaceMode)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'~' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::ToggleCaseInplace(count as u16))),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'u' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::Undo)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'v' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::VisualMode)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'V' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::VisualModeLine)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'o' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertModeLineBreak(Anchor::After))),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'O' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertModeLineBreak(Anchor::Before))),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'a' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||
motion: Some(MotionCmd(1, Motion::ForwardChar)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'A' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||
motion: Some(MotionCmd(1, Motion::EndOfLine)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
':' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::ExMode)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
})
|
||||
}
|
||||
'i' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'I' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||
motion: Some(MotionCmd(1, Motion::BeginningOfFirstWord)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'J' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::JoinLines)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'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));
|
||||
}
|
||||
'Y' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::Yank)),
|
||||
motion: Some(MotionCmd(1, Motion::EndOfLine)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'D' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::Delete)),
|
||||
motion: Some(MotionCmd(1, Motion::EndOfLine)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'C' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::Change)),
|
||||
motion: Some(MotionCmd(1, Motion::EndOfLine)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: self.flags(),
|
||||
});
|
||||
}
|
||||
'=' => {
|
||||
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;
|
||||
};
|
||||
// Double inputs like 'dd' and 'cc', and some special cases
|
||||
match (ch, &verb) {
|
||||
// Double inputs
|
||||
('?', Some(VerbCmd(_, Verb::Rot13)))
|
||||
| ('d', Some(VerbCmd(_, Verb::Delete)))
|
||||
| ('y', Some(VerbCmd(_, Verb::Yank)))
|
||||
| ('=', Some(VerbCmd(_, Verb::Equalize)))
|
||||
| ('u', Some(VerbCmd(_, Verb::ToLower)))
|
||||
| ('U', Some(VerbCmd(_, Verb::ToUpper)))
|
||||
| ('~', Some(VerbCmd(_, Verb::ToggleCaseRange)))
|
||||
| ('>', Some(VerbCmd(_, Verb::Indent)))
|
||||
| ('<', Some(VerbCmd(_, Verb::Dedent))) => {
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::WholeLineInclusive));
|
||||
}
|
||||
('c', Some(VerbCmd(_, Verb::Change))) => {
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::WholeLineExclusive));
|
||||
}
|
||||
('W', Some(VerbCmd(_, Verb::Change))) => {
|
||||
// Same with 'W'
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::End, Word::Big, Direction::Forward),
|
||||
));
|
||||
}
|
||||
_ => { /* Nothing weird, so let's continue */ }
|
||||
}
|
||||
match ch {
|
||||
'g' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
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::WordMotion(To::End, Word::Normal, Direction::Backward),
|
||||
));
|
||||
}
|
||||
'E' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::End, Word::Big, Direction::Backward),
|
||||
));
|
||||
}
|
||||
'k' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ScreenLineUp));
|
||||
}
|
||||
'j' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ScreenLineDown));
|
||||
}
|
||||
'_' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::EndOfLastWord));
|
||||
}
|
||||
'0' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::BeginningOfScreenLine));
|
||||
}
|
||||
'^' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::FirstGraphicalOnScreenLine));
|
||||
}
|
||||
_ => return self.quit_parse(),
|
||||
}
|
||||
}
|
||||
']' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
match ch {
|
||||
')' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToParen(Direction::Forward)));
|
||||
}
|
||||
'}' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToBrace(Direction::Forward)));
|
||||
}
|
||||
_ => return self.quit_parse(),
|
||||
}
|
||||
}
|
||||
'[' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
match ch {
|
||||
'(' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToParen(Direction::Backward)));
|
||||
}
|
||||
'{' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToBrace(Direction::Backward)));
|
||||
}
|
||||
_ => return self.quit_parse(),
|
||||
}
|
||||
}
|
||||
'%' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToDelimMatch));
|
||||
}
|
||||
'v' => {
|
||||
// We got 'v' after a verb
|
||||
// Instead of normal operations, we will calculate the span based on how visual
|
||||
// mode would see it
|
||||
if self
|
||||
.flags()
|
||||
.intersects(CmdFlags::VISUAL | CmdFlags::VISUAL_LINE | CmdFlags::VISUAL_BLOCK)
|
||||
{
|
||||
// We can't have more than one of these
|
||||
return self.quit_parse();
|
||||
}
|
||||
self.pending_flags |= CmdFlags::VISUAL;
|
||||
break 'motion_parse None;
|
||||
}
|
||||
'V' => {
|
||||
// We got 'V' after a verb
|
||||
// Instead of normal operations, we will calculate the span based on how visual
|
||||
// line mode would see it
|
||||
if self
|
||||
.flags()
|
||||
.intersects(CmdFlags::VISUAL | CmdFlags::VISUAL_LINE | CmdFlags::VISUAL_BLOCK)
|
||||
{
|
||||
// We can't have more than one of these
|
||||
// I know vim can technically do this, but it doesn't really make sense to allow
|
||||
// it since even in vim only the first one given is used
|
||||
return self.quit_parse();
|
||||
}
|
||||
self.pending_flags |= CmdFlags::VISUAL;
|
||||
break 'motion_parse None;
|
||||
}
|
||||
// TODO: figure out how to include 'Ctrl+V' here, might need a refactor
|
||||
'G' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::EndOfBuffer));
|
||||
}
|
||||
'f' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::CharSearch(Direction::Forward, Dest::On, *ch),
|
||||
));
|
||||
}
|
||||
'F' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::CharSearch(Direction::Backward, Dest::On, *ch),
|
||||
));
|
||||
}
|
||||
't' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::CharSearch(Direction::Forward, Dest::Before, *ch),
|
||||
));
|
||||
}
|
||||
'T' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::CharSearch(Direction::Backward, Dest::Before, *ch),
|
||||
));
|
||||
}
|
||||
';' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::RepeatMotion));
|
||||
}
|
||||
',' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::RepeatMotionRev));
|
||||
}
|
||||
'|' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToColumn));
|
||||
}
|
||||
'^' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::BeginningOfFirstWord));
|
||||
}
|
||||
'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::WordMotion(To::Start, Word::Normal, Direction::Forward),
|
||||
));
|
||||
}
|
||||
'W' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::Start, Word::Big, Direction::Forward),
|
||||
));
|
||||
}
|
||||
'e' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::End, Word::Normal, Direction::Forward),
|
||||
));
|
||||
}
|
||||
'E' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::End, Word::Big, Direction::Forward),
|
||||
));
|
||||
}
|
||||
'b' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::Start, Word::Normal, Direction::Backward),
|
||||
));
|
||||
}
|
||||
'B' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::Start, Word::Big, Direction::Backward),
|
||||
));
|
||||
}
|
||||
')' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::TextObj(TextObj::Sentence(Direction::Forward)),
|
||||
));
|
||||
}
|
||||
'(' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::TextObj(TextObj::Sentence(Direction::Backward)),
|
||||
));
|
||||
}
|
||||
'}' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::TextObj(TextObj::Paragraph(Direction::Forward)),
|
||||
));
|
||||
}
|
||||
'{' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::TextObj(TextObj::Paragraph(Direction::Backward)),
|
||||
));
|
||||
}
|
||||
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, bound),
|
||||
'W' => TextObj::Word(Word::Big, bound),
|
||||
's' => TextObj::WholeSentence(bound),
|
||||
'p' => TextObj::WholeParagraph(bound),
|
||||
'"' => TextObj::DoubleQuote(bound),
|
||||
'\'' => TextObj::SingleQuote(bound),
|
||||
'`' => TextObj::BacktickQuote(bound),
|
||||
'(' | ')' | 'b' => TextObj::Paren(bound),
|
||||
'{' | '}' | 'B' => TextObj::Brace(bound),
|
||||
'[' | ']' => TextObj::Bracket(bound),
|
||||
'<' | '>' => TextObj::Angle(bound),
|
||||
_ => return self.quit_parse(),
|
||||
};
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::TextObj(obj)));
|
||||
}
|
||||
_ => return self.quit_parse(),
|
||||
}
|
||||
};
|
||||
|
||||
let _ = chars; // suppresses unused warnings, creates an error if we decide to use chars later
|
||||
|
||||
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 => Some(ViCmd {
|
||||
register,
|
||||
verb,
|
||||
motion,
|
||||
raw_seq: std::mem::take(&mut self.pending_seq),
|
||||
flags: self.flags(),
|
||||
}),
|
||||
CmdState::Pending => None,
|
||||
CmdState::Invalid => {
|
||||
self.pending_seq.clear();
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ViMode for ViNormal {
|
||||
fn handle_key(&mut self, key: E) -> Option<ViCmd> {
|
||||
let mut cmd: Option<ViCmd> = match key {
|
||||
E(K::Char('V'), M::NONE) => Some(ViCmd {
|
||||
register: Default::default(),
|
||||
verb: Some(VerbCmd(1, Verb::VisualModeLine)),
|
||||
motion: None,
|
||||
raw_seq: "".into(),
|
||||
flags: self.flags(),
|
||||
}),
|
||||
E(K::Char('A'), M::CTRL) => {
|
||||
let count = self.parse_count(&mut self.pending_seq.chars().peekable()).unwrap_or(1) as u16;
|
||||
self.pending_seq.clear();
|
||||
Some(ViCmd {
|
||||
register: Default::default(),
|
||||
verb: Some(VerbCmd(1, Verb::IncrementNumber(count))),
|
||||
motion: None,
|
||||
raw_seq: "".into(),
|
||||
flags: self.flags(),
|
||||
})
|
||||
},
|
||||
E(K::Char('X'), M::CTRL) => {
|
||||
let count = self.parse_count(&mut self.pending_seq.chars().peekable()).unwrap_or(1) as u16;
|
||||
self.pending_seq.clear();
|
||||
Some(ViCmd {
|
||||
register: Default::default(),
|
||||
verb: Some(VerbCmd(1, Verb::DecrementNumber(count))),
|
||||
motion: None,
|
||||
raw_seq: "".into(),
|
||||
flags: self.flags(),
|
||||
})
|
||||
},
|
||||
|
||||
E(K::Char(ch), M::NONE) => self.try_parse(ch),
|
||||
E(K::Backspace, M::NONE) => Some(ViCmd {
|
||||
register: Default::default(),
|
||||
verb: None,
|
||||
motion: Some(MotionCmd(1, Motion::BackwardChar)),
|
||||
raw_seq: "".into(),
|
||||
flags: self.flags(),
|
||||
}),
|
||||
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(),
|
||||
flags: self.flags(),
|
||||
})
|
||||
}
|
||||
E(K::Esc, M::NONE) => {
|
||||
self.clear_cmd();
|
||||
None
|
||||
}
|
||||
_ => {
|
||||
if let Some(cmd) = common_cmds(key) {
|
||||
self.clear_cmd();
|
||||
Some(cmd)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(cmd) = cmd.as_mut() {
|
||||
cmd.normalize_counts();
|
||||
};
|
||||
cmd
|
||||
}
|
||||
|
||||
fn is_repeatable(&self) -> bool {
|
||||
false
|
||||
}
|
||||
|
||||
fn as_replay(&self) -> Option<CmdReplay> {
|
||||
None
|
||||
}
|
||||
|
||||
fn cursor_style(&self) -> String {
|
||||
"\x1b[2 q".to_string()
|
||||
}
|
||||
|
||||
fn pending_seq(&self) -> Option<String> {
|
||||
Some(self.pending_seq.clone())
|
||||
}
|
||||
|
||||
fn move_cursor_on_undo(&self) -> bool {
|
||||
false
|
||||
}
|
||||
fn clamp_cursor(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn hist_scroll_start_pos(&self) -> Option<To> {
|
||||
None
|
||||
}
|
||||
fn report_mode(&self) -> ModeReport {
|
||||
ModeReport::Normal
|
||||
}
|
||||
}
|
||||
107
src/readline/vimode/replace.rs
Normal file
107
src/readline/vimode/replace.rs
Normal file
@@ -0,0 +1,107 @@
|
||||
use super::{common_cmds, CmdReplay, ModeReport, ViMode};
|
||||
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
||||
use crate::readline::vicmd::{
|
||||
Direction, Motion, MotionCmd, To, Verb, VerbCmd, ViCmd, Word,
|
||||
};
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct ViReplace {
|
||||
cmds: Vec<ViCmd>,
|
||||
pending_cmd: ViCmd,
|
||||
repeat_count: u16,
|
||||
}
|
||||
|
||||
impl ViReplace {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
pub fn with_count(mut self, repeat_count: u16) -> Self {
|
||||
self.repeat_count = repeat_count;
|
||||
self
|
||||
}
|
||||
pub fn register_and_return(&mut self) -> Option<ViCmd> {
|
||||
let mut cmd = self.take_cmd();
|
||||
cmd.normalize_counts();
|
||||
self.register_cmd(&cmd);
|
||||
Some(cmd)
|
||||
}
|
||||
pub fn register_cmd(&mut self, cmd: &ViCmd) {
|
||||
self.cmds.push(cmd.clone())
|
||||
}
|
||||
pub fn take_cmd(&mut self) -> ViCmd {
|
||||
std::mem::take(&mut self.pending_cmd)
|
||||
}
|
||||
}
|
||||
|
||||
impl ViMode for ViReplace {
|
||||
fn handle_key(&mut self, key: E) -> Option<ViCmd> {
|
||||
match key {
|
||||
E(K::Char(ch), M::NONE) => {
|
||||
self.pending_cmd.set_verb(VerbCmd(1, Verb::ReplaceChar(ch)));
|
||||
self
|
||||
.pending_cmd
|
||||
.set_motion(MotionCmd(1, Motion::ForwardChar));
|
||||
self.register_and_return()
|
||||
}
|
||||
E(K::Char('W'), M::CTRL) => {
|
||||
self.pending_cmd.set_verb(VerbCmd(1, Verb::Delete));
|
||||
self.pending_cmd.set_motion(MotionCmd(
|
||||
1,
|
||||
Motion::WordMotion(To::Start, Word::Normal, Direction::Backward),
|
||||
));
|
||||
self.register_and_return()
|
||||
}
|
||||
E(K::Char('H'), M::CTRL) | E(K::Backspace, M::NONE) => {
|
||||
self
|
||||
.pending_cmd
|
||||
.set_motion(MotionCmd(1, Motion::BackwardChar));
|
||||
self.register_and_return()
|
||||
}
|
||||
|
||||
E(K::BackTab, M::NONE) => {
|
||||
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(VerbCmd(1, Verb::Complete));
|
||||
self.register_and_return()
|
||||
}
|
||||
|
||||
E(K::Esc, M::NONE) => {
|
||||
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),
|
||||
}
|
||||
}
|
||||
fn is_repeatable(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn cursor_style(&self) -> String {
|
||||
"\x1b[4 q".to_string()
|
||||
}
|
||||
fn pending_seq(&self) -> Option<String> {
|
||||
None
|
||||
}
|
||||
fn as_replay(&self) -> Option<CmdReplay> {
|
||||
Some(CmdReplay::mode(self.cmds.clone(), self.repeat_count))
|
||||
}
|
||||
fn move_cursor_on_undo(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn clamp_cursor(&self) -> bool {
|
||||
true
|
||||
}
|
||||
fn hist_scroll_start_pos(&self) -> Option<To> {
|
||||
Some(To::End)
|
||||
}
|
||||
fn report_mode(&self) -> ModeReport {
|
||||
ModeReport::Replace
|
||||
}
|
||||
}
|
||||
695
src/readline/vimode/visual.rs
Normal file
695
src/readline/vimode/visual.rs
Normal file
@@ -0,0 +1,695 @@
|
||||
use std::iter::Peekable;
|
||||
use std::str::Chars;
|
||||
|
||||
use super::{common_cmds, CmdReplay, CmdState, ModeReport, ViMode};
|
||||
use crate::readline::keys::{KeyCode as K, KeyEvent as E, ModKeys as M};
|
||||
use crate::readline::vicmd::{
|
||||
Anchor, Bound, CmdFlags, Dest, Direction, Motion, MotionCmd, RegisterName, TextObj, To, Verb,
|
||||
VerbCmd, ViCmd, Word,
|
||||
};
|
||||
|
||||
#[derive(Default, Debug)]
|
||||
pub struct ViVisual {
|
||||
pending_seq: String,
|
||||
}
|
||||
|
||||
impl ViVisual {
|
||||
pub fn new() -> Self {
|
||||
Self::default()
|
||||
}
|
||||
pub fn clear_cmd(&mut self) {
|
||||
self.pending_seq = String::new();
|
||||
}
|
||||
pub fn take_cmd(&mut self) -> String {
|
||||
std::mem::take(&mut self.pending_seq)
|
||||
}
|
||||
|
||||
#[allow(clippy::unnecessary_unwrap)]
|
||||
fn validate_combination(&self, verb: Option<&Verb>, motion: Option<&Motion>) -> CmdState {
|
||||
if verb.is_none() {
|
||||
match motion {
|
||||
Some(_) => return CmdState::Complete,
|
||||
None => return CmdState::Pending,
|
||||
}
|
||||
}
|
||||
if motion.is_none() && verb.is_some() {
|
||||
match verb.unwrap() {
|
||||
Verb::Put(_) => 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> {
|
||||
self.clear_cmd();
|
||||
None
|
||||
}
|
||||
pub fn try_parse(&mut self, ch: char) -> Option<ViCmd> {
|
||||
self.pending_seq.push(ch);
|
||||
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;
|
||||
};
|
||||
match ch {
|
||||
'g' => {
|
||||
if let Some(ch) = chars_clone.peek() {
|
||||
match ch {
|
||||
'v' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::VisualModeSelectLast)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'?' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::Rot13)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
_ => break 'verb_parse None,
|
||||
}
|
||||
} else {
|
||||
break 'verb_parse None;
|
||||
}
|
||||
}
|
||||
'.' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::RepeatLast)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'x' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Delete));
|
||||
}
|
||||
'X' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::Delete)),
|
||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'Y' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::Yank)),
|
||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'D' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::Delete)),
|
||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'R' | 'C' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::Change)),
|
||||
motion: Some(MotionCmd(1, Motion::WholeLineExclusive)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'>' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::Indent)),
|
||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'<' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::Dedent)),
|
||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'=' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::Equalize)),
|
||||
motion: Some(MotionCmd(1, Motion::WholeLineInclusive)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'p' | 'P' => {
|
||||
chars = chars_clone;
|
||||
break 'verb_parse Some(VerbCmd(count, Verb::Put(Anchor::Before)));
|
||||
}
|
||||
'r' => {
|
||||
let ch = chars_clone.next()?;
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::ReplaceChar(ch))),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'~' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(1, Verb::ToggleCaseRange)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'u' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::ToLower)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'U' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::ToUpper)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'O' | 'o' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::SwapVisualAnchor)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'A' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||
motion: Some(MotionCmd(1, Motion::ForwardChar)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'I' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::InsertMode)),
|
||||
motion: Some(MotionCmd(1, Motion::BeginningOfLine)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'J' => {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(VerbCmd(count, Verb::JoinLines)),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
'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));
|
||||
}
|
||||
_ => break 'verb_parse None,
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(verb) = verb {
|
||||
return Some(ViCmd {
|
||||
register,
|
||||
verb: Some(verb),
|
||||
motion: None,
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
});
|
||||
}
|
||||
|
||||
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;
|
||||
};
|
||||
match (ch, &verb) {
|
||||
('d', Some(VerbCmd(_, Verb::Delete)))
|
||||
| ('y', Some(VerbCmd(_, Verb::Yank)))
|
||||
| ('=', Some(VerbCmd(_, Verb::Equalize)))
|
||||
| ('>', Some(VerbCmd(_, Verb::Indent)))
|
||||
| ('<', Some(VerbCmd(_, Verb::Dedent))) => {
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::WholeLineInclusive));
|
||||
}
|
||||
('c', Some(VerbCmd(_, Verb::Change))) => {
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::WholeLineExclusive));
|
||||
}
|
||||
_ => {}
|
||||
}
|
||||
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_clone.next();
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::End, Word::Normal, Direction::Backward),
|
||||
));
|
||||
}
|
||||
'E' => {
|
||||
chars_clone.next();
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::End, Word::Big, Direction::Backward),
|
||||
));
|
||||
}
|
||||
'k' => {
|
||||
chars_clone.next();
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ScreenLineUp));
|
||||
}
|
||||
'j' => {
|
||||
chars_clone.next();
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ScreenLineDown));
|
||||
}
|
||||
_ => return self.quit_parse(),
|
||||
}
|
||||
} else {
|
||||
break 'motion_parse None;
|
||||
}
|
||||
}
|
||||
']' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
match ch {
|
||||
')' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToParen(Direction::Forward)));
|
||||
}
|
||||
'}' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToBrace(Direction::Forward)));
|
||||
}
|
||||
_ => return self.quit_parse(),
|
||||
}
|
||||
}
|
||||
'[' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
match ch {
|
||||
'(' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToParen(Direction::Backward)));
|
||||
}
|
||||
'{' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToBrace(Direction::Backward)));
|
||||
}
|
||||
_ => return self.quit_parse(),
|
||||
}
|
||||
}
|
||||
'%' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToDelimMatch));
|
||||
}
|
||||
'f' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::CharSearch(Direction::Forward, Dest::On, *ch),
|
||||
));
|
||||
}
|
||||
'F' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::CharSearch(Direction::Backward, Dest::On, *ch),
|
||||
));
|
||||
}
|
||||
't' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::CharSearch(Direction::Forward, Dest::Before, *ch),
|
||||
));
|
||||
}
|
||||
'T' => {
|
||||
let Some(ch) = chars_clone.peek() else {
|
||||
break 'motion_parse None;
|
||||
};
|
||||
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::CharSearch(Direction::Backward, Dest::Before, *ch),
|
||||
));
|
||||
}
|
||||
';' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::RepeatMotion));
|
||||
}
|
||||
',' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::RepeatMotionRev));
|
||||
}
|
||||
'|' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::ToColumn));
|
||||
}
|
||||
'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::WordMotion(To::Start, Word::Normal, Direction::Forward),
|
||||
));
|
||||
}
|
||||
'W' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::Start, Word::Big, Direction::Forward),
|
||||
));
|
||||
}
|
||||
'e' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::End, Word::Normal, Direction::Forward),
|
||||
));
|
||||
}
|
||||
'E' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::End, Word::Big, Direction::Forward),
|
||||
));
|
||||
}
|
||||
'b' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::Start, Word::Normal, Direction::Backward),
|
||||
));
|
||||
}
|
||||
'B' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::WordMotion(To::Start, Word::Big, Direction::Backward),
|
||||
));
|
||||
}
|
||||
')' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::TextObj(TextObj::Sentence(Direction::Forward)),
|
||||
));
|
||||
}
|
||||
'(' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::TextObj(TextObj::Sentence(Direction::Backward)),
|
||||
));
|
||||
}
|
||||
'}' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::TextObj(TextObj::Paragraph(Direction::Forward)),
|
||||
));
|
||||
}
|
||||
'{' => {
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(
|
||||
count,
|
||||
Motion::TextObj(TextObj::Paragraph(Direction::Backward)),
|
||||
));
|
||||
}
|
||||
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, bound),
|
||||
'W' => TextObj::Word(Word::Big, bound),
|
||||
's' => TextObj::WholeSentence(bound),
|
||||
'p' => TextObj::WholeParagraph(bound),
|
||||
'"' => TextObj::DoubleQuote(bound),
|
||||
'\'' => TextObj::SingleQuote(bound),
|
||||
'`' => TextObj::BacktickQuote(bound),
|
||||
'(' | ')' | 'b' => TextObj::Paren(bound),
|
||||
'{' | '}' | 'B' => TextObj::Brace(bound),
|
||||
'[' | ']' => TextObj::Bracket(bound),
|
||||
'<' | '>' => TextObj::Angle(bound),
|
||||
_ => return self.quit_parse(),
|
||||
};
|
||||
chars = chars_clone;
|
||||
break 'motion_parse Some(MotionCmd(count, Motion::TextObj(obj)));
|
||||
}
|
||||
_ => return self.quit_parse(),
|
||||
}
|
||||
};
|
||||
|
||||
let _ = chars; // suppresses unused warnings, creates an error if we decide to use chars later
|
||||
|
||||
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 => Some(ViCmd {
|
||||
register,
|
||||
verb,
|
||||
motion,
|
||||
raw_seq: std::mem::take(&mut self.pending_seq),
|
||||
flags: CmdFlags::empty(),
|
||||
}),
|
||||
CmdState::Pending => None,
|
||||
CmdState::Invalid => {
|
||||
self.pending_seq.clear();
|
||||
None
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ViMode for ViVisual {
|
||||
fn handle_key(&mut self, key: E) -> Option<ViCmd> {
|
||||
let mut cmd: Option<ViCmd> = match key {
|
||||
E(K::Char(ch), M::NONE) => self.try_parse(ch),
|
||||
E(K::Backspace, M::NONE) => Some(ViCmd {
|
||||
register: Default::default(),
|
||||
verb: None,
|
||||
motion: Some(MotionCmd(1, Motion::BackwardChar)),
|
||||
raw_seq: "".into(),
|
||||
flags: CmdFlags::empty(),
|
||||
}),
|
||||
E(K::Char('A'), M::CTRL) => {
|
||||
let count = self.parse_count(&mut self.pending_seq.chars().peekable()).unwrap_or(1) as u16;
|
||||
self.pending_seq.clear();
|
||||
Some(ViCmd {
|
||||
register: Default::default(),
|
||||
verb: Some(VerbCmd(1, Verb::IncrementNumber(count))),
|
||||
motion: None,
|
||||
raw_seq: "".into(),
|
||||
flags: CmdFlags::empty(),
|
||||
})
|
||||
},
|
||||
E(K::Char('X'), M::CTRL) => {
|
||||
let count = self.parse_count(&mut self.pending_seq.chars().peekable()).unwrap_or(1) as u16;
|
||||
self.pending_seq.clear();
|
||||
Some(ViCmd {
|
||||
register: Default::default(),
|
||||
verb: Some(VerbCmd(1, Verb::DecrementNumber(count))),
|
||||
motion: None,
|
||||
raw_seq: "".into(),
|
||||
flags: CmdFlags::empty(),
|
||||
})
|
||||
}
|
||||
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(),
|
||||
flags: CmdFlags::empty(),
|
||||
})
|
||||
}
|
||||
E(K::Esc, M::NONE) => Some(ViCmd {
|
||||
register: Default::default(),
|
||||
verb: Some(VerbCmd(1, Verb::NormalMode)),
|
||||
motion: Some(MotionCmd(1, Motion::Null)),
|
||||
raw_seq: self.take_cmd(),
|
||||
flags: CmdFlags::empty(),
|
||||
}),
|
||||
_ => {
|
||||
if let Some(cmd) = common_cmds(key) {
|
||||
self.clear_cmd();
|
||||
Some(cmd)
|
||||
} else {
|
||||
None
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
if let Some(cmd) = cmd.as_mut() {
|
||||
cmd.normalize_counts();
|
||||
};
|
||||
cmd
|
||||
}
|
||||
|
||||
fn is_repeatable(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn as_replay(&self) -> Option<CmdReplay> {
|
||||
None
|
||||
}
|
||||
|
||||
fn cursor_style(&self) -> String {
|
||||
"\x1b[2 q".to_string()
|
||||
}
|
||||
|
||||
fn pending_seq(&self) -> Option<String> {
|
||||
Some(self.pending_seq.clone())
|
||||
}
|
||||
|
||||
fn move_cursor_on_undo(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn clamp_cursor(&self) -> bool {
|
||||
true
|
||||
}
|
||||
|
||||
fn hist_scroll_start_pos(&self) -> Option<To> {
|
||||
None
|
||||
}
|
||||
|
||||
fn report_mode(&self) -> ModeReport {
|
||||
ModeReport::Visual
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user