Minor improvements related to the search entry
[geany-mirror.git] / src / keyfile.c
blob408c15107fa5615c1235d929dd9a25698a9815ec
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 "socket.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
69 /* define the configuration filenames */
70 #define PREFS_FILE "geany.conf"
71 #define SESSION_FILE "session.conf"
73 /* some default settings which are used at the very first start of Geany to fill
74 * the configuration file */
75 #define GEANY_MAX_SYMBOLLIST_HEIGHT 10
76 #define GEANY_MIN_SYMBOLLIST_CHARS 4
77 #define GEANY_MSGWIN_HEIGHT 208
78 #define GEANY_DISK_CHECK_TIMEOUT 30
79 #define GEANY_DEFAULT_TOOLS_MAKE "make"
80 #ifdef G_OS_WIN32
81 #define GEANY_DEFAULT_TOOLS_TERMINAL "cmd.exe /Q /C %c"
82 #elif defined(__APPLE__)
83 #define GEANY_DEFAULT_TOOLS_TERMINAL "open -a terminal %c"
84 #else
85 #define GEANY_DEFAULT_TOOLS_TERMINAL "xterm -e \"/bin/sh %c\""
86 #endif
87 #ifdef __APPLE__
88 #define GEANY_DEFAULT_TOOLS_BROWSER "open -a safari"
89 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Helvetica Medium 12"
90 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Menlo Medium 12"
91 #define GEANY_DEFAULT_FONT_EDITOR "Menlo Medium 12"
92 #else
93 #define GEANY_DEFAULT_TOOLS_BROWSER "firefox"
94 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Sans 9"
95 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Monospace 9"
96 #define GEANY_DEFAULT_FONT_EDITOR "Monospace 10"
97 #endif
98 #define GEANY_DEFAULT_TOOLS_PRINTCMD "lpr"
99 #define GEANY_DEFAULT_TOOLS_GREP "grep"
100 #define GEANY_DEFAULT_MRU_LENGTH 10
101 #define GEANY_TOGGLE_MARK "~ "
102 #define GEANY_MAX_AUTOCOMPLETE_WORDS 30
103 #define GEANY_MAX_SYMBOLS_UPDATE_FREQ 250
104 #define GEANY_DEFAULT_FILETYPE_REGEX "-\\*-\\s*([^\\s]+)\\s*-\\*-"
107 static gchar *scribble_text = NULL;
108 static gint scribble_pos = -1;
109 static GPtrArray *session_files = NULL;
110 static gint session_notebook_page;
111 static gint hpan_position;
112 static gint vpan_position;
113 static guint document_list_update_idle_func_id = 0;
114 static const gchar atomic_file_saving_key[] = "use_atomic_file_saving";
116 static GPtrArray *keyfile_groups = NULL;
118 GPtrArray *pref_groups = NULL;
120 static struct
122 gint number_ft_menu_items;
123 gint number_non_ft_menu_items;
124 gint number_exec_menu_items;
126 build_menu_prefs;
129 /* The group will be free'd on quitting.
130 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
131 void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
133 g_ptr_array_add(keyfile_groups, group);
135 if (for_prefs_dialog)
136 g_ptr_array_add(pref_groups, group);
140 /* The group will be free'd on quitting.
141 * prefix can be NULL to use group name */
142 void configuration_add_various_pref_group(struct StashGroup *group,
143 const gchar *prefix)
145 configuration_add_pref_group(group, TRUE);
146 stash_group_set_various(group, TRUE, prefix);
150 static void init_pref_groups(void)
152 StashGroup *group;
154 group = stash_group_new(PACKAGE);
155 configuration_add_pref_group(group, TRUE);
156 stash_group_add_entry(group, &prefs.default_open_path,
157 "default_open_path", "", "startup_path_entry");
159 stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
160 "cmdline_new_files", TRUE, "check_cmdline_new_files");
162 stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
163 "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
164 stash_group_add_toggle_button(group, &file_prefs.tab_close_switch_to_mru,
165 "tab_close_switch_to_mru", FALSE, "check_tab_close_switch_to_mru");
166 stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
167 stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
168 "sidebar_pos", GTK_POS_LEFT,
169 "radio_sidebar_left", GTK_POS_LEFT,
170 "radio_sidebar_right", GTK_POS_RIGHT,
171 NULL);
172 stash_group_add_radio_buttons(group, &interface_prefs.symbols_sort_mode,
173 "symbols_sort_mode", SYMBOLS_SORT_BY_NAME,
174 "radio_symbols_sort_by_name", SYMBOLS_SORT_BY_NAME,
175 "radio_symbols_sort_by_appearance", SYMBOLS_SORT_BY_APPEARANCE,
176 NULL);
177 stash_group_add_radio_buttons(group, &interface_prefs.msgwin_orientation,
178 "msgwin_orientation", GTK_ORIENTATION_VERTICAL,
179 "radio_msgwin_vertical", GTK_ORIENTATION_VERTICAL,
180 "radio_msgwin_horizontal", GTK_ORIENTATION_HORIZONTAL,
181 NULL);
183 /* editor display */
184 stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
185 "highlighting_invert_all", FALSE, "check_highlighting_invert");
187 stash_group_add_toggle_button(group, &search_prefs.use_current_word,
188 "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
190 /* editor */
191 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
192 "check_detect_indent", FALSE, "check_detect_indent_type");
193 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_width,
194 "detect_indent_width", FALSE, "check_detect_indent_width");
195 stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
196 "use_tab_to_indent", TRUE, "check_tab_key_indents");
197 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
198 "pref_editor_tab_width", 4, "spin_indent_width");
199 stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
200 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
201 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
202 "indent_type", GEANY_INDENT_TYPE_TABS,
203 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
204 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
205 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
206 NULL);
207 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
208 "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
209 "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
210 "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
211 "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
212 NULL);
213 stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
214 "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
215 stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
216 "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
217 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
218 "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
219 "spin_autocompletion_max_entries");
220 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_update_freq,
221 "autocompletion_update_freq", GEANY_MAX_SYMBOLS_UPDATE_FREQ, "spin_symbol_update_freq");
222 stash_group_add_string(group, &editor_prefs.color_scheme,
223 "color_scheme", NULL);
224 stash_group_add_spin_button_integer(group, &editor_prefs.scroll_lines_around_cursor,
225 "scroll_lines_around_cursor", 0, "spin_scroll_lines_around_cursor");
227 /* files */
228 stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
229 "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
230 stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
231 "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
233 /* various geany prefs */
234 group = stash_group_new(PACKAGE);
235 configuration_add_various_pref_group(group, "editor");
237 stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
238 "show_editor_scrollbars", TRUE);
239 stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
240 "brace_match_ltgt", FALSE);
241 stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
242 "use_gtk_word_boundaries", TRUE);
243 stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
244 "complete_snippets_whilst_editing", FALSE);
245 /* for backwards-compatibility */
246 stash_group_add_integer(group, &editor_prefs.indentation->hard_tab_width,
247 "indent_hard_tab_width", 8);
248 stash_group_add_integer(group, &editor_prefs.ime_interaction,
249 "editor_ime_interaction", SC_IME_WINDOWED);
251 group = stash_group_new(PACKAGE);
252 configuration_add_various_pref_group(group, "files");
254 stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
255 atomic_file_saving_key, FALSE);
256 stash_group_add_boolean(group, &file_prefs.gio_unsafe_save_backup,
257 "gio_unsafe_save_backup", FALSE);
258 stash_group_add_boolean(group, &file_prefs.use_gio_unsafe_file_saving,
259 "use_gio_unsafe_file_saving", TRUE);
260 stash_group_add_boolean(group, &file_prefs.keep_edit_history_on_reload,
261 "keep_edit_history_on_reload", TRUE);
262 stash_group_add_boolean(group, &file_prefs.show_keep_edit_history_on_reload_msg,
263 "show_keep_edit_history_on_reload_msg", TRUE);
264 stash_group_add_boolean(group, &file_prefs.reload_clean_doc_on_file_change,
265 "reload_clean_doc_on_file_change", FALSE);
266 stash_group_add_boolean(group, &file_prefs.save_config_on_file_change,
267 "save_config_on_file_change", TRUE);
268 stash_group_add_string(group, &file_prefs.extract_filetype_regex,
269 "extract_filetype_regex", GEANY_DEFAULT_FILETYPE_REGEX);
270 stash_group_add_boolean(group, &ui_prefs.allow_always_save,
271 "allow_always_save", FALSE);
273 group = stash_group_new(PACKAGE);
274 configuration_add_various_pref_group(group, "search");
276 stash_group_add_integer(group, (gint*)&search_prefs.find_selection_type,
277 "find_selection_type", GEANY_FIND_SEL_CURRENT_WORD);
278 stash_group_add_boolean(group, &search_prefs.replace_and_find_by_default,
279 "replace_and_find_by_default", TRUE);
281 group = stash_group_new(PACKAGE);
282 configuration_add_various_pref_group(group, "socket");
284 #ifdef G_OS_WIN32
285 stash_group_add_integer(group, (gint*)&prefs.socket_remote_cmd_port,
286 "socket_remote_cmd_port", SOCKET_WINDOWS_REMOTE_CMD_PORT);
287 #endif
289 /* Note: Interface-related various prefs are in ui_init_prefs() */
291 /* various build-menu prefs */
292 // Warning: don't move PACKAGE group name items here
293 group = stash_group_new("build-menu");
294 configuration_add_various_pref_group(group, "build");
296 stash_group_add_integer(group, &build_menu_prefs.number_ft_menu_items,
297 "number_ft_menu_items", 0);
298 stash_group_add_integer(group, &build_menu_prefs.number_non_ft_menu_items,
299 "number_non_ft_menu_items", 0);
300 stash_group_add_integer(group, &build_menu_prefs.number_exec_menu_items,
301 "number_exec_menu_items", 0);
305 typedef enum SettingAction
307 SETTING_READ,
308 SETTING_WRITE
310 SettingAction;
312 static void settings_action(GKeyFile *config, SettingAction action)
314 guint i;
315 StashGroup *group;
317 foreach_ptr_array(group, i, keyfile_groups)
319 switch (action)
321 case SETTING_READ:
322 stash_group_load_from_key_file(group, config); break;
323 case SETTING_WRITE:
324 stash_group_save_to_key_file(group, config); break;
330 static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
332 gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
333 guint i;
335 for (i = 0; i < file_prefs.mru_length; i++)
337 if (! g_queue_is_empty(queue))
339 /* copy the values, this is necessary when this function is called from the
340 * preferences dialog or when quitting is canceled to keep the queue intact */
341 recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
343 else
345 recent_files[i] = NULL;
346 break;
349 /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
350 recent_files[file_prefs.mru_length] = NULL;
351 g_key_file_set_string_list(config, "files", key,
352 (const gchar**)recent_files, file_prefs.mru_length);
353 g_strfreev(recent_files);
357 static gchar *get_session_file_string(GeanyDocument *doc)
359 gchar *fname;
360 gchar *locale_filename;
361 gchar *escaped_filename;
362 GeanyFiletype *ft = doc->file_type;
364 if (ft == NULL) /* can happen when saving a new file when quitting */
365 ft = filetypes[GEANY_FILETYPES_NONE];
367 locale_filename = utils_get_locale_from_utf8(doc->file_name);
368 escaped_filename = g_uri_escape_string(locale_filename, NULL, TRUE);
370 fname = g_strdup_printf("%d;%s;%d;E%s;%d;%d;%d;%s;%d;%d",
371 sci_get_current_position(doc->editor->sci),
372 ft->name,
373 doc->readonly,
374 doc->encoding,
375 doc->editor->indent_type,
376 doc->editor->auto_indent,
377 doc->editor->line_wrapping,
378 escaped_filename,
379 doc->editor->line_breaking,
380 doc->editor->indent_width);
381 g_free(escaped_filename);
382 g_free(locale_filename);
383 return fname;
387 static void remove_session_files(GKeyFile *config)
389 gchar **ptr;
390 gchar **keys = g_key_file_get_keys(config, "files", NULL, NULL);
392 foreach_strv(ptr, keys)
394 if (g_str_has_prefix(*ptr, "FILE_NAME_"))
395 g_key_file_remove_key(config, "files", *ptr, NULL);
397 g_strfreev(keys);
401 void configuration_save_session_files(GKeyFile *config)
403 gint npage;
404 gchar entry[16];
405 guint i = 0, j = 0, max;
407 npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
408 g_key_file_set_integer(config, "files", "current_page", npage);
410 // clear existing entries first as they might not all be overwritten
411 remove_session_files(config);
413 /* store the filenames in the notebook tab order to reopen them the next time */
414 max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
415 for (i = 0; i < max; i++)
417 GeanyDocument *doc = document_get_from_page(i);
419 if (doc != NULL && doc->real_path != NULL)
421 gchar *fname;
423 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
424 fname = get_session_file_string(doc);
425 g_key_file_set_string(config, "files", entry, fname);
426 g_free(fname);
427 j++;
431 #ifdef HAVE_VTE
432 if (vte_info.have_vte)
434 vte_get_working_directory(); /* refresh vte_info.dir */
435 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
437 #endif
441 static void save_dialog_prefs(GKeyFile *config)
443 /* new settings should be added in init_pref_groups() */
444 settings_action(config, SETTING_WRITE);
446 /* Some of the key names are not consistent, but this is for backwards compatibility */
448 /* general */
449 g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
450 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_session", project_prefs.project_session);
451 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
452 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
453 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_wingeom", prefs.save_wingeom);
454 g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
455 g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
456 g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
457 g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
458 g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
460 /* interface */
461 g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
462 g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
463 g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
464 g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
465 g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
466 g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
467 g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
468 g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
469 g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
470 g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
471 g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
472 g_key_file_set_boolean(config, PACKAGE, "use_native_windows_dialogs", interface_prefs.use_native_windows_dialogs);
474 /* display */
475 g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
476 g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
477 g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
478 g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
479 g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
480 g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_enabled);
481 g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_type);
482 g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_column);
483 g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
485 /* editor */
486 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
487 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
488 g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
489 g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
490 g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
491 g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
492 g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
493 g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
494 g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
495 g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
496 g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
497 g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
498 g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
499 g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
500 g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
501 g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
502 g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
504 /* files */
505 g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
506 if (file_prefs.default_open_encoding == -1)
507 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
508 else
509 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
510 g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
511 g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
512 g_key_file_set_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", file_prefs.ensure_convert_new_lines);
513 g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
514 g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
516 /* toolbar */
517 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
518 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
519 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
520 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
521 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
522 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
524 /* templates */
525 g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
526 g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
527 g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
528 g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
529 g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
530 g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
531 g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
532 g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
534 /* tools settings */
535 g_key_file_set_string(config, "tools", "terminal_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
536 g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
537 g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
538 g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
540 /* build menu */
541 build_save_menu(config, NULL, GEANY_BCS_PREF);
543 /* printing */
544 g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
545 g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
546 g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
547 g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
548 g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
549 g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
550 g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
552 /* VTE */
553 #ifdef HAVE_VTE
554 g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
555 if (vte_info.have_vte)
557 gchar *tmp_string;
559 g_key_file_set_string(config, "VTE", "font", vc->font);
560 g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
561 g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
562 g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
563 g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
564 g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
565 g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
566 g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
567 g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
568 g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
569 g_key_file_set_string(config, "VTE", "font", vc->font);
570 g_key_file_set_string(config, "VTE", "shell", vc->shell);
571 tmp_string = utils_get_hex_from_color(&vc->colour_fore);
572 g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
573 g_free(tmp_string);
574 tmp_string = utils_get_hex_from_color(&vc->colour_back);
575 g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
576 g_free(tmp_string);
578 #endif
581 typedef enum ConfigPayload
583 PREFS,
584 SESSION
586 ConfigPayload;
588 static void save_ui_prefs(GKeyFile *config)
590 g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
591 g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
592 g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
593 g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
594 g_key_file_set_string(config, PACKAGE, "color_picker_palette", ui_prefs.color_picker_palette);
596 /* get the text from the scribble textview */
598 GtkTextBuffer *buffer;
599 GtkTextIter start, end, iter;
600 GtkTextMark *mark;
602 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
603 gtk_text_buffer_get_bounds(buffer, &start, &end);
604 scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
605 g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
606 g_free(scribble_text);
608 mark = gtk_text_buffer_get_insert(buffer);
609 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
610 scribble_pos = gtk_text_iter_get_offset(&iter);
611 g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
614 g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
615 if (ui_prefs.custom_commands != NULL)
617 g_key_file_set_string_list(config, PACKAGE, "custom_commands",
618 (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
619 g_key_file_set_string_list(config, PACKAGE, "custom_commands_labels",
620 (const gchar**) ui_prefs.custom_commands_labels, g_strv_length(ui_prefs.custom_commands_labels));
624 static void save_ui_session(GKeyFile *config)
626 if (prefs.save_winpos || prefs.save_wingeom)
628 GdkWindowState wstate;
630 g_key_file_set_integer(config, PACKAGE, "treeview_position",
631 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
632 g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
633 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
635 gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
636 gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
637 wstate = gdk_window_get_state(gtk_widget_get_window(main_widgets.window));
638 ui_prefs.geometry[4] = (wstate & GDK_WINDOW_STATE_MAXIMIZED) ? 1 : 0;
639 g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
643 void write_config_file(gchar const *filename, ConfigPayload payload)
645 GKeyFile *config = g_key_file_new();
646 gchar *configfile = g_build_filename(app->configdir, filename, NULL);
647 gchar *data;
649 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
651 switch (payload)
653 case PREFS:
654 /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
655 g_signal_emit_by_name(geany_object, "save-settings", config);
656 save_dialog_prefs(config);
657 save_ui_prefs(config);
658 break;
659 case SESSION:
660 save_recent_files(config, ui_prefs.recent_queue, "recent_files");
661 save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
662 project_save_prefs(config); /* save project filename, etc. */
663 save_ui_session(config);
664 if (cl_options.load_session)
665 configuration_save_session_files(config);
666 #ifdef HAVE_VTE
667 else if (vte_info.have_vte)
669 vte_get_working_directory(); /* refresh vte_info.dir */
670 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
672 #endif
673 break;
675 /* write the file */
676 data = g_key_file_to_data(config, NULL, NULL);
677 utils_write_file(configfile, data);
678 g_free(data);
680 g_key_file_free(config);
681 g_free(configfile);
684 void configuration_save(void)
686 /* save all configuration files
687 * it is probably not very efficient to write both files every time
688 * could be more selective about which file is saved when */
689 write_config_file(PREFS_FILE, PREFS);
690 write_config_file(SESSION_FILE, SESSION);
693 static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
695 gchar **recent_files;
696 gsize i, len = 0;
698 recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
699 if (recent_files != NULL)
701 for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
703 gchar *filename = g_strdup(recent_files[i]);
704 g_queue_push_tail(queue, filename);
706 g_strfreev(recent_files);
712 * Load session list from the given keyfile, and store it in the global
713 * session_files variable for later file loading
714 * */
715 void configuration_load_session_files(GKeyFile *config, gboolean read_recent_files)
717 guint i;
718 gboolean have_session_files;
719 gchar entry[16];
720 gchar **tmp_array;
721 GError *error = NULL;
723 session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
725 if (read_recent_files)
727 load_recent_files(config, ui_prefs.recent_queue, "recent_files");
728 load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
731 /* the project may load another list than the main setting */
732 if (session_files != NULL)
734 foreach_ptr_array(tmp_array, i, session_files)
735 g_strfreev(tmp_array);
736 g_ptr_array_free(session_files, TRUE);
739 session_files = g_ptr_array_new();
740 have_session_files = TRUE;
741 i = 0;
742 while (have_session_files)
744 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
745 tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
746 if (! tmp_array || error)
748 g_error_free(error);
749 error = NULL;
750 have_session_files = FALSE;
752 g_ptr_array_add(session_files, tmp_array);
753 i++;
756 #ifdef HAVE_VTE
757 /* BUG: after loading project at startup, closing project doesn't restore old VTE path */
758 if (vte_info.have_vte)
760 gchar *tmp_string = utils_get_setting_string(config, "VTE", "last_dir", NULL);
761 vte_cwd(tmp_string,TRUE);
762 g_free(tmp_string);
764 #endif
768 #ifdef HAVE_VTE
769 static void get_setting_color(GKeyFile *config, const gchar *section, const gchar *key,
770 GdkColor *color, const gchar *default_color)
772 gchar *str = utils_get_setting_string(config, section, key, NULL);
773 if (str == NULL || ! utils_parse_color(str, color))
774 utils_parse_color(default_color, color);
775 g_free(str);
777 #endif
780 /* note: new settings should be added in init_pref_groups() */
781 static void load_dialog_prefs(GKeyFile *config)
783 gchar *tmp_string, *tmp_string2;
784 const gchar *default_charset = NULL;
785 gchar *cmd;
787 /* compatibility with Geany 0.20 */
788 if (!g_key_file_has_key(config, PACKAGE, atomic_file_saving_key, NULL))
790 g_key_file_set_boolean(config, PACKAGE, atomic_file_saving_key,
791 utils_get_setting_boolean(config, PACKAGE, "use_safe_file_saving", FALSE));
794 /* compatibility with Geany 0.21 */
796 gboolean suppress_search_dialogs = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_search_dialogs", FALSE);
798 if (!g_key_file_has_key(config, "search", "pref_search_always_wrap", NULL))
799 g_key_file_set_boolean(config, "search", "pref_search_always_wrap", suppress_search_dialogs);
801 if (!g_key_file_has_key(config, "search", "pref_search_hide_find_dialog", NULL))
802 g_key_file_set_boolean(config, "search", "pref_search_hide_find_dialog", suppress_search_dialogs);
805 /* general */
806 prefs.confirm_exit = utils_get_setting_boolean(config, PACKAGE, "pref_main_confirm_exit", FALSE);
807 prefs.suppress_status_messages = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_status_messages", FALSE);
808 prefs.load_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_load_session", TRUE);
809 project_prefs.project_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_session", TRUE);
810 project_prefs.project_file_in_basedir = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", FALSE);
811 prefs.save_winpos = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_winpos", TRUE);
812 prefs.save_wingeom = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_wingeom", prefs.save_winpos);
813 prefs.beep_on_errors = utils_get_setting_boolean(config, PACKAGE, "beep_on_errors", TRUE);
814 prefs.switch_to_status = utils_get_setting_boolean(config, PACKAGE, "switch_msgwin_pages", FALSE);
815 prefs.auto_focus = utils_get_setting_boolean(config, PACKAGE, "auto_focus", FALSE);
817 /* interface */
818 interface_prefs.tab_pos_editor = utils_get_setting_integer(config, PACKAGE, "tab_pos_editor", GTK_POS_TOP);
819 interface_prefs.tab_pos_msgwin = utils_get_setting_integer(config, PACKAGE, "tab_pos_msgwin",GTK_POS_LEFT);
820 interface_prefs.sidebar_symbol_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_symbol_visible", TRUE);
821 interface_prefs.sidebar_openfiles_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_openfiles_visible", TRUE);
822 interface_prefs.statusbar_visible = utils_get_setting_boolean(config, PACKAGE, "statusbar_visible", TRUE);
823 file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
824 file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
825 interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
826 file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
827 interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
828 interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
829 interface_prefs.msgwin_font = utils_get_setting_string(config, PACKAGE, "msgwin_font", GEANY_DEFAULT_FONT_MSG_WINDOW);
830 interface_prefs.use_native_windows_dialogs = utils_get_setting_boolean(config, PACKAGE, "use_native_windows_dialogs", FALSE);
832 /* display, editor */
833 editor_prefs.long_line_enabled = utils_get_setting_boolean(config, PACKAGE, "long_line_enabled", TRUE);
834 editor_prefs.long_line_type = utils_get_setting_integer(config, PACKAGE, "long_line_type", 0);
835 if (editor_prefs.long_line_type == 2) /* backward compatibility */
837 editor_prefs.long_line_type = 0;
838 editor_prefs.long_line_enabled = FALSE;
840 editor_prefs.long_line_color = utils_get_setting_string(config, PACKAGE, "long_line_color", "#C2EBC2");
841 editor_prefs.long_line_column = utils_get_setting_integer(config, PACKAGE, "long_line_column", 72);
842 editor_prefs.symbolcompletion_min_chars = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_min_chars", GEANY_MIN_SYMBOLLIST_CHARS);
843 editor_prefs.symbolcompletion_max_height = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_max_height", GEANY_MAX_SYMBOLLIST_HEIGHT);
844 editor_prefs.line_wrapping = utils_get_setting_boolean(config, PACKAGE, "line_wrapping", FALSE); /* default is off for better performance */
845 editor_prefs.use_indicators = utils_get_setting_boolean(config, PACKAGE, "use_indicators", TRUE);
846 editor_prefs.show_indent_guide = utils_get_setting_boolean(config, PACKAGE, "show_indent_guide", FALSE);
847 editor_prefs.show_white_space = utils_get_setting_boolean(config, PACKAGE, "show_white_space", FALSE);
848 editor_prefs.show_line_endings = utils_get_setting_boolean(config, PACKAGE, "show_line_endings", FALSE);
849 editor_prefs.scroll_stop_at_last_line = utils_get_setting_boolean(config, PACKAGE, "scroll_stop_at_last_line", TRUE);
850 editor_prefs.auto_close_xml_tags = utils_get_setting_boolean(config, PACKAGE, "auto_close_xml_tags", TRUE);
851 editor_prefs.complete_snippets = utils_get_setting_boolean(config, PACKAGE, "complete_snippets", TRUE);
852 editor_prefs.auto_complete_symbols = utils_get_setting_boolean(config, PACKAGE, "auto_complete_symbols", TRUE);
853 editor_prefs.folding = utils_get_setting_boolean(config, PACKAGE, "use_folding", TRUE);
854 editor_prefs.unfold_all_children = utils_get_setting_boolean(config, PACKAGE, "unfold_all_children", FALSE);
855 editor_prefs.show_markers_margin = utils_get_setting_boolean(config, PACKAGE, "show_markers_margin", TRUE);
856 editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
857 editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
858 editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
859 editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
860 editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
861 editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
862 editor_prefs.comment_toggle_mark = utils_get_setting_string(config, PACKAGE, "comment_toggle_mark", GEANY_TOGGLE_MARK);
863 editor_prefs.autoclose_chars = utils_get_setting_integer(config, PACKAGE, "autoclose_chars", 0);
865 /* Files
866 * use current locale encoding as default for new files (should be UTF-8 in most cases) */
867 g_get_charset(&default_charset);
868 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_new_encoding",
869 default_charset);
870 if (tmp_string)
872 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
873 if (enc != NULL)
874 file_prefs.default_new_encoding = enc->idx;
875 else
876 file_prefs.default_new_encoding = GEANY_ENCODING_UTF_8;
878 g_free(tmp_string);
880 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_open_encoding",
881 "none");
882 if (tmp_string)
884 const GeanyEncoding *enc = NULL;
885 if (strcmp(tmp_string, "none") != 0)
886 enc = encodings_get_from_charset(tmp_string);
887 if (enc != NULL)
888 file_prefs.default_open_encoding = enc->idx;
889 else
890 file_prefs.default_open_encoding = -1;
892 g_free(tmp_string);
894 file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
895 file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
896 file_prefs.ensure_convert_new_lines = utils_get_setting_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", FALSE);
897 file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
898 file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
900 /* toolbar */
901 toolbar_prefs.visible = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_show", TRUE);
902 toolbar_prefs.append_to_menu = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", FALSE);
904 toolbar_prefs.use_gtk_default_style = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", TRUE);
905 if (! toolbar_prefs.use_gtk_default_style)
906 toolbar_prefs.icon_style = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_style", GTK_TOOLBAR_ICONS);
908 toolbar_prefs.use_gtk_default_icon = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", TRUE);
909 if (! toolbar_prefs.use_gtk_default_icon)
910 toolbar_prefs.icon_size = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_size", GTK_ICON_SIZE_LARGE_TOOLBAR);
913 /* VTE */
914 #ifdef HAVE_VTE
915 vte_info.load_vte = utils_get_setting_boolean(config, "VTE", "load_vte", TRUE);
916 if (vte_info.load_vte && vte_info.load_vte_cmdline /* not disabled on the cmdline */)
918 StashGroup *group;
919 struct passwd *pw = getpwuid(getuid());
920 const gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
922 #ifdef __APPLE__
923 /* Geany is started using launchd on OS X and we don't get any environment variables
924 * so PS1 isn't defined. Start as a login shell to read the corresponding config files. */
925 if (strcmp(shell, "/bin/bash") == 0)
926 shell = "/bin/bash -l";
927 #endif
929 vc = g_new0(VteConfig, 1);
930 vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
931 if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
932 /* last dir is not set, fallback to user's home directory */
933 SETPTR(vte_info.dir, g_strdup(pw->pw_dir));
934 else if (vte_info.dir == NULL && pw == NULL)
935 /* fallback to root */
936 vte_info.dir = g_strdup("/");
938 vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
939 vc->font = utils_get_setting_string(config, "VTE", "font", GEANY_DEFAULT_FONT_EDITOR);
940 vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
941 vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
942 vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", TRUE);
943 vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
944 vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
945 vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
946 vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
947 vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
948 vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
949 get_setting_color(config, "VTE", "colour_fore", &vc->colour_fore, "#ffffff");
950 get_setting_color(config, "VTE", "colour_back", &vc->colour_back, "#000000");
952 /* various VTE prefs.
953 * this can't be done in init_pref_groups() because we need to know the value of
954 * vte_info.load_vte, and `vc` to be initialized */
955 group = stash_group_new("VTE");
956 configuration_add_various_pref_group(group, "terminal");
958 stash_group_add_string(group, &vc->send_cmd_prefix, "send_cmd_prefix", "");
959 stash_group_add_boolean(group, &vc->send_selection_unsafe, "send_selection_unsafe", FALSE);
961 #endif
962 /* templates */
963 template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
964 template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
965 tmp_string = utils_get_initials(template_prefs.developer);
966 template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
967 g_free(tmp_string);
969 template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
971 tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), g_get_host_name());
972 template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
973 g_free(tmp_string);
974 template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", GEANY_TEMPLATES_FORMAT_YEAR);
975 template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", GEANY_TEMPLATES_FORMAT_DATE);
976 template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", GEANY_TEMPLATES_FORMAT_DATETIME);
978 /* tools */
979 cmd = utils_get_setting_string(config, "tools", "terminal_cmd", "");
980 if (EMPTY(cmd))
982 SETPTR(cmd, utils_get_setting_string(config, "tools", "term_cmd", ""));
983 if (!EMPTY(cmd))
985 tmp_string = cmd;
986 #ifdef G_OS_WIN32
987 if (strstr(cmd, "cmd.exe"))
988 cmd = g_strconcat(cmd, " /Q /C %c", NULL);
989 else
990 cmd = g_strconcat(cmd, " %c", NULL);
991 #else
992 cmd = g_strconcat(cmd, " -e \"/bin/sh %c\"", NULL);
993 #endif
994 g_free(tmp_string);
996 else
997 SETPTR(cmd, g_strdup(GEANY_DEFAULT_TOOLS_TERMINAL));
999 tool_prefs.term_cmd = cmd;
1000 tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
1001 tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
1003 tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
1005 /* printing */
1006 tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
1008 if (!EMPTY(tmp_string2))
1010 #ifdef G_OS_WIN32
1011 tmp_string = g_strconcat(GEANY_DEFAULT_TOOLS_PRINTCMD, " \"%f\"", NULL);
1012 #else
1013 tmp_string = g_strconcat(GEANY_DEFAULT_TOOLS_PRINTCMD, " '%f'", NULL);
1014 #endif
1016 else
1018 tmp_string = g_strdup("");
1021 printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
1022 g_free(tmp_string);
1023 g_free(tmp_string2);
1025 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
1026 printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
1027 printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
1028 printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
1029 printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
1030 printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
1032 /* read stash prefs */
1033 settings_action(config, SETTING_READ);
1035 /* build menu
1036 * after stash prefs as it uses some of them */
1037 build_set_group_count(GEANY_GBG_FT, build_menu_prefs.number_ft_menu_items);
1038 build_set_group_count(GEANY_GBG_NON_FT, build_menu_prefs.number_non_ft_menu_items);
1039 build_set_group_count(GEANY_GBG_EXEC, build_menu_prefs.number_exec_menu_items);
1040 build_load_menu(config, GEANY_BCS_PREF, NULL);
1044 static void load_ui_prefs(GKeyFile *config)
1046 ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
1047 ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
1048 ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
1049 ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
1050 ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
1051 ui_prefs.custom_commands_labels = g_key_file_get_string_list(config, PACKAGE, "custom_commands_labels", NULL, NULL);
1052 ui_prefs.color_picker_palette = utils_get_setting_string(config, PACKAGE, "color_picker_palette", "");
1054 /* Load the saved color picker palette */
1055 if (!EMPTY(ui_prefs.color_picker_palette))
1057 GtkSettings *settings;
1058 settings = gtk_settings_get_for_screen(gtk_window_get_screen(GTK_WINDOW(main_widgets.window)));
1059 g_object_set(G_OBJECT(settings), "gtk-color-palette", ui_prefs.color_picker_palette, NULL);
1062 /* sanitize custom commands labels */
1063 if (ui_prefs.custom_commands || ui_prefs.custom_commands_labels)
1065 guint i;
1066 guint cc_len = ui_prefs.custom_commands ? g_strv_length(ui_prefs.custom_commands) : 0;
1067 guint cc_labels_len = ui_prefs.custom_commands_labels ? g_strv_length(ui_prefs.custom_commands_labels) : 0;
1069 /* not enough items, resize and fill */
1070 if (cc_labels_len < cc_len)
1072 ui_prefs.custom_commands_labels = g_realloc(ui_prefs.custom_commands_labels,
1073 (cc_len + 1) * sizeof *ui_prefs.custom_commands_labels);
1074 for (i = cc_labels_len; i < cc_len; i++)
1075 ui_prefs.custom_commands_labels[i] = g_strdup("");
1076 ui_prefs.custom_commands_labels[cc_len] = NULL;
1078 /* too many items, cut off */
1079 else if (cc_labels_len > cc_len)
1081 for (i = cc_len; i < cc_labels_len; i++)
1083 g_free(ui_prefs.custom_commands_labels[i]);
1084 ui_prefs.custom_commands_labels[i] = NULL;
1089 scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
1090 _("Type here what you want, use it as a notice/scratch board"));
1091 scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
1094 static void load_ui_session(GKeyFile *config)
1096 gint *geo;
1097 gsize geo_len;
1099 geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", &geo_len, NULL);
1100 if (! geo || geo_len < 5)
1102 ui_prefs.geometry[0] = -1;
1103 ui_prefs.geometry[1] = -1;
1104 ui_prefs.geometry[2] = -1;
1105 ui_prefs.geometry[3] = -1;
1106 ui_prefs.geometry[4] = 0;
1108 else
1110 /* don't use insane values but when main windows was maximized last time, pos might be
1111 * negative (due to differences in root window and window decorations) */
1112 /* quitting when minimized can make pos -32000, -32000 on Windows! */
1113 ui_prefs.geometry[0] = MAX(-1, geo[0]);
1114 ui_prefs.geometry[1] = MAX(-1, geo[1]);
1115 ui_prefs.geometry[2] = MAX(-1, geo[2]);
1116 ui_prefs.geometry[3] = MAX(-1, geo[3]);
1117 ui_prefs.geometry[4] = geo[4] != 0;
1119 hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
1120 vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
1121 (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
1122 (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
1124 g_free(geo);
1129 * Save current session in default configuration file
1131 void configuration_save_default_session(void)
1133 gchar *configfile = g_build_filename(app->configdir, PREFS_FILE, NULL);
1134 gchar *data;
1135 GKeyFile *config = g_key_file_new();
1137 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1139 if (cl_options.load_session)
1140 configuration_save_session_files(config);
1142 /* write the file */
1143 data = g_key_file_to_data(config, NULL, NULL);
1144 utils_write_file(configfile, data);
1145 g_free(data);
1147 g_key_file_free(config);
1148 g_free(configfile);
1152 void configuration_clear_default_session(void)
1154 gchar *configfile = g_build_filename(app->configdir, SESSION_FILE, NULL);
1155 gchar *data;
1156 GKeyFile *config = g_key_file_new();
1158 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1160 if (cl_options.load_session)
1161 remove_session_files(config);
1163 /* write the file */
1164 data = g_key_file_to_data(config, NULL, NULL);
1165 utils_write_file(configfile, data);
1166 g_free(data);
1168 g_key_file_free(config);
1169 g_free(configfile);
1174 * Only reload the session part of the default configuration
1176 void configuration_reload_default_session(void)
1178 gchar *configfile = g_build_filename(app->configdir, SESSION_FILE, NULL);
1179 GKeyFile *config = g_key_file_new();
1181 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1182 g_free(configfile);
1184 configuration_load_session_files(config, FALSE);
1186 g_key_file_free(config);
1189 gboolean read_config_file(gchar const *filename, ConfigPayload payload)
1191 gchar *configfile = g_build_filename(app->configdir, filename, NULL);
1192 GKeyFile *config = g_key_file_new();
1194 if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
1195 { /* config file does not (yet) exist, so try to load a global config file which may be */
1196 /* created by distributors */
1197 geany_debug("No user config file found, trying to use global configuration.");
1198 SETPTR(configfile, g_build_filename(app->datadir, filename, NULL));
1200 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1201 g_free(configfile);
1203 switch (payload)
1205 case PREFS:
1206 load_dialog_prefs(config);
1207 load_ui_prefs(config);
1208 /* this signal can be used e.g. to delay building UI elements until settings have been read */
1209 g_signal_emit_by_name(geany_object, "load-settings", config);
1210 break;
1211 case SESSION:
1212 project_load_prefs(config);
1213 load_ui_session(config);
1214 configuration_load_session_files(config, TRUE);
1215 break;
1218 g_key_file_free(config);
1219 return TRUE;
1223 gboolean configuration_load(void)
1225 gboolean prefs_loaded = read_config_file(PREFS_FILE, PREFS);
1226 /* backwards-compatibility: try to read session from preferences if session file doesn't exist */
1227 gchar *session_filename = SESSION_FILE;
1228 gchar *session_file = g_build_filename(app->configdir, session_filename, NULL);
1229 if (! g_file_test(session_file, G_FILE_TEST_IS_REGULAR))
1231 geany_debug("No user session file found, trying to use configuration file.");
1232 session_filename = PREFS_FILE;
1234 g_free(session_file);
1235 gboolean sess_loaded = read_config_file(session_filename, SESSION);
1236 return prefs_loaded && sess_loaded;
1240 static gboolean open_session_file(gchar **tmp, guint len)
1242 guint pos;
1243 const gchar *ft_name;
1244 gchar *locale_filename;
1245 gchar *unescaped_filename;
1246 const gchar *encoding;
1247 gint indent_type;
1248 gboolean ro, auto_indent, line_wrapping;
1249 /** TODO when we have a global pref for line breaking, use its value */
1250 gboolean line_breaking = FALSE;
1251 gboolean ret = FALSE;
1253 pos = atoi(tmp[0]);
1254 ft_name = tmp[1];
1255 ro = atoi(tmp[2]);
1256 if (isdigit(tmp[3][0]))
1258 encoding = encodings_get_charset_from_index(atoi(tmp[3]));
1260 else
1262 encoding = &(tmp[3][1]);
1264 indent_type = atoi(tmp[4]);
1265 auto_indent = atoi(tmp[5]);
1266 line_wrapping = atoi(tmp[6]);
1267 /* try to get the locale equivalent for the filename */
1268 unescaped_filename = g_uri_unescape_string(tmp[7], NULL);
1269 locale_filename = utils_get_locale_from_utf8(unescaped_filename);
1271 if (len > 8)
1272 line_breaking = atoi(tmp[8]);
1274 if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR))
1276 GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
1277 GeanyDocument *doc = document_open_file_full(
1278 NULL, locale_filename, pos, ro, ft, encoding);
1280 if (doc)
1282 gint indent_width = doc->editor->indent_width;
1284 if (len > 9)
1285 indent_width = atoi(tmp[9]);
1286 editor_set_indent(doc->editor, indent_type, indent_width);
1287 editor_set_line_wrapping(doc->editor, line_wrapping);
1288 doc->editor->line_breaking = line_breaking;
1289 doc->editor->auto_indent = auto_indent;
1290 ret = TRUE;
1293 else
1295 geany_debug("Could not find file '%s'.", unescaped_filename);
1298 g_free(locale_filename);
1299 g_free(unescaped_filename);
1300 return ret;
1304 /* Open session files
1305 * Note: notebook page switch handler and adding to recent files list is always disabled
1306 * for all files opened within this function */
1307 void configuration_open_files(void)
1309 gint i;
1310 gboolean failure = FALSE;
1312 /* necessary to set it to TRUE for project session support */
1313 main_status.opening_session_files = TRUE;
1315 i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
1316 while (TRUE)
1318 gchar **tmp = g_ptr_array_index(session_files, i);
1319 guint len;
1321 if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
1323 if (! open_session_file(tmp, len))
1324 failure = TRUE;
1326 g_strfreev(tmp);
1328 if (file_prefs.tab_order_ltr)
1330 i++;
1331 if (i >= (gint)session_files->len)
1332 break;
1334 else
1336 i--;
1337 if (i < 0)
1338 break;
1342 g_ptr_array_free(session_files, TRUE);
1343 session_files = NULL;
1345 if (failure)
1346 ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
1347 else
1349 /* explicitly trigger a notebook page switch after unsetting main_status.opening_session_files
1350 * for callbacks to run (and update window title, encoding settings, and so on) */
1351 gint n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1352 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
1353 gint target_page = session_notebook_page >= 0 ? session_notebook_page : cur_page;
1355 /* if target page is current page, switch to another page first to really trigger an event */
1356 if (target_page == cur_page && n_pages > 0)
1357 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), (cur_page + 1) % n_pages);
1359 main_status.opening_session_files = FALSE;
1360 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), target_page);
1362 main_status.opening_session_files = FALSE;
1366 /* set some settings which are already read from the config file, but need other things, like the
1367 * realisation of the main window */
1368 void configuration_apply_settings(void)
1370 if (scribble_text)
1371 { /* update the scribble widget, because now it's realized */
1372 GtkTextIter iter;
1373 GtkTextBuffer *buffer =
1374 gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
1376 gtk_text_buffer_set_text(buffer, scribble_text, -1);
1377 gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
1378 gtk_text_buffer_place_cursor(buffer, &iter);
1380 g_free(scribble_text);
1382 /* set the position of the hpaned and vpaned */
1383 if (prefs.save_winpos)
1385 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
1386 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
1389 /* set fullscreen after initial draw so that returning to normal view is the right size.
1390 * fullscreen mode is disabled by default, so act only if it is true */
1391 if (ui_prefs.fullscreen)
1393 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
1394 ui_prefs.fullscreen = TRUE;
1395 ui_set_fullscreen();
1398 msgwin_show_hide_tabs();
1402 static gboolean save_configuration_cb(gpointer data)
1404 configuration_save();
1405 if (app->project != NULL)
1407 project_write_config();
1409 document_list_update_idle_func_id = 0;
1410 return G_SOURCE_REMOVE;
1414 static void document_list_changed_cb(GObject *obj, GeanyDocument *doc, gpointer data)
1416 g_return_if_fail(doc != NULL && doc->is_valid);
1418 /* save configuration, especially session file list, but only if we are not just starting
1419 * and not about to quit */
1420 if (file_prefs.save_config_on_file_change &&
1421 main_status.main_window_realized &&
1422 !main_status.opening_session_files &&
1423 !main_status.quitting)
1425 if (document_list_update_idle_func_id == 0)
1427 document_list_update_idle_func_id = g_idle_add(save_configuration_cb, NULL);
1433 void configuration_init(void)
1435 keyfile_groups = g_ptr_array_new();
1436 pref_groups = g_ptr_array_new();
1437 init_pref_groups();
1439 g_signal_connect(geany_object, "document-open", G_CALLBACK(document_list_changed_cb), NULL);
1440 g_signal_connect(geany_object, "document-save", G_CALLBACK(document_list_changed_cb), NULL);
1441 g_signal_connect(geany_object, "document-close", G_CALLBACK(document_list_changed_cb), NULL);
1445 void configuration_finalize(void)
1447 guint i;
1448 StashGroup *group;
1450 g_signal_handlers_disconnect_by_func(geany_object, G_CALLBACK(document_list_changed_cb), NULL);
1452 foreach_ptr_array(group, i, keyfile_groups)
1453 stash_group_free(group);
1455 g_ptr_array_free(keyfile_groups, TRUE);
1456 g_ptr_array_free(pref_groups, TRUE);