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
)
56 if (silent
|| is_script_executing()) {
57 ok
= io_run_bg(argv
, dir
);
60 endwin(); /* restore original tty modes */
61 ok
= io_run_fg(argv
, dir
);
64 fprintf(stderr
, "%s", notice
);
65 fprintf(stderr
, "Press Enter to continue");
70 if (watch_update(WATCH_EVENT_AFTER_COMMAND
) && refresh
) {
74 foreach_displayed_view (view
, i
) {
75 if (watch_dirty(&view
->watch
))
83 #define EDITOR_LINENO_MSG \
84 "*** Your editor reported an error while opening the file.\n" \
85 "*** This is probably because it doesn't support the line\n" \
86 "*** number argument added automatically. The line number\n" \
87 "*** has been disabled for now. You can permanently disable\n" \
88 "*** it by adding the following line to ~/.tigrc\n" \
89 "*** set editor-line-number = no\n"
92 open_editor(const char *file
, unsigned int lineno
)
94 const char *editor_argv
[SIZEOF_ARG
+ 3] = { "vi", file
, NULL
};
95 char editor_cmd
[SIZEOF_STR
];
96 char lineno_cmd
[SIZEOF_STR
];
100 editor
= getenv("GIT_EDITOR");
101 if (!editor
&& *opt_editor
)
104 editor
= getenv("VISUAL");
106 editor
= getenv("EDITOR");
110 string_ncopy(editor_cmd
, editor
, strlen(editor
));
111 if (!argv_from_string_no_quotes(editor_argv
, &argc
, editor_cmd
)) {
112 report("Failed to read editor command");
116 if (lineno
&& opt_editor_line_number
&& string_format(lineno_cmd
, "+%u", lineno
))
117 editor_argv
[argc
++] = lineno_cmd
;
118 editor_argv
[argc
] = file
;
119 if (!open_external_viewer(editor_argv
, repo
.cdup
, false, false, true, EDITOR_LINENO_MSG
))
120 opt_editor_line_number
= false;
125 apply_horizontal_split(struct view
*base
, struct view
*view
)
127 view
->width
= base
->width
;
128 view
->height
= apply_step(opt_split_view_height
, base
->height
);
129 view
->height
= MAX(view
->height
, MIN_VIEW_HEIGHT
);
130 view
->height
= MIN(view
->height
, base
->height
- MIN_VIEW_HEIGHT
);
131 base
->height
-= view
->height
;
135 apply_vertical_split(int base_width
)
137 int width
= apply_step(opt_split_view_width
, base_width
);
139 width
= MAX(width
, MIN_VIEW_WIDTH
);
140 width
= MIN(width
, base_width
- MIN_VIEW_WIDTH
);
146 vertical_split_is_enabled(enum vertical_split vsplit
, int height
, int width
)
148 if (vsplit
== VERTICAL_SPLIT_AUTO
)
149 return width
> 160 || width
* VSPLIT_SCALE
> (height
- 1) * 2;
150 return vsplit
== VERTICAL_SPLIT_VERTICAL
;
154 redraw_display_separator(bool clear
)
157 chtype separator
= opt_line_graphics
? ACS_VLINE
: '|';
161 wbkgd(display_sep
, separator
+ get_line_attr(NULL
, LINE_TITLE_BLUR
));
162 wnoutrefresh(display_sep
);
166 static void create_or_move_display_separator(int height
, int x
)
169 display_sep
= newwin(height
, 1, 0, x
);
171 die("Failed to create separator window");
174 wresize(display_sep
, height
, 1);
175 mvwin(display_sep
, 0, x
);
179 static void remove_display_separator(void)
192 struct view
*base
= display
[0];
193 struct view
*view
= display
[1] ? display
[1] : display
[0];
196 /* Setup window dimensions */
198 getmaxyx(stdscr
, height
, width
);
200 /* Make room for the status window. */
201 base
->height
= height
- 1;
204 vsplit
= vertical_split_is_enabled(opt_vertical_split
, height
, width
);
208 view
->height
= base
->height
;
209 view
->width
= apply_vertical_split(base
->width
);
210 base
->width
-= view
->width
;
212 /* Make room for the separator bar. */
215 create_or_move_display_separator(base
->height
, base
->width
);
216 redraw_display_separator(false);
218 remove_display_separator();
219 apply_horizontal_split(base
, view
);
222 /* Make room for the title bar. */
226 remove_display_separator();
229 /* Make room for the title bar. */
234 foreach_displayed_view (view
, i
) {
235 if (!display_win
[i
]) {
236 display_win
[i
] = newwin(view
->height
, view
->width
, y
, x
);
238 die("Failed to create %s view", view
->name
);
240 scrollok(display_win
[i
], false);
242 display_title
[i
] = newwin(1, view
->width
, y
+ view
->height
, x
);
243 if (!display_title
[i
])
244 die("Failed to create title window");
247 wresize(display_win
[i
], view
->height
, view
->width
);
248 mvwin(display_win
[i
], y
, x
);
249 wresize(display_title
[i
], 1, view
->width
);
250 mvwin(display_title
[i
], y
+ view
->height
, x
);
253 view
->win
= display_win
[i
];
254 view
->title
= display_title
[i
];
257 x
+= view
->width
+ 1;
259 y
+= view
->height
+ 1;
262 redraw_display_separator(false);
266 redraw_display(bool clear
)
271 foreach_displayed_view (view
, i
) {
275 update_view_title(view
);
278 redraw_display_separator(clear
);
282 save_window_line(FILE *file
, WINDOW
*win
, int y
, char *buf
, size_t bufsize
)
284 int read
= mvwinnstr(win
, y
, 0, buf
, bufsize
);
286 return read
== ERR
? false : fprintf(file
, "%s\n", buf
) == read
+ 1;
290 save_window_vline(FILE *file
, WINDOW
*left
, WINDOW
*right
, int y
, char *buf
, size_t bufsize
)
292 int read1
= mvwinnstr(left
, y
, 0, buf
, bufsize
);
293 int read2
= read1
== ERR
? ERR
: mvwinnstr(right
, y
, 0, buf
+ read1
+ 1, bufsize
- read1
- 1);
299 return fprintf(file
, "%s\n", buf
) == read1
+ 1 + read2
+ 1;
303 save_display(const char *path
)
308 FILE *file
= fopen(path
, "w");
310 struct view
*view
= display
[0];
315 getmaxyx(stdscr
, i
, width
);
317 line
= malloc(linelen
+ 1);
323 if (view
->width
< width
&& display
[1]) {
324 struct view
*left
= display
[0],
327 for (i
= 0; ok
&& i
< left
->height
; i
++)
328 ok
= save_window_vline(file
, left
->win
, right
->win
, i
, line
, linelen
);
330 ok
= save_window_vline(file
, left
->title
, right
->title
, 0, line
, linelen
);
334 foreach_displayed_view (view
, j
) {
335 for (i
= 0; ok
&& i
< view
->height
; i
++)
336 ok
= save_window_line(file
, view
->win
, i
, line
, linelen
);
338 ok
= save_window_line(file
, view
->title
, 0, line
, linelen
);
351 /* Whether or not the curses interface has been initialized. */
352 static bool cursed
= false;
354 /* Terminal hacks and workarounds. */
355 static bool use_scroll_redrawwin
;
356 static bool use_scroll_status_wclear
;
358 /* The status window is used for polling keystrokes. */
361 /* Reading from the prompt? */
362 static bool input_mode
= false;
364 static bool status_empty
= false;
366 /* Update status and title window. */
368 update_status_window(struct view
*view
, const char *msg
, va_list args
)
373 if (!status_empty
|| *msg
) {
374 wmove(status_win
, 0, 0);
375 if (view
&& view
->has_scrolled
&& use_scroll_status_wclear
)
378 vwprintw(status_win
, msg
, args
);
379 status_empty
= false;
383 wclrtoeol(status_win
);
391 update_status(const char *msg
, ...)
396 update_status_window(display
[current_view
], msg
, args
);
401 report(const char *msg
, ...)
403 struct view
*view
= display
[current_view
];
407 char buf
[SIZEOF_STR
];
410 FORMAT_BUFFER(buf
, sizeof(buf
), msg
, retval
, true);
415 if (update_status_window(view
, msg
, args
))
416 wnoutrefresh(status_win
);
419 update_view_title(view
);
433 bool no_display
= !!getenv("TIG_NO_DISPLAY");
437 die_callback
= done_display
;
438 /* XXX: Restore tty modes and let the OS cleanup the rest! */
439 if (atexit(done_display
))
440 die("Failed to register done_display");
442 /* Initialize the curses library */
443 if (!no_display
&& isatty(STDIN_FILENO
)) {
444 cursed
= !!initscr();
447 /* Leave stdin and stdout alone when acting as a pager. */
450 opt_tty
= fopen("/dev/tty", "r+");
451 out_tty
= no_display
? fopen("/dev/null", "w+") : opt_tty
;
452 if (!opt_tty
|| !out_tty
)
453 die("Failed to open /dev/tty");
454 cursed
= !!newterm(NULL
, out_tty
, opt_tty
);
458 die("Failed to initialize curses");
460 nonl(); /* Disable conversion and detect newlines from input. */
461 cbreak(); /* Take input chars one at a time, no wait for \n */
462 noecho(); /* Don't echo input */
463 leaveok(stdscr
, false);
467 getmaxyx(stdscr
, y
, x
);
468 status_win
= newwin(1, x
, y
- 1, 0);
470 die("Failed to create status window");
472 /* Enable keyboard mapping */
473 keypad(status_win
, true);
474 wbkgdset(status_win
, get_line_attr(NULL
, LINE_STATUS
));
475 enable_mouse(opt_mouse
);
477 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20080119)
478 set_tabsize(opt_tab_size
);
480 TABSIZE
= opt_tab_size
;
483 term
= getenv("XTERM_VERSION") ? NULL
: getenv("COLORTERM");
484 if (term
&& !strcmp(term
, "gnome-terminal")) {
485 /* In the gnome-terminal-emulator, the warning message
486 * shown when scrolling up one line while the cursor is
487 * on the first line followed by scrolling down one line
488 * corrupts the status line. This is fixed by calling
490 use_scroll_status_wclear
= true;
491 use_scroll_redrawwin
= false;
493 } else if (term
&& !strcmp(term
, "xrvt-xpm")) {
494 /* No problems with full optimizations in xrvt-(unicode)
496 use_scroll_status_wclear
= use_scroll_redrawwin
= false;
499 /* When scrolling in (u)xterm the last line in the
500 * scrolling direction will update slowly. */
501 use_scroll_redrawwin
= true;
502 use_scroll_status_wclear
= false;
507 read_script(struct key
*key
)
509 static struct buffer input_buffer
;
510 static const char *line
= "";
511 enum status_code code
;
513 if (!line
|| !*line
) {
514 if (input_buffer
.data
&& *input_buffer
.data
== ':') {
516 memset(&input_buffer
, 0, sizeof(input_buffer
));
518 } else if (!io_get(&script_io
, &input_buffer
, '\n', true)) {
522 line
= input_buffer
.data
;
526 code
= get_key_value(&line
, key
);
528 die("Error reading script: %s", get_status_message(code
));
535 if (is_script_executing()) {
536 static struct key key
;
537 static int bytes_pos
;
539 if (!key
.modifiers
.multibytes
|| bytes_pos
>= strlen(key
.data
.bytes
)) {
540 if (!read_script(&key
))
545 if (!key
.modifiers
.multibytes
) {
546 if (key
.data
.value
< 128)
547 return key
.data
.value
;
548 die("Only ASCII control characters can be used in prompts: %d", key
.data
.value
);
551 return key
.data
.bytes
[bytes_pos
++];
554 return getc(opt_tty
);
558 get_input(int prompt_position
, struct key
*key
)
561 int i
, key_value
, cursor_y
, cursor_x
;
563 if (prompt_position
> 0)
566 memset(key
, 0, sizeof(*key
));
571 if (opt_refresh_mode
== REFRESH_MODE_PERIODIC
) {
572 delay
= watch_periodic(opt_refresh_interval
);
573 foreach_displayed_view (view
, i
) {
574 if (view_can_refresh(view
) &&
575 watch_dirty(&view
->watch
))
580 foreach_view (view
, i
) {
582 if (view_is_displayed(view
) && view
->has_scrolled
&&
583 use_scroll_redrawwin
)
584 redrawwin(view
->win
);
585 view
->has_scrolled
= false;
590 /* Update the cursor position. */
591 if (prompt_position
) {
592 getbegyx(status_win
, cursor_y
, cursor_x
);
593 cursor_x
= prompt_position
;
595 view
= display
[current_view
];
596 getbegyx(view
->win
, cursor_y
, cursor_x
);
597 cursor_x
+= view
->width
- 1;
598 cursor_y
+= view
->pos
.lineno
- view
->pos
.offset
;
600 set_cursor_pos(cursor_y
, cursor_x
);
602 if (is_script_executing()) {
603 /* Wait for the current command to complete. */
604 if (delay
== 0 || !read_script(key
))
606 return key
->modifiers
.multibytes
? OK
: key
->data
.value
;
609 /* Refresh, accept single keystroke of input */
611 wtimeout(status_win
, delay
);
612 key_value
= wgetch(status_win
);
615 /* wgetch() with nodelay() enabled returns ERR when
616 * there's no input. */
617 if (key_value
== ERR
) {
619 } else if (key_value
== KEY_RESIZE
) {
622 getmaxyx(stdscr
, height
, width
);
624 wresize(status_win
, 1, width
);
625 mvwin(status_win
, height
- 1, 0);
626 wnoutrefresh(status_win
);
628 redraw_display(true);
634 if (key_value
== erasechar())
635 key_value
= KEY_BACKSPACE
;
638 * Ctrl-<key> values are represented using a 0x1F
639 * bitmask on the key value. To 'unmap' we assume that:
641 * - Ctrl-Z is handled by Ncurses.
642 * - Ctrl-m is the same as Return/Enter.
643 * - Ctrl-i is the same as Tab.
645 * For all other key values in the range the Ctrl flag
646 * is set and the key value is updated to the proper
649 if (KEY_CTL('a') <= key_value
&& key_value
<= KEY_CTL('y') &&
650 key_value
!= KEY_RETURN
&& key_value
!= KEY_TAB
) {
651 key
->modifiers
.control
= 1;
652 key_value
= key_value
| 0x40;
655 if ((key_value
>= KEY_MIN
&& key_value
< KEY_MAX
) || key_value
< 0x1F) {
656 key
->data
.value
= key_value
;
657 return key
->data
.value
;
660 key
->modifiers
.multibytes
= 1;
661 key
->data
.bytes
[0] = key_value
;
663 key_length
= utf8_char_length(key
->data
.bytes
);
664 for (pos
= 1; pos
< key_length
&& pos
< sizeof(key
->data
.bytes
) - 1; pos
++) {
665 key
->data
.bytes
[pos
] = wgetch(status_win
);
674 enable_mouse(bool enable
)
676 #ifdef NCURSES_MOUSE_VERSION
677 static bool enabled
= false;
679 if (enable
!= enabled
) {
680 mmask_t mask
= enable
? ALL_MOUSE_EVENTS
: 0;
682 if (mousemask(mask
, NULL
))
689 /* vim: set ts=8 sw=8 noexpandtab: */