Remove unnecessary "lang" parameter of various functions
[geany-mirror.git] / src / keyfile.c
blob5607f5261119a760d3e0f5bbae8b085f28c73cbc
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 *default_session_files = NULL;
110 static gint session_notebook_page;
111 static gint hpan_position;
112 static gint vpan_position;
113 static const gchar atomic_file_saving_key[] = "use_atomic_file_saving";
115 typedef enum
117 PREFS,
118 SESSION,
120 MAX_PAYLOAD
121 } ConfigPayload;
123 static GPtrArray *keyfile_groups[MAX_PAYLOAD];
124 GPtrArray *pref_groups;
126 static struct
128 gint number_ft_menu_items;
129 gint number_non_ft_menu_items;
130 gint number_exec_menu_items;
132 build_menu_prefs;
134 /* The group will be free'd on quitting.
135 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
136 void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
138 g_ptr_array_add(keyfile_groups[PREFS], group);
140 if (for_prefs_dialog)
141 g_ptr_array_add(pref_groups, group);
145 /* The group will be free'd on quitting.
146 * prefix can be NULL to use group name */
147 void configuration_add_various_pref_group(struct StashGroup *group,
148 const gchar *prefix)
150 configuration_add_pref_group(group, TRUE);
151 stash_group_set_various(group, TRUE, prefix);
155 /* The group will be free'd on quitting.
156 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
157 void configuration_add_session_group(struct StashGroup *group)
159 g_ptr_array_add(keyfile_groups[SESSION], group);
163 static void init_pref_groups(void)
165 StashGroup *group;
167 group = stash_group_new(PACKAGE);
168 configuration_add_pref_group(group, TRUE);
169 stash_group_add_entry(group, &prefs.default_open_path,
170 "default_open_path", "", "startup_path_entry");
172 stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
173 "cmdline_new_files", TRUE, "check_cmdline_new_files");
175 stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
176 "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
177 stash_group_add_toggle_button(group, &file_prefs.tab_close_switch_to_mru,
178 "tab_close_switch_to_mru", FALSE, "check_tab_close_switch_to_mru");
179 stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
180 stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
181 "sidebar_pos", GTK_POS_LEFT,
182 "radio_sidebar_left", GTK_POS_LEFT,
183 "radio_sidebar_right", GTK_POS_RIGHT,
184 NULL);
185 stash_group_add_radio_buttons(group, &interface_prefs.symbols_sort_mode,
186 "symbols_sort_mode", SYMBOLS_SORT_BY_NAME,
187 "radio_symbols_sort_by_name", SYMBOLS_SORT_BY_NAME,
188 "radio_symbols_sort_by_appearance", SYMBOLS_SORT_BY_APPEARANCE,
189 NULL);
190 stash_group_add_radio_buttons(group, &interface_prefs.msgwin_orientation,
191 "msgwin_orientation", GTK_ORIENTATION_VERTICAL,
192 "radio_msgwin_vertical", GTK_ORIENTATION_VERTICAL,
193 "radio_msgwin_horizontal", GTK_ORIENTATION_HORIZONTAL,
194 NULL);
196 /* editor display */
197 stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
198 "highlighting_invert_all", FALSE, "check_highlighting_invert");
200 stash_group_add_toggle_button(group, &search_prefs.use_current_word,
201 "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
203 /* editor */
204 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
205 "check_detect_indent", FALSE, "check_detect_indent_type");
206 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_width,
207 "detect_indent_width", FALSE, "check_detect_indent_width");
208 stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
209 "use_tab_to_indent", TRUE, "check_tab_key_indents");
210 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
211 "pref_editor_tab_width", 4, "spin_indent_width");
212 stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
213 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
214 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
215 "indent_type", GEANY_INDENT_TYPE_TABS,
216 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
217 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
218 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
219 NULL);
220 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
221 "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
222 "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
223 "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
224 "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
225 NULL);
226 stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
227 "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
228 stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
229 "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
230 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
231 "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
232 "spin_autocompletion_max_entries");
233 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_update_freq,
234 "autocompletion_update_freq", GEANY_MAX_SYMBOLS_UPDATE_FREQ, "spin_symbol_update_freq");
235 stash_group_add_string(group, &editor_prefs.color_scheme,
236 "color_scheme", NULL);
237 stash_group_add_spin_button_integer(group, &editor_prefs.scroll_lines_around_cursor,
238 "scroll_lines_around_cursor", 0, "spin_scroll_lines_around_cursor");
240 /* files */
241 stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
242 "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
243 stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
244 "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
246 /* various geany prefs */
247 group = stash_group_new(PACKAGE);
248 configuration_add_various_pref_group(group, "editor");
250 stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
251 "show_editor_scrollbars", TRUE);
252 stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
253 "brace_match_ltgt", FALSE);
254 stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
255 "use_gtk_word_boundaries", TRUE);
256 stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
257 "complete_snippets_whilst_editing", FALSE);
258 /* for backwards-compatibility */
259 stash_group_add_integer(group, &editor_prefs.indentation->hard_tab_width,
260 "indent_hard_tab_width", 8);
261 stash_group_add_integer(group, &editor_prefs.ime_interaction,
262 "editor_ime_interaction", SC_IME_WINDOWED);
264 group = stash_group_new(PACKAGE);
265 configuration_add_various_pref_group(group, "files");
267 stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
268 atomic_file_saving_key, FALSE);
269 stash_group_add_boolean(group, &file_prefs.gio_unsafe_save_backup,
270 "gio_unsafe_save_backup", FALSE);
271 stash_group_add_boolean(group, &file_prefs.use_gio_unsafe_file_saving,
272 "use_gio_unsafe_file_saving", TRUE);
273 stash_group_add_boolean(group, &file_prefs.keep_edit_history_on_reload,
274 "keep_edit_history_on_reload", TRUE);
275 stash_group_add_boolean(group, &file_prefs.show_keep_edit_history_on_reload_msg,
276 "show_keep_edit_history_on_reload_msg", TRUE);
277 stash_group_add_boolean(group, &file_prefs.reload_clean_doc_on_file_change,
278 "reload_clean_doc_on_file_change", FALSE);
279 stash_group_add_boolean(group, &file_prefs.save_config_on_file_change,
280 "save_config_on_file_change", TRUE);
281 stash_group_add_string(group, &file_prefs.extract_filetype_regex,
282 "extract_filetype_regex", GEANY_DEFAULT_FILETYPE_REGEX);
283 stash_group_add_boolean(group, &ui_prefs.allow_always_save,
284 "allow_always_save", FALSE);
286 group = stash_group_new(PACKAGE);
287 configuration_add_various_pref_group(group, "search");
289 stash_group_add_integer(group, (gint*)&search_prefs.find_selection_type,
290 "find_selection_type", GEANY_FIND_SEL_CURRENT_WORD);
291 stash_group_add_boolean(group, &search_prefs.replace_and_find_by_default,
292 "replace_and_find_by_default", TRUE);
294 group = stash_group_new(PACKAGE);
295 configuration_add_various_pref_group(group, "socket");
297 #ifdef G_OS_WIN32
298 stash_group_add_integer(group, (gint*)&prefs.socket_remote_cmd_port,
299 "socket_remote_cmd_port", SOCKET_WINDOWS_REMOTE_CMD_PORT);
300 #endif
302 #ifdef HAVE_VTE
303 /* various VTE prefs. */
304 group = stash_group_new("VTE");
305 configuration_add_various_pref_group(group, "terminal");
307 stash_group_add_string(group, &vte_config.send_cmd_prefix,
308 "send_cmd_prefix", "");
309 stash_group_add_boolean(group, &vte_config.send_selection_unsafe,
310 "send_selection_unsafe", FALSE);
311 #endif
313 /* Note: Interface-related various prefs are in ui_init_prefs() */
315 /* various build-menu prefs */
316 // Warning: don't move PACKAGE group name items here
317 group = stash_group_new("build-menu");
318 configuration_add_various_pref_group(group, "build");
320 stash_group_add_integer(group, &build_menu_prefs.number_ft_menu_items,
321 "number_ft_menu_items", 0);
322 stash_group_add_integer(group, &build_menu_prefs.number_non_ft_menu_items,
323 "number_non_ft_menu_items", 0);
324 stash_group_add_integer(group, &build_menu_prefs.number_exec_menu_items,
325 "number_exec_menu_items", 0);
329 typedef enum SettingAction
331 SETTING_READ,
332 SETTING_WRITE
334 SettingAction;
336 static void settings_action(GKeyFile *config, SettingAction action, ConfigPayload payload)
338 guint i;
339 StashGroup *group;
341 foreach_ptr_array(group, i, keyfile_groups[payload])
343 switch (action)
345 case SETTING_READ:
346 stash_group_load_from_key_file(group, config); break;
347 case SETTING_WRITE:
348 stash_group_save_to_key_file(group, config); break;
354 static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
356 gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
357 guint i;
359 for (i = 0; i < file_prefs.mru_length; i++)
361 if (! g_queue_is_empty(queue))
363 /* copy the values, this is necessary when this function is called from the
364 * preferences dialog or when quitting is canceled to keep the queue intact */
365 recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
367 else
369 recent_files[i] = NULL;
370 break;
373 /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
374 recent_files[file_prefs.mru_length] = NULL;
375 g_key_file_set_string_list(config, "files", key,
376 (const gchar**)recent_files, file_prefs.mru_length);
377 g_strfreev(recent_files);
381 static gchar *get_session_file_string(GeanyDocument *doc)
383 gchar *fname;
384 gchar *locale_filename;
385 gchar *escaped_filename;
386 GeanyFiletype *ft = doc->file_type;
388 if (ft == NULL) /* can happen when saving a new file when quitting */
389 ft = filetypes[GEANY_FILETYPES_NONE];
391 locale_filename = utils_get_locale_from_utf8(doc->file_name);
392 escaped_filename = g_uri_escape_string(locale_filename, NULL, TRUE);
394 fname = g_strdup_printf("%d;%s;%d;E%s;%d;%d;%d;%s;%d;%d",
395 sci_get_current_position(doc->editor->sci),
396 ft->name,
397 doc->readonly,
398 doc->encoding,
399 doc->editor->indent_type,
400 doc->editor->auto_indent,
401 doc->editor->line_wrapping,
402 escaped_filename,
403 doc->editor->line_breaking,
404 doc->editor->indent_width);
405 g_free(escaped_filename);
406 g_free(locale_filename);
407 return fname;
411 static void remove_session_files(GKeyFile *config)
413 gchar **ptr;
414 gchar **keys = g_key_file_get_keys(config, "files", NULL, NULL);
416 foreach_strv(ptr, keys)
418 if (g_str_has_prefix(*ptr, "FILE_NAME_"))
419 g_key_file_remove_key(config, "files", *ptr, NULL);
421 g_strfreev(keys);
425 void configuration_save_session_files(GKeyFile *config)
427 gint npage;
428 gchar entry[16];
429 guint i = 0, j = 0, max;
431 npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
432 g_key_file_set_integer(config, "files", "current_page", npage);
434 // clear existing entries first as they might not all be overwritten
435 remove_session_files(config);
437 /* store the filenames in the notebook tab order to reopen them the next time */
438 max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
439 for (i = 0; i < max; i++)
441 GeanyDocument *doc = document_get_from_page(i);
443 if (doc != NULL && doc->real_path != NULL)
445 gchar *fname;
447 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
448 fname = get_session_file_string(doc);
449 g_key_file_set_string(config, "files", entry, fname);
450 g_free(fname);
451 j++;
455 #ifdef HAVE_VTE
456 if (vte_info.have_vte)
458 vte_get_working_directory(); /* refresh vte_info.dir */
459 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
461 #endif
465 static void save_dialog_prefs(GKeyFile *config)
467 /* Some of the key names are not consistent, but this is for backwards compatibility */
469 /* general */
470 g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
471 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
472 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
473 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_wingeom", prefs.save_wingeom);
474 g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
475 g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
476 g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
477 g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
478 g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
480 /* interface */
481 g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
482 g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
483 g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
484 g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
485 g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
486 g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
487 g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
488 g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
489 g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
490 g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
491 g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
492 g_key_file_set_boolean(config, PACKAGE, "use_native_windows_dialogs", interface_prefs.use_native_windows_dialogs);
494 /* display */
495 g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
496 g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
497 g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
498 g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
499 g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
500 g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_enabled);
501 g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_type);
502 g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_column);
503 g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
505 /* editor */
506 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
507 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
508 g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
509 g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
510 g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
511 g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
512 g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
513 g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
514 g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
515 g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
516 g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
517 g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
518 g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
519 g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
520 g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
521 g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
522 g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
524 /* files */
525 g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
526 if (file_prefs.default_open_encoding == -1)
527 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
528 else
529 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
530 g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
531 g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
532 g_key_file_set_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", file_prefs.ensure_convert_new_lines);
533 g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
534 g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
536 /* toolbar */
537 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
538 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
539 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
540 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
541 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
542 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
544 /* templates */
545 g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
546 g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
547 g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
548 g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
549 g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
550 g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
551 g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
552 g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
554 /* tools settings */
555 g_key_file_set_string(config, "tools", "terminal_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
556 g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
557 g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
558 g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
560 /* build menu */
561 build_save_menu(config, NULL, GEANY_BCS_PREF);
563 /* printing */
564 g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
565 g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
566 g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
567 g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
568 g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
569 g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
570 g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
572 /* VTE */
573 #ifdef HAVE_VTE
574 g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
575 if (vte_info.have_vte)
577 VteConfig *vc = &vte_config;
578 gchar *tmp_string;
580 g_key_file_set_string(config, "VTE", "font", vc->font);
581 g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
582 g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
583 g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
584 g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
585 g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
586 g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
587 g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
588 g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
589 g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
590 g_key_file_set_string(config, "VTE", "font", vc->font);
591 g_key_file_set_string(config, "VTE", "shell", vc->shell);
592 tmp_string = utils_get_hex_from_color(&vc->colour_fore);
593 g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
594 g_free(tmp_string);
595 tmp_string = utils_get_hex_from_color(&vc->colour_back);
596 g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
597 g_free(tmp_string);
599 #endif
603 static void save_ui_prefs(GKeyFile *config)
605 g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
606 g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
607 g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
608 g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
609 g_key_file_set_string(config, PACKAGE, "color_picker_palette", ui_prefs.color_picker_palette);
611 /* get the text from the scribble textview */
613 GtkTextBuffer *buffer;
614 GtkTextIter start, end, iter;
615 GtkTextMark *mark;
617 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
618 gtk_text_buffer_get_bounds(buffer, &start, &end);
619 scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
620 g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
621 g_free(scribble_text);
623 mark = gtk_text_buffer_get_insert(buffer);
624 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
625 scribble_pos = gtk_text_iter_get_offset(&iter);
626 g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
629 g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
630 if (ui_prefs.custom_commands != NULL)
632 g_key_file_set_string_list(config, PACKAGE, "custom_commands",
633 (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
634 g_key_file_set_string_list(config, PACKAGE, "custom_commands_labels",
635 (const gchar**) ui_prefs.custom_commands_labels, g_strv_length(ui_prefs.custom_commands_labels));
639 static void save_ui_session(GKeyFile *config)
641 if (prefs.save_winpos || prefs.save_wingeom)
643 GdkWindowState wstate;
645 g_key_file_set_integer(config, PACKAGE, "treeview_position",
646 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
647 g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
648 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
650 gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
651 gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
652 wstate = gdk_window_get_state(gtk_widget_get_window(main_widgets.window));
653 ui_prefs.geometry[4] = (wstate & GDK_WINDOW_STATE_MAXIMIZED) ? 1 : 0;
654 g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
658 void write_config_file(gchar const *filename, ConfigPayload payload)
660 GKeyFile *config = g_key_file_new();
661 gchar *configfile = g_build_filename(app->configdir, filename, NULL);
662 gchar *data;
664 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
666 switch (payload)
668 case PREFS:
669 /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
670 g_signal_emit_by_name(geany_object, "save-settings", config);
671 save_dialog_prefs(config);
672 save_ui_prefs(config);
673 break;
674 case SESSION:
675 save_recent_files(config, ui_prefs.recent_queue, "recent_files");
676 save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
677 project_save_prefs(config); /* save project filename, etc. */
678 save_ui_session(config);
679 if (cl_options.load_session && app->project == NULL)
680 configuration_save_session_files(config);
681 #ifdef HAVE_VTE
682 else if (vte_info.have_vte)
684 vte_get_working_directory(); /* refresh vte_info.dir */
685 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
687 #endif
688 break;
691 /* new settings should be added in init_pref_groups() */
692 settings_action(config, SETTING_WRITE, payload);
694 /* write the file */
695 data = g_key_file_to_data(config, NULL, NULL);
696 utils_write_file(configfile, data);
697 g_free(data);
699 g_key_file_free(config);
700 g_free(configfile);
703 void configuration_save(void)
705 /* save all configuration files
706 * it is probably not very efficient to write both files every time
707 * could be more selective about which file is saved when */
708 write_config_file(PREFS_FILE, PREFS);
709 write_config_file(SESSION_FILE, SESSION);
712 static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
714 gchar **recent_files;
715 gsize i, len = 0;
717 recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
718 if (recent_files != NULL)
720 for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
722 gchar *filename = g_strdup(recent_files[i]);
723 g_queue_push_tail(queue, filename);
725 g_strfreev(recent_files);
731 * Load session list from the given keyfile and return an array containg the file names
732 * */
733 GPtrArray *configuration_load_session_files(GKeyFile *config)
735 guint i;
736 gboolean have_session_files;
737 gchar entry[16];
738 gchar **tmp_array;
739 GError *error = NULL;
740 GPtrArray *files;
742 session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
744 files = g_ptr_array_new();
745 have_session_files = TRUE;
746 i = 0;
747 while (have_session_files)
749 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
750 tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
751 if (! tmp_array || error)
753 g_error_free(error);
754 error = NULL;
755 have_session_files = FALSE;
757 g_ptr_array_add(files, tmp_array);
758 i++;
761 #ifdef HAVE_VTE
762 /* BUG: after loading project at startup, closing project doesn't restore old VTE path */
763 if (vte_info.have_vte)
765 gchar *tmp_string = utils_get_setting_string(config, "VTE", "last_dir", NULL);
766 vte_cwd(tmp_string,TRUE);
767 g_free(tmp_string);
769 #endif
771 return files;
775 #ifdef HAVE_VTE
776 static void get_setting_color(GKeyFile *config, const gchar *section, const gchar *key,
777 GdkColor *color, const gchar *default_color)
779 gchar *str = utils_get_setting_string(config, section, key, NULL);
780 if (str == NULL || ! utils_parse_color(str, color))
781 utils_parse_color(default_color, color);
782 g_free(str);
784 #endif
787 /* note: new settings should be added in init_pref_groups() */
788 static void load_dialog_prefs(GKeyFile *config)
790 gchar *tmp_string, *tmp_string2;
791 const gchar *default_charset = NULL;
792 gchar *cmd;
794 /* compatibility with Geany 0.20 */
795 if (!g_key_file_has_key(config, PACKAGE, atomic_file_saving_key, NULL))
797 g_key_file_set_boolean(config, PACKAGE, atomic_file_saving_key,
798 utils_get_setting_boolean(config, PACKAGE, "use_safe_file_saving", FALSE));
801 /* compatibility with Geany 0.21 */
803 gboolean suppress_search_dialogs = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_search_dialogs", FALSE);
805 if (!g_key_file_has_key(config, "search", "pref_search_always_wrap", NULL))
806 g_key_file_set_boolean(config, "search", "pref_search_always_wrap", suppress_search_dialogs);
808 if (!g_key_file_has_key(config, "search", "pref_search_hide_find_dialog", NULL))
809 g_key_file_set_boolean(config, "search", "pref_search_hide_find_dialog", suppress_search_dialogs);
812 /* general */
813 prefs.confirm_exit = utils_get_setting_boolean(config, PACKAGE, "pref_main_confirm_exit", FALSE);
814 prefs.suppress_status_messages = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_status_messages", FALSE);
815 prefs.load_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_load_session", TRUE);
816 project_prefs.project_file_in_basedir = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", FALSE);
817 prefs.save_winpos = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_winpos", TRUE);
818 prefs.save_wingeom = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_wingeom", prefs.save_winpos);
819 prefs.beep_on_errors = utils_get_setting_boolean(config, PACKAGE, "beep_on_errors", TRUE);
820 prefs.switch_to_status = utils_get_setting_boolean(config, PACKAGE, "switch_msgwin_pages", FALSE);
821 prefs.auto_focus = utils_get_setting_boolean(config, PACKAGE, "auto_focus", FALSE);
823 /* interface */
824 interface_prefs.tab_pos_editor = utils_get_setting_integer(config, PACKAGE, "tab_pos_editor", GTK_POS_TOP);
825 interface_prefs.tab_pos_msgwin = utils_get_setting_integer(config, PACKAGE, "tab_pos_msgwin",GTK_POS_LEFT);
826 interface_prefs.sidebar_symbol_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_symbol_visible", TRUE);
827 interface_prefs.sidebar_openfiles_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_openfiles_visible", TRUE);
828 interface_prefs.statusbar_visible = utils_get_setting_boolean(config, PACKAGE, "statusbar_visible", TRUE);
829 file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
830 file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
831 interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
832 file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
833 interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
834 interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
835 interface_prefs.msgwin_font = utils_get_setting_string(config, PACKAGE, "msgwin_font", GEANY_DEFAULT_FONT_MSG_WINDOW);
836 interface_prefs.use_native_windows_dialogs = utils_get_setting_boolean(config, PACKAGE, "use_native_windows_dialogs", FALSE);
838 /* display, editor */
839 editor_prefs.long_line_enabled = utils_get_setting_boolean(config, PACKAGE, "long_line_enabled", TRUE);
840 editor_prefs.long_line_type = utils_get_setting_integer(config, PACKAGE, "long_line_type", 0);
841 if (editor_prefs.long_line_type == 2) /* backward compatibility */
843 editor_prefs.long_line_type = 0;
844 editor_prefs.long_line_enabled = FALSE;
846 editor_prefs.long_line_color = utils_get_setting_string(config, PACKAGE, "long_line_color", "#C2EBC2");
847 editor_prefs.long_line_column = utils_get_setting_integer(config, PACKAGE, "long_line_column", 72);
848 editor_prefs.symbolcompletion_min_chars = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_min_chars", GEANY_MIN_SYMBOLLIST_CHARS);
849 editor_prefs.symbolcompletion_max_height = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_max_height", GEANY_MAX_SYMBOLLIST_HEIGHT);
850 editor_prefs.line_wrapping = utils_get_setting_boolean(config, PACKAGE, "line_wrapping", FALSE); /* default is off for better performance */
851 editor_prefs.use_indicators = utils_get_setting_boolean(config, PACKAGE, "use_indicators", TRUE);
852 editor_prefs.show_indent_guide = utils_get_setting_boolean(config, PACKAGE, "show_indent_guide", FALSE);
853 editor_prefs.show_white_space = utils_get_setting_boolean(config, PACKAGE, "show_white_space", FALSE);
854 editor_prefs.show_line_endings = utils_get_setting_boolean(config, PACKAGE, "show_line_endings", FALSE);
855 editor_prefs.scroll_stop_at_last_line = utils_get_setting_boolean(config, PACKAGE, "scroll_stop_at_last_line", TRUE);
856 editor_prefs.auto_close_xml_tags = utils_get_setting_boolean(config, PACKAGE, "auto_close_xml_tags", TRUE);
857 editor_prefs.complete_snippets = utils_get_setting_boolean(config, PACKAGE, "complete_snippets", TRUE);
858 editor_prefs.auto_complete_symbols = utils_get_setting_boolean(config, PACKAGE, "auto_complete_symbols", TRUE);
859 editor_prefs.folding = utils_get_setting_boolean(config, PACKAGE, "use_folding", TRUE);
860 editor_prefs.unfold_all_children = utils_get_setting_boolean(config, PACKAGE, "unfold_all_children", FALSE);
861 editor_prefs.show_markers_margin = utils_get_setting_boolean(config, PACKAGE, "show_markers_margin", TRUE);
862 editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
863 editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
864 editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
865 editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
866 editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
867 editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
868 editor_prefs.comment_toggle_mark = utils_get_setting_string(config, PACKAGE, "comment_toggle_mark", GEANY_TOGGLE_MARK);
869 editor_prefs.autoclose_chars = utils_get_setting_integer(config, PACKAGE, "autoclose_chars", 0);
871 /* Files
872 * use current locale encoding as default for new files (should be UTF-8 in most cases) */
873 g_get_charset(&default_charset);
874 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_new_encoding",
875 default_charset);
876 if (tmp_string)
878 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
879 if (enc != NULL)
880 file_prefs.default_new_encoding = enc->idx;
881 else
882 file_prefs.default_new_encoding = GEANY_ENCODING_UTF_8;
884 g_free(tmp_string);
886 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_open_encoding",
887 "none");
888 if (tmp_string)
890 const GeanyEncoding *enc = NULL;
891 if (strcmp(tmp_string, "none") != 0)
892 enc = encodings_get_from_charset(tmp_string);
893 if (enc != NULL)
894 file_prefs.default_open_encoding = enc->idx;
895 else
896 file_prefs.default_open_encoding = -1;
898 g_free(tmp_string);
900 file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
901 file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
902 file_prefs.ensure_convert_new_lines = utils_get_setting_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", FALSE);
903 file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
904 file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
906 /* toolbar */
907 toolbar_prefs.visible = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_show", TRUE);
908 toolbar_prefs.append_to_menu = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", FALSE);
910 toolbar_prefs.use_gtk_default_style = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", TRUE);
911 if (! toolbar_prefs.use_gtk_default_style)
912 toolbar_prefs.icon_style = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_style", GTK_TOOLBAR_ICONS);
914 toolbar_prefs.use_gtk_default_icon = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", TRUE);
915 if (! toolbar_prefs.use_gtk_default_icon)
916 toolbar_prefs.icon_size = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_size", GTK_ICON_SIZE_LARGE_TOOLBAR);
919 /* VTE */
920 #ifdef HAVE_VTE
921 vte_info.load_vte = utils_get_setting_boolean(config, "VTE", "load_vte", TRUE);
922 if (vte_info.load_vte && vte_info.load_vte_cmdline /* not disabled on the cmdline */)
924 VteConfig *vc = &vte_config;
925 struct passwd *pw = getpwuid(getuid());
926 const gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
928 #ifdef __APPLE__
929 /* Geany is started using launchd on OS X and we don't get any environment variables
930 * so PS1 isn't defined. Start as a login shell to read the corresponding config files. */
931 if (strcmp(shell, "/bin/bash") == 0)
932 shell = "/bin/bash -l";
933 #endif
935 vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
936 if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
937 /* last dir is not set, fallback to user's home directory */
938 SETPTR(vte_info.dir, g_strdup(pw->pw_dir));
939 else if (vte_info.dir == NULL && pw == NULL)
940 /* fallback to root */
941 vte_info.dir = g_strdup("/");
943 vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
944 vc->font = utils_get_setting_string(config, "VTE", "font", GEANY_DEFAULT_FONT_EDITOR);
945 vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
946 vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
947 vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", TRUE);
948 vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
949 vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
950 vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
951 vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
952 vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
953 vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
954 get_setting_color(config, "VTE", "colour_fore", &vc->colour_fore, "#ffffff");
955 get_setting_color(config, "VTE", "colour_back", &vc->colour_back, "#000000");
957 #endif
958 /* templates */
959 template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
960 template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
961 tmp_string = utils_get_initials(template_prefs.developer);
962 template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
963 g_free(tmp_string);
965 template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
967 tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), g_get_host_name());
968 template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
969 g_free(tmp_string);
970 template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", GEANY_TEMPLATES_FORMAT_YEAR);
971 template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", GEANY_TEMPLATES_FORMAT_DATE);
972 template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", GEANY_TEMPLATES_FORMAT_DATETIME);
974 /* tools */
975 cmd = utils_get_setting_string(config, "tools", "terminal_cmd", "");
976 if (EMPTY(cmd))
978 SETPTR(cmd, utils_get_setting_string(config, "tools", "term_cmd", ""));
979 if (!EMPTY(cmd))
981 tmp_string = cmd;
982 #ifdef G_OS_WIN32
983 if (strstr(cmd, "cmd.exe"))
984 cmd = g_strconcat(cmd, " /Q /C %c", NULL);
985 else
986 cmd = g_strconcat(cmd, " %c", NULL);
987 #else
988 cmd = g_strconcat(cmd, " -e \"/bin/sh %c\"", NULL);
989 #endif
990 g_free(tmp_string);
992 else
993 SETPTR(cmd, g_strdup(GEANY_DEFAULT_TOOLS_TERMINAL));
995 tool_prefs.term_cmd = cmd;
996 tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
997 tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
999 tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
1001 /* printing */
1002 tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
1004 if (!EMPTY(tmp_string2))
1006 #ifdef G_OS_WIN32
1007 tmp_string = g_strconcat(GEANY_DEFAULT_TOOLS_PRINTCMD, " \"%f\"", NULL);
1008 #else
1009 tmp_string = g_strconcat(GEANY_DEFAULT_TOOLS_PRINTCMD, " '%f'", NULL);
1010 #endif
1012 else
1014 tmp_string = g_strdup("");
1017 printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
1018 g_free(tmp_string);
1019 g_free(tmp_string2);
1021 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
1022 printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
1023 printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
1024 printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
1025 printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
1026 printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
1030 static void load_ui_prefs(GKeyFile *config)
1032 ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
1033 ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
1034 ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
1035 ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
1036 ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
1037 ui_prefs.custom_commands_labels = g_key_file_get_string_list(config, PACKAGE, "custom_commands_labels", NULL, NULL);
1038 ui_prefs.color_picker_palette = utils_get_setting_string(config, PACKAGE, "color_picker_palette", "");
1040 /* Load the saved color picker palette */
1041 if (!EMPTY(ui_prefs.color_picker_palette))
1043 GtkSettings *settings;
1044 settings = gtk_settings_get_for_screen(gtk_window_get_screen(GTK_WINDOW(main_widgets.window)));
1045 g_object_set(G_OBJECT(settings), "gtk-color-palette", ui_prefs.color_picker_palette, NULL);
1048 /* sanitize custom commands labels */
1049 if (ui_prefs.custom_commands || ui_prefs.custom_commands_labels)
1051 guint i;
1052 guint cc_len = ui_prefs.custom_commands ? g_strv_length(ui_prefs.custom_commands) : 0;
1053 guint cc_labels_len = ui_prefs.custom_commands_labels ? g_strv_length(ui_prefs.custom_commands_labels) : 0;
1055 /* not enough items, resize and fill */
1056 if (cc_labels_len < cc_len)
1058 ui_prefs.custom_commands_labels = g_realloc(ui_prefs.custom_commands_labels,
1059 (cc_len + 1) * sizeof *ui_prefs.custom_commands_labels);
1060 for (i = cc_labels_len; i < cc_len; i++)
1061 ui_prefs.custom_commands_labels[i] = g_strdup("");
1062 ui_prefs.custom_commands_labels[cc_len] = NULL;
1064 /* too many items, cut off */
1065 else if (cc_labels_len > cc_len)
1067 for (i = cc_len; i < cc_labels_len; i++)
1069 g_free(ui_prefs.custom_commands_labels[i]);
1070 ui_prefs.custom_commands_labels[i] = NULL;
1075 scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
1076 _("Type here what you want, use it as a notice/scratch board"));
1077 scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
1080 static void load_ui_session(GKeyFile *config)
1082 gint *geo;
1083 gsize geo_len;
1085 geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", &geo_len, NULL);
1086 if (! geo || geo_len < 5)
1088 ui_prefs.geometry[0] = -1;
1089 ui_prefs.geometry[1] = -1;
1090 ui_prefs.geometry[2] = -1;
1091 ui_prefs.geometry[3] = -1;
1092 ui_prefs.geometry[4] = 0;
1094 else
1096 /* don't use insane values but when main windows was maximized last time, pos might be
1097 * negative (due to differences in root window and window decorations) */
1098 /* quitting when minimized can make pos -32000, -32000 on Windows! */
1099 ui_prefs.geometry[0] = MAX(-1, geo[0]);
1100 ui_prefs.geometry[1] = MAX(-1, geo[1]);
1101 ui_prefs.geometry[2] = MAX(-1, geo[2]);
1102 ui_prefs.geometry[3] = MAX(-1, geo[3]);
1103 ui_prefs.geometry[4] = geo[4] != 0;
1105 hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
1106 vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
1107 (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
1108 (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
1110 g_free(geo);
1115 * Save current session in default configuration file
1117 void configuration_save_default_session(void)
1119 gchar *configfile = g_build_filename(app->configdir, SESSION_FILE, NULL);
1120 gchar *data;
1121 GKeyFile *config = g_key_file_new();
1123 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1125 if (cl_options.load_session)
1126 configuration_save_session_files(config);
1128 /* write the file */
1129 data = g_key_file_to_data(config, NULL, NULL);
1130 utils_write_file(configfile, data);
1131 g_free(data);
1133 g_key_file_free(config);
1134 g_free(configfile);
1138 void configuration_clear_default_session(void)
1140 gchar *configfile = g_build_filename(app->configdir, SESSION_FILE, NULL);
1141 gchar *data;
1142 GKeyFile *config = g_key_file_new();
1144 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1146 if (cl_options.load_session)
1147 remove_session_files(config);
1149 /* write the file */
1150 data = g_key_file_to_data(config, NULL, NULL);
1151 utils_write_file(configfile, data);
1152 g_free(data);
1154 g_key_file_free(config);
1155 g_free(configfile);
1160 * Only reload the session part of the default configuration
1162 void configuration_load_default_session(void)
1164 gchar *configfile = g_build_filename(app->configdir, SESSION_FILE, NULL);
1165 GKeyFile *config = g_key_file_new();
1167 g_return_if_fail(default_session_files == NULL);
1169 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1170 g_free(configfile);
1172 default_session_files = configuration_load_session_files(config);
1174 g_key_file_free(config);
1177 gboolean read_config_file(gchar const *filename, ConfigPayload payload)
1179 gchar *configfile = g_build_filename(app->configdir, filename, NULL);
1180 GKeyFile *config = g_key_file_new();
1182 if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
1183 { /* config file does not (yet) exist, so try to load a global config file which may be */
1184 /* created by distributors */
1185 geany_debug("No user config file found, trying to use global configuration.");
1186 SETPTR(configfile, g_build_filename(app->datadir, filename, NULL));
1188 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1189 g_free(configfile);
1191 /* read stash prefs */
1192 settings_action(config, SETTING_READ, payload);
1194 switch (payload)
1196 case PREFS:
1197 load_dialog_prefs(config);
1198 load_ui_prefs(config);
1200 /* build menu, after stash prefs as it uses some of them */
1201 build_set_group_count(GEANY_GBG_FT, build_menu_prefs.number_ft_menu_items);
1202 build_set_group_count(GEANY_GBG_NON_FT, build_menu_prefs.number_non_ft_menu_items);
1203 build_set_group_count(GEANY_GBG_EXEC, build_menu_prefs.number_exec_menu_items);
1204 build_load_menu(config, GEANY_BCS_PREF, NULL);
1205 /* this signal can be used e.g. to delay building UI elements until settings have been read */
1206 g_signal_emit_by_name(geany_object, "load-settings", config);
1207 break;
1208 case SESSION:
1209 project_load_prefs(config);
1210 load_ui_session(config);
1211 load_recent_files(config, ui_prefs.recent_queue, "recent_files");
1212 load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
1213 break;
1216 g_key_file_free(config);
1217 return TRUE;
1221 gboolean configuration_load(void)
1223 gboolean prefs_loaded = read_config_file(PREFS_FILE, PREFS);
1224 /* backwards-compatibility: try to read session from preferences if session file doesn't exist */
1225 gchar *session_filename = SESSION_FILE;
1226 gchar *session_file = g_build_filename(app->configdir, session_filename, NULL);
1227 if (! g_file_test(session_file, G_FILE_TEST_IS_REGULAR))
1229 geany_debug("No user session file found, trying to use configuration file.");
1230 session_filename = PREFS_FILE;
1232 g_free(session_file);
1233 gboolean sess_loaded = read_config_file(session_filename, SESSION);
1234 return prefs_loaded && sess_loaded;
1238 static gboolean open_session_file(gchar **tmp, guint len)
1240 guint pos;
1241 const gchar *ft_name;
1242 gchar *locale_filename;
1243 gchar *unescaped_filename;
1244 const gchar *encoding;
1245 gint indent_type;
1246 gboolean ro, auto_indent, line_wrapping;
1247 /** TODO when we have a global pref for line breaking, use its value */
1248 gboolean line_breaking = FALSE;
1249 gboolean ret = FALSE;
1251 pos = atoi(tmp[0]);
1252 ft_name = tmp[1];
1253 ro = atoi(tmp[2]);
1254 if (isdigit(tmp[3][0]))
1256 encoding = encodings_get_charset_from_index(atoi(tmp[3]));
1258 else
1260 encoding = &(tmp[3][1]);
1262 indent_type = atoi(tmp[4]);
1263 auto_indent = atoi(tmp[5]);
1264 line_wrapping = atoi(tmp[6]);
1265 /* try to get the locale equivalent for the filename */
1266 unescaped_filename = g_uri_unescape_string(tmp[7], NULL);
1267 locale_filename = utils_get_locale_from_utf8(unescaped_filename);
1269 if (len > 8)
1270 line_breaking = atoi(tmp[8]);
1272 if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR))
1274 GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
1275 GeanyDocument *doc = document_open_file_full(
1276 NULL, locale_filename, pos, ro, ft, encoding);
1278 if (doc)
1280 gint indent_width = doc->editor->indent_width;
1282 if (len > 9)
1283 indent_width = atoi(tmp[9]);
1284 editor_set_indent(doc->editor, indent_type, indent_width);
1285 editor_set_line_wrapping(doc->editor, line_wrapping);
1286 doc->editor->line_breaking = line_breaking;
1287 doc->editor->auto_indent = auto_indent;
1288 ret = TRUE;
1291 else
1293 geany_debug("Could not find file '%s'.", unescaped_filename);
1296 g_free(locale_filename);
1297 g_free(unescaped_filename);
1298 return ret;
1301 /* trigger a notebook page switch after unsetting main_status.opening_session_files
1302 * for callbacks to run (and update window title, encoding settings, and so on)
1304 static gboolean switch_to_session_page(gpointer data)
1306 gint n_pages = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
1307 gint cur_page = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
1308 gint target_page = session_notebook_page >= 0 ? session_notebook_page : cur_page;
1310 if (n_pages > 0)
1312 if (target_page != cur_page)
1313 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), target_page);
1314 else
1315 g_signal_emit_by_name(GTK_NOTEBOOK(main_widgets.notebook), "switch-page",
1316 gtk_notebook_get_nth_page(GTK_NOTEBOOK(main_widgets.notebook), target_page),
1317 target_page);
1319 session_notebook_page = -1;
1321 return G_SOURCE_REMOVE;
1324 /* Open session files
1325 * Note: notebook page switch handler and adding to recent files list is always disabled
1326 * for all files opened within this function */
1327 void configuration_open_files(GPtrArray *session_files)
1329 gint i;
1330 gboolean failure = FALSE;
1332 /* necessary to set it to TRUE for project session support */
1333 main_status.opening_session_files++;
1335 i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
1336 while (TRUE)
1338 gchar **tmp = g_ptr_array_index(session_files, i);
1339 guint len;
1341 if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
1343 if (! open_session_file(tmp, len))
1344 failure = TRUE;
1346 g_strfreev(tmp);
1348 if (file_prefs.tab_order_ltr)
1350 i++;
1351 if (i >= (gint)session_files->len)
1352 break;
1354 else
1356 i--;
1357 if (i < 0)
1358 break;
1362 g_ptr_array_free(session_files, TRUE);
1364 if (failure)
1365 ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
1366 else
1367 g_idle_add(switch_to_session_page, NULL);
1369 main_status.opening_session_files--;
1373 /* Open session files
1374 * Note: notebook page switch handler and adding to recent files list is always disabled
1375 * for all files opened within this function */
1376 void configuration_open_default_session(void)
1378 g_return_if_fail(default_session_files != NULL);
1380 configuration_open_files(default_session_files);
1381 default_session_files = NULL;
1385 /* set some settings which are already read from the config file, but need other things, like the
1386 * realisation of the main window */
1387 void configuration_apply_settings(void)
1389 if (scribble_text)
1390 { /* update the scribble widget, because now it's realized */
1391 GtkTextIter iter;
1392 GtkTextBuffer *buffer =
1393 gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
1395 gtk_text_buffer_set_text(buffer, scribble_text, -1);
1396 gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
1397 gtk_text_buffer_place_cursor(buffer, &iter);
1399 g_free(scribble_text);
1401 /* set the position of the hpaned and vpaned */
1402 if (prefs.save_winpos)
1404 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
1405 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
1408 /* set fullscreen after initial draw so that returning to normal view is the right size.
1409 * fullscreen mode is disabled by default, so act only if it is true */
1410 if (ui_prefs.fullscreen)
1412 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
1413 ui_prefs.fullscreen = TRUE;
1414 ui_set_fullscreen();
1417 msgwin_show_hide_tabs();
1421 static gboolean save_configuration_cb(gpointer data)
1423 if (app->project != NULL)
1424 project_write_config();
1425 else
1426 configuration_save_default_session();
1427 return G_SOURCE_REMOVE;
1431 static void document_list_changed_cb(GObject *obj, GeanyDocument *doc, gpointer data)
1433 g_return_if_fail(doc != NULL && doc->is_valid);
1435 /* save configuration, especially session file list, but only if we are not just starting
1436 * and not about to quit */
1437 if (file_prefs.save_config_on_file_change &&
1438 main_status.main_window_realized &&
1439 !main_status.opening_session_files &&
1440 !main_status.quitting)
1442 g_idle_remove_by_data(save_configuration_cb);
1443 g_idle_add(save_configuration_cb, save_configuration_cb);
1448 void configuration_init(void)
1450 keyfile_groups[PREFS] = g_ptr_array_new_with_free_func((GDestroyNotify) stash_group_free);
1451 keyfile_groups[SESSION] = g_ptr_array_new_with_free_func((GDestroyNotify) stash_group_free);
1452 pref_groups = g_ptr_array_new();
1453 init_pref_groups();
1455 g_signal_connect(geany_object, "document-open", G_CALLBACK(document_list_changed_cb), NULL);
1456 g_signal_connect(geany_object, "document-save", G_CALLBACK(document_list_changed_cb), NULL);
1457 g_signal_connect(geany_object, "document-close", G_CALLBACK(document_list_changed_cb), NULL);
1461 void configuration_finalize(void)
1463 g_signal_handlers_disconnect_by_func(geany_object, G_CALLBACK(document_list_changed_cb), NULL);
1465 g_ptr_array_free(pref_groups, TRUE);
1466 g_ptr_array_free(keyfile_groups[SESSION], TRUE);
1467 g_ptr_array_free(keyfile_groups[PREFS], TRUE);