summarylogtreecommitdiffstats
path: root/osc8.patch
blob: dc8c841d2c30af4d90c7d62e1ee8856d860d9359 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
commit 4e545ac (origin/54-support-osc-hyperlinks)
Author: Dan Davison <dandavison7@gmail.com>
Date:   Wed Jul 22 18:18:43 2020 -0400

    Apply OSC 8 hyperlinks patch
    
    Patch from https://bug779734.bugzilla-attachments.gnome.org/attachment.cgi?id=349890
    
    Ref https://github.com/gwsw/less/issues/54

diff --git a/charset.c b/charset.c
index 1b74b2b..75f688c 100644
--- a/charset.c
+++ b/charset.c
@@ -426,6 +426,7 @@ control_char(c)
 	LWCHAR c;
 {
 	c &= 0377;
+	if (c == 7) return FALSE;  /* FIXME hack for OSC 8, should be shown as ^G if outside of this escape sequence */
 	return (chardef[c] & IS_CONTROL_CHAR);
 }
 
diff --git a/cvt.c b/cvt.c
index 3271cc9..1354752 100644
--- a/cvt.c
+++ b/cvt.c
@@ -78,13 +78,32 @@ cvt_text(odst, osrc, chpos, lenp, ops)
 				dst--;
 			} while (dst > odst && utf_mode &&
 				!IS_ASCII_OCTET(*dst) && !IS_UTF8_LEAD(*dst));
