-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathdisplay.c
More file actions
1067 lines (956 loc) · 25.8 KB
/
display.c
File metadata and controls
1067 lines (956 loc) · 25.8 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
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
#include "display.h"
#include "emsys.h"
#include "terminal.h"
#include "unicode.h"
#include "unused.h"
#include "region.h"
#include "buffer.h"
#include "util.h"
#include "wcwidth.h"
#include <errno.h>
#include <stdarg.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>
#include <unistd.h>
#include <limits.h>
#ifdef __sun
#include <termios.h>
#endif
extern struct editorConfig E;
extern void updateRow(erow *row);
const int minibuffer_height = 1;
const int statusbar_height = 1;
/* Append buffer implementation */
void abAppend(struct abuf *ab, const char *s, int len) {
if (ab->len + len > ab->capacity) {
int new_capacity = ab->capacity == 0 ? 1024 : ab->capacity * 2;
while (new_capacity < ab->len + len) {
if (new_capacity > INT_MAX / 2) {
die("buffer size overflow");
}
new_capacity *= 2;
}
ab->b = xrealloc(ab->b, new_capacity);
ab->capacity = new_capacity;
}
memcpy(&ab->b[ab->len], s, len);
ab->len += len;
}
void abFree(struct abuf *ab) {
free(ab->b);
}
/* Check if a render position is within the marked region */
static int isRenderPosInRegion(struct editorBuffer *buf, int row,
int render_pos) {
if (markInvalidSilent())
return 0;
erow *erow_ptr = &buf->row[row];
if (!erow_ptr)
return 0;
if (buf->rectangle_mode) {
int top_row = buf->cy < buf->marky ? buf->cy : buf->marky;
int bottom_row = buf->cy > buf->marky ? buf->cy : buf->marky;
int left_col = buf->cx < buf->markx ? buf->cx : buf->markx;
int right_col = buf->cx > buf->markx ? buf->cx : buf->markx;
if (row < top_row || row > bottom_row)
return 0;
int left_render = charsToDisplayColumn(erow_ptr, left_col);
int right_render = charsToDisplayColumn(erow_ptr, right_col);
return (render_pos >= left_render && render_pos < right_render);
} else {
int start_row = buf->cy < buf->marky ? buf->cy : buf->marky;
int end_row = buf->cy > buf->marky ? buf->cy : buf->marky;
int start_col =
(buf->cy < buf->marky ||
(buf->cy == buf->marky && buf->cx <= buf->markx)) ?
buf->cx :
buf->markx;
int end_col =
(buf->cy > buf->marky ||
(buf->cy == buf->marky && buf->cx >= buf->markx)) ?
buf->cx :
buf->markx;
if (row < start_row || row > end_row)
return 0;
if (row == start_row && row == end_row) {
int start_render =
charsToDisplayColumn(erow_ptr, start_col);
int end_render =
charsToDisplayColumn(erow_ptr, end_col);
return (render_pos >= start_render &&
render_pos < end_render);
}
if (row == start_row) {
int start_render =
charsToDisplayColumn(erow_ptr, start_col);
return (render_pos >= start_render);
}
if (row == end_row) {
int end_render =
charsToDisplayColumn(erow_ptr, end_col);
return (render_pos < end_render);
}
return 1; /* Entire middle row is in region */
}
}
/* Check if a render position is at the current search match */
static int isRenderPosCurrentSearchMatch(struct editorBuffer *buf, int row,
int render_pos) {
if (!buf->query || !buf->query[0] || !buf->match)
return 0;
if (row != buf->cy)
return 0;
erow *erow_ptr = &buf->row[row];
if (!erow_ptr)
return 0;
int match_len = strlen((char *)buf->query);
int start_render = charsToDisplayColumn(erow_ptr, buf->cx);
int end_render = charsToDisplayColumn(erow_ptr, buf->cx + match_len);
return (render_pos >= start_render && render_pos < end_render);
}
/* Calculate number of rows to scroll for smooth scrolling */
int calculateRowsToScroll(struct editorBuffer *buf, struct editorWindow *win,
int direction) {
int rendered_lines = 0;
int rows_to_scroll = 0;
int start_row = (direction > 0) ? win->rowoff : win->rowoff - 1;
while (rendered_lines < win->height) {
if (start_row < 0 || start_row >= buf->numrows)
break;
erow *row = &buf->row[start_row];
int line_height =
buf->truncate_lines ?
1 :
((calculateLineWidth(row) / E.screencols) + 1);
if (rendered_lines + line_height > win->height && direction < 0)
break;
rendered_lines += line_height;
rows_to_scroll++;
start_row += direction;
}
return rows_to_scroll;
}
/* Render a line with highlighting support */
static void renderLineWithHighlighting(erow *row, struct abuf *ab,
int start_col, int end_col,
struct editorBuffer *buf, int filerow) {
int render_x = 0;
int char_idx = 0;
int current_highlight = 0;
/* Skip to start column */
while (char_idx < row->size && render_x < start_col) {
if (row->chars[char_idx] < 0x80 &&
!ISCTRL(row->chars[char_idx])) {
render_x += 1;
char_idx++;
} else {
render_x = nextScreenX(row->chars, &char_idx, render_x);
char_idx++;
}
}
/* Render visible portion */
while (char_idx < row->size && render_x < end_col) {
uint8_t c = row->chars[char_idx];
int in_region = isRenderPosInRegion(buf, filerow, render_x);
int is_current_match =
isRenderPosCurrentSearchMatch(buf, filerow, render_x);
int new_highlight = (in_region || is_current_match) ? 1 : 0;
if (new_highlight != current_highlight) {
if (current_highlight > 0) {
abAppend(ab, "\x1b[0m", 4);
}
if (new_highlight == 1) {
abAppend(ab, "\x1b[7m", 4); /* Reverse video */
}
current_highlight = new_highlight;
}
if (c == '\t') {
int next_tab_stop = (render_x + EMSYS_TAB_STOP) /
EMSYS_TAB_STOP * EMSYS_TAB_STOP;
while (render_x < next_tab_stop && render_x < end_col) {
if (render_x >= start_col) {
abAppend(ab, " ", 1);
}
render_x++;
}
} else if (ISCTRL(c)) {
if (render_x >= start_col) {
abAppend(ab, "^", 1);
if (c == 0x7f) {
abAppend(ab, "?", 1);
} else {
char sym = c | 0x40;
abAppend(ab, &sym, 1);
}
}
render_x += 2;
} else {
int width = charInStringWidth(row->chars, char_idx);
if (render_x >= start_col) {
int bytes = utf8_nBytes(c);
abAppend(ab, (char *)&row->chars[char_idx],
bytes);
}
render_x += width;
}
char_idx += utf8_nBytes(row->chars[char_idx]);
}
if (current_highlight > 0) {
abAppend(ab, "\x1b[0m", 4);
}
}
/* Window management functions */
int windowFocusedIdx(void) {
for (int i = 0; i < E.nwindows; i++) {
if (E.windows[i]->focused) {
return i;
}
}
/* You're in trouble m80 */
return 0;
}
int findBufferWindow(struct editorBuffer *buf) {
for (int i = 0; i < E.nwindows; i++) {
if (E.windows[i]->buf == buf) {
return i;
}
}
return -1;
}
void synchronizeBufferCursor(struct editorBuffer *buf,
struct editorWindow *win) {
// Ensure the cursor is within the buffer's bounds
if (win->cy >= buf->numrows) {
win->cy = buf->numrows > 0 ? buf->numrows - 1 : 0;
}
if (win->cy < buf->numrows && win->cx > buf->row[win->cy].size) {
win->cx = buf->row[win->cy].size;
}
// Update the buffer's cursor position
buf->cx = win->cx;
buf->cy = win->cy;
}
void editorSwitchWindow(void) {
if (E.nwindows == 1) {
editorSetStatusMessage("No other windows to select");
return;
}
int currentIdx = windowFocusedIdx();
struct editorWindow *currentWindow = E.windows[currentIdx];
struct editorBuffer *currentBuffer = currentWindow->buf;
// Store the current buffer's cursor position in the current window
currentWindow->cx = currentBuffer->cx;
currentWindow->cy = currentBuffer->cy;
// Switch to the next window
currentWindow->focused = 0;
int nextIdx = (currentIdx + 1) % E.nwindows;
struct editorWindow *nextWindow = E.windows[nextIdx];
nextWindow->focused = 1;
// Update the focused buffer
E.buf = nextWindow->buf;
// Set the buffer's cursor position from the new window
E.buf->cx = nextWindow->cx;
E.buf->cy = nextWindow->cy;
// Synchronize the buffer's cursor with the new window's cursor
synchronizeBufferCursor(E.buf, nextWindow);
}
/* Display functions */
void setScxScy(struct editorWindow *win) {
struct editorBuffer *buf = win->buf;
erow *row = (buf->cy >= buf->numrows) ? NULL : &buf->row[buf->cy];
win->scy = 0;
win->scx = 0;
if (!buf->truncate_lines) {
if (buf->cy >= buf->numrows) {
// For virtual line, calculate position as if there's a line at buf->numrows
if (buf->numrows > 0) {
int virtual_screen_line = getScreenLineForRow(
buf, buf->numrows - 1);
// Add one line for the virtual line position
virtual_screen_line +=
((calculateLineWidth(
&buf->row[buf->numrows - 1]) /
E.screencols) +
1);
int rowoff_screen_line =
getScreenLineForRow(buf, win->rowoff);
win->scy = virtual_screen_line -
rowoff_screen_line;
} else {
// Empty buffer case - virtual line is at position 0
win->scy = 0 - win->rowoff;
}
} else {
int cursor_screen_line =
getScreenLineForRow(buf, buf->cy);
int rowoff_screen_line =
getScreenLineForRow(buf, win->rowoff);
win->scy = cursor_screen_line - rowoff_screen_line;
}
} else {
win->scy = buf->cy - win->rowoff;
}
if (buf->cy >= buf->numrows) {
return;
}
int total_width = charsToDisplayColumn(row, buf->cx);
if (buf->truncate_lines) {
win->scx = total_width - win->coloff;
} else {
int render_pos = charsToDisplayColumn(row, buf->cx);
win->scy += render_pos / E.screencols;
win->scx = render_pos % E.screencols;
}
if (win->scy < 0)
win->scy = 0;
if (win->scy >= win->height)
win->scy = win->height - 1;
if (win->scx < 0)
win->scx = 0;
if (win->scx >= E.screencols)
win->scx = E.screencols - 1;
}
void scroll(void) {
struct editorWindow *win = E.windows[windowFocusedIdx()];
struct editorBuffer *buf = win->buf;
if (buf->cy + 1 > buf->numrows) {
buf->cy = buf->numrows;
buf->cx = 0;
} else if (buf->cx > buf->row[buf->cy].size) {
buf->cx = buf->row[buf->cy].size;
}
if (!buf->truncate_lines) {
if (buf->cy < win->rowoff) {
win->rowoff = buf->cy;
} else {
int cursor_screen_row = 0;
for (int i = win->rowoff;
i < buf->cy && i < buf->numrows; i++) {
int line_height =
(calculateLineWidth(&buf->row[i]) /
E.screencols) +
1;
cursor_screen_row += line_height;
}
if (buf->cy < buf->numrows) {
erow *row = &buf->row[buf->cy];
int cursor_x = 0;
for (int j = 0; j < buf->cx;) {
int char_width;
if (j < row->size &&
row->chars[j] == '\t') {
char_width = EMSYS_TAB_STOP -
(cursor_x %
EMSYS_TAB_STOP);
} else {
char_width = charInStringWidth(
row->chars, j);
}
cursor_x += char_width;
j += utf8_nBytes(row->chars[j]);
}
cursor_screen_row += cursor_x / E.screencols;
}
if (cursor_screen_row >= win->height) {
int visible_rows = 0;
if (buf->cy == buf->numrows) {
visible_rows = 1;
}
for (int i = buf->cy; i >= 0; i--) {
if (i < buf->numrows) {
int line_height =
(calculateLineWidth(
&buf->row[i]) /
E.screencols) +
1;
if (visible_rows + line_height >
win->height) {
win->rowoff = i + 1;
break;
}
visible_rows += line_height;
}
if (i == 0) {
win->rowoff = 0;
break;
}
}
}
}
} else {
if (buf->cy < win->rowoff) {
win->rowoff = buf->cy;
} else if (buf->cy >= win->rowoff + win->height) {
win->rowoff = buf->cy - win->height + 1;
}
}
if (buf->truncate_lines) {
int rx = 0;
if (buf->cy < buf->numrows) {
erow *row = &buf->row[buf->cy];
for (int j = 0; j < buf->cx;) {
if (j < row->size) {
if (row->chars[j] == '\t') {
rx += EMSYS_TAB_STOP -
(rx % EMSYS_TAB_STOP);
} else {
int char_width =
charInStringWidth(
row->chars, j);
rx += char_width;
}
j += utf8_nBytes(row->chars[j]);
} else {
break;
}
}
}
if (rx < win->coloff) {
win->coloff = rx;
} else if (rx >= win->coloff + E.screencols) {
win->coloff = rx - E.screencols + 1;
}
} else {
win->coloff = 0;
}
setScxScy(win);
}
void drawRows(struct editorWindow *win, struct abuf *ab, int screenrows,
int screencols) {
struct editorBuffer *buf = win->buf;
int y;
int filerow = win->rowoff;
for (y = 0; y < screenrows; y++) {
if (filerow >= buf->numrows) {
abAppend(ab, CSI "34m~" CSI "0m", 10);
} else {
erow *row = &buf->row[filerow];
if (!row->render_valid) {
updateRow(row);
}
if (buf->truncate_lines) {
// Truncated mode with visual marking
renderLineWithHighlighting(
row, ab, win->coloff,
win->coloff + screencols, buf, filerow);
filerow++;
} else {
// Wrapped mode with visual marking support
int render_x = 0;
int char_idx = 0;
int current_highlight = 0;
int line_start_render_x = 0;
while (char_idx < row->size && y < screenrows) {
// Track start of current screen line
line_start_render_x = render_x;
// Render one screen line worth of content
while (char_idx < row->size &&
render_x - line_start_render_x <
screencols) {
uint8_t c =
row->chars[char_idx];
int in_region =
isRenderPosInRegion(
buf, filerow,
render_x);
int is_current_match =
isRenderPosCurrentSearchMatch(
buf, filerow,
render_x);
int new_highlight =
(in_region ||
is_current_match) ?
1 :
0;
if (new_highlight !=
current_highlight) {
if (current_highlight >
0) {
abAppend(
ab,
"\x1b[0m",
4);
}
if (new_highlight ==
1) {
abAppend(
ab,
"\x1b[7m",
4); /* Reverse video */
}
current_highlight =
new_highlight;
}
if (c == '\t') {
int next_tab_stop =
(render_x +
EMSYS_TAB_STOP) /
EMSYS_TAB_STOP *
EMSYS_TAB_STOP;
int tab_end =
next_tab_stop;
if (tab_end -
line_start_render_x >
screencols) {
tab_end =
line_start_render_x +
screencols;
}
while (render_x <
tab_end) {
// Check highlighting for each space in tab
int space_in_region = isRenderPosInRegion(
buf,
filerow,
render_x);
int space_is_match = isRenderPosCurrentSearchMatch(
buf,
filerow,
render_x);
int space_highlight =
(space_in_region ||
space_is_match) ?
1 :
0;
if (space_highlight !=
current_highlight) {
if (current_highlight >
0) {
abAppend(
ab,
"\x1b[0m",
4);
}
if (space_highlight ==
1) {
abAppend(
ab,
"\x1b[7m",
4);
}
current_highlight =
space_highlight;
}
abAppend(ab,
" ",
1);
render_x++;
}
} else if (ISCTRL(c)) {
abAppend(ab, "^", 1);
if (c == 0x7f) {
abAppend(ab,
"?",
1);
} else {
char sym = c |
0x40;
abAppend(ab,
&sym,
1);
}
render_x += 2;
} else {
int width = charInStringWidth(
row->chars,
char_idx);
int bytes =
utf8_nBytes(c);
abAppend(
ab,
(char *)&row->chars
[char_idx],
bytes);
render_x += width;
}
char_idx += utf8_nBytes(
row->chars[char_idx]);
}
// Fill rest of line with highlighted spaces if in region
while (render_x - line_start_render_x <
screencols) {
int space_in_region =
isRenderPosInRegion(
buf, filerow,
render_x);
int space_is_match =
isRenderPosCurrentSearchMatch(
buf, filerow,
render_x);
int space_highlight =
(space_in_region ||
space_is_match) ?
1 :
0;
if (space_highlight !=
current_highlight) {
if (current_highlight >
0) {
abAppend(
ab,
"\x1b[0m",
4);
}
if (space_highlight ==
1) {
abAppend(
ab,
"\x1b[7m",
4);
}
current_highlight =
space_highlight;
}
abAppend(ab, " ", 1);
render_x++;
}
// Reset highlighting at end of screen line
if (current_highlight > 0) {
abAppend(ab, "\x1b[0m", 4);
current_highlight = 0;
}
// Move to next screen line if there's more content
if (char_idx < row->size &&
y < screenrows - 1) {
abAppend(ab, "\r\n", 2);
y++;
}
}
filerow++;
}
}
abAppend(ab, "\x1b[K", 3);
if (y < screenrows - 1) {
abAppend(ab, "\r\n", 2);
}
}
}
void drawStatusBar(struct editorWindow *win, struct abuf *ab, int line) {
/* XXX: It's actually possible for the status bar to end up
* outside where it should be, so set it explicitly. */
char buf[32];
snprintf(buf, sizeof(buf), CSI "%d;%dH", line, 1);
abAppend(ab, buf, strlen(buf));
struct editorBuffer *bufr = win->buf;
abAppend(ab, "\x1b[7m", 4);
char status[80];
int len = 0;
if (win->focused) {
len = snprintf(status, sizeof(status),
"-- %.20s %c%c%c %2d:%2d --",
bufr->filename ? bufr->filename : "*scratch*",
bufr->dirty ? '*' : '-', bufr->dirty ? '*' : '-',
bufr->read_only ? '%' : ' ', bufr->cy + 1,
bufr->cx);
} else {
len = snprintf(status, sizeof(status),
" %.20s %c%c%c %2d:%2d ",
bufr->filename ? bufr->filename : "*scratch*",
bufr->dirty ? '*' : '-', bufr->dirty ? '*' : '-',
bufr->read_only ? '%' : ' ', win->cy + 1,
win->cx);
}
#ifdef EMSYS_DEBUG_UNDO
#ifdef EMSYS_DEBUG_REDO
#define DEBUG_UNDO bufr->redo
#else
#define DEBUG_UNDO bufr->undo
#endif
if (DEBUG_UNDO != NULL) {
len = 0;
for (len = 0; len < DEBUG_UNDO->datalen; len++) {
status[len] = DEBUG_UNDO->data[len];
if (DEBUG_UNDO->data[len] == '\n')
status[len] = '#';
}
status[len++] = '"';
len += snprintf(&status[len], sizeof(status) - len,
"sx %d sy %d ex %d ey %d cx %d cy %d",
DEBUG_UNDO->startx, DEBUG_UNDO->starty,
DEBUG_UNDO->endx, DEBUG_UNDO->endy, bufr->cx,
bufr->cy);
}
#endif
#ifdef EMSYS_DEBUG_MACROS
/* This can get quite wide, you may want to boost the size of status */
for (int i = 0; i < E.macro.nkeys; i++) {
len += snprintf(&status[len], sizeof(status) - len, "%d: %d ",
i, E.macro.keys[i]);
}
#endif
char perc[8] = " xxx --";
if (bufr->numrows == 0) {
perc[1] = 'E';
perc[2] = 'm';
perc[3] = 'p';
} else if (bufr->end) {
if (win->rowoff == 0) {
perc[1] = 'A';
perc[2] = 'l';
perc[3] = 'l';
} else {
perc[1] = 'B';
perc[2] = 'o';
perc[3] = 't';
}
} else if (win->rowoff == 0) {
perc[1] = 'T';
perc[2] = 'o';
perc[3] = 'p';
} else {
snprintf(perc, sizeof(perc), " %2d%% --",
(win->rowoff * 100) / bufr->numrows);
}
char fill[2] = "-";
if (!win->focused) {
perc[5] = ' ';
perc[6] = ' ';
fill[0] = ' ';
}
if (len > E.screencols)
len = E.screencols;
abAppend(ab, status, len);
while (len < E.screencols) {
if (E.screencols - len == 7) {
abAppend(ab, perc, 7);
break;
} else {
abAppend(ab, fill, 1);
len++;
}
}
abAppend(ab, "\x1b[m" CRLF, 5);
}
void drawMinibuffer(struct abuf *ab) {
abAppend(ab, "\x1b[K", 3);
// Show prefix first if active
if (E.prefix_display[0]) {
abAppend(ab, E.prefix_display, strlen(E.prefix_display));
}
// Then show message
int msglen = strlen(E.statusmsg);
if (msglen > E.screencols)
msglen = E.screencols;
if (msglen && time(NULL) - E.statusmsg_time < 5) {
if (E.buf->query && !E.buf->match) {
abAppend(ab, "\x1b[91m", 5);
}
abAppend(ab, E.statusmsg, msglen);
abAppend(ab, "\x1b[0m", 4);
}
}
void refreshScreen(void) {
struct abuf ab = ABUF_INIT;
abAppend(&ab, "\x1b[?25l", 6); // Hide cursor
abAppend(&ab, "\x1b[H", 3); // Move cursor to top-left corner
int focusedIdx = windowFocusedIdx();
int cumulative_height = 0;
int total_height = E.screenrows - minibuffer_height -
(statusbar_height * E.nwindows);
/* skip if heights already set */
int heights_set = 1;
for (int i = 0; i < E.nwindows; i++) {
if (E.windows[i]->height <= 0) {
heights_set = 0;
break;
}
}
if (!heights_set) {
int window_height = total_height / E.nwindows;
int remaining_height = total_height % E.nwindows;
for (int i = 0; i < E.nwindows; i++) {
struct editorWindow *win = E.windows[i];
win->height = window_height;
if (i == E.nwindows - 1)
win->height += remaining_height;
}
}
for (int i = 0; i < E.nwindows; i++) {
struct editorWindow *win = E.windows[i];
if (win->focused)
scroll();
drawRows(win, &ab, win->height, E.screencols);
cumulative_height += win->height + statusbar_height;
drawStatusBar(win, &ab, cumulative_height);
}
drawMinibuffer(&ab);
// Clear any remaining lines below content
abAppend(&ab, "\x1b[J", 3);
// Position the cursor for the focused window
struct editorWindow *focusedWin = E.windows[focusedIdx];
char buf[32];
int cursor_y = focusedWin->scy + 1; // 1-based index
for (int i = 0; i < focusedIdx; i++) {
cursor_y += E.windows[i]->height + statusbar_height;
}
// Ensure cursor doesn't go beyond the window's bottom
if (cursor_y > cumulative_height) {
struct editorBuffer *buf = focusedWin->buf;
if (buf->cy >= buf->numrows) {
cursor_y = cumulative_height;
} else {
cursor_y = cumulative_height - statusbar_height;
}
}
snprintf(buf, sizeof(buf), "\x1b[%d;%dH", cursor_y,
focusedWin->scx + 1);
abAppend(&ab, buf, strlen(buf));
abAppend(&ab, "\x1b[?25h", 6); // Show cursor
write(STDOUT_FILENO, ab.b, ab.len);
abFree(&ab);
}
void cursorBottomLine(int curs) {
char cbuf[32];
snprintf(cbuf, sizeof(cbuf), CSI "%d;%dH", E.screenrows, curs);
write(STDOUT_FILENO, cbuf, strlen(cbuf));
}
void cursorBottomLineLong(long curs) {
char cbuf[32];
/* Calculate actual minibuffer row position */
int minibuf_row = 0;
for (int i = 0; i < E.nwindows; i++) {
minibuf_row += E.windows[i]->height + statusbar_height;
}
minibuf_row++; /* minibuffer is after all windows/status bars */
snprintf(cbuf, sizeof(cbuf), CSI "%d;%ldH", minibuf_row, curs);
write(STDOUT_FILENO, cbuf, strlen(cbuf));
}
void editorSetStatusMessage(const char *fmt, ...) {
va_list ap;
va_start(ap, fmt);
vsnprintf(E.statusmsg, sizeof(E.statusmsg), fmt, ap);
va_end(ap);
E.statusmsg_time = time(NULL);
}
void editorResizeScreen(int UNUSED(sig)) {
if (getWindowSize(&E.screenrows, &E.screencols) == -1)
die("getWindowSize");
refreshScreen();
}
void editorCreateWindow(void) {
E.windows = xrealloc(E.windows,
sizeof(struct editorWindow *) * (++E.nwindows));
E.windows[E.nwindows - 1] = xcalloc(1, sizeof(struct editorWindow));
E.windows[E.nwindows - 1]->focused = 0;
E.windows[E.nwindows - 1]->buf = E.buf;
E.windows[E.nwindows - 1]->cx = E.buf->cx;
E.windows[E.nwindows - 1]->cy = E.buf->cy;
E.windows[E.nwindows - 1]->rowoff = 0;
E.windows[E.nwindows - 1]->coloff = 0;
// Force all windows to recalculate heights
for (int i = 0; i < E.nwindows; i++) {
E.windows[i]->height = 0;
}
}
void editorDestroyWindow(int window_idx) {
if (E.nwindows == 1) {
editorSetStatusMessage("Can't kill last window");
return;
}
int focused_idx = windowFocusedIdx();
/* switch focus before destroying current window */
if (window_idx == focused_idx) {
editorSwitchWindow();
}
free(E.windows[window_idx]);
struct editorWindow **windows =
xmalloc(sizeof(struct editorWindow *) * (--E.nwindows));
int j = 0;
for (int i = 0; i < E.nwindows + 1; i++) {
if (i != window_idx) {
windows[j] = E.windows[i];
j++;
}
}
free(E.windows);
E.windows = windows;
/* reset heights */
for (int i = 0; i < E.nwindows; i++) {
E.windows[i]->height = 0;
}
}
void editorDestroyOtherWindows(void) {
if (E.nwindows == 1) {
editorSetStatusMessage("No other windows to delete");
return;
}
int idx = windowFocusedIdx();
struct editorWindow **windows = xmalloc(sizeof(struct editorWindow *));
for (int i = 0; i < E.nwindows; i++) {
if (i != idx) {
free(E.windows[i]);
}
}
windows[0] = E.windows[idx];
windows[0]->focused = 1;
E.buf = windows[0]->buf;
E.nwindows = 1;
free(E.windows);
E.windows = windows;
refreshScreen();
}
void editorWhatCursor(void) {
/* Calculate rendered column position (accounting for tabs and Unicode) */
int rx = 0;