Code

Abbreviation of author names is now configurable and toggleable
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index 399567b07416ba63bb9a5bd4790f5fd0722c0cad..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;
@@ -330,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;
                }
@@ -381,23 +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_NO = 0,
-       DATE_DEFAULT,
-       DATE_RELATIVE,
-       DATE_SHORT
+#define DATE_(name) DATE_##name
+       DATE_INFO
+#undef DATE_
 };
 
 static const struct enum_map date_map[] = {
 #define DATE_(name) ENUM_MAP(#name, DATE_##name)
-       DATE_(NO),
-       DATE_(DEFAULT),
-       DATE_(RELATIVE),
-       DATE_(SHORT)
+       DATE_INFO
 #undef DATE_
 };
 
-static char *
+static const char *
 string_date(const time_t *time, enum date date)
 {
        static char buf[DATE_COLS + 1];
@@ -438,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)
 {
@@ -990,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;
@@ -1004,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;
@@ -1152,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;
@@ -1739,7 +1780,7 @@ option_set_command(int argc, const char *argv[])
        }
 
        if (!strcmp(argv[0], "show-author"))
-               return parse_bool(&opt_author, argv[2]);
+               return parse_enum(&opt_author, argv[2], author_map);
 
        if (!strcmp(argv[0], "show-date"))
                return parse_enum(&opt_date, argv[2], date_map);
@@ -2212,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;
+       bool trim = opt_author_cols == 0 || opt_author_cols > 5;
+       bool abbreviate = opt_author == AUTHOR_ABBREVIATED || !trim;
 
-       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++;
-               }
-
-               author = initials;
-       }
+       if (abbreviate && author)
+               author = get_author_initials(author, opt_author_cols);
 
        return draw_field(view, LINE_AUTHOR, author, opt_author_cols, trim);
 }
@@ -2484,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)
 {
@@ -2521,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);
        }
@@ -2913,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)
 {
@@ -2937,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))
@@ -3518,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: