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
//! The operator-pending state machine. SPEC.md §7.2.
//!
//! Normal-mode input accumulates as:
//!
//! register? · count₁? · (operator · count₂? · motion|object) | action
//!
//! Every keypress either EXTENDS the pending state, RESOLVES it into a
//! `Command`, or INVALIDATES it. `partial` additionally handles multi-key
//! sequences — the user keymap's own (`<leader>ff`) and the built-in prefixes
//! (`gg`, `gb`, `<C-w>l`) through the same path.
//!
//! This machine holds NO editor state: it never sees the buffer, so what it
//! emits is always a complete command for `app` to run. That is the whole point
//! of the split — every panel and mode `app` grows from here adds no key
//! handling to it.
use super::action::{Action, Operator, Target};
use super::bindings::{self, Command, Verb};
use super::keymap::{Keymap, Lookup};
/// Which mode's grammar to resolve against.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum Table {
Normal,
Visual,
}
impl Table {
fn name(self) -> &'static str {
match self {
Table::Normal => "normal",
Table::Visual => "visual",
}
}
fn is_visual(self) -> bool {
matches!(self, Table::Visual)
}
}
/// A key the machine owes an argument to before it can resolve.
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
enum Awaiting {
/// After `f`/`F`/`t`/`T` — the character to seek.
FindChar { forward: bool, till: bool },
/// After `r` — the replacement character.
ReplaceChar,
/// After `i`/`a` — the object's key.
Object { around: bool },
/// After `"` — the register name.
Register,
/// After a built-in prefix key (`g`, `z`, `<C-w>`).
Prefix(char),
}
#[derive(Default)]
pub struct Pending {
/// The count typed for the motion (or for a bare action).
pub count: Option<usize>,
/// The count typed BEFORE an operator. vim multiplies the two.
pub op_count: Option<usize>,
pub operator: Option<Operator>,
/// The key that set `operator`, so pressing it again is the doubled
/// linewise form (`dd`, `guu`) whatever that key happens to be bound to.
op_key: Option<Key>,
pub register: Option<char>,
/// Keys typed so far that have not yet resolved to a binding.
pub partial: Vec<Key>,
awaiting: Option<Awaiting>,
/// The key currently being fed, so a verb can know what pressed it.
last_key: Option<Key>,
}
/// A normalized key. Config strings like `<C-s>` parse into this.
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub struct Key {
pub code: KeyCode,
pub ctrl: bool,
pub alt: bool,
pub shift: bool,
}
impl Key {
/// Normalize a crossterm key event into a `Key`, or `None` for events we do
/// not bind (modifier-only presses, function keys, …). A `Shift`+letter is
/// folded into the uppercase char, so `shift` is only set for named keys.
pub fn from_event(ev: ratatui::crossterm::event::KeyEvent) -> Option<Key> {
use ratatui::crossterm::event::{KeyCode as Ct, KeyModifiers};
let code = match ev.code {
Ct::Char(c) => KeyCode::Char(c),
Ct::Enter => KeyCode::Enter,
Ct::Esc => KeyCode::Esc,
// Shift-Tab arrives as its own key; folded back to Tab + Shift so
// a config's `<S-Tab>` names it.
Ct::Tab | Ct::BackTab => KeyCode::Tab,
Ct::Backspace => KeyCode::Backspace,
Ct::Delete => KeyCode::Delete,
Ct::Up => KeyCode::Up,
Ct::Down => KeyCode::Down,
Ct::Left => KeyCode::Left,
Ct::Right => KeyCode::Right,
Ct::Home => KeyCode::Home,
Ct::End => KeyCode::End,
Ct::PageUp => KeyCode::PageUp,
Ct::PageDown => KeyCode::PageDown,
_ => return None,
};
let m = ev.modifiers;
Some(Key {
code,
ctrl: m.contains(KeyModifiers::CONTROL),
alt: m.contains(KeyModifiers::ALT),
// A letter already carries its case; only flag Shift for named keys.
shift: (m.contains(KeyModifiers::SHIFT) && !matches!(code, KeyCode::Char(_)))
|| ev.code == Ct::BackTab,
})
}
pub const fn char(c: char) -> Key {
Key {
code: KeyCode::Char(c),
ctrl: false,
alt: false,
shift: false,
}
}
/// The character this key types, if it is a plain one.
fn as_char(&self) -> Option<char> {
match self.code {
KeyCode::Char(c) if !self.ctrl && !self.alt => Some(c),
_ => None,
}
}
}
#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash)]
pub enum KeyCode {
Char(char),
Enter,
Esc,
Tab,
Backspace,
Delete,
Up,
Down,
Left,
Right,
Home,
End,
PageUp,
PageDown,
}
pub enum Resolution {
/// Keep collecting — the sequence is a valid prefix, or a count is growing.
Pending,
/// Run this, then reset.
Command(Command),
/// Nothing matches. Reset; the key is spent.
Invalid,
}
impl Pending {
pub fn reset(&mut self) {
*self = Pending::default();
}
/// A clean state with no half-typed command — a command boundary, which is
/// what dot-recording and the user keymap both key off.
/// The built-in prefix waiting for its second key (`g`, `z`, `Ctrl-w`).
pub fn prefix(&self) -> Option<char> {
match self.awaiting {
Some(Awaiting::Prefix(p)) => Some(p),
_ => None,
}
}
pub fn is_clean(&self) -> bool {
self.count.is_none()
&& self.op_count.is_none()
&& self.operator.is_none()
&& self.register.is_none()
&& self.partial.is_empty()
&& self.awaiting.is_none()
}
/// Feed one key. This is the whole of Normal- and Visual-mode input.
pub fn feed(&mut self, key: Key, keymap: &Keymap, table: Table) -> Resolution {
self.last_key = Some(key);
// 1. An owed argument beats everything: `f` takes the next key whatever
// it is bound to, or `fd` would delete instead of seeking a `d`.
if let Some(awaiting) = self.awaiting.take() {
return self.take_argument(awaiting, key, keymap, table);
}
// 2. The user keymap, checked before the built-ins so a binding can
// shadow one. It applies with an operator pending too — that is what
// lets a rebound MOTION complete a rebound operator, which is the
// whole reason the grammar moved behind the keymap.
if let Some(res) = self.user_binding(key, keymap, table) {
return res;
}
// 3. The operator's own key again is its doubled linewise form: `dd`,
// and equally `guu`, where the second `u` is bound to undo.
if let (Some(op), Some(k)) = (self.operator, self.op_key) {
if k == key {
return self.emit(Action::Operator { op, target: Target::Line });
}
}
// 4. A count prefix. A leading `0` is the LineStart motion, not a digit.
if let Some(c @ '0'..='9') = key.as_char() {
if c != '0' || self.count.is_some() {
let d = c as usize - '0' as usize;
self.count = Some(self.count.unwrap_or(0).saturating_mul(10) + d);
return Resolution::Pending;
}
}
// 5. The built-in grammar.
let verb = match table {
Table::Normal => bindings::normal(key),
Table::Visual => bindings::visual(key),
};
match verb {
Some(v) => self.apply_verb(v, table),
None => {
self.reset();
Resolution::Invalid
}
}
}
/// Resolve a key against the user's `[keys.*]` table, collecting a
/// multi-key sequence if one is in flight. `None` means "not ours" — a
/// lone unbound key falls through to the built-in grammar.
fn user_binding(&mut self, key: Key, keymap: &Keymap, table: Table) -> Option<Resolution> {
self.partial.push(key);
match keymap.lookup(table.name(), &self.partial) {
Lookup::Found(verb) => {
let verb = verb.clone();
self.partial.clear();
Some(self.apply_verb(verb, table))
}
Lookup::Prefix => Some(Resolution::Pending),
Lookup::None => {
// A fresh single key with no binding falls through to the
// built-ins; a longer failed sequence is simply dropped.
let lone = self.partial.len() == 1;
self.partial.clear();
if lone {
None
} else {
self.reset();
Some(Resolution::Invalid)
}
}
}
}
/// Fold one resolved verb into the pending state.
fn apply_verb(&mut self, verb: Verb, table: Table) -> Resolution {
match verb {
Verb::Motion(m) => self.with_target(Target::Motion(m), table),
Verb::Goto(fallback) => {
// `5G` is line 5; a bare `G` is the buffer end. The count is
// the TARGET here, so it must not also repeat the motion —
// and one typed before an operator counts too: `5dG` deletes
// to line 5, and `2d3G` to line 6, the counts multiplying.
let m = match (self.op_count.take(), self.count.take()) {
(None, None) => fallback,
(op, n) => {
let line = op.unwrap_or(1).saturating_mul(n.unwrap_or(1));
crate::text::motion::Motion::GotoLine(line.saturating_sub(1))
}
};
self.with_target(Target::Motion(m), table)
}
Verb::Operator(op) => {
if table.is_visual() {
// The selection is already the range.
return self.emit(Action::Operator { op, target: Target::Selection });
}
if self.operator == Some(op) {
// `dd` — the doubled form is linewise on the current lines.
return self.emit(Action::Operator { op, target: Target::Line });
}
// A different pending operator (`dc`) is invalid: the command
// is dropped, as vim cancels it.
if self.operator.is_some() {
self.reset();
return Resolution::Invalid;
}
self.operator = Some(op);
self.op_key = self.partial.last().copied().or(self.last_key);
// Set the count typed so far aside for the motion's own count.
self.op_count = self.count.take();
Resolution::Pending
}
Verb::Object { around } => {
if !table.is_visual() && self.operator.is_none() {
// Bare `i`/`a` in Normal mode are Insert and Append; the
// grammar only reads them as objects behind an operator.
return self.apply_verb(
Verb::Act(if around { Action::Append } else { Action::Insert }),
table,
);
}
self.awaiting = Some(Awaiting::Object { around });
Resolution::Pending
}
Verb::Find { forward, till } => {
self.awaiting = Some(Awaiting::FindChar { forward, till });
Resolution::Pending
}
Verb::ReplaceChar => {
self.awaiting = Some(Awaiting::ReplaceChar);
Resolution::Pending
}
Verb::Register => {
self.awaiting = Some(Awaiting::Register);
Resolution::Pending
}
Verb::Prefix(p) => {
self.awaiting = Some(Awaiting::Prefix(p));
Resolution::Pending
}
// Esc always resolves: it is how you abandon a half-typed command.
Verb::Act(action @ Action::NormalMode) => {
self.reset();
Resolution::Command(Command::bare(action))
}
Verb::Act(action) => {
// Any other direct action does not combine with a pending
// operator; a dangling one makes the key a no-op.
if self.operator.is_some() {
self.reset();
return Resolution::Invalid;
}
self.emit(action)
}
}
}
/// A motion or object: it gives a pending operator its range, or moves.
fn with_target(&mut self, target: Target, table: Table) -> Resolution {
match self.operator.take() {
Some(op) => self.emit(Action::Operator { op, target }),
None => match (target, table.is_visual()) {
// In Visual an object becomes the selection rather than a range
// to operate on: `vi(` selects, ready for any verb.
(Target::Object { key, around }, true) => {
self.emit(Action::SelectObject { key, around })
}
(Target::Motion(m), _) => self.emit(Action::Move(m)),
(Target::Object { key, around }, false) => {
self.emit(Action::SelectObject { key, around })
}
_ => {
self.reset();
Resolution::Invalid
}
},
}
}
/// The key an argument-taking verb was waiting for.
fn take_argument(
&mut self,
awaiting: Awaiting,
key: Key,
keymap: &Keymap,
table: Table,
) -> Resolution {
match awaiting {
Awaiting::FindChar { forward, till } => match key.as_char() {
Some(target) => self.with_target(
Target::Motion(crate::text::motion::Motion::FindChar { target, forward, till }),
table,
),
None => {
self.reset();
Resolution::Invalid
}
},
Awaiting::ReplaceChar => match key.as_char() {
Some(c) => self.emit(Action::ReplaceChar(c)),
None => {
self.reset();
Resolution::Invalid
}
},
Awaiting::Object { around } => match key.as_char() {
Some(obj) => self.with_target(Target::Object { key: obj, around }, table),
None => {
self.reset();
Resolution::Invalid
}
},
// A register prefix qualifies the command still to come, so unlike
// every other prefix it leaves the pending count and operator
// alone: `"a2dd` is one command.
Awaiting::Register => {
if let Some(name) = key.as_char() {
if name.is_ascii_alphanumeric() || matches!(name, '"' | '+' | '*') {
self.register = Some(name);
}
}
Resolution::Pending
}
Awaiting::Prefix(p) => match bindings::after_prefix(p, key, table.is_visual()) {
Some(verb) => self.apply_verb(verb, table),
// An unknown second key drops the sequence, as vim does — but
// the user keymap may still bind it (`z` is a config prefix).
None => match self.user_binding(key, keymap, table) {
Some(res) => res,
None => {
self.reset();
Resolution::Invalid
}
},
},
}
}
/// Package the pending state into a command and clear it.
fn emit(&mut self, action: Action) -> Resolution {
let count = self.take_count();
let register = self.register;
self.reset();
Resolution::Command(Command::new(action, count, register))
}
/// Consume the pending count(s), defaulting to 1. An operator count and a
/// motion count MULTIPLY, as in vim.
fn take_count(&mut self) -> usize {
let motion = self.count.take().unwrap_or(1).max(1);
let operator = self.op_count.take().unwrap_or(1).max(1);
motion.saturating_mul(operator)
}
}
#[cfg(test)]
mod tests {
use super::*;
use crate::config::schema::KeysConfig;
use crate::text::motion::Motion;
fn keymap() -> Keymap {
Keymap::from_config(&KeysConfig::default(), " ").0
}
/// Feed a string of plain keys, returning every command that resolved.
fn run(p: &mut Pending, keys: &str, table: Table) -> Vec<Command> {
let km = keymap();
let mut out = Vec::new();
for c in keys.chars() {
if let Resolution::Command(cmd) = p.feed(Key::char(c), &km, table) {
out.push(cmd);
}
}
out
}
fn one(keys: &str) -> Command {
let mut p = Pending::default();
let cmds = run(&mut p, keys, Table::Normal);
assert_eq!(cmds.len(), 1, "{keys:?} should resolve exactly once: {cmds:?}");
cmds.into_iter().next().unwrap()
}
#[test]
fn a_bare_motion_moves() {
let cmd = one("w");
assert_eq!(cmd.action, Action::Move(Motion::WordForward { big: false }));
assert_eq!(cmd.count, 1);
}
#[test]
fn counts_multiply_across_the_operator() {
let cmd = one("2d3w");
assert_eq!(
cmd.action,
Action::Operator {
op: Operator::Delete,
target: Target::Motion(Motion::WordForward { big: false }),
}
);
assert_eq!(cmd.count, 6, "2 × 3");
}
#[test]
fn a_doubled_operator_is_linewise() {
let cmd = one("dd");
assert_eq!(
cmd.action,
Action::Operator { op: Operator::Delete, target: Target::Line }
);
// `3dd` is three lines.
assert_eq!(one("3dd").count, 3);
}
#[test]
fn a_register_survives_the_count_and_operator() {
let cmd = one("\"a2dd");
assert_eq!(cmd.register, Some('a'));
assert_eq!(cmd.count, 2);
assert_eq!(
cmd.action,
Action::Operator { op: Operator::Delete, target: Target::Line }
);
}
#[test]
fn text_objects_need_an_operator_in_normal_mode() {
assert_eq!(
one("diw").action,
Action::Operator {
op: Operator::Delete,
target: Target::Object { key: 'w', around: false },
}
);
// Bare `i` is Insert, not an object.
assert_eq!(one("i").action, Action::Insert);
assert_eq!(one("a").action, Action::Append);
}
#[test]
fn find_takes_the_next_key_whatever_it_is_bound_to() {
assert_eq!(
one("fd").action,
Action::Move(Motion::FindChar { target: 'd', forward: true, till: false }),
"`fd` seeks a `d`; it must not start a delete"
);
assert_eq!(
one("dt,").action,
Action::Operator {
op: Operator::Delete,
target: Target::Motion(Motion::FindChar {
target: ',',
forward: true,
till: true
}),
}
);
}
#[test]
fn g_is_a_prefix_for_motions_and_writer_verbs() {
assert_eq!(one("gg").action, Action::Move(Motion::BufferStart));
assert_eq!(one("5gg").action, Action::Move(Motion::GotoLine(4)));
assert_eq!(one("gb").action, Action::ToggleBold);
assert_eq!(one("g3").action, Action::SetHeading(3));
assert_eq!(
one("gUiw").action,
Action::Operator {
op: Operator::Uppercase,
target: Target::Object { key: 'w', around: false },
}
);
}
#[test]
fn a_count_before_goto_is_the_line_number() {
assert_eq!(one("42G").action, Action::Move(Motion::GotoLine(41)));
assert_eq!(one("42G").count, 1, "the count was spent on the target");
assert_eq!(one("G").action, Action::Move(Motion::BufferEnd));
}
#[test]
fn visual_operators_fire_on_the_selection() {
let mut p = Pending::default();
let cmds = run(&mut p, "d", Table::Visual);
assert_eq!(
cmds[0].action,
Action::Operator { op: Operator::Delete, target: Target::Selection }
);
// `u`/`U` are the case operators in Visual, not undo.
let mut p = Pending::default();
assert_eq!(
run(&mut p, "u", Table::Visual)[0].action,
Action::Operator { op: Operator::Lowercase, target: Target::Selection }
);
// An object SELECTS rather than operating.
let mut p = Pending::default();
assert_eq!(
run(&mut p, "i(", Table::Visual)[0].action,
Action::SelectObject { key: '(', around: false }
);
}
#[test]
fn a_dangling_operator_swallows_a_direct_action() {
let mut p = Pending::default();
let cmds = run(&mut p, "dp", Table::Normal);
assert!(cmds.is_empty(), "`dp` is not a command: {cmds:?}");
assert!(p.is_clean(), "and it leaves nothing pending");
}
/// The point of the relocation: config can move an OPERATOR, and it keeps
/// its whole grammar — counts, motions, objects, the doubled form — which
/// the old action-only overlay could not express at all.
#[test]
fn config_can_rebind_the_grammar_itself() {
let mut cfg = KeysConfig::default();
cfg.normal.insert("s".into(), "operator_delete".into());
cfg.normal.insert("m".into(), "word_forward".into());
let (km, warnings) = Keymap::from_config(&cfg, " ");
assert!(warnings.is_empty(), "{warnings:?}");
let mut p = Pending::default();
let fire = |p: &mut Pending, keys: &str| -> Option<Command> {
let mut last = None;
for c in keys.chars() {
if let Resolution::Command(cmd) = p.feed(Key::char(c), &km, Table::Normal) {
last = Some(cmd);
}
}
last
};
// The rebound operator takes a rebound motion, with counts.
let cmd = fire(&mut p, "2s3m").expect("s + m resolves");
assert_eq!(
cmd.action,
Action::Operator {
op: Operator::Delete,
target: Target::Motion(Motion::WordForward { big: false }),
}
);
assert_eq!(cmd.count, 6);
// …its doubled form works on the new key, not on `d`.
let cmd = fire(&mut p, "ss").expect("ss is the linewise form");
assert_eq!(
cmd.action,
Action::Operator { op: Operator::Delete, target: Target::Line }
);
// …and it takes text objects.
let cmd = fire(&mut p, "siw").expect("s + object");
assert_eq!(
cmd.action,
Action::Operator {
op: Operator::Delete,
target: Target::Object { key: 'w', around: false },
}
);
}
#[test]
fn escape_clears_a_half_typed_command() {
let mut p = Pending::default();
let km = keymap();
p.feed(Key::char('2'), &km, Table::Normal);
p.feed(Key::char('d'), &km, Table::Normal);
assert!(!p.is_clean());
let esc = Key { code: KeyCode::Esc, ctrl: false, alt: false, shift: false };
match p.feed(esc, &km, Table::Normal) {
Resolution::Command(cmd) => assert_eq!(cmd.action, Action::NormalMode),
_ => panic!("Esc resolves to NormalMode"),
}
assert!(p.is_clean());
}
}