4 Copyright (C) 1999-2000 Timo Sirainen
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
22 #include "module-formats.h"
27 #include "special-vars.h"
28 #include "lib-config/iconfig.h"
32 #include "printtext.h"
34 #include "default-theme.h"
37 THEME_REC *current_theme;
38 GHashTable *default_formats;
40 static int init_finished;
41 static char *init_errors;
43 static int theme_read(THEME_REC *theme, const char *path, const char *data);
45 THEME_REC *theme_create(const char *path, const char *name)
49 g_return_val_if_fail(path != NULL, NULL);
50 g_return_val_if_fail(name != NULL, NULL);
52 rec = g_new0(THEME_REC, 1);
53 rec->path = g_strdup(path);
54 rec->name = g_strdup(name);
55 rec->abstracts = g_hash_table_new((GHashFunc) g_str_hash,
56 (GCompareFunc) g_str_equal);
57 rec->modules = g_hash_table_new((GHashFunc) g_istr_hash,
58 (GCompareFunc) g_istr_equal);
59 themes = g_slist_append(themes, rec);
60 signal_emit("theme created", 1, rec);
65 static void theme_abstract_destroy(char *key, char *value)
71 static void theme_module_destroy(const char *key, MODULE_THEME_REC *rec)
75 for (n = 0; n < rec->count; n++) {
76 g_free_not_null(rec->formats[n]);
77 g_free_not_null(rec->expanded_formats[n]);
80 g_free(rec->expanded_formats);
86 void theme_destroy(THEME_REC *rec)
88 themes = g_slist_remove(themes, rec);
90 signal_emit("theme destroyed", 1, rec);
92 g_hash_table_foreach(rec->abstracts, (GHFunc) theme_abstract_destroy, NULL);
93 g_hash_table_destroy(rec->abstracts);
94 g_hash_table_foreach(rec->modules, (GHFunc) theme_module_destroy, NULL);
95 g_hash_table_destroy(rec->modules);
97 g_slist_foreach(rec->replace_values, (GFunc) g_free, NULL);
98 g_slist_free(rec->replace_values);
105 static char *theme_replace_expand(THEME_REC *theme, int index,
106 char default_fg, char default_bg,
107 char *last_fg, char *last_bg,
111 char *ret, *abstract, data[2];
113 rec = g_slist_nth(theme->replace_values, index);
114 g_return_val_if_fail(rec != NULL, NULL);
116 data[0] = chr; data[1] = '\0';
118 abstract = rec->data;
119 abstract = theme_format_expand_data(theme, (const char **) &abstract,
120 default_fg, default_bg,
121 last_fg, last_bg, flags);
122 ret = parse_special_string(abstract, NULL, NULL, data, NULL,
123 PARSE_FLAG_ONLY_ARGS);
128 static const char *fgcolorformats = "nkrgybmpcwKRGYBMPCW";
129 static const char *bgcolorformats = "n01234567";
131 #define IS_FGCOLOR_FORMAT(c) \
132 ((c) != '\0' && strchr(fgcolorformats, c) != NULL)
133 #define IS_BGCOLOR_FORMAT(c) \
134 ((c) != '\0' && strchr(bgcolorformats, c) != NULL)
136 /* append "variable" part in $variable, ie. not the contents of the variable */
137 static void theme_format_append_variable(GString *str, const char **format)
140 char *value, *args[1] = { NULL };
146 value = parse_special((char **) format, NULL, NULL,
147 args, &free_ret, NULL, PARSE_FLAG_ONLY_ARGS);
148 if (free_ret) g_free(value);
151 /* append the variable name */
152 value = g_strndup(orig, (int) (*format-orig));
153 g_string_append(str, value);
157 /* append next "item", either a character, $variable or %format */
158 static void theme_format_append_next(THEME_REC *theme, GString *str,
160 char default_fg, char default_bg,
161 char *last_fg, char *last_bg,
168 if ((chr == '$' || chr == '%') &&
169 (*format)[1] == '\0') {
170 /* last char, always append */
171 g_string_append_c(str, chr);
177 /* $variable .. we'll always need to skip this, since it
178 may contain characters that are in replace chars. */
179 theme_format_append_variable(str, format);
183 if (**format == '%') {
186 if (**format != '{' && **format != '}') {
188 if (**format == 'n') {
189 /* %n = change to default color */
190 g_string_append(str, "%n");
192 if (default_bg != 'n') {
193 g_string_append_c(str, '%');
194 g_string_append_c(str, default_bg);
196 if (default_fg != 'n') {
197 g_string_append_c(str, '%');
198 g_string_append_c(str, default_fg);
201 *last_fg = default_fg;
202 *last_bg = default_bg;
204 if (IS_FGCOLOR_FORMAT(chr))
206 if (IS_BGCOLOR_FORMAT(chr))
208 g_string_append_c(str, '%');
209 g_string_append_c(str, chr);
215 /* %{ or %} gives us { or } char - keep the % char
216 though to make sure {} isn't treated as abstract */
217 g_string_append_c(str, '%');
221 index = (flags & EXPAND_FLAG_IGNORE_REPLACES) ? -1 :
222 theme->replace_keys[(int) chr];
224 g_string_append_c(str, chr);
228 value = theme_replace_expand(theme, index,
229 default_fg, default_bg,
230 last_fg, last_bg, chr, flags);
231 g_string_append(str, value);
238 /* returns TRUE if data is empty, or the data is a $variable which is empty */
239 static int data_is_empty(const char **data)
241 /* since we don't know the real argument list, assume there's always
242 an argument in them */
244 "x", "x", "x", "x", "x", "x","x", "x", "x", "x",
252 while (*p == ' ') p++;
265 /* variable - check if it's empty */
267 ret = parse_special((char **) &p,
268 active_win == NULL ? NULL : active_win->active_server,
269 active_win == NULL ? NULL : active_win->active,
270 arglist, &free_ret, NULL, 0);
273 while (*p == ' ') p++;
274 empty = *p == '}' && (ret == NULL || *ret == '\0');
275 if (free_ret) g_free(ret);
286 /* expand a single {abstract ...data... } */
287 static char *theme_format_expand_abstract(THEME_REC *theme,
288 const char **formatp,
289 char default_fg, char default_bg,
292 const char *p, *format;
293 char *abstract, *data, *ret;
298 /* get abstract name first */
300 while (*p != '\0' && *p != ' ' &&
301 *p != '{' && *p != '}') p++;
302 if (*p == '\0' || p == format)
303 return NULL; /* error */
305 len = (int) (p-format);
306 abstract = g_strndup(format, len);
308 /* skip the following space, if there's any more spaces they're
309 treated as arguments */
312 if ((flags & EXPAND_FLAG_IGNORE_EMPTY) && data_is_empty(&p)) {
318 *formatp = format+len;
320 /* get the abstract data */
321 data = g_hash_table_lookup(theme->abstracts, abstract);
324 /* unknown abstract, just display the data */
327 abstract = g_strdup(data);
329 /* we'll need to get the data part. it may contain
330 more abstracts, they are automatically expanded. */
331 data = theme_format_expand_data(theme, formatp, default_fg, default_bg,
335 if (len > 1 && i_isdigit(data[len-1]) && data[len-2] == '$') {
336 /* ends with $<digit> .. this breaks things if next
337 character is digit or '-' */
344 data = g_strdup_printf("%s{%c}", tmp, digit);
348 ret = parse_special_string(abstract, NULL, NULL, data, NULL,
349 PARSE_FLAG_ONLY_ARGS);
354 /* abstract may itself contain abstracts or replaces */
356 ret = theme_format_expand_data(theme, &p, default_fg, default_bg,
357 &default_fg, &default_bg,
358 flags | EXPAND_FLAG_LASTCOLOR_ARG);
363 /* expand the data part in {abstract data} */
364 char *theme_format_expand_data(THEME_REC *theme, const char **format,
365 char default_fg, char default_bg,
366 char *save_last_fg, char *save_last_bg,
370 char *ret, *abstract;
371 char last_fg, last_bg;
374 last_fg = default_fg;
375 last_bg = default_bg;
376 recurse_flags = flags & EXPAND_FLAG_RECURSIVE_MASK;
378 str = g_string_new(NULL);
379 while (**format != '\0') {
380 if ((flags & EXPAND_FLAG_ROOT) == 0 && **format == '}') {
381 /* ignore } if we're expanding original string */
386 if (**format != '{') {
387 if ((flags & EXPAND_FLAG_LASTCOLOR_ARG) &&
388 **format == '$' && (*format)[1] == '0') {
389 /* save the color before $0 ..
390 this is for the %n replacing */
391 if (save_last_fg != NULL) {
392 *save_last_fg = last_fg;
395 if (save_last_bg != NULL) {
396 *save_last_bg = last_bg;
401 theme_format_append_next(theme, str, format,
402 default_fg, default_bg,
409 if (**format == '\0' || **format == '}')
412 /* get a single {...} */
413 abstract = theme_format_expand_abstract(theme, format,
416 if (abstract != NULL) {
417 g_string_append(str, abstract);
422 if ((flags & EXPAND_FLAG_LASTCOLOR_ARG) == 0) {
423 /* save the last color */
424 if (save_last_fg != NULL)
425 *save_last_fg = last_fg;
426 if (save_last_bg != NULL)
427 *save_last_bg = last_bg;
431 g_string_free(str, FALSE);
435 #define IS_OLD_FORMAT(code, last_fg, last_bg) \
436 (((code) == 'n' && (last_fg) == 'n' && (last_bg) == 'n') || \
437 ((code) != 'n' && ((code) == (last_fg) || (code) == (last_bg))))
439 static char *theme_format_compress_colors(THEME_REC *theme, const char *format)
442 char *ret, last_fg, last_bg;
444 str = g_string_new(NULL);
446 last_fg = last_bg = '\0';
447 while (*format != '\0') {
448 if (*format == '$') {
449 /* $variable, skrip it entirely */
450 theme_format_append_variable(str, &format);
451 last_fg = last_bg = '\0';
452 } else if (*format != '%') {
453 /* a normal character */
454 g_string_append_c(str, *format);
459 if (IS_OLD_FORMAT(*format, last_fg, last_bg)) {
460 /* active color set again */
461 } else if (IS_FGCOLOR_FORMAT(*format) &&
463 IS_FGCOLOR_FORMAT(format[2]) &&
464 (*format != 'n' || format[2] == 'n')) {
465 /* two fg colors in a row. bg colors are
466 so rare that we don't bother checking
469 /* some format, add it */
470 g_string_append_c(str, '%');
471 g_string_append_c(str, *format);
473 if (IS_FGCOLOR_FORMAT(*format))
475 if (IS_BGCOLOR_FORMAT(*format))
483 g_string_free(str, FALSE);
487 char *theme_format_expand(THEME_REC *theme, const char *format)
491 g_return_val_if_fail(theme != NULL, NULL);
492 g_return_val_if_fail(format != NULL, NULL);
494 data = theme_format_expand_data(theme, &format, 'n', 'n', NULL, NULL,
496 ret = theme_format_compress_colors(theme, data);
501 static MODULE_THEME_REC *theme_module_create(THEME_REC *theme, const char *module)
503 MODULE_THEME_REC *rec;
506 rec = g_hash_table_lookup(theme->modules, module);
507 if (rec != NULL) return rec;
509 formats = g_hash_table_lookup(default_formats, module);
510 g_return_val_if_fail(formats != NULL, NULL);
512 rec = g_new0(MODULE_THEME_REC, 1);
513 rec->name = g_strdup(module);
515 for (rec->count = 0; formats[rec->count].def != NULL; rec->count++) ;
516 rec->formats = g_new0(char *, rec->count);
517 rec->expanded_formats = g_new0(char *, rec->count);
519 g_hash_table_insert(theme->modules, rec->name, rec);
523 static void theme_read_replaces(CONFIG_REC *config, THEME_REC *theme)
530 /* reset replace keys */
531 for (index = 0; index < 256; index++)
532 theme->replace_keys[index] = -1;
535 node = config_node_traverse(config, "replaces", FALSE);
536 if (node == NULL || node->type != NODE_TYPE_BLOCK) return;
538 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
541 if (node->key != NULL && node->value != NULL) {
542 for (p = node->key; *p != '\0'; p++)
543 theme->replace_keys[(int) *p] = index;
545 theme->replace_values =
546 g_slist_append(theme->replace_values,
547 g_strdup(node->value));
553 static void theme_read_abstracts(CONFIG_REC *config, THEME_REC *theme)
557 gpointer oldkey, oldvalue;
559 node = config_node_traverse(config, "abstracts", FALSE);
560 if (node == NULL || node->type != NODE_TYPE_BLOCK) return;
562 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
565 if (node->key == NULL || node->value == NULL)
568 if (g_hash_table_lookup_extended(theme->abstracts, node->key,
569 &oldkey, &oldvalue)) {
570 /* new values override old ones */
571 g_hash_table_remove(theme->abstracts, oldkey);
576 g_hash_table_insert(theme->abstracts, g_strdup(node->key),
577 g_strdup(node->value));
581 static void theme_set_format(THEME_REC *theme, MODULE_THEME_REC *rec,
583 const char *key, const char *value)
587 num = format_find_tag(module, key);
589 rec->formats[num] = g_strdup(value);
590 rec->expanded_formats[num] = theme_format_expand(theme, value);
594 static void theme_read_formats(THEME_REC *theme, const char *module,
595 CONFIG_REC *config, MODULE_THEME_REC *rec)
600 node = config_node_traverse(config, "formats", FALSE);
601 if (node == NULL) return;
602 node = config_node_section(node, module, -1);
603 if (node == NULL) return;
605 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
608 if (node->key != NULL && node->value != NULL) {
609 theme_set_format(theme, rec, module,
610 node->key, node->value);
615 static void theme_init_module(THEME_REC *theme, const char *module,
618 MODULE_THEME_REC *rec;
622 formats = g_hash_table_lookup(default_formats, module);
623 g_return_if_fail(formats != NULL);
625 rec = theme_module_create(theme, module);
628 theme_read_formats(theme, module, config, rec);
630 /* expand the remaining formats */
631 for (n = 0; n < rec->count; n++) {
632 if (rec->expanded_formats[n] == NULL) {
633 rec->expanded_formats[n] =
634 theme_format_expand(theme, formats[n].def);
639 static void sig_print_errors(void)
641 init_finished = TRUE;
643 if (init_errors != NULL) {
644 signal_emit("gui dialog", 2, "error", init_errors);
649 static void theme_read_module(THEME_REC *theme, const char *module)
653 config = config_open(theme->path, -1);
655 config_parse(config);
657 theme_init_module(theme, module, config);
659 if (config != NULL) config_close(config);
662 static void themes_read_module(const char *module)
664 g_slist_foreach(themes, (GFunc) theme_read_module, (void *) module);
667 static void theme_remove_module(THEME_REC *theme, const char *module)
669 MODULE_THEME_REC *rec;
671 rec = g_hash_table_lookup(theme->modules, module);
672 if (rec == NULL) return;
674 g_hash_table_remove(theme->modules, module);
675 theme_module_destroy(module, rec);
678 static void themes_remove_module(const char *module)
680 g_slist_foreach(themes, (GFunc) theme_remove_module, (void *) module);
683 void theme_register_module(const char *module, FORMAT_REC *formats)
685 if (g_hash_table_lookup(default_formats, module) != NULL)
688 g_hash_table_insert(default_formats, g_strdup(module), formats);
689 themes_read_module(module);
692 void theme_unregister_module(const char *module)
696 if (default_formats == NULL)
697 return; /* already uninitialized */
699 if (!g_hash_table_lookup_extended(default_formats, module, &key, &value))
702 g_hash_table_remove(default_formats, key);
705 themes_remove_module(module);
708 static THEME_REC *theme_find(const char *name)
712 for (tmp = themes; tmp != NULL; tmp = tmp->next) {
713 THEME_REC *rec = tmp->data;
715 if (g_strcasecmp(rec->name, name) == 0)
722 static void window_themes_update(void)
726 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
727 WINDOW_REC *rec = tmp->data;
729 if (rec->theme_name != NULL)
730 rec->theme = theme_load(rec->theme_name);
734 THEME_REC *theme_load(const char *setname)
736 THEME_REC *theme, *oldtheme;
738 char *fname, *name, *p;
740 name = g_strdup(setname);
741 p = strrchr(name, '.');
742 if (p != NULL && strcmp(p, ".theme") == 0) {
743 /* remove the trailing .theme */
747 theme = theme_find(name);
750 fname = g_strdup_printf("%s/%s.theme", get_irssi_dir(), name);
751 if (stat(fname, &statbuf) != 0) {
752 /* check global config dir */
754 fname = g_strdup_printf(THEMESDIR"/%s.theme", name);
755 if (stat(fname, &statbuf) != 0) {
756 /* theme not found */
759 return theme; /* use the one in memory if possible */
763 if (theme != NULL && theme->last_modify == statbuf.st_mtime) {
764 /* theme not modified, use the one already in memory */
771 theme = theme_create(fname, name);
772 theme->last_modify = statbuf.st_mtime;
773 if (!theme_read(theme, theme->path, NULL)) {
774 /* error reading .theme file */
775 theme_destroy(theme);
779 if (oldtheme != NULL && theme != NULL) {
780 theme_destroy(oldtheme);
781 window_themes_update();
794 static void theme_read_modules(const char *module, void *value,
797 theme_init_module(rec->theme, module, rec->config);
800 static void read_error(const char *str)
805 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", str);
806 else if (init_errors == NULL)
807 init_errors = g_strdup(str);
810 init_errors = g_strconcat(init_errors, "\n", str, NULL);
815 static int theme_read(THEME_REC *theme, const char *path, const char *data)
821 config = config_open(data == NULL ? path : NULL, -1) ;
822 if (config == NULL) {
823 /* didn't exist or no access? */
824 str = g_strdup_printf("Error reading theme file %s: %s",
825 path, g_strerror(errno));
832 config_parse_data(config, data, "internal");
834 config_parse(config);
836 if (config_last_error(config) != NULL) {
837 str = g_strdup_printf("Ignored errors in theme %s:\n%s",
838 theme->name, config_last_error(config));
843 theme->default_color =
844 config_get_int(config, NULL, "default_color", -1);
845 /* FIXME: remove after 0.7.99 */
846 if (theme->default_color == 0 &&
847 config_get_int(config, NULL, "default_real_color", -1) != -1)
848 theme->default_color = -1;
849 theme_read_replaces(config, theme);
852 /* get the default abstracts from default theme. */
853 CONFIG_REC *default_config;
855 default_config = config_open(NULL, -1);
856 config_parse_data(default_config, default_theme, "internal");
857 theme_read_abstracts(default_config, theme);
858 config_close(default_config);
860 theme_read_abstracts(config, theme);
864 g_hash_table_foreach(default_formats,
865 (GHFunc) theme_read_modules, &rec);
866 config_close(config);
876 static int theme_search_equal(THEME_SEARCH_REC *r1, THEME_SEARCH_REC *r2)
878 return g_strcasecmp(r1->short_name, r2->short_name);
881 static void theme_get_modules(char *module, FORMAT_REC *formats, GSList **list)
883 THEME_SEARCH_REC *rec;
885 rec = g_new(THEME_SEARCH_REC, 1);
887 rec->short_name = strrchr(module, '/');
888 if (rec->short_name != NULL)
889 rec->short_name++; else rec->short_name = module;
890 *list = g_slist_insert_sorted(*list, rec, (GCompareFunc) theme_search_equal);
893 static GSList *get_sorted_modules(void)
898 g_hash_table_foreach(default_formats, (GHFunc) theme_get_modules, &list);
902 static THEME_SEARCH_REC *theme_search(GSList *list, const char *module)
904 THEME_SEARCH_REC *rec;
906 while (list != NULL) {
909 if (g_strcasecmp(rec->short_name, module) == 0)
917 static void theme_show(THEME_SEARCH_REC *rec, const char *key, const char *value, int reset)
919 MODULE_THEME_REC *theme;
921 const char *text, *last_title;
924 formats = g_hash_table_lookup(default_formats, rec->name);
925 theme = g_hash_table_lookup(current_theme->modules, rec->name);
927 last_title = NULL; first = TRUE;
928 for (n = 1; formats[n].def != NULL; n++) {
929 text = theme != NULL && theme->formats[n] != NULL ?
930 theme->formats[n] : formats[n].def;
932 if (formats[n].tag == NULL)
934 else if ((value != NULL && key != NULL && g_strcasecmp(formats[n].tag, key) == 0) ||
935 (value == NULL && (key == NULL || stristr(formats[n].tag, key) != NULL))) {
937 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_FORMAT_TITLE, rec->short_name, formats[0].def);
940 if (last_title != NULL)
941 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_FORMAT_SUBTITLE, last_title);
942 if (reset || value != NULL) {
943 theme = theme_module_create(current_theme, rec->name);
944 g_free_not_null(theme->formats[n]);
945 g_free_not_null(theme->expanded_formats[n]);
947 text = reset ? formats[n].def : value;
948 theme->formats[n] = reset ? NULL : g_strdup(value);
949 theme->expanded_formats[n] = theme_format_expand(current_theme, text);
951 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_FORMAT_ITEM, formats[n].tag, text);
957 /* SYNTAX: FORMAT [-delete | -reset] [<module>] [<key> [<value>]] */
958 static void cmd_format(const char *data)
961 GSList *tmp, *modules;
962 char *module, *key, *value;
966 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
967 "format", &optlist, &module, &key, &value))
970 modules = get_sorted_modules();
973 else if (theme_search(modules, module) == NULL) {
974 /* first argument isn't module.. */
975 cmd_params_free(free_arg);
976 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
977 "format", &optlist, &key, &value))
983 if (*key == '\0') key = NULL;
984 if (g_hash_table_lookup(optlist, "reset"))
986 else if (g_hash_table_lookup(optlist, "delete"))
988 else if (*value == '\0')
991 for (tmp = modules; tmp != NULL; tmp = tmp->next) {
992 THEME_SEARCH_REC *rec = tmp->data;
994 if (module == NULL || g_strcasecmp(rec->short_name, module) == 0)
995 theme_show(rec, key, value, reset);
997 g_slist_foreach(modules, (GFunc) g_free, NULL);
998 g_slist_free(modules);
1000 cmd_params_free(free_arg);
1008 static void module_save(const char *module, MODULE_THEME_REC *rec,
1009 THEME_SAVE_REC *data)
1011 CONFIG_NODE *fnode, *node;
1012 FORMAT_REC *formats;
1015 formats = g_hash_table_lookup(default_formats, rec->name);
1016 if (formats == NULL) return;
1018 fnode = config_node_traverse(data->config, "formats", TRUE);
1020 node = config_node_section(fnode, rec->name, NODE_TYPE_BLOCK);
1021 for (n = 1; formats[n].def != NULL; n++) {
1022 if (rec->formats[n] != NULL) {
1023 config_node_set_str(data->config, node, formats[n].tag,
1025 } else if (data->save_all && formats[n].tag != NULL) {
1026 config_node_set_str(data->config, node, formats[n].tag,
1031 if (node->value == NULL) {
1032 /* not modified, don't keep the empty section */
1033 config_node_remove(data->config, fnode, node);
1034 if (fnode->value == NULL) {
1035 config_node_remove(data->config,
1036 data->config->mainnode, fnode);
1041 static void theme_save(THEME_REC *theme, int save_all)
1044 THEME_SAVE_REC data;
1048 config = config_open(theme->path, -1);
1050 config_parse(config);
1052 if (g_strcasecmp(theme->name, "default") == 0) {
1053 config = config_open(NULL, -1);
1054 config_parse_data(config, default_theme, "internal");
1055 config_change_file_name(config, theme->path, 0660);
1057 config = config_open(theme->path, 0660);
1060 config_parse(config);
1064 data.config = config;
1065 data.save_all = save_all;
1066 g_hash_table_foreach(theme->modules, (GHFunc) module_save, &data);
1068 /* always save the theme to ~/.irssi/ */
1069 path = g_strdup_printf("%s/%s", get_irssi_dir(),
1070 g_basename(theme->path));
1071 ok = config_write(config, path, 0660) == 0;
1073 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
1074 ok ? TXT_THEME_SAVED : TXT_THEME_SAVE_FAILED,
1075 path, config_last_error(config));
1078 config_close(config);
1081 /* save changed formats, -format saves all */
1082 static void cmd_save(const char *data)
1085 GHashTable *optlist;
1090 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
1091 "save", &optlist, &fname))
1094 saveall = g_hash_table_lookup(optlist, "formats") != NULL;
1095 for (tmp = themes; tmp != NULL; tmp = tmp->next) {
1096 THEME_REC *theme = tmp->data;
1098 theme_save(theme, saveall);
1101 cmd_params_free(free_arg);
1104 static void complete_format_list(THEME_SEARCH_REC *rec, const char *key, GList **list)
1106 FORMAT_REC *formats;
1109 formats = g_hash_table_lookup(default_formats, rec->name);
1112 for (n = 1; formats[n].def != NULL; n++) {
1113 const char *item = formats[n].tag;
1115 if (item != NULL && g_strncasecmp(item, key, len) == 0)
1116 *list = g_list_append(*list, g_strdup(item));
1120 static GList *completion_get_formats(const char *module, const char *key)
1122 GSList *modules, *tmp;
1125 g_return_val_if_fail(key != NULL, NULL);
1129 modules = get_sorted_modules();
1130 if (*module == '\0' || theme_search(modules, module) != NULL) {
1131 for (tmp = modules; tmp != NULL; tmp = tmp->next) {
1132 THEME_SEARCH_REC *rec = tmp->data;
1134 if (*module == '\0' || g_strcasecmp(rec->short_name, module) == 0)
1135 complete_format_list(rec, key, &list);
1138 g_slist_foreach(modules, (GFunc) g_free, NULL);
1139 g_slist_free(modules);
1144 static void sig_complete_format(GList **list, WINDOW_REC *window,
1145 const char *word, const char *line, int *want_space)
1150 g_return_if_fail(list != NULL);
1151 g_return_if_fail(word != NULL);
1152 g_return_if_fail(line != NULL);
1159 ptr = strchr(ptr, ' ');
1160 } while (ptr != NULL);
1165 *list = completion_get_formats(line, word);
1166 if (*list != NULL) signal_stop();
1169 static void change_theme(const char *name, int verbose)
1173 rec = theme_load(name);
1175 current_theme = rec;
1177 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
1179 rec->name, rec->path);
1181 } else if (verbose) {
1182 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
1183 TXT_THEME_NOT_FOUND, name);
1187 static void read_settings(void)
1192 theme = settings_get_str("theme");
1193 len = strlen(current_theme->name);
1194 if (strcmp(current_theme->name, theme) != 0 &&
1195 (strncmp(current_theme->name, theme, len) != 0 ||
1196 strcmp(theme+len, ".theme") != 0))
1197 change_theme(theme, TRUE);
1200 static void themes_read(void)
1204 while (themes != NULL)
1205 theme_destroy(themes->data);
1207 /* first there's default theme.. */
1208 current_theme = theme_load("default");
1209 if (current_theme == NULL) {
1210 fname = g_strdup_printf("%s/default.theme", get_irssi_dir());
1211 current_theme = theme_create(fname, "default");
1212 current_theme->default_color = -1;
1213 theme_read(current_theme, NULL, default_theme);
1217 window_themes_update();
1218 change_theme(settings_get_str("theme"), FALSE);
1221 void themes_init(void)
1223 settings_add_str("lookandfeel", "theme", "default");
1225 default_formats = g_hash_table_new((GHashFunc) g_str_hash,
1226 (GCompareFunc) g_str_equal);
1228 init_finished = FALSE;
1234 command_bind("format", NULL, (SIGNAL_FUNC) cmd_format);
1235 command_bind("save", NULL, (SIGNAL_FUNC) cmd_save);
1236 signal_add("complete command format", (SIGNAL_FUNC) sig_complete_format);
1237 signal_add("irssi init finished", (SIGNAL_FUNC) sig_print_errors);
1238 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
1239 signal_add("setup reread", (SIGNAL_FUNC) themes_read);
1241 command_set_options("format", "delete reset");
1242 command_set_options("save", "formats");
1245 void themes_deinit(void)
1247 while (themes != NULL)
1248 theme_destroy(themes->data);
1250 g_hash_table_destroy(default_formats);
1251 default_formats = NULL;
1253 command_unbind("format", (SIGNAL_FUNC) cmd_format);
1254 command_unbind("save", (SIGNAL_FUNC) cmd_save);
1255 signal_remove("complete command format", (SIGNAL_FUNC) sig_complete_format);
1256 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_print_errors);
1257 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
1258 signal_remove("setup reread", (SIGNAL_FUNC) themes_read);