Code

Add vi-like ^ bindings
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index 01f48c35766cdcb1541513eef7f48d078cd6f0dc..90c2fc148b724677fe0ef8c7c14008cdc1f90f71 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -114,6 +114,7 @@ static void report(const char *msg, ...);
 #define S_ISGITLINK(mode) (((mode) & S_IFMT) == 0160000)
 
 /* Some ASCII-shorthands fitted into the ncurses namespace. */
+#define KEY_CTL(x)     ((x) & 0x1f) /* KEY_CTL(A) == ^A == \1 */
 #define KEY_TAB                '\t'
 #define KEY_RETURN     '\r'
 #define KEY_ESC                27
@@ -209,7 +210,7 @@ string_ncopy_do(char *dst, size_t dstlen, const char *src, size_t srclen)
 #define string_add(dst, from, src) \
        string_ncopy_do(dst + (from), sizeof(dst) - (from), src, sizeof(src))
 
-static void
+static size_t
 string_expand(char *dst, size_t dstlen, const char *src, int tabsize)
 {
        size_t size, pos;
@@ -228,6 +229,7 @@ string_expand(char *dst, size_t dstlen, const char *src, int tabsize)
        }
 
        dst[size] = 0;
+       return pos;
 }
 
 static char *
@@ -683,15 +685,23 @@ argv_free(const char *argv[])
        argv[0] = NULL;
 }
 
