Warn about nothing to enter if REQ_ENTER reaches the view_driver switch
[tig] / tig.c
diff --git a/tig.c b/tig.c
index f7e9714..7c380e4 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -337,7 +337,8 @@ sq_quote(char buf[SIZEOF_STR], size_t bufsize, const char *src)
        REQ_(STOP_LOADING,      "Stop all loading views"), \
        REQ_(TOGGLE_LINENO,     "Toggle line numbers"), \
        REQ_(TOGGLE_REV_GRAPH,  "Toggle revision graph visualization"), \
-       REQ_(STATUS_UPDATE,     "Update file status") \
+       REQ_(STATUS_UPDATE,     "Update file status"), \
+       REQ_(EDIT,              "Open in editor")
 
 
 /* User action requests. */
@@ -424,6 +425,7 @@ static iconv_t opt_iconv            = ICONV_NONE;
 static char opt_search[SIZEOF_STR]     = "";
 static char opt_cdup[SIZEOF_STR]       = "";
 static char opt_git_dir[SIZEOF_STR]    = "";
+static char opt_editor[SIZEOF_STR]     = "";
 
 enum option_type {
        OPT_NONE,
@@ -767,6 +769,7 @@ static struct keybinding default_keybindings[] = {
        { 'g',          REQ_TOGGLE_REV_GRAPH },
        { ':',          REQ_PROMPT },
        { 'u',          REQ_STATUS_UPDATE },
+       { 'e',          REQ_EDIT },
 
        /* Using the ncurses SIGWINCH handler. */
        { KEY_RESIZE,   REQ_SCREEN_RESIZE },
@@ -2117,6 +2120,32 @@ open_view(struct view *prev, enum request request, enum open_flags flags)
                update_view_title(view);
 }
 
+static void
+open_editor(struct view *view, char *file)
+{
+       char cmd[SIZEOF_STR];
+       char file_sq[SIZEOF_STR];
+       char *editor;
+
+       editor = getenv("GIT_EDITOR");
+       if (!editor && *opt_editor)
+               editor = opt_editor;
+       if (!editor)
+               editor = getenv("VISUAL");
+       if (!editor)
+               editor = getenv("EDITOR");
+       if (!editor)
+               editor = "vi";
+
+       if (sq_quote(file_sq, 0, file) < sizeof(file_sq) &&
+           string_format(cmd, "%s %s", editor, file_sq)) {
+               def_prog_mode();           /* save current tty modes */
+               endwin();                  /* restore original tty modes */
+               system(cmd);
+               reset_prog_mode();
+               redraw_display();
+       }
+}
 
 /*
  * User request switch noodle
@@ -2262,6 +2291,13 @@ view_driver(struct view *view, enum request request)
                redraw_display();
                break;
 
+       case REQ_EDIT:
+               report("Nothing to edit");
+               break;
+
+       case REQ_ENTER:
+               report("Nothing to enter");
+               break;
 
        case REQ_NONE:
                doupdate();
@@ -3020,24 +3056,14 @@ status_draw(struct view *view, struct line *line, unsigned int lineno, bool sele
        return TRUE;
 }
 
-static void status_update(struct view *view);
-
 static enum request
-status_request(struct view *view, enum request request, struct line *line)
+status_enter(struct view *view, struct line *line)
 {
        struct status *status = line->data;
        char path[SIZEOF_STR] = "";
        char *info;
        size_t cmdsize = 0;
 
-       if (request == REQ_STATUS_UPDATE) {
-               status_update(view);
-               return REQ_NONE;
-       }
-
-       if (request != REQ_ENTER)
-               return request;
-
        if (line->type == LINE_STAT_NONE ||
            (!status && line[1].type == LINE_STAT_NONE)) {
                report("No file to diff");
@@ -3099,6 +3125,7 @@ status_request(struct view *view, enum request request, struct line *line)
        return REQ_NONE;
 }
 
+
 static bool
 status_update_file(struct view *view, struct status *status, enum line_type type)
 {
@@ -3178,6 +3205,34 @@ status_update(struct view *view)
        open_view(view, REQ_VIEW_STATUS, OPEN_RELOAD);
 }
 
+static enum request
+status_request(struct view *view, enum request request, struct line *line)
+{
+       struct status *status = line->data;
+
+       switch (request) {
+       case REQ_STATUS_UPDATE:
+               status_update(view);
+               break;
+
+       case REQ_EDIT:
+               if (!status)
+                       return request;
+
+               open_editor(view, status->name);
+               break;
+
+       case REQ_ENTER:
+               status_enter(view, line);
+               break;
+
+       default:
+               return request;
+       }
+
+       return REQ_NONE;
+}
+
 static void
 status_select(struct view *view, struct line *line)
 {
@@ -4192,6 +4247,9 @@ read_repo_config_option(char *name, size_t namelen, char *value, size_t valuelen
        if (!strcmp(name, "i18n.commitencoding"))
                string_ncopy(opt_encoding, value, valuelen);
 
+       if (!strcmp(name, "core.editor"))
+               string_ncopy(opt_editor, value, valuelen);
+
        return OK;
 }