From cd80b6cbf23bdf792dbbf6bedcba0bff2889a193 Mon Sep 17 00:00:00 2001 From: Michael Vetter Date: Wed, 29 Jan 2020 12:33:55 +0100 Subject: [PATCH] Change theme handling So far when loading a theme it also overwrote the preferences the user set. Lengthy discussion can be found at https://github.com/profanity-im/profanity/issues/1077 Now we use `/theme load themename` to load the [colours] part of a themem only. `/theme full-load themename` will load the complete theme including preferences set in there. Regards https://github.com/profanity-im/profanity/issues/1077 --- src/command/cmd_ac.c | 27 +++++++++++++++++++++++++++ src/command/cmd_defs.c | 10 ++++++---- src/command/cmd_funcs.c | 19 +++++++++++-------- src/config/theme.c | 8 +++++--- src/config/theme.h | 2 +- src/ui/core.c | 2 +- 6 files changed, 51 insertions(+), 17 deletions(-) diff --git a/src/command/cmd_ac.c b/src/command/cmd_ac.c index 0ee9af9f..f63c9e07 100644 --- a/src/command/cmd_ac.c +++ b/src/command/cmd_ac.c @@ -354,6 +354,7 @@ cmd_ac_init(void) theme_ac = autocomplete_new(); autocomplete_add(theme_ac, "load"); + autocomplete_add(theme_ac, "full-load"); autocomplete_add(theme_ac, "list"); autocomplete_add(theme_ac, "colours"); autocomplete_add(theme_ac, "properties"); @@ -2414,23 +2415,49 @@ static char* _theme_autocomplete(ProfWin *window, const char *const input, gboolean previous) { char *result = NULL; + if (strncmp(input, "/theme load ", 12) == 0) { if (theme_load_ac == NULL) { theme_load_ac = autocomplete_new(); GSList *themes = theme_list(); GSList *curr = themes; + while (curr) { autocomplete_add(theme_load_ac, curr->data); curr = g_slist_next(curr); } + g_slist_free_full(themes, g_free); autocomplete_add(theme_load_ac, "default"); } + result = autocomplete_param_with_ac(input, "/theme load", theme_load_ac, TRUE, previous); if (result) { return result; } } + + if (strncmp(input, "/theme full-load ", 17) == 0) { + if (theme_load_ac == NULL) { + theme_load_ac = autocomplete_new(); + GSList *themes = theme_list(); + GSList *curr = themes; + + while (curr) { + autocomplete_add(theme_load_ac, curr->data); + curr = g_slist_next(curr); + } + + g_slist_free_full(themes, g_free); + autocomplete_add(theme_load_ac, "default"); + } + + result = autocomplete_param_with_ac(input, "/theme full-load", theme_load_ac, TRUE, previous); + if (result) { + return result; + } + } + result = autocomplete_param_with_ac(input, "/theme", theme_ac, TRUE, previous); if (result) { return result; diff --git a/src/command/cmd_defs.c b/src/command/cmd_defs.c index b1ba363f..b583a06b 100644 --- a/src/command/cmd_defs.c +++ b/src/command/cmd_defs.c @@ -2136,15 +2136,17 @@ static struct cmd_t command_defs[] = CMD_SYN( "/theme list", "/theme load ", + "/theme full-load ", "/theme colours", "/theme properties") CMD_DESC( "Load a theme, includes colours and UI options.") CMD_ARGS( - { "list", "List all available themes." }, - { "load ", "Load the specified theme. 'default' will reset to the default theme." }, - { "colours", "Show colour values as rendered by the terminal." }, - { "properties", "Show colour settings for current theme." }) + { "list", "List all available themes." }, + { "load ", "Load colours from specified theme. 'default' will reset to the default theme." }, + { "full-load ", "Same as 'load' but will also load preferences set in the theme, not just colours." }, + { "colours", "Show colour values as rendered by the terminal." }, + { "properties", "Show colour settings for current theme." }) CMD_EXAMPLES( "/theme list", "/theme load forest") diff --git a/src/command/cmd_funcs.c b/src/command/cmd_funcs.c index a0d40cdd..930004b3 100644 --- a/src/command/cmd_funcs.c +++ b/src/command/cmd_funcs.c @@ -804,7 +804,7 @@ _account_set_theme(char *account_name, char *theme) ProfAccount *account = accounts_get_account(session_get_account_name()); if (account) { if (g_strcmp0(account->name, account_name) == 0) { - theme_load(theme); + theme_load(theme, false); ui_load_colours(); if (prefs_get_boolean(PREF_ROSTER)) { ui_show_roster(); @@ -1234,13 +1234,13 @@ cmd_disconnect(ProfWin *window, const char *const command, gchar **args) char *theme = prefs_get_string(PREF_THEME); if (theme) { - gboolean res = theme_load(theme); + gboolean res = theme_load(theme, false); prefs_free_string(theme); if (!res) { - theme_load("default"); + theme_load("default", false); } } else { - theme_load("default"); + theme_load("default", false); } ui_load_colours(); if (prefs_get_boolean(PREF_ROSTER)) { @@ -1699,6 +1699,9 @@ cmd_prefs(ProfWin *window, const char *const command, gchar **args) gboolean cmd_theme(ProfWin *window, const char *const command, gchar **args) { + // 'full-load' means to load the theme including the settings (not just [colours]) + gboolean fullload = (g_strcmp0(args[0], "full-load") == 0); + // list themes if (g_strcmp0(args[0], "list") == 0) { GSList *themes = theme_list(); @@ -1706,10 +1709,10 @@ cmd_theme(ProfWin *window, const char *const command, gchar **args) g_slist_free_full(themes, g_free); // load a theme - } else if (g_strcmp0(args[0], "load") == 0) { + } else if (g_strcmp0(args[0], "load") == 0 || fullload) { if (args[1] == NULL) { cons_bad_cmd_usage(command); - } else if (theme_load(args[1])) { + } else if (theme_load(args[1], fullload)) { ui_load_colours(); prefs_set_string(PREF_THEME, args[1]); if (prefs_get_boolean(PREF_ROSTER)) { @@ -8619,12 +8622,12 @@ cmd_color(ProfWin *window, const char *const command, gchar **args) char *theme = prefs_get_string(PREF_THEME); if (theme) { - gboolean res = theme_load(theme); + gboolean res = theme_load(theme, false); if (res) { cons_show("Theme reloaded: %s", theme); } else { - theme_load("default"); + theme_load("default", false); } prefs_free_string(theme); diff --git a/src/config/theme.c b/src/config/theme.c index 8863d5e9..f081bd21 100644 --- a/src/config/theme.c +++ b/src/config/theme.c @@ -154,7 +154,7 @@ theme_init(const char *const theme_name) g_hash_table_insert(defaults, strdup("untrusted"), strdup("red")); g_hash_table_insert(defaults, strdup("cmd.wins.unread"), strdup("default")); - _load_preferences(); + //_load_preferences(); } gboolean @@ -174,12 +174,14 @@ theme_exists(const char *const theme_name) } gboolean -theme_load(const char *const theme_name) +theme_load(const char *const theme_name, gboolean load_theme_prefs) { color_pair_cache_reset(); if (_theme_load_file(theme_name)) { - _load_preferences(); + if (load_theme_prefs) { + _load_preferences(); + } return TRUE; } else { return FALSE; diff --git a/src/config/theme.h b/src/config/theme.h index 65d91257..12b7616a 100644 --- a/src/config/theme.h +++ b/src/config/theme.h @@ -143,7 +143,7 @@ typedef enum { void theme_init(const char *const theme_name); void theme_init_colours(void); -gboolean theme_load(const char *const theme_name); +gboolean theme_load(const char *const theme_name, gboolean load_theme_prefs); gboolean theme_exists(const char *const theme_name); GSList* theme_list(void); void theme_close(void); diff --git a/src/ui/core.c b/src/ui/core.c index 545c508f..813fc9ad 100644 --- a/src/ui/core.c +++ b/src/ui/core.c @@ -364,7 +364,7 @@ void ui_handle_login_account_success(ProfAccount *account, gboolean secured) { if (account->theme) { - if (theme_load(account->theme)) { + if (theme_load(account->theme, false)) { ui_load_colours(); if (prefs_get_boolean(PREF_ROSTER)) { ui_show_roster();