Fix git-describe reference adding when there are no tags and thus no output
[tig] / tig.c
diff --git a/tig.c b/tig.c
index 708213e..bcff66f 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 <sys/types.h>
+#include <regex.h>
+
+#include <locale.h>
+#include <langinfo.h>
+#include <iconv.h>
+
 #include <curses.h>
 
 #if __GNUC__ >= 3
@@ -50,13 +57,15 @@ static size_t utf8_length(const char *string, size_t max_width, int *coloffset,
 #define ARRAY_SIZE(x)  (sizeof(x) / sizeof(x[0]))
 #define STRING_SIZE(x) (sizeof(x) - 1)
 
+#define SIZEOF_STR     1024    /* Default string size. */
 #define SIZEOF_REF     256     /* Size of symbolic or SHA1 ID. */
-#define SIZEOF_CMD     1024    /* Size of command buffer. */
 #define SIZEOF_REVGRAPH        19      /* Size of revision ancestry graphics. */
 
 /* 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 +83,19 @@ 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"
+
+#define TIG_TREE_CMD   \
+       "git ls-tree %s %s"
+
+#define TIG_BLOB_CMD   \
+       "git cat-file blob %s"
 
 /* XXX: Needs to be defined to the empty string. */
 #define TIG_HELP_CMD   ""
@@ -130,16 +145,22 @@ set_from_int_map(struct int_map *map, size_t map_size,
  */
 
 static inline void
-string_ncopy(char *dst, const char *src, int dstlen)
+string_ncopy_do(char *dst, size_t dstlen, const char *src, size_t srclen)
 {
-       strncpy(dst, src, dstlen - 1);
-       dst[dstlen - 1] = 0;
+       if (srclen > dstlen - 1)
+               srclen = dstlen - 1;
 
+       strncpy(dst, src, srclen);
+       dst[srclen] = 0;
 }
 
-/* Shorthand for safely copying into a fixed buffer. */
+/* Shorthands for safely copying into a fixed buffer. */
+
 #define string_copy(dst, src) \
-       string_ncopy(dst, src, sizeof(dst))
+       string_ncopy_do(dst, sizeof(dst), src, sizeof(dst))
+
+#define string_ncopy(dst, src, srclen) \
+       string_ncopy_do(dst, sizeof(dst), src, srclen)
 
 static char *
 chomp_string(char *name)
@@ -157,10 +178,10 @@ chomp_string(char *name)
 }
 
 static bool
-string_nformat(char *buf, size_t bufsize, int *bufpos, const char *fmt, ...)
+string_nformat(char *buf, size_t bufsize, size_t *bufpos, const char *fmt, ...)
 {
        va_list args;
-       int pos = bufpos ? *bufpos : 0;
+       size_t pos = bufpos ? *bufpos : 0;
 
        va_start(args, fmt);
        pos += vsnprintf(buf + pos, bufsize - pos, fmt, args);
@@ -218,11 +239,11 @@ string_enum_compare(const char *str1, const char *str2, int len)
  */
 
 static size_t
-sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src)
+sq_quote(char buf[SIZEOF_STR], size_t bufsize, const char *src)
 {
        char c;
 
-#define BUFPUT(x) do { if (bufsize < SIZEOF_CMD) buf[bufsize++] = (x); } while (0)
+#define BUFPUT(x) do { if (bufsize < SIZEOF_STR) buf[bufsize++] = (x); } while (0)
 
        BUFPUT('\'');
        while ((c = *src++)) {
@@ -251,6 +272,8 @@ sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src)
        REQ_(VIEW_MAIN,         "Show main view"), \
        REQ_(VIEW_DIFF,         "Show diff view"), \
        REQ_(VIEW_LOG,          "Show log view"), \
+       REQ_(VIEW_TREE,         "Show tree view"), \
+       REQ_(VIEW_BLOB,         "Show blob view"), \
        REQ_(VIEW_HELP,         "Show help page"), \
        REQ_(VIEW_PAGER,        "Show pager view"), \
        \
@@ -276,15 +299,21 @@ sq_quote(char buf[SIZEOF_CMD], size_t bufsize, const char *src)
        REQ_(SCROLL_PAGE_UP,    "Scroll one page up"), \
        REQ_(SCROLL_PAGE_DOWN,  "Scroll one page down"), \
        \
+       REQ_GROUP("Searching") \
+       REQ_(SEARCH,            "Search the view"), \
+       REQ_(SEARCH_BACK,       "Search backwards in the view"), \
+       REQ_(FIND_NEXT,         "Find next search match"), \
+       REQ_(FIND_PREV,         "Find previous search match"), \
+       \
        REQ_GROUP("Misc") \
+       REQ_(NONE,              "Do nothing"), \
        REQ_(PROMPT,            "Bring up the prompt"), \
-       REQ_(SCREEN_UPDATE,     "Update the screen"), \
        REQ_(SCREEN_REDRAW,     "Redraw the screen"), \
        REQ_(SCREEN_RESIZE,     "Resize the screen"), \
        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. */
@@ -294,7 +323,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_
@@ -302,17 +332,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
  */
@@ -337,15 +384,19 @@ VERSION " (" __DATE__ ")\n"
 "  -h, --help                  Show help message and exit\n";
 
 /* Option and state variables. */
-static bool opt_line_number    = FALSE;
-static bool opt_rev_graph      = TRUE;
-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 bool opt_line_number            = FALSE;
+static bool opt_rev_graph              = TRUE;
+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_STR]                = "";
+static char opt_path[SIZEOF_STR]       = "";
+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;
+static char opt_search[SIZEOF_STR]     = "";
 
 enum option_type {
        OPT_NONE,
@@ -525,6 +576,8 @@ LINE(MAIN_COMMIT,  "",                      COLOR_DEFAULT,  COLOR_DEFAULT,  0), \
 LINE(MAIN_DELIM,   "",                 COLOR_MAGENTA,  COLOR_DEFAULT,  0), \
 LINE(MAIN_TAG,     "",                 COLOR_MAGENTA,  COLOR_DEFAULT,  A_BOLD), \
 LINE(MAIN_REF,     "",                 COLOR_CYAN,     COLOR_DEFAULT,  A_BOLD), \
+LINE(TREE_DIR,     "",                 COLOR_DEFAULT,  COLOR_DEFAULT,  A_NORMAL), \
+LINE(TREE_FILE,    "",                 COLOR_DEFAULT,  COLOR_DEFAULT,  A_NORMAL)
 
 enum line_type {
 #define LINE(type, line, fg, bg, attr) \
@@ -619,6 +672,7 @@ struct line {
 struct keybinding {
        int alias;
        enum request request;
+       struct keybinding *next;
 };
 
 static struct keybinding default_keybindings[] = {
@@ -626,9 +680,10 @@ static struct keybinding default_keybindings[] = {
        { 'm',          REQ_VIEW_MAIN },
        { 'd',          REQ_VIEW_DIFF },
        { 'l',          REQ_VIEW_LOG },
+       { 't',          REQ_VIEW_TREE },
+       { 'f',          REQ_VIEW_BLOB },
        { 'p',          REQ_VIEW_PAGER },
        { 'h',          REQ_VIEW_HELP },
-       { '?',          REQ_VIEW_HELP },
 
        /* View manipulation */
        { 'q',          REQ_VIEW_CLOSE },
@@ -654,27 +709,86 @@ static struct keybinding default_keybindings[] = {
        { 'w',          REQ_SCROLL_PAGE_UP },
        { 's',          REQ_SCROLL_PAGE_DOWN },
 
+       /* Searching */
+       { '/',          REQ_SEARCH },
+       { '?',          REQ_SEARCH_BACK },
+       { 'n',          REQ_FIND_NEXT },
+       { 'N',          REQ_FIND_PREV },
+
        /* Misc */
        { 'Q',          REQ_QUIT },
        { 'z',          REQ_STOP_LOADING },
        { 'v',          REQ_SHOW_VERSION },
        { 'r',          REQ_SCREEN_REDRAW },
-       { 'n',          REQ_TOGGLE_LINENO },
-       { 'g',          REQ_TOGGLE_REV_GRAPH},
+       { '.',          REQ_TOGGLE_LINENO },
+       { 'g',          REQ_TOGGLE_REV_GRAPH },
        { ':',          REQ_PROMPT },
 
        /* wgetch() with nodelay() enabled returns ERR when there's no input. */
-       { ERR,          REQ_SCREEN_UPDATE },
+       { ERR,          REQ_NONE },
 
-       /* Use the ncurses SIGWINCH handler. */
+       /* Using the ncurses SIGWINCH handler. */
        { KEY_RESIZE,   REQ_SCREEN_RESIZE },
 };
 
+#define KEYMAP_INFO \
+       KEYMAP_(GENERIC), \
+       KEYMAP_(MAIN), \
+       KEYMAP_(DIFF), \
+       KEYMAP_(LOG), \
+       KEYMAP_(TREE), \
+       KEYMAP_(BLOB), \
+       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_keybinding(int key)
+get_keybinding(enum keymap keymap, int key)
 {
+       struct keybinding *kbd;
        int i;
 
+       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;
@@ -700,6 +814,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 },
@@ -718,12 +833,27 @@ 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)
 {
        static char buf[BUFSIZ];
        static char key_char[] = "'X'";
-       int pos = 0;
+       size_t pos = 0;
        char *sep = "    ";
        int i;
 
@@ -815,12 +945,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;
        }
@@ -864,14 +990,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)
 {
@@ -898,33 +1074,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;
        }
@@ -937,7 +1125,7 @@ static int
 load_options(void)
 {
        char *home = getenv("HOME");
-       char buf[1024];
+       char buf[SIZEOF_STR];
        FILE *file;
 
        config_lineno = 0;
@@ -970,12 +1158,13 @@ struct view_ops;
 static struct view *display[2];
 static unsigned int current_view;
 
-#define foreach_view(view, i) \
+#define foreach_displayed_view(view, i) \
        for (i = 0; i < ARRAY_SIZE(display) && (view = display[i]); i++)
 
 #define displayed_views()      (display[1] != NULL ? 2 : 1)
 
 /* Current head and commit ID */
+static char ref_blob[SIZEOF_REF]       = "";
 static char ref_commit[SIZEOF_REF]     = "HEAD";
 static char ref_head[SIZEOF_REF]       = "HEAD";
 
@@ -983,11 +1172,13 @@ struct view {
        const char *name;       /* View name */
        const char *cmd_fmt;    /* Default command line format */
        const char *cmd_env;    /* Command line set via environment */
-       const char *id;         /* Points to either of ref_{head,commit} */
+       const char *id;         /* Points to either of ref_{head,commit,blob} */
 
        struct view_ops *ops;   /* View operations */
 
-       char cmd[SIZEOF_CMD];   /* Command buffer */
+       enum keymap keymap;     /* What keymap does this view have */
+
+       char cmd[SIZEOF_STR];   /* Command buffer */
        char ref[SIZEOF_REF];   /* Hovered commit reference */
        char vid[SIZEOF_REF];   /* View ID. Set to id member when updating. */
 
@@ -999,6 +1190,10 @@ struct view {
        unsigned long offset;   /* Offset of the window top */
        unsigned long lineno;   /* Current line number */
 
+       /* Searching */
+       char grep[SIZEOF_STR];  /* Search string */
+       regex_t regex;          /* Pre-compiled regex */
+
        /* 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;
@@ -1023,32 +1218,45 @@ struct view_ops {
        bool (*read)(struct view *view, char *data);
        /* Depending on view, change display based on current line. */
        bool (*enter)(struct view *view, struct line *line);
+       /* Search for regex in a line. */
+       bool (*grep)(struct view *view, struct line *line);
 };
 
 static struct view_ops pager_ops;
 static struct view_ops main_ops;
+static struct view_ops tree_ops;
+static struct view_ops blob_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[] = {
        VIEW_(MAIN,  "main",  &main_ops,  ref_head),
        VIEW_(DIFF,  "diff",  &pager_ops, ref_commit),
        VIEW_(LOG,   "log",   &pager_ops, ref_head),
+       VIEW_(TREE,  "tree",  &tree_ops,  ref_commit),
+       VIEW_(BLOB,  "blob",  &blob_ops,  ref_blob),
        VIEW_(HELP,  "help",  &pager_ops, "static"),
        VIEW_(PAGER, "pager", &pager_ops, "static"),
 };
 
 #define VIEW(req) (&views[(req) - REQ_OFFSET - 1])
 
+#define foreach_view(view, i) \
+       for (i = 0; i < ARRAY_SIZE(views) && (view = &views[i]); i++)
+
+#define view_is_displayed(view) \
+       (view == display[0] || view == display[1])
 
 static bool
 draw_view_line(struct view *view, unsigned int lineno)
 {
+       assert(view_is_displayed(view));
+
        if (view->offset + lineno >= view->lines)
                return FALSE;
 
@@ -1080,6 +1288,8 @@ redraw_view(struct view *view)
 static void
 update_view_title(struct view *view)
 {
+       assert(view_is_displayed(view));
+
        if (view == display[current_view])
                wbkgdset(view->title, get_line_attr(LINE_TITLE_FOCUS));
        else
@@ -1147,7 +1357,7 @@ resize_display(void)
 
        offset = 0;
 
-       foreach_view (view, i) {
+       foreach_displayed_view (view, i) {
                if (!view->win) {
                        view->win = newwin(view->height, 0, offset, 0);
                        if (!view->win)
@@ -1175,7 +1385,7 @@ redraw_display(void)
        struct view *view;
        int i;
 
-       foreach_view (view, i) {
+       foreach_displayed_view (view, i) {
                redraw_view(view);
                update_view_title(view);
        }
@@ -1204,6 +1414,8 @@ update_display_cursor(void)
 static void
 do_scroll_view(struct view *view, int lines, bool redraw)
 {
+       assert(view_is_displayed(view));
+
        /* The rendering expects the new offset. */
        view->offset += lines;
 
@@ -1383,6 +1595,111 @@ move_view(struct view *view, enum request request, bool redraw)
 
 
 /*
+ * Searching
+ */
+
+static void search_view(struct view *view, enum request request, const char *search);
+
+static bool
+find_next_line(struct view *view, unsigned long lineno, struct line *line)
+{
+       assert(view_is_displayed(view));
+
+       if (!view->ops->grep(view, line))
+               return FALSE;
+
+       if (lineno - view->offset >= view->height) {
+               view->offset = lineno;
+               view->lineno = lineno;
+               redraw_view(view);
+
+       } else {
+               unsigned long old_lineno = view->lineno - view->offset;
+
+               view->lineno = lineno;
+
+               wmove(view->win, old_lineno, 0);
+               wclrtoeol(view->win);
+               draw_view_line(view, old_lineno);
+
+               draw_view_line(view, view->lineno - view->offset);
+               redrawwin(view->win);
+               wrefresh(view->win);
+       }
+
+       report("Line %ld matches '%s'", lineno + 1, view->grep);
+       return TRUE;
+}
+
+static void
+find_next(struct view *view, enum request request)
+{
+       unsigned long lineno = view->lineno;
+       int direction;
+
+       if (!*view->grep) {
+               if (!*opt_search)
+                       report("No previous search");
+               else
+                       search_view(view, request, opt_search);
+               return;
+       }
+
+       switch (request) {
+       case REQ_SEARCH:
+       case REQ_FIND_NEXT:
+               direction = 1;
+               break;
+
+       case REQ_SEARCH_BACK:
+       case REQ_FIND_PREV:
+               direction = -1;
+               break;
+
+       default:
+               return;
+       }
+
+       if (request == REQ_FIND_NEXT || request == REQ_FIND_PREV)
+               lineno += direction;
+
+       /* Note, lineno is unsigned long so will wrap around in which case it
+        * will become bigger than view->lines. */
+       for (; lineno < view->lines; lineno += direction) {
+               struct line *line = &view->line[lineno];
+
+               if (find_next_line(view, lineno, line))
+                       return;
+       }
+
+       report("No match found for '%s'", view->grep);
+}
+
+static void
+search_view(struct view *view, enum request request, const char *search)
+{
+       int regex_err;
+
+       if (*view->grep) {
+               regfree(&view->regex);
+               *view->grep = 0;
+       }
+
+       regex_err = regcomp(&view->regex, search, REG_EXTENDED);
+       if (regex_err != 0) {
+               char buf[SIZEOF_STR] = "unknown error";
+
+               regerror(regex_err, &view->regex, buf, sizeof(buf));
+               report("Search failed: %s", buf);
+               return;
+       }
+
+       string_copy(view->grep, search);
+
+       find_next(view, request);
+}
+
+/*
  * Incremental updating
  */
 
@@ -1413,6 +1730,16 @@ begin_update(struct view *view)
                /* When running random commands, the view ref could have become
                 * invalid so clear it. */
                view->ref[0] = 0;
+
+       } else if (view == VIEW(REQ_VIEW_TREE)) {
+               const char *format = view->cmd_env ? view->cmd_env : view->cmd_fmt;
+
+               if (strcmp(view->vid, view->id))
+                       opt_path[0] = 0;
+
+               if (!string_format(view->cmd, format, id, opt_path))
+                       return FALSE;
+
        } else {
                const char *format = view->cmd_env ? view->cmd_env : view->cmd_fmt;
 
@@ -1470,7 +1797,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
@@ -1485,15 +1813,32 @@ update_view(struct view *view)
        if (view->offset + view->height >= view->lines)
                redraw_from = view->lines - view->offset;
 
+       /* FIXME: This is probably not perfect for backgrounded views. */
        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;
 
@@ -1515,7 +1860,15 @@ update_view(struct view *view)
                }
        }
 
-       if (redraw_from >= 0) {
+       if (!view_is_displayed(view))
+               goto check_pipe;
+
+       if (view == VIEW(REQ_VIEW_TREE)) {
+               /* Clear the view and redraw everything since the tree sorting
+                * might have rearranged things. */
+               redraw_view(view);
+
+       } else if (redraw_from >= 0) {
                /* If this is an incremental update, redraw the previous line
                 * since for commits some members could have changed when
                 * loading the main view. */
@@ -1530,6 +1883,7 @@ update_view(struct view *view)
         * commit reference in view->ref it'll be available here. */
        update_view_title(view);
 
+check_pipe:
        if (ferror(view->pipe)) {
                report("Failed to read: %s", strerror(errno));
                goto end;
@@ -1701,9 +2055,16 @@ view_driver(struct view *view, enum request request)
                scroll_view(view, request);
                break;
 
+       case REQ_VIEW_BLOB:
+               if (!ref_blob[0]) {
+                       report("No file chosen, press 't' to open tree view");
+                       break;
+               }
+               /* Fall-through */
        case REQ_VIEW_MAIN:
        case REQ_VIEW_DIFF:
        case REQ_VIEW_LOG:
+       case REQ_VIEW_TREE:
        case REQ_VIEW_HELP:
        case REQ_VIEW_PAGER:
                open_view(view, request, OPEN_DEFAULT);
@@ -1713,8 +2074,10 @@ 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)) {
+               if ((view == VIEW(REQ_VIEW_DIFF) &&
+                    view->parent == VIEW(REQ_VIEW_MAIN)) ||
+                  (view == VIEW(REQ_VIEW_BLOB) &&
+                    view->parent == VIEW(REQ_VIEW_TREE))) {
                        bool redraw = display[1] == view;
 
                        view = view->parent;
@@ -1765,6 +2128,16 @@ view_driver(struct view *view, enum request request)
                open_view(view, opt_request, OPEN_RELOAD);
                break;
 
+       case REQ_SEARCH:
+       case REQ_SEARCH_BACK:
+               search_view(view, request, opt_search);
+               break;
+
+       case REQ_FIND_NEXT:
+       case REQ_FIND_PREV:
+               find_next(view, request);
+               break;
+
        case REQ_STOP_LOADING:
                for (i = 0; i < ARRAY_SIZE(views); i++) {
                        view = &views[i];
@@ -1785,7 +2158,7 @@ view_driver(struct view *view, enum request request)
                redraw_display();
                break;
 
-       case REQ_SCREEN_UPDATE:
+       case REQ_NONE:
                doupdate();
                return TRUE;
 
@@ -1835,6 +2208,10 @@ pager_draw(struct view *view, struct line *line, unsigned int lineno)
                if (type == LINE_COMMIT) {
                        string_copy(view->ref, text + 7);
                        string_copy(ref_commit, view->ref);
+
+               } else if (type == LINE_TREE_DIR || type == LINE_TREE_FILE) {
+                       string_ncopy(view->ref, text + STRING_SIZE("100644 blob "), 40);
+                       string_copy(ref_blob, view->ref);
                }
 
                type = LINE_CURSOR;
@@ -1896,20 +2273,52 @@ pager_draw(struct view *view, struct line *line, unsigned int lineno)
        return TRUE;
 }
 
+static bool
+add_describe_ref(char *buf, size_t *bufpos, char *commit_id, const char *sep)
+{
+       char refbuf[SIZEOF_STR];
+       char *ref = NULL;
+       FILE *pipe;
+
+       if (!string_format(refbuf, "git describe %s", commit_id))
+               return TRUE;
+
+       pipe = popen(refbuf, "r");
+       if (!pipe)
+               return TRUE;
+
+       if ((ref = fgets(refbuf, sizeof(refbuf), pipe)))
+               ref = chomp_string(ref);
+       pclose(pipe);
+
+       if (!ref || !*ref)
+               return TRUE;
+
+       /* This is the only fatal call, since it can "corrupt" the buffer. */
+       if (!string_nformat(buf, SIZEOF_STR, bufpos, "%s%s", sep, ref))
+               return FALSE;
+
+       return TRUE;
+}
+
 static void
 add_pager_refs(struct view *view, struct line *line)
 {
-       char buf[1024];
-       char *data = line->data;
+       char buf[SIZEOF_STR];
+       char *commit_id = line->data + STRING_SIZE("commit ");
        struct ref **refs;
-       int bufpos = 0, refpos = 0;
+       size_t bufpos = 0, refpos = 0;
        const char *sep = "Refs: ";
+       bool is_tag = FALSE;
 
        assert(line->type == LINE_COMMIT);
 
-       refs = get_refs(data + STRING_SIZE("commit "));
-       if (!refs)
+       refs = get_refs(commit_id);
+       if (!refs) {
+               if (view == VIEW(REQ_VIEW_DIFF))
+                       goto try_add_describe_ref;
                return;
+       }
 
        do {
                struct ref *ref = refs[refpos];
@@ -1918,8 +2327,20 @@ add_pager_refs(struct view *view, struct line *line)
                if (!string_format_from(buf, &bufpos, fmt, sep, ref->name))
                        return;
                sep = ", ";
+               if (ref->tag)
+                       is_tag = TRUE;
        } while (refs[refpos++]->next);
 
+       if (!is_tag && view == VIEW(REQ_VIEW_DIFF)) {
+try_add_describe_ref:
+               /* Add <tag>-g<commit_id> "fake" reference. */
+               if (!add_describe_ref(buf, &bufpos, commit_id, sep))
+                       return;
+       }
+
+       if (bufpos == 0)
+               return;
+
        if (!realloc_lines(view, view->line_size + 1))
                return;
 
@@ -1978,11 +2399,231 @@ pager_enter(struct view *view, struct line *line)
        return TRUE;
 }
 
+static bool
+pager_grep(struct view *view, struct line *line)
+{
+       regmatch_t pmatch;
+       char *text = line->data;
+
+       if (!*text)
+               return FALSE;
+
+       if (regexec(&view->regex, text, 1, &pmatch, 0) == REG_NOMATCH)
+               return FALSE;
+
+       return TRUE;
+}
+
 static struct view_ops pager_ops = {
        "line",
        pager_draw,
        pager_read,
        pager_enter,
+       pager_grep,
+};
+
+
+/*
+ * Tree backend
+ */
+
+/* Parse output from git ls-tree:
+ *
+ * 100644 blob fb0e31ea6cc679b7379631188190e975f5789c26        Makefile
+ * 100644 blob 5304ca4260aaddaee6498f9630e7d471b8591ea6        README
+ * 100644 blob f931e1d229c3e185caad4449bf5b66ed72462657        tig.c
+ * 100644 blob ed09fe897f3c7c9af90bcf80cae92558ea88ae38        web.conf
+ */
+
+#define SIZEOF_TREE_ATTR \
+       STRING_SIZE("100644 blob ed09fe897f3c7c9af90bcf80cae92558ea88ae38\t")
+
+#define TREE_UP_FORMAT "040000 tree %s\t.."
+
+static int
+tree_compare_entry(enum line_type type1, char *name1,
+                  enum line_type type2, char *name2)
+{
+       if (type1 != type2) {
+               if (type1 == LINE_TREE_DIR)
+                       return -1;
+               return 1;
+       }
+
+       return strcmp(name1, name2);
+}
+
+static bool
+tree_read(struct view *view, char *text)
+{
+       size_t textlen = strlen(text);
+       char buf[SIZEOF_STR];
+       unsigned long pos;
+       enum line_type type;
+       bool first_read = view->lines == 0;
+
+       if (textlen <= SIZEOF_TREE_ATTR)
+               return FALSE;
+
+       type = text[STRING_SIZE("100644 ")] == 't'
+            ? LINE_TREE_DIR : LINE_TREE_FILE;
+
+       if (first_read) {
+               /* Add path info line */
+               if (string_format(buf, "Directory path /%s", opt_path) &&
+                   realloc_lines(view, view->line_size + 1) &&
+                   pager_read(view, buf))
+                       view->line[view->lines - 1].type = LINE_DEFAULT;
+               else
+                       return FALSE;
+
+               /* Insert "link" to parent directory. */
+               if (*opt_path &&
+                   string_format(buf, TREE_UP_FORMAT, view->ref) &&
+                   realloc_lines(view, view->line_size + 1) &&
+                   pager_read(view, buf))
+                       view->line[view->lines - 1].type = LINE_TREE_DIR;
+               else if (*opt_path)
+                       return FALSE;
+       }
+
+       /* Strip the path part ... */
+       if (*opt_path) {
+               size_t pathlen = textlen - SIZEOF_TREE_ATTR;
+               size_t striplen = strlen(opt_path);
+               char *path = text + SIZEOF_TREE_ATTR;
+
+               if (pathlen > striplen)
+                       memmove(path, path + striplen,
+                               pathlen - striplen + 1);
+       }
+
+       /* Skip "Directory ..." and ".." line. */
+       for (pos = 1 + !!*opt_path; pos < view->lines; pos++) {
+               struct line *line = &view->line[pos];
+               char *path1 = ((char *) line->data) + SIZEOF_TREE_ATTR;
+               char *path2 = text + SIZEOF_TREE_ATTR;
+               int cmp = tree_compare_entry(line->type, path1, type, path2);
+
+               if (cmp <= 0)
+                       continue;
+
+               text = strdup(text);
+               if (!text)
+                       return FALSE;
+
+               if (view->lines > pos)
+                       memmove(&view->line[pos + 1], &view->line[pos],
+                               (view->lines - pos) * sizeof(*line));
+
+               line = &view->line[pos];
+               line->data = text;
+               line->type = type;
+               view->lines++;
+               return TRUE;
+       }
+
+       if (!pager_read(view, text))
+               return FALSE;
+
+       /* Move the current line to the first tree entry. */
+       if (first_read)
+               view->lineno++;
+
+       view->line[view->lines - 1].type = type;
+       return TRUE;
+}
+
+static bool
+tree_enter(struct view *view, struct line *line)
+{
+       enum open_flags flags = OPEN_DEFAULT;
+       char *data = line->data;
+       enum request request;
+
+       switch (line->type) {
+       case LINE_TREE_DIR:
+               /* Depending on whether it is a subdir or parent (updir?) link
+                * mangle the path buffer. */
+               if (line == &view->line[1] && *opt_path) {
+                       size_t path_len = strlen(opt_path);
+                       char *dirsep = opt_path + path_len - 1;
+
+                       while (dirsep > opt_path && dirsep[-1] != '/')
+                               dirsep--;
+
+                       dirsep[0] = 0;
+
+               } else {
+                       size_t pathlen = strlen(opt_path);
+                       size_t origlen = pathlen;
+                       char *basename = data + SIZEOF_TREE_ATTR;
+
+                       if (string_format_from(opt_path, &pathlen, "%s/", basename)) {
+                               opt_path[origlen] = 0;
+                               return TRUE;
+                       }
+               }
+
+               /* Trees and subtrees share the same ID, so they are not not
+                * unique like blobs. */
+               flags |= OPEN_RELOAD;
+               request = REQ_VIEW_TREE;
+               break;
+
+       case LINE_TREE_FILE:
+               /* This causes the blob view to become split, and not having it
+                * in the tree dir case will make the blob view automatically
+                * disappear when moving to a different directory. */
+               flags |= OPEN_SPLIT;
+               request = REQ_VIEW_BLOB;
+               break;
+
+       default:
+               return TRUE;
+       }
+
+       open_view(view, request, flags);
+
+       if (!VIEW(request)->pipe)
+               return TRUE;
+
+       /* For tree views insert the path to the parent as the first line. */
+       if (request == REQ_VIEW_BLOB) {
+               /* Mirror what is showed in the title bar. */
+               string_ncopy(ref_blob, data + STRING_SIZE("100644 blob "), 40);
+               string_copy(VIEW(REQ_VIEW_BLOB)->ref, ref_blob);
+               return TRUE;
+       }
+
+       return TRUE;
+}
+
+static struct view_ops tree_ops = {
+       "file",
+       pager_draw,
+       tree_read,
+       tree_enter,
+       pager_grep,
+};
+
+static bool
+blob_read(struct view *view, char *line)
+{
+       bool state = pager_read(view, line);
+
+       if (state == TRUE)
+               view->line[view->lines - 1].type = LINE_DEFAULT;
+
+       return state;
+}
+
+static struct view_ops blob_ops = {
+       "line",
+       pager_draw,
+       blob_read,
+       pager_enter,
+       pager_grep,
 };
 
 
@@ -2140,10 +2781,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 */
@@ -2207,11 +2860,43 @@ main_enter(struct view *view, struct line *line)
        return TRUE;
 }
 
+static bool
+main_grep(struct view *view, struct line *line)
+{
+       struct commit *commit = line->data;
+       enum { S_TITLE, S_AUTHOR, S_DATE, S_END } state;
+       char buf[DATE_COLS + 1];
+       regmatch_t pmatch;
+
+       for (state = S_TITLE; state < S_END; state++) {
+               char *text;
+
+               switch (state) {
+               case S_TITLE:   text = commit->title;   break;
+               case S_AUTHOR:  text = commit->author;  break;
+               case S_DATE:
+                       if (!strftime(buf, sizeof(buf), DATE_FORMAT, &commit->time))
+                               continue;
+                       text = buf;
+                       break;
+
+               default:
+                       return FALSE;
+               }
+
+               if (regexec(&view->regex, text, 1, &pmatch, 0) != REG_NOMATCH)
+                       return TRUE;
+       }
+
+       return FALSE;
+}
+
 static struct view_ops main_ops = {
        "commit",
        main_draw,
        main_read,
        main_enter,
+       main_grep,
 };
 
 
@@ -2437,6 +3122,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);
        }
 
@@ -2461,6 +3148,65 @@ init_display(void)
        wbkgdset(status_win, get_line_attr(LINE_STATUS));
 }
 
+static char *
+read_prompt(const char *prompt)
+{
+       enum { READING, STOP, CANCEL } status = READING;
+       static 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%.*s", prompt, 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 NULL;
+                       }
+
+                       if (isprint(key))
+                               buf[pos++] = (char) key;
+               }
+       }
+
+       if (status == CANCEL) {
+               /* Clear the status window */
+               report("");
+               return NULL;
+       }
+
+       buf[pos++] = 0;
+
+       return buf;
+}
 
 /*
  * Repository references
@@ -2669,6 +3415,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.");
 
@@ -2680,6 +3430,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_NONE)
+                       die("Failed to initialize character set conversion");
+       }
+
        if (load_refs() == ERR)
                die("Failed to load refs.");
 
@@ -2703,31 +3459,41 @@ main(int argc, char *argv[])
 
                /* Refresh, accept single keystroke of input */
                key = wgetch(status_win);
-               request = get_keybinding(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");
-                               request = REQ_SCREEN_UPDATE;
+               {
+                       char *cmd = read_prompt(":");
+
+                       if (cmd && string_format(opt_cmd, "git %s", cmd)) {
+                               if (strncmp(cmd, "show", 4) && isspace(cmd[4])) {
+                                       opt_request = REQ_VIEW_DIFF;
+                               } else {
+                                       opt_request = REQ_VIEW_PAGER;
+                               }
+                               break;
                        }
 
-                       noecho();
-                       cbreak();
+                       request = REQ_NONE;
                        break;
+               }
+               case REQ_SEARCH:
+               case REQ_SEARCH_BACK:
+               {
+                       const char *prompt = request == REQ_SEARCH
+                                          ? "/" : "?";
+                       char *search = read_prompt(prompt);
 
+                       if (search)
+                               string_copy(opt_search, search);
+                       else
+                               request = REQ_NONE;
+                       break;
+               }
                case REQ_SCREEN_RESIZE:
                {
                        int height, width;