sites

public wiki contents of suckless.org
git clone git://git.suckless.org/sites
Log | Files | Refs

st-scrollback-0.8.diff (8818B)


      1 diff --git a/config.def.h b/config.def.h
      2 index 82b1b09..27d42ca 100644
      3 --- a/config.def.h
      4 +++ b/config.def.h
      5 @@ -178,6 +178,8 @@ static Shortcut shortcuts[] = {
      6  	{ TERMMOD,              XK_Y,           selpaste,       {.i =  0} },
      7  	{ TERMMOD,              XK_Num_Lock,    numlock,        {.i =  0} },
      8  	{ TERMMOD,              XK_I,           iso14755,       {.i =  0} },
      9 +	{ ShiftMask,            XK_Page_Up,     kscrollup,      {.i = -1} },
     10 +	{ ShiftMask,            XK_Page_Down,   kscrolldown,    {.i = -1} },
     11  };
     12  
     13  /*
     14 diff --git a/st.c b/st.c
     15 index 65a0cb6..1d8c12a 100644
     16 --- a/st.c
     17 +++ b/st.c
     18 @@ -121,6 +121,9 @@ typedef struct {
     19  	int col;      /* nb col */
     20  	Line *line;   /* screen */
     21  	Line *alt;    /* alternate screen */
     22 +	Line hist[HISTSIZE]; /* history buffer */
     23 +	int histi;    /* history index */
     24 +	int scr;      /* scroll back */
     25  	int *dirty;   /* dirtyness of lines */
     26  	TCursor c;    /* cursor */
     27  	int ocx;      /* old cursor col */
     28 @@ -188,8 +191,8 @@ static void tnewline(int);
     29  static void tputtab(int);
     30  static void tputc(Rune);
     31  static void treset(void);
     32 -static void tscrollup(int, int);
     33 -static void tscrolldown(int, int);
     34 +static void tscrollup(int, int, int);
     35 +static void tscrolldown(int, int, int);
     36  static void tsetattr(int *, int);
     37  static void tsetchar(Rune, Glyph *, int, int);
     38  static void tsetdirt(int, int);
     39 @@ -431,10 +434,10 @@ tlinelen(int y)
     40  {
     41  	int i = term.col;
     42  
     43 -	if (term.line[y][i - 1].mode & ATTR_WRAP)
     44 +	if (TLINE(y)[i - 1].mode & ATTR_WRAP)
     45  		return i;
     46  
     47 -	while (i > 0 && term.line[y][i - 1].u == ' ')
     48 +	while (i > 0 && TLINE(y)[i - 1].u == ' ')
     49  		--i;
     50  
     51  	return i;
     52 @@ -543,7 +546,7 @@ selsnap(int *x, int *y, int direction)
     53  		 * Snap around if the word wraps around at the end or
     54  		 * beginning of a line.
     55  		 */
     56 -		prevgp = &term.line[*y][*x];
     57 +		prevgp = &TLINE(*y)[*x];
     58  		prevdelim = ISDELIM(prevgp->u);
     59  		for (;;) {
     60  			newx = *x + direction;
     61 @@ -558,14 +561,14 @@ selsnap(int *x, int *y, int direction)
     62  					yt = *y, xt = *x;
     63  				else
     64  					yt = newy, xt = newx;
     65 -				if (!(term.line[yt][xt].mode & ATTR_WRAP))
     66 +				if (!(TLINE(yt)[xt].mode & ATTR_WRAP))
     67  					break;
     68  			}
     69  
     70  			if (newx >= tlinelen(newy))
     71  				break;
     72  
     73 -			gp = &term.line[newy][newx];
     74 +			gp = &TLINE(newy)[newx];
     75  			delim = ISDELIM(gp->u);
     76  			if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
     77  					|| (delim && gp->u != prevgp->u)))
     78 @@ -586,14 +589,14 @@ selsnap(int *x, int *y, int direction)
     79  		*x = (direction < 0) ? 0 : term.col - 1;
     80  		if (direction < 0) {
     81  			for (; *y > 0; *y += direction) {
     82 -				if (!(term.line[*y-1][term.col-1].mode
     83 +				if (!(TLINE(*y-1)[term.col-1].mode
     84  						& ATTR_WRAP)) {
     85  					break;
     86  				}
     87  			}
     88  		} else if (direction > 0) {
     89  			for (; *y < term.row-1; *y += direction) {
     90 -				if (!(term.line[*y][term.col-1].mode
     91 +				if (!(TLINE(*y)[term.col-1].mode
     92  						& ATTR_WRAP)) {
     93  					break;
     94  				}
     95 @@ -624,13 +627,13 @@ getsel(void)
     96  		}
     97  
     98  		if (sel.type == SEL_RECTANGULAR) {
     99 -			gp = &term.line[y][sel.nb.x];
    100 +			gp = &TLINE(y)[sel.nb.x];
    101  			lastx = sel.ne.x;
    102  		} else {
    103 -			gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
    104 +			gp = &TLINE(y)[sel.nb.y == y ? sel.nb.x : 0];
    105  			lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
    106  		}
    107 -		last = &term.line[y][MIN(lastx, linelen-1)];
    108 +		last = &TLINE(y)[MIN(lastx, linelen-1)];
    109  		while (last >= gp && last->u == ' ')
    110  			--last;
    111  
    112 @@ -836,6 +839,9 @@ ttyread(void)
    113  	if (buflen > 0)
    114  		memmove(buf, buf + written, buflen);
    115  
    116 +	if (term.scr > 0 && term.scr < HISTSIZE-1)
    117 +		term.scr++;
    118 +
    119  	return ret;
    120  }
    121  
    122 @@ -843,6 +849,9 @@ void
    123  ttywrite(const char *s, size_t n, int may_echo)
    124  {
    125  	const char *next;
    126 +	Arg arg = (Arg) { .i = term.scr };
    127 +
    128 +	kscrolldown(&arg);
    129  
    130  	if (may_echo && IS_SET(MODE_ECHO))
    131  		twrite(s, n, 1);
    132 @@ -1054,13 +1063,54 @@ tswapscreen(void)
    133  }
    134  
    135  void
    136 -tscrolldown(int orig, int n)
    137 +kscrolldown(const Arg* a)
    138 +{
    139 +	int n = a->i;
    140 +
    141 +	if (n < 0)
    142 +		n = term.row + n;
    143 +
    144 +	if (n > term.scr)
    145 +		n = term.scr;
    146 +
    147 +	if (term.scr > 0) {
    148 +		term.scr -= n;
    149 +		selscroll(0, -n);
    150 +		tfulldirt();
    151 +	}
    152 +}
    153 +
    154 +void
    155 +kscrollup(const Arg* a)
    156 +{
    157 +	int n = a->i;
    158 +
    159 +	if (n < 0)
    160 +		n = term.row + n;
    161 +
    162 +	if (term.scr <= HISTSIZE-n) {
    163 +		term.scr += n;
    164 +		selscroll(0, n);
    165 +		tfulldirt();
    166 +	}
    167 +}
    168 +
    169 +
    170 +void
    171 +tscrolldown(int orig, int n, int copyhist)
    172  {
    173  	int i;
    174  	Line temp;
    175  
    176  	LIMIT(n, 0, term.bot-orig+1);
    177  
    178 +	if (copyhist) {
    179 +		term.histi = (term.histi - 1 + HISTSIZE) % HISTSIZE;
    180 +		temp = term.hist[term.histi];
    181 +		term.hist[term.histi] = term.line[term.bot];
    182 +		term.line[term.bot] = temp;
    183 +	}
    184 +
    185  	tsetdirt(orig, term.bot-n);
    186  	tclearregion(0, term.bot-n+1, term.col-1, term.bot);
    187  
    188 @@ -1074,13 +1124,20 @@ tscrolldown(int orig, int n)
    189  }
    190  
    191  void
    192 -tscrollup(int orig, int n)
    193 +tscrollup(int orig, int n, int copyhist)
    194  {
    195  	int i;
    196  	Line temp;
    197  
    198  	LIMIT(n, 0, term.bot-orig+1);
    199  
    200 +	if (copyhist) {
    201 +		term.histi = (term.histi + 1) % HISTSIZE;
    202 +		temp = term.hist[term.histi];
    203 +		term.hist[term.histi] = term.line[orig];
    204 +		term.line[orig] = temp;
    205 +	}
    206 +
    207  	tclearregion(0, orig, term.col-1, orig+n-1);
    208  	tsetdirt(orig+n, term.bot);
    209  
    210 @@ -1129,7 +1186,7 @@ tnewline(int first_col)
    211  	int y = term.c.y;
    212  
    213  	if (y == term.bot) {
    214 -		tscrollup(term.top, 1);
    215 +		tscrollup(term.top, 1, 1);
    216  	} else {
    217  		y++;
    218  	}
    219 @@ -1294,14 +1351,14 @@ void
    220  tinsertblankline(int n)
    221  {
    222  	if (BETWEEN(term.c.y, term.top, term.bot))
    223 -		tscrolldown(term.c.y, n);
    224 +		tscrolldown(term.c.y, n, 0);
    225  }
    226  
    227  void
    228  tdeleteline(int n)
    229  {
    230  	if (BETWEEN(term.c.y, term.top, term.bot))
    231 -		tscrollup(term.c.y, n);
    232 +		tscrollup(term.c.y, n, 0);
    233  }
    234  
    235  int32_t
    236 @@ -1730,11 +1787,11 @@ csihandle(void)
    237  		break;
    238  	case 'S': /* SU -- Scroll <n> line up */
    239  		DEFAULT(csiescseq.arg[0], 1);
    240 -		tscrollup(term.top, csiescseq.arg[0]);
    241 +		tscrollup(term.top, csiescseq.arg[0], 0);
    242  		break;
    243  	case 'T': /* SD -- Scroll <n> line down */
    244  		DEFAULT(csiescseq.arg[0], 1);
    245 -		tscrolldown(term.top, csiescseq.arg[0]);
    246 +		tscrolldown(term.top, csiescseq.arg[0], 0);
    247  		break;
    248  	case 'L': /* IL -- Insert <n> blank lines */
    249  		DEFAULT(csiescseq.arg[0], 1);
    250 @@ -2258,7 +2315,7 @@ eschandle(uchar ascii)
    251  		return 0;
    252  	case 'D': /* IND -- Linefeed */
    253  		if (term.c.y == term.bot) {
    254 -			tscrollup(term.top, 1);
    255 +			tscrollup(term.top, 1, 1);
    256  		} else {
    257  			tmoveto(term.c.x, term.c.y+1);
    258  		}
    259 @@ -2271,7 +2328,7 @@ eschandle(uchar ascii)
    260  		break;
    261  	case 'M': /* RI -- Reverse index */
    262  		if (term.c.y == term.top) {
    263 -			tscrolldown(term.top, 1);
    264 +			tscrolldown(term.top, 1, 1);
    265  		} else {
    266  			tmoveto(term.c.x, term.c.y-1);
    267  		}
    268 @@ -2490,7 +2547,7 @@ twrite(const char *buf, int buflen, int show_ctrl)
    269  void
    270  tresize(int col, int row)
    271  {
    272 -	int i;
    273 +	int i, j;
    274  	int minrow = MIN(row, term.row);
    275  	int mincol = MIN(col, term.col);
    276  	int *bp;
    277 @@ -2527,7 +2584,15 @@ tresize(int col, int row)
    278  	term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
    279  	term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
    280  
    281 -	/* resize each row to new width, zero-pad if needed */
    282 +	for (i = 0; i < HISTSIZE; i++) {
    283 +		term.hist[i] = xrealloc(term.hist[i], col * sizeof(Glyph));
    284 +		for (j = mincol; j < col; j++) {
    285 +			term.hist[i][j] = term.c.attr;
    286 +			term.hist[i][j].u = ' ';
    287 +		}
    288 +	}
    289 +
    290 +	/* resize each r w to new width, zero-pad if needed */
    291  	for (i = 0; i < minrow; i++) {
    292  		term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
    293  		term.alt[i]  = xrealloc(term.alt[i],  col * sizeof(Glyph));
    294 @@ -2584,7 +2649,7 @@ drawregion(int x1, int y1, int x2, int y2)
    295  			continue;
    296  
    297  		term.dirty[y] = 0;
    298 -		xdrawline(term.line[y], x1, y, x2);
    299 +		xdrawline(TLINE(y), x1, y, x2);
    300  	}
    301  }
    302  
    303 @@ -2605,8 +2670,10 @@ draw(void)
    304  		cx--;
    305  
    306  	drawregion(0, 0, term.col, term.row);
    307 -	xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
    308 -			term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
    309 +	if (term.scr == 0) {
    310 +		xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
    311 +				term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
    312 +	}
    313  	term.ocx = cx, term.ocy = term.c.y;
    314  	xfinishdraw();
    315  }
    316 diff --git a/st.h b/st.h
    317 index dac64d8..628e876 100644
    318 --- a/st.h
    319 +++ b/st.h
    320 @@ -3,6 +3,9 @@
    321  #include <stdint.h>
    322  #include <sys/types.h>
    323  
    324 +/* Arbitrary size */
    325 +#define HISTSIZE      2000
    326 +
    327  /* macros */
    328  #define MIN(a, b)		((a) < (b) ? (a) : (b))
    329  #define MAX(a, b)		((a) < (b) ? (b) : (a))
    330 @@ -19,6 +22,8 @@
    331  
    332  #define TRUECOLOR(r,g,b)	(1 << 24 | (r) << 16 | (g) << 8 | (b))
    333  #define IS_TRUECOL(x)		(1 << 24 & (x))
    334 +#define TLINE(y)       ((y) < term.scr ? term.hist[((y) + term.histi - term.scr \
    335 +               + HISTSIZE + 1) % HISTSIZE] : term.line[(y) - term.scr])
    336  
    337  enum glyph_attribute {
    338  	ATTR_NULL       = 0,
    339 @@ -111,6 +116,9 @@ void *xmalloc(size_t);
    340  void *xrealloc(void *, size_t);
    341  char *xstrdup(char *);
    342  
    343 +void kscrolldown(const Arg *);
    344 +void kscrollup(const Arg *);
    345 +
    346  /* config.h globals */
    347  extern char *utmp;
    348  extern char *stty_args;