Do not exit when the main view is reloaded due to option toggling
[tig.git] / include / tig / view.h
blob2ef4eaa5b53ebeb80ccadf7752ebb275fe6163fe
1 /* Copyright (c) 2006-2015 Jonas Fonseca <jonas.fonseca@gmail.com>
3 * This program is free software; you can redistribute it and/or
4 * modify it under the terms of the GNU General Public License as
5 * published by the Free Software Foundation; either version 2 of
6 * the License, or (at your option) any later version.
8 * This program is distributed in the hope that it will be useful,
9 * but WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11 * GNU General Public License for more details.
14 #ifndef TIG_VIEW_H
15 #define TIG_VIEW_H
17 #include "tig/tig.h"
18 #include "tig/types.h"
19 #include "tig/argv.h"
20 #include "tig/watch.h"
21 #include "tig/io.h"
22 #include "tig/line.h"
23 #include "tig/keys.h"
24 #include "tig/options.h"
26 struct view_ops;
28 struct box_cell {
29 enum line_type type;
30 size_t length;
33 struct box {
34 const char *text;
35 size_t cells;
36 struct box_cell cell[1];
39 struct line {
40 enum line_type type;
41 unsigned int lineno:24;
43 /* State flags */
44 unsigned int selected:1;
45 unsigned int dirty:1;
46 unsigned int cleareol:1;
47 unsigned int wrapped:1;
48 unsigned int commit_title:1;
49 unsigned int no_commit_refs:1;
50 unsigned int graph_indent:1;
51 unsigned int search_result:1;
53 void *data; /* User data */
56 enum view_flag {
57 VIEW_NO_FLAGS = 0,
58 VIEW_CUSTOM_STATUS = 1 << 1,
59 VIEW_ADD_DESCRIBE_REF = 1 << 2,
60 VIEW_ADD_PAGER_REFS = 1 << 3,
61 VIEW_OPEN_DIFF = 1 << 4,
62 VIEW_NO_REF = 1 << 5,
63 VIEW_NO_GIT_DIR = 1 << 6,
64 VIEW_DIFF_LIKE = 1 << 7,
65 VIEW_BLAME_LIKE = 1 << 8,
66 VIEW_SEND_CHILD_ENTER = 1 << 9,
67 VIEW_FILE_FILTER = 1 << 10,
68 VIEW_LOG_LIKE = 1 << 11,
69 VIEW_STATUS_LIKE = 1 << 12,
70 VIEW_REFRESH = 1 << 13,
71 VIEW_GREP_LIKE = 1 << 14,
72 VIEW_SORTABLE = 1 << 15,
73 VIEW_FLEX_WIDTH = 1 << 16,
75 VIEW_RESET_DISPLAY = 1 << 31,
78 #define view_has_flags(view, flag) ((view)->ops->flags & (flag))
79 #define view_can_refresh(view) \
80 (view_has_flags(view, VIEW_REFRESH) && !(view)->unrefreshable)
82 struct position {
83 unsigned long offset; /* Offset of the window top */
84 unsigned long col; /* Offset from the window side. */
85 unsigned long lineno; /* Current line number */
88 struct sort_state {
89 struct view_column *current;
90 bool reverse;
93 struct view_column {
94 struct view_column *next;
95 enum view_column_type type;
96 int width;
97 union view_column_options prev_opt;
98 union view_column_options opt;
99 bool hidden;
102 struct view {
103 const char *name; /* View name */
105 struct view_ops *ops; /* View operations */
106 struct argv_env *env; /* View variables. */
108 char ref[SIZEOF_REF]; /* Hovered commit reference */
109 char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */
111 int height, width; /* The width and height of the main window */
112 WINDOW *win; /* The main window */
113 WINDOW *title; /* The title window */
115 struct keymap *keymap; /* What keymap does this view have */
116 struct sort_state sort; /* Sorting information. */
118 /* Navigation */
119 struct position pos; /* Current position. */
120 struct position prev_pos; /* Previous position. */
122 /* View columns rendering state */
123 struct view_column *columns;
125 /* Searching */
126 char grep[SIZEOF_STR]; /* Search string */
127 regex_t *regex; /* Pre-compiled regexp */
128 unsigned int *matched_line;
129 size_t matched_lines;
131 /* If non-NULL, points to the view that opened this view. If this view
132 * is closed tig will switch back to the parent view. */
133 struct view *parent;
134 struct view *prev;
136 /* Buffering */
137 size_t lines; /* Total number of lines */
138 struct line *line; /* Line index */
140 /* Number of lines with custom status, not to be counted in the
141 * view title. */
142 unsigned int custom_lines;
144 /* Drawing */
145 struct line *curline; /* Line currently being drawn. */
146 enum line_type curtype; /* Attribute currently used for drawing. */
147 unsigned long col; /* Column when drawing. */
148 bool has_scrolled; /* View was scrolled. */
149 bool force_redraw; /* Whether to force a redraw after reading. */
151 /* Loading */
152 const char **argv; /* Shell command arguments. */
153 const char *dir; /* Directory from which to execute. */
154 struct io io;
155 struct io *pipe;
156 time_t start_time;
157 time_t update_secs;
158 struct encoding *encoding;
159 bool unrefreshable;
160 struct watch watch;
162 /* Private data */
163 void *private;
166 #define DEFINE_VIEW(name) struct view name ##_view = { #name, &name##_ops, &argv_env }
168 enum open_flags {
169 OPEN_DEFAULT = 0, /* Use default view switching. */
170 OPEN_STDIN = 1, /* Open in pager mode. */
171 OPEN_FORWARD_STDIN = 2, /* Forward stdin to I/O process. */
172 OPEN_SPLIT = 4, /* Split current view. */
173 OPEN_RELOAD = 8, /* Reload view even if it is the current. */
174 OPEN_REFRESH = 16, /* Refresh view using previous command. */
175 OPEN_PREPARED = 32, /* Open already prepared command. */
176 OPEN_EXTRA = 64, /* Open extra data from command. */
177 OPEN_WITH_STDERR = 128, /* Redirect stderr to stdin. */
179 OPEN_PAGER_MODE = OPEN_STDIN | OPEN_FORWARD_STDIN,
180 OPEN_ALWAYS_LOAD = OPEN_RELOAD | OPEN_REFRESH | OPEN_PREPARED | OPEN_EXTRA | OPEN_PAGER_MODE,
183 #define open_in_pager_mode(flags) ((flags) & OPEN_PAGER_MODE)
184 #define open_from_stdin(flags) ((flags) & OPEN_STDIN)
186 struct view_column_data {
187 struct view_column *section;
188 const struct ident *author;
189 const char *commit_title;
190 const struct time *date;
191 const char *file_name;
192 const unsigned long *file_size;
193 const struct graph *graph;
194 const struct graph_canvas *graph_canvas;
195 const char *id;
196 const unsigned long *line_number;
197 const mode_t *mode;
198 const struct ref *ref;
199 const char *reflog;
200 const struct ref *refs;
201 const char *status;
202 const char *text;
203 const struct box *box;
206 #define view_column_bit(id) (1 << VIEW_COLUMN_##id)
207 #define view_has_column(view, id) ((view)->ops->column_bits & view_column_bit(id))
209 #define view_column_name(id) enum_name(view_column_type_map->entries[id].name)
211 struct view_ops {
212 /* What type of content being displayed. Used in the title bar. */
213 const char *type;
214 /* Points to either of ref_{head,commit,blob} */
215 const char *id;
216 /* Flags to control the view behavior. */
217 enum view_flag flags;
218 /* Size of private data. */
219 size_t private_size;
220 /* Open and reads in all view content. */
221 bool (*open)(struct view *view, enum open_flags flags);
222 /* Read one line; updates view->line. */
223 bool (*read)(struct view *view, struct buffer *buf, bool force_stop);
224 /* Draw one line; @lineno must be < view->height. */
225 bool (*draw)(struct view *view, struct line *line, unsigned int lineno);
226 /* Depending on view handle a special requests. */
227 enum request (*request)(struct view *view, enum request request, struct line *line);
228 /* Search for regexp in a line. */
229 bool (*grep)(struct view *view, struct line *line);
230 /* Select line */
231 void (*select)(struct view *view, struct line *line);
232 /* Release resources when reloading the view */
233 void (*done)(struct view *view);
234 /* Supported view columns. */
235 unsigned long column_bits;
236 /* Extract line information. */
237 bool (*get_column_data)(struct view *view, const struct line *line, struct view_column_data *column_data);
241 * Global view state.
244 struct view *get_view(int index);
246 #define foreach_view(view, i) \
247 for (i = 0; (view = get_view(i)); i++)
249 #define view_has_line(view, line_) \
250 ((view)->line <= (line_) && (line_) < (view)->line + (view)->lines)
253 * Navigation
256 bool goto_view_line(struct view *view, unsigned long offset, unsigned long lineno);
257 void select_view_line(struct view *view, unsigned long lineno);
258 void do_scroll_view(struct view *view, int lines);
259 void scroll_view(struct view *view, enum request request);
260 void move_view(struct view *view, enum request request);
263 * View history
266 struct view_state {
267 struct view_state *prev; /* Entry below this in the stack */
268 struct position position; /* View position to restore */
269 void *data; /* View specific state */
272 struct view_history {
273 size_t state_alloc;
274 struct view_state *stack;
275 struct position position;
278 struct view_state *push_view_history_state(struct view_history *history, struct position *position, void *data);
279 bool pop_view_history_state(struct view_history *history, struct position *position, void *data);
280 void reset_view_history(struct view_history *history);
283 * View opening
286 void split_view(struct view *prev, struct view *view);
287 void maximize_view(struct view *view, bool redraw);
288 void load_view(struct view *view, struct view *prev, enum open_flags flags);
290 #define refresh_view(view) load_view(view, NULL, OPEN_REFRESH)
291 #define reload_view(view) load_view(view, NULL, OPEN_RELOAD)
293 void open_view(struct view *prev, struct view *view, enum open_flags flags);
294 void open_argv(struct view *prev, struct view *view, const char *argv[], const char *dir, enum open_flags flags);
295 bool view_exec(struct view *view, enum open_flags flags);
298 * Various utilities.
301 #define get_sort_field(view) ((view)->sort.current->type)
302 void resort_view(struct view *view, bool renumber);
303 void sort_view(struct view *view, bool change_field);
305 struct view_column *get_view_column(struct view *view, enum view_column_type type);
306 bool view_column_grep(struct view *view, struct line *line);
307 bool view_column_info_changed(struct view *view, bool update);
308 void view_column_reset(struct view *view);
309 bool view_column_info_update(struct view *view, struct line *line);
310 enum status_code parse_view_config(struct view_column **column, const char *view_name, const char *argv[]);
311 enum status_code parse_view_column_config(const char *view_name, enum view_column_type type, const char *option_name, const char *argv[]);
312 enum status_code format_view_config(struct view_column *column, char buf[], size_t bufsize);
313 bool view_has_wrapped_lines(struct view *view);
315 struct line *
316 find_line_by_type(struct view *view, struct line *line, enum line_type type, int direction);
318 #define find_prev_line_by_type(view, line, type) \
319 find_line_by_type(view, line, type, -1)
321 #define find_next_line_by_type(view, line, type) \
322 find_line_by_type(view, line, type, 1)
324 #define is_initial_view(view) (!(view)->prev && !(view)->argv)
325 #define failed_to_load_initial_view(view) (!(view)->prev && !(view)->lines)
327 #define get_view_color(view, type) get_line_color((view)->keymap->name, type)
328 #define get_view_attr(view, type) get_line_attr((view)->keymap->name, type)
331 * Incremental updating
334 static inline bool
335 check_position(struct position *pos)
337 return pos->lineno || pos->col || pos->offset;
340 static inline void
341 clear_position(struct position *pos)
343 memset(pos, 0, sizeof(*pos));
346 void reset_view(struct view *view);
347 bool begin_update(struct view *view, const char *dir, const char **argv, enum open_flags flags);
348 void end_update(struct view *view, bool force);
349 bool update_view(struct view *view);
350 void update_view_title(struct view *view);
353 * Line utilities.
356 static inline const char *
357 box_text(const struct line *line)
359 const struct box *box = line->data;
361 return box->text;
364 static inline size_t
365 box_text_length(const struct box *box)
367 size_t i, length = 0;
369 for (i = 0; i < box->cells; i++)
370 length += box->cell[i].length;
372 return length;
375 static inline size_t
376 box_sizeof(const struct box *box, size_t extra_cells, size_t extra_textlen)
378 size_t textlen = (box ? box_text_length(box) : 0) + extra_textlen;
379 size_t cells = (box ? box->cells : 0) + extra_cells;
380 size_t cells_size = cells > 1 ? sizeof(box->cell) * (cells - 1) : 0;
382 return sizeof(*box) + cells_size + textlen + 1;
385 void box_text_copy(struct box *box, size_t cells, const char *src, size_t srclen);
387 struct line *add_line_at(struct view *view, unsigned long pos, const void *data, enum line_type type, size_t data_size, bool custom);
388 struct line *add_line(struct view *view, const void *data, enum line_type type, size_t data_size, bool custom);
389 struct line *add_line_alloc_(struct view *view, void **ptr, enum line_type type, size_t data_size, bool custom);
391 #define add_line_alloc(view, data_ptr, type, extra_size, custom) \
392 add_line_alloc_(view, (void **) data_ptr, type, sizeof(**data_ptr) + extra_size, custom)
394 struct line *add_line_nodata(struct view *view, enum line_type type);
395 struct line *add_line_text(struct view *view, const char *text, enum line_type type);
396 struct line *add_line_text_at(struct view *view, unsigned long pos, const char *text, enum line_type type, size_t cells);
397 struct line * PRINTF_LIKE(3, 4) add_line_format(struct view *view, enum line_type type, const char *fmt, ...);
398 bool append_line_format(struct view *view, struct line *line, const char *fmt, ...);
400 #endif
401 /* vim: set ts=8 sw=8 noexpandtab: */