vis: use localtime_r(3) instead of localtime(3)
[vis.git] / view.h
blobe30946e0745111332af0d5120b12d0823d64f778
1 #ifndef VIEW_H
2 #define VIEW_H
4 #include <stddef.h>
5 #include <stdbool.h>
7 typedef struct View View;
8 typedef struct Selection Selection;
10 #include "text.h"
11 #include "ui.h"
12 #include "array.h"
14 typedef struct {
15 Mark anchor;
16 Mark cursor;
17 } SelectionRegion;
19 typedef struct {
20 char data[16]; /* utf8 encoded character displayed in this cell (might be more than
21 one Unicode codepoint. might also not be the same as in the
22 underlying text, for example tabs get expanded */
23 size_t len; /* number of bytes the character displayed in this cell uses, for
24 characters which use more than 1 column to display, their length
25 is stored in the leftmost cell whereas all following cells
26 occupied by the same character have a length of 0. */
27 int width; /* display width i.e. number of columns occupied by this character */
28 CellStyle style; /* colors and attributes used to display this cell */
29 } Cell;
31 typedef struct Line Line;
32 struct Line { /* a line on the screen, *not* in the file */
33 Line *prev, *next; /* pointer to neighbouring screen lines */
34 size_t len; /* line length in terms of bytes */
35 size_t lineno; /* line number from start of file */
36 int width; /* zero based position of last used column cell */
37 Cell cells[]; /* win->width cells storing information about the displayed characters */
40 /**
41 * @defgroup view_life
42 * @{
44 View *view_new(Text*);
45 void view_free(View*);
46 void view_ui(View*, UiWin*);
47 Text *view_text(View*);
48 void view_reload(View*, Text*);
49 /**
50 * @}
51 * @defgroup view_viewport
52 * @{
54 /** Get the currently displayed text range. */
55 Filerange view_viewport_get(View*);
56 /**
57 * Get window coordinate of text position.
58 * @param pos The position to query.
59 * @param line Will be updated with screen line on which ``pos`` resides.
60 * @param row Will be updaded with zero based window row on which ``pos`` resides.
61 * @param col Will be updated with zero based window column which ``pos`` resides.
62 * @return Whether ``pos`` is visible. If not, the pointer arguments are left unmodified.
64 bool view_coord_get(View*, size_t pos, Line **line, int *row, int *col);
65 /** Get position at the start ot the ``n``-th window line, counting from 1. */
66 size_t view_screenline_goto(View*, int n);
67 /** Get first screen line. */
68 Line *view_lines_first(View*);
69 /** Get last non-empty screen line. */
70 Line *view_lines_last(View*);
71 size_t view_slide_up(View*, int lines);
72 size_t view_slide_down(View*, int lines);
73 size_t view_scroll_up(View*, int lines);
74 size_t view_scroll_down(View*, int lines);
75 size_t view_scroll_page_up(View*);
76 size_t view_scroll_page_down(View*);
77 size_t view_scroll_halfpage_up(View*);
78 size_t view_scroll_halfpage_down(View*);
79 void view_redraw_top(View*);
80 void view_redraw_center(View*);
81 void view_redraw_bottom(View*);
82 void view_scroll_to(View*, size_t pos);
83 /**
84 * @}
85 * @defgroup view_size
86 * @{
88 bool view_resize(View*, int width, int height);
89 int view_height_get(View*);
90 int view_width_get(View*);
91 /**
92 * @}
93 * @defgroup view_draw
94 * @{
96 void view_invalidate(View*);
97 void view_draw(View*);
98 bool view_update(View*);
101 * @}
102 * @defgroup view_selnew
103 * @{
106 * Create a new singleton selection at the given position.
107 * @rst
108 * .. note:: New selections are created non-anchored.
109 * .. warning:: Fails if position is already covered by a selection.
110 * @endrst
112 Selection *view_selections_new(View*, size_t pos);
114 * Create a new selection even if position is already covered by an
115 * existing selection.
116 * @rst
117 * .. note:: This should only be used if the old selection is eventually
118 * disposed.
119 * @endrst
121 Selection *view_selections_new_force(View*, size_t pos);
123 * Dispose an existing selection.
124 * @rst
125 * .. warning:: Not applicaple for the last existing selection.
126 * @endrst
128 bool view_selections_dispose(Selection*);
130 * Forcefully dispose an existing selection.
132 * If called for the last existing selection, it will be reduced and
133 * marked for destruction. As soon as a new selection is created this one
134 * will be disposed.
136 bool view_selections_dispose_force(Selection*);
138 * Query state of primary selection.
140 * If the primary selection was marked for destruction, return it and
141 * clear descruction flag.
143 Selection *view_selection_disposed(View*);
144 /** Dispose all but the primary selection. */
145 void view_selections_dispose_all(View*);
146 /** Dispose all invalid and merge all overlapping selections. */
147 void view_selections_normalize(View*);
149 * Replace currently active selections.
150 * @param array The array of ``Filerange`` objects.
151 * @param anchored Whether *all* selection should be anchored.
153 void view_selections_set_all(View*, Array*, bool anchored);
154 /** Get array containing a ``Fileranges`` for each selection. */
155 Array view_selections_get_all(View*);
157 * @}
158 * @defgroup view_navigate
159 * @{
161 Selection *view_selections_primary_get(View*);
162 void view_selections_primary_set(Selection*);
163 /** Get first selection. */
164 Selection *view_selections(View*);
165 /** Get immediate predecessor of selection. */
166 Selection *view_selections_prev(Selection*);
167 /** Get immediate successor of selection. */
168 Selection *view_selections_next(Selection*);
170 * Get number of existing selections.
171 * @rst
172 * .. note:: Is always at least 1.
173 * @endrst
175 int view_selections_count(View*);
177 * Get selection index.
178 * @rst
179 * .. note:: Is always in range ``[0, count-1]``.
180 * .. warning: The relative order is determined during creation and assumed
181 * to remain the same.
182 * @endrst
184 int view_selections_number(Selection*);
185 /** Get maximal number of selections on a single line. */
186 int view_selections_column_count(View*);
188 * Starting from the start of the text, get the `column`-th selection on a line.
189 * @param column The zero based column index.
191 Selection *view_selections_column(View*, int column);
193 * Get the next `column`-th selection on a line.
194 * @param column The zero based column index.
196 Selection *view_selections_column_next(Selection*, int column);
198 * @}
199 * @defgroup view_cover
200 * @{
202 /** Get an inclusive range of the selection cover. */
203 Filerange view_selections_get(Selection*);
204 /** Set selection cover. Updates both cursor and anchor. */
205 bool view_selections_set(Selection*, const Filerange*);
207 * Reduce selection to character currently covered by the cursor.
208 * @rst
209 * .. note:: Sets selection to non-anchored mode.
210 * @endrst
212 void view_selection_clear(Selection*);
213 /** Reduce *all* currently active selections. */
214 void view_selections_clear_all(View*);
216 * Flip selection orientation. Swap cursor and anchor.
217 * @rst
218 * .. note:: Has no effect on singleton selections.
219 * @endrst
221 void view_selections_flip(Selection*);
223 * @}
224 * @defgroup view_anchor
225 * @{
228 * Anchor selection.
229 * Further updates will only update the cursor, the anchor will remain fixed.
231 void view_selections_anchor(Selection*, bool anchored);
232 /** Check whether selection is anchored. */
233 bool view_selections_anchored(Selection*);
234 /** Get position of selection cursor. */
236 * @}
237 * @defgroup view_props
238 * @{
240 /** Get position of selection cursor. */
241 size_t view_cursors_pos(Selection*);
242 /** Get 1-based line number of selection cursor. */
243 size_t view_cursors_line(Selection*);
245 * Get 1-based column of selection cursor.
246 * @rst
247 * .. note:: Counts the number of graphemes on the logical line up to the cursor
248 * position.
249 * @endrst
251 size_t view_cursors_col(Selection*);
253 * Get screen line of selection cursor.
254 * @rst
255 * .. warning: Is `NULL` for non-visible selections.
256 * @endrst
258 Line *view_cursors_line_get(Selection*);
260 * Get zero based index of screen cell on which selection cursor currently resides.
261 * @rst
262 * .. warning:: Returns ``-1`` if the selection cursor is currently not visible.
263 * @endrst
265 int view_cursors_cell_get(Selection*);
267 * @}
268 * @defgroup view_place
269 * @{
272 * Place cursor of selection at `pos`.
273 * @rst
274 * .. note:: If the selection is not anchored, both selection endpoints
275 * will be adjusted to form a singleton selection covering one
276 * character starting at `pos`. Otherwise only the selection
277 * cursor will be changed while the anchor remains fixed.
278 * @endrst
280 void view_cursors_to(Selection*, size_t pos);
282 * Adjusts window viewport until the requested position becomes visible.
283 * @rst
284 * .. note:: For all but the primary selection this is equivalent to
285 * ``view_selection_to``.
286 * .. warning:: Repeatedly redraws the window content. Should only be used for
287 * short distances between current cursor position and destination.
288 * @endrst
290 void view_cursors_scroll_to(Selection*, size_t pos);
292 * Place cursor on given (line, column) pair.
293 * @param line the 1-based line number
294 * @param col the 1 based column
295 * @rst
296 * .. note:: Except for the different addressing format this is equivalent to
297 * `view_selection_to`.
298 * @endrst
300 void view_cursors_place(Selection*, size_t line, size_t col);
302 * Place selection cursor on zero based window cell index.
303 * @rst
304 * .. warning:: Fails if the selection cursor is currently not visible.
305 * @endrst
307 int view_cursors_cell_set(Selection*, int cell);
309 * @}
310 * @defgroup view_motions
311 * @{
313 size_t view_line_down(Selection*);
314 size_t view_line_up(Selection*);
315 size_t view_screenline_down(Selection*);
316 size_t view_screenline_up(Selection*);
317 size_t view_screenline_begin(Selection*);
318 size_t view_screenline_middle(Selection*);
319 size_t view_screenline_end(Selection*);
321 * @}
322 * @defgroup view_primary
323 * @{
326 * Move primary selection cursor to the given position.
327 * Makes sure that position is visisble.
328 * @rst
329 * .. note:: If position was not visible before, we attempt to show
330 * surrounding context. The viewport will be adjusted such
331 * that the line holding the cursor is shown in the middle
332 * of the window.
333 * @endrst
335 void view_cursor_to(View*, size_t pos);
336 /** Get cursor position of primary selection. */
337 size_t view_cursor_get(View*);
339 * Get primary selection.
340 * @rst
341 * .. note:: Is always a non-empty range.
342 * @endrst
344 Filerange view_selection_get(View*);
346 * @}
347 * @defgroup view_save
348 * @{
350 Filerange view_regions_restore(View*, SelectionRegion*);
351 bool view_regions_save(View*, Filerange*, SelectionRegion*);
353 * @}
354 * @defgroup view_style
355 * @{
357 void view_options_set(View*, enum UiOption options);
358 enum UiOption view_options_get(View*);
359 void view_colorcolumn_set(View*, int col);
360 int view_colorcolumn_get(View*);
362 /** Set how many spaces are used to display a tab `\t` character. */
363 void view_tabwidth_set(View*, int tabwidth);
364 /** Define a display style. */
365 bool view_style_define(View*, enum UiStyle, const char *style);
366 /** Apply a style to a text range. */
367 void view_style(View*, enum UiStyle, size_t start, size_t end);
369 char *view_symbol_eof_get(View*);
371 /** @} */
373 #endif