+static size_t
+argv_size(const char **argv)
+{
+       int argc = 0;
+
+       while (argv && argv[argc])
+               argc++;
+
+       return argc;
+}
+
 DEFINE_ALLOCATOR(argv_realloc, const char *, SIZEOF_ARG)
 
 static bool
 argv_append(const char ***argv, const char *arg)
 {
-       int argc = 0;
-
-       while (*argv && (*argv)[argc])
-               argc++;
+       size_t argc = argv_size(*argv);
 
        if (!argv_realloc(argv, argc, 2))
                return FALSE;
@@ -1428,7 +1438,9 @@ static struct keybinding default_keybindings[] = {
        { KEY_TAB,      REQ_VIEW_NEXT },
        { KEY_RETURN,   REQ_ENTER },
        { KEY_UP,       REQ_PREVIOUS },
+       { KEY_CTL('P'), REQ_PREVIOUS },
        { KEY_DOWN,     REQ_NEXT },
+       { KEY_CTL('N'), REQ_NEXT },
        { 'R',          REQ_REFRESH },
        { KEY_F(5),     REQ_REFRESH },
        { 'O',          REQ_MAXIMIZE },
@@ -1441,6 +1453,7 @@ static struct keybinding default_keybindings[] = {
        { KEY_NPAGE,    REQ_MOVE_PAGE_DOWN },
        { ' ',          REQ_MOVE_PAGE_DOWN },
        { KEY_PPAGE,    REQ_MOVE_PAGE_UP },
+       { KEY_CTL('U'), REQ_MOVE_PAGE_UP },
        { 'b',          REQ_MOVE_PAGE_UP },
        { '-',          REQ_MOVE_PAGE_UP },
 
@@ -1448,7 +1461,9 @@ static struct keybinding default_keybindings[] = {
        { KEY_LEFT,     REQ_SCROLL_LEFT },
        { KEY_RIGHT,    REQ_SCROLL_RIGHT },
        { KEY_IC,       REQ_SCROLL_LINE_UP },
+       { KEY_CTL('Y'), REQ_SCROLL_LINE_UP },
        { KEY_DC,       REQ_SCROLL_LINE_DOWN },
+       { KEY_CTL('E'), REQ_SCROLL_LINE_DOWN },
        { 'w',          REQ_SCROLL_PAGE_UP },
        { 's',          REQ_SCROLL_PAGE_DOWN },
 
@@ -1463,6 +1478,7 @@ static struct keybinding default_keybindings[] = {
        { 'z',          REQ_STOP_LOADING },
        { 'v',          REQ_SHOW_VERSION },
        { 'r',          REQ_SCREEN_REDRAW },
+       { KEY_CTL('L'), REQ_SCREEN_REDRAW },
        { 'o',          REQ_OPTIONS },
        { '.',          REQ_TOGGLE_LINENO },
        { 'D',          REQ_TOGGLE_DATE },
@@ -1611,16 +1627,17 @@ get_key_value(const char *name)
                if (!strcasecmp(key_table[i].name, name))
                        return key_table[i].value;
 
+       if (strlen(name) == 2 && name[0] == '^' && isprint(*name))
+               return (int)name[1] & 0x1f;
        if (strlen(name) == 1 && isprint(*name))
                return (int) *name;
-
        return ERR;
 }
 
 static const char *
 get_key_name(int key_value)
 {
-       static char key_char[] = "'X'";
+       static char key_char[] = "'X'\0";
        const char *seq = NULL;
        int key;
 
@@ -1628,10 +1645,17 @@ get_key_name(int key_value)
                if (key_table[key].value == key_value)
                        seq = key_table[key].name;
 
-       if (seq == NULL &&
-           key_value < 127 &&
-           isprint(key_value)) {
-               key_char[1] = (char) key_value;
+       if (seq == NULL && key_value < 0x7f) {
+               char *s = key_char + 1;
+
+               if (key_value >= 0x20) {
+                       *s++ = key_value;
+               } else {
+                       *s++ = '^';
+                       *s++ = 0x40 | (key_value & 0x1f);
+               }
+               *s++ = '\'';
+               *s++ = '\0';
                seq = key_char;
        }
 
@@ -2134,6 +2158,7 @@ load_options(void)
        const char *home = getenv("HOME");
        const char *tigrc_user = getenv("TIGRC_USER");
        const char *tigrc_system = getenv("TIGRC_SYSTEM");
+       const char *tig_diff_opts = getenv("TIG_DIFF_OPTS");
        char buf[SIZEOF_STR];
 
        if (!tigrc_system)
@@ -2151,6 +2176,17 @@ load_options(void)
         * that conflict with keybindings. */
        add_builtin_run_requests();
 
+       if (!opt_diff_args && tig_diff_opts && *tig_diff_opts) {
+               static const char *diff_opts[SIZEOF_ARG] = { NULL };
+               int argc = 0;
+
+               if (!string_format(buf, "%s", tig_diff_opts) ||
+                   !argv_from_string(diff_opts, &argc, buf))
+                       die("TIG_DIFF_OPTS contains too many arguments");
+               else if (!argv_copy(&opt_diff_args, diff_opts))
+                       die("Failed to format TIG_DIFF_OPTS arguments");
+       }
+
        return OK;
 }
 
@@ -2295,7 +2331,7 @@ static struct view views[] = {
        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_(PAGER,  "pager",  &pager_ops,  FALSE, ""),
        VIEW_(STATUS, "status", &status_ops, TRUE,  ""),
        VIEW_(STAGE,  "stage",  &stage_ops,  TRUE,  ""),
 };
@@ -2396,7 +2432,15 @@ draw_space(struct view *view, enum line_type type, int max, int spaces)
 static bool
 draw_text(struct view *view, enum line_type type, const char *string, bool trim)
 {
-       view->col += draw_chars(view, type, string, view->width + view->yoffset - view->col, trim);
+       char text[SIZEOF_STR];
+
+       do {
+               size_t pos = string_expand(text, sizeof(text), string, opt_tab_size);
+
+               view->col += draw_chars(view, type, text, view->width + view->yoffset - view->col, trim);
+               string += pos;
+       } while (*string && view->width + view->yoffset > view->col);
+
        return view->width + view->yoffset <= view->col;
 }
 
@@ -3179,17 +3223,17 @@ format_argv(const char ***dst_argv, const char *src_argv[], bool replace)
                const char *arg = src_argv[argc];
                size_t bufpos = 0;
 
-               if (!strcmp(arg, "%(file-args)")) {
+               if (!strcmp(arg, "%(fileargs)")) {
                        if (!argv_append_array(dst_argv, opt_file_args))
                                break;
                        continue;
 
-               } else if (!strcmp(arg, "%(diff-args)")) {
+               } else if (!strcmp(arg, "%(diffargs)")) {
                        if (!argv_append_array(dst_argv, opt_diff_args))
                                break;
                        continue;
 
-               } else if (!strcmp(arg, "%(rev-args)")) {
+               } else if (!strcmp(arg, "%(revargs)")) {
                        if (!argv_append_array(dst_argv, opt_rev_args))
                                break;
                        continue;
@@ -3679,6 +3723,13 @@ view_driver(struct view *view, enum request request)
                break;
 
        case REQ_VIEW_PAGER:
+               if (view == NULL) {
+                       if (!io_open(&VIEW(REQ_VIEW_PAGER)->io, ""))
+                               die("Failed to open stdin");
+                       open_view(view, request, OPEN_PREPARED);
+                       break;
+               }
+
                if (!VIEW(REQ_VIEW_PAGER)->pipe && !VIEW(REQ_VIEW_PAGER)->lines) {
                        report("No pager content, press %s to run command from prompt",
                               get_key(view->keymap, REQ_PROMPT));
@@ -3977,73 +4028,6 @@ parse_author_line(char *ident, const char **author, struct time *time)
        }
 }
 
-static bool
-open_commit_parent_menu(char buf[SIZEOF_STR], int *parents)
-{
-       char rev[SIZEOF_REV];
-       const char *revlist_argv[] = {
-               "git", "log", "--no-color", "-1", "--pretty=format:%s", rev, NULL
-       };
-       struct menu_item *items;
-       char text[SIZEOF_STR];
-       bool ok = TRUE;
-       int i;
-
-       items = calloc(*parents + 1, sizeof(*items));
-       if (!items)
-               return FALSE;
-
-       for (i = 0; i < *parents; i++) {
-               string_copy_rev(rev, &buf[SIZEOF_REV * i]);
-               if (!io_run_buf(revlist_argv, text, sizeof(text)) ||
-                   !(items[i].text = strdup(text))) {
-                       ok = FALSE;
-                       break;
-               }
-       }
-
-       if (ok) {
-               *parents = 0;
-               ok = prompt_menu("Select parent", items, parents);
-       }
-       for (i = 0; items[i].text; i++)
-               free((char *) items[i].text);
-       free(items);
-       return ok;
-}
-
-static bool
-select_commit_parent(const char *id, char rev[SIZEOF_REV], const char *path)
-{
-       char buf[SIZEOF_STR * 4];
-       const char *revlist_argv[] = {
-               "git", "log", "--no-color", "-1",
-                       "--pretty=format:%P", id, "--", path, NULL
-       };
-       int parents;
-
-       if (!io_run_buf(revlist_argv, buf, sizeof(buf)) ||
-           (parents = strlen(buf) / 40) < 0) {
-               report("Failed to get parent information");
-               return FALSE;
-
-       } else if (parents == 0) {
-               if (path)
-                       report("Path '%s' does not exist in the parent", path);
-               else
-                       report("The selected commit has no parents");
-               return FALSE;
-       }
-
-       if (parents == 1)
-               parents = 0;
-       else if (!open_commit_parent_menu(buf, &parents))
-               return FALSE;
-
-       string_copy_rev(rev, &buf[41 * parents]);
-       return TRUE;
-}
-
 /*
  * Pager backend
  */
@@ -4051,13 +4035,10 @@ select_commit_parent(const char *id, char rev[SIZEOF_REV], const char *path)
 static bool
 pager_draw(struct view *view, struct line *line, unsigned int lineno)
 {
-       char text[SIZEOF_STR];
-
        if (opt_line_number && draw_lineno(view, lineno))
                return TRUE;
 
-       string_expand(text, sizeof(text), line->data, opt_tab_size);
-       draw_text(view, line->type, text, TRUE);
+       draw_text(view, line->type, line->data, TRUE);
        return TRUE;
 }
 
@@ -4232,14 +4213,41 @@ static struct view_ops log_ops = {
 static const char *diff_argv[SIZEOF_ARG] = {
        "git", "show", "--pretty=fuller", "--no-color", "--root",
                "--patch-with-stat", "--find-copies-harder", "-C",
-               "%(diff-args)", "%(commit)", "--", "%(file-args)", NULL
+               "%(diffargs)", "%(commit)", "--", "%(fileargs)", NULL
 };
 
+static bool
+diff_read(struct view *view, char *data)
+{
+       if (!data) {
+               /* Fall back to retry if no diff will be shown. */
+               if (view->lines == 0 && opt_file_args) {
+                       int pos = argv_size(view->argv)
+                               - argv_size(opt_file_args) - 1;
+
+                       if (pos > 0 && !strcmp(view->argv[pos], "--")) {
+                               for (; view->argv[pos]; pos++) {
+                                       free((void *) view->argv[pos]);
+                                       view->argv[pos] = NULL;
+                               }
+
+                               if (view->pipe)
+                                       io_done(view->pipe);
+                               if (io_run(&view->io, IO_RD, view->dir, view->argv))
+                                       return FALSE;
+                       }
+               }
+               return TRUE;
+       }
+
+       return pager_read(view, data);
+}
+
 static struct view_ops diff_ops = {
        "line",
        diff_argv,
        NULL,
-       pager_read,
+       diff_read,
        pager_draw,
        pager_request,
        pager_grep,
@@ -4898,7 +4906,8 @@ struct blame_commit {
        const char *author;             /* Author of the commit. */
        struct time time;               /* Date from the author ident. */
        char filename[128];             /* Name of file. */
-       bool has_previous;              /* Was a "previous" line detected. */
+       char parent_id[SIZEOF_REV];     /* Parent/previous SHA1 ID. */
+       char parent_filename[128];      /* Parent/previous name of file. */
 };
 
 struct blame {
@@ -4911,6 +4920,7 @@ static bool
 blame_open(struct view *view)
 {
        char path[SIZEOF_STR];
+       size_t i;
 
        if (!view->prev && *opt_prefix) {
                string_copy(path, opt_file);
@@ -4928,6 +4938,24 @@ blame_open(struct view *view)
                        return FALSE;
        }
 
+       /* First pass: remove multiple references to the same commit. */
+       for (i = 0; i < view->lines; i++) {
+               struct blame *blame = view->line[i].data;
+
+               if (blame->commit && blame->commit->id[0])
+                       blame->commit->id[0] = 0;
+               else
+                       blame->commit = NULL;
+       }
+
+       /* Second pass: free existing references. */
+       for (i = 0; i < view->lines; i++) {
+               struct blame *blame = view->line[i].data;
+
+               if (blame->commit)
+                       free(blame->commit);
+       }
+
        setup_update(view, opt_file);
        string_format(view->ref, "%s ...", opt_file);
 
@@ -5097,7 +5125,11 @@ blame_read(struct view *view, char *line)
                string_ncopy(commit->title, line, strlen(line));
 
        } else if (match_blame_header("previous ", &line)) {
-               commit->has_previous = TRUE;
+               if (strlen(line) <= SIZEOF_REV)
+                       return FALSE;
+               string_copy_rev(commit->parent_id, line);
+               line += SIZEOF_REV;
+               string_ncopy(commit->parent_filename, line, strlen(line));
 
        } else if (match_blame_header("filename ", &line)) {
                string_ncopy(commit->filename, line, strlen(line));
@@ -5113,7 +5145,6 @@ blame_draw(struct view *view, struct line *line, unsigned int lineno)
        struct blame *blame = line->data;
        struct time *time = NULL;
        const char *id = NULL, *author = NULL;
-       char text[SIZEOF_STR];
 
        if (blame->commit && *blame->commit->filename) {
                id = blame->commit->id;
@@ -5133,8 +5164,7 @@ blame_draw(struct view *view, struct line *line, unsigned int lineno)
        if (draw_lineno(view, lineno))
                return TRUE;
 
-       string_expand(text, sizeof(text), blame->text, opt_tab_size);
-       draw_text(view, LINE_DEFAULT, text, TRUE);
+       draw_text(view, LINE_DEFAULT, blame->text, TRUE);
        return TRUE;
 }
 
@@ -5153,16 +5183,20 @@ check_blame_commit(struct blame *blame, bool check_null_id)
 static void
 setup_blame_parent_line(struct view *view, struct blame *blame)
 {
+       char from[SIZEOF_REF + SIZEOF_STR];
+       char to[SIZEOF_REF + SIZEOF_STR];
        const char *diff_tree_argv[] = {
-               "git", "diff-tree", "-U0", blame->commit->id,
-                       "--", blame->commit->filename, NULL
+               "git", "diff", "--no-textconv", "--no-extdiff", "--no-color",
+                       "-U0", from, to, "--", NULL
        };
        struct io io;
        int parent_lineno = -1;
        int blamed_lineno = -1;
        char *line;
 
-       if (!io_run(&io, IO_RD, NULL, diff_tree_argv))
+       if (!string_format(from, "%s:%s", opt_ref, opt_file) ||
+           !string_format(to, "%s:%s", blame->commit->id, blame->commit->filename) ||
+           !io_run(&io, IO_RD, NULL, diff_tree_argv))
                return;
 
        while ((line = io_get(&io, '\n', TRUE))) {
@@ -5204,10 +5238,13 @@ blame_request(struct view *view, enum request request, struct line *line)
                break;
 
        case REQ_PARENT:
-               if (check_blame_commit(blame, TRUE) &&
-                   select_commit_parent(blame->commit->id, opt_ref,
-                                        blame->commit->filename)) {
-                       string_copy(opt_file, blame->commit->filename);
+               if (!check_blame_commit(blame, TRUE))
+                       break;
+               if (!*blame->commit->parent_id) {
+                       report("The selected commit has no parents");
+               } else {
+                       string_copy_rev(opt_ref, blame->commit->parent_id);
+                       string_copy(opt_file, blame->commit->parent_filename);
                        setup_blame_parent_line(view, blame);
                        open_view(view, REQ_VIEW_BLAME, OPEN_REFRESH);
                }
@@ -5228,7 +5265,7 @@ blame_request(struct view *view, enum request request, struct line *line)
                                        "-C", "-M", "HEAD", "--", view->vid, NULL
                        };
 
-                       if (!blame->commit->has_previous) {
+                       if (!*blame->commit->parent_id) {
                                diff_index_argv[1] = "diff";
                                diff_index_argv[2] = "--no-color";
                                diff_index_argv[6] = "--";
@@ -6727,8 +6764,8 @@ update_rev_graph(struct view *view, struct rev_graph *graph)
 
 static const char *main_argv[SIZEOF_ARG] = {
        "git", "log", "--no-color", "--pretty=raw", "--parents",
-               "--topo-order", "%(diff-args)", "%(rev-args)",
-               "--", "%(file-args)", NULL
+               "--topo-order", "%(diffargs)", "%(revargs)",
+               "--", "%(fileargs)", NULL
 };
 
 static bool
@@ -7086,12 +7123,13 @@ get_input(int prompt_position)
 {
        struct view *view;
        int i, key, cursor_y, cursor_x;
-       bool loading = FALSE;
 
        if (prompt_position)
                input_mode = TRUE;
 
        while (TRUE) {
+               bool loading = FALSE;
+
                foreach_view (view, i) {
                        update_view(view);
                        if (view_is_displayed(view) && view->has_scrolled &&
@@ -7748,10 +7786,8 @@ parse_options(int argc, const char *argv[])
        const char **filter_argv = NULL;
        int i;
 
-       if (!isatty(STDIN_FILENO)) {
-               io_open(&VIEW(REQ_VIEW_PAGER)->io, "");
+       if (!isatty(STDIN_FILENO))
                return REQ_VIEW_PAGER;
-       }
 
        if (argc <= 1)
                return REQ_VIEW_MAIN;