JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
On terminal resize, clear the alt screen with its own cursor.
[st.git] / st.c
diff --git a/st.c b/st.c
index 263abaa..b37069c 100644 (file)
--- a/st.c
+++ b/st.c
@@ -250,7 +250,8 @@ typedef struct {
        XSetWindowAttributes attrs;
        int scr;
        bool isfixed; /* is fixed geometry? */
-       int fx, fy, fw, fh; /* fixed geometry */
+       int l, t; /* left and top offset */
+       int gm; /* geometry mask */
        int tw, th; /* tty width and height */
        int w, h; /* window width and height */
        int ch; /* char height */
@@ -374,7 +375,7 @@ static void tmoveto(int, int);
 static void tmoveato(int, int);
 static void tnew(int, int);
 static void tnewline(int);
-static void tputtab(bool);
+static void tputtab(int);
 static void tputc(char *, int);
 static void treset(void);
 static int tresize(int, int);
@@ -406,6 +407,7 @@ static void xdrawcursor(void);
 static void xinit(void);
 static void xloadcols(void);
 static int xsetcolorname(int, const char *);
+static int xgeommasktogravity(int);
 static int xloadfont(Font *, FcPattern *);
 static void xloadfonts(char *, double);
 static int xloadfontset(Font *);
@@ -1587,30 +1589,32 @@ tclearregion(int x1, int y1, int x2, int y2) {
 void
 tdeletechar(int n) {
        int dst, src, size;
+       Glyph *line;
 
        LIMIT(n, 0, term.col - term.c.x);
 
        dst = term.c.x;
        src = term.c.x + n;
        size = term.col - src;
+       line = term.line[term.c.y];
 
-       memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
-               size * sizeof(Glyph));
+       memmove(&line[dst], &line[src], size * sizeof(Glyph));
        tclearregion(term.col-n, term.c.y, term.col-1, term.c.y);
 }
 
 void
 tinsertblank(int n) {
        int dst, src, size;
+       Glyph *line;
 
        LIMIT(n, 0, term.col - term.c.x);
 
        dst = term.c.x + n;
        src = term.c.x;
        size = term.col - dst;
+       line = term.line[term.c.y];
 
-       memmove(&term.line[term.c.y][dst], &term.line[term.c.y][src],
-               size * sizeof(Glyph));
+       memmove(&line[dst], &line[src], size * sizeof(Glyph));
        tclearregion(src, term.c.y, dst - 1, term.c.y);
 }
 
@@ -1992,8 +1996,7 @@ csihandle(void) {
                break;
        case 'I': /* CHT -- Cursor Forward Tabulation <n> tab stops */
                DEFAULT(csiescseq.arg[0], 1);
-               while(csiescseq.arg[0]--)
-                       tputtab(1);
+               tputtab(csiescseq.arg[0]);
                break;
        case 'J': /* ED -- Clear screen */
                selclear(NULL);
@@ -2061,8 +2064,7 @@ csihandle(void) {
                break;
        case 'Z': /* CBT -- Cursor Backward Tabulation <n> tab stops */
                DEFAULT(csiescseq.arg[0], 1);
-               while(csiescseq.arg[0]--)
-                       tputtab(0);
+               tputtab(-csiescseq.arg[0]);
                break;
        case 'd': /* VPA -- Move to <row> */
                DEFAULT(csiescseq.arg[0], 1);
@@ -2277,19 +2279,17 @@ tdump(void) {
 }
 
 void
-tputtab(bool forward) {
+tputtab(int n) {
        uint x = term.c.x;
 
-       if(forward) {
-               if(x == term.col)
-                       return;
-               for(++x; x < term.col && !term.tabs[x]; ++x)
-                       /* nothing */ ;
-       } else {
-               if(x == 0)
-                       return;
-               for(--x; x > 0 && !term.tabs[x]; --x)
-                       /* nothing */ ;
+       if(n > 0) {
+               while(x < term.col && n--)
+                       for(++x; x < term.col && !term.tabs[x]; ++x)
+                               /* nothing */ ;
+       } else if(n < 0) {
+               while(x > 0 && n++)
+                       for(--x; x > 0 && !term.tabs[x]; --x)
+                               /* nothing */ ;
        }
        tmoveto(x, term.c.y);
 }
@@ -2674,7 +2674,9 @@ tresize(int col, int row) {
                if(0 < col && minrow < row) {
                        tclearregion(0, minrow, col - 1, row - 1);
                }
+               tcursor(CURSOR_SAVE);
                tswapscreen();
+               tcursor(CURSOR_LOAD);
        } while(orig != term.line);
 
        return (slide > 0);
@@ -2747,10 +2749,10 @@ int
 xsetcolorname(int x, const char *name) {
        XRenderColor color = { .alpha = 0xffff };
        Colour colour;
-       if (x < 0 || x > LEN(colorname))
+       if(!BETWEEN(x, 0, LEN(colorname)))
                return -1;
        if(!name) {
-               if(16 <= x && x < 16 + 216) {
+               if(BETWEEN(x, 16, 16 + 215)) {
                        int r = (x - 16) / 36, g = ((x - 16) % 36) / 6, b = (x - 16) % 6;
                        color.red = sixd_to_16bit(r);
                        color.green = sixd_to_16bit(g);
@@ -2759,7 +2761,7 @@ xsetcolorname(int x, const char *name) {
                                return 0; /* something went wrong */
                        dc.col[x] = colour;
                        return 1;
-               } else if (16 + 216 <= x && x < 256) {
+               } else if(BETWEEN(x, 16 + 216, 255)) {
                        color.red = color.green = color.blue = 0x0808 + 0x0a0a * (x - (16 + 216));
                        if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour))
                                return 0; /* something went wrong */
@@ -2802,18 +2804,24 @@ xhints(void) {
        XSizeHints *sizeh = NULL;
 
        sizeh = XAllocSizeHints();
-       if(xw.isfixed == False) {
-               sizeh->flags = PSize | PResizeInc | PBaseSize;
-               sizeh->height = xw.h;
-               sizeh->width = xw.w;
-               sizeh->height_inc = xw.ch;
-               sizeh->width_inc = xw.cw;
-               sizeh->base_height = 2 * borderpx;
-               sizeh->base_width = 2 * borderpx;
-       } else {
-               sizeh->flags = PMaxSize | PMinSize;
-               sizeh->min_width = sizeh->max_width = xw.fw;
-               sizeh->min_height = sizeh->max_height = xw.fh;
+
+       sizeh->flags = PSize | PResizeInc | PBaseSize;
+       sizeh->height = xw.h;
+       sizeh->width = xw.w;
+       sizeh->height_inc = xw.ch;
+       sizeh->width_inc = xw.cw;
+       sizeh->base_height = 2 * borderpx;
+       sizeh->base_width = 2 * borderpx;
+       if(xw.isfixed == True) {
+               sizeh->flags |= PMaxSize | PMinSize;
+               sizeh->min_width = sizeh->max_width = xw.w;
+               sizeh->min_height = sizeh->max_height = xw.h;
+       }
+       if(xw.gm & (XValue|YValue)) {
+               sizeh->flags |= USPosition | PWinGravity;
+               sizeh->x = xw.l;
+               sizeh->y = xw.t;
+               sizeh->win_gravity = xgeommasktogravity(xw.gm);
        }
 
        XSetWMProperties(xw.dpy, xw.win, NULL, NULL, NULL, 0, sizeh, &wm,
@@ -2822,6 +2830,19 @@ xhints(void) {
 }
 
 int
+xgeommasktogravity(int mask) {
+       switch(mask & (XNegative|YNegative)) {
+       case 0:
+               return NorthWestGravity;
+       case XNegative:
+               return NorthEastGravity;
+       case YNegative:
+               return SouthWestGravity;
+       }
+       return SouthEastGravity;
+}
+
+int
 xloadfont(Font *f, FcPattern *pattern) {
        FcPattern *match;
        FcResult result;
@@ -2966,7 +2987,6 @@ xinit(void) {
        XGCValues gcvalues;
        Cursor cursor;
        Window parent;
-       int sw, sh;
        pid_t thispid = getpid();
 
        if(!(xw.dpy = XOpenDisplay(NULL)))
@@ -2986,23 +3006,12 @@ xinit(void) {
        xloadcols();
 
        /* adjust fixed window geometry */
-       if(xw.isfixed) {
-               sw = DisplayWidth(xw.dpy, xw.scr);
-               sh = DisplayHeight(xw.dpy, xw.scr);
-               if(xw.fx < 0)
-                       xw.fx = sw + xw.fx - xw.fw - 1;
-               if(xw.fy < 0)
-                       xw.fy = sh + xw.fy - xw.fh - 1;
-
-               xw.h = xw.fh;
-               xw.w = xw.fw;
-       } else {
-               /* window - default size */
-               xw.h = 2 * borderpx + term.row * xw.ch;
-               xw.w = 2 * borderpx + term.col * xw.cw;
-               xw.fx = 0;
-               xw.fy = 0;
-       }
+       xw.w = 2 * borderpx + term.col * xw.cw;
+       xw.h = 2 * borderpx + term.row * xw.ch;
+       if(xw.gm & XNegative)
+               xw.l += DisplayWidth(xw.dpy, xw.scr) - xw.w - 2;
+       if(xw.gm & YNegative)
+               xw.t += DisplayWidth(xw.dpy, xw.scr) - xw.h - 2;
 
        /* Events */
        xw.attrs.background_pixel = dc.col[defaultbg].pixel;
@@ -3015,7 +3024,7 @@ xinit(void) {
 
        parent = opt_embed ? strtol(opt_embed, NULL, 0) : \
                        XRootWindow(xw.dpy, xw.scr);
-       xw.win = XCreateWindow(xw.dpy, parent, xw.fx, xw.fy,
+       xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
                        xw.w, xw.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
                        xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
                        | CWEventMask | CWColormap, &xw.attrs);
@@ -3446,7 +3455,7 @@ drawregion(int x1, int y1, int x2, int y2) {
        bool ena_sel = sel.ob.x != -1;
        long unicodep;
 
-       if(sel.alt ^ IS_SET(MODE_ALTSCREEN))
+       if(sel.alt != IS_SET(MODE_ALTSCREEN))
                ena_sel = 0;
 
        if(!(xw.state & WIN_VISIBLE))
@@ -3712,10 +3721,7 @@ run(void) {
        }
 
        ttynew();
-       if(!xw.isfixed)
-               cresize(w, h);
-       else
-               cresize(xw.fw, xw.fh);
+       cresize(w, h);
 
        gettimeofday(&last, NULL);
        lastblink = last;
@@ -3805,11 +3811,10 @@ usage(void) {
 
 int
 main(int argc, char *argv[]) {
-       int bitm, xr, yr;
-       uint wr, hr;
        char *titles;
+       uint cols = 80, rows = 24;
 
-       xw.fw = xw.fh = xw.fx = xw.fy = 0;
+       xw.l = xw.t = 0;
        xw.isfixed = False;
 
        ARGBEGIN {
@@ -3833,22 +3838,11 @@ main(int argc, char *argv[]) {
                opt_font = EARGF(usage());
                break;
        case 'g':
-               bitm = XParseGeometry(EARGF(usage()), &xr, &yr, &wr, &hr);
-               if(bitm & XValue)
-                       xw.fx = xr;
-               if(bitm & YValue)
-                       xw.fy = yr;
-               if(bitm & WidthValue)
-                       xw.fw = (int)wr;
-               if(bitm & HeightValue)
-                       xw.fh = (int)hr;
-               if(bitm & XNegative && xw.fx == 0)
-                       xw.fx = -1;
-               if(bitm & YNegative && xw.fy == 0)
-                       xw.fy = -1;
-
-               if(xw.fh != 0 && xw.fw != 0)
-                       xw.isfixed = True;
+               xw.gm = XParseGeometry(EARGF(usage()),
+                               &xw.l, &xw.t, &cols, &rows);
+               break;
+       case 'i':
+               xw.isfixed = True;
                break;
        case 'o':
                opt_io = EARGF(usage());
@@ -3867,7 +3861,7 @@ main(int argc, char *argv[]) {
 run:
        setlocale(LC_CTYPE, "");
        XSetLocaleModifiers("");
-       tnew(80, 24);
+       tnew(cols? cols : 1, rows? rows : 1);
        xinit();
        selinit();
        run();