5 Commits

Author SHA1 Message Date
b1e6c91433 Applied clickurl patch 2025-05-05 12:56:54 +01:00
98610fcd37 Do not interpret CSI ? u as DECRC
The kitty keyboard protocol docs recommend CSI ? u to query support for
that protocol, see https://sw.kovidgoyal.net/kitty/keyboard-protocol/

For better or worse, fish shell uses this query to work around bugs
in other terminals triggered by requesting that protocol via CSI = 5 u.

Unfortunately, st interprets CSI ? u as DECRC (restore cursor
position). reproduce with 'printf "\x1b[?u"; cat'.

fish could work around this by switching to the alternate screen
before running this query; but that might cause tearing on terminals
that don't support Synchronized Output. I'm not sure.

In the meantime, let's correct our parser.

This adds a redundant else-after-return, for consistency with the
surrounding code.
2025-01-30 17:50:37 +01:00
6009e6e25b Clear screen: Fix edge case
With sequence \e[1J, if cursor is on second line, clear the first line.
2024-12-06 13:42:50 +01:00
a0274bc20e fix BadMatch error when embedding on some windows
When embedded, st fails with BadMatch error if the embedder's window has
non-default colormap/depth/visual.  This commit fixes that by creating
st's window inside root and then reparent it into embedder.

The reference window for dc.gc is also changed to match root's visuals.

A similar commit had been made for dmenu[1].
See this issue[2] on github for context.

[1]: https://git.suckless.org/dmenu/commit/0fe460dbd469a1d5b6a7140d0e1801935e4a923b.html
[2]: https://github.com/phillbush/xfiles/issues/47
2024-08-09 13:34:56 +02:00
5dbcca4926 support colons in SGR character attributes
Patch by Mikhail Kot <to@myrrc.dev>
With some modifications to behave more like xterm (see note below).

Example:

	printf '\033[48;2;255:0:0mtest\n'

https://invisible-island.net/xterm/ctlseqs/ctlseqs.html

Some notes:

"CSI Pm m  Character Attributes (SGR).
[...]
o   xterm allows either colons (standard) or semicolons
(legacy) to separate the subparameters (but after the
first colon, colons must be used).
2024-05-01 20:45:39 +02:00
5 changed files with 341 additions and 8 deletions

@ -472,3 +472,14 @@ static char ascii_printable[] =
" !\"#$%&'()*+,-./0123456789:;<=>?"
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
"`abcdefghijklmnopqrstuvwxyz{|}~";
/*
* Open urls starting with urlprefixes, contatining urlchars
* by passing as ARG1 to urlhandler.
*/
char* urlhandler = "xdg-open";
char urlchars[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
"abcdefghijklmnopqrstuvwxyz"
"0123456789-._~:/?#@!$&'*+,;=%";
char* urlprefixes[] = {"http://", "https://", NULL};

@ -0,0 +1,192 @@
From d5b492049f48dc411b0dd7dc01a403304c20438d Mon Sep 17 00:00:00 2001
From: Jishnu Sen <jishnu1@gmail.com>
Date: Sun, 7 Apr 2024 22:54:46 -0700
Subject: [PATCH] Highlight URLs with control and follow with click
---
config.def.h | 11 +++++++
st.c | 89 ++++++++++++++++++++++++++++++++++++++++++++++++++++
st.h | 9 ++++++
x.c | 24 +++++++++++++-
4 files changed, 132 insertions(+), 1 deletion(-)
diff --git a/config.def.h b/config.def.h
index 91ab8ca..4961830 100644
--- a/config.def.h
+++ b/config.def.h
@@ -472,3 +472,14 @@ static char ascii_printable[] =
" !\"#$%&'()*+,-./0123456789:;<=>?"
"@ABCDEFGHIJKLMNOPQRSTUVWXYZ[\\]^_"
"`abcdefghijklmnopqrstuvwxyz{|}~";
+
+/*
+ * Open urls starting with urlprefixes, contatining urlchars
+ * by passing as ARG1 to urlhandler.
+ */
+char* urlhandler = "xdg-open";
+char urlchars[] =
+ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
+ "abcdefghijklmnopqrstuvwxyz"
+ "0123456789-._~:/?#@!$&'*+,;=%";
+char* urlprefixes[] = {"http://", "https://", NULL};
diff --git a/st.c b/st.c
index 51049ba..8f2156c 100644
--- a/st.c
+++ b/st.c
@@ -643,6 +643,95 @@ getsel(void)
return str;
}
+char *
+strstrany(char* s, char** strs) {
+ char *match;
+ for (int i = 0; strs[i]; i++) {
+ if ((match = strstr(s, strs[i]))) {
+ return match;
+ }
+ }
+ return NULL;
+}
+
+void
+highlighturls(void)
+{
+ char *match;
+ char *linestr = calloc(sizeof(char), term.col+1); /* assume ascii */
+ for (int i = term.top; i < term.bot; i++) {
+ int url_start = -1;
+ for (int j = 0; j < term.col; j++) {
+ if (term.line[i][j].u < 127) {
+ linestr[j] = term.line[i][j].u;
+ }
+ linestr[term.col] = '\0';
+ }
+ while ((match = strstrany(linestr + url_start + 1, urlprefixes))) {
+ url_start = match - linestr;
+ for (int c = url_start; c < term.col && strchr(urlchars, linestr[c]); c++) {
+ term.line[i][c].mode |= ATTR_URL;
+ tsetdirt(i, c);
+ }
+ }
+ }
+ free(linestr);
+}
+
+void
+unhighlighturls(void)
+{
+ for (int i = term.top; i < term.bot; i++) {
+ for (int j = 0; j < term.col; j++) {
+ Glyph* g = &term.line[i][j];
+ if (g->mode & ATTR_URL) {
+ g->mode &= ~ATTR_URL;
+ tsetdirt(i, j);
+ }
+ }
+ }
+ return;
+}
+
+void
+followurl(int x, int y) {
+ char *linestr = calloc(sizeof(char), term.col+1); /* assume ascii */
+ char *match;
+ for (int i = 0; i < term.col; i++) {
+ if (term.line[x][i].u < 127) {
+ linestr[i] = term.line[x][i].u;
+ }
+ linestr[term.col] = '\0';
+ }
+ int url_start = -1;
+ while ((match = strstrany(linestr + url_start + 1, urlprefixes))) {
+ url_start = match - linestr;
+ int url_end = url_start;
+ for (int c = url_start; c < term.col && strchr(urlchars, linestr[c]); c++) {
+ url_end++;
+ }
+ if (url_start <= y && y < url_end) {
+ linestr[url_end] = '\0';
+ break;
+ }
+ }
+ if (url_start == -1) {
+ free(linestr);
+ return;
+ }
+
+ pid_t chpid;
+ if ((chpid = fork()) == 0) {
+ if (fork() == 0)
+ execlp(urlhandler, urlhandler, linestr + url_start, NULL);
+ exit(1);
+ }
+ if (chpid > 0)
+ waitpid(chpid, NULL, 0);
+ free(linestr);
+ unhighlighturls();
+}
+
void
selclear(void)
{
diff --git a/st.h b/st.h
index 519b9bd..354e7f9 100644
--- a/st.h
+++ b/st.h
@@ -34,6 +34,7 @@ enum glyph_attribute {
ATTR_WIDE = 1 << 9,
ATTR_WDUMMY = 1 << 10,
ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT,
+ ATTR_URL = 1 << 14,
};
enum selection_mode {
@@ -105,6 +106,10 @@ void selextend(int, int, int, int);
int selected(int, int);
char *getsel(void);
+void highlighturls(void);
+void unhighlighturls(void);
+void followurl(int, int);
+
size_t utf8encode(Rune, char *);
void *xmalloc(size_t);
@@ -126,3 +131,7 @@ extern unsigned int tabspaces;
extern unsigned int defaultfg;
extern unsigned int defaultbg;
extern unsigned int defaultcs;
+extern char *urlhandler;
+extern char urlchars[];
+extern char *urlprefixes[];
+extern int nurlprefixes;
diff --git a/x.c b/x.c
index 8a16faa..13f68e4 100644
--- a/x.c
+++ b/x.c
@@ -191,6 +191,7 @@ static void usage(void);
static void (*handler[LASTEvent])(XEvent *) = {
[KeyPress] = kpress,
+ [KeyRelease] = kpress,
[ClientMessage] = cmessage,
[ConfigureNotify] = resize,
[VisibilityNotify] = visibility,
@@ -445,6 +446,15 @@ mouseaction(XEvent *e, uint release)
/* ignore Button<N>mask for Button<N> - it's set on release */
uint state = e->xbutton.state & ~buttonmask(e->xbutton.button);
+ if (release == 0 &&
+ e->xbutton.button == Button1 &&
+ (match(ControlMask, state) ||
+ match(ControlMask, state & ~forcemousemod))) {
+ followurl(evrow(e), evcol(e));
+ return 1;
+ }
+
+
for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
if (ms->release == release &&
ms->button == e->xbutton.button &&
2.44.0

102
st.c

@ -636,6 +636,95 @@ getsel(void)
return str;
}
char *
strstrany(char* s, char** strs) {
char *match;
for (int i = 0; strs[i]; i++) {
if ((match = strstr(s, strs[i]))) {
return match;
}
}
return NULL;
}
void
highlighturls(void)
{
char *match;
char *linestr = calloc(sizeof(char), term.col+1); /* assume ascii */
for (int i = term.top; i < term.bot; i++) {
int url_start = -1;
for (int j = 0; j < term.col; j++) {
if (term.line[i][j].u < 127) {
linestr[j] = term.line[i][j].u;
}
linestr[term.col] = '\0';
}
while ((match = strstrany(linestr + url_start + 1, urlprefixes))) {
url_start = match - linestr;
for (int c = url_start; c < term.col && strchr(urlchars, linestr[c]); c++) {
term.line[i][c].mode |= ATTR_URL;
tsetdirt(i, c);
}
}
}
free(linestr);
}
void
unhighlighturls(void)
{
for (int i = term.top; i < term.bot; i++) {
for (int j = 0; j < term.col; j++) {
Glyph* g = &term.line[i][j];
if (g->mode & ATTR_URL) {
g->mode &= ~ATTR_URL;
tsetdirt(i, j);
}
}
}
return;
}
void
followurl(int x, int y) {
char *linestr = calloc(sizeof(char), term.col+1); /* assume ascii */
char *match;
for (int i = 0; i < term.col; i++) {
if (term.line[x][i].u < 127) {
linestr[i] = term.line[x][i].u;
}
linestr[term.col] = '\0';
}
int url_start = -1;
while ((match = strstrany(linestr + url_start + 1, urlprefixes))) {
url_start = match - linestr;
int url_end = url_start;
for (int c = url_start; c < term.col && strchr(urlchars, linestr[c]); c++) {
url_end++;
}
if (url_start <= y && y < url_end) {
linestr[url_end] = '\0';
break;
}
}
if (url_start == -1) {
free(linestr);
return;
}
pid_t chpid;
if ((chpid = fork()) == 0) {
if (fork() == 0)
execlp(urlhandler, urlhandler, linestr + url_start, NULL);
exit(1);
}
if (chpid > 0)
waitpid(chpid, NULL, 0);
free(linestr);
unhighlighturls();
}
void
selclear(void)
{
@ -1132,6 +1221,7 @@ csiparse(void)
{
char *p = csiescseq.buf, *np;
long int v;
int sep = ';'; /* colon or semi-colon, but not both */
csiescseq.narg = 0;
if (*p == '?') {
@ -1149,7 +1239,9 @@ csiparse(void)
v = -1;
csiescseq.arg[csiescseq.narg++] = v;
p = np;
if (*p != ';' || csiescseq.narg == ESC_ARG_SIZ)
if (sep == ';' && *p == ':')
sep = ':'; /* allow override to colon once */
if (*p != sep || csiescseq.narg == ESC_ARG_SIZ)
break;
p++;
}
@ -1702,7 +1794,7 @@ csihandle(void)
}
break;
case 1: /* above */
if (term.c.y > 1)
if (term.c.y > 0)
tclearregion(0, 0, term.col-1, term.c.y-1);
tclearregion(0, term.c.y, term.c.x, term.c.y);
break;
@ -1798,7 +1890,11 @@ csihandle(void)
tcursor(CURSOR_SAVE);
break;
case 'u': /* DECRC -- Restore cursor position (ANSI.SYS) */
tcursor(CURSOR_LOAD);
if (csiescseq.priv) {
goto unknown;
} else {
tcursor(CURSOR_LOAD);
}
break;
case ' ':
switch (csiescseq.mode[1]) {

9
st.h

@ -34,6 +34,7 @@ enum glyph_attribute {
ATTR_WIDE = 1 << 9,
ATTR_WDUMMY = 1 << 10,
ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT,
ATTR_URL = 1 << 14,
};
enum selection_mode {
@ -105,6 +106,10 @@ void selextend(int, int, int, int);
int selected(int, int);
char *getsel(void);
void highlighturls(void);
void unhighlighturls(void);
void followurl(int, int);
size_t utf8encode(Rune, char *);
void *xmalloc(size_t);
@ -124,3 +129,7 @@ extern unsigned int tabspaces;
extern unsigned int defaultfg;
extern unsigned int defaultbg;
extern unsigned int defaultcs;
extern char *urlhandler;
extern char urlchars[];
extern char *urlprefixes[];
extern int nurlprefixes;

35
x.c

@ -191,6 +191,7 @@ static void usage(void);
static void (*handler[LASTEvent])(XEvent *) = {
[KeyPress] = kpress,
[KeyRelease] = kpress,
[ClientMessage] = cmessage,
[ConfigureNotify] = resize,
[VisibilityNotify] = visibility,
@ -452,6 +453,15 @@ mouseaction(XEvent *e, uint release)
/* ignore Button<N>mask for Button<N> - it's set on release */
uint state = e->xbutton.state & ~buttonmask(e->xbutton.button);
if (release == 0 &&
e->xbutton.button == Button1 &&
(match(ControlMask, state) ||
match(ControlMask, state & ~forcemousemod))) {
followurl(evrow(e), evcol(e));
return 1;
}
for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
if (ms->release == release &&
ms->button == e->xbutton.button &&
@ -1131,7 +1141,7 @@ xinit(int cols, int rows)
{
XGCValues gcvalues;
Cursor cursor;
Window parent;
Window parent, root;
pid_t thispid = getpid();
XColor xmousefg, xmousebg;
@ -1168,16 +1178,19 @@ xinit(int cols, int rows)
| ButtonMotionMask | ButtonPressMask | ButtonReleaseMask;
xw.attrs.colormap = xw.cmap;
root = XRootWindow(xw.dpy, xw.scr);
if (!(opt_embed && (parent = strtol(opt_embed, NULL, 0))))
parent = XRootWindow(xw.dpy, xw.scr);
xw.win = XCreateWindow(xw.dpy, parent, xw.l, xw.t,
parent = root;
xw.win = XCreateWindow(xw.dpy, root, xw.l, xw.t,
win.w, win.h, 0, XDefaultDepth(xw.dpy, xw.scr), InputOutput,
xw.vis, CWBackPixel | CWBorderPixel | CWBitGravity
| CWEventMask | CWColormap, &xw.attrs);
if (parent != root)
XReparentWindow(xw.dpy, xw.win, parent, xw.l, xw.t);
memset(&gcvalues, 0, sizeof(gcvalues));
gcvalues.graphics_exposures = False;
dc.gc = XCreateGC(xw.dpy, parent, GCGraphicsExposures,
dc.gc = XCreateGC(xw.dpy, xw.win, GCGraphicsExposures,
&gcvalues);
xw.buf = XCreatePixmap(xw.dpy, xw.win, win.w, win.h,
DefaultDepth(xw.dpy, xw.scr));
@ -1492,7 +1505,7 @@ xdrawglyphfontspecs(const XftGlyphFontSpec *specs, Glyph base, int len, int x, i
XftDrawGlyphFontSpec(xw.draw, fg, specs, len);
/* Render underline and strikethrough. */
if (base.mode & ATTR_UNDERLINE) {
if (base.mode & ATTR_UNDERLINE || base.mode & ATTR_URL) {
XftDrawRect(xw.draw, fg, winx, winy + dc.font.ascent * chscale + 1,
width, 1);
}
@ -1856,6 +1869,18 @@ kpress(XEvent *ev)
} else {
len = XLookupString(e, buf, sizeof buf, &ksym, NULL);
}
/* 0. highlight URLs when control held */
if (ksym == XK_Control_L) {
highlighturls();
} else if (ev->type == KeyRelease && e->keycode == XKeysymToKeycode(e->display, XK_Control_L)) {
unhighlighturls();
}
/* KeyRelease not relevant to shortcuts */
if (ev->type == KeyRelease)
return;
/* 1. shortcuts */
for (bp = shortcuts; bp < shortcuts + LEN(shortcuts); bp++) {
if (ksym == bp->keysym && match(bp->mod, e->state)) {