vis: implement complement of selections
[vis.git] / view.c
blobd6de690406d3c65ff8e77d7d95afc8b4dd623cc6
1 #include <string.h>
2 #include <stdlib.h>
3 #include <wchar.h>
4 #include <ctype.h>
5 #include <errno.h>
6 #include <limits.h>
7 #include "view.h"
8 #include "text.h"
9 #include "text-motions.h"
10 #include "text-util.h"
11 #include "util.h"
13 typedef struct {
14 char *symbol;
15 } SyntaxSymbol;
17 enum {
18 SYNTAX_SYMBOL_SPACE,
19 SYNTAX_SYMBOL_TAB,
20 SYNTAX_SYMBOL_TAB_FILL,
21 SYNTAX_SYMBOL_EOL,
22 SYNTAX_SYMBOL_LAST,
25 /* A selection is made up of two marks named cursor and anchor.
26 * While the anchor remains fixed the cursor mark follows cursor motions.
27 * For a selection (indicated by []), the marks (^) are placed as follows:
29 * [some text] [!]
30 * ^ ^ ^
31 * ^
33 * That is the marks point to the *start* of the first and last character
34 * of the selection. In particular for a single character selection (as
35 * depicted on the right above) both marks point to the same location.
37 * The view_selections_{get,set} functions take care of adding/removing
38 * the necessary offset for the last character.
41 struct Selection {
42 Mark cursor; /* other selection endpoint where it changes */
43 Mark anchor; /* position where the selection was created */
44 bool anchored; /* whether anchor remains fixed */
45 size_t pos; /* in bytes from the start of the file */
46 int row, col; /* in terms of zero based screen coordinates */
47 int lastcol; /* remembered column used when moving across lines */
48 Line *line; /* screen line on which cursor currently resides */
49 int generation; /* used to filter out newly created cursors during iteration */
50 int number; /* how many cursors are located before this one */
51 SelectionRegion region; /* saved selection region */
52 View *view; /* associated view to which this cursor belongs */
53 Selection *prev, *next; /* previous/next cursors ordered by location at creation time */
56 struct View {
57 Text *text; /* underlying text management */
58 UiWin *ui; /* corresponding ui window */
59 Cell cell_blank; /* used for empty/blank cells */
60 int width, height; /* size of display area */
61 size_t start, end; /* currently displayed area [start, end] in bytes from the start of the file */
62 size_t start_last; /* previously used start of visible area, used to update the mark */
63 Mark start_mark; /* mark to keep track of the start of the visible area */
64 size_t lines_size; /* number of allocated bytes for lines (grows only) */
65 Line *lines; /* view->height number of lines representing view content */
66 Line *topline; /* top of the view, first line currently shown */
67 Line *lastline; /* last currently used line, always <= bottomline */
68 Line *bottomline; /* bottom of view, might be unused if lastline < bottomline */
69 Selection *selection; /* primary selection, always placed within the visible viewport */
70 Selection *selection_latest; /* most recently created cursor */
71 Selection *selection_dead; /* primary cursor which was disposed, will be removed when another cursor is created */
72 int selection_count; /* how many cursors do currently exist */
73 Line *line; /* used while drawing view content, line where next char will be drawn */
74 int col; /* used while drawing view content, column where next char will be drawn */
75 const SyntaxSymbol *symbols[SYNTAX_SYMBOL_LAST]; /* symbols to use for white spaces etc */
76 int tabwidth; /* how many spaces should be used to display a tab character */
77 Selection *selections; /* all cursors currently active */
78 int selection_generation; /* used to filter out newly created cursors during iteration */
79 bool need_update; /* whether view has been redrawn */
80 bool large_file; /* optimize for displaying large files */
81 int colorcolumn;
84 static const SyntaxSymbol symbols_none[] = {
85 [SYNTAX_SYMBOL_SPACE] = { " " },
86 [SYNTAX_SYMBOL_TAB] = { " " },
87 [SYNTAX_SYMBOL_TAB_FILL] = { " " },
88 [SYNTAX_SYMBOL_EOL] = { " " },
91 static const SyntaxSymbol symbols_default[] = {
92 [SYNTAX_SYMBOL_SPACE] = { "·" /* Middle Dot U+00B7 */ },
93 [SYNTAX_SYMBOL_TAB] = { "›" /* Single Right-Pointing Angle Quotation Mark U+203A */ },
94 [SYNTAX_SYMBOL_TAB_FILL] = { " " },
95 [SYNTAX_SYMBOL_EOL] = { "↵" /* Downwards Arrow with Corner Leftwards U+21B5 */ },
98 static Cell cell_unused;
101 /* move visible viewport n-lines up/down, redraws the view but does not change
102 * cursor position which becomes invalid and should be corrected by calling
103 * view_cursor_to. the return value indicates wether the visible area changed.
105 static bool view_viewport_up(View *view, int n);
106 static bool view_viewport_down(View *view, int n);
108 static void view_clear(View *view);
109 static bool view_addch(View *view, Cell *cell);
110 static void selection_free(Selection*);
111 /* set/move current cursor position to a given (line, column) pair */
112 static size_t cursor_set(Selection*, Line *line, int col);
114 void view_tabwidth_set(View *view, int tabwidth) {
115 view->tabwidth = tabwidth;
116 view_draw(view);
119 /* reset internal view data structures (cell matrix, line offsets etc.) */
120 static void view_clear(View *view) {
121 memset(view->lines, 0, view->lines_size);
122 if (view->start != view->start_last) {
123 if (view->start == 0)
124 view->start_mark = EMARK;
125 else
126 view->start_mark = text_mark_set(view->text, view->start);
127 } else {
128 size_t start;
129 if (view->start_mark == EMARK)
130 start = 0;
131 else
132 start = text_mark_get(view->text, view->start_mark);
133 if (start != EPOS)
134 view->start = start;
137 view->start_last = view->start;
138 view->topline = view->lines;
139 view->topline->lineno = view->large_file ? 1 : text_lineno_by_pos(view->text, view->start);
140 view->lastline = view->topline;
142 size_t line_size = sizeof(Line) + view->width*sizeof(Cell);
143 size_t end = view->height * line_size;
144 Line *prev = NULL;
145 for (size_t i = 0; i < end; i += line_size) {
146 Line *line = (Line*)(((char*)view->lines) + i);
147 line->prev = prev;
148 if (prev)
149 prev->next = line;
150 prev = line;
152 view->bottomline = prev ? prev : view->topline;
153 view->bottomline->next = NULL;
154 view->line = view->topline;
155 view->col = 0;
156 if (view->ui)
157 view->cell_blank.style = view->ui->style_get(view->ui, UI_STYLE_DEFAULT);
160 Filerange view_viewport_get(View *view) {
161 return (Filerange){ .start = view->start, .end = view->end };
164 /* try to add another character to the view, return whether there was space left */
165 static bool view_addch(View *view, Cell *cell) {
166 if (!view->line)
167 return false;
169 int width;
170 size_t lineno = view->line->lineno;
171 unsigned char ch = (unsigned char)cell->data[0];
172 cell->style = view->cell_blank.style;
174 switch (ch) {
175 case '\t':
176 cell->width = 1;
177 width = view->tabwidth - (view->col % view->tabwidth);
178 for (int w = 0; w < width; w++) {
179 if (view->col + 1 > view->width) {
180 view->line = view->line->next;
181 view->col = 0;
182 if (!view->line)
183 return false;
184 view->line->lineno = lineno;
187 cell->len = w == 0 ? 1 : 0;
188 int t = w == 0 ? SYNTAX_SYMBOL_TAB : SYNTAX_SYMBOL_TAB_FILL;
189 strncpy(cell->data, view->symbols[t]->symbol, sizeof(cell->data)-1);
190 view->line->cells[view->col] = *cell;
191 view->line->len += cell->len;
192 view->line->width += cell->width;
193 view->col++;
195 cell->len = 1;
196 return true;
197 case '\n':
198 cell->width = 1;
199 if (view->col + cell->width > view->width) {
200 view->line = view->line->next;
201 view->col = 0;
202 if (!view->line)
203 return false;
204 view->line->lineno = lineno;
207 strncpy(cell->data, view->symbols[SYNTAX_SYMBOL_EOL]->symbol, sizeof(cell->data)-1);
209 view->line->cells[view->col] = *cell;
210 view->line->len += cell->len;
211 view->line->width += cell->width;
212 for (int i = view->col + 1; i < view->width; i++)
213 view->line->cells[i] = view->cell_blank;
215 view->line = view->line->next;
216 if (view->line)
217 view->line->lineno = lineno + 1;
218 view->col = 0;
219 return true;
220 default:
221 if (ch < 128 && !isprint(ch)) {
222 /* non-printable ascii char, represent it as ^(char + 64) */
223 *cell = (Cell) {
224 .data = { '^', ch == 127 ? '?' : ch + 64, '\0' },
225 .len = 1,
226 .width = 2,
227 .style = cell->style,
231 if (ch == ' ') {
232 strncpy(cell->data, view->symbols[SYNTAX_SYMBOL_SPACE]->symbol, sizeof(cell->data)-1);
236 if (view->col + cell->width > view->width) {
237 for (int i = view->col; i < view->width; i++)
238 view->line->cells[i] = view->cell_blank;
239 view->line = view->line->next;
240 view->col = 0;
243 if (view->line) {
244 view->line->width += cell->width;
245 view->line->len += cell->len;
246 view->line->lineno = lineno;
247 view->line->cells[view->col] = *cell;
248 view->col++;
249 /* set cells of a character which uses multiple columns */
250 for (int i = 1; i < cell->width; i++)
251 view->line->cells[view->col++] = cell_unused;
252 return true;
254 return false;
258 static void cursor_to(Selection *s, size_t pos) {
259 Text *txt = s->view->text;
260 s->cursor = text_mark_set(txt, pos);
261 if (!s->anchored)
262 s->anchor = s->cursor;
263 if (pos != s->pos)
264 s->lastcol = 0;
265 s->pos = pos;
266 if (!view_coord_get(s->view, pos, &s->line, &s->row, &s->col)) {
267 if (s->view->selection == s) {
268 s->line = s->view->topline;
269 s->row = 0;
270 s->col = 0;
272 return;
274 // TODO: minimize number of redraws
275 view_draw(s->view);
278 bool view_coord_get(View *view, size_t pos, Line **retline, int *retrow, int *retcol) {
279 int row = 0, col = 0;
280 size_t cur = view->start;
281 Line *line = view->topline;
283 if (pos < view->start || pos > view->end) {
284 if (retline) *retline = NULL;
285 if (retrow) *retrow = -1;
286 if (retcol) *retcol = -1;
287 return false;
290 while (line && line != view->lastline && cur < pos) {
291 if (cur + line->len > pos)
292 break;
293 cur += line->len;
294 line = line->next;
295 row++;
298 if (line) {
299 int max_col = MIN(view->width, line->width);
300 while (cur < pos && col < max_col) {
301 cur += line->cells[col].len;
302 /* skip over columns occupied by the same character */
303 while (++col < max_col && line->cells[col].len == 0);
305 } else {
306 line = view->bottomline;
307 row = view->height - 1;
310 if (retline) *retline = line;
311 if (retrow) *retrow = row;
312 if (retcol) *retcol = col;
313 return true;
316 /* move the cursor to the character at pos bytes from the begining of the file.
317 * if pos is not in the current viewport, redraw the view to make it visible */
318 void view_cursor_to(View *view, size_t pos) {
319 view_cursors_to(view->selection, pos);
322 /* redraw the complete with data starting from view->start bytes into the file.
323 * stop once the screen is full, update view->end, view->lastline */
324 void view_draw(View *view) {
325 view_clear(view);
326 /* read a screenful of text considering each character as 4-byte UTF character*/
327 const size_t size = view->width * view->height * 4;
328 /* current buffer to work with */
329 char text[size+1];
330 /* remaining bytes to process in buffer */
331 size_t rem = text_bytes_get(view->text, view->start, size, text);
332 /* NUL terminate text section */
333 text[rem] = '\0';
334 /* absolute position of character currently being added to display */
335 size_t pos = view->start;
336 /* current position into buffer from which to interpret a character */
337 char *cur = text;
338 /* start from known multibyte state */
339 mbstate_t mbstate = { 0 };
341 Cell cell = { .data = "", .len = 0, .width = 0, }, prev_cell = cell;
343 while (rem > 0) {
345 /* current 'parsed' character' */
346 wchar_t wchar;
348 size_t len = mbrtowc(&wchar, cur, rem, &mbstate);
349 if (len == (size_t)-1 && errno == EILSEQ) {
350 /* ok, we encountered an invalid multibyte sequence,
351 * replace it with the Unicode Replacement Character
352 * (FFFD) and skip until the start of the next utf8 char */
353 for (len = 1; rem > len && !ISUTF8(cur[len]); len++);
354 cell = (Cell){ .data = "\xEF\xBF\xBD", .len = len, .width = 1 };
355 } else if (len == (size_t)-2) {
356 /* not enough bytes available to convert to a
357 * wide character. advance file position and read
358 * another junk into buffer.
360 rem = text_bytes_get(view->text, pos, size, text);
361 text[rem] = '\0';
362 cur = text;
363 continue;
364 } else if (len == 0) {
365 /* NUL byte encountered, store it and continue */
366 cell = (Cell){ .data = "\x00", .len = 1, .width = 2 };
367 } else {
368 if (len >= sizeof(cell.data))
369 len = sizeof(cell.data)-1;
370 for (size_t i = 0; i < len; i++)
371 cell.data[i] = cur[i];
372 cell.data[len] = '\0';
373 cell.len = len;
374 cell.width = wcwidth(wchar);
375 if (cell.width == -1)
376 cell.width = 1;
379 if (cell.width == 0 && prev_cell.len + cell.len < sizeof(cell.data)) {
380 prev_cell.len += cell.len;
381 strcat(prev_cell.data, cell.data);
382 } else {
383 if (prev_cell.len && !view_addch(view, &prev_cell))
384 break;
385 pos += prev_cell.len;
386 prev_cell = cell;
389 rem -= cell.len;
390 cur += cell.len;
392 memset(&cell, 0, sizeof cell);
395 if (prev_cell.len && view_addch(view, &prev_cell))
396 pos += prev_cell.len;
398 /* set end of viewing region */
399 view->end = pos;
400 if (view->line) {
401 bool eof = view->end == text_size(view->text);
402 if (view->line->len == 0 && eof && view->line->prev)
403 view->lastline = view->line->prev;
404 else
405 view->lastline = view->line;
406 } else {
407 view->lastline = view->bottomline;
410 /* clear remaining of line, important to show cursor at end of file */
411 if (view->line) {
412 for (int x = view->col; x < view->width; x++)
413 view->line->cells[x] = view->cell_blank;
416 /* resync position of cursors within visible area */
417 for (Selection *s = view->selections; s; s = s->next) {
418 size_t pos = view_cursors_pos(s);
419 if (!view_coord_get(view, pos, &s->line, &s->row, &s->col) &&
420 s == view->selection) {
421 s->line = view->topline;
422 s->row = 0;
423 s->col = 0;
427 view->need_update = true;
430 void view_invalidate(View *view) {
431 view->need_update = true;
434 bool view_update(View *view) {
435 if (!view->need_update)
436 return false;
437 for (Line *l = view->lastline->next; l; l = l->next) {
438 for (int x = 0; x < view->width; x++)
439 l->cells[x] = view->cell_blank;
441 view->need_update = false;
442 return true;
445 bool view_resize(View *view, int width, int height) {
446 if (width <= 0)
447 width = 1;
448 if (height <= 0)
449 height = 1;
450 if (view->width == width && view->height == height) {
451 view->need_update = true;
452 return true;
454 size_t lines_size = height*(sizeof(Line) + width*sizeof(Cell));
455 if (lines_size > view->lines_size) {
456 Line *lines = realloc(view->lines, lines_size);
457 if (!lines)
458 return false;
459 view->lines = lines;
460 view->lines_size = lines_size;
462 view->width = width;
463 view->height = height;
464 memset(view->lines, 0, view->lines_size);
465 view_draw(view);
466 return true;
469 int view_height_get(View *view) {
470 return view->height;
473 int view_width_get(View *view) {
474 return view->width;
477 void view_free(View *view) {
478 if (!view)
479 return;
480 while (view->selections)
481 selection_free(view->selections);
482 free(view->lines);
483 free(view);
486 void view_reload(View *view, Text *text) {
487 view->text = text;
488 view_selections_clear_all(view);
489 view_cursor_to(view, 0);
492 View *view_new(Text *text) {
493 if (!text)
494 return NULL;
495 View *view = calloc(1, sizeof(View));
496 if (!view)
497 return NULL;
498 view->text = text;
499 if (!view_selections_new(view, 0)) {
500 view_free(view);
501 return NULL;
504 view->cell_blank = (Cell) {
505 .width = 0,
506 .len = 0,
507 .data = " ",
509 view->tabwidth = 8;
510 view_options_set(view, 0);
512 if (!view_resize(view, 1, 1)) {
513 view_free(view);
514 return NULL;
517 view_cursor_to(view, 0);
519 return view;
522 void view_ui(View *view, UiWin* ui) {
523 view->ui = ui;
526 static size_t cursor_set(Selection *sel, Line *line, int col) {
527 int row = 0;
528 View *view = sel->view;
529 size_t pos = view->start;
530 /* get row number and file offset at start of the given line */
531 for (Line *l = view->topline; l && l != line; l = l->next) {
532 pos += l->len;
533 row++;
536 /* for characters which use more than 1 column, make sure we are on the left most */
537 while (col > 0 && line->cells[col].len == 0)
538 col--;
539 /* calculate offset within the line */
540 for (int i = 0; i < col; i++)
541 pos += line->cells[i].len;
543 sel->col = col;
544 sel->row = row;
545 sel->line = line;
547 cursor_to(sel, pos);
549 return pos;
552 static bool view_viewport_down(View *view, int n) {
553 Line *line;
554 if (view->end >= text_size(view->text))
555 return false;
556 if (n >= view->height) {
557 view->start = view->end;
558 } else {
559 for (line = view->topline; line && n > 0; line = line->next, n--)
560 view->start += line->len;
562 view_draw(view);
563 return true;
566 static bool view_viewport_up(View *view, int n) {
567 /* scrolling up is somewhat tricky because we do not yet know where
568 * the lines start, therefore scan backwards but stop at a reasonable
569 * maximum in case we are dealing with a file without any newlines
571 if (view->start == 0)
572 return false;
573 size_t max = view->width * view->height;
574 char c;
575 Iterator it = text_iterator_get(view->text, view->start - 1);
577 if (!text_iterator_byte_get(&it, &c))
578 return false;
579 size_t off = 0;
580 /* skip newlines immediately before display area */
581 if (c == '\n' && text_iterator_byte_prev(&it, &c))
582 off++;
583 do {
584 if (c == '\n' && --n == 0)
585 break;
586 if (++off > max)
587 break;
588 } while (text_iterator_byte_prev(&it, &c));
589 view->start -= MIN(view->start, off);
590 view_draw(view);
591 return true;
594 void view_redraw_top(View *view) {
595 Line *line = view->selection->line;
596 for (Line *cur = view->topline; cur && cur != line; cur = cur->next)
597 view->start += cur->len;
598 view_draw(view);
599 view_cursor_to(view, view->selection->pos);
602 void view_redraw_center(View *view) {
603 int center = view->height / 2;
604 size_t pos = view->selection->pos;
605 for (int i = 0; i < 2; i++) {
606 int linenr = 0;
607 Line *line = view->selection->line;
608 for (Line *cur = view->topline; cur && cur != line; cur = cur->next)
609 linenr++;
610 if (linenr < center) {
611 view_slide_down(view, center - linenr);
612 continue;
614 for (Line *cur = view->topline; cur && cur != line && linenr > center; cur = cur->next) {
615 view->start += cur->len;
616 linenr--;
618 break;
620 view_draw(view);
621 view_cursor_to(view, pos);
624 void view_redraw_bottom(View *view) {
625 Line *line = view->selection->line;
626 if (line == view->lastline)
627 return;
628 size_t pos = view->selection->pos;
629 view_viewport_up(view, view->height);
630 while (pos >= view->end && view_viewport_down(view, 1));
631 cursor_to(view->selection, pos);
634 size_t view_slide_up(View *view, int lines) {
635 Selection *sel = view->selection;
636 if (view_viewport_down(view, lines)) {
637 if (sel->line == view->topline)
638 cursor_set(sel, view->topline, sel->col);
639 else
640 view_cursor_to(view, sel->pos);
641 } else {
642 view_screenline_down(sel);
644 return sel->pos;
647 size_t view_slide_down(View *view, int lines) {
648 Selection *sel = view->selection;
649 bool lastline = sel->line == view->lastline;
650 size_t col = sel->col;
651 if (view_viewport_up(view, lines)) {
652 if (lastline)
653 cursor_set(sel, view->lastline, col);
654 else
655 view_cursor_to(view, sel->pos);
656 } else {
657 view_screenline_up(sel);
659 return sel->pos;
662 size_t view_scroll_up(View *view, int lines) {
663 Selection *sel = view->selection;
664 if (view_viewport_up(view, lines)) {
665 Line *line = sel->line < view->lastline ? sel->line : view->lastline;
666 cursor_set(sel, line, view->selection->col);
667 } else {
668 view_cursor_to(view, 0);
670 return sel->pos;
673 size_t view_scroll_page_up(View *view) {
674 Selection *sel = view->selection;
675 if (view->start == 0) {
676 view_cursor_to(view, 0);
677 } else {
678 view_cursor_to(view, view->start-1);
679 view_redraw_bottom(view);
680 view_screenline_begin(sel);
682 return sel->pos;
685 size_t view_scroll_page_down(View *view) {
686 view_scroll_down(view, view->height);
687 return view_screenline_begin(view->selection);
690 size_t view_scroll_halfpage_up(View *view) {
691 Selection *sel = view->selection;
692 if (view->start == 0) {
693 view_cursor_to(view, 0);
694 } else {
695 view_cursor_to(view, view->start-1);
696 view_redraw_center(view);
697 view_screenline_begin(sel);
699 return sel->pos;
702 size_t view_scroll_halfpage_down(View *view) {
703 size_t end = view->end;
704 size_t pos = view_scroll_down(view, view->height/2);
705 if (pos < text_size(view->text))
706 view_cursor_to(view, end);
707 return view->selection->pos;
710 size_t view_scroll_down(View *view, int lines) {
711 Selection *sel = view->selection;
712 if (view_viewport_down(view, lines)) {
713 Line *line = sel->line > view->topline ? sel->line : view->topline;
714 cursor_set(sel, line, sel->col);
715 } else {
716 view_cursor_to(view, text_size(view->text));
718 return sel->pos;
721 size_t view_line_up(Selection *sel) {
722 View *view = sel->view;
723 int lastcol = sel->lastcol;
724 if (!lastcol)
725 lastcol = sel->col;
726 size_t pos = text_line_up(sel->view->text, sel->pos);
727 bool offscreen = view->selection == sel && pos < view->start;
728 view_cursors_to(sel, pos);
729 if (offscreen)
730 view_redraw_top(view);
731 if (sel->line)
732 cursor_set(sel, sel->line, lastcol);
733 sel->lastcol = lastcol;
734 return sel->pos;
737 size_t view_line_down(Selection *sel) {
738 View *view = sel->view;
739 int lastcol = sel->lastcol;
740 if (!lastcol)
741 lastcol = sel->col;
742 size_t pos = text_line_down(sel->view->text, sel->pos);
743 bool offscreen = view->selection == sel && pos > view->end;
744 view_cursors_to(sel, pos);
745 if (offscreen)
746 view_redraw_bottom(view);
747 if (sel->line)
748 cursor_set(sel, sel->line, lastcol);
749 sel->lastcol = lastcol;
750 return sel->pos;
753 size_t view_screenline_up(Selection *sel) {
754 if (!sel->line)
755 return view_line_up(sel);
756 int lastcol = sel->lastcol;
757 if (!lastcol)
758 lastcol = sel->col;
759 if (!sel->line->prev)
760 view_scroll_up(sel->view, 1);
761 if (sel->line->prev)
762 cursor_set(sel, sel->line->prev, lastcol);
763 sel->lastcol = lastcol;
764 return sel->pos;
767 size_t view_screenline_down(Selection *sel) {
768 if (!sel->line)
769 return view_line_down(sel);
770 int lastcol = sel->lastcol;
771 if (!lastcol)
772 lastcol = sel->col;
773 if (!sel->line->next && sel->line == sel->view->bottomline)
774 view_scroll_down(sel->view, 1);
775 if (sel->line->next)
776 cursor_set(sel, sel->line->next, lastcol);
777 sel->lastcol = lastcol;
778 return sel->pos;
781 size_t view_screenline_begin(Selection *sel) {
782 if (!sel->line)
783 return sel->pos;
784 return cursor_set(sel, sel->line, 0);
787 size_t view_screenline_middle(Selection *sel) {
788 if (!sel->line)
789 return sel->pos;
790 return cursor_set(sel, sel->line, sel->line->width / 2);
793 size_t view_screenline_end(Selection *sel) {
794 if (!sel->line)
795 return sel->pos;
796 int col = sel->line->width - 1;
797 return cursor_set(sel, sel->line, col >= 0 ? col : 0);
800 size_t view_cursor_get(View *view) {
801 return view_cursors_pos(view->selection);
804 Line *view_lines_first(View *view) {
805 return view->topline;
808 Line *view_lines_last(View *view) {
809 return view->lastline;
812 Line *view_cursors_line_get(Selection *sel) {
813 return sel->line;
816 void view_scroll_to(View *view, size_t pos) {
817 view_cursors_scroll_to(view->selection, pos);
820 void view_options_set(View *view, enum UiOption options) {
821 const int mapping[] = {
822 [SYNTAX_SYMBOL_SPACE] = UI_OPTION_SYMBOL_SPACE,
823 [SYNTAX_SYMBOL_TAB] = UI_OPTION_SYMBOL_TAB,
824 [SYNTAX_SYMBOL_TAB_FILL] = UI_OPTION_SYMBOL_TAB_FILL,
825 [SYNTAX_SYMBOL_EOL] = UI_OPTION_SYMBOL_EOL,
828 for (int i = 0; i < LENGTH(mapping); i++) {
829 view->symbols[i] = (options & mapping[i]) ? &symbols_default[i] :
830 &symbols_none[i];
833 if (options & UI_OPTION_LINE_NUMBERS_ABSOLUTE)
834 options &= ~UI_OPTION_LARGE_FILE;
836 view->large_file = (options & UI_OPTION_LARGE_FILE);
838 if (view->ui)
839 view->ui->options_set(view->ui, options);
842 enum UiOption view_options_get(View *view) {
843 return view->ui ? view->ui->options_get(view->ui) : 0;
846 void view_colorcolumn_set(View *view, int col) {
847 if (col >= 0)
848 view->colorcolumn = col;
851 int view_colorcolumn_get(View *view) {
852 return view->colorcolumn;
855 size_t view_screenline_goto(View *view, int n) {
856 size_t pos = view->start;
857 for (Line *line = view->topline; --n > 0 && line != view->lastline; line = line->next)
858 pos += line->len;
859 return pos;
862 static Selection *selections_new(View *view, size_t pos, bool force) {
863 if (pos > text_size(view->text))
864 return NULL;
865 Selection *s = calloc(1, sizeof(*s));
866 if (!s)
867 return NULL;
868 s->view = view;
869 s->generation = view->selection_generation;
870 if (!view->selections) {
871 view->selection = s;
872 view->selection_latest = s;
873 view->selections = s;
874 view->selection_count = 1;
875 return s;
878 Selection *prev = NULL, *next = NULL;
879 Selection *latest = view->selection_latest ? view->selection_latest : view->selection;
880 size_t cur = view_cursors_pos(latest);
881 if (pos == cur) {
882 prev = latest;
883 next = prev->next;
884 } else if (pos > cur) {
885 prev = latest;
886 for (next = prev->next; next; prev = next, next = next->next) {
887 cur = view_cursors_pos(next);
888 if (pos <= cur)
889 break;
891 } else if (pos < cur) {
892 next = latest;
893 for (prev = next->prev; prev; next = prev, prev = prev->prev) {
894 cur = view_cursors_pos(prev);
895 if (pos >= cur)
896 break;
900 if (pos == cur && !force)
901 goto err;
903 for (Selection *after = next; after; after = after->next)
904 after->number++;
906 s->prev = prev;
907 s->next = next;
908 if (next)
909 next->prev = s;
910 if (prev) {
911 prev->next = s;
912 s->number = prev->number + 1;
913 } else {
914 view->selections = s;
916 view->selection_latest = s;
917 view->selection_count++;
918 view_selections_dispose(view->selection_dead);
919 view_cursors_to(s, pos);
920 return s;
921 err:
922 free(s);
923 return NULL;
926 Selection *view_selections_new(View *view, size_t pos) {
927 return selections_new(view, pos, false);
930 Selection *view_selections_new_force(View *view, size_t pos) {
931 return selections_new(view, pos, true);
934 int view_selections_count(View *view) {
935 return view->selection_count;
938 int view_selections_number(Selection *sel) {
939 return sel->number;
942 int view_selections_column_count(View *view) {
943 Text *txt = view->text;
944 int cpl_max = 0, cpl = 0; /* cursors per line */
945 size_t line_prev = 0;
946 for (Selection *sel = view->selections; sel; sel = sel->next) {
947 size_t pos = view_cursors_pos(sel);
948 size_t line = text_lineno_by_pos(txt, pos);
949 if (line == line_prev)
950 cpl++;
951 else
952 cpl = 1;
953 line_prev = line;
954 if (cpl > cpl_max)
955 cpl_max = cpl;
957 return cpl_max;
960 static Selection *selections_column_next(View *view, Selection *sel, int column) {
961 size_t line_cur = 0;
962 int column_cur = 0;
963 Text *txt = view->text;
964 if (sel) {
965 size_t pos = view_cursors_pos(sel);
966 line_cur = text_lineno_by_pos(txt, pos);
967 column_cur = INT_MIN;
968 } else {
969 sel = view->selections;
972 for (; sel; sel = sel->next) {
973 size_t pos = view_cursors_pos(sel);
974 size_t line = text_lineno_by_pos(txt, pos);
975 if (line != line_cur) {
976 line_cur = line;
977 column_cur = 0;
978 } else {
979 column_cur++;
981 if (column == column_cur)
982 return sel;
984 return NULL;
987 Selection *view_selections_column(View *view, int column) {
988 return selections_column_next(view, NULL, column);
991 Selection *view_selections_column_next(Selection *sel, int column) {
992 return selections_column_next(sel->view, sel, column);
995 static void selection_free(Selection *s) {
996 if (!s)
997 return;
998 for (Selection *after = s->next; after; after = after->next)
999 after->number--;
1000 if (s->prev)
1001 s->prev->next = s->next;
1002 if (s->next)
1003 s->next->prev = s->prev;
1004 if (s->view->selections == s)
1005 s->view->selections = s->next;
1006 if (s->view->selection == s)
1007 s->view->selection = s->next ? s->next : s->prev;
1008 if (s->view->selection_dead == s)
1009 s->view->selection_dead = NULL;
1010 if (s->view->selection_latest == s)
1011 s->view->selection_latest = s->prev ? s->prev : s->next;
1012 s->view->selection_count--;
1013 free(s);
1016 bool view_selections_dispose(Selection *sel) {
1017 if (!sel)
1018 return true;
1019 View *view = sel->view;
1020 if (!view->selections || !view->selections->next)
1021 return false;
1022 selection_free(sel);
1023 view_selections_primary_set(view->selection);
1024 return true;
1027 bool view_selections_dispose_force(Selection *sel) {
1028 if (view_selections_dispose(sel))
1029 return true;
1030 View *view = sel->view;
1031 if (view->selection_dead)
1032 return false;
1033 view_selection_clear(sel);
1034 view->selection_dead = sel;
1035 return true;
1038 Selection *view_selection_disposed(View *view) {
1039 Selection *sel = view->selection_dead;
1040 view->selection_dead = NULL;
1041 return sel;
1044 Selection *view_selections(View *view) {
1045 view->selection_generation++;
1046 return view->selections;
1049 Selection *view_selections_primary_get(View *view) {
1050 view->selection_generation++;
1051 return view->selection;
1054 void view_selections_primary_set(Selection *s) {
1055 if (!s)
1056 return;
1057 s->view->selection = s;
1058 Mark anchor = s->anchor;
1059 view_cursors_to(s, view_cursors_pos(s));
1060 s->anchor = anchor;
1063 Selection *view_selections_prev(Selection *s) {
1064 View *view = s->view;
1065 for (s = s->prev; s; s = s->prev) {
1066 if (s->generation != view->selection_generation)
1067 return s;
1069 view->selection_generation++;
1070 return NULL;
1073 Selection *view_selections_next(Selection *s) {
1074 View *view = s->view;
1075 for (s = s->next; s; s = s->next) {
1076 if (s->generation != view->selection_generation)
1077 return s;
1079 view->selection_generation++;
1080 return NULL;
1083 size_t view_cursors_pos(Selection *s) {
1084 return text_mark_get(s->view->text, s->cursor);
1087 size_t view_cursors_line(Selection *s) {
1088 size_t pos = view_cursors_pos(s);
1089 return text_lineno_by_pos(s->view->text, pos);
1092 size_t view_cursors_col(Selection *s) {
1093 size_t pos = view_cursors_pos(s);
1094 return text_line_char_get(s->view->text, pos) + 1;
1097 int view_cursors_cell_get(Selection *s) {
1098 return s->line ? s->col : -1;
1101 int view_cursors_cell_set(Selection *s, int cell) {
1102 if (!s->line || cell < 0)
1103 return -1;
1104 cursor_set(s, s->line, cell);
1105 return s->col;
1108 void view_cursors_scroll_to(Selection *s, size_t pos) {
1109 View *view = s->view;
1110 if (view->selection == s) {
1111 view_draw(view);
1112 while (pos < view->start && view_viewport_up(view, 1));
1113 while (pos > view->end && view_viewport_down(view, 1));
1115 view_cursors_to(s, pos);
1118 void view_cursors_to(Selection *s, size_t pos) {
1119 View *view = s->view;
1120 if (pos == EPOS)
1121 return;
1122 size_t size = text_size(view->text);
1123 if (pos > size)
1124 pos = size;
1125 if (s->view->selection == s) {
1126 /* make sure we redraw changes to the very first character of the window */
1127 if (view->start == pos)
1128 view->start_last = 0;
1130 if (view->end == pos && view->lastline == view->bottomline) {
1131 view->start += view->topline->len;
1132 view_draw(view);
1135 if (pos < view->start || pos > view->end) {
1136 view->start = pos;
1137 view_viewport_up(view, view->height / 2);
1140 if (pos <= view->start || pos > view->end) {
1141 view->start = text_line_begin(view->text, pos);
1142 view_draw(view);
1145 if (pos <= view->start || pos > view->end) {
1146 view->start = pos;
1147 view_draw(view);
1151 cursor_to(s, pos);
1154 void view_cursors_place(Selection *s, size_t line, size_t col) {
1155 Text *txt = s->view->text;
1156 size_t pos = text_pos_by_lineno(txt, line);
1157 pos = text_line_char_set(txt, pos, col > 0 ? col-1 : col);
1158 view_cursors_to(s, pos);
1161 void view_selections_anchor(Selection *s) {
1162 s->anchored = true;
1165 void view_selection_clear(Selection *s) {
1166 s->anchored = false;
1167 s->anchor = s->cursor;
1168 s->view->need_update = true;
1171 void view_selections_flip(Selection *s) {
1172 Mark temp = s->anchor;
1173 s->anchor = s->cursor;
1174 s->cursor = temp;
1175 view_cursors_to(s, text_mark_get(s->view->text, s->cursor));
1178 bool view_selections_anchored(Selection *s) {
1179 return s->anchored;
1182 void view_selections_clear_all(View *view) {
1183 for (Selection *s = view->selections; s; s = s->next)
1184 view_selection_clear(s);
1185 view_draw(view);
1188 void view_selections_dispose_all(View *view) {
1189 for (Selection *s = view->selections, *next; s; s = next) {
1190 next = s->next;
1191 if (s != view->selection)
1192 selection_free(s);
1194 view_draw(view);
1197 Filerange view_selection_get(View *view) {
1198 return view_selections_get(view->selection);
1201 Filerange view_selections_get(Selection *s) {
1202 if (!s)
1203 return text_range_empty();
1204 Text *txt = s->view->text;
1205 size_t anchor = text_mark_get(txt, s->anchor);
1206 size_t cursor = text_mark_get(txt, s->cursor);
1207 Filerange sel = text_range_new(anchor, cursor);
1208 if (text_range_valid(&sel))
1209 sel.end = text_char_next(txt, sel.end);
1210 return sel;
1213 bool view_selections_set(Selection *s, const Filerange *r) {
1214 Text *txt = s->view->text;
1215 size_t max = text_size(txt);
1216 if (!text_range_valid(r) || r->start >= max)
1217 return false;
1218 size_t anchor = text_mark_get(txt, s->anchor);
1219 size_t cursor = text_mark_get(txt, s->cursor);
1220 bool left_extending = anchor != EPOS && anchor > cursor;
1221 size_t end = r->end > max ? max : r->end;
1222 if (r->start != end)
1223 end = text_char_prev(txt, end);
1224 view_cursors_to(s, left_extending ? r->start : end);
1225 s->anchor = text_mark_set(txt, left_extending ? end : r->start);
1226 return true;
1229 void view_selections_save(Selection *s) {
1230 s->region.cursor = s->cursor;
1231 s->region.anchor = s->anchor;
1234 Filerange view_regions_restore(View *view, SelectionRegion *s) {
1235 Text *txt = view->text;
1236 size_t anchor = text_mark_get(txt, s->anchor);
1237 size_t cursor = text_mark_get(txt, s->cursor);
1238 Filerange sel = text_range_new(anchor, cursor);
1239 if (text_range_valid(&sel))
1240 sel.end = text_char_next(txt, sel.end);
1241 return sel;
1244 bool view_regions_save(View *view, Filerange *r, SelectionRegion *s) {
1245 Text *txt = view->text;
1246 size_t max = text_size(txt);
1247 if (!text_range_valid(r) || r->start >= max)
1248 return false;
1249 size_t end = r->end > max ? max : r->end;
1250 if (r->start != end)
1251 end = text_char_prev(txt, end);
1252 s->anchor = text_mark_set(txt, r->start);
1253 s->cursor = text_mark_set(txt, end);
1254 return true;
1257 bool view_selections_restore(Selection *s) {
1258 Text *txt = s->view->text;
1259 size_t pos = text_mark_get(txt, s->region.cursor);
1260 if (pos == EPOS)
1261 return false;
1262 if (s->region.anchor != s->region.cursor && text_mark_get(txt, s->region.anchor) == EPOS)
1263 return false;
1264 s->cursor = s->region.cursor;
1265 s->anchor = s->region.anchor;
1266 s->anchored = true;
1267 view_cursors_to(s, pos);
1268 return true;
1271 void view_selections_set_all(View *view, Array *arr) {
1272 Selection *s;
1273 Filerange *r;
1274 size_t i = 0;
1275 for (s = view->selections; s; s = s->next) {
1276 if (!(r = array_get(arr, i++)) || !view_selections_set(s, r)) {
1277 for (Selection *next; s; s = next) {
1278 next = view_selections_next(s);
1279 view_selections_dispose(s);
1281 break;
1284 while ((r = array_get(arr, i++))) {
1285 s = view_selections_new_force(view, r->start);
1286 if (!s || !view_selections_set(s, r))
1287 break;
1289 view_selections_primary_set(view->selections);
1292 Array view_selections_get_all(View *view) {
1293 Array arr;
1294 array_init_sized(&arr, sizeof(Filerange));
1295 if (!array_reserve(&arr, view_selections_count(view)))
1296 return arr;
1297 for (Selection *s = view->selections; s; s = s->next) {
1298 Filerange r = view_selections_get(s);
1299 if (text_range_valid(&r))
1300 array_add(&arr, &r);
1302 return arr;
1305 void view_selections_normalize(View *view) {
1306 Selection *prev = NULL;
1307 Filerange range_prev = text_range_empty();
1308 for (Selection *s = view->selections, *next; s; s = next) {
1309 next = s->next;
1310 Filerange range = view_selections_get(s);
1311 if (!text_range_valid(&range)) {
1312 view_selections_dispose(s);
1313 } else if (prev && text_range_overlap(&range_prev, &range)) {
1314 range_prev = text_range_union(&range_prev, &range);
1315 view_selections_dispose(s);
1316 } else {
1317 if (prev)
1318 view_selections_set(prev, &range_prev);
1319 range_prev = range;
1320 prev = s;
1323 if (prev)
1324 view_selections_set(prev, &range_prev);
1327 Text *view_text(View *view) {
1328 return view->text;
1331 bool view_style_define(View *view, enum UiStyle id, const char *style) {
1332 return view->ui->style_define(view->ui, id, style);
1335 void view_style(View *view, enum UiStyle style_id, size_t start, size_t end) {
1336 if (end < view->start || start > view->end)
1337 return;
1339 CellStyle style = view->ui->style_get(view->ui, style_id);
1340 size_t pos = view->start;
1341 Line *line = view->topline;
1343 /* skip lines before range to be styled */
1344 while (line && pos + line->len <= start) {
1345 pos += line->len;
1346 line = line->next;
1349 if (!line)
1350 return;
1352 int col = 0, width = view->width;
1354 /* skip columns before range to be styled */
1355 while (pos < start && col < width)
1356 pos += line->cells[col++].len;
1358 do {
1359 while (pos <= end && col < width) {
1360 pos += line->cells[col].len;
1361 line->cells[col++].style = style;
1363 col = 0;
1364 } while (pos <= end && (line = line->next));