modalkit 0.0.25

A library for building applications that use modal editing
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
//! # Macro recording and execution
//!
//! ## Overview
//!
//! This module provides a wrapper for [BindingMachine] implementors. The wrapper can then process
//! [MacroAction] values, and handle recording and executing macros.
//!
//! Every [BindingMachine::input_key] call is considered to be a key actually typed at the
//! terminal, and will be treated as part of a macro recording. These terminal keypresses will
//! interrupt any pending macro keypresses, which are fed incrementally as needed during every
//! [BindingMachine::pop] call. This allows creating stop-on-error behaviour when executing
//! macros.
//!
//! ## Examples
//!
//! ```
//! use modalkit::{
//!     editing::application::EmptyInfo,
//!     editing::key::KeyManager,
//!     env::vim::keybindings::default_vim_keys,
//!     key::TerminalKey,
//! };
//!
//! let bindings = default_vim_keys::<EmptyInfo>();
//! let bindings = KeyManager::new(bindings);
//! ```
use std::borrow::Cow;
use std::collections::VecDeque;

use crate::actions::MacroAction;
use crate::errors::{EditError, EditResult};
use crate::key::MacroError;
use crate::keybindings::{dialog::Dialog, BindingMachine, InputKey};
use crate::prelude::*;

use super::{
    application::ApplicationInfo,
    context::{EditContext, Resolve},
    rope::EditRope,
    store::{RegisterPutFlags, Store},
};

const MAX_MACRO_EXEC_DEPTH: usize = 100;

/// Wraps keybindings so that they can be fed simulated keypresses from macros.
pub struct KeyManager<K, A, S>
where
    K: InputKey,
{
    bindings: Box<dyn BindingMachine<K, A, S, EditContext>>,
    keystack: VecDeque<K>,

    recording: Option<(Register, bool)>,
    macro_exec_depth: usize,
    commit_on_input: bool,
    committed: EditRope,
    pending: EditRope,
}

impl<K, A, S> KeyManager<K, A, S>
where
    K: InputKey,
{
    /// Create a new instance.
    pub fn new<B: BindingMachine<K, A, S, EditContext> + 'static>(bindings: B) -> Self {
        let bindings = Box::new(bindings);

        Self {
            bindings,
            keystack: VecDeque::new(),

            recording: None,
            macro_exec_depth: 0,
            commit_on_input: false,
            committed: EditRope::from(""),
            pending: EditRope::from(""),
        }
    }

    /// Process a macro action.
    pub fn macro_command<I: ApplicationInfo>(
        &mut self,
        act: &MacroAction,
        ctx: &EditContext,
        store: &mut Store<I>,
    ) -> EditResult<EditInfo, I>
    where
        K: InputKey<Error = MacroError>,
    {
        let (mstr, count) = match act {
            MacroAction::Execute(count) => {
                let reg = ctx.get_register().unwrap_or(Register::UnnamedMacro);
                let rope = store.registers.get_macro(reg)?;

                (rope.to_string(), ctx.resolve(count))
            },
            MacroAction::Run(s, count) => (s.clone(), ctx.resolve(count)),
            MacroAction::Repeat(count) => {
                let rope = store.registers.get_last_macro()?;
                (rope.to_string(), ctx.resolve(count))
            },
            MacroAction::ToggleRecording => {
                if let Some((reg, append)) = &self.recording {
                    // Save macro to register.
                    let mut rope = EditRope::from("");
                    std::mem::swap(&mut rope, &mut self.committed);

                    let mut flags = RegisterPutFlags::NOTEXT;

                    if *append {
                        flags |= RegisterPutFlags::APPEND;
                    }

                    store.registers.put(reg, rope.into(), flags)?;

                    // Stop recording.
                    self.recording = None;
                    self.commit_on_input = false;
                    self.pending = EditRope::from("");
                } else {
                    let reg = ctx.get_register().unwrap_or(Register::UnnamedMacro);

                    self.recording = Some((reg, ctx.get_register_append()));
                }

                return Ok(None);
            },
            act => {
                return Err(EditError::Unimplemented(format!("unknown action: {act:?}")));
            },
        };

        self.macro_exec_depth += 1;

        if self.macro_exec_depth >= MAX_MACRO_EXEC_DEPTH {
            let err = MacroError::LoopingMacro(self.macro_exec_depth);
            return Err(err.into());
        }

        for _ in 0..count {
            let mut keys = VecDeque::from(K::from_macro_str(mstr.as_ref())?);
            keys.append(&mut self.keystack);
            self.keystack = keys;
        }

        return Ok(None);
    }
}

