1 /* Copyright (c) 2006-2014 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.
18 #include "tig/types.h"
23 #include "tig/options.h"
29 unsigned int lineno
:24;
32 unsigned int selected
:1;
34 unsigned int cleareol
:1;
35 unsigned int wrapped
:1;
36 unsigned int commit_title
:1;
37 unsigned int no_commit_refs
:1;
38 unsigned int graph_indent
:1;
40 void *data
; /* User data */
45 VIEW_CUSTOM_STATUS
= 1 << 1,
46 VIEW_ADD_DESCRIBE_REF
= 1 << 2,
47 VIEW_ADD_PAGER_REFS
= 1 << 3,
48 VIEW_OPEN_DIFF
= 1 << 4,
50 VIEW_NO_GIT_DIR
= 1 << 6,
51 VIEW_DIFF_LIKE
= 1 << 7,
52 VIEW_BLAME_LIKE
= 1 << 8,
53 VIEW_SEND_CHILD_ENTER
= 1 << 9,
54 VIEW_FILE_FILTER
= 1 << 10,
55 VIEW_LOG_LIKE
= 1 << 11,
56 VIEW_STATUS_LIKE
= 1 << 12,
57 VIEW_REFRESH
= 1 << 13,
58 VIEW_GREP_LIKE
= 1 << 14,
59 VIEW_SORTABLE
= 1 << 15,
61 VIEW_RESET_DISPLAY
= 1 << 31,
64 #define view_has_flags(view, flag) ((view)->ops->flags & (flag))
67 unsigned long offset
; /* Offset of the window top */
68 unsigned long col
; /* Offset from the window side. */
69 unsigned long lineno
; /* Current line number */
73 struct view_column
*current
;
78 struct view_column
*next
;
79 enum view_column_type type
;
81 union view_column_options prev_opt
;
82 union view_column_options opt
;
87 const char *name
; /* View name */
89 struct view_ops
*ops
; /* View operations */
90 struct argv_env
*env
; /* View variables. */
92 char ref
[SIZEOF_REF
]; /* Hovered commit reference */
93 char vid
[SIZEOF_REF
]; /* View ID. Set to id member when updating. */
95 int height
, width
; /* The width and height of the main window */
96 WINDOW
*win
; /* The main window */
97 WINDOW
*title
; /* The title window */
99 struct keymap
*keymap
; /* What keymap does this view have */
100 struct sort_state sort
; /* Sorting information. */
103 struct position pos
; /* Current position. */
104 struct position prev_pos
; /* Previous position. */
106 /* View columns rendering state */
107 struct view_column
*columns
;
110 char grep
[SIZEOF_STR
]; /* Search string */
111 regex_t
*regex
; /* Pre-compiled regexp */
112 unsigned int *matched_line
;
113 size_t matched_lines
;
115 /* If non-NULL, points to the view that opened this view. If this view
116 * is closed tig will switch back to the parent view. */
121 size_t lines
; /* Total number of lines */
122 struct line
*line
; /* Line index */
124 /* Number of lines with custom status, not to be counted in the
126 unsigned int custom_lines
;
129 struct line
*curline
; /* Line currently being drawn. */
130 enum line_type curtype
; /* Attribute currently used for drawing. */
131 unsigned long col
; /* Column when drawing. */
132 bool has_scrolled
; /* View was scrolled. */
133 bool force_redraw
; /* Whether to force a redraw after reading. */
136 const char **argv
; /* Shell command arguments. */
137 const char *dir
; /* Directory from which to execute. */
142 struct encoding
*encoding
;
149 #define DEFINE_VIEW(name) struct view name ##_view = { #name, &name##_ops, &argv_env }
152 OPEN_DEFAULT
= 0, /* Use default view switching. */
153 OPEN_STDIN
= 1, /* Open in pager mode. */
154 OPEN_FORWARD_STDIN
= 2, /* Forward stdin to I/O process. */
155 OPEN_SPLIT
= 4, /* Split current view. */
156 OPEN_RELOAD
= 8, /* Reload view even if it is the current. */
157 OPEN_REFRESH
= 16, /* Refresh view using previous command. */
158 OPEN_PREPARED
= 32, /* Open already prepared command. */
159 OPEN_EXTRA
= 64, /* Open extra data from command. */
160 OPEN_WITH_STDERR
= 128, /* Redirect stderr to stdin. */
162 OPEN_PAGER_MODE
= OPEN_STDIN
| OPEN_FORWARD_STDIN
,
163 OPEN_ALWAYS_LOAD
= OPEN_RELOAD
| OPEN_REFRESH
| OPEN_PREPARED
| OPEN_EXTRA
| OPEN_PAGER_MODE
,
166 #define open_in_pager_mode(flags) ((flags) & OPEN_PAGER_MODE)
167 #define open_from_stdin(flags) ((flags) & OPEN_STDIN)
169 struct view_column_data
{
170 struct view_column
*section
;
171 const struct ident
*author
;
172 const char *commit_title
;
173 const struct time
*date
;
174 const char *file_name
;
175 const unsigned long *file_size
;
176 const struct graph_canvas
*graph
;
178 const unsigned long *line_number
;
180 const struct ref
*ref
;
182 const struct ref_list
*refs
;
187 #define view_column_bit(id) (1 << VIEW_COLUMN_##id)
189 #define view_column_name(id) enum_name(view_column_type_map->entries[id].name)
192 /* What type of content being displayed. Used in the title bar. */
194 /* Points to either of ref_{head,commit,blob} */
196 /* Flags to control the view behavior. */
197 enum view_flag flags
;
198 /* Size of private data. */
200 /* Open and reads in all view content. */
201 bool (*open
)(struct view
*view
, enum open_flags flags
);
202 /* Read one line; updates view->line. */
203 bool (*read
)(struct view
*view
, char *data
);
204 /* Draw one line; @lineno must be < view->height. */
205 bool (*draw
)(struct view
*view
, struct line
*line
, unsigned int lineno
);
206 /* Depending on view handle a special requests. */
207 enum request (*request
)(struct view
*view
, enum request request
, struct line
*line
);
208 /* Search for regexp in a line. */
209 bool (*grep
)(struct view
*view
, struct line
*line
);
211 void (*select
)(struct view
*view
, struct line
*line
);
212 /* Release resources when reloading the view */
213 void (*done
)(struct view
*view
);
214 /* Supported view columns. */
215 unsigned long column_bits
;
216 /* Extract line information. */
217 bool (*get_column_data
)(struct view
*view
, const struct line
*line
, struct view_column_data
*column_data
);
224 struct view
*get_view(int index
);
226 #define foreach_view(view, i) \
227 for (i = 0; (view = get_view(i)); i++)
229 #define view_has_line(view, line_) \
230 ((view)->line <= (line_) && (line_) < (view)->line + (view)->lines)
236 bool goto_view_line(struct view
*view
, unsigned long offset
, unsigned long lineno
);
237 void select_view_line(struct view
*view
, unsigned long lineno
);
238 void do_scroll_view(struct view
*view
, int lines
);
239 void scroll_view(struct view
*view
, enum request request
);
240 void move_view(struct view
*view
, enum request request
);
246 void search_view(struct view
*view
, enum request request
);
247 void find_next(struct view
*view
, enum request request
);
248 bool grep_text(struct view
*view
, const char *text
[]);
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
{
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
);
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
);
288 #define get_sort_field(view) ((view)->sort.current->type)
289 void sort_view(struct view
*view
, bool change_field
);
291 struct view_column
*get_view_column(struct view
*view
, enum view_column_type type
);
292 bool view_column_grep(struct view
*view
, struct line
*line
);
293 bool view_column_info_changed(struct view
*view
, bool update
);
294 void view_column_reset(struct view
*view
);
295 bool view_column_info_update(struct view
*view
, struct line
*line
);
296 enum status_code
parse_view_config(const char *view_name
, const char *argv
[]);
299 find_line_by_type(struct view
*view
, struct line
*line
, enum line_type type
, int direction
);
301 #define find_prev_line_by_type(view, line, type) \
302 find_line_by_type(view, line, type, -1)
304 #define find_next_line_by_type(view, line, type) \
305 find_line_by_type(view, line, type, 1)
307 #define is_initial_view(view) (!(view)->prev && !(view)->argv)
308 #define failed_to_load_initial_view(view) (!(view)->prev && !(view)->lines)
310 #define get_view_color(view, type) get_line_color((view)->keymap->name, type)
311 #define get_view_attr(view, type) get_line_attr((view)->keymap->name, type)
314 * Incremental updating
318 check_position(struct position
*pos
)
320 return pos
->lineno
|| pos
->col
|| pos
->offset
;
324 clear_position(struct position
*pos
)
326 memset(pos
, 0, sizeof(*pos
));
329 void reset_view(struct view
*view
);
330 bool begin_update(struct view
*view
, const char *dir
, const char **argv
, enum open_flags flags
);
331 void end_update(struct view
*view
, bool force
);
332 bool update_view(struct view
*view
);
333 void update_view_title(struct view
*view
);
339 struct line
*add_line_at(struct view
*view
, unsigned long pos
, const void *data
, enum line_type type
, size_t data_size
, bool custom
);
340 struct line
*add_line(struct view
*view
, const void *data
, enum line_type type
, size_t data_size
, bool custom
);
341 struct line
*add_line_alloc_(struct view
*view
, void **ptr
, enum line_type type
, size_t data_size
, bool custom
);
343 #define add_line_alloc(view, data_ptr, type, extra_size, custom) \
344 add_line_alloc_(view, (void **) data_ptr, type, sizeof(**data_ptr) + extra_size, custom)
346 struct line
*add_line_nodata(struct view
*view
, enum line_type type
);
347 struct line
*add_line_text(struct view
*view
, const char *text
, enum line_type type
);
348 struct line
* PRINTF_LIKE(3, 4) add_line_format(struct view
*view
, enum line_type type
, const char *fmt
, ...);
351 /* vim: set ts=8 sw=8 noexpandtab: */