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.
18 #include "tig/options.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
;
33 static struct io script_io
= { -1 };
36 is_script_executing(void)
38 return script_io
.pipe
!= -1;
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
);
52 open_external_viewer(const char *argv
[], const char *dir
, bool silent
, bool confirm
, bool refresh
, const char *notice
)
60 def_prog_mode(); /* save current tty modes */
61 endwin(); /* restore original tty modes */
62 ok
= io_run_fg(argv
, dir
);
65 fprintf(stderr
, "%s", notice
);
66 if (!is_script_executing()) {
67 fprintf(stderr
, "Press Enter to continue");
74 if (watch_update(WATCH_EVENT_AFTER_COMMAND
) && refresh
) {
78 foreach_displayed_view (view
, i
) {
79 if (watch_dirty(&view
->watch
))
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"
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
];
104 editor
= getenv("GIT_EDITOR");
105 if (!editor
&& *opt_editor
)
108 editor
= getenv("VISUAL");
110 editor
= getenv("EDITOR");
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");
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
;
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
;
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
;
149 vertical_split_is_enabled(void)
151 if (opt_vertical_split
== VERTICAL_SPLIT_AUTO
) {
154 getmaxyx(stdscr
, height
, width
);
155 return width
> 160 || width
* VSPLIT_SCALE
> (height
- 1) * 2;
158 return opt_vertical_split
== VERTICAL_SPLIT_VERTICAL
;
162 redraw_display_separator(bool clear
)
164 if (displayed_views() > 1 && vertical_split_is_enabled()) {
165 chtype separator
= opt_line_graphics
? ACS_VLINE
: '|';
169 wbkgd(display_sep
, separator
+ get_line_attr(NULL
, LINE_TITLE_BLUR
));
170 wnoutrefresh(display_sep
);
178 struct view
*base
= display
[0];
179 struct view
*view
= display
[1] ? display
[1] : display
[0];
182 /* Setup window dimensions */
184 getmaxyx(stdscr
, base
->height
, base
->width
);
186 /* Make room for the status window. */
189 vsplit
= vertical_split_is_enabled();
193 apply_vertical_split(base
, view
);
195 /* Make room for the separator bar. */
198 apply_horizontal_split(base
, view
);
201 /* Make room for the title bar. */
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. */
213 foreach_displayed_view (view
, i
) {
214 if (!display_win
[i
]) {
215 display_win
[i
] = newwin(view
->height
, view
->width
, y
, x
);
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");
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
) {
234 display_sep
= newwin(view
->height
, 1, 0, x
- 1);
236 die("Failed to create separator window");
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
];
248 x
+= view
->width
+ 1;
250 y
+= view
->height
+ 1;
253 redraw_display_separator(FALSE
);
257 redraw_display(bool clear
)
262 foreach_displayed_view (view
, i
) {
266 update_view_title(view
);
269 redraw_display_separator(clear
);
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;
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);
290 return fprintf(file
, "%s\n", buf
) == read1
+ 1 + read2
+ 1;
294 save_display(const char *path
)
298 FILE *file
= fopen(path
, "w");
300 struct view
*view
= display
[0];
305 getmaxyx(stdscr
, i
, width
);
306 line
= malloc(width
+ 1);
312 if (view
->width
< width
&& display
[1]) {
313 struct view
*left
= display
[0],
316 for (i
= 0; ok
&& i
< left
->height
; i
++)
317 ok
= save_window_vline(file
, left
->win
, right
->win
, i
, line
, width
);
319 ok
= save_window_vline(file
, left
->title
, right
->title
, 0, line
, width
);
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
);
327 ok
= save_window_line(file
, view
->title
, 0, line
, width
);
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. */
350 /* Reading from the prompt? */
351 static bool input_mode
= FALSE
;
353 static bool status_empty
= FALSE
;
355 /* Update status and title window. */
357 update_status_window(struct view
*view
, const char *msg
, va_list args
)
362 if (!status_empty
|| *msg
) {
363 wmove(status_win
, 0, 0);
364 if (view
&& view
->has_scrolled
&& use_scroll_status_wclear
)
367 vwprintw(status_win
, msg
, args
);
368 status_empty
= FALSE
;
372 wclrtoeol(status_win
);
380 update_status(const char *msg
, ...)
385 update_status_window(display
[current_view
], msg
, args
);
390 report(const char *msg
, ...)
392 struct view
*view
= display
[current_view
];
396 char buf
[SIZEOF_STR
];
399 FORMAT_BUFFER(buf
, sizeof(buf
), msg
, retval
, TRUE
);
404 if (update_status_window(view
, msg
, args
))
405 wnoutrefresh(status_win
);
408 update_view_title(view
);
422 bool no_display
= !!getenv("TIG_NO_DISPLAY");
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();
436 /* Leave stdin and stdout alone when acting as a pager. */
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
);
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
);
456 getmaxyx(stdscr
, y
, x
);
457 status_win
= newwin(1, x
, y
- 1, 0);
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
);
469 TABSIZE
= opt_tab_size
;
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
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)
485 use_scroll_status_wclear
= use_scroll_redrawwin
= FALSE
;
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
;
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
== ':') {
505 memset(&input_buffer
, 0, sizeof(input_buffer
));
507 } else if (!io_get(&script_io
, &input_buffer
, '\n', TRUE
)) {
511 line
= input_buffer
.data
;
515 code
= get_key_value(&line
, key
);
517 die("Error reading script: %s", get_status_message(code
));
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
))
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
)
550 int i
, key_value
, cursor_y
, cursor_x
;
552 if (prompt_position
> 0)
555 memset(key
, 0, sizeof(*key
));
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
))
569 foreach_view (view
, i
) {
571 if (view_is_displayed(view
) && view
->has_scrolled
&&
572 use_scroll_redrawwin
)
573 redrawwin(view
->win
);
574 view
->has_scrolled
= FALSE
;
579 /* Update the cursor position. */
580 if (prompt_position
) {
581 getbegyx(status_win
, cursor_y
, cursor_x
);
582 cursor_x
= prompt_position
;
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
))
595 return key
->modifiers
.multibytes
? OK
: key
->data
.value
;
598 /* Refresh, accept single keystroke of input */
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
) {
611 getmaxyx(stdscr
, height
, width
);
613 wresize(status_win
, 1, width
);
614 mvwin(status_win
, height
- 1, 0);
615 wnoutrefresh(status_win
);
617 redraw_display(TRUE
);
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
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
);
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
))
678 /* vim: set ts=8 sw=8 noexpandtab: */