Add selected arg to the view draw operation
[tig] / tig.c
diff --git a/tig.c b/tig.c
index 29f64da..fb7a9a6 100644 (file)
--- a/tig.c
+++ b/tig.c
@@ -1213,13 +1213,15 @@ struct view_ops {
        /* What type of content being displayed. Used in the title bar. */
        const char *type;
        /* Draw one line; @lineno must be < view->height. */
-       bool (*draw)(struct view *view, struct line *line, unsigned int lineno);
+       bool (*draw)(struct view *view, struct line *line, unsigned int lineno, bool selected);
        /* Read one line; updates view->line. */
        bool (*read)(struct view *view, char *data);
        /* Depending on view, change display based on current line. */
        bool (*enter)(struct view *view, struct line *line);
        /* Search for regex in a line. */
        bool (*grep)(struct view *view, struct line *line);
+       /* Select line */
+       void (*select)(struct view *view, struct line *line);
 };
 
 static struct view_ops pager_ops;
@@ -1255,12 +1257,20 @@ static struct view views[] = {
 static bool
 draw_view_line(struct view *view, unsigned int lineno)
 {
+       struct line *line;
+       bool selected = (view->offset + lineno == view->lineno);
+
        assert(view_is_displayed(view));
 
        if (view->offset + lineno >= view->lines)
                return FALSE;
 
-       return view->ops->draw(view, &view->line[view->offset + lineno], lineno);
+       line = &view->line[view->offset + lineno];
+
+       if (selected)
+               view->ops->select(view, line);
+
+       return view->ops->draw(view, line, lineno, selected);
 }
 
 static void
@@ -2199,7 +2209,7 @@ view_driver(struct view *view, enum request request)
  */
 
 static bool
-pager_draw(struct view *view, struct line *line, unsigned int lineno)
+pager_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
 {
        char *text = line->data;
        enum line_type type = line->type;
@@ -2208,16 +2218,7 @@ pager_draw(struct view *view, struct line *line, unsigned int lineno)
 
        wmove(view->win, lineno, 0);
 
-       if (view->offset + lineno == view->lineno) {
-               if (type == LINE_COMMIT) {
-                       string_copy(view->ref, text + 7);
-                       string_copy(ref_commit, view->ref);
-
-               } else if (type == LINE_TREE_DIR || type == LINE_TREE_FILE) {
-                       string_ncopy(view->ref, text + STRING_SIZE("100644 blob "), 40);
-                       string_copy(ref_blob, view->ref);
-               }
-
+       if (selected) {
                type = LINE_CURSOR;
                wchgat(view->win, -1, 0, type, NULL);
        }
@@ -2418,12 +2419,24 @@ pager_grep(struct view *view, struct line *line)
        return TRUE;
 }
 
+static void
+pager_select(struct view *view, struct line *line)
+{
+       if (line->type == LINE_COMMIT) {
+               char *text = line->data;
+
+               string_copy(view->ref, text + STRING_SIZE("commit "));
+               string_copy(ref_commit, view->ref);
+       }
+}
+
 static struct view_ops pager_ops = {
        "line",
        pager_draw,
        pager_read,
        pager_enter,
        pager_grep,
+       pager_select,
 };
 
 
@@ -2603,12 +2616,24 @@ tree_enter(struct view *view, struct line *line)
        return TRUE;
 }
 
+static void
+tree_select(struct view *view, struct line *line)
+{
+       if (line->type == LINE_TREE_DIR || line->type == LINE_TREE_FILE) {
+               char *text = line->data;
+
+               string_ncopy(view->ref, text + STRING_SIZE("100644 blob "), 40);
+               string_copy(ref_blob, view->ref);
+       }
+}
+
 static struct view_ops tree_ops = {
        "file",
        pager_draw,
        tree_read,
        tree_enter,
        pager_grep,
+       tree_select,
 };
 
 static bool
@@ -2628,6 +2653,7 @@ static struct view_ops blob_ops = {
        blob_read,
        pager_enter,
        pager_grep,
+       pager_select,
 };
 
 
@@ -2646,7 +2672,7 @@ struct commit {
 };
 
 static bool
-main_draw(struct view *view, struct line *line, unsigned int lineno)
+main_draw(struct view *view, struct line *line, unsigned int lineno, bool selected)
 {
        char buf[DATE_COLS + 1];
        struct commit *commit = line->data;
@@ -2661,9 +2687,7 @@ main_draw(struct view *view, struct line *line, unsigned int lineno)
 
        wmove(view->win, lineno, col);
 
-       if (view->offset + lineno == view->lineno) {
-               string_copy(view->ref, commit->id);
-               string_copy(ref_commit, view->ref);
+       if (selected) {
                type = LINE_CURSOR;
                wattrset(view->win, get_line_attr(type));
                wchgat(view->win, -1, 0, type, NULL);
@@ -2895,12 +2919,22 @@ main_grep(struct view *view, struct line *line)
        return FALSE;
 }
 
+static void
+main_select(struct view *view, struct line *line)
+{
+       struct commit *commit = line->data;
+
+       string_copy(view->ref, commit->id);
+       string_copy(ref_commit, view->ref);
+}
+
 static struct view_ops main_ops = {
        "commit",
        main_draw,
        main_read,
        main_enter,
        main_grep,
+       main_select,
 };