Code

prepare_update_file: assume file is relative to root directory
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index 08b2993b7f8a664384c5f4812055162d85448682..77f06651a9acbe9c61c60794321cd19a79643016 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -107,6 +107,7 @@ static size_t utf8_length(const char **string, size_t col, int *width, size_t ma
 #define DATE_SHORT_COLS        STRING_SIZE("2006-04-29 ")
 
 #define ID_COLS                8
+#define AUTHOR_COLS    19
 
 #define MIN_VIEW_HEIGHT 4
 
@@ -297,6 +298,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 +334,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 +384,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 +443,46 @@ 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_
+};
+
+/* FIXME: Handle multi-byte and multi-column characters. */
+static const char *
+get_author_initials(const char *author, size_t max_columns)
+{
+       static char initials[AUTHOR_COLS];
+       size_t pos;
+
+#define is_initial_sep(c) (isspace(c) || ispunct(c) || (c) == '@' || (c) == '-')
+
+       memset(initials, 0, sizeof(initials));
+       for (pos = 0; *author && pos < sizeof(initials) - 1; author++, pos++) {
+               while (is_initial_sep(*author))
+                       author++;
+               strncpy(&initials[pos], author, sizeof(initials) - 1 - pos);
+               while (*author && author[1] && !is_initial_sep(author[1]))
+                       author++;
+       }
+
+       return initials;
+}
+
+
 static bool
 argv_from_string(const char *argv[SIZEOF_ARG], int *argc, char *cmd)
 {
@@ -990,8 +1035,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 +1048,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;
@@ -1013,7 +1057,7 @@ static int opt_num_interval               = 5;
 static double opt_hscroll              = 0.50;
 static double opt_scale_split_view     = 2.0 / 3.0;
 static int opt_tab_size                        = 8;
-static int opt_author_cols             = 19;
+static int opt_author_cols             = AUTHOR_COLS;
 static char opt_path[SIZEOF_STR]       = "";
 static char opt_file[SIZEOF_STR]       = "";
 static char opt_ref[SIZEOF_REF]                = "";
@@ -1152,8 +1196,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 +1782,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 +2255,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;
+       bool trim = opt_author_cols == 0 || opt_author_cols > 5;
+       bool abbreviate = opt_author == AUTHOR_ABBREVIATED || !trim;
 
-#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);
 }
@@ -2496,6 +2525,7 @@ toggle_enum_option_do(unsigned int *opt, const char *help,
        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,6 +2551,8 @@ open_option_menu(void)
        if (prompt_menu("Toggle option", menu, &selected)) {
                if (menu[selected].data == &opt_date)
                        toggle_date();
+               else if (menu[selected].data == &opt_author)
+                       toggle_author();
                else
                        toggle_view_option(menu[selected].data, menu[selected].text);
        }
@@ -2912,6 +2944,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)
 {
@@ -2936,27 +2994,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))
@@ -3037,7 +3081,7 @@ prepare_update_file(struct view *view, const char *name)
 {
        if (view->pipe)
                end_update(view, TRUE);
-       return io_open(&view->io, "%s", name);
+       return io_open(&view->io, "%s/%s", opt_cdup[0] ? opt_cdup : ".", name);
 }
 
 static bool
@@ -3315,7 +3359,7 @@ open_mergetool(const char *file)
 }
 
 static void
-open_editor(bool from_root, const char *file)
+open_editor(const char *file)
 {
        const char *editor_argv[] = { "vi", file, NULL };
        const char *editor;
@@ -3331,7 +3375,7 @@ open_editor(bool from_root, const char *file)
                editor = "vi";
 
        editor_argv[0] = editor;
-       open_external_viewer(editor_argv, from_root ? opt_cdup : NULL);
+       open_external_viewer(editor_argv, opt_cdup);
 }
 
 static void
@@ -3521,7 +3565,7 @@ view_driver(struct view *view, enum request request)
                break;
 
        case REQ_TOGGLE_AUTHOR:
-               toggle_view_option(&opt_author, "author display");
+               toggle_author();
                break;
 
        case REQ_TOGGLE_REV_GRAPH:
@@ -4428,7 +4472,7 @@ open_blob_editor()
        else if (!run_io_append(blob_ops.argv, FORMAT_ALL, fd))
                report("Failed to save blob data to file");
        else
-               open_editor(FALSE, file);
+               open_editor(file);
        if (fd != -1)
                unlink(file);
 }
@@ -4454,7 +4498,7 @@ tree_request(struct view *view, enum request request, struct line *line)
                } else if (!is_head_commit(view->vid)) {
                        open_blob_editor();
                } else {
-                       open_editor(TRUE, opt_file);
+                       open_editor(opt_file);
                }
                return REQ_NONE;
 
@@ -5406,7 +5450,7 @@ static const char *status_diff_files_argv[] = {
 };
 
 static const char *status_list_other_argv[] = {
-       "git", "ls-files", "-z", "--others", "--exclude-standard", NULL
+       "git", "ls-files", "-z", "--others", "--exclude-standard", opt_prefix, NULL
 };
 
 static const char *status_list_no_head_argv[] = {
@@ -5914,7 +5958,7 @@ status_request(struct view *view, enum request request, struct line *line)
                        return REQ_NONE;
                }
 
-               open_editor(status->status != '?', status->new.name);
+               open_editor(status->new.name);
                break;
 
        case REQ_VIEW_BLAME:
@@ -6196,7 +6240,7 @@ stage_request(struct view *view, enum request request, struct line *line)
                        return REQ_NONE;
                }
 
-               open_editor(stage_status.status != '?', stage_status.new.name);
+               open_editor(stage_status.new.name);
                break;
 
        case REQ_REFRESH: