on master help sign in
Repeat insert command (.).
commit88bb7a1f38764c1ae4781afecbf68a7707a60c49 authorBlaž Hrastnik <blaz@mxxn.io> dateTue 30 Mar 2021, 18:19 (6 years ago) parentebfd67a
3 files changed+102−74
helix-term/src/commands.rs+1−1 helix-term/src/keymap.rs+39−38 helix-term/src/ui/editor.rs+62−35
helix-term/src/commands.rs+1−1
@@ -722,7 +722,7 @@ pub fn delete_selection(cx: &mut Context) {
722722pub fn change_selection(cx: &mut Context) {
723723 let doc = cx.doc();
724724 _delete_selection(doc);
725− insert_mode(cx);
725+ enter_insert_mode(doc);
726726}
727727
728728pub fn collapse_selection(cx: &mut Context) {
helix-term/src/keymap.rs+39−38
@@ -88,43 +88,44 @@ use std::collections::HashMap;
8888// }
8989
9090// #[cfg(feature = "term")]
91−pub use crossterm::event::{KeyCode, KeyEvent as Key, KeyModifiers as Modifiers};
91+pub use crossterm::event::{KeyCode, KeyEvent, KeyModifiers};
9292
93−pub type Keymap = HashMap<Key, Command>;
93+pub type Keymap = HashMap<KeyEvent, Command>;
9494pub type Keymaps = HashMap<Mode, Keymap>;
9595
96+#[macro_export]
9697macro_rules! key {
97− ($ch:expr) => {
98− Key {
99− code: KeyCode::Char($ch),
100− modifiers: Modifiers::NONE,
98+ ($($ch:tt)*) => {
99+ KeyEvent {
100+ code: KeyCode::Char($($ch)*),
101+ modifiers: KeyModifiers::NONE,
101102 }
102103 };
103104}
104105
105106macro_rules! shift {
106− ($ch:expr) => {
107− Key {
108− code: KeyCode::Char($ch),
109− modifiers: Modifiers::SHIFT,
107+ ($($ch:tt)*) => {
108+ KeyEvent {
109+ code: KeyCode::Char($($ch)*),
110+ modifiers: KeyModifiers::SHIFT,
110111 }
111112 };
112113}
113114
114115macro_rules! ctrl {
115− ($ch:expr) => {
116− Key {
117− code: KeyCode::Char($ch),
118− modifiers: Modifiers::CONTROL,
116+ ($($ch:tt)*) => {
117+ KeyEvent {
118+ code: KeyCode::Char($($ch)*),
119+ modifiers: KeyModifiers::CONTROL,
119120 }
120121 };
121122}
122123
123124macro_rules! alt {
124− ($ch:expr) => {
125− Key {
126− code: KeyCode::Char($ch),
127− modifiers: Modifiers::ALT,
125+ ($($ch:tt)*) => {
126+ KeyEvent {
127+ code: KeyCode::Char($($ch)*),
128+ modifiers: KeyModifiers::ALT,
128129 }
129130 };
130131}
@@ -228,26 +229,26 @@ pub fn default() -> Keymaps {
228229
229230 // C / altC = copy (repeat) selections on prev/next lines
230231
231− Key {
232+ KeyEvent {
232233 code: KeyCode::Esc,
233− modifiers: Modifiers::NONE
234+ modifiers: KeyModifiers::NONE
234235 } => commands::normal_mode,
235− Key {
236+ KeyEvent {
236237 code: KeyCode::PageUp,
237− modifiers: Modifiers::NONE
238+ modifiers: KeyModifiers::NONE
238239 } => commands::page_up,
239− Key {
240+ KeyEvent {
240241 code: KeyCode::PageDown,
241− modifiers: Modifiers::NONE
242+ modifiers: KeyModifiers::NONE
242243 } => commands::page_down,
243244 ctrl!('u') => commands::half_page_up,
244245 ctrl!('d') => commands::half_page_down,
245246
246247 ctrl!('p') => commands::file_picker,
247248 ctrl!('b') => commands::buffer_picker,
248− Key {
249+ KeyEvent {
249250 code: KeyCode::Tab,
250− modifiers: Modifiers::NONE
251+ modifiers: KeyModifiers::NONE
251252 } => commands::next_view,
252253
253254 // move under <space>c
@@ -280,9 +281,9 @@ pub fn default() -> Keymaps {
280281 shift!('T') => commands::extend_till_prev_char,
281282 shift!('F') => commands::extend_prev_char,
282283
283− Key {
284+ KeyEvent {
284285 code: KeyCode::Esc,
285− modifiers: Modifiers::NONE
286+ modifiers: KeyModifiers::NONE
286287 } => commands::exit_select_mode as Command,
287288 )
288289 .into_iter(),
@@ -294,25 +295,25 @@ pub fn default() -> Keymaps {
294295 Mode::Normal => normal,
295296 Mode::Select => select,
296297 Mode::Insert => hashmap!(
297− Key {
298+ KeyEvent {
298299 code: KeyCode::Esc,
299− modifiers: Modifiers::NONE
300+ modifiers: KeyModifiers::NONE
300301 } => commands::normal_mode as Command,
301− Key {
302+ KeyEvent {
302303 code: KeyCode::Backspace,
303− modifiers: Modifiers::NONE
304+ modifiers: KeyModifiers::NONE
304305 } => commands::insert::delete_char_backward,
305− Key {
306+ KeyEvent {
306307 code: KeyCode::Delete,
307− modifiers: Modifiers::NONE
308+ modifiers: KeyModifiers::NONE
308309 } => commands::insert::delete_char_forward,
309− Key {
310+ KeyEvent {
310311 code: KeyCode::Enter,
311− modifiers: Modifiers::NONE
312+ modifiers: KeyModifiers::NONE
312313 } => commands::insert::insert_newline,
313− Key {
314+ KeyEvent {
314315 code: KeyCode::Tab,
315− modifiers: Modifiers::NONE
316+ modifiers: KeyModifiers::NONE
316317 } => commands::insert::insert_tab,
317318
318319 ctrl!('x') => commands::completion,
helix-term/src/ui/editor.rs+62−35
@@ -1,6 +1,7 @@
11use crate::{
22 commands,
33 compositor::{Component, Compositor, Context, EventResult},
4+ key,
45 keymap::{self, Keymaps},
56 ui::text_color,
67};
@@ -27,6 +28,7 @@ pub struct EditorView {
2728 keymap: Keymaps,
2829 on_next_key: Option<Box<dyn FnOnce(&mut commands::Context, KeyEvent)>>,
2930 status_msg: Option<String>,
31+ last_insert: (commands::Command, Vec<KeyEvent>),
3032}
3133
3234const OFFSET: u16 = 7; // 1 diagnostic + 5 linenr + 1 gutter
@@ -37,6 +39,7 @@ impl EditorView {
3739 keymap: keymap::default(),
3840 on_next_key: None,
3941 status_msg: None,
42+ last_insert: (commands::normal_mode, Vec::new()),
4043 }
4144 }
4245
@@ -429,6 +432,48 @@ impl EditorView {
429432 text_color,
430433 );
431434 }
435+
436+ fn insert_mode(&self, cxt: &mut commands::Context, event: KeyEvent) {
437+ if let Some(command) = self.keymap[&Mode::Insert].get(&event) {
438+ command(cxt);
439+ } else if let KeyEvent {
440+ code: KeyCode::Char(ch),
441+ ..
442+ } = event
443+ {
444+ commands::insert::insert_char(cxt, ch);
445+ }
446+ }
447+
448+ fn command_mode(&self, mode: Mode, cxt: &mut commands::Context, event: KeyEvent) {
449+ match event {
450+ // count handling
451+ key!(i @ '0'..='9') => {
452+ let i = i.to_digit(10).unwrap() as usize;
453+ cxt.editor.count = Some(cxt.editor.count.map_or(i, |c| c * 10 + i));
454+ }
455+ // special handling for repeat operator
456+ key!('.') => {
457+ // first execute whatever put us into insert mode
458+ (self.last_insert.0)(cxt);
459+ // then replay the inputs
460+ for key in &self.last_insert.1 {
461+ self.insert_mode(cxt, *key)
462+ }
463+ }
464+ _ => {
465+ // set the count
466+ cxt.count = cxt.editor.count.take().unwrap_or(1);
467+ // TODO: edge case: 0j -> reset to 1
468+ // if this fails, count was Some(0)
469+ // debug_assert!(cxt.count != 0);
470+
471+ if let Some(command) = self.keymap[&mode].get(&event) {
472+ command(cxt);
473+ }
474+ }
475+ }
476+ }
432477}
433478
434479impl Component for EditorView {
@@ -461,50 +506,32 @@ impl Component for EditorView {
461506 } else {
462507 match mode {
463508 Mode::Insert => {
464− if let Some(command) = self.keymap[&Mode::Insert].get(&event) {
465− command(&mut cxt);
466− } else if let KeyEvent {
467− code: KeyCode::Char(c),
468− ..
469− } = event
470− {
471− commands::insert::insert_char(&mut cxt, c);
472− }
473− }
474− mode => {
475− match event {
476− KeyEvent {
477− code: KeyCode::Char(i @ '0'..='9'),
478− modifiers: KeyModifiers::NONE,
479− } => {
480− let i = i.to_digit(10).unwrap() as usize;
481− cxt.editor.count =
482− Some(cxt.editor.count.map_or(i, |c| c * 10 + i));
483− }
484− _ => {
485− // set the count
486− cxt.count = cxt.editor.count.take().unwrap_or(1);
487− // TODO: edge case: 0j -> reset to 1
488− // if this fails, count was Some(0)
489− // debug_assert!(cxt.count != 0);
490−
491− if let Some(command) = self.keymap[&mode].get(&event) {
492− command(&mut cxt);
493− }
494− }
495− }
509+ // record last_insert key
510+ self.last_insert.1.push(event);
511+
512+ self.insert_mode(&mut cxt, event)
496513 }
514+ mode => self.command_mode(mode, &mut cxt, event),
497515 }
498516 }
499−
500517 self.on_next_key = cxt.on_next_key_callback.take();
501518 self.status_msg = cxt.status_msg.take();
502−
503519 // appease borrowck
504520 let callback = cxt.callback.take();
505− drop(cxt);
521+
506522 cx.editor.ensure_cursor_in_view(cx.editor.tree.focus);
507523
524+ if mode == Mode::Normal && cx.editor.document(id).unwrap().mode() == Mode::Insert {
525+ // HAXX: if we just entered insert mode from normal, clear key buf
526+ // and record the command that got us into this mode.
527+
528+ // how we entered insert mode is important, and we should track that so
529+ // we can repeat the side effect.
530+
531+ self.last_insert.0 = self.keymap[&mode][&event];
532+ self.last_insert.1.clear();
533+ };
534+
508535 EventResult::Consumed(callback)
509536 }
510537 Event::Mouse(_) => EventResult::Ignored,