summaryrefslogtreecommitdiff
path: root/src/terminal-util.c
diff options
context:
space:
mode:
authorGeorge Stark <[email protected]>2023-01-31 12:37:30 +0300
committerraveit65 <[email protected]>2023-08-31 14:25:41 +0200
commit649935a610d4f97ad99f98fed2facffdd5be5265 (patch)
tree195fdd0ec69ba6cfa30b714e8be3c81cdd3b4959 /src/terminal-util.c
parenta060d180f0e5e44ab0d0972c42dc4e392f458892 (diff)
downloadmate-terminal-649935a610d4f97ad99f98fed2facffdd5be5265.tar.bz2
mate-terminal-649935a610d4f97ad99f98fed2facffdd5be5265.tar.xz
Add support for OSC 8 hyperlinks (HTML-like anchors)
backport of 1c6f8db736efc62d9a9b38bfbc43ec03c8544696 from gnome-terminal
Diffstat (limited to 'src/terminal-util.c')
-rw-r--r--src/terminal-util.c50
1 files changed, 50 insertions, 0 deletions
diff --git a/src/terminal-util.c b/src/terminal-util.c
index ede72da..d0f1864 100644
--- a/src/terminal-util.c
+++ b/src/terminal-util.c
@@ -22,6 +22,7 @@
#include <config.h>
+#define _GNU_SOURCE /* for strchrnul */
#include <string.h>
#include <stdlib.h>
#include <unistd.h>
@@ -707,6 +708,55 @@ terminal_util_add_proxy_env (GHashTable *env_table)
g_object_unref (settings_socks);
}
+/**
+ * terminal_util_hyperlink_uri_label:
+ * @uri: a URI
+ *
+ * Formats @uri to be displayed in a tooltip.
+ * Performs URI-decoding and converts IDN hostname to UTF-8.
+ *
+ * Returns: (transfer full): The human readable URI as plain text
+ */
+char *terminal_util_hyperlink_uri_label (const char *uri)
+{
+ char *unesc = NULL;
+ gboolean replace_hostname;
+
+ if (uri == NULL)
+ return NULL;
+
+ unesc = g_uri_unescape_string(uri, NULL);
+ if (unesc == NULL)
+ unesc = g_strdup(uri);
+
+ if (g_ascii_strncasecmp(unesc, "ftp://", 6) == 0 ||
+ g_ascii_strncasecmp(unesc, "http://", 7) == 0 ||
+ g_ascii_strncasecmp(unesc, "https://", 8) == 0)
+ {
+ char *unidn = NULL;
+
+ char *hostname = strchr(unesc, '/') + 2;
+ char *hostname_end = strchrnul(hostname, '/');
+ char save = *hostname_end;
+ *hostname_end = '\0';
+ unidn = g_hostname_to_unicode(hostname);
+ replace_hostname = unidn != NULL && g_ascii_strcasecmp(unidn, hostname) != 0;
+ *hostname_end = save;
+ if (replace_hostname)
+ {
+ char *new_unesc = g_strdup_printf("%.*s%s%s",
+ (int) (hostname - unesc),
+ unesc,
+ unidn,
+ hostname_end);
+ g_free(unesc);
+ unesc = new_unesc;
+ }
+ g_free(unidn);
+ }
+ return unesc;
+}
+
/* Bidirectional object/widget binding */
typedef struct