-
Notifications
You must be signed in to change notification settings - Fork 2
/
main.go
1092 lines (956 loc) · 22.3 KB
/
main.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 main
import (
"bufio"
"bytes"
"encoding/json"
"fmt"
"log"
"math"
"os"
"path/filepath"
"strings"
"syscall"
"unicode"
"unsafe"
)
var errorlog *os.File
var logger *log.Logger
var prevCommand rune
var prevCharacters []rune
const (
TAB_STOP = 4
)
const (
BACKSPACE = 127
CURSOR_UP = 1000
CURSOR_DOWN = 1001
CURSOR_LEFT = 1002
CURSOR_RIGHT = 1003
PAGE_UP = 1004
PAGE_DOWN = 1005
HOME_KEY = 1006
END_KEY = 1007
DEL_KEY = 1008
)
const (
INSERT_MODE = 1
NORMAL_MODE = 2
CMD_MODE = 3
)
const (
BLACK = 30
RED = 31
GREEN = 32
YELLOW = 33
BLUE = 34
MAGENTA = 35
CYAN = 36
WHITE = 37
)
const (
HL_COMMENTS = RED
HL_STATEMENTS = YELLOW
HL_TYPES = GREEN
)
const (
FORWARD = 1
BACKWARD = 2
)
type hl_groups struct {
Comments []string `json:"comments"`
Statements []string `josn:"statements"`
Types []string `json:"types"`
}
type winsize struct {
height uint16
width uint16
x uint16
y uint16
}
type erow struct {
chars string
render string
size int
rsize int
highlight []int
}
type terminal int
func (t terminal) Read(buf []byte) (int, error) {
return syscall.Read(int(t), buf)
}
func (t terminal) Write(s string) {
b := bytes.NewBufferString(s)
if _, err := syscall.Write(int(t), b.Bytes()); err != nil {
logger.Fatal(err)
}
}
type cursor struct {
x, y int
}
type searchObject struct {
query string
location cursor
}
type editorMsgBar struct {
msg string
bgColor int
fgColor int
}
type editor struct {
reader terminal
orignial syscall.Termios
height int
width int
editorUI *bytes.Buffer
cursor cursor
mode int
filename string
rowOffSet int
colOffSet int
numOfRows int
rows []erow
rx int
editormsg editorMsgBar
lineNumOffSet int
search searchObject
modifiyed bool
}
func (r *erow) updateRow() {
r.highlight = nil
buf := bytes.NewBufferString("")
raw := []byte(r.chars)
rsize := 0
for x := 0; x < len(raw); x++ {
if raw[x] == '\t' {
for i := 0; i < TAB_STOP; i++ {
rsize++
buf.WriteByte(' ')
}
} else {
rsize++
buf.WriteByte(raw[x])
}
}
buf.WriteByte('\000')
r.rsize = rsize
r.render = buf.String()
r.updateSyntax()
}
func (r *erow) updateSyntax() {
raw := []byte(r.render)
for x := 0; x < r.rsize; x++ {
if unicode.IsDigit(rune(raw[x])) && x != 0 && !unicode.IsLetter(rune(raw[x-1])) {
r.highlight = append(r.highlight, MAGENTA)
} else {
r.highlight = append(r.highlight, WHITE)
}
}
}
func (r *erow) deleteRune(pos int) {
if pos < 0 || pos > r.size {
return
}
buf := bytes.NewBufferString("")
raw := []byte(r.chars)
if pos == 0 {
buf.Write(raw[1:])
} else if pos == r.size {
buf.Write(raw[0 : len(raw)-2])
} else {
buf.Write(raw[0:pos])
buf.Write(raw[pos+1:])
}
r.chars = buf.String()
r.size--
r.updateRow()
}
func editorReplaceRune() {
key := readKey()
if key < 32 || key > 126 {
return
}
goedit.moveCursor(CURSOR_RIGHT)
editorDelRune()
editorInsertRune(key)
goedit.modifiyed = true
}
func editorDelFromCursorToEndOfLine() {
raw := []byte(goedit.rows[goedit.cursor.y].chars)
buf := bytes.NewBuffer(raw[:goedit.cursor.x])
buf.WriteByte('\000')
goedit.rows[goedit.cursor.y].chars = buf.String()
goedit.rows[goedit.cursor.y].size = len(buf.String())
goedit.rows[goedit.cursor.y].updateRow()
goedit.modifiyed = true
}
func editorDelRune() {
if goedit.cursor.y == goedit.numOfRows {
return
}
if goedit.cursor.x == 0 && goedit.cursor.y == 0 {
return
}
if goedit.cursor.x > 0 {
goedit.rows[goedit.cursor.y].deleteRune(goedit.cursor.x - 1)
goedit.cursor.x--
} else {
goedit.cursor.x = goedit.rows[goedit.cursor.y-1].size
goedit.rows[goedit.cursor.y-1].appendRow(goedit.rows[goedit.cursor.y].chars)
editorDelRow(goedit.cursor.y)
goedit.cursor.y--
}
goedit.modifiyed = true
}
func editorDelRow(pos int) {
if pos < 0 || pos >= goedit.numOfRows {
return
}
goedit.rows = append(goedit.rows[:pos], goedit.rows[pos+1:]...)
goedit.numOfRows--
goedit.modifiyed = true
}
func (r *erow) appendRow(chars string) {
r.chars = fmt.Sprintf("%s%s\000", r.chars, chars)
r.size = len(r.chars)
r.updateRow()
}
func (r *erow) insertRune(c rune, pos int) {
if pos < 0 || pos > r.size {
pos = r.size
}
buf := bytes.NewBufferString("")
raw := []byte(r.chars)
if pos == 0 {
buf.WriteRune(c)
buf.WriteString(r.chars)
} else if pos == r.size {
buf.WriteString(r.chars)
buf.WriteRune(c)
} else {
buf.WriteString(string(raw[0:pos]))
buf.WriteRune(c)
buf.WriteString(string(raw[pos:]))
}
r.chars = buf.String()
r.size = len(r.chars)
r.updateRow()
}
func editorInsertRune(c rune) {
if goedit.cursor.y == goedit.numOfRows {
goedit.insertRow(goedit.numOfRows, "")
}
goedit.rows[goedit.cursor.y].insertRune(c, goedit.cursor.x)
goedit.cursor.x++
goedit.modifiyed = true
}
func (e *editor) insertRow(pos int, r string) {
if pos < 0 || pos > e.numOfRows {
return
}
buf := bytes.NewBufferString(strings.TrimRight(r, "\000"))
buf.WriteByte('\000')
row := erow{chars: buf.String()}
row.size = len(row.chars) - 1
row.updateRow()
if pos == 0 {
var rows []erow
rows = append(rows, row)
goedit.rows = append(rows, goedit.rows...)
} else if pos == goedit.numOfRows {
goedit.rows = append(goedit.rows, row)
} else {
rows := append(goedit.rows[:pos-1], row)
goedit.rows = append(rows, goedit.rows[pos-1:]...)
}
goedit.numOfRows++
e.lineNumOffSet = int(math.Log10(float64(e.numOfRows))) + 2
}
func editorInsertNewline() {
if goedit.cursor.x == 0 {
goedit.insertRow(goedit.cursor.y, "")
} else {
raw := []byte(goedit.rows[goedit.cursor.y].chars)
newline := string(raw[goedit.cursor.x:])
oldline := fmt.Sprintf("%s\000", string(raw[:goedit.cursor.x]))
goedit.insertRow(goedit.cursor.y+1, newline)
goedit.rows[goedit.cursor.y].chars = oldline
goedit.rows[goedit.cursor.y].size = len(oldline)
goedit.rows[goedit.cursor.y].updateRow()
}
goedit.cursor.x = 0
goedit.cursor.y++
goedit.modifiyed = true
}
func editorPrompt(msg string) string {
oldcursor := goedit.cursor
buf := bytes.NewBufferString("")
msgLength := len(msg)
goedit.cursor.x = msgLength
goedit.editormsg.fgColor = WHITE
goedit.editormsg.bgColor = 49
for {
goedit.editormsg.msg = fmt.Sprintf("%s%s", msg, buf)
goedit.cursor.y = goedit.height + 1
clearScreen()
key := readKey()
switch key {
case '\r':
//goedit.editormsg = ""
goedit.cursor = oldcursor
return buf.String()
case BACKSPACE:
if goedit.cursor.x <= msgLength {
break
}
if goedit.cursor.x == msgLength {
buf.Reset()
} else if goedit.cursor.x == len(goedit.editormsg.msg) {
tmp := bytes.NewBuffer(buf.Bytes()[:len(buf.String())-1])
buf = tmp
} else {
tmp := bytes.NewBuffer(buf.Bytes()[:goedit.cursor.x-2])
tmp.Write(buf.Bytes()[goedit.cursor.x-1:])
buf = tmp
}
goedit.cursor.x--
case CURSOR_LEFT:
if goedit.cursor.x != msgLength {
goedit.cursor.x--
}
case CURSOR_RIGHT:
if goedit.cursor.x < len(goedit.editormsg.msg) {
goedit.cursor.x++
}
default:
buf.WriteRune(key)
goedit.cursor.x++
}
}
}
func cursorxToRx(row erow, cx int) int {
rx := 0
raw := []byte(row.chars)
for x := 0; x < cx; x++ {
if raw[x] == '\t' {
rx += (TAB_STOP - 1) - (rx % TAB_STOP)
}
rx++
}
return rx
}
func cursorxToCx(row erow, rx int) int {
cur_rx := 0
cx := 0
for cx = 0; cx < row.size; cx++ {
if row.chars[cx] == '\t' {
cur_rx += (TAB_STOP - 1) - (cur_rx % TAB_STOP)
}
cur_rx++
if cur_rx > rx {
return cx
}
}
return cx
}
var goedit editor
func init() {
errorlog, errr := os.OpenFile("log.txt", os.O_RDWR|os.O_CREATE|os.O_APPEND, 0666)
if errr != nil {
logger.Fatal(errr)
}
logger = log.New(errorlog, "goedit: ", log.Lshortfile|log.LstdFlags)
goedit = editor{}
goedit.mode = NORMAL_MODE
goedit.reader = terminal(syscall.Stdin)
if err := goedit.getShellNormal(); err != 0 {
logger.Fatal(err)
}
winsize := winsize{}
if _, _, err := syscall.Syscall(syscall.SYS_IOCTL, uintptr(goedit.reader), syscall.TIOCGWINSZ, uintptr(unsafe.Pointer(&winsize))); err != 0 {
logger.Fatal(err)
}
goedit.height = int(winsize.height) - 2
goedit.width = int(winsize.width)
goedit.editorUI = bytes.NewBufferString("")
goedit.editormsg.fgColor = WHITE
goedit.editormsg.bgColor = 49
}
func openFile(filename string) {
file, err := os.Open(filename)
if err != nil {
logger.Fatal(err)
}
defer file.Close()
line := 0
goedit.rows = []erow{}
scanner := bufio.NewScanner(file)
for scanner.Scan() {
goedit.insertRow(line, scanner.Text())
line++
}
goedit.numOfRows = len(goedit.rows)
goedit.filename = filename
selectSyntax(filename)
}
func selectSyntax(filename string) {
var syntaxFile string
switch filepath.Ext(filename) {
case ".go":
syntaxFile = "go.json"
default:
return
}
syntax, errr := os.Open(syntaxFile)
if errr != nil {
logger.Println("no syntax file found")
return
}
defer syntax.Close()
var syn hl_groups
decoder := json.NewDecoder(syntax)
if errr := decoder.Decode(&syn); errr != nil {
logger.Println(errr)
return
}
}
func drawStatusBar() {
status := fmt.Sprintf("%.20s - %d lines", goedit.filename, goedit.numOfRows)
length := len(status)
rstatus := fmt.Sprintf("%d,%d", goedit.cursor.y+1, goedit.rx+1)
rlength := len(rstatus)
goedit.editorUI.WriteString("\x1b[7m")
goedit.editorUI.WriteString(status)
for x := length; x < goedit.width; x++ {
if goedit.width-x == rlength {
goedit.editorUI.WriteString(rstatus)
break
} else {
goedit.editorUI.WriteString(" ")
}
}
goedit.editorUI.WriteString("\x1b[m")
goedit.editorUI.WriteString("\r\n")
}
func drawMessageBar() {
goedit.editorUI.WriteString("\x1b[K")
goedit.editorUI.WriteString(fmt.Sprintf("\x1b[%d;%dm", goedit.editormsg.fgColor, goedit.editormsg.bgColor))
goedit.editorUI.WriteString(goedit.editormsg.msg)
goedit.editorUI.WriteString("\x1b[39;49m")
}
func drawRows() {
for x := 0; x < goedit.height; x++ {
filerow := x + goedit.rowOffSet
if filerow >= goedit.numOfRows {
goedit.editorUI.WriteString("~")
} else {
length := goedit.rows[filerow].rsize - goedit.colOffSet
if length < 0 {
length = 0
}
if length > goedit.width {
length = goedit.width
}
text := []byte(goedit.rows[filerow].render)
formatter := fmt.Sprintf("%%%dd ", goedit.lineNumOffSet-1)
goedit.editorUI.WriteString(fmt.Sprintf("\x1b[%dm", GREEN))
goedit.editorUI.WriteString(fmt.Sprintf(formatter, filerow+1))
goedit.editorUI.WriteString("\x1b[39;49m")
for i := goedit.colOffSet; i < length; i++ {
goedit.editorUI.WriteString(fmt.Sprintf("\x1b[%dm", goedit.rows[filerow].highlight[i]))
goedit.editorUI.WriteByte(text[i])
}
}
goedit.editorUI.WriteString("\x1b[K")
goedit.editorUI.WriteString("\r\n")
}
}
func scroll() {
if goedit.mode != CMD_MODE {
goedit.rx = 0
if goedit.cursor.y < goedit.numOfRows {
goedit.rx = cursorxToRx(goedit.rows[goedit.cursor.y], goedit.cursor.x)
}
if goedit.cursor.y < goedit.rowOffSet {
goedit.rowOffSet = goedit.cursor.y
}
if goedit.cursor.y >= goedit.rowOffSet+goedit.height {
goedit.rowOffSet = goedit.cursor.y - goedit.height + 1
}
if goedit.rx < goedit.colOffSet {
goedit.colOffSet = goedit.rx
}
if goedit.rx >= goedit.colOffSet+goedit.width {
goedit.colOffSet = goedit.rx - goedit.width + 1
}
}
}
func rawMode() {
argp := goedit.orignial
argp.Iflag &^= syscall.BRKINT | syscall.ICRNL | syscall.INPCK | syscall.ISTRIP | syscall.IXON
argp.Oflag &^= syscall.OPOST
argp.Cflag |= syscall.CS8
argp.Lflag &^= syscall.ECHO | syscall.ICANON | syscall.ISIG
argp.Cc[syscall.VMIN] = 1
argp.Cc[syscall.VTIME] = 0
if err := goedit.rawMode(argp); err != 0 {
logger.Fatal(err)
}
}
func readKey() rune {
var buf [1]byte
for {
n, err := goedit.reader.Read(buf[:])
if err != nil {
logger.Fatal(err)
}
if n == 1 {
break
}
}
if buf[0] == '\x1b' {
var seq [2]byte
n, err := goedit.reader.Read(seq[:])
if err != nil {
logger.Fatal(err)
}
if n != 2 {
return '\x1b'
}
if seq[0] == '[' {
if seq[1] >= '0' && seq[1] <= '9' {
var tilde [1]byte
n, err := goedit.reader.Read(tilde[:])
if err != nil {
logger.Fatal(err)
}
if n != 1 {
return '\x1b'
}
if tilde[0] == '~' {
switch seq[1] {
case '1', '7':
return HOME_KEY
case '3':
return DEL_KEY
case '4', '8':
return END_KEY
case '5':
return PAGE_UP
case '6':
return PAGE_DOWN
}
}
} else {
switch seq[1] {
case 'A':
return CURSOR_UP
case 'B':
return CURSOR_DOWN
case 'C':
return CURSOR_RIGHT
case 'D':
return CURSOR_LEFT
case 'H':
return HOME_KEY
case 'F':
return END_KEY
}
}
} else if seq[0] == 'O' {
switch seq[1] {
case 'H':
return HOME_KEY
case 'F':
return END_KEY
}
}
return '\x1b'
}
return bytes.Runes(buf[:])[0]
}
func (e *editor) moveCursor(key rune) {
switch key {
case CURSOR_DOWN:
if e.cursor.y < goedit.numOfRows {
e.cursor.y++
}
case CURSOR_UP:
if e.cursor.y != 0 {
e.cursor.y--
}
case CURSOR_LEFT:
if e.cursor.x != 0 {
e.cursor.x--
} else if e.cursor.y > 0 {
e.cursor.y--
e.cursor.x = e.rows[e.cursor.y].size
}
case CURSOR_RIGHT:
if e.cursor.y < e.numOfRows && e.cursor.x < e.rows[e.cursor.y].size {
e.cursor.x++
} else if e.cursor.y < e.numOfRows && e.cursor.x == e.rows[e.cursor.y].size {
e.cursor.y++
e.cursor.x = 0
}
}
if e.cursor.y < e.numOfRows {
if e.cursor.x > e.rows[e.cursor.y].size {
e.cursor.x = e.rows[e.cursor.y].size
}
}
}
func (e *editor) rowsToString() string {
buf := bytes.NewBufferString("")
for _, r := range e.rows {
raw := []byte(r.chars)
raw[len(raw)-1] = '\n'
buf.Write(raw)
}
return buf.String()
}
func (e *editor) save() {
if e.filename == "" {
e.filename = editorPrompt("Save as ")
selectSyntax(e.filename)
}
file, err := os.OpenFile(e.filename, os.O_RDWR|os.O_CREATE, 0644)
if err != nil {
logger.Fatal(err)
}
defer file.Close()
text := e.rowsToString()
length := len(text)
if err := file.Truncate(int64(length)); err != nil {
e.editormsg.msg = err.Error()
return
}
n, err := file.WriteAt([]byte(text), 0)
if err != nil {
e.editormsg.msg = err.Error()
return
}
e.editormsg.msg = fmt.Sprintf("\"%s\" %dL %d bytess written to disk", e.filename, e.numOfRows, n)
goedit.modifiyed = false
}
func clearScreen() {
scroll()
goedit.editorUI.Reset()
goedit.editorUI.WriteString("\x1b[?25l")
goedit.editorUI.WriteString("\x1b[H")
drawRows()
drawStatusBar()
drawMessageBar()
if goedit.mode == CMD_MODE {
goedit.editorUI.WriteString(fmt.Sprintf("\x1b[%d;%dH", goedit.cursor.y+1, goedit.cursor.x+1))
} else {
goedit.editorUI.WriteString(fmt.Sprintf("\x1b[%d;%dH", (goedit.cursor.y-goedit.rowOffSet)+1, (goedit.rx-goedit.colOffSet)+1+goedit.lineNumOffSet))
}
goedit.editorUI.WriteString("\x1b[?25h")
goedit.reader.Write(goedit.editorUI.String())
goedit.editorUI.Reset()
}
func editorSearch() {
query := editorPrompt("/")
for i, row := range goedit.rows {
indx := strings.Index(row.render, query)
if indx != -1 {
goedit.cursor.y = i
goedit.cursor.x = cursorxToCx(row, indx)
goedit.search.location = goedit.cursor
goedit.search.query = query
return
}
}
goedit.editormsg.msg = fmt.Sprintf("Pattern not found: %s", query)
goedit.editormsg.fgColor = WHITE
goedit.editormsg.bgColor = BLUE + 10
}
func editorNextSearch() {
if goedit.search.query == "" {
return
}
if goedit.search.location.x+1 < goedit.rows[goedit.search.location.y].rsize {
raw := []byte(goedit.rows[goedit.search.location.y].render)
loc := cursorxToRx(goedit.rows[goedit.search.location.y], goedit.search.location.x+1)
indx := strings.Index(string(raw[loc:]), goedit.search.query)
if indx != -1 {
goedit.cursor.x = cursorxToCx(goedit.rows[goedit.search.location.y], indx)
goedit.search.location.x = loc
return
}
}
for i := goedit.search.location.y + 1; i < goedit.numOfRows; i++ {
indx := strings.Index(goedit.rows[i].render, goedit.search.query)
if indx != -1 {
goedit.cursor.y = i
goedit.cursor.x = cursorxToCx(goedit.rows[i], indx)
goedit.search.location.y = i
goedit.search.location.x = indx
return
}
}
goedit.editormsg.msg = "Hit bottom, starting from the top"
goedit.editormsg.fgColor = RED
goedit.search.location.x = 0
goedit.search.location.y = 0
editorNextSearch()
}
func editorPrevSearch() {
if goedit.search.query == "" {
return
}
if goedit.search.location.x-1 > 0 {
raw := []byte(goedit.rows[goedit.search.location.y].render)
indx := strings.Index(string(raw[:goedit.search.location.x-1]), goedit.search.query)
if indx != -1 {
goedit.cursor.x = cursorxToCx(goedit.rows[goedit.search.location.y], indx)
goedit.search.location.x = indx
return
}
}
for i := goedit.search.location.y - 1; i > 0; i-- {
indx := strings.Index(goedit.rows[i].render, goedit.search.query)
if indx != -1 {
goedit.cursor.y = i
goedit.cursor.x = cursorxToCx(goedit.rows[i], indx)
goedit.search.location.y = i
goedit.search.location.x = indx
return
}
}
goedit.editormsg.msg = "Hit top, starting from the bottom"
goedit.editormsg.fgColor = RED
goedit.search.location.x = goedit.rows[goedit.numOfRows-1].rsize
goedit.search.location.y = goedit.numOfRows - 1
editorPrevSearch()
}
func editorQuit(force bool) {
if goedit.modifiyed == true && force == false {
goedit.editormsg.msg = "No write since last change (add ! to override)"
goedit.editormsg.fgColor = WHITE
goedit.editormsg.bgColor = BLUE + 10
return
}
goedit.resetMode()
os.Exit(0)
}
func editorFindInRow(direction int, modifier int) {
key := readKey()
if key < 32 || key > 126 {
return
}
var text string
var cursorx int
if direction == FORWARD {
text = string(goedit.rows[goedit.cursor.y].chars[goedit.cursor.x:])
cursorx = goedit.cursor.x
} else if direction == BACKWARD {
text = string(goedit.rows[goedit.cursor.y].chars[:goedit.cursor.x])
cursorx = 0
}
indx := strings.IndexRune(text, key)
if indx == -1 {
return
}
goedit.cursor.x = indx + modifier + cursorx
}
func editorCommandMode() {
result := editorPrompt(":")
cmd := strings.Split(result, " ")
switch cmd[0] {
case "q", "quit":
editorQuit(false)
case "q!", "quit!":
editorQuit(true)
case "w", "write":
goedit.save()
case "wq":
goedit.save()
goedit.resetMode()
os.Exit(0)
case "o", "open":
if len(cmd) == 2 {
openFile(cmd[1])
}
}
}
func getNormalModeCommand(key rune, clear bool) bool {
switch key {
case 'h':
goedit.moveCursor(CURSOR_LEFT)
case 'j':
goedit.moveCursor(CURSOR_DOWN)
case 'k':
goedit.moveCursor(CURSOR_UP)
case 'l':
goedit.moveCursor(CURSOR_RIGHT)
case ':':
goedit.mode = CMD_MODE
editorCommandMode()
goedit.mode = NORMAL_MODE
case '/':
goedit.mode = CMD_MODE
editorSearch()
goedit.mode = NORMAL_MODE
case 'i':
if clear {
prevCharacters = prevCharacters[:0]
prevCommand = key
}
goedit.mode = INSERT_MODE
goedit.editormsg.msg = "-- INSERT --"
prevCommand = key
return false
case 'n':
editorNextSearch()
prevCommand = key
case 'N':
editorPrevSearch()
prevCommand = key
case '$':
if goedit.cursor.y < goedit.numOfRows {
goedit.cursor.x = goedit.rows[goedit.cursor.y].size
}
case '0':
goedit.cursor.x = 0
case 'D':
editorDelFromCursorToEndOfLine()
goedit.moveCursor(CURSOR_LEFT)
prevCommand = key
case 'C':
editorDelFromCursorToEndOfLine()
goedit.mode = INSERT_MODE
goedit.editormsg.msg = "-- INSERT --"
prevCommand = key
return false
case 'a':
if clear {
prevCharacters = prevCharacters[:0]
prevCommand = key
}
goedit.moveCursor(CURSOR_RIGHT)
goedit.mode = INSERT_MODE
goedit.editormsg.msg = "-- INSERT --"
return false
case 'r':
editorReplaceRune()
prevCommand = key
case 'x':
goedit.moveCursor(CURSOR_RIGHT)
editorDelRune()
prevCommand = key
case 't':
editorFindInRow(FORWARD, -1)
case 'T':
editorFindInRow(BACKWARD, 1)
case 'f':
editorFindInRow(FORWARD, 0)
case 'F':
editorFindInRow(BACKWARD, 0)
case 'O':
if clear {
prevCharacters = prevCharacters[:0]
}
goedit.insertRow(goedit.cursor.y, "")
goedit.mode = INSERT_MODE
goedit.editormsg.msg = "-- INSERT --"
prevCommand = key
return false
case 's':
if clear {
prevCharacters = prevCharacters[:0]
}
goedit.moveCursor(CURSOR_RIGHT)
editorDelRune()
goedit.mode = INSERT_MODE
goedit.editormsg.msg = "-- INSERT --"
prevCommand = key
return false
case '.':
switch prevCommand {
case 'i', 'a', 's', 'o', 'O':
if len(prevCharacters) > 0 {