[wiki] [sites] Add patch for clicking URLs || Kyle Chui

From: <git_AT_suckless.org>
Date: Wed, 10 Apr 2024 01:34:36 +0200

commit 649348cbe04526502ce4fa572702d38146ea5d31
Author: Kyle Chui <kyle.chui+suckless_AT_pm.me>
Date: Tue Apr 9 16:33:49 2024 -0700

    Add patch for clicking URLs

diff --git a/st.suckless.org/patches/clickurl-nocontrol/index.md b/st.suckless.org/patches/clickurl-nocontrol/index.md
new file mode 100644
index 00000000..4df73421
--- /dev/null
+++ b/st.suckless.org/patches/clickurl-nocontrol/index.md
_AT_@ -0,0 +1,19 @@
+clickurl
+=======
+
+Description
+-----------
+Underlines URLs, open with mouse button 1
+
+Notes
+-----
+* URLs spanning multiple lines are not handled
+* Heavy inspiration from clickurl patch
+
+Download
+--------
+* [st-clickurl-nocontrol-0.8.5.diff](st-clickurl-nocontrol-0.8.5.diff)
+
+Authors
+-------
+* Kyle Chui - <kyle.chui+suckless_AT_pm.me>
diff --git a/st.suckless.org/patches/clickurl-nocontrol/st-clickurl-nocontrol-0.8.5.diff b/st.suckless.org/patches/clickurl-nocontrol/st-clickurl-nocontrol-0.8.5.diff
new file mode 100644
index 00000000..307ee287
--- /dev/null
+++ b/st.suckless.org/patches/clickurl-nocontrol/st-clickurl-nocontrol-0.8.5.diff
_AT_@ -0,0 +1,215 @@
+From 8d13e4a296f0b2a625df10c8ee6d2fc96ec97580 Mon Sep 17 00:00:00 2001
+From: Kyle Chui <kyle.chui+suckless_AT_pm.me>
+Date: Tue, 9 Apr 2024 16:31:25 -0700
+Subject: [PATCH] Underline URLs and follow with click
+
+---
+ config.def.h | 11 +++++++
+ st.c | 88 ++++++++++++++++++++++++++++++++++++++++++++++++++++
+ st.h | 9 ++++++
+ x.c | 12 ++++++-
+ 4 files changed, 119 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
+_AT_@ -472,3 +472,14 @@ static char ascii_printable[] =
+ " !\"#$%&'()*+,-./0123456789:;<=>?"
+ "_AT_ABCDEFGHIJKLMNOPQRSTUVWXYZ[\]^_"
+ "`abcdefghijklmnopqrstuvwxyz{|}~";
++
++/*
++ * Open urls starting with urlprefixes, contatining urlchars
++ * by passing as ARG1 to urlhandler.
++ */
++char* urlhandler = "xdg-open";
++char urlchars[] =
++ "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
++ "abcdefghijklmnopqrstuvwxyz"
++ "0123456789-._~:/?#_AT_!$&'*+,;=%";
++char* urlprefixes[] = {"http://", "https://", NULL};
+diff --git a/st.c b/st.c
+index 51049ba..a7eb86e 100644
+--- a/st.c
++++ b/st.c
+_AT_@ -643,6 +643,92 @@ 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
++highlighturlsline(int row)
++{
++ char *linestr = calloc(sizeof(char), term.col+1); /* assume ascii */
++ char *match;
++ for (int j = 0; j < term.col; j++) {
++ if (term.line[row][j].u < 127) {
++ linestr[j] = term.line[row][j].u;
++ }
++ linestr[term.col] = '++ }
++ int url_start = -1;
++ 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[row][c].mode |= ATTR_URL;
++ tsetdirt(row, c);
++ }
++ }
++ free(linestr);
++}
++
++void
++unhighlighturlsline(int row)
++{
++ for (int j = 0; j < term.col; j++) {
++ Glyph* g = &term.line[row][j];
++ if (g->mode & ATTR_URL) {
++ g->mode &= ~ATTR_URL;
++ tsetdirt(row, j);
++ }
++ }
++ return;
++}
++
++int
++followurl(int col, int row) {
++ char *linestr = calloc(sizeof(char), term.col+1); /* assume ascii */
++ char *match;
++ for (int i = 0; i < term.col; i++) {
++ if (term.line[row][i].u < 127) {
++ linestr[i] = term.line[row][i].u;
++ }
++ linestr[term.col] = '++ }
++ int url_start = -1, found_url = 0;
++ 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 <= col && col < url_end) {
++ found_url = 1;
++ linestr[url_end] = '++ break;
++ }
++ }
++ if (!found_url) {
++ free(linestr);
++ return 0;
++ }
++
++ 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);
++ return 1;
++}
++
+ void
+ selclear(void)
+ {
+_AT_@ -2652,6 +2738,8 @@ drawregion(int x1, int y1, int x2, int y2)
+ continue;
+
+ term.dirty[y] = 0;
++ unhighlighturlsline(y);
++ highlighturlsline(y);
+ xdrawline(term.line[y], x1, y, x2);
+ }
+ }
+diff --git a/st.h b/st.h
+index 519b9bd..5efc27e 100644
+--- a/st.h
++++ b/st.h
+_AT_@ -33,6 +33,7 @@ enum glyph_attribute {
+ ATTR_WRAP = 1 << 8,
+ ATTR_WIDE = 1 << 9,
+ ATTR_WDUMMY = 1 << 10,
++ ATTR_URL = 1 << 11,
+ ATTR_BOLD_FAINT = ATTR_BOLD | ATTR_FAINT,
+ };
+
+_AT_@ -105,6 +106,10 @@ void selextend(int, int, int, int);
+ int selected(int, int);
+ char *getsel(void);
+
++void highlighturlsline(int);
++void unhighlighturlsline(int);
++int followurl(int, int);
++
+ size_t utf8encode(Rune, char *);
+
+ void *xmalloc(size_t);
+_AT_@ -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..c721f8b 100644
+--- a/x.c
++++ b/x.c
+_AT_@ -191,6 +191,7 @@ static void usage(void);
+
+ static void (*handler[LASTEvent])(XEvent *) = {
+ [KeyPress] = kpress,
++ [KeyRelease] = kpress,
+ [ClientMessage] = cmessage,
+ [ConfigureNotify] = resize,
+ [VisibilityNotify] = visibility,
+_AT_@ -445,6 +446,10 @@ 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) {
++ return followurl(evcol(e), evrow(e));
++ }
++
+ for (ms = mshortcuts; ms < mshortcuts + LEN(mshortcuts); ms++) {
+ if (ms->release == release &&
+ ms->button == e->xbutton.button &&
+_AT_@ -1476,7 +1481,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 + 1,
+ width, 1);
+ }
+_AT_@ -1831,6 +1836,11 @@ kpress(XEvent *ev)
+ len = XmbLookupString(xw.ime.xic, e, buf, sizeof buf, &ksym, &status);
+ else
+ len = XLookupString(e, buf, sizeof buf, &ksym, NULL);
++
++ /* 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)) {
+--
+2.42.0
+
Received on Wed Apr 10 2024 - 01:34:36 CEST

This archive was generated by hypermail 2.3.0 : Wed Apr 10 2024 - 01:36:50 CEST