From b97ebe46bff8f04dc3ece6c6dacfaecf1bddd57b Mon Sep 17 00:00:00 2001 From: Jari Matilainen Date: Fri, 19 Jan 2018 12:32:02 +0100 Subject: [PATCH 1/5] Allow expandos in /hilight --- src/fe-common/core/hilight-text.c | 18 +++++++++++------- 1 file changed, 11 insertions(+), 7 deletions(-) diff --git a/src/fe-common/core/hilight-text.c b/src/fe-common/core/hilight-text.c index b9912457..e96f225b 100644 --- a/src/fe-common/core/hilight-text.c +++ b/src/fe-common/core/hilight-text.c @@ -36,6 +36,7 @@ #include "nickmatch-cache.h" #include "printtext.h" #include "formats.h" +#include "special-vars.h" static NICKMATCH_REC *nickmatch; static int never_hilight_level, default_hilight_level; @@ -190,7 +191,7 @@ static HILIGHT_REC *hilight_find(const char *text, char **channels) return NULL; } -static gboolean hilight_match_text(HILIGHT_REC *rec, const char *text, +static gboolean hilight_match_text(HILIGHT_REC *rec, SERVER_REC *server, const char *text, int *match_beg, int *match_end) { gboolean ret = FALSE; @@ -207,20 +208,23 @@ static gboolean hilight_match_text(HILIGHT_REC *rec, const char *text, } } else { char *match; + char *str = parse_special_string(rec->text, server, NULL, "", NULL, 0); + + if (g_strcmp0(str, "") == 0) return FALSE; if (rec->case_sensitive) { match = rec->fullword ? - strstr_full(text, rec->text) : - strstr(text, rec->text); + strstr_full(text, str) : + strstr(text, str); } else { match = rec->fullword ? - stristr_full(text, rec->text) : - stristr(text, rec->text); + stristr_full(text, str) : + stristr(text, str); } if (match != NULL) { if (match_beg != NULL && match_end != NULL) { *match_beg = (int) (match-text); - *match_end = *match_beg + strlen(rec->text); + *match_end = *match_beg + strlen(str); } ret = TRUE; } @@ -274,7 +278,7 @@ HILIGHT_REC *hilight_match(SERVER_REC *server, const char *channel, hilight_match_channel(rec, channel) && (rec->servertag == NULL || (server != NULL && g_ascii_strcasecmp(rec->servertag, server->tag) == 0)) && - hilight_match_text(rec, str, match_beg, match_end)) + hilight_match_text(rec, server, str, match_beg, match_end)) return rec; } From f6c589b88be6990cf7d07ecf438b62fbfa0a05c1 Mon Sep 17 00:00:00 2001 From: Jari Matilainen Date: Sat, 20 Jan 2018 13:33:49 +0100 Subject: [PATCH 2/5] Add -expando flag, allow case-sensitive regexp matching --- src/fe-common/core/hilight-text.c | 46 ++++++++++++++++++------------- src/fe-common/core/hilight-text.h | 1 + 2 files changed, 28 insertions(+), 19 deletions(-) diff --git a/src/fe-common/core/hilight-text.c b/src/fe-common/core/hilight-text.c index e96f225b..008c4582 100644 --- a/src/fe-common/core/hilight-text.c +++ b/src/fe-common/core/hilight-text.c @@ -37,6 +37,7 @@ #include "printtext.h" #include "formats.h" #include "special-vars.h" +#include "window-items.h" static NICKMATCH_REC *nickmatch; static int never_hilight_level, default_hilight_level; @@ -80,6 +81,7 @@ static void hilight_add_config(HILIGHT_REC *rec) if (rec->nickmask) iconfig_node_set_bool(node, "mask", TRUE); if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE); if (rec->regexp) iconfig_node_set_bool(node, "regexp", TRUE); + if (rec->expando) iconfig_node_set_bool(node, "expando", TRUE); if (rec->case_sensitive) iconfig_node_set_bool(node, "matchcase", TRUE); if (rec->servertag) iconfig_node_set_str(node, "servertag", rec->servertag); @@ -191,40 +193,43 @@ static HILIGHT_REC *hilight_find(const char *text, char **channels) return NULL; } -static gboolean hilight_match_text(HILIGHT_REC *rec, SERVER_REC *server, const char *text, +static gboolean hilight_match_text(HILIGHT_REC *rec, SERVER_REC *server, const char *channel, const char *text, int *match_beg, int *match_end) { gboolean ret = FALSE; + WI_ITEM_REC *witem = window_item_find(server, channel); + char *pattern = rec->expando ? (parse_special_string(rec->regexp ? g_regex_get_pattern(rec->preg) : rec->text, server, witem, "", NULL, 0)) : + rec->text; + + if (pattern == NULL || strlen(pattern) == 0) + return FALSE; if (rec->regexp) { - if (rec->preg != NULL) { - MatchInfo *match; - i_regex_match(rec->preg, text, 0, &match); + MatchInfo *match; + GRegex *regex = i_regex_new(pattern, G_REGEX_OPTIMIZE | (rec->case_sensitive ? 0 : G_REGEX_CASELESS), 0, NULL); + i_regex_match(regex, text, 0, &match); - if (i_match_info_matches(match)) - ret = i_match_info_fetch_pos(match, 0, match_beg, match_end); + if (i_match_info_matches(match)) + ret = i_match_info_fetch_pos(match, 0, match_beg, match_end); - i_match_info_free(match); - } + i_match_info_free(match); + i_regex_unref(regex); } else { char *match; - char *str = parse_special_string(rec->text, server, NULL, "", NULL, 0); - - if (g_strcmp0(str, "") == 0) return FALSE; if (rec->case_sensitive) { match = rec->fullword ? - strstr_full(text, str) : - strstr(text, str); + strstr_full(text, pattern) : + strstr(text, pattern); } else { match = rec->fullword ? - stristr_full(text, str) : - stristr(text, str); + stristr_full(text, pattern) : + stristr(text, pattern); } if (match != NULL) { if (match_beg != NULL && match_end != NULL) { *match_beg = (int) (match-text); - *match_end = *match_beg + strlen(str); + *match_end = *match_beg + strlen(pattern); } ret = TRUE; } @@ -278,7 +283,7 @@ HILIGHT_REC *hilight_match(SERVER_REC *server, const char *channel, hilight_match_channel(rec, channel) && (rec->servertag == NULL || (server != NULL && g_ascii_strcasecmp(rec->servertag, server->tag) == 0)) && - hilight_match_text(rec, server, str, match_beg, match_end)) + hilight_match_text(rec, server, channel, str, match_beg, match_end)) return rec; } @@ -472,6 +477,7 @@ static void read_hilight_config(void) rec->nickmask = config_node_get_bool(node, "mask", FALSE); rec->fullword = config_node_get_bool(node, "fullword", FALSE); rec->regexp = config_node_get_bool(node, "regexp", FALSE); + rec->expando = config_node_get_bool(node, "expando", FALSE); servertag = config_node_get_str(node, "servertag", NULL); rec->servertag = servertag == NULL || *servertag == '\0' ? NULL : g_strdup(servertag); @@ -502,6 +508,7 @@ static void hilight_print(int index, HILIGHT_REC *rec) if (rec->nickmask) g_string_append(options, "-mask "); if (rec->fullword) g_string_append(options, "-full "); if (rec->case_sensitive) g_string_append(options, "-matchcase "); + if (rec->expando) g_string_append(options, "-expando "); if (rec->regexp) { g_string_append(options, "-regexp "); if (rec->preg == NULL) @@ -549,7 +556,7 @@ static void cmd_hilight_show(void) } /* SYNTAX: HILIGHT [-nick | -word | -line] [-mask | -full | -matchcase | -regexp] - [-color ] [-actcolor ] [-level ] + [-expando] [-color ] [-actcolor ] [-level ] [-network ] [-channels ] */ static void cmd_hilight(const char *data) { @@ -616,6 +623,7 @@ static void cmd_hilight(const char *data) rec->nickmask = g_hash_table_lookup(optlist, "mask") != NULL; rec->fullword = g_hash_table_lookup(optlist, "full") != NULL; rec->regexp = g_hash_table_lookup(optlist, "regexp") != NULL; + rec->expando = g_hash_table_lookup(optlist, "expando") != NULL; rec->case_sensitive = g_hash_table_lookup(optlist, "matchcase") != NULL; if (colorarg != NULL) { @@ -723,7 +731,7 @@ void hilight_text_init(void) command_bind("hilight", NULL, (SIGNAL_FUNC) cmd_hilight); command_bind("dehilight", NULL, (SIGNAL_FUNC) cmd_dehilight); - command_set_options("hilight", "-color -actcolor -level -priority -network -channels nick word line mask full regexp matchcase"); + command_set_options("hilight", "-color -actcolor -level -priority -network -channels nick word line mask full regexp matchcase expando"); } void hilight_text_deinit(void) diff --git a/src/fe-common/core/hilight-text.h b/src/fe-common/core/hilight-text.h index 1d942f29..d47bcca0 100644 --- a/src/fe-common/core/hilight-text.h +++ b/src/fe-common/core/hilight-text.h @@ -17,6 +17,7 @@ struct _HILIGHT_REC { unsigned int nick:1; /* hilight only nick if possible */ unsigned int word:1; /* hilight only word, not full line */ + unsigned int expando:1; /* `text' contains expandos */ unsigned int nickmask:1; /* `text' is a nick mask */ unsigned int fullword:1; /* match `text' only for full words */ unsigned int regexp:1; /* `text' is a regular expression */ From 082fa971073bca6a8075cd4c6ddd360d3daff936 Mon Sep 17 00:00:00 2001 From: Jari Matilainen Date: Sat, 20 Jan 2018 13:57:56 +0100 Subject: [PATCH 3/5] make sure rec->preg isn't NULL --- src/fe-common/core/hilight-text.c | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/src/fe-common/core/hilight-text.c b/src/fe-common/core/hilight-text.c index 008c4582..366315bc 100644 --- a/src/fe-common/core/hilight-text.c +++ b/src/fe-common/core/hilight-text.c @@ -197,23 +197,29 @@ static gboolean hilight_match_text(HILIGHT_REC *rec, SERVER_REC *server, const c int *match_beg, int *match_end) { gboolean ret = FALSE; + + if (rec->regexp && rec->preg == NULL) + return ret; + WI_ITEM_REC *witem = window_item_find(server, channel); char *pattern = rec->expando ? (parse_special_string(rec->regexp ? g_regex_get_pattern(rec->preg) : rec->text, server, witem, "", NULL, 0)) : rec->text; if (pattern == NULL || strlen(pattern) == 0) - return FALSE; + return ret; if (rec->regexp) { MatchInfo *match; - GRegex *regex = i_regex_new(pattern, G_REGEX_OPTIMIZE | (rec->case_sensitive ? 0 : G_REGEX_CASELESS), 0, NULL); + GRegex *regex = !rec->expando ? rec->preg : + i_regex_new(pattern, G_REGEX_OPTIMIZE | (rec->case_sensitive ? 0 : G_REGEX_CASELESS), 0, NULL); i_regex_match(regex, text, 0, &match); if (i_match_info_matches(match)) ret = i_match_info_fetch_pos(match, 0, match_beg, match_end); i_match_info_free(match); - i_regex_unref(regex); + if(rec->expando) + i_regex_unref(regex); } else { char *match; From 2439ad2c4e4e5504c9e4f66fcccdfe46bea1ed3d Mon Sep 17 00:00:00 2001 From: Jari Matilainen Date: Sat, 20 Jan 2018 14:03:34 +0100 Subject: [PATCH 4/5] build error decl after stmt --- src/fe-common/core/hilight-text.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/fe-common/core/hilight-text.c b/src/fe-common/core/hilight-text.c index 366315bc..16536de4 100644 --- a/src/fe-common/core/hilight-text.c +++ b/src/fe-common/core/hilight-text.c @@ -197,11 +197,11 @@ static gboolean hilight_match_text(HILIGHT_REC *rec, SERVER_REC *server, const c int *match_beg, int *match_end) { gboolean ret = FALSE; + WI_ITEM_REC *witem = window_item_find(server, channel); if (rec->regexp && rec->preg == NULL) return ret; - WI_ITEM_REC *witem = window_item_find(server, channel); char *pattern = rec->expando ? (parse_special_string(rec->regexp ? g_regex_get_pattern(rec->preg) : rec->text, server, witem, "", NULL, 0)) : rec->text; From 3f4e3d7294295adebf7dc55ccaeb44b4dd2b2b76 Mon Sep 17 00:00:00 2001 From: Jari Matilainen Date: Sat, 20 Jan 2018 14:09:49 +0100 Subject: [PATCH 5/5] build error decl after stmt --- src/fe-common/core/hilight-text.c | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/fe-common/core/hilight-text.c b/src/fe-common/core/hilight-text.c index 16536de4..9d818e35 100644 --- a/src/fe-common/core/hilight-text.c +++ b/src/fe-common/core/hilight-text.c @@ -197,13 +197,15 @@ static gboolean hilight_match_text(HILIGHT_REC *rec, SERVER_REC *server, const c int *match_beg, int *match_end) { gboolean ret = FALSE; - WI_ITEM_REC *witem = window_item_find(server, channel); + WI_ITEM_REC *witem; + char *pattern; if (rec->regexp && rec->preg == NULL) return ret; - char *pattern = rec->expando ? (parse_special_string(rec->regexp ? g_regex_get_pattern(rec->preg) : rec->text, server, witem, "", NULL, 0)) : - rec->text; + witem = window_item_find(server, channel); + pattern = rec->expando ? (parse_special_string(rec->regexp ? g_regex_get_pattern(rec->preg) : rec->text, server, witem, "", NULL, 0)) : + rec->text; if (pattern == NULL || strlen(pattern) == 0) return ret;