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
17 #include "tig/types.h"
19 #include "tig/parse.h"
22 #include "tig/refdb.h"
23 #include "tig/watch.h"
24 #include "tig/graph.h"
26 #include "tig/request.h"
31 #include "tig/options.h"
33 #include "tig/display.h"
34 #include "tig/prompt.h"
37 #include "tig/blame.h"
44 #include "tig/pager.h"
46 #include "tig/stage.h"
47 #include "tig/stash.h"
48 #include "tig/status.h"
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
);
59 view_request(struct view
*view
, enum request request
)
61 if (!view
|| !view
->lines
)
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
);
74 if (request
== REQ_REFRESH
&& !view_can_refresh(view
)) {
75 report("This view can not be refreshed");
79 return view
->ops
->request(view
, request
, &view
->line
[view
->pos
.lineno
]);
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"), \
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
)
112 const char *toggle_argv
[] = { "toggle", NULL
, NULL
};
115 if (!prompt_menu("Toggle option", menu
, &i
))
118 toggle_argv
[1] = menu
[i
].data
;
119 run_prompt_command(view
, toggle_argv
);
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
;
137 report("Unknown run request");
141 if (!argv_format(view
->env
, &argv
, req
->argv
, FALSE
, TRUE
)) {
142 report("Failed to format arguments");
146 if (req
->flags
.internal
) {
147 request
= run_prompt_command(view
, argv
);
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
)) {
163 if (confirmed
&& argv_remove_quotes(argv
))
164 open_external_viewer(argv
, NULL
, req
->flags
.silent
,
165 !req
->flags
.exit
, FALSE
, "");
172 if (request
== REQ_NONE
) {
173 if (req
->flags
.confirm
&& !confirmed
)
176 else if (req
->flags
.exit
)
179 else if (!req
->flags
.internal
&& watch_dirty(&view
->watch
))
180 request
= REQ_REFRESH
;
187 * User request switch noodle
191 view_driver(struct view
*view
, enum request request
)
195 if (request
== REQ_NONE
)
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
)
206 request
= view_request(view
, request
);
207 if (request
== REQ_NONE
)
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
);
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
);
233 open_grep_view(view
);
237 open_main_view(view
, OPEN_DEFAULT
);
240 open_diff_view(view
, OPEN_DEFAULT
);
243 open_log_view(view
, OPEN_DEFAULT
);
246 open_tree_view(view
, OPEN_DEFAULT
);
249 open_help_view(view
, OPEN_DEFAULT
);
252 open_refs_view(view
, OPEN_DEFAULT
);
255 open_blame_view(view
, OPEN_DEFAULT
);
258 open_blob_view(view
, OPEN_DEFAULT
);
260 case REQ_VIEW_STATUS
:
261 open_status_view(view
, OPEN_DEFAULT
);
264 open_stage_view(view
, NULL
, 0, OPEN_DEFAULT
);
267 open_pager_view(view
, OPEN_DEFAULT
);
270 open_stash_view(view
, OPEN_DEFAULT
);
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
);
287 move_view(view
, request
);
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");
301 current_view
= next_view
;
302 /* Blur out the title of the previous view. */
303 update_view_title(view
);
308 report("Refreshing is not supported by the %s view", view
->name
);
312 report("Moving to parent is not supported by the the %s view", view
->name
);
316 report("Going back is not supported for by %s view", view
->name
);
320 if (displayed_views() == 2)
321 maximize_view(view
, TRUE
);
329 case REQ_SEARCH_BACK
:
330 search_view(view
, request
);
335 find_next(view
, request
);
338 case REQ_STOP_LOADING
:
339 foreach_view(view
, i
) {
341 report("Stopped loading the %s view", view
->name
),
342 end_update(view
, TRUE
);
346 case REQ_SHOW_VERSION
:
347 report("tig-%s (built %s)", TIG_VERSION
, __DATE__
);
350 case REQ_SCREEN_REDRAW
:
351 redraw_display(TRUE
);
355 report("Nothing to edit");
359 report("Nothing to enter");
363 /* XXX: Mark closed views by letting view->prev point to the
364 * view itself. Parents to closed view should never be
366 if (view
->prev
&& view
->prev
!= view
) {
367 maximize_view(view
->prev
, TRUE
);
369 watch_unregister(&view
->watch
);
377 report("Unknown key, press %s for help",
378 get_view_key(view
, REQ_VIEW_HELP
));
389 static const char usage_string
[] =
390 "tig " TIG_VERSION
" (" __DATE__
")\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"
399 " or: tig < [git command output]\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";
407 usage(const char *message
)
409 die("%s\n\n%s", message
, usage_string
);
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
;
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");
435 filter_options(const char *argv
[], bool rev_parse
)
437 const char **flags
= NULL
;
440 update_options_from_argv(argv
);
443 opt_cmdline_argv
= argv
;
447 filter_rev_parse(&opt_file_argv
, "--no-revs", "--no-flags", argv
);
448 filter_rev_parse(&flags
, "--flags", "--no-revs", argv
);
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
);
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
);
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
;
478 request
= pager_mode
? REQ_VIEW_PAGER
: REQ_VIEW_MAIN
;
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
;
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
;
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
;
516 } else if (!strcmp(opt
, "-v") || !strcmp(opt
, "--version")) {
517 printf("tig version %s\n", TIG_VERSION
);
520 } else if (!strcmp(opt
, "-h") || !strcmp(opt
, "--help")) {
521 printf("%s\n", usage_string
);
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;
533 if (!argv_append(&filter_argv
, opt
))
534 die("command too long");
538 filter_options(filter_argv
, rev_parse
);
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
);
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
);
560 open_diff_view(NULL
, OPEN_STDIN
);
564 report("Ignoring stdin.");
571 #ifdef NCURSES_MOUSE_VERSION
573 find_clicked_view(MEVENT
*event
)
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
) {
596 handle_mouse_event(void)
601 if (getmouse(&event
) != OK
)
604 view
= find_clicked_view(&event
);
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" */
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
);
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
);
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
);
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.");
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
)) {
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();
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. */
711 report("Unknown key, press %s for help",
712 get_view_key(view
, REQ_VIEW_HELP
));
715 request
= open_prompt(view
);
718 case REQ_SEARCH_BACK
:
720 const char *prompt
= request
== REQ_SEARCH
? "/" : "?";
721 char *search
= read_prompt(prompt
);
724 string_ncopy(argv_env
.search
, search
, strlen(search
));
725 else if (*argv_env
.search
)
726 request
= request
== REQ_SEARCH
?
743 /* vim: set ts=8 sw=8 noexpandtab: */