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
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
use alloc::{string::String, vec::Vec};
use core::{fmt, mem};
use crate::{Event, Key, Motion, Operator, Parser, TextObject, Word};
pub const VI_DEFAULT_REGISTER: char = '"';
#[derive(Debug)]
pub struct ViContext<F: FnMut(Event)> {
callback: F,
selection: bool,
pending_change: Option<Vec<Event>>,
change: Option<Vec<Event>>,
set_mode: Option<ViMode>,
}
impl<F: FnMut(Event)> ViContext<F> {
fn start_change(&mut self) {
if self.pending_change.is_none() {
self.pending_change = Some(Vec::new());
}
(self.callback)(Event::ChangeStart);
}
fn finish_change(&mut self) {
self.change = self.pending_change.take();
(self.callback)(Event::ChangeFinish);
}
fn e(&mut self, event: Event) {
match &mut self.pending_change {
Some(change) => change.push(event.clone()),
None => {}
}
(self.callback)(event);
}
}
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
pub struct ViCmd {
register: Option<char>,
count: Option<usize>,
operator: Option<Operator>,
motion: Option<Motion>,
text_object: Option<TextObject>,
}
impl fmt::Display for ViCmd {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
if let Some(register) = self.register {
write!(f, "\"{register}")?;
}
if let Some(count) = self.count {
write!(f, "{count}")?;
}
if let Some(operator) = self.operator {
write!(f, "{operator:?}")?;
}
if let Some(motion) = self.motion {
write!(f, "{motion:?}")?;
}
if let Some(text_object) = self.text_object {
write!(f, "{text_object:?}")?;
}
Ok(())
}
}
impl ViCmd {
/// Repeat the provided function count times, resetting count after
pub fn repeat<F: FnMut(usize)>(&mut self, mut f: F) {
for i in 0..self.count.take().unwrap_or(1) {
f(i);
}
}
/// Set motion
pub fn motion<F: FnMut(Event)>(&mut self, motion: Motion, ctx: &mut ViContext<F>) {
self.motion = Some(motion);
self.run(ctx);
}
/// Set operator, may set motion if operator is doubled like `dd`
pub fn operator<F: FnMut(Event)>(&mut self, operator: Operator, ctx: &mut ViContext<F>) {
if self.operator == Some(operator) {
self.motion = Some(Motion::Line);
} else {
self.operator = Some(operator);
}
self.run(ctx);
}
/// Set text object and return true if supported by the motion
pub fn text_object<F: FnMut(Event)>(
&mut self,
text_object: TextObject,
ctx: &mut ViContext<F>,
) -> bool {
if !self.motion.map_or(false, |motion| motion.text_object()) {
// Did not need text object
return false;
}
// Needed text object
self.text_object = Some(text_object);
self.run(ctx);
true
}
/// Run operation, resetting it to defaults if it runs
pub fn run<F: FnMut(Event)>(&mut self, ctx: &mut ViContext<F>) -> bool {
match self.motion {
Some(motion) => {
if motion.text_object() && self.text_object.is_none() {
// After or inside requires a text object
return false;
}
}
None => {
if !ctx.selection {
// No motion requires a selection
return false;
}
}
}
let register = self.register.take().unwrap_or(VI_DEFAULT_REGISTER);
let count = self.count.take().unwrap_or(1);
let motion = self.motion.take().unwrap_or(Motion::Selection);
let text_object = self.text_object.take();
//TODO: clean up logic of Motion, such that actual motions and references to
// text objects and selections are not in the same enum
match self.operator.take() {
Some(operator) => {
ctx.start_change();
match motion {
Motion::Around => ctx.e(Event::SelectTextObject(
text_object.expect("no text object"),
true,
)),
Motion::Inside => ctx.e(Event::SelectTextObject(
text_object.expect("no text object"),
false,
)),
Motion::Line => {
ctx.e(Event::SelectLineStart);
}
Motion::Selection => {}
_ => {
ctx.e(Event::SelectStart);
for _ in 0..count {
ctx.e(Event::Motion(motion));
}
}
}
let mut enter_insert_mode = false;
match operator {
Operator::AutoIndent => {
ctx.e(Event::AutoIndent);
}
Operator::Change => {
ctx.e(Event::Yank { register });
ctx.e(Event::Delete);
enter_insert_mode = true;
}
Operator::Delete => {
ctx.e(Event::Yank { register });
ctx.e(Event::Delete);
}
Operator::ShiftLeft => {
ctx.e(Event::ShiftLeft);
}
Operator::ShiftRight => {
ctx.e(Event::ShiftRight);
}
Operator::SwapCase => {
ctx.e(Event::SwapCase);
}
Operator::Yank => {
ctx.e(Event::Yank { register });
}
}
ctx.e(Event::SelectClear);
if enter_insert_mode {
ctx.set_mode = Some(ViMode::Insert);
} else {
ctx.finish_change();
ctx.set_mode = Some(ViMode::Normal);
}
}
None => match motion {
Motion::Around => ctx.e(Event::SelectTextObject(
text_object.expect("no text object"),
true,
)),
Motion::Inside => ctx.e(Event::SelectTextObject(
text_object.expect("no text object"),
false,
)),
_ => {
for _ in 0..count {
ctx.e(Event::Motion(motion));
}
}
},
}
true
}
}
#[derive(Clone, Debug, Eq, PartialEq)]
pub enum ViMode {
/// Normal mode
Normal,
/// Waiting for another character to complete command
Extra(char),
/// Insert mode
Insert,
/// Replace mode
Replace,
/// Visual mode
Visual,
/// Visual line mode
VisualLine,
/// Command mode
Command { value: String },
/// Search mode
Search { value: String, forwards: bool },
}
#[derive(Debug)]
pub struct ViParser {
pub mode: ViMode,
pub cmd: ViCmd,
pub register_mode: ViMode,
pub semicolon_motion: Option<Motion>,
pub pending_change: Option<Vec<Event>>,
pub last_change: Option<Vec<Event>>,
}
impl ViParser {
pub fn new() -> Self {
Self {
mode: ViMode::Normal,
cmd: ViCmd::default(),
register_mode: ViMode::Normal,
semicolon_motion: None,
pending_change: None,
last_change: None,
}
}
}
impl Parser for ViParser {
fn reset(&mut self) {
self.mode = ViMode::Normal;
self.cmd = ViCmd::default();
}
fn parse<F: FnMut(Event)>(&mut self, key: Key, selection: bool, callback: F) {
// Makes composing commands easier
let cmd = &mut self.cmd;
// Normalize key, so we don't deal with control characters below
let key = key.normalize();
// Makes managing callbacks easier
let mut ctx = ViContext {
selection,
callback,
pending_change: self.pending_change.take(),
change: None,
set_mode: None,
};
let ctx = &mut ctx;
match self.mode {
ViMode::Normal | ViMode::Visual | ViMode::VisualLine => match key {
Key::Backspace => cmd.motion(Motion::Left, ctx),
//TODO: what should backtab do?
Key::Backtab => (),
Key::Delete => {
ctx.start_change();
cmd.repeat(|_| ctx.e(Event::DeleteInLine));
ctx.finish_change();
}
Key::Down => cmd.motion(Motion::Down, ctx),
Key::End => cmd.motion(Motion::End, ctx),
Key::Enter => {
cmd.motion(Motion::Down, ctx);
cmd.motion(Motion::SoftHome, ctx);
}
Key::Escape => {
self.reset();
ctx.e(Event::Escape);
}
Key::Home => cmd.motion(Motion::Home, ctx),
Key::Left => cmd.motion(Motion::LeftInLine, ctx),
Key::PageDown => cmd.motion(Motion::PageDown, ctx),
Key::PageUp => cmd.motion(Motion::PageUp, ctx),
Key::Right => cmd.motion(Motion::RightInLine, ctx),
//TODO: what should tab do?
Key::Tab => (),
Key::Up => cmd.motion(Motion::Up, ctx),
Key::Char(c) => match c {
// Enter insert mode after cursor (if not awaiting text object)
'a' => {
if cmd.operator.is_some() || self.mode != ViMode::Normal {
cmd.motion(Motion::Around, ctx);
} else {
ctx.start_change();
ViCmd::default().motion(Motion::Right, ctx);
self.mode = ViMode::Insert;
}
}
// Enter insert mode at end of line
'A' => {
ctx.start_change();
ViCmd::default().motion(Motion::End, ctx);
self.mode = ViMode::Insert;
}
// Previous word (if not text object)
'b' => {
if !cmd.text_object(TextObject::Block, ctx) {
cmd.motion(Motion::PreviousWordStart(Word::Lower), ctx);
}
}
// Previous WORD (if not text object)
//TODO: should this TextObject be different?
'B' => {
if !cmd.text_object(TextObject::Block, ctx) {
cmd.motion(Motion::PreviousWordStart(Word::Upper), ctx);
}
}
// Change mode
'c' => {
cmd.operator(Operator::Change, ctx);
}
// Change to end of line
'C' => {
cmd.operator(Operator::Change, ctx);
cmd.motion(Motion::End, ctx);
}
// Delete mode
'd' => {
cmd.operator(Operator::Delete, ctx);
}
// Delete to end of line
'D' => {
cmd.operator(Operator::Delete, ctx);
cmd.motion(Motion::End, ctx);
}
// End of word
'e' => cmd.motion(Motion::NextWordEnd(Word::Lower), ctx),
// End of WORD
'E' => cmd.motion(Motion::NextWordEnd(Word::Upper), ctx),
// Find char forwards
'f' => {
self.mode = ViMode::Extra(c);
}
// Find char backwords
'F' => {
self.mode = ViMode::Extra(c);
}
// g commands
'g' => {
self.mode = ViMode::Extra(c);
}
// Goto line (or end of file)
'G' => match cmd.count.take() {
Some(line) => cmd.motion(Motion::GotoLine(line), ctx),
None => cmd.motion(Motion::GotoEof, ctx),
},
// Left (in line)
'h' => cmd.motion(Motion::LeftInLine, ctx),
// Top of screen
'H' => cmd.motion(Motion::ScreenHigh, ctx),
// Enter insert mode at cursor (if not awaiting text object)
'i' => {
if cmd.operator.is_some() || self.mode != ViMode::Normal {
cmd.motion(Motion::Inside, ctx);
} else {
ctx.start_change();
self.mode = ViMode::Insert;
}
}
// Enter insert mode at start of line
'I' => {
ctx.start_change();
ViCmd::default().motion(Motion::SoftHome, ctx);
self.mode = ViMode::Insert;
}
// Down
'j' => cmd.motion(Motion::Down, ctx),
//TODO: Join lines
'J' => {}
// Up
'k' => cmd.motion(Motion::Up, ctx),
//TODO: Look up keyword (vim looks up word under cursor in man pages)
'K' => {}
// Right (in line)
'l' => cmd.motion(Motion::RightInLine, ctx),
// Bottom of screen
'L' => cmd.motion(Motion::ScreenLow, ctx),
//TODO: Set mark
'm' => {}
// Middle of screen
'M' => cmd.motion(Motion::ScreenMiddle, ctx),
// Next search item
'n' => cmd.motion(Motion::NextSearch, ctx),
// Previous search item
'N' => cmd.motion(Motion::PreviousSearch, ctx),
// Create line after and enter insert mode
'o' => {
ctx.start_change();
ViCmd::default().motion(Motion::End, ctx);
ctx.e(Event::NewLine);
self.mode = ViMode::Insert;
}
// Create line before and enter insert mode
'O' => {
ctx.start_change();
ViCmd::default().motion(Motion::Home, ctx);
ctx.e(Event::NewLine);
ViCmd::default().motion(Motion::Up, ctx);
self.mode = ViMode::Insert;
}
// Paste after (if not text object)
'p' => {
if !cmd.text_object(TextObject::Paragraph, ctx) {
let register = cmd.register.unwrap_or(VI_DEFAULT_REGISTER);
ctx.e(Event::Put {
register,
after: true,
});
}
}
// Paste before
'P' => {
let register = cmd.register.unwrap_or(VI_DEFAULT_REGISTER);
ctx.e(Event::Put {
register,
after: false,
});
}
//TODO: q, Q
// Replace char
'r' => {
self.mode = ViMode::Extra(c);
}
// Replace mode
'R' => {
ctx.start_change();
self.mode = ViMode::Replace;
}
// Substitute char (if not text object)
's' => {
if !cmd.text_object(TextObject::Sentence, ctx) {
ctx.start_change();
cmd.repeat(|_| ctx.e(Event::DeleteInLine));
self.mode = ViMode::Insert;
}
}
// Substitute line
'S' => {
cmd.operator(Operator::Change, ctx);
cmd.motion(Motion::Line, ctx);
}
// Until character forwards (if not text object)
't' => {
if !cmd.text_object(TextObject::Tag, ctx) {
self.mode = ViMode::Extra(c);
}
}
// Until character backwards
'T' => {
self.mode = ViMode::Extra(c);
}
// Undo
'u' => {
ctx.e(Event::Undo);
}
//TODO: U
// Enter visual mode
'v' => {
//TODO: this is very hacky and has bugs
if self.mode == ViMode::Visual {
ctx.e(Event::SelectClear);
self.mode = ViMode::Normal;
} else {
ctx.e(Event::SelectStart);
self.mode = ViMode::Visual;
}
}
// Enter line visual mode
'V' => {
if self.mode == ViMode::VisualLine {
ctx.e(Event::SelectClear);
self.mode = ViMode::Normal;
} else {
ctx.e(Event::SelectLineStart);
self.mode = ViMode::VisualLine;
}
}
// Next word (if not text object)
'w' => {
if !cmd.text_object(TextObject::Word(Word::Lower), ctx) {
cmd.motion(Motion::NextWordStart(Word::Lower), ctx);
}
}
// Next WORD (if not text object)
'W' => {
if !cmd.text_object(TextObject::Word(Word::Upper), ctx) {
cmd.motion(Motion::NextWordStart(Word::Upper), ctx);
}
}
// Remove character at cursor
'x' => {
ctx.start_change();
cmd.repeat(|_| ctx.e(Event::DeleteInLine));
ctx.finish_change();
}
// Remove character before cursor
'X' => {
ctx.start_change();
cmd.repeat(|_| ctx.e(Event::BackspaceInLine));
ctx.finish_change();
}
// Yank
'y' => cmd.operator(Operator::Yank, ctx),
// Yank line
'Y' => {
cmd.operator(Operator::Yank, ctx);
cmd.motion(Motion::Line, ctx);
}
// z commands
'z' => {
self.mode = ViMode::Extra(c);
}
// Z commands
'Z' => {
self.mode = ViMode::Extra(c);
}
// Go to start of line
'0' => match cmd.count {
Some(ref mut count) => {
*count = count.saturating_mul(10);
}
None => {
cmd.motion(Motion::Home, ctx);
}
},
// Count of next action
'1'..='9' => {
let number = (c as u32).saturating_sub('0' as u32) as usize;
cmd.count = Some(match cmd.count.take() {
Some(count) => count.saturating_mul(10).saturating_add(number),
None => number,
});
}
// TODO (if not text object)
'`' => if !cmd.text_object(TextObject::Ticks, ctx) {},
// Swap case
'~' => cmd.operator(Operator::SwapCase, ctx),
// TODO: !, @, #
// Go to end of line
'$' => cmd.motion(Motion::End, ctx),
//TODO: %
// Go to start of line after whitespace
'^' => cmd.motion(Motion::SoftHome, ctx),
//TODO &, *
// TODO (if not text object)
'(' => if !cmd.text_object(TextObject::Parentheses, ctx) {},
// TODO (if not text object)
')' => if !cmd.text_object(TextObject::Parentheses, ctx) {},
// Move up and soft home
'-' => {
cmd.motion(Motion::Up, ctx);
cmd.motion(Motion::SoftHome, ctx);
}
// Move down and soft home
'+' => {
cmd.motion(Motion::Down, ctx);
cmd.motion(Motion::SoftHome, ctx);
}
// Auto indent
'=' => cmd.operator(Operator::AutoIndent, ctx),
// TODO (if not text object)
'[' => if !cmd.text_object(TextObject::SquareBrackets, ctx) {},
// TODO (if not text object)
'{' => if !cmd.text_object(TextObject::CurlyBrackets, ctx) {},
// TODO (if not text object)
']' => if !cmd.text_object(TextObject::SquareBrackets, ctx) {},
// TODO (if not text object)
'}' => if !cmd.text_object(TextObject::CurlyBrackets, ctx) {},
// Repeat f/F/t/T
';' => {
if let Some(motion) = self.semicolon_motion {
cmd.motion(motion, ctx);
}
}
// Enter command mode
':' => {
self.mode = ViMode::Command {
value: String::new(),
};
}
//TODO (if not text object)
'\'' => if !cmd.text_object(TextObject::SingleQuotes, ctx) {},
// Select register (if not text object)
'"' => {
if !cmd.text_object(TextObject::DoubleQuotes, ctx) {
self.register_mode = self.mode.clone();
self.mode = ViMode::Extra(c);
}
}
// Reverse f/F/t/T
',' => {
if let Some(motion) = self.semicolon_motion {
if let Some(reverse) = motion.reverse() {
cmd.motion(reverse, ctx);
}
}
}
// Unindent (if not text object)
'<' => {
if !cmd.text_object(TextObject::AngleBrackets, ctx) {
cmd.operator(Operator::ShiftLeft, ctx);
}
}
// Repeat change
'.' => {
if let Some(change) = &self.last_change {
ctx.start_change();
for event in change.iter() {
ctx.e(event.clone());
}
ctx.finish_change();
}
}
// Indent (if not text object)
'>' => {
if !cmd.text_object(TextObject::AngleBrackets, ctx) {
cmd.operator(Operator::ShiftRight, ctx);
}
}
// Enter search mode
'/' => {
self.mode = ViMode::Search {
value: String::new(),
forwards: true,
};
}
// Enter search backwards mode
'?' => {
self.mode = ViMode::Search {
value: String::new(),
forwards: false,
};
}
// Right
' ' => cmd.motion(Motion::Right, ctx),
_ => {}
},
Key::Ctrl(c) => {
//TODO: Ctrl characters
}
},
ViMode::Extra(extra) => match extra {
// Find/till character
'f' | 'F' | 't' | 'T' => {
match key {
Key::Char(c) => {
let motion = match extra {
'f' => Motion::NextChar(c),
'F' => Motion::PreviousChar(c),
't' => Motion::NextCharTill(c),
'T' => Motion::PreviousCharTill(c),
_ => unreachable!(),
};
cmd.motion(motion, ctx);
self.semicolon_motion = Some(motion);
}
_ => {}
}
self.reset();
}
// Extra commands
'g' => {
match key {
Key::Char(c) => match c {
// Previous word end
'e' => cmd.motion(Motion::PreviousWordEnd(Word::Lower), ctx),
// Prevous WORD end
'E' => cmd.motion(Motion::PreviousWordEnd(Word::Upper), ctx),
'g' => match cmd.count.take() {
Some(line) => cmd.motion(Motion::GotoLine(line), ctx),
None => cmd.motion(Motion::GotoLine(1), ctx),
},
'n' => {
cmd.motion(Motion::Inside, ctx);
cmd.text_object(TextObject::Search { forwards: true }, ctx);
}
'N' => {
cmd.motion(Motion::Inside, ctx);
cmd.text_object(TextObject::Search { forwards: false }, ctx);
}
//TODO: more g commands
_ => {}
},
//TODO: what do control keys do in this mode?
_ => {}
}
self.reset();
}
// Replace character
'r' => {
match key {
Key::Char(c) => {
//TODO: a visual selection allows replacing all characters
ctx.start_change();
ctx.e(Event::Delete);
ctx.e(Event::Insert(c));
ViCmd::default().motion(Motion::LeftInLine, ctx);
ctx.finish_change();
}
_ => {}
}
self.reset();
}
// Select register
'"' => {
match key {
Key::Char(c) => {
cmd.register = Some(c);
}
_ => {}
}
self.mode = self.register_mode.clone();
self.register_mode = ViMode::Normal;
}
_ => {
//TODO
log::info!("TODO: extra command {:?}{:?}", extra, key);
self.reset();
}
},
ViMode::Insert | ViMode::Replace => match key {
//TODO: FINISH CHANGE ON MOTION?
Key::Backspace => ctx.e(Event::Backspace),
Key::Backtab => ctx.e(Event::ShiftLeft),
Key::Char(c) => {
if self.mode == ViMode::Replace {
ctx.e(Event::Delete);
}
ctx.e(Event::Insert(c));
}
Key::Ctrl(c) => {
//TODO: control characters
}
Key::Down => ViCmd::default().motion(Motion::Down, ctx),
Key::Delete => ctx.e(Event::Delete),
Key::End => ViCmd::default().motion(Motion::End, ctx),
Key::Enter => ctx.e(Event::NewLine),
Key::Escape => {
ViCmd::default().motion(Motion::LeftInLine, ctx);
ctx.finish_change();
self.reset();
}
Key::Home => ViCmd::default().motion(Motion::Home, ctx),
Key::Left => ViCmd::default().motion(Motion::LeftInLine, ctx),
Key::PageDown => ViCmd::default().motion(Motion::PageDown, ctx),
Key::PageUp => ViCmd::default().motion(Motion::PageUp, ctx),
Key::Right => ViCmd::default().motion(Motion::RightInLine, ctx),
Key::Tab => ctx.e(Event::ShiftRight),
Key::Up => ViCmd::default().motion(Motion::Up, ctx),
},
ViMode::Command { ref mut value } => match key {
Key::Escape => {
self.reset();
}
Key::Enter => {
//TODO: run command
self.reset();
}
Key::Backspace => {
if value.pop().is_none() {
self.reset();
}
}
Key::Char(c) => {
value.push(c);
}
_ => {
//TODO: more keys
}
},
ViMode::Search {
ref mut value,
forwards,
} => match key {
Key::Escape => {
self.reset();
}
Key::Enter => {
// Swap search value to avoid allocations
let mut tmp = String::new();
mem::swap(value, &mut tmp);
ctx.e(Event::SetSearch(tmp, forwards));
self.reset();
ViCmd::default().motion(Motion::NextSearch, ctx);
}
Key::Backspace => {
if value.pop().is_none() {
self.reset();
}
}
Key::Char(c) => {
value.push(c);
}
_ => {
//TODO: more keys
}
},
}
// Reset mode after operators
if let Some(mode) = ctx.set_mode.take() {
self.mode = mode;
}
// Save change state
self.pending_change = ctx.pending_change.take();
if let Some(change) = ctx.change.take() {
self.last_change = Some(change);
}
//TODO: optimize redraw
ctx.e(Event::Redraw);
}
}