Code

Oops, fix enum_equals
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index bd30997c00ede3035271484f5cb18326fe64c34c..d9f26dd0f850c345093bc02a386e25421734221d 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -297,6 +297,9 @@ string_enum_compare(const char *str1, const char *str2, int len)
        return 0;
 }
 
+#define enum_equals(entry, str, len) \
+       ((entry).namelen == (len) && !string_enum_compare((entry).name, str, len))
+
 struct enum_map {
        const char *name;
        int namelen;
@@ -305,6 +308,24 @@ struct enum_map {
 
 #define ENUM_MAP(name, value) { name, STRING_SIZE(name), value }
 
+static char *
+enum_map_name(const char *name, size_t namelen)
+{
+       static char buf[SIZEOF_STR];
+       int bufpos;
+
+       for (bufpos = 0; bufpos <= namelen; bufpos++) {
+               buf[bufpos] = tolower(name[bufpos]);
+               if (buf[bufpos] == '_')
+                       buf[bufpos] = '-';
+       }
+
+       buf[bufpos] = 0;
+       return buf;
+}
+
+#define enum_name(entry) enum_map_name((entry).name, (entry).namelen)
+
 static bool
 map_enum_do(const struct enum_map *map, size_t map_size, int *value, const char *name)
 {
@@ -312,8 +333,7 @@ map_enum_do(const struct enum_map *map, size_t map_size, int *value, const char
        int i;
 
        for (i = 0; i < map_size; i++)
-               if (namelen == map[i].namelen &&
-                   !string_enum_compare(name, map[i].name, namelen)) {
+               if (enum_equals(map[i], name, namelen)) {
                        *value = map[i].value;
                        return TRUE;
                }
@@ -363,14 +383,25 @@ static int local_tzoffset(time_t time)
        return offset * eastwest;
 }
 
+#define DATE_INFO \
+       DATE_(NO), \
+       DATE_(DEFAULT), \
+       DATE_(RELATIVE), \
+       DATE_(SHORT)
+
 enum date {
-       DATE_NONE = 0,
-       DATE_DEFAULT,
-       DATE_RELATIVE,
-       DATE_SHORT
+#define DATE_(name) DATE_##name
+       DATE_INFO
+#undef DATE_
 };
 
-static char *
+static const struct enum_map date_map[] = {
+#define DATE_(name) ENUM_MAP(#name, DATE_##name)
+       DATE_INFO
+#undef DATE_
+};
+
+static const char *
 string_date(const time_t *time, enum date date)
 {
        static char buf[DATE_COLS + 1];
@@ -963,8 +994,7 @@ get_request(const char *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))
+               if (enum_equals(req_info[i], name, namelen))
                        return req_info[i].request;
 
        return REQ_NONE;
@@ -994,9 +1024,9 @@ static char opt_head[SIZEOF_REF]   = "";
 static char opt_head_rev[SIZEOF_REV]   = "";
 static char opt_remote[SIZEOF_REF]     = "";
 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 iconv_t opt_iconv_in            = ICONV_NONE;
+static iconv_t opt_iconv_out           = ICONV_NONE;
 static char opt_search[SIZEOF_STR]     = "";
 static char opt_cdup[SIZEOF_STR]       = "";
 static char opt_prefix[SIZEOF_STR]     = "";
@@ -1125,8 +1155,7 @@ get_line_info(const char *name)
        enum line_type type;
 
        for (type = 0; type < ARRAY_SIZE(line_info); type++)
-               if (namelen == line_info[type].namelen &&
-                   !string_enum_compare(line_info[type].name, name, namelen))
+               if (enum_equals(line_info[type], name, namelen))
                        return &line_info[type];
 
        return NULL;
@@ -1658,6 +1687,26 @@ static int parse_bool(bool *opt, const char *arg)
        return OK;
 }
 
+static int parse_enum_do(unsigned int *opt, const char *arg,
+                        const struct enum_map *map, size_t map_size)
+{
+       bool is_true;
+
+       assert(map_size > 1);
+
+       if (map_enum_do(map, map_size, (int *) opt, arg))
+               return OK;
+
+       if (parse_bool(&is_true, arg) != OK)
+               return ERR;
+
+       *opt = is_true ? map[1].value : map[0].value;
+       return OK;
+}
+
+#define parse_enum(opt, arg, map) \
+       parse_enum_do(opt, arg, map, ARRAY_SIZE(map))
+
 static int
 parse_string(char *opt, const char *arg, size_t optsize)
 {
@@ -1694,20 +1743,8 @@ option_set_command(int argc, const char *argv[])
        if (!strcmp(argv[0], "show-author"))
                return parse_bool(&opt_author, argv[2]);
 
-       if (!strcmp(argv[0], "show-date")) {
-               bool show_date;
-
-               if (!strcmp(argv[2], "relative")) {
-                       opt_date = DATE_RELATIVE;
-                       return OK;
-               } else if (!strcmp(argv[2], "short")) {
-                       opt_date = DATE_SHORT;
-                       return OK;
-               } else if (parse_bool(&show_date, argv[2])) {
-                       opt_date = show_date ? DATE_DEFAULT : DATE_NONE;
-               }
-               return ERR;
-       }
+       if (!strcmp(argv[0], "show-date"))
+               return parse_enum(&opt_date, argv[2], date_map);
 
        if (!strcmp(argv[0], "show-rev-graph"))
                return parse_bool(&opt_rev_graph, argv[2]);
@@ -2060,6 +2097,7 @@ static int
 draw_chars(struct view *view, enum line_type type, const char *string,
           int max_len, bool use_tilde)
 {
+       static char out_buffer[BUFSIZ * 2];
        int len = 0;
        int col = 0;
        int trimmed = FALSE;
@@ -2068,22 +2106,28 @@ draw_chars(struct view *view, enum line_type type, const char *string,
        if (max_len <= 0)
                return 0;
 
-       if (opt_utf8) {
-               len = utf8_length(&string, skip, &col, max_len, &trimmed, use_tilde);
-       } else {
-               col = len = strlen(string);
-               if (len > max_len) {
-                       if (use_tilde) {
-                               max_len -= 1;
+       len = utf8_length(&string, skip, &col, max_len, &trimmed, use_tilde);
+
+       set_view_attr(view, type);
+       if (len > 0) {
+               if (opt_iconv_out != ICONV_NONE) {
+                       ICONV_CONST char *inbuf = (ICONV_CONST char *) string;
+                       size_t inlen = len + 1;
+
+                       char *outbuf = out_buffer;
+                       size_t outlen = sizeof(out_buffer);
+
+                       size_t ret;
+
+                       ret = iconv(opt_iconv_out, &inbuf, &inlen, &outbuf, &outlen);
+                       if (ret != (size_t) -1) {
+                               string = out_buffer;
+                               len = sizeof(out_buffer) - outlen;
                        }
-                       col = len = max_len;
-                       trimmed = TRUE;
                }
-       }
 
-       set_view_attr(view, type);
-       if (len > 0)
                waddnstr(view->win, string, len);
+       }
        if (trimmed && use_tilde) {
                set_view_attr(view, LINE_DELIMITER);
                waddch(view->win, '~');
@@ -2442,20 +2486,19 @@ redraw_display(bool clear)
 }
 
 static void
-toggle_date_option(enum date *date)
+toggle_enum_option_do(unsigned int *opt, const char *help,
+                     const struct enum_map *map, size_t size)
 {
-       static const char *help[] = {
-               "no",
-               "default",
-               "relative",
-               "short"
-       };
-
-       opt_date = (opt_date + 1) % ARRAY_SIZE(help);
+       *opt = (*opt + 1) % size;
        redraw_display(FALSE);
-       report("Displaying %s dates", help[opt_date]);
+       report("Displaying %s %s", enum_name(map[*opt]), help);
 }
 
+#define toggle_enum_option(opt, help, map) \
+       toggle_enum_option_do(opt, help, map, ARRAY_SIZE(map))
+
+#define toggle_date() toggle_enum_option(&opt_date, "dates", date_map)
+
 static void
 toggle_view_option(bool *option, const char *help)
 {
@@ -2479,7 +2522,7 @@ open_option_menu(void)
 
        if (prompt_menu("Toggle option", menu, &selected)) {
                if (menu[selected].data == &opt_date)
-                       toggle_date_option(menu[selected].data);
+                       toggle_date();
                else
                        toggle_view_option(menu[selected].data, menu[selected].text);
        }
@@ -3056,7 +3099,7 @@ update_view(struct view *view)
        }
 
        for (; (line = io_get(view->pipe, '\n', can_read)); can_read = FALSE) {
-               if (opt_iconv != ICONV_NONE) {
+               if (opt_iconv_in != ICONV_NONE) {
                        ICONV_CONST char *inbuf = line;
                        size_t inlen = strlen(line) + 1;
 
@@ -3065,7 +3108,7 @@ update_view(struct view *view)
 
                        size_t ret;
 
-                       ret = iconv(opt_iconv, &inbuf, &inlen, &outbuf, &outlen);
+                       ret = iconv(opt_iconv_in, &inbuf, &inlen, &outbuf, &outlen);
                        if (ret != (size_t) -1)
                                line = out_buffer;
                }
@@ -3236,11 +3279,9 @@ open_view(struct view *prev, enum request request, enum open_flags flags)
                do_scroll_view(prev, lines);
        }
 
-       if (prev && view != prev) {
-               if (split) {
-                       /* "Blur" the previous view. */
-                       update_view_title(prev);
-               }
+       if (prev && view != prev && split && view_is_displayed(prev)) {
+               /* "Blur" the previous view. */
+               update_view_title(prev);
        }
 
        if (view->pipe && view->lines == 0) {
@@ -3478,7 +3519,7 @@ view_driver(struct view *view, enum request request)
                break;
 
        case REQ_TOGGLE_DATE:
-               toggle_date_option(&opt_date);
+               toggle_date();
                break;
 
        case REQ_TOGGLE_AUTHOR:
@@ -3956,33 +3997,14 @@ static struct view_ops diff_ops = {
 
 static bool help_keymap_hidden[ARRAY_SIZE(keymap_table)];
 
-static char *
-help_name(char buf[SIZEOF_STR], const char *name, size_t namelen)
-{
-       int bufpos;
-
-       for (bufpos = 0; bufpos <= namelen; bufpos++) {
-               buf[bufpos] = tolower(name[bufpos]);
-               if (buf[bufpos] == '_')
-                       buf[bufpos] = '-';
-       }
-
-       buf[bufpos] = 0;
-       return buf;
-}
-
-#define help_keymap_name(buf, keymap) \
-       help_name(buf, keymap_table[keymap].name, keymap_table[keymap].namelen)
-
 static bool
 help_open_keymap_title(struct view *view, enum keymap keymap)
 {
-       char buf[SIZEOF_STR];
        struct line *line;
 
        line = add_line_format(view, LINE_HELP_KEYMAP, "[%c] %s bindings",
                               help_keymap_hidden[keymap] ? '+' : '-',
-                              help_keymap_name(buf, keymap));
+                              enum_name(keymap_table[keymap]));
        if (line)
                line->other = keymap;
 
@@ -4023,8 +4045,7 @@ help_open_keymap(struct view *view, enum keymap keymap)
                }
 
                add_line_format(view, LINE_DEFAULT, "    %-25s %-20s %s", key,
-                               help_name(buf, req_info[i].name, req_info[i].namelen),
-                               req_info[i].help);
+                               enum_name(req_info[i]), req_info[i].help);
        }
 
        group = "External commands:";
@@ -5039,6 +5060,8 @@ struct branch {
        const struct ref *ref;          /* Name and commit ID information. */
 };
 
+static const struct ref branch_all;
+
 static const enum sort_field branch_sort_fields[] = {
        ORDERBY_NAME, ORDERBY_DATE, ORDERBY_AUTHOR
 };
@@ -5075,13 +5098,15 @@ branch_draw(struct view *view, struct line *line, unsigned int lineno)
        if (opt_author && draw_author(view, branch->author))
                return TRUE;
 
-       draw_text(view, type, branch->ref->name, TRUE);
+       draw_text(view, type, branch->ref == &branch_all ? "All branches" : branch->ref->name, TRUE);
        return TRUE;
 }
 
 static enum request
 branch_request(struct view *view, enum request request, struct line *line)
 {
+       struct branch *branch = line->data;
+
        switch (request) {
        case REQ_REFRESH:
                load_refs();
@@ -5094,7 +5119,21 @@ branch_request(struct view *view, enum request request, struct line *line)
                return REQ_NONE;
 
        case REQ_ENTER:
-               open_view(view, REQ_VIEW_MAIN, OPEN_SPLIT);
+               if (branch->ref == &branch_all) {
+                       const char *all_branches_argv[] = {
+                               "git", "log", "--no-color", "--pretty=raw", "--parents",
+                                     "--topo-order", "--all", NULL
+                       };
+                       struct view *main_view = VIEW(REQ_VIEW_MAIN);
+
+                       if (!prepare_update(main_view, all_branches_argv, NULL, FORMAT_NONE)) {
+                               report("Failed to load view of all branches");
+                               return REQ_NONE;
+                       }
+                       open_view(view, REQ_VIEW_MAIN, OPEN_PREPARED | OPEN_SPLIT);
+               } else {
+                       open_view(view, REQ_VIEW_MAIN, OPEN_SPLIT);
+               }
                return REQ_NONE;
 
        default:
@@ -5174,6 +5213,7 @@ branch_open(struct view *view)
        }
 
        setup_update(view, view->id);
+       branch_open_visitor(view, &branch_all);
        foreach_ref(branch_open_visitor, view);
        view->p_restore = TRUE;
 
@@ -5287,7 +5327,7 @@ status_run(struct view *view, const char *argv[], char status, enum line_type ty
        char *buf;
        struct io io = {};
 
-       if (!run_io(&io, argv, NULL, IO_RD))
+       if (!run_io(&io, argv, opt_cdup, IO_RD))
                return FALSE;
 
        add_line_data(view, NULL, type);
@@ -5685,10 +5725,8 @@ status_update_prepare(struct io *io, enum line_type type)
                return run_io(io, staged_argv, opt_cdup, IO_WR);
 
        case LINE_STAT_UNSTAGED:
-               return run_io(io, others_argv, opt_cdup, IO_WR);
-
        case LINE_STAT_UNTRACKED:
-               return run_io(io, others_argv, NULL, IO_WR);
+               return run_io(io, others_argv, opt_cdup, IO_WR);
 
        default:
                die("line type %d not handled in switch", type);
@@ -7681,12 +7719,15 @@ main(int argc, const char *argv[])
        if (!opt_git_dir[0] && request != REQ_VIEW_PAGER)
                die("Not a git repository");
 
-       if (*opt_encoding && strcasecmp(opt_encoding, "UTF-8"))
-               opt_utf8 = FALSE;
+       if (*opt_encoding && strcmp(opt_codeset, "UTF-8")) {
+               opt_iconv_in = iconv_open("UTF-8", opt_encoding);
+               if (opt_iconv_in == ICONV_NONE)
+                       die("Failed to initialize character set conversion");
+       }
 
-       if (*opt_codeset && strcmp(opt_codeset, opt_encoding)) {
-               opt_iconv = iconv_open(opt_codeset, opt_encoding);
-               if (opt_iconv == ICONV_NONE)
+       if (*opt_codeset && strcmp(opt_codeset, "UTF-8")) {
+               opt_iconv_out = iconv_open(opt_codeset, "UTF-8");
+               if (opt_iconv_out == ICONV_NONE)
                        die("Failed to initialize character set conversion");
        }