summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorSébastien Helleu <flashcode@flashtux.org>2022-09-17 11:07:22 +0200
committerSébastien Helleu <flashcode@flashtux.org>2022-09-17 11:07:22 +0200
commit0df582c7c1d35a7d97f997258215dca5a26866fc (patch)
treefb1315d73b738d7bd40c9727c27cc958f2047259
parenta99fc17d400b9931bdcbc49c53ab7ea4501a2306 (diff)
downloadweechat-0df582c7c1d35a7d97f997258215dca5a26866fc.zip
core: sort filters by name (closes #1695)
-rw-r--r--ChangeLog.adoc1
-rw-r--r--src/gui/gui-filter.c50
2 files changed, 44 insertions, 7 deletions
diff --git a/ChangeLog.adoc b/ChangeLog.adoc
index 7cf0f30f1..132ad39be 100644
--- a/ChangeLog.adoc
+++ b/ChangeLog.adoc
@@ -22,6 +22,7 @@ New features::
* core: add option `-save` in command `/upgrade` (issue #1630)
* core: add option weechat.look.highlight_disable_regex and buffer property "highlight_disable_regex" (issue #1798)
+ * core: sort filters by name (issue #1695)
* api: rename function string_build_with_split_string to string_rebuild_split_string, add arguments "index_start" and "index_end"
* api: add info "uptime_current"
* api: add function crypto_hash_file
diff --git a/src/gui/gui-filter.c b/src/gui/gui-filter.c
index 356770879..7e69fb38e 100644
--- a/src/gui/gui-filter.c
+++ b/src/gui/gui-filter.c
@@ -278,6 +278,26 @@ gui_filter_new_error (const char *name, const char *error)
}
/*
+ * Searches for position of filter in list (to keep filters sorted by name).
+ */
+
+struct t_gui_filter *
+gui_filter_find_pos (struct t_gui_filter *filter)
+{
+ struct t_gui_filter *ptr_filter;
+
+ for (ptr_filter = gui_filters; ptr_filter;
+ ptr_filter = ptr_filter->next_filter)
+ {
+ if (string_strcasecmp (filter->name, ptr_filter->name) < 0)
+ return ptr_filter;
+ }
+
+ /* position not found */
+ return NULL;
+}
+
+/*
* Creates a new filter.
*
* Returns pointer to new filter, NULL if error.
@@ -287,7 +307,7 @@ struct t_gui_filter *
gui_filter_new (int enabled, const char *name, const char *buffer_name,
const char *tags, const char *regex)
{
- struct t_gui_filter *new_filter;
+ struct t_gui_filter *new_filter, *pos_filter;
regex_t *regex1, *regex2;
char *pos_tab, *regex_prefix, buf[512], str_error[512];
const char *ptr_start_regex, *pos_regex_message;
@@ -412,13 +432,29 @@ gui_filter_new (int enabled, const char *name, const char *buffer_name,
new_filter->regex_message = regex2;
/* add filter to filters list */
- new_filter->prev_filter = last_gui_filter;
- if (last_gui_filter)
- last_gui_filter->next_filter = new_filter;
+ pos_filter = gui_filter_find_pos (new_filter);
+ if (pos_filter)
+ {
+ /* add filter before "pos_filter" */
+ new_filter->prev_filter = pos_filter->prev_filter;
+ new_filter->next_filter = pos_filter;
+ if (pos_filter->prev_filter)
+ (pos_filter->prev_filter)->next_filter = new_filter;
+ else
+ gui_filters = new_filter;
+ pos_filter->prev_filter = new_filter;
+ }
else
- gui_filters = new_filter;
- last_gui_filter = new_filter;
- new_filter->next_filter = NULL;
+ {
+ /* add filter to end of list */
+ new_filter->prev_filter = last_gui_filter;
+ new_filter->next_filter = NULL;
+ if (last_gui_filter)
+ last_gui_filter->next_filter = new_filter;
+ else
+ gui_filters = new_filter;
+ last_gui_filter = new_filter;
+ }
(void) hook_signal_send ("filter_added",
WEECHAT_HOOK_SIGNAL_POINTER, new_filter);