forked from martanne/vis
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathwindow.c
825 lines (742 loc) · 23.4 KB
/
window.c
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
/*
* Copyright (c) 2014 Marc André Tanner <mat at brain-dump.org>
*
* Permission to use, copy, modify, and/or distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#define _XOPEN_SOURCE
#include <string.h>
#include <stdlib.h>
#include <wchar.h>
#include <ctype.h>
#include <errno.h>
#include <regex.h>
#include <curses.h>
#include "window.h"
#include "syntax.h"
#include "text.h"
#include "text-motions.h"
#include "util.h"
typedef struct { /* used to calculate the display width of a character */
char c[7]; /* utf8 encoded bytes */
size_t len; /* number of bytes of the multibyte sequence */
wchar_t wchar; /* equivalent converted wide character, needed for wcwidth(3) */
} Char;
typedef struct {
unsigned char len; /* number of bytes the character displayed in this cell uses, for
character which use more than 1 column to display, their lenght
is stored in the leftmost cell wheras all following cells
occupied by the same character have a length of 0. */
char data; /* first byte of the utf8 sequence, used for white space handling */
} Cell;
typedef struct Line Line;
struct Line { /* a line on the screen, *not* in the file */
Line *prev, *next; /* pointer to neighbouring screen lines */
size_t len; /* line length in terms of bytes */
size_t lineno; /* line number from start of file */
int width; /* zero based position of last used column cell */
Cell cells[]; /* win->width cells storing information about the displayed characters */
};
typedef struct { /* cursor position */
Filepos pos; /* in bytes from the start of the file */
int row, col; /* in terms of zero based screen coordinates */
Line *line; /* screen line on which cursor currently resides */
} Cursor;
struct Win { /* window showing part of a file */
Text *text; /* underlying text management */
WINDOW *win; /* curses window for the text area */
int width, height; /* window text area size */
Filepos start, end; /* currently displayed area [start, end] in bytes from the start of the file */
Line *lines; /* win->height number of lines representing window content */
Line *topline; /* top of the window, first line currently shown */
Line *lastline; /* last currently used line, always <= bottomline */
Line *bottomline; /* bottom of screen, might be unused if lastline < bottomline */
Filerange sel; /* selected text range in bytes from start of file */
Cursor cursor; /* current window cursor position */
void (*cursor_moved)(Win*, void *); /* registered callback, fires whenever the cursor moved */
void *cursor_moved_data; /* user supplied data, passed as second argument to the above callback */
Line *line; /* used while drawing window content, line where next char will be drawn */
int col; /* used while drawing window content, column where next char will be drawn */
Syntax *syntax; /* syntax highlighting definitions for this window or NULL */
int tabwidth; /* how many spaces should be used to display a tab character */
};
static void window_clear(Win *win);
static bool window_addch(Win *win, Char *c);
static size_t window_cursor_update(Win *win);
/* set/move current cursor position to a given (line, column) pair */
static size_t window_cursor_set(Win *win, Line *line, int col);
/* move visible viewport n-lines up/down, redraws the window but does not change
* cursor position which becomes invalid and should be corrected by either:
*
* - window_cursor_to
* - window_cursor_set
*
* the return value indicates wether the visible area changed.
*/
static bool window_viewport_up(Win *win, int n);
static bool window_viewport_down(Win *win, int n);
void window_tabwidth_set(Win *win, int tabwidth) {
win->tabwidth = tabwidth;
window_draw(win);
}
void window_selection_clear(Win *win) {
win->sel = text_range_empty();
window_draw(win);
window_cursor_update(win);
curs_set(1);
}
/* reset internal window data structures (cell matrix, line offsets etc.) */
static void window_clear(Win *win) {
size_t line_size = sizeof(Line) + win->width*sizeof(Cell);
win->topline = win->lines;
win->topline->lineno = text_lineno_by_pos(win->text, win->start);
win->lastline = win->topline;
Line *prev = NULL;
for (int i = 0; i < win->height; i++) {
Line *line = (Line*)(((char*)win->lines) + i*line_size);
line->len = 0;
line->width = 0;
line->prev = prev;
if (prev)
prev->next = line;
prev = line;
}
win->bottomline = prev ? prev : win->topline;
win->line = win->topline;
win->col = 0;
}
Filerange window_selection_get(Win *win) {
Filerange sel = win->sel;
if (sel.start > sel.end) {
size_t tmp = sel.start;
sel.start = sel.end;
sel.end = tmp;
}
if (!text_range_valid(&sel))
return text_range_empty();
sel.end = text_char_next(win->text, sel.end);
return sel;
}
void window_selection_set(Win *win, Filerange *sel) {
win->sel = *sel;
window_draw(win);
}
Filerange window_viewport_get(Win *win) {
return (Filerange){ .start = win->start, .end = win->end };
}
/* try to add another character to the window, return whether there was space left */
static bool window_addch(Win *win, Char *c) {
if (!win->line)
return false;
Cell empty = {};
int width;
size_t lineno = win->line->lineno;
switch (c->wchar) {
case '\t':
width = win->tabwidth - (win->col % win->tabwidth);
for (int w = 0; w < width; w++) {
if (win->col + 1 > win->width) {
win->line = win->line->next;
win->col = 0;
if (!win->line)
return false;
win->line->lineno = lineno;
}
if (w == 0) {
/* first cell of a tab has a length of 1 */
win->line->cells[win->col].len = c->len;
win->line->len += c->len;
} else {
/* all remaining ones have a lenght of zero */
win->line->cells[win->col].len = 0;
}
/* but all are marked as part of a tabstop */
win->line->cells[win->col].data = '\t';
win->col++;
win->line->width++;
waddch(win->win, ' ');
}
return true;
case '\n':
width = 1;
if (win->col + width > win->width) {
win->line = win->line->next;
win->col = 0;
if (!win->line)
return false;
win->line->lineno = lineno + 1;
}
win->line->cells[win->col].len = c->len;
win->line->len += c->len;
win->line->cells[win->col].data = '\n';
for (int i = win->col + 1; i < win->width; i++)
win->line->cells[i] = empty;
if (win->line == win->bottomline) {
/* XXX: curses bug? the wclrtoeol(win->win); implied by waddch(win->win, '\n')
* doesn't seem to work on the last line!?
*
* Thus explicitly clear the remaining of the line.
*/
for (int i = win->col; i < win->width; i++)
waddch(win->win, ' ');
} else if (win->line->width == 0) {
/* add a single space in an otherwise empty line, makes selection cohorent */
waddch(win->win, ' ');
}
waddch(win->win, '\n');
win->line = win->line->next;
if (win->line)
win->line->lineno = lineno + 1;
win->col = 0;
return true;
default:
if (c->wchar < 128 && !isprint(c->wchar)) {
/* non-printable ascii char, represent it as ^(char + 64) */
Char s = { .c = "^_", .len = 1 };
s.c[1] = c->c[0] + 64;
*c = s;
width = 2;
} else {
if ((width = wcwidth(c->wchar)) == -1) {
/* this should never happen */
width = 1;
}
}
if (win->col + width > win->width) {
for (int i = win->col; i < win->width; i++)
win->line->cells[i] = empty;
win->line = win->line->next;
win->col = 0;
}
if (win->line) {
win->line->width += width;
win->line->len += c->len;
win->line->lineno = lineno;
win->line->cells[win->col].len = c->len;
win->line->cells[win->col].data = c->c[0];
win->col++;
/* set cells of a character which uses multiple columns */
for (int i = 1; i < width; i++)
win->line->cells[win->col++] = empty;
waddstr(win->win, c->c);
return true;
}
return false;
}
}
void window_cursor_getxy(Win *win, size_t *lineno, size_t *col) {
Cursor *cursor = &win->cursor;
Line *line = cursor->line;
*lineno = line->lineno;
*col = cursor->col;
while (line->prev && line->prev->lineno == *lineno) {
line = line->prev;
*col += line->width;
}
*col += 1;
}
/* place the cursor according to the screen coordinates in win->{row,col} and
* fire user callback. if a selection is active, redraw the window to reflect
* its changes. */
static size_t window_cursor_update(Win *win) {
Cursor *cursor = &win->cursor;
if (win->sel.start != EPOS) {
win->sel.end = cursor->pos;
window_draw(win);
}
wmove(win->win, cursor->row, cursor->col);
if (win->cursor_moved)
win->cursor_moved(win, win->cursor_moved_data);
return cursor->pos;
}
/* move the cursor to the character at pos bytes from the begining of the file.
* if pos is not in the current viewport, redraw the window to make it visible */
void window_cursor_to(Win *win, size_t pos) {
Line *line = win->topline;
int row = 0;
int col = 0;
size_t max = text_size(win->text);
if (pos > max)
pos = max > 0 ? max - 1 : 0;
if (pos == max && win->end != max) {
/* do not display an empty screen when showing the end of the file */
win->start = max - 1;
window_viewport_up(win, win->height / 2);
} else {
/* set the start of the viewable region to the start of the line on which
* the cursor should be placed. if this line requires more space than
* available in the window then simply start displaying text at the new
* cursor position */
for (int i = 0; i < 2 && (pos < win->start || pos > win->end); i++) {
win->start = i == 0 ? text_line_begin(win->text, pos) : pos;
window_draw(win);
}
}
size_t cur = win->start;
while (line && line != win->lastline && cur < pos) {
if (cur + line->len > pos)
break;
cur += line->len;
line = line->next;
row++;
}
if (line) {
int max_col = MIN(win->width, line->width);
while (cur < pos && col < max_col) {
cur += line->cells[col].len;
/* skip over columns occupied by the same character */
while (++col < max_col && line->cells[col].len == 0);
}
} else {
line = win->bottomline;
row = win->height - 1;
}
win->cursor.line = line;
win->cursor.row = row;
win->cursor.col = col;
win->cursor.pos = pos;
window_cursor_update(win);
}
/* redraw the complete with data starting from win->start bytes into the file.
* stop once the screen is full, update win->end, win->lastline */
void window_draw(Win *win) {
window_clear(win);
wmove(win->win, 0, 0);
/* current absolute file position */
size_t pos = win->start;
/* number of bytes to read in one go */
size_t text_len = win->width * win->height;
/* current buffer to work with */
char text[text_len+1];
/* remaining bytes to process in buffer*/
size_t rem = text_bytes_get(win->text, pos, text_len, text);
/* NUL terminate because regex(3) function expect it */
text[rem] = '\0';
/* current position into buffer from which to interpret a character */
char *cur = text;
/* current 'parsed' character' */
Char c;
/* current selection */
Filerange sel = window_selection_get(win);
/* syntax definition to use */
Syntax *syntax = win->syntax;
/* matched tokens for each syntax rule */
regmatch_t match[syntax ? LENGTH(syntax->rules) : 1][1], *matched = NULL;
memset(match, 0, sizeof match);
/* default and current curses attributes to use */
int default_attrs = COLOR_PAIR(0) | A_NORMAL, attrs = default_attrs;
while (rem > 0) {
if (syntax) {
if (matched && cur >= text + matched->rm_eo) {
/* end of current match */
matched = NULL;
attrs = default_attrs;
for (int i = 0; i < LENGTH(syntax->rules); i++) {
if (match[i][0].rm_so == -1)
continue; /* no match on whole text */
/* reset matches which overlap with matched */
if (text + match[i][0].rm_so <= cur && cur < text + match[i][0].rm_eo) {
match[i][0].rm_so = 0;
match[i][0].rm_eo = 0;
}
}
}
if (!matched) {
size_t off = cur - text; /* number of already processed bytes */
for (int i = 0; i < LENGTH(syntax->rules); i++) {
SyntaxRule *rule = &syntax->rules[i];
if (!rule->rule)
break;
if (match[i][0].rm_so == -1)
continue; /* no match on whole text */
if (off >= (size_t)match[i][0].rm_eo) {
/* past match, continue search from current position */
if (regexec(&rule->regex, cur, 1, match[i], 0) ||
match[i][0].rm_so == match[i][0].rm_eo) {
match[i][0].rm_so = -1;
match[i][0].rm_eo = -1;
continue;
}
match[i][0].rm_so += off;
match[i][0].rm_eo += off;
}
if (text + match[i][0].rm_so <= cur && cur < text + match[i][0].rm_eo) {
/* within matched expression */
matched = &match[i][0];
attrs = rule->color->attr;
break; /* first match wins */
}
}
}
}
size_t len = mbrtowc(&c.wchar, cur, rem, NULL);
if (len == (size_t)-1 && errno == EILSEQ) {
/* ok, we encountered an invalid multibyte sequence,
* replace it with the Unicode Replacement Character
* (FFFD) and skip until the start of the next utf8 char */
for (len = 1; rem > len && !ISUTF8(cur[len]); len++);
c = (Char){ .c = "\xEF\xBF\xBD", .wchar = 0xFFFD, .len = len };
} else if (len == (size_t)-2) {
/* not enough bytes available to convert to a
* wide character. advance file position and read
* another junk into buffer.
*/
rem = text_bytes_get(win->text, pos, text_len, text);
text[rem] = '\0';
cur = text;
continue;
} else if (len == 0) {
/* NUL byte encountered, store it and continue */
len = 1;
c = (Char){ .c = "\x00", .wchar = 0x00, .len = len };
} else {
for (size_t i = 0; i < len; i++)
c.c[i] = cur[i];
c.c[len] = '\0';
c.len = len;
}
if (cur[0] == '\r' && rem > 1 && cur[1] == '\n') {
/* convert windows style newline \r\n into a single char with len = 2 */
len = 2;
c = (Char){ .c = "\n", .wchar = L'\n', .len = len };
}
if (sel.start <= pos && pos < sel.end)
wattrset(win->win, attrs | A_REVERSE);
else
wattrset(win->win, attrs);
if (!window_addch(win, &c))
break;
rem -= len;
cur += len;
pos += len;
}
/* set end of viewing region */
win->end = pos;
win->lastline = win->line ? win->line : win->bottomline;
win->lastline->next = NULL;
/* and clear the rest of the unused window */
wclrtobot(win->win);
}
bool window_resize(Win *win, int width, int height) {
if (wresize(win->win, height, width) == ERR)
return false;
// TODO: only grow memory area
win->height = height;
win->width = width;
free(win->lines);
size_t line_size = sizeof(Line) + width*sizeof(Cell);
if (!(win->lines = calloc(height, line_size)))
return false;
return true;
}
void window_move(Win *win, int x, int y) {
mvwin(win->win, y, x);
}
void window_free(Win *win) {
if (!win)
return;
if (win->win)
delwin(win->win);
free(win->lines);
free(win);
}
void window_reload(Win *win, Text *text) {
win->text = text;
window_selection_clear(win);
window_cursor_to(win, 0);
}
Win *window_new(Text *text) {
if (!text)
return NULL;
Win *win = calloc(1, sizeof(Win));
if (!win || !(win->win = newwin(0, 0, 0, 0))) {
window_free(win);
return NULL;
}
win->text = text;
win->tabwidth = 8;
int width, height;
getmaxyx(win->win, height, width);
if (!window_resize(win, width, height)) {
window_free(win);
return NULL;
}
window_selection_clear(win);
window_cursor_to(win, 0);
return win;
}
size_t window_char_prev(Win *win) {
Cursor *cursor = &win->cursor;
Line *line = cursor->line;
do {
if (cursor->col == 0) {
if (!line->prev)
return cursor->pos;
cursor->line = line = line->prev;
cursor->col = MIN(line->width, win->width - 1);
cursor->row--;
} else {
cursor->col--;
}
} while (line->cells[cursor->col].len == 0);
cursor->pos -= line->cells[cursor->col].len;
return window_cursor_update(win);
}
size_t window_char_next(Win *win) {
Cursor *cursor = &win->cursor;
Line *line = cursor->line;
do {
cursor->pos += line->cells[cursor->col].len;
if ((line->width == win->width && cursor->col == win->width - 1) ||
cursor->col == line->width) {
if (!line->next)
return cursor->pos;
cursor->line = line = line->next;
cursor->row++;
cursor->col = 0;
} else {
cursor->col++;
}
} while (line->cells[cursor->col].len == 0);
return window_cursor_update(win);
}
static size_t window_cursor_set(Win *win, Line *line, int col) {
int row = 0;
size_t pos = win->start;
Cursor *cursor = &win->cursor;
/* get row number and file offset at start of the given line */
for (Line *cur = win->topline; cur && cur != line; cur = cur->next) {
pos += cur->len;
row++;
}
/* for characters which use more than 1 column, make sure we are on the left most */
while (col > 0 && line->cells[col].len == 0 && line->cells[col].data != '\t')
col--;
while (col < line->width && line->cells[col].data == '\t')
col++;
/* calculate offset within the line */
for (int i = 0; i < col; i++)
pos += line->cells[i].len;
cursor->col = col;
cursor->row = row;
cursor->pos = pos;
cursor->line = line;
window_cursor_update(win);
return pos;
}
static bool window_viewport_down(Win *win, int n) {
Line *line;
if (win->end == text_size(win->text))
return false;
if (n >= win->height) {
win->start = win->end;
} else {
for (line = win->topline; line && n > 0; line = line->next, n--)
win->start += line->len;
}
window_draw(win);
return true;
}
static bool window_viewport_up(Win *win, int n) {
/* scrolling up is somewhat tricky because we do not yet know where
* the lines start, therefore scan backwards but stop at a reasonable
* maximum in case we are dealing with a file without any newlines
*/
if (win->start == 0)
return false;
size_t max = win->width * win->height;
char c;
Iterator it = text_iterator_get(win->text, win->start - 1);
if (!text_iterator_byte_get(&it, &c))
return false;
size_t off = 0;
/* skip newlines immediately before display area */
if (c == '\n' && text_iterator_byte_prev(&it, &c))
off++;
if (c == '\r' && text_iterator_byte_prev(&it, &c))
off++;
do {
if (c == '\n' && --n == 0)
break;
if (++off > max)
break;
} while (text_iterator_byte_prev(&it, &c));
if (c == '\r')
off++;
win->start -= off;
window_draw(win);
return true;
}
size_t window_slide_up(Win *win, int lines) {
Cursor *cursor = &win->cursor;
if (window_viewport_down(win, lines)) {
if (cursor->line == win->topline)
window_cursor_set(win, win->topline, cursor->col);
else
window_cursor_to(win, cursor->pos);
} else {
window_line_down(win);
}
return cursor->pos;
}
size_t window_slide_down(Win *win, int lines) {
Cursor *cursor = &win->cursor;
if (window_viewport_up(win, lines)) {
if (cursor->line == win->lastline)
window_cursor_set(win, win->lastline, cursor->col);
else
window_cursor_to(win, cursor->pos);
} else {
window_line_up(win);
}
return cursor->pos;
}
size_t window_scroll_up(Win *win, int lines) {
Cursor *cursor = &win->cursor;
if (window_viewport_up(win, lines)) {
Line *line = cursor->line < win->lastline ? cursor->line : win->lastline;
window_cursor_set(win, line, win->cursor.col);
} else {
window_cursor_to(win, 0);
}
return cursor->pos;
}
size_t window_scroll_down(Win *win, int lines) {
Cursor *cursor = &win->cursor;
if (window_viewport_down(win, lines)) {
Line *line = cursor->line > win->topline ? cursor->line : win->topline;
window_cursor_set(win, line, cursor->col);
} else {
window_cursor_to(win, text_size(win->text));
}
return cursor->pos;
}
size_t window_line_up(Win *win) {
Cursor *cursor = &win->cursor;
if (!cursor->line->prev)
window_scroll_up(win, 1);
else
window_cursor_set(win, cursor->line->prev, cursor->col);
return cursor->pos;
}
size_t window_line_down(Win *win) {
Cursor *cursor = &win->cursor;
if (!cursor->line->next) {
if (cursor->line == win->bottomline)
window_scroll_down(win, 1);
} else {
window_cursor_set(win, cursor->line->next, cursor->col);
}
return cursor->pos;
}
size_t window_line_begin(Win *win) {
return window_cursor_set(win, win->cursor.line, 0);
}
size_t window_line_middle(Win *win) {
Cursor *cursor = &win->cursor;
return window_cursor_set(win, cursor->line, cursor->line->width / 2);
}
size_t window_line_end(Win *win) {
Cursor *cursor = &win->cursor;
int col = cursor->line->width - 1;
return window_cursor_set(win, cursor->line, col >= 0 ? col : 0);
}
void window_update(Win *win) {
wnoutrefresh(win->win);
}
size_t window_delete_key(Win *win) {
Cursor *cursor = &win->cursor;
Line *line = cursor->line;
size_t len = line->cells[cursor->col].len;
text_delete(win->text, cursor->pos, len);
window_draw(win);
window_cursor_to(win, cursor->pos);
return cursor->pos;
}
size_t window_backspace_key(Win *win) {
Cursor *cursor = &win->cursor;
if (win->start == cursor->pos) {
if (win->start == 0)
return cursor->pos;
/* if we are on the top left most position in the window
* first scroll up so that the to be deleted character is
* visible then proceed as normal */
size_t pos = cursor->pos;
window_viewport_up(win, 1);
window_cursor_to(win, pos);
}
window_char_prev(win);
size_t pos = cursor->pos;
size_t len = cursor->line->cells[cursor->col].len;
text_delete(win->text, pos, len);
window_draw(win);
window_cursor_to(win, pos);
return pos;
}
size_t window_insert_key(Win *win, const char *c, size_t len) {
size_t pos = win->cursor.pos;
text_insert(win->text, pos, c, len);
if (win->cursor.line == win->bottomline && memchr(c, '\n', len))
window_viewport_down(win, 1);
else
window_draw(win);
pos += len;
window_cursor_to(win, pos);
return pos;
}
size_t window_replace_key(Win *win, const char *c, size_t len) {
Cursor *cursor = &win->cursor;
Line *line = cursor->line;
size_t pos = cursor->pos;
/* do not overwrite new line which would merge the two lines */
if (line->cells[cursor->col].data != '\n') {
size_t oldlen = line->cells[cursor->col].len;
text_delete(win->text, pos, oldlen);
}
text_insert(win->text, pos, c, len);
if (cursor->line == win->bottomline && memchr(c, '\n', len))
window_viewport_down(win, 1);
else
window_draw(win);
pos += len;
window_cursor_to(win, pos);
return pos;
}
size_t window_cursor_get(Win *win) {
return win->cursor.pos;
}
void window_scroll_to(Win *win, size_t pos) {
while (pos < win->start && window_viewport_up(win, 1));
while (pos > win->end && window_viewport_down(win, 1));
window_cursor_to(win, pos);
}
void window_selection_start(Win *win) {
win->sel.start = win->sel.end = window_cursor_get(win);
window_draw(win);
curs_set(0);
}
void window_syntax_set(Win *win, Syntax *syntax) {
win->syntax = syntax;
}
Syntax *window_syntax_get(Win *win) {
return win->syntax;
}
void window_cursor_watch(Win *win, void (*cursor_moved)(Win*, void *), void *data) {
win->cursor_moved = cursor_moved;
win->cursor_moved_data = data;
}
size_t window_line_goto(Win *win, int n) {
size_t pos = win->start;
for (Line *line = win->topline; --n > 0 && line != win->lastline; line = line->next)
pos += line->len;
return pos;
}