JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Simplify selected().
[st.git] / st.c
diff --git a/st.c b/st.c
index 7afff39..5198749 100644 (file)
--- a/st.c
+++ b/st.c
@@ -399,7 +399,6 @@ static int32_t tdefcolor(int *, int *, int);
 static void tselcs(void);
 static void tdeftran(char);
 static inline bool match(uint, uint);
-static void dump(char c);
 static void ttynew(void);
 static void ttyread(void);
 static void ttyresize(void);
@@ -675,18 +674,13 @@ selsort(void) {
 
 static inline bool
 selected(int x, int y) {
-       if(sel.ne.y == y && sel.nb.y == y)
-               return BETWEEN(x, sel.nb.x, sel.ne.x);
+       if(sel.type == SEL_RECTANGULAR)
+               return BETWEEN(y, sel.nb.y, sel.ne.y)
+                   && BETWEEN(x, sel.nb.x, sel.ne.x);
 
-       if(sel.type == SEL_RECTANGULAR) {
-               return ((sel.nb.y <= y && y <= sel.ne.y)
-                       && (sel.nb.x <= x && x <= sel.ne.x));
-       }
-
-       return ((sel.nb.y < y && y < sel.ne.y)
-               || (y == sel.ne.y && x <= sel.ne.x))
-               || (y == sel.nb.y && x >= sel.nb.x
-                       && (x <= sel.ne.x || sel.nb.y != sel.ne.y));
+       return BETWEEN(y, sel.nb.y, sel.ne.y)
+           && (y != sel.nb.y || x >= sel.nb.x)
+           && (y != sel.ne.y || x <= sel.ne.x);
 }
 
 void
@@ -923,60 +917,59 @@ getsel(void) {
        int x, y, bufsize, size, i, ex;
        Glyph *gp, *last;
 
-       if(sel.ob.x == -1) {
-               str = NULL;
-       } else {
-               bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
-               ptr = str = xmalloc(bufsize);
+       if(sel.ob.x == -1)
+               return NULL;
 
-               /* append every set & selected glyph to the selection */
-               for(y = sel.nb.y; y < sel.ne.y + 1; y++) {
-                       gp = &term.line[y][0];
-                       last = &gp[term.col-1];
+       bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ;
+       ptr = str = xmalloc(bufsize);
 
-                       while(last >= gp && !(selected(last - gp, y) &&
-                                             strcmp(last->c, " ") != 0)) {
-                               --last;
-                       }
+       /* append every set & selected glyph to the selection */
+       for(y = sel.nb.y; y < sel.ne.y + 1; y++) {
+               gp = &term.line[y][0];
+               last = &gp[term.col-1];
 
-                       for(x = 0; gp <= last; x++, ++gp) {
-                               if(!selected(x, y) || (gp->mode & ATTR_WDUMMY))
-                                       continue;
+               while(last >= gp && !(selected(last - gp, y) &&
+                                     strcmp(last->c, " ") != 0)) {
+                       --last;
+               }
 
-                               size = utf8len(gp->c);
-                               memcpy(ptr, gp->c, size);
-                               ptr += size;
-                       }
+               for(x = 0; gp <= last; x++, ++gp) {
+                       if(!selected(x, y) || (gp->mode & ATTR_WDUMMY))
+                               continue;
 
-                       /*
-                        * Copy and pasting of line endings is inconsistent
-                        * in the inconsistent terminal and GUI world.
-                        * The best solution seems like to produce '\n' when
-                        * something is copied from st and convert '\n' to
-                        * '\r', when something to be pasted is received by
-                        * st.
-                        * FIXME: Fix the computer world.
-                        */
-                       if(y < sel.ne.y && x > 0 && !((gp-1)->mode & ATTR_WRAP))
-                               *ptr++ = '\n';
+                       size = utf8len(gp->c);
+                       memcpy(ptr, gp->c, size);
+                       ptr += size;
+               }
 
-                       /*
-                        * If the last selected line expands in the selection
-                        * 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)
-                                       *ptr++ = '\n';
-                       }
+               /*
+                * Copy and pasting of line endings is inconsistent
+                * in the inconsistent terminal and GUI world.
+                * The best solution seems like to produce '\n' when
+                * something is copied from st and convert '\n' to
+                * '\r', when something to be pasted is received by
+                * st.
+                * FIXME: Fix the computer world.
+                */
+               if(y < sel.ne.y && x > 0 && !((gp-1)->mode & ATTR_WRAP))
+                       *ptr++ = '\n';
+
+               /*
+                * If the last selected line expands in the selection
+                * 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)
+                               *ptr++ = '\n';
                }
-               *ptr = 0;
        }
+       *ptr = 0;
        return str;
 }
 
@@ -1243,15 +1236,6 @@ ttynew(void) {
 }
 
 void
-dump(char c) {
-       static int col;
-
-       fprintf(stderr, " %02x '%c' ", c, isprint(c)?c:'.');
-       if(++col % 10 == 0)
-               fprintf(stderr, "\n");
-}
-
-void
 ttyread(void) {
        static char buf[BUFSIZ];
        static int buflen = 0;
@@ -3004,13 +2988,9 @@ xunloadfont(Font *f) {
 
 void
 xunloadfonts(void) {
-       int i;
-
        /* Free the loaded fonts in the font cache.  */
-       for(i = 0; i < frclen; i++) {
-               XftFontClose(xw.dpy, frc[i].font);
-       }
-       frclen = 0;
+       while(frclen > 0)
+               XftFontClose(xw.dpy, frc[--frclen].font);
 
        xunloadfont(&dc.font);
        xunloadfont(&dc.bfont);