Merged from silc_1_0_branch.
[silc.git] / apps / irssi / src / fe-common / core / themes.c
index 514dd377ba78a0e2c4ec171622efef087345bf0d..437b118faeccf36a8a08eb1b6823c39164ecec22 100644 (file)
@@ -50,6 +50,7 @@ THEME_REC *theme_create(const char *path, const char *name)
        g_return_val_if_fail(name != NULL, NULL);
 
        rec = g_new0(THEME_REC, 1);
+       rec->refcount = 1;
        rec->path = g_strdup(path);
        rec->name = g_strdup(name);
        rec->abstracts = g_hash_table_new((GHashFunc) g_str_hash,
@@ -83,12 +84,8 @@ static void theme_module_destroy(const char *key, MODULE_THEME_REC *rec)
        g_free(rec);
 }
 
-void theme_destroy(THEME_REC *rec)
+static void theme_real_destroy(THEME_REC *rec)
 {
-       themes = g_slist_remove(themes, rec);
-
-       signal_emit("theme destroyed", 1, rec);
-
        g_hash_table_foreach(rec->abstracts, (GHFunc) theme_abstract_destroy, NULL);
        g_hash_table_destroy(rec->abstracts);
        g_hash_table_foreach(rec->modules, (GHFunc) theme_module_destroy, NULL);
@@ -102,6 +99,20 @@ void theme_destroy(THEME_REC *rec)
        g_free(rec);
 }
 
