X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=tig.c;h=a31d18eb23bd1213610bc94fe78f09e2836bff8d;hb=9e211ea9ee3c0563ec8310cfd40f311a302701f4;hp=c8ab944dd804192c6f37344fc24d4f19726d7d05;hpb=7332344186ab02b45babce8299162b1b56ad7cc0;p=tig.git diff --git a/tig.c b/tig.c index c8ab944..a31d18e 100644 --- a/tig.c +++ b/tig.c @@ -142,7 +142,6 @@ static int load_refs(void); enum format_flags { FORMAT_ALL, /* Perform replacement in all arguments. */ - FORMAT_DASH, /* Perform replacement up until "--". */ FORMAT_NONE /* No replacement should be performed. */ }; @@ -520,6 +519,7 @@ utf8_length(const char **start, size_t skip, int *width, size_t max_width, int * #define DATE_INFO \ DATE_(NO), \ DATE_(DEFAULT), \ + DATE_(LOCAL), \ DATE_(RELATIVE), \ DATE_(SHORT) @@ -584,7 +584,13 @@ mkdate(const struct time *time, enum date date) } } - gmtime_r(&time->sec, &tm); + if (date == DATE_LOCAL) { + time_t date = time->sec + time->tz; + localtime_r(&date, &tm); + } + else { + gmtime_r(&time->sec, &tm); + } return strftime(buf, sizeof(buf), DATE_FORMAT, &tm) ? buf : NULL; } @@ -1167,7 +1173,8 @@ enum request { #define REQ_(req, help) REQ_##req /* Offset all requests to avoid conflicts with ncurses getch values. */ - REQ_OFFSET = KEY_MAX + 1, + REQ_UNKNOWN = KEY_MAX + 1, + REQ_OFFSET, REQ_INFO #undef REQ_GROUP @@ -1199,7 +1206,7 @@ get_request(const char *name) if (enum_equals(req_info[i], name, namelen)) return req_info[i].request; - return REQ_NONE; + return REQ_UNKNOWN; } @@ -1272,6 +1279,8 @@ LINE(AUTHOR, "author ", COLOR_GREEN, COLOR_DEFAULT, 0), \ LINE(COMMITTER, "committer ", COLOR_MAGENTA, COLOR_DEFAULT, 0), \ LINE(SIGNOFF, " Signed-off-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \ LINE(ACKED, " Acked-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \ +LINE(TESTED, " Tested-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \ +LINE(REVIEWED, " Reviewed-by", COLOR_YELLOW, COLOR_DEFAULT, 0), \ LINE(DEFAULT, "", COLOR_DEFAULT, COLOR_DEFAULT, A_NORMAL), \ LINE(CURSOR, "", COLOR_WHITE, COLOR_GREEN, A_BOLD), \ LINE(STATUS, "", COLOR_GREEN, COLOR_DEFAULT, 0), \ @@ -1405,7 +1414,7 @@ struct keybinding { enum request request; }; -static const struct keybinding default_keybindings[] = { +static struct keybinding default_keybindings[] = { /* View switching */ { 'm', REQ_VIEW_MAIN }, { 'd', REQ_VIEW_DIFF }, @@ -1515,12 +1524,28 @@ static void add_keybinding(enum keymap keymap, enum request request, int key) { struct keybinding_table *table = &keybindings[keymap]; + size_t i; + + for (i = 0; i < keybindings[keymap].size; i++) { + if (keybindings[keymap].data[i].alias == key) { + keybindings[keymap].data[i].request = request; + return; + } + } table->data = realloc(table->data, (table->size + 1) * sizeof(*table->data)); if (!table->data) die("Failed to allocate keybinding"); table->data[table->size].alias = key; table->data[table->size++].request = request; + + if (request == REQ_NONE && keymap == KEYMAP_GENERIC) { + int i; + + for (i = 0; i < ARRAY_SIZE(default_keybindings); i++) + if (default_keybindings[i].alias == key) + default_keybindings[i].request = REQ_NONE; + } } /* Looks for a key binding first in the given map, then in the generic map, and @@ -1747,8 +1772,10 @@ add_builtin_run_requests(void) int i; for (i = 0; i < ARRAY_SIZE(reqs); i++) { - enum request req; + enum request req = get_keybinding(reqs[i].keymap, reqs[i].key); + if (req != reqs[i].key) + continue; req = add_run_request(reqs[i].keymap, reqs[i].key, reqs[i].argc, reqs[i].argv); if (req != REQ_NONE) add_keybinding(reqs[i].keymap, req, reqs[i].key); @@ -1994,7 +2021,7 @@ option_bind_command(int argc, const char *argv[]) return ERR; } - if (set_keymap(&keymap, argv[0]) == ERR) { + if (!set_keymap(&keymap, argv[0])) { config_msg = "Unknown key map"; return ERR; } @@ -2006,7 +2033,7 @@ option_bind_command(int argc, const char *argv[]) } request = get_request(argv[2]); - if (request == REQ_NONE) { + if (request == REQ_UNKNOWN) { static const struct enum_map obsolete[] = { ENUM_MAP("cherry-pick", REQ_NONE), ENUM_MAP("screen-resize", REQ_NONE), @@ -2021,9 +2048,9 @@ option_bind_command(int argc, const char *argv[]) return ERR; } } - if (request == REQ_NONE && *argv[2]++ == '!') + if (request == REQ_UNKNOWN && *argv[2]++ == '!') request = add_run_request(keymap, key, argc - 2, argv + 2); - if (request == REQ_NONE) { + if (request == REQ_UNKNOWN) { config_msg = "Unknown request name"; return ERR; } @@ -2122,8 +2149,6 @@ load_options(void) const char *tigrc_system = getenv("TIGRC_SYSTEM"); char buf[SIZEOF_STR]; - add_builtin_run_requests(); - if (!tigrc_system) tigrc_system = SYSCONFDIR "/tigrc"; load_option_file(tigrc_system); @@ -2135,6 +2160,10 @@ load_options(void) } load_option_file(tigrc_user); + /* Add _after_ loading config files to avoid adding run requests + * that conflict with keybindings. */ + add_builtin_run_requests(); + return OK; } @@ -2161,7 +2190,22 @@ static char ref_commit[SIZEOF_REF] = "HEAD"; static char ref_head[SIZEOF_REF] = "HEAD"; static char ref_branch[SIZEOF_REF] = ""; +enum view_type { + VIEW_MAIN, + VIEW_DIFF, + VIEW_LOG, + VIEW_TREE, + VIEW_BLOB, + VIEW_BLAME, + VIEW_BRANCH, + VIEW_HELP, + VIEW_PAGER, + VIEW_STATUS, + VIEW_STAGE, +}; + struct view { + enum view_type type; /* View type */ const char *name; /* View name */ const char *cmd_env; /* Command line set via environment */ const char *id; /* Points to either of ref_{head,commit,blob} */ @@ -2170,6 +2214,7 @@ struct view { enum keymap keymap; /* What keymap does this view have */ bool git_dir; /* Whether the view requires a git directory. */ + bool refresh; /* Whether the view supports refreshing. */ char ref[SIZEOF_REF]; /* Hovered commit reference */ char vid[SIZEOF_REF]; /* View ID. Set to id member when updating. */ @@ -2194,6 +2239,7 @@ struct view { /* If non-NULL, points to the view that opened this view. If this view * is closed tig will switch back to the parent view. */ struct view *parent; + struct view *prev; /* Buffering */ size_t lines; /* Total number of lines */ @@ -2246,29 +2292,27 @@ static struct view_ops status_ops; static struct view_ops tree_ops; static struct view_ops branch_ops; -#define VIEW_STR(name, env, ref, ops, map, git) \ - { name, #env, ref, ops, map, git } - -#define VIEW_(id, name, ops, git, ref) \ - VIEW_STR(name, TIG_##id##_CMD, ref, ops, KEYMAP_##id, git) +#define VIEW_STR(type, name, env, ref, ops, map, git, refresh) \ + { type, name, #env, ref, ops, map, git, refresh } +#define VIEW_(id, name, ops, git, refresh, ref) \ + VIEW_STR(VIEW_##id, name, TIG_##id##_CMD, ref, ops, KEYMAP_##id, git, refresh) static struct view views[] = { - VIEW_(MAIN, "main", &main_ops, TRUE, ref_head), - VIEW_(DIFF, "diff", &diff_ops, TRUE, ref_commit), - VIEW_(LOG, "log", &log_ops, TRUE, ref_head), - VIEW_(TREE, "tree", &tree_ops, TRUE, ref_commit), - VIEW_(BLOB, "blob", &blob_ops, TRUE, ref_blob), - VIEW_(BLAME, "blame", &blame_ops, TRUE, ref_commit), - VIEW_(BRANCH, "branch", &branch_ops, TRUE, ref_head), - VIEW_(HELP, "help", &help_ops, FALSE, ""), - VIEW_(PAGER, "pager", &pager_ops, FALSE, "stdin"), - VIEW_(STATUS, "status", &status_ops, TRUE, ""), - VIEW_(STAGE, "stage", &stage_ops, TRUE, ""), + VIEW_(MAIN, "main", &main_ops, TRUE, TRUE, ref_head), + VIEW_(DIFF, "diff", &diff_ops, TRUE, FALSE, ref_commit), + VIEW_(LOG, "log", &log_ops, TRUE, TRUE, ref_head), + VIEW_(TREE, "tree", &tree_ops, TRUE, FALSE, ref_commit), + VIEW_(BLOB, "blob", &blob_ops, TRUE, FALSE, ref_blob), + VIEW_(BLAME, "blame", &blame_ops, TRUE, FALSE, ref_commit), + VIEW_(BRANCH, "branch", &branch_ops, TRUE, TRUE, ref_head), + VIEW_(HELP, "help", &help_ops, FALSE, FALSE, ""), + VIEW_(PAGER, "pager", &pager_ops, FALSE, FALSE, "stdin"), + VIEW_(STATUS, "status", &status_ops, TRUE, TRUE, ""), + VIEW_(STAGE, "stage", &stage_ops, TRUE, TRUE, ""), }; #define VIEW(req) (&views[(req) - REQ_OFFSET - 1]) -#define VIEW_REQ(view) ((view) - views + REQ_OFFSET + 1) #define foreach_view(view, i) \ for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++) @@ -2277,6 +2321,10 @@ static struct view views[] = { (view == display[0] || view == display[1]) +/* + * View drawing. + */ + static inline void set_view_attr(struct view *view, enum line_type type) { @@ -2544,7 +2592,7 @@ update_view_title(struct view *view) assert(view_is_displayed(view)); - if (view != VIEW(REQ_VIEW_STATUS) && view->lines) { + if (view->type != VIEW_STATUS && view->lines) { unsigned int view_lines = view->offset + view->height; unsigned int lines = view->lines ? MIN(view_lines, view->lines) * 100 / view->lines @@ -2666,6 +2714,11 @@ redraw_display(bool clear) } } + +/* + * Option management + */ + static void toggle_enum_option_do(unsigned int *opt, const char *help, const struct enum_map *map, size_t size) @@ -3146,8 +3199,6 @@ format_argv(const char *dst_argv[], const char *src_argv[], enum format_flags fl const char *value; if (!next || noreplace) { - if (flags == FORMAT_DASH && !strcmp(arg, "--")) - noreplace = TRUE; len = strlen(arg); value = ""; @@ -3222,12 +3273,11 @@ setup_update(struct view *view, const char *vid) } static bool -prepare_update(struct view *view, const char *argv[], const char *dir, - enum format_flags flags) +prepare_update(struct view *view, const char *argv[], const char *dir) { if (view->pipe) end_update(view, TRUE); - return io_format(&view->io, dir, IO_RD, argv, flags); + return io_format(&view->io, dir, IO_RD, argv, FORMAT_NONE); } static bool @@ -3326,7 +3376,7 @@ update_view(struct view *view) /* Keep the displayed view in sync with line number scaling. */ if (digits != view->digits) { view->digits = digits; - if (opt_line_number || view == VIEW(REQ_VIEW_BLAME)) + if (opt_line_number || view->type == VIEW_BLAME) redraw = TRUE; } } @@ -3336,7 +3386,8 @@ update_view(struct view *view) end_update(view, TRUE); } else if (io_eof(view->pipe)) { - report(""); + if (view_is_displayed(view)) + report(""); end_update(view, FALSE); } @@ -3433,6 +3484,7 @@ open_view(struct view *prev, enum request request, enum open_flags flags) if (split) { display[1] = view; current_view = 1; + view->parent = prev; } else if (!nomaximize) { /* Maximize the current view. */ memset(display, 0, sizeof(display)); @@ -3440,9 +3492,9 @@ open_view(struct view *prev, enum request request, enum open_flags flags) display[current_view] = view; } - /* No parent signals that this is the first loaded view. */ + /* No prev signals that this is the first loaded view. */ if (prev && view != prev) { - view->parent = prev; + view->prev = prev; } /* Resize the view when switching between split- and full-screen, @@ -3563,11 +3615,7 @@ view_driver(struct view *view, enum request request) if (request > REQ_NONE) { open_run_request(request); /* FIXME: When all views can refresh always do this. */ - if (view == VIEW(REQ_VIEW_STATUS) || - view == VIEW(REQ_VIEW_MAIN) || - view == VIEW(REQ_VIEW_LOG) || - view == VIEW(REQ_VIEW_BRANCH) || - view == VIEW(REQ_VIEW_STAGE)) + if (view->refresh) request = REQ_REFRESH; else return TRUE; @@ -3655,16 +3703,7 @@ view_driver(struct view *view, enum request request) case REQ_PREVIOUS: request = request == REQ_NEXT ? REQ_MOVE_DOWN : REQ_MOVE_UP; - if ((view == VIEW(REQ_VIEW_DIFF) && - view->parent == VIEW(REQ_VIEW_MAIN)) || - (view == VIEW(REQ_VIEW_DIFF) && - view->parent == VIEW(REQ_VIEW_BLAME)) || - (view == VIEW(REQ_VIEW_STAGE) && - view->parent == VIEW(REQ_VIEW_STATUS)) || - (view == VIEW(REQ_VIEW_BLOB) && - view->parent == VIEW(REQ_VIEW_TREE)) || - (view == VIEW(REQ_VIEW_MAIN) && - view->parent == VIEW(REQ_VIEW_BRANCH))) { + if (view->parent) { int line; view = view->parent; @@ -3746,8 +3785,7 @@ view_driver(struct view *view, enum request request) break; case REQ_STOP_LOADING: - for (i = 0; i < ARRAY_SIZE(views); i++) { - view = &views[i]; + foreach_view(view, i) { if (view->pipe) report("Stopped loading the %s view", view->name), end_update(view, TRUE); @@ -3771,13 +3809,12 @@ view_driver(struct view *view, enum request request) break; case REQ_VIEW_CLOSE: - /* XXX: Mark closed views by letting view->parent point to the + /* XXX: Mark closed views by letting view->prev point to the * view itself. Parents to closed view should never be * followed. */ - if (view->parent && - view->parent->parent != view->parent) { - maximize_view(view->parent); - view->parent = view; + if (view->prev && view->prev != view) { + maximize_view(view->prev); + view->prev = view; break; } /* Fall-through */ @@ -3885,8 +3922,8 @@ parse_timezone(struct time *time, const char *zone) tz = ('0' - zone[1]) * 60 * 60 * 10; tz += ('0' - zone[2]) * 60 * 60; - tz += ('0' - zone[3]) * 60; - tz += ('0' - zone[4]); + tz += ('0' - zone[3]) * 60 * 10; + tz += ('0' - zone[4]) * 60; if (zone[0] == '-') tz = -tz; @@ -3986,7 +4023,9 @@ select_commit_parent(const char *id, char rev[SIZEOF_REV], const char *path) return FALSE; } - if (parents > 1 && !open_commit_parent_menu(buf, &parents)) + if (parents == 1) + parents = 0; + else if (!open_commit_parent_menu(buf, &parents)) return FALSE; string_copy_rev(rev, &buf[41 * parents]); @@ -4040,7 +4079,7 @@ add_pager_refs(struct view *view, struct line *line) list = get_ref_list(commit_id); if (!list) { - if (view == VIEW(REQ_VIEW_DIFF)) + if (view->type == VIEW_DIFF) goto try_add_describe_ref; return; } @@ -4057,7 +4096,7 @@ add_pager_refs(struct view *view, struct line *line) is_tag = TRUE; } - if (!is_tag && view == VIEW(REQ_VIEW_DIFF)) { + if (!is_tag && view->type == VIEW_DIFF) { try_add_describe_ref: /* Add -g "fake" reference. */ if (!add_describe_ref(buf, &bufpos, commit_id, sep)) @@ -4083,8 +4122,8 @@ pager_read(struct view *view, char *data) return FALSE; if (line->type == LINE_COMMIT && - (view == VIEW(REQ_VIEW_DIFF) || - view == VIEW(REQ_VIEW_LOG))) + (view->type == VIEW_DIFF || + view->type == VIEW_LOG)) add_pager_refs(view, line); return TRUE; @@ -4099,8 +4138,8 @@ pager_request(struct view *view, enum request request, struct line *line) return request; if (line->type == LINE_COMMIT && - (view == VIEW(REQ_VIEW_LOG) || - view == VIEW(REQ_VIEW_PAGER))) { + (view->type == VIEW_LOG || + view->type == VIEW_PAGER)) { open_view(view, REQ_VIEW_DIFF, OPEN_SPLIT); split = 1; } @@ -4133,7 +4172,7 @@ pager_select(struct view *view, struct line *line) if (line->type == LINE_COMMIT) { char *text = (char *)line->data + STRING_SIZE("commit "); - if (view != VIEW(REQ_VIEW_PAGER)) + if (view->type != VIEW_PAGER) string_copy_rev(view->ref, text); string_copy_rev(ref_commit, text); } @@ -4708,7 +4747,7 @@ tree_request(struct view *view, enum request request, struct line *line) break; case LINE_TREE_FILE: - flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT; + flags = view_is_displayed(view) ? OPEN_SPLIT : OPEN_DEFAULT; request = REQ_VIEW_BLOB; break; @@ -4873,7 +4912,7 @@ blame_open(struct view *view) { char path[SIZEOF_STR]; - if (!view->parent && *opt_prefix) { + if (!view->prev && *opt_prefix) { string_copy(path, opt_file); if (!string_format(opt_file, "%s%s", opt_prefix, path)) return FALSE; @@ -4973,7 +5012,7 @@ blame_read_file(struct view *view, const char *line, bool *read_file) const char **argv = *opt_ref ? blame_ref_argv : blame_head_argv; struct io io = {}; - if (view->lines == 0 && !view->parent) + if (view->lines == 0 && !view->prev) die("No blame exist for %s", view->vid); if (view->lines == 0 || !io_run_rd(&io, argv, opt_cdup, FORMAT_ALL)) { @@ -5145,7 +5184,7 @@ setup_blame_parent_line(struct view *view, struct blame *blame) static enum request blame_request(struct view *view, enum request request, struct line *line) { - enum open_flags flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT; + enum open_flags flags = view_is_displayed(view) ? OPEN_SPLIT : OPEN_DEFAULT; struct blame *blame = line->data; switch (request) { @@ -5191,7 +5230,7 @@ blame_request(struct view *view, enum request request, struct line *line) diff_index_argv[7] = "/dev/null"; } - if (!prepare_update(diff, diff_index_argv, NULL, FORMAT_DASH)) { + if (!prepare_update(diff, diff_index_argv, NULL)) { report("Failed to allocate diff command"); break; } @@ -5329,7 +5368,7 @@ branch_request(struct view *view, enum request request, struct line *line) }; struct view *main_view = VIEW(REQ_VIEW_MAIN); - if (!prepare_update(main_view, all_branches_argv, NULL, FORMAT_NONE)) { + if (!prepare_update(main_view, all_branches_argv, NULL)) { report("Failed to load view of all branches"); return REQ_NONE; } @@ -5814,7 +5853,7 @@ status_enter(struct view *view, struct line *line) "--", "/dev/null", newpath, NULL }; - if (!prepare_update(stage, no_head_diff_argv, opt_cdup, FORMAT_DASH)) + if (!prepare_update(stage, no_head_diff_argv, opt_cdup)) return status_load_error(view, stage, newpath); } else { const char *index_show_argv[] = { @@ -5823,7 +5862,7 @@ status_enter(struct view *view, struct line *line) oldpath, newpath, NULL }; - if (!prepare_update(stage, index_show_argv, opt_cdup, FORMAT_DASH)) + if (!prepare_update(stage, index_show_argv, opt_cdup)) return status_load_error(view, stage, newpath); } @@ -5840,7 +5879,7 @@ status_enter(struct view *view, struct line *line) "-C", "-M", "--", oldpath, newpath, NULL }; - if (!prepare_update(stage, files_show_argv, opt_cdup, FORMAT_DASH)) + if (!prepare_update(stage, files_show_argv, opt_cdup)) return status_load_error(view, stage, newpath); if (status) info = "Unstaged changes to %s"; @@ -5871,7 +5910,7 @@ status_enter(struct view *view, struct line *line) die("line type %d not handled in switch", line->type); } - split = view_is_displayed(view) ? OPEN_SPLIT : 0; + split = view_is_displayed(view) ? OPEN_SPLIT : OPEN_DEFAULT; open_view(view, REQ_VIEW_STAGE, OPEN_PREPARED | split); if (view_is_displayed(VIEW(REQ_VIEW_STAGE))) { if (status) { @@ -6751,7 +6790,7 @@ main_read(struct view *view, char *line) if (!line) { int i; - if (!view->lines && !view->parent) + if (!view->lines && !view->prev) die("No revisions match the given arguments."); if (view->lines > 0) { commit = view->line[view->lines - 1].data; @@ -6841,7 +6880,7 @@ main_read(struct view *view, char *line) static enum request main_request(struct view *view, enum request request, struct line *line) { - enum open_flags flags = display[0] == view ? OPEN_SPLIT : OPEN_DEFAULT; + enum open_flags flags = view_is_displayed(view) ? OPEN_SPLIT : OPEN_DEFAULT; switch (request) { case REQ_ENTER: @@ -7738,7 +7777,7 @@ parse_options(int argc, const char *argv[]) die("command too long"); } - if (!prepare_update(VIEW(request), custom_argv, NULL, FORMAT_NONE)) + if (!prepare_update(VIEW(request), custom_argv, NULL)) die("Failed to format arguments"); return request; @@ -7807,6 +7846,10 @@ main(int argc, const char *argv[]) /* Some low-level request handling. This keeps access to * status_win restricted. */ switch (request) { + case REQ_NONE: + report("Unknown key, press %s for help", + get_key(view->keymap, REQ_VIEW_HELP)); + break; case REQ_PROMPT: { char *cmd = read_prompt(":"); @@ -7833,7 +7876,7 @@ main(int argc, const char *argv[]) if (!argv_from_string(argv, &argc, cmd)) { report("Too many arguments"); - } else if (!prepare_update(next, argv, NULL, FORMAT_DASH)) { + } else if (!prepare_update(next, argv, NULL)) { report("Failed to format command"); } else { open_view(view, REQ_VIEW_PAGER, OPEN_PREPARED);