impl<K, A, S> BindingMachine<K, A, S, EditContext> for KeyManager<K, A, S>
where
    K: InputKey + ToString,
{
    fn input_key(&mut self, key: K) {
        self.macro_exec_depth = 0;

        if self.recording.is_some() {
            let mut rope = EditRope::from(key.to_string());

            if self.commit_on_input {
                std::mem::swap(&mut self.pending, &mut rope);
                self.committed += rope;
                self.commit_on_input = false;
            } else {
                self.pending += rope;
            }
        }

        self.keystack.clear();
        self.bindings.input_key(key);
    }

    fn pop(&mut self) -> Option<(A, EditContext)> {
        loop {
            if let res @ Some(_) = self.bindings.pop() {
                self.commit_on_input = true;

                return res;
            }

            match self.keystack.pop_front() {
                Some(key) => self.bindings.input_key(key),
                None => return None,
            }
        }
    }

    fn context(&mut self) -> EditContext {
        self.bindings.context()
    }

    fn show_dialog(&mut self, max_rows: usize, max_cols: usize) -> Vec<Cow<'_, str>> {
        self.bindings.show_dialog(max_rows, max_cols)
    }

    fn show_mode(&self) -> Option<String> {
        self.bindings.show_mode()
    }

    fn reset_mode(&mut self) {
        self.bindings.reset_mode()
    }

    fn get_cursor_indicator(&self) -> Option<char> {
        self.bindings.get_cursor_indicator()
    }

    fn repeat(&mut self, seq: S, other: Option<EditContext>) {
        self.bindings.repeat(seq, other)
    }

    fn run_dialog(&mut self, dialog: Box<dyn Dialog<A>>) {
        self.bindings.run_dialog(dialog)
    }
}

#[cfg(test)]
#[macro_use]
mod tests {
    use super::*;
    use crossterm::event::{KeyCode, KeyEvent};

    use crate::{
        editing::application::EmptyInfo,
        editing::store::{RegisterError, Store},
        env::vim::VimState,
        env::CommonKeyClass,
        errors::EditError,
        key::TerminalKey,
        keybindings::EdgeEvent::{Class, Key},
        keybindings::{dialog::PromptYesNo, EmptySequence, ModalMachine, Mode, ModeKeys, Step},
    };

    type TestStore = Store<EmptyInfo>;
    type TestMachine = ModalMachine<TerminalKey, TestStep>;
    type TestKeyManager = KeyManager<TerminalKey, TestAction, EmptySequence>;

    #[derive(Clone)]
    struct TestStep(Option<TestAction>, Option<TestMode>);

    impl Step<TerminalKey> for TestStep {
        type A = TestAction;
        type State = VimState;
        type Class = CommonKeyClass;
        type M = TestMode;
        type Sequence = EmptySequence;

        fn is_unmapped(&self) -> bool {
            self.0.is_none() && self.1.is_none()
        }

        fn step(&self, ctx: &mut VimState) -> (Vec<TestAction>, Option<TestMode>) {
            let act = self.0.clone().into_iter().collect();

            ctx.action.count = ctx.action.counting;

            (act, self.1)
        }
    }

    impl From<TestAction> for TestStep {
        fn from(action: TestAction) -> Self {
            TestStep(Some(action), None)
        }
    }

    impl From<TestMode> for TestStep {
        fn from(mode: TestMode) -> Self {
            TestStep(None, Some(mode))
        }
    }

    #[derive(Clone, Copy, Debug, Default, Hash, Eq, PartialEq)]
    enum TestMode {
        #[default]
        Normal,
        Insert,
    }

    impl Mode<TestAction, VimState> for TestMode {}

    impl ModeKeys<TerminalKey, TestAction, VimState> for TestMode {
        fn unmapped(
            &self,
            key: &TerminalKey,
            _: &mut VimState,
        ) -> (Vec<TestAction>, Option<TestMode>) {
            match self {
                TestMode::Normal => {
                    return (vec![], None);
                },
                TestMode::Insert => {
                    if let Some(c) = key.get_char() {
                        return (vec![TestAction::Type(c)], None);
                    }

                    return (vec![], None);
                },
            }
        }
    }

