Udated jscript to be able to parse SAPUI5 controller and view files.
[geany-mirror.git] / src / keyfile.c
blobe8573800e3e40f2194bfdbd4cfe909ea1b9b591b
1 /*
2 * keyfile.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2012 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2012 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License along
18 * with this program; if not, write to the Free Software Foundation, Inc.,
19 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 * geany.conf preferences file loading and saving.
27 * Session file format:
28 * filename_xx=pos;filetype UID;read only;Eencoding;use_tabs;auto_indent;line_wrapping;filename
31 #ifdef HAVE_CONFIG_H
32 # include "config.h"
33 #endif
35 #include "keyfile.h"
37 #include "app.h"
38 #include "build.h"
39 #include "document.h"
40 #include "encodings.h"
41 #include "filetypes.h"
42 #include "geanyobject.h"
43 #include "main.h"
44 #include "msgwindow.h"
45 #include "prefs.h"
46 #include "printing.h"
47 #include "project.h"
48 #include "sciwrappers.h"
49 #include "stash.h"
50 #include "support.h"
51 #include "templates.h"
52 #include "toolbar.h"
53 #include "ui_utils.h"
54 #include "utils.h"
55 #include "vte.h"
57 #include <stdlib.h>
58 #include <string.h>
59 #include <ctype.h>
61 #ifdef HAVE_VTE
62 #include <pwd.h>
63 #include <sys/types.h>
64 #include <unistd.h>
65 #endif
68 /* some default settings which are used at the very first start of Geany to fill
69 * the configuration file */
70 #define GEANY_MAX_SYMBOLLIST_HEIGHT 10
71 #define GEANY_MIN_SYMBOLLIST_CHARS 4
72 #define GEANY_MSGWIN_HEIGHT 208
73 #define GEANY_DISK_CHECK_TIMEOUT 30
74 #define GEANY_DEFAULT_TOOLS_MAKE "make"
75 #ifdef G_OS_WIN32
76 #define GEANY_DEFAULT_TOOLS_TERMINAL "cmd.exe /Q /C %c"
77 #else
78 #define GEANY_DEFAULT_TOOLS_TERMINAL "xterm -e \"/bin/sh %c\""
79 #endif
80 #define GEANY_DEFAULT_TOOLS_BROWSER "firefox"
81 #define GEANY_DEFAULT_TOOLS_PRINTCMD "lpr"
82 #define GEANY_DEFAULT_TOOLS_GREP "grep"
83 #define GEANY_DEFAULT_MRU_LENGTH 10
84 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Sans 9"
85 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Sans 9"
86 #define GEANY_DEFAULT_FONT_EDITOR "Monospace 10"
87 #define GEANY_TOGGLE_MARK "~ "
88 #define GEANY_MAX_AUTOCOMPLETE_WORDS 30
89 #define GEANY_MAX_SYMBOLS_UPDATE_FREQ 250
90 #define GEANY_DEFAULT_FILETYPE_REGEX "-\\*-\\s*([^\\s]+)\\s*-\\*-"
93 static gchar *scribble_text = NULL;
94 static gint scribble_pos = -1;
95 static GPtrArray *session_files = NULL;
96 static gint session_notebook_page;
97 static gint hpan_position;
98 static gint vpan_position;
99 static const gchar atomic_file_saving_key[] = "use_atomic_file_saving";
101 static GPtrArray *keyfile_groups = NULL;
103 GPtrArray *pref_groups = NULL;
105 static struct
107 gint number_ft_menu_items;
108 gint number_non_ft_menu_items;
109 gint number_exec_menu_items;
111 build_menu_prefs;
114 /* The group will be free'd on quitting.
115 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
116 void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
118 g_ptr_array_add(keyfile_groups, group);
120 if (for_prefs_dialog)
121 g_ptr_array_add(pref_groups, group);
125 /* The group will be free'd on quitting. */
126 void configuration_add_various_pref_group(struct StashGroup *group)
128 configuration_add_pref_group(group, TRUE);
129 stash_group_set_various(group, TRUE);
133 static void init_pref_groups(void)
135 StashGroup *group;
137 group = stash_group_new(PACKAGE);
138 configuration_add_pref_group(group, TRUE);
139 stash_group_add_entry(group, &prefs.default_open_path,
140 "default_open_path", "", "startup_path_entry");
142 stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
143 "cmdline_new_files", TRUE, "check_cmdline_new_files");
145 stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
146 "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
147 stash_group_add_toggle_button(group, &file_prefs.tab_close_switch_to_mru,
148 "tab_close_switch_to_mru", FALSE, "check_tab_close_switch_to_mru");
149 stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
150 stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
151 "sidebar_pos", GTK_POS_LEFT,
152 "radio_sidebar_left", GTK_POS_LEFT,
153 "radio_sidebar_right", GTK_POS_RIGHT,
154 NULL);
155 stash_group_add_radio_buttons(group, &interface_prefs.msgwin_orientation,
156 "msgwin_orientation", GTK_ORIENTATION_VERTICAL,
157 "radio_msgwin_vertical", GTK_ORIENTATION_VERTICAL,
158 "radio_msgwin_horizontal", GTK_ORIENTATION_HORIZONTAL,
159 NULL);
161 /* editor display */
162 stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
163 "highlighting_invert_all", FALSE, "check_highlighting_invert");
165 stash_group_add_toggle_button(group, &search_prefs.use_current_word,
166 "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
168 /* editor */
169 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
170 "check_detect_indent", FALSE, "check_detect_indent_type");
171 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_width,
172 "detect_indent_width", FALSE, "check_detect_indent_width");
173 stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
174 "use_tab_to_indent", TRUE, "check_tab_key_indents");
175 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
176 "pref_editor_tab_width", 4, "spin_indent_width");
177 stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
178 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
179 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
180 "indent_type", GEANY_INDENT_TYPE_TABS,
181 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
182 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
183 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
184 NULL);
185 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
186 "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
187 "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
188 "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
189 "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
190 NULL);
191 stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
192 "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
193 stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
194 "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
195 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
196 "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
197 "spin_autocompletion_max_entries");
198 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_update_freq,
199 "autocompletion_update_freq", GEANY_MAX_SYMBOLS_UPDATE_FREQ, "spin_symbol_update_freq");
200 stash_group_add_string(group, &editor_prefs.color_scheme,
201 "color_scheme", NULL);
203 /* files */
204 stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
205 "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
206 stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
207 "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
209 /* various geany prefs */
210 group = stash_group_new(PACKAGE);
211 configuration_add_various_pref_group(group);
213 stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
214 "show_editor_scrollbars", TRUE);
215 stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
216 "brace_match_ltgt", FALSE);
217 stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
218 "use_gtk_word_boundaries", TRUE);
219 stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
220 "complete_snippets_whilst_editing", FALSE);
221 stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
222 atomic_file_saving_key, FALSE);
223 stash_group_add_boolean(group, &file_prefs.gio_unsafe_save_backup,
224 "gio_unsafe_save_backup", FALSE);
225 stash_group_add_boolean(group, &file_prefs.use_gio_unsafe_file_saving,
226 "use_gio_unsafe_file_saving", TRUE);
227 /* for backwards-compatibility */
228 stash_group_add_integer(group, &editor_prefs.indentation->hard_tab_width,
229 "indent_hard_tab_width", 8);
230 stash_group_add_integer(group, (gint*)&search_prefs.find_selection_type,
231 "find_selection_type", GEANY_FIND_SEL_CURRENT_WORD);
232 stash_group_add_string(group, &file_prefs.extract_filetype_regex,
233 "extract_filetype_regex", GEANY_DEFAULT_FILETYPE_REGEX);
234 stash_group_add_boolean(group, &search_prefs.replace_and_find_by_default,
235 "replace_and_find_by_default", TRUE);
237 /* Note: Interface-related various prefs are in ui_init_prefs() */
239 /* various build-menu prefs */
240 group = stash_group_new("build-menu");
241 configuration_add_various_pref_group(group);
243 stash_group_add_integer(group, &build_menu_prefs.number_ft_menu_items,
244 "number_ft_menu_items", 0);
245 stash_group_add_integer(group, &build_menu_prefs.number_non_ft_menu_items,
246 "number_non_ft_menu_items", 0);
247 stash_group_add_integer(group, &build_menu_prefs.number_exec_menu_items,
248 "number_exec_menu_items", 0);
252 typedef enum SettingAction
254 SETTING_READ,
255 SETTING_WRITE
257 SettingAction;
259 static void settings_action(GKeyFile *config, SettingAction action)
261 guint i;
262 StashGroup *group;
264 foreach_ptr_array(group, i, keyfile_groups)
266 switch (action)
268 case SETTING_READ:
269 stash_group_load_from_key_file(group, config); break;
270 case SETTING_WRITE:
271 stash_group_save_to_key_file(group, config); break;
277 static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
279 gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
280 guint i;
282 for (i = 0; i < file_prefs.mru_length; i++)
284 if (! g_queue_is_empty(queue))
286 /* copy the values, this is necessary when this function is called from the
287 * preferences dialog or when quitting is canceled to keep the queue intact */
288 recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
290 else
292 recent_files[i] = NULL;
293 break;
296 /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
297 recent_files[file_prefs.mru_length] = NULL;
298 g_key_file_set_string_list(config, "files", key,
299 (const gchar**)recent_files, file_prefs.mru_length);
300 g_strfreev(recent_files);
304 static gchar *get_session_file_string(GeanyDocument *doc)
306 gchar *fname;
307 gchar *locale_filename;
308 gchar *escaped_filename;
309 GeanyFiletype *ft = doc->file_type;
311 if (ft == NULL) /* can happen when saving a new file when quitting */
312 ft = filetypes[GEANY_FILETYPES_NONE];
314 locale_filename = utils_get_locale_from_utf8(doc->file_name);
315 escaped_filename = g_uri_escape_string(locale_filename, NULL, TRUE);
317 fname = g_strdup_printf("%d;%s;%d;E%s;%d;%d;%d;%s;%d;%d",
318 sci_get_current_position(doc->editor->sci),
319 ft->name,
320 doc->readonly,
321 doc->encoding,
322 doc->editor->indent_type,
323 doc->editor->auto_indent,
324 doc->editor->line_wrapping,
325 escaped_filename,
326 doc->editor->line_breaking,
327 doc->editor->indent_width);
328 g_free(escaped_filename);
329 g_free(locale_filename);
330 return fname;
334 static void remove_session_files(GKeyFile *config)
336 gchar **ptr;
337 gchar **keys = g_key_file_get_keys(config, "files", NULL, NULL);
339 foreach_strv(ptr, keys)
341 if (g_str_has_prefix(*ptr, "FILE_NAME_"))
342 g_key_file_remove_key(config, "files", *ptr, NULL);
344 g_strfreev(keys);
348 void configuration_save_session_files(GKeyFile *config)
350 gint npage;
351 gchar entry[16];
352 guint i = 0, j = 0, max;
353 GeanyDocument *doc;
355 npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
356 g_key_file_set_integer(config, "files", "current_page", npage);
358 // clear existing entries first as they might not all be overwritten
359 remove_session_files(config);
361 /* store the filenames in the notebook tab order to reopen them the next time */
362 max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
363 for (i = 0; i < max; i++)
365 doc = document_get_from_page(i);
366 if (doc != NULL && doc->real_path != NULL)
368 gchar *fname;
370 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
371 fname = get_session_file_string(doc);
372 g_key_file_set_string(config, "files", entry, fname);
373 g_free(fname);
374 j++;
378 #ifdef HAVE_VTE
379 if (vte_info.have_vte)
381 vte_get_working_directory(); /* refresh vte_info.dir */
382 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
384 #endif
388 static void save_dialog_prefs(GKeyFile *config)
390 /* new settings should be added in init_pref_groups() */
391 settings_action(config, SETTING_WRITE);
393 /* Some of the key names are not consistent, but this is for backwards compatibility */
395 /* general */
396 g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
397 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_session", project_prefs.project_session);
398 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
399 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
400 g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
401 g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
402 g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
403 g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
404 g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
406 /* interface */
407 g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
408 g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
409 g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
410 g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
411 g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
412 g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
413 g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
414 g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
415 g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
416 g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
417 g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
418 g_key_file_set_boolean(config, PACKAGE, "use_native_windows_dialogs", interface_prefs.use_native_windows_dialogs);
420 /* display */
421 g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
422 g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
423 g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
424 g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
425 g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
426 g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_enabled);
427 g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_type);
428 g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_column);
429 g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
431 /* editor */
432 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
433 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
434 g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
435 g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
436 g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
437 g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
438 g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
439 g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
440 g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
441 g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
442 g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
443 g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
444 g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
445 g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
446 g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
447 g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
448 g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
450 /* files */
451 g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
452 if (file_prefs.default_open_encoding == -1)
453 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
454 else
455 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
456 g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
457 g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
458 g_key_file_set_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", file_prefs.ensure_convert_new_lines);
459 g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
460 g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
462 /* toolbar */
463 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
464 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
465 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
466 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
467 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
468 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
470 /* templates */
471 g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
472 g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
473 g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
474 g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
475 g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
476 g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
477 g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
478 g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
480 /* tools settings */
481 g_key_file_set_string(config, "tools", "terminal_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
482 g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
483 g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
484 g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
486 /* build menu */
487 build_save_menu(config, NULL, GEANY_BCS_PREF);
489 /* printing */
490 g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
491 g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
492 g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
493 g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
494 g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
495 g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
496 g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
498 /* VTE */
499 #ifdef HAVE_VTE
500 g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
501 if (vte_info.have_vte)
503 gchar *tmp_string;
505 if (!g_key_file_has_key(config, "VTE", "emulation", NULL)) /* hidden */
506 g_key_file_set_string(config, "VTE", "emulation", vc->emulation);
507 if (!g_key_file_has_key(config, "VTE", "send_selection_unsafe", NULL)) /* hidden */
508 g_key_file_set_boolean(config, "VTE", "send_selection_unsafe",
509 vc->send_selection_unsafe);
510 if (!g_key_file_has_key(config, "VTE", "send_cmd_prefix", NULL)) /* hidden */
511 g_key_file_set_string(config, "VTE", "send_cmd_prefix", vc->send_cmd_prefix);
512 g_key_file_set_string(config, "VTE", "font", vc->font);
513 g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
514 g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
515 g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
516 g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
517 g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
518 g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
519 g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
520 g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
521 g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
522 g_key_file_set_string(config, "VTE", "font", vc->font);
523 g_key_file_set_string(config, "VTE", "image", vc->image);
524 g_key_file_set_string(config, "VTE", "shell", vc->shell);
525 tmp_string = utils_get_hex_from_color(&vc->colour_fore);
526 g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
527 g_free(tmp_string);
528 tmp_string = utils_get_hex_from_color(&vc->colour_back);
529 g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
530 g_free(tmp_string);
532 #endif
536 static void save_ui_prefs(GKeyFile *config)
538 g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
539 g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
540 g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
541 g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
543 /* get the text from the scribble textview */
545 GtkTextBuffer *buffer;
546 GtkTextIter start, end, iter;
547 GtkTextMark *mark;
549 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
550 gtk_text_buffer_get_bounds(buffer, &start, &end);
551 scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
552 g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
553 g_free(scribble_text);
555 mark = gtk_text_buffer_get_insert(buffer);
556 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
557 scribble_pos = gtk_text_iter_get_offset(&iter);
558 g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
561 if (prefs.save_winpos)
563 GdkWindowState wstate;
565 g_key_file_set_integer(config, PACKAGE, "treeview_position",
566 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
567 g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
568 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
570 gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
571 gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
572 wstate = gdk_window_get_state(gtk_widget_get_window(main_widgets.window));
573 ui_prefs.geometry[4] = (wstate & GDK_WINDOW_STATE_MAXIMIZED) ? 1 : 0;
574 g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
577 g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
578 if (ui_prefs.custom_commands != NULL)
580 g_key_file_set_string_list(config, PACKAGE, "custom_commands",
581 (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
582 g_key_file_set_string_list(config, PACKAGE, "custom_commands_labels",
583 (const gchar**) ui_prefs.custom_commands_labels, g_strv_length(ui_prefs.custom_commands_labels));
588 void configuration_save(void)
590 GKeyFile *config = g_key_file_new();
591 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
592 gchar *data;
594 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
596 /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
597 g_signal_emit_by_name(geany_object, "save-settings", config);
599 save_dialog_prefs(config);
600 save_ui_prefs(config);
601 project_save_prefs(config); /* save project filename, etc. */
602 save_recent_files(config, ui_prefs.recent_queue, "recent_files");
603 save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
605 if (cl_options.load_session)
606 configuration_save_session_files(config);
607 #ifdef HAVE_VTE
608 else if (vte_info.have_vte)
610 vte_get_working_directory(); /* refresh vte_info.dir */
611 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
613 #endif
615 /* write the file */
616 data = g_key_file_to_data(config, NULL, NULL);
617 utils_write_file(configfile, data);
618 g_free(data);
620 g_key_file_free(config);
621 g_free(configfile);
625 static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
627 gchar **recent_files;
628 gsize i, len = 0;
630 recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
631 if (recent_files != NULL)
633 for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
635 gchar *filename = g_strdup(recent_files[i]);
636 g_queue_push_tail(queue, filename);
638 g_strfreev(recent_files);
644 * Load session list from the given keyfile, and store it in the global
645 * session_files variable for later file loading
646 * */
647 void configuration_load_session_files(GKeyFile *config, gboolean read_recent_files)
649 guint i;
650 gboolean have_session_files;
651 gchar entry[16];
652 gchar **tmp_array;
653 GError *error = NULL;
655 session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
657 if (read_recent_files)
659 load_recent_files(config, ui_prefs.recent_queue, "recent_files");
660 load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
663 /* the project may load another list than the main setting */
664 if (session_files != NULL)
665 g_ptr_array_free(session_files, TRUE);
667 session_files = g_ptr_array_new();
668 have_session_files = TRUE;
669 i = 0;
670 while (have_session_files)
672 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
673 tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
674 if (! tmp_array || error)
676 g_error_free(error);
677 error = NULL;
678 have_session_files = FALSE;
680 g_ptr_array_add(session_files, tmp_array);
681 i++;
684 #ifdef HAVE_VTE
685 /* BUG: after loading project at startup, closing project doesn't restore old VTE path */
686 if (vte_info.have_vte)
688 gchar *tmp_string = utils_get_setting_string(config, "VTE", "last_dir", NULL);
689 vte_cwd(tmp_string,TRUE);
690 g_free(tmp_string);
692 #endif
696 #ifdef HAVE_VTE
697 static void get_setting_color(GKeyFile *config, const gchar *section, const gchar *key,
698 GdkColor *color, const gchar *default_color)
700 gchar *str = utils_get_setting_string(config, section, key, NULL);
701 if (str == NULL || ! utils_parse_color(str, color))
702 utils_parse_color(default_color, color);
703 g_free(str);
705 #endif
708 /* note: new settings should be added in init_pref_groups() */
709 static void load_dialog_prefs(GKeyFile *config)
711 gchar *tmp_string, *tmp_string2;
712 const gchar *default_charset = NULL;
713 gchar *cmd;
715 /* compatibility with Geany 0.20 */
716 if (!g_key_file_has_key(config, PACKAGE, atomic_file_saving_key, NULL))
718 g_key_file_set_boolean(config, PACKAGE, atomic_file_saving_key,
719 utils_get_setting_boolean(config, PACKAGE, "use_safe_file_saving", FALSE));
722 /* compatibility with Geany 0.21 */
724 gboolean suppress_search_dialogs = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_search_dialogs", FALSE);
726 if (!g_key_file_has_key(config, "search", "pref_search_always_wrap", NULL))
727 g_key_file_set_boolean(config, "search", "pref_search_always_wrap", suppress_search_dialogs);
729 if (!g_key_file_has_key(config, "search", "pref_search_hide_find_dialog", NULL))
730 g_key_file_set_boolean(config, "search", "pref_search_hide_find_dialog", suppress_search_dialogs);
733 /* read stash prefs */
734 settings_action(config, SETTING_READ);
736 /* general */
737 prefs.confirm_exit = utils_get_setting_boolean(config, PACKAGE, "pref_main_confirm_exit", FALSE);
738 prefs.suppress_status_messages = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_status_messages", FALSE);
739 prefs.load_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_load_session", TRUE);
740 project_prefs.project_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_session", TRUE);
741 project_prefs.project_file_in_basedir = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", FALSE);
742 prefs.save_winpos = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_winpos", TRUE);
743 prefs.beep_on_errors = utils_get_setting_boolean(config, PACKAGE, "beep_on_errors", TRUE);
744 prefs.switch_to_status = utils_get_setting_boolean(config, PACKAGE, "switch_msgwin_pages", FALSE);
745 prefs.auto_focus = utils_get_setting_boolean(config, PACKAGE, "auto_focus", FALSE);
747 /* interface */
748 interface_prefs.tab_pos_editor = utils_get_setting_integer(config, PACKAGE, "tab_pos_editor", GTK_POS_TOP);
749 interface_prefs.tab_pos_msgwin = utils_get_setting_integer(config, PACKAGE, "tab_pos_msgwin",GTK_POS_LEFT);
750 interface_prefs.sidebar_symbol_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_symbol_visible", TRUE);
751 interface_prefs.sidebar_openfiles_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_openfiles_visible", TRUE);
752 interface_prefs.statusbar_visible = utils_get_setting_boolean(config, PACKAGE, "statusbar_visible", TRUE);
753 file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
754 file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
755 interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
756 file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
757 interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
758 interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
759 interface_prefs.msgwin_font = utils_get_setting_string(config, PACKAGE, "msgwin_font", GEANY_DEFAULT_FONT_MSG_WINDOW);
760 interface_prefs.use_native_windows_dialogs = utils_get_setting_boolean(config, PACKAGE, "use_native_windows_dialogs", FALSE);
762 /* display, editor */
763 editor_prefs.long_line_enabled = utils_get_setting_boolean(config, PACKAGE, "long_line_enabled", TRUE);
764 editor_prefs.long_line_type = utils_get_setting_integer(config, PACKAGE, "long_line_type", 0);
765 if (editor_prefs.long_line_type == 2) /* backward compatibility */
767 editor_prefs.long_line_type = 0;
768 editor_prefs.long_line_enabled = FALSE;
770 editor_prefs.long_line_color = utils_get_setting_string(config, PACKAGE, "long_line_color", "#C2EBC2");
771 editor_prefs.long_line_column = utils_get_setting_integer(config, PACKAGE, "long_line_column", 72);
772 editor_prefs.symbolcompletion_min_chars = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_min_chars", GEANY_MIN_SYMBOLLIST_CHARS);
773 editor_prefs.symbolcompletion_max_height = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_max_height", GEANY_MAX_SYMBOLLIST_HEIGHT);
774 editor_prefs.line_wrapping = utils_get_setting_boolean(config, PACKAGE, "line_wrapping", FALSE); /* default is off for better performance */
775 editor_prefs.use_indicators = utils_get_setting_boolean(config, PACKAGE, "use_indicators", TRUE);
776 editor_prefs.show_indent_guide = utils_get_setting_boolean(config, PACKAGE, "show_indent_guide", FALSE);
777 editor_prefs.show_white_space = utils_get_setting_boolean(config, PACKAGE, "show_white_space", FALSE);
778 editor_prefs.show_line_endings = utils_get_setting_boolean(config, PACKAGE, "show_line_endings", FALSE);
779 editor_prefs.scroll_stop_at_last_line = utils_get_setting_boolean(config, PACKAGE, "scroll_stop_at_last_line", TRUE);
780 editor_prefs.auto_close_xml_tags = utils_get_setting_boolean(config, PACKAGE, "auto_close_xml_tags", TRUE);
781 editor_prefs.complete_snippets = utils_get_setting_boolean(config, PACKAGE, "complete_snippets", TRUE);
782 editor_prefs.auto_complete_symbols = utils_get_setting_boolean(config, PACKAGE, "auto_complete_symbols", TRUE);
783 editor_prefs.folding = utils_get_setting_boolean(config, PACKAGE, "use_folding", TRUE);
784 editor_prefs.unfold_all_children = utils_get_setting_boolean(config, PACKAGE, "unfold_all_children", FALSE);
785 editor_prefs.show_markers_margin = utils_get_setting_boolean(config, PACKAGE, "show_markers_margin", TRUE);
786 editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
787 editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
788 editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
789 editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
790 editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
791 editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
792 editor_prefs.comment_toggle_mark = utils_get_setting_string(config, PACKAGE, "comment_toggle_mark", GEANY_TOGGLE_MARK);
793 editor_prefs.autoclose_chars = utils_get_setting_integer(config, PACKAGE, "autoclose_chars", 0);
795 /* Files
796 * use current locale encoding as default for new files (should be UTF-8 in most cases) */
797 g_get_charset(&default_charset);
798 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_new_encoding",
799 default_charset);
800 if (tmp_string)
802 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
803 if (enc != NULL)
804 file_prefs.default_new_encoding = enc->idx;
805 else
806 file_prefs.default_new_encoding = GEANY_ENCODING_UTF_8;
808 g_free(tmp_string);
810 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_open_encoding",
811 "none");
812 if (tmp_string)
814 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
815 if (enc != NULL)
816 file_prefs.default_open_encoding = enc->idx;
817 else
818 file_prefs.default_open_encoding = -1;
820 g_free(tmp_string);
822 file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
823 file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
824 file_prefs.ensure_convert_new_lines = utils_get_setting_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", FALSE);
825 file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
826 file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
828 /* toolbar */
829 toolbar_prefs.visible = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_show", TRUE);
830 toolbar_prefs.append_to_menu = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", FALSE);
832 toolbar_prefs.use_gtk_default_style = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", TRUE);
833 if (! toolbar_prefs.use_gtk_default_style)
834 toolbar_prefs.icon_style = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_style", GTK_TOOLBAR_ICONS);
836 toolbar_prefs.use_gtk_default_icon = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", TRUE);
837 if (! toolbar_prefs.use_gtk_default_icon)
838 toolbar_prefs.icon_size = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_size", GTK_ICON_SIZE_LARGE_TOOLBAR);
841 /* VTE */
842 #ifdef HAVE_VTE
843 vte_info.load_vte = utils_get_setting_boolean(config, "VTE", "load_vte", TRUE);
844 if (vte_info.load_vte)
846 struct passwd *pw = getpwuid(getuid());
847 const gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
849 vc = g_new0(VteConfig, 1);
850 vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
851 if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
852 /* last dir is not set, fallback to user's home directory */
853 SETPTR(vte_info.dir, g_strdup(pw->pw_dir));
854 else if (vte_info.dir == NULL && pw == NULL)
855 /* fallback to root */
856 vte_info.dir = g_strdup("/");
858 vc->emulation = utils_get_setting_string(config, "VTE", "emulation", "xterm");
859 vc->send_selection_unsafe = utils_get_setting_boolean(config, "VTE",
860 "send_selection_unsafe", FALSE);
861 vc->image = utils_get_setting_string(config, "VTE", "image", "");
862 vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
863 vc->font = utils_get_setting_string(config, "VTE", "font", "Monospace 10");
864 vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
865 vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
866 vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", TRUE);
867 vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
868 vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
869 vc->send_cmd_prefix = utils_get_setting_string(config, "VTE", "send_cmd_prefix", "");
870 vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
871 vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
872 vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
873 vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
874 get_setting_color(config, "VTE", "colour_fore", &vc->colour_fore, "#ffffff");
875 get_setting_color(config, "VTE", "colour_back", &vc->colour_back, "#000000");
877 #endif
878 /* templates */
879 template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
880 template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
881 tmp_string = utils_get_initials(template_prefs.developer);
882 template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
883 g_free(tmp_string);
885 template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
887 tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), g_get_host_name());
888 template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
889 g_free(tmp_string);
890 template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", GEANY_TEMPLATES_FORMAT_YEAR);
891 template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", GEANY_TEMPLATES_FORMAT_DATE);
892 template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", GEANY_TEMPLATES_FORMAT_DATETIME);
894 /* tools */
895 cmd = utils_get_setting_string(config, "tools", "terminal_cmd", "");
896 if (EMPTY(cmd))
898 SETPTR(cmd, utils_get_setting_string(config, "tools", "term_cmd", ""));
899 if (!EMPTY(cmd))
901 tmp_string = cmd;
902 #ifdef G_OS_WIN32
903 if (strstr(cmd, "cmd.exe"))
904 cmd = g_strconcat(cmd, " /Q /C %c", NULL);
905 else
906 cmd = g_strconcat(cmd, " %c", NULL);
907 #else
908 cmd = g_strconcat(cmd, " -e \"/bin/sh %c\"", NULL);
909 #endif
910 g_free(tmp_string);
912 else
913 SETPTR(cmd, g_strdup(GEANY_DEFAULT_TOOLS_TERMINAL));
915 tool_prefs.term_cmd = cmd;
916 tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
917 tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
919 tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
921 /* build menu */
922 build_set_group_count(GEANY_GBG_FT, build_menu_prefs.number_ft_menu_items);
923 build_set_group_count(GEANY_GBG_NON_FT, build_menu_prefs.number_non_ft_menu_items);
924 build_set_group_count(GEANY_GBG_EXEC, build_menu_prefs.number_exec_menu_items);
925 build_load_menu(config, GEANY_BCS_PREF, NULL);
927 /* printing */
928 tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
929 #ifdef G_OS_WIN32
930 if (!EMPTY(tmp_string2))
932 /* single quote paths on Win32 for g_spawn_command_line_async */
933 tmp_string = g_strconcat("'", tmp_string2, "' '%f'", NULL);
935 else
937 tmp_string = g_strdup("");
939 #else
940 tmp_string = g_strconcat(tmp_string2, " %f", NULL);
941 #endif
942 printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
943 g_free(tmp_string);
944 g_free(tmp_string2);
946 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
947 printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
948 printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
949 printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
950 printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
951 printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
955 static void load_ui_prefs(GKeyFile *config)
957 gint *geo;
958 gsize geo_len;
960 ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
961 ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
962 ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
963 ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
964 ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
965 ui_prefs.custom_commands_labels = g_key_file_get_string_list(config, PACKAGE, "custom_commands_labels", NULL, NULL);
967 /* sanitize custom commands labels */
968 if (ui_prefs.custom_commands || ui_prefs.custom_commands_labels)
970 guint i;
971 guint cc_len = ui_prefs.custom_commands ? g_strv_length(ui_prefs.custom_commands) : 0;
972 guint cc_labels_len = ui_prefs.custom_commands_labels ? g_strv_length(ui_prefs.custom_commands_labels) : 0;
974 /* not enough items, resize and fill */
975 if (cc_labels_len < cc_len)
977 ui_prefs.custom_commands_labels = g_realloc(ui_prefs.custom_commands_labels,
978 (cc_len + 1) * sizeof *ui_prefs.custom_commands_labels);
979 for (i = cc_labels_len; i < cc_len; i++)
980 ui_prefs.custom_commands_labels[i] = g_strdup("");
981 ui_prefs.custom_commands_labels[cc_len] = NULL;
983 /* too many items, cut off */
984 else if (cc_labels_len > cc_len)
986 for (i = cc_len; i < cc_labels_len; i++)
988 g_free(ui_prefs.custom_commands_labels[i]);
989 ui_prefs.custom_commands_labels[i] = NULL;
994 scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
995 _("Type here what you want, use it as a notice/scratch board"));
996 scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
998 geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", &geo_len, NULL);
999 if (! geo || geo_len < 5)
1001 ui_prefs.geometry[0] = -1;
1002 ui_prefs.geometry[1] = -1;
1003 ui_prefs.geometry[2] = -1;
1004 ui_prefs.geometry[3] = -1;
1005 ui_prefs.geometry[4] = 0;
1007 else
1009 /* don't use insane values but when main windows was maximized last time, pos might be
1010 * negative (due to differences in root window and window decorations) */
1011 /* quitting when minimized can make pos -32000, -32000 on Windows! */
1012 ui_prefs.geometry[0] = MAX(-1, geo[0]);
1013 ui_prefs.geometry[1] = MAX(-1, geo[1]);
1014 ui_prefs.geometry[2] = MAX(-1, geo[2]);
1015 ui_prefs.geometry[3] = MAX(-1, geo[3]);
1016 ui_prefs.geometry[4] = geo[4] != 0;
1018 hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
1019 vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
1020 (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
1021 (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
1023 g_free(geo);
1028 * Save current session in default configuration file
1030 void configuration_save_default_session(void)
1032 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1033 gchar *data;
1034 GKeyFile *config = g_key_file_new();
1036 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1038 if (cl_options.load_session)
1039 configuration_save_session_files(config);
1041 /* write the file */
1042 data = g_key_file_to_data(config, NULL, NULL);
1043 utils_write_file(configfile, data);
1044 g_free(data);
1046 g_key_file_free(config);
1047 g_free(configfile);
1051 void configuration_clear_default_session(void)
1053 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1054 gchar *data;
1055 GKeyFile *config = g_key_file_new();
1057 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1059 if (cl_options.load_session)
1060 remove_session_files(config);
1062 /* write the file */
1063 data = g_key_file_to_data(config, NULL, NULL);
1064 utils_write_file(configfile, data);
1065 g_free(data);
1067 g_key_file_free(config);
1068 g_free(configfile);
1073 * Only reload the session part of the default configuration
1075 void configuration_reload_default_session(void)
1077 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1078 GKeyFile *config = g_key_file_new();
1080 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1081 g_free(configfile);
1083 configuration_load_session_files(config, FALSE);
1085 g_key_file_free(config);
1089 gboolean configuration_load(void)
1091 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1092 GKeyFile *config = g_key_file_new();
1094 if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
1095 { /* config file does not (yet) exist, so try to load a global config file which may be */
1096 /* created by distributors */
1097 geany_debug("No user config file found, trying to use global configuration.");
1098 SETPTR(configfile, g_build_filename(app->datadir, "geany.conf", NULL));
1100 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1101 g_free(configfile);
1103 load_dialog_prefs(config);
1104 load_ui_prefs(config);
1105 project_load_prefs(config);
1106 configuration_load_session_files(config, TRUE);
1108 /* this signal can be used e.g. to delay building UI elements until settings have been read */
1109 g_signal_emit_by_name(geany_object, "load-settings", config);
1111 g_key_file_free(config);
1112 return TRUE;
1116 static gboolean open_session_file(gchar **tmp, guint len)
1118 guint pos;
1119 const gchar *ft_name;
1120 gchar *locale_filename;
1121 gchar *unescaped_filename;
1122 const gchar *encoding;
1123 gint indent_type;
1124 gboolean ro, auto_indent, line_wrapping;
1125 /** TODO when we have a global pref for line breaking, use its value */
1126 gboolean line_breaking = FALSE;
1127 gboolean ret = FALSE;
1129 pos = atoi(tmp[0]);
1130 ft_name = tmp[1];
1131 ro = atoi(tmp[2]);
1132 if (isdigit(tmp[3][0]))
1134 encoding = encodings_get_charset_from_index(atoi(tmp[3]));
1136 else
1138 encoding = &(tmp[3][1]);
1140 indent_type = atoi(tmp[4]);
1141 auto_indent = atoi(tmp[5]);
1142 line_wrapping = atoi(tmp[6]);
1143 /* try to get the locale equivalent for the filename */
1144 unescaped_filename = g_uri_unescape_string(tmp[7], NULL);
1145 locale_filename = utils_get_locale_from_utf8(unescaped_filename);
1147 if (len > 8)
1148 line_breaking = atoi(tmp[8]);
1150 if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR))
1152 GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
1153 GeanyDocument *doc = document_open_file_full(
1154 NULL, locale_filename, pos, ro, ft, encoding);
1156 if (doc)
1158 gint indent_width = doc->editor->indent_width;
1160 if (len > 9)
1161 indent_width = atoi(tmp[9]);
1162 editor_set_indent(doc->editor, indent_type, indent_width);
1163 editor_set_line_wrapping(doc->editor, line_wrapping);
1164 doc->editor->line_breaking = line_breaking;
1165 doc->editor->auto_indent = auto_indent;
1166 ret = TRUE;
1169 else
1171 geany_debug("Could not find file '%s'.", tmp[7]);
1174 g_free(locale_filename);
1175 g_free(unescaped_filename);
1176 return ret;
1180 /* Open session files
1181 * Note: notebook page switch handler and adding to recent files list is always disabled
1182 * for all files opened within this function */
1183 void configuration_open_files(void)
1185 gint i;
1186 gboolean failure = FALSE;
1188 /* necessary to set it to TRUE for project session support */
1189 main_status.opening_session_files = TRUE;
1191 i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
1192 while (TRUE)
1194 gchar **tmp = g_ptr_array_index(session_files, i);
1195 guint len;
1197 if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
1199 if (! open_session_file(tmp, len))
1200 failure = TRUE;
1202 g_strfreev(tmp);
1204 if (file_prefs.tab_order_ltr)
1206 i++;
1207 if (i >= (gint)session_files->len)
1208 break;
1210 else
1212 i--;
1213 if (i < 0)
1214 break;
1218 g_ptr_array_free(session_files, TRUE);
1219 session_files = NULL;
1221 if (failure)
1222 ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
1223 else
1225 /* explicitly trigger a notebook page switch after unsetting main_status.opening_session_files
1226 * for callbacks to run (and update window title, encoding settings, and so on) */
1227 gint n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1228 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
1229 gint target_page = session_notebook_page >= 0 ? session_notebook_page : cur_page;
1231 /* if target page is current page, switch to another page first to really trigger an event */
1232 if (target_page == cur_page && n_pages > 0)
1233 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), (cur_page + 1) % n_pages);
1235 main_status.opening_session_files = FALSE;
1236 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), target_page);
1238 main_status.opening_session_files = FALSE;
1242 /* set some settings which are already read from the config file, but need other things, like the
1243 * realisation of the main window */
1244 void configuration_apply_settings(void)
1246 if (scribble_text)
1247 { /* update the scribble widget, because now it's realized */
1248 GtkTextIter iter;
1249 GtkTextBuffer *buffer =
1250 gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
1252 gtk_text_buffer_set_text(buffer, scribble_text, -1);
1253 gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
1254 gtk_text_buffer_place_cursor(buffer, &iter);
1256 g_free(scribble_text);
1258 /* set the position of the hpaned and vpaned */
1259 if (prefs.save_winpos)
1261 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
1262 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
1265 /* set fullscreen after initial draw so that returning to normal view is the right size.
1266 * fullscreen mode is disabled by default, so act only if it is true */
1267 if (ui_prefs.fullscreen)
1269 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
1270 ui_prefs.fullscreen = TRUE;
1271 ui_set_fullscreen();
1274 msgwin_show_hide_tabs();
1278 void configuration_init(void)
1280 keyfile_groups = g_ptr_array_new();
1281 pref_groups = g_ptr_array_new();
1282 init_pref_groups();
1286 void configuration_finalize(void)
1288 guint i;
1289 StashGroup *group;
1291 foreach_ptr_array(group, i, keyfile_groups)
1292 stash_group_free(group);
1294 g_ptr_array_free(keyfile_groups, TRUE);
1295 g_ptr_array_free(pref_groups, TRUE);