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);
150 if (**format != '\0')
153 /* append the variable name */
154 value = g_strndup(orig, (int) (*format-orig));
155 g_string_append(str, value);
159 /* append next "item", either a character, $variable or %format */
160 static void theme_format_append_next(THEME_REC *theme, GString *str,
162 char default_fg, char default_bg,
163 char *last_fg, char *last_bg,
170 if ((chr == '$' || chr == '%') &&
171 (*format)[1] == '\0') {
172 /* last char, always append */
173 g_string_append_c(str, chr);
179 /* $variable .. we'll always need to skip this, since it
180 may contain characters that are in replace chars. */
181 theme_format_append_variable(str, format);
185 if (**format == '%') {
188 if (**format != '{' && **format != '}') {
190 if (**format == 'n') {
191 /* %n = change to default color */
192 g_string_append(str, "%n");
194 if (default_bg != 'n') {
195 g_string_append_c(str, '%');
196 g_string_append_c(str, default_bg);
198 if (default_fg != 'n') {
199 g_string_append_c(str, '%');
200 g_string_append_c(str, default_fg);
203 *last_fg = default_fg;
204 *last_bg = default_bg;
206 if (IS_FGCOLOR_FORMAT(chr))
208 if (IS_BGCOLOR_FORMAT(chr))
210 g_string_append_c(str, '%');
211 g_string_append_c(str, chr);
217 /* %{ or %} gives us { or } char - keep the % char
218 though to make sure {} isn't treated as abstract */
219 g_string_append_c(str, '%');
223 index = (flags & EXPAND_FLAG_IGNORE_REPLACES) ? -1 :
224 theme->replace_keys[(int) (unsigned char) chr];
226 g_string_append_c(str, chr);
230 value = theme_replace_expand(theme, index,
231 default_fg, default_bg,
232 last_fg, last_bg, chr, flags);
233 g_string_append(str, value);
240 /* returns TRUE if data is empty, or the data is a $variable which is empty */
241 static int data_is_empty(const char **data)
243 /* since we don't know the real argument list, assume there's always
244 an argument in them */
246 "x", "x", "x", "x", "x", "x","x", "x", "x", "x",
254 while (*p == ' ') p++;
267 /* variable - check if it's empty */
269 ret = parse_special((char **) &p,
270 active_win == NULL ? NULL : active_win->active_server,
271 active_win == NULL ? NULL : active_win->active,
272 arglist, &free_ret, NULL, 0);
275 while (*p == ' ') p++;
276 empty = *p == '}' && (ret == NULL || *ret == '\0');
277 if (free_ret) g_free(ret);
288 /* expand a single {abstract ...data... } */
289 static char *theme_format_expand_abstract(THEME_REC *theme,
290 const char **formatp,
291 char default_fg, char default_bg,
294 const char *p, *format;
295 char *abstract, *data, *ret;
300 /* get abstract name first */
302 while (*p != '\0' && *p != ' ' &&
303 *p != '{' && *p != '}') p++;
304 if (*p == '\0' || p == format)
305 return NULL; /* error */
307 len = (int) (p-format);
308 abstract = g_strndup(format, len);
310 /* skip the following space, if there's any more spaces they're
311 treated as arguments */
314 if ((flags & EXPAND_FLAG_IGNORE_EMPTY) && data_is_empty(&p)) {
320 *formatp = format+len;
322 /* get the abstract data */
323 data = g_hash_table_lookup(theme->abstracts, abstract);
326 /* unknown abstract, just display the data */
329 abstract = g_strdup(data);
331 /* we'll need to get the data part. it may contain
332 more abstracts, they are automatically expanded. */
333 data = theme_format_expand_data(theme, formatp, default_fg, default_bg,
337 if (len > 1 && i_isdigit(data[len-1]) && data[len-2] == '$') {
338 /* ends with $<digit> .. this breaks things if next
339 character is digit or '-' */
346 data = g_strdup_printf("%s{%c}", tmp, digit);
350 ret = parse_special_string(abstract, NULL, NULL, data, NULL,
351 PARSE_FLAG_ONLY_ARGS);
356 /* abstract may itself contain abstracts or replaces */
358 ret = theme_format_expand_data(theme, &p, default_fg, default_bg,
359 &default_fg, &default_bg,
360 flags | EXPAND_FLAG_LASTCOLOR_ARG);
365 /* expand the data part in {abstract data} */
366 char *theme_format_expand_data(THEME_REC *theme, const char **format,
367 char default_fg, char default_bg,
368 char *save_last_fg, char *save_last_bg,
372 char *ret, *abstract;
373 char last_fg, last_bg;
376 last_fg = default_fg;
377 last_bg = default_bg;
378 recurse_flags = flags & EXPAND_FLAG_RECURSIVE_MASK;
380 str = g_string_new(NULL);
381 while (**format != '\0') {
382 if ((flags & EXPAND_FLAG_ROOT) == 0 && **format == '}') {
383 /* ignore } if we're expanding original string */
388 if (**format != '{') {
389 if ((flags & EXPAND_FLAG_LASTCOLOR_ARG) &&
390 **format == '$' && (*format)[1] == '0') {
391 /* save the color before $0 ..
392 this is for the %n replacing */
393 if (save_last_fg != NULL) {
394 *save_last_fg = last_fg;
397 if (save_last_bg != NULL) {
398 *save_last_bg = last_bg;
403 theme_format_append_next(theme, str, format,
404 default_fg, default_bg,
411 if (**format == '\0' || **format == '}')
414 /* get a single {...} */
415 abstract = theme_format_expand_abstract(theme, format,
418 if (abstract != NULL) {
419 g_string_append(str, abstract);
424 if ((flags & EXPAND_FLAG_LASTCOLOR_ARG) == 0) {
425 /* save the last color */
426 if (save_last_fg != NULL)
427 *save_last_fg = last_fg;
428 if (save_last_bg != NULL)
429 *save_last_bg = last_bg;
433 g_string_free(str, FALSE);
437 #define IS_OLD_FORMAT(code, last_fg, last_bg) \
438 (((code) == 'n' && (last_fg) == 'n' && (last_bg) == 'n') || \
439 ((code) != 'n' && ((code) == (last_fg) || (code) == (last_bg))))
441 static char *theme_format_compress_colors(THEME_REC *theme, const char *format)
444 char *ret, last_fg, last_bg;
446 str = g_string_new(NULL);
448 last_fg = last_bg = '\0';
449 while (*format != '\0') {
450 if (*format == '$') {
451 /* $variable, skrip it entirely */
452 theme_format_append_variable(str, &format);
453 last_fg = last_bg = '\0';
454 } else if (*format != '%') {
455 /* a normal character */
456 g_string_append_c(str, *format);
461 if (IS_OLD_FORMAT(*format, last_fg, last_bg)) {
462 /* active color set again */
463 } else if (IS_FGCOLOR_FORMAT(*format) &&
465 IS_FGCOLOR_FORMAT(format[2]) &&
466 (*format != 'n' || format[2] == 'n')) {
467 /* two fg colors in a row. bg colors are
468 so rare that we don't bother checking
471 /* some format, add it */
472 g_string_append_c(str, '%');
473 g_string_append_c(str, *format);
475 if (IS_FGCOLOR_FORMAT(*format))
477 if (IS_BGCOLOR_FORMAT(*format))
485 g_string_free(str, FALSE);
489 char *theme_format_expand(THEME_REC *theme, const char *format)
493 g_return_val_if_fail(theme != NULL, NULL);
494 g_return_val_if_fail(format != NULL, NULL);
496 data = theme_format_expand_data(theme, &format, 'n', 'n', NULL, NULL,
498 ret = theme_format_compress_colors(theme, data);
503 static MODULE_THEME_REC *theme_module_create(THEME_REC *theme, const char *module)
505 MODULE_THEME_REC *rec;
508 rec = g_hash_table_lookup(theme->modules, module);
509 if (rec != NULL) return rec;
511 formats = g_hash_table_lookup(default_formats, module);
512 g_return_val_if_fail(formats != NULL, NULL);
514 rec = g_new0(MODULE_THEME_REC, 1);
515 rec->name = g_strdup(module);
517 for (rec->count = 0; formats[rec->count].def != NULL; rec->count++) ;
518 rec->formats = g_new0(char *, rec->count);
519 rec->expanded_formats = g_new0(char *, rec->count);
521 g_hash_table_insert(theme->modules, rec->name, rec);
525 static void theme_read_replaces(CONFIG_REC *config, THEME_REC *theme)
532 /* reset replace keys */
533 for (index = 0; index < 256; index++)
534 theme->replace_keys[index] = -1;
537 node = config_node_traverse(config, "replaces", FALSE);
538 if (node == NULL || node->type != NODE_TYPE_BLOCK) return;
540 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
543 if (node->key != NULL && node->value != NULL) {
544 for (p = node->key; *p != '\0'; p++)
545 theme->replace_keys[(int) (unsigned char) *p] = index;
547 theme->replace_values =
548 g_slist_append(theme->replace_values,
549 g_strdup(node->value));
555 static void theme_read_abstracts(CONFIG_REC *config, THEME_REC *theme)
559 gpointer oldkey, oldvalue;
561 node = config_node_traverse(config, "abstracts", FALSE);
562 if (node == NULL || node->type != NODE_TYPE_BLOCK) return;
564 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
567 if (node->key == NULL || node->value == NULL)
570 if (g_hash_table_lookup_extended(theme->abstracts, node->key,
571 &oldkey, &oldvalue)) {
572 /* new values override old ones */
573 g_hash_table_remove(theme->abstracts, oldkey);
578 g_hash_table_insert(theme->abstracts, g_strdup(node->key),
579 g_strdup(node->value));
583 static void theme_set_format(THEME_REC *theme, MODULE_THEME_REC *rec,
585 const char *key, const char *value)
589 num = format_find_tag(module, key);
591 rec->formats[num] = g_strdup(value);
592 rec->expanded_formats[num] = theme_format_expand(theme, value);
596 static void theme_read_formats(THEME_REC *theme, const char *module,
597 CONFIG_REC *config, MODULE_THEME_REC *rec)
602 node = config_node_traverse(config, "formats", FALSE);
603 if (node == NULL) return;
604 node = config_node_section(node, module, -1);
605 if (node == NULL) return;
607 for (tmp = node->value; tmp != NULL; tmp = tmp->next) {
610 if (node->key != NULL && node->value != NULL) {
611 theme_set_format(theme, rec, module,
612 node->key, node->value);
617 static void theme_init_module(THEME_REC *theme, const char *module,
620 MODULE_THEME_REC *rec;
624 formats = g_hash_table_lookup(default_formats, module);
625 g_return_if_fail(formats != NULL);
627 rec = theme_module_create(theme, module);
630 theme_read_formats(theme, module, config, rec);
632 /* expand the remaining formats */
633 for (n = 0; n < rec->count; n++) {
634 if (rec->expanded_formats[n] == NULL) {
635 rec->expanded_formats[n] =
636 theme_format_expand(theme, formats[n].def);
641 static void sig_print_errors(void)
643 init_finished = TRUE;
645 if (init_errors != NULL) {
646 signal_emit("gui dialog", 2, "error", init_errors);
651 static void theme_read_module(THEME_REC *theme, const char *module)
655 config = config_open(theme->path, -1);
657 config_parse(config);
659 theme_init_module(theme, module, config);
661 if (config != NULL) config_close(config);
664 static void themes_read_module(const char *module)
666 g_slist_foreach(themes, (GFunc) theme_read_module, (void *) module);
669 static void theme_remove_module(THEME_REC *theme, const char *module)
671 MODULE_THEME_REC *rec;
673 rec = g_hash_table_lookup(theme->modules, module);
674 if (rec == NULL) return;
676 g_hash_table_remove(theme->modules, module);
677 theme_module_destroy(module, rec);
680 static void themes_remove_module(const char *module)
682 g_slist_foreach(themes, (GFunc) theme_remove_module, (void *) module);
685 void theme_register_module(const char *module, FORMAT_REC *formats)
687 if (g_hash_table_lookup(default_formats, module) != NULL)
690 g_hash_table_insert(default_formats, g_strdup(module), formats);
691 themes_read_module(module);
694 void theme_unregister_module(const char *module)
698 if (default_formats == NULL)
699 return; /* already uninitialized */
701 if (!g_hash_table_lookup_extended(default_formats, module, &key, &value))
704 g_hash_table_remove(default_formats, key);
707 themes_remove_module(module);
710 static THEME_REC *theme_find(const char *name)
714 for (tmp = themes; tmp != NULL; tmp = tmp->next) {
715 THEME_REC *rec = tmp->data;
717 if (g_strcasecmp(rec->name, name) == 0)
724 static void window_themes_update(void)
728 for (tmp = windows; tmp != NULL; tmp = tmp->next) {
729 WINDOW_REC *rec = tmp->data;
731 if (rec->theme_name != NULL)
732 rec->theme = theme_load(rec->theme_name);
736 THEME_REC *theme_load(const char *setname)
738 THEME_REC *theme, *oldtheme;
740 char *fname, *name, *p;
742 name = g_strdup(setname);
743 p = strrchr(name, '.');
744 if (p != NULL && strcmp(p, ".theme") == 0) {
745 /* remove the trailing .theme */
749 theme = theme_find(name);
752 fname = g_strdup_printf("%s/%s.theme", get_irssi_dir(), name);
753 if (stat(fname, &statbuf) != 0) {
754 /* check global config dir */
756 fname = g_strdup_printf(THEMESDIR"/%s.theme", name);
757 if (stat(fname, &statbuf) != 0) {
758 /* theme not found */
761 return theme; /* use the one in memory if possible */
765 if (theme != NULL && theme->last_modify == statbuf.st_mtime) {
766 /* theme not modified, use the one already in memory */
773 theme = theme_create(fname, name);
774 theme->last_modify = statbuf.st_mtime;
775 if (!theme_read(theme, theme->path, NULL)) {
776 /* error reading .theme file */
777 theme_destroy(theme);
781 if (oldtheme != NULL && theme != NULL) {
782 theme_destroy(oldtheme);
783 window_themes_update();
796 static void theme_read_modules(const char *module, void *value,
799 theme_init_module(rec->theme, module, rec->config);
802 static void read_error(const char *str)
807 printtext(NULL, NULL, MSGLEVEL_CLIENTERROR, "%s", str);
808 else if (init_errors == NULL)
809 init_errors = g_strdup(str);
812 init_errors = g_strconcat(init_errors, "\n", str, NULL);
817 static int theme_read(THEME_REC *theme, const char *path, const char *data)
823 config = config_open(data == NULL ? path : NULL, -1) ;
824 if (config == NULL) {
825 /* didn't exist or no access? */
826 str = g_strdup_printf("Error reading theme file %s: %s",
827 path, g_strerror(errno));
834 config_parse_data(config, data, "internal");
836 config_parse(config);
838 if (config_last_error(config) != NULL) {
839 str = g_strdup_printf("Ignored errors in theme %s:\n%s",
840 theme->name, config_last_error(config));
845 theme->default_color =
846 config_get_int(config, NULL, "default_color", -1);
847 theme->info_eol = config_get_bool(config, NULL, "info_eol", FALSE);
849 /* FIXME: remove after 0.7.99 */
850 if (theme->default_color == 0 &&
851 config_get_int(config, NULL, "default_real_color", -1) != -1)
852 theme->default_color = -1;
853 theme_read_replaces(config, theme);
856 /* get the default abstracts from default theme. */
857 CONFIG_REC *default_config;
859 default_config = config_open(NULL, -1);
860 config_parse_data(default_config, default_theme, "internal");
861 theme_read_abstracts(default_config, theme);
862 config_close(default_config);
864 theme_read_abstracts(config, theme);
868 g_hash_table_foreach(default_formats,
869 (GHFunc) theme_read_modules, &rec);
870 config_close(config);
880 static int theme_search_equal(THEME_SEARCH_REC *r1, THEME_SEARCH_REC *r2)
882 return g_strcasecmp(r1->short_name, r2->short_name);
885 static void theme_get_modules(char *module, FORMAT_REC *formats, GSList **list)
887 THEME_SEARCH_REC *rec;
889 rec = g_new(THEME_SEARCH_REC, 1);
891 rec->short_name = strrchr(module, '/');
892 if (rec->short_name != NULL)
893 rec->short_name++; else rec->short_name = module;
894 *list = g_slist_insert_sorted(*list, rec, (GCompareFunc) theme_search_equal);
897 static GSList *get_sorted_modules(void)
902 g_hash_table_foreach(default_formats, (GHFunc) theme_get_modules, &list);
906 static THEME_SEARCH_REC *theme_search(GSList *list, const char *module)
908 THEME_SEARCH_REC *rec;
910 while (list != NULL) {
913 if (g_strcasecmp(rec->short_name, module) == 0)
921 static void theme_show(THEME_SEARCH_REC *rec, const char *key, const char *value, int reset)
923 MODULE_THEME_REC *theme;
925 const char *text, *last_title;
928 formats = g_hash_table_lookup(default_formats, rec->name);
929 theme = g_hash_table_lookup(current_theme->modules, rec->name);
931 last_title = NULL; first = TRUE;
932 for (n = 1; formats[n].def != NULL; n++) {
933 text = theme != NULL && theme->formats[n] != NULL ?
934 theme->formats[n] : formats[n].def;
936 if (formats[n].tag == NULL)
938 else if ((value != NULL && key != NULL && g_strcasecmp(formats[n].tag, key) == 0) ||
939 (value == NULL && (key == NULL || stristr(formats[n].tag, key) != NULL))) {
941 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_FORMAT_TITLE, rec->short_name, formats[0].def);
944 if (last_title != NULL)
945 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_FORMAT_SUBTITLE, last_title);
946 if (reset || value != NULL) {
947 theme = theme_module_create(current_theme, rec->name);
948 g_free_not_null(theme->formats[n]);
949 g_free_not_null(theme->expanded_formats[n]);
951 text = reset ? formats[n].def : value;
952 theme->formats[n] = reset ? NULL : g_strdup(value);
953 theme->expanded_formats[n] = theme_format_expand(current_theme, text);
955 printformat(NULL, NULL, MSGLEVEL_CLIENTCRAP, TXT_FORMAT_ITEM, formats[n].tag, text);
961 /* SYNTAX: FORMAT [-delete | -reset] [<module>] [<key> [<value>]] */
962 static void cmd_format(const char *data)
965 GSList *tmp, *modules;
966 char *module, *key, *value;
970 if (!cmd_get_params(data, &free_arg, 3 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
971 "format", &optlist, &module, &key, &value))
974 modules = get_sorted_modules();
977 else if (theme_search(modules, module) == NULL) {
978 /* first argument isn't module.. */
979 cmd_params_free(free_arg);
980 if (!cmd_get_params(data, &free_arg, 2 | PARAM_FLAG_GETREST | PARAM_FLAG_OPTIONS,
981 "format", &optlist, &key, &value))
987 if (*key == '\0') key = NULL;
988 if (g_hash_table_lookup(optlist, "reset"))
990 else if (g_hash_table_lookup(optlist, "delete"))
992 else if (*value == '\0')
995 for (tmp = modules; tmp != NULL; tmp = tmp->next) {
996 THEME_SEARCH_REC *rec = tmp->data;
998 if (module == NULL || g_strcasecmp(rec->short_name, module) == 0)
999 theme_show(rec, key, value, reset);
1001 g_slist_foreach(modules, (GFunc) g_free, NULL);
1002 g_slist_free(modules);
1004 cmd_params_free(free_arg);
1012 static void module_save(const char *module, MODULE_THEME_REC *rec,
1013 THEME_SAVE_REC *data)
1015 CONFIG_NODE *fnode, *node;
1016 FORMAT_REC *formats;
1019 formats = g_hash_table_lookup(default_formats, rec->name);
1020 if (formats == NULL) return;
1022 fnode = config_node_traverse(data->config, "formats", TRUE);
1024 node = config_node_section(fnode, rec->name, NODE_TYPE_BLOCK);
1025 for (n = 1; formats[n].def != NULL; n++) {
1026 if (rec->formats[n] != NULL) {
1027 config_node_set_str(data->config, node, formats[n].tag,
1029 } else if (data->save_all && formats[n].tag != NULL) {
1030 config_node_set_str(data->config, node, formats[n].tag,
1035 if (node->value == NULL) {
1036 /* not modified, don't keep the empty section */
1037 config_node_remove(data->config, fnode, node);
1038 if (fnode->value == NULL) {
1039 config_node_remove(data->config,
1040 data->config->mainnode, fnode);
1045 static void theme_save(THEME_REC *theme, int save_all)
1048 THEME_SAVE_REC data;
1052 config = config_open(theme->path, -1);
1054 config_parse(config);
1056 if (g_strcasecmp(theme->name, "default") == 0) {
1057 config = config_open(NULL, -1);
1058 config_parse_data(config, default_theme, "internal");
1059 config_change_file_name(config, theme->path, 0660);
1061 config = config_open(theme->path, 0660);
1064 config_parse(config);
1068 data.config = config;
1069 data.save_all = save_all;
1070 g_hash_table_foreach(theme->modules, (GHFunc) module_save, &data);
1072 /* always save the theme to ~/.irssi/ */
1073 path = g_strdup_printf("%s/%s", get_irssi_dir(),
1074 g_basename(theme->path));
1075 ok = config_write(config, path, 0660) == 0;
1077 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
1078 ok ? TXT_THEME_SAVED : TXT_THEME_SAVE_FAILED,
1079 path, config_last_error(config));
1082 config_close(config);
1085 /* save changed formats, -format saves all */
1086 static void cmd_save(const char *data)
1089 GHashTable *optlist;
1094 if (!cmd_get_params(data, &free_arg, 1 | PARAM_FLAG_OPTIONS,
1095 "save", &optlist, &fname))
1098 saveall = g_hash_table_lookup(optlist, "formats") != NULL;
1099 for (tmp = themes; tmp != NULL; tmp = tmp->next) {
1100 THEME_REC *theme = tmp->data;
1102 theme_save(theme, saveall);
1105 cmd_params_free(free_arg);
1108 static void complete_format_list(THEME_SEARCH_REC *rec, const char *key, GList **list)
1110 FORMAT_REC *formats;
1113 formats = g_hash_table_lookup(default_formats, rec->name);
1116 for (n = 1; formats[n].def != NULL; n++) {
1117 const char *item = formats[n].tag;
1119 if (item != NULL && g_strncasecmp(item, key, len) == 0)
1120 *list = g_list_append(*list, g_strdup(item));
1124 static GList *completion_get_formats(const char *module, const char *key)
1126 GSList *modules, *tmp;
1129 g_return_val_if_fail(key != NULL, NULL);
1133 modules = get_sorted_modules();
1134 if (*module == '\0' || theme_search(modules, module) != NULL) {
1135 for (tmp = modules; tmp != NULL; tmp = tmp->next) {
1136 THEME_SEARCH_REC *rec = tmp->data;
1138 if (*module == '\0' || g_strcasecmp(rec->short_name, module) == 0)
1139 complete_format_list(rec, key, &list);
1142 g_slist_foreach(modules, (GFunc) g_free, NULL);
1143 g_slist_free(modules);
1148 static void sig_complete_format(GList **list, WINDOW_REC *window,
1149 const char *word, const char *line, int *want_space)
1154 g_return_if_fail(list != NULL);
1155 g_return_if_fail(word != NULL);
1156 g_return_if_fail(line != NULL);
1165 ptr = strchr(ptr, ' ');
1166 } while (ptr != NULL);
1171 *list = completion_get_formats(line, word);
1172 if (*list != NULL) signal_stop();
1175 static void change_theme(const char *name, int verbose)
1179 rec = theme_load(name);
1181 current_theme = rec;
1182 signal_emit("theme changed", 1, rec);
1185 printformat(NULL, NULL, MSGLEVEL_CLIENTNOTICE,
1187 rec->name, rec->path);
1189 } else if (verbose) {
1190 printformat(NULL, NULL, MSGLEVEL_CLIENTERROR,
1191 TXT_THEME_NOT_FOUND, name);
1195 static void read_settings(void)
1200 theme = settings_get_str("theme");
1201 len = strlen(current_theme->name);
1202 if (strcmp(current_theme->name, theme) != 0 &&
1203 (strncmp(current_theme->name, theme, len) != 0 ||
1204 strcmp(theme+len, ".theme") != 0))
1205 change_theme(theme, TRUE);
1208 static void themes_read(void)
1212 while (themes != NULL)
1213 theme_destroy(themes->data);
1215 /* first there's default theme.. */
1216 current_theme = theme_load("default");
1217 if (current_theme == NULL) {
1218 fname = g_strdup_printf("%s/default.theme", get_irssi_dir());
1219 current_theme = theme_create(fname, "default");
1220 current_theme->default_color = -1;
1221 theme_read(current_theme, NULL, default_theme);
1225 window_themes_update();
1226 change_theme(settings_get_str("theme"), FALSE);
1229 void themes_init(void)
1231 settings_add_str("lookandfeel", "theme", "default");
1233 default_formats = g_hash_table_new((GHashFunc) g_str_hash,
1234 (GCompareFunc) g_str_equal);
1236 init_finished = FALSE;
1242 command_bind("format", NULL, (SIGNAL_FUNC) cmd_format);
1243 command_bind("save", NULL, (SIGNAL_FUNC) cmd_save);
1244 signal_add("complete command format", (SIGNAL_FUNC) sig_complete_format);
1245 signal_add("irssi init finished", (SIGNAL_FUNC) sig_print_errors);
1246 signal_add("setup changed", (SIGNAL_FUNC) read_settings);
1247 signal_add("setup reread", (SIGNAL_FUNC) themes_read);
1249 command_set_options("format", "delete reset");
1250 command_set_options("save", "formats");
1253 void themes_deinit(void)
1255 while (themes != NULL)
1256 theme_destroy(themes->data);
1258 g_hash_table_destroy(default_formats);
1259 default_formats = NULL;
1261 command_unbind("format", (SIGNAL_FUNC) cmd_format);
1262 command_unbind("save", (SIGNAL_FUNC) cmd_save);
1263 signal_remove("complete command format", (SIGNAL_FUNC) sig_complete_format);
1264 signal_remove("irssi init finished", (SIGNAL_FUNC) sig_print_errors);
1265 signal_remove("setup changed", (SIGNAL_FUNC) read_settings);
1266 signal_remove("setup reread", (SIGNAL_FUNC) themes_read);