Skip to content

vi mode: Introduce alternate escape sequence for insert mode #670

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion examples/demo.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,11 @@ fn main() -> std::io::Result<()> {

add_newline_keybinding(&mut insert_keybindings);

Box::new(Vi::new(insert_keybindings, normal_keybindings))
Box::new(Vi::new(
insert_keybindings,
normal_keybindings,
(KeyCode::Null, KeyCode::Null),
))
} else {
let mut keybindings = default_emacs_keybindings();
add_menu_keybindings(&mut keybindings);
Expand Down
66 changes: 54 additions & 12 deletions src/edit_mode/vi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -30,39 +30,84 @@ pub struct Vi {
previous: Option<ReedlineEvent>,
// last f, F, t, T motion for ; and ,
last_char_search: Option<ViCharSearch>,
alternate_esc_seq: (KeyCode, KeyCode),
most_recent_keycode: KeyCode,
}

impl Default for Vi {
fn default() -> Self {
Vi {
insert_keybindings: default_vi_insert_keybindings(),
normal_keybindings: default_vi_normal_keybindings(),
alternate_esc_seq: (KeyCode::Null, KeyCode::Null),
cache: Vec::new(),
mode: ViMode::Insert,
previous: None,
last_char_search: None,
most_recent_keycode: KeyCode::Null,
}
}
}

impl Vi {
/// Creates Vi editor using defined keybindings
pub fn new(insert_keybindings: Keybindings, normal_keybindings: Keybindings) -> Self {
pub fn new(
insert_keybindings: Keybindings,
normal_keybindings: Keybindings,
alternate_esc_seq: (KeyCode, KeyCode),
) -> Self {
Self {
insert_keybindings,
normal_keybindings,
alternate_esc_seq,
..Default::default()
}
}
}

fn exit_insert_mode(editor: &mut Vi) -> ReedlineEvent {
editor.most_recent_keycode = KeyCode::Null;
editor.cache.clear();
editor.mode = ViMode::Normal;
ReedlineEvent::Multiple(vec![ReedlineEvent::Esc, ReedlineEvent::Repaint])
}

impl EditMode for Vi {
fn parse_event(&mut self, event: ReedlineRawEvent) -> ReedlineEvent {
match event.into() {
Event::Key(KeyEvent {
code, modifiers, ..
}) => match (self.mode, modifiers, code) {
(ViMode::Normal, modifier, KeyCode::Char(c)) => {
}) => match (
self.mode,
modifiers,
code,
self.alternate_esc_seq,
self.most_recent_keycode,
) {
(ViMode::Insert, KeyModifiers::NONE, code, (e1, e2), mr)
if code == e2 && mr == e1 =>
{
exit_insert_mode(self)
}
(ViMode::Insert, KeyModifiers::NONE, code, (e1, _), _) if code == e1 => {
self.most_recent_keycode = code;
ReedlineEvent::None
}
(
ViMode::Insert,
KeyModifiers::NONE,
KeyCode::Char(code),
(KeyCode::Char(e1), KeyCode::Char(e2)),
KeyCode::Char(mr),
) if code != e2 && mr == e1 => {
self.most_recent_keycode = KeyCode::Char(code);
ReedlineEvent::Multiple(vec![
ReedlineEvent::Edit(vec![EditCommand::InsertChar(e1)]),
ReedlineEvent::Edit(vec![EditCommand::InsertChar(code)]),
])
}

(ViMode::Normal, modifier, KeyCode::Char(c), _, _) => {
let c = c.to_ascii_lowercase();

if let Some(event) = self
Expand Down Expand Up @@ -97,7 +142,7 @@ impl EditMode for Vi {
ReedlineEvent::None
}
}
(ViMode::Insert, modifier, KeyCode::Char(c)) => {
(ViMode::Insert, modifier, KeyCode::Char(c), _, _) => {
// Note. The modifier can also be a combination of modifiers, for
// example:
// KeyModifiers::CONTROL | KeyModifiers::ALT
Expand All @@ -122,6 +167,7 @@ impl EditMode for Vi {
| KeyModifiers::ALT
| KeyModifiers::SHIFT
{
self.most_recent_keycode = KeyCode::Char(c);
ReedlineEvent::Edit(vec![EditCommand::InsertChar(
if modifier == KeyModifiers::SHIFT {
c.to_ascii_uppercase()
Expand All @@ -134,20 +180,16 @@ impl EditMode for Vi {
}
})
}
(_, KeyModifiers::NONE, KeyCode::Esc) => {
self.cache.clear();
self.mode = ViMode::Normal;
ReedlineEvent::Multiple(vec![ReedlineEvent::Esc, ReedlineEvent::Repaint])
}
(_, KeyModifiers::NONE, KeyCode::Enter) => {
(_, KeyModifiers::NONE, KeyCode::Esc, _, _) => exit_insert_mode(self),
(_, KeyModifiers::NONE, KeyCode::Enter, _, _) => {
self.mode = ViMode::Insert;
ReedlineEvent::Enter
}
(ViMode::Normal, _, _) => self
(ViMode::Normal, _, _, _, _) => self
.normal_keybindings
.find_binding(modifiers, code)
.unwrap_or(ReedlineEvent::None),
(ViMode::Insert, _, _) => self
(ViMode::Insert, _, _, _, _) => self
.insert_keybindings
.find_binding(modifiers, code)
.unwrap_or(ReedlineEvent::None),
Expand Down
3 changes: 2 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -166,12 +166,13 @@
//! // Create a reedline object with custom edit mode
//! // This can define a keybinding setting or enable vi-emulation
//! use reedline::{
//! default_vi_insert_keybindings, default_vi_normal_keybindings, EditMode, Reedline, Vi,
//! default_vi_insert_keybindings, default_vi_normal_keybindings, EditMode, Reedline, Vi, KeyCode
//! };
//!
//! let mut line_editor = Reedline::create().with_edit_mode(Box::new(Vi::new(
//! default_vi_insert_keybindings(),
//! default_vi_normal_keybindings(),
//! (KeyCode::Null, KeyCode::Null),
//! )));
//! ```
//!
Expand Down