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
);
133 report("Unknown run request");
137 return exec_run_request(view
, req
);
141 * User request switch noodle
145 view_driver(struct view
*view
, enum request request
)
149 if (request
== REQ_NONE
)
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
)
160 request
= view_request(view
, request
);
161 if (request
== REQ_NONE
)
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
);
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
);
189 open_grep_view(view
);
193 open_main_view(view
, OPEN_DEFAULT
);
196 open_diff_view(view
, OPEN_DEFAULT
);
199 open_log_view(view
, OPEN_DEFAULT
);
202 open_tree_view(view
, OPEN_DEFAULT
);
205 open_help_view(view
, OPEN_DEFAULT
);
208 open_refs_view(view
, OPEN_DEFAULT
);
211 open_blame_view(view
, OPEN_DEFAULT
);
214 open_blob_view(view
, OPEN_DEFAULT
);
216 case REQ_VIEW_STATUS
:
217 open_status_view(view
, OPEN_DEFAULT
);
220 open_stage_view(view
, NULL
, 0, OPEN_DEFAULT
);
223 open_pager_view(view
, OPEN_DEFAULT
);
226 open_stash_view(view
, OPEN_DEFAULT
);
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
);
243 move_view(view
, request
);
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");
257 current_view
= next_view
;
258 /* Blur out the title of the previous view. */
259 update_view_title(view
);
264 report("Refreshing is not supported by the %s view", view
->name
);
268 report("Moving to parent is not supported by the the %s view", view
->name
);
272 report("Going back is not supported for by %s view", view
->name
);
276 if (displayed_views() == 2)
277 maximize_view(view
, TRUE
);
285 case REQ_SEARCH_BACK
:
286 search_view(view
, request
);
291 find_next(view
, request
);
294 case REQ_STOP_LOADING
:
295 foreach_view(view
, i
) {
297 report("Stopped loading the %s view", view
->name
),
298 end_update(view
, TRUE
);
302 case REQ_SHOW_VERSION
:
303 report("tig-%s (built %s)", TIG_VERSION
, __DATE__
);
306 case REQ_SCREEN_REDRAW
:
307 redraw_display(TRUE
);
311 report("Nothing to edit");
315 report("Nothing to enter");
319 /* XXX: Mark closed views by letting view->prev point to the
320 * view itself. Parents to closed view should never be
322 if (view
->prev
&& view
->prev
!= view
) {
323 maximize_view(view
->prev
, TRUE
);
325 watch_unregister(&view
->watch
);
333 report("Unknown key, press %s for help",
334 get_view_key(view
, REQ_VIEW_HELP
));
345 static const char usage_string
[] =
346 "tig " TIG_VERSION
" (" __DATE__
")\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"
356 " or: tig < [git command output]\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";
364 usage(const char *message
)
366 die("%s\n\n%s", message
, usage_string
);
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
;
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");
392 filter_options(const char *argv
[], bool rev_parse
)
394 const char **flags
= NULL
;
397 update_options_from_argv(argv
);
400 opt_cmdline_args
= argv
;
404 filter_rev_parse(&opt_file_args
, "--no-revs", "--no-flags", argv
);
405 filter_rev_parse(&flags
, "--flags", "--no-revs", argv
);
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
);
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
);
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
;
435 request
= pager_mode
? REQ_VIEW_PAGER
: REQ_VIEW_MAIN
;
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
;
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
;
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
;
476 } else if (!strcmp(opt
, "-v") || !strcmp(opt
, "--version")) {
477 printf("tig version %s\n", TIG_VERSION
);
480 } else if (!strcmp(opt
, "-h") || !strcmp(opt
, "--help")) {
481 printf("%s\n", usage_string
);
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;
493 if (!argv_append(&filter_argv
, opt
))
494 die("command too long");
498 filter_options(filter_argv
, rev_parse
);
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
);
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
);
520 open_diff_view(NULL
, OPEN_STDIN
);
524 report("Ignoring stdin.");
531 #ifdef NCURSES_MOUSE_VERSION
533 find_clicked_view(MEVENT
*event
)
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
) {
556 handle_mouse_event(void)
561 if (getmouse(&event
) != OK
)
564 view
= find_clicked_view(&event
);
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" */
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
);
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
);
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
);
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.");
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
)) {
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();
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. */
666 report("Unknown key, press %s for help",
667 get_view_key(view
, REQ_VIEW_HELP
));
670 request
= open_prompt(view
);
673 case REQ_SEARCH_BACK
:
675 const char *prompt
= request
== REQ_SEARCH
? "/" : "?";
676 char *search
= read_prompt(prompt
);
679 string_ncopy(argv_env
.search
, search
, strlen(search
));
680 else if (*argv_env
.search
)
681 request
= request
== REQ_SEARCH
?
698 /* vim: set ts=8 sw=8 noexpandtab: */