From 931e5010d51d1640c61034ada767715a544fa40b Mon Sep 17 00:00:00 2001 From: salmonik Date: Fri, 6 Feb 2026 02:14:51 +0100 Subject: apply scrollback patch and urlclick patch --- st.c | 92 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 92 insertions(+) (limited to 'st.c') diff --git a/st.c b/st.c index 17fc9fb..170a8a8 100644 --- a/st.c +++ b/st.c @@ -652,6 +652,98 @@ 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 = 0; i < term.row; i++) { + Line line = TLINE(i); + int url_start = -1; + for (int j = 0; j < term.col; j++) { + if (line[j].u < 127) { + linestr[j] = line[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++) { + line[c].mode |= ATTR_URL; + tsetdirt(i, c); + } + } + } + free(linestr); +} + +void +unhighlighturls(void) +{ + for (int i = 0; i < term.row; i++) { + Line line = TLINE(i); + for (int j = 0; j < term.col; j++) { + Glyph* g = &line[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; + Line line = TLINE(x); + for (int i = 0; i < term.col; i++) { + if (line[i].u < 127) { + linestr[i] = line[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) { -- cgit v1.2.3