    #[derive(Clone, Debug, Default, Eq, PartialEq)]
    enum TestAction {
        Macro(MacroAction),
        SetFlag(bool),
        Type(char),
        #[default]
        NoOp,
    }

    fn setup_recursive_bindings() -> (TestKeyManager, TestStore) {
        use crate::keybindings::EdgeRepeat::{Min, Once};

        let mut bindings = TestMachine::empty();
        let store = Store::default();

        // Normal mode mappings
        bindings.add_mapping(
            TestMode::Normal,
            &[(Once, Key("n".parse().unwrap()))],
            &TestAction::NoOp.into(),
        );
        bindings.add_mapping(
            TestMode::Normal,
            &[(Once, Key("a".parse().unwrap()))],
            &TestAction::Macro(MacroAction::Run("n".into(), Count::Contextual)).into(),
        );
        bindings.add_mapping(
            TestMode::Normal,
            &[(Once, Key("b".parse().unwrap()))],
            &TestAction::Macro(MacroAction::Run("aa".into(), Count::Contextual)).into(),
        );
        bindings.add_mapping(
            TestMode::Normal,
            &[(Once, Key("c".parse().unwrap()))],
            &TestAction::Macro(MacroAction::Run("c".into(), Count::Contextual)).into(),
        );

        // Normal mode prefixes
        bindings.add_prefix(
            TestMode::Normal,
            &[
                (Once, Key("\"".parse().unwrap())),
                (Once, Class(CommonKeyClass::Register)),
            ],
            &None,
        );
        bindings.add_prefix(TestMode::Normal, &[(Min(1), Class(CommonKeyClass::Count))], &None);

        (TestKeyManager::new(bindings), store)
    }

    fn setup_bindings(skip_confirm: bool) -> (TestKeyManager, TestStore) {
        use crate::keybindings::EdgeRepeat::{Min, Once};

        let mut bindings = TestMachine::empty();
        let store = Store::default();

        // Normal mode mappings
        bindings.add_mapping(
            TestMode::Normal,
            &[
                (Once, Key("q".parse().unwrap())),
                (Once, Key("q".parse().unwrap())),
                (Once, Key("q".parse().unwrap())),
            ],
            &TestAction::Macro(MacroAction::ToggleRecording).into(),
        );
        bindings.add_mapping(
            TestMode::Normal,
            &[(Once, Key("@".parse().unwrap()))],
            &TestAction::Macro(MacroAction::Repeat(Count::Contextual)).into(),
        );
        bindings.add_mapping(
            TestMode::Normal,
            &[(Once, Key("Q".parse().unwrap()))],
            &TestAction::Macro(MacroAction::Execute(Count::Contextual)).into(),
        );
        bindings.add_mapping(
            TestMode::Normal,
            &[(Once, Key("f".parse().unwrap()))],
            &TestAction::SetFlag(skip_confirm).into(),
        );
        bindings.add_mapping(
            TestMode::Normal,
            &[(Once, Key("i".parse().unwrap()))],
            &TestMode::Insert.into(),
        );

        // Normal mode prefixes
        bindings.add_prefix(
            TestMode::Normal,
            &[
                (Once, Key("\"".parse().unwrap())),
                (Once, Class(CommonKeyClass::Register)),
            ],
            &None,
        );
        bindings.add_prefix(TestMode::Normal, &[(Min(1), Class(CommonKeyClass::Count))], &None);

        // Insert mode mappings
        bindings.add_mapping(
            TestMode::Insert,
            &[(Once, Key("<Esc>".parse().unwrap()))],
            &TestMode::Normal.into(),
        );

        (TestKeyManager::new(bindings), store)
    }

    fn input(
        key: TerminalKey,
        bindings: &mut TestKeyManager,
        store: &mut TestStore,
        s: &mut String,
        flag: &mut bool,
        noops: &mut usize,
        err: &mut Option<EditError<EmptyInfo>>,
    ) {
        *noops = 0;
        *err = None;

        bindings.input_key(key);

        while let Some((act, ctx)) = bindings.pop() {
            match act {
                TestAction::NoOp => {
                    *noops += 1;
                    continue;
                },
                TestAction::Macro(act) => {
                    *err = bindings.macro_command(&act, &ctx, store).err();

                    if err.is_some() {
                        return;
                    }
                },
                TestAction::SetFlag(skip_confirm) => {
                    if skip_confirm {
                        *flag = true;
                    } else {
                        let act = vec![TestAction::SetFlag(true)];
                        let msg = "Are you sure you want to set the flag";
                        let confirm = PromptYesNo::new(msg, act);
                        bindings.run_dialog(Box::new(confirm));
                    }
                },
                TestAction::Type(c) => s.push(c),
            }
        }
    }

