From 0f483d65ceb06e40eab8d5f5291e681112f06249 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 20 May 2021 17:56:56 +0200 Subject: config: move url-mode related options to a dedicated section, ‘url’ MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- config.c | 100 ++++++++++++++++++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 87 insertions(+), 13 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 563130a7..80f944b5 100644 --- a/config.c +++ b/config.c @@ -530,6 +530,26 @@ str_to_spawn_template(struct config *conf, return true; } +static void +deprecated_url_option(struct config *conf, + const char *old_name, const char *new_name, + const char *path, unsigned lineno) +{ + LOG_WARN( + "deprecated: %s:%d: [default]: %s: use '%s' in section '[url]' instead", + path, lineno, old_name, new_name); + + const char fmt[] = + "%s:%d: \033[1m%s\033[22m, use \033[1m%s\033[22m in the \033[1m[url]\033[22m section instead"; + char *text = xasprintf(fmt, path, lineno, old_name, new_name); + + struct user_notification deprecation = { + .kind = USER_NOTIFICATION_DEPRECATED, + .text = text, + }; + tll_push_back(conf->notifications, deprecation); +} + static bool parse_section_main(const char *key, const char *value, struct config *conf, const char *path, unsigned lineno) @@ -773,14 +793,17 @@ parse_section_main(const char *key, const char *value, struct config *conf, } else if (strcmp(key, "jump-label-letters") == 0) { + deprecated_url_option( + conf, "jump-label-letters", "label-letters", path, lineno); + wchar_t *letters; if (!str_to_wchars(value, &letters, conf, path, lineno, - "default", "jump-label-letters")) + "default", "label-letters")) { return false; } - free(conf->jump_label_letters); - conf->jump_label_letters = letters; + free(conf->url.label_letters); + conf->url.label_letters = letters; } else if (strcmp(key, "notify") == 0) { @@ -792,7 +815,10 @@ parse_section_main(const char *key, const char *value, struct config *conf, } else if (strcmp(key, "url-launch") == 0) { - if (!str_to_spawn_template(conf, value, &conf->url_launch, path, lineno, + deprecated_url_option( + conf, "url-launch", "launch", path, lineno); + + if (!str_to_spawn_template(conf, value, &conf->url.launch, path, lineno, "default", "url-launch")) { return false; @@ -822,10 +848,13 @@ parse_section_main(const char *key, const char *value, struct config *conf, } else if (strcmp(key, "osc8-underline") == 0) { + deprecated_url_option( + conf, "osc8-underline", "osc8-underline", path, lineno); + if (strcmp(value, "url-mode") == 0) - conf->osc8_underline = OSC8_UNDERLINE_URL_MODE; + conf->url.osc8_underline = OSC8_UNDERLINE_URL_MODE; else if (strcmp(value, "always") == 0) - conf->osc8_underline = OSC8_UNDERLINE_ALWAYS; + conf->url.osc8_underline = OSC8_UNDERLINE_ALWAYS; else { LOG_AND_NOTIFY_ERR( "%s:%u: [default]: %s: invalid 'osc8-underline'; " @@ -938,6 +967,48 @@ parse_section_scrollback(const char *key, const char *value, struct config *conf return true; } +static bool +parse_section_url(const char *key, const char *value, struct config *conf, + const char *path, unsigned lineno) +{ + if (strcmp(key, "launch") == 0) { + if (!str_to_spawn_template(conf, value, &conf->url.launch, path, lineno, + "url", "launch")) + { + return false; + } + } + + else if (strcmp(key, "label-letters") == 0) { + wchar_t *letters; + if (!str_to_wchars(value, &letters, conf, path, lineno, "url", "letters")) + return false; + + free(conf->url.label_letters); + conf->url.label_letters = letters; + } + + else if (strcmp(key, "osc8-underline") == 0) { + if (strcmp(value, "url-mode") == 0) + conf->url.osc8_underline = OSC8_UNDERLINE_URL_MODE; + else if (strcmp(value, "always") == 0) + conf->url.osc8_underline = OSC8_UNDERLINE_ALWAYS; + else { + LOG_AND_NOTIFY_ERR( + "%s:%u: [url]: %s: invalid 'osc8-underline'; " + "must be one of 'url-mode', or 'always'", path, lineno, value); + return false; + } + } + + else { + LOG_AND_NOTIFY_ERR("%s:%d: [url]: %s: invalid key", path, lineno, key); + return false; + } + + return true; +} + static bool parse_section_colors(const char *key, const char *value, struct config *conf, const char *path, unsigned lineno) @@ -1962,6 +2033,7 @@ parse_config_file(FILE *f, struct config *conf, const char *path, bool errors_ar SECTION_MAIN, SECTION_BELL, SECTION_SCROLLBACK, + SECTION_URL, SECTION_COLORS, SECTION_CURSOR, SECTION_MOUSE, @@ -1986,6 +2058,7 @@ parse_config_file(FILE *f, struct config *conf, const char *path, bool errors_ar [SECTION_MAIN] = {&parse_section_main, "main"}, [SECTION_BELL] = {&parse_section_bell, "bell"}, [SECTION_SCROLLBACK] = {&parse_section_scrollback, "scrollback"}, + [SECTION_URL] = {&parse_section_url, "url"}, [SECTION_COLORS] = {&parse_section_colors, "colors"}, [SECTION_CURSOR] = {&parse_section_cursor, "cursor"}, [SECTION_MOUSE] = {&parse_section_mouse, "mouse"}, @@ -2301,7 +2374,6 @@ config_load(struct config *conf, const char *conf_path, .title = xstrdup("foot"), .app_id = xstrdup("foot"), .word_delimiters = xwcsdup(L",│`|:\"'()[]{}<>"), - .jump_label_letters = xwcsdup(L"sadfjklewcmpgh"), .size = { .type = CONF_SIZE_PX, .width = 700, @@ -2331,6 +2403,10 @@ config_load(struct config *conf, const char *conf_path, }, .command_focused = false, }, + .url = { + .label_letters = xwcsdup(L"sadfjklewcmpgh"), + .osc8_underline = OSC8_UNDERLINE_URL_MODE, + }, .scrollback = { .lines = 1000, .indicator = { @@ -2403,8 +2479,6 @@ config_load(struct config *conf, const char *conf_path, .argv = NULL, }, - .osc8_underline = OSC8_UNDERLINE_URL_MODE, - .tweak = { .fcft_filter = FCFT_SCALING_FILTER_LANCZOS3, .allow_overflowing_double_width_glyphs = true, @@ -2450,8 +2524,8 @@ config_load(struct config *conf, const char *conf_path, "notify-send -a foot -i foot ${title} ${body}"); tokenize_cmdline(conf->notify.raw_cmd, &conf->notify.argv); - conf->url_launch.raw_cmd = xstrdup("xdg-open ${url}"); - tokenize_cmdline(conf->url_launch.raw_cmd, &conf->url_launch.argv); + conf->url.launch.raw_cmd = xstrdup("xdg-open ${url}"); + tokenize_cmdline(conf->url.launch.raw_cmd, &conf->url.launch.argv); tll_foreach(*initial_user_notifications, it) { tll_push_back(conf->notifications, it->item); @@ -2557,11 +2631,9 @@ config_free(struct config conf) free(conf.title); free(conf.app_id); free(conf.word_delimiters); - free(conf.jump_label_letters); free_spawn_template(&conf.bell.command); free(conf.scrollback.indicator.text); free_spawn_template(&conf.notify); - free_spawn_template(&conf.url_launch); for (size_t i = 0; i < ALEN(conf.fonts); i++) { tll_foreach(conf.fonts[i], it) { config_font_destroy(&it->item); @@ -2570,6 +2642,8 @@ config_free(struct config conf) } free(conf.server_socket_path); + free(conf.url.label_letters); + free_spawn_template(&conf.url.launch); key_binding_list_free(&conf.bindings.key); key_binding_list_free(&conf.bindings.search); key_binding_list_free(&conf.bindings.url); -- cgit v1.3 From 53516aceec01779cc62af134f11bd7d9be71fcba Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 20 May 2021 17:58:06 +0200 Subject: config: add url.protocols This makes the protocols recognized by auto-detected URLs configurable. Closes #531 --- CHANGELOG.md | 2 ++ config.c | 66 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ config.h | 3 +++ doc/foot.ini.5.scd | 7 ++++++ foot.ini | 2 ++ url-mode.c | 24 +++++--------------- 6 files changed, 85 insertions(+), 19 deletions(-) (limited to 'config.c') diff --git a/CHANGELOG.md b/CHANGELOG.md index 9ad6b871..0474fe3d 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -44,6 +44,8 @@ and a new `command` action to run an arbitrary command. (https://codeberg.org/dnkl/foot/pulls/483) * Dedicated `[url]` section to config. +* `[url].protocols` option to `foot.ini` + (https://codeberg.org/dnkl/foot/issues/531). * Support for setting the full 256 color palette in foot.ini (https://codeberg.org/dnkl/foot/issues/489) * XDG activation support, will be used by `[bell].urgent` when diff --git a/config.c b/config.c index 80f944b5..08446e7d 100644 --- a/config.c +++ b/config.c @@ -1001,6 +1001,47 @@ parse_section_url(const char *key, const char *value, struct config *conf, } } + else if (strcmp(key, "protocols") == 0) { + for (size_t i = 0; i < conf->url.prot_count; i++) + free(conf->url.protocols[i]); + free(conf->url.protocols); + + conf->url.max_prot_len = 0; + conf->url.prot_count = 0; + conf->url.protocols = NULL; + + char *copy = xstrdup(value); + + for (char *prot = strtok(copy, " "); + prot != NULL; + prot = strtok(NULL, " ")) + { + size_t chars = mbstowcs(NULL, prot, 0); + if (chars == (size_t)-1) { + LOG_AND_NOTIFY_ERRNO( + "%s:%u: [url]: protocols: invalid protocol name: %s", + path, lineno, prot); + return false; + } + + conf->url.prot_count++; + conf->url.protocols = xrealloc( + conf->url.protocols, + conf->url.prot_count * sizeof(conf->url.protocols[0])); + + size_t idx = conf->url.prot_count - 1; + conf->url.protocols[idx] = xmalloc((chars + 1 + 3) * sizeof(wchar_t)); + mbstowcs(conf->url.protocols[idx], prot, chars + 1); + wcscpy(&conf->url.protocols[idx][chars], L"://"); + + size_t len = chars + 3; /* Include the "://" */ + if (len > conf->url.max_prot_len) + conf->url.max_prot_len = len; + } + + free(copy); + } + else { LOG_AND_NOTIFY_ERR("%s:%d: [url]: %s: invalid key", path, lineno, key); return false; @@ -2527,6 +2568,27 @@ config_load(struct config *conf, const char *conf_path, conf->url.launch.raw_cmd = xstrdup("xdg-open ${url}"); tokenize_cmdline(conf->url.launch.raw_cmd, &conf->url.launch.argv); + static const wchar_t *url_protocols[] = { + L"http://", + L"https://", + L"ftp://", + L"ftps://", + L"file://", + L"gemini://", + L"gopher://", + }; + conf->url.protocols = xmalloc( + ALEN(url_protocols) * sizeof(conf->url.protocols[0])); + conf->url.prot_count = ALEN(url_protocols); + conf->url.max_prot_len = 0; + + for (size_t i = 0; i < ALEN(url_protocols); i++) { + size_t len = wcslen(url_protocols[i]); + if (len > conf->url.max_prot_len) + conf->url.max_prot_len = len; + conf->url.protocols[i] = xwcsdup(url_protocols[i]); + } + tll_foreach(*initial_user_notifications, it) { tll_push_back(conf->notifications, it->item); tll_remove(*initial_user_notifications, it); @@ -2644,6 +2706,10 @@ config_free(struct config conf) free(conf.url.label_letters); free_spawn_template(&conf.url.launch); + for (size_t i = 0; i < conf.url.prot_count; i++) + free(conf.url.protocols[i]); + free(conf.url.protocols); + key_binding_list_free(&conf.bindings.key); key_binding_list_free(&conf.bindings.search); key_binding_list_free(&conf.bindings.url); diff --git a/config.h b/config.h index 69fb8f07..95083e6c 100644 --- a/config.h +++ b/config.h @@ -135,6 +135,9 @@ struct config { OSC8_UNDERLINE_ALWAYS, } osc8_underline; + wchar_t **protocols; + size_t prot_count; + size_t max_prot_len; } url; struct { diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index f6a91b8a..f224c957 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -323,6 +323,13 @@ in this order: String of characters to use when generating key sequences for URL jump labels. Default: _sadfjklewcmpgh_. +*protocols* + List of protocols (schemes) that should be recognized in URL + mode. Note that only auto-detected URLs are affected by this + option. OSC-8 URLs are always enabled, regardless of + protocol. Default: _http https ftp ftps file gemini gopher_. + + # SECTION: cursor This section controls the cursor style and color. Note that diff --git a/foot.ini b/foot.ini index d6ad17ed..c4f786a9 100644 --- a/foot.ini +++ b/foot.ini @@ -45,6 +45,8 @@ # launch=xdg-open ${url} # label-letters=sadfjklewcmpgh # osc8-underline=url-mode +# protocols = http https ftp ftps file gemini gopher + [cursor] # style=block # color=111111 dcdccc diff --git a/url-mode.c b/url-mode.c index fc1812ae..dc63ebbe 100644 --- a/url-mode.c +++ b/url-mode.c @@ -212,23 +212,9 @@ static void auto_detected(const struct terminal *term, enum url_action action, url_list_t *urls) { - static const wchar_t *const prots[] = { - L"http://", - L"https://", - L"ftp://", - L"ftps://", - L"file://", - L"gemini://", - L"gopher://", - }; - - size_t max_prot_len = 0; - for (size_t i = 0; i < ALEN(prots); i++) { - size_t len = wcslen(prots[i]); - if (len > max_prot_len) - max_prot_len = len; - } + const struct config *conf = term->conf; + size_t max_prot_len = conf->url.max_prot_len; wchar_t proto_chars[max_prot_len]; struct coord proto_start[max_prot_len]; size_t proto_char_count = 0; @@ -266,15 +252,15 @@ auto_detected(const struct terminal *term, enum url_action action, proto_start[max_prot_len - 1] = (struct coord){c, r}; proto_char_count++; - for (size_t i = 0; i < ALEN(prots); i++) { - size_t prot_len = wcslen(prots[i]); + for (size_t i = 0; i < conf->url.prot_count; i++) { + size_t prot_len = wcslen(conf->url.protocols[i]); if (proto_char_count < prot_len) continue; const wchar_t *proto = &proto_chars[max_prot_len - prot_len]; - if (wcsncasecmp(prots[i], proto, prot_len) == 0) { + if (wcsncasecmp(conf->url.protocols[i], proto, prot_len) == 0) { state = STATE_URL; start = proto_start[max_prot_len - prot_len]; -- cgit v1.3 From 121ea39942ce8756f4f6c9e45e5c8b60ad819d15 Mon Sep 17 00:00:00 2001 From: Daniel Eklöf Date: Thu, 20 May 2021 20:45:01 +0200 Subject: config: url.protocols: make it comma separated, for consistency --- config.c | 20 +++++++++++++++----- doc/foot.ini.5.scd | 9 +++++---- foot.ini | 2 +- 3 files changed, 21 insertions(+), 10 deletions(-) (limited to 'config.c') diff --git a/config.c b/config.c index 08446e7d..d0b36129 100644 --- a/config.c +++ b/config.c @@ -1012,10 +1012,20 @@ parse_section_url(const char *key, const char *value, struct config *conf, char *copy = xstrdup(value); - for (char *prot = strtok(copy, " "); + for (char *prot = strtok(copy, ","); prot != NULL; - prot = strtok(NULL, " ")) + prot = strtok(NULL, ",")) { + + /* Strip leading whitespace */ + while (isspace(*prot)) + prot++; + + /* Strip trailing whitespace */ + size_t len = strlen(prot); + while (len > 0 && isspace(prot[len - 1])) + prot[--len] = '\0'; + size_t chars = mbstowcs(NULL, prot, 0); if (chars == (size_t)-1) { LOG_AND_NOTIFY_ERRNO( @@ -1034,9 +1044,9 @@ parse_section_url(const char *key, const char *value, struct config *conf, mbstowcs(conf->url.protocols[idx], prot, chars + 1); wcscpy(&conf->url.protocols[idx][chars], L"://"); - size_t len = chars + 3; /* Include the "://" */ - if (len > conf->url.max_prot_len) - conf->url.max_prot_len = len; + chars += 3; /* Include the "://" */ + if (chars > conf->url.max_prot_len) + conf->url.max_prot_len = chars; } free(copy); diff --git a/doc/foot.ini.5.scd b/doc/foot.ini.5.scd index f224c957..8ee3b581 100644 --- a/doc/foot.ini.5.scd +++ b/doc/foot.ini.5.scd @@ -324,10 +324,11 @@ in this order: jump labels. Default: _sadfjklewcmpgh_. *protocols* - List of protocols (schemes) that should be recognized in URL - mode. Note that only auto-detected URLs are affected by this - option. OSC-8 URLs are always enabled, regardless of - protocol. Default: _http https ftp ftps file gemini gopher_. + Comma separated list of protocols (schemes) that should be + recognized in URL mode. Note that only auto-detected URLs are + affected by this option. OSC-8 URLs are always enabled, regardless + of protocol. Default: _http, https, ftp, ftps, file, gemini, + gopher_. # SECTION: cursor diff --git a/foot.ini b/foot.ini index c4f786a9..12bb0aa7 100644 --- a/foot.ini +++ b/foot.ini @@ -45,7 +45,7 @@ # launch=xdg-open ${url} # label-letters=sadfjklewcmpgh # osc8-underline=url-mode -# protocols = http https ftp ftps file gemini gopher +# protocols = http, https, ftp, ftps, file, gemini, gopher [cursor] # style=block -- cgit v1.3