vis: more comments and cleanups
[vis.git] / view.h
blob70b64d6ec929adf14d1b6813e2ab3d44ae651989
1 #ifndef VIEW_H
2 #define VIEW_H
4 #include <stddef.h>
5 #include <stdbool.h>
6 #include "register.h"
7 #include "text.h"
8 #include "ui.h"
9 #include "syntax.h"
11 typedef struct View View;
12 typedef struct Cursor Cursor;
13 typedef struct Selection Selection;
15 typedef struct {
16 void *data;
17 void (*selection)(void *data, Filerange*);
18 } ViewEvent;
20 typedef struct {
21 int width; /* display width i.e. number of columns ocupied by this character */
22 size_t len; /* number of bytes the character displayed in this cell uses, for
23 character which use more than 1 column to display, their lenght
24 is stored in the leftmost cell wheras all following cells
25 occupied by the same character have a length of 0. */
26 char data[8]; /* utf8 encoded character displayed in this cell might not be the
27 the same as in the underlying text, for example tabs get expanded */
28 unsigned int attr;
29 bool istab;
30 bool selected; /* whether this cell is part of a selected region */
31 bool cursor; /* whether a cursor is currently locaated on the cell */
32 } Cell;
34 typedef struct Line Line;
35 struct Line { /* a line on the screen, *not* in the file */
36 Line *prev, *next; /* pointer to neighbouring screen lines */
37 size_t len; /* line length in terms of bytes */
38 size_t lineno; /* line number from start of file */
39 int width; /* zero based position of last used column cell */
40 Cell cells[]; /* win->width cells storing information about the displayed characters */
43 typedef struct {
44 size_t line;
45 size_t col;
46 } CursorPos;
48 View *view_new(Text*, ViewEvent*);
49 void view_ui(View*, UiWin*);
50 /* change associated text displayed in this window */
51 void view_reload(View*, Text*);
52 void view_free(View*);
54 bool view_resize(View*, int width, int height);
55 int view_height_get(View*);
56 int view_width_get(View*);
57 void view_draw(View*);
58 /* changes how many spaces are used for one tab (must be >0), redraws the window */
59 void view_tabwidth_set(View*, int tabwidth);
61 /* cursor movements which also update selection if one is active.
62 * they return new cursor postion */
63 size_t view_line_down(Cursor*);
64 size_t view_line_up(Cursor*);
65 size_t view_screenline_down(Cursor*);
66 size_t view_screenline_up(Cursor*);
67 size_t view_screenline_begin(Cursor*);
68 size_t view_screenline_middle(Cursor*);
69 size_t view_screenline_end(Cursor*);
70 /* move window content up/down, but keep cursor position unchanged unless it is
71 * on a now invisible line in which case we try to preserve the column position */
72 size_t view_slide_up(View*, int lines);
73 size_t view_slide_down(View*, int lines);
74 /* scroll window contents up/down by lines, place the cursor on the newly
75 * visible line, try to preserve the column position */
76 size_t view_scroll_up(View*, int lines);
77 size_t view_scroll_down(View*, int lines);
78 /* place the cursor at the start ot the n-th window line, counting from 1 */
79 size_t view_screenline_goto(View*, int n);
81 const Line *view_lines_get(View*);
82 /* redraw current cursor line at top/center/bottom of window */
83 void view_redraw_top(View*);
84 void view_redraw_center(View*);
85 void view_redraw_bottom(View*);
86 /* get the currently displayed area in bytes from the start of the file */
87 Filerange view_viewport_get(View*);
88 /* move visible viewport n-lines up/down, redraws the view but does not change
89 * cursor position which becomes invalid and should be corrected by calling
90 * view_cursor_to. the return value indicates wether the visible area changed.
92 bool view_viewport_up(View *view, int n);
93 bool view_viewport_down(View *view, int n);
94 /* associate a set of syntax highlighting rules to this window. */
95 void view_syntax_set(View*, Syntax*);
96 Syntax *view_syntax_get(View*);
98 void view_options_set(View*, enum UiOption options);
99 enum UiOption view_options_get(View*);
101 /* A view can manage multiple cursors, one of which (the main cursor) is always
102 * placed within the visible viewport. All functions named view_cursor_* operate
103 * on this cursor. Additional cursor can be created and manipulated using the
104 * functions named view_cursors_* */
106 /* get main cursor position in terms of screen coordinates */
107 CursorPos view_cursor_getpos(View*);
108 /* get main cursor position in bytes from start of the file */
109 size_t view_cursor_get(View*);
110 /* moves window viewport in direction until pos is visible. should only be
111 * used for short distances between current cursor position and destination */
112 void view_scroll_to(View*, size_t pos);
113 /* move cursor to a given position. changes the viewport to make sure that
114 * position is visible. if the position is in the middle of a line, try to
115 * adjust the viewport in such a way that the whole line is displayed */
116 void view_cursor_to(View*, size_t pos);
117 /* create a new cursor */
118 Cursor *view_cursors_new(View*);
119 /* get number of active cursors */
120 int view_cursors_count(View*);
121 /* dispose an existing cursor with its associated selection (if any),
122 * not applicaple for the last existing cursor */
123 void view_cursors_dispose(Cursor*);
124 /* only keep the main cursor, release all others together with their
125 * selections (if any) */
126 void view_cursors_clear(View*);
127 /* get the main cursor which is always in the visible viewport */
128 Cursor *view_cursor(View*);
129 /* get the first cursor */
130 Cursor *view_cursors(View*);
131 Cursor *view_cursors_prev(Cursor*);
132 Cursor *view_cursors_next(Cursor*);
133 /* get current position of cursor in bytes from the start of the file */
134 size_t view_cursors_pos(Cursor*);
135 /* place cursor at `pos' which should be in the interval [0, text-size] */
136 void view_cursors_to(Cursor*, size_t pos);
137 void view_cursors_scroll_to(Cursor*, size_t pos);
138 /* get register associated with this register */
139 Register *view_cursors_register(Cursor*);
140 /* start selected area at current cursor position. further cursor movements
141 * will affect the selected region. */
142 void view_cursors_selection_start(Cursor*);
143 /* detach cursor from selection, further cursor movements will not affect
144 * the selected region. */
145 void view_cursors_selection_stop(Cursor*);
146 /* clear selection associated with this cursor (if any) */
147 void view_cursors_selection_clear(Cursor*);
148 /* move cursor position from one end of the selection to the other */
149 void view_cursors_selection_swap(Cursor*);
150 /* move cursor to the end/boundary of the associated selection */
151 void view_cursors_selection_sync(Cursor*);
152 /* restore previous used selection of this cursor */
153 void view_cursors_selection_restore(Cursor*);
154 /* get/set the selected region associated with this cursor */
155 Filerange view_cursors_selection_get(Cursor*);
156 void view_cursors_selection_set(Cursor*, Filerange*);
158 Selection *view_selections_new(View*);
159 void view_selections_free(Selection*);
160 void view_selections_clear(View*);
161 void view_selections_swap(Selection*);
162 Selection *view_selections(View*);
163 Selection *view_selections_prev(Selection*);
164 Selection *view_selections_next(Selection*);
165 Filerange view_selections_get(Selection*);
166 void view_selections_set(Selection*, Filerange*);
168 #endif