waf: The GEANY_*_SYMBOL flags also need to be defined for C++
[geany-mirror.git] / src / keyfile.c
blob1cffe0090efdd409a5e1404bb4d8b8e51a29f55b
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 #elif defined(__APPLE__)
78 #define GEANY_DEFAULT_TOOLS_TERMINAL "open -a terminal %c"
79 #else
80 #define GEANY_DEFAULT_TOOLS_TERMINAL "xterm -e \"/bin/sh %c\""
81 #endif
82 #ifdef __APPLE__
83 #define GEANY_DEFAULT_TOOLS_BROWSER "open -a safari"
84 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Helvetica Medium 12"
85 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Helvetica Medium 12"
86 #define GEANY_DEFAULT_FONT_EDITOR "Menlo Medium 12"
87 #else
88 #define GEANY_DEFAULT_TOOLS_BROWSER "firefox"
89 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Sans 9"
90 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Sans 9"
91 #define GEANY_DEFAULT_FONT_EDITOR "Monospace 10"
92 #endif
93 #define GEANY_DEFAULT_TOOLS_PRINTCMD "lpr"
94 #define GEANY_DEFAULT_TOOLS_GREP "grep"
95 #define GEANY_DEFAULT_MRU_LENGTH 10
96 #define GEANY_TOGGLE_MARK "~ "
97 #define GEANY_MAX_AUTOCOMPLETE_WORDS 30
98 #define GEANY_MAX_SYMBOLS_UPDATE_FREQ 250
99 #define GEANY_DEFAULT_FILETYPE_REGEX "-\\*-\\s*([^\\s]+)\\s*-\\*-"
102 static gchar *scribble_text = NULL;
103 static gint scribble_pos = -1;
104 static GPtrArray *session_files = NULL;
105 static gint session_notebook_page;
106 static gint hpan_position;
107 static gint vpan_position;
108 static const gchar atomic_file_saving_key[] = "use_atomic_file_saving";
110 static GPtrArray *keyfile_groups = NULL;
112 GPtrArray *pref_groups = NULL;
114 static struct
116 gint number_ft_menu_items;
117 gint number_non_ft_menu_items;
118 gint number_exec_menu_items;
120 build_menu_prefs;
123 /* The group will be free'd on quitting.
124 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
125 void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
127 g_ptr_array_add(keyfile_groups, group);
129 if (for_prefs_dialog)
130 g_ptr_array_add(pref_groups, group);
134 /* The group will be free'd on quitting. */
135 void configuration_add_various_pref_group(struct StashGroup *group)
137 configuration_add_pref_group(group, TRUE);
138 stash_group_set_various(group, TRUE);
142 static void init_pref_groups(void)
144 StashGroup *group;
146 group = stash_group_new(PACKAGE);
147 configuration_add_pref_group(group, TRUE);
148 stash_group_add_entry(group, &prefs.default_open_path,
149 "default_open_path", "", "startup_path_entry");
151 stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
152 "cmdline_new_files", TRUE, "check_cmdline_new_files");
154 stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
155 "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
156 stash_group_add_toggle_button(group, &file_prefs.tab_close_switch_to_mru,
157 "tab_close_switch_to_mru", FALSE, "check_tab_close_switch_to_mru");
158 stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
159 stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
160 "sidebar_pos", GTK_POS_LEFT,
161 "radio_sidebar_left", GTK_POS_LEFT,
162 "radio_sidebar_right", GTK_POS_RIGHT,
163 NULL);
164 stash_group_add_radio_buttons(group, &interface_prefs.msgwin_orientation,
165 "msgwin_orientation", GTK_ORIENTATION_VERTICAL,
166 "radio_msgwin_vertical", GTK_ORIENTATION_VERTICAL,
167 "radio_msgwin_horizontal", GTK_ORIENTATION_HORIZONTAL,
168 NULL);
170 /* editor display */
171 stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
172 "highlighting_invert_all", FALSE, "check_highlighting_invert");
174 stash_group_add_toggle_button(group, &search_prefs.use_current_word,
175 "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
177 /* editor */
178 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
179 "check_detect_indent", FALSE, "check_detect_indent_type");
180 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_width,
181 "detect_indent_width", FALSE, "check_detect_indent_width");
182 stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
183 "use_tab_to_indent", TRUE, "check_tab_key_indents");
184 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
185 "pref_editor_tab_width", 4, "spin_indent_width");
186 stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
187 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
188 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
189 "indent_type", GEANY_INDENT_TYPE_TABS,
190 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
191 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
192 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
193 NULL);
194 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
195 "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
196 "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
197 "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
198 "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
199 NULL);
200 stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
201 "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
202 stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
203 "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
204 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
205 "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
206 "spin_autocompletion_max_entries");
207 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_update_freq,
208 "autocompletion_update_freq", GEANY_MAX_SYMBOLS_UPDATE_FREQ, "spin_symbol_update_freq");
209 stash_group_add_string(group, &editor_prefs.color_scheme,
210 "color_scheme", NULL);
212 /* files */
213 stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
214 "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
215 stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
216 "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
218 /* various geany prefs */
219 group = stash_group_new(PACKAGE);
220 configuration_add_various_pref_group(group);
222 stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
223 "show_editor_scrollbars", TRUE);
224 stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
225 "brace_match_ltgt", FALSE);
226 stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
227 "use_gtk_word_boundaries", TRUE);
228 stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
229 "complete_snippets_whilst_editing", FALSE);
230 stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
231 atomic_file_saving_key, FALSE);
232 stash_group_add_boolean(group, &file_prefs.gio_unsafe_save_backup,
233 "gio_unsafe_save_backup", FALSE);
234 stash_group_add_boolean(group, &file_prefs.use_gio_unsafe_file_saving,
235 "use_gio_unsafe_file_saving", TRUE);
236 stash_group_add_boolean(group, &file_prefs.keep_edit_history_on_reload,
237 "keep_edit_history_on_reload", TRUE);
238 /* for backwards-compatibility */
239 stash_group_add_integer(group, &editor_prefs.indentation->hard_tab_width,
240 "indent_hard_tab_width", 8);
241 stash_group_add_integer(group, (gint*)&search_prefs.find_selection_type,
242 "find_selection_type", GEANY_FIND_SEL_CURRENT_WORD);
243 stash_group_add_string(group, &file_prefs.extract_filetype_regex,
244 "extract_filetype_regex", GEANY_DEFAULT_FILETYPE_REGEX);
245 stash_group_add_boolean(group, &search_prefs.replace_and_find_by_default,
246 "replace_and_find_by_default", TRUE);
248 /* Note: Interface-related various prefs are in ui_init_prefs() */
250 /* various build-menu prefs */
251 group = stash_group_new("build-menu");
252 configuration_add_various_pref_group(group);
254 stash_group_add_integer(group, &build_menu_prefs.number_ft_menu_items,
255 "number_ft_menu_items", 0);
256 stash_group_add_integer(group, &build_menu_prefs.number_non_ft_menu_items,
257 "number_non_ft_menu_items", 0);
258 stash_group_add_integer(group, &build_menu_prefs.number_exec_menu_items,
259 "number_exec_menu_items", 0);
263 typedef enum SettingAction
265 SETTING_READ,
266 SETTING_WRITE
268 SettingAction;
270 static void settings_action(GKeyFile *config, SettingAction action)
272 guint i;
273 StashGroup *group;
275 foreach_ptr_array(group, i, keyfile_groups)
277 switch (action)
279 case SETTING_READ:
280 stash_group_load_from_key_file(group, config); break;
281 case SETTING_WRITE:
282 stash_group_save_to_key_file(group, config); break;
288 static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
290 gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
291 guint i;
293 for (i = 0; i < file_prefs.mru_length; i++)
295 if (! g_queue_is_empty(queue))
297 /* copy the values, this is necessary when this function is called from the
298 * preferences dialog or when quitting is canceled to keep the queue intact */
299 recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
301 else
303 recent_files[i] = NULL;
304 break;
307 /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
308 recent_files[file_prefs.mru_length] = NULL;
309 g_key_file_set_string_list(config, "files", key,
310 (const gchar**)recent_files, file_prefs.mru_length);
311 g_strfreev(recent_files);
315 static gchar *get_session_file_string(GeanyDocument *doc)
317 gchar *fname;
318 gchar *locale_filename;
319 gchar *escaped_filename;
320 GeanyFiletype *ft = doc->file_type;
322 if (ft == NULL) /* can happen when saving a new file when quitting */
323 ft = filetypes[GEANY_FILETYPES_NONE];
325 locale_filename = utils_get_locale_from_utf8(doc->file_name);
326 escaped_filename = g_uri_escape_string(locale_filename, NULL, TRUE);
328 fname = g_strdup_printf("%d;%s;%d;E%s;%d;%d;%d;%s;%d;%d",
329 sci_get_current_position(doc->editor->sci),
330 ft->name,
331 doc->readonly,
332 doc->encoding,
333 doc->editor->indent_type,
334 doc->editor->auto_indent,
335 doc->editor->line_wrapping,
336 escaped_filename,
337 doc->editor->line_breaking,
338 doc->editor->indent_width);
339 g_free(escaped_filename);
340 g_free(locale_filename);
341 return fname;
345 static void remove_session_files(GKeyFile *config)
347 gchar **ptr;
348 gchar **keys = g_key_file_get_keys(config, "files", NULL, NULL);
350 foreach_strv(ptr, keys)
352 if (g_str_has_prefix(*ptr, "FILE_NAME_"))
353 g_key_file_remove_key(config, "files", *ptr, NULL);
355 g_strfreev(keys);
359 void configuration_save_session_files(GKeyFile *config)
361 gint npage;
362 gchar entry[16];
363 guint i = 0, j = 0, max;
364 GeanyDocument *doc;
366 npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
367 g_key_file_set_integer(config, "files", "current_page", npage);
369 // clear existing entries first as they might not all be overwritten
370 remove_session_files(config);
372 /* store the filenames in the notebook tab order to reopen them the next time */
373 max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
374 for (i = 0; i < max; i++)
376 doc = document_get_from_page(i);
377 if (doc != NULL && doc->real_path != NULL)
379 gchar *fname;
381 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
382 fname = get_session_file_string(doc);
383 g_key_file_set_string(config, "files", entry, fname);
384 g_free(fname);
385 j++;
389 #ifdef HAVE_VTE
390 if (vte_info.have_vte)
392 vte_get_working_directory(); /* refresh vte_info.dir */
393 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
395 #endif
399 static void save_dialog_prefs(GKeyFile *config)
401 /* new settings should be added in init_pref_groups() */
402 settings_action(config, SETTING_WRITE);
404 /* Some of the key names are not consistent, but this is for backwards compatibility */
406 /* general */
407 g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
408 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_session", project_prefs.project_session);
409 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
410 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
411 g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
412 g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
413 g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
414 g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
415 g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
417 /* interface */
418 g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
419 g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
420 g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
421 g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
422 g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
423 g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
424 g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
425 g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
426 g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
427 g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
428 g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
429 g_key_file_set_boolean(config, PACKAGE, "use_native_windows_dialogs", interface_prefs.use_native_windows_dialogs);
431 /* display */
432 g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
433 g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
434 g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
435 g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
436 g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
437 g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_enabled);
438 g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_type);
439 g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_column);
440 g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
442 /* editor */
443 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
444 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
445 g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
446 g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
447 g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
448 g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
449 g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
450 g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
451 g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
452 g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
453 g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
454 g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
455 g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
456 g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
457 g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
458 g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
459 g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
461 /* files */
462 g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
463 if (file_prefs.default_open_encoding == -1)
464 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
465 else
466 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
467 g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
468 g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
469 g_key_file_set_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", file_prefs.ensure_convert_new_lines);
470 g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
471 g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
473 /* toolbar */
474 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
475 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
476 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
477 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
478 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
479 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
481 /* templates */
482 g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
483 g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
484 g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
485 g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
486 g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
487 g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
488 g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
489 g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
491 /* tools settings */
492 g_key_file_set_string(config, "tools", "terminal_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
493 g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
494 g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
495 g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
497 /* build menu */
498 build_save_menu(config, NULL, GEANY_BCS_PREF);
500 /* printing */
501 g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
502 g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
503 g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
504 g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
505 g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
506 g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
507 g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
509 /* VTE */
510 #ifdef HAVE_VTE
511 g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
512 if (vte_info.have_vte)
514 gchar *tmp_string;
516 if (!g_key_file_has_key(config, "VTE", "emulation", NULL)) /* hidden */
517 g_key_file_set_string(config, "VTE", "emulation", vc->emulation);
518 if (!g_key_file_has_key(config, "VTE", "send_selection_unsafe", NULL)) /* hidden */
519 g_key_file_set_boolean(config, "VTE", "send_selection_unsafe",
520 vc->send_selection_unsafe);
521 if (!g_key_file_has_key(config, "VTE", "send_cmd_prefix", NULL)) /* hidden */
522 g_key_file_set_string(config, "VTE", "send_cmd_prefix", vc->send_cmd_prefix);
523 g_key_file_set_string(config, "VTE", "font", vc->font);
524 g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
525 g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
526 g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
527 g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
528 g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
529 g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
530 g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
531 g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
532 g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
533 g_key_file_set_string(config, "VTE", "font", vc->font);
534 g_key_file_set_string(config, "VTE", "image", vc->image);
535 g_key_file_set_string(config, "VTE", "shell", vc->shell);
536 tmp_string = utils_get_hex_from_color(&vc->colour_fore);
537 g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
538 g_free(tmp_string);
539 tmp_string = utils_get_hex_from_color(&vc->colour_back);
540 g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
541 g_free(tmp_string);
543 #endif
547 static void save_ui_prefs(GKeyFile *config)
549 g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
550 g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
551 g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
552 g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
554 /* get the text from the scribble textview */
556 GtkTextBuffer *buffer;
557 GtkTextIter start, end, iter;
558 GtkTextMark *mark;
560 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
561 gtk_text_buffer_get_bounds(buffer, &start, &end);
562 scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
563 g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
564 g_free(scribble_text);
566 mark = gtk_text_buffer_get_insert(buffer);
567 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
568 scribble_pos = gtk_text_iter_get_offset(&iter);
569 g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
572 if (prefs.save_winpos)
574 GdkWindowState wstate;
576 g_key_file_set_integer(config, PACKAGE, "treeview_position",
577 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
578 g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
579 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
581 gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
582 gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
583 wstate = gdk_window_get_state(gtk_widget_get_window(main_widgets.window));
584 ui_prefs.geometry[4] = (wstate & GDK_WINDOW_STATE_MAXIMIZED) ? 1 : 0;
585 g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
588 g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
589 if (ui_prefs.custom_commands != NULL)
591 g_key_file_set_string_list(config, PACKAGE, "custom_commands",
592 (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
593 g_key_file_set_string_list(config, PACKAGE, "custom_commands_labels",
594 (const gchar**) ui_prefs.custom_commands_labels, g_strv_length(ui_prefs.custom_commands_labels));
599 void configuration_save(void)
601 GKeyFile *config = g_key_file_new();
602 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
603 gchar *data;
605 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
607 /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
608 g_signal_emit_by_name(geany_object, "save-settings", config);
610 save_dialog_prefs(config);
611 save_ui_prefs(config);
612 project_save_prefs(config); /* save project filename, etc. */
613 save_recent_files(config, ui_prefs.recent_queue, "recent_files");
614 save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
616 if (cl_options.load_session)
617 configuration_save_session_files(config);
618 #ifdef HAVE_VTE
619 else if (vte_info.have_vte)
621 vte_get_working_directory(); /* refresh vte_info.dir */
622 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
624 #endif
626 /* write the file */
627 data = g_key_file_to_data(config, NULL, NULL);
628 utils_write_file(configfile, data);
629 g_free(data);
631 g_key_file_free(config);
632 g_free(configfile);
636 static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
638 gchar **recent_files;
639 gsize i, len = 0;
641 recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
642 if (recent_files != NULL)
644 for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
646 gchar *filename = g_strdup(recent_files[i]);
647 g_queue_push_tail(queue, filename);
649 g_strfreev(recent_files);
655 * Load session list from the given keyfile, and store it in the global
656 * session_files variable for later file loading
657 * */
658 void configuration_load_session_files(GKeyFile *config, gboolean read_recent_files)
660 guint i;
661 gboolean have_session_files;
662 gchar entry[16];
663 gchar **tmp_array;
664 GError *error = NULL;
666 session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
668 if (read_recent_files)
670 load_recent_files(config, ui_prefs.recent_queue, "recent_files");
671 load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
674 /* the project may load another list than the main setting */
675 if (session_files != NULL)
677 foreach_ptr_array(tmp_array, i, session_files)
678 g_strfreev(tmp_array);
679 g_ptr_array_free(session_files, TRUE);
682 session_files = g_ptr_array_new();
683 have_session_files = TRUE;
684 i = 0;
685 while (have_session_files)
687 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
688 tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
689 if (! tmp_array || error)
691 g_error_free(error);
692 error = NULL;
693 have_session_files = FALSE;
695 g_ptr_array_add(session_files, tmp_array);
696 i++;
699 #ifdef HAVE_VTE
700 /* BUG: after loading project at startup, closing project doesn't restore old VTE path */
701 if (vte_info.have_vte)
703 gchar *tmp_string = utils_get_setting_string(config, "VTE", "last_dir", NULL);
704 vte_cwd(tmp_string,TRUE);
705 g_free(tmp_string);
707 #endif
711 #ifdef HAVE_VTE
712 static void get_setting_color(GKeyFile *config, const gchar *section, const gchar *key,
713 GdkColor *color, const gchar *default_color)
715 gchar *str = utils_get_setting_string(config, section, key, NULL);
716 if (str == NULL || ! utils_parse_color(str, color))
717 utils_parse_color(default_color, color);
718 g_free(str);
720 #endif
723 /* note: new settings should be added in init_pref_groups() */
724 static void load_dialog_prefs(GKeyFile *config)
726 gchar *tmp_string, *tmp_string2;
727 const gchar *default_charset = NULL;
728 gchar *cmd;
730 /* compatibility with Geany 0.20 */
731 if (!g_key_file_has_key(config, PACKAGE, atomic_file_saving_key, NULL))
733 g_key_file_set_boolean(config, PACKAGE, atomic_file_saving_key,
734 utils_get_setting_boolean(config, PACKAGE, "use_safe_file_saving", FALSE));
737 /* compatibility with Geany 0.21 */
739 gboolean suppress_search_dialogs = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_search_dialogs", FALSE);
741 if (!g_key_file_has_key(config, "search", "pref_search_always_wrap", NULL))
742 g_key_file_set_boolean(config, "search", "pref_search_always_wrap", suppress_search_dialogs);
744 if (!g_key_file_has_key(config, "search", "pref_search_hide_find_dialog", NULL))
745 g_key_file_set_boolean(config, "search", "pref_search_hide_find_dialog", suppress_search_dialogs);
748 /* read stash prefs */
749 settings_action(config, SETTING_READ);
751 /* general */
752 prefs.confirm_exit = utils_get_setting_boolean(config, PACKAGE, "pref_main_confirm_exit", FALSE);
753 prefs.suppress_status_messages = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_status_messages", FALSE);
754 prefs.load_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_load_session", TRUE);
755 project_prefs.project_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_session", TRUE);
756 project_prefs.project_file_in_basedir = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", FALSE);
757 prefs.save_winpos = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_winpos", TRUE);
758 prefs.beep_on_errors = utils_get_setting_boolean(config, PACKAGE, "beep_on_errors", TRUE);
759 prefs.switch_to_status = utils_get_setting_boolean(config, PACKAGE, "switch_msgwin_pages", FALSE);
760 prefs.auto_focus = utils_get_setting_boolean(config, PACKAGE, "auto_focus", FALSE);
762 /* interface */
763 interface_prefs.tab_pos_editor = utils_get_setting_integer(config, PACKAGE, "tab_pos_editor", GTK_POS_TOP);
764 interface_prefs.tab_pos_msgwin = utils_get_setting_integer(config, PACKAGE, "tab_pos_msgwin",GTK_POS_LEFT);
765 interface_prefs.sidebar_symbol_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_symbol_visible", TRUE);
766 interface_prefs.sidebar_openfiles_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_openfiles_visible", TRUE);
767 interface_prefs.statusbar_visible = utils_get_setting_boolean(config, PACKAGE, "statusbar_visible", TRUE);
768 file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
769 file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
770 interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
771 file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
772 interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
773 interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
774 interface_prefs.msgwin_font = utils_get_setting_string(config, PACKAGE, "msgwin_font", GEANY_DEFAULT_FONT_MSG_WINDOW);
775 interface_prefs.use_native_windows_dialogs = utils_get_setting_boolean(config, PACKAGE, "use_native_windows_dialogs", FALSE);
777 /* display, editor */
778 editor_prefs.long_line_enabled = utils_get_setting_boolean(config, PACKAGE, "long_line_enabled", TRUE);
779 editor_prefs.long_line_type = utils_get_setting_integer(config, PACKAGE, "long_line_type", 0);
780 if (editor_prefs.long_line_type == 2) /* backward compatibility */
782 editor_prefs.long_line_type = 0;
783 editor_prefs.long_line_enabled = FALSE;
785 editor_prefs.long_line_color = utils_get_setting_string(config, PACKAGE, "long_line_color", "#C2EBC2");
786 editor_prefs.long_line_column = utils_get_setting_integer(config, PACKAGE, "long_line_column", 72);
787 editor_prefs.symbolcompletion_min_chars = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_min_chars", GEANY_MIN_SYMBOLLIST_CHARS);
788 editor_prefs.symbolcompletion_max_height = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_max_height", GEANY_MAX_SYMBOLLIST_HEIGHT);
789 editor_prefs.line_wrapping = utils_get_setting_boolean(config, PACKAGE, "line_wrapping", FALSE); /* default is off for better performance */
790 editor_prefs.use_indicators = utils_get_setting_boolean(config, PACKAGE, "use_indicators", TRUE);
791 editor_prefs.show_indent_guide = utils_get_setting_boolean(config, PACKAGE, "show_indent_guide", FALSE);
792 editor_prefs.show_white_space = utils_get_setting_boolean(config, PACKAGE, "show_white_space", FALSE);
793 editor_prefs.show_line_endings = utils_get_setting_boolean(config, PACKAGE, "show_line_endings", FALSE);
794 editor_prefs.scroll_stop_at_last_line = utils_get_setting_boolean(config, PACKAGE, "scroll_stop_at_last_line", TRUE);
795 editor_prefs.auto_close_xml_tags = utils_get_setting_boolean(config, PACKAGE, "auto_close_xml_tags", TRUE);
796 editor_prefs.complete_snippets = utils_get_setting_boolean(config, PACKAGE, "complete_snippets", TRUE);
797 editor_prefs.auto_complete_symbols = utils_get_setting_boolean(config, PACKAGE, "auto_complete_symbols", TRUE);
798 editor_prefs.folding = utils_get_setting_boolean(config, PACKAGE, "use_folding", TRUE);
799 editor_prefs.unfold_all_children = utils_get_setting_boolean(config, PACKAGE, "unfold_all_children", FALSE);
800 editor_prefs.show_markers_margin = utils_get_setting_boolean(config, PACKAGE, "show_markers_margin", TRUE);
801 editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
802 editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
803 editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
804 editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
805 editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
806 editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
807 editor_prefs.comment_toggle_mark = utils_get_setting_string(config, PACKAGE, "comment_toggle_mark", GEANY_TOGGLE_MARK);
808 editor_prefs.autoclose_chars = utils_get_setting_integer(config, PACKAGE, "autoclose_chars", 0);
810 /* Files
811 * use current locale encoding as default for new files (should be UTF-8 in most cases) */
812 g_get_charset(&default_charset);
813 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_new_encoding",
814 default_charset);
815 if (tmp_string)
817 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
818 if (enc != NULL)
819 file_prefs.default_new_encoding = enc->idx;
820 else
821 file_prefs.default_new_encoding = GEANY_ENCODING_UTF_8;
823 g_free(tmp_string);
825 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_open_encoding",
826 "none");
827 if (tmp_string)
829 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
830 if (enc != NULL)
831 file_prefs.default_open_encoding = enc->idx;
832 else
833 file_prefs.default_open_encoding = -1;
835 g_free(tmp_string);
837 file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
838 file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
839 file_prefs.ensure_convert_new_lines = utils_get_setting_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", FALSE);
840 file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
841 file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
843 /* toolbar */
844 toolbar_prefs.visible = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_show", TRUE);
845 toolbar_prefs.append_to_menu = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", FALSE);
847 toolbar_prefs.use_gtk_default_style = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", TRUE);
848 if (! toolbar_prefs.use_gtk_default_style)
849 toolbar_prefs.icon_style = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_style", GTK_TOOLBAR_ICONS);
851 toolbar_prefs.use_gtk_default_icon = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", TRUE);
852 if (! toolbar_prefs.use_gtk_default_icon)
853 toolbar_prefs.icon_size = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_size", GTK_ICON_SIZE_LARGE_TOOLBAR);
856 /* VTE */
857 #ifdef HAVE_VTE
858 vte_info.load_vte = utils_get_setting_boolean(config, "VTE", "load_vte", TRUE);
859 if (vte_info.load_vte)
861 struct passwd *pw = getpwuid(getuid());
862 const gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
864 vc = g_new0(VteConfig, 1);
865 vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
866 if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
867 /* last dir is not set, fallback to user's home directory */
868 SETPTR(vte_info.dir, g_strdup(pw->pw_dir));
869 else if (vte_info.dir == NULL && pw == NULL)
870 /* fallback to root */
871 vte_info.dir = g_strdup("/");
873 vc->emulation = utils_get_setting_string(config, "VTE", "emulation", "xterm");
874 vc->send_selection_unsafe = utils_get_setting_boolean(config, "VTE",
875 "send_selection_unsafe", FALSE);
876 vc->image = utils_get_setting_string(config, "VTE", "image", "");
877 vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
878 vc->font = utils_get_setting_string(config, "VTE", "font", "Monospace 10");
879 vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
880 vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
881 vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", TRUE);
882 vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
883 vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
884 vc->send_cmd_prefix = utils_get_setting_string(config, "VTE", "send_cmd_prefix", "");
885 vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
886 vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
887 vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
888 vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
889 get_setting_color(config, "VTE", "colour_fore", &vc->colour_fore, "#ffffff");
890 get_setting_color(config, "VTE", "colour_back", &vc->colour_back, "#000000");
892 #endif
893 /* templates */
894 template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
895 template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
896 tmp_string = utils_get_initials(template_prefs.developer);
897 template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
898 g_free(tmp_string);
900 template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
902 tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), g_get_host_name());
903 template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
904 g_free(tmp_string);
905 template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", GEANY_TEMPLATES_FORMAT_YEAR);
906 template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", GEANY_TEMPLATES_FORMAT_DATE);
907 template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", GEANY_TEMPLATES_FORMAT_DATETIME);
909 /* tools */
910 cmd = utils_get_setting_string(config, "tools", "terminal_cmd", "");
911 if (EMPTY(cmd))
913 SETPTR(cmd, utils_get_setting_string(config, "tools", "term_cmd", ""));
914 if (!EMPTY(cmd))
916 tmp_string = cmd;
917 #ifdef G_OS_WIN32
918 if (strstr(cmd, "cmd.exe"))
919 cmd = g_strconcat(cmd, " /Q /C %c", NULL);
920 else
921 cmd = g_strconcat(cmd, " %c", NULL);
922 #else
923 cmd = g_strconcat(cmd, " -e \"/bin/sh %c\"", NULL);
924 #endif
925 g_free(tmp_string);
927 else
928 SETPTR(cmd, g_strdup(GEANY_DEFAULT_TOOLS_TERMINAL));
930 tool_prefs.term_cmd = cmd;
931 tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
932 tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
934 tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
936 /* build menu */
937 build_set_group_count(GEANY_GBG_FT, build_menu_prefs.number_ft_menu_items);
938 build_set_group_count(GEANY_GBG_NON_FT, build_menu_prefs.number_non_ft_menu_items);
939 build_set_group_count(GEANY_GBG_EXEC, build_menu_prefs.number_exec_menu_items);
940 build_load_menu(config, GEANY_BCS_PREF, NULL);
942 /* printing */
943 tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
944 #ifdef G_OS_WIN32
945 if (!EMPTY(tmp_string2))
947 /* single quote paths on Win32 for g_spawn_command_line_async */
948 tmp_string = g_strconcat("'", tmp_string2, "' '%f'", NULL);
950 else
952 tmp_string = g_strdup("");
954 #else
955 tmp_string = g_strconcat(tmp_string2, " %f", NULL);
956 #endif
957 printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
958 g_free(tmp_string);
959 g_free(tmp_string2);
961 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
962 printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
963 printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
964 printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
965 printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
966 printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
970 static void load_ui_prefs(GKeyFile *config)
972 gint *geo;
973 gsize geo_len;
975 ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
976 ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
977 ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
978 ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
979 ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
980 ui_prefs.custom_commands_labels = g_key_file_get_string_list(config, PACKAGE, "custom_commands_labels", NULL, NULL);
982 /* sanitize custom commands labels */
983 if (ui_prefs.custom_commands || ui_prefs.custom_commands_labels)
985 guint i;
986 guint cc_len = ui_prefs.custom_commands ? g_strv_length(ui_prefs.custom_commands) : 0;
987 guint cc_labels_len = ui_prefs.custom_commands_labels ? g_strv_length(ui_prefs.custom_commands_labels) : 0;
989 /* not enough items, resize and fill */
990 if (cc_labels_len < cc_len)
992 ui_prefs.custom_commands_labels = g_realloc(ui_prefs.custom_commands_labels,
993 (cc_len + 1) * sizeof *ui_prefs.custom_commands_labels);
994 for (i = cc_labels_len; i < cc_len; i++)
995 ui_prefs.custom_commands_labels[i] = g_strdup("");
996 ui_prefs.custom_commands_labels[cc_len] = NULL;
998 /* too many items, cut off */
999 else if (cc_labels_len > cc_len)
1001 for (i = cc_len; i < cc_labels_len; i++)
1003 g_free(ui_prefs.custom_commands_labels[i]);
1004 ui_prefs.custom_commands_labels[i] = NULL;
1009 scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
1010 _("Type here what you want, use it as a notice/scratch board"));
1011 scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
1013 geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", &geo_len, NULL);
1014 if (! geo || geo_len < 5)
1016 ui_prefs.geometry[0] = -1;
1017 ui_prefs.geometry[1] = -1;
1018 ui_prefs.geometry[2] = -1;
1019 ui_prefs.geometry[3] = -1;
1020 ui_prefs.geometry[4] = 0;
1022 else
1024 /* don't use insane values but when main windows was maximized last time, pos might be
1025 * negative (due to differences in root window and window decorations) */
1026 /* quitting when minimized can make pos -32000, -32000 on Windows! */
1027 ui_prefs.geometry[0] = MAX(-1, geo[0]);
1028 ui_prefs.geometry[1] = MAX(-1, geo[1]);
1029 ui_prefs.geometry[2] = MAX(-1, geo[2]);
1030 ui_prefs.geometry[3] = MAX(-1, geo[3]);
1031 ui_prefs.geometry[4] = geo[4] != 0;
1033 hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
1034 vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
1035 (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
1036 (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
1038 g_free(geo);
1043 * Save current session in default configuration file
1045 void configuration_save_default_session(void)
1047 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1048 gchar *data;
1049 GKeyFile *config = g_key_file_new();
1051 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1053 if (cl_options.load_session)
1054 configuration_save_session_files(config);
1056 /* write the file */
1057 data = g_key_file_to_data(config, NULL, NULL);
1058 utils_write_file(configfile, data);
1059 g_free(data);
1061 g_key_file_free(config);
1062 g_free(configfile);
1066 void configuration_clear_default_session(void)
1068 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1069 gchar *data;
1070 GKeyFile *config = g_key_file_new();
1072 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1074 if (cl_options.load_session)
1075 remove_session_files(config);
1077 /* write the file */
1078 data = g_key_file_to_data(config, NULL, NULL);
1079 utils_write_file(configfile, data);
1080 g_free(data);
1082 g_key_file_free(config);
1083 g_free(configfile);
1088 * Only reload the session part of the default configuration
1090 void configuration_reload_default_session(void)
1092 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1093 GKeyFile *config = g_key_file_new();
1095 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1096 g_free(configfile);
1098 configuration_load_session_files(config, FALSE);
1100 g_key_file_free(config);
1104 gboolean configuration_load(void)
1106 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1107 GKeyFile *config = g_key_file_new();
1109 if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
1110 { /* config file does not (yet) exist, so try to load a global config file which may be */
1111 /* created by distributors */
1112 geany_debug("No user config file found, trying to use global configuration.");
1113 SETPTR(configfile, g_build_filename(app->datadir, "geany.conf", NULL));
1115 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1116 g_free(configfile);
1118 load_dialog_prefs(config);
1119 load_ui_prefs(config);
1120 project_load_prefs(config);
1121 configuration_load_session_files(config, TRUE);
1123 /* this signal can be used e.g. to delay building UI elements until settings have been read */
1124 g_signal_emit_by_name(geany_object, "load-settings", config);
1126 g_key_file_free(config);
1127 return TRUE;
1131 static gboolean open_session_file(gchar **tmp, guint len)
1133 guint pos;
1134 const gchar *ft_name;
1135 gchar *locale_filename;
1136 gchar *unescaped_filename;
1137 const gchar *encoding;
1138 gint indent_type;
1139 gboolean ro, auto_indent, line_wrapping;
1140 /** TODO when we have a global pref for line breaking, use its value */
1141 gboolean line_breaking = FALSE;
1142 gboolean ret = FALSE;
1144 pos = atoi(tmp[0]);
1145 ft_name = tmp[1];
1146 ro = atoi(tmp[2]);
1147 if (isdigit(tmp[3][0]))
1149 encoding = encodings_get_charset_from_index(atoi(tmp[3]));
1151 else
1153 encoding = &(tmp[3][1]);
1155 indent_type = atoi(tmp[4]);
1156 auto_indent = atoi(tmp[5]);
1157 line_wrapping = atoi(tmp[6]);
1158 /* try to get the locale equivalent for the filename */
1159 unescaped_filename = g_uri_unescape_string(tmp[7], NULL);
1160 locale_filename = utils_get_locale_from_utf8(unescaped_filename);
1162 if (len > 8)
1163 line_breaking = atoi(tmp[8]);
1165 if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR))
1167 GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
1168 GeanyDocument *doc = document_open_file_full(
1169 NULL, locale_filename, pos, ro, ft, encoding);
1171 if (doc)
1173 gint indent_width = doc->editor->indent_width;
1175 if (len > 9)
1176 indent_width = atoi(tmp[9]);
1177 editor_set_indent(doc->editor, indent_type, indent_width);
1178 editor_set_line_wrapping(doc->editor, line_wrapping);
1179 doc->editor->line_breaking = line_breaking;
1180 doc->editor->auto_indent = auto_indent;
1181 ret = TRUE;
1184 else
1186 geany_debug("Could not find file '%s'.", tmp[7]);
1189 g_free(locale_filename);
1190 g_free(unescaped_filename);
1191 return ret;
1195 /* Open session files
1196 * Note: notebook page switch handler and adding to recent files list is always disabled
1197 * for all files opened within this function */
1198 void configuration_open_files(void)
1200 gint i;
1201 gboolean failure = FALSE;
1203 /* necessary to set it to TRUE for project session support */
1204 main_status.opening_session_files = TRUE;
1206 i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
1207 while (TRUE)
1209 gchar **tmp = g_ptr_array_index(session_files, i);
1210 guint len;
1212 if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
1214 if (! open_session_file(tmp, len))
1215 failure = TRUE;
1217 g_strfreev(tmp);
1219 if (file_prefs.tab_order_ltr)
1221 i++;
1222 if (i >= (gint)session_files->len)
1223 break;
1225 else
1227 i--;
1228 if (i < 0)
1229 break;
1233 g_ptr_array_free(session_files, TRUE);
1234 session_files = NULL;
1236 if (failure)
1237 ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
1238 else
1240 /* explicitly trigger a notebook page switch after unsetting main_status.opening_session_files
1241 * for callbacks to run (and update window title, encoding settings, and so on) */
1242 gint n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1243 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
1244 gint target_page = session_notebook_page >= 0 ? session_notebook_page : cur_page;
1246 /* if target page is current page, switch to another page first to really trigger an event */
1247 if (target_page == cur_page && n_pages > 0)
1248 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), (cur_page + 1) % n_pages);
1250 main_status.opening_session_files = FALSE;
1251 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), target_page);
1253 main_status.opening_session_files = FALSE;
1257 /* set some settings which are already read from the config file, but need other things, like the
1258 * realisation of the main window */
1259 void configuration_apply_settings(void)
1261 if (scribble_text)
1262 { /* update the scribble widget, because now it's realized */
1263 GtkTextIter iter;
1264 GtkTextBuffer *buffer =
1265 gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
1267 gtk_text_buffer_set_text(buffer, scribble_text, -1);
1268 gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
1269 gtk_text_buffer_place_cursor(buffer, &iter);
1271 g_free(scribble_text);
1273 /* set the position of the hpaned and vpaned */
1274 if (prefs.save_winpos)
1276 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
1277 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
1280 /* set fullscreen after initial draw so that returning to normal view is the right size.
1281 * fullscreen mode is disabled by default, so act only if it is true */
1282 if (ui_prefs.fullscreen)
1284 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
1285 ui_prefs.fullscreen = TRUE;
1286 ui_set_fullscreen();
1289 msgwin_show_hide_tabs();
1293 void configuration_init(void)
1295 keyfile_groups = g_ptr_array_new();
1296 pref_groups = g_ptr_array_new();
1297 init_pref_groups();
1301 void configuration_finalize(void)
1303 guint i;
1304 StashGroup *group;
1306 foreach_ptr_array(group, i, keyfile_groups)
1307 stash_group_free(group);
1309 g_ptr_array_free(keyfile_groups, TRUE);
1310 g_ptr_array_free(pref_groups, TRUE);