Code

Abbreviation of author names is now configurable and toggleable
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index cf4e61a131a5b8c8286e4d97bba2f0faaa88e180..b668cf002a7e66c6ed3ff4fd36cda7f11f5635c7 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];
@@ -411,6 +442,45 @@ string_date(const time_t *time, enum date date)
 }
 
 
+#define AUTHOR_VALUES \
+       AUTHOR_(NO), \
+       AUTHOR_(FULL), \
+       AUTHOR_(ABBREVIATED)
+
+enum author {
+#define AUTHOR_(name) AUTHOR_##name
+       AUTHOR_VALUES,
+#undef AUTHOR_
+       AUTHOR_DEFAULT = AUTHOR_FULL
+};
+
+static const struct enum_map author_map[] = {
+#define AUTHOR_(name) ENUM_MAP(#name, AUTHOR_##name)
+       AUTHOR_VALUES
+#undef AUTHOR_
+};
+
+static const char *
+get_author_initials(const char *author, size_t max_columns)
+{
+       static char initials[10];
+       size_t pos;
+
+#define is_initial_sep(c) (isspace(c) || ispunct(c) || (c) == '@')
+
+       memset(initials, 0, sizeof(initials));
+       for (pos = 0; *author && pos < max_columns - 1; author++, pos++) {
+               while (is_initial_sep(*author))
+                       author++;
+               strncpy(&initials[pos], author, sizeof(initials) - 1 - pos);
+               while (*author && !is_initial_sep(author[1]))
+                       author++;
+       }
+
+       return initials;
+}
+
+
 static bool
 argv_from_string(const char *argv[SIZEOF_ARG], int *argc, char *cmd)
 {
@@ -963,8 +1033,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;
@@ -977,7 +1046,7 @@ get_request(const char *name)
 
 /* Option and state variables. */
 static enum date opt_date              = DATE_DEFAULT;
-static bool opt_author                 = TRUE;
+static enum author opt_author          = AUTHOR_DEFAULT;
 static bool opt_line_number            = FALSE;
 static bool opt_line_graphics          = TRUE;
 static bool opt_rev_graph              = FALSE;
@@ -1125,8 +1194,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 +1726,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)
 {
@@ -1692,23 +1780,10 @@ 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]) == OK) {
-                       opt_date = show_date ? DATE_DEFAULT : DATE_NONE;
-                       return OK;
-               }
-               return ERR;
-       }
+               return parse_enum(&opt_author, argv[2], author_map);
+
+       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]);
@@ -2178,25 +2253,11 @@ draw_date(struct view *view, time_t *time)
 static bool
 draw_author(struct view *view, const char *author)
 {
-       bool trim = opt_author_cols == 0 || opt_author_cols > 5 || !author;
-
-       if (!trim) {
-               static char initials[10];
-               size_t pos;
-
-#define is_initial_sep(c) (isspace(c) || ispunct(c) || (c) == '@')
-
-               memset(initials, 0, sizeof(initials));
-               for (pos = 0; *author && pos < opt_author_cols - 1; author++, pos++) {
-                       while (is_initial_sep(*author))
-                               author++;
-                       strncpy(&initials[pos], author, sizeof(initials) - 1 - pos);
-                       while (*author && !is_initial_sep(author[1]))
-                               author++;
-               }
+       bool trim = opt_author_cols == 0 || opt_author_cols > 5;
+       bool abbreviate = opt_author == AUTHOR_ABBREVIATED || !trim;
 
-               author = initials;
-       }
+       if (abbreviate && author)
+               author = get_author_initials(author, opt_author_cols);
 
        return draw_field(view, LINE_AUTHOR, author, opt_author_cols, trim);
 }
@@ -2450,20 +2511,20 @@ 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"
-       };
-
-       *date = (*date + 1) % ARRAY_SIZE(help);
+       *opt = (*opt + 1) % size;
        redraw_display(FALSE);
-       report("Displaying %s dates", help[*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)
+#define toggle_author() toggle_enum_option(&opt_author, "author names", author_map)
+
 static void
 toggle_view_option(bool *option, const char *help)
 {
@@ -2487,7 +2548,9 @@ 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 if (menu[selected].data == &opt_author)
+                       toggle_author();
                else
                        toggle_view_option(menu[selected].data, menu[selected].text);
        }
@@ -2879,6 +2942,32 @@ free_argv(const char *argv[])
                free((void *) argv[argc]);
 }
 
+static const char *
+format_arg(const char *name)
+{
+       static struct {
+               const char *name;
+               size_t namelen;
+               const char *value;
+               const char *value_if_empty;
+       } vars[] = {
+#define FORMAT_VAR(name, value, value_if_empty) \
+       { name, STRING_SIZE(name), value, value_if_empty }
+               FORMAT_VAR("%(directory)",      opt_path,       ""),
+               FORMAT_VAR("%(file)",           opt_file,       ""),
+               FORMAT_VAR("%(ref)",            opt_ref,        "HEAD"),
+               FORMAT_VAR("%(head)",           ref_head,       ""),
+               FORMAT_VAR("%(commit)",         ref_commit,     ""),
+               FORMAT_VAR("%(blob)",           ref_blob,       ""),
+       };
+       int i;
+
+       for (i = 0; i < ARRAY_SIZE(vars); i++)
+               if (!strncmp(name, vars[i].name, vars[i].namelen))
+                       return *vars[i].value ? vars[i].value : vars[i].value_if_empty;
+
+       return NULL;
+}
 static bool
 format_argv(const char *dst_argv[], const char *src_argv[], enum format_flags flags)
 {
@@ -2903,27 +2992,13 @@ format_argv(const char *dst_argv[], const char *src_argv[], enum format_flags fl
                                len = strlen(arg);
                                value = "";
 
-                       } else if (!prefixcmp(next, "%(directory)")) {
-                               value = opt_path;
-
-                       } else if (!prefixcmp(next, "%(file)")) {
-                               value = opt_file;
-
-                       } else if (!prefixcmp(next, "%(ref)")) {
-                               value = *opt_ref ? opt_ref : "HEAD";
-
-                       } else if (!prefixcmp(next, "%(head)")) {
-                               value = ref_head;
-
-                       } else if (!prefixcmp(next, "%(commit)")) {
-                               value = ref_commit;
-
-                       } else if (!prefixcmp(next, "%(blob)")) {
-                               value = ref_blob;
-
                        } else {
-                               report("Unknown replacement: `%s`", next);
-                               return FALSE;
+                               value = format_arg(next);
+
+                               if (!value) {
+                                       report("Unknown replacement: `%s`", next);
+                                       return FALSE;
+                               }
                        }
 
                        if (!string_format_from(buf, &bufpos, "%.*s%s", len, arg, value))
@@ -3484,11 +3559,11 @@ 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:
-               toggle_view_option(&opt_author, "author display");
+               toggle_author();
                break;
 
        case REQ_TOGGLE_REV_GRAPH:
@@ -3962,33 +4037,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;
 
@@ -4029,8 +4085,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:";