Code

Refactor help_name into enum_name
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index 222b3698de6bcc60395ab74519b4c9048e2e5ed6..41d8123379eec382c30f9e7d9e9bb9c1f09892d2 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -137,7 +137,7 @@ struct ref_list {
 };
 
 static struct ref_list *get_ref_list(const char *id);
-static void foreach_ref(bool (*visitor)(void *data, struct ref *ref), void *data);
+static void foreach_ref(bool (*visitor)(void *data, const struct ref *ref), void *data);
 static int load_refs(void);
 
 enum format_flags {
@@ -305,6 +305,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)
 {
@@ -994,9 +1012,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]     = "";
@@ -1703,8 +1721,9 @@ option_set_command(int argc, const char *argv[])
                } else if (!strcmp(argv[2], "short")) {
                        opt_date = DATE_SHORT;
                        return OK;
-               } else if (parse_bool(&show_date, argv[2])) {
+               } else if (parse_bool(&show_date, argv[2]) == OK) {
                        opt_date = show_date ? DATE_DEFAULT : DATE_NONE;
+                       return OK;
                }
                return ERR;
        }
@@ -2060,6 +2079,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 +2088,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, '~');
@@ -2451,9 +2477,9 @@ toggle_date_option(enum date *date)
                "short"
        };
 
-       opt_date = (opt_date + 1) % ARRAY_SIZE(help);
+       *date = (*date + 1) % ARRAY_SIZE(help);
        redraw_display(FALSE);
-       report("Displaying %s dates", help[opt_date]);
+       report("Displaying %s dates", help[*date]);
 }
 
 static void
@@ -3056,7 +3082,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 +3091,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 +3262,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) {
@@ -3956,33 +3980,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 +4028,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:";
@@ -5036,9 +5040,11 @@ static struct view_ops blame_ops = {
 struct branch {
        const char *author;             /* Author of the last commit. */
        time_t time;                    /* Date of the last activity. */
-       struct ref *ref;                /* Name and commit ID information. */
+       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 +5081,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 +5102,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:
@@ -5144,7 +5166,7 @@ branch_read(struct view *view, char *line)
 }
 
 static bool
-branch_open_visitor(void *data, struct ref *ref)
+branch_open_visitor(void *data, const struct ref *ref)
 {
        struct view *view = data;
        struct branch *branch;
@@ -5174,6 +5196,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 +5310,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 +5708,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);
@@ -5882,10 +5903,8 @@ status_request(struct view *view, enum request request, struct line *line)
                break;
 
        case REQ_VIEW_BLAME:
-               if (status) {
-                       string_copy(opt_file, status->new.name);
+               if (status)
                        opt_ref[0] = 0;
-               }
                return request;
 
        case REQ_ENTER:
@@ -5952,6 +5971,8 @@ status_select(struct view *view, struct line *line)
        }
 
        string_format(view->ref, text, key, file);
+       if (status)
+               string_copy(opt_file, status->new.name);
 }
 
 static bool
@@ -7207,7 +7228,7 @@ compare_refs(const void *ref1_, const void *ref2_)
 }
 
 static void
-foreach_ref(bool (*visitor)(void *data, struct ref *ref), void *data)
+foreach_ref(bool (*visitor)(void *data, const struct ref *ref), void *data)
 {
        size_t i;
 
@@ -7681,12 +7702,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");
        }