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);
54 rec->path = g_strdup(path);
55 rec->name = g_strdup(name);
56 rec->abstracts = g_hash_table_new((GHashFunc) g_str_hash,
57 (GCompareFunc) g_str_equal);
58 rec->modules = g_hash_table_new((GHashFunc) g_istr_hash,
59 (GCompareFunc) g_istr_equal);
60 themes = g_slist_append(themes, rec);
61 signal_emit("theme created", 1, rec);
66 static void theme_abstract_destroy(char *key, char *value)
72 static void theme_module_destroy(const char *key, MODULE_THEME_REC *rec)
76 for (n = 0; n < rec->count; n++) {
77 g_free_not_null(rec->formats[n]);
78 g_free_not_null(rec->expanded_formats[n]);
81 g_free(rec->expanded_formats);
87 static void theme_real_destroy(THEME_REC *rec)
89 g_hash_table_foreach(rec->abstracts, (GHFunc) theme_abstract_destroy, NULL);
90 g_hash_table_destroy(rec->abstracts);
91 g_hash_table_foreach(rec->modules, (GHFunc) theme_module_destroy, NULL);
92 g_hash_table_destroy(rec->modules);
94 g_slist_foreach(rec->replace_values, (GFunc) g_free, NULL);
95 g_slist_free(rec->replace_values);
102 static void theme_unref(THEME_REC *rec)
104 if (--rec->refcount == 0)
105 theme_real_destroy(rec);
108 void theme_destroy(THEME_REC *rec)
110 themes = g_slist_remove(themes, rec);
111 signal_emit("theme destroyed", 1, rec);
116 static char *theme_replace_expand(THEME_REC *theme, int index,
117 char default_fg, char default_bg,
118 char *last_fg, char *last_bg,
122 char *ret, *abstract, data[2];
124 rec = g_slist_nth(theme->replace_values, index);
125 g_return_val_if_fail(rec != NULL, NULL);
127 data[0] = chr; data[1] = '\0';
129 abstract = rec->data;
130 abstract = theme_format_expand_data(theme, (const char **) &abstract,
131 default_fg, default_bg,
132 last_fg, last_bg, flags);
133 ret = parse_special_string(abstract, NULL, NULL, data, NULL,
134 PARSE_FLAG_ONLY_ARGS);
139 static const char *fgcolorformats = "nkrgybmpcwKRGYBMPCW";
140 static const char *bgcolorformats = "n01234567";
142 #define IS_FGCOLOR_FORMAT(c) \
143 ((c) != '\0' && strchr(fgcolorformats, c) != NULL)
144 #define IS_BGCOLOR_FORMAT(c) \
145 ((c) != '\0' && strchr(bgcolorformats, c) != NULL)
147 /* append "variable" part in $variable, ie. not the contents of the variable */
148 static void theme_format_append_variable(GString *str, const char **format)
151 char *value, *args[1] = { NULL };
157 value = parse_special((char **) format, NULL, NULL,
158 args, &free_ret, NULL, PARSE_FLAG_ONLY_ARGS);
159 if (free_ret) g_free(value);
161 if (**format != '\0')
164 /* append the variable name */
165 value = g_strndup(orig, (int) (*format-orig));
166 g_string_append(str, value);
170 /* append next "item", either a character, $variable or %format */
171 static void theme_format_append_next(THEME_REC *theme, GString *str,
173 char default_fg, char default_bg,
174 char *last_fg, char *last_bg,
181 if ((chr == '$' || chr == '%') &&
182 (*format)[1] == '\0') {
183 /* last char, always append */
184 g_string_append_c(str, chr);
190 /* $variable .. we'll always need to skip this, since it
191 may contain characters that are in replace chars. */
192 theme_format_append_variable(str, format);
196 if (**format == '%') {
199 if (**format != '{' && **format != '}') {
201 if (**format == 'n') {
202 /* %n = change to default color */
203 g_string_append(str, "%n");
205 if (default_bg != 'n') {
206 g_string_append_c(str, '%');
207 g_string_append_c(str, default_bg);
209 if (default_fg != 'n') {
210 g_string_append_c(str, '%');
211 g_string_append_c(str, default_fg);
214 *last_fg = default_fg;
215 *last_bg = default_bg;
217 if (IS_FGCOLOR_FORMAT(chr))
219 if (IS_BGCOLOR_FORMAT(chr))
221 g_string_append_c(str, '%');
222 g_string_append_c(str, chr);
228 /* %{ or %} gives us { or } char - keep the % char
229 though to make sure {} isn't treated as abstract */
230 g_string_append_c(str, '%');
234 index = (flags & EXPAND_FLAG_IGNORE_REPLACES) ? -1 :
235 theme->replace_keys[(int) (unsigned char) chr];
237 g_string_append_c(str, chr);
241 value = theme_replace_expand(theme, index,
242 default_fg, default_bg,
243 last_fg, last_bg, chr, flags);
244 g_string_append(str, value);
251 /* returns TRUE if data is empty, or the data is a $variable which is empty */
252 static int data_is_empty(const char **data)
254 /* since we don't know the real argument list, assume there's always
255 an argument in them */
256 static char *arglist[] = {
257 "x", "x", "x", "x", "x", "x","x", "x", "x", "x",
266 while (*p == ' ') p++;
279 /* variable - check if it's empty */
282 server = active_win == NULL ? NULL :
283 active_win->active_server != NULL ?
284 active_win->active_server : active_win->connect_server;
286 ret = parse_special((char **) &p, server,
287 active_win == NULL ? NULL : active_win->active,
288 arglist, &free_ret, NULL, 0);
291 while (*p == ' ') p++;
292 empty = *p == '}' && (ret == NULL || *ret == '\0');
293 if (free_ret) g_free(ret);
304 /* return "data" from {abstract data} string */
305 char *theme_format_expand_get(THEME_REC *theme, const char **format)
309 int braces = 1; /* we start with one brace opened */
311 str = g_string_new(NULL);
312 while ((**format != '\0') && (braces)) {
315 else if (**format == '}')
317 else if ((braces > 1) && (**format == ' ')) {
318 g_string_append(str, "\\x20");
322 theme_format_append_next(theme, str, format,
333 g_string_append_c(str, **format);
338 g_string_free(str, FALSE);
342 /* expand a single {abstract ...data... } */
343 static char *theme_format_expand_abstract(THEME_REC *theme,
344 const char **formatp,
345 char default_fg, char default_bg,
349 const char *p, *format;
350 char *abstract, *data, *ret;
355 /* get abstract name first */
357 while (*p != '\0' && *p != ' ' &&
358 *p != '{' && *p != '}') p++;
359 if (*p == '\0' || p == format)
360 return NULL; /* error */
362 len = (int) (p-format);
363 abstract = g_strndup(format, len);
365 /* skip the following space, if there's any more spaces they're
366 treated as arguments */
369 if ((flags & EXPAND_FLAG_IGNORE_EMPTY) && data_is_empty(&p)) {
375 *formatp = format+len;
377 /* get the abstract data */
378 data = g_hash_table_lookup(theme->abstracts, abstract);
381 /* unknown abstract, just display the data */
384 abstract = g_strdup(data);
386 /* we'll need to get the data part. it may contain
387 more abstracts, they are _NOT_ expanded. */
388 data = theme_format_expand_get(theme, formatp);
391 if (len > 1 && i_isdigit(data[len-1]) && data[len-2] == '$') {
392 /* ends with $<digit> .. this breaks things if next
393 character is digit or '-' */
400 data = g_strdup_printf("%s{%c}", tmp, digit);
404 ret = parse_special_string(abstract, NULL, NULL, data, NULL,
405 PARSE_FLAG_ONLY_ARGS);
408 str = g_string_new(NULL);
414 chr = expand_escape(&p);
415 g_string_append_c(str, chr != -1 ? chr : *p);
417 g_string_append_c(str, *p);
422 g_string_free(str, FALSE);
424 /* abstract may itself contain abstracts or replaces */
426 ret = theme_format_expand_data(theme, &p, default_fg, default_bg,
427 &default_fg, &default_bg,
428 flags | EXPAND_FLAG_LASTCOLOR_ARG);
433 /* expand the data part in {abstract data} */
434 char *theme_format_expand_data(THEME_REC *theme, const char **format,
435 char default_fg, char default_bg,
436 char *save_last_fg, char *save_last_bg,
440 char *ret, *abstract;
441 char last_fg, last_bg;
444 last_fg = default_fg;
445 last_bg = default_bg;
446 recurse_flags = flags & EXPAND_FLAG_RECURSIVE_MASK;
448 str = g_string_new(NULL);
449 while (**format != '\0') {
450 if ((flags & EXPAND_FLAG_ROOT) == 0 && **format == '}') {
451 /* ignore } if we're expanding original string */
456 if (**format != '{') {
457 if ((flags & EXPAND_FLAG_LASTCOLOR_ARG) &&
458 **format == '$' && (*format)[1] == '0') {
459 /* save the color before $0 ..
460 this is for the %n replacing */
461 if (save_last_fg != NULL) {
462 *save_last_fg = last_fg;
465 if (save_last_bg != NULL) {
466 *save_last_bg = last_bg;
471 theme_format_append_next(theme, str, format,
472 default_fg, default_bg,
479 if (**format == '\0' || **format == '}')
482 /* get a single {...} */
483 abstract = theme_format_expand_abstract(theme, format,
486 if (abstract != NULL) {
487 g_string_append(str, abstract);
492 if ((flags & EXPAND_FLAG_LASTCOLOR_ARG) == 0) {
493 /* save the last color */
494 if (save_last_fg != NULL)
495 *save_last_fg = last_fg;
496 if (save_last_bg != NULL)
497 *save_last_bg = last_bg;
501 g_string_free(str, FALSE);
505 #define IS_OLD_FORMAT(code, last_fg, last_bg) \
506 (((code) == 'n' && (last_fg) == 'n' && (last_bg) == 'n') || \
507 ((code) != 'n' && ((code) == (last_fg) || (code) == (last_bg))))
509 static char *theme_format_compress_colors(THEME_REC *theme, const char *format)
512 char *ret, last_fg, last_bg;
514 str = g_string_new(NULL);
516 last_fg = last_bg = '\0';
517 while (*format != '\0') {
518 if (*format == '$') {
519 /* $variable, skrip it entirely */
520 theme_format_append_variable(str, &format);
521 last_fg = last_bg = '\0';
522 } else if (*format != '%') {
523 /* a normal character */
524 g_string_append_c(str, *format);
529 if (IS_OLD_FORMAT(*format, last_fg, last_bg)) {
530 /* active color set again */
531 } else if (IS_FGCOLOR_FORMAT(*format) &&
533 IS_FGCOLOR_FORMAT(format[2]) &&
534 (*format != 'n' || format[2] == 'n')) {
535 /* two fg colors in a row. bg colors are
536 so rare that we don't bother checking
539 /* some format, add it */
540 g_string_append_c(str, '%');
541 g_string_append_c(str, *format);
543 if (IS_FGCOLOR_FORMAT(*format))
545 if (IS_BGCOLOR_FORMAT(*format))
553 g_string_free(str, FALSE);
557 char *theme_format_expand(THEME_REC *theme, const char *format)
561 g_return_val_if_fail(theme != NULL, NULL);
562 g_return_val_if_fail(format != NULL, NULL);
564 data = theme_format_expand_data(theme, &format, 'n', 'n', NULL, NULL,
566 ret = theme_format_compress_colors(theme, data);
571 static MODULE_THEME_REC *theme_module_create(THEME_REC *theme, const char *module)
573 MODULE_THEME_REC *rec;
576 rec = g_hash_table_lookup(theme->modules, module);
577 if (rec != NULL) return rec;
579 formats = g_hash_table_lookup(default_formats, module);
580 g_return_val_if_fail(formats != NULL, NULL);
582 rec = g_new0(MODULE_THEME_REC, 1);
583 rec->name = g_strdup(module);
585 for (rec->count = 0; formats[rec->count].def != NULL; rec->count++) ;
586 rec->formats = g_new0(char *, rec->count);
587 rec->expanded_formats = g_new0(char *, rec->count);
589 g_hash_table_insert(theme->modules, rec->name, rec);
593 static void theme_read_replaces(CONFIG_REC *config, THEME_REC *theme)
600 /* reset replace keys */
601 for (index = 0; index < 256; index++)
602 theme->replace_keys[index] = -1;
605 node = config_node_traverse(config, "replaces", FALSE);
606 if (node == NULL || node->type != NODE_TYPE_BLOCK) return;
608 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
611 if (node->key != NULL && node->value != NULL) {
612 for (p = node->key; *p != '\0'; p++)
613 theme->replace_keys[(int) (unsigned char) *p] = index;
615 theme->replace_values =
616 g_slist_append(theme->replace_values,
617 g_strdup(node->value));
623 static void theme_read_abstracts(CONFIG_REC *config, THEME_REC *theme)
627 gpointer oldkey, oldvalue;
629 node = config_node_traverse(config, "abstracts", FALSE);
630 if (node == NULL || node->type != NODE_TYPE_BLOCK) return;
632 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
635 if (node->key == NULL || node->value == NULL)
638 if (g_hash_table_lookup_extended(theme->abstracts, node->key,
639 &oldkey, &oldvalue)) {
640 /* new values override old ones */
641 g_hash_table_remove(theme->abstracts, oldkey);
646 g_hash_table_insert(theme->abstracts, g_strdup(node->key),
647 g_strdup(node->value));
651 static void theme_set_format(THEME_REC *theme, MODULE_THEME_REC *rec,
653 const char *key, const char *value)
657 num = format_find_tag(module, key);
659 rec->formats[num] = g_strdup(value);
660 rec->expanded_formats[num] = theme_format_expand(theme, value);
664 static void theme_read_formats(THEME_REC *theme, const char *module,
665 CONFIG_REC *config, MODULE_THEME_REC *rec)
670 node = config_node_traverse(config, "formats", FALSE);
671 if (node == NULL) return;
672 node = config_node_section(node, module, -1);
673 if (node == NULL) return;
675 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
678 if (node->key != NULL && node->value != NULL) {
679 theme_set_format(theme, rec, module,
680 node->key, node->value);
685 static void theme_init_module(THEME_REC *theme, const char *module,
688 MODULE_THEME_REC *rec;
692 formats = g_hash_table_lookup(default_formats, module);
693 g_return_if_fail(formats != NULL);
695 rec = theme_module_create(theme, module);
698 theme_read_formats(theme, module, config, rec);
700 /* expand the remaining formats */
701 for (n = 0; n < rec->count; n++) {
702 if (rec->expanded_formats[n] == NULL) {
703 rec->expanded_formats[n] =
704 theme_format_expand(theme, formats[n].def);
709 static void sig_print_errors(void)
711 init_finished = TRUE;
713 if (init_errors != NULL) {
714 signal_emit("gui dialog", 2, "error", init_errors);
719 static void theme_read_module(THEME_REC *theme, const char *module)
723 config = config_open(theme->path, -1);
725 config_parse(config);
727 theme_init_module(theme, module, config);
729 if (config != NULL) config_close(config);
732 static void themes_read_module(const char *module)
734 g_slist_foreach(themes, (GFunc) theme_read_module, (void *) module);
737 static void theme_remove_module(THEME_REC *theme, const char *module)
739 MODULE_THEME_REC *rec;
741 rec = g_hash_table_lookup(theme->modules, module);
742 if (rec == NULL) return;
744 g_hash_table_remove(theme->modules, module);
745 theme_module_destroy(module, rec);
748 static void themes_remove_module(const char *module)
750 g_slist_foreach(themes, (GFunc) theme_remove_module, (void *) module);
753 void theme_register_module(const char *module, FORMAT_REC *formats)
755 if (g_hash_table_lookup(default_formats, module) != NULL)
758 g_hash_table_insert(default_formats, g_strdup(module), formats);
759 themes_read_module(module);
762 void theme_unregister_module(const char *module)
766 if (default_formats == NULL)
767 return; /* already uninitialized */
769 if (!g_hash_table_lookup_extended(default_formats, module, &key, &value))
772 g_hash_table_remove(default_formats, key);
775 themes_remove_module(module);
778 static THEME_REC *theme_find(const char *name)
782 for (tmp = themes; tmp != NULL; tmp = tmp->next) {
783 THEME_REC *rec = tmp->data;
785 if (g_strcasecmp(rec->name, name) == 0)
792 static void window_themes_update(void)
796 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
797 WINDOW_REC *rec = tmp->data;
799 if (rec->theme_name != NULL)
800 rec->theme = theme_load(rec->theme_name);
804 THEME_REC *theme_load(const char *setname)
806 THEME_REC *theme, *oldtheme;
808 char *fname, *name, *p;
810 name = g_strdup(setname);
811 p = strrchr(name, '.');
812 if (p != NULL && strcmp(p, ".theme") == 0) {
813 /* remove the trailing .theme */
817 theme = theme_find(name);
820 fname = g_strdup_printf("%s/%s.theme", get_irssi_dir(), name);
821 if (stat(fname, &statbuf) != 0) {
822 /* check global config dir */
824 fname = g_strdup_printf(THEMESDIR"/%s.theme", name);
825 if (stat(fname, &statbuf) != 0) {
826 /* theme not found */
829 return theme; /* use the one in memory if possible */
833 if (theme != NULL && theme->last_modify == statbuf.st_mtime) {
834 /* theme not modified, use the one already in memory */
841 theme = theme_create(fname, name);
842 theme->last_modify = statbuf.st_mtime;
843 if (!theme_read(theme, theme->path, NULL)) {
844 /* error reading .theme file */
845 theme_destroy(theme);
849 if (oldtheme != NULL && theme != NULL) {
850 theme_destroy(oldtheme);
851 window_themes_update();
864 static void theme_read_modules(const char *module, void *value,
867 theme_init_module(rec->theme, module, rec->config);
870 static void read_error(const char *str)
875 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", str);
876 else if (init_errors == NULL)
877 init_errors = g_strdup(str);
880 init_errors = g_strconcat(init_errors, "\n", str, NULL);
885 static int theme_read(THEME_REC *theme, const char *path, const char *data)
891 config = config_open(data == NULL ? path : NULL, -1) ;
892 if (config == NULL) {
893 /* didn't exist or no access? */
894 str = g_strdup_printf("Error reading theme file %s: %s",
895 path, g_strerror(errno));
902 config_parse_data(config, data, "internal");
904 config_parse(config);
906 if (config_last_error(config) != NULL) {
907 str = g_strdup_printf("Ignored errors in theme %s:\n%s",
908 theme->name, config_last_error(config));
913 theme->default_color =
914 config_get_int(config, NULL, "default_color", -1);
915 theme->info_eol = config_get_bool(config, NULL, "info_eol", FALSE);
917 /* FIXME: remove after 0.7.99 */
918 if (theme->default_color == 0 &&
919 config_get_int(config, NULL, "default_real_color", -1) != -1)
920 theme->default_color = -1;
921 theme_read_replaces(config, theme);
924 /* get the default abstracts from default theme. */
925 CONFIG_REC *default_config;
927 default_config = config_open(NULL, -1);
928 config_parse_data(default_config, default_theme, "internal");
929 theme_read_abstracts(default_config, theme);
930 config_close(default_config);
932 theme_read_abstracts(config, theme);
936 g_hash_table_foreach(default_formats,
937 (GHFunc) theme_read_modules, &rec);
938 config_close(config);
948 static int theme_search_equal(THEME_SEARCH_REC *r1, THEME_SEARCH_REC *r2)
950 return g_strcasecmp(r1->short_name, r2->short_name);
953 static void theme_get_modules(char *module, FORMAT_REC *formats, GSList **list)
955 THEME_SEARCH_REC *rec;
957 rec = g_new(THEME_SEARCH_REC, 1);
959 rec->short_name = strrchr(module, '/');
960 if (rec->short_name != NULL)
961 rec->short_name++; else rec->short_name = module;
962 *list = g_slist_insert_sorted(*list, rec, (GCompareFunc) theme_search_equal);
965 static GSList *get_sorted_modules(void)
970 g_hash_table_foreach(default_formats, (GHFunc) theme_get_modules, &list);
974 static THEME_SEARCH_REC *theme_search(GSList *list, const char *module)
976 THEME_SEARCH_REC *rec;
978 while (list != NULL) {
981 if (g_strcasecmp(rec->short_name, module) == 0)
989 static void theme_show(THEME_SEARCH_REC *rec, const char *key, const char *value, int reset)
991 MODULE_THEME_REC *theme;
993 const char *text, *last_title;
996 formats = g_hash_table_lookup(default_formats, rec->name);
997 theme = g_hash_table_lookup(current_theme->modules, rec->name);
999 last_title = NULL; first = TRUE;
1000 for (n = 1; formats[n].def != NULL; n++) {
1001 text = theme != NULL && theme->formats[n] != NULL ?
1002 theme->formats[n] : formats[n].def;
1004 if (formats[n].tag == NULL)
1006 else if ((value != NULL && key != NULL && g_strcasecmp(formats[n].tag, key) == 0) ||
1007 (value == NULL && (key == NULL || stristr(formats[n].tag, key) != NULL))) {
1009 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_FORMAT_TITLE, rec->short_name, formats[0].def);
1012 if (last_title != NULL)
1013 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_FORMAT_SUBTITLE, last_title);
1014 if (reset || value != NULL) {
1015 theme = theme_module_create(current_theme, rec->name);
1016 g_free_not_null(theme->formats[n]);
1017 g_free_not_null(theme->expanded_formats[n]);
1019 text = reset ? formats[n].def : value;
1020 theme->formats[n] = reset ? NULL : g_strdup(value);
1021 theme->expanded_formats[n] = theme_format_expand(current_theme, text);
1023 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_FORMAT_ITEM, formats[n].tag, text);
1029 /* SYNTAX: FORMAT [-delete | -reset] [<module>] [<key> [<value>]] */
1030 static void cmd_format(const char *data)
1032 GHashTable *optlist;
1033 GSList *tmp, *modules;
1034 char *module, *key, *value;
1038 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
1039 "format", &optlist, &module, &key, &value))
1042 modules = get_sorted_modules();
1043 if (*module == '\0')
1045 else if (theme_search(modules, module) == NULL) {
1046 /* first argument isn't module.. */
1047 cmd_params_free(free_arg);
1048 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
1049 "format", &optlist, &key, &value))
1055 if (*key == '\0') key = NULL;
1056 if (g_hash_table_lookup(optlist, "reset"))
1058 else if (g_hash_table_lookup(optlist, "delete"))
1060 else if (*value == '\0')
1063 for (tmp = modules; tmp != NULL; tmp = tmp->next) {
1064 THEME_SEARCH_REC *rec = tmp->data;
1066 if (module == NULL || g_strcasecmp(rec->short_name, module) == 0)
1067 theme_show(rec, key, value, reset);
1069 g_slist_foreach(modules, (GFunc) g_free, NULL);
1070 g_slist_free(modules);
1072 cmd_params_free(free_arg);
1080 static void module_save(const char *module, MODULE_THEME_REC *rec,
1081 THEME_SAVE_REC *data)
1083 CONFIG_NODE *fnode, *node;
1084 FORMAT_REC *formats;
1087 formats = g_hash_table_lookup(default_formats, rec->name);
1088 if (formats == NULL) return;
1090 fnode = config_node_traverse(data->config, "formats", TRUE);
1092 node = config_node_section(fnode, rec->name, NODE_TYPE_BLOCK);
1093 for (n = 1; formats[n].def != NULL; n++) {
1094 if (rec->formats[n] != NULL) {
1095 config_node_set_str(data->config, node, formats[n].tag,
1097 } else if (data->save_all && formats[n].tag != NULL) {
1098 config_node_set_str(data->config, node, formats[n].tag,
1103 if (node->value == NULL) {
1104 /* not modified, don't keep the empty section */
1105 config_node_remove(data->config, fnode, node);
1106 if (fnode->value == NULL) {
1107 config_node_remove(data->config,
1108 data->config->mainnode, fnode);
1113 static void theme_save(THEME_REC *theme, int save_all)
1116 THEME_SAVE_REC data;
1120 config = config_open(theme->path, -1);
1122 config_parse(config);
1124 if (g_strcasecmp(theme->name, "default") == 0) {
1125 config = config_open(NULL, -1);
1126 config_parse_data(config, default_theme, "internal");
1127 config_change_file_name(config, theme->path, 0660);
1129 config = config_open(theme->path, 0660);
1132 config_parse(config);
1136 data.config = config;
1137 data.save_all = save_all;
1138 g_hash_table_foreach(theme->modules, (GHFunc) module_save, &data);
1140 /* always save the theme to ~/.irssi/ */
1141 path = g_strdup_printf("%s/%s", get_irssi_dir(),
1142 g_basename(theme->path));
1143 ok = config_write(config, path, 0660) == 0;
1145 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
1146 ok ? TXT_THEME_SAVED : TXT_THEME_SAVE_FAILED,
1147 path, config_last_error(config));
1150 config_close(config);
1153 /* save changed formats, -format saves all */
1154 static void cmd_save(const char *data)
1157 GHashTable *optlist;
1162 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
1163 "save", &optlist, &fname))
1166 saveall = g_hash_table_lookup(optlist, "formats") != NULL;
1167 for (tmp = themes; tmp != NULL; tmp = tmp->next) {
1168 THEME_REC *theme = tmp->data;
1170 theme_save(theme, saveall);
1173 cmd_params_free(free_arg);
1176 static void complete_format_list(THEME_SEARCH_REC *rec, const char *key, GList **list)
1178 FORMAT_REC *formats;
1181 formats = g_hash_table_lookup(default_formats, rec->name);
1184 for (n = 1; formats[n].def != NULL; n++) {
1185 const char *item = formats[n].tag;
1187 if (item != NULL && g_strncasecmp(item, key, len) == 0)
1188 *list = g_list_append(*list, g_strdup(item));
1192 static GList *completion_get_formats(const char *module, const char *key)
1194 GSList *modules, *tmp;
1197 g_return_val_if_fail(key != NULL, NULL);
1201 modules = get_sorted_modules();
1202 if (*module == '\0' || theme_search(modules, module) != NULL) {
1203 for (tmp = modules; tmp != NULL; tmp = tmp->next) {
1204 THEME_SEARCH_REC *rec = tmp->data;
1206 if (*module == '\0' || g_strcasecmp(rec->short_name, module) == 0)
1207 complete_format_list(rec, key, &list);
1210 g_slist_foreach(modules, (GFunc) g_free, NULL);
1211 g_slist_free(modules);
1216 static void sig_complete_format(GList **list, WINDOW_REC *window,
1217 const char *word, const char *line, int *want_space)
1222 g_return_if_fail(list != NULL);
1223 g_return_if_fail(word != NULL);
1224 g_return_if_fail(line != NULL);
1233 ptr = strchr(ptr, ' ');
1234 } while (ptr != NULL);
1239 *list = completion_get_formats(line, word);
1240 if (*list != NULL) signal_stop();
1243 static void change_theme(const char *name, int verbose)
1247 rec = theme_load(name);
1249 current_theme = rec;
1250 signal_emit("theme changed", 1, rec);
1253 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
1255 rec->name, rec->path);
1257 } else if (verbose) {
1258 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
1259 TXT_THEME_NOT_FOUND, name);
1263 static void read_settings(void)
1268 theme = settings_get_str("theme");
1269 len = strlen(current_theme->name);
1270 if (strcmp(current_theme->name, theme) != 0 &&
1271 (strncmp(current_theme->name, theme, len) != 0 ||
1272 strcmp(theme+len, ".theme") != 0))
1273 change_theme(theme, TRUE);
1276 static void themes_read(void)
1281 /* increase every theme's refcount, and destroy them. this way if
1282 we want to use the theme before it's reloaded we don't crash. */
1284 while (themes != NULL) {
1285 THEME_REC *theme = themes->data;
1287 refs = g_slist_prepend(refs, theme);
1290 theme_destroy(theme);
1293 /* first there's default theme.. */
1294 current_theme = theme_load("default");
1295 if (current_theme == NULL) {
1296 fname = g_strdup_printf("%s/default.theme", get_irssi_dir());
1297 current_theme = theme_create(fname, "default");
1298 current_theme->default_color = -1;
1299 theme_read(current_theme, NULL, default_theme);
1303 window_themes_update();
1304 change_theme(settings_get_str("theme"), FALSE);
1306 while (refs != NULL) {
1307 theme_unref(refs->data);
1308 refs = g_slist_remove(refs, refs->data);
1312 void themes_init(void)
1314 settings_add_str("lookandfeel", "theme", "default");
1316 default_formats = g_hash_table_new((GHashFunc) g_str_hash,
1317 (GCompareFunc) g_str_equal);
1319 init_finished = FALSE;
1325 command_bind("format", NULL, (SIGNAL_FUNC) cmd_format);
1326 command_bind("save", NULL, (SIGNAL_FUNC) cmd_save);
1327 signal_add("complete command format", (SIGNAL_FUNC) sig_complete_format);
1328 signal_add("irssi init finished", (SIGNAL_FUNC) sig_print_errors);
1329 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
1330 signal_add("setup reread", (SIGNAL_FUNC) themes_read);
1332 command_set_options("format", "delete reset");
1333 command_set_options("save", "formats");
1336 void themes_deinit(void)
1338 while (themes != NULL)
1339 theme_destroy(themes->data);
1341 g_hash_table_destroy(default_formats);
1342 default_formats = NULL;
1344 command_unbind("format", (SIGNAL_FUNC) cmd_format);
1345 command_unbind("save", (SIGNAL_FUNC) cmd_save);
1346 signal_remove("complete command format", (SIGNAL_FUNC) sig_complete_format);
1347 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_print_errors);
1348 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
1349 signal_remove("setup reread", (SIGNAL_FUNC) themes_read);