    #[test]
    fn test_macro_run() {
        let (mut bindings, mut store) = setup_bindings(true);
        let ctx = EditContext::from(VimState::<EmptyInfo>::default());

        // Run a sequence of key presses twice.
        let run = MacroAction::Run("flif<Esc>fi".into(), 2.into());
        let _ = bindings.macro_command(&run, &ctx, &mut store).unwrap();

        // Run the first time, starting in Normal mode.
        assert_pop1!(bindings, TestAction::SetFlag(true), ctx);
        assert_pop1!(bindings, TestAction::NoOp, ctx);
        assert_pop1!(bindings, TestAction::NoOp, ctx);
        assert_pop1!(bindings, TestAction::Type('f'), ctx);
        assert_pop1!(bindings, TestAction::NoOp, ctx);
        assert_pop1!(bindings, TestAction::SetFlag(true), ctx);
        assert_pop1!(bindings, TestAction::NoOp, ctx);

        // Run the second time, but this time in Insert mode.
        assert_pop1!(bindings, TestAction::Type('f'), ctx);
        assert_pop1!(bindings, TestAction::Type('l'), ctx);
        assert_pop1!(bindings, TestAction::Type('i'), ctx);
        assert_pop1!(bindings, TestAction::Type('f'), ctx);
        assert_pop1!(bindings, TestAction::NoOp, ctx);
        assert_pop1!(bindings, TestAction::SetFlag(true), ctx);
        assert_pop2!(bindings, TestAction::NoOp, ctx);
    }

    #[test]
    fn test_record_and_execute() {
        let (mut bindings, mut store) = setup_bindings(true);
        let mut s = String::new();
        let mut flag = false;
        let mut err = None;
        let mut noops = 0;

        macro_rules! get_register {
            ($reg: expr) => {
                store.registers.get(&$reg).unwrap().value
            };
        }

        macro_rules! input {
            ($key: expr) => {
                input($key, &mut bindings, &mut store, &mut s, &mut flag, &mut noops, &mut err)
            };
        }

        // Register::UnnamedMacro is currently empty.
        assert_eq!(get_register!(Register::UnnamedMacro).to_string(), "");

        // No last macro to repeat.
        input!(key!('@'));
        assert!(matches!(err, Some(EditError::Register(RegisterError::NoLastMacro))), "{:?}", err);

        // Press an unmapped key before recording.
        input!(key!('l'));

        // Start recording to "a.
        input!(key!('"'));
        input!(key!('a'));
        input!(key!('q'));
        input!(key!('q'));
        input!(key!('q'));

        // Type some unmapped keys.
        input!(key!('n'));
        input!(key!('z'));

        // Type a mapped key.
        input!(key!('f'));

        // Move to Insert mode and type some characters.
        input!(key!('i'));
        input!(key!('a'));
        input!(key!('b'));
        input!(key!('c'));

        // Move back to Normal mode.
        input!("<Esc>".parse().unwrap());

        // End recording.
        input!(key!('q'));
        input!(key!('q'));
        input!(key!('q'));

        // Register::Named('a') now contains macro text.
        assert_eq!(get_register!(Register::Named('a')).to_string(), "nzfiabc<Esc>");
        assert_eq!(s, "abc");
        assert_eq!(flag, true);

        // Reset flag before replaying.
        flag = false;

        // Replay macro twice.
        input!(key!('"'));
        input!(key!('a'));
        input!(key!('2'));
        input!(key!('Q'));
        assert_eq!(s, "abcabcabc");
        assert_eq!(flag, true);

        // Reset flag before replaying.
        flag = false;

        // Replay last macro.
        input!(key!('@'));
        assert_eq!(s, "abcabcabcabc");
        assert_eq!(flag, true);

        // Reset flag before replaying.
        flag = false;

        // Record an unnamed macro that executes "a.
        input!(key!('q'));
        input!(key!('q'));
        input!(key!('q'));
        input!(key!('i'));
        input!(key!('d'));
        input!(key!('e'));
        input!(key!('f'));
        input!("<Esc>".parse().unwrap());
        input!(key!('"'));
        input!(key!('a'));
        input!(key!('Q'));
        input!(key!('q'));
        input!(key!('q'));
        input!(key!('q'));

        // Register::UnnamedMacro now contains macro text.
        assert_eq!(get_register!(Register::UnnamedMacro).to_string(), "idef<Esc>\"aQ");
        assert_eq!(get_register!(Register::Named('a')).to_string(), "nzfiabc<Esc>");
        assert_eq!(s, "abcabcabcabcdefabc");
        assert_eq!(flag, true);

        // Reset flag before replaying.
        flag = false;

        // Execute Register::UnnamedMacro.
        input!(key!('3'));
        input!(key!('Q'));
        assert_eq!(s, "abcabcabcabcdefabcdefabcdefabcdefabc");
        assert_eq!(flag, true);

        // Reset flag before replaying.
        flag = false;

        // Replaying replays "a, since that was last used inside "@.
        input!(key!('2'));
        input!(key!('@'));
        assert_eq!(s, "abcabcabcabcdefabcdefabcdefabcdefabcabcabc");
        assert_eq!(flag, true);
    }

