-
Notifications
You must be signed in to change notification settings - Fork 12
/
vim.go
1524 lines (1223 loc) · 37.2 KB
/
vim.go
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
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
package readline
import (
"unicode"
"github.com/reeflective/readline/inputrc"
"github.com/reeflective/readline/internal/keymap"
"github.com/reeflective/readline/internal/strutil"
)
// commands maps widget names to their implementation.
type commands map[string]func()
// viCommands returns all Vim commands.
// Under each comment are gathered all commands related to the comment's
// subject. When there are two subgroups separated by an empty line, the
// second one comprises commands that are not legacy readline commands.
//
// Modes
// Moving
// Changing text
// Killing and Yanking
// Selecting text
// Miscellaneous.
func (rl *Shell) viCommands() commands {
return map[string]func(){
// Modes
"vi-append-mode": rl.viAddNext,
"vi-append-eol": rl.viAddEol,
"vi-insertion-mode": rl.viInsertMode,
"vi-insert-beg": rl.viInsertBol,
"vi-movement-mode": rl.viCommandMode,
"vi-visual-mode": rl.viVisualMode,
"vi-editing-mode": rl.viInsertMode,
"vi-visual-line-mode": rl.viVisualLineMode,
// Movement
"vi-backward-char": rl.viBackwardChar,
"vi-forward-char": rl.viForwardChar,
"vi-prev-word": rl.viBackwardWord,
"vi-next-word": rl.viForwardWord,
"vi-backward-word": rl.viBackwardWord,
"vi-forward-word": rl.viForwardWord,
"vi-backward-bigword": rl.viBackwardBlankWord,
"vi-forward-bigword": rl.viForwardBlankWord,
"vi-end-word": rl.viForwardWordEnd,
"vi-end-bigword": rl.viForwardBlankWordEnd,
"vi-match": rl.viMatchBracket,
"vi-column": rl.viGotoColumn,
"vi-end-of-line": rl.viEndOfLine,
"vi-back-to-indent": rl.viBackToIndent,
"vi-first-print": rl.viFirstPrint,
"vi-goto-mark": rl.viGotoMark,
"vi-backward-end-word": rl.viBackwardWordEnd,
"vi-backward-end-bigword": rl.viBackwardBlankWordEnd,
// Changing text
"vi-change-to": rl.viChangeTo,
"vi-delete-to": rl.viDeleteTo,
"vi-delete": rl.viDeleteChar,
"vi-change-char": rl.viChangeChar,
"vi-backward-delete-char": rl.viBackwardDeleteChar,
"vi-replace": rl.viReplace, // missing vi-overstrike-delete
"vi-overstrike": rl.viReplace,
"vi-change-case": rl.viChangeCase,
"vi-subst": rl.viSubstitute,
"vi-change-eol": rl.viChangeEol,
"vi-add-surround": rl.viAddSurround,
"vi-open-line-above": rl.viOpenLineAbove,
"vi-open-line-below": rl.viOpenLineBelow,
"vi-down-case": rl.viDownCase,
"vi-up-case": rl.viUpCase,
// Kill and Yanking
"vi-kill-eol": rl.viKillEol,
"vi-unix-word-rubout": rl.backwardKillWord,
"vi-rubout": rl.viRubout,
"vi-yank-to": rl.viYankTo,
"vi-yank-pop": rl.yankPop,
"vi-yank-arg": rl.yankLastArg,
"vi-kill-line": rl.viKillLine,
"vi-put": rl.viPut,
"vi-put-after": rl.viPutAfter,
"vi-put-before": rl.viPutBefore,
"vi-set-buffer": rl.viSetBuffer,
"vi-yank-whole-line": rl.viYankWholeLine,
// Selecting text
"select-a-blank-word": rl.viSelectABlankWord,
"select-a-shell-word": rl.viSelectAShellWord,
"select-a-word": rl.viSelectAWord,
"select-in-blank-word": rl.viSelectInBlankWord,
"select-in-shell-word": rl.viSelectInShellWord,
"select-in-word": rl.viSelectInWord,
"vi-select-inside": rl.viSelectInside,
"vi-select-surround": rl.viSelectSurround,
// Miscellaneous
"vi-eof-maybe": rl.viEOFMaybe,
"vi-search": rl.viSearch,
"vi-search-again": rl.viSearchAgain,
"vi-arg-digit": rl.viArgDigit,
"vi-char-search": rl.viCharSearch,
"vi-set-mark": rl.viSetMark,
"vi-edit-and-execute-command": rl.viEditAndExecuteCommand,
"vi-undo": rl.undoLast,
"vi-redo": rl.viRedo,
"vi-edit-command-line": rl.viEditCommandLine,
"vi-find-next-char": rl.viFindNextChar,
"vi-find-next-char-skip": rl.viFindNextCharSkip,
"vi-find-prev-char": rl.viFindPrevChar,
"vi-find-prev-char-skip": rl.viFindPrevCharSkip,
"vi-search-forward": rl.viSearchForward,
"vi-search-backward": rl.viSearchBackward,
"vi-search-again-forward": rl.viSearchAgainForward,
"vi-search-again-backward": rl.viSearchAgainBackward,
}
}
//
// Modes ----------------------------------------------------------------
//
// Enter Vim insertion mode.
func (rl *Shell) viInsertMode() {
rl.History.Save()
// Reset any visual selection and iterations.
rl.selection.Reset()
rl.Iterations.Reset()
rl.Buffers.Reset()
// Change the keymap and mark the insertion point.
rl.Keymap.SetLocal("")
rl.Keymap.SetMain(keymap.ViInsert)
rl.cursor.SetMark()
}
// Enter Vim command mode.
func (rl *Shell) viCommandMode() {
// Reset any visual selection and iterations.
rl.selection.Reset()
rl.Iterations.Reset()
rl.Buffers.Reset()
// Cancel completions and hints if any, and reassign the
// current line/cursor/selection for the cursor check below
// to be effective. This is needed when in isearch mode.
rl.Hint.Reset()
rl.completer.Reset()
rl.line, rl.cursor, rl.selection = rl.completer.GetBuffer()
// Only go back if not in insert mode
if rl.Keymap.Main() == keymap.ViInsert && !rl.cursor.AtBeginningOfLine() {
rl.cursor.Dec()
}
// Update the cursor position, keymap and insertion point.
rl.cursor.CheckCommand()
rl.Keymap.SetLocal("")
rl.Keymap.SetMain(keymap.ViCommand)
}
// Enter Vim visual mode.
func (rl *Shell) viVisualMode() {
rl.History.SkipSave()
rl.Iterations.Reset()
rl.Buffers.Reset()
// Cancel completions and hints if any.
rl.Hint.Reset()
rl.completer.Reset()
// Mark the selection as visual at the current cursor position.
rl.selection.Mark(rl.cursor.Pos())
rl.selection.Visual(false)
rl.Keymap.SetLocal(keymap.Visual)
}
// Enter Vim visual mode, selecting the entire
// line on which the cursor is currently.
func (rl *Shell) viVisualLineMode() {
rl.History.SkipSave()
rl.Iterations.Reset()
rl.Buffers.Reset()
rl.Hint.Reset()
rl.completer.Reset()
// Mark the selection as visual at the current
// cursor position, in visual line mode.
rl.selection.Mark(rl.cursor.Pos())
rl.selection.Visual(true)
rl.Keymap.SetLocal(keymap.Visual)
rl.Keymap.PrintCursor(keymap.Visual)
}
// Go to the beginning of the current line, and enter Vim insert mode.
func (rl *Shell) viInsertBol() {
rl.Iterations.Reset()
rl.beginningOfLine()
rl.viInsertMode()
}
// Enter insert mode on the next character.
func (rl *Shell) viAddNext() {
if rl.line.Len() > 0 {
rl.cursor.Inc()
}
rl.viInsertMode()
}
// Go to the end of the current line, and enter insert mode.
func (rl *Shell) viAddEol() {
rl.Iterations.Reset()
if rl.Keymap.Local() == keymap.Visual {
rl.cursor.Inc()
rl.viInsertMode()
return
}
rl.endOfLine()
rl.viInsertMode()
}
//
// Movement -------------------------------------------------------------
//
// Move forward one character, without changing lines.
func (rl *Shell) viForwardChar() {
// Only exception where we actually don't forward a character.
if rl.Config.GetBool("history-autosuggest") && rl.cursor.Pos() == rl.line.Len()-1 {
rl.autosuggestAccept()
return
}
rl.History.SkipSave()
// In vi-cmd-mode, we don't go further than the
// last character in the line, hence rl.line-1
if rl.Keymap.Main() != keymap.ViInsert && rl.cursor.Pos() < rl.line.Len()-1 {
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
if (*rl.line)[rl.cursor.Pos()+1] == '\n' {
break
}
rl.cursor.Inc()
}
}
}
// Move backward one character, without changing lines.
func (rl *Shell) viBackwardChar() {
rl.History.SkipSave()
vii := rl.Iterations.Get()
if rl.cursor.Pos() == 0 {
return
}
for i := 1; i <= vii; i++ {
if (*rl.line)[rl.cursor.Pos()-1] == '\n' {
break
}
rl.cursor.Dec()
}
}
// Move to the beginning of the previous word, vi-style.
func (rl *Shell) viBackwardWord() {
rl.History.SkipSave()
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
backward := rl.line.Backward(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(backward)
}
}
// Move to the beginning of the next word.
func (rl *Shell) viForwardWord() {
rl.History.Save()
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
// When we have an autosuggested history and if we are at the end
// of the line, insert the next word from this suggested line.
rl.insertAutosuggestPartial(false)
forward := rl.line.Forward(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(forward)
}
}
// Move backward one word, where a word is defined as a series of non-blank characters.
func (rl *Shell) viBackwardBlankWord() {
rl.History.SkipSave()
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
backward := rl.line.Backward(rl.line.TokenizeSpace, rl.cursor.Pos())
rl.cursor.Move(backward)
}
}
// Move forward one word, where a word is defined as a series of non-blank characters.
func (rl *Shell) viForwardBlankWord() {
rl.History.SkipSave()
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
forward := rl.line.Forward(rl.line.TokenizeSpace, rl.cursor.Pos())
rl.cursor.Move(forward)
}
}
// Move to the end of the previous word, vi-style.
func (rl *Shell) viBackwardWordEnd() {
rl.History.SkipSave()
vii := rl.Iterations.Get()
if rl.line.Len() == 0 {
return
}
for i := 1; i <= vii; i++ {
rl.cursor.Inc()
rl.cursor.Move(rl.line.Backward(rl.line.Tokenize, rl.cursor.Pos()))
rl.cursor.Move(rl.line.Backward(rl.line.Tokenize, rl.cursor.Pos()))
// Then move forward, adjusting if we are on a punctuation.
if unicode.IsPunct(rl.cursor.Char()) {
rl.cursor.Dec()
}
rl.cursor.Move(rl.line.ForwardEnd(rl.line.Tokenize, rl.cursor.Pos()))
}
}
func (rl *Shell) viForwardWordEnd() {
rl.History.SkipSave()
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
forward := rl.line.ForwardEnd(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(forward)
}
}
// Move to the end of the previous word, where a word is defined as a series of non-blank characters.
func (rl *Shell) viBackwardBlankWordEnd() {
rl.History.SkipSave()
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
rl.cursor.Inc()
rl.cursor.Move(rl.line.Backward(rl.line.TokenizeSpace, rl.cursor.Pos()))
rl.cursor.Move(rl.line.Backward(rl.line.TokenizeSpace, rl.cursor.Pos()))
rl.cursor.Move(rl.line.ForwardEnd(rl.line.TokenizeSpace, rl.cursor.Pos()))
}
}
// Move to the end of the current word, or, if at the end
// of the current word, to the end of the next word, where
// a word is defined as a series of non-blank characters.
func (rl *Shell) viForwardBlankWordEnd() {
rl.History.SkipSave()
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
rl.cursor.Move(rl.line.ForwardEnd(rl.line.TokenizeSpace, rl.cursor.Pos()))
}
}
// Move to the bracket character (one of {}, () or []) that matches the one under
// the cursor. If the cursor is not on a bracket character, move forward without
// going past the end of the line to find one, and then go to the matching bracket.
func (rl *Shell) viMatchBracket() {
rl.History.SkipSave()
nextPos := rl.cursor.Pos()
found := false
// If we are on a bracket/brace/parenthesis, we just find the matcher
if !strutil.IsBracket(rl.cursor.Char()) {
for i := rl.cursor.Pos() + 1; i < rl.line.Len(); i++ {
char := (*rl.line)[i]
if char == '}' || char == ')' || char == ']' {
nextPos = i - rl.cursor.Pos()
found = true
break
}
}
if !found {
return
}
rl.cursor.Move(nextPos)
}
var adjust int
split, index, pos := rl.line.TokenizeBlock(rl.cursor.Pos())
switch {
case len(split) == 0:
return
case pos == 0:
adjust = len(split[index])
default:
adjust = pos * -1
}
rl.cursor.Move(adjust)
}
// Move to the column specified by the numeric argument.
func (rl *Shell) viGotoColumn() {
rl.History.SkipSave()
column := rl.Iterations.Get()
if column < 0 {
return
}
cpos := rl.cursor.Pos()
rl.cursor.BeginningOfLine()
bpos := rl.cursor.Pos()
rl.cursor.EndOfLine()
epos := rl.cursor.Pos()
rl.cursor.Set(cpos)
switch {
case column > epos-cpos:
rl.cursor.Set(epos)
default:
rl.cursor.Set(bpos + column - 1)
}
}
// Move to the end of the line, vi-style.
func (rl *Shell) viEndOfLine() {
rl.History.SkipSave()
// We use append so that any y$ / d$
// will include the last character.
rl.cursor.EndOfLineAppend()
}
// Move to the first non-blank character after cursor.
func (rl *Shell) viFirstPrint() {
rl.cursor.BeginningOfLine()
rl.cursor.ToFirstNonSpace(true)
}
// Move to the first non-blank character in the line.
func (rl *Shell) viBackToIndent() {
rl.cursor.BeginningOfLine()
rl.cursor.ToFirstNonSpace(true)
}
// Move to the specified mark.
func (rl *Shell) viGotoMark() {
switch {
case rl.selection.Active():
// We either an active selection, in which case
// we go to the position (begin or end) that is
// set and not equal to the cursor.
bpos, epos := rl.selection.Pos()
if bpos != rl.cursor.Pos() {
rl.cursor.Set(bpos)
} else {
rl.cursor.Set(epos)
}
case rl.cursor.Mark() != -1:
// Or we go to the cursor mark, which was set when
// entering insert mode. This might have no effect.
rl.cursor.Set(rl.cursor.Mark())
}
}
//
// Changing Text --------------------------------------------------------
//
// Read a movement command from the keyboard, and kill from the cursor
// position to the endpoint of the movement. Then enter insert mode.
// If the command is vi-change, change the current line.
func (rl *Shell) viChangeTo() {
switch {
case rl.Keymap.IsPending():
// In vi operator pending mode, it's that we've been called
// twice in a row (eg. `cc`), so copy the entire current line.
rl.Keymap.CancelPending()
rl.History.Save()
rl.selection.Mark(rl.cursor.Pos())
rl.selection.Visual(true)
rl.selection.Cut()
rl.viInsertMode()
case len(rl.selection.Surrounds()) == 2:
// In surround selection mode, change the surrounding chars.
rl.Display.Refresh()
defer rl.selection.Reset()
// Now read another key
done := rl.Keymap.PendingCursor()
defer done()
rchar, isAbort := rl.Keys.ReadKey()
if isAbort {
return
}
rl.History.Save()
// There might be a matching equivalent.
bchar, echar := strutil.MatchSurround(rchar)
surrounds := rl.selection.Surrounds()
bpos, _ := surrounds[0].Pos()
epos, _ := surrounds[1].Pos()
(*rl.line)[bpos] = bchar
(*rl.line)[epos] = echar
case rl.selection.Active():
// In visual mode, we have just have a selection to delete.
rl.History.Save()
rl.adjustSelectionPending()
cpos := rl.selection.Cursor()
cut := rl.selection.Cut()
rl.Buffers.Write([]rune(cut)...)
rl.cursor.Set(cpos)
rl.viInsertMode()
default:
// Since we must emulate the default readline behavior,
// we vary our behavior depending on the caller key.
keys := rl.Keys.Caller()
switch keys[0] {
case 'c':
rl.Keymap.Pending()
rl.selection.Mark(rl.cursor.Pos())
case 'C':
rl.viChangeEol()
}
}
}
// Read a movement command from the keyboard, and kill from the cursor
// position to the endpoint of the movement. If the command is vi-delete,
// kill the current line.
func (rl *Shell) viDeleteTo() {
switch {
case rl.Keymap.IsPending():
// In vi operator pending mode, it's that we've been called
// twice in a row (eg. `dd`), so delete the entire current line.
rl.Keymap.CancelPending()
rl.History.Save()
rl.selection.Mark(rl.cursor.Pos())
rl.selection.Visual(true)
cpos := rl.selection.Cursor()
text := rl.selection.Cut()
// Get buffer and add newline if there isn't one at the end
if len(text) > 0 && rune(text[len(text)-1]) != inputrc.Newline {
text += string(inputrc.Newline)
}
rl.Buffers.Write([]rune(text)...)
rl.cursor.Set(cpos)
case rl.selection.Active():
// In visual mode, or with a non-empty selection, just cut it.
rl.History.Save()
rl.adjustSelectionPending()
cpos := rl.selection.Cursor()
cut := rl.selection.Cut()
rl.Buffers.Write([]rune(cut)...)
rl.cursor.Set(cpos)
rl.viCommandMode()
default:
// Since we must emulate the default readline behavior,
// we vary our behavior depending on the caller key.
keys := rl.Keys.Caller()
switch keys[0] {
case 'd':
rl.Keymap.Pending()
rl.selection.Mark(rl.cursor.Pos())
case 'D':
rl.viKillEol()
}
}
}
// Delete the character under the cursor, without going past the end of the line.
func (rl *Shell) viDeleteChar() {
if rl.line.Len() == 0 || rl.cursor.Pos() == rl.line.Len() {
return
}
rl.History.Save()
cutBuf := make([]rune, 0)
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
cutBuf = append(cutBuf, rl.cursor.Char())
rl.line.CutRune(rl.cursor.Pos())
}
rl.Buffers.Write(cutBuf...)
}
// Replace the character under the cursor with a character read from the keyboard.
func (rl *Shell) viChangeChar() {
rl.History.Save()
// We read a character to use first.
done := rl.Keymap.PendingCursor()
defer done()
key, isAbort := rl.Keys.ReadKey()
if isAbort {
rl.History.SkipSave()
return
}
switch {
case rl.selection.Active() && rl.selection.IsVisual():
// In visual mode, we replace all chars of the selection
rl.selection.ReplaceWith(func(r rune) rune {
return key
})
default:
// Or simply the character under the cursor.
rl.cursor.ReplaceWith(key)
}
}
// Delete the character behind the cursor, without changing lines.
func (rl *Shell) viBackwardDeleteChar() {
if !rl.cursor.AtBeginningOfLine() {
rl.backwardDeleteChar()
}
}
// Enter overwrite mode.
func (rl *Shell) viReplace() {
// The the standard emacs replace loop,
// which blocks until the ESC is pressed
rl.overwriteMode()
// And after exiting, move the cursor back
rl.cursor.Dec()
}
// Swap the case of the character under the cursor and move past it.
// If in visual mode, change the case of each character in the selection.
func (rl *Shell) viChangeCase() {
switch {
case rl.selection.Active() && rl.selection.IsVisual():
rl.selection.ReplaceWith(func(char rune) rune {
if unicode.IsLower(char) {
return unicode.ToUpper(char)
}
return unicode.ToLower(char)
})
default:
if rl.line.Len() == 0 || rl.cursor.Pos() == rl.line.Len() {
return
}
char := rl.cursor.Char()
if unicode.IsLower(char) {
char = unicode.ToUpper(char)
} else {
char = unicode.ToLower(char)
}
rl.cursor.ReplaceWith(char)
}
}
// Substitute the next character(s).
func (rl *Shell) viSubstitute() {
rl.History.Save()
defer rl.viInsertMode()
switch {
case rl.selection.Active():
// Delete the selection and enter insert mode.
cpos := rl.selection.Cursor()
rl.selection.Cut()
rl.cursor.Set(cpos)
default:
// Since we must emulate the default readline behavior,
// we vary our behavior depending on the caller key.
keys := rl.Keys.Caller()
switch keys[0] {
case 's':
// Delete next characters and enter insert mode.
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
rl.line.CutRune(rl.cursor.Pos())
}
case 'S':
if rl.cursor.OnEmptyLine() {
return
}
// Pass the buffer to register.
rl.selection.Mark(rl.cursor.Pos())
rl.selection.Visual(true)
bpos, epos := rl.selection.Pos()
rl.Buffers.Write((*rl.line)[bpos:epos]...)
// If selection has a new line, remove it.
if (*rl.line)[epos-1] == '\n' {
epos--
}
// Kill the line
rl.line.Cut(bpos, epos)
rl.cursor.Set(bpos)
}
}
}
// Kill to the end of the line and enter insert mode.
func (rl *Shell) viChangeEol() {
rl.History.Save()
rl.History.SkipSave()
pos := rl.cursor.Pos()
rl.selection.Mark(pos)
rl.cursor.EndOfLineAppend()
rl.selection.Cut()
rl.cursor.Set(pos)
rl.Iterations.Reset()
rl.Display.ResetHelpers()
rl.viInsertMode()
}
// Read a key from the keyboard, and add it to the lead and trail of the current
// selection. If the key is matcher type (quote/bracket/brace, etc), each of the
// matchers is used accordingly (an opening and closing one).
func (rl *Shell) viAddSurround() {
// Get the surround character to change.
done := rl.Keymap.PendingCursor()
defer done()
key, isAbort := rl.Keys.ReadKey()
if isAbort {
rl.History.SkipSave()
return
}
bchar, echar := strutil.MatchSurround(key)
rl.History.Save()
// Surround the selection
rl.selection.Surround(bchar, echar)
}
// Create a new line above the current one, and enter insert mode.
func (rl *Shell) viOpenLineAbove() {
rl.History.Save()
if !rl.cursor.OnEmptyLine() {
rl.beginningOfLine()
}
rl.cursor.InsertAt('\n')
rl.cursor.Dec()
rl.viInsertMode()
}
// Create a new line below the current one, and enter insert mode.
func (rl *Shell) viOpenLineBelow() {
rl.History.Save()
if !rl.cursor.OnEmptyLine() {
rl.endOfLine()
}
rl.cursor.InsertAt('\n')
rl.viInsertMode()
}
// Convert the current word to all lowercase and move past it.
// If in visual mode, operate on the whole selection.
func (rl *Shell) viDownCase() {
switch {
case rl.Keymap.IsPending():
// In vi operator pending mode, it's that we've been called
// twice in a row (eg. `uu`), so modify the entire current line.
rl.History.Save()
rl.selection.Mark(rl.cursor.Pos())
rl.selection.Visual(true)
rl.selection.ReplaceWith(unicode.ToLower)
rl.viCommandMode()
case rl.selection.Active():
rl.History.Save()
rl.selection.ReplaceWith(unicode.ToLower)
rl.viCommandMode()
default:
// Else if we are actually starting a yank action.
rl.History.SkipSave()
rl.Keymap.Pending()
rl.selection.Mark(rl.cursor.Pos())
}
}
// Convert the current word to all uppercase and move past it.
// If in visual mode, operate on the whole selection.
func (rl *Shell) viUpCase() {
switch {
case rl.Keymap.IsPending():
// In vi operator pending mode, it's that we've been called
// twice in a row (eg. `uu`), so modify the entire current line.
rl.History.Save()
rl.selection.Mark(rl.cursor.Pos())
rl.selection.Visual(true)
rl.selection.ReplaceWith(unicode.ToUpper)
rl.viCommandMode()
case rl.selection.Active():
rl.History.Save()
rl.selection.ReplaceWith(unicode.ToUpper)
rl.viCommandMode()
default:
// Else if we are actually starting a yank action.
rl.History.SkipSave()
rl.Keymap.Pending()
rl.selection.Mark(rl.cursor.Pos())
}
}
//
// Killing & Yanking ----------------------------------------------------
//
// Kill from the cursor to the end of the line.
func (rl *Shell) viKillEol() {
rl.History.Save()
pos := rl.cursor.Pos()
rl.selection.Mark(rl.cursor.Pos())
rl.cursor.EndOfLineAppend()
cut := rl.selection.Cut()
rl.Buffers.Write([]rune(cut)...)
rl.cursor.Set(pos)
if !rl.cursor.AtBeginningOfLine() {
rl.cursor.Dec()
}
rl.Iterations.Reset()
rl.Display.ResetHelpers()
}
// Kill the word from its beginning up to the cursor point.
func (rl *Shell) viRubout() {
if rl.Keymap.Main() != keymap.ViInsert {
rl.History.Save()
}
vii := rl.Iterations.Get()
cut := make([]rune, 0)
// Delete the chars in the line anyway
for i := 1; i <= vii; i++ {
if rl.cursor.Pos() == 0 {
break
}
rl.cursor.Dec()
cut = append(cut, rl.cursor.Char())
rl.line.CutRune(rl.cursor.Pos())
}
rl.Buffers.Write(cut...)
}
// Read a movement command from the keyboard, and copy the region
// from the cursor position to the endpoint of the movement into
// the kill buffer. If the command is vi-yank, copy the current line.
func (rl *Shell) viYankTo() {
switch {
case rl.Keymap.IsPending():
// In vi operator pending mode, it's that we've been called
// twice in a row (eg. `yy`), so copy the entire current line.
rl.Keymap.CancelPending()
rl.History.Save()
rl.selection.Mark(rl.cursor.Pos())
rl.selection.Visual(true)
// Get buffer and add newline if there isn't one at the end
text, _, _, _ := rl.selection.Pop()
if len(text) > 0 && rune(text[len(text)-1]) != inputrc.Newline {
text += string(inputrc.Newline)
}
rl.Buffers.Write([]rune(text)...)
case rl.selection.Active():
// In visual mode, or with a non-empty selection, just yank.
rl.History.Save()
rl.adjustSelectionPending()
text, _, _, cpos := rl.selection.Pop()
rl.Buffers.Write([]rune(text)...)
rl.cursor.Set(cpos)
rl.viCommandMode()
default:
// Since we must emulate the default readline behavior,
// we vary our behavior depending on the caller key.
keys := rl.Keys.Caller()
switch keys[0] {
case 'y':
rl.Keymap.Pending()
rl.selection.Mark(rl.cursor.Pos())
case 'Y':
rl.viYankWholeLine()
}
}
}
// Copy the current line into the kill buffer.
func (rl *Shell) viYankWholeLine() {
rl.History.SkipSave()
// calculate line selection.
rl.selection.Mark(rl.cursor.Pos())
rl.selection.Visual(true)
bpos, epos := rl.selection.Pos()
// If selection has a new line, remove it.
if (*rl.line)[epos-1] == '\n' {
epos--
}
// Pass the buffer to register.
buffer := (*rl.line)[bpos:epos]
rl.Buffers.Write(buffer...)
// Done with any selection.