JasonWoof Got questions, comments, patches, etc.? Contact Jason Woofenden
Copy non set positions as spaces
[st.git] / st.c
diff --git a/st.c b/st.c
index d5ecf61..b2e5e22 100644 (file)
--- a/st.c
+++ b/st.c
@@ -324,6 +324,7 @@ static int isfullutf8(char *, int);
 
 static void *xmalloc(size_t);
 static void *xrealloc(void *, size_t);
+static void *xcalloc(size_t nmemb, size_t size);
 
 static void (*handler[LASTEvent])(XEvent *) = {
        [KeyPress] = kpress,
@@ -362,14 +363,22 @@ void *
 xmalloc(size_t len) {
        void *p = malloc(len);
        if(!p)
-               die("Out of memory");
+               die("Out of memory\n");
        return p;
 }
 
 void *
 xrealloc(void *p, size_t len) {
        if((p = realloc(p, len)) == NULL)
-               die("Out of memory");
+               die("Out of memory\n");
+       return p;
+}
+
+void *
+xcalloc(size_t nmemb, size_t size) {
+       void *p = calloc(nmemb, size);
+       if(!p)
+               die("Out of memory\n");
        return p;
 }
 
@@ -587,14 +596,17 @@ selcopy(void) {
                /* append every set & selected glyph to the selection */
                for(y = 0; y < term.row; y++) {
                        for(x = 0; x < term.col; x++) {
-                               is_selected = selected(x, y);
-                               if((term.line[y][x].state & GLYPH_SET) && is_selected) {
-                                       int size = utf8size(term.line[y][x].c);
-                                       memcpy(ptr, term.line[y][x].c, size);
-                                       ptr += size;
-                               }
+                               int size;
+                               char *p;
+                               Glyph *gp = &term.line[y][x];
+
+                               if(!(is_selected = selected(x, y)))
+                                       continue;
+                               p = (gp->state & GLYPH_SET) ? gp->c : " ";
+                               size = utf8size(p);
+                               memcpy(ptr, p, size);
+                               ptr += size;
                        }
-
                        /* \n at the end of every selected line except for the last one */
                        if(is_selected && y < sel.e.y)
                                *ptr++ = '\n';
@@ -1801,8 +1813,8 @@ tresize(int col, int row) {
        /* allocate any new rows */
        for(/* i == minrow */; i < row; i++) {
                term.dirty[i] = 1;
-               term.line[i] = calloc(col, sizeof(Glyph));
-               term.alt [i] = calloc(col, sizeof(Glyph));
+               term.line[i] = xcalloc(col, sizeof(Glyph));
+               term.alt [i] = xcalloc(col, sizeof(Glyph));
        }
        if(col > term.col) {
                bool *bp = term.tabs + term.col;