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 confirm
, bool refresh
, const char *notice
)
38 def_prog_mode(); /* save current tty modes */
39 endwin(); /* restore original tty modes */
40 ok
= io_run_fg(argv
, dir
);
43 fprintf(stderr
, "%s", notice
);
44 if (!is_script_executing()) {
45 fprintf(stderr
, "Press Enter to continue");
50 if (watch_update(WATCH_EVENT_AFTER_COMMAND
) && refresh
) {
54 foreach_displayed_view (view
, i
) {
55 if (watch_dirty(&view
->watch
))
63 #define EDITOR_LINENO_MSG \
64 "*** Your editor reported an error while opening the file.\n" \
65 "*** This is probably because it doesn't support the line\n" \
66 "*** number argument added automatically. The line number\n" \
67 "*** has been disabled for now. You can permanently disable\n" \
68 "*** it by adding the following line to ~/.tigrc\n" \
69 "*** set editor-line-number = no\n"
72 open_editor(const char *file
, unsigned int lineno
)
74 const char *editor_argv
[SIZEOF_ARG
+ 3] = { "vi", file
, NULL
};
75 char editor_cmd
[SIZEOF_STR
];
76 char lineno_cmd
[SIZEOF_STR
];
80 editor
= getenv("GIT_EDITOR");
81 if (!editor
&& *opt_editor
)
84 editor
= getenv("VISUAL");
86 editor
= getenv("EDITOR");
90 string_ncopy(editor_cmd
, editor
, strlen(editor
));
91 if (!argv_from_string_no_quotes(editor_argv
, &argc
, editor_cmd
)) {
92 report("Failed to read editor command");
96 if (lineno
&& opt_editor_line_number
&& string_format(lineno_cmd
, "+%u", lineno
))
97 editor_argv
[argc
++] = lineno_cmd
;
98 editor_argv
[argc
] = file
;
99 if (!open_external_viewer(editor_argv
, repo
.cdup
, FALSE
, TRUE
, EDITOR_LINENO_MSG
))
100 opt_editor_line_number
= FALSE
;
105 apply_horizontal_split(struct view
*base
, struct view
*view
)
107 view
->width
= base
->width
;
108 view
->height
= apply_step(opt_split_view_height
, base
->height
);
109 view
->height
= MAX(view
->height
, MIN_VIEW_HEIGHT
);
110 view
->height
= MIN(view
->height
, base
->height
- MIN_VIEW_HEIGHT
);
111 base
->height
-= view
->height
;
115 apply_vertical_split(struct view
*base
, struct view
*view
)
117 view
->height
= base
->height
;
118 view
->width
= apply_step(VSPLIT_SCALE
, base
->width
);
119 view
->width
= MAX(view
->width
, MIN_VIEW_WIDTH
);
120 view
->width
= MIN(view
->width
, base
->width
- MIN_VIEW_WIDTH
);
121 base
->width
-= view
->width
;
125 vertical_split_is_enabled(void)
127 if (opt_vertical_split
== VERTICAL_SPLIT_AUTO
) {
130 getmaxyx(stdscr
, height
, width
);
131 return width
> 160 || width
* VSPLIT_SCALE
> (height
- 1) * 2;
134 return opt_vertical_split
== VERTICAL_SPLIT_VERTICAL
;
138 redraw_display_separator(bool clear
)
140 if (displayed_views() > 1 && vertical_split_is_enabled()) {
141 chtype separator
= opt_line_graphics
? ACS_VLINE
: '|';
145 wbkgd(display_sep
, separator
+ get_line_attr(NULL
, LINE_TITLE_BLUR
));
146 wnoutrefresh(display_sep
);
154 struct view
*base
= display
[0];
155 struct view
*view
= display
[1] ? display
[1] : display
[0];
158 /* Setup window dimensions */
160 getmaxyx(stdscr
, base
->height
, base
->width
);
162 /* Make room for the status window. */
165 vsplit
= vertical_split_is_enabled();
169 apply_vertical_split(base
, view
);
171 /* Make room for the separator bar. */
174 apply_horizontal_split(base
, view
);
177 /* Make room for the title bar. */
181 string_format(opt_env_columns
, "COLUMNS=%d", base
->width
);
182 string_format(opt_env_lines
, "LINES=%d", base
->height
);
184 /* Make room for the title bar. */
189 foreach_displayed_view (view
, i
) {
190 if (!display_win
[i
]) {
191 display_win
[i
] = newwin(view
->height
, view
->width
, y
, x
);
193 die("Failed to create %s view", view
->name
);
195 scrollok(display_win
[i
], FALSE
);
197 display_title
[i
] = newwin(1, view
->width
, y
+ view
->height
, x
);
198 if (!display_title
[i
])
199 die("Failed to create title window");
202 wresize(display_win
[i
], view
->height
, view
->width
);
203 mvwin(display_win
[i
], y
, x
);
204 wresize(display_title
[i
], 1, view
->width
);
205 mvwin(display_title
[i
], y
+ view
->height
, x
);
208 if (i
> 0 && vsplit
) {
210 display_sep
= newwin(view
->height
, 1, 0, x
- 1);
212 die("Failed to create separator window");
215 wresize(display_sep
, view
->height
, 1);
216 mvwin(display_sep
, 0, x
- 1);
220 view
->win
= display_win
[i
];
221 view
->title
= display_title
[i
];
224 x
+= view
->width
+ 1;
226 y
+= view
->height
+ 1;
229 redraw_display_separator(FALSE
);
233 redraw_display(bool clear
)
238 foreach_displayed_view (view
, i
) {
242 update_view_title(view
);
245 redraw_display_separator(clear
);
249 save_window_line(FILE *file
, WINDOW
*win
, int y
, char *buf
, size_t bufsize
)
251 int read
= mvwinnstr(win
, y
, 0, buf
, bufsize
);
253 return read
== ERR
? FALSE
: fprintf(file
, "%s\n", buf
) == read
+ 1;
257 save_window_vline(FILE *file
, WINDOW
*left
, WINDOW
*right
, int y
, char *buf
, size_t bufsize
)
259 int read1
= mvwinnstr(left
, y
, 0, buf
, bufsize
);
260 int read2
= read1
== ERR
? ERR
: mvwinnstr(right
, y
, 0, buf
+ read1
+ 1, bufsize
- read1
- 1);
266 return fprintf(file
, "%s\n", buf
) == read1
+ 1 + read2
+ 1;
270 save_display(const char *path
)
274 FILE *file
= fopen(path
, "w");
276 struct view
*view
= display
[0];
281 getmaxyx(stdscr
, i
, width
);
282 line
= malloc(width
+ 1);
288 if (view
->width
< width
) {
289 struct view
*left
= display
[0],
292 for (i
= 0; ok
&& i
< left
->height
; i
++)
293 ok
= save_window_vline(file
, left
->win
, right
->win
, i
, line
, width
);
295 ok
= save_window_vline(file
, left
->title
, right
->title
, 0, line
, width
);
299 foreach_displayed_view (view
, j
) {
300 for (i
= 0; ok
&& i
< view
->height
; i
++)
301 ok
= save_window_line(file
, view
->win
, i
, line
, width
);
303 ok
= save_window_line(file
, view
->title
, 0, line
, width
);
316 /* Whether or not the curses interface has been initialized. */
317 static bool cursed
= FALSE
;
319 /* Terminal hacks and workarounds. */
320 static bool use_scroll_redrawwin
;
321 static bool use_scroll_status_wclear
;
323 /* The status window is used for polling keystrokes. */
326 /* Reading from the prompt? */
327 static bool input_mode
= FALSE
;
329 static bool status_empty
= FALSE
;
331 /* Update status and title window. */
333 update_status_window(struct view
*view
, const char *msg
, va_list args
)
338 if (!status_empty
|| *msg
) {
339 wmove(status_win
, 0, 0);
340 if (view
&& view
->has_scrolled
&& use_scroll_status_wclear
)
343 vwprintw(status_win
, msg
, args
);
344 status_empty
= FALSE
;
348 wclrtoeol(status_win
);
356 update_status(const char *msg
, ...)
361 update_status_window(display
[current_view
], msg
, args
);
366 report(const char *msg
, ...)
368 struct view
*view
= display
[current_view
];
372 char buf
[SIZEOF_STR
];
375 FORMAT_BUFFER(buf
, sizeof(buf
), msg
, retval
, TRUE
);
380 if (update_status_window(view
, msg
, args
))
381 wnoutrefresh(status_win
);
384 update_view_title(view
);
396 bool no_display
= !!getenv("TIG_NO_DISPLAY");
400 die_callback
= done_display
;
401 /* XXX: Restore tty modes and let the OS cleanup the rest! */
402 if (atexit(done_display
))
403 die("Failed to register done_display");
405 /* Initialize the curses library */
406 if (!no_display
&& isatty(STDIN_FILENO
)) {
407 cursed
= !!initscr();
410 /* Leave stdin and stdout alone when acting as a pager. */
413 opt_tty
= fopen("/dev/tty", "r+");
414 out_tty
= no_display
? fopen("/dev/null", "w+") : opt_tty
;
415 if (!opt_tty
|| !out_tty
)
416 die("Failed to open /dev/tty");
417 cursed
= !!newterm(NULL
, out_tty
, opt_tty
);
421 die("Failed to initialize curses");
423 nonl(); /* Disable conversion and detect newlines from input. */
424 cbreak(); /* Take input chars one at a time, no wait for \n */
425 noecho(); /* Don't echo input */
426 leaveok(stdscr
, FALSE
);
430 getmaxyx(stdscr
, y
, x
);
431 status_win
= newwin(1, x
, y
- 1, 0);
433 die("Failed to create status window");
435 /* Enable keyboard mapping */
436 keypad(status_win
, TRUE
);
437 wbkgdset(status_win
, get_line_attr(NULL
, LINE_STATUS
));
438 enable_mouse(opt_mouse
);
440 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20080119)
441 set_tabsize(opt_tab_size
);
443 TABSIZE
= opt_tab_size
;
446 term
= getenv("XTERM_VERSION") ? NULL
: getenv("COLORTERM");
447 if (term
&& !strcmp(term
, "gnome-terminal")) {
448 /* In the gnome-terminal-emulator, the message from
449 * scrolling up one line when impossible followed by
450 * scrolling down one line causes corruption of the
451 * status line. This is fixed by calling wclear. */
452 use_scroll_status_wclear
= TRUE
;
453 use_scroll_redrawwin
= FALSE
;
455 } else if (term
&& !strcmp(term
, "xrvt-xpm")) {
456 /* No problems with full optimizations in xrvt-(unicode)
458 use_scroll_status_wclear
= use_scroll_redrawwin
= FALSE
;
461 /* When scrolling in (u)xterm the last line in the
462 * scrolling direction will update slowly. */
463 use_scroll_redrawwin
= TRUE
;
464 use_scroll_status_wclear
= FALSE
;
468 static struct io script_io
= { -1 };
471 open_script(const char *path
)
473 return io_open(&script_io
, "%s", path
);
477 is_script_executing(void)
479 return script_io
.pipe
!= -1;
483 read_script(struct key
*key
, int delay
)
485 static struct buffer input_buffer
;
486 static const char *line
= "";
487 enum status_code code
;
489 if (!line
|| !*line
) {
490 if (input_buffer
.data
&& *input_buffer
.data
== ':') {
492 memset(&input_buffer
, 0, sizeof(input_buffer
));
494 } else if (!io_get(&script_io
, &input_buffer
, '\n', TRUE
)) {
498 line
= input_buffer
.data
;
502 if (!strcmp(line
, ":wait")) {
504 line
= input_buffer
.data
= NULL
;
508 code
= get_key_value(&line
, key
);
510 die("Error reading script: %s", get_status_message(code
));
517 if (is_script_executing()) {
518 static struct key key
;
519 static int bytes_pos
;
521 if (!key
.modifiers
.multibytes
|| bytes_pos
>= strlen(key
.data
.bytes
)) {
522 if (!read_script(&key
, 0))
527 if (!key
.modifiers
.multibytes
) {
528 if (key
.data
.value
< 128)
529 return key
.data
.value
;
530 die("Only ASCII control characters can be used in prompts: %d", key
.data
.value
);
533 return key
.data
.bytes
[bytes_pos
++];
536 return getc(opt_tty
);
540 get_input(int prompt_position
, struct key
*key
, bool modifiers
)
543 int i
, key_value
, cursor_y
, cursor_x
;
548 memset(key
, 0, sizeof(*key
));
553 if (opt_refresh_mode
== REFRESH_MODE_PERIODIC
) {
554 delay
= watch_periodic(opt_refresh_interval
);
555 foreach_displayed_view (view
, i
) {
556 if (view_can_refresh(view
) &&
557 watch_dirty(&view
->watch
))
562 foreach_view (view
, i
) {
564 if (view_is_displayed(view
) && view
->has_scrolled
&&
565 use_scroll_redrawwin
)
566 redrawwin(view
->win
);
567 view
->has_scrolled
= FALSE
;
572 /* Update the cursor position. */
573 if (prompt_position
) {
574 getbegyx(status_win
, cursor_y
, cursor_x
);
575 cursor_x
= prompt_position
;
577 view
= display
[current_view
];
578 getbegyx(view
->win
, cursor_y
, cursor_x
);
579 cursor_x
= view
->width
- 1;
580 cursor_y
+= view
->pos
.lineno
- view
->pos
.offset
;
582 setsyx(cursor_y
, cursor_x
);
584 if (is_script_executing()) {
585 if (!read_script(key
, delay
))
587 return key
->modifiers
.multibytes
? OK
: key
->data
.value
;
590 /* Refresh, accept single keystroke of input */
592 wtimeout(status_win
, delay
);
593 key_value
= wgetch(status_win
);
596 /* wgetch() with nodelay() enabled returns ERR when
597 * there's no input. */
598 if (key_value
== ERR
) {
600 } else if (key_value
== KEY_ESC
&& modifiers
) {
601 key
->modifiers
.escape
= 1;
603 } else if (key_value
== KEY_RESIZE
) {
606 getmaxyx(stdscr
, height
, width
);
608 wresize(status_win
, 1, width
);
609 mvwin(status_win
, height
- 1, 0);
610 wnoutrefresh(status_win
);
612 redraw_display(TRUE
);
618 if (key_value
== erasechar())
619 key_value
= KEY_BACKSPACE
;
622 * Ctrl-<key> values are represented using a 0x1F
623 * bitmask on the key value. To 'unmap' we assume that:
625 * - Ctrl-Z is handled by Ncurses.
626 * - Ctrl-m is the same as Return/Enter.
627 * - Ctrl-i is the same as Tab.
629 * For all other key values in the range the Ctrl flag
630 * is set and the key value is updated to the proper
633 if (KEY_CTL('a') <= key_value
&& key_value
<= KEY_CTL('y') &&
634 key_value
!= KEY_RETURN
&& key_value
!= KEY_TAB
) {
635 key
->modifiers
.control
= 1;
636 key_value
= key_value
| 0x40;
639 if ((key_value
>= KEY_MIN
&& key_value
< KEY_MAX
) || key_value
< 0x1F) {
640 key
->data
.value
= key_value
;
641 return key
->data
.value
;
644 key
->modifiers
.multibytes
= 1;
645 key
->data
.bytes
[0] = key_value
;
647 key_length
= utf8_char_length(key
->data
.bytes
);
648 for (pos
= 1; pos
< key_length
&& pos
< sizeof(key
->data
.bytes
) - 1; pos
++) {
649 key
->data
.bytes
[pos
] = wgetch(status_win
);
658 enable_mouse(bool enable
)
660 #ifdef NCURSES_MOUSE_VERSION
661 static bool enabled
= FALSE
;
663 if (enable
!= enabled
) {
664 mmask_t mask
= enable
? ALL_MOUSE_EVENTS
: 0;
666 if (mousemask(mask
, NULL
))
673 /* vim: set ts=8 sw=8 noexpandtab: */