Fix insertion/ordering of refs in refs_by_id map.
[tig.git] / src / display.c
blob1f5aeff6d38bc061b3145157c7a0932a4d5d7692
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 #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 static FILE *opt_tty;
33 static struct io script_io = { -1 };
35 static bool
36 is_script_executing(void)
38 return script_io.pipe != -1;
41 enum status_code
42 open_script(const char *path)
44 if (is_script_executing())
45 return error("Scripts cannot be run from scripts");
47 return io_open(&script_io, "%s", path)
48 ? SUCCESS : error("Failed to open %s", path);
51 bool
52 open_external_viewer(const char *argv[], const char *dir, bool silent, bool confirm, bool refresh, const char *notice)
54 bool ok;
56 if (silent) {
57 ok = io_run_bg(argv);
59 } else {
60 def_prog_mode(); /* save current tty modes */
61 endwin(); /* restore original tty modes */
62 ok = io_run_fg(argv, dir);
63 if (confirm || !ok) {
64 if (!ok && *notice)
65 fprintf(stderr, "%s", notice);
66 if (!is_script_executing()) {
67 fprintf(stderr, "Press Enter to continue");
68 getc(opt_tty);
71 reset_prog_mode();
74 if (watch_update(WATCH_EVENT_AFTER_COMMAND) && refresh) {
75 struct view *view;
76 int i;
78 foreach_displayed_view (view, i) {
79 if (watch_dirty(&view->watch))
80 refresh_view(view);
83 redraw_display(TRUE);
84 return ok;
87 #define EDITOR_LINENO_MSG \
88 "*** Your editor reported an error while opening the file.\n" \
89 "*** This is probably because it doesn't support the line\n" \
90 "*** number argument added automatically. The line number\n" \
91 "*** has been disabled for now. You can permanently disable\n" \
92 "*** it by adding the following line to ~/.tigrc\n" \
93 "*** set editor-line-number = no\n"
95 void
96 open_editor(const char *file, unsigned int lineno)
98 const char *editor_argv[SIZEOF_ARG + 3] = { "vi", file, NULL };
99 char editor_cmd[SIZEOF_STR];
100 char lineno_cmd[SIZEOF_STR];
101 const char *editor;
102 int argc = 0;
104 editor = getenv("GIT_EDITOR");
105 if (!editor && *opt_editor)
106 editor = opt_editor;
107 if (!editor)
108 editor = getenv("VISUAL");
109 if (!editor)
110 editor = getenv("EDITOR");
111 if (!editor)
112 editor = "vi";
114 string_ncopy(editor_cmd, editor, strlen(editor));
115 if (!argv_from_string_no_quotes(editor_argv, &argc, editor_cmd)) {
116 report("Failed to read editor command");
117 return;
120 if (lineno && opt_editor_line_number && string_format(lineno_cmd, "+%u", lineno))
121 editor_argv[argc++] = lineno_cmd;
122 editor_argv[argc] = file;
123 if (!open_external_viewer(editor_argv, repo.cdup, FALSE, FALSE, TRUE, EDITOR_LINENO_MSG))
124 opt_editor_line_number = FALSE;
128 static void
129 apply_horizontal_split(struct view *base, struct view *view)
131 view->width = base->width;
132 view->height = apply_step(opt_split_view_height, base->height);
133 view->height = MAX(view->height, MIN_VIEW_HEIGHT);
134 view->height = MIN(view->height, base->height - MIN_VIEW_HEIGHT);
135 base->height -= view->height;
138 static void
139 apply_vertical_split(struct view *base, struct view *view)
141 view->height = base->height;
142 view->width = apply_step(opt_split_view_width, base->width);
143 view->width = MAX(view->width, MIN_VIEW_WIDTH);
144 view->width = MIN(view->width, base->width - MIN_VIEW_WIDTH);
145 base->width -= view->width;
148 static bool
149 vertical_split_is_enabled(void)
151 if (opt_vertical_split == VERTICAL_SPLIT_AUTO) {
152 int height, width;
154 getmaxyx(stdscr, height, width);
155 return width > 160 || width * VSPLIT_SCALE > (height - 1) * 2;
158 return opt_vertical_split == VERTICAL_SPLIT_VERTICAL;
161 static void
162 redraw_display_separator(bool clear)
164 if (displayed_views() > 1 && vertical_split_is_enabled()) {
165 chtype separator = opt_line_graphics ? ACS_VLINE : '|';
167 if (clear)
168 wclear(display_sep);
169 wbkgd(display_sep, separator + get_line_attr(NULL, LINE_TITLE_BLUR));
170 wnoutrefresh(display_sep);
174 void
175 resize_display(void)
177 int x, y, i;
178 struct view *base = display[0];
179 struct view *view = display[1] ? display[1] : display[0];
180 bool vsplit;
182 /* Setup window dimensions */
184 getmaxyx(stdscr, base->height, base->width);
186 /* Make room for the status window. */
187 base->height -= 1;
189 vsplit = vertical_split_is_enabled();
191 if (view != base) {
192 if (vsplit) {
193 apply_vertical_split(base, view);
195 /* Make room for the separator bar. */
196 view->width -= 1;
197 } else {
198 apply_horizontal_split(base, view);
201 /* Make room for the title bar. */
202 view->height -= 1;
205 string_format(opt_env_columns, "COLUMNS=%d", base->width);
206 string_format(opt_env_lines, "LINES=%d", base->height);
208 /* Make room for the title bar. */
209 base->height -= 1;
211 x = y = 0;
213 foreach_displayed_view (view, i) {
214 if (!display_win[i]) {
215 display_win[i] = newwin(view->height, view->width, y, x);
216 if (!display_win[i])
217 die("Failed to create %s view", view->name);
219 scrollok(display_win[i], FALSE);
221 display_title[i] = newwin(1, view->width, y + view->height, x);
222 if (!display_title[i])
223 die("Failed to create title window");
225 } else {
226 wresize(display_win[i], view->height, view->width);
227 mvwin(display_win[i], y, x);
228 wresize(display_title[i], 1, view->width);
229 mvwin(display_title[i], y + view->height, x);
232 if (i > 0 && vsplit) {
233 if (!display_sep) {
234 display_sep = newwin(view->height, 1, 0, x - 1);
235 if (!display_sep)
236 die("Failed to create separator window");
238 } else {
239 wresize(display_sep, view->height, 1);
240 mvwin(display_sep, 0, x - 1);
244 view->win = display_win[i];
245 view->title = display_title[i];
247 if (vsplit)
248 x += view->width + 1;
249 else
250 y += view->height + 1;
253 redraw_display_separator(FALSE);
256 void
257 redraw_display(bool clear)
259 struct view *view;
260 int i;
262 foreach_displayed_view (view, i) {
263 if (clear)
264 wclear(view->win);
265 redraw_view(view);
266 update_view_title(view);
269 redraw_display_separator(clear);
272 static bool
273 save_window_line(FILE *file, WINDOW *win, int y, char *buf, size_t bufsize)
275 int read = mvwinnstr(win, y, 0, buf, bufsize);
277 return read == ERR ? FALSE : fprintf(file, "%s\n", buf) == read + 1;
280 static bool
281 save_window_vline(FILE *file, WINDOW *left, WINDOW *right, int y, char *buf, size_t bufsize)
283 int read1 = mvwinnstr(left, y, 0, buf, bufsize);
284 int read2 = read1 == ERR ? ERR : mvwinnstr(right, y, 0, buf + read1 + 1, bufsize - read1 - 1);
286 if (read2 == ERR)
287 return FALSE;
288 buf[read1] = '|';
290 return fprintf(file, "%s\n", buf) == read1 + 1 + read2 + 1;
293 bool
294 save_display(const char *path)
296 int i, width;
297 char *line;
298 FILE *file = fopen(path, "w");
299 bool ok = TRUE;
300 struct view *view = display[0];
302 if (!file)
303 return FALSE;
305 getmaxyx(stdscr, i, width);
306 line = malloc(width + 1);
307 if (!line) {
308 fclose(file);
309 return FALSE;
312 if (view->width < width && display[1]) {
313 struct view *left = display[0],
314 *right = display[1];
316 for (i = 0; ok && i < left->height; i++)
317 ok = save_window_vline(file, left->win, right->win, i, line, width);
318 if (ok)
319 ok = save_window_vline(file, left->title, right->title, 0, line, width);
320 } else {
321 int j;
323 foreach_displayed_view (view, j) {
324 for (i = 0; ok && i < view->height; i++)
325 ok = save_window_line(file, view->win, i, line, width);
326 if (ok)
327 ok = save_window_line(file, view->title, 0, line, width);
331 free(line);
332 fclose(file);
333 return ok;
337 * Status management
340 /* Whether or not the curses interface has been initialized. */
341 static bool cursed = FALSE;
343 /* Terminal hacks and workarounds. */
344 static bool use_scroll_redrawwin;
345 static bool use_scroll_status_wclear;
347 /* The status window is used for polling keystrokes. */
348 WINDOW *status_win;
350 /* Reading from the prompt? */
351 static bool input_mode = FALSE;
353 static bool status_empty = FALSE;
355 /* Update status and title window. */
356 static bool
357 update_status_window(struct view *view, const char *msg, va_list args)
359 if (input_mode)
360 return FALSE;
362 if (!status_empty || *msg) {
363 wmove(status_win, 0, 0);
364 if (view && view->has_scrolled && use_scroll_status_wclear)
365 wclear(status_win);
366 if (*msg) {
367 vwprintw(status_win, msg, args);
368 status_empty = FALSE;
369 } else {
370 status_empty = TRUE;
372 wclrtoeol(status_win);
373 return TRUE;
376 return FALSE;
379 void
380 update_status(const char *msg, ...)
382 va_list args;
384 va_start(args, msg);
385 update_status_window(display[current_view], msg, args);
386 va_end(args);
389 void
390 report(const char *msg, ...)
392 struct view *view = display[current_view];
393 va_list args;
395 if (!view) {
396 char buf[SIZEOF_STR];
397 int retval;
399 FORMAT_BUFFER(buf, sizeof(buf), msg, retval, TRUE);
400 die("%s", buf);
403 va_start(args, msg);
404 if (update_status_window(view, msg, args))
405 wnoutrefresh(status_win);
406 va_end(args);
408 update_view_title(view);
411 static void
412 done_display(void)
414 if (cursed)
415 endwin();
416 cursed = FALSE;
419 void
420 init_display(void)
422 bool no_display = !!getenv("TIG_NO_DISPLAY");
423 const char *term;
424 int x, y;
426 die_callback = done_display;
427 /* XXX: Restore tty modes and let the OS cleanup the rest! */
428 if (atexit(done_display))
429 die("Failed to register done_display");
431 /* Initialize the curses library */
432 if (!no_display && isatty(STDIN_FILENO)) {
433 cursed = !!initscr();
434 opt_tty = stdin;
435 } else {
436 /* Leave stdin and stdout alone when acting as a pager. */
437 FILE *out_tty;
439 opt_tty = fopen("/dev/tty", "r+");
440 out_tty = no_display ? fopen("/dev/null", "w+") : opt_tty;
441 if (!opt_tty || !out_tty)
442 die("Failed to open /dev/tty");
443 cursed = !!newterm(NULL, out_tty, opt_tty);
446 if (!cursed)
447 die("Failed to initialize curses");
449 nonl(); /* Disable conversion and detect newlines from input. */
450 cbreak(); /* Take input chars one at a time, no wait for \n */
451 noecho(); /* Don't echo input */
452 leaveok(stdscr, FALSE);
454 init_colors();
456 getmaxyx(stdscr, y, x);
457 status_win = newwin(1, x, y - 1, 0);
458 if (!status_win)
459 die("Failed to create status window");
461 /* Enable keyboard mapping */
462 keypad(status_win, TRUE);
463 wbkgdset(status_win, get_line_attr(NULL, LINE_STATUS));
464 enable_mouse(opt_mouse);
466 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20080119)
467 set_tabsize(opt_tab_size);
468 #else
469 TABSIZE = opt_tab_size;
470 #endif
472 term = getenv("XTERM_VERSION") ? NULL : getenv("COLORTERM");
473 if (term && !strcmp(term, "gnome-terminal")) {
474 /* In the gnome-terminal-emulator, the warning message
475 * shown when scrolling up one line while the cursor is
476 * on the first line followed by scrolling down one line
477 * corrupts the status line. This is fixed by calling
478 * wclear. */
479 use_scroll_status_wclear = TRUE;
480 use_scroll_redrawwin = FALSE;
482 } else if (term && !strcmp(term, "xrvt-xpm")) {
483 /* No problems with full optimizations in xrvt-(unicode)
484 * and aterm. */
485 use_scroll_status_wclear = use_scroll_redrawwin = FALSE;
487 } else {
488 /* When scrolling in (u)xterm the last line in the
489 * scrolling direction will update slowly. */
490 use_scroll_redrawwin = TRUE;
491 use_scroll_status_wclear = FALSE;
495 static bool
496 read_script(struct key *key)
498 static struct buffer input_buffer;
499 static const char *line = "";
500 enum status_code code;
502 if (!line || !*line) {
503 if (input_buffer.data && *input_buffer.data == ':') {
504 line = "<Enter>";
505 memset(&input_buffer, 0, sizeof(input_buffer));
507 } else if (!io_get(&script_io, &input_buffer, '\n', TRUE)) {
508 io_done(&script_io);
509 return FALSE;
510 } else {
511 line = input_buffer.data;
515 code = get_key_value(&line, key);
516 if (code != SUCCESS)
517 die("Error reading script: %s", get_status_message(code));
518 return TRUE;
522 get_input_char(void)
524 if (is_script_executing()) {
525 static struct key key;
526 static int bytes_pos;
528 if (!key.modifiers.multibytes || bytes_pos >= strlen(key.data.bytes)) {
529 if (!read_script(&key))
530 return 0;
531 bytes_pos = 0;
534 if (!key.modifiers.multibytes) {
535 if (key.data.value < 128)
536 return key.data.value;
537 die("Only ASCII control characters can be used in prompts: %d", key.data.value);
540 return key.data.bytes[bytes_pos++];
543 return getc(opt_tty);
547 get_input(int prompt_position, struct key *key)
549 struct view *view;
550 int i, key_value, cursor_y, cursor_x;
552 if (prompt_position > 0)
553 input_mode = TRUE;
555 memset(key, 0, sizeof(*key));
557 while (TRUE) {
558 int delay = -1;
560 if (opt_refresh_mode == REFRESH_MODE_PERIODIC) {
561 delay = watch_periodic(opt_refresh_interval);
562 foreach_displayed_view (view, i) {
563 if (view_can_refresh(view) &&
564 watch_dirty(&view->watch))
565 refresh_view(view);
569 foreach_view (view, i) {
570 update_view(view);
571 if (view_is_displayed(view) && view->has_scrolled &&
572 use_scroll_redrawwin)
573 redrawwin(view->win);
574 view->has_scrolled = FALSE;
575 if (view->pipe)
576 delay = 0;
579 /* Update the cursor position. */
580 if (prompt_position) {
581 getbegyx(status_win, cursor_y, cursor_x);
582 cursor_x = prompt_position;
583 } else {
584 view = display[current_view];
585 getbegyx(view->win, cursor_y, cursor_x);
586 cursor_x += view->width - 1;
587 cursor_y += view->pos.lineno - view->pos.offset;
589 setsyx(cursor_y, cursor_x);
591 if (is_script_executing()) {
592 /* Wait for the current command to complete. */
593 if (delay == 0 || !read_script(key))
594 continue;
595 return key->modifiers.multibytes ? OK : key->data.value;
597 } else {
598 /* Refresh, accept single keystroke of input */
599 doupdate();
600 wtimeout(status_win, delay);
601 key_value = wgetch(status_win);
604 /* wgetch() with nodelay() enabled returns ERR when
605 * there's no input. */
606 if (key_value == ERR) {
608 } else if (key_value == KEY_RESIZE) {
609 int height, width;
611 getmaxyx(stdscr, height, width);
613 wresize(status_win, 1, width);
614 mvwin(status_win, height - 1, 0);
615 wnoutrefresh(status_win);
616 resize_display();
617 redraw_display(TRUE);
619 } else {
620 int pos, key_length;
622 input_mode = FALSE;
623 if (key_value == erasechar())
624 key_value = KEY_BACKSPACE;
627 * Ctrl-<key> values are represented using a 0x1F
628 * bitmask on the key value. To 'unmap' we assume that:
630 * - Ctrl-Z is handled by Ncurses.
631 * - Ctrl-m is the same as Return/Enter.
632 * - Ctrl-i is the same as Tab.
634 * For all other key values in the range the Ctrl flag
635 * is set and the key value is updated to the proper
636 * ASCII value.
638 if (KEY_CTL('a') <= key_value && key_value <= KEY_CTL('y') &&
639 key_value != KEY_RETURN && key_value != KEY_TAB) {
640 key->modifiers.control = 1;
641 key_value = key_value | 0x40;
644 if ((key_value >= KEY_MIN && key_value < KEY_MAX) || key_value < 0x1F) {
645 key->data.value = key_value;
646 return key->data.value;
649 key->modifiers.multibytes = 1;
650 key->data.bytes[0] = key_value;
652 key_length = utf8_char_length(key->data.bytes);
653 for (pos = 1; pos < key_length && pos < sizeof(key->data.bytes) - 1; pos++) {
654 key->data.bytes[pos] = wgetch(status_win);
657 return OK;
662 void
663 enable_mouse(bool enable)
665 #ifdef NCURSES_MOUSE_VERSION
666 static bool enabled = FALSE;
668 if (enable != enabled) {
669 mmask_t mask = enable ? ALL_MOUSE_EVENTS : 0;
671 if (mousemask(mask, NULL))
672 mouseinterval(0);
673 enabled = enable;
675 #endif
678 /* vim: set ts=8 sw=8 noexpandtab: */