add uparrow and downarrow instead of pgup and pgdown

This commit is contained in:
Ohio2 2021-07-30 16:09:49 +02:00
parent 239574f91f
commit 802b10e8da
8 changed files with 388 additions and 32 deletions

View file

@ -1,11 +1,10 @@
--- config.def.h
+++ config.def.h
@@ -199,6 +199,8 @@ static Shortcut shortcuts[] = {
@@ -214,6 +229,7 @@ static Shortcut shortcuts[] = {
{ TERMMOD, XK_Y, selpaste, {.i = 0} },
{ ShiftMask, XK_Insert, selpaste, {.i = 0} },
{ TERMMOD, XK_Num_Lock, numlock, {.i = 0} },
+ { ShiftMask, XK_Page_Up, kscrollup, {.i = -1} },
+ { ShiftMask, XK_Page_Down, kscrolldown, {.i = -1} },
+ { MODKEY, XK_c, normalMode, {.i = 0} },
};
/*

View file

@ -193,8 +193,8 @@ static Shortcut shortcuts[] = {
{ TERMMOD, XK_Y, selpaste, {.i = 0} },
{ ShiftMask, XK_Insert, selpaste, {.i = 0} },
{ TERMMOD, XK_Num_Lock, numlock, {.i = 0} },
{ ShiftMask, XK_Page_Up, kscrollup, {.i = -1} },
{ ShiftMask, XK_Page_Down, kscrolldown, {.i = -1} },
{ ShiftMask, XK_Up, kscrollup, {.i = -1} },
{ ShiftMask, XK_Down, kscrolldown, {.i = -1} },
{ MODKEY, XK_l, copyurl, {.i = 0} },
};

BIN
st

Binary file not shown.

125
st.c.orig
View file

@ -35,6 +35,7 @@
#define ESC_ARG_SIZ 16
#define STR_BUF_SIZ ESC_BUF_SIZ
#define STR_ARG_SIZ ESC_ARG_SIZ
#define HISTSIZE 2000
/* macros */
#define IS_SET(flag) ((term.mode & (flag)) != 0)
@ -42,6 +43,9 @@
#define ISCONTROLC1(c) (BETWEEN(c, 0x80, 0x9f))
#define ISCONTROL(c) (ISCONTROLC0(c) || ISCONTROLC1(c))
#define ISDELIM(u) (u && wcschr(worddelimiters, u))
#define TLINE(y) ((y) < term.scr ? term.hist[((y) + term.histi - \
term.scr + HISTSIZE + 1) % HISTSIZE] : \
term.line[(y) - term.scr])
enum term_mode {
MODE_WRAP = 1 << 0,
@ -115,6 +119,9 @@ typedef struct {
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 */
TCursor c; /* cursor */
int ocx; /* old cursor col */
@ -184,8 +191,8 @@ static void tnewline(int);
static void tputtab(int);
static void tputc(Rune);
static void treset(void);
static void tscrollup(int, int);
static void tscrolldown(int, int);
static void tscrollup(int, int, int);
static void tscrolldown(int, int, int);
static void tsetattr(int *, int);
static void tsetchar(Rune, Glyph *, int, int);
static void tsetdirt(int, int);
@ -416,10 +423,10 @@ tlinelen(int y)
{
int i = term.col;
if (term.line[y][i - 1].mode & ATTR_WRAP)
if (TLINE(y)[i - 1].mode & ATTR_WRAP)
return i;
while (i > 0 && term.line[y][i - 1].u == ' ')
while (i > 0 && TLINE(y)[i - 1].u == ' ')
--i;
return i;
@ -528,7 +535,7 @@ selsnap(int *x, int *y, int direction)
* Snap around if the word wraps around at the end or
* beginning of a line.
*/
prevgp = &term.line[*y][*x];
prevgp = &TLINE(*y)[*x];
prevdelim = ISDELIM(prevgp->u);
for (;;) {
newx = *x + direction;
@ -543,14 +550,14 @@ selsnap(int *x, int *y, int direction)
yt = *y, xt = *x;
else
yt = newy, xt = newx;
if (!(term.line[yt][xt].mode & ATTR_WRAP))
if (!(TLINE(yt)[xt].mode & ATTR_WRAP))
break;
}
if (newx >= tlinelen(newy))
break;
gp = &term.line[newy][newx];
gp = &TLINE(newy)[newx];
delim = ISDELIM(gp->u);
if (!(gp->mode & ATTR_WDUMMY) && (delim != prevdelim
|| (delim && gp->u != prevgp->u)))
@ -571,14 +578,14 @@ selsnap(int *x, int *y, int direction)
*x = (direction < 0) ? 0 : term.col - 1;
if (direction < 0) {
for (; *y > 0; *y += direction) {
if (!(term.line[*y-1][term.col-1].mode
if (!(TLINE(*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
if (!(TLINE(*y)[term.col-1].mode
& ATTR_WRAP)) {
break;
}
@ -609,13 +616,13 @@ getsel(void)
}
if (sel.type == SEL_RECTANGULAR) {
gp = &term.line[y][sel.nb.x];
gp = &TLINE(y)[sel.nb.x];
lastx = sel.ne.x;
} 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;
}
last = &term.line[y][MIN(lastx, linelen-1)];
last = &TLINE(y)[MIN(lastx, linelen-1)];
while (last >= gp && last->u == ' ')
--last;
@ -850,6 +857,9 @@ void
ttywrite(const char *s, size_t n, int may_echo)
{
const char *next;
Arg arg = (Arg) { .i = term.scr };
kscrolldown(&arg);
if (may_echo && IS_SET(MODE_ECHO))
twrite(s, n, 1);
@ -1061,13 +1071,53 @@ tswapscreen(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;
Line temp;
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);
tclearregion(0, term.bot-n+1, term.col-1, term.bot);
@ -1077,17 +1127,28 @@ tscrolldown(int orig, int n)
term.line[i-n] = temp;
}
selscroll(orig, n);
if (term.scr == 0)
selscroll(orig, n);
}
void
tscrollup(int orig, int n)
tscrollup(int orig, int n, int copyhist)
{
int i;
Line temp;
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;
}
if (term.scr > 0 && term.scr < HISTSIZE)
term.scr = MIN(term.scr + n, HISTSIZE-1);
tclearregion(0, orig, term.col-1, orig+n-1);
tsetdirt(orig+n, term.bot);
@ -1097,7 +1158,8 @@ tscrollup(int orig, int n)
term.line[i+n] = temp;
}
selscroll(orig, -n);
if (term.scr == 0)
selscroll(orig, -n);
}
void
@ -1126,7 +1188,7 @@ tnewline(int first_col)
int y = term.c.y;
if (y == term.bot) {
tscrollup(term.top, 1);
tscrollup(term.top, 1, 1);
} else {
y++;
}
@ -1291,14 +1353,14 @@ void
tinsertblankline(int n)
{
if (BETWEEN(term.c.y, term.top, term.bot))
tscrolldown(term.c.y, n);
tscrolldown(term.c.y, n, 0);
}
void
tdeleteline(int n)
{
if (BETWEEN(term.c.y, term.top, term.bot))
tscrollup(term.c.y, n);
tscrollup(term.c.y, n, 0);
}
int32_t
@ -1735,11 +1797,11 @@ csihandle(void)
break;
case 'S': /* SU -- Scroll <n> line up */
DEFAULT(csiescseq.arg[0], 1);
tscrollup(term.top, csiescseq.arg[0]);
tscrollup(term.top, csiescseq.arg[0], 0);
break;
case 'T': /* SD -- Scroll <n> line down */
DEFAULT(csiescseq.arg[0], 1);
tscrolldown(term.top, csiescseq.arg[0]);
tscrolldown(term.top, csiescseq.arg[0], 0);
break;
case 'L': /* IL -- Insert <n> blank lines */
DEFAULT(csiescseq.arg[0], 1);
@ -2243,7 +2305,7 @@ eschandle(uchar ascii)
return 0;
case 'D': /* IND -- Linefeed */
if (term.c.y == term.bot) {
tscrollup(term.top, 1);
tscrollup(term.top, 1, 1);
} else {
tmoveto(term.c.x, term.c.y+1);
}
@ -2256,7 +2318,7 @@ eschandle(uchar ascii)
break;
case 'M': /* RI -- Reverse index */
if (term.c.y == term.top) {
tscrolldown(term.top, 1);
tscrolldown(term.top, 1, 1);
} else {
tmoveto(term.c.x, term.c.y-1);
}
@ -2466,7 +2528,7 @@ twrite(const char *buf, int buflen, int show_ctrl)
void
tresize(int col, int row)
{
int i;
int i, j;
int minrow = MIN(row, term.row);
int mincol = MIN(col, term.col);
int *bp;
@ -2503,6 +2565,14 @@ tresize(int col, int row)
term.dirty = xrealloc(term.dirty, row * sizeof(*term.dirty));
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 */
for (i = 0; i < minrow; i++) {
term.line[i] = xrealloc(term.line[i], col * sizeof(Glyph));
@ -2561,7 +2631,7 @@ drawregion(int x1, int y1, int x2, int y2)
continue;
term.dirty[y] = 0;
xdrawline(term.line[y], x1, y, x2);
xdrawline(TLINE(y), x1, y, x2);
}
}
@ -2582,8 +2652,9 @@ draw(void)
cx--;
drawregion(0, 0, term.col, term.row);
xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
if (term.scr == 0)
xdrawcursor(cx, term.c.y, term.line[term.c.y][cx],
term.ocx, term.ocy, term.line[term.ocy][term.ocx]);
term.ocx = cx;
term.ocy = term.c.y;
xfinishdraw();

262
st.c.rej Normal file
View file

@ -0,0 +1,262 @@
--- 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;

23
utils.h Normal file
View file

@ -0,0 +1,23 @@
/// Dynamic memory-chunk, with (1) datatype size, (2/3) initialized / allocated chunk, (4) content
typedef struct { uint8_t const elSize; uint32_t init, alloc; char* content; } DynamicArray;
#define UTF8_ARRAY {4, 0, 0, NULL}
static inline int p_alloc(DynamicArray *s, uint32_t amount) {
uint32_t const diff=s->init+s->elSize*amount-s->alloc, nas=s->alloc+max(diff,15)*s->elSize;
if (s->alloc < s->init + s->elSize * amount) {
char* tmp = realloc(s->content, nas);
if (!tmp) return 0;
s->alloc = nas, s->content = tmp;
}
return 1;
}
static inline char *view(DynamicArray * s, uint32_t i) { return s->content + i*s->elSize; }
static inline char *end(DynamicArray *s, uint32_t i) { return s->content +s->init-(i+1)*s->elSize; }
static inline uint32_t getU32(DynamicArray* s, uint32_t i, int b) { return *((uint32_t*) (b ?view(s,i) :end(s,i))); }
static char *expand(DynamicArray *s) { if (!p_alloc(s, 1)) return NULL; s->init += s->elSize; return end(s, 0); }
static inline void pop(DynamicArray* s) { s->init -= s->elSize; }
static inline void empty(DynamicArray* s) { s->init = 0; }
static inline int size(DynamicArray const * s) { return s->init / s->elSize; }
static inline void assign(DynamicArray* s, DynamicArray const *o) {
if (p_alloc(s, size(o))) memcpy(s->content, o->content, (s->init=o->init));
}

1
win.h
View file

@ -19,6 +19,7 @@ enum win_mode {
MODE_MOUSEMANY = 1 << 15,
MODE_BRCKTPASTE = 1 << 16,
MODE_NUMLOCK = 1 << 17,
MODE_NORMAL = 1 << 18,
MODE_MOUSE = MODE_MOUSEBTN|MODE_MOUSEMOTION|MODE_MOUSEX10\
|MODE_MOUSEMANY,
};

BIN
x.o

Binary file not shown.