--- st.c +++ st.c @@ -43,6 +44,8 @@ #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f)) #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c)) #define ISDELIM(u) (u && wcschr(worddelimiters, u)) +static inline int max(int a, int b) { return a > b ? a : b; } +static inline int min(int a, int b) { return a < b ? a : b; } enum term_mode { MODE_WRAP = 1 << 0, @@ -585,119 +664,47 @@ selected(int x, int y) return BETWEEN(y, sel.nb.y, sel.ne.y) && BETWEEN(x, sel.nb.x, sel.ne.x); - 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 -selsnap(int *x, int *y, int direction) -{ - int newx, newy, xt, yt; - int delim, prevdelim; - Glyph *gp, *prevgp; - - switch (sel.snap) { - case SNAP_WORD: - /* - * Snap around if the word wraps around at the end or - * beginning of a line. - */ - prevgp = &term.line[*y][*x]; - prevdelim = ISDELIM(prevgp->u); - for (;;) { - newx = *x + direction; - newy = *y; - if (!BETWEEN(newx, 0, term.col - 1)) { - newy += direction; - newx = (newx + term.col) % term.col; - if (!BETWEEN(newy, 0, term.row - 1)) - break; - - if (direction > 0) - yt = *y, xt = *x; - else - yt = newy, xt = newx; - if (!(term.line[yt][xt].mode & ATTR_WRAP)) - break; - } - - if (newx >= tlinelen(newy)) - break; - - gp = &term.line[newy][newx]; - delim = ISDELIM(gp->u); - if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim - || (delim && gp->u != prevgp->u))) - break; - - *x = newx; - *y = newy; - prevgp = gp; - prevdelim = delim; - } - break; - case SNAP_LINE: - /* - * Snap around if the the previous line or the current one - * has set ATTR_WRAP at its end. Then the whole next or - * previous line will be selected. - */ - *x = (direction < 0) ? 0 : term.col - 1; - if (direction < 0) { - for (; *y > 0; *y += direction) { - if (!(term.line[*y-1][term.col-1].mode - & ATTR_WRAP)) { - break; - } - } - } else if (direction > 0) { - for (; *y < term.row-1; *y += direction) { - if (!(term.line[*y][term.col-1].mode - & ATTR_WRAP)) { - break; - } - } - } - break; - } + return ((sel.nb.y > sel.ne.y) ? OUT(y, sel.nb.y, sel.ne.y) + : BETWEEN(y, sel.nb.y, sel.ne.y)) && + (y != sel.nb.y || x >= sel.nb.x) && + (y != sel.ne.y || x <= sel.ne.x); } char * getsel(void) { char *str, *ptr; - int y, bufsize, lastx, linelen; + int y, yy, bufsize, lastx; Glyph *gp, *last; if (sel.ob.x == -1) return NULL; - bufsize = (term.col+1) * (sel.ne.y-sel.nb.y+1) * UTF_SIZ; + int const start = sel.swap ? sel.oe.y : sel.ob.y, h = rows(); + int endy = (sel.swap ? sel.ob.y : sel.oe.y); + for (; endy < start; endy += h); + Line * const cbuf = IS_SET(MODE_ALTSCREEN) ? term.line : buf; + bufsize = (term.col+1) * (endy-start+1 ) * UTF_SIZ; + assert(bufsize > 0); ptr = str = xmalloc(bufsize); /* append every set & selected glyph to the selection */ - for (y = sel.nb.y; y <= sel.ne.y; y++) { - if ((linelen = tlinelen(y)) == 0) { - *ptr++ = '\n'; - continue; - } + for (y = start; y <= endy; y++) { + yy = y % h; if (sel.type == SEL_RECTANGULAR) { - gp = &term.line[y][sel.nb.x]; + gp = &cbuf[yy][sel.nb.x]; lastx = sel.ne.x; } else { - gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0]; - lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1; + gp = &cbuf[yy][start == y ? sel.nb.x : 0]; + lastx = (endy == y) ? sel.ne.x : term.col-1; } - last = &term.line[y][MIN(lastx, linelen-1)]; - while (last >= gp && last->u == ' ') - --last; + last = &cbuf[yy][lastx]; + if (!(cbuf[yy][term.col - 1].mode & ATTR_WRAP)) + while (last > gp && last->u == ' ') --last; for ( ; gp <= last; ++gp) { - if (gp->mode & ATTR_WDUMMY) - continue; - + if (gp->mode & ATTR_WDUMMY) continue; ptr += utf8encode(gp->u, ptr); } @@ -2541,8 +2547,10 @@ void tresize(int col, int row) { int i; - int minrow = MIN(row, term.row); - int mincol = MIN(col, term.col); + int const colSet = col, alt = IS_SET(MODE_ALTSCREEN), ini = buf == NULL; + col = MAX(col, buffCols); + row = MIN(row, buffSize); + int const minrow = MIN(row, term.row), mincol = MIN(col, buffCols); int *bp; TCursor c; @@ -2559,48 +2568,54 @@ tresize(int col, int row) * memmove because we're freeing the earlier lines */ for (i = 0; i <= term.c.y - row; i++) { - free(term.line[i]); free(term.alt[i]); } /* ensure that both src and dst are not NULL */ if (i > 0) { - memmove(term.line, term.line + i, row * sizeof(Line)); memmove(term.alt, term.alt + i, row * sizeof(Line)); } for (i += row; i < term.row; i++) { - free(term.line[i]); free(term.alt[i]); } /* resize to new height */ - term.line = xrealloc(term.line, row * sizeof(Line)); + buf = xrealloc(buf, (buffSize + row) * sizeof(Line)); term.alt = xrealloc(term.alt, row * sizeof(Line)); term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty)); + mark = xrealloc(mark, col * row * sizeof(*mark)); term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs)); /* resize each row to new width, zero-pad if needed */ for (i = 0; i < minrow; i++) { - term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph)); term.alt[i] = xrealloc(term.alt[i], col * sizeof(Glyph)); } /* allocate any new rows */ for (/* i = minrow */; i < row; i++) { - term.line[i] = xmalloc(col * sizeof(Glyph)); term.alt[i] = xmalloc(col * sizeof(Glyph)); } - if (col > term.col) { - bp = term.tabs + term.col; + if (col > buffCols) { + bp = term.tabs + buffCols; - memset(bp, 0, sizeof(*term.tabs) * (col - term.col)); + memset(bp, 0, sizeof(*term.tabs) * (col - buffCols)); while (--bp > term.tabs && !*bp) /* nothing */ ; for (bp += tabspaces; bp < term.tabs + col; bp += tabspaces) *bp = 1; } + Glyph g=(Glyph){.bg=term.c.attr.bg, .fg=term.c.attr.fg, .u=' ', .mode=0}; + for (i = 0; i < buffSize; ++i) { + buf[i] = xrealloc(ini ? NULL : buf[i], col*sizeof(Glyph)); + for (int j = ini ? 0 : buffCols; j < col; ++j) buf[i][j] = g; + } + for (i = 0; i < row; ++i) buf[buffSize + i] = buf[i]; + term.line = &buf[*(histOp?&histOff:&insertOff) +=MAX(term.c.y-row+1,0)]; + memset(mark, 0, col * row * sizeof(*mark)); /* update terminal size */ - term.col = col; + term.col = colSet; + buffCols = col; term.row = row; + if (alt) tswapscreen(); /* reset scrolling region */ tsetscroll(0, row-1); /* make use of the LIMIT in tmoveto */ @@ -2629,15 +2644,17 @@ resettitle(void) void drawregion(int x1, int y1, int x2, int y2) { + if (altToggle && histMode && !histOp) + memset(term.dirty, 0, sizeof(*term.dirty) * term.row); + int const o = !IS_SET(MODE_ALTSCREEN) && histMode && !histOp, h =rows(); int y; for (y = y1; y < y2; y++) { - if (!term.dirty[y]) - continue; - - term.dirty[y] = 0; - xdrawline(term.line[y], x1, y, x2); + int const oy = o ? (y + insertOff - histOff + h) % h : y; + if (!BETWEEN(oy, 0, term.row-1) || !term.dirty[y]) continue; + xdrawline(term.line[y], x1, oy, x2); } + memset(&term.dirty[y1], 0, sizeof(*term.dirty) * (y2 - y1)); } void @@ -2656,7 +2673,9 @@ draw(void) if (term.line[term.c.y][cx].mode & ATTR_WDUMMY) cx--; + if (histMode) historyPreDraw(); drawregion(0, 0, term.col, term.row); + if (!histMode) xdrawcursor(cx, term.c.y, term.line[term.c.y][cx], term.ocx, term.ocy, term.line[term.ocy][term.ocx]); term.ocx = cx;