-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy patheditor_methods.go
More file actions
867 lines (743 loc) · 20.9 KB
/
editor_methods.go
File metadata and controls
867 lines (743 loc) · 20.9 KB
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
package main
import (
//"bufio"
"bytes"
"fmt"
"os"
"strings"
// "time"
"unicode/utf8"
"github.com/slzatz/vimango/vim"
)
func (e *Editor) highlightInfo() { // [2][4]int {
e.highlight = vim.GetVisualRange() //[]line col []line col
}
// highlight matched braces in NORMAL and INSERT modes
func (e *Editor) drawHighlightedBraces() {
if len(e.ss) == 0 || len(e.ss[e.fr]) == 0 {
return
}
var b byte
var back int
//if below handles case when in insert mode and brace is last char
//in a row and cursor is beyond that last char (which is a brace)
if e.fc == len(e.ss[e.fr]) {
b = e.ss[e.fr][e.fc-1]
back = 1
} else {
b = e.ss[e.fr][e.fc]
back = 0
}
if !strings.ContainsAny(string(b), "{}()") {
return
}
pos := vim.GetMatchingPair()
if pos == [2]int{0, 0} {
return
}
y := e.getScreenYFromRowColWW(pos[0]-1, pos[1]) - e.lineOffset
if y >= e.screenlines {
return
}
match := e.ss[pos[0]-1][pos[1]]
x := e.getScreenXFromRowColWW(pos[0]-1, pos[1]) + e.left_margin + e.left_margin_offset + 1
fmt.Printf("\x1b[%d;%dH\x1b[48;5;237m%s", y+e.top_margin, x, string(match))
x = e.getScreenXFromRowColWW(e.fr, e.fc-back) + e.left_margin + e.left_margin_offset + 1
y = e.getScreenYFromRowColWW(e.fr, e.fc-back) + e.top_margin - e.lineOffset // added line offset 12-25-2019
fmt.Printf("\x1b[%d;%dH\x1b[48;5;237m%s\x1b[0m", y, x, string(b))
return
}
func (e *Editor) setLinesMargins() { //also sets top margin
/*
if e.output != nil {
if e.output.is_below {
e.screenlines = e.Screen.textLines - LINKED_NOTE_HEIGHT - 1
e.top_margin = TOP_MARGIN + 1
} else {
e.screenlines = e.Screen.textLines
e.top_margin = TOP_MARGIN + 1
}
} else {
*/
e.screenlines = e.Screen.textLines
e.top_margin = TOP_MARGIN + 1
}
// used by updateNote
func (e *Editor) bufferToString() string {
return strings.Join(e.ss, "\n")
}
func (e *Editor) getScreenXFromRowColWW(r, c int) int {
row := e.ss[r]
row = strings.ReplaceAll(row, "\t", "$$$$")
tabCount := strings.Count(e.ss[r][:c], "\t")
c = c + 3*tabCount
width := e.screencols - e.left_margin_offset
if width >= len(row) {
return c
}
start := 0
end := 0
for {
if width >= len(row[start:]) {
break
}
pos := strings.LastIndex(row[start:start+width], " ")
if pos == -1 {
end = start + width - 1
} else {
end = start + pos
}
if end >= c {
break
}
start = end + 1
}
return c - start
}
func (e *Editor) getScreenYFromRowColWW(r, c int) int {
screenLine := 0
for n := 0; n < r; n++ {
screenLine += e.getLinesInRowWW(n)
}
screenLine = screenLine + e.getLineInRowWW(r, c) - 1
return screenLine
}
func (e *Editor) getLinesInRowWW(r int) int {
row := e.ss[r]
row = strings.ReplaceAll(row, "\t", "$$$$")
width := e.screencols - e.left_margin_offset
if width >= len(row) {
return 1
}
lines := 0
start := 0
end := 0
for {
if width >= len(row[start:]) {
lines++
break
}
pos := strings.LastIndex(row[start:start+width], " ")
if pos == -1 {
end = start + width - 1
} else {
end = start + pos
}
lines++
start = end + 1
}
return lines
}
func (e *Editor) getLineInRowWW(r, c int) int {
row := e.ss[r]
tabCount := strings.Count(row[:c], "\t")
if tabCount > 0 {
row = strings.ReplaceAll(row, "\t", "$$$$")
c += 3 * tabCount
}
width := e.screencols - e.left_margin_offset
if width >= len(row) {
return 1
}
lines := 0
start := 0
end := 0
for {
if width >= len(row[start:]) {
lines++
break
}
pos := strings.LastIndex(row[start:start+width], " ")
if pos == -1 {
end = start + width - 1
} else {
end = start + pos
}
lines++
if end >= c { //+3
break
}
start = end + 1
}
return lines
}
func (e *Editor) drawText() {
var ab strings.Builder
fmt.Fprintf(&ab, "\x1b[?25l\x1b[%d;%dH", e.top_margin, e.left_margin+1)
// \x1b[NC moves cursor forward by n columns
lf_ret := fmt.Sprintf("\r\n\x1b[%dC", e.left_margin)
erase_chars := fmt.Sprintf("\x1b[%dX", e.screencols)
for i := 0; i < e.screenlines; i++ {
//time.Sleep(10 * time.Millisecond) //helps test if redraws are happening by slowing down redraws
ab.WriteString(erase_chars)
ab.WriteString(lf_ret)
}
if e.highlightSyntax { //includes markdown
e.drawCodeRows(&ab)
} else {
e.drawPlainRows(&ab)
}
if e.highlightPositions != nil {
e.drawHighlights(&ab)
}
if e.mode == VISUAL {
e.drawVisual(&ab)
}
fmt.Print(ab.String())
e.drawHighlightedBraces() //has to come after drawing rows
}
func (e *Editor) drawVisual(pab *strings.Builder) {
lf_ret := fmt.Sprintf("\r\n\x1b[%dC", e.left_margin+e.left_margin_offset)
if e.vmode == VISUAL_LINE {
startRow := e.highlight[0][0] - 1 // highlight line starts a 1
endRow := e.highlight[1][0] - 1 //ditto - done differently for visual and v_block
x := e.left_margin + e.left_margin_offset + 1
y := e.getScreenYFromRowColWW(startRow, 0) - e.lineOffset
if y >= 0 {
fmt.Fprintf(pab, "\x1b[%d;%dH\x1b[48;5;237m", y+e.top_margin, x) //244
} else {
fmt.Fprintf(pab, "\x1b[%d;%dH\x1b[48;5;237m", e.top_margin, x)
}
for n := 0; n < (endRow - startRow + 1); n++ { //++n
rowNum := startRow + n
row := e.ss[rowNum]
row = strings.ReplaceAll(row, "\t", " ")
pos := 0
for line := 1; line <= e.getLinesInRowWW(rowNum); line++ { //++line
if y < 0 {
y += 1
continue
}
if y == e.screenlines {
break //out for should be done (theoretically) - 1
}
line_char_count := e.getLineCharCountWW(rowNum, line)
//pab.WriteString(strings.ReplaceAll(e.ss[rowNum][pos:pos+line_char_count], "\t", "$$$$"))
pab.WriteString(row[pos : pos+line_char_count])
pab.WriteString(lf_ret)
y += 1
pos += line_char_count
}
}
}
if e.vmode == VISUAL {
startCol, endcol := e.highlight[0][1], e.highlight[1][1]
// startRow always <= endRow and need to subtract 1 since counting starts at 1 not zero
startRow, endRow := e.highlight[0][0]-1, e.highlight[1][0]-1 //startRow always <= endRow
numrows := endRow - startRow + 1
x := e.getScreenXFromRowColWW(startRow, startCol) + e.left_margin + e.left_margin_offset + 1
y := e.getScreenYFromRowColWW(startRow, startCol) + e.top_margin - e.lineOffset // - 1
pab.WriteString("\x1b[48;5;237m")
for n := 0; n < numrows; n++ {
// i think would check here to see if a row has multiple lines (ie wraps)
if n == 0 {
fmt.Fprintf(pab, "\x1b[%d;%dH", y+n, x)
} else {
fmt.Fprintf(pab, "\x1b[%d;%dH", y+n, 1+e.left_margin+e.left_margin_offset)
}
row := e.ss[startRow+n]
// I do not know why this works!!
row = strings.ReplaceAll(row, "\t", " ")
if len(row) == 0 {
continue
}
if numrows == 1 {
// in VISUAL mode like INSERT mode, the cursor can go beyond end of row
if len(row) == endcol {
pab.WriteString(row[startCol:endcol])
} else {
pab.WriteString(row[startCol : endcol+1])
}
} else if n == 0 {
pab.WriteString(row[startCol:])
} else if n < numrows-1 {
pab.WriteString(row)
} else {
if len(row) < endcol {
pab.WriteString(row)
} else {
pab.WriteString(row[:endcol])
}
}
}
}
if e.vmode == VISUAL_BLOCK {
var left, right int
if e.highlight[1][1] > e.highlight[0][1] {
right, left = e.highlight[1][1], e.highlight[0][1]
} else {
left, right = e.highlight[1][1], e.highlight[0][1]
}
x := e.getScreenXFromRowColWW(e.highlight[0][0]-1, left) + e.left_margin + e.left_margin_offset + 1 //-1
y := e.getScreenYFromRowColWW(e.highlight[0][0]-1, left) + e.top_margin - e.lineOffset
//sess.showOrgMessage("highlight = %v, right = %v, left = %v, x = %v, y = %v", e.highlight, right, left, x, y)
pab.WriteString("\x1b[48;5;237m")
for n := 0; n < (e.highlight[1][0] - e.highlight[0][0] + 1); n++ {
fmt.Fprintf(pab, "\x1b[%d;%dH", y+n, x)
row := e.ss[e.highlight[0][0]+n-1]
rowLen := len(row)
if rowLen == 0 || rowLen < left {
continue
}
if rowLen < right+1 {
pab.WriteString(row[left:rowLen])
} else {
pab.WriteString(row[left : right+1])
}
}
}
pab.WriteString(RESET)
}
func (e *Editor) getLineCharCountWW(r, line int) int {
row := e.ss[r]
row = strings.ReplaceAll(row, "\t", "$$$$")
width := e.screencols - e.left_margin_offset
if width >= len(row) {
return len(row)
}
lines := 0
pos := 0
prev_pos := 0
for {
if width >= len(row[prev_pos:]) {
return len(row[prev_pos:])
}
pos = strings.LastIndex(row[prev_pos:pos+width], " ")
if pos == -1 {
pos = prev_pos + width - 1
} else {
pos = pos + prev_pos
}
lines++
if lines == line {
break
}
prev_pos = pos + 1
}
return pos - prev_pos + 1
}
func (e *Editor) drawPlainRows(pab *strings.Builder) {
note := e.generateWWStringFromBuffer() // need the \t for line num to be correct
nnote := strings.Split(note, "\n")
lf_ret := fmt.Sprintf("\r\n\x1b[%dC", e.left_margin)
fmt.Fprintf(pab, "\x1b[?25l\x1b[%d;%dH", e.top_margin, e.left_margin+1) //+1
var s string
if e.numberLines {
var numCols strings.Builder
// below draws the line number 'rectangle'
// can be drawm to pab or &numCols
fmt.Fprintf(&numCols, "\x1b[2*x\x1b[%d;%d;%d;%d;48;5;235$r\x1b[*x",
e.top_margin,
e.left_margin,
e.top_margin+e.screenlines,
e.left_margin+e.left_margin_offset)
fmt.Fprintf(&numCols, "\x1b[?25l\x1b[%d;%dH", e.top_margin, e.left_margin+1)
s = fmt.Sprintf("\x1b[%dC", e.left_margin_offset) + "%s" + lf_ret
for n := e.firstVisibleRow; n < len(nnote); n++ {
row := nnote[n]
fmt.Fprintf(&numCols, "\x1b[48;5;235m\x1b[38;5;245m%3d \x1b[49m", n+1)
line := strings.Split(row, "\t")
for i := 0; i < len(line); i++ {
fmt.Fprintf(pab, s, line[i])
numCols.WriteString(lf_ret)
}
}
pab.WriteString(numCols.String())
} else {
s = "%s" + lf_ret
for n := e.firstVisibleRow; n < len(nnote); n++ {
row := nnote[n]
line := strings.Split(row, "\t")
for i := 0; i < len(line); i++ {
fmt.Fprintf(pab, s, line[i])
}
}
}
}
func (e *Editor) drawCodeRows(pab *strings.Builder) {
//note := e.generateWWStringFromBuffer()
var lang string
if e.Database.taskFolder(e.id) == "code" {
c := e.Database.taskContext(e.id)
var ok bool
if lang, ok = Languages[c]; !ok {
lang = "markdown"
}
} else {
lang = "markdown"
}
note := e.generateWWStringFromBuffer()
var buf bytes.Buffer
if lang == "markdown" {
_ = Highlight2(&buf, note, lang, "terminal16m", e.Session.markdown_style)
} else {
_ = Highlight(&buf, note, lang, "terminal16m", e.Session.style[e.Session.styleIndex])
}
note = buf.String()
nnote := strings.Split(note, "\n")
lf_ret := fmt.Sprintf("\r\n\x1b[%dC", e.left_margin)
fmt.Fprintf(pab, "\x1b[?25l\x1b[%d;%dH", e.top_margin, e.left_margin+1)
if e.numberLines {
var numCols strings.Builder
// below draws the line number 'rectangle'
// cam be drawm to pab or &numCols
fmt.Fprintf(&numCols, "\x1b[2*x\x1b[%d;%d;%d;%d;48;5;235$r\x1b[*x",
e.top_margin,
e.left_margin,
e.top_margin+e.screenlines,
e.left_margin+e.left_margin_offset)
fmt.Fprintf(&numCols, "\x1b[?25l\x1b[%d;%dH", e.top_margin, e.left_margin+1)
s := fmt.Sprintf("\x1b[%dC", e.left_margin_offset) + "%s" + lf_ret
for n := e.firstVisibleRow; n < len(nnote); n++ {
row := nnote[n]
fmt.Fprintf(&numCols, "\x1b[48;5;235m\x1b[38;5;245m%3d \x1b[49m", n+1)
line := strings.Split(row, "\t")
for i := 0; i < len(line); i++ {
fmt.Fprintf(pab, s, line[i])
numCols.WriteString(lf_ret)
}
}
pab.WriteString(numCols.String())
} else {
s := "%s" + lf_ret
for n := e.firstVisibleRow; n < len(nnote); n++ {
row := nnote[n]
line := strings.Split(row, "\t")
for i := 0; i < len(line); i++ {
fmt.Fprintf(pab, s, line[i])
}
}
}
}
func (e *Editor) highlightMispelledWords() {
if !IsSpellCheckAvailable() {
e.ShowMessage(BR, ShowSpellCheckNotAvailableMessage())
return
}
e.highlightPositions = nil
curPos := vim.GetCursorPosition()
for i, line := range e.ss {
wordPositions := GetWordPositionsSingleLine(line)
for _, wp := range wordPositions {
if CheckSpelling(wp.Word) {
continue
}
e.highlightPositions = append(e.highlightPositions, Position{i, wp.Start, wp.End + 1})
}
}
vim.SetCursorPosition(curPos[0], curPos[1]) //return cursor to where it was
}
func (e *Editor) drawHighlights(pab *strings.Builder) {
if e.highlightPositions == nil {
return
}
if e.isModified() {
e.highlightPositions = nil
return
}
for _, p := range e.highlightPositions {
row := e.ss[p.rowNum]
if p.start < 0 || p.end > len(row) {
continue // skip if out of bounds
}
chars := "\x1b[48;5;31m" + string(row[p.start:p.end]) + "\x1b[0m"
start := utf8.RuneCountInString(row[:p.start])
y := e.getScreenYFromRowColWW(p.rowNum, start) + e.top_margin - e.lineOffset // - 1
x := e.getScreenXFromRowColWW(p.rowNum, start) + e.left_margin + e.left_margin_offset // - 1
if y >= e.top_margin && y <= e.screenlines {
fmt.Fprintf(pab, "\x1b[%d;%dH\x1b[0m", y, x+1) //not sure why the +1
fmt.Fprint(pab, chars)
}
}
}
/*
* simplified version of generateWWStringFromBuffer
* used by editor.showMarkdown and editor.spellCheck in editor_normal
* we know we want the whole buffer not just what is visible
* unlike the situation with syntax highlighting for code and markdown
* we don't have to handle word-wrapped lines in a special way
* had been in use in showMarkdownPreview in editor_normal.go
func (e *Editor) generateWWStringFromBuffer2() string {
numRows := len(e.ss)
if numRows == 0 {
return ""
}
var ab strings.Builder
y := 0
filerow := 0
//width := e.screencols - e.left_margin_offset
width := e.screencols //05042021
for {
if filerow == numRows {
return ab.String()
}
row := e.ss[filerow]
if len(row) == 0 {
ab.WriteString("\n")
filerow++
y++
continue
}
// added 04052022 so urls that word wrap handled correctly`
// question if might cause issue for spellcheck
if strings.Index(row, "](http") != -1 {
ab.WriteString(row)
ab.WriteString("\n")
filerow++
y++
continue
}
start := 0
end := 0
for {
// if remainder of line is less than screen width
if start+width > len(row)-1 {
//ab.WriteString(row[start:])
//ab.WriteString("\n")
fmt.Fprintf(&ab, "%s%s", row[start:], "\n")
y++
filerow++
break
}
pos := strings.LastIndex(row[start:start+width], " ")
if pos == -1 {
end = start + width - 1
} else {
end = start + pos
}
//ab.WriteString(row[start : end+1])
//ab.WriteString("\n")
//fmt.Fprintf(&ab, "%s%s", row[start:end+1], "\n")
fmt.Fprintf(&ab, "%s%s", row[start:end+1], "^^^") //04052022
y++
start = end + 1
}
}
}
*/
/* below exists to create a string that has the proper
* line breaks based on screen width for syntax highlighting
* being done in drawcoderows
* produces a text string that starts at the first line of the
* file (need to deal with comments where start of comment might be scrolled
* and ends on the last visible line. Word-wrapped rows are terminated by \t
* so highlighter deals with them correctly and converted to \n in drawcoderows
* very similar to dbfunc generateWWString except this uses buffer
* and only returns as much file as fits the screen
* and deals with the issue of multi-line comments
*/
func (e *Editor) generateWWStringFromBuffer() string {
numRows := len(e.ss)
if numRows == 0 {
return ""
}
var ab strings.Builder
y := 0
filerow := 0
width := e.screencols - e.left_margin_offset
for {
if filerow == numRows || y == e.screenlines+e.lineOffset {
return ab.String()[:ab.Len()-1] // delete last \n
}
row := strings.ReplaceAll(e.ss[filerow], "\t", " ")
if len(row) == 0 {
ab.WriteString("\n")
filerow++
y++
continue
}
start := 0
end := 0
for {
// if remainder of line is less than screen width
if start+width > len(row)-1 {
//ab.WriteString(row[start:])
//ab.WriteString("\n")
fmt.Fprintf(&ab, "%s%s", row[start:], "\n")
y++
filerow++
break
}
pos := strings.LastIndex(row[start:start+width], " ")
if pos == -1 {
end = start + width - 1
} else {
end = start + pos
}
ab.WriteString(row[start : end+1])
if y == e.screenlines+e.lineOffset-1 {
return ab.String()
}
ab.WriteString("\t")
y++
start = end + 1
}
}
}
func (e *Editor) drawStatusBar() {
var ab strings.Builder
fmt.Fprintf(&ab, "\x1b[%d;%dH", e.screenlines+e.top_margin, e.left_margin+1)
//erase from start of an Editor's status bar to the end of the Editor's status bar
fmt.Fprintf(&ab, "\x1b[%dX", e.screencols)
ab.WriteString("\x1b[7m ") //switches to inverted colors
title := e.title
if len(title) > 30 {
title = title[:30]
}
if e.isModified() {
title += "[+]"
}
status := fmt.Sprintf("%d - %s ...", e.id, title)
if len(status) > e.screencols-1 {
status = status[:e.screencols-1]
}
fmt.Fprintf(&ab, "%-*s", e.screencols, status)
ab.WriteString("\x1b[0m") //switches back to normal formatting
fmt.Print(ab.String())
}
func (e *Editor) drawFrame() {
var ab strings.Builder
ab.WriteString("\x1b(0") // Enter line drawing mode
for j := 1; j < e.screenlines+1; j++ {
fmt.Fprintf(&ab, "\x1b[%d;%dH", e.top_margin-1+j, e.left_margin+e.screencols+1)
// below x = 0x78 vertical line (q = 0x71 is horizontal) 37 = white; 1m = bold (note
// only need one 'm'
ab.WriteString("\x1b[37;1mx")
}
//'T' corner = w or right top corner = k
fmt.Fprintf(&ab, "\x1b[%d;%dH", e.top_margin-1, e.left_margin+e.screencols+1)
if e.left_margin+e.screencols > e.Screen.screenCols-4 {
ab.WriteString("\x1b[37;1mk") //draw corner
} else {
ab.WriteString("\x1b[37;1mw")
}
//exit line drawing mode
ab.WriteString("\x1b(B")
ab.WriteString("\x1b[?25h") //shows the cursor
ab.WriteString("\x1b[0m") //or else subsequent editors are bold
fmt.Print(ab.String())
}
// note that app.returnCursor uses e.cy and e.cx to position the terminal cursor
func (e *Editor) scroll() (offset_changed bool) {
prevOffset := e.lineOffset
if e.fc == 0 && e.fr == 0 {
e.cy, e.cx, e.lineOffset, e.firstVisibleRow = 0, 0, 0, 0
return prevOffset != e.lineOffset
}
e.cx = e.getScreenXFromRowColWW(e.fr, e.fc)
cy := e.getScreenYFromRowColWW(e.fr, e.fc)
//deal with scroll insufficient to include the current line
if cy > e.screenlines+e.lineOffset-1 {
e.lineOffset = cy - e.screenlines + 1 ////
e.adjustFirstVisibleRow() //can also change e.lineOffset
}
if cy < e.lineOffset {
e.lineOffset = cy
e.adjustFirstVisibleRow()
}
if e.lineOffset == 0 {
e.firstVisibleRow = 0
}
e.cy = cy - e.lineOffset
return prevOffset != e.lineOffset
}
// e.lineOffset determines the first
// visible row but we want the whole row
// visible so that can change e.lineOffset
func (e *Editor) adjustFirstVisibleRow() {
if e.lineOffset == 0 {
e.firstVisibleRow = 0
return
}
rowNum := 0
lines := 0
for {
lines += e.getLinesInRowWW(rowNum)
rowNum++
/*
there is no need to adjust line_offset
if it happens that we start
on the first line of the first visible row
*/
if lines == e.lineOffset {
break
}
/*
need to adjust line_offset
so we can start on the first
line of the top row
*/
if lines > e.lineOffset {
e.lineOffset = lines
break
}
}
e.firstVisibleRow = rowNum
}
func (e *Editor) readFileIntoNote(filename string) error {
b, err := os.ReadFile(filename)
if err != nil {
return fmt.Errorf("Error opening file %s: %w", filename, err)
}
e.ss = strings.Split(string(b), "\n")
e.vbuf.SetLines(0, -1, e.ss)
e.fr, e.fc, e.cy, e.cx, e.lineOffset, e.firstVisibleRow = 0, 0, 0, 0, 0, 0
e.drawText()
e.drawStatusBar() // not sure what state of isModified would be so not sure need to draw statubBar
return nil
}
func (e *Editor) drawPreview() {
rows := strings.Split(e.renderedNote, "\n")
fmt.Printf("\x1b[?25l\x1b[%d;%dH", e.top_margin, e.left_margin+1)
lf_ret := fmt.Sprintf("\r\n\x1b[%dC", e.left_margin)
// erase specific editors 'window'
erase_chars := fmt.Sprintf("\x1b[%dX", e.screencols)
for i := 0; i < e.screenlines; i++ {
fmt.Printf("%s%s", erase_chars, lf_ret)
}
fmt.Printf("\x1b[%d;%dH", e.top_margin, e.left_margin+1)
fr := e.previewLineOffset - 1
y := 0
for {
fr++
if fr > len(rows)-1 || y > e.screenlines-1 {
break
}
fmt.Printf("%s%s", rows[fr], lf_ret)
y++
}
}
func (e *Editor) drawOverlay() {
fmt.Print("\x1b_Ga=d\x1b\\") //delete any images
//rows := strings.Split(s, "\n")
fmt.Printf("\x1b[?25l\x1b[%d;%dH", e.top_margin, e.left_margin+1)
lf_ret := fmt.Sprintf("\r\n\x1b[%dC", e.left_margin)
// erase specific editors 'window'
erase_chars := fmt.Sprintf("\x1b[%dX", e.screencols)
for i := 0; i < e.screenlines; i++ {
fmt.Printf("%s%s", erase_chars, lf_ret)
}
fmt.Printf("\x1b[%d;%dH", e.top_margin, e.left_margin+1)
fr := e.previewLineOffset - 1
y := 0
rows := e.overlay
for {
fr++
if fr > len(rows)-1 || y > e.screenlines-1 {
break
}
fmt.Printf("%s%s", rows[fr], lf_ret)
y++
}
}
// this func is reason that we are writing notes to file
// allows easy testing if a file is modified with BufferOption
func (e *Editor) isModified() bool {
//return e.vbuf.IsModified()
tick := e.vbuf.GetLastChangedTick()
if tick > e.saveTick {
//e.bufferTick = tick
return true
}
return false
}