Update of German translation
[geany-mirror.git] / src / keyfile.c
blob05373af88845466e21ff5f0e54aed54f58c2d1dc
1 /*
2 * keyfile.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005 The Geany contributors
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 along
17 * with this program; if not, write to the Free Software Foundation, Inc.,
18 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 * geany.conf preferences file loading and saving.
26 * Session file format:
27 * filename_xx=pos;filetype UID;read only;Eencoding;use_tabs;auto_indent;line_wrapping;filename
30 #ifdef HAVE_CONFIG_H
31 # include "config.h"
32 #endif
34 #include "keyfile.h"
36 #include "app.h"
37 #include "build.h"
38 #include "document.h"
39 #include "encodings.h"
40 #include "encodingsprivate.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 "symbols.h"
52 #include "templates.h"
53 #include "toolbar.h"
54 #include "ui_utils.h"
55 #include "utils.h"
56 #include "vte.h"
58 #include <stdlib.h>
59 #include <string.h>
60 #include <ctype.h>
62 #ifdef HAVE_VTE
63 #include <pwd.h>
64 #include <sys/types.h>
65 #include <unistd.h>
66 #endif
69 /* some default settings which are used at the very first start of Geany to fill
70 * the configuration file */
71 #define GEANY_MAX_SYMBOLLIST_HEIGHT 10
72 #define GEANY_MIN_SYMBOLLIST_CHARS 4
73 #define GEANY_MSGWIN_HEIGHT 208
74 #define GEANY_DISK_CHECK_TIMEOUT 30
75 #define GEANY_DEFAULT_TOOLS_MAKE "make"
76 #ifdef G_OS_WIN32
77 #define GEANY_DEFAULT_TOOLS_TERMINAL "cmd.exe /Q /C %c"
78 #elif defined(__APPLE__)
79 #define GEANY_DEFAULT_TOOLS_TERMINAL "open -a terminal %c"
80 #else
81 #define GEANY_DEFAULT_TOOLS_TERMINAL "xterm -e \"/bin/sh %c\""
82 #endif
83 #ifdef __APPLE__
84 #define GEANY_DEFAULT_TOOLS_BROWSER "open -a safari"
85 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Helvetica Medium 12"
86 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Menlo Medium 12"
87 #define GEANY_DEFAULT_FONT_EDITOR "Menlo Medium 12"
88 #else
89 #define GEANY_DEFAULT_TOOLS_BROWSER "firefox"
90 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Sans 9"
91 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Monospace 9"
92 #define GEANY_DEFAULT_FONT_EDITOR "Monospace 10"
93 #endif
94 #define GEANY_DEFAULT_TOOLS_PRINTCMD "lpr"
95 #define GEANY_DEFAULT_TOOLS_GREP "grep"
96 #define GEANY_DEFAULT_MRU_LENGTH 10
97 #define GEANY_TOGGLE_MARK "~ "
98 #define GEANY_MAX_AUTOCOMPLETE_WORDS 30
99 #define GEANY_MAX_SYMBOLS_UPDATE_FREQ 250
100 #define GEANY_DEFAULT_FILETYPE_REGEX "-\\*-\\s*([^\\s]+)\\s*-\\*-"
103 static gchar *scribble_text = NULL;
104 static gint scribble_pos = -1;
105 static GPtrArray *session_files = NULL;
106 static gint session_notebook_page;
107 static gint hpan_position;
108 static gint vpan_position;
109 static const gchar atomic_file_saving_key[] = "use_atomic_file_saving";
111 static GPtrArray *keyfile_groups = NULL;
113 GPtrArray *pref_groups = NULL;
115 static struct
117 gint number_ft_menu_items;
118 gint number_non_ft_menu_items;
119 gint number_exec_menu_items;
121 build_menu_prefs;
124 /* The group will be free'd on quitting.
125 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
126 void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
128 g_ptr_array_add(keyfile_groups, group);
130 if (for_prefs_dialog)
131 g_ptr_array_add(pref_groups, group);
135 /* The group will be free'd on quitting. */
136 void configuration_add_various_pref_group(struct StashGroup *group)
138 configuration_add_pref_group(group, TRUE);
139 stash_group_set_various(group, TRUE);
143 static void init_pref_groups(void)
145 StashGroup *group;
147 group = stash_group_new(PACKAGE);
148 configuration_add_pref_group(group, TRUE);
149 stash_group_add_entry(group, &prefs.default_open_path,
150 "default_open_path", "", "startup_path_entry");
152 stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
153 "cmdline_new_files", TRUE, "check_cmdline_new_files");
155 stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
156 "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
157 stash_group_add_toggle_button(group, &file_prefs.tab_close_switch_to_mru,
158 "tab_close_switch_to_mru", FALSE, "check_tab_close_switch_to_mru");
159 stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
160 stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
161 "sidebar_pos", GTK_POS_LEFT,
162 "radio_sidebar_left", GTK_POS_LEFT,
163 "radio_sidebar_right", GTK_POS_RIGHT,
164 NULL);
165 stash_group_add_radio_buttons(group, &interface_prefs.symbols_sort_mode,
166 "symbols_sort_mode", SYMBOLS_SORT_BY_NAME,
167 "radio_symbols_sort_by_name", SYMBOLS_SORT_BY_NAME,
168 "radio_symbols_sort_by_appearance", SYMBOLS_SORT_BY_APPEARANCE,
169 NULL);
170 stash_group_add_radio_buttons(group, &interface_prefs.msgwin_orientation,
171 "msgwin_orientation", GTK_ORIENTATION_VERTICAL,
172 "radio_msgwin_vertical", GTK_ORIENTATION_VERTICAL,
173 "radio_msgwin_horizontal", GTK_ORIENTATION_HORIZONTAL,
174 NULL);
176 /* editor display */
177 stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
178 "highlighting_invert_all", FALSE, "check_highlighting_invert");
180 stash_group_add_toggle_button(group, &search_prefs.use_current_word,
181 "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
183 /* editor */
184 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
185 "check_detect_indent", FALSE, "check_detect_indent_type");
186 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_width,
187 "detect_indent_width", FALSE, "check_detect_indent_width");
188 stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
189 "use_tab_to_indent", TRUE, "check_tab_key_indents");
190 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
191 "pref_editor_tab_width", 4, "spin_indent_width");
192 stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
193 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
194 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
195 "indent_type", GEANY_INDENT_TYPE_TABS,
196 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
197 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
198 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
199 NULL);
200 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
201 "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
202 "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
203 "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
204 "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
205 NULL);
206 stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
207 "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
208 stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
209 "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
210 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
211 "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
212 "spin_autocompletion_max_entries");
213 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_update_freq,
214 "autocompletion_update_freq", GEANY_MAX_SYMBOLS_UPDATE_FREQ, "spin_symbol_update_freq");
215 stash_group_add_string(group, &editor_prefs.color_scheme,
216 "color_scheme", NULL);
217 stash_group_add_spin_button_integer(group, &editor_prefs.scroll_lines_around_cursor,
218 "scroll_lines_around_cursor", 0, "spin_scroll_lines_around_cursor");
220 /* files */
221 stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
222 "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
223 stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
224 "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
226 /* various geany prefs */
227 group = stash_group_new(PACKAGE);
228 configuration_add_various_pref_group(group);
230 stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
231 "show_editor_scrollbars", TRUE);
232 stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
233 "brace_match_ltgt", FALSE);
234 stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
235 "use_gtk_word_boundaries", TRUE);
236 stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
237 "complete_snippets_whilst_editing", FALSE);
238 stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
239 atomic_file_saving_key, FALSE);
240 stash_group_add_boolean(group, &file_prefs.gio_unsafe_save_backup,
241 "gio_unsafe_save_backup", FALSE);
242 stash_group_add_boolean(group, &file_prefs.use_gio_unsafe_file_saving,
243 "use_gio_unsafe_file_saving", TRUE);
244 stash_group_add_boolean(group, &file_prefs.keep_edit_history_on_reload,
245 "keep_edit_history_on_reload", TRUE);
246 stash_group_add_boolean(group, &file_prefs.show_keep_edit_history_on_reload_msg,
247 "show_keep_edit_history_on_reload_msg", TRUE);
248 stash_group_add_boolean(group, &file_prefs.reload_clean_doc_on_file_change,
249 "reload_clean_doc_on_file_change", FALSE);
250 /* for backwards-compatibility */
251 stash_group_add_integer(group, &editor_prefs.indentation->hard_tab_width,
252 "indent_hard_tab_width", 8);
253 stash_group_add_integer(group, (gint*)&search_prefs.find_selection_type,
254 "find_selection_type", GEANY_FIND_SEL_CURRENT_WORD);
255 stash_group_add_string(group, &file_prefs.extract_filetype_regex,
256 "extract_filetype_regex", GEANY_DEFAULT_FILETYPE_REGEX);
257 stash_group_add_boolean(group, &search_prefs.replace_and_find_by_default,
258 "replace_and_find_by_default", TRUE);
259 stash_group_add_integer(group, &editor_prefs.ime_interaction,
260 "editor_ime_interaction", SC_IME_WINDOWED);
262 /* Note: Interface-related various prefs are in ui_init_prefs() */
264 /* various build-menu prefs */
265 group = stash_group_new("build-menu");
266 configuration_add_various_pref_group(group);
268 stash_group_add_integer(group, &build_menu_prefs.number_ft_menu_items,
269 "number_ft_menu_items", 0);
270 stash_group_add_integer(group, &build_menu_prefs.number_non_ft_menu_items,
271 "number_non_ft_menu_items", 0);
272 stash_group_add_integer(group, &build_menu_prefs.number_exec_menu_items,
273 "number_exec_menu_items", 0);
277 typedef enum SettingAction
279 SETTING_READ,
280 SETTING_WRITE
282 SettingAction;
284 static void settings_action(GKeyFile *config, SettingAction action)
286 guint i;
287 StashGroup *group;
289 foreach_ptr_array(group, i, keyfile_groups)
291 switch (action)
293 case SETTING_READ:
294 stash_group_load_from_key_file(group, config); break;
295 case SETTING_WRITE:
296 stash_group_save_to_key_file(group, config); break;
302 static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
304 gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
305 guint i;
307 for (i = 0; i < file_prefs.mru_length; i++)
309 if (! g_queue_is_empty(queue))
311 /* copy the values, this is necessary when this function is called from the
312 * preferences dialog or when quitting is canceled to keep the queue intact */
313 recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
315 else
317 recent_files[i] = NULL;
318 break;
321 /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
322 recent_files[file_prefs.mru_length] = NULL;
323 g_key_file_set_string_list(config, "files", key,
324 (const gchar**)recent_files, file_prefs.mru_length);
325 g_strfreev(recent_files);
329 static gchar *get_session_file_string(GeanyDocument *doc)
331 gchar *fname;
332 gchar *locale_filename;
333 gchar *escaped_filename;
334 GeanyFiletype *ft = doc->file_type;
336 if (ft == NULL) /* can happen when saving a new file when quitting */
337 ft = filetypes[GEANY_FILETYPES_NONE];
339 locale_filename = utils_get_locale_from_utf8(doc->file_name);
340 escaped_filename = g_uri_escape_string(locale_filename, NULL, TRUE);
342 fname = g_strdup_printf("%d;%s;%d;E%s;%d;%d;%d;%s;%d;%d",
343 sci_get_current_position(doc->editor->sci),
344 ft->name,
345 doc->readonly,
346 doc->encoding,
347 doc->editor->indent_type,
348 doc->editor->auto_indent,
349 doc->editor->line_wrapping,
350 escaped_filename,
351 doc->editor->line_breaking,
352 doc->editor->indent_width);
353 g_free(escaped_filename);
354 g_free(locale_filename);
355 return fname;
359 static void remove_session_files(GKeyFile *config)
361 gchar **ptr;
362 gchar **keys = g_key_file_get_keys(config, "files", NULL, NULL);
364 foreach_strv(ptr, keys)
366 if (g_str_has_prefix(*ptr, "FILE_NAME_"))
367 g_key_file_remove_key(config, "files", *ptr, NULL);
369 g_strfreev(keys);
373 void configuration_save_session_files(GKeyFile *config)
375 gint npage;
376 gchar entry[16];
377 guint i = 0, j = 0, max;
379 npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
380 g_key_file_set_integer(config, "files", "current_page", npage);
382 // clear existing entries first as they might not all be overwritten
383 remove_session_files(config);
385 /* store the filenames in the notebook tab order to reopen them the next time */
386 max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
387 for (i = 0; i < max; i++)
389 GeanyDocument *doc = document_get_from_page(i);
391 if (doc != NULL && doc->real_path != NULL)
393 gchar *fname;
395 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
396 fname = get_session_file_string(doc);
397 g_key_file_set_string(config, "files", entry, fname);
398 g_free(fname);
399 j++;
403 #ifdef HAVE_VTE
404 if (vte_info.have_vte)
406 vte_get_working_directory(); /* refresh vte_info.dir */
407 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
409 #endif
413 static void save_dialog_prefs(GKeyFile *config)
415 /* new settings should be added in init_pref_groups() */
416 settings_action(config, SETTING_WRITE);
418 /* Some of the key names are not consistent, but this is for backwards compatibility */
420 /* general */
421 g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
422 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_session", project_prefs.project_session);
423 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
424 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
425 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_wingeom", prefs.save_wingeom);
426 g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
427 g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
428 g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
429 g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
430 g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
432 /* interface */
433 g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
434 g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
435 g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
436 g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
437 g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
438 g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
439 g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
440 g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
441 g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
442 g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
443 g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
444 g_key_file_set_boolean(config, PACKAGE, "use_native_windows_dialogs", interface_prefs.use_native_windows_dialogs);
446 /* display */
447 g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
448 g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
449 g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
450 g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
451 g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
452 g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_enabled);
453 g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_type);
454 g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_column);
455 g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
457 /* editor */
458 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
459 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
460 g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
461 g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
462 g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
463 g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
464 g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
465 g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
466 g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
467 g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
468 g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
469 g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
470 g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
471 g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
472 g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
473 g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
474 g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
476 /* files */
477 g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
478 if (file_prefs.default_open_encoding == -1)
479 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
480 else
481 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
482 g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
483 g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
484 g_key_file_set_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", file_prefs.ensure_convert_new_lines);
485 g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
486 g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
488 /* toolbar */
489 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
490 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
491 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
492 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
493 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
494 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
496 /* templates */
497 g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
498 g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
499 g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
500 g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
501 g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
502 g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
503 g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
504 g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
506 /* tools settings */
507 g_key_file_set_string(config, "tools", "terminal_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
508 g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
509 g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
510 g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
512 /* build menu */
513 build_save_menu(config, NULL, GEANY_BCS_PREF);
515 /* printing */
516 g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
517 g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
518 g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
519 g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
520 g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
521 g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
522 g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
524 /* VTE */
525 #ifdef HAVE_VTE
526 g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
527 if (vte_info.have_vte)
529 gchar *tmp_string;
531 g_key_file_set_string(config, "VTE", "font", vc->font);
532 g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
533 g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
534 g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
535 g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
536 g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
537 g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
538 g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
539 g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
540 g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
541 g_key_file_set_string(config, "VTE", "font", vc->font);
542 g_key_file_set_string(config, "VTE", "shell", vc->shell);
543 tmp_string = utils_get_hex_from_color(&vc->colour_fore);
544 g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
545 g_free(tmp_string);
546 tmp_string = utils_get_hex_from_color(&vc->colour_back);
547 g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
548 g_free(tmp_string);
550 #endif
554 static void save_ui_prefs(GKeyFile *config)
556 g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
557 g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
558 g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
559 g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
561 /* get the text from the scribble textview */
563 GtkTextBuffer *buffer;
564 GtkTextIter start, end, iter;
565 GtkTextMark *mark;
567 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
568 gtk_text_buffer_get_bounds(buffer, &start, &end);
569 scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
570 g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
571 g_free(scribble_text);
573 mark = gtk_text_buffer_get_insert(buffer);
574 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
575 scribble_pos = gtk_text_iter_get_offset(&iter);
576 g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
579 if (prefs.save_winpos || prefs.save_wingeom)
581 GdkWindowState wstate;
583 g_key_file_set_integer(config, PACKAGE, "treeview_position",
584 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
585 g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
586 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
588 gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
589 gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
590 wstate = gdk_window_get_state(gtk_widget_get_window(main_widgets.window));
591 ui_prefs.geometry[4] = (wstate & GDK_WINDOW_STATE_MAXIMIZED) ? 1 : 0;
592 g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
595 g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
596 if (ui_prefs.custom_commands != NULL)
598 g_key_file_set_string_list(config, PACKAGE, "custom_commands",
599 (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
600 g_key_file_set_string_list(config, PACKAGE, "custom_commands_labels",
601 (const gchar**) ui_prefs.custom_commands_labels, g_strv_length(ui_prefs.custom_commands_labels));
606 void configuration_save(void)
608 GKeyFile *config = g_key_file_new();
609 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
610 gchar *data;
612 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
614 /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
615 g_signal_emit_by_name(geany_object, "save-settings", config);
617 save_dialog_prefs(config);
618 save_ui_prefs(config);
619 project_save_prefs(config); /* save project filename, etc. */
620 save_recent_files(config, ui_prefs.recent_queue, "recent_files");
621 save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
623 if (cl_options.load_session)
624 configuration_save_session_files(config);
625 #ifdef HAVE_VTE
626 else if (vte_info.have_vte)
628 vte_get_working_directory(); /* refresh vte_info.dir */
629 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
631 #endif
633 /* write the file */
634 data = g_key_file_to_data(config, NULL, NULL);
635 utils_write_file(configfile, data);
636 g_free(data);
638 g_key_file_free(config);
639 g_free(configfile);
643 static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
645 gchar **recent_files;
646 gsize i, len = 0;
648 recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
649 if (recent_files != NULL)
651 for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
653 gchar *filename = g_strdup(recent_files[i]);
654 g_queue_push_tail(queue, filename);
656 g_strfreev(recent_files);
662 * Load session list from the given keyfile, and store it in the global
663 * session_files variable for later file loading
664 * */
665 void configuration_load_session_files(GKeyFile *config, gboolean read_recent_files)
667 guint i;
668 gboolean have_session_files;
669 gchar entry[16];
670 gchar **tmp_array;
671 GError *error = NULL;
673 session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
675 if (read_recent_files)
677 load_recent_files(config, ui_prefs.recent_queue, "recent_files");
678 load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
681 /* the project may load another list than the main setting */
682 if (session_files != NULL)
684 foreach_ptr_array(tmp_array, i, session_files)
685 g_strfreev(tmp_array);
686 g_ptr_array_free(session_files, TRUE);
689 session_files = g_ptr_array_new();
690 have_session_files = TRUE;
691 i = 0;
692 while (have_session_files)
694 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
695 tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
696 if (! tmp_array || error)
698 g_error_free(error);
699 error = NULL;
700 have_session_files = FALSE;
702 g_ptr_array_add(session_files, tmp_array);
703 i++;
706 #ifdef HAVE_VTE
707 /* BUG: after loading project at startup, closing project doesn't restore old VTE path */
708 if (vte_info.have_vte)
710 gchar *tmp_string = utils_get_setting_string(config, "VTE", "last_dir", NULL);
711 vte_cwd(tmp_string,TRUE);
712 g_free(tmp_string);
714 #endif
718 #ifdef HAVE_VTE
719 static void get_setting_color(GKeyFile *config, const gchar *section, const gchar *key,
720 GdkColor *color, const gchar *default_color)
722 gchar *str = utils_get_setting_string(config, section, key, NULL);
723 if (str == NULL || ! utils_parse_color(str, color))
724 utils_parse_color(default_color, color);
725 g_free(str);
727 #endif
730 /* note: new settings should be added in init_pref_groups() */
731 static void load_dialog_prefs(GKeyFile *config)
733 gchar *tmp_string, *tmp_string2;
734 const gchar *default_charset = NULL;
735 gchar *cmd;
737 /* compatibility with Geany 0.20 */
738 if (!g_key_file_has_key(config, PACKAGE, atomic_file_saving_key, NULL))
740 g_key_file_set_boolean(config, PACKAGE, atomic_file_saving_key,
741 utils_get_setting_boolean(config, PACKAGE, "use_safe_file_saving", FALSE));
744 /* compatibility with Geany 0.21 */
746 gboolean suppress_search_dialogs = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_search_dialogs", FALSE);
748 if (!g_key_file_has_key(config, "search", "pref_search_always_wrap", NULL))
749 g_key_file_set_boolean(config, "search", "pref_search_always_wrap", suppress_search_dialogs);
751 if (!g_key_file_has_key(config, "search", "pref_search_hide_find_dialog", NULL))
752 g_key_file_set_boolean(config, "search", "pref_search_hide_find_dialog", suppress_search_dialogs);
755 /* general */
756 prefs.confirm_exit = utils_get_setting_boolean(config, PACKAGE, "pref_main_confirm_exit", FALSE);
757 prefs.suppress_status_messages = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_status_messages", FALSE);
758 prefs.load_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_load_session", TRUE);
759 project_prefs.project_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_session", TRUE);
760 project_prefs.project_file_in_basedir = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", FALSE);
761 prefs.save_winpos = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_winpos", TRUE);
762 prefs.save_wingeom = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_wingeom", prefs.save_winpos);
763 prefs.beep_on_errors = utils_get_setting_boolean(config, PACKAGE, "beep_on_errors", TRUE);
764 prefs.switch_to_status = utils_get_setting_boolean(config, PACKAGE, "switch_msgwin_pages", FALSE);
765 prefs.auto_focus = utils_get_setting_boolean(config, PACKAGE, "auto_focus", FALSE);
767 /* interface */
768 interface_prefs.tab_pos_editor = utils_get_setting_integer(config, PACKAGE, "tab_pos_editor", GTK_POS_TOP);
769 interface_prefs.tab_pos_msgwin = utils_get_setting_integer(config, PACKAGE, "tab_pos_msgwin",GTK_POS_LEFT);
770 interface_prefs.sidebar_symbol_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_symbol_visible", TRUE);
771 interface_prefs.sidebar_openfiles_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_openfiles_visible", TRUE);
772 interface_prefs.statusbar_visible = utils_get_setting_boolean(config, PACKAGE, "statusbar_visible", TRUE);
773 file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
774 file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
775 interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
776 file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
777 interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
778 interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
779 interface_prefs.msgwin_font = utils_get_setting_string(config, PACKAGE, "msgwin_font", GEANY_DEFAULT_FONT_MSG_WINDOW);
780 interface_prefs.use_native_windows_dialogs = utils_get_setting_boolean(config, PACKAGE, "use_native_windows_dialogs", FALSE);
782 /* display, editor */
783 editor_prefs.long_line_enabled = utils_get_setting_boolean(config, PACKAGE, "long_line_enabled", TRUE);
784 editor_prefs.long_line_type = utils_get_setting_integer(config, PACKAGE, "long_line_type", 0);
785 if (editor_prefs.long_line_type == 2) /* backward compatibility */
787 editor_prefs.long_line_type = 0;
788 editor_prefs.long_line_enabled = FALSE;
790 editor_prefs.long_line_color = utils_get_setting_string(config, PACKAGE, "long_line_color", "#C2EBC2");
791 editor_prefs.long_line_column = utils_get_setting_integer(config, PACKAGE, "long_line_column", 72);
792 editor_prefs.symbolcompletion_min_chars = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_min_chars", GEANY_MIN_SYMBOLLIST_CHARS);
793 editor_prefs.symbolcompletion_max_height = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_max_height", GEANY_MAX_SYMBOLLIST_HEIGHT);
794 editor_prefs.line_wrapping = utils_get_setting_boolean(config, PACKAGE, "line_wrapping", FALSE); /* default is off for better performance */
795 editor_prefs.use_indicators = utils_get_setting_boolean(config, PACKAGE, "use_indicators", TRUE);
796 editor_prefs.show_indent_guide = utils_get_setting_boolean(config, PACKAGE, "show_indent_guide", FALSE);
797 editor_prefs.show_white_space = utils_get_setting_boolean(config, PACKAGE, "show_white_space", FALSE);
798 editor_prefs.show_line_endings = utils_get_setting_boolean(config, PACKAGE, "show_line_endings", FALSE);
799 editor_prefs.scroll_stop_at_last_line = utils_get_setting_boolean(config, PACKAGE, "scroll_stop_at_last_line", TRUE);
800 editor_prefs.auto_close_xml_tags = utils_get_setting_boolean(config, PACKAGE, "auto_close_xml_tags", TRUE);
801 editor_prefs.complete_snippets = utils_get_setting_boolean(config, PACKAGE, "complete_snippets", TRUE);
802 editor_prefs.auto_complete_symbols = utils_get_setting_boolean(config, PACKAGE, "auto_complete_symbols", TRUE);
803 editor_prefs.folding = utils_get_setting_boolean(config, PACKAGE, "use_folding", TRUE);
804 editor_prefs.unfold_all_children = utils_get_setting_boolean(config, PACKAGE, "unfold_all_children", FALSE);
805 editor_prefs.show_markers_margin = utils_get_setting_boolean(config, PACKAGE, "show_markers_margin", TRUE);
806 editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
807 editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
808 editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
809 editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
810 editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
811 editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
812 editor_prefs.comment_toggle_mark = utils_get_setting_string(config, PACKAGE, "comment_toggle_mark", GEANY_TOGGLE_MARK);
813 editor_prefs.autoclose_chars = utils_get_setting_integer(config, PACKAGE, "autoclose_chars", 0);
815 /* Files
816 * use current locale encoding as default for new files (should be UTF-8 in most cases) */
817 g_get_charset(&default_charset);
818 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_new_encoding",
819 default_charset);
820 if (tmp_string)
822 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
823 if (enc != NULL)
824 file_prefs.default_new_encoding = enc->idx;
825 else
826 file_prefs.default_new_encoding = GEANY_ENCODING_UTF_8;
828 g_free(tmp_string);
830 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_open_encoding",
831 "none");
832 if (tmp_string)
834 const GeanyEncoding *enc = NULL;
835 if (strcmp(tmp_string, "none") != 0)
836 enc = encodings_get_from_charset(tmp_string);
837 if (enc != NULL)
838 file_prefs.default_open_encoding = enc->idx;
839 else
840 file_prefs.default_open_encoding = -1;
842 g_free(tmp_string);
844 file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
845 file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
846 file_prefs.ensure_convert_new_lines = utils_get_setting_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", FALSE);
847 file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
848 file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
850 /* toolbar */
851 toolbar_prefs.visible = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_show", TRUE);
852 toolbar_prefs.append_to_menu = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", FALSE);
854 toolbar_prefs.use_gtk_default_style = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", TRUE);
855 if (! toolbar_prefs.use_gtk_default_style)
856 toolbar_prefs.icon_style = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_style", GTK_TOOLBAR_ICONS);
858 toolbar_prefs.use_gtk_default_icon = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", TRUE);
859 if (! toolbar_prefs.use_gtk_default_icon)
860 toolbar_prefs.icon_size = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_size", GTK_ICON_SIZE_LARGE_TOOLBAR);
863 /* VTE */
864 #ifdef HAVE_VTE
865 vte_info.load_vte = utils_get_setting_boolean(config, "VTE", "load_vte", TRUE);
866 if (vte_info.load_vte && vte_info.load_vte_cmdline /* not disabled on the cmdline */)
868 StashGroup *group;
869 struct passwd *pw = getpwuid(getuid());
870 const gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
872 #ifdef __APPLE__
873 /* Geany is started using launchd on OS X and we don't get any environment variables
874 * so PS1 isn't defined. Start as a login shell to read the corresponding config files. */
875 if (strcmp(shell, "/bin/bash") == 0)
876 shell = "/bin/bash -l";
877 #endif
879 vc = g_new0(VteConfig, 1);
880 vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
881 if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
882 /* last dir is not set, fallback to user's home directory */
883 SETPTR(vte_info.dir, g_strdup(pw->pw_dir));
884 else if (vte_info.dir == NULL && pw == NULL)
885 /* fallback to root */
886 vte_info.dir = g_strdup("/");
888 vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
889 vc->font = utils_get_setting_string(config, "VTE", "font", GEANY_DEFAULT_FONT_EDITOR);
890 vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
891 vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
892 vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", TRUE);
893 vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
894 vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
895 vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
896 vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
897 vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
898 vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
899 get_setting_color(config, "VTE", "colour_fore", &vc->colour_fore, "#ffffff");
900 get_setting_color(config, "VTE", "colour_back", &vc->colour_back, "#000000");
902 /* various VTE prefs.
903 * this can't be done in init_pref_groups() because we need to know the value of
904 * vte_info.load_vte, and `vc` to be initialized */
905 group = stash_group_new("VTE");
906 configuration_add_various_pref_group(group);
908 stash_group_add_string(group, &vc->send_cmd_prefix, "send_cmd_prefix", "");
909 stash_group_add_boolean(group, &vc->send_selection_unsafe, "send_selection_unsafe", FALSE);
911 #endif
912 /* templates */
913 template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
914 template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
915 tmp_string = utils_get_initials(template_prefs.developer);
916 template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
917 g_free(tmp_string);
919 template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
921 tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), g_get_host_name());
922 template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
923 g_free(tmp_string);
924 template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", GEANY_TEMPLATES_FORMAT_YEAR);
925 template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", GEANY_TEMPLATES_FORMAT_DATE);
926 template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", GEANY_TEMPLATES_FORMAT_DATETIME);
928 /* tools */
929 cmd = utils_get_setting_string(config, "tools", "terminal_cmd", "");
930 if (EMPTY(cmd))
932 SETPTR(cmd, utils_get_setting_string(config, "tools", "term_cmd", ""));
933 if (!EMPTY(cmd))
935 tmp_string = cmd;
936 #ifdef G_OS_WIN32
937 if (strstr(cmd, "cmd.exe"))
938 cmd = g_strconcat(cmd, " /Q /C %c", NULL);
939 else
940 cmd = g_strconcat(cmd, " %c", NULL);
941 #else
942 cmd = g_strconcat(cmd, " -e \"/bin/sh %c\"", NULL);
943 #endif
944 g_free(tmp_string);
946 else
947 SETPTR(cmd, g_strdup(GEANY_DEFAULT_TOOLS_TERMINAL));
949 tool_prefs.term_cmd = cmd;
950 tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
951 tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
953 tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
955 /* printing */
956 tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
958 if (!EMPTY(tmp_string2))
960 #ifdef G_OS_WIN32
961 tmp_string = g_strconcat(GEANY_DEFAULT_TOOLS_PRINTCMD, " \"%f\"", NULL);
962 #else
963 tmp_string = g_strconcat(GEANY_DEFAULT_TOOLS_PRINTCMD, " '%f'", NULL);
964 #endif
966 else
968 tmp_string = g_strdup("");
971 printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
972 g_free(tmp_string);
973 g_free(tmp_string2);
975 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
976 printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
977 printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
978 printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
979 printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
980 printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
982 /* read stash prefs */
983 settings_action(config, SETTING_READ);
985 /* build menu
986 * after stash prefs as it uses some of them */
987 build_set_group_count(GEANY_GBG_FT, build_menu_prefs.number_ft_menu_items);
988 build_set_group_count(GEANY_GBG_NON_FT, build_menu_prefs.number_non_ft_menu_items);
989 build_set_group_count(GEANY_GBG_EXEC, build_menu_prefs.number_exec_menu_items);
990 build_load_menu(config, GEANY_BCS_PREF, NULL);
994 static void load_ui_prefs(GKeyFile *config)
996 gint *geo;
997 gsize geo_len;
999 ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
1000 ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
1001 ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
1002 ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
1003 ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
1004 ui_prefs.custom_commands_labels = g_key_file_get_string_list(config, PACKAGE, "custom_commands_labels", NULL, NULL);
1006 /* sanitize custom commands labels */
1007 if (ui_prefs.custom_commands || ui_prefs.custom_commands_labels)
1009 guint i;
1010 guint cc_len = ui_prefs.custom_commands ? g_strv_length(ui_prefs.custom_commands) : 0;
1011 guint cc_labels_len = ui_prefs.custom_commands_labels ? g_strv_length(ui_prefs.custom_commands_labels) : 0;
1013 /* not enough items, resize and fill */
1014 if (cc_labels_len < cc_len)
1016 ui_prefs.custom_commands_labels = g_realloc(ui_prefs.custom_commands_labels,
1017 (cc_len + 1) * sizeof *ui_prefs.custom_commands_labels);
1018 for (i = cc_labels_len; i < cc_len; i++)
1019 ui_prefs.custom_commands_labels[i] = g_strdup("");
1020 ui_prefs.custom_commands_labels[cc_len] = NULL;
1022 /* too many items, cut off */
1023 else if (cc_labels_len > cc_len)
1025 for (i = cc_len; i < cc_labels_len; i++)
1027 g_free(ui_prefs.custom_commands_labels[i]);
1028 ui_prefs.custom_commands_labels[i] = NULL;
1033 scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
1034 _("Type here what you want, use it as a notice/scratch board"));
1035 scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
1037 geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", &geo_len, NULL);
1038 if (! geo || geo_len < 5)
1040 ui_prefs.geometry[0] = -1;
1041 ui_prefs.geometry[1] = -1;
1042 ui_prefs.geometry[2] = -1;
1043 ui_prefs.geometry[3] = -1;
1044 ui_prefs.geometry[4] = 0;
1046 else
1048 /* don't use insane values but when main windows was maximized last time, pos might be
1049 * negative (due to differences in root window and window decorations) */
1050 /* quitting when minimized can make pos -32000, -32000 on Windows! */
1051 ui_prefs.geometry[0] = MAX(-1, geo[0]);
1052 ui_prefs.geometry[1] = MAX(-1, geo[1]);
1053 ui_prefs.geometry[2] = MAX(-1, geo[2]);
1054 ui_prefs.geometry[3] = MAX(-1, geo[3]);
1055 ui_prefs.geometry[4] = geo[4] != 0;
1057 hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
1058 vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
1059 (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
1060 (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
1062 g_free(geo);
1067 * Save current session in default configuration file
1069 void configuration_save_default_session(void)
1071 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1072 gchar *data;
1073 GKeyFile *config = g_key_file_new();
1075 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1077 if (cl_options.load_session)
1078 configuration_save_session_files(config);
1080 /* write the file */
1081 data = g_key_file_to_data(config, NULL, NULL);
1082 utils_write_file(configfile, data);
1083 g_free(data);
1085 g_key_file_free(config);
1086 g_free(configfile);
1090 void configuration_clear_default_session(void)
1092 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1093 gchar *data;
1094 GKeyFile *config = g_key_file_new();
1096 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1098 if (cl_options.load_session)
1099 remove_session_files(config);
1101 /* write the file */
1102 data = g_key_file_to_data(config, NULL, NULL);
1103 utils_write_file(configfile, data);
1104 g_free(data);
1106 g_key_file_free(config);
1107 g_free(configfile);
1112 * Only reload the session part of the default configuration
1114 void configuration_reload_default_session(void)
1116 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1117 GKeyFile *config = g_key_file_new();
1119 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1120 g_free(configfile);
1122 configuration_load_session_files(config, FALSE);
1124 g_key_file_free(config);
1128 gboolean configuration_load(void)
1130 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1131 GKeyFile *config = g_key_file_new();
1133 if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
1134 { /* config file does not (yet) exist, so try to load a global config file which may be */
1135 /* created by distributors */
1136 geany_debug("No user config file found, trying to use global configuration.");
1137 SETPTR(configfile, g_build_filename(app->datadir, "geany.conf", NULL));
1139 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1140 g_free(configfile);
1142 load_dialog_prefs(config);
1143 load_ui_prefs(config);
1144 project_load_prefs(config);
1145 configuration_load_session_files(config, TRUE);
1147 /* this signal can be used e.g. to delay building UI elements until settings have been read */
1148 g_signal_emit_by_name(geany_object, "load-settings", config);
1150 g_key_file_free(config);
1151 return TRUE;
1155 static gboolean open_session_file(gchar **tmp, guint len)
1157 guint pos;
1158 const gchar *ft_name;
1159 gchar *locale_filename;
1160 gchar *unescaped_filename;
1161 const gchar *encoding;
1162 gint indent_type;
1163 gboolean ro, auto_indent, line_wrapping;
1164 /** TODO when we have a global pref for line breaking, use its value */
1165 gboolean line_breaking = FALSE;
1166 gboolean ret = FALSE;
1168 pos = atoi(tmp[0]);
1169 ft_name = tmp[1];
1170 ro = atoi(tmp[2]);
1171 if (isdigit(tmp[3][0]))
1173 encoding = encodings_get_charset_from_index(atoi(tmp[3]));
1175 else
1177 encoding = &(tmp[3][1]);
1179 indent_type = atoi(tmp[4]);
1180 auto_indent = atoi(tmp[5]);
1181 line_wrapping = atoi(tmp[6]);
1182 /* try to get the locale equivalent for the filename */
1183 unescaped_filename = g_uri_unescape_string(tmp[7], NULL);
1184 locale_filename = utils_get_locale_from_utf8(unescaped_filename);
1186 if (len > 8)
1187 line_breaking = atoi(tmp[8]);
1189 if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR))
1191 GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
1192 GeanyDocument *doc = document_open_file_full(
1193 NULL, locale_filename, pos, ro, ft, encoding);
1195 if (doc)
1197 gint indent_width = doc->editor->indent_width;
1199 if (len > 9)
1200 indent_width = atoi(tmp[9]);
1201 editor_set_indent(doc->editor, indent_type, indent_width);
1202 editor_set_line_wrapping(doc->editor, line_wrapping);
1203 doc->editor->line_breaking = line_breaking;
1204 doc->editor->auto_indent = auto_indent;
1205 ret = TRUE;
1208 else
1210 geany_debug("Could not find file '%s'.", unescaped_filename);
1213 g_free(locale_filename);
1214 g_free(unescaped_filename);
1215 return ret;
1219 /* Open session files
1220 * Note: notebook page switch handler and adding to recent files list is always disabled
1221 * for all files opened within this function */
1222 void configuration_open_files(void)
1224 gint i;
1225 gboolean failure = FALSE;
1227 /* necessary to set it to TRUE for project session support */
1228 main_status.opening_session_files = TRUE;
1230 i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
1231 while (TRUE)
1233 gchar **tmp = g_ptr_array_index(session_files, i);
1234 guint len;
1236 if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
1238 if (! open_session_file(tmp, len))
1239 failure = TRUE;
1241 g_strfreev(tmp);
1243 if (file_prefs.tab_order_ltr)
1245 i++;
1246 if (i >= (gint)session_files->len)
1247 break;
1249 else
1251 i--;
1252 if (i < 0)
1253 break;
1257 g_ptr_array_free(session_files, TRUE);
1258 session_files = NULL;
1260 if (failure)
1261 ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
1262 else
1264 /* explicitly trigger a notebook page switch after unsetting main_status.opening_session_files
1265 * for callbacks to run (and update window title, encoding settings, and so on) */
1266 gint n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1267 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
1268 gint target_page = session_notebook_page >= 0 ? session_notebook_page : cur_page;
1270 /* if target page is current page, switch to another page first to really trigger an event */
1271 if (target_page == cur_page && n_pages > 0)
1272 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), (cur_page + 1) % n_pages);
1274 main_status.opening_session_files = FALSE;
1275 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), target_page);
1277 main_status.opening_session_files = FALSE;
1281 /* set some settings which are already read from the config file, but need other things, like the
1282 * realisation of the main window */
1283 void configuration_apply_settings(void)
1285 if (scribble_text)
1286 { /* update the scribble widget, because now it's realized */
1287 GtkTextIter iter;
1288 GtkTextBuffer *buffer =
1289 gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
1291 gtk_text_buffer_set_text(buffer, scribble_text, -1);
1292 gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
1293 gtk_text_buffer_place_cursor(buffer, &iter);
1295 g_free(scribble_text);
1297 /* set the position of the hpaned and vpaned */
1298 if (prefs.save_winpos)
1300 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
1301 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
1304 /* set fullscreen after initial draw so that returning to normal view is the right size.
1305 * fullscreen mode is disabled by default, so act only if it is true */
1306 if (ui_prefs.fullscreen)
1308 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
1309 ui_prefs.fullscreen = TRUE;
1310 ui_set_fullscreen();
1313 msgwin_show_hide_tabs();
1317 void configuration_init(void)
1319 keyfile_groups = g_ptr_array_new();
1320 pref_groups = g_ptr_array_new();
1321 init_pref_groups();
1325 void configuration_finalize(void)
1327 guint i;
1328 StashGroup *group;
1330 foreach_ptr_array(group, i, keyfile_groups)
1331 stash_group_free(group);
1333 g_ptr_array_free(keyfile_groups, TRUE);
1334 g_ptr_array_free(pref_groups, TRUE);