9 #include "text-motions.h"
10 #include "text-util.h"
20 SYNTAX_SYMBOL_TAB_FILL
,
26 /* A selection is made up of two marks named cursor and anchor.
27 * While the anchor remains fixed the cursor mark follows cursor motions.
28 * For a selection (indicated by []), the marks (^) are placed as follows:
34 * That is the marks point to the *start* of the first and last character
35 * of the selection. In particular for a single character selection (as
36 * depicted on the right above) both marks point to the same location.
38 * The view_selections_{get,set} functions take care of adding/removing
39 * the necessary offset for the last character.
43 Mark cursor
; /* other selection endpoint where it changes */
44 Mark anchor
; /* position where the selection was created */
45 bool anchored
; /* whether anchor remains fixed */
46 size_t pos
; /* in bytes from the start of the file */
47 int row
, col
; /* in terms of zero based screen coordinates */
48 int lastcol
; /* remembered column used when moving across lines */
49 Line
*line
; /* screen line on which cursor currently resides */
50 int generation
; /* used to filter out newly created cursors during iteration */
51 int number
; /* how many cursors are located before this one */
52 View
*view
; /* associated view to which this cursor belongs */
53 Selection
*prev
, *next
; /* previous/next cursors ordered by location at creation time */
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 */
84 static const SyntaxSymbol symbols_none
[] = {
85 [SYNTAX_SYMBOL_SPACE
] = { " " },
86 [SYNTAX_SYMBOL_TAB
] = { " " },
87 [SYNTAX_SYMBOL_TAB_FILL
] = { " " },
88 [SYNTAX_SYMBOL_EOL
] = { " " },
89 [SYNTAX_SYMBOL_EOF
] = { " " },
92 static const SyntaxSymbol symbols_default
[] = {
93 [SYNTAX_SYMBOL_SPACE
] = { "·" /* Middle Dot U+00B7 */ },
94 [SYNTAX_SYMBOL_TAB
] = { "›" /* Single Right-Pointing Angle Quotation Mark U+203A */ },
95 [SYNTAX_SYMBOL_TAB_FILL
] = { " " },
96 [SYNTAX_SYMBOL_EOL
] = { "↵" /* Downwards Arrow with Corner Leftwards U+21B5 */ },
97 [SYNTAX_SYMBOL_EOF
] = { "~" },
100 static Cell cell_unused
;
103 /* move visible viewport n-lines up/down, redraws the view but does not change
104 * cursor position which becomes invalid and should be corrected by calling
105 * view_cursor_to. the return value indicates wether the visible area changed.
107 static bool view_viewport_up(View
*view
, int n
);
108 static bool view_viewport_down(View
*view
, int n
);
110 static void view_clear(View
*view
);
111 static bool view_addch(View
*view
, Cell
*cell
);
112 static void selection_free(Selection
*);
113 /* set/move current cursor position to a given (line, column) pair */
114 static size_t cursor_set(Selection
*, Line
*line
, int col
);
116 void view_tabwidth_set(View
*view
, int tabwidth
) {
117 view
->tabwidth
= tabwidth
;
121 /* reset internal view data structures (cell matrix, line offsets etc.) */
122 static void view_clear(View
*view
) {
123 memset(view
->lines
, 0, view
->lines_size
);
124 if (view
->start
!= view
->start_last
) {
125 if (view
->start
== 0)
126 view
->start_mark
= EMARK
;
128 view
->start_mark
= text_mark_set(view
->text
, view
->start
);
131 if (view
->start_mark
== EMARK
)
134 start
= text_mark_get(view
->text
, view
->start_mark
);
139 view
->start_last
= view
->start
;
140 view
->topline
= view
->lines
;
141 view
->topline
->lineno
= view
->large_file
? 1 : text_lineno_by_pos(view
->text
, view
->start
);
142 view
->lastline
= view
->topline
;
144 size_t line_size
= sizeof(Line
) + view
->width
*sizeof(Cell
);
145 size_t end
= view
->height
* line_size
;
147 for (size_t i
= 0; i
< end
; i
+= line_size
) {
148 Line
*line
= (Line
*)(((char*)view
->lines
) + i
);
154 view
->bottomline
= prev
? prev
: view
->topline
;
155 view
->bottomline
->next
= NULL
;
156 view
->line
= view
->topline
;
159 view
->cell_blank
.style
= view
->ui
->style_get(view
->ui
, UI_STYLE_DEFAULT
);
162 Filerange
view_viewport_get(View
*view
) {
163 return (Filerange
){ .start
= view
->start
, .end
= view
->end
};
166 /* try to add another character to the view, return whether there was space left */
167 static bool view_addch(View
*view
, Cell
*cell
) {
172 size_t lineno
= view
->line
->lineno
;
173 unsigned char ch
= (unsigned char)cell
->data
[0];
174 cell
->style
= view
->cell_blank
.style
;
179 width
= view
->tabwidth
- (view
->col
% view
->tabwidth
);
180 for (int w
= 0; w
< width
; w
++) {
181 if (view
->col
+ 1 > view
->width
) {
182 view
->line
= view
->line
->next
;
186 view
->line
->lineno
= lineno
;
189 cell
->len
= w
== 0 ? 1 : 0;
190 int t
= w
== 0 ? SYNTAX_SYMBOL_TAB
: SYNTAX_SYMBOL_TAB_FILL
;
191 strncpy(cell
->data
, view
->symbols
[t
]->symbol
, sizeof(cell
->data
)-1);
192 view
->line
->cells
[view
->col
] = *cell
;
193 view
->line
->len
+= cell
->len
;
194 view
->line
->width
+= cell
->width
;
201 if (view
->col
+ cell
->width
> view
->width
) {
202 view
->line
= view
->line
->next
;
206 view
->line
->lineno
= lineno
;
209 strncpy(cell
->data
, view
->symbols
[SYNTAX_SYMBOL_EOL
]->symbol
, sizeof(cell
->data
)-1);
211 view
->line
->cells
[view
->col
] = *cell
;
212 view
->line
->len
+= cell
->len
;
213 view
->line
->width
+= cell
->width
;
214 for (int i
= view
->col
+ 1; i
< view
->width
; i
++)
215 view
->line
->cells
[i
] = view
->cell_blank
;
217 view
->line
= view
->line
->next
;
219 view
->line
->lineno
= lineno
+ 1;
223 if (ch
< 128 && !isprint(ch
)) {
224 /* non-printable ascii char, represent it as ^(char + 64) */
226 .data
= { '^', ch
== 127 ? '?' : ch
+ 64, '\0' },
229 .style
= cell
->style
,
234 strncpy(cell
->data
, view
->symbols
[SYNTAX_SYMBOL_SPACE
]->symbol
, sizeof(cell
->data
)-1);
238 if (view
->col
+ cell
->width
> view
->width
) {
239 for (int i
= view
->col
; i
< view
->width
; i
++)
240 view
->line
->cells
[i
] = view
->cell_blank
;
241 view
->line
= view
->line
->next
;
246 view
->line
->width
+= cell
->width
;
247 view
->line
->len
+= cell
->len
;
248 view
->line
->lineno
= lineno
;
249 view
->line
->cells
[view
->col
] = *cell
;
251 /* set cells of a character which uses multiple columns */
252 for (int i
= 1; i
< cell
->width
; i
++)
253 view
->line
->cells
[view
->col
++] = cell_unused
;
260 static void cursor_to(Selection
*s
, size_t pos
) {
261 Text
*txt
= s
->view
->text
;
262 s
->cursor
= text_mark_set(txt
, pos
);
264 s
->anchor
= s
->cursor
;
268 if (!view_coord_get(s
->view
, pos
, &s
->line
, &s
->row
, &s
->col
)) {
269 if (s
->view
->selection
== s
) {
270 s
->line
= s
->view
->topline
;
276 // TODO: minimize number of redraws
280 bool view_coord_get(View
*view
, size_t pos
, Line
**retline
, int *retrow
, int *retcol
) {
281 int row
= 0, col
= 0;
282 size_t cur
= view
->start
;
283 Line
*line
= view
->topline
;
285 if (pos
< view
->start
|| pos
> view
->end
) {
286 if (retline
) *retline
= NULL
;
287 if (retrow
) *retrow
= -1;
288 if (retcol
) *retcol
= -1;
292 while (line
&& line
!= view
->lastline
&& cur
< pos
) {
293 if (cur
+ line
->len
> pos
)
301 int max_col
= MIN(view
->width
, line
->width
);
302 while (cur
< pos
&& col
< max_col
) {
303 cur
+= line
->cells
[col
].len
;
304 /* skip over columns occupied by the same character */
305 while (++col
< max_col
&& line
->cells
[col
].len
== 0);
308 line
= view
->bottomline
;
309 row
= view
->height
- 1;
312 if (retline
) *retline
= line
;
313 if (retrow
) *retrow
= row
;
314 if (retcol
) *retcol
= col
;
318 /* move the cursor to the character at pos bytes from the begining of the file.
319 * if pos is not in the current viewport, redraw the view to make it visible */
320 void view_cursor_to(View
*view
, size_t pos
) {
321 view_cursors_to(view
->selection
, pos
);
324 /* redraw the complete with data starting from view->start bytes into the file.
325 * stop once the screen is full, update view->end, view->lastline */
326 void view_draw(View
*view
) {
328 /* read a screenful of text considering each character as 4-byte UTF character*/
329 const size_t size
= view
->width
* view
->height
* 4;
330 /* current buffer to work with */
332 /* remaining bytes to process in buffer */
333 size_t rem
= text_bytes_get(view
->text
, view
->start
, size
, text
);
334 /* NUL terminate text section */
336 /* absolute position of character currently being added to display */
337 size_t pos
= view
->start
;
338 /* current position into buffer from which to interpret a character */
340 /* start from known multibyte state */
341 mbstate_t mbstate
= { 0 };
343 Cell cell
= { .data
= "", .len
= 0, .width
= 0, }, prev_cell
= cell
;
347 /* current 'parsed' character' */
350 size_t len
= mbrtowc(&wchar
, cur
, rem
, &mbstate
);
351 if (len
== (size_t)-1 && errno
== EILSEQ
) {
352 /* ok, we encountered an invalid multibyte sequence,
353 * replace it with the Unicode Replacement Character
354 * (FFFD) and skip until the start of the next utf8 char */
355 mbstate
= (mbstate_t){0};
356 for (len
= 1; rem
> len
&& !ISUTF8(cur
[len
]); len
++);
357 cell
= (Cell
){ .data
= "\xEF\xBF\xBD", .len
= len
, .width
= 1 };
358 } else if (len
== (size_t)-2) {
359 /* not enough bytes available to convert to a
360 * wide character. advance file position and read
361 * another junk into buffer.
363 rem
= text_bytes_get(view
->text
, pos
+prev_cell
.len
, size
, text
);
367 } else if (len
== 0) {
368 /* NUL byte encountered, store it and continue */
369 cell
= (Cell
){ .data
= "\x00", .len
= 1, .width
= 2 };
371 if (len
>= sizeof(cell
.data
))
372 len
= sizeof(cell
.data
)-1;
373 for (size_t i
= 0; i
< len
; i
++)
374 cell
.data
[i
] = cur
[i
];
375 cell
.data
[len
] = '\0';
377 cell
.width
= wcwidth(wchar
);
378 if (cell
.width
== -1)
382 if (cell
.width
== 0) {
383 strncat(prev_cell
.data
, cell
.data
, sizeof(prev_cell
.data
)-strlen(prev_cell
.data
)-1);
384 prev_cell
.len
+= cell
.len
;
386 if (prev_cell
.len
&& !view_addch(view
, &prev_cell
))
388 pos
+= prev_cell
.len
;
395 memset(&cell
, 0, sizeof cell
);
398 if (prev_cell
.len
&& view_addch(view
, &prev_cell
))
399 pos
+= prev_cell
.len
;
401 /* set end of viewing region */
404 bool eof
= view
->end
== text_size(view
->text
);
405 if (view
->line
->len
== 0 && eof
&& view
->line
->prev
)
406 view
->lastline
= view
->line
->prev
;
408 view
->lastline
= view
->line
;
410 view
->lastline
= view
->bottomline
;
413 /* clear remaining of line, important to show cursor at end of file */
415 for (int x
= view
->col
; x
< view
->width
; x
++)
416 view
->line
->cells
[x
] = view
->cell_blank
;
419 /* resync position of cursors within visible area */
420 for (Selection
*s
= view
->selections
; s
; s
= s
->next
) {
421 size_t pos
= view_cursors_pos(s
);
422 if (!view_coord_get(view
, pos
, &s
->line
, &s
->row
, &s
->col
) &&
423 s
== view
->selection
) {
424 s
->line
= view
->topline
;
430 view
->need_update
= true;
433 void view_invalidate(View
*view
) {
434 view
->need_update
= true;
437 bool view_update(View
*view
) {
438 if (!view
->need_update
)
440 for (Line
*l
= view
->lastline
->next
; l
; l
= l
->next
) {
441 for (int x
= 0; x
< view
->width
; x
++)
442 l
->cells
[x
] = view
->cell_blank
;
444 view
->need_update
= false;
448 bool view_resize(View
*view
, int width
, int height
) {
453 if (view
->width
== width
&& view
->height
== height
) {
454 view
->need_update
= true;
457 size_t lines_size
= height
*(sizeof(Line
) + width
*sizeof(Cell
));
458 if (lines_size
> view
->lines_size
) {
459 Line
*lines
= realloc(view
->lines
, lines_size
);
463 view
->lines_size
= lines_size
;
466 view
->height
= height
;
467 memset(view
->lines
, 0, view
->lines_size
);
472 int view_height_get(View
*view
) {
476 int view_width_get(View
*view
) {
480 void view_free(View
*view
) {
483 while (view
->selections
)
484 selection_free(view
->selections
);
489 void view_reload(View
*view
, Text
*text
) {
491 view_selections_clear_all(view
);
492 view_cursor_to(view
, 0);
495 View
*view_new(Text
*text
) {
498 View
*view
= calloc(1, sizeof(View
));
502 if (!view_selections_new(view
, 0)) {
507 view
->cell_blank
= (Cell
) {
513 view_options_set(view
, 0);
515 if (!view_resize(view
, 1, 1)) {
520 view_cursor_to(view
, 0);
525 void view_ui(View
*view
, UiWin
* ui
) {
529 static size_t cursor_set(Selection
*sel
, Line
*line
, int col
) {
531 View
*view
= sel
->view
;
532 size_t pos
= view
->start
;
533 /* get row number and file offset at start of the given line */
534 for (Line
*l
= view
->topline
; l
&& l
!= line
; l
= l
->next
) {
539 /* for characters which use more than 1 column, make sure we are on the left most */
540 while (col
> 0 && line
->cells
[col
].len
== 0)
542 /* calculate offset within the line */
543 for (int i
= 0; i
< col
; i
++)
544 pos
+= line
->cells
[i
].len
;
555 static bool view_viewport_down(View
*view
, int n
) {
557 if (view
->end
>= text_size(view
->text
))
559 if (n
>= view
->height
) {
560 view
->start
= view
->end
;
562 for (line
= view
->topline
; line
&& n
> 0; line
= line
->next
, n
--)
563 view
->start
+= line
->len
;
569 static bool view_viewport_up(View
*view
, int n
) {
570 /* scrolling up is somewhat tricky because we do not yet know where
571 * the lines start, therefore scan backwards but stop at a reasonable
572 * maximum in case we are dealing with a file without any newlines
574 if (view
->start
== 0)
576 size_t max
= view
->width
* view
->height
;
578 Iterator it
= text_iterator_get(view
->text
, view
->start
- 1);
580 if (!text_iterator_byte_get(&it
, &c
))
583 /* skip newlines immediately before display area */
584 if (c
== '\n' && text_iterator_byte_prev(&it
, &c
))
587 if (c
== '\n' && --n
== 0)
591 } while (text_iterator_byte_prev(&it
, &c
));
592 view
->start
-= MIN(view
->start
, off
);
597 void view_redraw_top(View
*view
) {
598 Line
*line
= view
->selection
->line
;
599 for (Line
*cur
= view
->topline
; cur
&& cur
!= line
; cur
= cur
->next
)
600 view
->start
+= cur
->len
;
602 view_cursor_to(view
, view
->selection
->pos
);
605 void view_redraw_center(View
*view
) {
606 int center
= view
->height
/ 2;
607 size_t pos
= view
->selection
->pos
;
608 for (int i
= 0; i
< 2; i
++) {
610 Line
*line
= view
->selection
->line
;
611 for (Line
*cur
= view
->topline
; cur
&& cur
!= line
; cur
= cur
->next
)
613 if (linenr
< center
) {
614 view_slide_down(view
, center
- linenr
);
617 for (Line
*cur
= view
->topline
; cur
&& cur
!= line
&& linenr
> center
; cur
= cur
->next
) {
618 view
->start
+= cur
->len
;
624 view_cursor_to(view
, pos
);
627 void view_redraw_bottom(View
*view
) {
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
);
640 view_cursor_to(view
, sel
->pos
);
642 view_screenline_down(sel
);
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
)) {
653 cursor_set(sel
, view
->lastline
, col
);
655 view_cursor_to(view
, sel
->pos
);
657 view_screenline_up(sel
);
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
);
668 view_cursor_to(view
, 0);
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);
678 view_cursor_to(view
, view
->start
-1);
679 view_redraw_bottom(view
);
680 view_screenline_begin(sel
);
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);
695 view_cursor_to(view
, view
->start
-1);
696 view_redraw_center(view
);
697 view_screenline_begin(sel
);
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
);
716 view_cursor_to(view
, text_size(view
->text
));
721 size_t view_line_up(Selection
*sel
) {
722 View
*view
= sel
->view
;
723 int lastcol
= sel
->lastcol
;
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
);
730 view_redraw_top(view
);
732 cursor_set(sel
, sel
->line
, lastcol
);
733 sel
->lastcol
= lastcol
;
737 size_t view_line_down(Selection
*sel
) {
738 View
*view
= sel
->view
;
739 int lastcol
= sel
->lastcol
;
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
);
746 view_redraw_bottom(view
);
748 cursor_set(sel
, sel
->line
, lastcol
);
749 sel
->lastcol
= lastcol
;
753 size_t view_screenline_up(Selection
*sel
) {
755 return view_line_up(sel
);
756 int lastcol
= sel
->lastcol
;
759 if (!sel
->line
->prev
)
760 view_scroll_up(sel
->view
, 1);
762 cursor_set(sel
, sel
->line
->prev
, lastcol
);
763 sel
->lastcol
= lastcol
;
767 size_t view_screenline_down(Selection
*sel
) {
769 return view_line_down(sel
);
770 int lastcol
= sel
->lastcol
;
773 if (!sel
->line
->next
&& sel
->line
== sel
->view
->bottomline
)
774 view_scroll_down(sel
->view
, 1);
776 cursor_set(sel
, sel
->line
->next
, lastcol
);
777 sel
->lastcol
= lastcol
;
781 size_t view_screenline_begin(Selection
*sel
) {
784 return cursor_set(sel
, sel
->line
, 0);
787 size_t view_screenline_middle(Selection
*sel
) {
790 return cursor_set(sel
, sel
->line
, sel
->line
->width
/ 2);
793 size_t view_screenline_end(Selection
*sel
) {
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
) {
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
,
826 [SYNTAX_SYMBOL_EOF
] = UI_OPTION_SYMBOL_EOF
,
829 for (int i
= 0; i
< LENGTH(mapping
); i
++) {
830 view
->symbols
[i
] = (options
& mapping
[i
]) ? &symbols_default
[i
] :
834 if (options
& UI_OPTION_LINE_NUMBERS_ABSOLUTE
)
835 options
&= ~UI_OPTION_LARGE_FILE
;
837 view
->large_file
= (options
& UI_OPTION_LARGE_FILE
);
840 view
->ui
->options_set(view
->ui
, options
);
843 enum UiOption
view_options_get(View
*view
) {
844 return view
->ui
? view
->ui
->options_get(view
->ui
) : 0;
847 void view_colorcolumn_set(View
*view
, int col
) {
849 view
->colorcolumn
= col
;
852 int view_colorcolumn_get(View
*view
) {
853 return view
->colorcolumn
;
856 size_t view_screenline_goto(View
*view
, int n
) {
857 size_t pos
= view
->start
;
858 for (Line
*line
= view
->topline
; --n
> 0 && line
!= view
->lastline
; line
= line
->next
)
863 static Selection
*selections_new(View
*view
, size_t pos
, bool force
) {
864 if (pos
> text_size(view
->text
))
866 Selection
*s
= calloc(1, sizeof(*s
));
870 s
->generation
= view
->selection_generation
;
871 if (!view
->selections
) {
873 view
->selection_latest
= s
;
874 view
->selections
= s
;
875 view
->selection_count
= 1;
879 Selection
*prev
= NULL
, *next
= NULL
;
880 Selection
*latest
= view
->selection_latest
? view
->selection_latest
: view
->selection
;
881 size_t cur
= view_cursors_pos(latest
);
885 } else if (pos
> cur
) {
887 for (next
= prev
->next
; next
; prev
= next
, next
= next
->next
) {
888 cur
= view_cursors_pos(next
);
892 } else if (pos
< cur
) {
894 for (prev
= next
->prev
; prev
; next
= prev
, prev
= prev
->prev
) {
895 cur
= view_cursors_pos(prev
);
901 if (pos
== cur
&& !force
)
904 for (Selection
*after
= next
; after
; after
= after
->next
)
913 s
->number
= prev
->number
+ 1;
915 view
->selections
= s
;
917 view
->selection_latest
= s
;
918 view
->selection_count
++;
919 view_selections_dispose(view
->selection_dead
);
920 view_cursors_to(s
, pos
);
927 Selection
*view_selections_new(View
*view
, size_t pos
) {
928 return selections_new(view
, pos
, false);
931 Selection
*view_selections_new_force(View
*view
, size_t pos
) {
932 return selections_new(view
, pos
, true);
935 int view_selections_count(View
*view
) {
936 return view
->selection_count
;
939 int view_selections_number(Selection
*sel
) {
943 int view_selections_column_count(View
*view
) {
944 Text
*txt
= view
->text
;
945 int cpl_max
= 0, cpl
= 0; /* cursors per line */
946 size_t line_prev
= 0;
947 for (Selection
*sel
= view
->selections
; sel
; sel
= sel
->next
) {
948 size_t pos
= view_cursors_pos(sel
);
949 size_t line
= text_lineno_by_pos(txt
, pos
);
950 if (line
== line_prev
)
961 static Selection
*selections_column_next(View
*view
, Selection
*sel
, int column
) {
964 Text
*txt
= view
->text
;
966 size_t pos
= view_cursors_pos(sel
);
967 line_cur
= text_lineno_by_pos(txt
, pos
);
968 column_cur
= INT_MIN
;
970 sel
= view
->selections
;
973 for (; sel
; sel
= sel
->next
) {
974 size_t pos
= view_cursors_pos(sel
);
975 size_t line
= text_lineno_by_pos(txt
, pos
);
976 if (line
!= line_cur
) {
982 if (column
== column_cur
)
988 Selection
*view_selections_column(View
*view
, int column
) {
989 return selections_column_next(view
, NULL
, column
);
992 Selection
*view_selections_column_next(Selection
*sel
, int column
) {
993 return selections_column_next(sel
->view
, sel
, column
);
996 static void selection_free(Selection
*s
) {
999 for (Selection
*after
= s
->next
; after
; after
= after
->next
)
1002 s
->prev
->next
= s
->next
;
1004 s
->next
->prev
= s
->prev
;
1005 if (s
->view
->selections
== s
)
1006 s
->view
->selections
= s
->next
;
1007 if (s
->view
->selection
== s
)
1008 s
->view
->selection
= s
->next
? s
->next
: s
->prev
;
1009 if (s
->view
->selection_dead
== s
)
1010 s
->view
->selection_dead
= NULL
;
1011 if (s
->view
->selection_latest
== s
)
1012 s
->view
->selection_latest
= s
->prev
? s
->prev
: s
->next
;
1013 s
->view
->selection_count
--;
1017 bool view_selections_dispose(Selection
*sel
) {
1020 View
*view
= sel
->view
;
1021 if (!view
->selections
|| !view
->selections
->next
)
1023 selection_free(sel
);
1024 view_selections_primary_set(view
->selection
);
1028 bool view_selections_dispose_force(Selection
*sel
) {
1029 if (view_selections_dispose(sel
))
1031 View
*view
= sel
->view
;
1032 if (view
->selection_dead
)
1034 view_selection_clear(sel
);
1035 view
->selection_dead
= sel
;
1039 Selection
*view_selection_disposed(View
*view
) {
1040 Selection
*sel
= view
->selection_dead
;
1041 view
->selection_dead
= NULL
;
1045 Selection
*view_selections(View
*view
) {
1046 view
->selection_generation
++;
1047 return view
->selections
;
1050 Selection
*view_selections_primary_get(View
*view
) {
1051 view
->selection_generation
++;
1052 return view
->selection
;
1055 void view_selections_primary_set(Selection
*s
) {
1058 s
->view
->selection
= s
;
1059 Mark anchor
= s
->anchor
;
1060 view_cursors_to(s
, view_cursors_pos(s
));
1064 Selection
*view_selections_prev(Selection
*s
) {
1065 View
*view
= s
->view
;
1066 for (s
= s
->prev
; s
; s
= s
->prev
) {
1067 if (s
->generation
!= view
->selection_generation
)
1070 view
->selection_generation
++;
1074 Selection
*view_selections_next(Selection
*s
) {
1075 View
*view
= s
->view
;
1076 for (s
= s
->next
; s
; s
= s
->next
) {
1077 if (s
->generation
!= view
->selection_generation
)
1080 view
->selection_generation
++;
1084 size_t view_cursors_pos(Selection
*s
) {
1085 return text_mark_get(s
->view
->text
, s
->cursor
);
1088 size_t view_cursors_line(Selection
*s
) {
1089 size_t pos
= view_cursors_pos(s
);
1090 return text_lineno_by_pos(s
->view
->text
, pos
);
1093 size_t view_cursors_col(Selection
*s
) {
1094 size_t pos
= view_cursors_pos(s
);
1095 return text_line_char_get(s
->view
->text
, pos
) + 1;
1098 int view_cursors_cell_get(Selection
*s
) {
1099 return s
->line
? s
->col
: -1;
1102 int view_cursors_cell_set(Selection
*s
, int cell
) {
1103 if (!s
->line
|| cell
< 0)
1105 cursor_set(s
, s
->line
, cell
);
1109 void view_cursors_scroll_to(Selection
*s
, size_t pos
) {
1110 View
*view
= s
->view
;
1111 if (view
->selection
== s
) {
1113 while (pos
< view
->start
&& view_viewport_up(view
, 1));
1114 while (pos
> view
->end
&& view_viewport_down(view
, 1));
1116 view_cursors_to(s
, pos
);
1119 void view_cursors_to(Selection
*s
, size_t pos
) {
1120 View
*view
= s
->view
;
1123 size_t size
= text_size(view
->text
);
1126 if (s
->view
->selection
== s
) {
1127 /* make sure we redraw changes to the very first character of the window */
1128 if (view
->start
== pos
)
1129 view
->start_last
= 0;
1131 if (view
->end
== pos
&& view
->lastline
== view
->bottomline
) {
1132 view
->start
+= view
->topline
->len
;
1136 if (pos
< view
->start
|| pos
> view
->end
) {
1138 view_viewport_up(view
, view
->height
/ 2);
1141 if (pos
<= view
->start
|| pos
> view
->end
) {
1142 view
->start
= text_line_begin(view
->text
, pos
);
1146 if (pos
<= view
->start
|| pos
> view
->end
) {
1155 void view_cursors_place(Selection
*s
, size_t line
, size_t col
) {
1156 Text
*txt
= s
->view
->text
;
1157 size_t pos
= text_pos_by_lineno(txt
, line
);
1158 pos
= text_line_char_set(txt
, pos
, col
> 0 ? col
-1 : col
);
1159 view_cursors_to(s
, pos
);
1162 void view_selections_anchor(Selection
*s
, bool anchored
) {
1163 s
->anchored
= anchored
;
1166 void view_selection_clear(Selection
*s
) {
1167 s
->anchored
= false;
1168 s
->anchor
= s
->cursor
;
1169 s
->view
->need_update
= true;
1172 void view_selections_flip(Selection
*s
) {
1173 Mark temp
= s
->anchor
;
1174 s
->anchor
= s
->cursor
;
1176 view_cursors_to(s
, text_mark_get(s
->view
->text
, s
->cursor
));
1179 bool view_selections_anchored(Selection
*s
) {
1183 void view_selections_clear_all(View
*view
) {
1184 for (Selection
*s
= view
->selections
; s
; s
= s
->next
)
1185 view_selection_clear(s
);
1189 void view_selections_dispose_all(View
*view
) {
1190 for (Selection
*s
= view
->selections
, *next
; s
; s
= next
) {
1192 if (s
!= view
->selection
)
1198 Filerange
view_selection_get(View
*view
) {
1199 return view_selections_get(view
->selection
);
1202 Filerange
view_selections_get(Selection
*s
) {
1204 return text_range_empty();
1205 Text
*txt
= s
->view
->text
;
1206 size_t anchor
= text_mark_get(txt
, s
->anchor
);
1207 size_t cursor
= text_mark_get(txt
, s
->cursor
);
1208 Filerange sel
= text_range_new(anchor
, cursor
);
1209 if (text_range_valid(&sel
))
1210 sel
.end
= text_char_next(txt
, sel
.end
);
1214 bool view_selections_set(Selection
*s
, const Filerange
*r
) {
1215 Text
*txt
= s
->view
->text
;
1216 size_t max
= text_size(txt
);
1217 if (!text_range_valid(r
) || r
->start
>= max
)
1219 size_t anchor
= text_mark_get(txt
, s
->anchor
);
1220 size_t cursor
= text_mark_get(txt
, s
->cursor
);
1221 bool left_extending
= anchor
!= EPOS
&& anchor
> cursor
;
1222 size_t end
= r
->end
> max
? max
: r
->end
;
1223 if (r
->start
!= end
)
1224 end
= text_char_prev(txt
, end
);
1225 view_cursors_to(s
, left_extending
? r
->start
: end
);
1226 s
->anchor
= text_mark_set(txt
, left_extending
? end
: r
->start
);
1230 Filerange
view_regions_restore(View
*view
, SelectionRegion
*s
) {
1231 Text
*txt
= view
->text
;
1232 size_t anchor
= text_mark_get(txt
, s
->anchor
);
1233 size_t cursor
= text_mark_get(txt
, s
->cursor
);
1234 Filerange sel
= text_range_new(anchor
, cursor
);
1235 if (text_range_valid(&sel
))
1236 sel
.end
= text_char_next(txt
, sel
.end
);
1240 bool view_regions_save(View
*view
, Filerange
*r
, SelectionRegion
*s
) {
1241 Text
*txt
= view
->text
;
1242 size_t max
= text_size(txt
);
1243 if (!text_range_valid(r
) || r
->start
>= max
)
1245 size_t end
= r
->end
> max
? max
: r
->end
;
1246 if (r
->start
!= end
)
1247 end
= text_char_prev(txt
, end
);
1248 s
->anchor
= text_mark_set(txt
, r
->start
);
1249 s
->cursor
= text_mark_set(txt
, end
);
1253 void view_selections_set_all(View
*view
, Array
*arr
, bool anchored
) {
1257 for (s
= view
->selections
; s
; s
= s
->next
) {
1258 if (!(r
= array_get(arr
, i
++)) || !view_selections_set(s
, r
)) {
1259 for (Selection
*next
; s
; s
= next
) {
1260 next
= view_selections_next(s
);
1261 if (i
== 1 && s
== view
->selection
)
1262 view_selection_clear(s
);
1264 view_selections_dispose(s
);
1268 s
->anchored
= anchored
;
1270 while ((r
= array_get(arr
, i
++))) {
1271 s
= view_selections_new_force(view
, r
->start
);
1272 if (!s
|| !view_selections_set(s
, r
))
1274 s
->anchored
= anchored
;
1276 view_selections_primary_set(view
->selections
);
1279 Array
view_selections_get_all(View
*view
) {
1281 array_init_sized(&arr
, sizeof(Filerange
));
1282 if (!array_reserve(&arr
, view_selections_count(view
)))
1284 for (Selection
*s
= view
->selections
; s
; s
= s
->next
) {
1285 Filerange r
= view_selections_get(s
);
1286 if (text_range_valid(&r
))
1287 array_add(&arr
, &r
);
1292 void view_selections_normalize(View
*view
) {
1293 Selection
*prev
= NULL
;
1294 Filerange range_prev
= text_range_empty();
1295 for (Selection
*s
= view
->selections
, *next
; s
; s
= next
) {
1297 Filerange range
= view_selections_get(s
);
1298 if (!text_range_valid(&range
)) {
1299 view_selections_dispose(s
);
1300 } else if (prev
&& text_range_overlap(&range_prev
, &range
)) {
1301 range_prev
= text_range_union(&range_prev
, &range
);
1302 view_selections_dispose(s
);
1305 view_selections_set(prev
, &range_prev
);
1311 view_selections_set(prev
, &range_prev
);
1314 Text
*view_text(View
*view
) {
1318 char *view_symbol_eof_get(View
*view
) {
1319 return view
->symbols
[SYNTAX_SYMBOL_EOF
]->symbol
;
1322 bool view_style_define(View
*view
, enum UiStyle id
, const char *style
) {
1323 return view
->ui
->style_define(view
->ui
, id
, style
);
1326 void view_style(View
*view
, enum UiStyle style_id
, size_t start
, size_t end
) {
1327 if (end
< view
->start
|| start
> view
->end
)
1330 CellStyle style
= view
->ui
->style_get(view
->ui
, style_id
);
1331 size_t pos
= view
->start
;
1332 Line
*line
= view
->topline
;
1334 /* skip lines before range to be styled */
1335 while (line
&& pos
+ line
->len
<= start
) {
1343 int col
= 0, width
= view
->width
;
1345 /* skip columns before range to be styled */
1346 while (pos
< start
&& col
< width
)
1347 pos
+= line
->cells
[col
++].len
;
1350 while (pos
<= end
&& col
< width
) {
1351 pos
+= line
->cells
[col
].len
;
1352 line
->cells
[col
++].style
= style
;
1355 } while (pos
<= end
&& (line
= line
->next
));