Handle binary files matches reported by git-grep
[tig.git] / src / tig.c
blob4dea15551129c2218af214b3f0aa1c843e5f581d
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 refs\n"
398 " or: tig stash\n"
399 " or: tig status\n"
400 " or: tig < [git command output]\n"
401 "\n"
402 "Options:\n"
403 " +<number> Select line <number> in the first view\n"
404 " -v, --version Show version and exit\n"
405 " -h, --help Show help message and exit";
407 void
408 usage(const char *message)
410 die("%s\n\n%s", message, usage_string);
413 static int
414 read_filter_args(char *name, size_t namelen, char *value, size_t valuelen, void *data)
416 const char ***filter_args = data;
418 return argv_append(filter_args, name) ? OK : ERR;
421 static void
422 filter_rev_parse(const char ***args, const char *arg1, const char *arg2, const char *argv[])
424 const char *rev_parse_argv[SIZEOF_ARG] = { "git", "rev-parse", arg1, arg2 };
425 const char **all_argv = NULL;
427 if (!argv_append_array(&all_argv, rev_parse_argv) ||
428 !argv_append_array(&all_argv, argv) ||
429 io_run_load(all_argv, "\n", read_filter_args, args) == ERR)
430 die("Failed to split arguments");
431 argv_free(all_argv);
432 free(all_argv);
435 static void
436 filter_options(const char *argv[], bool rev_parse)
438 const char **flags = NULL;
439 int next, flags_pos;
441 update_options_from_argv(argv);
443 if (!rev_parse) {
444 opt_cmdline_argv = argv;
445 return;
448 filter_rev_parse(&opt_file_argv, "--no-revs", "--no-flags", argv);
449 filter_rev_parse(&flags, "--flags", "--no-revs", argv);
451 if (flags) {
452 for (next = flags_pos = 0; flags && flags[next]; next++) {
453 const char *flag = flags[next];
455 if (argv_parse_rev_flag(flag, NULL))
456 argv_append(&opt_rev_argv, flag);
457 else
458 flags[flags_pos++] = flag;
461 flags[flags_pos] = NULL;
463 opt_cmdline_argv = flags;
466 filter_rev_parse(&opt_rev_argv, "--symbolic", "--revs-only", argv);
469 static enum request
470 parse_options(int argc, const char *argv[], bool pager_mode)
472 enum request request;
473 const char *subcommand;
474 bool seen_dashdash = FALSE;
475 bool rev_parse = TRUE;
476 const char **filter_argv = NULL;
477 int i;
479 request = pager_mode ? REQ_VIEW_PAGER : REQ_VIEW_MAIN;
481 if (argc <= 1)
482 return request;
484 subcommand = argv[1];
485 if (!strcmp(subcommand, "status")) {
486 request = REQ_VIEW_STATUS;
488 } else if (!strcmp(subcommand, "blame")) {
489 request = REQ_VIEW_BLAME;
491 } else if (!strcmp(subcommand, "grep")) {
492 request = REQ_VIEW_GREP;
493 rev_parse = FALSE;
495 } else if (!strcmp(subcommand, "show")) {
496 request = REQ_VIEW_DIFF;
498 } else if (!strcmp(subcommand, "log")) {
499 request = REQ_VIEW_LOG;
501 } else if (!strcmp(subcommand, "stash")) {
502 request = REQ_VIEW_STASH;
504 } else if (!strcmp(subcommand, "refs")) {
505 request = REQ_VIEW_REFS;
507 } else {
508 subcommand = NULL;
511 for (i = 1 + !!subcommand; i < argc; i++) {
512 const char *opt = argv[i];
514 // stop parsing our options after -- and let rev-parse handle the rest
515 if (!seen_dashdash) {
516 if (!strcmp(opt, "--")) {
517 seen_dashdash = TRUE;
518 continue;
520 } else if (!strcmp(opt, "-v") || !strcmp(opt, "--version")) {
521 printf("tig version %s\n", TIG_VERSION);
522 exit(EXIT_SUCCESS);
524 } else if (!strcmp(opt, "-h") || !strcmp(opt, "--help")) {
525 printf("%s\n", usage_string);
526 exit(EXIT_SUCCESS);
528 } else if (strlen(opt) >= 2 && *opt == '+' && string_isnumber(opt + 1)) {
529 int lineno = atoi(opt + 1);
531 argv_env.lineno = lineno > 0 ? lineno - 1 : 0;
532 continue;
537 if (!argv_append(&filter_argv, opt))
538 die("command too long");
541 if (filter_argv)
542 filter_options(filter_argv, rev_parse);
544 return request;
547 static enum request
548 open_pager_mode(enum request request)
550 if (request == REQ_VIEW_PAGER) {
551 /* Detect if the user requested the main view. */
552 if (argv_contains(opt_rev_argv, "--stdin")) {
553 open_main_view(NULL, OPEN_FORWARD_STDIN);
554 } else if (argv_contains(opt_cmdline_argv, "--pretty=raw")) {
555 open_main_view(NULL, OPEN_STDIN);
556 } else {
557 open_pager_view(NULL, OPEN_STDIN);
560 } else if (request == REQ_VIEW_DIFF) {
561 if (argv_contains(opt_rev_argv, "--stdin"))
562 open_diff_view(NULL, OPEN_FORWARD_STDIN);
563 else
564 open_diff_view(NULL, OPEN_STDIN);
566 } else {
567 close(STDIN_FILENO);
568 report("Ignoring stdin.");
569 return request;
572 return REQ_NONE;
575 #ifdef NCURSES_MOUSE_VERSION
576 static struct view *
577 find_clicked_view(MEVENT *event)
579 struct view *view;
580 int i;
582 foreach_displayed_view (view, i) {
583 int beg_y = 0, beg_x = 0;
585 getbegyx(view->win, beg_y, beg_x);
587 if (beg_y <= event->y && event->y < beg_y + view->height
588 && beg_x <= event->x && event->x < beg_x + view->width) {
589 if (i != current_view) {
590 current_view = i;
592 return view;
596 return NULL;
599 static enum request
600 handle_mouse_event(void)
602 MEVENT event;
603 struct view *view;
605 if (getmouse(&event) != OK)
606 return REQ_NONE;
608 view = find_clicked_view(&event);
609 if (!view)
610 return REQ_NONE;
612 if (event.bstate & BUTTON2_PRESSED)
613 return REQ_SCROLL_WHEEL_DOWN;
615 if (event.bstate & BUTTON4_PRESSED)
616 return REQ_SCROLL_WHEEL_UP;
618 if (event.bstate & BUTTON1_PRESSED) {
619 if (event.y == view->pos.lineno - view->pos.offset) {
620 /* Click is on the same line, perform an "ENTER" */
621 return REQ_ENTER;
623 } else {
624 int y = getbegy(view->win);
625 unsigned long lineno = (event.y - y) + view->pos.offset;
627 select_view_line(view, lineno);
628 update_view_title(view);
629 report_clear();
633 return REQ_NONE;
635 #endif
638 main(int argc, const char *argv[])
640 const char *codeset = ENCODING_UTF8;
641 bool pager_mode = !isatty(STDIN_FILENO);
642 enum request request = parse_options(argc, argv, pager_mode);
643 struct view *view;
645 prompt_init();
647 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
648 die("Failed to setup signal handler");
650 if (setlocale(LC_ALL, "")) {
651 codeset = nl_langinfo(CODESET);
654 if (load_repo_info() == ERR)
655 die("Failed to load repo info.");
657 if (load_options() == ERR)
658 die("Failed to load user config.");
660 if (load_git_config() == ERR)
661 die("Failed to load repo config.");
663 /* Require a git repository unless when running in pager mode. */
664 if (!repo.git_dir[0] && request != REQ_VIEW_PAGER)
665 die("Not a git repository");
667 if (codeset && strcmp(codeset, ENCODING_UTF8)) {
668 char translit[SIZEOF_STR];
670 if (string_format(translit, "%s%s", codeset, ICONV_TRANSLIT))
671 opt_iconv_out = iconv_open(translit, ENCODING_UTF8);
672 else
673 opt_iconv_out = iconv_open(codeset, ENCODING_UTF8);
674 if (opt_iconv_out == ICONV_NONE)
675 die("Failed to initialize character set conversion");
678 if (load_refs(FALSE) == ERR)
679 die("Failed to load refs.");
681 init_display();
683 if (pager_mode)
684 request = open_pager_mode(request);
686 if (getenv("TIG_SCRIPT")) {
687 const char *script_command[] = { "script", getenv("TIG_SCRIPT"), NULL };
689 if (!displayed_views()) {
690 /* Open a 'neutral' view. */
691 open_help_view(NULL, OPEN_DEFAULT);
694 request = run_prompt_command(NULL, script_command);
697 while (view_driver(display[current_view], request)) {
698 struct key key;
699 int key_value = get_input(0, &key, TRUE);
701 #ifdef NCURSES_MOUSE_VERSION
702 if (key_value == KEY_MOUSE) {
703 request = handle_mouse_event();
704 continue;
706 #endif
708 view = display[current_view];
709 request = get_keybinding(view->keymap, &key, 1);
711 /* Some low-level request handling. This keeps access to
712 * status_win restricted. */
713 switch (request) {
714 case REQ_NONE:
715 report("Unknown key, press %s for help",
716 get_view_key(view, REQ_VIEW_HELP));
717 break;
718 case REQ_PROMPT:
719 request = open_prompt(view);
720 break;
721 case REQ_SEARCH:
722 case REQ_SEARCH_BACK:
724 const char *prompt = request == REQ_SEARCH ? "/" : "?";
725 char *search = read_prompt(prompt);
727 if (search)
728 string_ncopy(argv_env.search, search, strlen(search));
729 else if (*argv_env.search)
730 request = request == REQ_SEARCH ?
731 REQ_FIND_NEXT :
732 REQ_FIND_PREV;
733 else
734 request = REQ_NONE;
735 break;
737 default:
738 break;
742 exit(EXIT_SUCCESS);
744 return 0;
747 /* vim: set ts=8 sw=8 noexpandtab: */