Fix anonymous tag renaming when interleaved by scopeless macros
[geany-mirror.git] / src / keyfile.c
blob23b616d1f038a606877df7016b1187c86466ea74
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 "sidebar.h"
50 #include "socket.h"
51 #include "stash.h"
52 #include "support.h"
53 #include "symbols.h"
54 #include "templates.h"
55 #include "toolbar.h"
56 #include "ui_utils.h"
57 #include "utils.h"
58 #include "vte.h"
60 #include <stdlib.h>
61 #include <string.h>
62 #include <ctype.h>
64 #ifdef HAVE_VTE
65 #include <pwd.h>
66 #include <sys/types.h>
67 #include <unistd.h>
68 #endif
70 /* define the configuration filenames */
71 #define PREFS_FILE "geany.conf"
72 #define SESSION_FILE "session.conf"
74 /* some default settings which are used at the very first start of Geany to fill
75 * the configuration file */
76 #define GEANY_MAX_SYMBOLLIST_HEIGHT 10
77 #define GEANY_MIN_SYMBOLLIST_CHARS 4
78 #define GEANY_MSGWIN_HEIGHT 208
79 #define GEANY_DISK_CHECK_TIMEOUT 30
80 #define GEANY_DEFAULT_TOOLS_MAKE "make"
81 #ifdef G_OS_WIN32
82 #define GEANY_DEFAULT_TOOLS_TERMINAL "cmd.exe /Q /C %c"
83 #elif defined(__APPLE__)
84 #define GEANY_DEFAULT_TOOLS_TERMINAL "open -a terminal %c"
85 #else
86 #define GEANY_DEFAULT_TOOLS_TERMINAL "xterm -e \"/bin/sh %c\""
87 #endif
88 #ifdef __APPLE__
89 #define GEANY_DEFAULT_TOOLS_BROWSER "open -a safari"
90 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Helvetica Medium 12"
91 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Menlo Medium 12"
92 #define GEANY_DEFAULT_FONT_EDITOR "Menlo Medium 12"
93 #else
94 #define GEANY_DEFAULT_TOOLS_BROWSER "firefox"
95 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Sans 9"
96 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Monospace 9"
97 #define GEANY_DEFAULT_FONT_EDITOR "Monospace 10"
98 #endif
99 #define GEANY_DEFAULT_TOOLS_PRINTCMD "lpr"
100 #define GEANY_DEFAULT_TOOLS_GREP "grep"
101 #define GEANY_DEFAULT_MRU_LENGTH 10
102 #define GEANY_TOGGLE_MARK "~ "
103 #define GEANY_MAX_AUTOCOMPLETE_WORDS 30
104 #define GEANY_MAX_SYMBOLS_UPDATE_FREQ 250
105 #define GEANY_DEFAULT_FILETYPE_REGEX "-\\*-\\s*([^\\s]+)\\s*-\\*-"
108 static gchar *scribble_text = NULL;
109 static gint scribble_pos = -1;
110 static GPtrArray *default_session_files = NULL;
111 static gint session_notebook_page;
112 static gint hpan_position;
113 static gint vpan_position;
114 static const gchar atomic_file_saving_key[] = "use_atomic_file_saving";
116 typedef enum
118 PREFS,
119 SESSION,
121 MAX_PAYLOAD
122 } ConfigPayload;
124 static GPtrArray *keyfile_groups[MAX_PAYLOAD];
125 GPtrArray *pref_groups;
127 static struct
129 gint number_ft_menu_items;
130 gint number_non_ft_menu_items;
131 gint number_exec_menu_items;
133 build_menu_prefs;
135 /* The group will be free'd on quitting.
136 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
137 void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
139 g_ptr_array_add(keyfile_groups[PREFS], group);
141 if (for_prefs_dialog)
142 g_ptr_array_add(pref_groups, group);
146 /* The group will be free'd on quitting.
147 * prefix can be NULL to use group name */
148 void configuration_add_various_pref_group(struct StashGroup *group,
149 const gchar *prefix)
151 configuration_add_pref_group(group, TRUE);
152 stash_group_set_various(group, TRUE, prefix);
156 /* The group will be free'd on quitting.
158 * @a for_prefs_dialog is typically @c FALSE as session configuration is not
159 * well suited for the Preferences dialog. Probably only existing prefs that
160 * migrated to session data should set this to @c TRUE.
162 * @param for_prefs_dialog is whether the group also has Prefs dialog items.
164 void configuration_add_session_group(struct StashGroup *group, gboolean for_prefs_dialog)
166 g_ptr_array_add(keyfile_groups[SESSION], group);
168 if (for_prefs_dialog)
169 g_ptr_array_add(pref_groups, group);
173 static void init_pref_groups(void)
175 StashGroup *group;
177 group = stash_group_new(PACKAGE);
178 configuration_add_pref_group(group, TRUE);
179 stash_group_add_entry(group, &prefs.default_open_path,
180 "default_open_path", "", "startup_path_entry");
182 stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
183 "cmdline_new_files", TRUE, "check_cmdline_new_files");
185 stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
186 "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
187 stash_group_add_toggle_button(group, &file_prefs.tab_close_switch_to_mru,
188 "tab_close_switch_to_mru", FALSE, "check_tab_close_switch_to_mru");
189 stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
190 stash_group_add_integer(group, &interface_prefs.openfiles_path_mode, "openfiles_path_mode", -1);
191 stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
192 "sidebar_pos", GTK_POS_LEFT,
193 "radio_sidebar_left", GTK_POS_LEFT,
194 "radio_sidebar_right", GTK_POS_RIGHT,
195 NULL);
196 stash_group_add_radio_buttons(group, &interface_prefs.symbols_sort_mode,
197 "symbols_sort_mode", SYMBOLS_SORT_BY_NAME,
198 "radio_symbols_sort_by_name", SYMBOLS_SORT_BY_NAME,
199 "radio_symbols_sort_by_appearance", SYMBOLS_SORT_BY_APPEARANCE,
200 NULL);
201 stash_group_add_radio_buttons(group, &interface_prefs.msgwin_orientation,
202 "msgwin_orientation", GTK_ORIENTATION_VERTICAL,
203 "radio_msgwin_vertical", GTK_ORIENTATION_VERTICAL,
204 "radio_msgwin_horizontal", GTK_ORIENTATION_HORIZONTAL,
205 NULL);
207 /* editor display */
208 stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
209 "highlighting_invert_all", FALSE, "check_highlighting_invert");
211 stash_group_add_toggle_button(group, &search_prefs.use_current_word,
212 "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
214 /* editor */
215 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
216 "check_detect_indent", FALSE, "check_detect_indent_type");
217 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_width,
218 "detect_indent_width", FALSE, "check_detect_indent_width");
219 stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
220 "use_tab_to_indent", TRUE, "check_tab_key_indents");
221 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
222 "pref_editor_tab_width", 4, "spin_indent_width");
223 stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
224 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
225 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
226 "indent_type", GEANY_INDENT_TYPE_TABS,
227 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
228 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
229 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
230 NULL);
231 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
232 "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
233 "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
234 "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
235 "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
236 NULL);
237 stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
238 "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
239 stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
240 "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
241 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
242 "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
243 "spin_autocompletion_max_entries");
244 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_update_freq,
245 "autocompletion_update_freq", GEANY_MAX_SYMBOLS_UPDATE_FREQ, "spin_symbol_update_freq");
246 stash_group_add_string(group, &editor_prefs.color_scheme,
247 "color_scheme", NULL);
248 stash_group_add_spin_button_integer(group, &editor_prefs.scroll_lines_around_cursor,
249 "scroll_lines_around_cursor", 0, "spin_scroll_lines_around_cursor");
251 /* files */
252 stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
253 "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
254 stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
255 "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
257 /* various geany prefs */
258 group = stash_group_new(PACKAGE);
259 configuration_add_various_pref_group(group, "editor");
261 stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
262 "show_editor_scrollbars", TRUE);
263 stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
264 "brace_match_ltgt", FALSE);
265 stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
266 "use_gtk_word_boundaries", TRUE);
267 stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
268 "complete_snippets_whilst_editing", FALSE);
269 /* for backwards-compatibility */
270 stash_group_add_integer(group, &editor_prefs.indentation->hard_tab_width,
271 "indent_hard_tab_width", 8);
272 stash_group_add_integer(group, &editor_prefs.ime_interaction,
273 "editor_ime_interaction", SC_IME_WINDOWED);
275 group = stash_group_new(PACKAGE);
276 configuration_add_various_pref_group(group, "files");
278 stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
279 atomic_file_saving_key, FALSE);
280 stash_group_add_boolean(group, &file_prefs.gio_unsafe_save_backup,
281 "gio_unsafe_save_backup", FALSE);
282 stash_group_add_boolean(group, &file_prefs.use_gio_unsafe_file_saving,
283 "use_gio_unsafe_file_saving", TRUE);
284 stash_group_add_boolean(group, &file_prefs.keep_edit_history_on_reload,
285 "keep_edit_history_on_reload", TRUE);
286 stash_group_add_boolean(group, &file_prefs.show_keep_edit_history_on_reload_msg,
287 "show_keep_edit_history_on_reload_msg", TRUE);
288 stash_group_add_boolean(group, &file_prefs.reload_clean_doc_on_file_change,
289 "reload_clean_doc_on_file_change", FALSE);
290 stash_group_add_boolean(group, &file_prefs.save_config_on_file_change,
291 "save_config_on_file_change", TRUE);
292 stash_group_add_string(group, &file_prefs.extract_filetype_regex,
293 "extract_filetype_regex", GEANY_DEFAULT_FILETYPE_REGEX);
294 stash_group_add_boolean(group, &ui_prefs.allow_always_save,
295 "allow_always_save", FALSE);
297 group = stash_group_new(PACKAGE);
298 configuration_add_various_pref_group(group, "search");
300 stash_group_add_integer(group, (gint*)&search_prefs.find_selection_type,
301 "find_selection_type", GEANY_FIND_SEL_CURRENT_WORD);
302 stash_group_add_boolean(group, &search_prefs.replace_and_find_by_default,
303 "replace_and_find_by_default", TRUE);
305 group = stash_group_new(PACKAGE);
306 configuration_add_various_pref_group(group, "socket");
308 #ifdef G_OS_WIN32
309 stash_group_add_integer(group, (gint*)&prefs.socket_remote_cmd_port,
310 "socket_remote_cmd_port", SOCKET_WINDOWS_REMOTE_CMD_PORT);
311 #endif
313 #ifdef HAVE_VTE
314 /* various VTE prefs. */
315 group = stash_group_new("VTE");
316 configuration_add_various_pref_group(group, "terminal");
318 stash_group_add_string(group, &vte_config.send_cmd_prefix,
319 "send_cmd_prefix", "");
320 stash_group_add_boolean(group, &vte_config.send_selection_unsafe,
321 "send_selection_unsafe", FALSE);
322 #endif
324 /* Note: Interface-related various prefs are in ui_init_prefs() */
326 /* various build-menu prefs */
327 // Warning: don't move PACKAGE group name items here
328 group = stash_group_new("build-menu");
329 configuration_add_various_pref_group(group, "build");
331 stash_group_add_integer(group, &build_menu_prefs.number_ft_menu_items,
332 "number_ft_menu_items", 0);
333 stash_group_add_integer(group, &build_menu_prefs.number_non_ft_menu_items,
334 "number_non_ft_menu_items", 0);
335 stash_group_add_integer(group, &build_menu_prefs.number_exec_menu_items,
336 "number_exec_menu_items", 0);
340 typedef enum SettingAction
342 SETTING_READ,
343 SETTING_WRITE
345 SettingAction;
347 static void settings_action(GKeyFile *config, SettingAction action, ConfigPayload payload)
349 guint i;
350 StashGroup *group;
352 foreach_ptr_array(group, i, keyfile_groups[payload])
354 switch (action)
356 case SETTING_READ:
357 stash_group_load_from_key_file(group, config); break;
358 case SETTING_WRITE:
359 stash_group_save_to_key_file(group, config); break;
365 static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
367 gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
368 guint i;
370 for (i = 0; i < file_prefs.mru_length; i++)
372 if (! g_queue_is_empty(queue))
374 /* copy the values, this is necessary when this function is called from the
375 * preferences dialog or when quitting is canceled to keep the queue intact */
376 recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
378 else
380 recent_files[i] = NULL;
381 break;
384 /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
385 recent_files[file_prefs.mru_length] = NULL;
386 g_key_file_set_string_list(config, "files", key,
387 (const gchar**)recent_files, file_prefs.mru_length);
388 g_strfreev(recent_files);
392 static gchar *get_session_file_string(GeanyDocument *doc)
394 gchar *fname;
395 gchar *locale_filename;
396 gchar *escaped_filename;
397 GeanyFiletype *ft = doc->file_type;
399 if (ft == NULL) /* can happen when saving a new file when quitting */
400 ft = filetypes[GEANY_FILETYPES_NONE];
402 locale_filename = utils_get_locale_from_utf8(doc->file_name);
403 escaped_filename = g_uri_escape_string(locale_filename, NULL, TRUE);
405 fname = g_strdup_printf("%d;%s;%d;E%s;%d;%d;%d;%s;%d;%d",
406 sci_get_current_position(doc->editor->sci),
407 ft->name,
408 doc->readonly,
409 doc->encoding,
410 doc->editor->indent_type,
411 doc->editor->auto_indent,
412 doc->editor->line_wrapping,
413 escaped_filename,
414 doc->editor->line_breaking,
415 doc->editor->indent_width);
416 g_free(escaped_filename);
417 g_free(locale_filename);
418 return fname;
422 static void remove_session_files(GKeyFile *config)
424 gchar **ptr;
425 gchar **keys = g_key_file_get_keys(config, "files", NULL, NULL);
427 foreach_strv(ptr, keys)
429 if (g_str_has_prefix(*ptr, "FILE_NAME_"))
430 g_key_file_remove_key(config, "files", *ptr, NULL);
432 g_strfreev(keys);
436 void configuration_save_session_files(GKeyFile *config)
438 gint npage;
439 gchar entry[16];
440 guint i = 0, j = 0, max;
442 npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
443 g_key_file_set_integer(config, "files", "current_page", npage);
445 // clear existing entries first as they might not all be overwritten
446 remove_session_files(config);
448 /* store the filenames in the notebook tab order to reopen them the next time */
449 max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
450 for (i = 0; i < max; i++)
452 GeanyDocument *doc = document_get_from_page(i);
454 if (doc != NULL && doc->real_path != NULL)
456 gchar *fname;
458 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
459 fname = get_session_file_string(doc);
460 g_key_file_set_string(config, "files", entry, fname);
461 g_free(fname);
462 j++;
466 #ifdef HAVE_VTE
467 if (vte_info.have_vte)
469 vte_get_working_directory(); /* refresh vte_info.dir */
470 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
472 #endif
476 static void save_dialog_prefs(GKeyFile *config)
478 /* Some of the key names are not consistent, but this is for backwards compatibility */
480 /* general */
481 g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
482 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
483 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
484 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_wingeom", prefs.save_wingeom);
485 g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
486 g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
487 g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
488 g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
489 g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
491 /* interface */
492 g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
493 g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
494 g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
495 g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
496 g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
497 g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
498 g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
499 g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
500 g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
501 g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
502 g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
503 g_key_file_set_boolean(config, PACKAGE, "use_native_windows_dialogs", interface_prefs.use_native_windows_dialogs);
505 /* display */
506 g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
507 g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
508 g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
509 g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
510 g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
511 g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_enabled);
512 g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_type);
513 g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_column);
514 g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
516 /* editor */
517 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
518 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
519 g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
520 g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
521 g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
522 g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
523 g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
524 g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
525 g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
526 g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
527 g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
528 g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
529 g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
530 g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
531 g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
532 g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
533 g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
535 /* files */
536 g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
537 if (file_prefs.default_open_encoding == -1)
538 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
539 else
540 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
541 g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
542 g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
543 g_key_file_set_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", file_prefs.ensure_convert_new_lines);
544 g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
545 g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
547 /* toolbar */
548 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
549 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
550 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
551 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
552 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
553 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
555 /* templates */
556 g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
557 g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
558 g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
559 g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
560 g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
561 g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
562 g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
563 g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
565 /* tools settings */
566 g_key_file_set_string(config, "tools", "terminal_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
567 g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
568 g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
569 g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
571 /* build menu */
572 build_save_menu(config, NULL, GEANY_BCS_PREF);
574 /* printing */
575 g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
576 g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
577 g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
578 g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
579 g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
580 g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
581 g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
583 /* VTE */
584 #ifdef HAVE_VTE
585 g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
586 if (vte_info.have_vte)
588 VteConfig *vc = &vte_config;
589 gchar *tmp_string;
591 g_key_file_set_string(config, "VTE", "font", vc->font);
592 g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
593 g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
594 g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
595 g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
596 g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
597 g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
598 g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
599 g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
600 g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
601 g_key_file_set_string(config, "VTE", "font", vc->font);
602 g_key_file_set_string(config, "VTE", "shell", vc->shell);
603 tmp_string = utils_get_hex_from_color(&vc->colour_fore);
604 g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
605 g_free(tmp_string);
606 tmp_string = utils_get_hex_from_color(&vc->colour_back);
607 g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
608 g_free(tmp_string);
610 #endif
614 static void save_ui_prefs(GKeyFile *config)
616 g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
617 g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
618 g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
619 g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
620 g_key_file_set_string(config, PACKAGE, "color_picker_palette", ui_prefs.color_picker_palette);
622 /* get the text from the scribble textview */
624 GtkTextBuffer *buffer;
625 GtkTextIter start, end, iter;
626 GtkTextMark *mark;
628 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
629 gtk_text_buffer_get_bounds(buffer, &start, &end);
630 scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
631 g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
632 g_free(scribble_text);
634 mark = gtk_text_buffer_get_insert(buffer);
635 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
636 scribble_pos = gtk_text_iter_get_offset(&iter);
637 g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
640 g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
641 if (ui_prefs.custom_commands != NULL)
643 g_key_file_set_string_list(config, PACKAGE, "custom_commands",
644 (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
645 g_key_file_set_string_list(config, PACKAGE, "custom_commands_labels",
646 (const gchar**) ui_prefs.custom_commands_labels, g_strv_length(ui_prefs.custom_commands_labels));
650 static void save_ui_session(GKeyFile *config)
652 if (prefs.save_winpos || prefs.save_wingeom)
654 GdkWindowState wstate;
656 g_key_file_set_integer(config, PACKAGE, "treeview_position",
657 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
658 g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
659 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
661 gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
662 gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
663 wstate = gdk_window_get_state(gtk_widget_get_window(main_widgets.window));
664 ui_prefs.geometry[4] = (wstate & GDK_WINDOW_STATE_MAXIMIZED) ? 1 : 0;
665 g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
669 void write_config_file(gchar const *filename, ConfigPayload payload)
671 GKeyFile *config = g_key_file_new();
672 gchar *configfile = g_build_filename(app->configdir, filename, NULL);
673 gchar *data;
675 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
677 switch (payload)
679 case PREFS:
680 /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
681 g_signal_emit_by_name(geany_object, "save-settings", config);
682 save_dialog_prefs(config);
683 save_ui_prefs(config);
684 break;
685 case SESSION:
686 save_recent_files(config, ui_prefs.recent_queue, "recent_files");
687 save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
688 project_save_prefs(config); /* save project filename, etc. */
689 save_ui_session(config);
690 if (cl_options.load_session && app->project == NULL)
691 configuration_save_session_files(config);
692 #ifdef HAVE_VTE
693 else if (vte_info.have_vte)
695 vte_get_working_directory(); /* refresh vte_info.dir */
696 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
698 #endif
699 break;
702 /* new settings should be added in init_pref_groups() */
703 settings_action(config, SETTING_WRITE, payload);
705 /* write the file */
706 data = g_key_file_to_data(config, NULL, NULL);
707 utils_write_file(configfile, data);
708 g_free(data);
710 g_key_file_free(config);
711 g_free(configfile);
714 void configuration_save(void)
716 /* save all configuration files
717 * it is probably not very efficient to write both files every time
718 * could be more selective about which file is saved when */
719 write_config_file(PREFS_FILE, PREFS);
720 write_config_file(SESSION_FILE, SESSION);
723 static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
725 gchar **recent_files;
726 gsize i, len = 0;
728 recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
729 if (recent_files != NULL)
731 for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
733 gchar *filename = g_strdup(recent_files[i]);
734 g_queue_push_tail(queue, filename);
736 g_strfreev(recent_files);
742 * Load session list from the given keyfile and return an array containg the file names
743 * */
744 GPtrArray *configuration_load_session_files(GKeyFile *config)
746 guint i;
747 gboolean have_session_files;
748 gchar entry[16];
749 gchar **tmp_array;
750 GError *error = NULL;
751 GPtrArray *files;
753 session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
755 files = g_ptr_array_new();
756 have_session_files = TRUE;
757 i = 0;
758 while (have_session_files)
760 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
761 tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
762 if (! tmp_array || error)
764 g_error_free(error);
765 error = NULL;
766 have_session_files = FALSE;
768 g_ptr_array_add(files, tmp_array);
769 i++;
772 #ifdef HAVE_VTE
773 /* BUG: after loading project at startup, closing project doesn't restore old VTE path */
774 if (vte_info.have_vte)
776 gchar *tmp_string = utils_get_setting_string(config, "VTE", "last_dir", NULL);
777 vte_cwd(tmp_string,TRUE);
778 g_free(tmp_string);
780 #endif
782 return files;
786 #ifdef HAVE_VTE
787 static void get_setting_color(GKeyFile *config, const gchar *section, const gchar *key,
788 GdkColor *color, const gchar *default_color)
790 gchar *str = utils_get_setting_string(config, section, key, NULL);
791 if (str == NULL || ! utils_parse_color(str, color))
792 utils_parse_color(default_color, color);
793 g_free(str);
795 #endif
798 /* note: new settings should be added in init_pref_groups() */
799 static void load_dialog_prefs(GKeyFile *config)
801 gchar *tmp_string, *tmp_string2;
802 const gchar *default_charset = NULL;
803 gchar *cmd;
805 /* compatibility with Geany 0.20 */
806 if (!g_key_file_has_key(config, PACKAGE, atomic_file_saving_key, NULL))
808 g_key_file_set_boolean(config, PACKAGE, atomic_file_saving_key,
809 utils_get_setting_boolean(config, PACKAGE, "use_safe_file_saving", FALSE));
812 /* compatibility with Geany 0.21 */
814 gboolean suppress_search_dialogs = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_search_dialogs", FALSE);
816 if (!g_key_file_has_key(config, "search", "pref_search_always_wrap", NULL))
817 g_key_file_set_boolean(config, "search", "pref_search_always_wrap", suppress_search_dialogs);
819 if (!g_key_file_has_key(config, "search", "pref_search_hide_find_dialog", NULL))
820 g_key_file_set_boolean(config, "search", "pref_search_hide_find_dialog", suppress_search_dialogs);
823 /* general */
824 prefs.confirm_exit = utils_get_setting_boolean(config, PACKAGE, "pref_main_confirm_exit", FALSE);
825 prefs.suppress_status_messages = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_status_messages", FALSE);
826 prefs.load_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_load_session", TRUE);
827 project_prefs.project_file_in_basedir = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", TRUE);
828 prefs.save_winpos = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_winpos", TRUE);
829 prefs.save_wingeom = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_wingeom", prefs.save_winpos);
830 prefs.beep_on_errors = utils_get_setting_boolean(config, PACKAGE, "beep_on_errors", TRUE);
831 prefs.switch_to_status = utils_get_setting_boolean(config, PACKAGE, "switch_msgwin_pages", FALSE);
832 prefs.auto_focus = utils_get_setting_boolean(config, PACKAGE, "auto_focus", FALSE);
834 /* interface */
835 interface_prefs.tab_pos_editor = utils_get_setting_integer(config, PACKAGE, "tab_pos_editor", GTK_POS_TOP);
836 interface_prefs.tab_pos_msgwin = utils_get_setting_integer(config, PACKAGE, "tab_pos_msgwin",GTK_POS_LEFT);
837 interface_prefs.sidebar_symbol_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_symbol_visible", TRUE);
838 interface_prefs.sidebar_openfiles_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_openfiles_visible", TRUE);
839 interface_prefs.statusbar_visible = utils_get_setting_boolean(config, PACKAGE, "statusbar_visible", TRUE);
840 file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
841 file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
842 interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
843 file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
844 interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
845 interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
846 interface_prefs.msgwin_font = utils_get_setting_string(config, PACKAGE, "msgwin_font", GEANY_DEFAULT_FONT_MSG_WINDOW);
847 interface_prefs.use_native_windows_dialogs = utils_get_setting_boolean(config, PACKAGE, "use_native_windows_dialogs", FALSE);
849 /* display, editor */
850 editor_prefs.long_line_enabled = utils_get_setting_boolean(config, PACKAGE, "long_line_enabled", TRUE);
851 editor_prefs.long_line_type = utils_get_setting_integer(config, PACKAGE, "long_line_type", 0);
852 if (editor_prefs.long_line_type == 2) /* backward compatibility */
854 editor_prefs.long_line_type = 0;
855 editor_prefs.long_line_enabled = FALSE;
857 editor_prefs.long_line_color = utils_get_setting_string(config, PACKAGE, "long_line_color", "#C2EBC2");
858 editor_prefs.long_line_column = utils_get_setting_integer(config, PACKAGE, "long_line_column", 72);
859 editor_prefs.symbolcompletion_min_chars = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_min_chars", GEANY_MIN_SYMBOLLIST_CHARS);
860 editor_prefs.symbolcompletion_max_height = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_max_height", GEANY_MAX_SYMBOLLIST_HEIGHT);
861 editor_prefs.line_wrapping = utils_get_setting_boolean(config, PACKAGE, "line_wrapping", FALSE); /* default is off for better performance */
862 editor_prefs.use_indicators = utils_get_setting_boolean(config, PACKAGE, "use_indicators", TRUE);
863 editor_prefs.show_indent_guide = utils_get_setting_boolean(config, PACKAGE, "show_indent_guide", FALSE);
864 editor_prefs.show_white_space = utils_get_setting_boolean(config, PACKAGE, "show_white_space", FALSE);
865 editor_prefs.show_line_endings = utils_get_setting_boolean(config, PACKAGE, "show_line_endings", FALSE);
866 editor_prefs.scroll_stop_at_last_line = utils_get_setting_boolean(config, PACKAGE, "scroll_stop_at_last_line", TRUE);
867 editor_prefs.auto_close_xml_tags = utils_get_setting_boolean(config, PACKAGE, "auto_close_xml_tags", TRUE);
868 editor_prefs.complete_snippets = utils_get_setting_boolean(config, PACKAGE, "complete_snippets", TRUE);
869 editor_prefs.auto_complete_symbols = utils_get_setting_boolean(config, PACKAGE, "auto_complete_symbols", TRUE);
870 editor_prefs.folding = utils_get_setting_boolean(config, PACKAGE, "use_folding", TRUE);
871 editor_prefs.unfold_all_children = utils_get_setting_boolean(config, PACKAGE, "unfold_all_children", FALSE);
872 editor_prefs.show_markers_margin = utils_get_setting_boolean(config, PACKAGE, "show_markers_margin", TRUE);
873 editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
874 editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
875 editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
876 editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
877 editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
878 editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
879 editor_prefs.comment_toggle_mark = utils_get_setting_string(config, PACKAGE, "comment_toggle_mark", GEANY_TOGGLE_MARK);
880 editor_prefs.autoclose_chars = utils_get_setting_integer(config, PACKAGE, "autoclose_chars", 0);
882 /* Files
883 * use current locale encoding as default for new files (should be UTF-8 in most cases) */
884 g_get_charset(&default_charset);
885 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_new_encoding",
886 default_charset);
887 if (tmp_string)
889 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
890 if (enc != NULL)
891 file_prefs.default_new_encoding = enc->idx;
892 else
893 file_prefs.default_new_encoding = GEANY_ENCODING_UTF_8;
895 g_free(tmp_string);
897 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_open_encoding",
898 "none");
899 if (tmp_string)
901 const GeanyEncoding *enc = NULL;
902 if (strcmp(tmp_string, "none") != 0)
903 enc = encodings_get_from_charset(tmp_string);
904 if (enc != NULL)
905 file_prefs.default_open_encoding = enc->idx;
906 else
907 file_prefs.default_open_encoding = -1;
909 g_free(tmp_string);
911 file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
912 file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
913 file_prefs.ensure_convert_new_lines = utils_get_setting_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", FALSE);
914 file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
915 file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
917 /* toolbar */
918 toolbar_prefs.visible = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_show", TRUE);
919 toolbar_prefs.append_to_menu = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", FALSE);
921 toolbar_prefs.use_gtk_default_style = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", TRUE);
922 if (! toolbar_prefs.use_gtk_default_style)
923 toolbar_prefs.icon_style = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_style", GTK_TOOLBAR_ICONS);
925 toolbar_prefs.use_gtk_default_icon = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", TRUE);
926 if (! toolbar_prefs.use_gtk_default_icon)
927 toolbar_prefs.icon_size = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_size", GTK_ICON_SIZE_LARGE_TOOLBAR);
930 /* VTE */
931 #ifdef HAVE_VTE
932 vte_info.load_vte = utils_get_setting_boolean(config, "VTE", "load_vte", TRUE);
933 if (vte_info.load_vte && vte_info.load_vte_cmdline /* not disabled on the cmdline */)
935 VteConfig *vc = &vte_config;
936 struct passwd *pw = getpwuid(getuid());
937 const gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
939 #ifdef __APPLE__
940 /* Geany is started using launchd on OS X and we don't get any environment variables
941 * so PS1 isn't defined. Start as a login shell to read the corresponding config files. */
942 if (strcmp(shell, "/bin/bash") == 0)
943 shell = "/bin/bash -l";
944 #endif
946 vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
947 if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
948 /* last dir is not set, fallback to user's home directory */
949 SETPTR(vte_info.dir, g_strdup(pw->pw_dir));
950 else if (vte_info.dir == NULL && pw == NULL)
951 /* fallback to root */
952 vte_info.dir = g_strdup("/");
954 vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
955 vc->font = utils_get_setting_string(config, "VTE", "font", GEANY_DEFAULT_FONT_EDITOR);
956 vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
957 vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
958 vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", TRUE);
959 vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
960 vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
961 vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
962 vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
963 vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
964 vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
965 get_setting_color(config, "VTE", "colour_fore", &vc->colour_fore, "#ffffff");
966 get_setting_color(config, "VTE", "colour_back", &vc->colour_back, "#000000");
968 #endif
969 /* templates */
970 template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
971 template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
972 tmp_string = utils_get_initials(template_prefs.developer);
973 template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
974 g_free(tmp_string);
976 template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
978 tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), g_get_host_name());
979 template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
980 g_free(tmp_string);
981 template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", GEANY_TEMPLATES_FORMAT_YEAR);
982 template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", GEANY_TEMPLATES_FORMAT_DATE);
983 template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", GEANY_TEMPLATES_FORMAT_DATETIME);
985 /* tools */
986 cmd = utils_get_setting_string(config, "tools", "terminal_cmd", "");
987 if (EMPTY(cmd))
989 SETPTR(cmd, utils_get_setting_string(config, "tools", "term_cmd", ""));
990 if (!EMPTY(cmd))
992 tmp_string = cmd;
993 #ifdef G_OS_WIN32
994 if (strstr(cmd, "cmd.exe"))
995 cmd = g_strconcat(cmd, " /Q /C %c", NULL);
996 else
997 cmd = g_strconcat(cmd, " %c", NULL);
998 #else
999 cmd = g_strconcat(cmd, " -e \"/bin/sh %c\"", NULL);
1000 #endif
1001 g_free(tmp_string);
1003 else
1004 SETPTR(cmd, g_strdup(GEANY_DEFAULT_TOOLS_TERMINAL));
1006 tool_prefs.term_cmd = cmd;
1007 tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
1008 tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
1010 tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
1012 /* printing */
1013 tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
1015 if (!EMPTY(tmp_string2))
1017 #ifdef G_OS_WIN32
1018 tmp_string = g_strconcat(GEANY_DEFAULT_TOOLS_PRINTCMD, " \"%f\"", NULL);
1019 #else
1020 tmp_string = g_strconcat(GEANY_DEFAULT_TOOLS_PRINTCMD, " '%f'", NULL);
1021 #endif
1023 else
1025 tmp_string = g_strdup("");
1028 printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
1029 g_free(tmp_string);
1030 g_free(tmp_string2);
1032 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
1033 printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
1034 printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
1035 printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
1036 printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
1037 printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
1041 static void load_ui_prefs(GKeyFile *config)
1043 ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
1044 ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
1045 ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
1046 ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
1047 ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
1048 ui_prefs.custom_commands_labels = g_key_file_get_string_list(config, PACKAGE, "custom_commands_labels", NULL, NULL);
1049 ui_prefs.color_picker_palette = utils_get_setting_string(config, PACKAGE, "color_picker_palette", "");
1051 /* Load the saved color picker palette */
1052 if (!EMPTY(ui_prefs.color_picker_palette))
1054 GtkSettings *settings;
1055 settings = gtk_settings_get_for_screen(gtk_window_get_screen(GTK_WINDOW(main_widgets.window)));
1056 g_object_set(G_OBJECT(settings), "gtk-color-palette", ui_prefs.color_picker_palette, NULL);
1059 /* sanitize custom commands labels */
1060 if (ui_prefs.custom_commands || ui_prefs.custom_commands_labels)
1062 guint i;
1063 guint cc_len = ui_prefs.custom_commands ? g_strv_length(ui_prefs.custom_commands) : 0;
1064 guint cc_labels_len = ui_prefs.custom_commands_labels ? g_strv_length(ui_prefs.custom_commands_labels) : 0;
1066 /* not enough items, resize and fill */
1067 if (cc_labels_len < cc_len)
1069 ui_prefs.custom_commands_labels = g_realloc(ui_prefs.custom_commands_labels,
1070 (cc_len + 1) * sizeof *ui_prefs.custom_commands_labels);
1071 for (i = cc_labels_len; i < cc_len; i++)
1072 ui_prefs.custom_commands_labels[i] = g_strdup("");
1073 ui_prefs.custom_commands_labels[cc_len] = NULL;
1075 /* too many items, cut off */
1076 else if (cc_labels_len > cc_len)
1078 for (i = cc_len; i < cc_labels_len; i++)
1080 g_free(ui_prefs.custom_commands_labels[i]);
1081 ui_prefs.custom_commands_labels[i] = NULL;
1086 scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
1087 _("Type here what you want, use it as a notice/scratch board"));
1088 scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
1091 static void load_ui_session(GKeyFile *config)
1093 gint *geo;
1094 gsize geo_len;
1096 geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", &geo_len, NULL);
1097 if (! geo || geo_len < 5)
1099 ui_prefs.geometry[0] = -1;
1100 ui_prefs.geometry[1] = -1;
1101 ui_prefs.geometry[2] = -1;
1102 ui_prefs.geometry[3] = -1;
1103 ui_prefs.geometry[4] = 0;
1105 else
1107 /* don't use insane values but when main windows was maximized last time, pos might be
1108 * negative (due to differences in root window and window decorations) */
1109 /* quitting when minimized can make pos -32000, -32000 on Windows! */
1110 ui_prefs.geometry[0] = MAX(-1, geo[0]);
1111 ui_prefs.geometry[1] = MAX(-1, geo[1]);
1112 ui_prefs.geometry[2] = MAX(-1, geo[2]);
1113 ui_prefs.geometry[3] = MAX(-1, geo[3]);
1114 ui_prefs.geometry[4] = geo[4] != 0;
1116 hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
1117 vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
1118 (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
1119 (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
1121 g_free(geo);
1126 * Save current session in default configuration file
1128 void configuration_save_default_session(void)
1130 gchar *configfile = g_build_filename(app->configdir, SESSION_FILE, NULL);
1131 gchar *data;
1132 GKeyFile *config = g_key_file_new();
1134 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1136 if (cl_options.load_session)
1137 configuration_save_session_files(config);
1139 /* write the file */
1140 data = g_key_file_to_data(config, NULL, NULL);
1141 utils_write_file(configfile, data);
1142 g_free(data);
1144 g_key_file_free(config);
1145 g_free(configfile);
1149 void configuration_clear_default_session(void)
1151 gchar *configfile = g_build_filename(app->configdir, SESSION_FILE, NULL);
1152 gchar *data;
1153 GKeyFile *config = g_key_file_new();
1155 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1157 if (cl_options.load_session)
1158 remove_session_files(config);
1160 /* write the file */
1161 data = g_key_file_to_data(config, NULL, NULL);
1162 utils_write_file(configfile, data);
1163 g_free(data);
1165 g_key_file_free(config);
1166 g_free(configfile);
1171 * Only reload the session part of the default configuration
1173 void configuration_load_default_session(void)
1175 gchar *configfile = g_build_filename(app->configdir, SESSION_FILE, NULL);
1176 GKeyFile *config = g_key_file_new();
1178 g_return_if_fail(default_session_files == NULL);
1180 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1181 g_free(configfile);
1183 default_session_files = configuration_load_session_files(config);
1185 g_key_file_free(config);
1188 gboolean read_config_file(gchar const *filename, ConfigPayload payload)
1190 gchar *configfile = g_build_filename(app->configdir, filename, NULL);
1191 GKeyFile *config = g_key_file_new();
1193 if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
1194 { /* config file does not (yet) exist, so try to load a global config file which may be */
1195 /* created by distributors */
1196 geany_debug("No user config file found, trying to use global configuration.");
1197 SETPTR(configfile, g_build_filename(app->datadir, filename, NULL));
1199 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1200 g_free(configfile);
1202 /* read stash prefs */
1203 settings_action(config, SETTING_READ, payload);
1205 switch (payload)
1207 case PREFS:
1208 load_dialog_prefs(config);
1209 load_ui_prefs(config);
1211 /* build menu, after stash prefs as it uses some of them */
1212 build_set_group_count(GEANY_GBG_FT, build_menu_prefs.number_ft_menu_items);
1213 build_set_group_count(GEANY_GBG_NON_FT, build_menu_prefs.number_non_ft_menu_items);
1214 build_set_group_count(GEANY_GBG_EXEC, build_menu_prefs.number_exec_menu_items);
1215 build_load_menu(config, GEANY_BCS_PREF, NULL);
1216 /* this signal can be used e.g. to delay building UI elements until settings have been read */
1217 g_signal_emit_by_name(geany_object, "load-settings", config);
1218 break;
1219 case SESSION:
1220 project_load_prefs(config);
1221 load_ui_session(config);
1222 load_recent_files(config, ui_prefs.recent_queue, "recent_files");
1223 load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
1224 break;
1227 g_key_file_free(config);
1228 return TRUE;
1232 gboolean configuration_load(void)
1234 gboolean prefs_loaded = read_config_file(PREFS_FILE, PREFS);
1235 /* backwards-compatibility: try to read session from preferences if session file doesn't exist */
1236 gchar *session_filename = SESSION_FILE;
1237 gchar *session_file = g_build_filename(app->configdir, session_filename, NULL);
1238 if (! g_file_test(session_file, G_FILE_TEST_IS_REGULAR))
1240 geany_debug("No user session file found, trying to use configuration file.");
1241 session_filename = PREFS_FILE;
1243 g_free(session_file);
1244 gboolean sess_loaded = read_config_file(session_filename, SESSION);
1245 return prefs_loaded && sess_loaded;
1249 static gboolean open_session_file(gchar **tmp, guint len)
1251 guint pos;
1252 const gchar *ft_name;
1253 gchar *locale_filename;
1254 gchar *unescaped_filename;
1255 const gchar *encoding;
1256 gint indent_type;
1257 gboolean ro, auto_indent, line_wrapping;
1258 /** TODO when we have a global pref for line breaking, use its value */
1259 gboolean line_breaking = FALSE;
1260 gboolean ret = FALSE;
1262 pos = atoi(tmp[0]);
1263 ft_name = tmp[1];
1264 ro = atoi(tmp[2]);
1265 if (isdigit(tmp[3][0]))
1267 encoding = encodings_get_charset_from_index(atoi(tmp[3]));
1269 else
1271 encoding = &(tmp[3][1]);
1273 indent_type = atoi(tmp[4]);
1274 auto_indent = atoi(tmp[5]);
1275 line_wrapping = atoi(tmp[6]);
1276 /* try to get the locale equivalent for the filename */
1277 unescaped_filename = g_uri_unescape_string(tmp[7], NULL);
1278 locale_filename = utils_get_locale_from_utf8(unescaped_filename);
1280 if (len > 8)
1281 line_breaking = atoi(tmp[8]);
1283 if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR))
1285 GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
1286 GeanyDocument *doc = document_open_file_full(
1287 NULL, locale_filename, pos, ro, ft, encoding);
1289 if (doc)
1291 gint indent_width = doc->editor->indent_width;
1293 if (len > 9)
1294 indent_width = atoi(tmp[9]);
1295 editor_set_indent(doc->editor, indent_type, indent_width);
1296 editor_set_line_wrapping(doc->editor, line_wrapping);
1297 doc->editor->line_breaking = line_breaking;
1298 doc->editor->auto_indent = auto_indent;
1299 ret = TRUE;
1302 else
1304 geany_debug("Could not find file '%s'.", unescaped_filename);
1307 g_free(locale_filename);
1308 g_free(unescaped_filename);
1309 return ret;
1312 /* trigger a notebook page switch after unsetting main_status.opening_session_files
1313 * for callbacks to run (and update window title, encoding settings, and so on)
1315 static gboolean switch_to_session_page(gpointer data)
1317 gint n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1318 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
1319 gint target_page = session_notebook_page >= 0 ? session_notebook_page : cur_page;
1321 if (n_pages > 0)
1323 if (target_page != cur_page)
1324 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), target_page);
1325 else
1326 g_signal_emit_by_name(GTK_NOTEBOOK(main_widgets.notebook), "switch-page",
1327 gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.notebook), target_page),
1328 target_page);
1330 session_notebook_page = -1;
1332 return G_SOURCE_REMOVE;
1335 /* Open session files
1336 * Note: notebook page switch handler and adding to recent files list is always disabled
1337 * for all files opened within this function */
1338 void configuration_open_files(GPtrArray *session_files)
1340 gint i;
1341 gboolean failure = FALSE;
1343 /* necessary to set it to TRUE for project session support */
1344 main_status.opening_session_files++;
1346 i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
1347 while (TRUE)
1349 gchar **tmp = g_ptr_array_index(session_files, i);
1350 guint len;
1352 if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
1354 if (! open_session_file(tmp, len))
1355 failure = TRUE;
1357 g_strfreev(tmp);
1359 if (file_prefs.tab_order_ltr)
1361 i++;
1362 if (i >= (gint)session_files->len)
1363 break;
1365 else
1367 i--;
1368 if (i < 0)
1369 break;
1373 g_ptr_array_free(session_files, TRUE);
1375 if (failure)
1376 ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
1377 else
1378 g_idle_add(switch_to_session_page, NULL);
1380 main_status.opening_session_files--;
1384 /* Open session files
1385 * Note: notebook page switch handler and adding to recent files list is always disabled
1386 * for all files opened within this function */
1387 void configuration_open_default_session(void)
1389 g_return_if_fail(default_session_files != NULL);
1391 configuration_open_files(default_session_files);
1392 default_session_files = NULL;
1396 /* set some settings which are already read from the config file, but need other things, like the
1397 * realisation of the main window */
1398 void configuration_apply_settings(void)
1400 if (scribble_text)
1401 { /* update the scribble widget, because now it's realized */
1402 GtkTextIter iter;
1403 GtkTextBuffer *buffer =
1404 gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
1406 gtk_text_buffer_set_text(buffer, scribble_text, -1);
1407 gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
1408 gtk_text_buffer_place_cursor(buffer, &iter);
1410 g_free(scribble_text);
1412 /* set the position of the hpaned and vpaned */
1413 if (prefs.save_winpos)
1415 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
1416 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
1419 /* set fullscreen after initial draw so that returning to normal view is the right size.
1420 * fullscreen mode is disabled by default, so act only if it is true */
1421 if (ui_prefs.fullscreen)
1423 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
1424 ui_prefs.fullscreen = TRUE;
1425 ui_set_fullscreen();
1428 msgwin_show_hide_tabs();
1432 static gboolean save_configuration_cb(gpointer data)
1434 if (app->project != NULL)
1435 project_write_config();
1436 else
1437 configuration_save_default_session();
1438 return G_SOURCE_REMOVE;
1442 static void document_list_changed_cb(GObject *obj, GeanyDocument *doc, gpointer data)
1444 g_return_if_fail(doc != NULL && doc->is_valid);
1446 /* save configuration, especially session file list, but only if we are not just starting
1447 * and not about to quit */
1448 if (file_prefs.save_config_on_file_change &&
1449 main_status.main_window_realized &&
1450 !main_status.opening_session_files &&
1451 !main_status.quitting)
1453 g_idle_remove_by_data(save_configuration_cb);
1454 g_idle_add(save_configuration_cb, save_configuration_cb);
1459 void configuration_init(void)
1461 keyfile_groups[PREFS] = g_ptr_array_new_with_free_func((GDestroyNotify) stash_group_free);
1462 keyfile_groups[SESSION] = g_ptr_array_new_with_free_func((GDestroyNotify) stash_group_free);
1463 pref_groups = g_ptr_array_new();
1464 init_pref_groups();
1466 g_signal_connect(geany_object, "document-open", G_CALLBACK(document_list_changed_cb), NULL);
1467 g_signal_connect(geany_object, "document-save", G_CALLBACK(document_list_changed_cb), NULL);
1468 g_signal_connect(geany_object, "document-close", G_CALLBACK(document_list_changed_cb), NULL);
1472 void configuration_finalize(void)
1474 g_signal_handlers_disconnect_by_func(geany_object, G_CALLBACK(document_list_changed_cb), NULL);
1476 g_ptr_array_free(pref_groups, TRUE);
1477 g_ptr_array_free(keyfile_groups[SESSION], TRUE);
1478 g_ptr_array_free(keyfile_groups[PREFS], TRUE);