1
0
mirror of https://github.com/weechat/weechat.git synced 2026-07-09 11:13:12 +02:00

Add new option weechat.look.highlight_regex and function string_has_highlight_regex in plugin API (task #10321)

This commit is contained in:
Sebastien Helleu
2010-11-25 21:28:14 +01:00
parent 8b9abab711
commit e92079cfe9
32 changed files with 578 additions and 18 deletions
+71
View File
@@ -31,6 +31,7 @@
#include <string.h>
#include <ctype.h>
#include <wctype.h>
#include <regex.h>
#if defined(__OpenBSD__)
#include <utf8/wchar.h>
@@ -708,6 +709,76 @@ string_has_highlight (const char *string, const char *highlight_words)
return 0;
}
/*
* string_has_highlight_regex_compiled: return 1 if string contains a highlight
* using a regular expression (any match
* in string must be surrounded by word
* chars)
*/
int
string_has_highlight_regex_compiled (const char *string, regex_t *regex)
{
int rc, startswith, endswith;
regmatch_t regex_match;
const char *match_pre;
if (!string || !regex)
return 0;
while (string && string[0])
{
rc = regexec (regex, string, 1, &regex_match, 0);
if ((rc != 0) || (regex_match.rm_so < 0) || (regex_match.rm_eo < 0))
break;
startswith = (regex_match.rm_so == 0);
if (!startswith)
{
match_pre = utf8_prev_char (string, string + regex_match.rm_so);
startswith = !string_is_word_char (match_pre);
}
endswith = 0;
if (startswith)
{
endswith = ((regex_match.rm_eo == (int)strlen (string))
|| !string_is_word_char (string + regex_match.rm_eo));
}
if (startswith && endswith)
return 1;
string += regex_match.rm_eo;
}
/* no highlight found */
return 0;
}
/*
* string_has_highlight_regex: return 1 if string contains a highlight
* using a regular expression (any match in string
* must be surrounded by word chars)
*/
int
string_has_highlight_regex (const char *string, const char *regex)
{
regex_t reg;
int rc;
if (!string || !regex)
return 0;
if (regcomp (&reg, regex, REG_EXTENDED) != 0)
return 0;
rc = string_has_highlight_regex_compiled (string, &reg);
regfree (&reg);
return rc;
}
/*
* string_mask_to_regex: convert a mask (string with only "*" as wildcard) to a
* regex, paying attention to special chars in a regex