-
Notifications
You must be signed in to change notification settings - Fork 12
/
emacs.go
1621 lines (1309 loc) · 42.5 KB
/
emacs.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 (
"fmt"
"io"
"sort"
"strings"
"unicode"
"github.com/rivo/uniseg"
"github.com/reeflective/readline/inputrc"
"github.com/reeflective/readline/internal/color"
"github.com/reeflective/readline/internal/completion"
"github.com/reeflective/readline/internal/keymap"
"github.com/reeflective/readline/internal/strutil"
"github.com/reeflective/readline/internal/term"
)
// standardCommands returns all standard/emacs 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
// Numeric arguments.
// Macros
// Miscellaneous.
func (rl *Shell) standardCommands() commands {
widgets := map[string]func(){
// Modes
"emacs-editing-mode": rl.emacsEditingMode,
// Moving
"forward-char": rl.forwardChar,
"backward-char": rl.backwardChar,
"forward-word": rl.forwardWord,
"backward-word": rl.backwardWord,
"shell-forward-word": rl.forwardShellWord,
"shell-backward-word": rl.backwardShellWord,
"beginning-of-line": rl.beginningOfLine,
"end-of-line": rl.endOfLine,
"previous-screen-line": rl.upLine,
"next-screen-line": rl.downLine,
"clear-screen": rl.clearScreen,
"clear-display": rl.clearDisplay,
"redraw-current-line": rl.Display.Refresh,
// Changing text
"end-of-file": rl.endOfFile,
"delete-char": rl.deleteChar,
"backward-delete-char": rl.backwardDeleteChar,
"forward-backward-delete-char": rl.forwardBackwardDeleteChar,
"quoted-insert": rl.quotedInsert,
"tab-insert": rl.tabInsert,
"self-insert": rl.selfInsert,
"bracketed-paste-begin": rl.bracketedPasteBegin,
"transpose-chars": rl.transposeChars,
"transpose-words": rl.transposeWords,
"shell-transpose-words": rl.shellTransposeWords,
"down-case-word": rl.downCaseWord,
"up-case-word": rl.upCaseWord,
"capitalize-word": rl.capitalizeWord,
"overwrite-mode": rl.overwriteMode,
"delete-horizontal-whitespace": rl.deleteHorizontalWhitespace,
"delete-word": rl.deleteWord,
"quote-region": rl.quoteRegion,
"quote-line": rl.quoteLine,
"keyword-increase": rl.keywordIncrease,
"keyword-decrease": rl.keywordDecrease,
// Killing & yanking
"kill-line": rl.killLine,
"backward-kill-line": rl.backwardKillLine,
"unix-line-discard": rl.backwardKillLine,
"kill-whole-line": rl.killWholeLine,
"kill-word": rl.killWord,
"backward-kill-word": rl.backwardKillWord,
"unix-word-rubout": rl.backwardKillWord,
"kill-region": rl.killRegion,
"copy-region-as-kill": rl.copyRegionAsKill,
"copy-backward-word": rl.copyBackwardWord,
"copy-forward-word": rl.copyForwardWord,
"yank": rl.yank,
"yank-pop": rl.yankPop,
"kill-buffer": rl.killBuffer,
"shell-kill-word": rl.shellKillWord,
"shell-backward-kill-word": rl.shellBackwardKillWord,
"copy-prev-shell-word": rl.copyPrevShellWord,
// Numeric arguments
"digit-argument": rl.digitArgument,
// Macros
"start-kbd-macro": rl.startKeyboardMacro,
"end-kbd-macro": rl.endKeyboardMacro,
"call-last-kbd-macro": rl.callLastKeyboardMacro,
"print-last-kbd-macro": rl.printLastKeyboardMacro,
"macro-toggle-record": rl.macroToggleRecord,
"macro-run": rl.macroRun,
// Miscellaneous
"re-read-init-file": rl.reReadInitFile,
"abort": rl.abort,
"do-lowercase-version": rl.doLowercaseVersion,
"prefix-meta": rl.prefixMeta,
"undo": rl.undoLast,
"revert-line": rl.revertLine,
"set-mark": rl.setMark,
"exchange-point-and-mark": rl.exchangePointAndMark,
"character-search": rl.characterSearch,
"character-search-backward": rl.characterSearchBackward,
"insert-comment": rl.insertComment,
"dump-functions": rl.dumpFunctions,
"dump-variables": rl.dumpVariables,
"dump-macros": rl.dumpMacros,
"magic-space": rl.magicSpace,
"edit-and-execute-command": rl.editAndExecuteCommand,
"edit-command-line": rl.editCommandLine,
"redo": rl.redo,
"select-keyword-next": rl.selectKeywordNext,
"select-keyword-prev": rl.selectKeywordPrev,
}
return widgets
}
//
// Modes ----------------------------------------------------------------
//
// When in vi command mode, this causes a switch to emacs editing mode.
func (rl *Shell) emacsEditingMode() {
// 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()
// Update the keymap.
rl.Keymap.SetMain(keymap.Emacs)
}
//
// Movement -------------------------------------------------------------
//
// Move forward one character.
func (rl *Shell) forwardChar() {
startPos := rl.cursor.Pos()
// 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()
}
if rl.cursor.Pos() > startPos {
return
}
// Else, we move forward.
rl.History.SkipSave()
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
rl.cursor.Inc()
}
}
// Move backward one character.
func (rl *Shell) backwardChar() {
rl.History.SkipSave()
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
rl.cursor.Dec()
}
}
// Move to the beginning of the next word. The editor’s idea
// of a word is any sequence of alphanumeric characters.
func (rl *Shell) forwardWord() {
rl.History.SkipSave()
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(true)
forward := rl.line.ForwardEnd(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(forward + 1)
}
}
// Move to the beginning of the current or previousword. The editor’s
// idea of a word is any sequence of alphanumeric characters.
func (rl *Shell) backwardWord() {
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 forward to the beginning of the next word.
// The editor's idea of a word is defined by classic sh-style word splitting:
// any non-spaced sequence of characters, or a quoted sequence.
func (rl *Shell) forwardShellWord() {
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
rl.selection.SelectAShellWord()
_, _, tepos, _ := rl.selection.Pop()
rl.cursor.Set(tepos)
}
}
// Move to the beginning of the current or previous word.
// The editor's idea of a word is defined by classic sh-style word splitting:
// any non-spaced sequence of characters, or a quoted sequence.
func (rl *Shell) backwardShellWord() {
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
// First go the beginning of the blank word
startPos := rl.cursor.Pos()
backward := rl.line.Backward(rl.line.TokenizeSpace, startPos)
rl.cursor.Move(backward)
// Now try to find enclosing quotes from here.
bpos, _ := rl.selection.SelectAShellWord()
rl.cursor.Set(bpos)
}
}
// Move to the beginning of the line. If already at the beginning
// of the line, move to the beginning of the previous line, if any.
func (rl *Shell) beginningOfLine() {
rl.History.SkipSave()
// Handle 0 as iteration to Vim.
if !rl.Keymap.IsEmacs() && rl.Iterations.IsSet() {
rl.Iterations.Add("0")
return
}
rl.cursor.BeginningOfLine()
}
// Move to the end of the line. If already at the end
// of the line, move to the end of the next line, if any.
func (rl *Shell) endOfLine() {
rl.History.SkipSave()
// If in Vim command mode, cursor
// will be brought back once later.
rl.cursor.EndOfLineAppend()
}
// Move up one line if the current buffer has more than one line.
func (rl *Shell) upLine() {
lines := rl.Iterations.Get()
rl.cursor.LineMove(lines * -1)
}
// Move down one line if the current buffer has more than one line.
func (rl *Shell) downLine() {
lines := rl.Iterations.Get()
rl.cursor.LineMove(lines)
}
// Clear the current screen and redisplay the prompt and input line.
// This does not clear the terminal's output buffer.
func (rl *Shell) clearScreen() {
rl.History.SkipSave()
fmt.Print(term.CursorTopLeft)
fmt.Print(term.ClearScreen)
rl.Display.PrintPrimaryPrompt()
}
// Clear the current screen and redisplay the prompt and input line.
// This does clear the terminal's output buffer.
func (rl *Shell) clearDisplay() {
rl.History.SkipSave()
fmt.Print(term.CursorTopLeft)
fmt.Print(term.ClearDisplay)
rl.Display.PrintPrimaryPrompt()
}
//
// Changing Text --------------------------------------------------------
//
// The character indicating end-of-file as set, for example,
// by “stty”. If this character is read when there are no
// characters on the line, and point is at the beginning of
// the line, readline interprets it as the end of input and
// returns EOF.
func (rl *Shell) endOfFile() {
switch rl.line.Len() {
case 0:
rl.Display.AcceptLine()
rl.History.Accept(false, false, io.EOF)
default:
rl.deleteChar()
}
}
// Delete the character under the cursor.
func (rl *Shell) deleteChar() {
rl.History.Save()
vii := rl.Iterations.Get()
// Delete the chars in the line anyway
for i := 1; i <= vii; i++ {
rl.line.CutRune(rl.cursor.Pos())
}
}
// Delete the character behind the cursor.
// If the character to delete is a matching character
// (quote/brackets/braces,etc) and that the character
// under the cursor is its matching counterpart, this
// character will also be deleted.
func (rl *Shell) backwardDeleteChar() {
if rl.Keymap.Main() == keymap.ViInsert {
rl.History.SkipSave()
} else {
rl.History.Save()
}
// We might currently have a selected candidate inserted,
// and thus we should accept it as part of the real input
// line before cutting any character.
completion.UpdateInserted(rl.completer)
if rl.cursor.Pos() == 0 {
return
}
vii := rl.Iterations.Get()
switch vii {
case 1:
// Handle removal of autopairs characters.
if rl.Config.GetBool("autopairs") {
completion.AutopairDelete(rl.line, rl.cursor)
}
// And then delete the character under cursor.
rl.cursor.Dec()
rl.line.CutRune(rl.cursor.Pos())
default:
for i := 1; i <= vii; i++ {
rl.cursor.Dec()
rl.line.CutRune(rl.cursor.Pos())
}
}
}
// Delete the character under the cursor, unless the cursor is at the
// end of the line, in which case the character behind the cursor is deleted.
func (rl *Shell) forwardBackwardDeleteChar() {
switch rl.cursor.Pos() {
case rl.line.Len():
rl.backwardDeleteChar()
default:
rl.deleteChar()
}
}
// Add the next character that you type to the line verbatim.
// This is how to insert characters like C-q, for example.
func (rl *Shell) quotedInsert() {
rl.History.SkipSave()
rl.completer.TrimSuffix()
done := rl.Keymap.PendingCursor()
defer done()
key, _ := rl.Keys.ReadKey()
quoted, _ := strutil.Quote(key)
rl.cursor.InsertAt(quoted...)
}
// Insert a tab character.
func (rl *Shell) tabInsert() {
rl.History.SkipSave()
rl.cursor.InsertAt('\t')
}
// Insert the character typed.
func (rl *Shell) selfInsert() {
rl.History.SkipSave()
// Handle suffix-autoremoval for inserted completions.
rl.completer.TrimSuffix()
key := rl.Keys.Caller()
// Handle autopair insertion (for the closer only)
searching, _, _ := rl.completer.NonIncrementallySearching()
isearch := rl.Keymap.Local() == keymap.Isearch
if !searching && !isearch && rl.Config.GetBool("autopairs") {
if jump := completion.AutopairInsertOrJump(key[0], rl.line, rl.cursor); jump {
return
}
}
var quoted []rune
var length int
if rl.Config.GetBool("output-meta") && key[0] != inputrc.Esc {
quoted = append(quoted, key[0])
length = uniseg.StringWidth(string(quoted))
} else {
quoted, length = strutil.Quote(key[0])
}
rl.cursor.InsertAt(quoted...)
rl.cursor.Move(-1 * len(quoted))
rl.cursor.Move(length)
}
func (rl *Shell) bracketedPasteBegin() {
// keys, _ := rl.Keys.PeekAllBytes()
// fmt.Println(string(keys))
}
// Drag the character before point forward over the character
// at point, moving point forward as well. If point is at the
// end of the line, then this transposes the two characters
// before point. Negative arguments have no effect.
func (rl *Shell) transposeChars() {
if rl.cursor.Pos() < 2 || rl.line.Len() < 2 {
rl.History.SkipSave()
return
}
rl.History.Save()
switch {
case rl.cursor.Pos() == rl.line.Len():
last := (*rl.line)[rl.cursor.Pos()-1]
blast := (*rl.line)[rl.cursor.Pos()-2]
(*rl.line)[rl.cursor.Pos()-2] = last
(*rl.line)[rl.cursor.Pos()-1] = blast
default:
last := rl.cursor.Char()
blast := (*rl.line)[rl.cursor.Pos()-1]
(*rl.line)[rl.cursor.Pos()-1] = last
rl.cursor.ReplaceWith(blast)
}
}
// Drag the word before point past the word after point,
// moving point over that word as well. If point is at the
// end of the line, this transposes the last two words on the
// line. If a numeric argument is given, the word to transpose
// is chosen backward.
func (rl *Shell) transposeWords() {
rl.History.Save()
startPos := rl.cursor.Pos()
rl.cursor.ToFirstNonSpace(true)
rl.cursor.CheckCommand()
// Save the current word and move the cursor to its beginning
rl.viSelectInWord()
rl.selection.Visual(false)
toTranspose, tbpos, tepos, _ := rl.selection.Pop()
// Then move some number of words.
// Either use words backward (if we are at end of line) or forward.
rl.cursor.Set(tbpos)
if tepos >= rl.line.Len()-1 || rl.Iterations.IsSet() {
rl.backwardWord()
} else {
rl.viForwardWord()
}
// Save the word to transpose with
rl.viSelectInWord()
rl.selection.Visual(false)
transposeWith, wbpos, wepos, _ := rl.selection.Pop()
// We might be on the first word of the line,
// in which case we don't do anything.
if tbpos == 0 {
rl.cursor.Set(startPos)
return
}
// If we went forward rather than backward, swap everything.
if wbpos > tbpos {
wbpos, tbpos = tbpos, wbpos
wepos, tepos = tepos, wepos
transposeWith, toTranspose = toTranspose, transposeWith
}
// Assemble the newline
begin := string((*rl.line)[:wbpos])
newLine := append([]rune(begin), []rune(toTranspose)...)
newLine = append(newLine, (*rl.line)[wepos:tbpos]...)
newLine = append(newLine, []rune(transposeWith)...)
newLine = append(newLine, (*rl.line)[tepos:]...)
rl.line.Set(newLine...)
// And replace the cursor
rl.cursor.Set(tepos)
}
// Drag the shell word before point past the shell word after point,
// moving point over that shell word as well. If point is at the
// end of the line, this transposes the last two words on the line.
// If a numeric argument is given, the word to transpose is chosen backward.
func (rl *Shell) shellTransposeWords() {
rl.History.Save()
startPos := rl.cursor.Pos()
// Save the current word
rl.viSelectAShellWord()
toTranspose, tbpos, tepos, _ := rl.selection.Pop()
// First move back the number of words
rl.cursor.Set(tbpos)
rl.backwardShellWord()
// Save the word to transpose with
rl.viSelectAShellWord()
transposeWith, wbpos, wepos, _ := rl.selection.Pop()
// We might be on the first word of the line,
// in which case we don't do anything.
if wepos > tbpos {
rl.cursor.Set(startPos)
return
}
// Assemble the newline
begin := string((*rl.line)[:wbpos])
newLine := append([]rune(begin), []rune(toTranspose)...)
newLine = append(newLine, (*rl.line)[wepos:tbpos]...)
newLine = append(newLine, []rune(transposeWith)...)
newLine = append(newLine, (*rl.line)[tepos:]...)
rl.line.Set(newLine...)
// And replace cursor
rl.cursor.Set(tepos)
}
// Lowercase the current (or following) word. With a negative argument,
// lowercase the previous word, but do not move point.
func (rl *Shell) downCaseWord() {
rl.History.Save()
startPos := rl.cursor.Pos()
// Save the current word
rl.cursor.Inc()
backward := rl.line.Backward(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(backward)
rl.selection.Mark(rl.cursor.Pos())
forward := rl.line.ForwardEnd(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(forward)
rl.selection.ReplaceWith(unicode.ToLower)
rl.cursor.Set(startPos)
}
// Uppercase the current (or following) word. With a negative argument,
// uppercase the previous word, but do not move point.
func (rl *Shell) upCaseWord() {
rl.History.Save()
startPos := rl.cursor.Pos()
// Save the current word
rl.cursor.Inc()
backward := rl.line.Backward(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(backward)
rl.selection.Mark(rl.cursor.Pos())
forward := rl.line.ForwardEnd(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(forward)
rl.selection.ReplaceWith(unicode.ToUpper)
rl.cursor.Set(startPos)
}
// Capitalize the current (or following) word. With a negative argument,
// capitalize the previous word, but do not move point.
func (rl *Shell) capitalizeWord() {
if rl.line.Len() == 0 {
return
}
rl.History.Save()
startPos := rl.cursor.Pos()
rl.cursor.Inc()
backward := rl.line.Backward(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(backward)
letter := rl.cursor.Char()
rl.cursor.ReplaceWith(unicode.ToUpper(letter))
rl.cursor.Set(startPos)
}
// Toggle overwrite mode. In overwrite mode, characters bound to
// self-insert replace the text at point rather than pushing the
// text to the right. Characters bound to backward-delete-char
// replace the character before point with a space.
func (rl *Shell) overwriteMode() {
// We store the current line as an undo item first, but will not
// store any intermediate changes (in the loop below) as undo items.
rl.History.Save()
done := rl.Keymap.PendingCursor()
defer done()
// All replaced characters are stored, to be used with backspace
cache := make([]rune, 0)
// Don't use the delete cache past the end of the line
lineStart := rl.line.Len()
// The replace mode is quite special in that it does escape back
// to the main readline loop: it keeps reading characters and inserts
// them as long as the escape key is not pressed.
for {
// We read a character to use first.
key, isAbort := rl.Keys.ReadKey()
if isAbort {
break
}
// If the key is a backspace, we go back one character
if string(key) == inputrc.Unescape(string(`\C-?`)) {
if rl.cursor.Pos() > lineStart {
rl.backwardDeleteChar()
} else if rl.cursor.Pos() > 0 {
rl.cursor.Dec()
}
// And recover the last replaced character
if len(cache) > 0 && rl.cursor.Pos() < lineStart {
key = cache[len(cache)-1]
cache = cache[:len(cache)-1]
rl.cursor.ReplaceWith(key)
}
} else {
// If the cursor is at the end of the line,
// we insert the character instead of replacing.
if rl.line.Len() == rl.cursor.Pos() {
rl.cursor.InsertAt(key)
} else {
cache = append(cache, rl.cursor.Char())
rl.cursor.ReplaceWith(key)
rl.cursor.Inc()
}
}
// Update the line
rl.Display.Refresh()
}
}
// Delete all spaces and tabs around point.
func (rl *Shell) deleteHorizontalWhitespace() {
rl.History.Save()
startPos := rl.cursor.Pos()
rl.cursor.ToFirstNonSpace(false)
if rl.cursor.Pos() != startPos {
rl.cursor.Inc()
}
bpos := rl.cursor.Pos()
rl.cursor.ToFirstNonSpace(true)
if rl.cursor.Pos() != startPos {
rl.cursor.Dec()
}
epos := rl.cursor.Pos()
rl.line.Cut(bpos, epos)
rl.cursor.Set(bpos)
}
// Delete the current word from the cursor point up to the end of it.
func (rl *Shell) deleteWord() {
rl.History.Save()
rl.selection.Mark(rl.cursor.Pos())
forward := rl.line.ForwardEnd(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(forward)
rl.selection.Cut()
}
// Quote the region from the cursor to the mark.
func (rl *Shell) quoteRegion() {
rl.History.Save()
rl.selection.Surround('\'', '\'')
rl.cursor.Inc()
}
// Quote the entire line buffer.
func (rl *Shell) quoteLine() {
if rl.line.Len() == 0 {
return
}
for pos, char := range *rl.line {
if char == '\n' {
break
}
if char == '\'' {
(*rl.line)[pos] = '"'
}
}
rl.line.Insert(0, '\'')
rl.line.Insert(rl.line.Len(), '\'')
}
// Modifies the current word under the cursor, increasing it.
// The following word types can be incremented/decremented:
//
// Booleans: true|false, t|f, on|off, yes|no, y|n.
// Operators: &&|||, ++|--, ==|!=, ===| !==, +| -, -| *, *| /, /| +, and| or.
// Hex digits 0xDe => 0xdf, 0xdE => 0xDF, 0xde0 => 0xddf, 0xffffffffffffffff => 0x0000000000000000.
// Binary digits: 0b1 => 0b10, 0B0 => 0B1, etc.
// Integers.
func (rl *Shell) keywordIncrease() {
rl.History.Save()
rl.keywordSwitch(true)
}
// Modifies the current word under the cursor, decreasing it.
// The following word types can be incremented/decremented:
//
// Booleans: true|false, t|f, on|off, yes|no, y|n.
// Operators: &&|||, ++|--, ==|!=, ===| !==, +| -, -| *, *| /, /| +, and| or.
// Hex digits 0xDe => 0xdf, 0xdE => 0xDF, 0xde0 => 0xddf, 0xffffffffffffffff => 0x0000000000000000.
// Binary digits: 0b1 => 0b10, 0B0 => 0B1, etc.
// Integers.
func (rl *Shell) keywordDecrease() {
rl.History.Save()
rl.keywordSwitch(false)
}
// Switches the current word under the cursor, increasing or decreasing it.
func (rl *Shell) keywordSwitch(increase bool) {
cpos := strutil.AdjustNumberOperatorPos(rl.cursor.Pos(), *rl.line)
// Select in word and get the selection positions
bpos, epos := rl.line.SelectWord(cpos)
epos++
// Move the cursor backward if needed/possible
if bpos != 0 && ((*rl.line)[bpos-1] == '+' || (*rl.line)[bpos-1] == '-') {
bpos--
}
// Get the selection string
selection := string((*rl.line)[bpos:epos])
// For each of the keyword handlers, run it, which returns
// false/none if didn't operate, then continue to next handler.
for _, switcher := range strutil.KeywordSwitchers() {
vii := rl.Iterations.Get()
changed, word, obpos, oepos := switcher(selection, increase, vii)
if !changed {
continue
}
// We are only interested in the end position after all runs
epos = bpos + oepos
bpos += obpos
if cpos < bpos || cpos >= epos {
continue
}
// Update the line and the cursor, and return
// since we have a handler that has been ran.
begin := string((*rl.line)[:bpos])
end := string((*rl.line)[epos:])
newLine := append([]rune(begin), []rune(word)...)
newLine = append(newLine, []rune(end)...)
rl.line.Set(newLine...)
rl.cursor.Set(bpos + len(word) - 1)
return
}
}
//
// Killing & Yanking ----------------------------------------------------------
//
// Kill from the cursor to the end of the line. If already
// on the end of the line, kill the newline character.
func (rl *Shell) killLine() {
rl.Iterations.Reset()
rl.History.Save()
if rl.line.Len() == 0 {
return
}
cpos := rl.cursor.Pos()
rl.cursor.EndOfLineAppend()
rl.selection.MarkRange(cpos, rl.cursor.Pos())
text := rl.selection.Cut()
rl.Buffers.Write([]rune(text)...)
rl.cursor.Set(cpos)
}
// Kill backward to the beginning of the line.
func (rl *Shell) backwardKillLine() {
rl.Iterations.Reset()
rl.History.Save()
if rl.line.Len() == 0 {
return
}
cpos := rl.cursor.Pos()
rl.cursor.BeginningOfLine()
rl.selection.MarkRange(rl.cursor.Pos(), cpos)
text := rl.selection.Cut()
rl.Buffers.Write([]rune(text)...)
}
// Kill all characters on the current line, no matter where point is.
func (rl *Shell) killWholeLine() {
rl.History.Save()
if rl.line.Len() == 0 {
return
}
rl.Buffers.Write(*rl.line...)
rl.line.Cut(0, rl.line.Len())
}
// Kill the entire buffer.
func (rl *Shell) killBuffer() {
rl.History.Save()
if rl.line.Len() == 0 {
return
}
rl.Buffers.Write(*rl.line...)
rl.line.Cut(0, rl.line.Len())
}
// Kill the current word from the cursor point up to the end of it.
func (rl *Shell) killWord() {
rl.History.Save()
bpos := rl.cursor.Pos()
rl.cursor.ToFirstNonSpace(true)
forward := rl.line.Forward(rl.line.TokenizeSpace, rl.cursor.Pos())
rl.cursor.Move(forward - 1)
epos := rl.cursor.Pos()
rl.selection.MarkRange(bpos, epos)
rl.Buffers.Write([]rune(rl.selection.Cut())...)
rl.cursor.Set(bpos)
}
// Kill the word behind point. Word boundaries
// are the same as those used by backward-word.
func (rl *Shell) backwardKillWord() {
rl.History.Save()
rl.History.SkipSave()
rl.selection.Mark(rl.cursor.Pos())
adjust := rl.line.Backward(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(adjust)
rl.Buffers.Write([]rune(rl.selection.Cut())...)
}
// Kill the text between the point and mark (saved cursor
// position). This text is referred to as the region.
func (rl *Shell) killRegion() {
rl.History.Save()
if !rl.selection.Active() {
return
}
rl.Buffers.Write([]rune(rl.selection.Cut())...)
}
// Copy the text in the region to the kill buffer.
func (rl *Shell) copyRegionAsKill() {
rl.History.SkipSave()
if !rl.selection.Active() {
return
}
rl.Buffers.Write([]rune(rl.selection.Text())...)
rl.selection.Reset()
}
// Copy the word before point to the kill buffer.
// The word boundaries are the same as backward-word.
func (rl *Shell) copyBackwardWord() {
rl.History.Save()
rl.selection.Mark(rl.cursor.Pos())
adjust := rl.line.Backward(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(adjust)
rl.Buffers.Write([]rune(rl.selection.Text())...)
rl.selection.Reset()
}
// Copy the word following point to the kill buffer.
// The word boundaries are the same as forward-word.
func (rl *Shell) copyForwardWord() {
rl.History.Save()
rl.selection.Mark(rl.cursor.Pos())
adjust := rl.line.Forward(rl.line.Tokenize, rl.cursor.Pos())
rl.cursor.Move(adjust + 1)
rl.Buffers.Write([]rune(rl.selection.Text())...)
rl.selection.Reset()
}
// Yank the top of the kill ring into the buffer at point.
func (rl *Shell) yank() {
buf := rl.Buffers.Active()
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
rl.cursor.InsertAt(buf...)
}
}
// Rotate the kill ring, and yank the new top.
// Only works following yank or yank-pop.
func (rl *Shell) yankPop() {
vii := rl.Iterations.Get()
for i := 1; i <= vii; i++ {
buf := rl.Buffers.Pop()
rl.cursor.InsertAt(buf...)
}