-		} else if ((ops & CVT_ANSI) && IS_CSI_START(ch))
+		} else if ((ops & CVT_ANSI) && ch == ESC)
 		{
-			/* Skip to end of ANSI escape sequence. */
-			src++;  /* skip the CSI start char */
-			while (src < src_end)
-				if (!is_ansi_middle(*src++))
-					break;
+            if (src[0] == ']' && src[1] == '8' && src[2] == ';')
+            {
+                /* Skip to the end of a hyperlink. */
+                src += 3;
+                while (src < src_end)
+                {
+                    if (src[0] == BEL)
+                    {
+                        src++;
+                        break;
+                    } else if (src[0] == ESC && src[1] == '\\')
+                    {
+                        src += 2;
+                        break;
+                    }
+                    src++;
+                }
+            } else
+            {
+                /* Skip to end of ANSI escape sequence. */
+                while (src < src_end)
+                    if (!is_ansi_middle(*src++))
+                        break;
+            }
 		} else
 		{
 			/* Just copy the char to the destination buffer. */
diff --git a/less.h b/less.h
index 1cbc1c0..10f57e7 100644
--- a/less.h
+++ b/less.h
@@ -456,7 +456,10 @@ struct wchar_range_table
 
 #define	ESC		CONTROL('[')
 #define	ESCS		"\33"
+#define	BEL		CONTROL('g')
 #define	CSI		((unsigned char)'\233')
+#define ST      ((unsigned char)'\234')
+#define OSC     ((unsigned char)'\235')
 #define	CHAR_END_COMMAND 0x40000000
 
 #if _OSK_MWC32
diff --git a/line.c b/line.c
index e46aa39..cd6a6e0 100644
--- a/line.c
+++ b/line.c
@@ -288,18 +288,30 @@ pshift(shift)
 	while (shifted <= shift && from < curr)
 	{
 		c = linebuf[from];
-		if (ctldisp == OPT_ONPLUS && IS_CSI_START(c))
+        /* FIXME: accept C1 CSI and C1 OSC as well! */
+        if (ctldisp == OPT_ONPLUS && c == ESC)
 		{
 			/* Keep cumulative effect.  */
 			linebuf[to] = c;
 			attr[to++] = attr[from++];
-			while (from < curr && linebuf[from])
-			{
-				linebuf[to] = linebuf[from];
-				attr[to++] = attr[from];
-				if (!is_ansi_middle(linebuf[from++]))
-					break;
-			} 
+            if (linebuf[from] == ']' && linebuf[from+1] == '8' && linebuf[from+2] == ';') {
+                while (from < curr && linebuf[from])
+                {
+                    linebuf[to] = linebuf[from];
+                    attr[to++] = attr[from++];
+                    /* FIXME: accept C1 ST */
+                    if (linebuf[from-1] == BEL || (linebuf[from-1] == '\\' && linebuf[from-2] == ESC))
+                        break;
+                }
+            } else {
+                while (from < curr && linebuf[from])
+                {
+                    linebuf[to] = linebuf[from];
+                    attr[to++] = attr[from];
+                    if (!is_ansi_middle(linebuf[from++]))
+                        break;
+                }
+            }
 			continue;
 		}
 
@@ -530,23 +542,141 @@ backc(VOID_PARAM)
 
 /*
  * Are we currently within a recognized ANSI escape sequence?
+ *
+ * FIXME This code is a nightmare!
+ * Displaying a line potentially takes O(n^2) time as this function is called
+ * for each character.
+ * Why do we walk backwards at all, rather having a state machine that knows
+ * the current state???
+ * Not to mention the if-else hell...
  */
 	static int
 in_ansi_esc_seq(VOID_PARAM)
 {
-	char *p;
+    char *p, *save;
+    LWCHAR ch, ch2;
 
 	/*
-	 * Search backwards for either an ESC (which means we ARE in a seq);
+     * Search backwards for either a CSI (which means we ARE in a seq);
 	 * or an end char (which means we're NOT in a seq).
 	 */
+
+    /* Potential lone ESC */
+    p = &linebuf[curr];
+    if (p > linebuf)
+    {
+        ch = step_char(&p, -1, linebuf);
+        if (ch == ESC)
+            return (1);
+    }
+
+    /* Search for ESC [ or a terminator character */
 	for (p = &linebuf[curr];  p > linebuf; )
 	{
-		LWCHAR ch = step_char(&p, -1, linebuf);
-		if (IS_CSI_START(ch))
+        ch = step_char(&p, -1, linebuf);
+        save = p;
+        if (ch == ESC)
 			return (1);
+        if (ch == CSI)
+            return (1);
+        else if (ch == '[' && p > linebuf)
+        {
+            ch2 = step_char(&p, -1, linebuf);
+            if (ch2 == ESC)
+                return (1);
+        }
 		if (!is_ansi_middle(ch))
 			return (0);
+        p = save;
+    }
+    return (0);
+}
+
+
+
+/*
+ * Are we currently within a hyperlink escape sequence?
+ * (Not to be confused with the the visible anchor text of the hyperlink.)
+ *
+ * FIXME This code is a nightmare!
+ * Displaying a line potentially takes O(n^2) time as this function is called
+* for each character.
+ * Why do we walk backwards at all, rather having a state machine that knows
+ * the current state???
+ * Not to mention the if-else hell...
+ */
+	static int
+in_hyperlink_esc_seq()
+{
+	char *p, *save;
+	LWCHAR ch, ch2, ch3, ch4;
+
+	/*
+	 * Search backwards for either an OSC 8 ; (which means we ARE in a hyperlink);
+	 * or a BEL or ST (which means we're NOT in a seq).
+	 */
+
+	/* Potential prefix (ESC, ESC [ or ESC [ 8 but no semicolon yet) */
+	p = &linebuf[curr];
+	if (p > linebuf)
+	{
+		ch = step_char(&p, -1, linebuf);
+		if (ch == ESC)
+			return (1);
+		else if (ch == OSC)
+			return (1);
+		else if (ch == ']' && p > linebuf)
+		{
+			ch2 = step_char(&p, -1, linebuf);
+			if (ch2 == ESC)
+				return (1);
+		}
+		else if (ch == '8' && p > linebuf)
+		{
+			ch2 = step_char(&p, -1, linebuf);
+			if (ch2 == OSC)
+				return (1);
+			else if (ch2 == ']' && p > linebuf)
+			{
+				ch3 = step_char(&p, -1, linebuf);
+				if (ch3 == ESC)
+					return (1);
+			}
+		}
+	}
+
+	/* ESC [ 8 ; or a terminator */
+	for (p = &linebuf[curr];  p > linebuf; )
+	{
+		ch = step_char(&p, -1, linebuf);
+		save = p;
+		if (ch == BEL)
+			return (0);
+		else if (ch == ST)
+			return (0);
+		else if (ch == '\\' && p > linebuf)
+		{
+			ch2 = step_char(&p, -1, linebuf);
+			if (ch2 == ESC)
+				return (0);
+		}
+		else if (ch == ';' && p > linebuf)
+		{
+			ch2 = step_char(&p, -1, linebuf);
+			if (ch2 == '8' && p > linebuf)
+			{
+				ch3 = step_char(&p, -1, linebuf);
+				if (ch3 == OSC)
+					return (1);
+				else if (ch3 == ']' && p > linebuf)
+				{
+					ch4 = step_char(&p, -1, linebuf);
+					if (ch4 == ESC)
+						return (1);
+				}
+			}
+		}
+		p = save;
 	}
 	return (0);
 }
@@ -637,7 +767,12 @@ store_char(ch, a, rep, pos)
 	}
 #endif
 
-	if (ctldisp == OPT_ONPLUS && in_ansi_esc_seq())
+	if (ctldisp == OPT_ONPLUS && in_hyperlink_esc_seq())
+	{
+		a = AT_ANSI;
+		w = 0;
+	}
+	else if (ctldisp == OPT_ONPLUS && in_ansi_esc_seq())
 	{
 		if (!is_ansi_end(ch) && !is_ansi_middle(ch)) {
 			/* Remove whole unrecognized sequence.  */
@@ -1008,7 +1143,7 @@ do_append(ch, rep, pos)
 		{
 			STORE_PRCHAR((char) ch, pos);
 		}
-	} else if (utf_mode && ctldisp != OPT_ON && is_ubin_char(ch))
+	} else if (utf_mode && ctldisp != OPT_ON && is_ubin_char(ch) && ch != 7 /* FIXME hack for OSC 8 */)
 	{
 		char *s;
 
@@ -1056,6 +1191,16 @@ add_attr_normal(VOID_PARAM)
 		return;
 	for ( ;  *p != '\0';  p++)
 		add_linebuf(*p, AT_ANSI, 0);
+	if (ctldisp == OPT_ONPLUS)
+	{
+		/* Turn off hyperlink at end of line. */
+		char *p = "\033]8;;\007";
+		for ( ;  *p != '\0';  p++)
+		{
+			linebuf[curr] = *p;
+ 			attr[curr++] = AT_ANSI;
+ 		}
+ 	}
 }
 
 /*