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/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
;
34 open_external_viewer(const char *argv
[], const char *dir
, bool silent
, bool confirm
, bool refresh
, const char *notice
)
42 def_prog_mode(); /* save current tty modes */
43 endwin(); /* restore original tty modes */
44 ok
= io_run_fg(argv
, dir
);
47 fprintf(stderr
, "%s", notice
);
48 if (!is_script_executing()) {
49 fprintf(stderr
, "Press Enter to continue");
56 if (watch_update(WATCH_EVENT_AFTER_COMMAND
) && refresh
) {
60 foreach_displayed_view (view
, i
) {
61 if (watch_dirty(&view
->watch
))
69 #define EDITOR_LINENO_MSG \
70 "*** Your editor reported an error while opening the file.\n" \
71 "*** This is probably because it doesn't support the line\n" \
72 "*** number argument added automatically. The line number\n" \
73 "*** has been disabled for now. You can permanently disable\n" \
74 "*** it by adding the following line to ~/.tigrc\n" \
75 "*** set editor-line-number = no\n"
78 open_editor(const char *file
, unsigned int lineno
)
80 const char *editor_argv
[SIZEOF_ARG
+ 3] = { "vi", file
, NULL
};
81 char editor_cmd
[SIZEOF_STR
];
82 char lineno_cmd
[SIZEOF_STR
];
86 editor
= getenv("GIT_EDITOR");
87 if (!editor
&& *opt_editor
)
90 editor
= getenv("VISUAL");
92 editor
= getenv("EDITOR");
96 string_ncopy(editor_cmd
, editor
, strlen(editor
));
97 if (!argv_from_string_no_quotes(editor_argv
, &argc
, editor_cmd
)) {
98 report("Failed to read editor command");
102 if (lineno
&& opt_editor_line_number
&& string_format(lineno_cmd
, "+%u", lineno
))
103 editor_argv
[argc
++] = lineno_cmd
;
104 editor_argv
[argc
] = file
;
105 if (!open_external_viewer(editor_argv
, repo
.cdup
, FALSE
, FALSE
, TRUE
, EDITOR_LINENO_MSG
))
106 opt_editor_line_number
= FALSE
;
111 apply_horizontal_split(struct view
*base
, struct view
*view
)
113 view
->width
= base
->width
;
114 view
->height
= apply_step(opt_split_view_height
, base
->height
);
115 view
->height
= MAX(view
->height
, MIN_VIEW_HEIGHT
);
116 view
->height
= MIN(view
->height
, base
->height
- MIN_VIEW_HEIGHT
);
117 base
->height
-= view
->height
;
121 apply_vertical_split(struct view
*base
, struct view
*view
)
123 view
->height
= base
->height
;
124 view
->width
= apply_step(opt_split_view_width
, base
->width
);
125 view
->width
= MAX(view
->width
, MIN_VIEW_WIDTH
);
126 view
->width
= MIN(view
->width
, base
->width
- MIN_VIEW_WIDTH
);
127 base
->width
-= view
->width
;
131 vertical_split_is_enabled(void)
133 if (opt_vertical_split
== VERTICAL_SPLIT_AUTO
) {
136 getmaxyx(stdscr
, height
, width
);
137 return width
> 160 || width
* VSPLIT_SCALE
> (height
- 1) * 2;
140 return opt_vertical_split
== VERTICAL_SPLIT_VERTICAL
;
144 redraw_display_separator(bool clear
)
146 if (displayed_views() > 1 && vertical_split_is_enabled()) {
147 chtype separator
= opt_line_graphics
? ACS_VLINE
: '|';
151 wbkgd(display_sep
, separator
+ get_line_attr(NULL
, LINE_TITLE_BLUR
));
152 wnoutrefresh(display_sep
);
160 struct view
*base
= display
[0];
161 struct view
*view
= display
[1] ? display
[1] : display
[0];
164 /* Setup window dimensions */
166 getmaxyx(stdscr
, base
->height
, base
->width
);
168 /* Make room for the status window. */
171 vsplit
= vertical_split_is_enabled();
175 apply_vertical_split(base
, view
);
177 /* Make room for the separator bar. */
180 apply_horizontal_split(base
, view
);
183 /* Make room for the title bar. */
187 string_format(opt_env_columns
, "COLUMNS=%d", base
->width
);
188 string_format(opt_env_lines
, "LINES=%d", base
->height
);
190 /* Make room for the title bar. */
195 foreach_displayed_view (view
, i
) {
196 if (!display_win
[i
]) {
197 display_win
[i
] = newwin(view
->height
, view
->width
, y
, x
);
199 die("Failed to create %s view", view
->name
);
201 scrollok(display_win
[i
], FALSE
);
203 display_title
[i
] = newwin(1, view
->width
, y
+ view
->height
, x
);
204 if (!display_title
[i
])
205 die("Failed to create title window");
208 wresize(display_win
[i
], view
->height
, view
->width
);
209 mvwin(display_win
[i
], y
, x
);
210 wresize(display_title
[i
], 1, view
->width
);
211 mvwin(display_title
[i
], y
+ view
->height
, x
);
214 if (i
> 0 && vsplit
) {
216 display_sep
= newwin(view
->height
, 1, 0, x
- 1);
218 die("Failed to create separator window");
221 wresize(display_sep
, view
->height
, 1);
222 mvwin(display_sep
, 0, x
- 1);
226 view
->win
= display_win
[i
];
227 view
->title
= display_title
[i
];
230 x
+= view
->width
+ 1;
232 y
+= view
->height
+ 1;
235 redraw_display_separator(FALSE
);
239 redraw_display(bool clear
)
244 foreach_displayed_view (view
, i
) {
248 update_view_title(view
);
251 redraw_display_separator(clear
);
255 save_window_line(FILE *file
, WINDOW
*win
, int y
, char *buf
, size_t bufsize
)
257 int read
= mvwinnstr(win
, y
, 0, buf
, bufsize
);
259 return read
== ERR
? FALSE
: fprintf(file
, "%s\n", buf
) == read
+ 1;
263 save_window_vline(FILE *file
, WINDOW
*left
, WINDOW
*right
, int y
, char *buf
, size_t bufsize
)
265 int read1
= mvwinnstr(left
, y
, 0, buf
, bufsize
);
266 int read2
= read1
== ERR
? ERR
: mvwinnstr(right
, y
, 0, buf
+ read1
+ 1, bufsize
- read1
- 1);
272 return fprintf(file
, "%s\n", buf
) == read1
+ 1 + read2
+ 1;
276 save_display(const char *path
)
280 FILE *file
= fopen(path
, "w");
282 struct view
*view
= display
[0];
287 getmaxyx(stdscr
, i
, width
);
288 line
= malloc(width
+ 1);
294 if (view
->width
< width
&& display
[1]) {
295 struct view
*left
= display
[0],
298 for (i
= 0; ok
&& i
< left
->height
; i
++)
299 ok
= save_window_vline(file
, left
->win
, right
->win
, i
, line
, width
);
301 ok
= save_window_vline(file
, left
->title
, right
->title
, 0, line
, width
);
305 foreach_displayed_view (view
, j
) {
306 for (i
= 0; ok
&& i
< view
->height
; i
++)
307 ok
= save_window_line(file
, view
->win
, i
, line
, width
);
309 ok
= save_window_line(file
, view
->title
, 0, line
, width
);
322 /* Whether or not the curses interface has been initialized. */
323 static bool cursed
= FALSE
;
325 /* Terminal hacks and workarounds. */
326 static bool use_scroll_redrawwin
;
327 static bool use_scroll_status_wclear
;
329 /* The status window is used for polling keystrokes. */
332 /* Reading from the prompt? */
333 static bool input_mode
= FALSE
;
335 static bool status_empty
= FALSE
;
337 /* Update status and title window. */
339 update_status_window(struct view
*view
, const char *msg
, va_list args
)
344 if (!status_empty
|| *msg
) {
345 wmove(status_win
, 0, 0);
346 if (view
&& view
->has_scrolled
&& use_scroll_status_wclear
)
349 vwprintw(status_win
, msg
, args
);
350 status_empty
= FALSE
;
354 wclrtoeol(status_win
);
362 update_status(const char *msg
, ...)
367 update_status_window(display
[current_view
], msg
, args
);
372 report(const char *msg
, ...)
374 struct view
*view
= display
[current_view
];
378 char buf
[SIZEOF_STR
];
381 FORMAT_BUFFER(buf
, sizeof(buf
), msg
, retval
, TRUE
);
386 if (update_status_window(view
, msg
, args
))
387 wnoutrefresh(status_win
);
390 update_view_title(view
);
402 bool no_display
= !!getenv("TIG_NO_DISPLAY");
406 die_callback
= done_display
;
407 /* XXX: Restore tty modes and let the OS cleanup the rest! */
408 if (atexit(done_display
))
409 die("Failed to register done_display");
411 /* Initialize the curses library */
412 if (!no_display
&& isatty(STDIN_FILENO
)) {
413 cursed
= !!initscr();
416 /* Leave stdin and stdout alone when acting as a pager. */
419 opt_tty
= fopen("/dev/tty", "r+");
420 out_tty
= no_display
? fopen("/dev/null", "w+") : opt_tty
;
421 if (!opt_tty
|| !out_tty
)
422 die("Failed to open /dev/tty");
423 cursed
= !!newterm(NULL
, out_tty
, opt_tty
);
427 die("Failed to initialize curses");
429 nonl(); /* Disable conversion and detect newlines from input. */
430 cbreak(); /* Take input chars one at a time, no wait for \n */
431 noecho(); /* Don't echo input */
432 leaveok(stdscr
, FALSE
);
436 getmaxyx(stdscr
, y
, x
);
437 status_win
= newwin(1, x
, y
- 1, 0);
439 die("Failed to create status window");
441 /* Enable keyboard mapping */
442 keypad(status_win
, TRUE
);
443 wbkgdset(status_win
, get_line_attr(NULL
, LINE_STATUS
));
444 enable_mouse(opt_mouse
);
446 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20080119)
447 set_tabsize(opt_tab_size
);
449 TABSIZE
= opt_tab_size
;
452 term
= getenv("XTERM_VERSION") ? NULL
: getenv("COLORTERM");
453 if (term
&& !strcmp(term
, "gnome-terminal")) {
454 /* In the gnome-terminal-emulator, the warning message
455 * shown when scrolling up one line while the cursor is
456 * on the first line followed by scrolling down one line
457 * corrupts the status line. This is fixed by calling
459 use_scroll_status_wclear
= TRUE
;
460 use_scroll_redrawwin
= FALSE
;
462 } else if (term
&& !strcmp(term
, "xrvt-xpm")) {
463 /* No problems with full optimizations in xrvt-(unicode)
465 use_scroll_status_wclear
= use_scroll_redrawwin
= FALSE
;
468 /* When scrolling in (u)xterm the last line in the
469 * scrolling direction will update slowly. */
470 use_scroll_redrawwin
= TRUE
;
471 use_scroll_status_wclear
= FALSE
;
475 static struct io script_io
= { -1 };
478 open_script(const char *path
)
480 return io_open(&script_io
, "%s", path
);
484 is_script_executing(void)
486 return script_io
.pipe
!= -1;
490 read_script(struct key
*key
)
492 static struct buffer input_buffer
;
493 static const char *line
= "";
494 enum status_code code
;
496 if (!line
|| !*line
) {
497 if (input_buffer
.data
&& *input_buffer
.data
== ':') {
499 memset(&input_buffer
, 0, sizeof(input_buffer
));
501 } else if (!io_get(&script_io
, &input_buffer
, '\n', TRUE
)) {
505 line
= input_buffer
.data
;
509 code
= get_key_value(&line
, key
);
511 die("Error reading script: %s", get_status_message(code
));
518 if (is_script_executing()) {
519 static struct key key
;
520 static int bytes_pos
;
522 if (!key
.modifiers
.multibytes
|| bytes_pos
>= strlen(key
.data
.bytes
)) {
523 if (!read_script(&key
))
528 if (!key
.modifiers
.multibytes
) {
529 if (key
.data
.value
< 128)
530 return key
.data
.value
;
531 die("Only ASCII control characters can be used in prompts: %d", key
.data
.value
);
534 return key
.data
.bytes
[bytes_pos
++];
537 return getc(opt_tty
);
541 get_input(int prompt_position
, struct key
*key
, bool modifiers
)
544 int i
, key_value
, cursor_y
, cursor_x
;
549 memset(key
, 0, sizeof(*key
));
554 if (opt_refresh_mode
== REFRESH_MODE_PERIODIC
) {
555 delay
= watch_periodic(opt_refresh_interval
);
556 foreach_displayed_view (view
, i
) {
557 if (view_can_refresh(view
) &&
558 watch_dirty(&view
->watch
))
563 foreach_view (view
, i
) {
565 if (view_is_displayed(view
) && view
->has_scrolled
&&
566 use_scroll_redrawwin
)
567 redrawwin(view
->win
);
568 view
->has_scrolled
= FALSE
;
573 /* Update the cursor position. */
574 if (prompt_position
) {
575 getbegyx(status_win
, cursor_y
, cursor_x
);
576 cursor_x
= prompt_position
;
578 view
= display
[current_view
];
579 getbegyx(view
->win
, cursor_y
, cursor_x
);
580 cursor_x
+= view
->width
- 1;
581 cursor_y
+= view
->pos
.lineno
- view
->pos
.offset
;
583 setsyx(cursor_y
, cursor_x
);
585 if (is_script_executing()) {
586 /* Wait for the current command to complete. */
587 if (delay
== 0 || !read_script(key
))
589 return key
->modifiers
.multibytes
? OK
: key
->data
.value
;
592 /* Refresh, accept single keystroke of input */
594 wtimeout(status_win
, delay
);
595 key_value
= wgetch(status_win
);
598 /* wgetch() with nodelay() enabled returns ERR when
599 * there's no input. */
600 if (key_value
== ERR
) {
602 } else if (key_value
== KEY_ESC
&& modifiers
) {
603 key
->modifiers
.escape
= 1;
605 } else if (key_value
== KEY_RESIZE
) {
608 getmaxyx(stdscr
, height
, width
);
610 wresize(status_win
, 1, width
);
611 mvwin(status_win
, height
- 1, 0);
612 wnoutrefresh(status_win
);
614 redraw_display(TRUE
);
620 if (key_value
== erasechar())
621 key_value
= KEY_BACKSPACE
;
624 * Ctrl-<key> values are represented using a 0x1F
625 * bitmask on the key value. To 'unmap' we assume that:
627 * - Ctrl-Z is handled by Ncurses.
628 * - Ctrl-m is the same as Return/Enter.
629 * - Ctrl-i is the same as Tab.
631 * For all other key values in the range the Ctrl flag
632 * is set and the key value is updated to the proper
635 if (KEY_CTL('a') <= key_value
&& key_value
<= KEY_CTL('y') &&
636 key_value
!= KEY_RETURN
&& key_value
!= KEY_TAB
) {
637 key
->modifiers
.control
= 1;
638 key_value
= key_value
| 0x40;
641 if ((key_value
>= KEY_MIN
&& key_value
< KEY_MAX
) || key_value
< 0x1F) {
642 key
->data
.value
= key_value
;
643 return key
->data
.value
;
646 key
->modifiers
.multibytes
= 1;
647 key
->data
.bytes
[0] = key_value
;
649 key_length
= utf8_char_length(key
->data
.bytes
);
650 for (pos
= 1; pos
< key_length
&& pos
< sizeof(key
->data
.bytes
) - 1; pos
++) {
651 key
->data
.bytes
[pos
] = wgetch(status_win
);
660 enable_mouse(bool enable
)
662 #ifdef NCURSES_MOUSE_VERSION
663 static bool enabled
= FALSE
;
665 if (enable
!= enabled
) {
666 mmask_t mask
= enable
? ALL_MOUSE_EVENTS
: 0;
668 if (mousemask(mask
, NULL
))
675 /* vim: set ts=8 sw=8 noexpandtab: */