Add -expando flag, allow case-sensitive regexp matching

This commit is contained in:
Jari Matilainen 2018-01-20 13:33:49 +01:00
commit f6c589b88b
2 changed files with 28 additions and 19 deletions

View file

@ -37,6 +37,7 @@
#include "printtext.h" #include "printtext.h"
#include "formats.h" #include "formats.h"
#include "special-vars.h" #include "special-vars.h"
#include "window-items.h"
static NICKMATCH_REC *nickmatch; static NICKMATCH_REC *nickmatch;
static int never_hilight_level, default_hilight_level; 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->nickmask) iconfig_node_set_bool(node, "mask", TRUE);
if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE); if (rec->fullword) iconfig_node_set_bool(node, "fullword", TRUE);
if (rec->regexp) iconfig_node_set_bool(node, "regexp", 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->case_sensitive) iconfig_node_set_bool(node, "matchcase", TRUE);
if (rec->servertag) iconfig_node_set_str(node, "servertag", rec->servertag); 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; 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) int *match_beg, int *match_end)
{ {
gboolean ret = FALSE; 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->regexp) {
if (rec->preg != NULL) { MatchInfo *match;
MatchInfo *match; GRegex *regex = i_regex_new(pattern, G_REGEX_OPTIMIZE | (rec->case_sensitive ? 0 : G_REGEX_CASELESS), 0, NULL);
i_regex_match(rec->preg, text, 0, &match); i_regex_match(regex, text, 0, &match);
if (i_match_info_matches(match)) if (i_match_info_matches(match))
ret = i_match_info_fetch_pos(match, 0, match_beg, match_end); 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 { } else {
char *match; char *match;
char *str = parse_special_string(rec->text, server, NULL, "", NULL, 0);
if (g_strcmp0(str, "") == 0) return FALSE;
if (rec->case_sensitive) { if (rec->case_sensitive) {
match = rec->fullword ? match = rec->fullword ?
strstr_full(text, str) : strstr_full(text, pattern) :
strstr(text, str); strstr(text, pattern);
} else { } else {
match = rec->fullword ? match = rec->fullword ?
stristr_full(text, str) : stristr_full(text, pattern) :
stristr(text, str); stristr(text, pattern);
} }
if (match != NULL) { if (match != NULL) {
if (match_beg != NULL && match_end != NULL) { if (match_beg != NULL && match_end != NULL) {
*match_beg = (int) (match-text); *match_beg = (int) (match-text);
*match_end = *match_beg + strlen(str); *match_end = *match_beg + strlen(pattern);
} }
ret = TRUE; ret = TRUE;
} }
@ -278,7 +283,7 @@ HILIGHT_REC *hilight_match(SERVER_REC *server, const char *channel,
hilight_match_channel(rec, channel) && hilight_match_channel(rec, channel) &&
(rec->servertag == NULL || (rec->servertag == NULL ||
(server != NULL && g_ascii_strcasecmp(rec->servertag, server->tag) == 0)) && (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; return rec;
} }
@ -472,6 +477,7 @@ static void read_hilight_config(void)
rec->nickmask = config_node_get_bool(node, "mask", FALSE); rec->nickmask = config_node_get_bool(node, "mask", FALSE);
rec->fullword = config_node_get_bool(node, "fullword", FALSE); rec->fullword = config_node_get_bool(node, "fullword", FALSE);
rec->regexp = config_node_get_bool(node, "regexp", 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); servertag = config_node_get_str(node, "servertag", NULL);
rec->servertag = servertag == NULL || *servertag == '\0' ? NULL : rec->servertag = servertag == NULL || *servertag == '\0' ? NULL :
g_strdup(servertag); 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->nickmask) g_string_append(options, "-mask ");
if (rec->fullword) g_string_append(options, "-full "); if (rec->fullword) g_string_append(options, "-full ");
if (rec->case_sensitive) g_string_append(options, "-matchcase "); if (rec->case_sensitive) g_string_append(options, "-matchcase ");
if (rec->expando) g_string_append(options, "-expando ");
if (rec->regexp) { if (rec->regexp) {
g_string_append(options, "-regexp "); g_string_append(options, "-regexp ");
if (rec->preg == NULL) if (rec->preg == NULL)
@ -549,7 +556,7 @@ static void cmd_hilight_show(void)
} }
/* SYNTAX: HILIGHT [-nick | -word | -line] [-mask | -full | -matchcase | -regexp] /* SYNTAX: HILIGHT [-nick | -word | -line] [-mask | -full | -matchcase | -regexp]
[-color <color>] [-actcolor <color>] [-level <level>] [-expando] [-color <color>] [-actcolor <color>] [-level <level>]
[-network <network>] [-channels <channels>] <text> */ [-network <network>] [-channels <channels>] <text> */
static void cmd_hilight(const char *data) 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->nickmask = g_hash_table_lookup(optlist, "mask") != NULL;
rec->fullword = g_hash_table_lookup(optlist, "full") != NULL; rec->fullword = g_hash_table_lookup(optlist, "full") != NULL;
rec->regexp = g_hash_table_lookup(optlist, "regexp") != 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; rec->case_sensitive = g_hash_table_lookup(optlist, "matchcase") != NULL;
if (colorarg != NULL) { if (colorarg != NULL) {
@ -723,7 +731,7 @@ void hilight_text_init(void)
command_bind("hilight", NULL, (SIGNAL_FUNC) cmd_hilight); command_bind("hilight", NULL, (SIGNAL_FUNC) cmd_hilight);
command_bind("dehilight", NULL, (SIGNAL_FUNC) cmd_dehilight); 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) void hilight_text_deinit(void)

View file

@ -17,6 +17,7 @@ struct _HILIGHT_REC {
unsigned int nick:1; /* hilight only nick if possible */ unsigned int nick:1; /* hilight only nick if possible */
unsigned int word:1; /* hilight only word, not full line */ 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 nickmask:1; /* `text' is a nick mask */
unsigned int fullword:1; /* match `text' only for full words */ unsigned int fullword:1; /* match `text' only for full words */
unsigned int regexp:1; /* `text' is a regular expression */ unsigned int regexp:1; /* `text' is a regular expression */