Auto-refresh views when changes are detected in the repository
[tig.git] / src / display.c
bloba81653e5aee991252208f5de35743e74c839ce2c
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.
14 #include "tig/tig.h"
15 #include "tig/argv.h"
16 #include "tig/io.h"
17 #include "tig/repo.h"
18 #include "tig/options.h"
19 #include "tig/view.h"
20 #include "tig/draw.h"
21 #include "tig/display.h"
22 #include "tig/watch.h"
24 struct view *display[2];
25 unsigned int current_view;
27 static WINDOW *display_win[2];
28 static WINDOW *display_title[2];
29 static WINDOW *display_sep;
31 FILE *opt_tty;
33 bool
34 open_external_viewer(const char *argv[], const char *dir, bool confirm, const char *notice)
36 bool ok;
38 def_prog_mode(); /* save current tty modes */
39 endwin(); /* restore original tty modes */
40 ok = io_run_fg(argv, dir);
41 if (confirm || !ok) {
42 if (!ok && *notice)
43 fprintf(stderr, "%s", notice);
44 fprintf(stderr, "Press Enter to continue");
45 getc(opt_tty);
47 reset_prog_mode();
48 if (watch_update(WATCH_EVENT_AFTER_EXTERNAL)) {
49 struct view *view;
50 int i;
52 foreach_displayed_view (view, i) {
53 if (watch_dirty(&view->watch))
54 refresh_view(view);
57 redraw_display(TRUE);
58 return ok;
61 #define EDITOR_LINENO_MSG \
62 "*** Your editor reported an error while opening the file.\n" \
63 "*** This is probably because it doesn't support the line\n" \
64 "*** number argument added automatically. The line number\n" \
65 "*** has been disabled for now. You can permanently disable\n" \
66 "*** it by adding the following line to ~/.tigrc\n" \
67 "*** set editor-line-number = no\n"
69 void
70 open_editor(const char *file, unsigned int lineno)
72 const char *editor_argv[SIZEOF_ARG + 3] = { "vi", file, NULL };
73 char editor_cmd[SIZEOF_STR];
74 char lineno_cmd[SIZEOF_STR];
75 const char *editor;
76 int argc = 0;
78 editor = getenv("GIT_EDITOR");
79 if (!editor && *opt_editor)
80 editor = opt_editor;
81 if (!editor)
82 editor = getenv("VISUAL");
83 if (!editor)
84 editor = getenv("EDITOR");
85 if (!editor)
86 editor = "vi";
88 string_ncopy(editor_cmd, editor, strlen(editor));
89 if (!argv_from_string_no_quotes(editor_argv, &argc, editor_cmd)) {
90 report("Failed to read editor command");
91 return;
94 if (lineno && opt_editor_line_number && string_format(lineno_cmd, "+%u", lineno))
95 editor_argv[argc++] = lineno_cmd;
96 editor_argv[argc] = file;
97 if (!open_external_viewer(editor_argv, repo.cdup, FALSE, EDITOR_LINENO_MSG))
98 opt_editor_line_number = FALSE;
102 static void
103 apply_horizontal_split(struct view *base, struct view *view)
105 view->width = base->width;
106 view->height = apply_step(opt_split_view_height, base->height);
107 view->height = MAX(view->height, MIN_VIEW_HEIGHT);
108 view->height = MIN(view->height, base->height - MIN_VIEW_HEIGHT);
109 base->height -= view->height;
112 static void
113 apply_vertical_split(struct view *base, struct view *view)
115 view->height = base->height;
116 view->width = apply_step(VSPLIT_SCALE, base->width);
117 view->width = MAX(view->width, MIN_VIEW_WIDTH);
118 view->width = MIN(view->width, base->width - MIN_VIEW_WIDTH);
119 base->width -= view->width;
122 static bool
123 vertical_split_is_enabled(void)
125 if (opt_vertical_split == VERTICAL_SPLIT_AUTO) {
126 int height, width;
128 getmaxyx(stdscr, height, width);
129 return width > 160 || width * VSPLIT_SCALE > (height - 1) * 2;
132 return opt_vertical_split == VERTICAL_SPLIT_VERTICAL;
135 static void
136 redraw_display_separator(bool clear)
138 if (displayed_views() > 1 && vertical_split_is_enabled()) {
139 chtype separator = opt_line_graphics ? ACS_VLINE : '|';
141 if (clear)
142 wclear(display_sep);
143 wbkgd(display_sep, separator + get_line_attr(NULL, LINE_TITLE_BLUR));
144 wnoutrefresh(display_sep);
148 void
149 resize_display(void)
151 int x, y, i;
152 struct view *base = display[0];
153 struct view *view = display[1] ? display[1] : display[0];
154 bool vsplit;
156 /* Setup window dimensions */
158 getmaxyx(stdscr, base->height, base->width);
160 /* Make room for the status window. */
161 base->height -= 1;
163 vsplit = vertical_split_is_enabled();
165 if (view != base) {
166 if (vsplit) {
167 apply_vertical_split(base, view);
169 /* Make room for the separator bar. */
170 view->width -= 1;
171 } else {
172 apply_horizontal_split(base, view);
175 /* Make room for the title bar. */
176 view->height -= 1;
179 string_format(opt_env_columns, "COLUMNS=%d", base->width);
180 string_format(opt_env_lines, "LINES=%d", base->height);
182 /* Make room for the title bar. */
183 base->height -= 1;
185 x = y = 0;
187 foreach_displayed_view (view, i) {
188 if (!display_win[i]) {
189 display_win[i] = newwin(view->height, view->width, y, x);
190 if (!display_win[i])
191 die("Failed to create %s view", view->name);
193 scrollok(display_win[i], FALSE);
195 display_title[i] = newwin(1, view->width, y + view->height, x);
196 if (!display_title[i])
197 die("Failed to create title window");
199 } else {
200 wresize(display_win[i], view->height, view->width);
201 mvwin(display_win[i], y, x);
202 wresize(display_title[i], 1, view->width);
203 mvwin(display_title[i], y + view->height, x);
206 if (i > 0 && vsplit) {
207 if (!display_sep) {
208 display_sep = newwin(view->height, 1, 0, x - 1);
209 if (!display_sep)
210 die("Failed to create separator window");
212 } else {
213 wresize(display_sep, view->height, 1);
214 mvwin(display_sep, 0, x - 1);
218 view->win = display_win[i];
219 view->title = display_title[i];
221 if (vsplit)
222 x += view->width + 1;
223 else
224 y += view->height + 1;
227 redraw_display_separator(FALSE);
230 void
231 redraw_display(bool clear)
233 struct view *view;
234 int i;
236 foreach_displayed_view (view, i) {
237 if (clear)
238 wclear(view->win);
239 redraw_view(view);
240 update_view_title(view);
243 redraw_display_separator(clear);
247 * Status management
250 /* Whether or not the curses interface has been initialized. */
251 static bool cursed = FALSE;
253 /* Terminal hacks and workarounds. */
254 static bool use_scroll_redrawwin;
255 static bool use_scroll_status_wclear;
257 /* The status window is used for polling keystrokes. */
258 WINDOW *status_win;
260 /* Reading from the prompt? */
261 static bool input_mode = FALSE;
263 static bool status_empty = FALSE;
265 /* Update status and title window. */
266 static bool
267 update_status_window(struct view *view, const char *msg, va_list args)
269 if (input_mode)
270 return FALSE;
272 if (!status_empty || *msg) {
273 wmove(status_win, 0, 0);
274 if (view && view->has_scrolled && use_scroll_status_wclear)
275 wclear(status_win);
276 if (*msg) {
277 vwprintw(status_win, msg, args);
278 status_empty = FALSE;
279 } else {
280 status_empty = TRUE;
282 wclrtoeol(status_win);
283 return TRUE;
286 return FALSE;
289 void
290 update_status(const char *msg, ...)
292 va_list args;
294 va_start(args, msg);
295 update_status_window(display[current_view], msg, args);
296 va_end(args);
299 void
300 report(const char *msg, ...)
302 struct view *view = display[current_view];
303 va_list args;
305 if (!view) {
306 char buf[SIZEOF_STR];
307 int retval;
309 FORMAT_BUFFER(buf, sizeof(buf), msg, retval, TRUE);
310 die("%s", buf);
313 va_start(args, msg);
314 if (update_status_window(view, msg, args))
315 wnoutrefresh(status_win);
316 va_end(args);
318 update_view_title(view);
321 static void
322 done_display(void)
324 endwin();
327 void
328 init_display(void)
330 const char *term;
331 int x, y;
333 die_callback = done_display;
334 /* XXX: Restore tty modes and let the OS cleanup the rest! */
335 if (atexit(done_display))
336 die("Failed to register done_display");
338 /* Initialize the curses library */
339 if (isatty(STDIN_FILENO)) {
340 cursed = !!initscr();
341 opt_tty = stdin;
342 } else {
343 /* Leave stdin and stdout alone when acting as a pager. */
344 opt_tty = fopen("/dev/tty", "r+");
345 if (!opt_tty)
346 die("Failed to open /dev/tty");
347 cursed = !!newterm(NULL, opt_tty, opt_tty);
350 if (!cursed)
351 die("Failed to initialize curses");
353 nonl(); /* Disable conversion and detect newlines from input. */
354 cbreak(); /* Take input chars one at a time, no wait for \n */
355 noecho(); /* Don't echo input */
356 leaveok(stdscr, FALSE);
358 if (has_colors())
359 init_colors();
361 getmaxyx(stdscr, y, x);
362 status_win = newwin(1, x, y - 1, 0);
363 if (!status_win)
364 die("Failed to create status window");
366 /* Enable keyboard mapping */
367 keypad(status_win, TRUE);
368 wbkgdset(status_win, get_line_attr(NULL, LINE_STATUS));
369 enable_mouse(opt_mouse);
371 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20080119)
372 set_tabsize(opt_tab_size);
373 #else
374 TABSIZE = opt_tab_size;
375 #endif
377 term = getenv("XTERM_VERSION") ? NULL : getenv("COLORTERM");
378 if (term && !strcmp(term, "gnome-terminal")) {
379 /* In the gnome-terminal-emulator, the message from
380 * scrolling up one line when impossible followed by
381 * scrolling down one line causes corruption of the
382 * status line. This is fixed by calling wclear. */
383 use_scroll_status_wclear = TRUE;
384 use_scroll_redrawwin = FALSE;
386 } else if (term && !strcmp(term, "xrvt-xpm")) {
387 /* No problems with full optimizations in xrvt-(unicode)
388 * and aterm. */
389 use_scroll_status_wclear = use_scroll_redrawwin = FALSE;
391 } else {
392 /* When scrolling in (u)xterm the last line in the
393 * scrolling direction will update slowly. */
394 use_scroll_redrawwin = TRUE;
395 use_scroll_status_wclear = FALSE;
400 get_input(int prompt_position, struct key *key, bool modifiers)
402 struct view *view;
403 int i, key_value, cursor_y, cursor_x;
405 if (prompt_position)
406 input_mode = TRUE;
408 memset(key, 0, sizeof(*key));
410 while (TRUE) {
411 int delay = -1;
413 if (opt_refresh_mode == REFRESH_MODE_PERIODIC) {
414 delay = watch_periodic(opt_refresh_interval);
415 foreach_displayed_view (view, i) {
416 if (view_can_refresh(view) &&
417 watch_dirty(&view->watch))
418 refresh_view(view);
422 foreach_view (view, i) {
423 update_view(view);
424 if (view_is_displayed(view) && view->has_scrolled &&
425 use_scroll_redrawwin)
426 redrawwin(view->win);
427 view->has_scrolled = FALSE;
428 if (view->pipe)
429 delay = 0;
432 /* Update the cursor position. */
433 if (prompt_position) {
434 getbegyx(status_win, cursor_y, cursor_x);
435 cursor_x = prompt_position;
436 } else {
437 view = display[current_view];
438 getbegyx(view->win, cursor_y, cursor_x);
439 cursor_x = view->width - 1;
440 cursor_y += view->pos.lineno - view->pos.offset;
442 setsyx(cursor_y, cursor_x);
444 /* Refresh, accept single keystroke of input */
445 doupdate();
446 wtimeout(status_win, delay);
447 key_value = wgetch(status_win);
449 /* wgetch() with nodelay() enabled returns ERR when
450 * there's no input. */
451 if (key_value == ERR) {
453 } else if (key_value == KEY_ESC && modifiers) {
454 key->modifiers.escape = 1;
456 } else if (key_value == KEY_RESIZE) {
457 int height, width;
459 getmaxyx(stdscr, height, width);
461 wresize(status_win, 1, width);
462 mvwin(status_win, height - 1, 0);
463 wnoutrefresh(status_win);
464 resize_display();
465 redraw_display(TRUE);
467 } else {
468 int pos, key_length;
470 input_mode = FALSE;
471 if (key_value == erasechar())
472 key_value = KEY_BACKSPACE;
475 * Ctrl-<key> values are represented using a 0x1F
476 * bitmask on the key value. To 'unmap' we assume that:
478 * - Ctrl-Z is handled by Ncurses.
479 * - Ctrl-m is the same as Return/Enter.
480 * - Ctrl-i is the same as Tab.
482 * For all other key values in the range the Ctrl flag
483 * is set and the key value is updated to the proper
484 * ASCII value.
486 if (KEY_CTL('a') <= key_value && key_value <= KEY_CTL('y') &&
487 key_value != KEY_RETURN && key_value != KEY_TAB) {
488 key->modifiers.control = 1;
489 key_value = key_value | 0x40;
492 if ((key_value >= KEY_MIN && key_value < KEY_MAX) || key_value < 0x1F) {
493 key->data.value = key_value;
494 return key->data.value;
497 key->modifiers.multibytes = 1;
498 key->data.bytes[0] = key_value;
500 key_length = utf8_char_length(key->data.bytes);
501 nodelay(status_win, TRUE);
502 for (pos = 1; pos < key_length && pos < sizeof(key->data.bytes) - 1; pos++) {
503 key->data.bytes[pos] = wgetch(status_win);
506 return OK;
511 void
512 enable_mouse(bool enable)
514 #ifdef NCURSES_MOUSE_VERSION
515 static bool enabled = FALSE;
517 if (enable != enabled) {
518 mmask_t mask = enable ? ALL_MOUSE_EVENTS : 0;
520 if (mousemask(mask, NULL))
521 mouseinterval(0);
522 enabled = enable;
524 #endif
527 /* vim: set ts=8 sw=8 noexpandtab: */