prompt: make ':show <id>' use the diff view
[tig] / tig.c
diff --git a/tig.c b/tig.c
index 3bf5a25..1ad1994 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -12,7 +12,7 @@
  */
 
 #ifndef        VERSION
-#define VERSION        "tig-0.3"
+#define VERSION        "tig-0.4.git"
 #endif
 
 #ifndef DEBUG
 #include <unistd.h>
 #include <time.h>
 
+#include <locale.h>
+#include <langinfo.h>
+#include <iconv.h>
+
 #include <curses.h>
 
 #if __GNUC__ >= 3
@@ -57,6 +61,8 @@ static size_t utf8_length(const char *string, size_t max_width, int *coloffset,
 /* This color name can be used to refer to the default term colors. */
 #define COLOR_DEFAULT  (-1)
 
+#define ICONV_NONE     ((iconv_t) -1)
+
 /* The format and size of the date column in the main view. */
 #define DATE_FORMAT    "%Y-%m-%d %H:%M"
 #define DATE_COLS      STRING_SIZE("2006-04-29 14:21 ")
@@ -74,13 +80,13 @@ static size_t utf8_length(const char *string, size_t max_width, int *coloffset,
        "git ls-remote . 2>/dev/null"
 
 #define TIG_DIFF_CMD \
-       "git show --patch-with-stat --find-copies-harder -B -C %s"
+       "git show --root --patch-with-stat --find-copies-harder -B -C %s 2>/dev/null"
 
 #define TIG_LOG_CMD    \
-       "git log --cc --stat -n100 %s"
+       "git log --cc --stat -n100 %s 2>/dev/null"
 
 #define TIG_MAIN_CMD \
-       "git log --topo-order --stat --pretty=raw %s"
+       "git log --topo-order --pretty=raw %s 2>/dev/null"
 
 /* XXX: Needs to be defined to the empty string. */
 #define TIG_HELP_CMD   ""
@@ -178,6 +184,28 @@ string_nformat(char *buf, size_t bufsize, int *bufpos, const char *fmt, ...)
 #define string_format_from(buf, from, fmt, args...) \
        string_nformat(buf, sizeof(buf), from, fmt, args)
 
+static int
+string_enum_compare(const char *str1, const char *str2, int len)
+{
+       size_t i;
+
+#define string_enum_sep(x) ((x) == '-' || (x) == '_' || (x) == '.')
+
+       /* Diff-Header == DIFF_HEADER */
+       for (i = 0; i < len; i++) {
+               if (toupper(str1[i]) == toupper(str2[i]))
+                       continue;
+
+               if (string_enum_sep(str1[i]) &&
+                   string_enum_sep(str2[i]))
+                       continue;
+
+               return str1[i] - str2[i];
+       }
+
+       return 0;
+}
+
 /* Shell quoting
  *
  * NOTE: The following is a slightly modified copy of the git project's shell
@@ -262,7 +290,7 @@ sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src)
        REQ_(SHOW_VERSION,      "Show version information"), \
        REQ_(STOP_LOADING,      "Stop all loading views"), \
        REQ_(TOGGLE_LINENO,     "Toggle line numbers"), \
-       REQ_(TOGGLE_REV_GRAPH,  "Toggle revision graph visualization"),
+       REQ_(TOGGLE_REV_GRAPH,  "Toggle revision graph visualization")
 
 
 /* User action requests. */
@@ -272,7 +300,8 @@ enum request {
 
        /* Offset all requests to avoid conflicts with ncurses getch values. */
        REQ_OFFSET = KEY_MAX + 1,
-       REQ_INFO
+       REQ_INFO,
+       REQ_UNKNOWN,
 
 #undef REQ_GROUP
 #undef REQ_
@@ -280,17 +309,34 @@ enum request {
 
 struct request_info {
        enum request request;
+       char *name;
+       int namelen;
        char *help;
 };
 
 static struct request_info req_info[] = {
-#define REQ_GROUP(help)        { 0, (help) },
-#define REQ_(req, help)        { REQ_##req, (help) }
+#define REQ_GROUP(help)        { 0, NULL, 0, (help) },
+#define REQ_(req, help)        { REQ_##req, (#req), STRING_SIZE(#req), (help) }
        REQ_INFO
 #undef REQ_GROUP
 #undef REQ_
 };
 
+static enum request
+get_request(const char *name)
+{
+       int namelen = strlen(name);
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(req_info); i++)
+               if (req_info[i].namelen == namelen &&
+                   !string_enum_compare(req_info[i].name, name, namelen))
+                       return req_info[i].request;
+
+       return REQ_UNKNOWN;
+}
+
+
 /*
  * Options
  */
@@ -321,9 +367,11 @@ static int opt_num_interval        = NUMBER_INTERVAL;
 static int opt_tab_size                = TABSIZE;
 static enum request opt_request = REQ_VIEW_MAIN;
 static char opt_cmd[SIZEOF_CMD]        = "";
-static char opt_encoding[20]   = "";
-static bool opt_utf8           = TRUE;
 static FILE *opt_pipe          = NULL;
+static char opt_encoding[20]   = "UTF-8";
+static bool opt_utf8           = TRUE;
+static char opt_codeset[20]    = "UTF-8";
+static iconv_t opt_iconv       = ICONV_NONE;
 
 enum option_type {
        OPT_NONE,
@@ -552,19 +600,10 @@ static struct line_info *
 get_line_info(char *name, int namelen)
 {
        enum line_type type;
-       int i;
-
-       /* Diff-Header -> DIFF_HEADER */
-       for (i = 0; i < namelen; i++) {
-               if (name[i] == '-')
-                       name[i] = '_';
-               else if (name[i] == '.')
-                       name[i] = '_';
-       }
 
        for (type = 0; type < ARRAY_SIZE(line_info); type++)
                if (namelen == line_info[type].namelen &&
-                   !strncasecmp(line_info[type].name, name, namelen))
+                   !string_enum_compare(line_info[type].name, name, namelen))
                        return &line_info[type];
 
        return NULL;
@@ -603,12 +642,13 @@ struct line {
  * Keys
  */
 
-struct keymap {
+struct keybinding {
        int alias;
-       int request;
+       enum request request;
+       struct keybinding *next;
 };
 
-static struct keymap keymap[] = {
+static struct keybinding default_keybindings[] = {
        /* View switching */
        { 'm',          REQ_VIEW_MAIN },
        { 'd',          REQ_VIEW_DIFF },
@@ -647,7 +687,7 @@ static struct keymap keymap[] = {
        { 'v',          REQ_SHOW_VERSION },
        { 'r',          REQ_SCREEN_REDRAW },
        { 'n',          REQ_TOGGLE_LINENO },
-       { 'g',          REQ_TOGGLE_REV_GRAPH},
+       { 'g',          REQ_TOGGLE_REV_GRAPH },
        { ':',          REQ_PROMPT },
 
        /* wgetch() with nodelay() enabled returns ERR when there's no input. */
@@ -657,18 +697,70 @@ static struct keymap keymap[] = {
        { KEY_RESIZE,   REQ_SCREEN_RESIZE },
 };
 
+#define KEYMAP_INFO \
+       KEYMAP_(GENERIC), \
+       KEYMAP_(MAIN), \
+       KEYMAP_(DIFF), \
+       KEYMAP_(LOG), \
+       KEYMAP_(PAGER), \
+       KEYMAP_(HELP) \
+
+enum keymap {
+#define KEYMAP_(name) KEYMAP_##name
+       KEYMAP_INFO
+#undef KEYMAP_
+};
+
+static struct int_map keymap_table[] = {
+#define KEYMAP_(name) { #name, STRING_SIZE(#name), KEYMAP_##name }
+       KEYMAP_INFO
+#undef KEYMAP_
+};
+
+#define set_keymap(map, name) \
+       set_from_int_map(keymap_table, ARRAY_SIZE(keymap_table), map, name, strlen(name))
+
+static struct keybinding *keybindings[ARRAY_SIZE(keymap_table)];
+
+static void
+add_keybinding(enum keymap keymap, enum request request, int key)
+{
+       struct keybinding *keybinding;
+
+       keybinding = calloc(1, sizeof(*keybinding));
+       if (!keybinding)
+               die("Failed to allocate keybinding");
+
+       keybinding->alias = key;
+       keybinding->request = request;
+       keybinding->next = keybindings[keymap];
+       keybindings[keymap] = keybinding;
+}
+
+/* Looks for a key binding first in the given map, then in the generic map, and
+ * lastly in the default keybindings. */
 static enum request
-get_request(int key)
+get_keybinding(enum keymap keymap, int key)
 {
+       struct keybinding *kbd;
        int i;
 
-       for (i = 0; i < ARRAY_SIZE(keymap); i++)
-               if (keymap[i].alias == key)
-                       return keymap[i].request;
+       for (kbd = keybindings[keymap]; kbd; kbd = kbd->next)
+               if (kbd->alias == key)
+                       return kbd->request;
+
+       for (kbd = keybindings[KEYMAP_GENERIC]; kbd; kbd = kbd->next)
+               if (kbd->alias == key)
+                       return kbd->request;
+
+       for (i = 0; i < ARRAY_SIZE(default_keybindings); i++)
+               if (default_keybindings[i].alias == key)
+                       return default_keybindings[i].request;
 
        return (enum request) key;
 }
 
+
 struct key {
        char *name;
        int value;
@@ -686,6 +778,7 @@ static struct key key_table[] = {
        { "Down",       KEY_DOWN },
        { "Insert",     KEY_IC },
        { "Delete",     KEY_DC },
+       { "Hash",       '#' },
        { "Home",       KEY_HOME },
        { "End",        KEY_END },
        { "PageUp",     KEY_PPAGE },
@@ -704,6 +797,21 @@ static struct key key_table[] = {
        { "F12",        KEY_F(12) },
 };
 
+static int
+get_key_value(const char *name)
+{
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(key_table); i++)
+               if (!strcasecmp(key_table[i].name, name))
+                       return key_table[i].value;
+
+       if (strlen(name) == 1 && isprint(*name))
+               return (int) *name;
+
+       return ERR;
+}
+
 static char *
 get_key(enum request request)
 {
@@ -715,21 +823,22 @@ get_key(enum request request)
 
        buf[pos] = 0;
 
-       for (i = 0; i < ARRAY_SIZE(keymap); i++) {
+       for (i = 0; i < ARRAY_SIZE(default_keybindings); i++) {
+               struct keybinding *keybinding = &default_keybindings[i];
                char *seq = NULL;
                int key;
 
-               if (keymap[i].request != request)
+               if (keybinding->request != request)
                        continue;
 
                for (key = 0; key < ARRAY_SIZE(key_table); key++)
-                       if (key_table[key].value == keymap[i].alias)
+                       if (key_table[key].value == keybinding->alias)
                                seq = key_table[key].name;
 
                if (seq == NULL &&
-                   keymap[i].alias < 127 &&
-                   isprint(keymap[i].alias)) {
-                       key_char[1] = (char) keymap[i].alias;
+                   keybinding->alias < 127 &&
+                   isprint(keybinding->alias)) {
+                       key_char[1] = (char) keybinding->alias;
                        seq = key_char;
                }
 
@@ -800,12 +909,8 @@ option_color_command(int argc, char *argv[])
                return ERR;
        }
 
-       if (set_color(&info->fg, argv[1]) == ERR) {
-               config_msg = "Unknown color";
-               return ERR;
-       }
-
-       if (set_color(&info->bg, argv[2]) == ERR) {
+       if (set_color(&info->fg, argv[1]) == ERR ||
+           set_color(&info->bg, argv[2]) == ERR) {
                config_msg = "Unknown color";
                return ERR;
        }
@@ -849,14 +954,64 @@ option_set_command(int argc, char *argv[])
                return OK;
        }
 
-       if (!strcmp(argv[0], "encoding")) {
-               string_copy(opt_encoding, argv[2]);
-               return OK;
+       if (!strcmp(argv[0], "commit-encoding")) {
+               char *arg = argv[2];
+               int delimiter = *arg;
+               int i;
+
+               switch (delimiter) {
+               case '"':
+               case '\'':
+                       for (arg++, i = 0; arg[i]; i++)
+                               if (arg[i] == delimiter) {
+                                       arg[i] = 0;
+                                       break;
+                               }
+               default:
+                       string_copy(opt_encoding, arg);
+                       return OK;
+               }
        }
 
+       config_msg = "Unknown variable name";
        return ERR;
 }
 
+/* Wants: mode request key */
+static int
+option_bind_command(int argc, char *argv[])
+{
+       enum request request;
+       int keymap;
+       int key;
+
+       if (argc != 3) {
+               config_msg = "Wrong number of arguments given to bind command";
+               return ERR;
+       }
+
+       if (set_keymap(&keymap, argv[0]) == ERR) {
+               config_msg = "Unknown key map";
+               return ERR;
+       }
+
+       key = get_key_value(argv[1]);
+       if (key == ERR) {
+               config_msg = "Unknown key";
+               return ERR;
+       }
+
+       request = get_request(argv[2]);
+       if (request == REQ_UNKNOWN) {
+               config_msg = "Unknown request name";
+               return ERR;
+       }
+
+       add_keybinding(keymap, request, key);
+
+       return OK;
+}
+
 static int
 set_option(char *opt, char *value)
 {
@@ -883,33 +1038,45 @@ set_option(char *opt, char *value)
        if (!strcmp(opt, "set"))
                return option_set_command(argc, argv);
 
+       if (!strcmp(opt, "bind"))
+               return option_bind_command(argc, argv);
+
+       config_msg = "Unknown option command";
        return ERR;
 }
 
 static int
 read_option(char *opt, int optlen, char *value, int valuelen)
 {
+       int status = OK;
+
        config_lineno++;
        config_msg = "Internal error";
 
-       optlen = strcspn(opt, "#;");
-       if (optlen == 0) {
-               /* The whole line is a commend or empty. */
+       /* Check for comment markers, since read_properties() will
+        * only ensure opt and value are split at first " \t". */
+       optlen = strcspn(opt, "#");
+       if (optlen == 0)
                return OK;
 
-       } else if (opt[optlen] != 0) {
-               /* Part of the option name is a comment, so the value part
-                * should be ignored. */
-               valuelen = 0;
-               opt[optlen] = value[valuelen] = 0;
-       } else {
-               /* Else look for comment endings in the value. */
-               valuelen = strcspn(value, "#;");
-               value[valuelen] = 0;
+       if (opt[optlen] != 0) {
+               config_msg = "No option value";
+               status = ERR;
+
+       }  else {
+               /* Look for comment endings in the value. */
+               int len = strcspn(value, "#");
+
+               if (len < valuelen) {
+                       valuelen = len;
+                       value[valuelen] = 0;
+               }
+
+               status = set_option(opt, value);
        }
 
-       if (set_option(opt, value) == ERR) {
-               fprintf(stderr, "Error on line %d, near '%.*s' option: %s\n",
+       if (status == ERR) {
+               fprintf(stderr, "Error on line %d, near '%.*s': %s\n",
                        config_lineno, optlen, opt, config_msg);
                config_errors = TRUE;
        }
@@ -972,6 +1139,8 @@ struct view {
 
        struct view_ops *ops;   /* View operations */
 
+       enum keymap keymap;     /* What keymap does this view have */
+
        char cmd[SIZEOF_CMD];   /* Command buffer */
        char ref[SIZEOF_REF];   /* Hovered commit reference */
        char vid[SIZEOF_REF];   /* View ID. Set to id member when updating. */
@@ -1013,11 +1182,11 @@ struct view_ops {
 static struct view_ops pager_ops;
 static struct view_ops main_ops;
 
-#define VIEW_STR(name, cmd, env, ref, ops) \
-       { name, cmd, #env, ref, ops }
+#define VIEW_STR(name, cmd, env, ref, ops, map) \
+       { name, cmd, #env, ref, ops, map}
 
 #define VIEW_(id, name, ops, ref) \
-       VIEW_STR(name, TIG_##id##_CMD,  TIG_##id##_CMD, ref, ops)
+       VIEW_STR(name, TIG_##id##_CMD,  TIG_##id##_CMD, ref, ops, KEYMAP_##id)
 
 
 static struct view views[] = {
@@ -1455,7 +1624,8 @@ realloc_lines(struct view *view, size_t line_size)
 static bool
 update_view(struct view *view)
 {
-       char buffer[BUFSIZ];
+       char in_buffer[BUFSIZ];
+       char out_buffer[BUFSIZ * 2];
        char *line;
        /* The number of lines to read. If too low it will cause too much
         * redrawing (and possible flickering), if too high responsiveness
@@ -1473,12 +1643,28 @@ update_view(struct view *view)
        if (!realloc_lines(view, view->lines + lines))
                goto alloc_error;
 
-       while ((line = fgets(buffer, sizeof(buffer), view->pipe))) {
-               int linelen = strlen(line);
+       while ((line = fgets(in_buffer, sizeof(in_buffer), view->pipe))) {
+               size_t linelen = strlen(line);
 
                if (linelen)
                        line[linelen - 1] = 0;
 
+               if (opt_iconv != ICONV_NONE) {
+                       char *inbuf = line;
+                       size_t inlen = linelen;
+
+                       char *outbuf = out_buffer;
+                       size_t outlen = sizeof(out_buffer);
+
+                       size_t ret;
+
+                       ret = iconv(opt_iconv, &inbuf, &inlen, &outbuf, &outlen);
+                       if (ret != (size_t) -1) {
+                               line = out_buffer;
+                               linelen = strlen(out_buffer);
+                       }
+               }
+
                if (!view->ops->read(view, line))
                        goto alloc_error;
 
@@ -2125,10 +2311,22 @@ main_read(struct view *view, char *line)
                        break;
 
                if (end) {
+                       char *email = end + 1;
+
                        for (; end > ident && isspace(end[-1]); end--) ;
+
+                       if (end == ident && *email) {
+                               ident = email;
+                               end = strchr(ident, '>');
+                               for (; end > ident && isspace(end[-1]); end--) ;
+                       }
                        *end = 0;
                }
 
+               /* End is NULL or ident meaning there's no author. */
+               if (end <= ident)
+                       ident = "Unknown";
+
                string_copy(commit->author, ident);
 
                /* Parse epoch and timezone */
@@ -2422,6 +2620,8 @@ init_display(void)
                /* Leave stdin and stdout alone when acting as a pager. */
                FILE *io = fopen("/dev/tty", "r+");
 
+               if (!io)
+                       die("Failed to open /dev/tty");
                cursed = !!newterm(NULL, io, io);
        }
 
@@ -2446,6 +2646,71 @@ init_display(void)
        wbkgdset(status_win, get_line_attr(LINE_STATUS));
 }
 
+static int
+read_prompt(void)
+{
+       enum { READING, STOP, CANCEL } status = READING;
+       char buf[sizeof(opt_cmd) - STRING_SIZE("git \0")];
+       int pos = 0;
+
+       while (status == READING) {
+               struct view *view;
+               int i, key;
+
+               foreach_view (view, i)
+                       update_view(view);
+
+               report(":%.*s", pos, buf);
+               /* Refresh, accept single keystroke of input */
+               key = wgetch(status_win);
+               switch (key) {
+               case KEY_RETURN:
+               case KEY_ENTER:
+               case '\n':
+                       status = pos ? STOP : CANCEL;
+                       break;
+
+               case KEY_BACKSPACE:
+                       if (pos > 0)
+                               pos--;
+                       else
+                               status = CANCEL;
+                       break;
+
+               case KEY_ESC:
+                       status = CANCEL;
+                       break;
+
+               case ERR:
+                       break;
+
+               default:
+                       if (pos >= sizeof(buf)) {
+                               report("Input string too long");
+                               return ERR;
+                       }
+
+                       if (isprint(key))
+                               buf[pos++] = (char) key;
+               }
+       }
+
+       if (status == CANCEL) {
+               /* Clear the status window */
+               report("");
+               return ERR;
+       }
+
+       buf[pos++] = 0;
+       if (!string_format(opt_cmd, "git %s", buf))
+               return ERR;
+       if (strncmp(buf, "show", 4) && isspace(buf[4]))
+               opt_request = REQ_VIEW_DIFF;
+       else
+               opt_request = REQ_VIEW_PAGER;
+
+       return OK;
+}
 
 /*
  * Repository references
@@ -2654,6 +2919,10 @@ main(int argc, char *argv[])
 
        signal(SIGINT, quit);
 
+       if (setlocale(LC_ALL, "")) {
+               string_copy(opt_codeset, nl_langinfo(CODESET));
+       }
+
        if (load_options() == ERR)
                die("Failed to load user config.");
 
@@ -2665,6 +2934,12 @@ main(int argc, char *argv[])
        if (!parse_options(argc, argv))
                return 0;
 
+       if (*opt_codeset && strcmp(opt_codeset, opt_encoding)) {
+               opt_iconv = iconv_open(opt_codeset, opt_encoding);
+               if (opt_iconv == (iconv_t) -1)
+                       die("Failed to initialize character set conversion");
+       }
+
        if (load_refs() == ERR)
                die("Failed to load refs.");
 
@@ -2688,29 +2963,15 @@ main(int argc, char *argv[])
 
                /* Refresh, accept single keystroke of input */
                key = wgetch(status_win);
-               request = get_request(key);
+
+               request = get_keybinding(display[current_view]->keymap, key);
 
                /* Some low-level request handling. This keeps access to
                 * status_win restricted. */
                switch (request) {
                case REQ_PROMPT:
-                       report(":");
-                       /* Temporarily switch to line-oriented and echoed
-                        * input. */
-                       nocbreak();
-                       echo();
-
-                       if (wgetnstr(status_win, opt_cmd + 4, sizeof(opt_cmd) - 4) == OK) {
-                               memcpy(opt_cmd, "git ", 4);
-                               opt_request = REQ_VIEW_PAGER;
-                       } else {
-                               report("Prompt interrupted by loading view, "
-                                      "press 'z' to stop loading views");
+                       if (read_prompt() == ERR)
                                request = REQ_SCREEN_UPDATE;
-                       }
-
-                       noecho();
-                       cbreak();
                        break;
 
                case REQ_SCREEN_RESIZE: