I did a patch today to only display changed values in /set, I've already
had it come in handy when helping my friend config his weechat to
something similar to mine.

Doesn't have tab completion yet, FlashCode said he would look into
it. :)


Syntax:
 
/set diff [<option>...]

Try set-diff using:

/set diff
/set diff irc.* weechat.look.*
/set diff doesn't.exist* what.is.this weechat.look.*

- Peter
>From 6221686256dd9ec4605ce3992c6c3e48d300535c Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Peter=20Bostr=C3=B6m?= <[email protected]>
Date: Sun, 16 Dec 2012 02:55:51 +0100
Subject: [PATCH] diff switch for /set, display only changed values

---
 src/core/wee-command.c |  166 ++++++++++++++++++++++++++++++++++--------------
 src/core/wee-config.c  |   27 ++++++++
 src/core/wee-config.h  |    1 +
 3 files changed, 146 insertions(+), 48 deletions(-)

diff --git a/src/core/wee-command.c b/src/core/wee-command.c
index dada867..efff09d 100644
--- a/src/core/wee-command.c
+++ b/src/core/wee-command.c
@@ -4354,14 +4354,14 @@ command_set_display_option (struct t_config_option *option,
 {
     const char *color_name;
     const char *display_undefined = _("(undefined)");
-    const char *display_default;
+    const char *display_default = NULL;
     char str_default[128];
     int is_file_plugins_conf;
 
-    display_default = NULL;
     is_file_plugins_conf = (option->config_file && option->config_file->name
                             && (strcmp (option->config_file->name, "plugins") == 0));
 
+
     if (option->value)
     {
         if (!is_file_plugins_conf && !option->default_value)
@@ -4532,7 +4532,8 @@ command_set_display_option (struct t_config_option *option,
  */
 
 int
-command_set_display_option_list (const char *message, const char *search)
+command_set_display_option_list (const char *message, const char *search,
+                                 int display_only_changed)
 {
     int number_found, section_displayed, length;
     struct t_config_file *ptr_config;
@@ -4545,6 +4546,8 @@ command_set_display_option_list (const char *message, const char *search)
     for (ptr_config = config_files; ptr_config;
          ptr_config = ptr_config->next_config)
     {
+        if (display_only_changed && strcmp(ptr_config->name, "plugins") == 0)
+            continue;
         for (ptr_section = ptr_config->sections; ptr_section;
              ptr_section = ptr_section->next_section)
         {
@@ -4553,6 +4556,10 @@ command_set_display_option_list (const char *message, const char *search)
             for (ptr_option = ptr_section->options; ptr_option;
                  ptr_option = ptr_option->next_option)
             {
+                if (display_only_changed &&
+                    !config_option_has_changed(ptr_option))
+                    continue;
+
                 length = strlen (ptr_config->name) + 1
                     + strlen (ptr_section->name) + 1
                     + strlen (ptr_option->name) + 1;
@@ -4585,72 +4592,133 @@ command_set_display_option_list (const char *message, const char *search)
 }
 
 /*
- * Callback for command "/set": sets configuration options.
+ * Display multiple lists of options
+ * 
+ * Returns the total number of options displayed.
  */
-
-COMMAND_CALLBACK(set)
+int
+command_set_display_option_lists (const char *message, char **search,
+                                 char **search_end,
+                                 int display_only_changed)
 {
-    char *value;
-    int number_found, rc;
-    struct t_config_option *ptr_option, *ptr_option_before;
+    int total_number_found = 0, number_found, display_single_changed;
 
-    /* make C compiler happy */
-    (void) data;
-    (void) buffer;
-
-    /* display list of options */
-    if (argc < 3)
+    display_single_changed = display_only_changed && search == search_end;
+    
+    while (1)
     {
-        number_found = 0;
+        number_found = command_set_display_option_list (message,
+                search ? *search : NULL,
+                display_only_changed);
 
-        number_found += command_set_display_option_list (NULL,
-                                                         (argc == 2) ?
-                                                         argv[1] : NULL);
+        total_number_found += number_found;
+
+        if (display_single_changed)
+            break;
 
         if (number_found == 0)
         {
-            if (argc == 2)
+            if (search)
             {
                 gui_chat_printf (NULL,
-                                 _("%sOption \"%s\" not found (tip: you can use "
-                                   "\"*\" at beginning and/or end of option to "
-                                   "see a sublist)"),
-                                 gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
-                                 argv[1]);
+                        _("%sOption \"%s\" not found (tip: you can use "
+                            "\"*\" at beginning and/or end of option to "
+                            "see a sublist)"),
+                        gui_chat_prefix[GUI_CHAT_PREFIX_ERROR],
+                        *search);
             }
             else
             {
                 gui_chat_printf (NULL,
-                                 _("No configuration option found"));
+                        _("No configuration options found"));
             }
         }
         else
         {
             gui_chat_printf (NULL, "");
-            if (argc == 2)
+            if (search)
             {
                 gui_chat_printf (NULL,
-                                 NG_("%s%d%s configuration option found "
-                                     "matching with \"%s\"",
-                                     "%s%d%s configuration options found "
-                                     "matching with \"%s\"",
-                                     number_found),
-                                 GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
-                                 number_found,
-                                 GUI_COLOR(GUI_COLOR_CHAT),
-                                 argv[1]);
+                        NG_("%s%d%s configuration option found "
+                            "matching with \"%s\"",
+                            "%s%d%s configuration options found "
+                            "matching with \"%s\"",
+                            number_found),
+                        GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
+                        number_found,
+                        GUI_COLOR(GUI_COLOR_CHAT),
+                        *search);
             }
             else
             {
                 gui_chat_printf (NULL,
-                                 NG_("%s%d%s configuration option found",
-                                     "%s%d%s configuration options found",
-                                     number_found),
-                                 GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
-                                 number_found,
-                                 GUI_COLOR(GUI_COLOR_CHAT));
+                        NG_("%s%d%s configuration option found",
+                            "%s%d%s configuration options found",
+                            number_found),
+                        GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
+                        number_found,
+                        GUI_COLOR(GUI_COLOR_CHAT));
             }
         }
+
+        if (search == search_end)
+            break;
+        search++;
+    }
+
+    return total_number_found;
+}
+
+/*
+ * Callback for command "/set": sets configuration options.
+ */
+
+COMMAND_CALLBACK(set)
+{
+    char *value, **argv_option = NULL;
+    int number_found, rc, display_only_changed = 0, display_matching;
+    struct t_config_option *ptr_option, *ptr_option_before;
+
+    /* if "diff" is specified as first argument, only display changed values */
+    if (argc >= 2 && string_strcasecmp(argv[1], "diff") == 0)
+    {
+        if (argc >= 3)
+            argv_option = &argv[2];
+
+        display_only_changed = 1;
+    }
+    else if (argc >= 2)
+    {
+        argv_option = &argv[1];
+    }
+
+    /* make C compiler happy */
+    (void) data;
+    (void) buffer;
+
+    /* display list of options */
+    if (argc < 3 || display_only_changed)
+    {
+        display_matching = argv_option == &argv[argc-1];
+        number_found = command_set_display_option_lists(NULL, argv_option,
+                                                       argv_option ? &argv[argc-1]: NULL,
+                                                       display_only_changed);
+        
+        if (display_only_changed)
+        {
+            gui_chat_printf (NULL, "");
+            gui_chat_printf (NULL,
+                    NG_("%s%d%s changed configuration option found%s%s%s",
+                        "%s%d%s changed configuration options found%s%s%s",
+                        number_found),
+                    GUI_COLOR(GUI_COLOR_CHAT_BUFFER),
+                    number_found,
+                    GUI_COLOR(GUI_COLOR_CHAT),
+                    display_matching ? " matching with \"" : "",
+                    display_matching ? *argv_option : "",
+                    display_matching ? "\"" : "");
+        }
+
         return WEECHAT_RC_OK;
     }
 
@@ -4694,7 +4762,6 @@ COMMAND_CALLBACK(set)
 
     return WEECHAT_RC_OK;
 }
-
 /*
  * Callback for command "/unset": unsets/resets configuration options.
  */
@@ -6346,15 +6413,16 @@ command_init ()
                   &command_save, NULL);
     hook_command (NULL, "set",
                   N_("set config options"),
-                  N_("[<option> [<value>]]"),
+                  N_("[<option> [<value>]] | diff [<option>...]"),
                   N_("option: name of an option (can start or end with '*' "
                      "to list many options)\n"
-                     " value: new value for option\n\n"
+                     " value: new value for option\n"
+                     "  diff: display only changed values\n\n"
                      "New value can be, according to variable type:\n"
                      "  boolean: on, off or toggle\n"
                      "  integer: number, ++number or --number\n"
-                     "  string : any string (\"\" for empty string)\n"
-                     "  color  : color name, ++number or --number\n\n"
+                     "   string: any string (\"\" for empty string)\n"
+                     "    color: color name, ++number or --number\n\n"
                      "For all types, you can use null to remove "
                      "option value (undefined value). This works only "
                      "for some special plugin variables.\n\n"
@@ -6362,7 +6430,9 @@ command_init ()
                      "  display options about highlight:\n"
                      "    /set *highlight*\n"
                      "  add a word to highlight:\n"
-                     "    /set weechat.look.highlight \"word\""),
+                     "    /set weechat.look.highlight \"word\"\n"
+                     "  display changed options:\n"
+                     "    /set diff"),
                   "%(config_options) %(config_option_values)",
                   &command_set, NULL);
     hook_command (NULL, "unset",
diff --git a/src/core/wee-config.c b/src/core/wee-config.c
index 530bd9d..e31c3a0 100644
--- a/src/core/wee-config.c
+++ b/src/core/wee-config.c
@@ -748,6 +748,33 @@ config_day_change_timer_cb (void *data, int remaining_calls)
 }
 
 /*
+ * Returns non-zero if a specific option has changed.
+ */
+
+int config_option_has_changed(struct t_config_option *option)
+{
+    if (!option->value)
+        return 0;
+    if (!option->default_value)
+        return 1;
+    switch (option->type)
+    {
+        case CONFIG_OPTION_TYPE_BOOLEAN:
+            return CONFIG_BOOLEAN(option) != CONFIG_BOOLEAN_DEFAULT(option);
+        case CONFIG_OPTION_TYPE_INTEGER:
+            return CONFIG_INTEGER(option) != CONFIG_INTEGER_DEFAULT(option);
+        case CONFIG_OPTION_TYPE_STRING:
+            return strcmp(CONFIG_STRING(option), CONFIG_STRING_DEFAULT(option)) != 0;
+        case CONFIG_OPTION_TYPE_COLOR:
+            return CONFIG_COLOR(option) != CONFIG_COLOR_DEFAULT(option);
+        case CONFIG_NUM_OPTION_TYPES:
+            /* make C compiler happy */
+            break;
+    }
+    return 0;
+}
+
+/*
  * Initializes some things after reading/reloading WeeChat configuration file.
  */
 
diff --git a/src/core/wee-config.h b/src/core/wee-config.h
index 1834b04..fc5412d 100644
--- a/src/core/wee-config.h
+++ b/src/core/wee-config.h
@@ -267,6 +267,7 @@ extern int config_num_highlight_tags;
 extern char **config_plugin_extensions;
 extern int config_num_plugin_extensions;
 
+extern int config_option_has_changed(struct t_config_option *option);
 
 extern struct t_config_option *config_weechat_debug_get (const char *plugin_name);
 extern int config_weechat_debug_set (const char *plugin_name,
-- 
1.7.9.5

_______________________________________________
Weechat-dev mailing list
[email protected]
https://lists.nongnu.org/mailman/listinfo/weechat-dev

Reply via email to