9 #include "text-motions.h"
10 #include "text-util.h"
20 SYNTAX_SYMBOL_TAB_FILL
,
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:
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.
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 */
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
] = { " " },
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
;
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
;
126 view
->start_mark
= text_mark_set(view
->text
, view
->start
);
129 if (view
->start_mark
== EMARK
)
132 start
= text_mark_get(view
->text
, view
->start_mark
);
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
;
145 for (size_t i
= 0; i
< end
; i
+= line_size
) {
146 Line
*line
= (Line
*)(((char*)view
->lines
) + i
);
152 view
->bottomline
= prev
? prev
: view
->topline
;
153 view
->bottomline
->next
= NULL
;
154 view
->line
= view
->topline
;
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
) {
170 size_t lineno
= view
->line
->lineno
;
171 unsigned char ch
= (unsigned char)cell
->data
[0];
172 cell
->style
= view
->cell_blank
.style
;
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
;
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
;
199 if (view
->col
+ cell
->width
> view
->width
) {
200 view
->line
= view
->line
->next
;
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
;
217 view
->line
->lineno
= lineno
+ 1;
221 if (ch
< 128 && !isprint(ch
)) {
222 /* non-printable ascii char, represent it as ^(char + 64) */
224 .data
= { '^', ch
== 127 ? '?' : ch
+ 64, '\0' },
227 .style
= cell
->style
,
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
;
244 view
->line
->width
+= cell
->width
;
245 view
->line
->len
+= cell
->len
;
246 view
->line
->lineno
= lineno
;
247 view
->line
->cells
[view
->col
] = *cell
;
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
;
258 static void cursor_to(Selection
*s
, size_t pos
) {
259 Text
*txt
= s
->view
->text
;
260 s
->cursor
= text_mark_set(txt
, pos
);
262 s
->anchor
= s
->cursor
;
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
;
274 // TODO: minimize number of redraws
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;
290 while (line
&& line
!= view
->lastline
&& cur
< pos
) {
291 if (cur
+ line
->len
> pos
)
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);
306 line
= view
->bottomline
;
307 row
= view
->height
- 1;
310 if (retline
) *retline
= line
;
311 if (retrow
) *retrow
= row
;
312 if (retcol
) *retcol
= col
;
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
) {
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 */
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 */
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 */
338 /* start from known multibyte state */
339 mbstate_t mbstate
= { 0 };
341 Cell cell
= { .data
= "", .len
= 0, .width
= 0, }, prev_cell
= cell
;
345 /* current 'parsed' character' */
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
);
364 } else if (len
== 0) {
365 /* NUL byte encountered, store it and continue */
366 cell
= (Cell
){ .data
= "\x00", .len
= 1, .width
= 2 };
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';
374 cell
.width
= wcwidth(wchar
);
375 if (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
);
383 if (prev_cell
.len
&& !view_addch(view
, &prev_cell
))
385 pos
+= prev_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 */
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
;
405 view
->lastline
= view
->line
;
407 view
->lastline
= view
->bottomline
;
410 /* clear remaining of line, important to show cursor at end of file */
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
;
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
)
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;
445 bool view_resize(View
*view
, int width
, int height
) {
450 if (view
->width
== width
&& view
->height
== height
) {
451 view
->need_update
= 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
);
460 view
->lines_size
= lines_size
;
463 view
->height
= height
;
464 memset(view
->lines
, 0, view
->lines_size
);
469 int view_height_get(View
*view
) {
473 int view_width_get(View
*view
) {
477 void view_free(View
*view
) {
480 while (view
->selections
)
481 selection_free(view
->selections
);
486 void view_reload(View
*view
, Text
*text
) {
488 view_selections_clear_all(view
);
489 view_cursor_to(view
, 0);
492 View
*view_new(Text
*text
) {
495 View
*view
= calloc(1, sizeof(View
));
499 if (!view_selections_new(view
, 0)) {
504 view
->cell_blank
= (Cell
) {
510 view_options_set(view
, 0);
512 if (!view_resize(view
, 1, 1)) {
517 view_cursor_to(view
, 0);
522 void view_ui(View
*view
, UiWin
* ui
) {
526 static size_t cursor_set(Selection
*sel
, Line
*line
, int col
) {
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
) {
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)
539 /* calculate offset within the line */
540 for (int i
= 0; i
< col
; i
++)
541 pos
+= line
->cells
[i
].len
;
552 static bool view_viewport_down(View
*view
, int n
) {
554 if (view
->end
>= text_size(view
->text
))
556 if (n
>= view
->height
) {
557 view
->start
= view
->end
;
559 for (line
= view
->topline
; line
&& n
> 0; line
= line
->next
, n
--)
560 view
->start
+= line
->len
;
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)
573 size_t max
= view
->width
* view
->height
;
575 Iterator it
= text_iterator_get(view
->text
, view
->start
- 1);
577 if (!text_iterator_byte_get(&it
, &c
))
580 /* skip newlines immediately before display area */
581 if (c
== '\n' && text_iterator_byte_prev(&it
, &c
))
584 if (c
== '\n' && --n
== 0)
588 } while (text_iterator_byte_prev(&it
, &c
));
589 view
->start
-= MIN(view
->start
, off
);
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
;
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
++) {
607 Line
*line
= view
->selection
->line
;
608 for (Line
*cur
= view
->topline
; cur
&& cur
!= line
; cur
= cur
->next
)
610 if (linenr
< center
) {
611 view_slide_down(view
, center
- linenr
);
614 for (Line
*cur
= view
->topline
; cur
&& cur
!= line
&& linenr
> center
; cur
= cur
->next
) {
615 view
->start
+= cur
->len
;
621 view_cursor_to(view
, pos
);
624 void view_redraw_bottom(View
*view
) {
625 Line
*line
= view
->selection
->line
;
626 if (line
== view
->lastline
)
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
,
828 for (int i
= 0; i
< LENGTH(mapping
); i
++) {
829 view
->symbols
[i
] = (options
& mapping
[i
]) ? &symbols_default
[i
] :
833 if (options
& UI_OPTION_LINE_NUMBERS_ABSOLUTE
)
834 options
&= ~UI_OPTION_LARGE_FILE
;
836 view
->large_file
= (options
& UI_OPTION_LARGE_FILE
);
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
) {
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
)
862 static Selection
*selections_new(View
*view
, size_t pos
, bool force
) {
863 if (pos
> text_size(view
->text
))
865 Selection
*s
= calloc(1, sizeof(*s
));
869 s
->generation
= view
->selection_generation
;
870 if (!view
->selections
) {
872 view
->selection_latest
= s
;
873 view
->selections
= s
;
874 view
->selection_count
= 1;
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
);
884 } else if (pos
> cur
) {
886 for (next
= prev
->next
; next
; prev
= next
, next
= next
->next
) {
887 cur
= view_cursors_pos(next
);
891 } else if (pos
< cur
) {
893 for (prev
= next
->prev
; prev
; next
= prev
, prev
= prev
->prev
) {
894 cur
= view_cursors_pos(prev
);
900 if (pos
== cur
&& !force
)
903 for (Selection
*after
= next
; after
; after
= after
->next
)
912 s
->number
= prev
->number
+ 1;
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
);
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
) {
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
)
960 static Selection
*selections_column_next(View
*view
, Selection
*sel
, int column
) {
963 Text
*txt
= view
->text
;
965 size_t pos
= view_cursors_pos(sel
);
966 line_cur
= text_lineno_by_pos(txt
, pos
);
967 column_cur
= INT_MIN
;
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
) {
981 if (column
== column_cur
)
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
) {
998 for (Selection
*after
= s
->next
; after
; after
= after
->next
)
1001 s
->prev
->next
= 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
--;
1016 bool view_selections_dispose(Selection
*sel
) {
1019 View
*view
= sel
->view
;
1020 if (!view
->selections
|| !view
->selections
->next
)
1022 selection_free(sel
);
1023 view_selections_primary_set(view
->selection
);
1027 bool view_selections_dispose_force(Selection
*sel
) {
1028 if (view_selections_dispose(sel
))
1030 View
*view
= sel
->view
;
1031 if (view
->selection_dead
)
1033 view_selection_clear(sel
);
1034 view
->selection_dead
= sel
;
1038 Selection
*view_selection_disposed(View
*view
) {
1039 Selection
*sel
= view
->selection_dead
;
1040 view
->selection_dead
= NULL
;
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
) {
1057 s
->view
->selection
= s
;
1058 Mark anchor
= s
->anchor
;
1059 view_cursors_to(s
, view_cursors_pos(s
));
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
)
1069 view
->selection_generation
++;
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
)
1079 view
->selection_generation
++;
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)
1104 cursor_set(s
, s
->line
, cell
);
1108 void view_cursors_scroll_to(Selection
*s
, size_t pos
) {
1109 View
*view
= s
->view
;
1110 if (view
->selection
== s
) {
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
;
1122 size_t size
= text_size(view
->text
);
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
;
1135 if (pos
< view
->start
|| pos
> view
->end
) {
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
);
1145 if (pos
<= view
->start
|| pos
> view
->end
) {
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
) {
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
;
1175 view_cursors_to(s
, text_mark_get(s
->view
->text
, s
->cursor
));
1178 bool view_selections_anchored(Selection
*s
) {
1182 void view_selections_clear_all(View
*view
) {
1183 for (Selection
*s
= view
->selections
; s
; s
= s
->next
)
1184 view_selection_clear(s
);
1188 void view_selections_dispose_all(View
*view
) {
1189 for (Selection
*s
= view
->selections
, *next
; s
; s
= next
) {
1191 if (s
!= view
->selection
)
1197 Filerange
view_selection_get(View
*view
) {
1198 return view_selections_get(view
->selection
);
1201 Filerange
view_selections_get(Selection
*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
);
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
)
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
);
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
);
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
)
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
);
1257 bool view_selections_restore(Selection
*s
) {
1258 Text
*txt
= s
->view
->text
;
1259 size_t pos
= text_mark_get(txt
, s
->region
.cursor
);
1262 if (s
->region
.anchor
!= s
->region
.cursor
&& text_mark_get(txt
, s
->region
.anchor
) == EPOS
)
1264 s
->cursor
= s
->region
.cursor
;
1265 s
->anchor
= s
->region
.anchor
;
1267 view_cursors_to(s
, pos
);
1271 void view_selections_set_all(View
*view
, Array
*arr
) {
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
);
1284 while ((r
= array_get(arr
, i
++))) {
1285 s
= view_selections_new_force(view
, r
->start
);
1286 if (!s
|| !view_selections_set(s
, r
))
1289 view_selections_primary_set(view
->selections
);
1292 Array
view_selections_get_all(View
*view
) {
1294 array_init_sized(&arr
, sizeof(Filerange
));
1295 if (!array_reserve(&arr
, view_selections_count(view
)))
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
);
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
) {
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
);
1318 view_selections_set(prev
, &range_prev
);
1324 view_selections_set(prev
, &range_prev
);
1327 Text
*view_text(View
*view
) {
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
)
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
) {
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
;
1359 while (pos
<= end
&& col
< width
) {
1360 pos
+= line
->cells
[col
].len
;
1361 line
->cells
[col
++].style
= style
;
1364 } while (pos
<= end
&& (line
= line
->next
));