summaryrefslogtreecommitdiff
path: root/src/core
diff options
context:
space:
mode:
authorSebastien Helleu <flashcode@flashtux.org>2010-11-25 21:28:14 +0100
committerSebastien Helleu <flashcode@flashtux.org>2010-11-25 21:28:14 +0100
commite92079cfe9c2ad89cf4c9f7d2ce146f4393cb9f4 (patch)
tree09343bd04a8c939351237d3babdcf0b05f0a0eb4 /src/core
parent8b9abab711ccdccceafcbea351b8bef0d23b8ecd (diff)
downloadweechat-e92079cfe9c2ad89cf4c9f7d2ce146f4393cb9f4.zip
Add new option weechat.look.highlight_regex and function string_has_highlight_regex in plugin API (task #10321)
Diffstat (limited to 'src/core')
-rw-r--r--src/core/wee-config.c47
-rw-r--r--src/core/wee-config.h8
-rw-r--r--src/core/wee-string.c71
-rw-r--r--src/core/wee-string.h5
4 files changed, 129 insertions, 2 deletions
diff --git a/src/core/wee-config.c b/src/core/wee-config.c
index 82c300d18..07865c785 100644
--- a/src/core/wee-config.c
+++ b/src/core/wee-config.c
@@ -35,6 +35,7 @@
#include <time.h>
#include <sys/types.h>
#include <sys/stat.h>
+#include <regex.h>
#include "weechat.h"
#include "wee-config.h"
@@ -83,6 +84,7 @@ struct t_config_option *config_look_confirm_quit;
struct t_config_option *config_look_day_change;
struct t_config_option *config_look_day_change_time_format;
struct t_config_option *config_look_highlight;
+struct t_config_option *config_look_highlight_regex;
struct t_config_option *config_look_hline_char;
struct t_config_option *config_look_hotlist_names_count;
struct t_config_option *config_look_hotlist_names_length;
@@ -200,6 +202,7 @@ struct t_config_option *config_plugin_save_config_on_unload;
struct t_hook *config_day_change_timer = NULL;
int config_day_change_old_day = -1;
+regex_t *config_highlight_regex = NULL;
/*
@@ -284,6 +287,41 @@ config_change_buffer_time_format (void *data, struct t_config_option *option)
}
/*
+ * config_change_highlight_regex: called when highlight_regex changes
+ */
+
+void
+config_change_highlight_regex (void *data, struct t_config_option *option)
+{
+ /* make C compiler happy */
+ (void) data;
+ (void) option;
+
+ if (config_highlight_regex)
+ {
+ regfree (config_highlight_regex);
+ free (config_highlight_regex);
+ config_highlight_regex = NULL;
+ }
+
+ if (CONFIG_STRING(config_look_highlight_regex)
+ && CONFIG_STRING(config_look_highlight_regex)[0])
+ {
+ config_highlight_regex = malloc (sizeof (*config_highlight_regex));
+ if (config_highlight_regex)
+ {
+ if (regcomp (config_highlight_regex,
+ CONFIG_STRING(config_look_highlight_regex),
+ REG_EXTENDED) != 0)
+ {
+ free (config_highlight_regex);
+ config_highlight_regex = NULL;
+ }
+ }
+ }
+}
+
+/*
* config_change_hotlist: called when hotlist changes
*/
@@ -1334,6 +1372,14 @@ config_weechat_init_options ()
N_("comma separated list of words to highlight (case insensitive "
"comparison, words may begin or end with \"*\" for partial match)"),
NULL, 0, 0, "", NULL, 0, NULL, NULL, NULL, NULL, NULL, NULL);
+ config_look_highlight_regex = config_file_new_option (
+ weechat_config_file, ptr_section,
+ "highlight_regex", "string",
+ N_("regular expression used to check if a message has highlight or not, "
+ "at least one match in string must be surrounded by word chars "
+ "(alphanumeric, \"-\", \"_\" or \"|\"), regular expression is case "
+ "sensitive, example: \"FlashCode|flashy\""),
+ NULL, 0, 0, "", NULL, 0, NULL, NULL, &config_change_highlight_regex, NULL, NULL, NULL);
config_look_hline_char = config_file_new_option (
weechat_config_file, ptr_section,
"hline_char", "string",
@@ -2196,6 +2242,7 @@ config_weechat_init ()
0,
&config_day_change_timer_cb,
NULL);
+ config_change_highlight_regex (NULL, NULL);
}
return rc;
diff --git a/src/core/wee-config.h b/src/core/wee-config.h
index d84476384..ed7d45dde 100644
--- a/src/core/wee-config.h
+++ b/src/core/wee-config.h
@@ -21,10 +21,11 @@
#ifndef __WEECHAT_CONFIG_H
#define __WEECHAT_CONFIG_H 1
-struct t_gui_buffer;
-
+#include <regex.h>
#include "wee-config-file.h"
+struct t_gui_buffer;
+
#define WEECHAT_CONFIG_NAME "weechat"
enum t_config_look_nicklist
@@ -112,6 +113,7 @@ extern struct t_config_option *config_look_color_real_white;
extern struct t_config_option *config_look_day_change;
extern struct t_config_option *config_look_day_change_time_format;
extern struct t_config_option *config_look_highlight;
+extern struct t_config_option *config_look_highlight_regex;
extern struct t_config_option *config_look_hline_char;
extern struct t_config_option *config_look_hotlist_names_count;
extern struct t_config_option *config_look_hotlist_names_length;
@@ -215,6 +217,8 @@ extern struct t_config_option *config_plugin_extension;
extern struct t_config_option *config_plugin_path;
extern struct t_config_option *config_plugin_save_config_on_unload;
+extern regex_t *config_highlight_regex;
+
extern struct t_config_option *config_weechat_debug_get (const char *plugin_name);
extern int config_weechat_debug_set (const char *plugin_name,
diff --git a/src/core/wee-string.c b/src/core/wee-string.c
index b5fad5398..24224ccf8 100644
--- a/src/core/wee-string.c
+++ b/src/core/wee-string.c
@@ -31,6 +31,7 @@
#include <string.h>
#include <ctype.h>
#include <wctype.h>
+#include <regex.h>
#if defined(__OpenBSD__)
#include <utf8/wchar.h>
@@ -709,6 +710,76 @@ string_has_highlight (const char *string, const char *highlight_words)
}
/*
+ * 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
*/
diff --git a/src/core/wee-string.h b/src/core/wee-string.h
index bb6377713..e6928c9cc 100644
--- a/src/core/wee-string.h
+++ b/src/core/wee-string.h
@@ -20,6 +20,8 @@
#ifndef __WEECHAT_STRING_H
#define __WEECHAT_STRING_H 1
+#include <regex.h>
+
extern char *string_strndup (const char *string, int length);
extern void string_tolower (char *string);
extern void string_toupper (char *string);
@@ -42,6 +44,9 @@ extern char *string_strip (const char *string, int left, int right,
extern char *string_convert_hex_chars (const char *string);
extern int string_has_highlight (const char *string,
const char *highlight_words);
+extern int string_has_highlight_regex_compiled (const char *string,
+ regex_t *regex);
+extern int string_has_highlight_regex (const char *string, const char *regex);
extern char *string_mask_to_regex (const char *mask);
extern char **string_split (const char *string, const char *separators,
int keep_eol, int num_items_max, int *num_items);