    #[test]
    fn test_macro_dialog() {
        let (mut bindings, mut store) = setup_bindings(false);
        let mut s = String::new();
        let mut flag = false;
        let mut err = None;
        let mut noops = 0;

        macro_rules! get_register {
            ($reg: expr) => {
                store.registers.get(&$reg).unwrap().value
            };
        }

        macro_rules! input {
            ($key: expr) => {
                input($key, &mut bindings, &mut store, &mut s, &mut flag, &mut noops, &mut err)
            };
        }

        assert_eq!(bindings.show_dialog(10, 100).len(), 0);
        assert_eq!(flag, false);

        // Start recording to "a.
        input!(key!('"'));
        input!(key!('a'));
        input!(key!('q'));
        input!(key!('q'));
        input!(key!('q'));

        // Typing `f` starts a new dialog.
        input!(key!('f'));
        assert_eq!(bindings.show_dialog(10, 100).len(), 1);
        assert_eq!(flag, false);

        // Answer `y` to the prompt to clear dialog and set flag.
        input!(key!('y'));
        assert_eq!(bindings.show_dialog(10, 100).len(), 0);
        assert_eq!(flag, true);

        // End recording.
        input!(key!('q'));
        input!(key!('q'));
        input!(key!('q'));

        // Register::Named('a') now contains macro text.
        assert_eq!(get_register!(Register::Named('a')).to_string(), "fy");

        // Now reset the flag.
        flag = false;

        // Replay macro.
        input!(key!('"'));
        input!(key!('a'));
        input!(key!('Q'));

        // Flag is now true, and we end with the dialog cleared.
        assert_eq!(bindings.show_dialog(10, 100).len(), 0);
        assert_eq!(flag, true);
    }

    #[test]
    fn test_macro_limit_depth() {
        let (mut bindings, mut store) = setup_recursive_bindings();
        let mut s = String::new();
        let mut flag = false;
        let mut err = None;
        let mut noops = 0;

        macro_rules! input {
            ($key: expr) => {
                input($key, &mut bindings, &mut store, &mut s, &mut flag, &mut noops, &mut err)
            };
        }

        // It's okay to run a MacroAction::Run with a count >100.
        input!(key!('1'));
        input!(key!('1'));
        input!(key!('0'));
        input!(key!('a'));
        assert!(err.is_none());
        assert_eq!(noops, 110);

        // It's okay to run a macro that calls other macros multiple times.
        input!(key!('4'));
        input!(key!('9'));
        input!(key!('b'));
        assert!(err.is_none());
        assert_eq!(noops, 98);

        // But if they repeatedly call other macros enough times, we abort.
        input!(key!('5'));
        input!(key!('0'));
        input!(key!('b'));
        println!("err = {err:?}");
        assert!(matches!(
            err.take().unwrap(),
            EditError::MacroFailure(MacroError::LoopingMacro(100))
        ));
        assert_eq!(noops, 98);

        // And a key that just always triggers itself should error.
        input!(key!('c'));
        println!("err = {err:?}");
        assert!(matches!(
            err.take().unwrap(),
            EditError::MacroFailure(MacroError::LoopingMacro(100))
        ));
        assert_eq!(noops, 0);
    }
}