Code

prepare_update_file: assume file is relative to root directory
[tig.git] / tig.c
diff --git a/tig.c b/tig.c
index d2dff15fd17bcd60c6a56209a420f33c9fc37a5a..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
 
@@ -442,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)
 {
@@ -1007,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;
@@ -1016,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]                = "";
@@ -1741,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);
@@ -2214,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;
+       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);
 }
@@ -2498,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)
@@ -2523,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);
        }
@@ -3051,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
@@ -3329,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;
@@ -3345,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
@@ -3535,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:
@@ -4442,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);
 }
@@ -4468,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;
 
@@ -5420,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[] = {
@@ -5928,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:
@@ -6210,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: