-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcommands.go
917 lines (756 loc) · 25.9 KB
/
commands.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
// Package vimtea provides a Vim-like text editor component for terminal applications
package vimtea
import (
"fmt"
"strings"
"time"
tea "github.com/charmbracelet/bubbletea"
"golang.design/x/clipboard"
)
// CommandFn is a function that can be executed when a command is run in command mode
// It takes a buffer reference and command arguments, and returns a bubbletea command
type CommandFn func(Buffer, []string) tea.Cmd
// CommandMsg is sent when a command is executed from command mode
// It contains the command name that should be looked up in the CommandRegistry
type CommandMsg struct {
Command string // Command name without arguments
}
// withCountPrefix executes a function multiple times based on the numeric prefix
// This implements commands like "5j" to move down 5 lines
func withCountPrefix(model *editorModel, fn func()) {
count := model.countPrefix
for range count {
fn()
}
model.countPrefix = 1
}
// switchMode changes the editor mode and performs necessary setup for the new mode
// Different modes require different cursor handling and UI state
func switchMode(model *editorModel, newMode EditorMode) tea.Cmd {
model.mode = newMode
switch newMode {
case ModeNormal:
// In normal mode, cursor can't be at end of line
if model.buffer.lineLength(model.cursor.Row) > 0 &&
model.cursor.Col >= model.buffer.lineLength(model.cursor.Row) {
model.cursor.Col = max(0, model.buffer.lineLength(model.cursor.Row)-1)
}
model.isVisualLine = false
model.statusMessage = ""
case ModeCommand:
// Reset command buffer when entering command mode
model.commandBuffer = ""
}
return func() tea.Msg {
return EditorModeMsg{newMode}
}
}
func registerBindings(m *editorModel) {
m.registry.Add("i", enterModeInsert, ModeNormal, "Enter insert mode")
m.registry.Add("v", beginVisualSelection, ModeNormal, "Enter visual mode")
m.registry.Add("V", beginVisualLineSelection, ModeNormal, "Enter visual line mode")
m.registry.Add("x", deleteCharAtCursor, ModeNormal, "Delete character at cursor")
if m.enableCommandMode {
m.registry.Add(":", enterModeCommand, ModeNormal, "Enter command mode")
}
m.registry.Add("a", appendAfterCursor, ModeNormal, "Append after cursor")
m.registry.Add("A", appendAtEndOfLine, ModeNormal, "Append at end of line")
m.registry.Add("I", insertAtStartOfLine, ModeNormal, "Insert at start of line")
m.registry.Add("o", openLineBelow, ModeNormal, "Open line below")
m.registry.Add("O", openLineAbove, ModeNormal, "Open line above")
m.registry.Add("yy", yankLine, ModeNormal, "Yank line")
m.registry.Add("dd", deleteLine, ModeNormal, "Delete line")
m.registry.Add("D", deleteToEndOfLine, ModeNormal, "Delete to end of line")
m.registry.Add("p", pasteAfter, ModeNormal, "Paste after cursor")
m.registry.Add("P", pasteBefore, ModeNormal, "Paste before cursor")
m.registry.Add("u", undo, ModeNormal, "Undo")
m.registry.Add("ctrl+r", redo, ModeNormal, "Redo")
m.registry.Add("diw", deleteInnerWord, ModeNormal, "Delete inner word")
m.registry.Add("yiw", yankInnerWord, ModeNormal, "Yank inner word")
m.registry.Add("ciw", changeInnerWord, ModeNormal, "Change inner word")
for _, mode := range []EditorMode{ModeNormal, ModeVisual} {
m.registry.Add("h", moveCursorLeft, mode, "Move cursor left")
m.registry.Add("j", moveCursorDown, mode, "Move cursor down")
m.registry.Add("k", moveCursorUp, mode, "Move cursor up")
m.registry.Add("l", moveCursorRight, mode, "Move cursor right")
m.registry.Add("w", moveToNextWordStart, mode, "Move to next word")
m.registry.Add("b", moveToPrevWordStart, mode, "Move to previous word")
m.registry.Add(" ", moveCursorRightOrNextLine, mode, "Move cursor right")
m.registry.Add("0", moveToStartOfLine, mode, "Move to start of line")
m.registry.Add("^", moveToFirstNonWhitespace, mode, "Move to first non-whitespace character")
m.registry.Add("$", moveToEndOfLine, mode, "Move to end of line")
m.registry.Add("gg", moveToStartOfDocument, mode, "Move to document start")
m.registry.Add("G", moveToEndOfDocument, mode, "Move to document end")
m.registry.Add("up", moveCursorUp, mode, "Move cursor up")
m.registry.Add("down", moveCursorDown, mode, "Move cursor down")
m.registry.Add("left", moveCursorLeft, mode, "Move cursor left")
m.registry.Add("right", moveCursorRight, mode, "Move cursor right")
}
m.registry.Add("esc", exitModeVisual, ModeVisual, "Exit visual mode")
m.registry.Add("v", exitModeVisual, ModeVisual, "Exit visual mode")
m.registry.Add("V", exitModeVisual, ModeVisual, "Exit visual mode")
m.registry.Add(":", enterModeCommand, ModeVisual, "Enter command mode")
m.registry.Add("y", yankVisualSelection, ModeVisual, "Yank selection")
m.registry.Add("d", deleteVisualSelection, ModeVisual, "Delete selection")
m.registry.Add("x", deleteVisualSelection, ModeVisual, "Delete selection")
m.registry.Add("p", replaceVisualSelectionWithYank, ModeVisual, "Replace with yanked text")
m.registry.Add("esc", exitModeInsert, ModeInsert, "Exit insert mode")
m.registry.Add("backspace", handleInsertBackspace, ModeInsert, "Backspace")
m.registry.Add("tab", handleInsertTab, ModeInsert, "Tab")
m.registry.Add("enter", handleInsertEnterKey, ModeInsert, "Enter")
m.registry.Add("up", handleArrowKeys("up"), ModeInsert, "Move cursor up")
m.registry.Add("down", handleArrowKeys("down"), ModeInsert, "Move cursor down")
m.registry.Add("left", handleArrowKeys("left"), ModeInsert, "Move cursor left")
m.registry.Add("right", handleArrowKeys("right"), ModeInsert, "Move cursor right")
m.registry.Add("esc", exitModeCommand, ModeCommand, "Exit command mode")
m.registry.Add("enter", executeCommand, ModeCommand, "Execute command")
m.registry.Add("backspace", commandBackspace, ModeCommand, "Backspace")
m.commands.Register("zr", toggleRelativeLineNumbers)
m.commands.Register("clear", clearBuffer)
m.commands.Register("reset", resetEditor)
}
func toggleRelativeLineNumbers(model *editorModel) tea.Cmd {
model.relativeNumbers = !model.relativeNumbers
if model.relativeNumbers {
return SetStatusMsg("relative line numbers: on")
} else {
return SetStatusMsg("relative line numbers: off")
}
}
func clearBuffer(model *editorModel) tea.Cmd {
model.buffer.saveUndoState(model.cursor)
model.buffer.clear()
model.cursor = newCursor(0, 0)
return SetStatusMsg("buffer cleared")
}
func resetEditor(model *editorModel) tea.Cmd {
return model.Reset()
}
func moveToFirstNonWhitespace(model *editorModel) tea.Cmd {
line := model.buffer.Line(model.cursor.Row)
for i, char := range line {
if char != ' ' && char != '\t' {
model.cursor.Col = i
model.desiredCol = model.cursor.Col
break
}
}
return nil
}
func deleteToEndOfLine(model *editorModel) tea.Cmd {
row := model.cursor.Row
col := model.cursor.Col
line := model.buffer.Line(row)
if len(line) > 0 {
model.buffer.saveUndoState(model.cursor)
start := Cursor{Row: row, Col: col}
end := Cursor{Row: row, Col: len(line) - 1}
model.yankBuffer = model.buffer.deleteRange(start, end)
clipboard.Write(clipboard.FmtText, []byte(model.yankBuffer))
}
return nil
}
func exitModeCommand(model *editorModel) tea.Cmd {
return switchMode(model, ModeNormal)
}
func exitModeVisual(model *editorModel) tea.Cmd {
return switchMode(model, ModeNormal)
}
func exitModeInsert(model *editorModel) tea.Cmd {
return switchMode(model, ModeNormal)
}
func enterModeInsert(model *editorModel) tea.Cmd {
return switchMode(model, ModeInsert)
}
func enterModeCommand(model *editorModel) tea.Cmd {
return switchMode(model, ModeCommand)
}
func beginVisualSelection(model *editorModel) tea.Cmd {
model.visualStart = model.cursor.Clone()
model.isVisualLine = false
model.statusMessage = "-- VISUAL --"
return switchMode(model, ModeVisual)
}
func beginVisualLineSelection(model *editorModel) tea.Cmd {
model.visualStart = newCursor(model.cursor.Row, 0)
model.isVisualLine = true
model.statusMessage = "-- VISUAL LINE --"
return switchMode(model, ModeVisual)
}
func appendAfterCursor(model *editorModel) tea.Cmd {
if model.cursor.Col < model.buffer.lineLength(model.cursor.Row) {
model.cursor.Col++
}
return switchMode(model, ModeInsert)
}
func appendAtEndOfLine(model *editorModel) tea.Cmd {
model.cursor.Col = model.buffer.lineLength(model.cursor.Row)
return switchMode(model, ModeInsert)
}
func insertAtStartOfLine(model *editorModel) tea.Cmd {
model.cursor.Col = 0
return switchMode(model, ModeInsert)
}
func openLineBelow(model *editorModel) tea.Cmd {
model.buffer.saveUndoState(model.cursor)
model.buffer.insertLine(model.cursor.Row+1, "")
model.cursor.Row++
model.cursor.Col = 0
model.ensureCursorVisible()
return switchMode(model, ModeInsert)
}
func openLineAbove(model *editorModel) tea.Cmd {
model.buffer.saveUndoState(model.cursor)
model.buffer.insertLine(model.cursor.Row, "")
model.cursor.Col = 0
model.ensureCursorVisible()
return switchMode(model, ModeInsert)
}
func insertCharacter(model *editorModel, char string) (tea.Model, tea.Cmd) {
model.buffer.saveUndoState(model.cursor)
if model.cursor.Col > model.buffer.lineLength(model.cursor.Row) {
model.cursor.Col = model.buffer.lineLength(model.cursor.Row)
}
line := model.buffer.Line(model.cursor.Row)
newLine := line[:model.cursor.Col] + char + line[model.cursor.Col:]
model.buffer.setLine(model.cursor.Row, newLine)
model.cursor.Col++
return model, nil
}
func handleInsertBackspace(model *editorModel) tea.Cmd {
model.buffer.saveUndoState(model.cursor)
if model.cursor.Col > 0 {
model.buffer.deleteAt(model.cursor.Row, model.cursor.Col-1, model.cursor.Row, model.cursor.Col-1)
model.cursor.Col--
} else if model.cursor.Row > 0 {
prevLineLen := model.buffer.lineLength(model.cursor.Row - 1)
model.buffer.deleteAt(model.cursor.Row-1, prevLineLen, model.cursor.Row, 0)
model.cursor.Row--
model.cursor.Col = prevLineLen
}
return nil
}
func handleInsertTab(model *editorModel) tea.Cmd {
model.buffer.saveUndoState(model.cursor)
line := model.buffer.Line(model.cursor.Row)
newLine := line[:model.cursor.Col] + "\t" + line[model.cursor.Col:]
model.buffer.setLine(model.cursor.Row, newLine)
model.cursor.Col += 1
return nil
}
func handleInsertEnterKey(m *editorModel) tea.Cmd {
m.buffer.saveUndoState(m.cursor)
currentLine := m.buffer.Line(m.cursor.Row)
newLine := ""
if m.cursor.Col < len(currentLine) {
newLine = currentLine[m.cursor.Col:]
m.buffer.setLine(m.cursor.Row, currentLine[:m.cursor.Col])
}
m.buffer.insertLine(m.cursor.Row+1, newLine)
m.cursor.Row++
m.cursor.Col = 0
m.ensureCursorVisible()
return nil
}
func moveCursorLeft(model *editorModel) tea.Cmd {
withCountPrefix(model, func() {
if model.cursor.Col > 0 {
model.cursor.Col--
}
})
model.desiredCol = model.cursor.Col
return nil
}
func moveCursorDown(model *editorModel) tea.Cmd {
withCountPrefix(model, func() {
if model.cursor.Row < model.buffer.lineCount()-1 {
model.cursor.Row++
model.cursor.Col = min(model.desiredCol, model.buffer.lineLength(model.cursor.Row)-1)
}
})
model.ensureCursorVisible()
return nil
}
func moveCursorUp(model *editorModel) tea.Cmd {
withCountPrefix(model, func() {
if model.cursor.Row > 0 {
model.cursor.Row--
model.cursor.Col = min(model.desiredCol, model.buffer.lineLength(model.cursor.Row)-1)
}
})
model.ensureCursorVisible()
return nil
}
func moveCursorRight(model *editorModel) tea.Cmd {
lineLen := model.buffer.lineLength(model.cursor.Row)
withCountPrefix(model, func() {
if lineLen > 0 && model.cursor.Col < lineLen-1 {
model.cursor.Col++
}
})
model.desiredCol = model.cursor.Col
return nil
}
func moveCursorRightOrNextLine(model *editorModel) tea.Cmd {
lineLen := model.buffer.lineLength(model.cursor.Row)
if lineLen > 0 && model.cursor.Col < lineLen-1 {
model.cursor.Col++
} else if model.cursor.Row < model.buffer.lineCount()-1 {
model.cursor.Row++
model.cursor.Col = 0
}
model.desiredCol = model.cursor.Col
model.ensureCursorVisible()
return nil
}
func moveToStartOfLine(model *editorModel) tea.Cmd {
model.cursor.Col = 0
return nil
}
func moveToEndOfLine(model *editorModel) tea.Cmd {
lineLen := model.buffer.lineLength(model.cursor.Row)
if lineLen > 0 {
model.cursor.Col = lineLen - 1
} else {
model.cursor.Col = 0
}
model.desiredCol = model.cursor.Col
return nil
}
func moveToStartOfDocument(model *editorModel) tea.Cmd {
model.cursor.Row = 0
model.cursor.Col = min(model.desiredCol, model.buffer.lineLength(model.cursor.Row)-1)
model.keySequence = []string{}
model.ensureCursorVisible()
return nil
}
func moveToEndOfDocument(model *editorModel) tea.Cmd {
model.cursor.Row = model.buffer.lineCount() - 1
model.cursor.Col = min(model.desiredCol, model.buffer.lineLength(model.cursor.Row)-1)
model.keySequence = []string{}
model.ensureCursorVisible()
return nil
}
func handleArrowKeys(key string) func(*editorModel) tea.Cmd {
return func(m *editorModel) tea.Cmd {
switch key {
case "up":
return moveCursorUp(m)
case "down":
return moveCursorDown(m)
case "left":
return moveCursorLeft(m)
case "right":
return moveCursorRight(m)
}
return nil
}
}
func executeCommand(model *editorModel) tea.Cmd {
command := model.commandBuffer
model.commandBuffer = ""
return func() tea.Msg {
return CommandMsg{command}
}
}
func addCommandCharacter(model *editorModel, char string) (tea.Model, tea.Cmd) {
model.commandBuffer += char
return model, nil
}
func commandBackspace(model *editorModel) tea.Cmd {
if len(model.commandBuffer) > 0 {
model.commandBuffer = model.commandBuffer[:len(model.commandBuffer)-1]
}
return nil
}
func moveToNextWordStart(model *editorModel) tea.Cmd {
currRow := model.cursor.Row
if currRow >= model.buffer.lineCount() {
return nil
}
line := model.buffer.Line(currRow)
startPos := model.cursor.Col + 1
if startPos >= len(line) {
if currRow < model.buffer.lineCount()-1 {
model.cursor.Row++
model.cursor.Col = 0
model.ensureCursorVisible()
}
model.desiredCol = model.cursor.Col
return nil
}
for i := startPos; i < len(line); i++ {
if (i == 0 || isWordSeparator(line[i-1])) && !isWordSeparator(line[i]) {
model.cursor.Col = i
model.desiredCol = model.cursor.Col
return nil
}
}
model.cursor.Col = max(0, len(line)-1)
model.desiredCol = model.cursor.Col
return nil
}
func moveToPrevWordStart(model *editorModel) tea.Cmd {
currRow := model.cursor.Row
if currRow >= model.buffer.lineCount() {
return nil
}
line := model.buffer.Line(currRow)
if model.cursor.Col <= 0 {
if currRow > 0 {
model.cursor.Row--
prevLineLen := model.buffer.lineLength(model.cursor.Row)
model.cursor.Col = max(0, prevLineLen-1)
model.desiredCol = model.cursor.Col
model.ensureCursorVisible()
}
return nil
}
for i := model.cursor.Col - 1; i >= 0; i-- {
if (i == 0 || isWordSeparator(line[i-1])) && !isWordSeparator(line[i]) {
model.cursor.Col = i
model.desiredCol = model.cursor.Col
return nil
}
}
model.cursor.Col = 0
model.desiredCol = model.cursor.Col
return nil
}
func undo(model *editorModel) tea.Cmd {
return model.buffer.undo(model.cursor)
}
func redo(model *editorModel) tea.Cmd {
return model.buffer.redo(model.cursor)
}
func deleteCharAtCursor(model *editorModel) tea.Cmd {
model.buffer.saveUndoState(model.cursor)
lineLen := model.buffer.lineLength(model.cursor.Row)
if lineLen > 0 && model.cursor.Col < lineLen {
model.buffer.deleteAt(model.cursor.Row, model.cursor.Col, model.cursor.Row, model.cursor.Col)
newLineLen := model.buffer.lineLength(model.cursor.Row)
if model.cursor.Col >= newLineLen && newLineLen > 0 {
model.cursor.Col = newLineLen - 1
}
}
return nil
}
func setupYankHighlight(model *editorModel, start, end Cursor, text string, isLinewise bool) {
model.yankBuffer = text
clipboard.Write(clipboard.FmtText, []byte(model.yankBuffer))
model.statusMessage = fmt.Sprintf("yanked %d characters", len(text))
model.yankHighlight.Start = start
model.yankHighlight.End = end
model.yankHighlight.StartTime = time.Now()
model.yankHighlight.IsLinewise = isLinewise
model.yankHighlight.Active = true
}
func yankLine(model *editorModel) tea.Cmd {
line := model.buffer.Line(model.cursor.Row)
setupYankHighlight(
model,
Cursor{model.cursor.Row, 0},
Cursor{model.cursor.Row, max(0, len(line)-1)},
"\n"+line,
true,
)
model.keySequence = []string{}
return nil
}
func deleteLine(model *editorModel) tea.Cmd {
model.buffer.saveUndoState(model.cursor)
row := model.cursor.Row
lineContent := model.buffer.Line(row)
model.yankBuffer = "\n" + lineContent
clipboard.Write(clipboard.FmtText, []byte(model.yankBuffer))
model.buffer.deleteLine(row)
if model.buffer.lineCount() == 0 {
model.buffer.insertLine(0, "")
}
if model.cursor.Row >= model.buffer.lineCount() {
model.cursor.Row = model.buffer.lineCount() - 1
}
if model.cursor.Col >= model.buffer.lineLength(model.cursor.Row) {
model.cursor.Col = max(0, model.buffer.lineLength(model.cursor.Row)-1)
}
model.ensureCursorVisible()
return nil
}
func pasteAfter(model *editorModel) tea.Cmd {
data := clipboard.Read(clipboard.FmtText)
model.yankBuffer = string(data)
if model.yankBuffer == "" {
return nil
}
model.buffer.saveUndoState(model.cursor)
// Line-wise paste
if strings.HasPrefix(model.yankBuffer, "\n") {
return pasteLineAfter(model)
}
// Character-wise paste
currLine := model.buffer.Line(model.cursor.Row)
insertPos := model.cursor.Col
// Check if the yanked text contains newlines (multi-line character-wise yank)
if strings.Contains(model.yankBuffer, "\n") {
// Split the yanked text by newlines
lines := strings.Split(model.yankBuffer, "\n")
// Handle the first line - insert at cursor position in current line
firstLine := lines[0]
remainderOfLine := ""
if insertPos < len(currLine) {
remainderOfLine = currLine[insertPos+1:]
}
// Set the first line with the content before cursor + first part of yanked text
if insertPos >= len(currLine) {
model.buffer.setLine(model.cursor.Row, currLine+firstLine)
} else {
model.buffer.setLine(model.cursor.Row,
currLine[:insertPos+1]+firstLine)
}
// Insert middle lines as new lines
row := model.cursor.Row
for i := 1; i < len(lines)-1; i++ {
model.buffer.insertLine(row+i, lines[i])
}
// Handle the last line separately
if len(lines) > 1 {
lastLine := lines[len(lines)-1]
model.buffer.insertLine(row+len(lines)-1, lastLine+remainderOfLine)
} else {
// If only one line, append the remainder to the current line
currLineContent := model.buffer.Line(model.cursor.Row)
model.buffer.setLine(model.cursor.Row, currLineContent+remainderOfLine)
}
// Position cursor at the end of the last inserted line
model.cursor.Row = row + len(lines) - 1
if len(lines) > 1 {
// For multi-line pastes, position at the end of the last line's content
model.cursor.Col = len(lines[len(lines)-1])
} else {
// For single line pastes, position at the end of what was pasted
model.cursor.Col = insertPos + len(firstLine) + 1
}
if model.mode != ModeInsert && model.cursor.Col > 0 {
model.cursor.Col--
}
} else {
// Single-line paste - original behavior
if insertPos >= len(currLine) {
model.buffer.setLine(model.cursor.Row, currLine+model.yankBuffer)
} else {
model.buffer.setLine(model.cursor.Row,
currLine[:insertPos+1]+model.yankBuffer+currLine[insertPos+1:])
}
model.cursor.Col = insertPos + len(model.yankBuffer) + 1
if model.mode != ModeInsert && model.cursor.Col > 0 {
model.cursor.Col--
}
}
model.ensureCursorVisible()
return nil
}
func pasteBefore(model *editorModel) tea.Cmd {
data := clipboard.Read(clipboard.FmtText)
model.yankBuffer = string(data)
if model.yankBuffer == "" {
return nil
}
model.buffer.saveUndoState(model.cursor)
// Line-wise paste
if strings.HasPrefix(model.yankBuffer, "\n") {
return pasteLineBefore(model)
}
// Character-wise paste
currLine := model.buffer.Line(model.cursor.Row)
insertPos := model.cursor.Col
// Check if the yanked text contains newlines (multi-line character-wise yank)
if strings.Contains(model.yankBuffer, "\n") {
// Split the yanked text by newlines
lines := strings.Split(model.yankBuffer, "\n")
// Handle the first line - insert at cursor position in current line
firstLine := lines[0]
newFirstLine := currLine[:insertPos] + firstLine
model.buffer.setLine(model.cursor.Row, newFirstLine)
// If this is the last line, append the remainder of the original line
if len(lines) == 1 {
model.buffer.setLine(model.cursor.Row, newFirstLine+currLine[insertPos:])
} else {
// Handle the last line - combine with remainder of current line
lastLineIndex := len(lines) - 1
lastLine := lines[lastLineIndex] + currLine[insertPos:]
// Insert middle and last lines as new lines
row := model.cursor.Row
for i := 1; i < lastLineIndex; i++ {
model.buffer.insertLine(row+i, lines[i])
}
model.buffer.insertLine(row+lastLineIndex, lastLine)
}
// Position cursor appropriately depending on where the paste ended
if len(lines) > 1 {
// For multi-line pastes in pasteBefore, cursor stays at the insertion point
model.cursor.Col = insertPos + len(firstLine)
} else {
// For single line pastes, position at the end of what was pasted
model.cursor.Col = insertPos + len(firstLine)
}
if model.mode != ModeInsert && model.cursor.Col > 0 {
model.cursor.Col--
}
} else {
// Single-line paste - original behavior
model.buffer.setLine(model.cursor.Row,
currLine[:insertPos]+model.yankBuffer+currLine[insertPos:])
model.cursor.Col = max(insertPos+len(model.yankBuffer)-1, 0)
}
model.ensureCursorVisible()
return nil
}
func pasteLineAfter(model *editorModel) tea.Cmd {
data := clipboard.Read(clipboard.FmtText)
model.yankBuffer = string(data)
lines := strings.Split(model.yankBuffer[1:], "\n")
row := model.cursor.Row
for i := range lines {
model.buffer.insertLine(row+1+i, lines[i])
}
model.cursor.Row = row + 1
model.cursor.Col = 0
model.ensureCursorVisible()
return nil
}
func pasteLineBefore(model *editorModel) tea.Cmd {
data := clipboard.Read(clipboard.FmtText)
model.yankBuffer = string(data)
lines := strings.Split(model.yankBuffer[1:], "\n")
row := model.cursor.Row
for i := range lines {
model.buffer.insertLine(row+i, lines[i])
}
model.cursor.Col = 0
model.ensureCursorVisible()
return nil
}
func yankVisualSelection(model *editorModel) tea.Cmd {
start, end := model.GetSelectionBoundary()
selectedText := model.buffer.getRange(start, end)
if model.isVisualLine {
selectedText = "\n" + selectedText
}
setupYankHighlight(model, start, end, selectedText, model.isVisualLine)
return switchMode(model, ModeNormal)
}
func deleteVisualSelection(model *editorModel) tea.Cmd {
model.buffer.saveUndoState(model.cursor)
start, end := model.GetSelectionBoundary()
selectedText := model.buffer.getRange(start, end)
if model.isVisualLine {
selectedText = "\n" + selectedText
}
model.yankBuffer = selectedText
clipboard.Write(clipboard.FmtText, []byte(model.yankBuffer))
model.buffer.deleteRange(start, end)
model.cursor = start
model.ensureCursorVisible()
return switchMode(model, ModeNormal)
}
func replaceVisualSelectionWithYank(model *editorModel) tea.Cmd {
model.buffer.saveUndoState(model.cursor)
start, end := model.GetSelectionBoundary()
oldSelection := model.buffer.deleteRange(start, end)
model.yankBuffer = oldSelection
clipboard.Write(clipboard.FmtText, []byte(model.yankBuffer))
model.cursor = start
if strings.Contains(model.yankBuffer, "\n") {
pasteLineBefore(model)
} else {
currLine := model.buffer.Line(model.cursor.Row)
insertPos := model.cursor.Col
model.buffer.setLine(model.cursor.Row,
currLine[:insertPos]+model.yankBuffer+currLine[insertPos:])
model.cursor.Col = max(insertPos+len(model.yankBuffer)-1, 0)
}
model.ensureCursorVisible()
return switchMode(model, ModeNormal)
}
func performWordOperation(model *editorModel, operation string) tea.Cmd {
start, end := getWordBoundary(model)
if start == end {
return nil
}
word := model.buffer.Line(model.cursor.Row)[start:end]
if operation == "delete" || operation == "change" {
model.buffer.saveUndoState(model.cursor)
}
model.yankBuffer = word
clipboard.Write(clipboard.FmtText, []byte(model.yankBuffer))
switch operation {
case "yank":
model.statusMessage = fmt.Sprintf("yanked word: %s", model.yankBuffer)
model.yankHighlight.Start = Cursor{model.cursor.Row, start}
model.yankHighlight.End = Cursor{model.cursor.Row, end - 1}
model.yankHighlight.StartTime = time.Now()
model.yankHighlight.IsLinewise = false
model.yankHighlight.Active = true
case "delete", "change":
line := model.buffer.Line(model.cursor.Row)
newLine := line[:start] + line[end:]
model.buffer.setLine(model.cursor.Row, newLine)
model.cursor.Col = start
if operation == "change" {
return switchMode(model, ModeInsert)
}
}
model.keySequence = []string{}
return nil
}
func deleteInnerWord(model *editorModel) tea.Cmd {
return performWordOperation(model, "delete")
}
func yankInnerWord(model *editorModel) tea.Cmd {
return performWordOperation(model, "yank")
}
func changeInnerWord(model *editorModel) tea.Cmd {
return performWordOperation(model, "change")
}
func getWordBoundary(model *editorModel) (int, int) {
line := model.buffer.Line(model.cursor.Row)
if len(line) == 0 {
return 0, 0
}
col := model.cursor.Col
if col >= len(line) {
col = len(line) - 1
}
start := col
if isWordSeparator(line[col]) {
for start > 0 && isWordSeparator(line[start-1]) {
start--
}
} else {
for start > 0 && !isWordSeparator(line[start-1]) {
start--
}
}
end := col
if isWordSeparator(line[col]) {
for end < len(line)-1 && isWordSeparator(line[end+1]) {
end++
}
} else {
for end < len(line)-1 && !isWordSeparator(line[end+1]) {
end++
}
}
return start, end + 1
}
func isWordSeparator(ch byte) bool {
return ch == ' ' || ch == '\t' || ch == '.' || ch == ',' ||
ch == ';' || ch == ':' || ch == '!' || ch == '?' ||
ch == '(' || ch == ')' || ch == '[' || ch == ']' ||
ch == '{' || ch == '}' || ch == '<' || ch == '>' ||
ch == '/' || ch == '\\' || ch == '+' || ch == '-' ||
ch == '*' || ch == '&' || ch == '^' || ch == '%' ||
ch == '$' || ch == '#' || ch == '@' || ch == '=' ||
ch == '|' || ch == '`' || ch == '~' || ch == '"' ||
ch == '\''
}