diff options
author | Kirill Chibisov <contact@kchibisov.com> | 2023-03-18 22:25:05 +0300 |
---|---|---|
committer | Robin Jarry <robin@jarry.cc> | 2023-03-26 21:05:46 +0200 |
commit | 65b1524bc63ddb926c4b83b1a34b89599fd8d156 (patch) | |
tree | ddec14cd5eefa9c3653ae8f8e0adf7e265d24bc3 /filters | |
parent | 946c74daeedcf9bef9972a5418044c413334b7b7 (diff) | |
download | aerc-65b1524bc63ddb926c4b83b1a34b89599fd8d156.tar.gz |
colorize: emit OSC 8 for URLs and emails
Mark URLs with OSC 8 escape sequence to help terminal emulators with
opening multi-line URLs with the mouse and attach the hyperlink to
email addresses, so the users could open them.
Link: https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
Signed-off-by: Kirill Chibisov <contact@kchibisov.com>
Acked-by: Robin Jarry <robin@jarry.cc>
Diffstat (limited to 'filters')
-rw-r--r-- | filters/colorize.c | 26 | ||||
-rw-r--r-- | filters/vectors/colorize-patch.expected | 4 | ||||
-rw-r--r-- | filters/vectors/colorize-quotes.expected | 4 |
3 files changed, 27 insertions, 7 deletions
diff --git a/filters/colorize.c b/filters/colorize.c index e1d57be6..26c25a8a 100644 --- a/filters/colorize.c +++ b/filters/colorize.c @@ -413,6 +413,17 @@ static inline size_t print_notabs(const char *in, size_t max_len) return len; } +static void print_osc8(const char *url, size_t len, size_t id, bool email) { + printf("\x1b]8;id=colorize-%lu;", id); + if (url != NULL) { + if (email) { + print("mailto://"); + } + print_notabs(url, len); + } + print("\x1b\\"); +} + static void diff_chunk(const char *in) { size_t len = 0; @@ -429,17 +440,22 @@ static void diff_chunk(const char *in) } #define URL_RE \ - "[a-z]{2,8}://[][:alnum:]._~:/?#[@!$&'()*+,;=%-]{4,}" \ + "([a-z]{2,8}:)//[][:alnum:]._~:/?#[@!$&'()*+,;=%-]{4,}" \ "|(mailto:)?[[:alnum:]_+.~/-]*[[:alnum:]]@[a-z][[:alnum:].-]*[a-z]" static regex_t url_re; static void urls(const char *in, struct style *ctx) { - regmatch_t groups[2]; + /* ID of the next link to print for OSC 8. The purpose of passing + * explicit ID is to help terminal emulator with grouping of + * multi-line links in nested terminal sessions */ + static size_t url_id = 0; + + regmatch_t groups[3]; size_t len; bool trim; - while (!regexec(&url_re, in, 2, groups, 0)) { + while (!regexec(&url_re, in, 3, groups, 0)) { in += print_notabs(in, groups[0].rm_so); print(seq(&styles.url)); len = groups[0].rm_eo - groups[0].rm_so; @@ -457,7 +473,11 @@ static void urls(const char *in, struct style *ctx) break; } } + bool email = groups[2].rm_so == -1 && groups[1].rm_so == -1; + print_osc8(in, len, url_id, email); in += print_notabs(in, len); + print_osc8(NULL, 0, url_id, email); + url_id++; print(RESET); if (ctx) { print(seq(ctx)); diff --git a/filters/vectors/colorize-patch.expected b/filters/vectors/colorize-patch.expected index de3f2399..619e118f 100644 --- a/filters/vectors/colorize-patch.expected +++ b/filters/vectors/colorize-patch.expected @@ -1,11 +1,11 @@ -[1;38;2;175;135;255mFrom:[0m Robin Jarry <[4;38;2;255;255;175mrobin@jarry.cc[0m> +[1;38;2;175;135;255mFrom:[0m Robin Jarry <[4;38;2;255;255;175m]8;id=colorize-0;mailto://robin@jarry.cc\robin@jarry.cc]8;id=colorize-0;\[0m> [1;38;2;175;135;255mDate:[0m Mon, 26 Dec 2022 17:02:14 +0100 [1;38;2;175;135;255mSubject:[0m [PATCH aerc] doc: fix numbered lists According to scdoc(5), numbered lists start with a period. [1;38;2;175;135;255mFixes:[0m af63bd0188d1 ("doc: homogenize scdoc markup") -[1;38;2;175;135;255mSigned-off-by:[0m Robin Jarry <[4;38;2;255;255;175mrobin@jarry.cc[0m> +[1;38;2;175;135;255mSigned-off-by:[0m Robin Jarry <[4;38;2;255;255;175m]8;id=colorize-1;mailto://robin@jarry.cc\robin@jarry.cc]8;id=colorize-1;\[0m> --- doc/aerc-stylesets.7.scd | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/filters/vectors/colorize-quotes.expected b/filters/vectors/colorize-quotes.expected index 6e71d60a..46d3adba 100644 --- a/filters/vectors/colorize-quotes.expected +++ b/filters/vectors/colorize-quotes.expected @@ -17,7 +17,7 @@ nibh petentium at! Sit docendi laboramus ei, animal insolens ad mea. [38;2;95;175;255m> Erat alienum interpretaris has et, te vim aliquam molestie. Nam vivendum[0m [38;2;95;175;255m> facilisis qualisque at, ex his mucius qualisque! Fabulas lucilius adversarium[0m [38;2;95;175;255m> eu his. Cu soluta inermis accusata usu, his nulla dolore ne, vis id semper[0m -[38;2;95;175;255m> detracto sententia <[4;38;2;255;255;175mhttps://foobar.com[0m[38;2;95;175;255m> && "[4;38;2;255;255;175mhttps://foobaz.org/[0m[38;2;95;175;255m".[0m +[38;2;95;175;255m> detracto sententia <[4;38;2;255;255;175m]8;id=colorize-0;https://foobar.com\https://foobar.com]8;id=colorize-0;\[0m[38;2;95;175;255m> && "[4;38;2;255;255;175m]8;id=colorize-1;https://foobaz.org/\https://foobaz.org/]8;id=colorize-1;\[0m[38;2;95;175;255m".[0m [38;2;95;175;255m>[0m [38;2;95;175;255m> Error libris deleniti ea mei, vis at elit probo munere, his sint unum[0m [38;2;95;175;255m> albucius ex.[0m @@ -52,7 +52,7 @@ Has putent verterem constituto ex, tale electram duo at! Ei nulla lucilius intellegat nam, pro quod epicuri dissentiet ut, omnis voluptatibus definitiones vim at. -[4;38;2;255;255;175mhttps://git-man-page-generator.lokaltog.net/#Y2xhcCQkY29tbWFuZA==[0m +[4;38;2;255;255;175m]8;id=colorize-2;https://git-man-page-generator.lokaltog.net/#Y2xhcCQkY29tbWFuZA==\https://git-man-page-generator.lokaltog.net/#Y2xhcCQkY29tbWFuZA==]8;id=colorize-2;\[0m Eam mundi libris debitis ad, eam regione numquam at. Eum omnes bonorum eu, oporteat assueverit disputationi nam ne, nonumes iracundia mea ad! Duo libris |