JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
tiny cleanup
[st.git] / st.c
diff --git a/st.c b/st.c
index 79a4e0a..2e55963 100644 (file)
--- a/st.c
+++ b/st.c
@@ -179,8 +179,8 @@ typedef unsigned long ulong;
 typedef unsigned short ushort;
 
 typedef XftDraw *Draw;
-typedef XftColor Colour;
-typedef Colormap Colourmap;
+typedef XftColor Color;
+typedef Colormap Colormap;
 
 typedef struct {
        char c[UTF_SIZ]; /* character code */
@@ -241,7 +241,7 @@ typedef struct {
 /* Purely graphic info */
 typedef struct {
        Display *dpy;
-       Colourmap cmap;
+       Colormap cmap;
        Window win;
        Drawable buf;
        Atom xembed, wmdeletewin, netwmname, netwmpid;
@@ -340,7 +340,7 @@ typedef struct {
 
 /* Drawing Context */
 typedef struct {
-       Colour col[MAX(LEN(colorname), 256)];
+       Color col[MAX(LEN(colorname), 256)];
        Font font, bfont, ifont, ibfont;
        GC gc;
 } DC;
@@ -441,7 +441,7 @@ static void selclear(XEvent *);
 static void selrequest(XEvent *);
 
 static void selinit(void);
-static void selsort(void);
+static void selnormalize(void);
 static inline bool selected(int, int);
 static char *getsel(void);
 static void selcopy(void);
@@ -657,8 +657,19 @@ y2row(int y) {
        return LIMIT(y, 0, term.row-1);
 }
 
+static int tlinelen(int y) {
+       int i = term.col;
+
+       while (i > 0 && term.line[y][i - 1].c[0] == ' ')
+               --i;
+
+       return i;
+}
+
 static void
-selsort(void) {
+selnormalize(void) {
+       int i;
+
        if(sel.ob.y == sel.oe.y) {
                sel.nb.x = MIN(sel.ob.x, sel.oe.x);
                sel.ne.x = MAX(sel.ob.x, sel.oe.x);
@@ -668,6 +679,15 @@ selsort(void) {
        }
        sel.nb.y = MIN(sel.ob.y, sel.oe.y);
        sel.ne.y = MAX(sel.ob.y, sel.oe.y);
+
+       /* expand selection over line breaks */
+       if (sel.type == SEL_RECTANGULAR)
+               return;
+       i = tlinelen(sel.nb.y);
+       if (i < sel.nb.x)
+               sel.nb.x = i;
+       if (tlinelen(sel.ne.y) <= sel.ne.x)
+               sel.ne.x = term.col - 1;
 }
 
 static inline bool
@@ -683,8 +703,6 @@ selected(int x, int y) {
 
 void
 selsnap(int mode, int *x, int *y, int direction) {
-       int i;
-
        switch(mode) {
        case SNAP_WORD:
                /*
@@ -716,7 +734,7 @@ selsnap(int mode, int *x, int *y, int direction) {
                                continue;
                        }
 
-                       if(strchr(worddelimiters,
+                       if(*x >= tlinelen(*y) || strchr(worddelimiters,
                                        term.line[*y][*x+direction].c[0])) {
                                break;
                        }
@@ -747,18 +765,6 @@ selsnap(int mode, int *x, int *y, int direction) {
                        }
                }
                break;
-       default:
-               /*
-                * Select the whole line when the end of line is reached.
-                */
-               if(direction > 0) {
-                       i = term.col;
-                       while(--i > 0 && term.line[*y][i].c[0] == ' ')
-                               /* nothing */;
-                       if(i > 0 && i < *x)
-                               *x = term.col - 1;
-               }
-               break;
        }
 }
 
@@ -780,7 +786,7 @@ getbuttoninfo(XEvent *e) {
                selsnap(sel.snap, &sel.oe.x, &sel.oe.y, -1);
                selsnap(sel.snap, &sel.ob.x, &sel.ob.y, +1);
        }
-       selsort();
+       selnormalize();
 
        sel.type = SEL_REGULAR;
        for(type = 1; type < LEN(selmasks); ++type) {
@@ -896,7 +902,7 @@ bpress(XEvent *e) {
                }
                selsnap(sel.snap, &sel.ob.x, &sel.ob.y, -1);
                selsnap(sel.snap, &sel.oe.x, &sel.oe.y, +1);
-               selsort();
+               selnormalize();
 
                /*
                 * Draw selection, unless it's regular and we don't want to
@@ -914,7 +920,7 @@ bpress(XEvent *e) {
 char *
 getsel(void) {
        char *str, *ptr;
-       int x, y, bufsize, size, i, ex;
+       int x, y, bufsize, size, ex;
        Glyph *gp, *last;
 
        if(sel.ob.x == -1)
@@ -959,13 +965,10 @@ getsel(void) {
                 * after the visible text '\n' is appended.
                 */
                if(y == sel.ne.y) {
-                       i = term.col;
-                       while(--i > 0 && term.line[y][i].c[0] == ' ')
-                               /* nothing */;
                        ex = sel.ne.x;
                        if(sel.nb.y == sel.ne.y && sel.ne.x < sel.nb.x)
                                ex = sel.nb.x;
-                       if(i < ex)
+                       if(tlinelen(y) < ex)
                                *ptr++ = '\n';
                }
        }
@@ -1451,7 +1454,7 @@ selscroll(int orig, int n) {
                                sel.oe.x = term.col;
                        }
                }
-               selsort();
+               selnormalize();
        }
 }
 
@@ -1631,7 +1634,7 @@ tdefcolor(int *attr, int *npar, int l) {
        uint r, g, b;
 
        switch (attr[*npar + 1]) {
-       case 2: /* direct colour in RGB space */
+       case 2: /* direct color in RGB space */
                if (*npar + 4 >= l) {
                        fprintf(stderr,
                                "erresc(38): Incorrect number of parameters (%d)\n",
@@ -1648,7 +1651,7 @@ tdefcolor(int *attr, int *npar, int l) {
                else
                        idx = TRUECOLOR(r, g, b);
                break;
-       case 5: /* indexed colour */
+       case 5: /* indexed color */
                if (*npar + 2 >= l) {
                        fprintf(stderr,
                                "erresc(38): Incorrect number of parameters (%d)\n",
@@ -1663,8 +1666,8 @@ tdefcolor(int *attr, int *npar, int l) {
                break;
        case 0: /* implemented defined (only foreground) */
        case 1: /* transparent */
-       case 3: /* direct colour in CMY space */
-       case 4: /* direct colour in CMYK space */
+       case 3: /* direct color in CMY space */
+       case 4: /* direct color in CMYK space */
        default:
                fprintf(stderr,
                        "erresc(38): gfx attr %d unknown\n", attr[*npar]);
@@ -2151,7 +2154,7 @@ strhandle(void) {
                        /* FALLTHROUGH */
                case 104: /* color reset, here p = NULL */
                        j = (narg > 1) ? atoi(strescseq.args[1]) : -1;
-                       if (!xsetcolorname(j, p)) {
+                       if(xsetcolorname(j, p)) {
                                fprintf(stderr, "erresc: invalid color %s\n", p);
                        } else {
                                /*
@@ -2720,14 +2723,14 @@ xloadcols(void) {
        int i;
        XRenderColor color = { .alpha = 0xffff };
        static bool loaded;
-       Colour *cp;
+       Color *cp;
 
        if(loaded) {
                for (cp = dc.col; cp < dc.col + LEN(dc.col); ++cp)
                        XftColorFree(xw.dpy, xw.vis, xw.cmap, cp);
        }
 
-       /* load colours [0-15] and [256-LEN(colorname)] (config.h) */
+       /* load colors [0-15] and [256-LEN(colorname)] (config.h) */
        for(i = 0; i < LEN(colorname); i++) {
                if(!colorname[i])
                        continue;
@@ -2736,7 +2739,7 @@ xloadcols(void) {
                }
        }
 
-       /* load colours [16-231] ; same colours as xterm */
+       /* load colors [16-231] ; same colors as xterm */
        for(i = 16; i < 6*6*6+16; i++) {
                color.red   = sixd_to_16bit( ((i-16)/36)%6 );
                color.green = sixd_to_16bit( ((i-16)/6) %6 );
@@ -2745,7 +2748,7 @@ xloadcols(void) {
                        die("Could not allocate color %d\n", i);
        }
 
-       /* load colours [232-255] ; grayscale */
+       /* load colors [232-255] ; grayscale */
        for(; i < 256; i++) {
                color.red = color.green = color.blue = 0x0808 + 0x0a0a * (i-(6*6*6+16));
                if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &dc.col[i]))
@@ -2757,33 +2760,45 @@ xloadcols(void) {
 int
 xsetcolorname(int x, const char *name) {
        XRenderColor color = { .alpha = 0xffff };
-       Colour colour;
+       Color ncolor;
+
        if(!BETWEEN(x, 0, LEN(colorname)))
-               return -1;
+               return 1;
+
        if(!name) {
-               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);
-                       color.blue = sixd_to_16bit(b);
-                       if(!XftColorAllocValue(xw.dpy, xw.vis, xw.cmap, &color, &colour))
-                               return 0; /* something went wrong */
-                       dc.col[x] = colour;
-                       return 1;
-               } 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 */
-                       dc.col[x] = colour;
-                       return 1;
-               } else {
+               if(BETWEEN(x, 16, 16 + 215)) { /* 256 color */
+                       color.red   = sixd_to_16bit( ((x-16)/36)%6 );
+                       color.green = sixd_to_16bit( ((x-16)/6) %6 );
+                       color.blue  = sixd_to_16bit( ((x-16)/1) %6 );
+                       if(!XftColorAllocValue(xw.dpy, xw.vis,
+                                               xw.cmap, &color, &ncolor)) {
+                               return 1;
+                       }
+
+                       XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
+                       dc.col[x] = ncolor;
+                       return 0;
+               } else if(BETWEEN(x, 16 + 216, 255)) { /* greyscale */
+                       color.red = color.green = color.blue = \
+                                   0x0808 + 0x0a0a * (x - (16 + 216));
+                       if(!XftColorAllocValue(xw.dpy, xw.vis,
+                                               xw.cmap, &color, &ncolor)) {
+                               return 1;
+                       }
+
+                       XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
+                       dc.col[x] = ncolor;
+                       return 0;
+               } else { /* system colors */
                        name = colorname[x];
                }
        }
-       if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, &colour))
-               return 0;
-       dc.col[x] = colour;
-       return 1;
+       if(!XftColorAllocName(xw.dpy, xw.vis, xw.cmap, name, &ncolor))
+               return 1;
+
+       XftColorFree(xw.dpy, xw.vis, xw.cmap, &dc.col[x]);
+       dc.col[x] = ncolor;
+       return 0;
 }
 
 void
@@ -3099,7 +3114,7 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
        FcPattern *fcpattern, *fontpattern;
        FcFontSet *fcsets[] = { NULL };
        FcCharSet *fccharset;
-       Colour *fg, *bg, *temp, revfg, revbg, truefg, truebg;
+       Color *fg, *bg, *temp, revfg, revbg, truefg, truebg;
        XRenderColor colfg, colbg;
        XRectangle r;
        int oneatatime;
@@ -3145,8 +3160,8 @@ xdraws(char *s, Glyph base, int x, int y, int charlen, int bytelen) {
 
        if(base.mode & ATTR_BOLD) {
                /*
-                * change basic system colours [0-7]
-                * to bright system colours [8-15]
+                * change basic system colors [0-7]
+                * to bright system colors [8-15]
                 */
                if(BETWEEN(base.fg, 0, 7))
                        fg = &dc.col[base.fg + 8];