Move remaining selection mode logic into selextend
[st.git] / st.c
diff --git a/st.c b/st.c
index d4dfe6e..b7e215e 100644 (file)
--- a/st.c
+++ b/st.c
@@ -140,6 +140,7 @@ static void tscrollup(int, int);
 static void tscrolldown(int, int);
 static void tsetattr(int *, int);
 static void tsetchar(Rune, Glyph *, int, int);
+static void tsetdirt(int, int);
 static void tsetscroll(int, int);
 static void tswapscreen(void);
 static void tsetmode(int, int, int *, int);
@@ -166,11 +167,11 @@ static ssize_t xwrite(int, const char *, size_t);
 
 /* Globals */
 Term term;
-Selection sel;
 int cmdfd;
 pid_t pid;
 int oldbutton   = 3; /* button event on startup: 3 = release */
 
+static Selection sel;
 static CSIEscape csiescseq;
 static STREscape strescseq;
 static int iofd = 1;
@@ -365,13 +366,9 @@ base64dec(const char *src)
 void
 selinit(void)
 {
-       clock_gettime(CLOCK_MONOTONIC, &sel.tclick1);
-       clock_gettime(CLOCK_MONOTONIC, &sel.tclick2);
        sel.mode = SEL_IDLE;
        sel.snap = 0;
        sel.ob.x = -1;
-       sel.primary = NULL;
-       sel.clipboard = NULL;
 }
 
 int
@@ -388,6 +385,52 @@ tlinelen(int y)
        return i;
 }
 
+void
+selstart(int col, int row, int snap)
+{
+       selclear();
+       sel.mode = SEL_EMPTY;
+       sel.type = SEL_REGULAR;
+       sel.snap = snap;
+       sel.oe.x = sel.ob.x = col;
+       sel.oe.y = sel.ob.y = row;
+       selnormalize();
+
+       if (sel.snap != 0)
+               sel.mode = SEL_READY;
+       tsetdirt(sel.nb.y, sel.ne.y);
+}
+
+void
+selextend(int col, int row, int type, int done)
+{
+       int oldey, oldex, oldsby, oldsey, oldtype;
+
+       if (!sel.mode)
+               return;
+       if (done && sel.mode == SEL_EMPTY) {
+               selclear();
+               return;
+       }
+
+       oldey = sel.oe.y;
+       oldex = sel.oe.x;
+       oldsby = sel.nb.y;
+       oldsey = sel.ne.y;
+       oldtype = sel.type;
+
+       sel.alt = IS_SET(MODE_ALTSCREEN);
+       sel.oe.x = col;
+       sel.oe.y = row;
+       selnormalize();
+       sel.type = type;
+
+       if (oldey != sel.oe.y || oldex != sel.oe.x || oldtype != sel.type)
+               tsetdirt(MIN(sel.nb.y, oldsby), MAX(sel.ne.y, oldsey));
+
+       sel.mode = done ? SEL_IDLE : SEL_READY;
+}
+
 void
 selnormalize(void)
 {