Skip to content
Merged
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
33 changes: 22 additions & 11 deletions crates/modalkit/src/env/vim/keybindings.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1888,20 +1888,28 @@ fn default_ctrlcd<I: ApplicationInfo>() -> Vec<(MappedModes, &'static str, Input
}

#[rustfmt::skip]
fn submit_on_enter<I: ApplicationInfo>() -> Vec<(MappedModes, &'static str, InputStep<I>)> {
[
fn submit_on_enter<I: ApplicationInfo>(insert_mode_newline: bool) -> Vec<(MappedModes, &'static str, InputStep<I>)> {
let mut mappings = [
// <Enter> in Normal and Visual mode submits contents.
( NVMAP, "<Enter>", prompt!(PromptAction::Submit, VimMode::Normal) ),

// <Enter> in Insert mode submits contents and stays in Insert mode.
( IMAP, "<Enter>", prompt!(PromptAction::Submit, VimMode::Insert) ),

// <Enter> in Command mode submits the command.
( CMAP, "<Enter>", command_exit!(PromptAction::Submit) ),

// <Enter> in Operator-Pending mode moves to the next line.
( OMAP, "<Enter>", edit_end!(MoveType::FirstWord(MoveDir1D::Next)) ),
].to_vec()
].to_vec();

let imap = if insert_mode_newline {
// <Enter> in Insert mode types a newlines.
( IMAP, "<Enter>", chartype!(Char::Single('\n')) )
} else {
// <Enter> in Insert mode submits contents and stays in Insert mode.
( IMAP, "<Enter>", prompt!(PromptAction::Submit, VimMode::Insert) )
};
mappings.push(imap);

mappings
}

#[rustfmt::skip]
Expand Down Expand Up @@ -1995,10 +2003,13 @@ pub struct VimBindings<I: ApplicationInfo> {
}

impl<I: ApplicationInfo> VimBindings<I> {
/// Remap the Enter key in Normal, Visual, Select, and Insert mode to
/// [submit](PromptAction::Submit) instead.
pub fn submit_on_enter(mut self) -> Self {
self.enter = submit_on_enter();
/// Remap the Enter key in Normal, Visual, and Select mode to [submit](PromptAction::Submit)
/// instead.
///
/// `insert_mode_newline` decides whether Enter in Insert mode adds a newline as usual or
/// triggers [submit](PromptAction::Submit) as well.
pub fn submit_on_enter(mut self, insert_mode_newline: bool) -> Self {
self.enter = submit_on_enter(insert_mode_newline);
self
}

Expand Down Expand Up @@ -2029,7 +2040,7 @@ impl<I: ApplicationInfo> VimBindings<I> {

impl<I: ApplicationInfo> ShellBindings for VimBindings<I> {
fn shell(self) -> Self {
self.submit_on_enter().search_is_action().ctrlcd_is_abort()
self.submit_on_enter(false).search_is_action().ctrlcd_is_abort()
}
}

Expand Down
2 changes: 1 addition & 1 deletion crates/scansion/examples/read-loop.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,7 @@ fn create_readline(mode: MixedChoice) -> Result<ReadLine, std::io::Error> {
},
_ => {
let mut vi = VimMachine::<TerminalKey, ReadLineInfo>::empty();
VimBindings::default().submit_on_enter().setup(&mut vi);
VimBindings::default().submit_on_enter(false).setup(&mut vi);
ReadLine::new(vi)
},
}
Expand Down
2 changes: 1 addition & 1 deletion crates/scansion/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
//!
//! fn main() -> Result<(), std::io::Error> {
//! let mut vi = VimMachine::<TerminalKey, ReadLineInfo>::empty();
//! VimBindings::default().submit_on_enter().setup(&mut vi);
//! VimBindings::default().submit_on_enter(false).setup(&mut vi);
//!
//! let mut rl = ReadLine::new(vi)?;
//!
Expand Down
Loading