+static void theme_unref(THEME_REC *rec)
+{
+       if (--rec->refcount == 0)
+               theme_real_destroy(rec);
+}
+
+void theme_destroy(THEME_REC *rec)
+{
+       themes = g_slist_remove(themes, rec);
+       signal_emit("theme destroyed", 1, rec);
+
+       theme_unref(rec);
+}
+
 static char *theme_replace_expand(THEME_REC *theme, int index,
                                  char default_fg, char default_bg,
                                  char *last_fg, char *last_bg,
@@ -221,7 +232,7 @@ static void theme_format_append_next(THEME_REC *theme, GString *str,
        }
 
        index = (flags & EXPAND_FLAG_IGNORE_REPLACES) ? -1 :
-               theme->replace_keys[(int) chr];
+               theme->replace_keys[(int) (unsigned char) chr];
        if (index == -1)
                g_string_append_c(str, chr);
        else {
@@ -242,10 +253,11 @@ static int data_is_empty(const char **data)
 {
        /* since we don't know the real argument list, assume there's always
           an argument in them */
-       char *arglist[] = {
+       static char *arglist[] = {
                "x", "x", "x", "x", "x", "x","x", "x", "x", "x",
                NULL
        };
+       SERVER_REC *server;
        const char *p;
        char *ret;
         int free_ret, empty;
@@ -266,8 +278,12 @@ static int data_is_empty(const char **data)
 
        /* variable - check if it's empty */
         p++;
-       ret = parse_special((char **) &p,
-                           active_win == NULL ? NULL : active_win->active_server,
+
+       server = active_win == NULL ? NULL :
+               active_win->active_server != NULL ?
+               active_win->active_server : active_win->connect_server;
+
+       ret = parse_special((char **) &p, server,
                            active_win == NULL ? NULL : active_win->active,
                            arglist, &free_ret, NULL, 0);
         p++;
@@ -285,12 +301,51 @@ static int data_is_empty(const char **data)
         return FALSE;
 }
 
+/* return "data" from {abstract data} string */
+char *theme_format_expand_get(THEME_REC *theme, const char **format)
+{
+       GString *str;
+       char *ret, dummy;
+       int braces = 1; /* we start with one brace opened */
+
+       str = g_string_new(NULL);
+       while ((**format != '\0') && (braces)) {
+               if (**format == '{')
+                       braces++;
+               else if (**format == '}')
+                       braces--;
+               else if ((braces > 1) && (**format == ' ')) {
+                       g_string_append(str, "\\x20");
+                       (*format)++;
+                       continue;
+               } else {
+                       theme_format_append_next(theme, str, format,
+                                                'n', 'n',
+                                                &dummy, &dummy, 0);
+                       continue;
+               }
+               
+               if (!braces) {
+                       (*format)++;
+                       break;
+               }
+
+               g_string_append_c(str, **format);
+               (*format)++;
+       }
+
+       ret = str->str;
+        g_string_free(str, FALSE);
+        return ret;
+}
+
 /* expand a single {abstract ...data... } */
 static char *theme_format_expand_abstract(THEME_REC *theme,
                                          const char **formatp,
                                          char default_fg, char default_bg,
                                          int flags)
 {
+       GString *str;
        const char *p, *format;
        char *abstract, *data, *ret;
        int len;
@@ -329,9 +384,8 @@ static char *theme_format_expand_abstract(THEME_REC *theme,
        abstract = g_strdup(data);
 
        /* we'll need to get the data part. it may contain
-          more abstracts, they are automatically expanded. */
-       data = theme_format_expand_data(theme, formatp, default_fg, default_bg,
-                                       NULL, NULL, flags);
+          more abstracts, they are _NOT_ expanded. */
+       data = theme_format_expand_get(theme, formatp);
        len = strlen(data);
 
        if (len > 1 && i_isdigit(data[len-1]) && data[len-2] == '$') {
@@ -351,7 +405,21 @@ static char *theme_format_expand_abstract(THEME_REC *theme,
                                   PARSE_FLAG_ONLY_ARGS);
        g_free(abstract);
         g_free(data);
-       abstract = ret;
+       str = g_string_new(NULL);
+       p = ret;
+       while (*p != '\0') {
+               if (*p == '\\') {
+                       int chr;
+                       p++;
+                       chr = expand_escape(&p);
+                       g_string_append_c(str, chr != -1 ? chr : *p);
+               } else
+                       g_string_append_c(str, *p);
+               p++;
+       }
+       g_free(ret);
+       abstract = str->str;
+       g_string_free(str, FALSE);
 
        /* abstract may itself contain abstracts or replaces */
        p = abstract;
@@ -542,7 +610,7 @@ static void theme_read_replaces(CONFIG_REC *config, THEME_REC *theme)
 
                if (node->key != NULL && node->value != NULL) {
                        for (p = node->key; *p != '\0'; p++)
-                                theme->replace_keys[(int) *p] = index;
+                                theme->replace_keys[(int) (unsigned char) *p] = index;
 
                        theme->replace_values =
                                g_slist_append(theme->replace_values,
@@ -844,6 +912,8 @@ static int theme_read(THEME_REC *theme, const char *path, const char *data)
 
        theme->default_color =
                config_get_int(config, NULL, "default_color", -1);
+       theme->info_eol = config_get_bool(config, NULL, "info_eol", FALSE);
+
        /* FIXME: remove after 0.7.99 */
        if (theme->default_color == 0 &&
            config_get_int(config, NULL, "default_real_color", -1) != -1)
@@ -1157,6 +1227,8 @@ static void sig_complete_format(GList **list, WINDOW_REC *window,
 
        words = 0;
        do {
+                ptr++;
+
                words++;
                 ptr = strchr(ptr, ' ');
        } while (ptr != NULL);
@@ -1203,10 +1275,20 @@ static void read_settings(void)
 
 static void themes_read(void)
 {
+       GSList *refs;
        char *fname;
 
-       while (themes != NULL)
-               theme_destroy(themes->data);
+       /* increase every theme's refcount, and destroy them. this way if
+          we want to use the theme before it's reloaded we don't crash. */
+       refs = NULL;
+       while (themes != NULL) {
+               THEME_REC *theme = themes->data;
+
+               refs = g_slist_prepend(refs, theme);
+
+               theme->refcount++;
+               theme_destroy(theme);
+       }
 
        /* first there's default theme.. */
        current_theme = theme_load("default");
@@ -1219,7 +1301,12 @@ static void themes_read(void)
        }
 
         window_themes_update();
-        change_theme(settings_get_str("theme"), FALSE);
+       change_theme(settings_get_str("theme"), FALSE);
+
+       while (refs != NULL) {
+               theme_unref(refs->data);
+               refs = g_slist_remove(refs, refs->data);
+       }
 }
 
 void themes_init(void)