scrollback

This commit is contained in:
hybris 2017-11-04 01:50:00 +01:00
parent fb04d66975
commit 0533149a96
2 changed files with 143 additions and 55 deletions

View File

@ -7,6 +7,7 @@
*/ */
static char font[] = "xos4 Terminus:pixelsize=13:antialias=true:autohint=true"; static char font[] = "xos4 Terminus:pixelsize=13:antialias=true:autohint=true";
static int borderpx = 2; static int borderpx = 2;
#define histsize 2000
/* /*
* What program is execed by st depends of these precedence rules: * What program is execed by st depends of these precedence rules:
@ -161,17 +162,18 @@ static Axiskey ashortcuts[] = {
#define MODKEY MOD_MASK_ALT #define MODKEY MOD_MASK_ALT
static Shortcut shortcuts[] = { static Shortcut shortcuts[] = {
/* modifier key function argument */ /* modifier key function argument */
{ MOD_MASK_ANY, XKB_KEY_Break, sendbreak, {.i = 0} }, { MOD_MASK_ANY, XKB_KEY_Break, sendbreak, {.i = 0} },
{ MOD_MASK_CTRL, XKB_KEY_Print, toggleprinter, {.i = 0} }, { MOD_MASK_CTRL, XKB_KEY_Print, toggleprinter, {.i = 0} },
{ MOD_MASK_SHIFT, XKB_KEY_Print, printscreen, {.i = 0} }, { MOD_MASK_SHIFT, XKB_KEY_Print, printscreen, {.i = 0} },
{ MOD_MASK_ANY, XKB_KEY_Print, printsel, {.i = 0} }, { MOD_MASK_ANY, XKB_KEY_Print, printsel, {.i = 0} },
{ MODKEY|MOD_MASK_SHIFT, XKB_KEY_Prior, wlzoom, {.f = +1} }, { MODKEY|MOD_MASK_SHIFT, XKB_KEY_Prior, wlzoom, {.f = +1} },
{ MODKEY|MOD_MASK_SHIFT, XKB_KEY_Next, wlzoom, {.f = -1} }, { MODKEY|MOD_MASK_SHIFT, XKB_KEY_Next, wlzoom, {.f = -1} },
{ MODKEY|MOD_MASK_SHIFT, XKB_KEY_Home, wlzoomreset, {.f = 0} }, { MODKEY|MOD_MASK_SHIFT, XKB_KEY_Home, wlzoomreset, {.f = 0} },
{ MOD_MASK_SHIFT, XKB_KEY_Insert, selpaste, {.i = 0} }, { MOD_MASK_SHIFT, XKB_KEY_Insert, selpaste, {.i = 0} },
{ MODKEY, XKB_KEY_Num_Lock, numlock, {.i = 0} }, { MODKEY, XKB_KEY_Num_Lock, numlock, {.i = 0} },
{ MODKEY, XKB_KEY_Control_L, iso14755, {.i = 0} }, { MOD_MASK_SHIFT, XKB_KEY_Page_Up, kscrollup, {.i = -1} },
{ MOD_MASK_SHIFT, XKB_KEY_Page_Down, kscrolldown, {.i = -1} },
}; };
/* /*

174
st.c
View File

@ -93,6 +93,9 @@ char *argv0;
#define TRUEGREEN(x) (((x) & 0xff00)) #define TRUEGREEN(x) (((x) & 0xff00))
#define TRUEBLUE(x) (((x) & 0xff) << 8) #define TRUEBLUE(x) (((x) & 0xff) << 8)
#define TLINE(y) ((y) < term.scr ? term.hist[((y) + term.histi - term.scr \
+ histsize + 1) % histsize] : term.line[(y) - term.scr])
/* constants */ /* constants */
#define ISO14755CMD "dmenu -p codepoint: </dev/null" #define ISO14755CMD "dmenu -p codepoint: </dev/null"
@ -237,25 +240,6 @@ typedef struct {
int narg; /* nb of args */ int narg; /* nb of args */
} STREscape; } STREscape;
/* Internal representation of the screen */
typedef struct {
int row; /* nb row */
int col; /* nb col */
Line *line; /* screen */
Line *alt; /* alternate screen */
int *dirty; /* dirtyness of lines */
TCursor c; /* cursor */
int top; /* top scroll limit */
int bot; /* bottom scroll limit */
int mode; /* terminal mode flags */
int esc; /* escape state flags */
char trantbl[4]; /* charset table translation */
int charset; /* current charset */
int icharset; /* selected charset for sequence */
int numlock; /* lock numbers in keyboard */
int *tabs;
} Term;
typedef struct { typedef struct {
struct xkb_context *ctx; struct xkb_context *ctx;
struct xkb_keymap *keymap; struct xkb_keymap *keymap;
@ -382,10 +366,36 @@ static void printscreen(const Arg *) ;
static void iso14755(const Arg *); static void iso14755(const Arg *);
static void toggleprinter(const Arg *); static void toggleprinter(const Arg *);
static void sendbreak(const Arg *); static void sendbreak(const Arg *);
static void kscrollup(const Arg *);
static void kscrolldown(const Arg *);
/* Config.h for applying patches and the configuration. */ /* Config.h for applying patches and the configuration. */
#include "config.h" #include "config.h"
/* Internal representation of the screen */
typedef struct {
int row; /* nb row */
int col; /* nb col */
Line *line; /* screen */
Line *alt; /* alternate screen */
Line hist[histsize]; /* history buffer */
int histi; /* history index */
int scr; /* scroll back */
int *dirty; /* dirtyness of lines */
/* XftGlyphFontSpec *specbuf; /* font spec buffer used for rendering */
TCursor c; /* cursor */
int top; /* top scroll limit */
int bot; /* bottom scroll limit */
int mode; /* terminal mode flags */
int esc; /* escape state flags */
char trantbl[4]; /* charset table translation */
int charset; /* current charset */
int icharset; /* selected charset for sequence */
int numlock; /* lock numbers in keyboard */
int *tabs;
} Term;
/* Font structure */ /* Font structure */
typedef struct { typedef struct {
int height; int height;
@ -447,8 +457,8 @@ static void tputtab(int);
static void tputc(Rune); static void tputc(Rune);
static void treset(void); static void treset(void);
static void tresize(int, int); static void tresize(int, int);
static void tscrollup(int, int); static void tscrollup(int, int, int);
static void tscrolldown(int, int); static void tscrolldown(int, int, int);
static void tsetattr(int *, int); static void tsetattr(int *, int);
static void tsetchar(Rune, Glyph *, int, int); static void tsetchar(Rune, Glyph *, int, int);
static void tsetscroll(int, int); static void tsetscroll(int, int);
@ -800,10 +810,10 @@ tlinelen(int y)
{ {
int i = term.col; int i = term.col;
if (term.line[y][i - 1].mode & ATTR_WRAP) if (TLINE(y)[i - 1].mode & ATTR_WRAP)
return i; return i;
while (i > 0 && term.line[y][i - 1].u == ' ') while (i > 0 && TLINE(y)[i - 1].u == ' ')
--i; --i;
return i; return i;
@ -865,7 +875,7 @@ selsnap(int *x, int *y, int direction)
* Snap around if the word wraps around at the end or * Snap around if the word wraps around at the end or
* beginning of a line. * beginning of a line.
*/ */
prevgp = &term.line[*y][*x]; prevgp = &TLINE(*y)[*x];
prevdelim = ISDELIM(prevgp->u); prevdelim = ISDELIM(prevgp->u);
for (;;) { for (;;) {
newx = *x + direction; newx = *x + direction;
@ -880,14 +890,14 @@ selsnap(int *x, int *y, int direction)
yt = *y, xt = *x; yt = *y, xt = *x;
else else
yt = newy, xt = newx; yt = newy, xt = newx;
if (!(term.line[yt][xt].mode & ATTR_WRAP)) if (!(TLINE(yt)[xt].mode & ATTR_WRAP))
break; break;
} }
if (newx >= tlinelen(newy)) if (newx >= tlinelen(newy))
break; break;
gp = &term.line[newy][newx]; gp = &TLINE(newy)[newx];
delim = ISDELIM(gp->u); delim = ISDELIM(gp->u);
if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
|| (delim && gp->u != prevgp->u))) || (delim && gp->u != prevgp->u)))
@ -908,14 +918,14 @@ selsnap(int *x, int *y, int direction)
*x = (direction < 0) ? 0 : term.col - 1; *x = (direction < 0) ? 0 : term.col - 1;
if (direction < 0) { if (direction < 0) {
for (; *y > 0; *y += direction) { for (; *y > 0; *y += direction) {
if (!(term.line[*y-1][term.col-1].mode if (!(TLINE(*y-1)[term.col-1].mode
& ATTR_WRAP)) { & ATTR_WRAP)) {
break; break;
} }
} }
} else if (direction > 0) { } else if (direction > 0) {
for (; *y < term.row-1; *y += direction) { for (; *y < term.row-1; *y += direction) {
if (!(term.line[*y][term.col-1].mode if (!(TLINE(*y)[term.col-1].mode
& ATTR_WRAP)) { & ATTR_WRAP)) {
break; break;
} }
@ -1048,13 +1058,13 @@ getsel(void)
} }
if (sel.type == SEL_RECTANGULAR) { if (sel.type == SEL_RECTANGULAR) {
gp = &term.line[y][sel.nb.x]; gp = &TLINE(y)[sel.nb.x];
lastx = sel.ne.x; lastx = sel.ne.x;
} else { } else {
gp = &term.line[y][sel.nb.y == y ? sel.nb.x : 0]; gp = &TLINE(y)[sel.nb.y == y ? sel.nb.x : 0];
lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1; lastx = (sel.ne.y == y) ? sel.ne.x : term.col-1;
} }
last = &term.line[y][MIN(lastx, linelen-1)]; last = &TLINE(y)[MIN(lastx, linelen-1)];
while (last >= gp && last->u == ' ') while (last >= gp && last->u == ' ')
--last; --last;
@ -1355,6 +1365,10 @@ ttyread(void)
if (buflen > 0) if (buflen > 0)
memmove(buf, ptr, buflen); memmove(buf, ptr, buflen);
if (term.scr > 0 && term.scr < histsize-1)
term.scr++;
needdraw = true; needdraw = true;
return ret; return ret;
} }
@ -1365,6 +1379,9 @@ ttywrite(const char *s, size_t n)
fd_set wfd, rfd; fd_set wfd, rfd;
ssize_t r; ssize_t r;
size_t lim = 256; size_t lim = 256;
Arg arg = (Arg){ .i = term.scr };
kscrolldown(&arg);
/* /*
* Remember that we are using a pty, which might be a modem line. * Remember that we are using a pty, which might be a modem line.
@ -1569,13 +1586,53 @@ tswapscreen(void)
} }
void void
tscrolldown(int orig, int n) kscrolldown(const Arg* a)
{
int n = a->i;
if (n < 0)
n = term.row + n;
if (n > term.scr)
n = term.scr;
if (term.scr > 0) {
term.scr -= n;
selscroll(0, -n);
tfulldirt();
}
}
void
kscrollup(const Arg* a)
{
int n = a->i;
if (n < 0)
n = term.row + n;
if (term.scr <= histsize - n) {
term.scr += n;
selscroll(0, n);
tfulldirt();
}
}
void
tscrolldown(int orig, int n, int copyhist)
{ {
int i; int i;
Line temp; Line temp;
LIMIT(n, 0, term.bot-orig+1); LIMIT(n, 0, term.bot-orig+1);
if (copyhist) {
term.histi = (term.histi - 1 + histsize) % histsize;
temp = term.hist[term.histi];
term.hist[term.histi] = term.line[term.bot];
term.line[term.bot] = temp;
}
tsetdirt(orig, term.bot-n); tsetdirt(orig, term.bot-n);
tclearregion(0, term.bot-n+1, term.col-1, term.bot); tclearregion(0, term.bot-n+1, term.col-1, term.bot);
@ -1589,13 +1646,20 @@ tscrolldown(int orig, int n)
} }
void void
tscrollup(int orig, int n) tscrollup(int orig, int n, int copyhist)
{ {
int i; int i;
Line temp; Line temp;
LIMIT(n, 0, term.bot-orig+1); LIMIT(n, 0, term.bot-orig+1);
if (copyhist) {
term.histi = (term.histi + 1) % histsize;
temp = term.hist[term.histi];
term.hist[term.histi] = term.line[orig];
term.line[orig] = temp;
}
tclearregion(0, orig, term.col-1, orig+n-1); tclearregion(0, orig, term.col-1, orig+n-1);
tsetdirt(orig+n, term.bot); tsetdirt(orig+n, term.bot);
@ -1644,7 +1708,7 @@ tnewline(int first_col)
int y = term.c.y; int y = term.c.y;
if (y == term.bot) { if (y == term.bot) {
tscrollup(term.top, 1); tscrollup(term.top, 1, 1);
} else { } else {
y++; y++;
} }
@ -1809,14 +1873,14 @@ void
tinsertblankline(int n) tinsertblankline(int n)
{ {
if (BETWEEN(term.c.y, term.top, term.bot)) if (BETWEEN(term.c.y, term.top, term.bot))
tscrolldown(term.c.y, n); tscrolldown(term.c.y, n, 0);
} }
void void
tdeleteline(int n) tdeleteline(int n)
{ {
if (BETWEEN(term.c.y, term.top, term.bot)) if (BETWEEN(term.c.y, term.top, term.bot))
tscrollup(term.c.y, n); tscrollup(term.c.y, n, 0);
} }
int32_t int32_t
@ -2246,11 +2310,11 @@ csihandle(void)
break; break;
case 'S': /* SU -- Scroll <n> line up */ case 'S': /* SU -- Scroll <n> line up */
DEFAULT(csiescseq.arg[0], 1); DEFAULT(csiescseq.arg[0], 1);
tscrollup(term.top, csiescseq.arg[0]); tscrollup(term.top, csiescseq.arg[0], 0);
break; break;
case 'T': /* SD -- Scroll <n> line down */ case 'T': /* SD -- Scroll <n> line down */
DEFAULT(csiescseq.arg[0], 1); DEFAULT(csiescseq.arg[0], 1);
tscrolldown(term.top, csiescseq.arg[0]); tscrolldown(term.top, csiescseq.arg[0], 0);
break; break;
case 'L': /* IL -- Insert <n> blank lines */ case 'L': /* IL -- Insert <n> blank lines */
DEFAULT(csiescseq.arg[0], 1); DEFAULT(csiescseq.arg[0], 1);
@ -2787,7 +2851,7 @@ eschandle(uchar ascii)
return 0; return 0;
case 'D': /* IND -- Linefeed */ case 'D': /* IND -- Linefeed */
if (term.c.y == term.bot) { if (term.c.y == term.bot) {
tscrollup(term.top, 1); tscrollup(term.top, 1, 1);
} else { } else {
tmoveto(term.c.x, term.c.y+1); tmoveto(term.c.x, term.c.y+1);
} }
@ -2800,7 +2864,7 @@ eschandle(uchar ascii)
break; break;
case 'M': /* RI -- Reverse index */ case 'M': /* RI -- Reverse index */
if (term.c.y == term.top) { if (term.c.y == term.top) {
tscrolldown(term.top, 1); tscrolldown(term.top, 1, 1);
} else { } else {
tmoveto(term.c.x, term.c.y-1); tmoveto(term.c.x, term.c.y-1);
} }
@ -2987,7 +3051,7 @@ check_control_code:
void void
tresize(int col, int row) tresize(int col, int row)
{ {
int i; int i, j;
int minrow = MIN(row, term.row); int minrow = MIN(row, term.row);
int mincol = MIN(col, term.col); int mincol = MIN(col, term.col);
int *bp; int *bp;
@ -3024,6 +3088,14 @@ tresize(int col, int row)
term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty)); term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs)); term.tabs = xrealloc(term.tabs, col * sizeof(*term.tabs));
for (i = 0; i < histsize; i++) {
term.hist[i] = xrealloc(term.hist[i], col * sizeof(Glyph));
for (j = mincol; j < col; j++) {
term.hist[i][j] = term.c.attr;
term.hist[i][j].u = ' ';
}
}
/* resize each row to new width, zero-pad if needed */ /* resize each row to new width, zero-pad if needed */
for (i = 0; i < minrow; i++) { for (i = 0; i < minrow; i++) {
term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph)); term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
@ -3413,11 +3485,22 @@ wlinit(void)
wl.surface = wl_compositor_create_surface(wl.cmp); wl.surface = wl_compositor_create_surface(wl.cmp);
wl_surface_add_listener(wl.surface, &surflistener, NULL); wl_surface_add_listener(wl.surface, &surflistener, NULL);
if (wl.shell == NULL)
die("wl.shell is not initialized.\n");
wl.xdgsurface = zxdg_shell_v6_get_xdg_surface(wl.shell, wl.surface); wl.xdgsurface = zxdg_shell_v6_get_xdg_surface(wl.shell, wl.surface);
if (wl.xdgsurface == NULL)
die("wl.xdgsurface is not initialized.\n");
zxdg_surface_v6_add_listener(wl.xdgsurface, &xdgsurflistener, NULL); zxdg_surface_v6_add_listener(wl.xdgsurface, &xdgsurflistener, NULL);
wl.xdgtoplevel = zxdg_surface_v6_get_toplevel(wl.xdgsurface); wl.xdgtoplevel = zxdg_surface_v6_get_toplevel(wl.xdgsurface);
if (wl.xdgtoplevel == NULL)
die("wl.xdgtoplevel is not initialized.\n");
zxdg_toplevel_v6_set_app_id(wl.xdgtoplevel, zxdg_toplevel_v6_set_app_id(wl.xdgtoplevel,
opt_class ? opt_class : termname); opt_class ? opt_class : termname);
zxdg_toplevel_v6_add_listener(wl.xdgtoplevel, &xdgtoplevellistener, zxdg_toplevel_v6_add_listener(wl.xdgtoplevel, &xdgtoplevellistener,
NULL); NULL);
@ -3845,10 +3928,10 @@ drawregion(int x1, int y1, int x2, int y2)
continue; continue;
term.dirty[y] = 0; term.dirty[y] = 0;
base = term.line[y][0]; base = TLINE(y)[0];
ic = ib = ox = 0; ic = ib = ox = 0;
for (x = x1; x < x2; x++) { for (x = x1; x < x2; x++) {
new = term.line[y][x]; new = TLINE(y)[x];
if (new.mode == ATTR_WDUMMY) if (new.mode == ATTR_WDUMMY)
continue; continue;
if (ena_sel && selected(x, y)) if (ena_sel && selected(x, y))
@ -3869,7 +3952,8 @@ drawregion(int x1, int y1, int x2, int y2)
if (ib > 0) if (ib > 0)
wldraws(buf, base, ox, y, ic, ib); wldraws(buf, base, ox, y, ic, ib);
} }
wldrawcursor(); if(term.scr == 0)
wldrawcursor();
} }
void void
@ -3957,6 +4041,8 @@ regglobal(void *data, struct wl_registry *registry, uint32_t name,
} else if (strcmp(interface, "zxdg_shell_v6") == 0) { } else if (strcmp(interface, "zxdg_shell_v6") == 0) {
wl.shell = wl_registry_bind(registry, name, wl.shell = wl_registry_bind(registry, name,
&zxdg_shell_v6_interface, 1); &zxdg_shell_v6_interface, 1);
if(wl.shell == NULL)
die("wl.shell is null.\n");
zxdg_shell_v6_add_listener(wl.shell, &xdgshelllistener, NULL); zxdg_shell_v6_add_listener(wl.shell, &xdgshelllistener, NULL);
} else if (strcmp(interface, "wl_shm") == 0) { } else if (strcmp(interface, "wl_shm") == 0) {
wl.shm = wl_registry_bind(registry, name, &wl_shm_interface, 1); wl.shm = wl_registry_bind(registry, name, &wl_shm_interface, 1);