VTE: Remove support for background image
[geany-mirror.git] / src / keyfile.c
blob6a91988b5d9fe2c0faa659d12fc714f4908158f3
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 "encodingsprivate.h"
42 #include "filetypes.h"
43 #include "geanyobject.h"
44 #include "main.h"
45 #include "msgwindow.h"
46 #include "prefs.h"
47 #include "printing.h"
48 #include "project.h"
49 #include "sciwrappers.h"
50 #include "stash.h"
51 #include "support.h"
52 #include "symbols.h"
53 #include "templates.h"
54 #include "toolbar.h"
55 #include "ui_utils.h"
56 #include "utils.h"
57 #include "vte.h"
59 #include <stdlib.h>
60 #include <string.h>
61 #include <ctype.h>
63 #ifdef HAVE_VTE
64 #include <pwd.h>
65 #include <sys/types.h>
66 #include <unistd.h>
67 #endif
70 /* some default settings which are used at the very first start of Geany to fill
71 * the configuration file */
72 #define GEANY_MAX_SYMBOLLIST_HEIGHT 10
73 #define GEANY_MIN_SYMBOLLIST_CHARS 4
74 #define GEANY_MSGWIN_HEIGHT 208
75 #define GEANY_DISK_CHECK_TIMEOUT 30
76 #define GEANY_DEFAULT_TOOLS_MAKE "make"
77 #ifdef G_OS_WIN32
78 #define GEANY_DEFAULT_TOOLS_TERMINAL "cmd.exe /Q /C %c"
79 #elif defined(__APPLE__)
80 #define GEANY_DEFAULT_TOOLS_TERMINAL "open -a terminal %c"
81 #else
82 #define GEANY_DEFAULT_TOOLS_TERMINAL "xterm -e \"/bin/sh %c\""
83 #endif
84 #ifdef __APPLE__
85 #define GEANY_DEFAULT_TOOLS_BROWSER "open -a safari"
86 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Helvetica Medium 12"
87 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Menlo Medium 12"
88 #define GEANY_DEFAULT_FONT_EDITOR "Menlo Medium 12"
89 #else
90 #define GEANY_DEFAULT_TOOLS_BROWSER "firefox"
91 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Sans 9"
92 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Monospace 9"
93 #define GEANY_DEFAULT_FONT_EDITOR "Monospace 10"
94 #endif
95 #define GEANY_DEFAULT_TOOLS_PRINTCMD "lpr"
96 #define GEANY_DEFAULT_TOOLS_GREP "grep"
97 #define GEANY_DEFAULT_MRU_LENGTH 10
98 #define GEANY_TOGGLE_MARK "~ "
99 #define GEANY_MAX_AUTOCOMPLETE_WORDS 30
100 #define GEANY_MAX_SYMBOLS_UPDATE_FREQ 250
101 #define GEANY_DEFAULT_FILETYPE_REGEX "-\\*-\\s*([^\\s]+)\\s*-\\*-"
104 static gchar *scribble_text = NULL;
105 static gint scribble_pos = -1;
106 static GPtrArray *session_files = NULL;
107 static gint session_notebook_page;
108 static gint hpan_position;
109 static gint vpan_position;
110 static const gchar atomic_file_saving_key[] = "use_atomic_file_saving";
112 static GPtrArray *keyfile_groups = NULL;
114 GPtrArray *pref_groups = NULL;
116 static struct
118 gint number_ft_menu_items;
119 gint number_non_ft_menu_items;
120 gint number_exec_menu_items;
122 build_menu_prefs;
125 /* The group will be free'd on quitting.
126 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
127 void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
129 g_ptr_array_add(keyfile_groups, group);
131 if (for_prefs_dialog)
132 g_ptr_array_add(pref_groups, group);
136 /* The group will be free'd on quitting. */
137 void configuration_add_various_pref_group(struct StashGroup *group)
139 configuration_add_pref_group(group, TRUE);
140 stash_group_set_various(group, TRUE);
144 static void init_pref_groups(void)
146 StashGroup *group;
148 group = stash_group_new(PACKAGE);
149 configuration_add_pref_group(group, TRUE);
150 stash_group_add_entry(group, &prefs.default_open_path,
151 "default_open_path", "", "startup_path_entry");
153 stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
154 "cmdline_new_files", TRUE, "check_cmdline_new_files");
156 stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
157 "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
158 stash_group_add_toggle_button(group, &file_prefs.tab_close_switch_to_mru,
159 "tab_close_switch_to_mru", FALSE, "check_tab_close_switch_to_mru");
160 stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
161 stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
162 "sidebar_pos", GTK_POS_LEFT,
163 "radio_sidebar_left", GTK_POS_LEFT,
164 "radio_sidebar_right", GTK_POS_RIGHT,
165 NULL);
166 stash_group_add_radio_buttons(group, &interface_prefs.symbols_sort_mode,
167 "symbols_sort_mode", SYMBOLS_SORT_BY_NAME,
168 "radio_symbols_sort_by_name", SYMBOLS_SORT_BY_NAME,
169 "radio_symbols_sort_by_appearance", SYMBOLS_SORT_BY_APPEARANCE,
170 NULL);
171 stash_group_add_radio_buttons(group, &interface_prefs.msgwin_orientation,
172 "msgwin_orientation", GTK_ORIENTATION_VERTICAL,
173 "radio_msgwin_vertical", GTK_ORIENTATION_VERTICAL,
174 "radio_msgwin_horizontal", GTK_ORIENTATION_HORIZONTAL,
175 NULL);
177 /* editor display */
178 stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
179 "highlighting_invert_all", FALSE, "check_highlighting_invert");
181 stash_group_add_toggle_button(group, &search_prefs.use_current_word,
182 "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
184 /* editor */
185 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
186 "check_detect_indent", FALSE, "check_detect_indent_type");
187 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_width,
188 "detect_indent_width", FALSE, "check_detect_indent_width");
189 stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
190 "use_tab_to_indent", TRUE, "check_tab_key_indents");
191 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
192 "pref_editor_tab_width", 4, "spin_indent_width");
193 stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
194 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
195 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
196 "indent_type", GEANY_INDENT_TYPE_TABS,
197 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
198 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
199 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
200 NULL);
201 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
202 "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
203 "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
204 "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
205 "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
206 NULL);
207 stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
208 "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
209 stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
210 "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
211 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
212 "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
213 "spin_autocompletion_max_entries");
214 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_update_freq,
215 "autocompletion_update_freq", GEANY_MAX_SYMBOLS_UPDATE_FREQ, "spin_symbol_update_freq");
216 stash_group_add_string(group, &editor_prefs.color_scheme,
217 "color_scheme", NULL);
218 stash_group_add_spin_button_integer(group, &editor_prefs.scroll_lines_around_cursor,
219 "scroll_lines_around_cursor", 0, "spin_scroll_lines_around_cursor");
221 /* files */
222 stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
223 "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
224 stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
225 "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
227 /* various geany prefs */
228 group = stash_group_new(PACKAGE);
229 configuration_add_various_pref_group(group);
231 stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
232 "show_editor_scrollbars", TRUE);
233 stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
234 "brace_match_ltgt", FALSE);
235 stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
236 "use_gtk_word_boundaries", TRUE);
237 stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
238 "complete_snippets_whilst_editing", FALSE);
239 stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
240 atomic_file_saving_key, FALSE);
241 stash_group_add_boolean(group, &file_prefs.gio_unsafe_save_backup,
242 "gio_unsafe_save_backup", FALSE);
243 stash_group_add_boolean(group, &file_prefs.use_gio_unsafe_file_saving,
244 "use_gio_unsafe_file_saving", TRUE);
245 stash_group_add_boolean(group, &file_prefs.keep_edit_history_on_reload,
246 "keep_edit_history_on_reload", TRUE);
247 stash_group_add_boolean(group, &file_prefs.show_keep_edit_history_on_reload_msg,
248 "show_keep_edit_history_on_reload_msg", TRUE);
249 /* for backwards-compatibility */
250 stash_group_add_integer(group, &editor_prefs.indentation->hard_tab_width,
251 "indent_hard_tab_width", 8);
252 stash_group_add_integer(group, (gint*)&search_prefs.find_selection_type,
253 "find_selection_type", GEANY_FIND_SEL_CURRENT_WORD);
254 stash_group_add_string(group, &file_prefs.extract_filetype_regex,
255 "extract_filetype_regex", GEANY_DEFAULT_FILETYPE_REGEX);
256 stash_group_add_boolean(group, &search_prefs.replace_and_find_by_default,
257 "replace_and_find_by_default", TRUE);
259 /* Note: Interface-related various prefs are in ui_init_prefs() */
261 /* various build-menu prefs */
262 group = stash_group_new("build-menu");
263 configuration_add_various_pref_group(group);
265 stash_group_add_integer(group, &build_menu_prefs.number_ft_menu_items,
266 "number_ft_menu_items", 0);
267 stash_group_add_integer(group, &build_menu_prefs.number_non_ft_menu_items,
268 "number_non_ft_menu_items", 0);
269 stash_group_add_integer(group, &build_menu_prefs.number_exec_menu_items,
270 "number_exec_menu_items", 0);
274 typedef enum SettingAction
276 SETTING_READ,
277 SETTING_WRITE
279 SettingAction;
281 static void settings_action(GKeyFile *config, SettingAction action)
283 guint i;
284 StashGroup *group;
286 foreach_ptr_array(group, i, keyfile_groups)
288 switch (action)
290 case SETTING_READ:
291 stash_group_load_from_key_file(group, config); break;
292 case SETTING_WRITE:
293 stash_group_save_to_key_file(group, config); break;
299 static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
301 gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
302 guint i;
304 for (i = 0; i < file_prefs.mru_length; i++)
306 if (! g_queue_is_empty(queue))
308 /* copy the values, this is necessary when this function is called from the
309 * preferences dialog or when quitting is canceled to keep the queue intact */
310 recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
312 else
314 recent_files[i] = NULL;
315 break;
318 /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
319 recent_files[file_prefs.mru_length] = NULL;
320 g_key_file_set_string_list(config, "files", key,
321 (const gchar**)recent_files, file_prefs.mru_length);
322 g_strfreev(recent_files);
326 static gchar *get_session_file_string(GeanyDocument *doc)
328 gchar *fname;
329 gchar *locale_filename;
330 gchar *escaped_filename;
331 GeanyFiletype *ft = doc->file_type;
333 if (ft == NULL) /* can happen when saving a new file when quitting */
334 ft = filetypes[GEANY_FILETYPES_NONE];
336 locale_filename = utils_get_locale_from_utf8(doc->file_name);
337 escaped_filename = g_uri_escape_string(locale_filename, NULL, TRUE);
339 fname = g_strdup_printf("%d;%s;%d;E%s;%d;%d;%d;%s;%d;%d",
340 sci_get_current_position(doc->editor->sci),
341 ft->name,
342 doc->readonly,
343 doc->encoding,
344 doc->editor->indent_type,
345 doc->editor->auto_indent,
346 doc->editor->line_wrapping,
347 escaped_filename,
348 doc->editor->line_breaking,
349 doc->editor->indent_width);
350 g_free(escaped_filename);
351 g_free(locale_filename);
352 return fname;
356 static void remove_session_files(GKeyFile *config)
358 gchar **ptr;
359 gchar **keys = g_key_file_get_keys(config, "files", NULL, NULL);
361 foreach_strv(ptr, keys)
363 if (g_str_has_prefix(*ptr, "FILE_NAME_"))
364 g_key_file_remove_key(config, "files", *ptr, NULL);
366 g_strfreev(keys);
370 void configuration_save_session_files(GKeyFile *config)
372 gint npage;
373 gchar entry[16];
374 guint i = 0, j = 0, max;
376 npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
377 g_key_file_set_integer(config, "files", "current_page", npage);
379 // clear existing entries first as they might not all be overwritten
380 remove_session_files(config);
382 /* store the filenames in the notebook tab order to reopen them the next time */
383 max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
384 for (i = 0; i < max; i++)
386 GeanyDocument *doc = document_get_from_page(i);
388 if (doc != NULL && doc->real_path != NULL)
390 gchar *fname;
392 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
393 fname = get_session_file_string(doc);
394 g_key_file_set_string(config, "files", entry, fname);
395 g_free(fname);
396 j++;
400 #ifdef HAVE_VTE
401 if (vte_info.have_vte)
403 vte_get_working_directory(); /* refresh vte_info.dir */
404 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
406 #endif
410 static void save_dialog_prefs(GKeyFile *config)
412 /* new settings should be added in init_pref_groups() */
413 settings_action(config, SETTING_WRITE);
415 /* Some of the key names are not consistent, but this is for backwards compatibility */
417 /* general */
418 g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
419 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_session", project_prefs.project_session);
420 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
421 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
422 g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
423 g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
424 g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
425 g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
426 g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
428 /* interface */
429 g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
430 g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
431 g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
432 g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
433 g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
434 g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
435 g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
436 g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
437 g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
438 g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
439 g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
440 g_key_file_set_boolean(config, PACKAGE, "use_native_windows_dialogs", interface_prefs.use_native_windows_dialogs);
442 /* display */
443 g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
444 g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
445 g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
446 g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
447 g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
448 g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_enabled);
449 g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_type);
450 g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_column);
451 g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
453 /* editor */
454 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
455 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
456 g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
457 g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
458 g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
459 g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
460 g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
461 g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
462 g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
463 g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
464 g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
465 g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
466 g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
467 g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
468 g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
469 g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
470 g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
472 /* files */
473 g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
474 if (file_prefs.default_open_encoding == -1)
475 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
476 else
477 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
478 g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
479 g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
480 g_key_file_set_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", file_prefs.ensure_convert_new_lines);
481 g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
482 g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
484 /* toolbar */
485 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
486 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
487 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
488 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
489 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
490 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
492 /* templates */
493 g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
494 g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
495 g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
496 g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
497 g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
498 g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
499 g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
500 g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
502 /* tools settings */
503 g_key_file_set_string(config, "tools", "terminal_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
504 g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
505 g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
506 g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
508 /* build menu */
509 build_save_menu(config, NULL, GEANY_BCS_PREF);
511 /* printing */
512 g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
513 g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
514 g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
515 g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
516 g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
517 g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
518 g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
520 /* VTE */
521 #ifdef HAVE_VTE
522 g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
523 if (vte_info.have_vte)
525 gchar *tmp_string;
527 g_key_file_set_string(config, "VTE", "font", vc->font);
528 g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
529 g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
530 g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
531 g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
532 g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
533 g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
534 g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
535 g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
536 g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
537 g_key_file_set_string(config, "VTE", "font", vc->font);
538 g_key_file_set_string(config, "VTE", "shell", vc->shell);
539 tmp_string = utils_get_hex_from_color(&vc->colour_fore);
540 g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
541 g_free(tmp_string);
542 tmp_string = utils_get_hex_from_color(&vc->colour_back);
543 g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
544 g_free(tmp_string);
546 #endif
550 static void save_ui_prefs(GKeyFile *config)
552 g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
553 g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
554 g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
555 g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
557 /* get the text from the scribble textview */
559 GtkTextBuffer *buffer;
560 GtkTextIter start, end, iter;
561 GtkTextMark *mark;
563 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
564 gtk_text_buffer_get_bounds(buffer, &start, &end);
565 scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
566 g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
567 g_free(scribble_text);
569 mark = gtk_text_buffer_get_insert(buffer);
570 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
571 scribble_pos = gtk_text_iter_get_offset(&iter);
572 g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
575 if (prefs.save_winpos)
577 GdkWindowState wstate;
579 g_key_file_set_integer(config, PACKAGE, "treeview_position",
580 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
581 g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
582 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
584 gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
585 gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
586 wstate = gdk_window_get_state(gtk_widget_get_window(main_widgets.window));
587 ui_prefs.geometry[4] = (wstate & GDK_WINDOW_STATE_MAXIMIZED) ? 1 : 0;
588 g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
591 g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
592 if (ui_prefs.custom_commands != NULL)
594 g_key_file_set_string_list(config, PACKAGE, "custom_commands",
595 (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
596 g_key_file_set_string_list(config, PACKAGE, "custom_commands_labels",
597 (const gchar**) ui_prefs.custom_commands_labels, g_strv_length(ui_prefs.custom_commands_labels));
602 void configuration_save(void)
604 GKeyFile *config = g_key_file_new();
605 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
606 gchar *data;
608 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
610 /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
611 g_signal_emit_by_name(geany_object, "save-settings", config);
613 save_dialog_prefs(config);
614 save_ui_prefs(config);
615 project_save_prefs(config); /* save project filename, etc. */
616 save_recent_files(config, ui_prefs.recent_queue, "recent_files");
617 save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
619 if (cl_options.load_session)
620 configuration_save_session_files(config);
621 #ifdef HAVE_VTE
622 else if (vte_info.have_vte)
624 vte_get_working_directory(); /* refresh vte_info.dir */
625 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
627 #endif
629 /* write the file */
630 data = g_key_file_to_data(config, NULL, NULL);
631 utils_write_file(configfile, data);
632 g_free(data);
634 g_key_file_free(config);
635 g_free(configfile);
639 static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
641 gchar **recent_files;
642 gsize i, len = 0;
644 recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
645 if (recent_files != NULL)
647 for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
649 gchar *filename = g_strdup(recent_files[i]);
650 g_queue_push_tail(queue, filename);
652 g_strfreev(recent_files);
658 * Load session list from the given keyfile, and store it in the global
659 * session_files variable for later file loading
660 * */
661 void configuration_load_session_files(GKeyFile *config, gboolean read_recent_files)
663 guint i;
664 gboolean have_session_files;
665 gchar entry[16];
666 gchar **tmp_array;
667 GError *error = NULL;
669 session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
671 if (read_recent_files)
673 load_recent_files(config, ui_prefs.recent_queue, "recent_files");
674 load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
677 /* the project may load another list than the main setting */
678 if (session_files != NULL)
680 foreach_ptr_array(tmp_array, i, session_files)
681 g_strfreev(tmp_array);
682 g_ptr_array_free(session_files, TRUE);
685 session_files = g_ptr_array_new();
686 have_session_files = TRUE;
687 i = 0;
688 while (have_session_files)
690 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
691 tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
692 if (! tmp_array || error)
694 g_error_free(error);
695 error = NULL;
696 have_session_files = FALSE;
698 g_ptr_array_add(session_files, tmp_array);
699 i++;
702 #ifdef HAVE_VTE
703 /* BUG: after loading project at startup, closing project doesn't restore old VTE path */
704 if (vte_info.have_vte)
706 gchar *tmp_string = utils_get_setting_string(config, "VTE", "last_dir", NULL);
707 vte_cwd(tmp_string,TRUE);
708 g_free(tmp_string);
710 #endif
714 #ifdef HAVE_VTE
715 static void get_setting_color(GKeyFile *config, const gchar *section, const gchar *key,
716 GdkColor *color, const gchar *default_color)
718 gchar *str = utils_get_setting_string(config, section, key, NULL);
719 if (str == NULL || ! utils_parse_color(str, color))
720 utils_parse_color(default_color, color);
721 g_free(str);
723 #endif
726 /* note: new settings should be added in init_pref_groups() */
727 static void load_dialog_prefs(GKeyFile *config)
729 gchar *tmp_string, *tmp_string2;
730 const gchar *default_charset = NULL;
731 gchar *cmd;
733 /* compatibility with Geany 0.20 */
734 if (!g_key_file_has_key(config, PACKAGE, atomic_file_saving_key, NULL))
736 g_key_file_set_boolean(config, PACKAGE, atomic_file_saving_key,
737 utils_get_setting_boolean(config, PACKAGE, "use_safe_file_saving", FALSE));
740 /* compatibility with Geany 0.21 */
742 gboolean suppress_search_dialogs = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_search_dialogs", FALSE);
744 if (!g_key_file_has_key(config, "search", "pref_search_always_wrap", NULL))
745 g_key_file_set_boolean(config, "search", "pref_search_always_wrap", suppress_search_dialogs);
747 if (!g_key_file_has_key(config, "search", "pref_search_hide_find_dialog", NULL))
748 g_key_file_set_boolean(config, "search", "pref_search_hide_find_dialog", suppress_search_dialogs);
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 && vte_info.load_vte_cmdline /* not disabled on the cmdline */)
861 StashGroup *group;
862 struct passwd *pw = getpwuid(getuid());
863 const gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
865 #ifdef __APPLE__
866 /* Geany is started using launchd on OS X and we don't get any environment variables
867 * so PS1 isn't defined. Start as a login shell to read the corresponding config files. */
868 if (strcmp(shell, "/bin/bash") == 0)
869 shell = "/bin/bash -l";
870 #endif
872 vc = g_new0(VteConfig, 1);
873 vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
874 if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
875 /* last dir is not set, fallback to user's home directory */
876 SETPTR(vte_info.dir, g_strdup(pw->pw_dir));
877 else if (vte_info.dir == NULL && pw == NULL)
878 /* fallback to root */
879 vte_info.dir = g_strdup("/");
881 vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
882 vc->font = utils_get_setting_string(config, "VTE", "font", GEANY_DEFAULT_FONT_EDITOR);
883 vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
884 vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
885 vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", TRUE);
886 vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
887 vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
888 vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
889 vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
890 vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
891 vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
892 get_setting_color(config, "VTE", "colour_fore", &vc->colour_fore, "#ffffff");
893 get_setting_color(config, "VTE", "colour_back", &vc->colour_back, "#000000");
895 /* various VTE prefs.
896 * this can't be done in init_pref_groups() because we need to know the value of
897 * vte_info.load_vte, and `vc` to be initialized */
898 group = stash_group_new("VTE");
899 configuration_add_various_pref_group(group);
901 stash_group_add_string(group, &vc->send_cmd_prefix, "send_cmd_prefix", "");
902 stash_group_add_boolean(group, &vc->send_selection_unsafe, "send_selection_unsafe", FALSE);
904 #endif
905 /* templates */
906 template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
907 template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
908 tmp_string = utils_get_initials(template_prefs.developer);
909 template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
910 g_free(tmp_string);
912 template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
914 tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), g_get_host_name());
915 template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
916 g_free(tmp_string);
917 template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", GEANY_TEMPLATES_FORMAT_YEAR);
918 template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", GEANY_TEMPLATES_FORMAT_DATE);
919 template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", GEANY_TEMPLATES_FORMAT_DATETIME);
921 /* tools */
922 cmd = utils_get_setting_string(config, "tools", "terminal_cmd", "");
923 if (EMPTY(cmd))
925 SETPTR(cmd, utils_get_setting_string(config, "tools", "term_cmd", ""));
926 if (!EMPTY(cmd))
928 tmp_string = cmd;
929 #ifdef G_OS_WIN32
930 if (strstr(cmd, "cmd.exe"))
931 cmd = g_strconcat(cmd, " /Q /C %c", NULL);
932 else
933 cmd = g_strconcat(cmd, " %c", NULL);
934 #else
935 cmd = g_strconcat(cmd, " -e \"/bin/sh %c\"", NULL);
936 #endif
937 g_free(tmp_string);
939 else
940 SETPTR(cmd, g_strdup(GEANY_DEFAULT_TOOLS_TERMINAL));
942 tool_prefs.term_cmd = cmd;
943 tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
944 tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
946 tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
948 /* printing */
949 tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
951 if (!EMPTY(tmp_string2))
953 #ifdef G_OS_WIN32
954 tmp_string = g_strconcat(GEANY_DEFAULT_TOOLS_PRINTCMD, " \"%f\"", NULL);
955 #else
956 tmp_string = g_strconcat(GEANY_DEFAULT_TOOLS_PRINTCMD, " '%f'", NULL);
957 #endif
959 else
961 tmp_string = g_strdup("");
964 printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
965 g_free(tmp_string);
966 g_free(tmp_string2);
968 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
969 printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
970 printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
971 printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
972 printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
973 printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
975 /* read stash prefs */
976 settings_action(config, SETTING_READ);
978 /* build menu
979 * after stash prefs as it uses some of them */
980 build_set_group_count(GEANY_GBG_FT, build_menu_prefs.number_ft_menu_items);
981 build_set_group_count(GEANY_GBG_NON_FT, build_menu_prefs.number_non_ft_menu_items);
982 build_set_group_count(GEANY_GBG_EXEC, build_menu_prefs.number_exec_menu_items);
983 build_load_menu(config, GEANY_BCS_PREF, NULL);
987 static void load_ui_prefs(GKeyFile *config)
989 gint *geo;
990 gsize geo_len;
992 ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
993 ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
994 ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
995 ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
996 ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
997 ui_prefs.custom_commands_labels = g_key_file_get_string_list(config, PACKAGE, "custom_commands_labels", NULL, NULL);
999 /* sanitize custom commands labels */
1000 if (ui_prefs.custom_commands || ui_prefs.custom_commands_labels)
1002 guint i;
1003 guint cc_len = ui_prefs.custom_commands ? g_strv_length(ui_prefs.custom_commands) : 0;
1004 guint cc_labels_len = ui_prefs.custom_commands_labels ? g_strv_length(ui_prefs.custom_commands_labels) : 0;
1006 /* not enough items, resize and fill */
1007 if (cc_labels_len < cc_len)
1009 ui_prefs.custom_commands_labels = g_realloc(ui_prefs.custom_commands_labels,
1010 (cc_len + 1) * sizeof *ui_prefs.custom_commands_labels);
1011 for (i = cc_labels_len; i < cc_len; i++)
1012 ui_prefs.custom_commands_labels[i] = g_strdup("");
1013 ui_prefs.custom_commands_labels[cc_len] = NULL;
1015 /* too many items, cut off */
1016 else if (cc_labels_len > cc_len)
1018 for (i = cc_len; i < cc_labels_len; i++)
1020 g_free(ui_prefs.custom_commands_labels[i]);
1021 ui_prefs.custom_commands_labels[i] = NULL;
1026 scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
1027 _("Type here what you want, use it as a notice/scratch board"));
1028 scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
1030 geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", &geo_len, NULL);
1031 if (! geo || geo_len < 5)
1033 ui_prefs.geometry[0] = -1;
1034 ui_prefs.geometry[1] = -1;
1035 ui_prefs.geometry[2] = -1;
1036 ui_prefs.geometry[3] = -1;
1037 ui_prefs.geometry[4] = 0;
1039 else
1041 /* don't use insane values but when main windows was maximized last time, pos might be
1042 * negative (due to differences in root window and window decorations) */
1043 /* quitting when minimized can make pos -32000, -32000 on Windows! */
1044 ui_prefs.geometry[0] = MAX(-1, geo[0]);
1045 ui_prefs.geometry[1] = MAX(-1, geo[1]);
1046 ui_prefs.geometry[2] = MAX(-1, geo[2]);
1047 ui_prefs.geometry[3] = MAX(-1, geo[3]);
1048 ui_prefs.geometry[4] = geo[4] != 0;
1050 hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
1051 vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
1052 (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
1053 (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
1055 g_free(geo);
1060 * Save current session in default configuration file
1062 void configuration_save_default_session(void)
1064 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1065 gchar *data;
1066 GKeyFile *config = g_key_file_new();
1068 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1070 if (cl_options.load_session)
1071 configuration_save_session_files(config);
1073 /* write the file */
1074 data = g_key_file_to_data(config, NULL, NULL);
1075 utils_write_file(configfile, data);
1076 g_free(data);
1078 g_key_file_free(config);
1079 g_free(configfile);
1083 void configuration_clear_default_session(void)
1085 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1086 gchar *data;
1087 GKeyFile *config = g_key_file_new();
1089 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1091 if (cl_options.load_session)
1092 remove_session_files(config);
1094 /* write the file */
1095 data = g_key_file_to_data(config, NULL, NULL);
1096 utils_write_file(configfile, data);
1097 g_free(data);
1099 g_key_file_free(config);
1100 g_free(configfile);
1105 * Only reload the session part of the default configuration
1107 void configuration_reload_default_session(void)
1109 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1110 GKeyFile *config = g_key_file_new();
1112 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1113 g_free(configfile);
1115 configuration_load_session_files(config, FALSE);
1117 g_key_file_free(config);
1121 gboolean configuration_load(void)
1123 gchar *configfile = g_build_filename(app->configdir, "geany.conf", NULL);
1124 GKeyFile *config = g_key_file_new();
1126 if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
1127 { /* config file does not (yet) exist, so try to load a global config file which may be */
1128 /* created by distributors */
1129 geany_debug("No user config file found, trying to use global configuration.");
1130 SETPTR(configfile, g_build_filename(app->datadir, "geany.conf", NULL));
1132 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1133 g_free(configfile);
1135 load_dialog_prefs(config);
1136 load_ui_prefs(config);
1137 project_load_prefs(config);
1138 configuration_load_session_files(config, TRUE);
1140 /* this signal can be used e.g. to delay building UI elements until settings have been read */
1141 g_signal_emit_by_name(geany_object, "load-settings", config);
1143 g_key_file_free(config);
1144 return TRUE;
1148 static gboolean open_session_file(gchar **tmp, guint len)
1150 guint pos;
1151 const gchar *ft_name;
1152 gchar *locale_filename;
1153 gchar *unescaped_filename;
1154 const gchar *encoding;
1155 gint indent_type;
1156 gboolean ro, auto_indent, line_wrapping;
1157 /** TODO when we have a global pref for line breaking, use its value */
1158 gboolean line_breaking = FALSE;
1159 gboolean ret = FALSE;
1161 pos = atoi(tmp[0]);
1162 ft_name = tmp[1];
1163 ro = atoi(tmp[2]);
1164 if (isdigit(tmp[3][0]))
1166 encoding = encodings_get_charset_from_index(atoi(tmp[3]));
1168 else
1170 encoding = &(tmp[3][1]);
1172 indent_type = atoi(tmp[4]);
1173 auto_indent = atoi(tmp[5]);
1174 line_wrapping = atoi(tmp[6]);
1175 /* try to get the locale equivalent for the filename */
1176 unescaped_filename = g_uri_unescape_string(tmp[7], NULL);
1177 locale_filename = utils_get_locale_from_utf8(unescaped_filename);
1179 if (len > 8)
1180 line_breaking = atoi(tmp[8]);
1182 if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR))
1184 GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
1185 GeanyDocument *doc = document_open_file_full(
1186 NULL, locale_filename, pos, ro, ft, encoding);
1188 if (doc)
1190 gint indent_width = doc->editor->indent_width;
1192 if (len > 9)
1193 indent_width = atoi(tmp[9]);
1194 editor_set_indent(doc->editor, indent_type, indent_width);
1195 editor_set_line_wrapping(doc->editor, line_wrapping);
1196 doc->editor->line_breaking = line_breaking;
1197 doc->editor->auto_indent = auto_indent;
1198 ret = TRUE;
1201 else
1203 geany_debug("Could not find file '%s'.", tmp[7]);
1206 g_free(locale_filename);
1207 g_free(unescaped_filename);
1208 return ret;
1212 /* Open session files
1213 * Note: notebook page switch handler and adding to recent files list is always disabled
1214 * for all files opened within this function */
1215 void configuration_open_files(void)
1217 gint i;
1218 gboolean failure = FALSE;
1220 /* necessary to set it to TRUE for project session support */
1221 main_status.opening_session_files = TRUE;
1223 i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
1224 while (TRUE)
1226 gchar **tmp = g_ptr_array_index(session_files, i);
1227 guint len;
1229 if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
1231 if (! open_session_file(tmp, len))
1232 failure = TRUE;
1234 g_strfreev(tmp);
1236 if (file_prefs.tab_order_ltr)
1238 i++;
1239 if (i >= (gint)session_files->len)
1240 break;
1242 else
1244 i--;
1245 if (i < 0)
1246 break;
1250 g_ptr_array_free(session_files, TRUE);
1251 session_files = NULL;
1253 if (failure)
1254 ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
1255 else
1257 /* explicitly trigger a notebook page switch after unsetting main_status.opening_session_files
1258 * for callbacks to run (and update window title, encoding settings, and so on) */
1259 gint n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1260 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
1261 gint target_page = session_notebook_page >= 0 ? session_notebook_page : cur_page;
1263 /* if target page is current page, switch to another page first to really trigger an event */
1264 if (target_page == cur_page && n_pages > 0)
1265 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), (cur_page + 1) % n_pages);
1267 main_status.opening_session_files = FALSE;
1268 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), target_page);
1270 main_status.opening_session_files = FALSE;
1274 /* set some settings which are already read from the config file, but need other things, like the
1275 * realisation of the main window */
1276 void configuration_apply_settings(void)
1278 if (scribble_text)
1279 { /* update the scribble widget, because now it's realized */
1280 GtkTextIter iter;
1281 GtkTextBuffer *buffer =
1282 gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
1284 gtk_text_buffer_set_text(buffer, scribble_text, -1);
1285 gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
1286 gtk_text_buffer_place_cursor(buffer, &iter);
1288 g_free(scribble_text);
1290 /* set the position of the hpaned and vpaned */
1291 if (prefs.save_winpos)
1293 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
1294 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
1297 /* set fullscreen after initial draw so that returning to normal view is the right size.
1298 * fullscreen mode is disabled by default, so act only if it is true */
1299 if (ui_prefs.fullscreen)
1301 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
1302 ui_prefs.fullscreen = TRUE;
1303 ui_set_fullscreen();
1306 msgwin_show_hide_tabs();
1310 void configuration_init(void)
1312 keyfile_groups = g_ptr_array_new();
1313 pref_groups = g_ptr_array_new();
1314 init_pref_groups();
1318 void configuration_finalize(void)
1320 guint i;
1321 StashGroup *group;
1323 foreach_ptr_array(group, i, keyfile_groups)
1324 stash_group_free(group);
1326 g_ptr_array_free(keyfile_groups, TRUE);
1327 g_ptr_array_free(pref_groups, TRUE);