mirror of
https://github.com/weechat/weechat.git
synced 2026-07-06 01:33:12 +02:00
core: fix check of tags in lines
All changes:
- fix check of tags in lines: check lines without tags, fix check of tags with
negation ("!tag")
- add string functions string_split_tags and string_free_split_tags
- add tests on function gui_line_match_tags
This commit is contained in:
+11
-67
@@ -1629,9 +1629,6 @@ void
|
||||
gui_buffer_set_highlight_tags_restrict (struct t_gui_buffer *buffer,
|
||||
const char *new_tags)
|
||||
{
|
||||
int i;
|
||||
char **tags_array;
|
||||
|
||||
if (!buffer)
|
||||
return;
|
||||
|
||||
@@ -1642,11 +1639,7 @@ gui_buffer_set_highlight_tags_restrict (struct t_gui_buffer *buffer,
|
||||
}
|
||||
if (buffer->highlight_tags_restrict_array)
|
||||
{
|
||||
for (i = 0; i < buffer->highlight_tags_restrict_count; i++)
|
||||
{
|
||||
string_free_split (buffer->highlight_tags_restrict_array[i]);
|
||||
}
|
||||
free (buffer->highlight_tags_restrict_array);
|
||||
string_free_split_tags (buffer->highlight_tags_restrict_array);
|
||||
buffer->highlight_tags_restrict_array = NULL;
|
||||
}
|
||||
buffer->highlight_tags_restrict_count = 0;
|
||||
@@ -1658,24 +1651,9 @@ gui_buffer_set_highlight_tags_restrict (struct t_gui_buffer *buffer,
|
||||
if (!buffer->highlight_tags_restrict)
|
||||
return;
|
||||
|
||||
tags_array = string_split (buffer->highlight_tags_restrict, ",", 0, 0,
|
||||
&buffer->highlight_tags_restrict_count);
|
||||
if (tags_array)
|
||||
{
|
||||
buffer->highlight_tags_restrict_array =
|
||||
malloc (buffer->highlight_tags_restrict_count *
|
||||
sizeof (*buffer->highlight_tags_restrict_array));
|
||||
if (buffer->highlight_tags_restrict_array)
|
||||
{
|
||||
for (i = 0; i < buffer->highlight_tags_restrict_count; i++)
|
||||
{
|
||||
buffer->highlight_tags_restrict_array[i] = string_split (tags_array[i],
|
||||
"+", 0, 0,
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
string_free_split (tags_array);
|
||||
}
|
||||
buffer->highlight_tags_restrict_array = string_split_tags (
|
||||
buffer->highlight_tags_restrict,
|
||||
&buffer->highlight_tags_restrict_count);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -1686,9 +1664,6 @@ void
|
||||
gui_buffer_set_highlight_tags (struct t_gui_buffer *buffer,
|
||||
const char *new_tags)
|
||||
{
|
||||
int i;
|
||||
char **tags_array;
|
||||
|
||||
if (!buffer)
|
||||
return;
|
||||
|
||||
@@ -1699,11 +1674,7 @@ gui_buffer_set_highlight_tags (struct t_gui_buffer *buffer,
|
||||
}
|
||||
if (buffer->highlight_tags_array)
|
||||
{
|
||||
for (i = 0; i < buffer->highlight_tags_count; i++)
|
||||
{
|
||||
string_free_split (buffer->highlight_tags_array[i]);
|
||||
}
|
||||
free (buffer->highlight_tags_array);
|
||||
string_free_split_tags (buffer->highlight_tags_array);
|
||||
buffer->highlight_tags_array = NULL;
|
||||
}
|
||||
buffer->highlight_tags_count = 0;
|
||||
@@ -1715,24 +1686,9 @@ gui_buffer_set_highlight_tags (struct t_gui_buffer *buffer,
|
||||
if (!buffer->highlight_tags)
|
||||
return;
|
||||
|
||||
tags_array = string_split (buffer->highlight_tags, ",", 0, 0,
|
||||
&buffer->highlight_tags_count);
|
||||
if (tags_array)
|
||||
{
|
||||
buffer->highlight_tags_array =
|
||||
malloc (buffer->highlight_tags_count *
|
||||
sizeof (*buffer->highlight_tags_array));
|
||||
if (buffer->highlight_tags_array)
|
||||
{
|
||||
for (i = 0; i < buffer->highlight_tags_count; i++)
|
||||
{
|
||||
buffer->highlight_tags_array[i] = string_split (tags_array[i],
|
||||
"+", 0, 0,
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
string_free_split (tags_array);
|
||||
}
|
||||
buffer->highlight_tags_array = string_split_tags (
|
||||
buffer->highlight_tags,
|
||||
&buffer->highlight_tags_count);
|
||||
}
|
||||
|
||||
/*
|
||||
@@ -2717,7 +2673,7 @@ gui_buffer_close (struct t_gui_buffer *buffer)
|
||||
struct t_gui_window *ptr_window;
|
||||
struct t_gui_buffer *ptr_buffer, *ptr_back_to_buffer;
|
||||
struct t_gui_buffer *ptr_buffer_visited_buffer;
|
||||
int index, i;
|
||||
int index;
|
||||
struct t_gui_buffer_visited *ptr_buffer_visited;
|
||||
|
||||
if (!buffer)
|
||||
@@ -2877,23 +2833,11 @@ gui_buffer_close (struct t_gui_buffer *buffer)
|
||||
if (buffer->highlight_tags_restrict)
|
||||
free (buffer->highlight_tags_restrict);
|
||||
if (buffer->highlight_tags_restrict_array)
|
||||
{
|
||||
for (i = 0; i < buffer->highlight_tags_restrict_count; i++)
|
||||
{
|
||||
string_free_split (buffer->highlight_tags_restrict_array[i]);
|
||||
}
|
||||
free (buffer->highlight_tags_restrict_array);
|
||||
}
|
||||
string_free_split_tags (buffer->highlight_tags_restrict_array);
|
||||
if (buffer->highlight_tags)
|
||||
free (buffer->highlight_tags);
|
||||
if (buffer->highlight_tags_array)
|
||||
{
|
||||
for (i = 0; i < buffer->highlight_tags_count; i++)
|
||||
{
|
||||
string_free_split (buffer->highlight_tags_array[i]);
|
||||
}
|
||||
free (buffer->highlight_tags_array);
|
||||
}
|
||||
string_free_split_tags (buffer->highlight_tags_array);
|
||||
if (buffer->input_callback_data)
|
||||
free (buffer->input_callback_data);
|
||||
if (buffer->close_callback_data)
|
||||
|
||||
+5
-33
@@ -281,9 +281,9 @@ gui_filter_new (int enabled, const char *name, const char *buffer_name,
|
||||
{
|
||||
struct t_gui_filter *new_filter;
|
||||
regex_t *regex1, *regex2;
|
||||
char *pos_tab, *regex_prefix, **tags_array, buf[512], str_error[512];
|
||||
char *pos_tab, *regex_prefix, buf[512], str_error[512];
|
||||
const char *ptr_start_regex, *pos_regex_message;
|
||||
int i, rc;
|
||||
int rc;
|
||||
|
||||
if (!name || !buffer_name || !tags || !regex)
|
||||
{
|
||||
@@ -389,28 +389,8 @@ gui_filter_new (int enabled, const char *name, const char *buffer_name,
|
||||
",", 0, 0,
|
||||
&new_filter->num_buffers);
|
||||
new_filter->tags = (tags) ? strdup (tags) : NULL;
|
||||
new_filter->tags_count = 0;
|
||||
new_filter->tags_array = NULL;
|
||||
if (new_filter->tags)
|
||||
{
|
||||
tags_array = string_split (new_filter->tags, ",", 0, 0,
|
||||
&new_filter->tags_count);
|
||||
if (tags_array)
|
||||
{
|
||||
new_filter->tags_array = malloc (new_filter->tags_count *
|
||||
sizeof (*new_filter->tags_array));
|
||||
if (new_filter->tags_array)
|
||||
{
|
||||
for (i = 0; i < new_filter->tags_count; i++)
|
||||
{
|
||||
new_filter->tags_array[i] = string_split (tags_array[i],
|
||||
"+", 0, 0,
|
||||
NULL);
|
||||
}
|
||||
}
|
||||
string_free_split (tags_array);
|
||||
}
|
||||
}
|
||||
new_filter->tags_array = string_split_tags (new_filter->tags,
|
||||
&new_filter->tags_count);
|
||||
new_filter->regex = strdup (regex);
|
||||
new_filter->regex_prefix = regex1;
|
||||
new_filter->regex_message = regex2;
|
||||
@@ -465,8 +445,6 @@ gui_filter_rename (struct t_gui_filter *filter, const char *new_name)
|
||||
void
|
||||
gui_filter_free (struct t_gui_filter *filter)
|
||||
{
|
||||
int i;
|
||||
|
||||
if (!filter)
|
||||
return;
|
||||
|
||||
@@ -483,13 +461,7 @@ gui_filter_free (struct t_gui_filter *filter)
|
||||
if (filter->tags)
|
||||
free (filter->tags);
|
||||
if (filter->tags_array)
|
||||
{
|
||||
for (i = 0; i < filter->tags_count; i++)
|
||||
{
|
||||
string_free_split (filter->tags_array[i]);
|
||||
}
|
||||
free (filter->tags_array);
|
||||
}
|
||||
string_free_split_tags (filter->tags_array);
|
||||
if (filter->regex)
|
||||
free (filter->regex);
|
||||
if (filter->regex_prefix)
|
||||
|
||||
+21
-12
@@ -604,35 +604,44 @@ gui_line_match_tags (struct t_gui_line_data *line_data,
|
||||
int tags_count, char ***tags_array)
|
||||
{
|
||||
int i, j, k, match, tag_found, tag_negated;
|
||||
const char *ptr_tag;
|
||||
|
||||
if (!line_data)
|
||||
return 0;
|
||||
|
||||
if (line_data->tags_count == 0)
|
||||
return 0;
|
||||
|
||||
for (i = 0; i < tags_count; i++)
|
||||
{
|
||||
match = 1;
|
||||
for (j = 0; tags_array[i][j]; j++)
|
||||
{
|
||||
ptr_tag = tags_array[i][j];
|
||||
tag_found = 0;
|
||||
tag_negated = 0;
|
||||
|
||||
/* check if tag is negated (prefixed with a '!') */
|
||||
if ((tags_array[i][j][0] == '!') && tags_array[i][j][1])
|
||||
tag_negated = 1;
|
||||
|
||||
for (k = 0; k < line_data->tags_count; k++)
|
||||
if ((ptr_tag[0] == '!') && ptr_tag[1])
|
||||
{
|
||||
if (string_match (line_data->tags_array[k],
|
||||
(tag_negated) ? tags_array[i][j] + 1 : tags_array[i][j],
|
||||
0))
|
||||
ptr_tag++;
|
||||
tag_negated = 1;
|
||||
}
|
||||
|
||||
if (strcmp (ptr_tag, "*") == 0)
|
||||
{
|
||||
tag_found = 1;
|
||||
}
|
||||
else
|
||||
{
|
||||
for (k = 0; k < line_data->tags_count; k++)
|
||||
{
|
||||
tag_found = 1;
|
||||
break;
|
||||
if (string_match (line_data->tags_array[k], ptr_tag, 0))
|
||||
{
|
||||
tag_found = 1;
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
if (tag_found && tag_negated)
|
||||
return 0;
|
||||
if ((!tag_found && !tag_negated) || (tag_found && tag_negated))
|
||||
{
|
||||
match = 0;
|
||||
|
||||
@@ -70,6 +70,9 @@ struct t_gui_lines
|
||||
|
||||
extern struct t_gui_lines *gui_lines_alloc ();
|
||||
extern void gui_lines_free (struct t_gui_lines *lines);
|
||||
extern void gui_line_tags_alloc (struct t_gui_line_data *line_data,
|
||||
const char *tags);
|
||||
extern void gui_line_tags_free (struct t_gui_line_data *line_data);
|
||||
extern void gui_line_get_prefix_for_display (struct t_gui_line *line,
|
||||
char **prefix, int *length,
|
||||
char **color, int *prefix_is_nick);
|
||||
|
||||
Reference in New Issue
Block a user