1 /* Copyright (c) 2006-2013 Jonas Fonseca <fonseca@diku.dk>
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
29 static void report(const char *msg
, ...) PRINTF_LIKE(1, 2);
30 #define report_clear() report("%s", "")
40 typedef enum input_status (*input_handler
)(void *data
, char *buf
, int c
);
42 static char *prompt_input(const char *prompt
, input_handler handler
, void *data
);
43 static bool prompt_yesno(const char *prompt
);
44 static char *read_prompt(const char *prompt
);
52 static bool prompt_menu(const char *prompt
, const struct menu_item
*items
, int *selected
);
58 /* Option variables. */
59 static enum graphic opt_line_graphics
;
60 static enum date opt_date
;
61 static enum author opt_author
;
62 static enum filename opt_filename
;
63 static enum file_size opt_file_size
;
64 static bool opt_rev_graph
;
65 static bool opt_line_number
;
66 static bool opt_show_refs
;
67 static bool opt_show_changes
;
68 static bool opt_untracked_dirs_content
;
69 static bool opt_read_git_colors
;
70 static bool opt_wrap_lines
;
71 static bool opt_ignore_case
;
72 static bool opt_focus_child
;
73 static int opt_diff_context
;
74 static enum ignore_space opt_ignore_space
;
75 static enum commit_order opt_commit_order
;
76 static bool opt_notes
;
77 static int opt_num_interval
;
78 static double opt_hscroll
;
79 static double opt_scale_split_view
;
80 static double opt_scale_vsplit_view
;
81 static enum vertical_split opt_vertical_split
;
82 static int opt_tab_size
;
83 static int opt_author_width
;
84 static int opt_filename_width
;
85 static bool opt_editor_lineno
;
86 static bool opt_show_id
;
87 static int opt_id_cols
;
88 static bool opt_show_title_overflow
;
89 static int opt_title_overflow
;
90 static bool opt_mouse
;
91 static int opt_scroll_wheel_lines
;
93 /* State variables. */
94 static char opt_diff_context_arg
[9] = "";
95 static char opt_ignore_space_arg
[22] = "";
96 static char opt_commit_order_arg
[22] = "";
97 static char opt_notes_arg
[SIZEOF_STR
] = "--show-notes";
98 static char opt_path
[SIZEOF_STR
] = "";
99 static char opt_file
[SIZEOF_STR
] = "";
100 static char opt_ref
[SIZEOF_REF
] = "";
101 static unsigned long opt_goto_line
= 0;
102 static int opt_lineno
= 0;
103 static bool opt_file_filter
;
104 static iconv_t opt_iconv_out
= ICONV_NONE
;
105 static char opt_search
[SIZEOF_STR
] = "";
106 static char opt_editor
[SIZEOF_STR
] = "";
107 static FILE *opt_tty
= NULL
;
108 static const char **opt_cmdline_argv
= NULL
;
109 static const char **opt_diff_argv
= NULL
;
110 static const char **opt_rev_argv
= NULL
;
111 static const char **opt_file_argv
= NULL
;
112 static const char **opt_blame_argv
= NULL
;
113 static char opt_env_lines
[64] = "";
114 static char opt_env_columns
[64] = "";
115 static char *opt_env
[] = { opt_env_lines
, opt_env_columns
, NULL
};
118 vertical_split_is_enabled(void)
120 if (opt_vertical_split
== VERTICAL_SPLIT_AUTO
) {
123 getmaxyx(stdscr
, height
, width
);
124 return width
* opt_scale_vsplit_view
> (height
- 1) * 2;
127 return opt_vertical_split
== VERTICAL_SPLIT_VERTICAL
;
131 update_diff_context_arg(int diff_context
)
133 if (!string_format(opt_diff_context_arg
, "-U%u", diff_context
))
134 string_ncopy(opt_diff_context_arg
, "-U3", 3);
137 #define ENUM_ARG(enum_name, arg_string) ENUM_MAP_ENTRY(arg_string, enum_name)
139 static const struct enum_map_entry ignore_space_arg_map
[] = {
140 ENUM_ARG(IGNORE_SPACE_NO
, ""),
141 ENUM_ARG(IGNORE_SPACE_ALL
, "--ignore-all-space"),
142 ENUM_ARG(IGNORE_SPACE_SOME
, "--ignore-space-change"),
143 ENUM_ARG(IGNORE_SPACE_AT_EOL
, "--ignore-space-at-eol"),
147 update_ignore_space_arg()
149 enum_copy_name(opt_ignore_space_arg
, ignore_space_arg_map
[opt_ignore_space
]);
152 static const struct enum_map_entry commit_order_arg_map
[] = {
153 ENUM_ARG(COMMIT_ORDER_DEFAULT
, ""),
154 ENUM_ARG(COMMIT_ORDER_TOPO
, "--topo-order"),
155 ENUM_ARG(COMMIT_ORDER_DATE
, "--date-order"),
156 ENUM_ARG(COMMIT_ORDER_REVERSE
, "--reverse"),
160 update_commit_order_arg()
162 enum_copy_name(opt_commit_order_arg
, commit_order_arg_map
[opt_commit_order
]);
169 string_copy(opt_notes_arg
, "--show-notes");
171 /* Notes are disabled by default when passing --pretty args. */
172 string_copy(opt_notes_arg
, "");
178 unsigned int lineno
:24;
181 unsigned int selected
:1;
182 unsigned int dirty
:1;
183 unsigned int cleareol
:1;
184 unsigned int wrapped
:1;
186 unsigned int user_flags
:6;
187 void *data
; /* User data */
192 * User config file handling.
195 static const struct enum_map_entry color_map
[] = {
196 #define COLOR_MAP(name) ENUM_MAP_ENTRY(#name, COLOR_##name)
208 static const struct enum_map_entry attr_map
[] = {
209 #define ATTR_MAP(name) ENUM_MAP_ENTRY(#name, A_##name)
219 #define set_attribute(attr, name) map_enum(attr, attr_map, name)
221 static enum status_code
222 parse_step(double *opt
, const char *arg
)
225 if (!strchr(arg
, '%'))
228 /* "Shift down" so 100% and 1 does not conflict. */
229 *opt
= (*opt
- 1) / 100;
232 return ERROR_INVALID_STEP_VALUE
;
236 return ERROR_INVALID_STEP_VALUE
;
241 static enum status_code
242 parse_int(int *opt
, const char *arg
, int min
, int max
)
244 int value
= atoi(arg
);
246 if (min
<= value
&& value
<= max
) {
251 return ERROR_INTEGER_VALUE_OUT_OF_BOUND
;
254 #define parse_id(opt, arg) \
255 parse_int(opt, arg, 4, SIZEOF_REV - 1)
258 set_color(int *color
, const char *name
)
260 if (map_enum(color
, color_map
, name
))
262 if (!prefixcmp(name
, "color"))
263 return parse_int(color
, name
+ 5, 0, 255) == SUCCESS
;
264 /* Used when reading git colors. Git expects a plain int w/o prefix. */
265 return parse_int(color
, name
, 0, 255) == SUCCESS
;
268 /* Wants: object fgcolor bgcolor [attribute] */
269 static enum status_code
270 option_color_command(int argc
, const char *argv
[])
272 struct line_info
*info
;
275 return ERROR_WRONG_NUMBER_OF_ARGUMENTS
;
277 if (*argv
[0] == '"' || *argv
[0] == '\'') {
278 info
= add_custom_color(argv
[0]);
280 info
= find_line_info(argv
[0], strlen(argv
[0]), FALSE
);
283 static const struct enum_map_entry obsolete
[] = {
284 ENUM_MAP_ENTRY("main-delim", LINE_DELIMITER
),
285 ENUM_MAP_ENTRY("main-date", LINE_DATE
),
286 ENUM_MAP_ENTRY("main-author", LINE_AUTHOR
),
287 ENUM_MAP_ENTRY("blame-id", LINE_ID
),
291 if (!map_enum(&index
, obsolete
, argv
[0]))
292 return ERROR_UNKNOWN_COLOR_NAME
;
293 info
= get_line_info(index
);
296 if (!set_color(&info
->fg
, argv
[1]) ||
297 !set_color(&info
->bg
, argv
[2]))
298 return ERROR_UNKNOWN_COLOR
;
304 if (!set_attribute(&attr
, argv
[argc
]))
305 return ERROR_UNKNOWN_ATTRIBUTE
;
312 static enum status_code
313 parse_bool_matched(bool *opt
, const char *arg
, bool *matched
)
315 *opt
= (!strcmp(arg
, "1") || !strcmp(arg
, "true") || !strcmp(arg
, "yes"))
318 *matched
= *opt
|| (!strcmp(arg
, "0") || !strcmp(arg
, "false") || !strcmp(arg
, "no"));
322 #define parse_bool(opt, arg) parse_bool_matched(opt, arg, NULL)
324 static enum status_code
325 parse_enum(unsigned int *opt
, const char *arg
, const struct enum_map
*map
)
329 assert(map
->size
> 1);
331 if (map_enum_do(map
->entries
, map
->size
, (int *) opt
, arg
))
334 parse_bool(&is_true
, arg
);
335 *opt
= is_true
? map
->entries
[1].value
: map
->entries
[0].value
;
339 static enum status_code
340 parse_string(char *opt
, const char *arg
, size_t optsize
)
342 int arglen
= strlen(arg
);
347 if (arglen
== 1 || arg
[arglen
- 1] != arg
[0])
348 return ERROR_UNMATCHED_QUOTATION
;
349 arg
+= 1; arglen
-= 2;
351 string_ncopy_do(opt
, optsize
, arg
, arglen
);
356 static enum status_code
357 parse_encoding(struct encoding
**encoding_ref
, const char *arg
, bool priority
)
359 char buf
[SIZEOF_STR
];
360 enum status_code code
= parse_string(buf
, arg
, sizeof(buf
));
362 if (code
== SUCCESS
) {
363 struct encoding
*encoding
= *encoding_ref
;
365 if (encoding
&& !priority
)
367 encoding
= encoding_open(buf
);
369 *encoding_ref
= encoding
;
375 static enum status_code
376 parse_args(const char ***args
, const char *argv
[])
378 if (!argv_copy(args
, argv
))
379 return ERROR_OUT_OF_MEMORY
;
383 /* Wants: name = value */
384 static enum status_code
385 option_set_command(int argc
, const char *argv
[])
388 return ERROR_WRONG_NUMBER_OF_ARGUMENTS
;
390 if (strcmp(argv
[1], "="))
391 return ERROR_NO_VALUE_ASSIGNED
;
393 if (!strcmp(argv
[0], "blame-options"))
394 return parse_args(&opt_blame_argv
, argv
+ 2);
396 if (!strcmp(argv
[0], "diff-options"))
397 return parse_args(&opt_diff_argv
, argv
+ 2);
400 return ERROR_WRONG_NUMBER_OF_ARGUMENTS
;
402 if (!strcmp(argv
[0], "show-author"))
403 return parse_enum(&opt_author
, argv
[2], author_map
);
405 if (!strcmp(argv
[0], "show-date"))
406 return parse_enum(&opt_date
, argv
[2], date_map
);
408 if (!strcmp(argv
[0], "show-rev-graph"))
409 return parse_bool(&opt_rev_graph
, argv
[2]);
411 if (!strcmp(argv
[0], "show-refs"))
412 return parse_bool(&opt_show_refs
, argv
[2]);
414 if (!strcmp(argv
[0], "show-changes"))
415 return parse_bool(&opt_show_changes
, argv
[2]);
417 if (!strcmp(argv
[0], "show-notes")) {
418 bool matched
= FALSE
;
419 enum status_code res
= parse_bool_matched(&opt_notes
, argv
[2], &matched
);
421 if (res
== SUCCESS
&& matched
) {
427 strcpy(opt_notes_arg
, "--show-notes=");
428 res
= parse_string(opt_notes_arg
+ 8, argv
[2],
429 sizeof(opt_notes_arg
) - 8);
430 if (res
== SUCCESS
&& opt_notes_arg
[8] == '\0')
431 opt_notes_arg
[7] = '\0';
435 if (!strcmp(argv
[0], "show-line-numbers"))
436 return parse_bool(&opt_line_number
, argv
[2]);
438 if (!strcmp(argv
[0], "line-graphics"))
439 return parse_enum(&opt_line_graphics
, argv
[2], graphic_map
);
441 if (!strcmp(argv
[0], "line-number-interval"))
442 return parse_int(&opt_num_interval
, argv
[2], 1, 1024);
444 if (!strcmp(argv
[0], "author-width"))
445 return parse_int(&opt_author_width
, argv
[2], 0, 1024);
447 if (!strcmp(argv
[0], "filename-width"))
448 return parse_int(&opt_filename_width
, argv
[2], 0, 1024);
450 if (!strcmp(argv
[0], "show-filename"))
451 return parse_enum(&opt_filename
, argv
[2], filename_map
);
453 if (!strcmp(argv
[0], "show-file-size"))
454 return parse_enum(&opt_file_size
, argv
[2], file_size_map
);
456 if (!strcmp(argv
[0], "horizontal-scroll"))
457 return parse_step(&opt_hscroll
, argv
[2]);
459 if (!strcmp(argv
[0], "split-view-height"))
460 return parse_step(&opt_scale_split_view
, argv
[2]);
462 if (!strcmp(argv
[0], "vertical-split"))
463 return parse_enum(&opt_vertical_split
, argv
[2], vertical_split_map
);
465 if (!strcmp(argv
[0], "tab-size"))
466 return parse_int(&opt_tab_size
, argv
[2], 1, 1024);
468 if (!strcmp(argv
[0], "diff-context") && !*opt_diff_context_arg
) {
469 enum status_code code
= parse_int(&opt_diff_context
, argv
[2], 0, 999999);
472 update_diff_context_arg(opt_diff_context
);
476 if (!strcmp(argv
[0], "ignore-space") && !*opt_ignore_space_arg
) {
477 enum status_code code
= parse_enum(&opt_ignore_space
, argv
[2], ignore_space_map
);
480 update_ignore_space_arg();
484 if (!strcmp(argv
[0], "commit-order") && !*opt_commit_order_arg
) {
485 enum status_code code
= parse_enum(&opt_commit_order
, argv
[2], commit_order_map
);
488 update_commit_order_arg();
492 if (!strcmp(argv
[0], "status-untracked-dirs"))
493 return parse_bool(&opt_untracked_dirs_content
, argv
[2]);
495 if (!strcmp(argv
[0], "read-git-colors"))
496 return parse_bool(&opt_read_git_colors
, argv
[2]);
498 if (!strcmp(argv
[0], "ignore-case"))
499 return parse_bool(&opt_ignore_case
, argv
[2]);
501 if (!strcmp(argv
[0], "focus-child"))
502 return parse_bool(&opt_focus_child
, argv
[2]);
504 if (!strcmp(argv
[0], "wrap-lines"))
505 return parse_bool(&opt_wrap_lines
, argv
[2]);
507 if (!strcmp(argv
[0], "show-id"))
508 return parse_bool(&opt_show_id
, argv
[2]);
510 if (!strcmp(argv
[0], "id-width"))
511 return parse_id(&opt_id_cols
, argv
[2]);
513 if (!strcmp(argv
[0], "title-overflow")) {
515 enum status_code code
;
518 * "title-overflow" is considered a boolint.
519 * We try to parse it as a boolean (and set the value to 50 if true),
520 * otherwise we parse it as an integer and use the given value.
522 code
= parse_bool_matched(&opt_show_title_overflow
, argv
[2], &matched
);
523 if (code
== SUCCESS
&& matched
) {
524 if (opt_show_title_overflow
)
525 opt_title_overflow
= 50;
527 code
= parse_int(&opt_title_overflow
, argv
[2], 2, 1024);
529 opt_show_title_overflow
= TRUE
;
535 if (!strcmp(argv
[0], "editor-line-number"))
536 return parse_bool(&opt_editor_lineno
, argv
[2]);
538 if (!strcmp(argv
[0], "mouse"))
539 return parse_bool(&opt_mouse
, argv
[2]);
541 if (!strcmp(argv
[0], "mouse-scroll"))
542 return parse_int(&opt_scroll_wheel_lines
, argv
[2], 0, 1024);
544 return ERROR_UNKNOWN_VARIABLE_NAME
;
547 /* Wants: mode request key */
548 static enum status_code
549 option_bind_command(int argc
, const char *argv
[])
551 enum request request
;
552 struct keymap
*keymap
;
556 return ERROR_WRONG_NUMBER_OF_ARGUMENTS
;
558 if (!(keymap
= get_keymap(argv
[0])))
559 return ERROR_UNKNOWN_KEY_MAP
;
561 key
= get_key_value(argv
[1]);
563 return ERROR_UNKNOWN_KEY
;
565 request
= get_request(argv
[2]);
566 if (request
== REQ_UNKNOWN
) {
567 static const struct enum_map_entry obsolete
[] = {
568 ENUM_MAP_ENTRY("cherry-pick", REQ_NONE
),
569 ENUM_MAP_ENTRY("screen-resize", REQ_NONE
),
570 ENUM_MAP_ENTRY("tree-parent", REQ_PARENT
),
574 if (map_enum(&alias
, obsolete
, argv
[2])) {
575 if (alias
!= REQ_NONE
)
576 add_keybinding(keymap
, alias
, key
);
577 return ERROR_OBSOLETE_REQUEST_NAME
;
581 if (request
== REQ_UNKNOWN
) {
582 enum run_request_flag flags
= RUN_REQUEST_FORCE
;
584 if (strchr("!?@<", *argv
[2])) {
586 if (*argv
[2] == '@') {
587 flags
|= RUN_REQUEST_SILENT
;
588 } else if (*argv
[2] == '?') {
589 flags
|= RUN_REQUEST_CONFIRM
;
590 } else if (*argv
[2] == '<') {
591 flags
|= RUN_REQUEST_EXIT
;
592 } else if (*argv
[2] != '!') {
598 } else if (*argv
[2] == ':') {
600 flags
|= RUN_REQUEST_INTERNAL
;
603 return ERROR_UNKNOWN_REQUEST_NAME
;
606 return add_run_request(keymap
, key
, argv
+ 2, flags
)
607 ? SUCCESS
: ERROR_OUT_OF_MEMORY
;
610 add_keybinding(keymap
, request
, key
);
616 static enum status_code
load_option_file(const char *path
);
618 static enum status_code
619 option_source_command(int argc
, const char *argv
[])
622 return ERROR_WRONG_NUMBER_OF_ARGUMENTS
;
624 return load_option_file(argv
[0]);
627 static enum status_code
628 set_option(const char *opt
, char *value
)
630 const char *argv
[SIZEOF_ARG
];
633 if (!argv_from_string(argv
, &argc
, value
))
634 return ERROR_TOO_MANY_OPTION_ARGUMENTS
;
636 if (!strcmp(opt
, "color"))
637 return option_color_command(argc
, argv
);
639 if (!strcmp(opt
, "set"))
640 return option_set_command(argc
, argv
);
642 if (!strcmp(opt
, "bind"))
643 return option_bind_command(argc
, argv
);
645 if (!strcmp(opt
, "source"))
646 return option_source_command(argc
, argv
);
648 return ERROR_UNKNOWN_OPTION_COMMAND
;
651 struct config_state
{
658 read_option(char *opt
, size_t optlen
, char *value
, size_t valuelen
, void *data
)
660 struct config_state
*config
= data
;
661 enum status_code status
= ERROR_NO_OPTION_VALUE
;
665 /* Check for comment markers, since read_properties() will
666 * only ensure opt and value are split at first " \t". */
667 optlen
= strcspn(opt
, "#");
671 if (opt
[optlen
] == 0) {
672 /* Look for comment endings in the value. */
673 size_t len
= strcspn(value
, "#");
675 if (len
< valuelen
) {
680 status
= set_option(opt
, value
);
683 if (status
!= SUCCESS
) {
684 warn("%s line %d: %s near '%.*s'", config
->path
, config
->lineno
,
685 get_status_message(status
), (int) optlen
, opt
);
686 config
->errors
= TRUE
;
689 /* Always keep going if errors are encountered. */
693 static enum status_code
694 load_option_file(const char *path
)
696 struct config_state config
= { path
, 0, FALSE
};
698 char buf
[SIZEOF_STR
];
700 /* Do not read configuration from stdin if set to "" */
701 if (!path
|| !strlen(path
))
704 if (!prefixcmp(path
, "~/")) {
705 const char *home
= getenv("HOME");
707 if (!home
|| !string_format(buf
, "%s/%s", home
, path
+ 2))
708 return ERROR_HOME_UNRESOLVABLE
;
712 /* It's OK that the file doesn't exist. */
713 if (!io_open(&io
, "%s", path
))
714 return ERROR_FILE_DOES_NOT_EXIST
;
716 if (io_load(&io
, " \t", read_option
, &config
) == ERR
||
717 config
.errors
== TRUE
)
718 warn("Errors while loading %s.", path
);
722 extern const char *builtin_config
;
727 const char *tigrc_user
= getenv("TIGRC_USER");
728 const char *tigrc_system
= getenv("TIGRC_SYSTEM");
729 const char *tig_diff_opts
= getenv("TIG_DIFF_OPTS");
730 const bool diff_opts_from_args
= !!opt_diff_argv
;
731 bool custom_tigrc_system
= !!tigrc_system
;
733 if (!custom_tigrc_system
)
734 tigrc_system
= SYSCONFDIR
"/tigrc";
736 if (load_option_file(tigrc_system
) == ERROR_FILE_DOES_NOT_EXIST
&& !custom_tigrc_system
) {
737 struct config_state config
= { "<built-in>", 0, FALSE
};
740 if (!io_from_string(&io
, builtin_config
) ||
741 !io_load(&io
, " \t", read_option
, &config
) == ERR
||
742 config
.errors
== TRUE
)
743 die("Error in built-in config");
747 tigrc_user
= "~/.tigrc";
748 load_option_file(tigrc_user
);
750 if (!diff_opts_from_args
&& tig_diff_opts
&& *tig_diff_opts
) {
751 static const char *diff_opts
[SIZEOF_ARG
] = { NULL
};
752 char buf
[SIZEOF_STR
];
755 if (!string_format(buf
, "%s", tig_diff_opts
) ||
756 !argv_from_string(diff_opts
, &argc
, buf
))
757 die("TIG_DIFF_OPTS contains too many arguments");
758 else if (!argv_copy(&opt_diff_argv
, diff_opts
))
759 die("Failed to format TIG_DIFF_OPTS arguments");
773 /* The display array of active views and the index of the current view. */
774 static struct view
*display
[2];
775 static WINDOW
*display_win
[2];
776 static WINDOW
*display_title
[2];
777 static WINDOW
*display_sep
;
779 static unsigned int current_view
;
781 #define foreach_displayed_view(view, i) \
782 for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
784 #define displayed_views() (display[1] != NULL ? 2 : 1)
786 /* Current head and commit ID */
787 static char ref_blob
[SIZEOF_REF
] = "";
788 static char ref_commit
[SIZEOF_REF
] = "HEAD";
789 static char ref_head
[SIZEOF_REF
] = "HEAD";
790 static char ref_branch
[SIZEOF_REF
] = "";
791 static char ref_status
[SIZEOF_STR
] = "";
792 static char ref_stash
[SIZEOF_REF
] = "";
794 #define VIEW_OPS(id, name, ref) name##_ops
795 static struct view_ops
VIEW_INFO(VIEW_OPS
);
797 static struct view views
[] = {
798 #define VIEW_DATA(id, name, ref) \
799 { #name, ref, &name##_ops }
803 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
805 #define foreach_view(view, i) \
806 for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++)
808 #define view_is_displayed(view) \
809 (view == display[0] || view == display[1])
811 #define view_has_line(view, line_) \
812 ((view)->line <= (line_) && (line_) < (view)->line + (view)->lines)
815 forward_request_to_child(struct view
*child
, enum request request
)
817 return displayed_views() == 2 && view_is_displayed(child
) &&
818 !strcmp(child
->vid
, child
->id
);
822 view_request(struct view
*view
, enum request request
)
824 if (!view
|| !view
->lines
)
827 if (request
== REQ_ENTER
&& !opt_focus_child
&&
828 view_has_flags(view
, VIEW_SEND_CHILD_ENTER
)) {
829 struct view
*child
= display
[1];
831 if (forward_request_to_child(child
, request
)) {
832 view_request(child
, request
);
837 if (request
== REQ_REFRESH
&& view
->unrefreshable
) {
838 report("This view can not be refreshed");
842 return view
->ops
->request(view
, request
, &view
->line
[view
->pos
.lineno
]);
850 set_view_attr(struct view
*view
, enum line_type type
)
852 if (!view
->curline
->selected
&& view
->curtype
!= type
) {
853 (void) wattrset(view
->win
, get_line_attr(type
));
854 wchgat(view
->win
, -1, 0, get_line_color(type
), NULL
);
855 view
->curtype
= type
;
859 #define VIEW_MAX_LEN(view) ((view)->width + (view)->pos.col - (view)->col)
862 draw_chars(struct view
*view
, enum line_type type
, const char *string
,
863 int max_len
, bool use_tilde
)
868 size_t skip
= view
->pos
.col
> view
->col
? view
->pos
.col
- view
->col
: 0;
871 return VIEW_MAX_LEN(view
) <= 0;
873 if (opt_iconv_out
!= ICONV_NONE
) {
874 string
= encoding_iconv(opt_iconv_out
, string
);
876 return VIEW_MAX_LEN(view
) <= 0;
879 len
= utf8_length(&string
, skip
, &col
, max_len
, &trimmed
, use_tilde
, opt_tab_size
);
881 set_view_attr(view
, type
);
883 waddnstr(view
->win
, string
, len
);
885 if (trimmed
&& use_tilde
) {
886 set_view_attr(view
, LINE_DELIMITER
);
887 waddch(view
->win
, '~');
893 return VIEW_MAX_LEN(view
) <= 0;
897 draw_space(struct view
*view
, enum line_type type
, int max
, int spaces
)
899 static char space
[] = " ";
901 spaces
= MIN(max
, spaces
);
904 int len
= MIN(spaces
, sizeof(space
) - 1);
906 if (draw_chars(view
, type
, space
, len
, FALSE
))
911 return VIEW_MAX_LEN(view
) <= 0;
915 draw_text_expanded(struct view
*view
, enum line_type type
, const char *string
, int max_len
, bool use_tilde
)
917 static char text
[SIZEOF_STR
];
920 size_t pos
= string_expand(text
, sizeof(text
), string
, opt_tab_size
);
922 if (draw_chars(view
, type
, text
, max_len
, use_tilde
))
927 return VIEW_MAX_LEN(view
) <= 0;
931 draw_text(struct view
*view
, enum line_type type
, const char *string
)
933 return draw_text_expanded(view
, type
, string
, VIEW_MAX_LEN(view
), TRUE
);
937 draw_text_overflow(struct view
*view
, const char *text
, bool on
, int overflow
, enum line_type type
)
940 int max
= MIN(VIEW_MAX_LEN(view
), overflow
);
941 int len
= strlen(text
);
943 if (draw_text_expanded(view
, type
, text
, max
, max
< overflow
))
946 text
= len
> overflow
? text
+ overflow
: "";
947 type
= LINE_OVERFLOW
;
950 if (*text
&& draw_text(view
, type
, text
))
953 return VIEW_MAX_LEN(view
) <= 0;
956 #define draw_commit_title(view, text, offset) \
957 draw_text_overflow(view, text, opt_show_title_overflow, opt_title_overflow + offset, LINE_DEFAULT)
959 static bool PRINTF_LIKE(3, 4)
960 draw_formatted(struct view
*view
, enum line_type type
, const char *format
, ...)
962 char text
[SIZEOF_STR
];
965 FORMAT_BUFFER(text
, sizeof(text
), format
, retval
, TRUE
);
966 return retval
>= 0 ? draw_text(view
, type
, text
) : VIEW_MAX_LEN(view
) <= 0;
970 draw_graphic(struct view
*view
, enum line_type type
, const chtype graphic
[], size_t size
, bool separator
)
972 size_t skip
= view
->pos
.col
> view
->col
? view
->pos
.col
- view
->col
: 0;
973 int max
= VIEW_MAX_LEN(view
);
979 set_view_attr(view
, type
);
980 /* Using waddch() instead of waddnstr() ensures that
981 * they'll be rendered correctly for the cursor line. */
982 for (i
= skip
; i
< size
; i
++)
983 waddch(view
->win
, graphic
[i
]);
987 if (size
< max
&& skip
<= size
)
988 waddch(view
->win
, ' ');
992 return VIEW_MAX_LEN(view
) <= 0;
1001 draw_field(struct view
*view
, enum line_type type
, const char *text
, int width
, enum align align
, bool trim
)
1003 int max
= MIN(VIEW_MAX_LEN(view
), width
+ 1);
1004 int col
= view
->col
;
1007 return draw_space(view
, type
, max
, max
);
1009 if (align
== ALIGN_RIGHT
) {
1010 int textlen
= strlen(text
);
1011 int leftpad
= max
- textlen
- 1;
1014 if (draw_space(view
, type
, leftpad
, leftpad
))
1021 return draw_chars(view
, type
, text
, max
- 1, trim
)
1022 || draw_space(view
, LINE_DEFAULT
, max
- (view
->col
- col
), max
);
1026 draw_date(struct view
*view
, struct time
*time
)
1028 const char *date
= mkdate(time
, opt_date
);
1029 int cols
= opt_date
== DATE_SHORT
? DATE_SHORT_WIDTH
: DATE_WIDTH
;
1031 if (opt_date
== DATE_NO
)
1034 return draw_field(view
, LINE_DATE
, date
, cols
, ALIGN_LEFT
, FALSE
);
1038 draw_author(struct view
*view
, const struct ident
*author
)
1040 bool trim
= author_trim(opt_author_width
);
1041 const char *text
= mkauthor(author
, opt_author_width
, opt_author
);
1043 if (opt_author
== AUTHOR_NO
)
1046 return draw_field(view
, LINE_AUTHOR
, text
, opt_author_width
, ALIGN_LEFT
, trim
);
1050 draw_id_custom(struct view
*view
, enum line_type type
, const char *id
, int width
)
1052 return draw_field(view
, type
, id
, width
, ALIGN_LEFT
, FALSE
);
1056 draw_id(struct view
*view
, const char *id
)
1061 return draw_id_custom(view
, LINE_ID
, id
, opt_id_cols
);
1065 draw_filename(struct view
*view
, const char *filename
, bool auto_enabled
)
1067 bool trim
= filename
&& strlen(filename
) >= opt_filename_width
;
1069 if (opt_filename
== FILENAME_NO
)
1072 if (opt_filename
== FILENAME_AUTO
&& !auto_enabled
)
1075 return draw_field(view
, LINE_FILENAME
, filename
, opt_filename_width
, ALIGN_LEFT
, trim
);
1079 draw_file_size(struct view
*view
, unsigned long size
, int width
, bool pad
)
1081 const char *str
= pad
? NULL
: mkfilesize(size
, opt_file_size
);
1083 if (!width
|| opt_file_size
== FILE_SIZE_NO
)
1086 return draw_field(view
, LINE_FILE_SIZE
, str
, width
, ALIGN_RIGHT
, FALSE
);
1090 draw_mode(struct view
*view
, mode_t mode
)
1092 const char *str
= mkmode(mode
);
1094 return draw_field(view
, LINE_MODE
, str
, STRING_SIZE("-rw-r--r--"), ALIGN_LEFT
, FALSE
);
1098 draw_lineno(struct view
*view
, unsigned int lineno
)
1101 int digits3
= view
->digits
< 3 ? 3 : view
->digits
;
1102 int max
= MIN(VIEW_MAX_LEN(view
), digits3
);
1104 chtype separator
= opt_line_graphics
? ACS_VLINE
: '|';
1106 if (!opt_line_number
)
1109 lineno
+= view
->pos
.offset
+ 1;
1110 if (lineno
== 1 || (lineno
% opt_num_interval
) == 0) {
1111 static char fmt
[] = "%1ld";
1113 fmt
[1] = '0' + (view
->digits
<= 9 ? digits3
: 1);
1114 if (string_format(number
, fmt
, lineno
))
1118 draw_chars(view
, LINE_LINE_NUMBER
, text
, max
, TRUE
);
1120 draw_space(view
, LINE_LINE_NUMBER
, max
, digits3
);
1121 return draw_graphic(view
, LINE_DEFAULT
, &separator
, 1, TRUE
);
1125 draw_refs(struct view
*view
, struct ref_list
*refs
)
1129 if (!opt_show_refs
|| !refs
)
1132 for (i
= 0; i
< refs
->size
; i
++) {
1133 struct ref
*ref
= refs
->refs
[i
];
1134 enum line_type type
= get_line_type_from_ref(ref
);
1136 if (draw_formatted(view
, type
, "[%s]", ref
->name
))
1139 if (draw_text(view
, LINE_DEFAULT
, " "))
1147 draw_view_line(struct view
*view
, unsigned int lineno
)
1150 bool selected
= (view
->pos
.offset
+ lineno
== view
->pos
.lineno
);
1152 assert(view_is_displayed(view
));
1154 if (view
->pos
.offset
+ lineno
>= view
->lines
)
1157 line
= &view
->line
[view
->pos
.offset
+ lineno
];
1159 wmove(view
->win
, lineno
, 0);
1161 wclrtoeol(view
->win
);
1163 view
->curline
= line
;
1164 view
->curtype
= LINE_NONE
;
1165 line
->selected
= FALSE
;
1166 line
->dirty
= line
->cleareol
= 0;
1169 set_view_attr(view
, LINE_CURSOR
);
1170 line
->selected
= TRUE
;
1171 view
->ops
->select(view
, line
);
1174 return view
->ops
->draw(view
, line
, lineno
);
1178 redraw_view_dirty(struct view
*view
)
1183 for (lineno
= 0; lineno
< view
->height
; lineno
++) {
1184 if (view
->pos
.offset
+ lineno
>= view
->lines
)
1186 if (!view
->line
[view
->pos
.offset
+ lineno
].dirty
)
1189 if (!draw_view_line(view
, lineno
))
1195 wnoutrefresh(view
->win
);
1199 redraw_view_from(struct view
*view
, int lineno
)
1201 assert(0 <= lineno
&& lineno
< view
->height
);
1203 for (; lineno
< view
->height
; lineno
++) {
1204 if (!draw_view_line(view
, lineno
))
1208 wnoutrefresh(view
->win
);
1212 redraw_view(struct view
*view
)
1215 redraw_view_from(view
, 0);
1220 update_view_title(struct view
*view
)
1222 WINDOW
*window
= display
[0] == view
? display_title
[0] : display_title
[1];
1223 struct line
*line
= &view
->line
[view
->pos
.lineno
];
1224 unsigned int view_lines
, lines
;
1226 assert(view_is_displayed(view
));
1228 if (view
== display
[current_view
])
1229 wbkgdset(window
, get_line_attr(LINE_TITLE_FOCUS
));
1231 wbkgdset(window
, get_line_attr(LINE_TITLE_BLUR
));
1234 mvwprintw(window
, 0, 0, "[%s]", view
->name
);
1237 wprintw(window
, " %s", view
->ref
);
1240 if (!view_has_flags(view
, VIEW_CUSTOM_STATUS
) && view_has_line(view
, line
) &&
1242 wprintw(window
, " - %s %d of %zd",
1245 view
->lines
- view
->custom_lines
);
1249 time_t secs
= time(NULL
) - view
->start_time
;
1251 /* Three git seconds are a long time ... */
1253 wprintw(window
, " loading %lds", secs
);
1256 view_lines
= view
->pos
.offset
+ view
->height
;
1257 lines
= view
->lines
? MIN(view_lines
, view
->lines
) * 100 / view
->lines
: 0;
1258 mvwprintw(window
, 0, view
->width
- count_digits(lines
) - 1, "%d%%", lines
);
1260 wnoutrefresh(window
);
1264 apply_step(double step
, int value
)
1268 value
*= step
+ 0.01;
1269 return value
? value
: 1;
1273 apply_horizontal_split(struct view
*base
, struct view
*view
)
1275 view
->width
= base
->width
;
1276 view
->height
= apply_step(opt_scale_split_view
, base
->height
);
1277 view
->height
= MAX(view
->height
, MIN_VIEW_HEIGHT
);
1278 view
->height
= MIN(view
->height
, base
->height
- MIN_VIEW_HEIGHT
);
1279 base
->height
-= view
->height
;
1283 apply_vertical_split(struct view
*base
, struct view
*view
)
1285 view
->height
= base
->height
;
1286 view
->width
= apply_step(opt_scale_vsplit_view
, base
->width
);
1287 view
->width
= MAX(view
->width
, MIN_VIEW_WIDTH
);
1288 view
->width
= MIN(view
->width
, base
->width
- MIN_VIEW_WIDTH
);
1289 base
->width
-= view
->width
;
1293 redraw_display_separator(bool clear
)
1295 if (displayed_views() > 1 && vertical_split_is_enabled()) {
1296 chtype separator
= opt_line_graphics
? ACS_VLINE
: '|';
1299 wclear(display_sep
);
1300 wbkgd(display_sep
, separator
+ get_line_attr(LINE_TITLE_BLUR
));
1301 wnoutrefresh(display_sep
);
1306 resize_display(void)
1309 struct view
*base
= display
[0];
1310 struct view
*view
= display
[1] ? display
[1] : display
[0];
1313 /* Setup window dimensions */
1315 getmaxyx(stdscr
, base
->height
, base
->width
);
1316 string_format(opt_env_columns
, "COLUMNS=%d", base
->width
);
1317 string_format(opt_env_lines
, "LINES=%d", base
->height
);
1319 /* Make room for the status window. */
1322 vsplit
= vertical_split_is_enabled();
1326 apply_vertical_split(base
, view
);
1328 /* Make room for the separator bar. */
1331 apply_horizontal_split(base
, view
);
1334 /* Make room for the title bar. */
1338 /* Make room for the title bar. */
1343 foreach_displayed_view (view
, i
) {
1344 if (!display_win
[i
]) {
1345 display_win
[i
] = newwin(view
->height
, view
->width
, y
, x
);
1346 if (!display_win
[i
])
1347 die("Failed to create %s view", view
->name
);
1349 scrollok(display_win
[i
], FALSE
);
1351 display_title
[i
] = newwin(1, view
->width
, y
+ view
->height
, x
);
1352 if (!display_title
[i
])
1353 die("Failed to create title window");
1356 wresize(display_win
[i
], view
->height
, view
->width
);
1357 mvwin(display_win
[i
], y
, x
);
1358 wresize(display_title
[i
], 1, view
->width
);
1359 mvwin(display_title
[i
], y
+ view
->height
, x
);
1362 if (i
> 0 && vsplit
) {
1364 display_sep
= newwin(view
->height
, 1, 0, x
- 1);
1366 die("Failed to create separator window");
1369 wresize(display_sep
, view
->height
, 1);
1370 mvwin(display_sep
, 0, x
- 1);
1374 view
->win
= display_win
[i
];
1377 x
+= view
->width
+ 1;
1379 y
+= view
->height
+ 1;
1382 redraw_display_separator(FALSE
);
1386 redraw_display(bool clear
)
1391 foreach_displayed_view (view
, i
) {
1395 update_view_title(view
);
1398 redraw_display_separator(clear
);
1405 #define VIEW_FLAG_RESET_DISPLAY ((enum view_flag) -1)
1407 #define TOGGLE_MENU_INFO(_) \
1408 _(LINENO, '.', "line numbers", &opt_line_number, NULL, VIEW_NO_FLAGS), \
1409 _(DATE, 'D', "dates", &opt_date, date_map, VIEW_NO_FLAGS), \
1410 _(AUTHOR, 'A', "author", &opt_author, author_map, VIEW_NO_FLAGS), \
1411 _(GRAPHIC, '~', "graphics", &opt_line_graphics, graphic_map, VIEW_NO_FLAGS), \
1412 _(REV_GRAPH, 'g', "revision graph", &opt_rev_graph, NULL, VIEW_LOG_LIKE), \
1413 _(FILENAME, '#', "file names", &opt_filename, filename_map, VIEW_NO_FLAGS), \
1414 _(FILE_SIZE, '*', "file sizes", &opt_file_size, file_size_map, VIEW_NO_FLAGS), \
1415 _(IGNORE_SPACE, 'W', "space changes", &opt_ignore_space, ignore_space_map, VIEW_DIFF_LIKE), \
1416 _(COMMIT_ORDER, 'l', "commit order", &opt_commit_order, commit_order_map, VIEW_LOG_LIKE), \
1417 _(REFS, 'F', "reference display", &opt_show_refs, NULL, VIEW_NO_FLAGS), \
1418 _(CHANGES, 'C', "local change display", &opt_show_changes, NULL, VIEW_NO_FLAGS), \
1419 _(ID, 'X', "commit ID display", &opt_show_id, NULL, VIEW_NO_FLAGS), \
1420 _(FILES, '%', "file filtering", &opt_file_filter, NULL, VIEW_DIFF_LIKE | VIEW_LOG_LIKE), \
1421 _(TITLE_OVERFLOW, '$', "commit title overflow display", &opt_show_title_overflow, NULL, VIEW_NO_FLAGS), \
1422 _(UNTRACKED_DIRS, 'd', "untracked directory info", &opt_untracked_dirs_content, NULL, VIEW_STATUS_LIKE), \
1423 _(VERTICAL_SPLIT, '|', "view split", &opt_vertical_split, vertical_split_map, VIEW_FLAG_RESET_DISPLAY), \
1425 static enum view_flag
1426 toggle_option(struct view
*view
, enum request request
, char msg
[SIZEOF_STR
])
1429 enum request request
;
1430 const struct enum_map
*map
;
1431 enum view_flag reload_flags
;
1433 #define DEFINE_TOGGLE_DATA(id, key, help, value, map, vflags) { REQ_TOGGLE_ ## id, map, vflags }
1434 TOGGLE_MENU_INFO(DEFINE_TOGGLE_DATA
)
1436 const struct menu_item menu
[] = {
1437 #define DEFINE_TOGGLE_MENU(id, key, help, value, map, vflags) { key, help, value }
1438 TOGGLE_MENU_INFO(DEFINE_TOGGLE_MENU
)
1443 if (request
== REQ_OPTIONS
) {
1444 if (!prompt_menu("Toggle option", menu
, &i
))
1445 return VIEW_NO_FLAGS
;
1447 while (i
< ARRAY_SIZE(data
) && data
[i
].request
!= request
)
1449 if (i
>= ARRAY_SIZE(data
))
1450 die("Invalid request (%d)", request
);
1453 if (data
[i
].map
!= NULL
) {
1454 unsigned int *opt
= menu
[i
].data
;
1456 *opt
= (*opt
+ 1) % data
[i
].map
->size
;
1457 if (data
[i
].map
== ignore_space_map
) {
1458 update_ignore_space_arg();
1459 string_format_size(msg
, SIZEOF_STR
,
1460 "Ignoring %s %s", enum_name(data
[i
].map
->entries
[*opt
]), menu
[i
].text
);
1462 } else if (data
[i
].map
== commit_order_map
) {
1463 update_commit_order_arg();
1464 string_format_size(msg
, SIZEOF_STR
,
1465 "Using %s %s", enum_name(data
[i
].map
->entries
[*opt
]), menu
[i
].text
);
1468 string_format_size(msg
, SIZEOF_STR
,
1469 "Displaying %s %s", enum_name(data
[i
].map
->entries
[*opt
]), menu
[i
].text
);
1473 bool *option
= menu
[i
].data
;
1476 string_format_size(msg
, SIZEOF_STR
,
1477 "%sabling %s", *option
? "En" : "Dis", menu
[i
].text
);
1480 return data
[i
].reload_flags
;
1489 goto_view_line(struct view
*view
, unsigned long offset
, unsigned long lineno
)
1491 if (lineno
>= view
->lines
)
1492 lineno
= view
->lines
> 0 ? view
->lines
- 1 : 0;
1494 if (offset
> lineno
|| offset
+ view
->height
<= lineno
) {
1495 unsigned long half
= view
->height
/ 2;
1498 offset
= lineno
- half
;
1503 if (offset
!= view
->pos
.offset
|| lineno
!= view
->pos
.lineno
) {
1504 view
->pos
.offset
= offset
;
1505 view
->pos
.lineno
= lineno
;
1512 /* Scrolling backend */
1514 do_scroll_view(struct view
*view
, int lines
)
1516 bool redraw_current_line
= FALSE
;
1518 /* The rendering expects the new offset. */
1519 view
->pos
.offset
+= lines
;
1521 assert(0 <= view
->pos
.offset
&& view
->pos
.offset
< view
->lines
);
1524 /* Move current line into the view. */
1525 if (view
->pos
.lineno
< view
->pos
.offset
) {
1526 view
->pos
.lineno
= view
->pos
.offset
;
1527 redraw_current_line
= TRUE
;
1528 } else if (view
->pos
.lineno
>= view
->pos
.offset
+ view
->height
) {
1529 view
->pos
.lineno
= view
->pos
.offset
+ view
->height
- 1;
1530 redraw_current_line
= TRUE
;
1533 assert(view
->pos
.offset
<= view
->pos
.lineno
&& view
->pos
.lineno
< view
->lines
);
1535 /* Redraw the whole screen if scrolling is pointless. */
1536 if (view
->height
< ABS(lines
)) {
1540 int line
= lines
> 0 ? view
->height
- lines
: 0;
1541 int end
= line
+ ABS(lines
);
1543 scrollok(view
->win
, TRUE
);
1544 wscrl(view
->win
, lines
);
1545 scrollok(view
->win
, FALSE
);
1547 while (line
< end
&& draw_view_line(view
, line
))
1550 if (redraw_current_line
)
1551 draw_view_line(view
, view
->pos
.lineno
- view
->pos
.offset
);
1552 wnoutrefresh(view
->win
);
1555 view
->has_scrolled
= TRUE
;
1559 /* Scroll frontend */
1561 scroll_view(struct view
*view
, enum request request
)
1565 assert(view_is_displayed(view
));
1567 if (request
== REQ_SCROLL_WHEEL_DOWN
|| request
== REQ_SCROLL_WHEEL_UP
)
1568 lines
= opt_scroll_wheel_lines
;
1571 case REQ_SCROLL_FIRST_COL
:
1573 redraw_view_from(view
, 0);
1576 case REQ_SCROLL_LEFT
:
1577 if (view
->pos
.col
== 0) {
1578 report("Cannot scroll beyond the first column");
1581 if (view
->pos
.col
<= apply_step(opt_hscroll
, view
->width
))
1584 view
->pos
.col
-= apply_step(opt_hscroll
, view
->width
);
1585 redraw_view_from(view
, 0);
1588 case REQ_SCROLL_RIGHT
:
1589 view
->pos
.col
+= apply_step(opt_hscroll
, view
->width
);
1593 case REQ_SCROLL_PAGE_DOWN
:
1594 lines
= view
->height
;
1595 case REQ_SCROLL_WHEEL_DOWN
:
1596 case REQ_SCROLL_LINE_DOWN
:
1597 if (view
->pos
.offset
+ lines
> view
->lines
)
1598 lines
= view
->lines
- view
->pos
.offset
;
1600 if (lines
== 0 || view
->pos
.offset
+ view
->height
>= view
->lines
) {
1601 report("Cannot scroll beyond the last line");
1606 case REQ_SCROLL_PAGE_UP
:
1607 lines
= view
->height
;
1608 case REQ_SCROLL_LINE_UP
:
1609 case REQ_SCROLL_WHEEL_UP
:
1610 if (lines
> view
->pos
.offset
)
1611 lines
= view
->pos
.offset
;
1614 report("Cannot scroll beyond the first line");
1622 die("request %d not handled in switch", request
);
1625 do_scroll_view(view
, lines
);
1630 move_view(struct view
*view
, enum request request
)
1632 int scroll_steps
= 0;
1636 case REQ_MOVE_FIRST_LINE
:
1637 steps
= -view
->pos
.lineno
;
1640 case REQ_MOVE_LAST_LINE
:
1641 steps
= view
->lines
- view
->pos
.lineno
- 1;
1644 case REQ_MOVE_PAGE_UP
:
1645 steps
= view
->height
> view
->pos
.lineno
1646 ? -view
->pos
.lineno
: -view
->height
;
1649 case REQ_MOVE_PAGE_DOWN
:
1650 steps
= view
->pos
.lineno
+ view
->height
>= view
->lines
1651 ? view
->lines
- view
->pos
.lineno
- 1 : view
->height
;
1665 die("request %d not handled in switch", request
);
1668 if (steps
<= 0 && view
->pos
.lineno
== 0) {
1669 report("Cannot move beyond the first line");
1672 } else if (steps
>= 0 && view
->pos
.lineno
+ 1 >= view
->lines
) {
1673 report("Cannot move beyond the last line");
1677 /* Move the current line */
1678 view
->pos
.lineno
+= steps
;
1679 assert(0 <= view
->pos
.lineno
&& view
->pos
.lineno
< view
->lines
);
1681 /* Check whether the view needs to be scrolled */
1682 if (view
->pos
.lineno
< view
->pos
.offset
||
1683 view
->pos
.lineno
>= view
->pos
.offset
+ view
->height
) {
1684 scroll_steps
= steps
;
1685 if (steps
< 0 && -steps
> view
->pos
.offset
) {
1686 scroll_steps
= -view
->pos
.offset
;
1688 } else if (steps
> 0) {
1689 if (view
->pos
.lineno
== view
->lines
- 1 &&
1690 view
->lines
> view
->height
) {
1691 scroll_steps
= view
->lines
- view
->pos
.offset
- 1;
1692 if (scroll_steps
>= view
->height
)
1693 scroll_steps
-= view
->height
- 1;
1698 if (!view_is_displayed(view
)) {
1699 view
->pos
.offset
+= scroll_steps
;
1700 assert(0 <= view
->pos
.offset
&& view
->pos
.offset
< view
->lines
);
1701 view
->ops
->select(view
, &view
->line
[view
->pos
.lineno
]);
1705 /* Repaint the old "current" line if we be scrolling */
1706 if (ABS(steps
) < view
->height
)
1707 draw_view_line(view
, view
->pos
.lineno
- steps
- view
->pos
.offset
);
1710 do_scroll_view(view
, scroll_steps
);
1714 /* Draw the current line */
1715 draw_view_line(view
, view
->pos
.lineno
- view
->pos
.offset
);
1717 wnoutrefresh(view
->win
);
1726 static void search_view(struct view
*view
, enum request request
);
1729 grep_text(struct view
*view
, const char *text
[])
1734 for (i
= 0; text
[i
]; i
++)
1735 if (*text
[i
] && !regexec(view
->regex
, text
[i
], 1, &pmatch
, 0))
1741 select_view_line(struct view
*view
, unsigned long lineno
)
1743 struct position old
= view
->pos
;
1745 if (goto_view_line(view
, view
->pos
.offset
, lineno
)) {
1746 if (view_is_displayed(view
)) {
1747 if (old
.offset
!= view
->pos
.offset
) {
1750 draw_view_line(view
, old
.lineno
- view
->pos
.offset
);
1751 draw_view_line(view
, view
->pos
.lineno
- view
->pos
.offset
);
1752 wnoutrefresh(view
->win
);
1755 view
->ops
->select(view
, &view
->line
[view
->pos
.lineno
]);
1761 find_next(struct view
*view
, enum request request
)
1763 unsigned long lineno
= view
->pos
.lineno
;
1768 report("No previous search");
1770 search_view(view
, request
);
1780 case REQ_SEARCH_BACK
:
1789 if (request
== REQ_FIND_NEXT
|| request
== REQ_FIND_PREV
)
1790 lineno
+= direction
;
1792 /* Note, lineno is unsigned long so will wrap around in which case it
1793 * will become bigger than view->lines. */
1794 for (; lineno
< view
->lines
; lineno
+= direction
) {
1795 if (view
->ops
->grep(view
, &view
->line
[lineno
])) {
1796 select_view_line(view
, lineno
);
1797 report("Line %ld matches '%s'", lineno
+ 1, view
->grep
);
1802 report("No match found for '%s'", view
->grep
);
1806 search_view(struct view
*view
, enum request request
)
1809 int regex_flags
= opt_ignore_case
? REG_ICASE
: 0;
1812 regfree(view
->regex
);
1815 view
->regex
= calloc(1, sizeof(*view
->regex
));
1820 regex_err
= regcomp(view
->regex
, opt_search
, REG_EXTENDED
| regex_flags
);
1821 if (regex_err
!= 0) {
1822 char buf
[SIZEOF_STR
] = "unknown error";
1824 regerror(regex_err
, view
->regex
, buf
, sizeof(buf
));
1825 report("Search failed: %s", buf
);
1829 string_copy(view
->grep
, opt_search
);
1831 find_next(view
, request
);
1835 * Incremental updating
1839 check_position(struct position
*pos
)
1841 return pos
->lineno
|| pos
->col
|| pos
->offset
;
1845 clear_position(struct position
*pos
)
1847 memset(pos
, 0, sizeof(*pos
));
1851 reset_view(struct view
*view
)
1855 if (view
->ops
->done
)
1856 view
->ops
->done(view
);
1858 for (i
= 0; i
< view
->lines
; i
++)
1859 free(view
->line
[i
].data
);
1862 view
->prev_pos
= view
->pos
;
1863 clear_position(&view
->pos
);
1868 view
->custom_lines
= 0;
1869 view
->update_secs
= 0;
1872 struct format_context
{
1874 char buf
[SIZEOF_STR
];
1880 format_expand_arg(struct format_context
*format
, const char *name
, const char *end
)
1886 const char *value_if_empty
;
1888 #define FORMAT_VAR(name, value, value_if_empty) \
1889 { name, STRING_SIZE(name), value, value_if_empty }
1890 FORMAT_VAR("%(directory)", opt_path
, "."),
1891 FORMAT_VAR("%(file)", opt_file
, ""),
1892 FORMAT_VAR("%(ref)", opt_ref
, "HEAD"),
1893 FORMAT_VAR("%(head)", ref_head
, ""),
1894 FORMAT_VAR("%(commit)", ref_commit
, ""),
1895 FORMAT_VAR("%(blob)", ref_blob
, ""),
1896 FORMAT_VAR("%(branch)", ref_branch
, ""),
1897 FORMAT_VAR("%(stash)", ref_stash
, ""),
1901 if (!prefixcmp(name
, "%(prompt")) {
1902 const char *prompt
= "Command argument: ";
1903 char msgbuf
[SIZEOF_STR
];
1905 const char *msgstart
= name
+ STRING_SIZE("%(prompt");
1906 int msglen
= end
- msgstart
- 1;
1908 if (end
&& msglen
> 0 && string_format(msgbuf
, "%.*s", msglen
, msgstart
)) {
1909 const char *msg
= msgbuf
;
1911 while (isspace(*msg
))
1917 value
= read_prompt(prompt
);
1920 return string_format_from(format
->buf
, &format
->bufpos
, "%s", value
);
1923 for (i
= 0; i
< ARRAY_SIZE(vars
); i
++) {
1926 if (strncmp(name
, vars
[i
].name
, vars
[i
].namelen
))
1929 if (vars
[i
].value
== opt_file
&& !format
->file_filter
)
1932 value
= *vars
[i
].value
? vars
[i
].value
: vars
[i
].value_if_empty
;
1936 return string_format_from(format
->buf
, &format
->bufpos
, "%s", value
);
1939 report("Unknown replacement: `%s`", name
);
1944 format_append_arg(struct format_context
*format
, const char ***dst_argv
, const char *arg
)
1946 memset(format
->buf
, 0, sizeof(format
->buf
));
1950 char *var
= strstr(arg
, "%(");
1951 int len
= var
? var
- arg
: strlen(arg
);
1952 char *next
= var
? strchr(var
, ')') + 1 : NULL
;
1954 if (len
&& !string_format_from(format
->buf
, &format
->bufpos
, "%.*s", len
, arg
))
1957 if (var
&& !format_expand_arg(format
, var
, next
))
1963 return argv_append(dst_argv
, format
->buf
);
1967 format_append_argv(struct format_context
*format
, const char ***dst_argv
, const char *src_argv
[])
1974 for (argc
= 0; src_argv
[argc
]; argc
++)
1975 if (!format_append_arg(format
, dst_argv
, src_argv
[argc
]))
1978 return src_argv
[argc
] == NULL
;
1982 format_argv(struct view
*view
, const char ***dst_argv
, const char *src_argv
[], bool first
, bool file_filter
)
1984 struct format_context format
= { view
, "", 0, file_filter
};
1987 argv_free(*dst_argv
);
1989 for (argc
= 0; src_argv
[argc
]; argc
++) {
1990 const char *arg
= src_argv
[argc
];
1992 if (!strcmp(arg
, "%(fileargs)")) {
1993 if (file_filter
&& !argv_append_array(dst_argv
, opt_file_argv
))
1996 } else if (!strcmp(arg
, "%(diffargs)")) {
1997 if (!format_append_argv(&format
, dst_argv
, opt_diff_argv
))
2000 } else if (!strcmp(arg
, "%(blameargs)")) {
2001 if (!format_append_argv(&format
, dst_argv
, opt_blame_argv
))
2004 } else if (!strcmp(arg
, "%(cmdlineargs)")) {
2005 if (!format_append_argv(&format
, dst_argv
, opt_cmdline_argv
))
2008 } else if (!strcmp(arg
, "%(revargs)") ||
2009 (first
&& !strcmp(arg
, "%(commit)"))) {
2010 if (!argv_append_array(dst_argv
, opt_rev_argv
))
2013 } else if (!format_append_arg(&format
, dst_argv
, arg
)) {
2018 return src_argv
[argc
] == NULL
;
2022 restore_view_position(struct view
*view
)
2024 /* A view without a previous view is the first view */
2025 if (!view
->prev
&& opt_lineno
&& opt_lineno
<= view
->lines
) {
2026 select_view_line(view
, opt_lineno
- 1);
2030 /* Ensure that the view position is in a valid state. */
2031 if (!check_position(&view
->prev_pos
) ||
2032 (view
->pipe
&& view
->lines
<= view
->prev_pos
.lineno
))
2033 return goto_view_line(view
, view
->pos
.offset
, view
->pos
.lineno
);
2035 /* Changing the view position cancels the restoring. */
2036 /* FIXME: Changing back to the first line is not detected. */
2037 if (check_position(&view
->pos
)) {
2038 clear_position(&view
->prev_pos
);
2042 if (goto_view_line(view
, view
->prev_pos
.offset
, view
->prev_pos
.lineno
) &&
2043 view_is_displayed(view
))
2046 view
->pos
.col
= view
->prev_pos
.col
;
2047 clear_position(&view
->prev_pos
);
2053 end_update(struct view
*view
, bool force
)
2057 while (!view
->ops
->read(view
, NULL
))
2061 io_kill(view
->pipe
);
2062 io_done(view
->pipe
);
2067 setup_update(struct view
*view
, const char *vid
)
2070 /* XXX: Do not use string_copy_rev(), it copies until first space. */
2071 string_ncopy(view
->vid
, vid
, strlen(vid
));
2072 view
->pipe
= &view
->io
;
2073 view
->start_time
= time(NULL
);
2077 begin_update(struct view
*view
, const char *dir
, const char **argv
, enum open_flags flags
)
2079 bool extra
= !!(flags
& (OPEN_EXTRA
));
2080 bool reload
= !!(flags
& (OPEN_RELOAD
| OPEN_REFRESH
| OPEN_PREPARED
| OPEN_EXTRA
| OPEN_PAGER_MODE
));
2081 bool refresh
= flags
& (OPEN_REFRESH
| OPEN_PREPARED
| OPEN_STDIN
);
2082 bool forward_stdin
= flags
& OPEN_FORWARD_STDIN
;
2083 enum io_type io_type
= forward_stdin
? IO_RD_STDIN
: IO_RD
;
2085 if ((!reload
&& !strcmp(view
->vid
, view
->id
)) ||
2086 ((flags
& OPEN_REFRESH
) && view
->unrefreshable
))
2091 io_done(view
->pipe
);
2093 end_update(view
, TRUE
);
2096 view
->unrefreshable
= open_in_pager_mode(flags
);
2098 if (!refresh
&& argv
) {
2099 bool file_filter
= !view_has_flags(view
, VIEW_FILE_FILTER
) || opt_file_filter
;
2102 if (!format_argv(view
, &view
->argv
, argv
, !view
->prev
, file_filter
)) {
2103 report("Failed to format %s arguments", view
->name
);
2107 /* Put the current ref_* value to the view title ref
2108 * member. This is needed by the blob view. Most other
2109 * views sets it automatically after loading because the
2110 * first line is a commit line. */
2111 string_copy_rev(view
->ref
, view
->id
);
2114 if (view
->argv
&& view
->argv
[0] &&
2115 !io_run(&view
->io
, io_type
, view
->dir
, opt_env
, view
->argv
)) {
2116 report("Failed to open %s view", view
->name
);
2120 if (open_from_stdin(flags
)) {
2121 if (!io_open(&view
->io
, "%s", ""))
2122 die("Failed to open stdin");
2126 setup_update(view
, view
->id
);
2132 update_view(struct view
*view
)
2135 /* Clear the view and redraw everything since the tree sorting
2136 * might have rearranged things. */
2137 bool redraw
= view
->lines
== 0;
2138 bool can_read
= TRUE
;
2139 struct encoding
*encoding
= view
->encoding
? view
->encoding
: default_encoding
;
2144 if (!io_can_read(view
->pipe
, FALSE
)) {
2145 if (view
->lines
== 0 && view_is_displayed(view
)) {
2146 time_t secs
= time(NULL
) - view
->start_time
;
2148 if (secs
> 1 && secs
> view
->update_secs
) {
2149 if (view
->update_secs
== 0)
2151 update_view_title(view
);
2152 view
->update_secs
= secs
;
2158 for (; (line
= io_get(view
->pipe
, '\n', can_read
)); can_read
= FALSE
) {
2160 line
= encoding_convert(encoding
, line
);
2163 if (!view
->ops
->read(view
, line
)) {
2164 report("Allocation failure");
2165 end_update(view
, TRUE
);
2171 int digits
= count_digits(view
->lines
);
2173 /* Keep the displayed view in sync with line number scaling. */
2174 if (digits
!= view
->digits
) {
2175 view
->digits
= digits
;
2176 if (opt_line_number
|| view_has_flags(view
, VIEW_ALWAYS_LINENO
))
2181 if (io_error(view
->pipe
)) {
2182 report("Failed to read: %s", io_strerror(view
->pipe
));
2183 end_update(view
, TRUE
);
2185 } else if (io_eof(view
->pipe
)) {
2186 end_update(view
, FALSE
);
2189 if (restore_view_position(view
))
2192 if (!view_is_displayed(view
))
2195 if (redraw
|| view
->force_redraw
)
2196 redraw_view_from(view
, 0);
2198 redraw_view_dirty(view
);
2199 view
->force_redraw
= FALSE
;
2201 /* Update the title _after_ the redraw so that if the redraw picks up a
2202 * commit reference in view->ref it'll be available here. */
2203 update_view_title(view
);
2207 DEFINE_ALLOCATOR(realloc_lines
, struct line
, 256)
2209 static struct line
*
2210 add_line_at(struct view
*view
, unsigned long pos
, const void *data
, enum line_type type
, size_t data_size
, bool custom
)
2213 unsigned long lineno
;
2215 if (!realloc_lines(&view
->line
, view
->lines
, 1))
2219 void *alloc_data
= calloc(1, data_size
);
2225 memcpy(alloc_data
, data
, data_size
);
2229 if (pos
< view
->lines
) {
2231 line
= view
->line
+ pos
;
2232 lineno
= line
->lineno
;
2234 memmove(line
+ 1, line
, (view
->lines
- pos
) * sizeof(*view
->line
));
2235 while (pos
< view
->lines
) {
2236 view
->line
[pos
].lineno
++;
2237 view
->line
[pos
++].dirty
= 1;
2240 line
= &view
->line
[view
->lines
++];
2241 lineno
= view
->lines
- view
->custom_lines
;
2244 memset(line
, 0, sizeof(*line
));
2246 line
->data
= (void *) data
;
2250 view
->custom_lines
++;
2252 line
->lineno
= lineno
;
2257 static struct line
*
2258 add_line(struct view
*view
, const void *data
, enum line_type type
, size_t data_size
, bool custom
)
2260 return add_line_at(view
, view
->lines
, data
, type
, data_size
, custom
);
2263 static struct line
*
2264 add_line_alloc_(struct view
*view
, void **ptr
, enum line_type type
, size_t data_size
, bool custom
)
2266 struct line
*line
= add_line(view
, NULL
, type
, data_size
, custom
);
2273 #define add_line_alloc(view, data_ptr, type, extra_size, custom) \
2274 add_line_alloc_(view, (void **) data_ptr, type, sizeof(**data_ptr) + extra_size, custom)
2276 static struct line
*
2277 add_line_nodata(struct view
*view
, enum line_type type
)
2279 return add_line(view
, NULL
, type
, 0, FALSE
);
2282 static struct line
*
2283 add_line_text(struct view
*view
, const char *text
, enum line_type type
)
2285 return add_line(view
, text
, type
, strlen(text
) + 1, FALSE
);
2288 static struct line
* PRINTF_LIKE(3, 4)
2289 add_line_format(struct view
*view
, enum line_type type
, const char *fmt
, ...)
2291 char buf
[SIZEOF_STR
];
2294 FORMAT_BUFFER(buf
, sizeof(buf
), fmt
, retval
, FALSE
);
2295 return retval
>= 0 ? add_line_text(view
, buf
, type
) : NULL
;
2303 split_view(struct view
*prev
, struct view
*view
)
2306 current_view
= opt_focus_child
? 1 : 0;
2307 view
->parent
= prev
;
2310 if (prev
->pos
.lineno
- prev
->pos
.offset
>= prev
->height
) {
2311 /* Take the title line into account. */
2312 int lines
= prev
->pos
.lineno
- prev
->pos
.offset
- prev
->height
+ 1;
2314 /* Scroll the view that was split if the current line is
2315 * outside the new limited view. */
2316 do_scroll_view(prev
, lines
);
2319 if (view
!= prev
&& view_is_displayed(prev
)) {
2320 /* "Blur" the previous view. */
2321 update_view_title(prev
);
2326 maximize_view(struct view
*view
, bool redraw
)
2328 memset(display
, 0, sizeof(display
));
2330 display
[current_view
] = view
;
2333 redraw_display(FALSE
);
2339 load_view(struct view
*view
, struct view
*prev
, enum open_flags flags
)
2342 end_update(view
, TRUE
);
2343 if (view
->ops
->private_size
) {
2345 view
->private = calloc(1, view
->ops
->private_size
);
2347 memset(view
->private, 0, view
->ops
->private_size
);
2350 /* When prev == view it means this is the first loaded view. */
2351 if (prev
&& view
!= prev
) {
2355 if (!view
->ops
->open(view
, flags
))
2359 bool split
= !!(flags
& OPEN_SPLIT
);
2362 split_view(prev
, view
);
2364 maximize_view(view
, FALSE
);
2368 restore_view_position(view
);
2370 if (view
->pipe
&& view
->lines
== 0) {
2371 /* Clear the old view and let the incremental updating refill
2374 if (!(flags
& (OPEN_RELOAD
| OPEN_REFRESH
)))
2375 clear_position(&view
->prev_pos
);
2377 } else if (view_is_displayed(view
)) {
2383 #define refresh_view(view) load_view(view, NULL, OPEN_REFRESH)
2384 #define reload_view(view) load_view(view, NULL, OPEN_RELOAD)
2387 open_view(struct view
*prev
, enum request request
, enum open_flags flags
)
2389 bool reload
= !!(flags
& (OPEN_RELOAD
| OPEN_PREPARED
));
2390 struct view
*view
= VIEW(request
);
2391 int nviews
= displayed_views();
2393 assert(flags
^ OPEN_REFRESH
);
2395 if (view
== prev
&& nviews
== 1 && !reload
) {
2396 report("Already in %s view", view
->name
);
2400 if (!view_has_flags(view
, VIEW_NO_GIT_DIR
) && !repo
.git_dir
[0]) {
2401 report("The %s view is disabled in pager view", view
->name
);
2405 load_view(view
, prev
? prev
: view
, flags
);
2409 open_argv(struct view
*prev
, struct view
*view
, const char *argv
[], const char *dir
, enum open_flags flags
)
2411 enum request request
= view
- views
+ REQ_OFFSET
+ 1;
2414 end_update(view
, TRUE
);
2417 if (!argv_copy(&view
->argv
, argv
)) {
2418 report("Failed to open %s view: %s", view
->name
, io_strerror(&view
->io
));
2420 open_view(prev
, request
, flags
| OPEN_PREPARED
);
2425 open_external_viewer(const char *argv
[], const char *dir
, bool confirm
, const char *notice
)
2429 def_prog_mode(); /* save current tty modes */
2430 endwin(); /* restore original tty modes */
2431 ok
= io_run_fg(argv
, dir
);
2434 fprintf(stderr
, "%s", notice
);
2435 fprintf(stderr
, "Press Enter to continue");
2439 redraw_display(TRUE
);
2444 open_mergetool(const char *file
)
2446 const char *mergetool_argv
[] = { "git", "mergetool", file
, NULL
};
2448 open_external_viewer(mergetool_argv
, repo
.cdup
, TRUE
, "");
2451 #define EDITOR_LINENO_MSG \
2452 "*** Your editor reported an error while opening the file.\n" \
2453 "*** This is probably because it doesn't support the line\n" \
2454 "*** number argument added automatically. The line number\n" \
2455 "*** has been disabled for now. You can permanently disable\n" \
2456 "*** it by adding the following line to ~/.tigrc\n" \
2457 "*** set editor-line-number = no\n"
2460 open_editor(const char *file
, unsigned int lineno
)
2462 const char *editor_argv
[SIZEOF_ARG
+ 3] = { "vi", file
, NULL
};
2463 char editor_cmd
[SIZEOF_STR
];
2464 char lineno_cmd
[SIZEOF_STR
];
2468 editor
= getenv("GIT_EDITOR");
2469 if (!editor
&& *opt_editor
)
2470 editor
= opt_editor
;
2472 editor
= getenv("VISUAL");
2474 editor
= getenv("EDITOR");
2478 string_ncopy(editor_cmd
, editor
, strlen(editor
));
2479 if (!argv_from_string_no_quotes(editor_argv
, &argc
, editor_cmd
)) {
2480 report("Failed to read editor command");
2484 if (lineno
&& opt_editor_lineno
&& string_format(lineno_cmd
, "+%u", lineno
))
2485 editor_argv
[argc
++] = lineno_cmd
;
2486 editor_argv
[argc
] = file
;
2487 if (!open_external_viewer(editor_argv
, repo
.cdup
, TRUE
, EDITOR_LINENO_MSG
))
2488 opt_editor_lineno
= FALSE
;
2491 static enum request
run_prompt_command(struct view
*view
, char *cmd
);
2494 open_run_request(struct view
*view
, enum request request
)
2496 struct run_request
*req
= get_run_request(request
);
2497 const char **argv
= NULL
;
2498 bool confirmed
= FALSE
;
2503 report("Unknown run request");
2507 if (format_argv(view
, &argv
, req
->argv
, FALSE
, TRUE
)) {
2508 if (req
->internal
) {
2509 char cmd
[SIZEOF_STR
];
2511 if (argv_to_string(argv
, cmd
, sizeof(cmd
), " ")) {
2512 request
= run_prompt_command(view
, cmd
);
2516 confirmed
= !req
->confirm
;
2519 char cmd
[SIZEOF_STR
], prompt
[SIZEOF_STR
];
2520 const char *and_exit
= req
->exit
? " and exit" : "";
2522 if (argv_to_string(argv
, cmd
, sizeof(cmd
), " ") &&
2523 string_format(prompt
, "Run `%s`%s?", cmd
, and_exit
) &&
2524 prompt_yesno(prompt
)) {
2529 if (confirmed
&& argv_remove_quotes(argv
)) {
2533 open_external_viewer(argv
, NULL
, !req
->exit
, "");
2542 if (request
== REQ_NONE
) {
2543 if (req
->confirm
&& !confirmed
)
2549 else if (view_has_flags(view
, VIEW_REFRESH
) && !view
->unrefreshable
)
2550 request
= REQ_REFRESH
;
2556 * User request switch noodle
2560 view_driver(struct view
*view
, enum request request
)
2564 if (request
== REQ_NONE
)
2567 if (request
>= REQ_RUN_REQUESTS
) {
2568 request
= open_run_request(view
, request
);
2570 // exit quickly rather than going through view_request and back
2571 if (request
== REQ_QUIT
)
2575 request
= view_request(view
, request
);
2576 if (request
== REQ_NONE
)
2582 case REQ_MOVE_PAGE_UP
:
2583 case REQ_MOVE_PAGE_DOWN
:
2584 case REQ_MOVE_FIRST_LINE
:
2585 case REQ_MOVE_LAST_LINE
:
2586 move_view(view
, request
);
2589 case REQ_SCROLL_FIRST_COL
:
2590 case REQ_SCROLL_LEFT
:
2591 case REQ_SCROLL_RIGHT
:
2592 case REQ_SCROLL_LINE_DOWN
:
2593 case REQ_SCROLL_LINE_UP
:
2594 case REQ_SCROLL_PAGE_DOWN
:
2595 case REQ_SCROLL_PAGE_UP
:
2596 case REQ_SCROLL_WHEEL_DOWN
:
2597 case REQ_SCROLL_WHEEL_UP
:
2598 scroll_view(view
, request
);
2606 case REQ_VIEW_BRANCH
:
2607 case REQ_VIEW_BLAME
:
2609 case REQ_VIEW_STATUS
:
2610 case REQ_VIEW_STAGE
:
2611 case REQ_VIEW_PAGER
:
2612 case REQ_VIEW_STASH
:
2613 open_view(view
, request
, OPEN_DEFAULT
);
2621 view
= view
->parent
;
2622 line
= view
->pos
.lineno
;
2623 view_request(view
, request
);
2624 move_view(view
, request
);
2625 if (view_is_displayed(view
))
2626 update_view_title(view
);
2627 if (line
!= view
->pos
.lineno
)
2628 view_request(view
, REQ_ENTER
);
2630 move_view(view
, request
);
2636 int nviews
= displayed_views();
2637 int next_view
= (current_view
+ 1) % nviews
;
2639 if (next_view
== current_view
) {
2640 report("Only one view is displayed");
2644 current_view
= next_view
;
2645 /* Blur out the title of the previous view. */
2646 update_view_title(view
);
2651 report("Refreshing is not supported by the %s view", view
->name
);
2655 report("Moving to parent is not supported by the the %s view", view
->name
);
2659 report("Going back is not supported for by %s view", view
->name
);
2663 if (displayed_views() == 2)
2664 maximize_view(view
, TRUE
);
2668 case REQ_TOGGLE_LINENO
:
2669 case REQ_TOGGLE_DATE
:
2670 case REQ_TOGGLE_AUTHOR
:
2671 case REQ_TOGGLE_FILENAME
:
2672 case REQ_TOGGLE_GRAPHIC
:
2673 case REQ_TOGGLE_REV_GRAPH
:
2674 case REQ_TOGGLE_REFS
:
2675 case REQ_TOGGLE_CHANGES
:
2676 case REQ_TOGGLE_IGNORE_SPACE
:
2678 case REQ_TOGGLE_FILES
:
2679 case REQ_TOGGLE_TITLE_OVERFLOW
:
2680 case REQ_TOGGLE_FILE_SIZE
:
2681 case REQ_TOGGLE_UNTRACKED_DIRS
:
2682 case REQ_TOGGLE_VERTICAL_SPLIT
:
2684 char action
[SIZEOF_STR
] = "";
2685 enum view_flag flags
= toggle_option(view
, request
, action
);
2687 if (flags
== VIEW_FLAG_RESET_DISPLAY
) {
2689 redraw_display(TRUE
);
2691 foreach_displayed_view(view
, i
) {
2692 if (view_has_flags(view
, flags
) && !view
->unrefreshable
)
2700 report("%s", action
);
2704 case REQ_TOGGLE_SORT_FIELD
:
2705 case REQ_TOGGLE_SORT_ORDER
:
2706 report("Sorting is not yet supported for the %s view", view
->name
);
2709 case REQ_DIFF_CONTEXT_UP
:
2710 case REQ_DIFF_CONTEXT_DOWN
:
2711 report("Changing the diff context is not yet supported for the %s view", view
->name
);
2715 case REQ_SEARCH_BACK
:
2716 search_view(view
, request
);
2721 find_next(view
, request
);
2724 case REQ_STOP_LOADING
:
2725 foreach_view(view
, i
) {
2727 report("Stopped loading the %s view", view
->name
),
2728 end_update(view
, TRUE
);
2732 case REQ_SHOW_VERSION
:
2733 report("tig-%s (built %s)", TIG_VERSION
, __DATE__
);
2736 case REQ_SCREEN_REDRAW
:
2737 redraw_display(TRUE
);
2741 report("Nothing to edit");
2745 report("Nothing to enter");
2748 case REQ_VIEW_CLOSE
:
2749 /* XXX: Mark closed views by letting view->prev point to the
2750 * view itself. Parents to closed view should never be
2752 if (view
->prev
&& view
->prev
!= view
) {
2753 maximize_view(view
->prev
, TRUE
);
2762 report("Unknown key, press %s for help",
2763 get_view_key(view
, REQ_VIEW_HELP
));
2772 * View backend utilities
2782 const enum sort_field
*fields
;
2783 size_t size
, current
;
2787 #define SORT_STATE(fields) { fields, ARRAY_SIZE(fields), 0 }
2788 #define get_sort_field(state) ((state).fields[(state).current])
2789 #define sort_order(state, result) ((state).reverse ? -(result) : (result))
2792 sort_view(struct view
*view
, enum request request
, struct sort_state
*state
,
2793 int (*compare
)(const void *, const void *))
2796 case REQ_TOGGLE_SORT_FIELD
:
2797 state
->current
= (state
->current
+ 1) % state
->size
;
2800 case REQ_TOGGLE_SORT_ORDER
:
2801 state
->reverse
= !state
->reverse
;
2804 die("Not a sort request");
2807 qsort(view
->line
, view
->lines
, sizeof(*view
->line
), compare
);
2812 update_diff_context(enum request request
)
2814 int diff_context
= opt_diff_context
;
2817 case REQ_DIFF_CONTEXT_UP
:
2818 opt_diff_context
+= 1;
2819 update_diff_context_arg(opt_diff_context
);
2822 case REQ_DIFF_CONTEXT_DOWN
:
2823 if (opt_diff_context
== 0) {
2824 report("Diff context cannot be less than zero");
2827 opt_diff_context
-= 1;
2828 update_diff_context_arg(opt_diff_context
);
2832 die("Not a diff context request");
2835 return diff_context
!= opt_diff_context
;
2838 DEFINE_ALLOCATOR(realloc_paths
, const char *, 256)
2840 /* Small cache to reduce memory consumption. It uses binary search to
2841 * lookup or find place to position new entries. No entries are ever
2844 get_path(const char *path
)
2846 static const char **paths
;
2847 static size_t paths_size
;
2848 int from
= 0, to
= paths_size
- 1;
2851 while (from
<= to
) {
2852 size_t pos
= (to
+ from
) / 2;
2853 int cmp
= strcmp(path
, paths
[pos
]);
2864 if (!realloc_paths(&paths
, paths_size
, 1))
2866 entry
= strdup(path
);
2870 memmove(paths
+ from
+ 1, paths
+ from
, (paths_size
- from
) * sizeof(*paths
));
2871 paths
[from
] = entry
;
2877 DEFINE_ALLOCATOR(realloc_authors
, struct ident
*, 256)
2879 /* Small author cache to reduce memory consumption. It uses binary
2880 * search to lookup or find place to position new entries. No entries
2881 * are ever freed. */
2882 static struct ident
*
2883 get_author(const char *name
, const char *email
)
2885 static struct ident
**authors
;
2886 static size_t authors_size
;
2887 int from
= 0, to
= authors_size
- 1;
2888 struct ident
*ident
;
2890 while (from
<= to
) {
2891 size_t pos
= (to
+ from
) / 2;
2892 int cmp
= strcmp(name
, authors
[pos
]->name
);
2895 return authors
[pos
];
2903 if (!realloc_authors(&authors
, authors_size
, 1))
2905 ident
= calloc(1, sizeof(*ident
));
2908 ident
->name
= strdup(name
);
2909 ident
->email
= strdup(email
);
2910 if (!ident
->name
|| !ident
->email
) {
2911 free((void *) ident
->name
);
2916 memmove(authors
+ from
+ 1, authors
+ from
, (authors_size
- from
) * sizeof(*authors
));
2917 authors
[from
] = ident
;
2924 parse_timesec(struct time
*time
, const char *sec
)
2926 time
->sec
= (time_t) atol(sec
);
2930 parse_timezone(struct time
*time
, const char *zone
)
2934 tz
= ('0' - zone
[1]) * 60 * 60 * 10;
2935 tz
+= ('0' - zone
[2]) * 60 * 60;
2936 tz
+= ('0' - zone
[3]) * 60 * 10;
2937 tz
+= ('0' - zone
[4]) * 60;
2946 /* Parse author lines where the name may be empty:
2947 * author <email@address.tld> 1138474660 +0100
2950 parse_author_line(char *ident
, const struct ident
**author
, struct time
*time
)
2952 char *nameend
= strchr(ident
, '<');
2953 char *emailend
= strchr(ident
, '>');
2954 const char *name
, *email
= "";
2956 if (nameend
&& emailend
)
2957 *nameend
= *emailend
= 0;
2958 name
= chomp_string(ident
);
2960 email
= chomp_string(nameend
+ 1);
2962 name
= *email
? email
: unknown_ident
.name
;
2964 email
= *name
? name
: unknown_ident
.email
;
2966 *author
= get_author(name
, email
);
2968 /* Parse epoch and timezone */
2969 if (time
&& emailend
&& emailend
[1] == ' ') {
2970 char *secs
= emailend
+ 2;
2971 char *zone
= strchr(secs
, ' ');
2973 parse_timesec(time
, secs
);
2975 if (zone
&& strlen(zone
) == STRING_SIZE(" +0700"))
2976 parse_timezone(time
, zone
+ 1);
2980 static struct line
*
2981 find_line_by_type(struct view
*view
, struct line
*line
, enum line_type type
, int direction
)
2983 for (; view_has_line(view
, line
); line
+= direction
)
2984 if (line
->type
== type
)
2990 #define find_prev_line_by_type(view, line, type) \
2991 find_line_by_type(view, line, type, -1)
2993 #define find_next_line_by_type(view, line, type) \
2994 find_line_by_type(view, line, type, 1)
3001 struct view_state
*prev
; /* Entry below this in the stack */
3002 struct position position
; /* View position to restore */
3003 void *data
; /* View specific state */
3006 struct view_history
{
3008 struct view_state
*stack
;
3009 struct position position
;
3013 view_history_is_empty(struct view_history
*history
)
3015 return !history
->stack
;
3018 static struct view_state
*
3019 push_view_history_state(struct view_history
*history
, struct position
*position
, void *data
)
3021 struct view_state
*state
= history
->stack
;
3023 if (state
&& data
&& history
->state_alloc
&&
3024 !memcmp(state
->data
, data
, history
->state_alloc
))
3027 state
= calloc(1, sizeof(*state
) + history
->state_alloc
);
3031 state
->prev
= history
->stack
;
3032 history
->stack
= state
;
3033 clear_position(&history
->position
);
3034 state
->position
= *position
;
3035 state
->data
= &state
[1];
3036 if (data
&& history
->state_alloc
)
3037 memcpy(state
->data
, data
, history
->state_alloc
);
3042 pop_view_history_state(struct view_history
*history
, struct position
*position
, void *data
)
3044 struct view_state
*state
= history
->stack
;
3046 if (view_history_is_empty(history
))
3049 history
->position
= state
->position
;
3050 history
->stack
= state
->prev
;
3052 if (data
&& history
->state_alloc
)
3053 memcpy(data
, state
->data
, history
->state_alloc
);
3055 *position
= state
->position
;
3062 reset_view_history(struct view_history
*history
)
3064 while (pop_view_history_state(history
, NULL
, NULL
))
3072 struct blame_commit
{
3073 char id
[SIZEOF_REV
]; /* SHA1 ID. */
3074 char title
[128]; /* First line of the commit message. */
3075 const struct ident
*author
; /* Author of the commit. */
3076 struct time time
; /* Date from the author ident. */
3077 const char *filename
; /* Name of file. */
3078 char parent_id
[SIZEOF_REV
]; /* Parent/previous SHA1 ID. */
3079 const char *parent_filename
; /* Parent/previous name of file. */
3082 struct blame_header
{
3083 char id
[SIZEOF_REV
]; /* SHA1 ID. */
3090 parse_number(const char **posref
, size_t *number
, size_t min
, size_t max
)
3092 const char *pos
= *posref
;
3095 pos
= strchr(pos
+ 1, ' ');
3096 if (!pos
|| !isdigit(pos
[1]))
3098 *number
= atoi(pos
+ 1);
3099 if (*number
< min
|| *number
> max
)
3107 parse_blame_header(struct blame_header
*header
, const char *text
, size_t max_lineno
)
3109 const char *pos
= text
+ SIZEOF_REV
- 2;
3111 if (strlen(text
) <= SIZEOF_REV
|| pos
[1] != ' ')
3114 string_ncopy(header
->id
, text
, SIZEOF_REV
);
3116 if (!parse_number(&pos
, &header
->orig_lineno
, 1, 9999999) ||
3117 !parse_number(&pos
, &header
->lineno
, 1, max_lineno
) ||
3118 !parse_number(&pos
, &header
->group
, 1, max_lineno
- header
->lineno
+ 1))
3125 match_blame_header(const char *name
, char **line
)
3127 size_t namelen
= strlen(name
);
3128 bool matched
= !strncmp(name
, *line
, namelen
);
3137 parse_blame_info(struct blame_commit
*commit
, char *line
)
3139 if (match_blame_header("author ", &line
)) {
3140 parse_author_line(line
, &commit
->author
, NULL
);
3142 } else if (match_blame_header("author-time ", &line
)) {
3143 parse_timesec(&commit
->time
, line
);
3145 } else if (match_blame_header("author-tz ", &line
)) {
3146 parse_timezone(&commit
->time
, line
);
3148 } else if (match_blame_header("summary ", &line
)) {
3149 string_ncopy(commit
->title
, line
, strlen(line
));
3151 } else if (match_blame_header("previous ", &line
)) {
3152 if (strlen(line
) <= SIZEOF_REV
)
3154 string_copy_rev(commit
->parent_id
, line
);
3156 commit
->parent_filename
= get_path(line
);
3157 if (!commit
->parent_filename
)
3160 } else if (match_blame_header("filename ", &line
)) {
3161 commit
->filename
= get_path(line
);
3173 pager_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
3175 if (draw_lineno(view
, lineno
))
3178 if (line
->wrapped
&& draw_text(view
, LINE_DELIMITER
, "+"))
3181 draw_text(view
, line
->type
, line
->data
);
3186 add_describe_ref(char *buf
, size_t *bufpos
, const char *commit_id
, const char *sep
)
3188 const char *describe_argv
[] = { "git", "describe", commit_id
, NULL
};
3189 char ref
[SIZEOF_STR
];
3191 if (!io_run_buf(describe_argv
, ref
, sizeof(ref
)) || !*ref
)
3194 /* This is the only fatal call, since it can "corrupt" the buffer. */
3195 if (!string_nformat(buf
, SIZEOF_STR
, bufpos
, "%s%s", sep
, ref
))
3202 add_pager_refs(struct view
*view
, const char *commit_id
)
3204 char buf
[SIZEOF_STR
];
3205 struct ref_list
*list
;
3206 size_t bufpos
= 0, i
;
3207 const char *sep
= "Refs: ";
3208 bool is_tag
= FALSE
;
3210 list
= get_ref_list(commit_id
);
3212 if (view_has_flags(view
, VIEW_ADD_DESCRIBE_REF
))
3213 goto try_add_describe_ref
;
3217 for (i
= 0; i
< list
->size
; i
++) {
3218 struct ref
*ref
= list
->refs
[i
];
3219 const char *fmt
= ref
->tag
? "%s[%s]" :
3220 ref
->remote
? "%s<%s>" : "%s%s";
3222 if (!string_format_from(buf
, &bufpos
, fmt
, sep
, ref
->name
))
3229 if (!is_tag
&& view_has_flags(view
, VIEW_ADD_DESCRIBE_REF
)) {
3230 try_add_describe_ref
:
3231 /* Add <tag>-g<commit_id> "fake" reference. */
3232 if (!add_describe_ref(buf
, &bufpos
, commit_id
, sep
))
3239 add_line_text(view
, buf
, LINE_PP_REFS
);
3242 static struct line
*
3243 pager_wrap_line(struct view
*view
, const char *data
, enum line_type type
)
3245 size_t first_line
= 0;
3246 bool has_first_line
= FALSE
;
3247 size_t datalen
= strlen(data
);
3250 while (datalen
> 0 || !has_first_line
) {
3251 bool wrapped
= !!first_line
;
3252 size_t linelen
= string_expanded_length(data
, datalen
, opt_tab_size
, view
->width
- !!wrapped
);
3256 line
= add_line(view
, NULL
, type
, linelen
+ 1, wrapped
);
3259 if (!has_first_line
) {
3260 first_line
= view
->lines
- 1;
3261 has_first_line
= TRUE
;
3265 lineno
= line
->lineno
;
3267 line
->wrapped
= wrapped
;
3268 line
->lineno
= lineno
;
3271 strncpy(text
, data
, linelen
);
3278 return has_first_line
? &view
->line
[first_line
] : NULL
;
3282 pager_common_read(struct view
*view
, const char *data
, enum line_type type
)
3289 if (opt_wrap_lines
) {
3290 line
= pager_wrap_line(view
, data
, type
);
3292 line
= add_line_text(view
, data
, type
);
3298 if (line
->type
== LINE_COMMIT
&& view_has_flags(view
, VIEW_ADD_PAGER_REFS
))
3299 add_pager_refs(view
, data
+ STRING_SIZE("commit "));
3305 pager_read(struct view
*view
, char *data
)
3310 return pager_common_read(view
, data
, get_line_type(data
));
3314 pager_request(struct view
*view
, enum request request
, struct line
*line
)
3318 if (request
!= REQ_ENTER
)
3321 if (line
->type
== LINE_COMMIT
&& view_has_flags(view
, VIEW_OPEN_DIFF
)) {
3322 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
);
3326 /* Always scroll the view even if it was split. That way
3327 * you can use Enter to scroll through the log view and
3328 * split open each commit diff. */
3329 scroll_view(view
, REQ_SCROLL_LINE_DOWN
);
3331 /* FIXME: A minor workaround. Scrolling the view will call report_clear()
3332 * but if we are scrolling a non-current view this won't properly
3333 * update the view title. */
3335 update_view_title(view
);
3341 pager_grep(struct view
*view
, struct line
*line
)
3343 const char *text
[] = { line
->data
, NULL
};
3345 return grep_text(view
, text
);
3349 pager_select(struct view
*view
, struct line
*line
)
3351 if (line
->type
== LINE_COMMIT
) {
3352 string_copy_rev_from_commit_line(ref_commit
, line
->data
);
3353 if (!view_has_flags(view
, VIEW_NO_REF
))
3354 string_copy_rev(view
->ref
, ref_commit
);
3359 /* Used for tracking when we need to recalculate the previous
3360 * commit, for example when the user scrolls up or uses the page
3361 * up/down in the log view. */
3363 enum line_type last_type
;
3367 log_select(struct view
*view
, struct line
*line
)
3369 struct log_state
*state
= view
->private;
3370 int last_lineno
= state
->last_lineno
;
3372 if (!last_lineno
|| abs(last_lineno
- line
->lineno
) > 1
3373 || (state
->last_type
== LINE_COMMIT
&& last_lineno
> line
->lineno
)) {
3374 const struct line
*commit_line
= find_prev_line_by_type(view
, line
, LINE_COMMIT
);
3377 string_copy_rev_from_commit_line(view
->ref
, commit_line
->data
);
3380 if (line
->type
== LINE_COMMIT
&& !view_has_flags(view
, VIEW_NO_REF
)) {
3381 string_copy_rev_from_commit_line(view
->ref
, (char *)line
->data
);
3383 string_copy_rev(ref_commit
, view
->ref
);
3384 state
->last_lineno
= line
->lineno
;
3385 state
->last_type
= line
->type
;
3389 pager_open(struct view
*view
, enum open_flags flags
)
3391 if (!open_from_stdin(flags
) && !view
->lines
) {
3392 report("No pager content, press %s to run command from prompt",
3393 get_view_key(view
, REQ_PROMPT
));
3397 return begin_update(view
, NULL
, NULL
, flags
);
3400 static struct view_ops pager_ops
= {
3403 VIEW_OPEN_DIFF
| VIEW_NO_REF
| VIEW_NO_GIT_DIR
,
3414 log_open(struct view
*view
, enum open_flags flags
)
3416 static const char *log_argv
[] = {
3417 "git", "log", encoding_arg
, "--no-color", "--cc", "--stat", "-n100", "%(head)", "--", NULL
3420 return begin_update(view
, NULL
, log_argv
, flags
);
3424 log_request(struct view
*view
, enum request request
, struct line
*line
)
3433 if (!display
[1] || strcmp(display
[1]->vid
, view
->ref
))
3434 open_view(view
, REQ_VIEW_DIFF
, OPEN_SPLIT
);
3442 static struct view_ops log_ops
= {
3445 VIEW_ADD_PAGER_REFS
| VIEW_OPEN_DIFF
| VIEW_SEND_CHILD_ENTER
| VIEW_LOG_LIKE
| VIEW_REFRESH
,
3446 sizeof(struct log_state
),
3456 bool after_commit_title
;
3458 bool reading_diff_stat
;
3462 #define DIFF_LINE_COMMIT_TITLE 1
3465 diff_open(struct view
*view
, enum open_flags flags
)
3467 static const char *diff_argv
[] = {
3468 "git", "show", encoding_arg
, "--pretty=fuller", "--root",
3469 "--patch-with-stat",
3470 opt_notes_arg
, opt_diff_context_arg
, opt_ignore_space_arg
,
3471 "%(diffargs)", "%(cmdlineargs)", "--no-color", "%(commit)",
3472 "--", "%(fileargs)", NULL
3475 return begin_update(view
, NULL
, diff_argv
, flags
);
3479 diff_common_read(struct view
*view
, const char *data
, struct diff_state
*state
)
3481 enum line_type type
= get_line_type(data
);
3483 if (!view
->lines
&& type
!= LINE_COMMIT
)
3484 state
->reading_diff_stat
= TRUE
;
3486 if (state
->combined_diff
&& !state
->after_diff
&& data
[0] == ' ' && data
[1] != ' ')
3487 state
->reading_diff_stat
= TRUE
;
3489 if (state
->reading_diff_stat
) {
3490 size_t len
= strlen(data
);
3491 char *pipe
= strchr(data
, '|');
3492 bool has_histogram
= data
[len
- 1] == '-' || data
[len
- 1] == '+';
3493 bool has_bin_diff
= pipe
&& strstr(pipe
, "Bin") && strstr(pipe
, "->");
3494 bool has_rename
= data
[len
- 1] == '0' && (strstr(data
, "=>") || !strncmp(data
, " ...", 4));
3495 bool has_no_change
= pipe
&& strstr(pipe
, " 0");
3497 if (pipe
&& (has_histogram
|| has_bin_diff
|| has_rename
|| has_no_change
)) {
3498 return add_line_text(view
, data
, LINE_DIFF_STAT
) != NULL
;
3500 state
->reading_diff_stat
= FALSE
;
3503 } else if (!strcmp(data
, "---")) {
3504 state
->reading_diff_stat
= TRUE
;
3507 if (!state
->after_commit_title
&& !prefixcmp(data
, " ")) {
3508 struct line
*line
= add_line_text(view
, data
, LINE_DEFAULT
);
3511 line
->user_flags
|= DIFF_LINE_COMMIT_TITLE
;
3512 state
->after_commit_title
= TRUE
;
3513 return line
!= NULL
;
3516 if (type
== LINE_DIFF_HEADER
) {
3517 const int len
= get_line_info(LINE_DIFF_HEADER
)->linelen
;
3519 state
->after_diff
= TRUE
;
3520 if (!strncmp(data
+ len
, "combined ", strlen("combined ")) ||
3521 !strncmp(data
+ len
, "cc ", strlen("cc ")))
3522 state
->combined_diff
= TRUE
;
3524 } else if (type
== LINE_PP_MERGE
) {
3525 state
->combined_diff
= TRUE
;
3528 /* ADD2 and DEL2 are only valid in combined diff hunks */
3529 if (!state
->combined_diff
&& (type
== LINE_DIFF_ADD2
|| type
== LINE_DIFF_DEL2
))
3530 type
= LINE_DEFAULT
;
3532 return pager_common_read(view
, data
, type
);
3536 diff_find_stat_entry(struct view
*view
, struct line
*line
, enum line_type type
)
3538 struct line
*marker
= find_next_line_by_type(view
, line
, type
);
3541 line
== find_prev_line_by_type(view
, marker
, LINE_DIFF_HEADER
);
3545 diff_common_enter(struct view
*view
, enum request request
, struct line
*line
)
3547 if (line
->type
== LINE_DIFF_STAT
) {
3548 int file_number
= 0;
3550 while (view_has_line(view
, line
) && line
->type
== LINE_DIFF_STAT
) {
3555 for (line
= view
->line
; view_has_line(view
, line
); line
++) {
3556 line
= find_next_line_by_type(view
, line
, LINE_DIFF_HEADER
);
3560 if (diff_find_stat_entry(view
, line
, LINE_DIFF_INDEX
)
3561 || diff_find_stat_entry(view
, line
, LINE_DIFF_SIMILARITY
)) {
3562 if (file_number
== 1) {
3570 report("Failed to find file diff");
3574 select_view_line(view
, line
- view
->line
);
3579 return pager_request(view
, request
, line
);
3584 diff_common_draw_part(struct view
*view
, enum line_type
*type
, char **text
, char c
, enum line_type next_type
)
3586 char *sep
= strchr(*text
, c
);
3590 draw_text(view
, *type
, *text
);
3600 diff_common_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
3602 char *text
= line
->data
;
3603 enum line_type type
= line
->type
;
3605 if (draw_lineno(view
, lineno
))
3608 if (line
->wrapped
&& draw_text(view
, LINE_DELIMITER
, "+"))
3611 if (type
== LINE_DIFF_STAT
) {
3612 diff_common_draw_part(view
, &type
, &text
, '|', LINE_DEFAULT
);
3613 if (diff_common_draw_part(view
, &type
, &text
, 'B', LINE_DEFAULT
)) {
3614 /* Handle binary diffstat: Bin <deleted> -> <added> bytes */
3615 diff_common_draw_part(view
, &type
, &text
, ' ', LINE_DIFF_DEL
);
3616 diff_common_draw_part(view
, &type
, &text
, '-', LINE_DEFAULT
);
3617 diff_common_draw_part(view
, &type
, &text
, ' ', LINE_DIFF_ADD
);
3618 diff_common_draw_part(view
, &type
, &text
, 'b', LINE_DEFAULT
);
3621 diff_common_draw_part(view
, &type
, &text
, '+', LINE_DIFF_ADD
);
3622 diff_common_draw_part(view
, &type
, &text
, '-', LINE_DIFF_DEL
);
3626 if (line
->user_flags
& DIFF_LINE_COMMIT_TITLE
)
3627 draw_commit_title(view
, text
, 4);
3629 draw_text(view
, type
, text
);
3634 diff_read(struct view
*view
, char *data
)
3636 struct diff_state
*state
= view
->private;
3639 /* Fall back to retry if no diff will be shown. */
3640 if (view
->lines
== 0 && opt_file_argv
) {
3641 int pos
= argv_size(view
->argv
)
3642 - argv_size(opt_file_argv
) - 1;
3644 if (pos
> 0 && !strcmp(view
->argv
[pos
], "--")) {
3645 for (; view
->argv
[pos
]; pos
++) {
3646 free((void *) view
->argv
[pos
]);
3647 view
->argv
[pos
] = NULL
;
3651 io_done(view
->pipe
);
3652 if (io_run(&view
->io
, IO_RD
, view
->dir
, opt_env
, view
->argv
))
3659 return diff_common_read(view
, data
, state
);
3663 diff_blame_line(const char *ref
, const char *file
, unsigned long lineno
,
3664 struct blame_header
*header
, struct blame_commit
*commit
)
3666 char line_arg
[SIZEOF_STR
];
3667 const char *blame_argv
[] = {
3668 "git", "blame", encoding_arg
, "-p", line_arg
, ref
, "--", file
, NULL
3674 if (!string_format(line_arg
, "-L%ld,+1", lineno
))
3677 if (!io_run(&io
, IO_RD
, repo
.cdup
, opt_env
, blame_argv
))
3680 while ((buf
= io_get(&io
, '\n', TRUE
))) {
3682 if (!parse_blame_header(header
, buf
, 9999999))
3686 } else if (parse_blame_info(commit
, buf
)) {
3687 ok
= commit
->filename
!= NULL
;
3699 struct chunk_header_position
{
3700 unsigned long position
;
3701 unsigned long lines
;
3704 struct chunk_header
{
3705 struct chunk_header_position old
;
3706 struct chunk_header_position
new;
3710 parse_ulong(const char **pos_ptr
, unsigned long *value
, const char *skip
)
3712 const char *start
= *pos_ptr
;
3715 if (!isdigit(*start
))
3718 *value
= strtoul(start
, &end
, 10);
3723 while (skip
&& *start
&& strchr(skip
, *start
))
3730 parse_chunk_header(struct chunk_header
*header
, const char *line
)
3732 memset(header
, 0, sizeof(*header
));
3734 if (prefixcmp(line
, "@@ -"))
3737 line
+= STRING_SIZE("@@ -");
3739 return parse_ulong(&line
, &header
->old
.position
, ",") &&
3740 parse_ulong(&line
, &header
->old
.lines
, " +") &&
3741 parse_ulong(&line
, &header
->new.position
, ",") &&
3742 parse_ulong(&line
, &header
->new.lines
, NULL
);
3746 diff_get_lineno(struct view
*view
, struct line
*line
)
3748 const struct line
*header
, *chunk
;
3749 unsigned int lineno
;
3750 struct chunk_header chunk_header
;
3752 /* Verify that we are after a diff header and one of its chunks */
3753 header
= find_prev_line_by_type(view
, line
, LINE_DIFF_HEADER
);
3754 chunk
= find_prev_line_by_type(view
, line
, LINE_DIFF_CHUNK
);
3755 if (!header
|| !chunk
|| chunk
< header
)
3759 * In a chunk header, the number after the '+' sign is the number of its
3760 * following line, in the new version of the file. We increment this
3761 * number for each non-deletion line, until the given line position.
3763 if (!parse_chunk_header(&chunk_header
, chunk
->data
))
3766 lineno
= chunk_header
.new.position
;
3768 while (chunk
++ < line
)
3769 if (chunk
->type
!= LINE_DIFF_DEL
)
3776 parse_chunk_lineno(unsigned long *lineno
, const char *chunk
, int marker
)
3778 struct chunk_header chunk_header
;
3782 if (!parse_chunk_header(&chunk_header
, chunk
))
3785 *lineno
= marker
== '-' ? chunk_header
.old
.position
: chunk_header
.new.position
;
3790 diff_trace_origin(struct view
*view
, struct line
*line
)
3792 struct line
*diff
= find_prev_line_by_type(view
, line
, LINE_DIFF_HEADER
);
3793 struct line
*chunk
= find_prev_line_by_type(view
, line
, LINE_DIFF_CHUNK
);
3794 const char *chunk_data
;
3795 int chunk_marker
= line
->type
== LINE_DIFF_DEL
? '-' : '+';
3796 unsigned long lineno
= 0;
3797 const char *file
= NULL
;
3798 char ref
[SIZEOF_REF
];
3799 struct blame_header header
;
3800 struct blame_commit commit
;
3802 if (!diff
|| !chunk
|| chunk
== line
) {
3803 report("The line to trace must be inside a diff chunk");
3807 for (; diff
< line
&& !file
; diff
++) {
3808 const char *data
= diff
->data
;
3810 if (!prefixcmp(data
, "--- a/")) {
3811 file
= data
+ STRING_SIZE("--- a/");
3816 if (diff
== line
|| !file
) {
3817 report("Failed to read the file name");
3821 chunk_data
= chunk
->data
;
3823 if (!parse_chunk_lineno(&lineno
, chunk_data
, chunk_marker
)) {
3824 report("Failed to read the line number");
3829 report("This is the origin of the line");
3833 for (chunk
+= 1; chunk
< line
; chunk
++) {
3834 if (chunk
->type
== LINE_DIFF_ADD
) {
3835 lineno
+= chunk_marker
== '+';
3836 } else if (chunk
->type
== LINE_DIFF_DEL
) {
3837 lineno
+= chunk_marker
== '-';
3843 if (chunk_marker
== '+')
3844 string_copy(ref
, view
->vid
);
3846 string_format(ref
, "%s^", view
->vid
);
3848 if (!diff_blame_line(ref
, file
, lineno
, &header
, &commit
)) {
3849 report("Failed to read blame data");
3853 string_ncopy(opt_file
, commit
.filename
, strlen(commit
.filename
));
3854 string_copy(opt_ref
, header
.id
);
3855 opt_goto_line
= header
.orig_lineno
- 1;
3857 return REQ_VIEW_BLAME
;
3861 diff_get_pathname(struct view
*view
, struct line
*line
)
3863 const struct line
*header
;
3864 const char *dst
= NULL
;
3865 const char *prefixes
[] = { " b/", "cc ", "combined " };
3868 header
= find_prev_line_by_type(view
, line
, LINE_DIFF_HEADER
);
3872 for (i
= 0; i
< ARRAY_SIZE(prefixes
) && !dst
; i
++)
3873 dst
= strstr(header
->data
, prefixes
[i
]);
3875 return dst
? dst
+ strlen(prefixes
[--i
]) : NULL
;
3879 diff_common_edit(struct view
*view
, enum request request
, struct line
*line
)
3881 const char *file
= diff_get_pathname(view
, line
);
3882 char path
[SIZEOF_STR
];
3883 bool has_path
= file
&& string_format(path
, "%s%s", repo
.cdup
, file
);
3885 if (has_path
&& access(path
, R_OK
)) {
3886 report("Failed to open file: %s", file
);
3890 open_editor(file
, diff_get_lineno(view
, line
));
3895 diff_request(struct view
*view
, enum request request
, struct line
*line
)
3898 case REQ_VIEW_BLAME
:
3899 return diff_trace_origin(view
, line
);
3901 case REQ_DIFF_CONTEXT_UP
:
3902 case REQ_DIFF_CONTEXT_DOWN
:
3903 if (!update_diff_context(request
))
3910 return diff_common_edit(view
, request
, line
);
3913 return diff_common_enter(view
, request
, line
);
3916 if (string_rev_is_null(view
->vid
))
3923 return pager_request(view
, request
, line
);
3928 diff_select(struct view
*view
, struct line
*line
)
3930 if (line
->type
== LINE_DIFF_STAT
) {
3931 string_format(view
->ref
, "Press '%s' to jump to file diff",
3932 get_view_key(view
, REQ_ENTER
));
3934 const char *file
= diff_get_pathname(view
, line
);
3937 string_format(view
->ref
, "Changes to '%s'", file
);
3938 string_format(opt_file
, "%s", file
);
3941 string_ncopy(view
->ref
, view
->id
, strlen(view
->id
));
3942 pager_select(view
, line
);
3947 static struct view_ops diff_ops
= {
3950 VIEW_DIFF_LIKE
| VIEW_ADD_DESCRIBE_REF
| VIEW_ADD_PAGER_REFS
| VIEW_FILE_FILTER
| VIEW_REFRESH
,
3951 sizeof(struct diff_state
),
3965 help_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
3967 if (line
->type
== LINE_HELP_KEYMAP
) {
3968 struct keymap
*keymap
= line
->data
;
3970 draw_formatted(view
, line
->type
, "[%c] %s bindings",
3971 keymap
->hidden
? '+' : '-', keymap
->name
);
3974 return pager_draw(view
, line
, lineno
);
3979 help_open_keymap_title(struct view
*view
, struct keymap
*keymap
)
3981 add_line(view
, keymap
, LINE_HELP_KEYMAP
, 0, FALSE
);
3982 return keymap
->hidden
;
3985 struct help_request_iterator
{
3987 struct keymap
*keymap
;
3993 help_open_keymap(void *data
, const struct request_info
*req_info
, const char *group
)
3995 struct help_request_iterator
*iterator
= data
;
3996 struct view
*view
= iterator
->view
;
3997 struct keymap
*keymap
= iterator
->keymap
;
3998 const char *key
= get_keys(keymap
, req_info
->request
, TRUE
);
4000 if (req_info
->request
== REQ_NONE
|| !key
|| !*key
)
4003 if (iterator
->add_title
&& help_open_keymap_title(view
, keymap
))
4005 iterator
->add_title
= FALSE
;
4007 if (iterator
->group
!= group
) {
4008 add_line_text(view
, group
, LINE_HELP_GROUP
);
4009 iterator
->group
= group
;
4012 add_line_format(view
, LINE_DEFAULT
, " %-25s %-20s %s", key
,
4013 enum_name(*req_info
), req_info
->help
);
4018 help_open_keymap_run_requests(struct help_request_iterator
*iterator
)
4020 struct view
*view
= iterator
->view
;
4021 struct keymap
*keymap
= iterator
->keymap
;
4022 char buf
[SIZEOF_STR
];
4023 const char *group
= "External commands:";
4026 for (i
= 0; TRUE
; i
++) {
4027 struct run_request
*req
= get_run_request(REQ_NONE
+ i
+ 1);
4033 if (req
->keymap
!= keymap
)
4036 key
= get_key_name(req
->key
);
4038 key
= "(no key defined)";
4040 if (iterator
->add_title
&& help_open_keymap_title(view
, keymap
))
4042 iterator
->add_title
= FALSE
;
4045 add_line_text(view
, group
, LINE_HELP_GROUP
);
4049 if (!argv_to_string(req
->argv
, buf
, sizeof(buf
), " "))
4052 add_line_format(view
, LINE_DEFAULT
, " %-25s `%s`", key
, buf
);
4057 help_open(struct view
*view
, enum open_flags flags
)
4059 struct keymap
*keymap
;
4062 add_line_text(view
, "Quick reference for tig keybindings:", LINE_DEFAULT
);
4063 add_line_text(view
, "", LINE_DEFAULT
);
4065 for (keymap
= get_keymaps(); keymap
; keymap
= keymap
->next
) {
4066 struct help_request_iterator iterator
= { view
, keymap
, TRUE
};
4068 if (foreach_request(help_open_keymap
, &iterator
))
4069 help_open_keymap_run_requests(&iterator
);
4076 help_request(struct view
*view
, enum request request
, struct line
*line
)
4080 if (line
->type
== LINE_HELP_KEYMAP
) {
4081 struct keymap
*keymap
= line
->data
;
4083 keymap
->hidden
= !keymap
->hidden
;
4089 return pager_request(view
, request
, line
);
4094 help_done(struct view
*view
)
4098 for (i
= 0; i
< view
->lines
; i
++)
4099 if (view
->line
[i
].type
== LINE_HELP_KEYMAP
)
4100 view
->line
[i
].data
= NULL
;
4103 static struct view_ops help_ops
= {
4122 /* The top of the path stack. */
4123 static struct view_history tree_view_history
= { sizeof(char *) };
4126 pop_tree_stack_entry(struct position
*position
)
4128 char *path_position
= NULL
;
4130 pop_view_history_state(&tree_view_history
, position
, &path_position
);
4131 path_position
[0] = 0;
4135 push_tree_stack_entry(const char *name
, struct position
*position
)
4137 size_t pathlen
= strlen(opt_path
);
4138 char *path_position
= opt_path
+ pathlen
;
4139 struct view_state
*state
= push_view_history_state(&tree_view_history
, position
, &path_position
);
4144 if (!string_format_from(opt_path
, &pathlen
, "%s/", name
)) {
4145 pop_tree_stack_entry(NULL
);
4149 clear_position(position
);
4152 /* Parse output from git-ls-tree(1):
4154 * 100644 blob 95925677ca47beb0b8cce7c0e0011bcc3f61470f 213045 tig.c
4157 #define SIZEOF_TREE_ATTR \
4158 STRING_SIZE("100644 blob f931e1d229c3e185caad4449bf5b66ed72462657\t")
4160 #define SIZEOF_TREE_MODE \
4161 STRING_SIZE("100644 ")
4163 #define TREE_ID_OFFSET \
4164 STRING_SIZE("100644 blob ")
4166 #define tree_path_is_parent(path) (!strcmp("..", (path)))
4169 char id
[SIZEOF_REV
];
4170 char commit
[SIZEOF_REV
];
4172 struct time time
; /* Date from the author ident. */
4173 const struct ident
*author
; /* Author of the commit. */
4179 char commit
[SIZEOF_REV
];
4180 const struct ident
*author
;
4181 struct time author_time
;
4187 tree_path(const struct line
*line
)
4189 return ((struct tree_entry
*) line
->data
)->name
;
4193 tree_compare_entry(const struct line
*line1
, const struct line
*line2
)
4195 if (line1
->type
!= line2
->type
)
4196 return line1
->type
== LINE_TREE_DIR
? -1 : 1;
4197 return strcmp(tree_path(line1
), tree_path(line2
));
4200 static const enum sort_field tree_sort_fields
[] = {
4201 ORDERBY_NAME
, ORDERBY_DATE
, ORDERBY_AUTHOR
4203 static struct sort_state tree_sort_state
= SORT_STATE(tree_sort_fields
);
4206 tree_compare(const void *l1
, const void *l2
)
4208 const struct line
*line1
= (const struct line
*) l1
;
4209 const struct line
*line2
= (const struct line
*) l2
;
4210 const struct tree_entry
*entry1
= ((const struct line
*) l1
)->data
;
4211 const struct tree_entry
*entry2
= ((const struct line
*) l2
)->data
;
4213 if (line1
->type
== LINE_TREE_HEAD
)
4215 if (line2
->type
== LINE_TREE_HEAD
)
4218 switch (get_sort_field(tree_sort_state
)) {
4220 return sort_order(tree_sort_state
, timecmp(&entry1
->time
, &entry2
->time
));
4222 case ORDERBY_AUTHOR
:
4223 return sort_order(tree_sort_state
, ident_compare(entry1
->author
, entry2
->author
));
4227 return sort_order(tree_sort_state
, tree_compare_entry(line1
, line2
));
4232 static struct line
*
4233 tree_entry(struct view
*view
, enum line_type type
, const char *path
,
4234 const char *mode
, const char *id
, unsigned long size
)
4236 bool custom
= type
== LINE_TREE_HEAD
|| tree_path_is_parent(path
);
4237 struct tree_entry
*entry
;
4238 struct line
*line
= add_line_alloc(view
, &entry
, type
, strlen(path
), custom
);
4243 strncpy(entry
->name
, path
, strlen(path
));
4245 entry
->mode
= strtoul(mode
, NULL
, 8);
4247 string_copy_rev(entry
->id
, id
);
4254 tree_read_date(struct view
*view
, char *text
, struct tree_state
*state
)
4256 if (!text
&& state
->read_date
) {
4257 state
->read_date
= FALSE
;
4261 /* Find next entry to process */
4262 const char *log_file
[] = {
4263 "git", "log", encoding_arg
, "--no-color", "--pretty=raw",
4264 "--cc", "--raw", view
->id
, "--", "%(directory)", NULL
4268 tree_entry(view
, LINE_TREE_HEAD
, opt_path
, NULL
, NULL
, 0);
4269 tree_entry(view
, LINE_TREE_DIR
, "..", "040000", view
->ref
, 0);
4270 report("Tree is empty");
4274 if (!begin_update(view
, repo
.cdup
, log_file
, OPEN_EXTRA
)) {
4275 report("Failed to load tree data");
4279 state
->read_date
= TRUE
;
4282 } else if (*text
== 'c' && get_line_type(text
) == LINE_COMMIT
) {
4283 string_copy_rev_from_commit_line(state
->commit
, text
);
4285 } else if (*text
== 'a' && get_line_type(text
) == LINE_AUTHOR
) {
4286 parse_author_line(text
+ STRING_SIZE("author "),
4287 &state
->author
, &state
->author_time
);
4289 } else if (*text
== ':') {
4291 size_t annotated
= 1;
4294 pos
= strchr(text
, '\t');
4298 if (*opt_path
&& !strncmp(text
, opt_path
, strlen(opt_path
)))
4299 text
+= strlen(opt_path
);
4300 pos
= strchr(text
, '/');
4304 for (i
= 1; i
< view
->lines
; i
++) {
4305 struct line
*line
= &view
->line
[i
];
4306 struct tree_entry
*entry
= line
->data
;
4308 annotated
+= !!entry
->author
;
4309 if (entry
->author
|| strcmp(entry
->name
, text
))
4312 string_copy_rev(entry
->commit
, state
->commit
);
4313 entry
->author
= state
->author
;
4314 entry
->time
= state
->author_time
;
4319 if (annotated
== view
->lines
)
4320 io_kill(view
->pipe
);
4325 static inline size_t
4326 parse_size(const char *text
, int *max_digits
)
4331 while (*text
== ' ')
4334 while (isdigit(*text
)) {
4335 size
= (size
* 10) + (*text
++ - '0');
4339 if (digits
> *max_digits
)
4340 *max_digits
= digits
;
4346 tree_read(struct view
*view
, char *text
)
4348 struct tree_state
*state
= view
->private;
4349 struct tree_entry
*data
;
4350 struct line
*entry
, *line
;
4351 enum line_type type
;
4352 size_t textlen
= text
? strlen(text
) : 0;
4353 const char *attr_offset
= text
+ SIZEOF_TREE_ATTR
;
4357 if (state
->read_date
|| !text
)
4358 return tree_read_date(view
, text
, state
);
4360 if (textlen
<= SIZEOF_TREE_ATTR
)
4362 if (view
->lines
== 0 &&
4363 !tree_entry(view
, LINE_TREE_HEAD
, opt_path
, NULL
, NULL
, 0))
4366 size
= parse_size(attr_offset
, &state
->size_width
);
4367 path
= strchr(attr_offset
, '\t');
4372 /* Strip the path part ... */
4374 size_t pathlen
= textlen
- SIZEOF_TREE_ATTR
;
4375 size_t striplen
= strlen(opt_path
);
4377 if (pathlen
> striplen
)
4378 memmove(path
, path
+ striplen
,
4379 pathlen
- striplen
+ 1);
4381 /* Insert "link" to parent directory. */
4382 if (view
->lines
== 1 &&
4383 !tree_entry(view
, LINE_TREE_DIR
, "..", "040000", view
->ref
, 0))
4387 type
= text
[SIZEOF_TREE_MODE
] == 't' ? LINE_TREE_DIR
: LINE_TREE_FILE
;
4388 entry
= tree_entry(view
, type
, path
, text
, text
+ TREE_ID_OFFSET
, size
);
4393 /* Skip "Directory ..." and ".." line. */
4394 for (line
= &view
->line
[1 + !!*opt_path
]; line
< entry
; line
++) {
4395 if (tree_compare_entry(line
, entry
) <= 0)
4398 memmove(line
+ 1, line
, (entry
- line
) * sizeof(*entry
));
4402 for (; line
<= entry
; line
++)
4403 line
->dirty
= line
->cleareol
= 1;
4407 /* Move the current line to the first tree entry. */
4408 if (!check_position(&view
->prev_pos
) && !check_position(&view
->pos
))
4409 goto_view_line(view
, 0, 1);
4415 tree_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
4417 struct tree_state
*state
= view
->private;
4418 struct tree_entry
*entry
= line
->data
;
4420 if (line
->type
== LINE_TREE_HEAD
) {
4421 if (draw_text(view
, line
->type
, "Directory path /"))
4424 if (draw_mode(view
, entry
->mode
))
4427 if (draw_author(view
, entry
->author
))
4430 if (draw_file_size(view
, entry
->size
, state
->size_width
,
4431 line
->type
!= LINE_TREE_FILE
))
4434 if (draw_date(view
, &entry
->time
))
4437 if (draw_id(view
, entry
->commit
))
4441 draw_text(view
, line
->type
, entry
->name
);
4446 open_blob_editor(const char *id
, const char *name
, unsigned int lineno
)
4448 const char *blob_argv
[] = { "git", "cat-file", "blob", id
, NULL
};
4449 char file
[SIZEOF_STR
];
4455 if (!string_format(file
, "%s/tigblob.XXXXXX.%s", get_temp_dir(), name
)) {
4456 report("Temporary file name is too long");
4460 fd
= mkstemps(file
, strlen(name
) + 1);
4463 report("Failed to create temporary file");
4464 else if (!io_run_append(blob_argv
, fd
))
4465 report("Failed to save blob data to file");
4467 open_editor(file
, lineno
);
4473 tree_request(struct view
*view
, enum request request
, struct line
*line
)
4475 enum open_flags flags
;
4476 struct tree_entry
*entry
= line
->data
;
4479 case REQ_VIEW_BLAME
:
4480 if (line
->type
!= LINE_TREE_FILE
) {
4481 report("Blame only supported for files");
4485 string_copy(opt_ref
, view
->vid
);
4489 if (line
->type
!= LINE_TREE_FILE
) {
4490 report("Edit only supported for files");
4491 } else if (!is_head_commit(view
->vid
)) {
4492 open_blob_editor(entry
->id
, entry
->name
, 0);
4494 open_editor(opt_file
, 0);
4498 case REQ_TOGGLE_SORT_FIELD
:
4499 case REQ_TOGGLE_SORT_ORDER
:
4500 sort_view(view
, request
, &tree_sort_state
, tree_compare
);
4506 /* quit view if at top of tree */
4507 return REQ_VIEW_CLOSE
;
4510 line
= &view
->line
[1];
4520 /* Cleanup the stack if the tree view is at a different tree. */
4522 reset_view_history(&tree_view_history
);
4524 switch (line
->type
) {
4526 /* Depending on whether it is a subdirectory or parent link
4527 * mangle the path buffer. */
4528 if (line
== &view
->line
[1] && *opt_path
) {
4529 pop_tree_stack_entry(&view
->pos
);
4532 const char *basename
= tree_path(line
);
4534 push_tree_stack_entry(basename
, &view
->pos
);
4537 /* Trees and subtrees share the same ID, so they are not not
4538 * unique like blobs. */
4539 flags
= OPEN_RELOAD
;
4540 request
= REQ_VIEW_TREE
;
4543 case LINE_TREE_FILE
:
4544 flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
4545 request
= REQ_VIEW_BLOB
;
4552 open_view(view
, request
, flags
);
4558 tree_grep(struct view
*view
, struct line
*line
)
4560 struct tree_entry
*entry
= line
->data
;
4561 const char *text
[] = {
4563 mkauthor(entry
->author
, opt_author_width
, opt_author
),
4564 mkdate(&entry
->time
, opt_date
),
4568 return grep_text(view
, text
);
4572 tree_select(struct view
*view
, struct line
*line
)
4574 struct tree_entry
*entry
= line
->data
;
4576 if (line
->type
== LINE_TREE_HEAD
) {
4577 string_format(view
->ref
, "Files in /%s", opt_path
);
4581 if (line
->type
== LINE_TREE_DIR
&& tree_path_is_parent(entry
->name
)) {
4582 string_copy(view
->ref
, "Open parent directory");
4587 if (line
->type
== LINE_TREE_FILE
) {
4588 string_copy_rev(ref_blob
, entry
->id
);
4589 string_format(opt_file
, "%s%s", opt_path
, tree_path(line
));
4592 string_copy_rev(view
->ref
, entry
->id
);
4596 tree_open(struct view
*view
, enum open_flags flags
)
4598 static const char *tree_argv
[] = {
4599 "git", "ls-tree", "-l", "%(commit)", "%(directory)", NULL
4602 if (string_rev_is_null(ref_commit
)) {
4603 report("No tree exists for this commit");
4607 if (view
->lines
== 0 && repo
.prefix
[0]) {
4608 char *pos
= repo
.prefix
;
4610 while (pos
&& *pos
) {
4611 char *end
= strchr(pos
, '/');
4615 push_tree_stack_entry(pos
, &view
->pos
);
4623 } else if (strcmp(view
->vid
, view
->id
)) {
4627 return begin_update(view
, repo
.cdup
, tree_argv
, flags
);
4630 static struct view_ops tree_ops
= {
4633 VIEW_SEND_CHILD_ENTER
,
4634 sizeof(struct tree_state
),
4644 blob_open(struct view
*view
, enum open_flags flags
)
4646 static const char *blob_argv
[] = {
4647 "git", "cat-file", "blob", "%(blob)", NULL
4650 if (!ref_blob
[0] && opt_file
[0]) {
4651 const char *commit
= ref_commit
[0] ? ref_commit
: "HEAD";
4652 char blob_spec
[SIZEOF_STR
];
4653 const char *rev_parse_argv
[] = {
4654 "git", "rev-parse", blob_spec
, NULL
4657 if (!string_format(blob_spec
, "%s:%s", commit
, opt_file
) ||
4658 !io_run_buf(rev_parse_argv
, ref_blob
, sizeof(ref_blob
))) {
4659 report("Failed to resolve blob from file name");
4665 report("No file chosen, press %s to open tree view",
4666 get_view_key(view
, REQ_VIEW_TREE
));
4670 view
->encoding
= get_path_encoding(opt_file
, default_encoding
);
4672 return begin_update(view
, NULL
, blob_argv
, flags
);
4676 blob_read(struct view
*view
, char *line
)
4680 return add_line_text(view
, line
, LINE_DEFAULT
) != NULL
;
4684 blob_request(struct view
*view
, enum request request
, struct line
*line
)
4687 case REQ_VIEW_BLAME
:
4689 string_copy(opt_ref
, view
->parent
->vid
);
4693 open_blob_editor(view
->vid
, NULL
, (line
- view
->line
) + 1);
4696 return pager_request(view
, request
, line
);
4700 static struct view_ops blob_ops
= {
4716 * Loading the blame view is a two phase job:
4718 * 1. File content is read either using opt_file from the
4719 * filesystem or using git-cat-file.
4720 * 2. Then blame information is incrementally added by
4721 * reading output from git-blame.
4724 struct blame_history_state
{
4725 char id
[SIZEOF_REV
]; /* SHA1 ID. */
4726 const char *filename
; /* Name of file. */
4729 static struct view_history blame_view_history
= { sizeof(struct blame_history_state
) };
4732 struct blame_commit
*commit
;
4733 unsigned long lineno
;
4737 struct blame_state
{
4738 struct blame_commit
*commit
;
4741 bool auto_filename_display
;
4742 /* The history state for the current view is cached in the view
4743 * state so it always matches what was used to load the current blame
4745 struct blame_history_state history_state
;
4749 blame_detect_filename_display(struct view
*view
)
4751 bool show_filenames
= FALSE
;
4752 const char *filename
= NULL
;
4755 if (opt_blame_argv
) {
4756 for (i
= 0; opt_blame_argv
[i
]; i
++) {
4757 if (prefixcmp(opt_blame_argv
[i
], "-C"))
4760 show_filenames
= TRUE
;
4764 for (i
= 0; i
< view
->lines
; i
++) {
4765 struct blame
*blame
= view
->line
[i
].data
;
4767 if (blame
->commit
&& blame
->commit
->id
[0]) {
4769 filename
= blame
->commit
->filename
;
4770 else if (strcmp(filename
, blame
->commit
->filename
))
4771 show_filenames
= TRUE
;
4775 return show_filenames
;
4779 blame_open(struct view
*view
, enum open_flags flags
)
4781 struct blame_state
*state
= view
->private;
4782 const char *file_argv
[] = { repo
.cdup
, opt_file
, NULL
};
4783 char path
[SIZEOF_STR
];
4787 report("No file chosen, press %s to open tree view",
4788 get_view_key(view
, REQ_VIEW_TREE
));
4792 if (!view
->prev
&& *repo
.prefix
&& !(flags
& (OPEN_RELOAD
| OPEN_REFRESH
))) {
4793 string_copy(path
, opt_file
);
4794 if (!string_format(opt_file
, "%s%s", repo
.prefix
, path
)) {
4795 report("Failed to setup the blame view");
4800 if (*opt_ref
|| !begin_update(view
, repo
.cdup
, file_argv
, flags
)) {
4801 const char *blame_cat_file_argv
[] = {
4802 "git", "cat-file", "blob", "%(ref):%(file)", NULL
4805 if (!begin_update(view
, repo
.cdup
, blame_cat_file_argv
, flags
))
4809 /* First pass: remove multiple references to the same commit. */
4810 for (i
= 0; i
< view
->lines
; i
++) {
4811 struct blame
*blame
= view
->line
[i
].data
;
4813 if (blame
->commit
&& blame
->commit
->id
[0])
4814 blame
->commit
->id
[0] = 0;
4816 blame
->commit
= NULL
;
4819 /* Second pass: free existing references. */
4820 for (i
= 0; i
< view
->lines
; i
++) {
4821 struct blame
*blame
= view
->line
[i
].data
;
4824 free(blame
->commit
);
4827 if (!(flags
& OPEN_RELOAD
))
4828 reset_view_history(&blame_view_history
);
4829 string_copy_rev(state
->history_state
.id
, opt_ref
);
4830 state
->history_state
.filename
= get_path(opt_file
);
4831 if (!state
->history_state
.filename
)
4833 string_format(view
->vid
, "%s", opt_file
);
4834 string_format(view
->ref
, "%s ...", opt_file
);
4839 static struct blame_commit
*
4840 get_blame_commit(struct view
*view
, const char *id
)
4844 for (i
= 0; i
< view
->lines
; i
++) {
4845 struct blame
*blame
= view
->line
[i
].data
;
4850 if (!strncmp(blame
->commit
->id
, id
, SIZEOF_REV
- 1))
4851 return blame
->commit
;
4855 struct blame_commit
*commit
= calloc(1, sizeof(*commit
));
4858 string_ncopy(commit
->id
, id
, SIZEOF_REV
);
4863 static struct blame_commit
*
4864 read_blame_commit(struct view
*view
, const char *text
, struct blame_state
*state
)
4866 struct blame_header header
;
4867 struct blame_commit
*commit
;
4868 struct blame
*blame
;
4870 if (!parse_blame_header(&header
, text
, view
->lines
))
4873 commit
= get_blame_commit(view
, text
);
4877 state
->blamed
+= header
.group
;
4878 while (header
.group
--) {
4879 struct line
*line
= &view
->line
[header
.lineno
+ header
.group
- 1];
4882 blame
->commit
= commit
;
4883 blame
->lineno
= header
.orig_lineno
+ header
.group
- 1;
4891 blame_read_file(struct view
*view
, const char *text
, struct blame_state
*state
)
4894 const char *blame_argv
[] = {
4895 "git", "blame", encoding_arg
, "%(blameargs)", "--incremental",
4896 *opt_ref
? opt_ref
: "--incremental", "--", opt_file
, NULL
4899 if (view
->lines
== 0 && !view
->prev
)
4900 die("No blame exist for %s", view
->vid
);
4902 if (view
->lines
== 0 || !begin_update(view
, repo
.cdup
, blame_argv
, OPEN_EXTRA
)) {
4903 report("Failed to load blame data");
4907 if (opt_goto_line
> 0) {
4908 select_view_line(view
, opt_goto_line
);
4912 state
->done_reading
= TRUE
;
4916 size_t textlen
= strlen(text
);
4917 struct blame
*blame
;
4919 if (!add_line_alloc(view
, &blame
, LINE_ID
, textlen
, FALSE
))
4922 blame
->commit
= NULL
;
4923 strncpy(blame
->text
, text
, textlen
);
4924 blame
->text
[textlen
] = 0;
4930 blame_read(struct view
*view
, char *line
)
4932 struct blame_state
*state
= view
->private;
4934 if (!state
->done_reading
)
4935 return blame_read_file(view
, line
, state
);
4938 state
->auto_filename_display
= blame_detect_filename_display(view
);
4939 string_format(view
->ref
, "%s", view
->vid
);
4940 if (view_is_displayed(view
)) {
4941 update_view_title(view
);
4942 redraw_view_from(view
, 0);
4947 if (!state
->commit
) {
4948 state
->commit
= read_blame_commit(view
, line
, state
);
4949 string_format(view
->ref
, "%s %2zd%%", view
->vid
,
4950 view
->lines
? state
->blamed
* 100 / view
->lines
: 0);
4952 } else if (parse_blame_info(state
->commit
, line
)) {
4953 if (!state
->commit
->filename
)
4955 state
->commit
= NULL
;
4962 blame_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
4964 struct blame_state
*state
= view
->private;
4965 struct blame
*blame
= line
->data
;
4966 struct time
*time
= NULL
;
4967 const char *id
= NULL
, *filename
= NULL
;
4968 const struct ident
*author
= NULL
;
4969 enum line_type id_type
= LINE_ID
;
4970 static const enum line_type blame_colors
[] = {
4980 #define BLAME_COLOR(i) \
4981 (blame_colors[(i) % ARRAY_SIZE(blame_colors)])
4983 if (blame
->commit
&& blame
->commit
->filename
) {
4984 id
= blame
->commit
->id
;
4985 author
= blame
->commit
->author
;
4986 filename
= blame
->commit
->filename
;
4987 time
= &blame
->commit
->time
;
4988 id_type
= BLAME_COLOR((long) blame
->commit
);
4991 if (draw_date(view
, time
))
4994 if (draw_author(view
, author
))
4997 if (draw_filename(view
, filename
, state
->auto_filename_display
))
5000 if (draw_id_custom(view
, id_type
, id
, opt_id_cols
))
5003 if (draw_lineno(view
, lineno
))
5006 draw_text(view
, LINE_DEFAULT
, blame
->text
);
5011 check_blame_commit(struct blame
*blame
, bool check_null_id
)
5014 report("Commit data not loaded yet");
5015 else if (check_null_id
&& string_rev_is_null(blame
->commit
->id
))
5016 report("No commit exist for the selected line");
5023 setup_blame_parent_line(struct view
*view
, struct blame
*blame
)
5025 char from
[SIZEOF_REF
+ SIZEOF_STR
];
5026 char to
[SIZEOF_REF
+ SIZEOF_STR
];
5027 const char *diff_tree_argv
[] = {
5028 "git", "diff", encoding_arg
, "--no-textconv", "--no-extdiff",
5029 "--no-color", "-U0", from
, to
, "--", NULL
5032 int parent_lineno
= -1;
5033 int blamed_lineno
= -1;
5036 if (!string_format(from
, "%s:%s", opt_ref
, opt_file
) ||
5037 !string_format(to
, "%s:%s", blame
->commit
->id
, blame
->commit
->filename
) ||
5038 !io_run(&io
, IO_RD
, NULL
, opt_env
, diff_tree_argv
))
5041 while ((line
= io_get(&io
, '\n', TRUE
))) {
5043 char *pos
= strchr(line
, '+');
5045 parent_lineno
= atoi(line
+ 4);
5047 blamed_lineno
= atoi(pos
+ 1);
5049 } else if (*line
== '+' && parent_lineno
!= -1) {
5050 if (blame
->lineno
== blamed_lineno
- 1 &&
5051 !strcmp(blame
->text
, line
+ 1)) {
5052 view
->pos
.lineno
= parent_lineno
? parent_lineno
- 1 : 0;
5063 blame_go_forward(struct view
*view
, struct blame
*blame
, bool parent
)
5065 struct blame_state
*state
= view
->private;
5066 struct blame_history_state
*history_state
= &state
->history_state
;
5067 struct blame_commit
*commit
= blame
->commit
;
5068 const char *id
= parent
? commit
->parent_id
: commit
->id
;
5069 const char *filename
= parent
? commit
->parent_filename
: commit
->filename
;
5071 if (!*id
&& parent
) {
5072 report("The selected commit has no parents");
5076 if (!strcmp(history_state
->id
, id
) && !strcmp(history_state
->filename
, filename
)) {
5077 report("The selected commit is already displayed");
5081 if (!push_view_history_state(&blame_view_history
, &view
->pos
, history_state
)) {
5082 report("Failed to save current view state");
5086 string_ncopy(opt_ref
, id
, sizeof(commit
->id
));
5087 string_ncopy(opt_file
, filename
, strlen(filename
));
5089 setup_blame_parent_line(view
, blame
);
5090 opt_goto_line
= blame
->lineno
;
5095 blame_go_back(struct view
*view
)
5097 struct blame_history_state history_state
;
5099 if (!pop_view_history_state(&blame_view_history
, &view
->pos
, &history_state
)) {
5100 report("Already at start of history");
5104 string_copy(opt_ref
, history_state
.id
);
5105 string_ncopy(opt_file
, history_state
.filename
, strlen(history_state
.filename
));
5106 opt_goto_line
= view
->pos
.lineno
;
5111 blame_request(struct view
*view
, enum request request
, struct line
*line
)
5113 enum open_flags flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
5114 struct blame
*blame
= line
->data
;
5117 case REQ_VIEW_BLAME
:
5119 if (!check_blame_commit(blame
, TRUE
))
5121 blame_go_forward(view
, blame
, request
== REQ_PARENT
);
5125 blame_go_back(view
);
5129 if (!check_blame_commit(blame
, FALSE
))
5132 if (view_is_displayed(VIEW(REQ_VIEW_DIFF
)) &&
5133 !strcmp(blame
->commit
->id
, VIEW(REQ_VIEW_DIFF
)->ref
))
5136 if (string_rev_is_null(blame
->commit
->id
)) {
5137 struct view
*diff
= VIEW(REQ_VIEW_DIFF
);
5138 const char *diff_parent_argv
[] = {
5139 GIT_DIFF_BLAME(encoding_arg
,
5140 opt_diff_context_arg
,
5141 opt_ignore_space_arg
, view
->vid
)
5143 const char *diff_no_parent_argv
[] = {
5144 GIT_DIFF_BLAME_NO_PARENT(encoding_arg
,
5145 opt_diff_context_arg
,
5146 opt_ignore_space_arg
, view
->vid
)
5148 const char **diff_index_argv
= *blame
->commit
->parent_id
5149 ? diff_parent_argv
: diff_no_parent_argv
;
5151 open_argv(view
, diff
, diff_index_argv
, NULL
, flags
);
5153 string_copy_rev(diff
->ref
, NULL_ID
);
5155 open_view(view
, REQ_VIEW_DIFF
, flags
);
5167 blame_grep(struct view
*view
, struct line
*line
)
5169 struct blame
*blame
= line
->data
;
5170 struct blame_commit
*commit
= blame
->commit
;
5171 const char *text
[] = {
5173 commit
? commit
->title
: "",
5174 commit
? commit
->id
: "",
5175 commit
? mkauthor(commit
->author
, opt_author_width
, opt_author
) : "",
5176 commit
? mkdate(&commit
->time
, opt_date
) : "",
5180 return grep_text(view
, text
);
5184 blame_select(struct view
*view
, struct line
*line
)
5186 struct blame
*blame
= line
->data
;
5187 struct blame_commit
*commit
= blame
->commit
;
5192 if (string_rev_is_null(commit
->id
))
5193 string_ncopy(ref_commit
, "HEAD", 4);
5195 string_copy_rev(ref_commit
, commit
->id
);
5198 static struct view_ops blame_ops
= {
5201 VIEW_ALWAYS_LINENO
| VIEW_SEND_CHILD_ENTER
,
5202 sizeof(struct blame_state
),
5216 const struct ident
*author
; /* Author of the last commit. */
5217 struct time time
; /* Date of the last activity. */
5218 char title
[128]; /* First line of the commit message. */
5219 const struct ref
*ref
; /* Name and commit ID information. */
5222 static const struct ref branch_all
;
5223 #define BRANCH_ALL_NAME "All branches"
5224 #define branch_is_all(branch) ((branch)->ref == &branch_all)
5226 static const enum sort_field branch_sort_fields
[] = {
5227 ORDERBY_NAME
, ORDERBY_DATE
, ORDERBY_AUTHOR
5229 static struct sort_state branch_sort_state
= SORT_STATE(branch_sort_fields
);
5231 struct branch_state
{
5232 char id
[SIZEOF_REV
];
5233 size_t max_ref_length
;
5237 branch_compare(const void *l1
, const void *l2
)
5239 const struct branch
*branch1
= ((const struct line
*) l1
)->data
;
5240 const struct branch
*branch2
= ((const struct line
*) l2
)->data
;
5242 if (branch_is_all(branch1
))
5244 else if (branch_is_all(branch2
))
5247 switch (get_sort_field(branch_sort_state
)) {
5249 return sort_order(branch_sort_state
, timecmp(&branch1
->time
, &branch2
->time
));
5251 case ORDERBY_AUTHOR
:
5252 return sort_order(branch_sort_state
, ident_compare(branch1
->author
, branch2
->author
));
5256 return sort_order(branch_sort_state
, strcmp(branch1
->ref
->name
, branch2
->ref
->name
));
5261 branch_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
5263 struct branch_state
*state
= view
->private;
5264 struct branch
*branch
= line
->data
;
5265 enum line_type type
= branch_is_all(branch
) ? LINE_DEFAULT
: get_line_type_from_ref(branch
->ref
);
5266 const char *branch_name
= branch_is_all(branch
) ? BRANCH_ALL_NAME
: branch
->ref
->name
;
5268 if (draw_lineno(view
, lineno
))
5271 if (draw_date(view
, &branch
->time
))
5274 if (draw_author(view
, branch
->author
))
5277 if (draw_field(view
, type
, branch_name
, state
->max_ref_length
, ALIGN_LEFT
, FALSE
))
5280 if (draw_id(view
, branch
->ref
->id
))
5283 draw_text(view
, LINE_DEFAULT
, branch
->title
);
5288 branch_request(struct view
*view
, enum request request
, struct line
*line
)
5290 struct branch
*branch
= line
->data
;
5298 case REQ_TOGGLE_SORT_FIELD
:
5299 case REQ_TOGGLE_SORT_ORDER
:
5300 sort_view(view
, request
, &branch_sort_state
, branch_compare
);
5305 const struct ref
*ref
= branch
->ref
;
5306 const char *all_branches_argv
[] = {
5307 GIT_MAIN_LOG(encoding_arg
, "", branch_is_all(branch
) ? "--all" : ref
->name
, "")
5309 struct view
*main_view
= VIEW(REQ_VIEW_MAIN
);
5311 open_argv(view
, main_view
, all_branches_argv
, NULL
, OPEN_SPLIT
);
5314 case REQ_JUMP_COMMIT
:
5318 for (lineno
= 0; lineno
< view
->lines
; lineno
++) {
5319 struct branch
*branch
= view
->line
[lineno
].data
;
5321 if (!strncasecmp(branch
->ref
->id
, opt_search
, strlen(opt_search
))) {
5322 select_view_line(view
, lineno
);
5334 branch_read(struct view
*view
, char *line
)
5336 struct branch_state
*state
= view
->private;
5337 const char *title
= NULL
;
5338 const struct ident
*author
= NULL
;
5339 struct time time
= {};
5345 switch (get_line_type(line
)) {
5347 string_copy_rev_from_commit_line(state
->id
, line
);
5351 parse_author_line(line
+ STRING_SIZE("author "), &author
, &time
);
5355 title
= line
+ STRING_SIZE("title ");
5358 for (i
= 0; i
< view
->lines
; i
++) {
5359 struct branch
*branch
= view
->line
[i
].data
;
5361 if (strcmp(branch
->ref
->id
, state
->id
))
5365 branch
->author
= author
;
5366 branch
->time
= time
;
5370 string_expand(branch
->title
, sizeof(branch
->title
), title
, 1);
5372 view
->line
[i
].dirty
= TRUE
;
5379 branch_open_visitor(void *data
, const struct ref
*ref
)
5381 struct view
*view
= data
;
5382 struct branch_state
*state
= view
->private;
5383 struct branch
*branch
;
5384 bool is_all
= ref
== &branch_all
;
5387 if (ref
->tag
|| ref
->ltag
)
5390 if (!add_line_alloc(view
, &branch
, LINE_DEFAULT
, 0, is_all
))
5393 ref_length
= is_all
? STRING_SIZE(BRANCH_ALL_NAME
) : strlen(ref
->name
);
5394 if (ref_length
> state
->max_ref_length
)
5395 state
->max_ref_length
= ref_length
;
5402 branch_open(struct view
*view
, enum open_flags flags
)
5404 const char *branch_log
[] = {
5405 "git", "log", encoding_arg
, "--no-color", "--date=raw",
5406 "--pretty=format:commit %H%nauthor %an <%ae> %ad%ntitle %s",
5407 "--all", "--simplify-by-decoration", NULL
5410 if (!begin_update(view
, NULL
, branch_log
, OPEN_RELOAD
)) {
5411 report("Failed to load branch data");
5415 branch_open_visitor(view
, &branch_all
);
5416 foreach_ref(branch_open_visitor
, view
);
5422 branch_grep(struct view
*view
, struct line
*line
)
5424 struct branch
*branch
= line
->data
;
5425 const char *text
[] = {
5427 mkauthor(branch
->author
, opt_author_width
, opt_author
),
5431 return grep_text(view
, text
);
5435 branch_select(struct view
*view
, struct line
*line
)
5437 struct branch
*branch
= line
->data
;
5439 if (branch_is_all(branch
)) {
5440 string_copy(view
->ref
, BRANCH_ALL_NAME
);
5443 string_copy_rev(view
->ref
, branch
->ref
->id
);
5444 string_copy_rev(ref_commit
, branch
->ref
->id
);
5445 string_copy_rev(ref_head
, branch
->ref
->id
);
5446 string_copy_rev(ref_branch
, branch
->ref
->name
);
5449 static struct view_ops branch_ops
= {
5453 sizeof(struct branch_state
),
5470 char rev
[SIZEOF_REV
];
5471 char name
[SIZEOF_STR
];
5475 char rev
[SIZEOF_REV
];
5476 char name
[SIZEOF_STR
];
5480 static char status_onbranch
[SIZEOF_STR
];
5481 static struct status stage_status
;
5482 static enum line_type stage_line_type
;
5484 DEFINE_ALLOCATOR(realloc_ints
, int, 32)
5486 /* This should work even for the "On branch" line. */
5488 status_has_none(struct view
*view
, struct line
*line
)
5490 return view_has_line(view
, line
) && !line
[1].data
;
5493 /* Get fields from the diff line:
5494 * :100644 100644 06a5d6ae9eca55be2e0e585a152e6b1336f2b20e 0000000000000000000000000000000000000000 M
5497 status_get_diff(struct status
*file
, const char *buf
, size_t bufsize
)
5499 const char *old_mode
= buf
+ 1;
5500 const char *new_mode
= buf
+ 8;
5501 const char *old_rev
= buf
+ 15;
5502 const char *new_rev
= buf
+ 56;
5503 const char *status
= buf
+ 97;
5506 old_mode
[-1] != ':' ||
5507 new_mode
[-1] != ' ' ||
5508 old_rev
[-1] != ' ' ||
5509 new_rev
[-1] != ' ' ||
5513 file
->status
= *status
;
5515 string_copy_rev(file
->old
.rev
, old_rev
);
5516 string_copy_rev(file
->new.rev
, new_rev
);
5518 file
->old
.mode
= strtoul(old_mode
, NULL
, 8);
5519 file
->new.mode
= strtoul(new_mode
, NULL
, 8);
5521 file
->old
.name
[0] = file
->new.name
[0] = 0;
5527 status_run(struct view
*view
, const char *argv
[], char status
, enum line_type type
)
5529 struct status
*unmerged
= NULL
;
5533 if (!io_run(&io
, IO_RD
, repo
.cdup
, opt_env
, argv
))
5536 add_line_nodata(view
, type
);
5538 while ((buf
= io_get(&io
, 0, TRUE
))) {
5539 struct status
*file
= unmerged
;
5542 if (!add_line_alloc(view
, &file
, type
, 0, FALSE
))
5546 /* Parse diff info part. */
5548 file
->status
= status
;
5550 string_copy(file
->old
.rev
, NULL_ID
);
5552 } else if (!file
->status
|| file
== unmerged
) {
5553 if (!status_get_diff(file
, buf
, strlen(buf
)))
5556 buf
= io_get(&io
, 0, TRUE
);
5560 /* Collapse all modified entries that follow an
5561 * associated unmerged entry. */
5562 if (unmerged
== file
) {
5563 unmerged
->status
= 'U';
5565 } else if (file
->status
== 'U') {
5570 /* Grab the old name for rename/copy. */
5571 if (!*file
->old
.name
&&
5572 (file
->status
== 'R' || file
->status
== 'C')) {
5573 string_ncopy(file
->old
.name
, buf
, strlen(buf
));
5575 buf
= io_get(&io
, 0, TRUE
);
5580 /* git-ls-files just delivers a NUL separated list of
5581 * file names similar to the second half of the
5582 * git-diff-* output. */
5583 string_ncopy(file
->new.name
, buf
, strlen(buf
));
5584 if (!*file
->old
.name
)
5585 string_copy(file
->old
.name
, file
->new.name
);
5589 if (io_error(&io
)) {
5595 if (!view
->line
[view
->lines
- 1].data
)
5596 add_line_nodata(view
, LINE_STAT_NONE
);
5602 static const char *status_diff_index_argv
[] = { GIT_DIFF_STAGED_FILES("-z") };
5603 static const char *status_diff_files_argv
[] = { GIT_DIFF_UNSTAGED_FILES("-z") };
5605 static const char *status_list_other_argv
[] = {
5606 "git", "ls-files", "-z", "--others", "--exclude-standard", repo
.prefix
, NULL
, NULL
,
5609 static const char *status_list_no_head_argv
[] = {
5610 "git", "ls-files", "-z", "--cached", "--exclude-standard", NULL
5613 static const char *update_index_argv
[] = {
5614 "git", "update-index", "-q", "--unmerged", "--refresh", NULL
5617 /* Restore the previous line number to stay in the context or select a
5618 * line with something that can be updated. */
5620 status_restore(struct view
*view
)
5622 if (!check_position(&view
->prev_pos
))
5625 if (view
->prev_pos
.lineno
>= view
->lines
)
5626 view
->prev_pos
.lineno
= view
->lines
- 1;
5627 while (view
->prev_pos
.lineno
< view
->lines
&& !view
->line
[view
->prev_pos
.lineno
].data
)
5628 view
->prev_pos
.lineno
++;
5629 while (view
->prev_pos
.lineno
> 0 && !view
->line
[view
->prev_pos
.lineno
].data
)
5630 view
->prev_pos
.lineno
--;
5632 /* If the above fails, always skip the "On branch" line. */
5633 if (view
->prev_pos
.lineno
< view
->lines
)
5634 view
->pos
.lineno
= view
->prev_pos
.lineno
;
5636 view
->pos
.lineno
= 1;
5638 if (view
->prev_pos
.offset
> view
->pos
.lineno
)
5639 view
->pos
.offset
= view
->pos
.lineno
;
5640 else if (view
->prev_pos
.offset
< view
->lines
)
5641 view
->pos
.offset
= view
->prev_pos
.offset
;
5643 clear_position(&view
->prev_pos
);
5647 status_update_onbranch(void)
5649 static const char *paths
[][2] = {
5650 { "rebase-apply/rebasing", "Rebasing" },
5651 { "rebase-apply/applying", "Applying mailbox" },
5652 { "rebase-apply/", "Rebasing mailbox" },
5653 { "rebase-merge/interactive", "Interactive rebase" },
5654 { "rebase-merge/", "Rebase merge" },
5655 { "MERGE_HEAD", "Merging" },
5656 { "BISECT_LOG", "Bisecting" },
5657 { "HEAD", "On branch" },
5659 char buf
[SIZEOF_STR
];
5663 if (is_initial_commit()) {
5664 string_copy(status_onbranch
, "Initial commit");
5668 for (i
= 0; i
< ARRAY_SIZE(paths
); i
++) {
5669 char *head
= repo
.head
;
5671 if (!string_format(buf
, "%s/%s", repo
.git_dir
, paths
[i
][0]) ||
5672 lstat(buf
, &stat
) < 0)
5678 if (io_open(&io
, "%s/rebase-merge/head-name", repo
.git_dir
) &&
5679 io_read_buf(&io
, buf
, sizeof(buf
))) {
5681 if (!prefixcmp(head
, "refs/heads/"))
5682 head
+= STRING_SIZE("refs/heads/");
5686 if (!string_format(status_onbranch
, "%s %s", paths
[i
][1], head
))
5687 string_copy(status_onbranch
, repo
.head
);
5691 string_copy(status_onbranch
, "Not currently on any branch");
5694 /* First parse staged info using git-diff-index(1), then parse unstaged
5695 * info using git-diff-files(1), and finally untracked files using
5696 * git-ls-files(1). */
5698 status_open(struct view
*view
, enum open_flags flags
)
5700 const char **staged_argv
= is_initial_commit() ?
5701 status_list_no_head_argv
: status_diff_index_argv
;
5702 char staged_status
= staged_argv
== status_list_no_head_argv
? 'A' : 0;
5704 if (repo
.is_inside_work_tree
== FALSE
) {
5705 report("The status view requires a working tree");
5711 add_line_nodata(view
, LINE_STAT_HEAD
);
5712 status_update_onbranch();
5714 io_run_bg(update_index_argv
);
5716 status_list_other_argv
[ARRAY_SIZE(status_list_other_argv
) - 2] =
5717 opt_untracked_dirs_content
? NULL
: "--directory";
5719 if (!status_run(view
, staged_argv
, staged_status
, LINE_STAT_STAGED
) ||
5720 !status_run(view
, status_diff_files_argv
, 0, LINE_STAT_UNSTAGED
) ||
5721 !status_run(view
, status_list_other_argv
, '?', LINE_STAT_UNTRACKED
)) {
5722 report("Failed to load status data");
5726 /* Restore the exact position or use the specialized restore
5728 status_restore(view
);
5733 status_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
5735 struct status
*status
= line
->data
;
5736 enum line_type type
;
5740 switch (line
->type
) {
5741 case LINE_STAT_STAGED
:
5742 type
= LINE_STAT_SECTION
;
5743 text
= "Changes to be committed:";
5746 case LINE_STAT_UNSTAGED
:
5747 type
= LINE_STAT_SECTION
;
5748 text
= "Changed but not updated:";
5751 case LINE_STAT_UNTRACKED
:
5752 type
= LINE_STAT_SECTION
;
5753 text
= "Untracked files:";
5756 case LINE_STAT_NONE
:
5757 type
= LINE_DEFAULT
;
5758 text
= " (no files)";
5761 case LINE_STAT_HEAD
:
5762 type
= LINE_STAT_HEAD
;
5763 text
= status_onbranch
;
5770 static char buf
[] = { '?', ' ', ' ', ' ', 0 };
5772 buf
[0] = status
->status
;
5773 if (draw_text(view
, line
->type
, buf
))
5775 type
= LINE_DEFAULT
;
5776 text
= status
->new.name
;
5779 draw_text(view
, type
, text
);
5784 status_enter(struct view
*view
, struct line
*line
)
5786 struct status
*status
= line
->data
;
5787 enum open_flags flags
= view_is_displayed(view
) ? OPEN_SPLIT
: OPEN_DEFAULT
;
5789 if (line
->type
== LINE_STAT_NONE
||
5790 (!status
&& line
[1].type
== LINE_STAT_NONE
)) {
5791 report("No file to diff");
5795 switch (line
->type
) {
5796 case LINE_STAT_STAGED
:
5797 case LINE_STAT_UNSTAGED
:
5800 case LINE_STAT_UNTRACKED
:
5802 report("No file to show");
5806 if (!suffixcmp(status
->new.name
, -1, "/")) {
5807 report("Cannot display a directory");
5812 case LINE_STAT_HEAD
:
5816 die("line type %d not handled in switch", line
->type
);
5820 stage_status
= *status
;
5822 memset(&stage_status
, 0, sizeof(stage_status
));
5825 stage_line_type
= line
->type
;
5827 open_view(view
, REQ_VIEW_STAGE
, flags
);
5832 status_exists(struct view
*view
, struct status
*status
, enum line_type type
)
5834 unsigned long lineno
;
5836 for (lineno
= 0; lineno
< view
->lines
; lineno
++) {
5837 struct line
*line
= &view
->line
[lineno
];
5838 struct status
*pos
= line
->data
;
5840 if (line
->type
!= type
)
5842 if (!pos
&& (!status
|| !status
->status
) && line
[1].data
) {
5843 select_view_line(view
, lineno
);
5846 if (pos
&& !strcmp(status
->new.name
, pos
->new.name
)) {
5847 select_view_line(view
, lineno
);
5857 status_update_prepare(struct io
*io
, enum line_type type
)
5859 const char *staged_argv
[] = {
5860 "git", "update-index", "-z", "--index-info", NULL
5862 const char *others_argv
[] = {
5863 "git", "update-index", "-z", "--add", "--remove", "--stdin", NULL
5867 case LINE_STAT_STAGED
:
5868 return io_run(io
, IO_WR
, repo
.cdup
, opt_env
, staged_argv
);
5870 case LINE_STAT_UNSTAGED
:
5871 case LINE_STAT_UNTRACKED
:
5872 return io_run(io
, IO_WR
, repo
.cdup
, opt_env
, others_argv
);
5875 die("line type %d not handled in switch", type
);
5881 status_update_write(struct io
*io
, struct status
*status
, enum line_type type
)
5884 case LINE_STAT_STAGED
:
5885 return io_printf(io
, "%06o %s\t%s%c", status
->old
.mode
,
5886 status
->old
.rev
, status
->old
.name
, 0);
5888 case LINE_STAT_UNSTAGED
:
5889 case LINE_STAT_UNTRACKED
:
5890 return io_printf(io
, "%s%c", status
->new.name
, 0);
5893 die("line type %d not handled in switch", type
);
5899 status_update_file(struct status
*status
, enum line_type type
)
5904 if (!status_update_prepare(&io
, type
))
5907 result
= status_update_write(&io
, status
, type
);
5908 return io_done(&io
) && result
;
5912 status_update_files(struct view
*view
, struct line
*line
)
5914 char buf
[sizeof(view
->ref
)];
5920 int cursor_y
= -1, cursor_x
= -1;
5922 if (!status_update_prepare(&io
, line
->type
))
5925 for (pos
= line
; view_has_line(view
, pos
) && pos
->data
; pos
++)
5928 string_copy(buf
, view
->ref
);
5929 getsyx(cursor_y
, cursor_x
);
5930 for (file
= 0, done
= 5; result
&& file
< files
; line
++, file
++) {
5931 int almost_done
= file
* 100 / files
;
5933 if (almost_done
> done
) {
5935 string_format(view
->ref
, "updating file %u of %u (%d%% done)",
5937 update_view_title(view
);
5938 setsyx(cursor_y
, cursor_x
);
5941 result
= status_update_write(&io
, line
->data
, line
->type
);
5943 string_copy(view
->ref
, buf
);
5945 return io_done(&io
) && result
;
5949 status_update(struct view
*view
)
5951 struct line
*line
= &view
->line
[view
->pos
.lineno
];
5953 assert(view
->lines
);
5956 if (status_has_none(view
, line
)) {
5957 report("Nothing to update");
5961 if (!status_update_files(view
, line
+ 1)) {
5962 report("Failed to update file status");
5966 } else if (!status_update_file(line
->data
, line
->type
)) {
5967 report("Failed to update file status");
5975 status_revert(struct status
*status
, enum line_type type
, bool has_none
)
5977 if (!status
|| type
!= LINE_STAT_UNSTAGED
) {
5978 if (type
== LINE_STAT_STAGED
) {
5979 report("Cannot revert changes to staged files");
5980 } else if (type
== LINE_STAT_UNTRACKED
) {
5981 report("Cannot revert changes to untracked files");
5982 } else if (has_none
) {
5983 report("Nothing to revert");
5985 report("Cannot revert changes to multiple files");
5988 } else if (prompt_yesno("Are you sure you want to revert changes?")) {
5989 char mode
[10] = "100644";
5990 const char *reset_argv
[] = {
5991 "git", "update-index", "--cacheinfo", mode
,
5992 status
->old
.rev
, status
->old
.name
, NULL
5994 const char *checkout_argv
[] = {
5995 "git", "checkout", "--", status
->old
.name
, NULL
5998 if (status
->status
== 'U') {
5999 string_format(mode
, "%5o", status
->old
.mode
);
6001 if (status
->old
.mode
== 0 && status
->new.mode
== 0) {
6002 reset_argv
[2] = "--force-remove";
6003 reset_argv
[3] = status
->old
.name
;
6004 reset_argv
[4] = NULL
;
6007 if (!io_run_fg(reset_argv
, repo
.cdup
))
6009 if (status
->old
.mode
== 0 && status
->new.mode
== 0)
6013 return io_run_fg(checkout_argv
, repo
.cdup
);
6020 status_request(struct view
*view
, enum request request
, struct line
*line
)
6022 struct status
*status
= line
->data
;
6025 case REQ_STATUS_UPDATE
:
6026 if (!status_update(view
))
6030 case REQ_STATUS_REVERT
:
6031 if (!status_revert(status
, line
->type
, status_has_none(view
, line
)))
6035 case REQ_STATUS_MERGE
:
6036 if (!status
|| status
->status
!= 'U') {
6037 report("Merging only possible for files with unmerged status ('U').");
6040 open_mergetool(status
->new.name
);
6046 if (status
->status
== 'D') {
6047 report("File has been deleted.");
6051 open_editor(status
->new.name
, 0);
6054 case REQ_VIEW_BLAME
:
6055 if (line
->type
== LINE_STAT_UNTRACKED
|| !status
) {
6056 report("Nothing to blame here");
6064 /* After returning the status view has been split to
6065 * show the stage view. No further reloading is
6067 return status_enter(view
, line
);
6070 /* Load the current branch information and then the view. */
6084 status_stage_info_(char *buf
, size_t bufsize
,
6085 enum line_type type
, struct status
*status
)
6087 const char *file
= status
? status
->new.name
: "";
6091 case LINE_STAT_STAGED
:
6092 if (status
&& status
->status
)
6093 info
= "Staged changes to %s";
6095 info
= "Staged changes";
6098 case LINE_STAT_UNSTAGED
:
6099 if (status
&& status
->status
)
6100 info
= "Unstaged changes to %s";
6102 info
= "Unstaged changes";
6105 case LINE_STAT_UNTRACKED
:
6106 info
= "Untracked file %s";
6109 case LINE_STAT_HEAD
:
6114 return string_nformat(buf
, bufsize
, NULL
, info
, file
);
6116 #define status_stage_info(buf, type, status) \
6117 status_stage_info_(buf, sizeof(buf), type, status)
6120 status_select(struct view
*view
, struct line
*line
)
6122 struct status
*status
= line
->data
;
6123 char file
[SIZEOF_STR
] = "all files";
6127 if (status
&& !string_format(file
, "'%s'", status
->new.name
))
6130 if (!status
&& line
[1].type
== LINE_STAT_NONE
)
6133 switch (line
->type
) {
6134 case LINE_STAT_STAGED
:
6135 text
= "Press %s to unstage %s for commit";
6138 case LINE_STAT_UNSTAGED
:
6139 text
= "Press %s to stage %s for commit";
6142 case LINE_STAT_UNTRACKED
:
6143 text
= "Press %s to stage %s for addition";
6146 case LINE_STAT_HEAD
:
6147 case LINE_STAT_NONE
:
6148 text
= "Nothing to update";
6152 die("line type %d not handled in switch", line
->type
);
6155 if (status
&& status
->status
== 'U') {
6156 text
= "Press %s to resolve conflict in %s";
6157 key
= get_view_key(view
, REQ_STATUS_MERGE
);
6160 key
= get_view_key(view
, REQ_STATUS_UPDATE
);
6163 string_format(view
->ref
, text
, key
, file
);
6164 status_stage_info(ref_status
, line
->type
, status
);
6166 string_copy(opt_file
, status
->new.name
);
6170 status_grep(struct view
*view
, struct line
*line
)
6172 struct status
*status
= line
->data
;
6175 const char buf
[2] = { status
->status
, 0 };
6176 const char *text
[] = { status
->new.name
, buf
, NULL
};
6178 return grep_text(view
, text
);
6184 static struct view_ops status_ops
= {
6187 VIEW_CUSTOM_STATUS
| VIEW_SEND_CHILD_ENTER
| VIEW_STATUS_LIKE
| VIEW_REFRESH
,
6198 struct stage_state
{
6199 struct diff_state diff
;
6205 stage_diff_write(struct io
*io
, struct line
*line
, struct line
*end
)
6207 while (line
< end
) {
6208 if (!io_write(io
, line
->data
, strlen(line
->data
)) ||
6209 !io_write(io
, "\n", 1))
6212 if (line
->type
== LINE_DIFF_CHUNK
||
6213 line
->type
== LINE_DIFF_HEADER
)
6221 stage_apply_chunk(struct view
*view
, struct line
*chunk
, struct line
*line
, bool revert
)
6223 const char *apply_argv
[SIZEOF_ARG
] = {
6224 "git", "apply", "--whitespace=nowarn", NULL
6226 struct line
*diff_hdr
;
6230 diff_hdr
= find_prev_line_by_type(view
, chunk
, LINE_DIFF_HEADER
);
6235 apply_argv
[argc
++] = "--cached";
6237 apply_argv
[argc
++] = "--unidiff-zero";
6238 if (revert
|| stage_line_type
== LINE_STAT_STAGED
)
6239 apply_argv
[argc
++] = "-R";
6240 apply_argv
[argc
++] = "-";
6241 apply_argv
[argc
++] = NULL
;
6242 if (!io_run(&io
, IO_WR
, repo
.cdup
, opt_env
, apply_argv
))
6246 unsigned long lineno
= 0;
6247 struct line
*context
= chunk
+ 1;
6248 const char *markers
[] = {
6249 line
->type
== LINE_DIFF_DEL
? "" : ",0",
6250 line
->type
== LINE_DIFF_DEL
? ",0" : "",
6253 parse_chunk_lineno(&lineno
, chunk
->data
, line
->type
== LINE_DIFF_DEL
? '+' : '-');
6255 while (context
< line
) {
6256 if (context
->type
== LINE_DIFF_CHUNK
|| context
->type
== LINE_DIFF_HEADER
) {
6258 } else if (context
->type
!= LINE_DIFF_DEL
&& context
->type
!= LINE_DIFF_ADD
) {
6264 if (!stage_diff_write(&io
, diff_hdr
, chunk
) ||
6265 !io_printf(&io
, "@@ -%lu%s +%lu%s @@\n",
6266 lineno
, markers
[0], lineno
, markers
[1]) ||
6267 !stage_diff_write(&io
, line
, line
+ 1)) {
6271 if (!stage_diff_write(&io
, diff_hdr
, chunk
) ||
6272 !stage_diff_write(&io
, chunk
, view
->line
+ view
->lines
))
6278 return chunk
? TRUE
: FALSE
;
6282 stage_update(struct view
*view
, struct line
*line
, bool single
)
6284 struct line
*chunk
= NULL
;
6286 if (!is_initial_commit() && stage_line_type
!= LINE_STAT_UNTRACKED
)
6287 chunk
= find_prev_line_by_type(view
, line
, LINE_DIFF_CHUNK
);
6290 if (!stage_apply_chunk(view
, chunk
, single
? line
: NULL
, FALSE
)) {
6291 report("Failed to apply chunk");
6295 } else if (!stage_status
.status
) {
6296 view
= view
->parent
;
6298 for (line
= view
->line
; view_has_line(view
, line
); line
++)
6299 if (line
->type
== stage_line_type
)
6302 if (!status_update_files(view
, line
+ 1)) {
6303 report("Failed to update files");
6307 } else if (!status_update_file(&stage_status
, stage_line_type
)) {
6308 report("Failed to update file");
6316 stage_revert(struct view
*view
, struct line
*line
)
6318 struct line
*chunk
= NULL
;
6320 if (!is_initial_commit() && stage_line_type
== LINE_STAT_UNSTAGED
)
6321 chunk
= find_prev_line_by_type(view
, line
, LINE_DIFF_CHUNK
);
6324 if (!prompt_yesno("Are you sure you want to revert changes?"))
6327 if (!stage_apply_chunk(view
, chunk
, NULL
, TRUE
)) {
6328 report("Failed to revert chunk");
6334 return status_revert(stage_status
.status
? &stage_status
: NULL
,
6335 stage_line_type
, FALSE
);
6341 stage_next(struct view
*view
, struct line
*line
)
6343 struct stage_state
*state
= view
->private;
6346 if (!state
->chunks
) {
6347 for (line
= view
->line
; view_has_line(view
, line
); line
++) {
6348 if (line
->type
!= LINE_DIFF_CHUNK
)
6351 if (!realloc_ints(&state
->chunk
, state
->chunks
, 1)) {
6352 report("Allocation failure");
6356 state
->chunk
[state
->chunks
++] = line
- view
->line
;
6360 for (i
= 0; i
< state
->chunks
; i
++) {
6361 if (state
->chunk
[i
] > view
->pos
.lineno
) {
6362 do_scroll_view(view
, state
->chunk
[i
] - view
->pos
.lineno
);
6363 report("Chunk %d of %zd", i
+ 1, state
->chunks
);
6368 report("No next chunk found");
6371 static struct line
*
6372 stage_insert_chunk(struct view
*view
, struct chunk_header
*header
,
6373 struct line
*from
, struct line
*to
, struct line
*last_unchanged_line
)
6375 char buf
[SIZEOF_STR
];
6377 unsigned long from_lineno
= last_unchanged_line
- view
->line
;
6378 unsigned long to_lineno
= to
- view
->line
;
6379 unsigned long after_lineno
= to_lineno
;
6381 if (!string_format(buf
, "@@ -%lu,%lu +%lu,%lu @@",
6382 header
->old
.position
, header
->old
.lines
,
6383 header
->new.position
, header
->new.lines
))
6386 chunk_line
= strdup(buf
);
6391 from
->data
= chunk_line
;
6396 if (!add_line_at(view
, after_lineno
++, buf
, LINE_DIFF_CHUNK
, strlen(buf
) + 1, FALSE
))
6399 while (from_lineno
< to_lineno
) {
6400 struct line
*line
= &view
->line
[from_lineno
++];
6402 if (!add_line_at(view
, after_lineno
++, line
->data
, line
->type
, strlen(line
->data
) + 1, FALSE
))
6406 return view
->line
+ after_lineno
;
6410 stage_split_chunk(struct view
*view
, struct line
*chunk_start
)
6412 struct chunk_header header
;
6413 struct line
*last_changed_line
= NULL
, *last_unchanged_line
= NULL
, *pos
;
6416 if (!chunk_start
|| !parse_chunk_header(&header
, chunk_start
->data
)) {
6417 report("Failed to parse chunk header");
6421 header
.old
.lines
= header
.new.lines
= 0;
6423 for (pos
= chunk_start
+ 1; view_has_line(view
, pos
); pos
++) {
6424 const char *chunk_line
= pos
->data
;
6426 if (*chunk_line
== '@' || *chunk_line
== '\\')
6429 if (*chunk_line
== ' ') {
6432 if (last_unchanged_line
< last_changed_line
)
6433 last_unchanged_line
= pos
;
6437 if (last_changed_line
&& last_changed_line
< last_unchanged_line
) {
6438 unsigned long chunk_start_lineno
= pos
- view
->line
;
6439 unsigned long diff
= pos
- last_unchanged_line
;
6441 pos
= stage_insert_chunk(view
, &header
, chunk_start
, pos
, last_unchanged_line
);
6443 header
.old
.position
+= header
.old
.lines
- diff
;
6444 header
.new.position
+= header
.new.lines
- diff
;
6445 header
.old
.lines
= header
.new.lines
= diff
;
6447 chunk_start
= view
->line
+ chunk_start_lineno
;
6448 last_changed_line
= last_unchanged_line
= NULL
;
6452 if (*chunk_line
== '-') {
6454 last_changed_line
= pos
;
6455 } else if (*chunk_line
== '+') {
6457 last_changed_line
= pos
;
6462 stage_insert_chunk(view
, &header
, chunk_start
, NULL
, NULL
);
6464 report("Split the chunk in %d", chunks
+ 1);
6466 report("The chunk cannot be split");
6471 stage_request(struct view
*view
, enum request request
, struct line
*line
)
6474 case REQ_STATUS_UPDATE
:
6475 if (!stage_update(view
, line
, FALSE
))
6479 case REQ_STATUS_REVERT
:
6480 if (!stage_revert(view
, line
))
6484 case REQ_STAGE_UPDATE_LINE
:
6485 if (stage_line_type
== LINE_STAT_UNTRACKED
||
6486 stage_status
.status
== 'A') {
6487 report("Staging single lines is not supported for new files");
6490 if (line
->type
!= LINE_DIFF_DEL
&& line
->type
!= LINE_DIFF_ADD
) {
6491 report("Please select a change to stage");
6494 if (!stage_update(view
, line
, TRUE
))
6498 case REQ_STAGE_NEXT
:
6499 if (stage_line_type
== LINE_STAT_UNTRACKED
) {
6500 report("File is untracked; press %s to add",
6501 get_view_key(view
, REQ_STATUS_UPDATE
));
6504 stage_next(view
, line
);
6507 case REQ_STAGE_SPLIT_CHUNK
:
6508 if (stage_line_type
== LINE_STAT_UNTRACKED
||
6509 !(line
= find_prev_line_by_type(view
, line
, LINE_DIFF_CHUNK
))) {
6510 report("No chunks to split in sight");
6513 stage_split_chunk(view
, line
);
6517 if (!stage_status
.new.name
[0])
6518 return diff_common_edit(view
, request
, line
);
6520 if (stage_status
.status
== 'D') {
6521 report("File has been deleted.");
6525 if (stage_line_type
== LINE_STAT_UNTRACKED
) {
6526 open_editor(stage_status
.new.name
, (line
- view
->line
) + 1);
6528 open_editor(stage_status
.new.name
, diff_get_lineno(view
, line
));
6533 /* Reload everything(including current branch information) ... */
6537 case REQ_VIEW_BLAME
:
6538 if (stage_line_type
== LINE_STAT_UNTRACKED
) {
6539 report("Nothing to blame here");
6543 if (stage_status
.new.name
[0]) {
6544 string_copy(opt_file
, stage_status
.new.name
);
6546 const char *file
= diff_get_pathname(view
, line
);
6549 string_copy(opt_file
, file
);
6553 opt_goto_line
= diff_get_lineno(view
, line
);
6554 if (opt_goto_line
> 0)
6559 return diff_common_enter(view
, request
, line
);
6561 case REQ_DIFF_CONTEXT_UP
:
6562 case REQ_DIFF_CONTEXT_DOWN
:
6563 if (!update_diff_context(request
))
6571 refresh_view(view
->parent
);
6573 /* Check whether the staged entry still exists, and close the
6574 * stage view if it doesn't. */
6575 if (!status_exists(view
->parent
, &stage_status
, stage_line_type
)) {
6576 status_restore(view
->parent
);
6577 return REQ_VIEW_CLOSE
;
6586 stage_open(struct view
*view
, enum open_flags flags
)
6588 static const char *no_head_diff_argv
[] = {
6589 GIT_DIFF_STAGED_INITIAL(encoding_arg
, opt_diff_context_arg
, opt_ignore_space_arg
,
6590 stage_status
.new.name
)
6592 static const char *index_show_argv
[] = {
6593 GIT_DIFF_STAGED(encoding_arg
, opt_diff_context_arg
, opt_ignore_space_arg
,
6594 stage_status
.old
.name
, stage_status
.new.name
)
6596 static const char *files_show_argv
[] = {
6597 GIT_DIFF_UNSTAGED(encoding_arg
, opt_diff_context_arg
, opt_ignore_space_arg
,
6598 stage_status
.old
.name
, stage_status
.new.name
)
6600 /* Diffs for unmerged entries are empty when passing the new
6601 * path, so leave out the new path. */
6602 static const char *files_unmerged_argv
[] = {
6603 "git", "diff-files", encoding_arg
, "--root", "--patch-with-stat",
6604 opt_diff_context_arg
, opt_ignore_space_arg
, "--",
6605 stage_status
.old
.name
, NULL
6607 static const char *file_argv
[] = { repo
.cdup
, stage_status
.new.name
, NULL
};
6608 const char **argv
= NULL
;
6610 if (!stage_line_type
) {
6611 report("No stage content, press %s to open the status view and choose file",
6612 get_view_key(view
, REQ_VIEW_STATUS
));
6616 view
->encoding
= NULL
;
6618 switch (stage_line_type
) {
6619 case LINE_STAT_STAGED
:
6620 if (is_initial_commit()) {
6621 argv
= no_head_diff_argv
;
6623 argv
= index_show_argv
;
6627 case LINE_STAT_UNSTAGED
:
6628 if (stage_status
.status
!= 'U')
6629 argv
= files_show_argv
;
6631 argv
= files_unmerged_argv
;
6634 case LINE_STAT_UNTRACKED
:
6636 view
->encoding
= get_path_encoding(stage_status
.old
.name
, default_encoding
);
6639 case LINE_STAT_HEAD
:
6641 die("line type %d not handled in switch", stage_line_type
);
6644 if (!status_stage_info(view
->ref
, stage_line_type
, &stage_status
)
6645 || !argv_copy(&view
->argv
, argv
)) {
6646 report("Failed to open staged view");
6651 view
->dir
= repo
.cdup
;
6652 return begin_update(view
, NULL
, NULL
, flags
);
6656 stage_read(struct view
*view
, char *data
)
6658 struct stage_state
*state
= view
->private;
6660 if (stage_line_type
== LINE_STAT_UNTRACKED
)
6661 return pager_common_read(view
, data
, LINE_DEFAULT
);
6663 if (data
&& diff_common_read(view
, data
, &state
->diff
))
6666 return pager_read(view
, data
);
6669 static struct view_ops stage_ops
= {
6672 VIEW_DIFF_LIKE
| VIEW_REFRESH
,
6673 sizeof(struct stage_state
),
6687 static const enum line_type graph_colors
[] = {
6697 static enum line_type
get_graph_color(struct graph_symbol
*symbol
)
6700 return LINE_GRAPH_COMMIT
;
6701 assert(symbol
->color
< ARRAY_SIZE(graph_colors
));
6702 return graph_colors
[symbol
->color
];
6706 draw_graph_utf8(struct view
*view
, struct graph_symbol
*symbol
, enum line_type color
, bool first
)
6708 const char *chars
= graph_symbol_to_utf8(symbol
);
6710 return draw_text(view
, color
, chars
+ !!first
);
6714 draw_graph_ascii(struct view
*view
, struct graph_symbol
*symbol
, enum line_type color
, bool first
)
6716 const char *chars
= graph_symbol_to_ascii(symbol
);
6718 return draw_text(view
, color
, chars
+ !!first
);
6722 draw_graph_chtype(struct view
*view
, struct graph_symbol
*symbol
, enum line_type color
, bool first
)
6724 const chtype
*chars
= graph_symbol_to_chtype(symbol
);
6726 return draw_graphic(view
, color
, chars
+ !!first
, 2 - !!first
, FALSE
);
6729 typedef bool (*draw_graph_fn
)(struct view
*, struct graph_symbol
*, enum line_type
, bool);
6731 static bool draw_graph(struct view
*view
, struct graph_canvas
*canvas
)
6733 static const draw_graph_fn fns
[] = {
6738 draw_graph_fn fn
= fns
[opt_line_graphics
];
6741 for (i
= 0; i
< canvas
->size
; i
++) {
6742 struct graph_symbol
*symbol
= &canvas
->symbols
[i
];
6743 enum line_type color
= get_graph_color(symbol
);
6745 if (fn(view
, symbol
, color
, i
== 0))
6749 return draw_text(view
, LINE_MAIN_REVGRAPH
, " ");
6756 DEFINE_ALLOCATOR(realloc_reflogs
, char *, 32)
6759 char id
[SIZEOF_REV
]; /* SHA1 ID. */
6760 const struct ident
*author
; /* Author of the commit. */
6761 struct time time
; /* Date from the author ident. */
6762 struct graph_canvas graph
; /* Ancestry chain graphics. */
6763 char title
[1]; /* First line of the commit message. */
6768 struct commit current
;
6772 char reflogmsg
[SIZEOF_STR
/ 2];
6774 bool added_changes_commits
;
6779 main_register_commit(struct view
*view
, struct commit
*commit
, const char *ids
, bool is_boundary
)
6781 struct main_state
*state
= view
->private;
6783 string_copy_rev(commit
->id
, ids
);
6784 if (state
->with_graph
)
6785 graph_add_commit(&state
->graph
, &commit
->graph
, commit
->id
, ids
, is_boundary
);
6788 static struct commit
*
6789 main_add_commit(struct view
*view
, enum line_type type
, struct commit
*template,
6790 const char *title
, bool custom
)
6792 struct main_state
*state
= view
->private;
6793 size_t titlelen
= strlen(title
);
6794 struct commit
*commit
;
6795 char buf
[SIZEOF_STR
/ 2];
6797 /* FIXME: More graceful handling of titles; append "..." to
6798 * shortened titles, etc. */
6799 string_expand(buf
, sizeof(buf
), title
, 1);
6801 titlelen
= strlen(title
);
6803 if (!add_line_alloc(view
, &commit
, type
, titlelen
, custom
))
6806 *commit
= *template;
6807 strncpy(commit
->title
, title
, titlelen
);
6808 state
->graph
.canvas
= &commit
->graph
;
6809 memset(template, 0, sizeof(*template));
6810 state
->reflogmsg
[0] = 0;
6815 main_flush_commit(struct view
*view
, struct commit
*commit
)
6818 main_add_commit(view
, LINE_MAIN_COMMIT
, commit
, "", FALSE
);
6822 main_has_changes(const char *argv
[])
6826 if (!io_run(&io
, IO_BG
, NULL
, opt_env
, argv
, -1))
6829 return io
.status
== 1;
6833 main_add_changes_commit(struct view
*view
, enum line_type type
, const char *parent
, const char *title
)
6835 char ids
[SIZEOF_STR
] = NULL_ID
" ";
6836 struct main_state
*state
= view
->private;
6837 struct commit commit
= {};
6844 string_copy_rev(ids
+ STRING_SIZE(NULL_ID
" "), parent
);
6846 if (!gettimeofday(&now
, &tz
)) {
6847 commit
.time
.tz
= tz
.tz_minuteswest
* 60;
6848 commit
.time
.sec
= now
.tv_sec
- commit
.time
.tz
;
6851 commit
.author
= &unknown_ident
;
6852 main_register_commit(view
, &commit
, ids
, FALSE
);
6853 if (main_add_commit(view
, type
, &commit
, title
, TRUE
) && state
->with_graph
)
6854 graph_render_parents(&state
->graph
);
6858 main_add_changes_commits(struct view
*view
, struct main_state
*state
, const char *parent
)
6860 const char *staged_argv
[] = { GIT_DIFF_STAGED_FILES("--quiet") };
6861 const char *unstaged_argv
[] = { GIT_DIFF_UNSTAGED_FILES("--quiet") };
6862 const char *staged_parent
= NULL_ID
;
6863 const char *unstaged_parent
= parent
;
6865 if (!is_head_commit(parent
))
6868 state
->added_changes_commits
= TRUE
;
6870 io_run_bg(update_index_argv
);
6872 if (!main_has_changes(unstaged_argv
)) {
6873 unstaged_parent
= NULL
;
6874 staged_parent
= parent
;
6877 if (!main_has_changes(staged_argv
)) {
6878 staged_parent
= NULL
;
6881 main_add_changes_commit(view
, LINE_STAT_STAGED
, staged_parent
, "Staged changes");
6882 main_add_changes_commit(view
, LINE_STAT_UNSTAGED
, unstaged_parent
, "Unstaged changes");
6886 main_open(struct view
*view
, enum open_flags flags
)
6888 static const char *main_argv
[] = {
6889 GIT_MAIN_LOG(encoding_arg
, "%(cmdlineargs)", "%(revargs)", "%(fileargs)")
6891 struct main_state
*state
= view
->private;
6893 state
->with_graph
= opt_rev_graph
&&
6894 opt_commit_order
!= COMMIT_ORDER_REVERSE
;
6896 if (flags
& OPEN_PAGER_MODE
) {
6897 state
->added_changes_commits
= TRUE
;
6898 state
->with_graph
= FALSE
;
6901 return begin_update(view
, NULL
, main_argv
, flags
);
6905 main_done(struct view
*view
)
6907 struct main_state
*state
= view
->private;
6910 for (i
= 0; i
< view
->lines
; i
++) {
6911 struct commit
*commit
= view
->line
[i
].data
;
6913 free(commit
->graph
.symbols
);
6916 for (i
= 0; i
< state
->reflogs
; i
++)
6917 free(state
->reflog
[i
]);
6918 free(state
->reflog
);
6921 #define MAIN_NO_COMMIT_REFS 1
6922 #define main_check_commit_refs(line) !((line)->user_flags & MAIN_NO_COMMIT_REFS)
6923 #define main_mark_no_commit_refs(line) ((line)->user_flags |= MAIN_NO_COMMIT_REFS)
6925 static inline struct ref_list
*
6926 main_get_commit_refs(struct line
*line
, struct commit
*commit
)
6928 struct ref_list
*refs
= NULL
;
6930 if (main_check_commit_refs(line
) && !(refs
= get_ref_list(commit
->id
)))
6931 main_mark_no_commit_refs(line
);
6937 main_draw(struct view
*view
, struct line
*line
, unsigned int lineno
)
6939 struct main_state
*state
= view
->private;
6940 struct commit
*commit
= line
->data
;
6941 struct ref_list
*refs
= NULL
;
6943 if (!commit
->author
)
6946 if (draw_lineno(view
, lineno
))
6950 if (state
->reflogs
) {
6951 const char *id
= state
->reflog
[line
->lineno
- 1];
6953 if (draw_id_custom(view
, LINE_ID
, id
, state
->reflog_width
))
6955 } else if (draw_id(view
, commit
->id
)) {
6960 if (draw_date(view
, &commit
->time
))
6963 if (draw_author(view
, commit
->author
))
6966 if (state
->with_graph
&& draw_graph(view
, &commit
->graph
))
6969 if ((refs
= main_get_commit_refs(line
, commit
)) && draw_refs(view
, refs
))
6973 draw_commit_title(view
, commit
->title
, 0);
6978 main_add_reflog(struct view
*view
, struct main_state
*state
, char *reflog
)
6980 char *end
= strchr(reflog
, ' ');
6987 if (!realloc_reflogs(&state
->reflog
, state
->reflogs
, 1)
6988 || !(reflog
= strdup(reflog
)))
6991 state
->reflog
[state
->reflogs
++] = reflog
;
6992 id_width
= strlen(reflog
);
6993 if (state
->reflog_width
< id_width
) {
6994 state
->reflog_width
= id_width
;
6996 view
->force_redraw
= TRUE
;
7002 /* Reads git log --pretty=raw output and parses it into the commit struct. */
7004 main_read(struct view
*view
, char *line
)
7006 struct main_state
*state
= view
->private;
7007 struct graph
*graph
= &state
->graph
;
7008 enum line_type type
;
7009 struct commit
*commit
= &state
->current
;
7012 main_flush_commit(view
, commit
);
7014 if (!view
->lines
&& !view
->prev
)
7015 die("No revisions match the given arguments.");
7016 if (view
->lines
> 0) {
7017 struct commit
*last
= view
->line
[view
->lines
- 1].data
;
7019 view
->line
[view
->lines
- 1].dirty
= 1;
7020 if (!last
->author
) {
7026 if (state
->with_graph
)
7031 type
= get_line_type(line
);
7032 if (type
== LINE_COMMIT
) {
7035 state
->in_header
= TRUE
;
7036 line
+= STRING_SIZE("commit ");
7037 is_boundary
= *line
== '-';
7038 while (*line
&& !isalnum(*line
))
7041 if (!state
->added_changes_commits
&& opt_show_changes
&& repo
.is_inside_work_tree
)
7042 main_add_changes_commits(view
, state
, line
);
7044 main_flush_commit(view
, commit
);
7046 main_register_commit(view
, &state
->current
, line
, is_boundary
);
7053 /* Empty line separates the commit header from the log itself. */
7055 state
->in_header
= FALSE
;
7058 case LINE_PP_REFLOG
:
7059 if (!main_add_reflog(view
, state
, line
+ STRING_SIZE("Reflog: ")))
7063 case LINE_PP_REFLOGMSG
:
7064 line
+= STRING_SIZE("Reflog message: ");
7065 string_ncopy(state
->reflogmsg
, line
, strlen(line
));
7069 if (state
->with_graph
&& !graph
->has_parents
)
7070 graph_add_parent(graph
, line
+ STRING_SIZE("parent "));
7074 parse_author_line(line
+ STRING_SIZE("author "),
7075 &commit
->author
, &commit
->time
);
7076 if (state
->with_graph
)
7077 graph_render_parents(graph
);
7081 /* Fill in the commit title if it has not already been set. */
7085 /* Skip lines in the commit header. */
7086 if (state
->in_header
)
7089 /* Require titles to start with a non-space character at the
7090 * offset used by git log. */
7091 if (strncmp(line
, " ", 4))
7094 /* Well, if the title starts with a whitespace character,
7095 * try to be forgiving. Otherwise we end up with no title. */
7096 while (isspace(*line
))
7100 if (*state
->reflogmsg
)
7101 line
= state
->reflogmsg
;
7102 main_add_commit(view
, LINE_MAIN_COMMIT
, commit
, line
, FALSE
);
7109 main_request(struct view
*view
, enum request request
, struct line
*line
)
7111 enum open_flags flags
= (view_is_displayed(view
) && request
!= REQ_VIEW_DIFF
)
7112 ? OPEN_SPLIT
: OPEN_DEFAULT
;
7117 if (view_is_displayed(view
) && display
[0] != view
)
7119 /* Do not pass navigation requests to the branch view
7120 * when the main view is maximized. (GH #38) */
7121 return request
== REQ_NEXT
? REQ_MOVE_DOWN
: REQ_MOVE_UP
;
7125 if (view_is_displayed(view
) && display
[0] != view
)
7126 maximize_view(view
, TRUE
);
7128 if (line
->type
== LINE_STAT_UNSTAGED
7129 || line
->type
== LINE_STAT_STAGED
) {
7130 struct view
*diff
= VIEW(REQ_VIEW_DIFF
);
7131 const char *diff_staged_argv
[] = {
7132 GIT_DIFF_STAGED(encoding_arg
,
7133 opt_diff_context_arg
,
7134 opt_ignore_space_arg
, NULL
, NULL
)
7136 const char *diff_unstaged_argv
[] = {
7137 GIT_DIFF_UNSTAGED(encoding_arg
,
7138 opt_diff_context_arg
,
7139 opt_ignore_space_arg
, NULL
, NULL
)
7141 const char **diff_argv
= line
->type
== LINE_STAT_STAGED
7142 ? diff_staged_argv
: diff_unstaged_argv
;
7144 open_argv(view
, diff
, diff_argv
, NULL
, flags
);
7148 open_view(view
, REQ_VIEW_DIFF
, flags
);
7156 case REQ_JUMP_COMMIT
:
7160 for (lineno
= 0; lineno
< view
->lines
; lineno
++) {
7161 struct commit
*commit
= view
->line
[lineno
].data
;
7163 if (!strncasecmp(commit
->id
, opt_search
, strlen(opt_search
))) {
7164 select_view_line(view
, lineno
);
7170 report("Unable to find commit '%s'", opt_search
);
7181 grep_refs(struct line
*line
, struct commit
*commit
, regex_t
*regex
)
7183 struct ref_list
*list
;
7187 if (!opt_show_refs
|| !(list
= main_get_commit_refs(line
, commit
)))
7190 for (i
= 0; i
< list
->size
; i
++) {
7191 if (!regexec(regex
, list
->refs
[i
]->name
, 1, &pmatch
, 0))
7199 main_grep(struct view
*view
, struct line
*line
)
7201 struct commit
*commit
= line
->data
;
7202 const char *text
[] = {
7205 mkauthor(commit
->author
, opt_author_width
, opt_author
),
7206 mkdate(&commit
->time
, opt_date
),
7210 return grep_text(view
, text
) || grep_refs(line
, commit
, view
->regex
);
7214 main_get_commit_branch(struct line
*line
, struct commit
*commit
)
7216 struct ref_list
*list
= main_get_commit_refs(line
, commit
);
7217 struct ref
*branch
= NULL
;
7220 for (i
= 0; list
&& i
< list
->size
; i
++) {
7221 struct ref
*ref
= list
->refs
[i
];
7223 switch (get_line_type_from_ref(ref
)) {
7224 case LINE_MAIN_HEAD
:
7226 /* Always prefer local branches. */
7238 main_select(struct view
*view
, struct line
*line
)
7240 struct commit
*commit
= line
->data
;
7242 if (line
->type
== LINE_STAT_STAGED
|| line
->type
== LINE_STAT_UNSTAGED
) {
7243 string_ncopy(view
->ref
, commit
->title
, strlen(commit
->title
));
7245 struct ref
*branch
= main_get_commit_branch(line
, commit
);
7248 string_copy_rev(ref_branch
, branch
->name
);
7249 string_copy_rev(view
->ref
, commit
->id
);
7251 string_copy_rev(ref_commit
, commit
->id
);
7254 static struct view_ops main_ops
= {
7257 VIEW_SEND_CHILD_ENTER
| VIEW_FILE_FILTER
| VIEW_LOG_LIKE
| VIEW_REFRESH
,
7258 sizeof(struct main_state
),
7269 stash_open(struct view
*view
, enum open_flags flags
)
7271 static const char *stash_argv
[] = { "git", "stash", "list",
7272 encoding_arg
, "--no-color", "--pretty=raw", NULL
};
7273 struct main_state
*state
= view
->private;
7275 state
->added_changes_commits
= TRUE
;
7276 state
->with_graph
= FALSE
;
7277 return begin_update(view
, NULL
, stash_argv
, flags
| OPEN_RELOAD
);
7281 stash_select(struct view
*view
, struct line
*line
)
7283 main_select(view
, line
);
7284 string_format(ref_stash
, "stash@{%d}", line
->lineno
- 1);
7285 string_copy(view
->ref
, ref_stash
);
7288 static struct view_ops stash_ops
= {
7291 VIEW_SEND_CHILD_ENTER
| VIEW_REFRESH
,
7292 sizeof(struct main_state
),
7305 /* Whether or not the curses interface has been initialized. */
7306 static bool cursed
= FALSE
;
7308 /* Terminal hacks and workarounds. */
7309 static bool use_scroll_redrawwin
;
7310 static bool use_scroll_status_wclear
;
7312 /* The status window is used for polling keystrokes. */
7313 static WINDOW
*status_win
;
7315 /* Reading from the prompt? */
7316 static bool input_mode
= FALSE
;
7318 static bool status_empty
= FALSE
;
7320 /* Update status and title window. */
7322 report(const char *msg
, ...)
7324 struct view
*view
= display
[current_view
];
7330 char buf
[SIZEOF_STR
];
7333 FORMAT_BUFFER(buf
, sizeof(buf
), msg
, retval
, TRUE
);
7337 if (!status_empty
|| *msg
) {
7340 va_start(args
, msg
);
7342 wmove(status_win
, 0, 0);
7343 if (view
->has_scrolled
&& use_scroll_status_wclear
)
7346 vwprintw(status_win
, msg
, args
);
7347 status_empty
= FALSE
;
7349 status_empty
= TRUE
;
7351 wclrtoeol(status_win
);
7352 wnoutrefresh(status_win
);
7357 update_view_title(view
);
7372 die_callback
= done_display
;
7374 /* Initialize the curses library */
7375 if (isatty(STDIN_FILENO
)) {
7376 cursed
= !!initscr();
7379 /* Leave stdin and stdout alone when acting as a pager. */
7380 opt_tty
= fopen("/dev/tty", "r+");
7382 die("Failed to open /dev/tty");
7383 cursed
= !!newterm(NULL
, opt_tty
, opt_tty
);
7387 die("Failed to initialize curses");
7389 nonl(); /* Disable conversion and detect newlines from input. */
7390 cbreak(); /* Take input chars one at a time, no wait for \n */
7391 noecho(); /* Don't echo input */
7392 leaveok(stdscr
, FALSE
);
7397 getmaxyx(stdscr
, y
, x
);
7398 status_win
= newwin(1, x
, y
- 1, 0);
7400 die("Failed to create status window");
7402 /* Enable keyboard mapping */
7403 keypad(status_win
, TRUE
);
7404 wbkgdset(status_win
, get_line_attr(LINE_STATUS
));
7405 #ifdef NCURSES_MOUSE_VERSION
7408 mousemask(ALL_MOUSE_EVENTS
, NULL
);
7413 #if defined(NCURSES_VERSION_PATCH) && (NCURSES_VERSION_PATCH >= 20080119)
7414 set_tabsize(opt_tab_size
);
7416 TABSIZE
= opt_tab_size
;
7419 term
= getenv("XTERM_VERSION") ? NULL
: getenv("COLORTERM");
7420 if (term
&& !strcmp(term
, "gnome-terminal")) {
7421 /* In the gnome-terminal-emulator, the message from
7422 * scrolling up one line when impossible followed by
7423 * scrolling down one line causes corruption of the
7424 * status line. This is fixed by calling wclear. */
7425 use_scroll_status_wclear
= TRUE
;
7426 use_scroll_redrawwin
= FALSE
;
7428 } else if (term
&& !strcmp(term
, "xrvt-xpm")) {
7429 /* No problems with full optimizations in xrvt-(unicode)
7431 use_scroll_status_wclear
= use_scroll_redrawwin
= FALSE
;
7434 /* When scrolling in (u)xterm the last line in the
7435 * scrolling direction will update slowly. */
7436 use_scroll_redrawwin
= TRUE
;
7437 use_scroll_status_wclear
= FALSE
;
7442 get_input(int prompt_position
)
7445 int i
, key
, cursor_y
, cursor_x
;
7447 if (prompt_position
)
7451 bool loading
= FALSE
;
7453 foreach_view (view
, i
) {
7455 if (view_is_displayed(view
) && view
->has_scrolled
&&
7456 use_scroll_redrawwin
)
7457 redrawwin(view
->win
);
7458 view
->has_scrolled
= FALSE
;
7463 /* Update the cursor position. */
7464 if (prompt_position
) {
7465 getbegyx(status_win
, cursor_y
, cursor_x
);
7466 cursor_x
= prompt_position
;
7468 view
= display
[current_view
];
7469 getbegyx(view
->win
, cursor_y
, cursor_x
);
7470 cursor_x
= view
->width
- 1;
7471 cursor_y
+= view
->pos
.lineno
- view
->pos
.offset
;
7473 setsyx(cursor_y
, cursor_x
);
7475 /* Refresh, accept single keystroke of input */
7477 nodelay(status_win
, loading
);
7478 key
= wgetch(status_win
);
7480 /* wgetch() with nodelay() enabled returns ERR when
7481 * there's no input. */
7484 } else if (key
== KEY_RESIZE
) {
7487 getmaxyx(stdscr
, height
, width
);
7489 wresize(status_win
, 1, width
);
7490 mvwin(status_win
, height
- 1, 0);
7491 wnoutrefresh(status_win
);
7493 redraw_display(TRUE
);
7497 if (key
== erasechar())
7498 key
= KEY_BACKSPACE
;
7505 prompt_input(const char *prompt
, input_handler handler
, void *data
)
7507 enum input_status status
= INPUT_OK
;
7508 static char buf
[SIZEOF_STR
];
7513 while (status
== INPUT_OK
|| status
== INPUT_SKIP
) {
7516 mvwprintw(status_win
, 0, 0, "%s%.*s", prompt
, pos
, buf
);
7517 wclrtoeol(status_win
);
7519 key
= get_input(pos
+ 1);
7524 status
= pos
? INPUT_STOP
: INPUT_CANCEL
;
7531 status
= INPUT_CANCEL
;
7535 status
= INPUT_CANCEL
;
7539 if (pos
>= sizeof(buf
)) {
7540 report("Input string too long");
7544 status
= handler(data
, buf
, key
);
7545 if (status
== INPUT_OK
)
7546 buf
[pos
++] = (char) key
;
7550 /* Clear the status window */
7551 status_empty
= FALSE
;
7554 if (status
== INPUT_CANCEL
)
7562 static enum input_status
7563 prompt_yesno_handler(void *data
, char *buf
, int c
)
7565 if (c
== 'y' || c
== 'Y')
7567 if (c
== 'n' || c
== 'N')
7568 return INPUT_CANCEL
;
7573 prompt_yesno(const char *prompt
)
7575 char prompt2
[SIZEOF_STR
];
7577 if (!string_format(prompt2
, "%s [Yy/Nn]", prompt
))
7580 return !!prompt_input(prompt2
, prompt_yesno_handler
, NULL
);
7583 static enum input_status
7584 read_prompt_handler(void *data
, char *buf
, int c
)
7586 return isprint(c
) ? INPUT_OK
: INPUT_SKIP
;
7590 read_prompt(const char *prompt
)
7592 return prompt_input(prompt
, read_prompt_handler
, NULL
);
7595 static bool prompt_menu(const char *prompt
, const struct menu_item
*items
, int *selected
)
7597 enum input_status status
= INPUT_OK
;
7600 while (items
[size
].text
)
7605 while (status
== INPUT_OK
) {
7606 const struct menu_item
*item
= &items
[*selected
];
7610 mvwprintw(status_win
, 0, 0, "%s (%d of %d) ",
7611 prompt
, *selected
+ 1, size
);
7613 wprintw(status_win
, "[%c] ", (char) item
->hotkey
);
7614 wprintw(status_win
, "%s", item
->text
);
7615 wclrtoeol(status_win
);
7617 key
= get_input(COLS
- 1);
7622 status
= INPUT_STOP
;
7627 *selected
= *selected
- 1;
7629 *selected
= size
- 1;
7634 *selected
= (*selected
+ 1) % size
;
7638 status
= INPUT_CANCEL
;
7642 for (i
= 0; items
[i
].text
; i
++)
7643 if (items
[i
].hotkey
== key
) {
7645 status
= INPUT_STOP
;
7651 /* Clear the status window */
7652 status_empty
= FALSE
;
7655 return status
!= INPUT_CANCEL
;
7659 * Repository properties
7664 set_remote_branch(const char *name
, const char *value
, size_t valuelen
)
7666 if (!strcmp(name
, ".remote")) {
7667 string_ncopy(repo
.remote
, value
, valuelen
);
7669 } else if (*repo
.remote
&& !strcmp(name
, ".merge")) {
7670 size_t from
= strlen(repo
.remote
);
7672 if (!prefixcmp(value
, "refs/heads/"))
7673 value
+= STRING_SIZE("refs/heads/");
7675 if (!string_format_from(repo
.remote
, &from
, "/%s", value
))
7681 set_repo_config_option(char *name
, char *value
, enum status_code (*cmd
)(int, const char **))
7683 const char *argv
[SIZEOF_ARG
] = { name
, "=" };
7684 int argc
= 1 + (cmd
== option_set_command
);
7685 enum status_code error
;
7687 if (!argv_from_string(argv
, &argc
, value
))
7688 error
= ERROR_TOO_MANY_OPTION_ARGUMENTS
;
7690 error
= cmd(argc
, argv
);
7692 if (error
!= SUCCESS
)
7693 warn("Option 'tig.%s': %s", name
, get_status_message(error
));
7697 set_work_tree(const char *value
)
7699 char cwd
[SIZEOF_STR
];
7701 if (!getcwd(cwd
, sizeof(cwd
)))
7702 die("Failed to get cwd path: %s", strerror(errno
));
7704 die("Failed to chdir(%s): %s", cwd
, strerror(errno
));
7705 if (chdir(repo
.git_dir
) < 0)
7706 die("Failed to chdir(%s): %s", repo
.git_dir
, strerror(errno
));
7707 if (!getcwd(repo
.git_dir
, sizeof(repo
.git_dir
)))
7708 die("Failed to get git path: %s", strerror(errno
));
7709 if (chdir(value
) < 0)
7710 die("Failed to chdir(%s): %s", value
, strerror(errno
));
7711 if (!getcwd(cwd
, sizeof(cwd
)))
7712 die("Failed to get cwd path: %s", strerror(errno
));
7713 if (setenv("GIT_WORK_TREE", cwd
, TRUE
))
7714 die("Failed to set GIT_WORK_TREE to '%s'", cwd
);
7715 if (setenv("GIT_DIR", repo
.git_dir
, TRUE
))
7716 die("Failed to set GIT_DIR to '%s'", repo
.git_dir
);
7717 repo
.is_inside_work_tree
= TRUE
;
7721 parse_git_color_option(enum line_type type
, char *value
)
7723 struct line_info
*info
= get_line_info(type
);
7724 const char *argv
[SIZEOF_ARG
];
7726 bool first_color
= TRUE
;
7729 if (!argv_from_string(argv
, &argc
, value
))
7732 info
->fg
= COLOR_DEFAULT
;
7733 info
->bg
= COLOR_DEFAULT
;
7736 for (i
= 0; i
< argc
; i
++) {
7739 if (set_attribute(&attr
, argv
[i
])) {
7742 } else if (set_color(&attr
, argv
[i
])) {
7747 first_color
= FALSE
;
7753 set_git_color_option(const char *name
, char *value
)
7755 static const struct enum_map_entry color_option_map
[] = {
7756 ENUM_MAP_ENTRY("branch.current", LINE_MAIN_HEAD
),
7757 ENUM_MAP_ENTRY("branch.local", LINE_MAIN_REF
),
7758 ENUM_MAP_ENTRY("branch.plain", LINE_MAIN_REF
),
7759 ENUM_MAP_ENTRY("branch.remote", LINE_MAIN_REMOTE
),
7761 ENUM_MAP_ENTRY("diff.meta", LINE_DIFF_HEADER
),
7762 ENUM_MAP_ENTRY("diff.meta", LINE_DIFF_INDEX
),
7763 ENUM_MAP_ENTRY("diff.meta", LINE_DIFF_OLDMODE
),
7764 ENUM_MAP_ENTRY("diff.meta", LINE_DIFF_NEWMODE
),
7765 ENUM_MAP_ENTRY("diff.frag", LINE_DIFF_CHUNK
),
7766 ENUM_MAP_ENTRY("diff.old", LINE_DIFF_DEL
),
7767 ENUM_MAP_ENTRY("diff.new", LINE_DIFF_ADD
),
7769 //ENUM_MAP_ENTRY("diff.commit", LINE_DIFF_ADD),
7771 ENUM_MAP_ENTRY("status.branch", LINE_STAT_HEAD
),
7772 //ENUM_MAP_ENTRY("status.nobranch", LINE_STAT_HEAD),
7773 ENUM_MAP_ENTRY("status.added", LINE_STAT_STAGED
),
7774 ENUM_MAP_ENTRY("status.updated", LINE_STAT_STAGED
),
7775 ENUM_MAP_ENTRY("status.changed", LINE_STAT_UNSTAGED
),
7776 ENUM_MAP_ENTRY("status.untracked", LINE_STAT_UNTRACKED
),
7778 int type
= LINE_NONE
;
7780 if (opt_read_git_colors
&& map_enum(&type
, color_option_map
, name
)) {
7781 parse_git_color_option(type
, value
);
7786 set_encoding(struct encoding
**encoding_ref
, const char *arg
, bool priority
)
7788 if (parse_encoding(encoding_ref
, arg
, priority
) == SUCCESS
)
7789 encoding_arg
[0] = 0;
7793 read_repo_config_option(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
7795 if (!strcmp(name
, "i18n.commitencoding"))
7796 set_encoding(&default_encoding
, value
, FALSE
);
7798 else if (!strcmp(name
, "gui.encoding"))
7799 set_encoding(&default_encoding
, value
, TRUE
);
7801 else if (!strcmp(name
, "core.editor"))
7802 string_ncopy(opt_editor
, value
, valuelen
);
7804 else if (!strcmp(name
, "core.worktree"))
7805 set_work_tree(value
);
7807 else if (!strcmp(name
, "core.abbrev"))
7808 parse_id(&opt_id_cols
, value
);
7810 else if (!prefixcmp(name
, "tig.color."))
7811 set_repo_config_option(name
+ 10, value
, option_color_command
);
7813 else if (!prefixcmp(name
, "tig.bind."))
7814 set_repo_config_option(name
+ 9, value
, option_bind_command
);
7816 else if (!prefixcmp(name
, "tig."))
7817 set_repo_config_option(name
+ 4, value
, option_set_command
);
7819 else if (!prefixcmp(name
, "color."))
7820 set_git_color_option(name
+ STRING_SIZE("color."), value
);
7822 else if (*repo
.head
&& !prefixcmp(name
, "branch.") &&
7823 !strncmp(name
+ 7, repo
.head
, strlen(repo
.head
)))
7824 set_remote_branch(name
+ 7 + strlen(repo
.head
), value
, valuelen
);
7830 load_git_config(void)
7832 const char *config_list_argv
[] = { "git", "config", "--list", NULL
};
7834 return io_run_load(config_list_argv
, "=", read_repo_config_option
, NULL
);
7842 static const char usage
[] =
7843 "tig " TIG_VERSION
" (" __DATE__
")\n"
7845 "Usage: tig [options] [revs] [--] [paths]\n"
7846 " or: tig log [options] [revs] [--] [paths]\n"
7847 " or: tig show [options] [revs] [--] [paths]\n"
7848 " or: tig blame [options] [rev] [--] path\n"
7851 " or: tig < [git command output]\n"
7854 " +<number> Select line <number> in the first view\n"
7855 " -v, --version Show version and exit\n"
7856 " -h, --help Show help message and exit";
7858 static void TIG_NORETURN
7862 signal(sig
, SIG_DFL
);
7864 /* XXX: Restore tty modes and let the OS cleanup the rest! */
7871 read_filter_args(char *name
, size_t namelen
, char *value
, size_t valuelen
, void *data
)
7873 const char ***filter_args
= data
;
7875 return argv_append(filter_args
, name
) ? OK
: ERR
;
7879 filter_rev_parse(const char ***args
, const char *arg1
, const char *arg2
, const char *argv
[])
7881 const char *rev_parse_argv
[SIZEOF_ARG
] = { "git", "rev-parse", arg1
, arg2
};
7882 const char **all_argv
= NULL
;
7884 if (!argv_append_array(&all_argv
, rev_parse_argv
) ||
7885 !argv_append_array(&all_argv
, argv
) ||
7886 io_run_load(all_argv
, "\n", read_filter_args
, args
) == ERR
)
7887 die("Failed to split arguments");
7888 argv_free(all_argv
);
7893 is_rev_flag(const char *flag
)
7895 static const char *rev_flags
[] = { GIT_REV_FLAGS
};
7898 for (i
= 0; i
< ARRAY_SIZE(rev_flags
); i
++)
7899 if (!strcmp(flag
, rev_flags
[i
]))
7906 filter_options(const char *argv
[], bool blame
)
7908 const char **flags
= NULL
;
7909 int next
, flags_pos
;
7911 for (next
= flags_pos
= 0; argv
[next
]; next
++) {
7912 const char *flag
= argv
[next
];
7915 if (map_enum(&value
, commit_order_arg_map
, flag
)) {
7916 opt_commit_order
= value
;
7917 update_commit_order_arg();
7921 if (map_enum(&value
, ignore_space_arg_map
, flag
)) {
7922 opt_ignore_space
= value
;
7923 update_ignore_space_arg();
7927 if (!prefixcmp(flag
, "-U")
7928 && parse_int(&value
, flag
+ 2, 0, 999999) == SUCCESS
) {
7929 opt_diff_context
= value
;
7930 update_diff_context_arg(opt_diff_context
);
7934 argv
[flags_pos
++] = flag
;
7937 argv
[flags_pos
] = NULL
;
7939 filter_rev_parse(&opt_file_argv
, "--no-revs", "--no-flags", argv
);
7940 filter_rev_parse(&flags
, "--flags", "--no-revs", argv
);
7943 for (next
= flags_pos
= 0; flags
&& flags
[next
]; next
++) {
7944 const char *flag
= flags
[next
];
7946 if (is_rev_flag(flag
))
7947 argv_append(&opt_rev_argv
, flag
);
7949 flags
[flags_pos
++] = flag
;
7952 flags
[flags_pos
] = NULL
;
7955 opt_blame_argv
= flags
;
7957 opt_cmdline_argv
= flags
;
7960 filter_rev_parse(&opt_rev_argv
, "--symbolic", "--revs-only", argv
);
7964 parse_options(int argc
, const char *argv
[], bool pager_mode
)
7966 enum request request
;
7967 const char *subcommand
;
7968 bool seen_dashdash
= FALSE
;
7969 const char **filter_argv
= NULL
;
7972 request
= pager_mode
? REQ_VIEW_PAGER
: REQ_VIEW_MAIN
;
7977 subcommand
= argv
[1];
7978 if (!strcmp(subcommand
, "status")) {
7979 request
= REQ_VIEW_STATUS
;
7981 } else if (!strcmp(subcommand
, "blame")) {
7982 request
= REQ_VIEW_BLAME
;
7984 } else if (!strcmp(subcommand
, "show")) {
7985 request
= REQ_VIEW_DIFF
;
7987 } else if (!strcmp(subcommand
, "log")) {
7988 request
= REQ_VIEW_LOG
;
7990 } else if (!strcmp(subcommand
, "stash")) {
7991 request
= REQ_VIEW_STASH
;
7997 for (i
= 1 + !!subcommand
; i
< argc
; i
++) {
7998 const char *opt
= argv
[i
];
8000 // stop parsing our options after -- and let rev-parse handle the rest
8001 if (!seen_dashdash
) {
8002 if (!strcmp(opt
, "--")) {
8003 seen_dashdash
= TRUE
;
8006 } else if (!strcmp(opt
, "-v") || !strcmp(opt
, "--version")) {
8007 printf("tig version %s\n", TIG_VERSION
);
8010 } else if (!strcmp(opt
, "-h") || !strcmp(opt
, "--help")) {
8011 printf("%s\n", usage
);
8014 } else if (strlen(opt
) >= 2 && *opt
== '+' && string_isnumber(opt
+ 1)) {
8015 opt_lineno
= atoi(opt
+ 1);
8021 if (!argv_append(&filter_argv
, opt
))
8022 die("command too long");
8026 filter_options(filter_argv
, request
== REQ_VIEW_BLAME
);
8028 /* Finish validating and setting up blame options */
8029 if (request
== REQ_VIEW_BLAME
) {
8030 if (!opt_file_argv
|| opt_file_argv
[1] || (opt_rev_argv
&& opt_rev_argv
[1]))
8031 die("invalid number of options to blame\n\n%s", usage
);
8034 string_ncopy(opt_ref
, opt_rev_argv
[0], strlen(opt_rev_argv
[0]));
8037 string_ncopy(opt_file
, opt_file_argv
[0], strlen(opt_file_argv
[0]));
8044 open_pager_mode(enum request request
)
8046 enum open_flags flags
= OPEN_DEFAULT
;
8048 if (request
== REQ_VIEW_PAGER
) {
8049 /* Detect if the user requested the main view. */
8050 if (argv_contains(opt_rev_argv
, "--stdin")) {
8051 request
= REQ_VIEW_MAIN
;
8052 flags
|= OPEN_FORWARD_STDIN
;
8053 } else if (argv_contains(opt_cmdline_argv
, "--pretty=raw")) {
8054 request
= REQ_VIEW_MAIN
;
8055 flags
|= OPEN_STDIN
;
8057 flags
|= OPEN_STDIN
;
8060 } else if (request
== REQ_VIEW_DIFF
) {
8061 if (argv_contains(opt_rev_argv
, "--stdin"))
8062 flags
|= OPEN_FORWARD_STDIN
;
8065 /* Open the requested view even if the pager mode is enabled so
8066 * the warning message below is displayed correctly. */
8067 open_view(NULL
, request
, flags
);
8069 if (!open_in_pager_mode(flags
)) {
8070 close(STDIN_FILENO
);
8071 report("Ignoring stdin.");
8078 run_prompt_command(struct view
*view
, char *cmd
)
8080 enum request request
;
8082 if (cmd
&& string_isnumber(cmd
)) {
8083 int lineno
= view
->pos
.lineno
+ 1;
8085 if (parse_int(&lineno
, cmd
, 1, view
->lines
+ 1) == SUCCESS
) {
8086 select_view_line(view
, lineno
- 1);
8089 report("Unable to parse '%s' as a line number", cmd
);
8091 } else if (cmd
&& iscommit(cmd
)) {
8092 string_ncopy(opt_search
, cmd
, strlen(cmd
));
8094 request
= view_request(view
, REQ_JUMP_COMMIT
);
8095 if (request
== REQ_JUMP_COMMIT
) {
8096 report("Jumping to commits is not supported by the '%s' view", view
->name
);
8099 } else if (cmd
&& strlen(cmd
) == 1) {
8100 request
= get_keybinding(&view
->ops
->keymap
, cmd
[0]);
8103 } else if (cmd
&& cmd
[0] == '!') {
8104 struct view
*next
= VIEW(REQ_VIEW_PAGER
);
8105 const char *argv
[SIZEOF_ARG
];
8109 /* When running random commands, initially show the
8110 * command in the title. However, it maybe later be
8111 * overwritten if a commit line is selected. */
8112 string_ncopy(next
->ref
, cmd
, strlen(cmd
));
8114 if (!argv_from_string(argv
, &argc
, cmd
)) {
8115 report("Too many arguments");
8116 } else if (!format_argv(view
, &next
->argv
, argv
, FALSE
, TRUE
)) {
8117 report("Argument formatting failed");
8120 open_view(view
, REQ_VIEW_PAGER
, OPEN_PREPARED
);
8124 request
= get_request(cmd
);
8125 if (request
!= REQ_UNKNOWN
)
8128 char *args
= strchr(cmd
, ' ');
8131 if (set_option(cmd
, args
) == SUCCESS
) {
8132 request
= !view
->unrefreshable
? REQ_REFRESH
: REQ_SCREEN_REDRAW
;
8133 if (!strcmp(cmd
, "color"))
8142 #ifdef NCURSES_MOUSE_VERSION
8143 static struct view
*
8144 find_clicked_view(MEVENT
*event
)
8149 foreach_displayed_view (view
, i
) {
8150 int beg_y
= 0, beg_x
= 0;
8152 getbegyx(view
->win
, beg_y
, beg_x
);
8154 if (beg_y
<= event
->y
&& event
->y
< beg_y
+ view
->height
8155 && beg_x
<= event
->x
&& event
->x
< beg_x
+ view
->width
) {
8156 if (i
!= current_view
) {
8167 handle_mouse_event(void)
8172 if (getmouse(&event
) != OK
)
8175 view
= find_clicked_view(&event
);
8179 if (event
.bstate
& BUTTON2_PRESSED
)
8180 return REQ_SCROLL_WHEEL_DOWN
;
8182 if (event
.bstate
& BUTTON4_PRESSED
)
8183 return REQ_SCROLL_WHEEL_UP
;
8185 if (event
.bstate
& BUTTON1_PRESSED
) {
8186 if (event
.y
== view
->pos
.lineno
- view
->pos
.offset
) {
8187 /* Click is on the same line, perform an "ENTER" */
8191 int y
= getbegy(view
->win
);
8192 unsigned long lineno
= (event
.y
- y
) + view
->pos
.offset
;
8194 select_view_line(view
, lineno
);
8195 update_view_title(view
);
8205 main(int argc
, const char *argv
[])
8207 const char *codeset
= ENCODING_UTF8
;
8208 bool pager_mode
= !isatty(STDIN_FILENO
);
8209 enum request request
= parse_options(argc
, argv
, pager_mode
);
8213 signal(SIGINT
, quit
);
8214 signal(SIGQUIT
, quit
);
8215 signal(SIGPIPE
, SIG_IGN
);
8217 if (setlocale(LC_ALL
, "")) {
8218 codeset
= nl_langinfo(CODESET
);
8221 foreach_view(view
, i
) {
8222 add_keymap(&view
->ops
->keymap
);
8225 if (load_repo_info() == ERR
)
8226 die("Failed to load repo info.");
8228 if (load_options() == ERR
)
8229 die("Failed to load user config.");
8231 if (load_git_config() == ERR
)
8232 die("Failed to load repo config.");
8234 /* Require a git repository unless when running in pager mode. */
8235 if (!repo
.git_dir
[0] && request
!= REQ_VIEW_PAGER
)
8236 die("Not a git repository");
8238 if (codeset
&& strcmp(codeset
, ENCODING_UTF8
)) {
8239 char translit
[SIZEOF_STR
];
8241 if (string_format(translit
, "%s%s", codeset
, ICONV_TRANSLIT
))
8242 opt_iconv_out
= iconv_open(translit
, ENCODING_UTF8
);
8244 opt_iconv_out
= iconv_open(codeset
, ENCODING_UTF8
);
8245 if (opt_iconv_out
== ICONV_NONE
)
8246 die("Failed to initialize character set conversion");
8249 if (load_refs(FALSE
) == ERR
)
8250 die("Failed to load refs.");
8255 request
= open_pager_mode(request
);
8257 while (view_driver(display
[current_view
], request
)) {
8258 int key
= get_input(0);
8260 #ifdef NCURSES_MOUSE_VERSION
8261 if (key
== KEY_MOUSE
) {
8262 request
= handle_mouse_event();
8268 key
= get_input(0) + 0x80;
8270 view
= display
[current_view
];
8271 request
= get_keybinding(&view
->ops
->keymap
, key
);
8273 /* Some low-level request handling. This keeps access to
8274 * status_win restricted. */
8277 report("Unknown key, press %s for help",
8278 get_view_key(view
, REQ_VIEW_HELP
));
8282 char *cmd
= read_prompt(":");
8283 request
= run_prompt_command(view
, cmd
);
8287 case REQ_SEARCH_BACK
:
8289 const char *prompt
= request
== REQ_SEARCH
? "/" : "?";
8290 char *search
= read_prompt(prompt
);
8293 string_ncopy(opt_search
, search
, strlen(search
));
8294 else if (*opt_search
)
8295 request
= request
== REQ_SEARCH
?
8312 /* vim: set ts=8 sw=8 noexpandtab: */