Add :exec prompt command and inline test commands that uses key bindings
[tig.git] / src / tig.c
blobf3c2a08bfb6c82b982df17128de712feee24ca64
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);
132 if (!req) {
133 report("Unknown run request");
134 return REQ_NONE;
137 return exec_run_request(view, req);
141 * User request switch noodle
144 static int
145 view_driver(struct view *view, enum request request)
147 int i;
149 if (request == REQ_NONE)
150 return TRUE;
152 if (request >= REQ_RUN_REQUESTS) {
153 request = open_run_request(view, request);
155 // exit quickly rather than going through view_request and back
156 if (request == REQ_QUIT)
157 return FALSE;
160 request = view_request(view, request);
161 if (request == REQ_NONE)
162 return TRUE;
164 switch (request) {
165 case REQ_MOVE_UP:
166 case REQ_MOVE_DOWN:
167 case REQ_MOVE_PAGE_UP:
168 case REQ_MOVE_PAGE_DOWN:
169 case REQ_MOVE_HALF_PAGE_UP:
170 case REQ_MOVE_HALF_PAGE_DOWN:
171 case REQ_MOVE_FIRST_LINE:
172 case REQ_MOVE_LAST_LINE:
173 move_view(view, request);
174 break;
176 case REQ_SCROLL_FIRST_COL:
177 case REQ_SCROLL_LEFT:
178 case REQ_SCROLL_RIGHT:
179 case REQ_SCROLL_LINE_DOWN:
180 case REQ_SCROLL_LINE_UP:
181 case REQ_SCROLL_PAGE_DOWN:
182 case REQ_SCROLL_PAGE_UP:
183 case REQ_SCROLL_WHEEL_DOWN:
184 case REQ_SCROLL_WHEEL_UP:
185 scroll_view(view, request);
186 break;
188 case REQ_VIEW_GREP:
189 open_grep_view(view);
190 break;
192 case REQ_VIEW_MAIN:
193 open_main_view(view, OPEN_DEFAULT);
194 break;
195 case REQ_VIEW_DIFF:
196 open_diff_view(view, OPEN_DEFAULT);
197 break;
198 case REQ_VIEW_LOG:
199 open_log_view(view, OPEN_DEFAULT);
200 break;
201 case REQ_VIEW_TREE:
202 open_tree_view(view, OPEN_DEFAULT);
203 break;
204 case REQ_VIEW_HELP:
205 open_help_view(view, OPEN_DEFAULT);
206 break;
207 case REQ_VIEW_REFS:
208 open_refs_view(view, OPEN_DEFAULT);
209 break;
210 case REQ_VIEW_BLAME:
211 open_blame_view(view, OPEN_DEFAULT);
212 break;
213 case REQ_VIEW_BLOB:
214 open_blob_view(view, OPEN_DEFAULT);
215 break;
216 case REQ_VIEW_STATUS:
217 open_status_view(view, OPEN_DEFAULT);
218 break;
219 case REQ_VIEW_STAGE:
220 open_stage_view(view, NULL, 0, OPEN_DEFAULT);
221 break;
222 case REQ_VIEW_PAGER:
223 open_pager_view(view, OPEN_DEFAULT);
224 break;
225 case REQ_VIEW_STASH:
226 open_stash_view(view, OPEN_DEFAULT);
227 break;
229 case REQ_NEXT:
230 case REQ_PREVIOUS:
231 if (view->parent) {
232 int line;
234 view = view->parent;
235 line = view->pos.lineno;
236 view_request(view, request);
237 move_view(view, request);
238 if (view_is_displayed(view))
239 update_view_title(view);
240 if (line != view->pos.lineno)
241 view_request(view, REQ_ENTER);
242 } else {
243 move_view(view, request);
245 break;
247 case REQ_VIEW_NEXT:
249 int nviews = displayed_views();
250 int next_view = nviews ? (current_view + 1) % nviews : current_view;
252 if (next_view == current_view) {
253 report("Only one view is displayed");
254 break;
257 current_view = next_view;
258 /* Blur out the title of the previous view. */
259 update_view_title(view);
260 report_clear();
261 break;
263 case REQ_REFRESH:
264 report("Refreshing is not supported by the %s view", view->name);
265 break;
267 case REQ_PARENT:
268 report("Moving to parent is not supported by the the %s view", view->name);
269 break;
271 case REQ_BACK:
272 report("Going back is not supported for by %s view", view->name);
273 break;
275 case REQ_MAXIMIZE:
276 if (displayed_views() == 2)
277 maximize_view(view, TRUE);
278 break;
280 case REQ_OPTIONS:
281 toggle_option(view);
282 break;
284 case REQ_SEARCH:
285 case REQ_SEARCH_BACK:
286 search_view(view, request);
287 break;
289 case REQ_FIND_NEXT:
290 case REQ_FIND_PREV:
291 find_next(view, request);
292 break;
294 case REQ_STOP_LOADING:
295 foreach_view(view, i) {
296 if (view->pipe)
297 report("Stopped loading the %s view", view->name),
298 end_update(view, TRUE);
300 break;
302 case REQ_SHOW_VERSION:
303 report("tig-%s (built %s)", TIG_VERSION, __DATE__);
304 return TRUE;
306 case REQ_SCREEN_REDRAW:
307 redraw_display(TRUE);
308 break;
310 case REQ_EDIT:
311 report("Nothing to edit");
312 break;
314 case REQ_ENTER:
315 report("Nothing to enter");
316 break;
318 case REQ_VIEW_CLOSE:
319 /* XXX: Mark closed views by letting view->prev point to the
320 * view itself. Parents to closed view should never be
321 * followed. */
322 if (view->prev && view->prev != view) {
323 maximize_view(view->prev, TRUE);
324 view->prev = view;
325 watch_unregister(&view->watch);
326 break;
328 /* Fall-through */
329 case REQ_QUIT:
330 return FALSE;
332 default:
333 report("Unknown key, press %s for help",
334 get_view_key(view, REQ_VIEW_HELP));
335 return TRUE;
338 return TRUE;
342 * Main
345 static const char usage_string[] =
346 "tig " TIG_VERSION " (" __DATE__ ")\n"
347 "\n"
348 "Usage: tig [options] [revs] [--] [paths]\n"
349 " or: tig log [options] [revs] [--] [paths]\n"
350 " or: tig show [options] [revs] [--] [paths]\n"
351 " or: tig blame [options] [rev] [--] path\n"
352 " or: tig grep [options] [pattern]\n"
353 " or: tig refs\n"
354 " or: tig stash\n"
355 " or: tig status\n"
356 " or: tig < [git command output]\n"
357 "\n"
358 "Options:\n"
359 " +<number> Select line <number> in the first view\n"
360 " -v, --version Show version and exit\n"
361 " -h, --help Show help message and exit";
363 void
364 usage(const char *message)
366 die("%s\n\n%s", message, usage_string);
369 static int
370 read_filter_args(char *name, size_t namelen, char *value, size_t valuelen, void *data)
372 const char ***filter_args = data;
374 return argv_append(filter_args, name) ? OK : ERR;
377 static void
378 filter_rev_parse(const char ***args, const char *arg1, const char *arg2, const char *argv[])
380 const char *rev_parse_argv[SIZEOF_ARG] = { "git", "rev-parse", arg1, arg2 };
381 const char **all_argv = NULL;
383 if (!argv_append_array(&all_argv, rev_parse_argv) ||
384 !argv_append_array(&all_argv, argv) ||
385 io_run_load(all_argv, "\n", read_filter_args, args) == ERR)
386 die("Failed to split arguments");
387 argv_free(all_argv);
388 free(all_argv);
391 static void
392 filter_options(const char *argv[], bool rev_parse)
394 const char **flags = NULL;
395 int next, flags_pos;
397 update_options_from_argv(argv);
399 if (!rev_parse) {
400 opt_cmdline_args = argv;
401 return;
404 filter_rev_parse(&opt_file_args, "--no-revs", "--no-flags", argv);
405 filter_rev_parse(&flags, "--flags", "--no-revs", argv);
407 if (flags) {
408 for (next = flags_pos = 0; flags && flags[next]; next++) {
409 const char *flag = flags[next];
411 if (argv_parse_rev_flag(flag, NULL))
412 argv_append(&opt_rev_args, flag);
413 else
414 flags[flags_pos++] = flag;
417 flags[flags_pos] = NULL;
419 opt_cmdline_args = flags;
422 filter_rev_parse(&opt_rev_args, "--symbolic", "--revs-only", argv);
425 static enum request
426 parse_options(int argc, const char *argv[], bool pager_mode)
428 enum request request;
429 const char *subcommand;
430 bool seen_dashdash = FALSE;
431 bool rev_parse = TRUE;
432 const char **filter_argv = NULL;
433 int i;
435 request = pager_mode ? REQ_VIEW_PAGER : REQ_VIEW_MAIN;
437 if (argc <= 1)
438 return request;
440 subcommand = argv[1];
441 if (!strcmp(subcommand, "status")) {
442 request = REQ_VIEW_STATUS;
444 } else if (!strcmp(subcommand, "blame")) {
445 request = REQ_VIEW_BLAME;
447 } else if (!strcmp(subcommand, "grep")) {
448 request = REQ_VIEW_GREP;
449 rev_parse = FALSE;
451 } else if (!strcmp(subcommand, "show")) {
452 request = REQ_VIEW_DIFF;
454 } else if (!strcmp(subcommand, "log")) {
455 request = REQ_VIEW_LOG;
457 } else if (!strcmp(subcommand, "stash")) {
458 request = REQ_VIEW_STASH;
460 } else if (!strcmp(subcommand, "refs")) {
461 request = REQ_VIEW_REFS;
463 } else {
464 subcommand = NULL;
467 for (i = 1 + !!subcommand; i < argc; i++) {
468 const char *opt = argv[i];
470 // stop parsing our options after -- and let rev-parse handle the rest
471 if (!seen_dashdash) {
472 if (!strcmp(opt, "--")) {
473 seen_dashdash = TRUE;
474 continue;
476 } else if (!strcmp(opt, "-v") || !strcmp(opt, "--version")) {
477 printf("tig version %s\n", TIG_VERSION);
478 exit(EXIT_SUCCESS);
480 } else if (!strcmp(opt, "-h") || !strcmp(opt, "--help")) {
481 printf("%s\n", usage_string);
482 exit(EXIT_SUCCESS);
484 } else if (strlen(opt) >= 2 && *opt == '+' && string_isnumber(opt + 1)) {
485 int lineno = atoi(opt + 1);
487 argv_env.lineno = lineno > 0 ? lineno - 1 : 0;
488 continue;
493 if (!argv_append(&filter_argv, opt))
494 die("command too long");
497 if (filter_argv)
498 filter_options(filter_argv, rev_parse);
500 return request;
503 static enum request
504 open_pager_mode(enum request request)
506 if (request == REQ_VIEW_PAGER) {
507 /* Detect if the user requested the main view. */
508 if (argv_contains(opt_rev_args, "--stdin")) {
509 open_main_view(NULL, OPEN_FORWARD_STDIN);
510 } else if (argv_contains(opt_cmdline_args, "--pretty=raw")) {
511 open_main_view(NULL, OPEN_STDIN);
512 } else {
513 open_pager_view(NULL, OPEN_STDIN);
516 } else if (request == REQ_VIEW_DIFF) {
517 if (argv_contains(opt_rev_args, "--stdin"))
518 open_diff_view(NULL, OPEN_FORWARD_STDIN);
519 else
520 open_diff_view(NULL, OPEN_STDIN);
522 } else {
523 close(STDIN_FILENO);
524 report("Ignoring stdin.");
525 return request;
528 return REQ_NONE;
531 #ifdef NCURSES_MOUSE_VERSION
532 static struct view *
533 find_clicked_view(MEVENT *event)
535 struct view *view;
536 int i;
538 foreach_displayed_view (view, i) {
539 int beg_y = 0, beg_x = 0;
541 getbegyx(view->win, beg_y, beg_x);
543 if (beg_y <= event->y && event->y < beg_y + view->height
544 && beg_x <= event->x && event->x < beg_x + view->width) {
545 if (i != current_view) {
546 current_view = i;
548 return view;
552 return NULL;
555 static enum request
556 handle_mouse_event(void)
558 MEVENT event;
559 struct view *view;
561 if (getmouse(&event) != OK)
562 return REQ_NONE;
564 view = find_clicked_view(&event);
565 if (!view)
566 return REQ_NONE;
568 if (event.bstate & BUTTON2_PRESSED)
569 return REQ_SCROLL_WHEEL_DOWN;
571 if (event.bstate & BUTTON4_PRESSED)
572 return REQ_SCROLL_WHEEL_UP;
574 if (event.bstate & BUTTON1_PRESSED) {
575 if (event.y == view->pos.lineno - view->pos.offset) {
576 /* Click is on the same line, perform an "ENTER" */
577 return REQ_ENTER;
579 } else {
580 int y = getbegy(view->win);
581 unsigned long lineno = (event.y - y) + view->pos.offset;
583 select_view_line(view, lineno);
584 update_view_title(view);
585 report_clear();
589 return REQ_NONE;
591 #endif
594 main(int argc, const char *argv[])
596 const char *codeset = ENCODING_UTF8;
597 bool pager_mode = !isatty(STDIN_FILENO);
598 enum request request = parse_options(argc, argv, pager_mode);
599 struct view *view;
601 prompt_init();
603 if (signal(SIGPIPE, SIG_IGN) == SIG_ERR)
604 die("Failed to setup signal handler");
606 if (setlocale(LC_ALL, "")) {
607 codeset = nl_langinfo(CODESET);
610 if (load_repo_info() == ERR)
611 die("Failed to load repo info.");
613 if (load_options() == ERR)
614 die("Failed to load user config.");
616 if (load_git_config() == ERR)
617 die("Failed to load repo config.");
619 /* Require a git repository unless when running in pager mode. */
620 if (!repo.git_dir[0] && request != REQ_VIEW_PAGER)
621 die("Not a git repository");
623 if (codeset && strcmp(codeset, ENCODING_UTF8)) {
624 char translit[SIZEOF_STR];
626 if (string_format(translit, "%s%s", codeset, ICONV_TRANSLIT))
627 opt_iconv_out = iconv_open(translit, ENCODING_UTF8);
628 else
629 opt_iconv_out = iconv_open(codeset, ENCODING_UTF8);
630 if (opt_iconv_out == ICONV_NONE)
631 die("Failed to initialize character set conversion");
634 if (load_refs(FALSE) == ERR)
635 die("Failed to load refs.");
637 init_display();
639 if (pager_mode)
640 request = open_pager_mode(request);
642 if (getenv("TIG_SCRIPT")) {
643 const char *script_command[] = { "script", getenv("TIG_SCRIPT"), NULL };
645 run_prompt_command(NULL, script_command);
648 while (view_driver(display[current_view], request)) {
649 struct key key;
650 int key_value = get_input(0, &key, TRUE);
652 #ifdef NCURSES_MOUSE_VERSION
653 if (key_value == KEY_MOUSE) {
654 request = handle_mouse_event();
655 continue;
657 #endif
659 view = display[current_view];
660 request = get_keybinding(view->keymap, &key, 1);
662 /* Some low-level request handling. This keeps access to
663 * status_win restricted. */
664 switch (request) {
665 case REQ_NONE:
666 report("Unknown key, press %s for help",
667 get_view_key(view, REQ_VIEW_HELP));
668 break;
669 case REQ_PROMPT:
670 request = open_prompt(view);
671 break;
672 case REQ_SEARCH:
673 case REQ_SEARCH_BACK:
675 const char *prompt = request == REQ_SEARCH ? "/" : "?";
676 char *search = read_prompt(prompt);
678 if (search)
679 string_ncopy(argv_env.search, search, strlen(search));
680 else if (*argv_env.search)
681 request = request == REQ_SEARCH ?
682 REQ_FIND_NEXT :
683 REQ_FIND_PREV;
684 else
685 request = REQ_NONE;
686 break;
688 default:
689 break;
693 exit(EXIT_SUCCESS);
695 return 0;
698 /* vim: set ts=8 sw=8 noexpandtab: */