st/patches/st-copyurl-20190202-0.8.1.diff (view raw)
1From be408247f1c1ff8ccf7ab128b126f54d19bd4392 Mon Sep 17 00:00:00 2001
2From: Michael Buch <michaelbuch12@gmail.com>
3Date: Sat, 2 Feb 2019 14:20:52 +0000
4Subject: [PATCH] Port the copyurl patch to the 0.8.1 st release. Mainly fix
5 usage of depracted selcopy
6
7---
8 config.def.h | 1 +
9 st.c | 62 ++++++++++++++++++++++++++++++++++++++++++++++++++++
10 st.h | 1 +
11 3 files changed, 64 insertions(+)
12
13diff --git a/config.def.h b/config.def.h
14index 82b1b09..cbe923e 100644
15--- a/config.def.h
16+++ b/config.def.h
17@@ -178,6 +178,7 @@ static Shortcut shortcuts[] = {
18 { TERMMOD, XK_Y, selpaste, {.i = 0} },
19 { TERMMOD, XK_Num_Lock, numlock, {.i = 0} },
20 { TERMMOD, XK_I, iso14755, {.i = 0} },
21+ { MODKEY, XK_l, copyurl, {.i = 0} },
22 };
23
24 /*
25diff --git a/st.c b/st.c
26index 46c954b..476eb31 100644
27--- a/st.c
28+++ b/st.c
29@@ -2616,3 +2616,65 @@ redraw(void)
30 tfulldirt();
31 draw();
32 }
33+
34+/* select and copy the previous url on screen (do nothing if there's no url).
35+ * known bug: doesn't handle urls that span multiple lines (wontfix)
36+ * known bug: only finds first url on line (mightfix)
37+ */
38+void
39+copyurl(const Arg *arg) {
40+ /* () and [] can appear in urls, but excluding them here will reduce false
41+ * positives when figuring out where a given url ends.
42+ */
43+ static char URLCHARS[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
44+ "abcdefghijklmnopqrstuvwxyz"
45+ "0123456789-._~:/?#@!$&'*+,;=%";
46+
47+ int i, row, startrow;
48+ char *linestr = calloc(sizeof(char), term.col+1); /* assume ascii */
49+ char *c, *match = NULL;
50+
51+ row = (sel.ob.x >= 0 && sel.nb.y > 0) ? sel.nb.y-1 : term.bot;
52+ LIMIT(row, term.top, term.bot);
53+ startrow = row;
54+
55+ /* find the start of the last url before selection */
56+ do {
57+ for (i = 0; i < term.col; ++i) {
58+ if (term.line[row][i].u > 127) /* assume ascii */
59+ continue;
60+ linestr[i] = term.line[row][i].u;
61+ }
62+ linestr[term.col] = '\0';
63+ if ((match = strstr(linestr, "http://"))
64+ || (match = strstr(linestr, "https://")))
65+ break;
66+ if (--row < term.top)
67+ row = term.bot;
68+ } while (row != startrow);
69+
70+ if (match) {
71+ /* must happen before trim */
72+ selclear();
73+ sel.ob.x = strlen(linestr) - strlen(match);
74+
75+ /* trim the rest of the line from the url match */
76+ for (c = match; *c != '\0'; ++c)
77+ if (!strchr(URLCHARS, *c)) {
78+ *c = '\0';
79+ break;
80+ }
81+
82+ /* select and copy */
83+ sel.mode = 1;
84+ sel.type = SEL_REGULAR;
85+ sel.oe.x = sel.ob.x + strlen(match)-1;
86+ sel.ob.y = sel.oe.y = row;
87+ selnormalize();
88+ tsetdirt(sel.nb.y, sel.ne.y);
89+ xsetsel(getsel());
90+ xclipcopy();
91+ }
92+
93+ free(linestr);
94+}
95diff --git a/st.h b/st.h
96index dac64d8..5a58f8f 100644
97--- a/st.h
98+++ b/st.h
99@@ -85,6 +85,7 @@ void printscreen(const Arg *);
100 void printsel(const Arg *);
101 void sendbreak(const Arg *);
102 void toggleprinter(const Arg *);
103+void copyurl(const Arg *);
104
105 int tattrset(int);
106 void tnew(int, int);
107--
1082.20.1
109