Skip to content

Commit

Permalink
fix url highlighting due to deprecation of vte_terminal_match_add_gregex
Browse files Browse the repository at this point in the history
  • Loading branch information
norbusan committed Mar 18, 2020
1 parent de8961d commit 6ff414f
Showing 1 changed file with 50 additions and 5 deletions.
55 changes: 50 additions & 5 deletions src/terminal-screen.c
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,11 @@

#include "eggshell.h"

#if VTE_CHECK_VERSION (0, 46, 0)
#define PCRE2_CODE_UNIT_WIDTH 0
#include <pcre2.h>
#endif

#define URL_MATCH_CURSOR (GDK_HAND2)
#define SKEY_MATCH_CURSOR (GDK_HAND2)

Expand Down Expand Up @@ -158,19 +163,33 @@ typedef struct
{
const char *pattern;
TerminalURLFlavour flavor;
#if VTE_CHECK_VERSION (0, 46, 0)
guint32 flags;
#else
GRegexCompileFlags flags;
#endif
} TerminalRegexPattern;

#if VTE_CHECK_VERSION (0, 46, 0)
#define FLAG_CASELESS PCRE2_CASELESS
#else
#define FLAG_CASELESS G_REGEX_CASELESS
#endif

static const TerminalRegexPattern url_regex_patterns[] =
{
{ SCHEME "//(?:" USERPASS "\\@)?" HOST PORT URLPATH, FLAVOR_AS_IS, G_REGEX_CASELESS },
{ "(?:www|ftp)" HOSTCHARS_CLASS "*\\." HOST PORT URLPATH , FLAVOR_DEFAULT_TO_HTTP, G_REGEX_CASELESS },
{ "(?:callto:|h323:|sip:)" USERCHARS_CLASS "[" USERCHARS ".]*(?:" PORT "/[a-z0-9]+)?\\@" HOST, FLAVOR_VOIP_CALL, G_REGEX_CASELESS },
{ "(?:mailto:)?" USERCHARS_CLASS "[" USERCHARS ".]*\\@" HOSTCHARS_CLASS "+\\." HOST, FLAVOR_EMAIL, G_REGEX_CASELESS },
{ "news:[[:alnum:]\\Q^_{|}~!\"#$%&'()*+,./;:=?`\\E]+", FLAVOR_AS_IS, G_REGEX_CASELESS },
{ SCHEME "//(?:" USERPASS "\\@)?" HOST PORT URLPATH, FLAVOR_AS_IS, FLAG_CASELESS },
{ "(?:www|ftp)" HOSTCHARS_CLASS "*\\." HOST PORT URLPATH , FLAVOR_DEFAULT_TO_HTTP, FLAG_CASELESS },
{ "(?:callto:|h323:|sip:)" USERCHARS_CLASS "[" USERCHARS ".]*(?:" PORT "/[a-z0-9]+)?\\@" HOST, FLAVOR_VOIP_CALL, FLAG_CASELESS },
{ "(?:mailto:)?" USERCHARS_CLASS "[" USERCHARS ".]*\\@" HOSTCHARS_CLASS "+\\." HOST, FLAVOR_EMAIL, FLAG_CASELESS },
{ "news:[[:alnum:]\\Q^_{|}~!\"#$%&'()*+,./;:=?`\\E]+", FLAVOR_AS_IS, FLAG_CASELESS },
};

#if VTE_CHECK_VERSION (0, 46, 0)
static VteRegex **url_regexes;
#else
static GRegex **url_regexes;
#endif
static TerminalURLFlavour *url_regex_flavors;
static guint n_url_regexes;

Expand All @@ -184,7 +203,11 @@ static const TerminalRegexPattern skey_regex_patterns[] =
{ "otp-[a-z0-9]* [[:digit:]]* [-[:alnum:]]*", FLAVOR_AS_IS },
};

#if VTE_CHECK_VERSION (0, 46, 0)
static VteRegex **skey_regexes;
#else
static GRegex **skey_regexes;
#endif
static guint n_skey_regexes;

static void terminal_screen_skey_match_remove (TerminalScreen *screen);
Expand Down Expand Up @@ -570,16 +593,25 @@ terminal_screen_class_init (TerminalScreenClass *klass)

/* Precompile the regexes */
n_url_regexes = G_N_ELEMENTS (url_regex_patterns);
#if VTE_CHECK_VERSION (0, 46, 0)
url_regexes = g_new0 (VteRegex*, n_url_regexes);
#else
url_regexes = g_new0 (GRegex*, n_url_regexes);
#endif
url_regex_flavors = g_new0 (TerminalURLFlavour, n_url_regexes);

for (i = 0; i < n_url_regexes; ++i)
{
GError *error = NULL;

#if VTE_CHECK_VERSION (0, 46, 0)
url_regexes[i] = vte_regex_new_for_match(url_regex_patterns[i].pattern, -1,
url_regex_patterns[i].flags | PCRE2_MULTILINE, &error);
#else
url_regexes[i] = g_regex_new (url_regex_patterns[i].pattern,
url_regex_patterns[i].flags | G_REGEX_OPTIMIZE | G_REGEX_MULTILINE,
0, &error);
#endif
if (error)
{
g_message ("%s", error->message);
Expand All @@ -597,9 +629,14 @@ terminal_screen_class_init (TerminalScreenClass *klass)
{
GError *error = NULL;

#if VTE_CHECK_VERSION (0, 46, 0)
skey_regexes[i] = vte_regex_new_for_match(skey_regex_patterns[i].pattern, -1,
PCRE2_MULTILINE, &error);
#else
skey_regexes[i] = g_regex_new (skey_regex_patterns[i].pattern,
G_REGEX_OPTIMIZE | G_REGEX_MULTILINE,
0, &error);
#endif
if (error)
{
g_message ("%s", error->message);
Expand Down Expand Up @@ -1014,7 +1051,11 @@ terminal_screen_profile_notify_cb (TerminalProfile *profile,

tag_data = g_slice_new (TagData);
tag_data->flavor = FLAVOR_SKEY;
#if VTE_CHECK_VERSION (0, 46, 0)
tag_data->tag = vte_terminal_match_add_regex (vte_terminal, skey_regexes[i], 0);
#else
tag_data->tag = vte_terminal_match_add_gregex (vte_terminal, skey_regexes[i], 0);
#endif
vte_terminal_match_set_cursor_type (vte_terminal, tag_data->tag, SKEY_MATCH_CURSOR);

priv->match_tags = g_slist_prepend (priv->match_tags, tag_data);
Expand Down Expand Up @@ -1059,7 +1100,11 @@ terminal_screen_profile_notify_cb (TerminalProfile *profile,

tag_data = g_slice_new (TagData);
tag_data->flavor = url_regex_flavors[i];
#if VTE_CHECK_VERSION (0, 46, 0)
tag_data->tag = vte_terminal_match_add_regex (vte_terminal, url_regexes[i], 0);
#else
tag_data->tag = vte_terminal_match_add_gregex (vte_terminal, url_regexes[i], 0);
#endif
vte_terminal_match_set_cursor_type (vte_terminal, tag_data->tag, URL_MATCH_CURSOR);

priv->match_tags = g_slist_prepend (priv->match_tags, tag_data);
Expand Down

0 comments on commit 6ff414f

Please sign in to comment.