From 5fcc670860d99bc22b015f43c1205b8a154dc6b9 Mon Sep 17 00:00:00 2001 From: Fernando Herrera Date: Sat, 12 Mar 2022 11:51:08 +0000 Subject: [PATCH] allow list to keybinding mode (#4821) * allow list to keybinding mode * added comments to default.nu --- Cargo.lock | 2 +- docs/sample_config/default_config.nu | 8 +-- src/reedline_config.rs | 95 +++++++++++++++++++--------- 3 files changed, 69 insertions(+), 36 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 2148efa34c..db45e38209 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -3402,7 +3402,7 @@ dependencies = [ [[package]] name = "reedline" version = "0.2.0" -source = "git+https://github.com/nushell/reedline?branch=main#87a32d51476222026ed514da11914de6cb3059ab" +source = "git+https://github.com/nushell/reedline?branch=main#4e5a20d127d84f4722e1d39d1852ccb42aa973f6" dependencies = [ "chrono", "crossterm", diff --git a/docs/sample_config/default_config.nu b/docs/sample_config/default_config.nu index 1e3a10a7e5..94cfa97487 100644 --- a/docs/sample_config/default_config.nu +++ b/docs/sample_config/default_config.nu @@ -198,7 +198,7 @@ let $config = { name: completion_menu modifier: none keycode: tab - mode: emacs # emacs vi_normal vi_insert + mode: emacs # Options: emacs vi_normal vi_insert event: { until: [ { send: menu name: completion_menu } @@ -210,14 +210,14 @@ let $config = { name: completion_previous modifier: shift keycode: backtab - mode: emacs # emacs vi_normal vi_insert + mode: [emacs, vi_normal, vi_insert] # Note: You can add the same keybinding to all modes by using a list event: { send: menuprevious } } { name: history_menu modifier: control keycode: char_x - mode: emacs # emacs vi_normal vi_insert + mode: emacs event: { until: [ { send: menu name: history_menu } @@ -229,7 +229,7 @@ let $config = { name: history_previous modifier: control keycode: char_z - mode: emacs # emacs vi_normal vi_insert + mode: emacs event: { until: [ { send: menupageprevious } diff --git a/src/reedline_config.rs b/src/reedline_config.rs index 739c37a8ff..4a2fb9446e 100644 --- a/src/reedline_config.rs +++ b/src/reedline_config.rs @@ -168,35 +168,32 @@ pub enum KeybindingsMode { pub(crate) fn create_keybindings(config: &Config) -> Result { let parsed_keybindings = &config.keybindings; + + let mut emacs_keybindings = default_emacs_keybindings(); + let mut insert_keybindings = default_vi_insert_keybindings(); + let mut normal_keybindings = default_vi_normal_keybindings(); + + for keybinding in parsed_keybindings { + add_keybinding( + &keybinding.mode, + keybinding, + config, + &mut emacs_keybindings, + &mut insert_keybindings, + &mut normal_keybindings, + )? + } + match config.edit_mode.as_str() { "emacs" => { - let mut keybindings = default_emacs_keybindings(); + add_menu_keybindings(&mut emacs_keybindings); - add_menu_keybindings(&mut keybindings); - - for parsed_keybinding in parsed_keybindings { - if parsed_keybinding.mode.into_string("", config).as_str() == "emacs" { - add_keybinding(&mut keybindings, parsed_keybinding, config)? - } - } - - Ok(KeybindingsMode::Emacs(keybindings)) + Ok(KeybindingsMode::Emacs(emacs_keybindings)) } _ => { - let mut insert_keybindings = default_vi_insert_keybindings(); - let mut normal_keybindings = default_vi_normal_keybindings(); - add_menu_keybindings(&mut insert_keybindings); add_menu_keybindings(&mut normal_keybindings); - for parsed_keybinding in parsed_keybindings { - if parsed_keybinding.mode.into_string("", config).as_str() == "vi_insert" { - add_keybinding(&mut insert_keybindings, parsed_keybinding, config)? - } else if parsed_keybinding.mode.into_string("", config).as_str() == "vi_normal" { - add_keybinding(&mut normal_keybindings, parsed_keybinding, config)? - } - } - Ok(KeybindingsMode::Vi { insert_keybindings, normal_keybindings, @@ -206,6 +203,47 @@ pub(crate) fn create_keybindings(config: &Config) -> Result Result<(), ShellError> { + match &mode { + Value::String { val, span } => match val.as_str() { + "emacs" => add_parsed_keybinding(emacs_keybindings, keybinding, config), + "vi_insert" => add_parsed_keybinding(insert_keybindings, keybinding, config), + "vi_normal" => add_parsed_keybinding(normal_keybindings, keybinding, config), + m => Err(ShellError::UnsupportedConfigValue( + "emacs, vi_insert or vi_normal".to_string(), + m.to_string(), + *span, + )), + }, + Value::List { vals, .. } => { + for inner_mode in vals { + add_keybinding( + inner_mode, + keybinding, + config, + emacs_keybindings, + insert_keybindings, + normal_keybindings, + )? + } + + Ok(()) + } + v => Err(ShellError::UnsupportedConfigValue( + "string or list of strings".to_string(), + v.into_abbreviated_string(config), + v.span()?, + )), + } +} + +fn add_parsed_keybinding( keybindings: &mut Keybindings, keybinding: &ParsedKeybinding, config: &Config, @@ -403,16 +441,11 @@ fn event_from_record( let menu = extract_value("name", cols, vals, span)?; Ok(ReedlineEvent::Menu(menu.into_string("", config))) } - "edit" => { - let edit_command = parse_edit( - &Value::Record { - cols: cols.to_vec(), - vals: vals.to_vec(), - span: *span, - }, - config, - )?; - Ok(ReedlineEvent::Edit(vec![edit_command])) + "executehostcommand" => { + let cmd = extract_value("cmd", cols, vals, span)?; + Ok(ReedlineEvent::ExecuteHostCommand( + cmd.into_string("", config), + )) } v => Err(ShellError::UnsupportedConfigValue( "Reedline event".to_string(),