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;
39 void *data
; /* User data */
44 VIEW_CUSTOM_STATUS
= 1 << 1,
45 VIEW_ADD_DESCRIBE_REF
= 1 << 2,
46 VIEW_ADD_PAGER_REFS
= 1 << 3,
47 VIEW_OPEN_DIFF
= 1 << 4,
49 VIEW_NO_GIT_DIR
= 1 << 6,
50 VIEW_DIFF_LIKE
= 1 << 7,
51 VIEW_BLAME_LIKE
= 1 << 8,
52 VIEW_SEND_CHILD_ENTER
= 1 << 9,
53 VIEW_FILE_FILTER
= 1 << 10,
54 VIEW_LOG_LIKE
= 1 << 11,
55 VIEW_STATUS_LIKE
= 1 << 12,
56 VIEW_REFRESH
= 1 << 13,
57 VIEW_GREP_LIKE
= 1 << 14,
58 VIEW_SORTABLE
= 1 << 15,
60 VIEW_RESET_DISPLAY
= 1 << 31,
63 #define view_has_flags(view, flag) ((view)->ops->flags & (flag))
66 unsigned long offset
; /* Offset of the window top */
67 unsigned long col
; /* Offset from the window side. */
68 unsigned long lineno
; /* Current line number */
72 struct view_column
*current
;
77 struct view_column
*next
;
78 enum view_column_type type
;
80 union view_column_options prev_opt
;
81 union view_column_options opt
;
86 const char *name
; /* View name */
88 struct view_ops
*ops
; /* View operations */
89 struct argv_env
*env
; /* View variables. */
91 char ref
[SIZEOF_REF
]; /* Hovered commit reference */
92 char vid
[SIZEOF_REF
]; /* View ID. Set to id member when updating. */
94 int height
, width
; /* The width and height of the main window */
95 WINDOW
*win
; /* The main window */
96 WINDOW
*title
; /* The title window */
98 struct keymap
*keymap
; /* What keymap does this view have */
99 struct sort_state sort
; /* Sorting information. */
102 struct position pos
; /* Current position. */
103 struct position prev_pos
; /* Previous position. */
105 /* View columns rendering state */
106 struct view_column
*columns
;
109 char grep
[SIZEOF_STR
]; /* Search string */
110 regex_t
*regex
; /* Pre-compiled regexp */
111 unsigned int *matched_line
;
112 size_t matched_lines
;
114 /* If non-NULL, points to the view that opened this view. If this view
115 * is closed tig will switch back to the parent view. */
120 size_t lines
; /* Total number of lines */
121 struct line
*line
; /* Line index */
123 /* Number of lines with custom status, not to be counted in the
125 unsigned int custom_lines
;
128 struct line
*curline
; /* Line currently being drawn. */
129 enum line_type curtype
; /* Attribute currently used for drawing. */
130 unsigned long col
; /* Column when drawing. */
131 bool has_scrolled
; /* View was scrolled. */
132 bool force_redraw
; /* Whether to force a redraw after reading. */
135 const char **argv
; /* Shell command arguments. */
136 const char *dir
; /* Directory from which to execute. */
141 struct encoding
*encoding
;
148 #define DEFINE_VIEW(name) struct view name ##_view = { #name, &name##_ops, &argv_env }
151 OPEN_DEFAULT
= 0, /* Use default view switching. */
152 OPEN_STDIN
= 1, /* Open in pager mode. */
153 OPEN_FORWARD_STDIN
= 2, /* Forward stdin to I/O process. */
154 OPEN_SPLIT
= 4, /* Split current view. */
155 OPEN_RELOAD
= 8, /* Reload view even if it is the current. */
156 OPEN_REFRESH
= 16, /* Refresh view using previous command. */
157 OPEN_PREPARED
= 32, /* Open already prepared command. */
158 OPEN_EXTRA
= 64, /* Open extra data from command. */
159 OPEN_WITH_STDERR
= 128, /* Redirect stderr to stdin. */
161 OPEN_PAGER_MODE
= OPEN_STDIN
| OPEN_FORWARD_STDIN
,
162 OPEN_ALWAYS_LOAD
= OPEN_RELOAD
| OPEN_REFRESH
| OPEN_PREPARED
| OPEN_EXTRA
| OPEN_PAGER_MODE
,
165 #define open_in_pager_mode(flags) ((flags) & OPEN_PAGER_MODE)
166 #define open_from_stdin(flags) ((flags) & OPEN_STDIN)
168 struct view_column_data
{
169 struct view_column
*section
;
170 const struct ident
*author
;
171 const char *commit_title
;
172 const struct time
*date
;
173 const char *file_name
;
174 const unsigned long *file_size
;
175 const struct graph_canvas
*graph
;
177 const unsigned long *line_number
;
179 const struct ref
*ref
;
180 const struct ref_list
*refs
;
185 /* What type of content being displayed. Used in the title bar. */
187 /* Points to either of ref_{head,commit,blob} */
189 /* Flags to control the view behavior. */
190 enum view_flag flags
;
191 /* Size of private data. */
193 /* Open and reads in all view content. */
194 bool (*open
)(struct view
*view
, enum open_flags flags
);
195 /* Read one line; updates view->line. */
196 bool (*read
)(struct view
*view
, char *data
);
197 /* Draw one line; @lineno must be < view->height. */
198 bool (*draw
)(struct view
*view
, struct line
*line
, unsigned int lineno
);
199 /* Depending on view handle a special requests. */
200 enum request (*request
)(struct view
*view
, enum request request
, struct line
*line
);
201 /* Search for regexp in a line. */
202 bool (*grep
)(struct view
*view
, struct line
*line
);
204 void (*select
)(struct view
*view
, struct line
*line
);
205 /* Release resources when reloading the view */
206 void (*done
)(struct view
*view
);
207 /* Extract line information. */
208 bool (*get_column_data
)(struct view
*view
, const struct line
*line
, struct view_column_data
*column_data
);
215 struct view
*get_view(int index
);
217 #define foreach_view(view, i) \
218 for (i = 0; (view = get_view(i)); i++)
220 #define view_has_line(view, line_) \
221 ((view)->line <= (line_) && (line_) < (view)->line + (view)->lines)
227 bool goto_view_line(struct view
*view
, unsigned long offset
, unsigned long lineno
);
228 void select_view_line(struct view
*view
, unsigned long lineno
);
229 void do_scroll_view(struct view
*view
, int lines
);
230 void scroll_view(struct view
*view
, enum request request
);
231 void move_view(struct view
*view
, enum request request
);
237 void search_view(struct view
*view
, enum request request
);
238 void find_next(struct view
*view
, enum request request
);
239 bool grep_text(struct view
*view
, const char *text
[]);
246 struct view_state
*prev
; /* Entry below this in the stack */
247 struct position position
; /* View position to restore */
248 void *data
; /* View specific state */
251 struct view_history
{
253 struct view_state
*stack
;
254 struct position position
;
257 struct view_state
*push_view_history_state(struct view_history
*history
, struct position
*position
, void *data
);
258 bool pop_view_history_state(struct view_history
*history
, struct position
*position
, void *data
);
259 void reset_view_history(struct view_history
*history
);
265 void split_view(struct view
*prev
, struct view
*view
);
266 void maximize_view(struct view
*view
, bool redraw
);
267 void load_view(struct view
*view
, struct view
*prev
, enum open_flags flags
);
269 #define refresh_view(view) load_view(view, NULL, OPEN_REFRESH)
270 #define reload_view(view) load_view(view, NULL, OPEN_RELOAD)
272 void open_view(struct view
*prev
, struct view
*view
, enum open_flags flags
);
273 void open_argv(struct view
*prev
, struct view
*view
, const char *argv
[], const char *dir
, enum open_flags flags
);
279 #define get_sort_field(view) ((view)->sort.current->type)
280 void sort_view(struct view
*view
, bool change_field
);
282 struct view_column
*get_view_column(struct view
*view
, enum view_column_type type
);
283 bool view_column_grep(struct view
*view
, struct line
*line
);
284 bool view_column_info_changed(struct view
*view
, bool update
);
285 bool view_column_init(struct view
*view
, const enum view_column_type columns
[], size_t columns_size
);
286 void view_column_reset(struct view
*view
);
287 bool view_column_info_update(struct view
*view
, struct line
*line
);
290 find_line_by_type(struct view
*view
, struct line
*line
, enum line_type type
, int direction
);
292 #define find_prev_line_by_type(view, line, type) \
293 find_line_by_type(view, line, type, -1)
295 #define find_next_line_by_type(view, line, type) \
296 find_line_by_type(view, line, type, 1)
298 #define is_initial_view(view) (!(view)->prev && !(view)->argv)
299 #define failed_to_load_initial_view(view) (!(view)->prev && !(view)->lines)
301 #define get_view_color(view, type) get_line_color((view)->keymap->name, type)
302 #define get_view_attr(view, type) get_line_attr((view)->keymap->name, type)
305 * Incremental updating
309 check_position(struct position
*pos
)
311 return pos
->lineno
|| pos
->col
|| pos
->offset
;
315 clear_position(struct position
*pos
)
317 memset(pos
, 0, sizeof(*pos
));
320 void reset_view(struct view
*view
);
321 bool begin_update(struct view
*view
, const char *dir
, const char **argv
, enum open_flags flags
);
322 void end_update(struct view
*view
, bool force
);
323 bool update_view(struct view
*view
);
324 void update_view_title(struct view
*view
);
330 struct line
*add_line_at(struct view
*view
, unsigned long pos
, const void *data
, enum line_type type
, size_t data_size
, bool custom
);
331 struct line
*add_line(struct view
*view
, const void *data
, enum line_type type
, size_t data_size
, bool custom
);
332 struct line
*add_line_alloc_(struct view
*view
, void **ptr
, enum line_type type
, size_t data_size
, bool custom
);
334 #define add_line_alloc(view, data_ptr, type, extra_size, custom) \
335 add_line_alloc_(view, (void **) data_ptr, type, sizeof(**data_ptr) + extra_size, custom)
337 struct line
*add_line_nodata(struct view
*view
, enum line_type type
);
338 struct line
*add_line_text(struct view
*view
, const char *text
, enum line_type type
);
339 struct line
* PRINTF_LIKE(3, 4) add_line_format(struct view
*view
, enum line_type type
, const char *fmt
, ...);
342 /* vim: set ts=8 sw=8 noexpandtab: */