Highlight search results
[tig.git] / include / tig / view.h
blobded803e02499c4b0056a4fbb3567f05f53b6abab
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 line {
29 enum line_type type;
30 unsigned int lineno:24;
32 /* State flags */
33 unsigned int selected:1;
34 unsigned int dirty:1;
35 unsigned int cleareol:1;
36 unsigned int wrapped:1;
37 unsigned int commit_title:1;
38 unsigned int no_commit_refs:1;
39 unsigned int graph_indent:1;
40 unsigned int search_result:1;
42 void *data; /* User data */
45 enum view_flag {
46 VIEW_NO_FLAGS = 0,
47 VIEW_CUSTOM_STATUS = 1 << 1,
48 VIEW_ADD_DESCRIBE_REF = 1 << 2,
49 VIEW_ADD_PAGER_REFS = 1 << 3,
50 VIEW_OPEN_DIFF = 1 << 4,
51 VIEW_NO_REF = 1 << 5,
52 VIEW_NO_GIT_DIR = 1 << 6,
53 VIEW_DIFF_LIKE = 1 << 7,
54 VIEW_BLAME_LIKE = 1 << 8,
55 VIEW_SEND_CHILD_ENTER = 1 << 9,
56 VIEW_FILE_FILTER = 1 << 10,
57 VIEW_LOG_LIKE = 1 << 11,
58 VIEW_STATUS_LIKE = 1 << 12,
59 VIEW_REFRESH = 1 << 13,
60 VIEW_GREP_LIKE = 1 << 14,
61 VIEW_SORTABLE = 1 << 15,
62 VIEW_FLEX_WIDTH = 1 << 16,
64 VIEW_RESET_DISPLAY = 1 << 31,
67 #define view_has_flags(view, flag) ((view)->ops->flags & (flag))
68 #define view_can_refresh(view) \
69 (view_has_flags(view, VIEW_REFRESH) && !(view)->unrefreshable)
71 struct position {
72 unsigned long offset; /* Offset of the window top */
73 unsigned long col; /* Offset from the window side. */
74 unsigned long lineno; /* Current line number */
77 struct sort_state {
78 struct view_column *current;
79 bool reverse;
82 struct view_column {
83 struct view_column *next;
84 enum view_column_type type;
85 int width;
86 union view_column_options prev_opt;
87 union view_column_options opt;
88 bool hidden;
91 struct view {
92 const char *name; /* View name */
94 struct view_ops *ops; /* View operations */
95 struct argv_env *env; /* View variables. */
97 char ref[SIZEOF_REF]; /* Hovered commit reference */
98 char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */
100 int height, width; /* The width and height of the main window */
101 WINDOW *win; /* The main window */
102 WINDOW *title; /* The title window */
104 struct keymap *keymap; /* What keymap does this view have */
105 struct sort_state sort; /* Sorting information. */
107 /* Navigation */
108 struct position pos; /* Current position. */
109 struct position prev_pos; /* Previous position. */
111 /* View columns rendering state */
112 struct view_column *columns;
114 /* Searching */
115 char grep[SIZEOF_STR]; /* Search string */
116 regex_t *regex; /* Pre-compiled regexp */
117 unsigned int *matched_line;
118 size_t matched_lines;
120 /* If non-NULL, points to the view that opened this view. If this view
121 * is closed tig will switch back to the parent view. */
122 struct view *parent;
123 struct view *prev;
125 /* Buffering */
126 size_t lines; /* Total number of lines */
127 struct line *line; /* Line index */
129 /* Number of lines with custom status, not to be counted in the
130 * view title. */
131 unsigned int custom_lines;
133 /* Drawing */
134 struct line *curline; /* Line currently being drawn. */
135 enum line_type curtype; /* Attribute currently used for drawing. */
136 unsigned long col; /* Column when drawing. */
137 bool has_scrolled; /* View was scrolled. */
138 bool force_redraw; /* Whether to force a redraw after reading. */
140 /* Loading */
141 const char **argv; /* Shell command arguments. */
142 const char *dir; /* Directory from which to execute. */
143 struct io io;
144 struct io *pipe;
145 time_t start_time;
146 time_t update_secs;
147 struct encoding *encoding;
148 bool unrefreshable;
149 struct watch watch;
151 /* Private data */
152 void *private;
155 #define DEFINE_VIEW(name) struct view name ##_view = { #name, &name##_ops, &argv_env }
157 enum open_flags {
158 OPEN_DEFAULT = 0, /* Use default view switching. */
159 OPEN_STDIN = 1, /* Open in pager mode. */
160 OPEN_FORWARD_STDIN = 2, /* Forward stdin to I/O process. */
161 OPEN_SPLIT = 4, /* Split current view. */
162 OPEN_RELOAD = 8, /* Reload view even if it is the current. */
163 OPEN_REFRESH = 16, /* Refresh view using previous command. */
164 OPEN_PREPARED = 32, /* Open already prepared command. */
165 OPEN_EXTRA = 64, /* Open extra data from command. */
166 OPEN_WITH_STDERR = 128, /* Redirect stderr to stdin. */
168 OPEN_PAGER_MODE = OPEN_STDIN | OPEN_FORWARD_STDIN,
169 OPEN_ALWAYS_LOAD = OPEN_RELOAD | OPEN_REFRESH | OPEN_PREPARED | OPEN_EXTRA | OPEN_PAGER_MODE,
172 #define open_in_pager_mode(flags) ((flags) & OPEN_PAGER_MODE)
173 #define open_from_stdin(flags) ((flags) & OPEN_STDIN)
175 struct view_column_data {
176 struct view_column *section;
177 const struct ident *author;
178 const char *commit_title;
179 const struct time *date;
180 const char *file_name;
181 const unsigned long *file_size;
182 const struct graph *graph;
183 const struct graph_canvas *graph_canvas;
184 const char *id;
185 const unsigned long *line_number;
186 const mode_t *mode;
187 const struct ref *ref;
188 const char *reflog;
189 const struct ref *refs;
190 const char *status;
191 const char *text;
194 #define view_column_bit(id) (1 << VIEW_COLUMN_##id)
195 #define view_has_column(view, id) ((view)->ops->column_bits & view_column_bit(id))
197 #define view_column_name(id) enum_name(view_column_type_map->entries[id].name)
199 struct view_ops {
200 /* What type of content being displayed. Used in the title bar. */
201 const char *type;
202 /* Points to either of ref_{head,commit,blob} */
203 const char *id;
204 /* Flags to control the view behavior. */
205 enum view_flag flags;
206 /* Size of private data. */
207 size_t private_size;
208 /* Open and reads in all view content. */
209 bool (*open)(struct view *view, enum open_flags flags);
210 /* Read one line; updates view->line. */
211 bool (*read)(struct view *view, struct buffer *buf);
212 /* Draw one line; @lineno must be < view->height. */
213 bool (*draw)(struct view *view, struct line *line, unsigned int lineno);
214 /* Depending on view handle a special requests. */
215 enum request (*request)(struct view *view, enum request request, struct line *line);
216 /* Search for regexp in a line. */
217 bool (*grep)(struct view *view, struct line *line);
218 /* Select line */
219 void (*select)(struct view *view, struct line *line);
220 /* Release resources when reloading the view */
221 void (*done)(struct view *view);
222 /* Supported view columns. */
223 unsigned long column_bits;
224 /* Extract line information. */
225 bool (*get_column_data)(struct view *view, const struct line *line, struct view_column_data *column_data);
229 * Global view state.
232 struct view *get_view(int index);
234 #define foreach_view(view, i) \
235 for (i = 0; (view = get_view(i)); i++)
237 #define view_has_line(view, line_) \
238 ((view)->line <= (line_) && (line_) < (view)->line + (view)->lines)
241 * Navigation
244 bool goto_view_line(struct view *view, unsigned long offset, unsigned long lineno);
245 void select_view_line(struct view *view, unsigned long lineno);
246 void do_scroll_view(struct view *view, int lines);
247 void scroll_view(struct view *view, enum request request);
248 void move_view(struct view *view, enum request request);
251 * View history
254 struct view_state {
255 struct view_state *prev; /* Entry below this in the stack */
256 struct position position; /* View position to restore */
257 void *data; /* View specific state */
260 struct view_history {
261 size_t state_alloc;
262 struct view_state *stack;
263 struct position position;
266 struct view_state *push_view_history_state(struct view_history *history, struct position *position, void *data);
267 bool pop_view_history_state(struct view_history *history, struct position *position, void *data);
268 void reset_view_history(struct view_history *history);
271 * View opening
274 void split_view(struct view *prev, struct view *view);
275 void maximize_view(struct view *view, bool redraw);
276 void load_view(struct view *view, struct view *prev, enum open_flags flags);
278 #define refresh_view(view) load_view(view, NULL, OPEN_REFRESH)
279 #define reload_view(view) load_view(view, NULL, OPEN_RELOAD)
281 void open_view(struct view *prev, struct view *view, enum open_flags flags);
282 void open_argv(struct view *prev, struct view *view, const char *argv[], const char *dir, enum open_flags flags);
283 bool view_exec(struct view *view, enum open_flags flags);
286 * Various utilities.
289 #define get_sort_field(view) ((view)->sort.current->type)
290 void resort_view(struct view *view, bool renumber);
291 void sort_view(struct view *view, bool change_field);
293 struct view_column *get_view_column(struct view *view, enum view_column_type type);
294 bool view_column_grep(struct view *view, struct line *line);
295 bool view_column_info_changed(struct view *view, bool update);
296 void view_column_reset(struct view *view);
297 bool view_column_info_update(struct view *view, struct line *line);
298 enum status_code parse_view_config(struct view_column **column, const char *view_name, const char *argv[]);
299 enum status_code parse_view_column_config(const char *view_name, enum view_column_type type, const char *option_name, const char *argv[]);
300 enum status_code format_view_config(struct view_column *column, char buf[], size_t bufsize);
301 bool view_has_wrapped_lines(struct view *view);
303 struct line *
304 find_line_by_type(struct view *view, struct line *line, enum line_type type, int direction);
306 #define find_prev_line_by_type(view, line, type) \
307 find_line_by_type(view, line, type, -1)
309 #define find_next_line_by_type(view, line, type) \
310 find_line_by_type(view, line, type, 1)
312 #define is_initial_view(view) (!(view)->prev && !(view)->argv)
313 #define failed_to_load_initial_view(view) (!(view)->prev && !(view)->lines)
315 #define get_view_color(view, type) get_line_color((view)->keymap->name, type)
316 #define get_view_attr(view, type) get_line_attr((view)->keymap->name, type)
319 * Incremental updating
322 static inline bool
323 check_position(struct position *pos)
325 return pos->lineno || pos->col || pos->offset;
328 static inline void
329 clear_position(struct position *pos)
331 memset(pos, 0, sizeof(*pos));
334 void reset_view(struct view *view);
335 bool begin_update(struct view *view, const char *dir, const char **argv, enum open_flags flags);
336 void end_update(struct view *view, bool force);
337 bool update_view(struct view *view);
338 void update_view_title(struct view *view);
341 * Line utilities.
344 struct line *add_line_at(struct view *view, unsigned long pos, const void *data, enum line_type type, size_t data_size, bool custom);
345 struct line *add_line(struct view *view, const void *data, enum line_type type, size_t data_size, bool custom);
346 struct line *add_line_alloc_(struct view *view, void **ptr, enum line_type type, size_t data_size, bool custom);
348 #define add_line_alloc(view, data_ptr, type, extra_size, custom) \
349 add_line_alloc_(view, (void **) data_ptr, type, sizeof(**data_ptr) + extra_size, custom)
351 struct line *add_line_nodata(struct view *view, enum line_type type);
352 struct line *add_line_text(struct view *view, const char *text, enum line_type type);
353 struct line * PRINTF_LIKE(3, 4) add_line_format(struct view *view, enum line_type type, const char *fmt, ...);
354 bool append_line_format(struct view *view, struct line *line, const char *fmt, ...);
356 #endif
357 /* vim: set ts=8 sw=8 noexpandtab: */