My patched version of suckless' terminal - st.
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

347 lines
8.9 KiB

9 months ago
  1. From aecdff3915abe3e8b41275ea7f13f3ac6c54eb4a Mon Sep 17 00:00:00 2001
  2. From: Jacob Prosser <jacoblouisprosser@protonmail.com>
  3. Date: Thu, 25 Apr 2019 12:55:32 +1000
  4. Subject: [PATCH] Scrollback w/ mouse
  5. ---
  6. config.def.h | 2 +
  7. st.c | 115 ++++++++++++++++++++++++++++++++++++++++-----------
  8. st.h | 2 +
  9. 3 files changed, 95 insertions(+), 24 deletions(-)
  10. diff --git a/config.def.h b/config.def.h
  11. index 0e01717..7326a74 100644
  12. --- a/config.def.h
  13. +++ b/config.def.h
  14. @@ -176,6 +176,8 @@ static Shortcut shortcuts[] = {
  15. { TERMMOD, XK_C, clipcopy, {.i = 0} },
  16. { TERMMOD, XK_V, clippaste, {.i = 0} },
  17. { TERMMOD, XK_Y, selpaste, {.i = 0} },
  18. + { ShiftMask, XK_Page_Up, kscrollup, {.i = -1} },
  19. + { ShiftMask, XK_Page_Down, kscrolldown, {.i = -1} },
  20. { ShiftMask, XK_Insert, selpaste, {.i = 0} },
  21. { TERMMOD, XK_Num_Lock, numlock, {.i = 0} },
  22. };
  23. diff --git a/st.c b/st.c
  24. index b8e6077..07e71e1 100644
  25. --- a/st.c
  26. +++ b/st.c
  27. @@ -35,6 +35,7 @@
  28. #define ESC_ARG_SIZ 16
  29. #define STR_BUF_SIZ ESC_BUF_SIZ
  30. #define STR_ARG_SIZ ESC_ARG_SIZ
  31. +#define HISTSIZE 2000
  32. /* macros */
  33. #define IS_SET(flag) ((term.mode & (flag)) != 0)
  34. @@ -42,6 +43,9 @@
  35. #define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
  36. #define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
  37. #define ISDELIM(u) (utf8strchr(worddelimiters, u) != NULL)
  38. +#define TLINE(y) ((y) < term.scr ? term.hist[((y) + term.histi - \
  39. + term.scr + HISTSIZE + 1) % HISTSIZE] : \
  40. + term.line[(y) - term.scr])
  41. enum term_mode {
  42. MODE_WRAP = 1 << 0,
  43. @@ -117,6 +121,9 @@ typedef struct {
  44. int col; /* nb col */
  45. Line *line; /* screen */
  46. Line *alt; /* alternate screen */
  47. + Line hist[HISTSIZE]; /* history buffer */
  48. + int histi; /* history index */
  49. + int scr; /* scroll back */
  50. int *dirty; /* dirtyness of lines */
  51. TCursor c; /* cursor */
  52. int ocx; /* old cursor col */
  53. @@ -184,8 +191,8 @@ static void tnewline(int);
  54. static void tputtab(int);
  55. static void tputc(Rune);
  56. static void treset(void);
  57. -static void tscrollup(int, int);
  58. -static void tscrolldown(int, int);
  59. +static void tscrollup(int, int, int);
  60. +static void tscrolldown(int, int, int);
  61. static void tsetattr(int *, int);
  62. static void tsetchar(Rune, Glyph *, int, int);
  63. static void tsetdirt(int, int);
  64. @@ -427,10 +434,10 @@ tlinelen(int y)
  65. {
  66. int i = term.col;
  67. - if (term.line[y][i - 1].mode & ATTR_WRAP)
  68. + if (TLINE(y)[i - 1].mode & ATTR_WRAP)
  69. return i;
  70. - while (i > 0 && term.line[y][i - 1].u == ' ')
  71. + while (i > 0 && TLINE(y)[i - 1].u == ' ')
  72. --i;
  73. return i;
  74. @@ -539,7 +546,7 @@ selsnap(int *x, int *y, int direction)
  75. * Snap around if the word wraps around at the end or
  76. * beginning of a line.
  77. */
  78. - prevgp = &term.line[*y][*x];
  79. + prevgp = &TLINE(*y)[*x];
  80. prevdelim = ISDELIM(prevgp->u);
  81. for (;;) {
  82. newx = *x + direction;
  83. @@ -554,14 +561,14 @@ selsnap(int *x, int *y, int direction)
  84. yt = *y, xt = *x;
  85. else
  86. yt = newy, xt = newx;
  87. - if (!(term.line[yt][xt].mode & ATTR_WRAP))
  88. + if (!(TLINE(yt)[xt].mode & ATTR_WRAP))
  89. break;
  90. }
  91. if (newx >= tlinelen(newy))
  92. break;
  93. - gp = &term.line[newy][newx];
  94. + gp = &TLINE(newy)[newx];
  95. delim = ISDELIM(gp->u);
  96. if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
  97. || (delim && gp->u != prevgp->u)))
  98. @@ -582,14 +589,14 @@ selsnap(int *x, int *y, int direction)
  99. *x = (direction < 0) ? 0 : term.col - 1;
  100. if (direction < 0) {
  101. for (; *y > 0; *y += direction) {
  102. - if (!(term.line[*y-1][term.col-1].mode
  103. + if (!(TLINE(*y-1)[term.col-1].mode
  104. & ATTR_WRAP)) {
  105. break;
  106. }
  107. }
  108. } else if (direction > 0) {
  109. for (; *y < term.row-1; *y += direction) {
  110. - if (!(term.line[*y][term.col-1].mode
  111. + if (!(TLINE(*y)[term.col-1].mode
  112. & ATTR_WRAP)) {
  113. break;
  114. }
  115. @@ -620,13 +627,13 @@ getsel(void)
  116. }
  117. if (sel.type == SEL_RECTANGULAR) {
  118. - gp = &term.line[y][sel.nb.x];
  119. + gp = &TLINE(y)[sel.nb.x];
  120. lastx = sel.ne.x;
  121. } else {
  122. - gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0];
  123. + gp = &TLINE(y)[sel.nb.y == y ? sel.nb.x : 0];
  124. lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
  125. }
  126. - last = &term.line[y][MIN(lastx, linelen-1)];
  127. + last = &TLINE(y)[MIN(lastx, linelen-1)];
  128. while (last >= gp && last->u == ' ')
  129. --last;
  130. @@ -849,6 +856,9 @@ void
  131. ttywrite(const char *s, size_t n, int may_echo)
  132. {
  133. const char *next;
  134. + Arg arg = (Arg) { .i = term.scr };
  135. +
  136. + kscrolldown(&arg);
  137. if (may_echo && IS_SET(MODE_ECHO))
  138. twrite(s, n, 1);
  139. @@ -1058,15 +1068,53 @@ tswapscreen(void)
  140. term.mode ^= MODE_ALTSCREEN;
  141. tfulldirt();
  142. }
  143. +void
  144. +kscrolldown(const Arg* a)
  145. +{
  146. + int n = a->i;
  147. +
  148. + if (n < 0)
  149. + n = term.row + n;
  150. +
  151. + if (n > term.scr)
  152. + n = term.scr;
  153. +
  154. + if (term.scr > 0) {
  155. + term.scr -= n;
  156. + selscroll(0, -n);
  157. + tfulldirt();
  158. + }
  159. +}
  160. +
  161. +void
  162. +kscrollup(const Arg* a)
  163. +{
  164. + int n = a->i;
  165. + if (n < 0)
  166. + n = term.row + n;
  167. +
  168. + if (term.scr <= HISTSIZE-n) {
  169. + term.scr += n;
  170. + selscroll(0, n);
  171. + tfulldirt();
  172. + }
  173. +}
  174. void
  175. -tscrolldown(int orig, int n)
  176. +tscrolldown(int orig, int n, int copyhist)
  177. {
  178. int i;
  179. Line temp;
  180. LIMIT(n, 0, term.bot-orig+1);
  181. + if (copyhist) {
  182. + term.histi = (term.histi - 1 + HISTSIZE) % HISTSIZE;
  183. + temp = term.hist[term.histi];
  184. + term.hist[term.histi] = term.line[term.bot];
  185. + term.line[term.bot] = temp;
  186. + }
  187. +
  188. tsetdirt(orig, term.bot-n);
  189. tclearregion(0, term.bot-n+1, term.col-1, term.bot);
  190. @@ -1080,13 +1128,23 @@ tscrolldown(int orig, int n)
  191. }
  192. void
  193. -tscrollup(int orig, int n)
  194. +tscrollup(int orig, int n, int copyhist)
  195. {
  196. int i;
  197. Line temp;
  198. LIMIT(n, 0, term.bot-orig+1);
  199. + if (copyhist) {
  200. + term.histi = (term.histi + 1) % HISTSIZE;
  201. + temp = term.hist[term.histi];
  202. + term.hist[term.histi] = term.line[orig];
  203. + term.line[orig] = temp;
  204. + }
  205. +
  206. + if (term.scr > 0 && term.scr < HISTSIZE)
  207. + term.scr = MIN(term.scr + n, HISTSIZE-1);
  208. +
  209. tclearregion(0, orig, term.col-1, orig+n-1);
  210. tsetdirt(orig+n, term.bot);
  211. @@ -1135,7 +1193,7 @@ tnewline(int first_col)
  212. int y = term.c.y;
  213. if (y == term.bot) {
  214. - tscrollup(term.top, 1);
  215. + tscrollup(term.top, 1, 1);
  216. } else {
  217. y++;
  218. }
  219. @@ -1300,14 +1358,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. void
  227. tdeleteline(int n)
  228. {
  229. if (BETWEEN(term.c.y, term.top, term.bot))
  230. - tscrollup(term.c.y, n);
  231. + tscrollup(term.c.y, n, 0);
  232. }
  233. int32_t
  234. @@ -1737,11 +1795,11 @@ csihandle(void)
  235. break;
  236. case 'S': /* SU -- Scroll <n> line up */
  237. DEFAULT(csiescseq.arg[0], 1);
  238. - tscrollup(term.top, csiescseq.arg[0]);
  239. + tscrollup(term.top, csiescseq.arg[0], 0);
  240. break;
  241. case 'T': /* SD -- Scroll <n> line down */
  242. DEFAULT(csiescseq.arg[0], 1);
  243. - tscrolldown(term.top, csiescseq.arg[0]);
  244. + tscrolldown(term.top, csiescseq.arg[0], 0);
  245. break;
  246. case 'L': /* IL -- Insert <n> blank lines */
  247. DEFAULT(csiescseq.arg[0], 1);
  248. @@ -2243,7 +2301,7 @@ eschandle(uchar ascii)
  249. return 0;
  250. case 'D': /* IND -- Linefeed */
  251. if (term.c.y == term.bot) {
  252. - tscrollup(term.top, 1);
  253. + tscrollup(term.top, 1, 1);
  254. } else {
  255. tmoveto(term.c.x, term.c.y+1);
  256. }
  257. @@ -2256,7 +2314,7 @@ eschandle(uchar ascii)
  258. break;
  259. case 'M': /* RI -- Reverse index */
  260. if (term.c.y == term.top) {
  261. - tscrolldown(term.top, 1);
  262. + tscrolldown(term.top, 1, 1);
  263. } else {
  264. tmoveto(term.c.x, term.c.y-1);
  265. }
  266. @@ -2475,7 +2533,7 @@ twrite(const char *buf, int buflen, int show_ctrl)
  267. void
  268. tresize(int col, int row)
  269. {
  270. - int i;
  271. + int i, j;
  272. int minrow = MIN(row, term.row);
  273. int mincol = MIN(col, term.col);
  274. int *bp;
  275. @@ -2512,6 +2570,14 @@ tresize(int col, int row)
  276. term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
  277. term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
  278. + for (i = 0; i < HISTSIZE; i++) {
  279. + term.hist[i] = xrealloc(term.hist[i], col * sizeof(Glyph));
  280. + for (j = mincol; j < col; j++) {
  281. + term.hist[i][j] = term.c.attr;
  282. + term.hist[i][j].u = ' ';
  283. + }
  284. + }
  285. +
  286. /* resize each row to new width, zero-pad if needed */
  287. for (i = 0; i < minrow; i++) {
  288. term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
  289. @@ -2569,7 +2635,7 @@ drawregion(int x1, int y1, int x2, int y2)
  290. continue;
  291. term.dirty[y] = 0;
  292. - xdrawline(term.line[y], x1, y, x2);
  293. + xdrawline(TLINE(y), x1, y, x2);
  294. }
  295. }
  296. @@ -2590,7 +2656,8 @@ draw(void)
  297. cx--;
  298. drawregion(0, 0, term.col, term.row);
  299. - xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
  300. + if (term.scr == 0)
  301. + xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
  302. term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
  303. term.ocx = cx, term.ocy = term.c.y;
  304. xfinishdraw();
  305. diff --git a/st.h b/st.h
  306. index 38c61c4..17a79e0 100644
  307. --- a/st.h
  308. +++ b/st.h
  309. @@ -80,6 +80,8 @@ void die(const char *, ...);
  310. void redraw(void);
  311. void draw(void);
  312. +void kscrolldown(const Arg *);
  313. +void kscrollup(const Arg *);
  314. void printscreen(const Arg *);
  315. void printsel(const Arg *);
  316. void sendbreak(const Arg *);
  317. --
  318. 2.21.0