Change test scripts to always wait for previous command to finish
[tig.git] / src / tig.c
blob9141acca144c61244f4edec1fa17c4d5c6f043e1
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.
14 #define WARN_MISSING_CURSES_CONFIGURATION
16 #include "tig/tig.h"
17 #include "tig/types.h"
18 #include "tig/util.h"
19 #include "tig/parse.h"
20 #include "tig/io.h"
21 #include "tig/argv.h"
22 #include "tig/refdb.h"
23 #include "tig/watch.h"
24 #include "tig/graph.h"
25 #include "tig/git.h"
26 #include "tig/request.h"
27 #include "tig/line.h"
28 #include "tig/keys.h"
29 #include "tig/view.h"
30 #include "tig/repo.h"
31 #include "tig/options.h"
32 #include "tig/draw.h"
33 #include "tig/display.h"
34 #include "tig/prompt.h"
36 /* Views. */
37 #include "tig/blame.h"
38 #include "tig/blob.h"
39 #include "tig/diff.h"
40 #include "tig/grep.h"
41 #include "tig/help.h"
42 #include "tig/log.h"
43 #include "tig/main.h"
44 #include "tig/pager.h"
45 #include "tig/refs.h"
46 #include "tig/stage.h"
47 #include "tig/stash.h"
48 #include "tig/status.h"
49 #include "tig/tree.h"
51 static bool
52 forward_request_to_child(struct view *child, enum request request)
54 return displayed_views() == 2 && view_is_displayed(child) &&
55 !strcmp(child->vid, child->ops->id);
58 static enum request
59 view_request(struct view *view, enum request request)
61 if (!view || !view->lines)
62 return request;
64 if (request == REQ_ENTER && !opt_focus_child &&
65 view_has_flags(view, VIEW_SEND_CHILD_ENTER)) {
66 struct view *child = display[1];
68 if (forward_request_to_child(child, request)) {
69 view_request(child, request);
70 return REQ_NONE;
74 if (request == REQ_REFRESH && !view_can_refresh(view)) {
75 report("This view can not be refreshed");
76 return REQ_NONE;
79 return view->ops->request(view, request, &view->line[view->pos.lineno]);
83 * Option management
86 #define TOGGLE_MENU_INFO(_) \
87 _('.', "line numbers", "line-number"), \
88 _('D', "dates", "date"), \
89 _('A', "author", "author"), \
90 _('~', "graphics", "line-graphics"), \
91 _('g', "revision graph", "commit-title-graph"), \
92 _('#', "file names", "file-name"), \
93 _('*', "file sizes", "file-size"), \
94 _('W', "space changes", "ignore-space"), \
95 _('l', "commit order", "commit-order"), \
96 _('F', "reference display", "commit-title-refs"), \
97 _('C', "local change display", "show-changes"), \
98 _('X', "commit ID display", "id"), \
99 _('%', "file filtering", "file-filter"), \
100 _('$', "commit title overflow display", "commit-title-overflow"), \
101 _('d', "untracked directory info", "status-untracked-dirs"), \
102 _('|', "view split", "vertical-split"), \
104 static void
105 toggle_option(struct view *view)
107 const struct menu_item menu[] = {
108 #define DEFINE_TOGGLE_MENU(key, help, name) { key, help, name }
109 TOGGLE_MENU_INFO(DEFINE_TOGGLE_MENU)
110 { 0 }
112 const char *toggle_argv[] = { "toggle", NULL, NULL };
113 int i = 0;
115 if (!prompt_menu("Toggle option", menu, &i))
116 return;
118 toggle_argv[1] = menu[i].data;
119 run_prompt_command(view, toggle_argv);
124 * View opening
127 static enum request
128 open_run_request(struct view *view, enum request request)
130 struct run_request *req = get_run_request(request);
131 const char **argv = NULL;
132 bool confirmed = FALSE;
134 request = REQ_NONE;
136 if (!req) {
137 report("Unknown run request");
138 return request;
141 if (!argv_format(view->env, &argv, req->argv, FALSE, TRUE)) {
142 report("Failed to format arguments");
143 return REQ_NONE;
146 if (req->flags.internal) {
147 request = run_prompt_command(view, argv);
149 } else {
150 confirmed = !req->flags.confirm;
152 if (req->flags.confirm) {
153 char cmd[SIZEOF_STR], prompt[SIZEOF_STR];
154 const char *and_exit = req->flags.exit ? " and exit" : "";
156 if (argv_to_string(argv, cmd, sizeof(cmd), " ") &&
157 string_format(prompt, "Run `%s`%s?", cmd, and_exit) &&
158 prompt_yesno(prompt)) {
159 confirmed = TRUE;
163 if (confirmed && argv_remove_quotes(argv))
164 open_external_viewer(argv, NULL, req->flags.silent,
165 !req->flags.exit, FALSE, "");
168 if (argv)
169 argv_free(argv);
170 free(argv);
172 if (request == REQ_NONE) {
173 if (req->flags.confirm && !confirmed)
174 request = REQ_NONE;
176 else if (req->flags.exit)
177 request = REQ_QUIT;
179 else if (!req->flags.internal && watch_dirty(&view->watch))
180 request = REQ_REFRESH;
183 return request;
187 * User request switch noodle
190 static int
191 view_driver(struct view *view, enum request request)
193 int i;
195 if (request == REQ_NONE)
196 return TRUE;
198 if (request >= REQ_RUN_REQUESTS) {
199 request = open_run_request(view, request);
201 // exit quickly rather than going through view_request and back
202 if (request == REQ_QUIT)
203 return FALSE;
206 request = view_request(view, request);
207 if (request == REQ_NONE)
208 return TRUE;
210 switch (request) {
211 case REQ_MOVE_UP:
212 case REQ_MOVE_DOWN:
213 case REQ_MOVE_PAGE_UP:
214 case REQ_MOVE_PAGE_DOWN:
215 case REQ_MOVE_FIRST_LINE:
216 case REQ_MOVE_LAST_LINE:
217 move_view(view, request);
218 break;
220 case REQ_SCROLL_FIRST_COL:
221 case REQ_SCROLL_LEFT:
222 case REQ_SCROLL_RIGHT:
223 case REQ_SCROLL_LINE_DOWN:
224 case REQ_SCROLL_LINE_UP:
225 case REQ_SCROLL_PAGE_DOWN:
226 case REQ_SCROLL_PAGE_UP:
227 case REQ_SCROLL_WHEEL_DOWN:
228 case REQ_SCROLL_WHEEL_UP:
229 scroll_view(view, request);
230 break;
232 case REQ_VIEW_GREP:
233 open_grep_view(view);
234 break;
236 case REQ_VIEW_MAIN:
237 open_main_view(view, OPEN_DEFAULT);
238 break;
239 case REQ_VIEW_DIFF:
240 open_diff_view(view, OPEN_DEFAULT);
241 break;
242 case REQ_VIEW_LOG:
243 open_log_view(view, OPEN_DEFAULT);
244 break;
245 case REQ_VIEW_TREE:
246 open_tree_view(view, OPEN_DEFAULT);
247 break;
248 case REQ_VIEW_HELP:
249 open_help_view(view, OPEN_DEFAULT);
250 break;
251 case REQ_VIEW_REFS:
252 open_refs_view(view, OPEN_DEFAULT);
253 break;
254 case REQ_VIEW_BLAME:
255 open_blame_view(view, OPEN_DEFAULT);
256 break;
257 case REQ_VIEW_BLOB:
258 open_blob_view(view, OPEN_DEFAULT);
259 break;
260 case REQ_VIEW_STATUS:
261 open_status_view(view, OPEN_DEFAULT);
262 break;
263 case REQ_VIEW_STAGE:
264 open_stage_view(view, NULL, 0, OPEN_DEFAULT);
265 break;
266 case REQ_VIEW_PAGER:
267 open_pager_view(view, OPEN_DEFAULT);
268 break;
269 case REQ_VIEW_STASH:
270 open_stash_view(view, OPEN_DEFAULT);
271 break;
273 case REQ_NEXT:
274 case REQ_PREVIOUS:
275 if (view->parent) {
276 int line;
278 view = view->parent;
279 line = view->pos.lineno;
280 view_request(view, request);
281 move_view(view, request);
282 if (view_is_displayed(view))
283 update_view_title(view);
284 if (line != view->pos.lineno)
285 view_request(view, REQ_ENTER);
286 } else {
287 move_view(view, request);
289 break;
291 case REQ_VIEW_NEXT:
293 int nviews = displayed_views();
294 int next_view = (current_view + 1) % nviews;
296 if (next_view == current_view) {
297 report("Only one view is displayed");
298 break;
301 current_view = next_view;
302 /* Blur out the title of the previous view. */
303 update_view_title(view);
304 report_clear();
305 break;
307 case REQ_REFRESH:
308 report("Refreshing is not supported by the %s view", view->name);
309 break;
311 case REQ_PARENT:
312 report("Moving to parent is not supported by the the %s view", view->name);
313 break;
315 case REQ_BACK:
316 report("Going back is not supported for by %s view", view->name);
317 break;
319 case REQ_MAXIMIZE:
320 if (displayed_views() == 2)
321 maximize_view(view, TRUE);
322 break;
324 case REQ_OPTIONS:
325 toggle_option(view);
326 break;
328 case REQ_SEARCH:
329 case REQ_SEARCH_BACK:
330 search_view(view, request);
331 break;
333 case REQ_FIND_NEXT:
334 case REQ_FIND_PREV:
335 find_next(view, request);
336 break;
338 case REQ_STOP_LOADING:
339 foreach_view(view, i) {
340 if (view->pipe)
341 report("Stopped loading the %s view", view->name),
342 end_update(view, TRUE);
344 break;
346 case REQ_SHOW_VERSION:
347 report("tig-%s (built %s)", TIG_VERSION, __DATE__);
348 return TRUE;
350 case REQ_SCREEN_REDRAW:
351 redraw_display(TRUE);
352 break;
354 case REQ_EDIT:
355 report("Nothing to edit");
356 break;
358 case REQ_ENTER:
359 report("Nothing to enter");
360 break;
362 case REQ_VIEW_CLOSE:
363 /* XXX: Mark closed views by letting view->prev point to the
364 * view itself. Parents to closed view should never be
365 * followed. */
366 if (view->prev && view->prev != view) {
367 maximize_view(view->prev, TRUE);
368 view->prev = view;
369 watch_unregister(&view->watch);
370 break;
372 /* Fall-through */
373 case REQ_QUIT:
374 return FALSE;
376 default:
377 report("Unknown key, press %s for help",
378 get_view_key(view, REQ_VIEW_HELP));
379 return TRUE;
382 return TRUE;
386 * Main
389 static const char usage_string[] =
390 "tig " TIG_VERSION " (" __DATE__ ")\n"
391 "\n"
392 "Usage: tig [options] [revs] [--] [paths]\n"
393 " or: tig log [options] [revs] [--] [paths]\n"
394 " or: tig show [options] [revs] [--] [paths]\n"
395 " or: tig blame [options] [rev] [--] path\n"
396 " or: tig grep [options] [pattern]\n"
397 " or: tig stash\n"
398 " or: tig status\n"
399 " or: tig < [git command output]\n"
400 "\n"
401 "Options:\n"
402 " +<number> Select line <number> in the first view\n"
403 " -v, --version Show version and exit\n"
404 " -h, --help Show help message and exit";
406 void
407 usage(const char *message)
409 die("%s\n\n%s", message, usage_string);
412 static int
413 read_filter_args(char *name, size_t namelen, char *value, size_t valuelen, void *data)
415 const char ***filter_args = data;
417 return argv_append(filter_args, name) ? OK : ERR;
420 static void
421 filter_rev_parse(const char ***args, const char *arg1, const char *arg2, const char *argv[])
423 const char *rev_parse_argv[SIZEOF_ARG] = { "git", "rev-parse", arg1, arg2 };
424 const char **all_argv = NULL;
426 if (!argv_append_array(&all_argv, rev_parse_argv) ||
427 !argv_append_array(&all_argv, argv) ||
428 io_run_load(all_argv, "\n", read_filter_args, args) == ERR)
429 die("Failed to split arguments");
430 argv_free(all_argv);
431 free(all_argv);
434 static void
435 filter_options(const char *argv[], bool rev_parse)
437 const char **flags = NULL;
438 int next, flags_pos;
440 update_options_from_argv(argv);
442 if (!rev_parse) {
443 opt_cmdline_argv = argv;
444 return;
447 filter_rev_parse(&opt_file_argv, "--no-revs", "--no-flags", argv);
448 filter_rev_parse(&flags, "--flags", "--no-revs", argv);
450 if (flags) {
451 for (next = flags_pos = 0; flags && flags[next]; next++) {
452 const char *flag = flags[next];
454 if (argv_parse_rev_flag(flag, NULL))
455 argv_append(&opt_rev_argv, flag);
456 else
457 flags[flags_pos++] = flag;
460 flags[flags_pos] = NULL;
462 opt_cmdline_argv = flags;
465 filter_rev_parse(&opt_rev_argv, "--symbolic", "--revs-only", argv);
468 static enum request
469 parse_options(int argc, const char *argv[], bool pager_mode)
471 enum request request;
472 const char *subcommand;
473 bool seen_dashdash = FALSE;
474 bool rev_parse = TRUE;
475 const char **filter_argv = NULL;
476 int i;
478 request = pager_mode ? REQ_VIEW_PAGER : REQ_VIEW_MAIN;
480 if (argc <= 1)
481 return request;
483 subcommand = argv[1];
484 if (!strcmp(subcommand, "status")) {
485 request = REQ_VIEW_STATUS;
487 } else if (!strcmp(subcommand, "blame")) {
488 request = REQ_VIEW_BLAME;
490 } else if (!strcmp(subcommand, "grep")) {
491 request = REQ_VIEW_GREP;
492 rev_parse = FALSE;
494 } else if (!strcmp(subcommand, "show")) {
495 request = REQ_VIEW_DIFF;
497 } else if (!strcmp(subcommand, "log")) {
498 request = REQ_VIEW_LOG;
500 } else if (!strcmp(subcommand, "stash")) {
501 request = REQ_VIEW_STASH;
503 } else {
504 subcommand = NULL;
507 for (i = 1 + !!subcommand; i < argc; i++) {
508 const char *opt = argv[i];
510 // stop parsing our options after -- and let rev-parse handle the rest
511 if (!seen_dashdash) {
512 if (!strcmp(opt, "--")) {
513 seen_dashdash = TRUE;
514 continue;
516 } else if (!strcmp(opt, "-v") || !strcmp(opt, "--version")) {
517 printf("tig version %s\n", TIG_VERSION);
518 exit(EXIT_SUCCESS);
520 } else if (!strcmp(opt, "-h") || !strcmp(opt, "--help")) {
521 printf("%s\n", usage_string);
522 exit(EXIT_SUCCESS);
524 } else if (strlen(opt) >= 2 && *opt == '+' && string_isnumber(opt + 1)) {
525 int lineno = atoi(opt + 1);
527 argv_env.lineno = lineno > 0 ? lineno - 1 : 0;
528 continue;
533 if (!argv_append(&filter_argv, opt))
534 die("command too long");
537 if (filter_argv)
538 filter_options(filter_argv, rev_parse);
540 return request;
543 static enum request
544 open_pager_mode(enum request request)
546 if (request == REQ_VIEW_PAGER) {
547 /* Detect if the user requested the main view. */
548 if (argv_contains(opt_rev_argv, "--stdin")) {
549 open_main_view(NULL, OPEN_FORWARD_STDIN);
550 } else if (argv_contains(opt_cmdline_argv, "--pretty=raw")) {
551 open_main_view(NULL, OPEN_STDIN);
552 } else {
553 open_pager_view(NULL, OPEN_STDIN);
556 } else if (request == REQ_VIEW_DIFF) {
557 if (argv_contains(opt_rev_argv, "--stdin"))
558 open_diff_view(NULL, OPEN_FORWARD_STDIN);
559 else
560 open_diff_view(NULL, OPEN_STDIN);
562 } else {
563 close(STDIN_FILENO);
564 report("Ignoring stdin.");
565 return request;
568 return REQ_NONE;
571 #ifdef NCURSES_MOUSE_VERSION
572 static struct view *
573 find_clicked_view(MEVENT *event)
575 struct view *view;
576 int i;
578 foreach_displayed_view (view, i) {
579 int beg_y = 0, beg_x = 0;
581 getbegyx(view->win, beg_y, beg_x);
583 if (beg_y <= event->y && event->y < beg_y + view->height
584 && beg_x <= event->x && event->x < beg_x + view->width) {
585 if (i != current_view) {
586 current_view = i;
588 return view;
592 return NULL;
595 static enum request
596 handle_mouse_event(void)
598 MEVENT event;
599 struct view *view;
601 if (getmouse(&event) != OK)
602 return REQ_NONE;
604 view = find_clicked_view(&event);
605 if (!view)
606 return REQ_NONE;
608 if (event.bstate & BUTTON2_PRESSED)
609 return REQ_SCROLL_WHEEL_DOWN;
611 if (event.bstate & BUTTON4_PRESSED)
612 return REQ_SCROLL_WHEEL_UP;
614 if (event.bstate & BUTTON1_PRESSED) {
615 if (event.y == view->pos.lineno - view->pos.offset) {
616 /* Click is on the same line, perform an "ENTER" */
617 return REQ_ENTER;
619 } else {
620 int y = getbegy(view->win);
621 unsigned long lineno = (event.y - y) + view->pos.offset;
623 select_view_line(view, lineno);
624 update_view_title(view);
625 report_clear();
629 return REQ_NONE;
631 #endif
634 main(int argc, const char *argv[])
636 const char *codeset = ENCODING_UTF8;
637 bool pager_mode = !isatty(STDIN_FILENO);
638 enum request request = parse_options(argc, argv, pager_mode);
639 struct view *view;
641 prompt_init();
643 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
644 die("Failed to setup signal handler");
646 if (setlocale(LC_ALL, "")) {
647 codeset = nl_langinfo(CODESET);
650 if (load_repo_info() == ERR)
651 die("Failed to load repo info.");
653 if (load_options() == ERR)
654 die("Failed to load user config.");
656 if (load_git_config() == ERR)
657 die("Failed to load repo config.");
659 /* Require a git repository unless when running in pager mode. */
660 if (!repo.git_dir[0] && request != REQ_VIEW_PAGER)
661 die("Not a git repository");
663 if (codeset && strcmp(codeset, ENCODING_UTF8)) {
664 char translit[SIZEOF_STR];
666 if (string_format(translit, "%s%s", codeset, ICONV_TRANSLIT))
667 opt_iconv_out = iconv_open(translit, ENCODING_UTF8);
668 else
669 opt_iconv_out = iconv_open(codeset, ENCODING_UTF8);
670 if (opt_iconv_out == ICONV_NONE)
671 die("Failed to initialize character set conversion");
674 if (load_refs(FALSE) == ERR)
675 die("Failed to load refs.");
677 init_display();
679 if (pager_mode)
680 request = open_pager_mode(request);
682 if (getenv("TIG_SCRIPT")) {
683 const char *script_command[] = { "script", getenv("TIG_SCRIPT"), NULL };
685 if (!displayed_views()) {
686 /* Open a 'neutral' view. */
687 open_help_view(NULL, OPEN_DEFAULT);
690 request = run_prompt_command(NULL, script_command);
693 while (view_driver(display[current_view], request)) {
694 struct key key;
695 int key_value = get_input(0, &key, TRUE);
697 #ifdef NCURSES_MOUSE_VERSION
698 if (key_value == KEY_MOUSE) {
699 request = handle_mouse_event();
700 continue;
702 #endif
704 view = display[current_view];
705 request = get_keybinding(view->keymap, &key, 1);
707 /* Some low-level request handling. This keeps access to
708 * status_win restricted. */
709 switch (request) {
710 case REQ_NONE:
711 report("Unknown key, press %s for help",
712 get_view_key(view, REQ_VIEW_HELP));
713 break;
714 case REQ_PROMPT:
715 request = open_prompt(view);
716 break;
717 case REQ_SEARCH:
718 case REQ_SEARCH_BACK:
720 const char *prompt = request == REQ_SEARCH ? "/" : "?";
721 char *search = read_prompt(prompt);
723 if (search)
724 string_ncopy(argv_env.search, search, strlen(search));
725 else if (*argv_env.search)
726 request = request == REQ_SEARCH ?
727 REQ_FIND_NEXT :
728 REQ_FIND_PREV;
729 else
730 request = REQ_NONE;
731 break;
733 default:
734 break;
738 exit(EXIT_SUCCESS);
740 return 0;
743 /* vim: set ts=8 sw=8 noexpandtab: */