Add filetype extraction by regex
[geany-mirror.git] / src / keyfile.c
blob4c798af90d5c91e3226bd23f11e12a880ab0033b
1 /*
2 * keyfile.c - this file is part of Geany, a fast and lightweight IDE
4 * Copyright 2005-2011 Enrico Tröger <enrico(dot)troeger(at)uvena(dot)de>
5 * Copyright 2006-2011 Nick Treleaven <nick(dot)treleaven(at)btinternet(dot)com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
23 * geany.conf preferences file loading and saving.
27 * Session file format:
28 * filename_xx=pos;filetype UID;read only;encoding idx;use_tabs;auto_indent;line_wrapping;filename
31 #include <stdlib.h>
32 #include <string.h>
34 #include "geany.h"
36 #ifdef HAVE_VTE
37 #include <pwd.h>
38 #include <sys/types.h>
39 #include <unistd.h>
40 #endif
42 #include "support.h"
43 #include "keyfile.h"
44 #include "prefs.h"
45 #include "ui_utils.h"
46 #include "utils.h"
47 #include "document.h"
48 #include "filetypes.h"
49 #include "sciwrappers.h"
50 #include "encodings.h"
51 #include "vte.h"
52 #include "main.h"
53 #include "msgwindow.h"
54 #include "search.h"
55 #include "project.h"
56 #include "editor.h"
57 #include "printing.h"
58 #include "templates.h"
59 #include "toolbar.h"
60 #include "stash.h"
63 /* some default settings which are used at the very first start of Geany to fill
64 * the configuration file */
65 #define GEANY_MAX_SYMBOLLIST_HEIGHT 10
66 #define GEANY_MIN_SYMBOLLIST_CHARS 4
67 #define GEANY_MSGWIN_HEIGHT 208
68 #define GEANY_DISK_CHECK_TIMEOUT 30
69 #define GEANY_DEFAULT_TOOLS_MAKE "make"
70 #ifdef G_OS_WIN32
71 #define GEANY_DEFAULT_TOOLS_TERMINAL "cmd.exe"
72 #else
73 #define GEANY_DEFAULT_TOOLS_TERMINAL "xterm"
74 #endif
75 #define GEANY_DEFAULT_TOOLS_BROWSER "firefox"
76 #define GEANY_DEFAULT_TOOLS_PRINTCMD "lpr"
77 #define GEANY_DEFAULT_TOOLS_GREP "grep"
78 #define GEANY_DEFAULT_MRU_LENGTH 10
79 #define GEANY_DEFAULT_FONT_SYMBOL_LIST "Sans 9"
80 #define GEANY_DEFAULT_FONT_MSG_WINDOW "Sans 9"
81 #define GEANY_DEFAULT_FONT_EDITOR "Monospace 10"
82 #define GEANY_TOGGLE_MARK "~ "
83 #define GEANY_MAX_AUTOCOMPLETE_WORDS 30
84 #define GEANY_MAX_SYMBOLS_UPDATE_FREQ 250
85 #define GEANY_DEFAULT_FILETYPE_REGEX "-\\*-\\s*([^\\s]+)\\s*-\\*-"
88 static gchar *scribble_text = NULL;
89 static gint scribble_pos = -1;
90 static GPtrArray *session_files = NULL;
91 static gint session_notebook_page;
92 static gint hpan_position;
93 static gint vpan_position;
94 static const gchar atomic_file_saving_key[] = "use_atomic_file_saving";
96 static GPtrArray *keyfile_groups = NULL;
98 GPtrArray *pref_groups = NULL;
100 static struct
102 int number_ft_menu_items;
103 int number_non_ft_menu_items;
104 int number_exec_menu_items;
106 build_menu_prefs;
109 /* The group will be free'd on quitting.
110 * @param for_prefs_dialog is whether the group also has Prefs dialog items. */
111 void configuration_add_pref_group(struct StashGroup *group, gboolean for_prefs_dialog)
113 g_ptr_array_add(keyfile_groups, group);
115 if (for_prefs_dialog)
116 g_ptr_array_add(pref_groups, group);
120 /* The group will be free'd on quitting. */
121 void configuration_add_various_pref_group(struct StashGroup *group)
123 configuration_add_pref_group(group, TRUE);
124 stash_group_set_various(group, TRUE);
128 static void init_pref_groups(void)
130 StashGroup *group;
132 group = stash_group_new(PACKAGE);
133 configuration_add_pref_group(group, TRUE);
134 stash_group_add_entry(group, &prefs.default_open_path,
135 "default_open_path", "", "startup_path_entry");
137 stash_group_add_toggle_button(group, &file_prefs.cmdline_new_files,
138 "cmdline_new_files", TRUE, "check_cmdline_new_files");
140 stash_group_add_toggle_button(group, &interface_prefs.notebook_double_click_hides_widgets,
141 "notebook_double_click_hides_widgets", FALSE, "check_double_click_hides_widgets");
142 stash_group_add_integer(group, &interface_prefs.tab_pos_sidebar, "tab_pos_sidebar", GTK_POS_TOP);
143 stash_group_add_radio_buttons(group, &interface_prefs.sidebar_pos,
144 "sidebar_pos", GTK_POS_LEFT,
145 "radio_sidebar_left", GTK_POS_LEFT,
146 "radio_sidebar_right", GTK_POS_RIGHT,
147 NULL);
149 /* editor display */
150 stash_group_add_toggle_button(group, &interface_prefs.highlighting_invert_all,
151 "highlighting_invert_all", FALSE, "check_highlighting_invert");
153 stash_group_add_toggle_button(group, &search_prefs.suppress_dialogs,
154 "pref_main_suppress_search_dialogs", FALSE, "check_ask_suppress_search_dialogs");
155 stash_group_add_toggle_button(group, &search_prefs.use_current_word,
156 "pref_main_search_use_current_word", TRUE, "check_search_use_current_word");
158 /* editor */
159 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_type,
160 "check_detect_indent", FALSE, "check_detect_indent_type");
161 stash_group_add_toggle_button(group, &editor_prefs.indentation->detect_width,
162 "detect_indent_width", FALSE, "check_detect_indent_width");
163 stash_group_add_toggle_button(group, &editor_prefs.use_tab_to_indent,
164 "use_tab_to_indent", TRUE, "check_tab_key_indents");
165 stash_group_add_spin_button_integer(group, &editor_prefs.indentation->width,
166 "pref_editor_tab_width", 4, "spin_indent_width");
167 stash_group_add_combo_box(group, (gint*)(void*)&editor_prefs.indentation->auto_indent_mode,
168 "indent_mode", GEANY_AUTOINDENT_CURRENTCHARS, "combo_auto_indent_mode");
169 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.indentation->type,
170 "indent_type", GEANY_INDENT_TYPE_TABS,
171 "radio_indent_spaces", GEANY_INDENT_TYPE_SPACES,
172 "radio_indent_tabs", GEANY_INDENT_TYPE_TABS,
173 "radio_indent_both", GEANY_INDENT_TYPE_BOTH,
174 NULL);
175 stash_group_add_radio_buttons(group, (gint*)(void*)&editor_prefs.show_virtual_space,
176 "virtualspace", GEANY_VIRTUAL_SPACE_SELECTION,
177 "radio_virtualspace_disabled", GEANY_VIRTUAL_SPACE_DISABLED,
178 "radio_virtualspace_selection", GEANY_VIRTUAL_SPACE_SELECTION,
179 "radio_virtualspace_always", GEANY_VIRTUAL_SPACE_ALWAYS,
180 NULL);
181 stash_group_add_toggle_button(group, &editor_prefs.autocomplete_doc_words,
182 "autocomplete_doc_words", FALSE, "check_autocomplete_doc_words");
183 stash_group_add_toggle_button(group, &editor_prefs.completion_drops_rest_of_word,
184 "completion_drops_rest_of_word", FALSE, "check_completion_drops_rest_of_word");
185 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_max_entries,
186 "autocompletion_max_entries", GEANY_MAX_AUTOCOMPLETE_WORDS,
187 "spin_autocompletion_max_entries");
188 stash_group_add_spin_button_integer(group, (gint*)&editor_prefs.autocompletion_update_freq,
189 "autocompletion_update_freq", GEANY_MAX_SYMBOLS_UPDATE_FREQ, "spin_symbol_update_freq");
190 stash_group_add_string(group, &editor_prefs.color_scheme,
191 "color_scheme", NULL);
193 /* files */
194 stash_group_add_spin_button_integer(group, (gint*)&file_prefs.mru_length,
195 "mru_length", GEANY_DEFAULT_MRU_LENGTH, "spin_mru");
196 stash_group_add_spin_button_integer(group, &file_prefs.disk_check_timeout,
197 "disk_check_timeout", GEANY_DISK_CHECK_TIMEOUT, "spin_disk_check");
199 /* various geany prefs */
200 group = stash_group_new(PACKAGE);
201 configuration_add_various_pref_group(group);
203 stash_group_add_boolean(group, &editor_prefs.show_scrollbars,
204 "show_editor_scrollbars", TRUE);
205 stash_group_add_boolean(group, &editor_prefs.brace_match_ltgt,
206 "brace_match_ltgt", FALSE);
207 stash_group_add_boolean(group, &editor_prefs.use_gtk_word_boundaries,
208 "use_gtk_word_boundaries", TRUE);
209 stash_group_add_boolean(group, &editor_prefs.complete_snippets_whilst_editing,
210 "complete_snippets_whilst_editing", FALSE);
211 stash_group_add_boolean(group, &file_prefs.use_safe_file_saving,
212 atomic_file_saving_key, FALSE);
213 stash_group_add_boolean(group, &file_prefs.gio_unsafe_save_backup,
214 "gio_unsafe_save_backup", FALSE);
215 stash_group_add_boolean(group, &file_prefs.use_gio_unsafe_file_saving,
216 "use_gio_unsafe_file_saving", TRUE);
217 /* for backwards-compatibility */
218 stash_group_add_integer(group, &editor_prefs.indentation->hard_tab_width,
219 "indent_hard_tab_width", 8);
220 stash_group_add_integer(group, (gint*)&search_prefs.find_selection_type,
221 "find_selection_type", GEANY_FIND_SEL_CURRENT_WORD);
222 stash_group_add_string(group, &file_prefs.extract_filetype_regex,
223 "extract_filetype_regex", GEANY_DEFAULT_FILETYPE_REGEX);
225 /* Note: Interface-related various prefs are in ui_init_prefs() */
227 /* various build-menu prefs */
228 group = stash_group_new("build-menu");
229 configuration_add_various_pref_group(group);
231 stash_group_add_integer(group, &build_menu_prefs.number_ft_menu_items,
232 "number_ft_menu_items", 0);
233 stash_group_add_integer(group, &build_menu_prefs.number_non_ft_menu_items,
234 "number_non_ft_menu_items", 0);
235 stash_group_add_integer(group, &build_menu_prefs.number_exec_menu_items,
236 "number_exec_menu_items", 0);
240 typedef enum SettingAction
242 SETTING_READ,
243 SETTING_WRITE
245 SettingAction;
247 static void settings_action(GKeyFile *config, SettingAction action)
249 guint i;
250 StashGroup *group;
252 foreach_ptr_array(group, i, keyfile_groups)
254 switch (action)
256 case SETTING_READ:
257 stash_group_load_from_key_file(group, config); break;
258 case SETTING_WRITE:
259 stash_group_save_to_key_file(group, config); break;
265 static void save_recent_files(GKeyFile *config, GQueue *queue, gchar const *key)
267 gchar **recent_files = g_new0(gchar*, file_prefs.mru_length + 1);
268 guint i;
270 for (i = 0; i < file_prefs.mru_length; i++)
272 if (! g_queue_is_empty(queue))
274 /* copy the values, this is necessary when this function is called from the
275 * preferences dialog or when quitting is canceled to keep the queue intact */
276 recent_files[i] = g_strdup(g_queue_peek_nth(queue, i));
278 else
280 recent_files[i] = NULL;
281 break;
284 /* There is a bug in GTK 2.6 g_key_file_set_string_list, we must NULL terminate. */
285 recent_files[file_prefs.mru_length] = NULL;
286 g_key_file_set_string_list(config, "files", key,
287 (const gchar**)recent_files, file_prefs.mru_length);
288 g_strfreev(recent_files);
292 static gchar *get_session_file_string(GeanyDocument *doc)
294 gchar *fname;
295 gchar *locale_filename;
296 GeanyFiletype *ft = doc->file_type;
298 if (ft == NULL) /* can happen when saving a new file when quitting */
299 ft = filetypes[GEANY_FILETYPES_NONE];
301 locale_filename = utils_get_locale_from_utf8(doc->file_name);
302 /* If the filename contains any ';' (semi-colons) we need to escape them otherwise
303 * g_key_file_get_string_list() would fail reading them, so we replace them before
304 * writing with usual colons which must never appear in a filename and replace them
305 * back when we read the file again from the file.
306 * (g_path_skip_root() to skip C:\... on Windows) */
307 g_strdelimit((gchar*) utils_path_skip_root(locale_filename), ";", ':');
309 fname = g_strdup_printf("%d;%s;%d;%d;%d;%d;%d;%s;%d;%d",
310 sci_get_current_position(doc->editor->sci),
311 ft->name,
312 doc->readonly,
313 encodings_get_idx_from_charset(doc->encoding),
314 doc->editor->indent_type,
315 doc->editor->auto_indent,
316 doc->editor->line_wrapping,
317 locale_filename,
318 doc->editor->line_breaking,
319 doc->editor->indent_width);
320 g_free(locale_filename);
321 return fname;
325 void configuration_save_session_files(GKeyFile *config)
327 gint npage;
328 gchar *tmp;
329 gchar entry[16];
330 guint i = 0, j = 0, max;
331 GeanyDocument *doc;
333 npage = gtk_notebook_get_current_page(GTK_NOTEBOOK(main_widgets.notebook));
334 g_key_file_set_integer(config, "files", "current_page", npage);
336 /* store the filenames to reopen them the next time */
337 max = gtk_notebook_get_n_pages(GTK_NOTEBOOK(main_widgets.notebook));
338 for (i = 0; i < max; i++)
340 doc = document_get_from_page(i);
341 if (doc != NULL && doc->real_path != NULL)
343 gchar *fname;
345 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", j);
346 fname = get_session_file_string(doc);
347 g_key_file_set_string(config, "files", entry, fname);
348 g_free(fname);
349 j++;
352 /* if open filenames less than saved session files, delete existing entries in the list */
353 i = j;
354 while (TRUE)
356 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
357 tmp = g_key_file_get_string(config, "files", entry, NULL);
358 if (G_UNLIKELY(tmp == NULL))
360 break;
362 else
364 g_key_file_remove_key(config, "files", entry, NULL);
365 g_free(tmp);
366 i++;
370 #ifdef HAVE_VTE
371 if (vte_info.have_vte)
373 vte_get_working_directory(); /* refresh vte_info.dir */
374 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
376 #endif
380 static void save_dialog_prefs(GKeyFile *config)
382 /* new settings should be added in init_pref_groups() */
383 settings_action(config, SETTING_WRITE);
385 /* Some of the key names are not consistent, but this is for backwards compatibility */
387 /* general */
388 g_key_file_set_boolean(config, PACKAGE, "pref_main_load_session", prefs.load_session);
389 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_session", project_prefs.project_session);
390 g_key_file_set_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", project_prefs.project_file_in_basedir);
391 g_key_file_set_boolean(config, PACKAGE, "pref_main_save_winpos", prefs.save_winpos);
392 g_key_file_set_boolean(config, PACKAGE, "pref_main_confirm_exit", prefs.confirm_exit);
393 g_key_file_set_boolean(config, PACKAGE, "pref_main_suppress_status_messages", prefs.suppress_status_messages);
394 g_key_file_set_boolean(config, PACKAGE, "switch_msgwin_pages", prefs.switch_to_status);
395 g_key_file_set_boolean(config, PACKAGE, "beep_on_errors", prefs.beep_on_errors);
396 g_key_file_set_boolean(config, PACKAGE, "auto_focus", prefs.auto_focus);
398 /* interface */
399 g_key_file_set_boolean(config, PACKAGE, "sidebar_symbol_visible", interface_prefs.sidebar_symbol_visible);
400 g_key_file_set_boolean(config, PACKAGE, "sidebar_openfiles_visible", interface_prefs.sidebar_openfiles_visible);
401 g_key_file_set_string(config, PACKAGE, "editor_font", interface_prefs.editor_font);
402 g_key_file_set_string(config, PACKAGE, "tagbar_font", interface_prefs.tagbar_font);
403 g_key_file_set_string(config, PACKAGE, "msgwin_font", interface_prefs.msgwin_font);
404 g_key_file_set_boolean(config, PACKAGE, "show_notebook_tabs", interface_prefs.show_notebook_tabs);
405 g_key_file_set_boolean(config, PACKAGE, "show_tab_cross", file_prefs.show_tab_cross);
406 g_key_file_set_boolean(config, PACKAGE, "tab_order_ltr", file_prefs.tab_order_ltr);
407 g_key_file_set_boolean(config, PACKAGE, "tab_order_beside", file_prefs.tab_order_beside);
408 g_key_file_set_integer(config, PACKAGE, "tab_pos_editor", interface_prefs.tab_pos_editor);
409 g_key_file_set_integer(config, PACKAGE, "tab_pos_msgwin", interface_prefs.tab_pos_msgwin);
410 g_key_file_set_boolean(config, PACKAGE, "use_native_windows_dialogs", interface_prefs.use_native_windows_dialogs);
412 /* display */
413 g_key_file_set_boolean(config, PACKAGE, "show_indent_guide", editor_prefs.show_indent_guide);
414 g_key_file_set_boolean(config, PACKAGE, "show_white_space", editor_prefs.show_white_space);
415 g_key_file_set_boolean(config, PACKAGE, "show_line_endings", editor_prefs.show_line_endings);
416 g_key_file_set_boolean(config, PACKAGE, "show_markers_margin", editor_prefs.show_markers_margin);
417 g_key_file_set_boolean(config, PACKAGE, "show_linenumber_margin", editor_prefs.show_linenumber_margin);
418 g_key_file_set_boolean(config, PACKAGE, "long_line_enabled", editor_prefs.long_line_enabled);
419 g_key_file_set_integer(config, PACKAGE, "long_line_type", editor_prefs.long_line_type);
420 g_key_file_set_integer(config, PACKAGE, "long_line_column", editor_prefs.long_line_column);
421 g_key_file_set_string(config, PACKAGE, "long_line_color", editor_prefs.long_line_color);
423 /* editor */
424 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_max_height", editor_prefs.symbolcompletion_max_height);
425 g_key_file_set_integer(config, PACKAGE, "symbolcompletion_min_chars", editor_prefs.symbolcompletion_min_chars);
426 g_key_file_set_boolean(config, PACKAGE, "use_folding", editor_prefs.folding);
427 g_key_file_set_boolean(config, PACKAGE, "unfold_all_children", editor_prefs.unfold_all_children);
428 g_key_file_set_boolean(config, PACKAGE, "use_indicators", editor_prefs.use_indicators);
429 g_key_file_set_boolean(config, PACKAGE, "line_wrapping", editor_prefs.line_wrapping);
430 g_key_file_set_boolean(config, PACKAGE, "auto_close_xml_tags", editor_prefs.auto_close_xml_tags);
431 g_key_file_set_boolean(config, PACKAGE, "complete_snippets", editor_prefs.complete_snippets);
432 g_key_file_set_boolean(config, PACKAGE, "auto_complete_symbols", editor_prefs.auto_complete_symbols);
433 g_key_file_set_boolean(config, PACKAGE, "pref_editor_disable_dnd", editor_prefs.disable_dnd);
434 g_key_file_set_boolean(config, PACKAGE, "pref_editor_smart_home_key", editor_prefs.smart_home_key);
435 g_key_file_set_boolean(config, PACKAGE, "pref_editor_newline_strip", editor_prefs.newline_strip);
436 g_key_file_set_integer(config, PACKAGE, "line_break_column", editor_prefs.line_break_column);
437 g_key_file_set_boolean(config, PACKAGE, "auto_continue_multiline", editor_prefs.auto_continue_multiline);
438 g_key_file_set_string(config, PACKAGE, "comment_toggle_mark", editor_prefs.comment_toggle_mark);
439 g_key_file_set_boolean(config, PACKAGE, "scroll_stop_at_last_line", editor_prefs.scroll_stop_at_last_line);
440 g_key_file_set_integer(config, PACKAGE, "autoclose_chars", editor_prefs.autoclose_chars);
442 /* files */
443 g_key_file_set_string(config, PACKAGE, "pref_editor_default_new_encoding", encodings[file_prefs.default_new_encoding].charset);
444 if (file_prefs.default_open_encoding == -1)
445 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", "none");
446 else
447 g_key_file_set_string(config, PACKAGE, "pref_editor_default_open_encoding", encodings[file_prefs.default_open_encoding].charset);
448 g_key_file_set_integer(config, PACKAGE, "default_eol_character", file_prefs.default_eol_character);
449 g_key_file_set_boolean(config, PACKAGE, "pref_editor_new_line", file_prefs.final_new_line);
450 g_key_file_set_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", file_prefs.ensure_convert_new_lines);
451 g_key_file_set_boolean(config, PACKAGE, "pref_editor_replace_tabs", file_prefs.replace_tabs);
452 g_key_file_set_boolean(config, PACKAGE, "pref_editor_trail_space", file_prefs.strip_trailing_spaces);
454 /* toolbar */
455 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_show", toolbar_prefs.visible);
456 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", toolbar_prefs.append_to_menu);
457 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", toolbar_prefs.use_gtk_default_style);
458 g_key_file_set_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", toolbar_prefs.use_gtk_default_icon);
459 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_style", toolbar_prefs.icon_style);
460 g_key_file_set_integer(config, PACKAGE, "pref_toolbar_icon_size", toolbar_prefs.icon_size);
462 /* templates */
463 g_key_file_set_string(config, PACKAGE, "pref_template_developer", template_prefs.developer);
464 g_key_file_set_string(config, PACKAGE, "pref_template_company", template_prefs.company);
465 g_key_file_set_string(config, PACKAGE, "pref_template_mail", template_prefs.mail);
466 g_key_file_set_string(config, PACKAGE, "pref_template_initial", template_prefs.initials);
467 g_key_file_set_string(config, PACKAGE, "pref_template_version", template_prefs.version);
468 g_key_file_set_string(config, PACKAGE, "pref_template_year", template_prefs.year_format);
469 g_key_file_set_string(config, PACKAGE, "pref_template_date", template_prefs.date_format);
470 g_key_file_set_string(config, PACKAGE, "pref_template_datetime", template_prefs.datetime_format);
472 /* tools settings */
473 g_key_file_set_string(config, "tools", "term_cmd", tool_prefs.term_cmd ? tool_prefs.term_cmd : "");
474 g_key_file_set_string(config, "tools", "browser_cmd", tool_prefs.browser_cmd ? tool_prefs.browser_cmd : "");
475 g_key_file_set_string(config, "tools", "grep_cmd", tool_prefs.grep_cmd ? tool_prefs.grep_cmd : "");
476 g_key_file_set_string(config, PACKAGE, "context_action_cmd", tool_prefs.context_action_cmd);
478 /* build menu */
479 build_save_menu(config, NULL, GEANY_BCS_PREF);
481 /* printing */
482 g_key_file_set_string(config, "printing", "print_cmd", printing_prefs.external_print_cmd ? printing_prefs.external_print_cmd : "");
483 g_key_file_set_boolean(config, "printing", "use_gtk_printing", printing_prefs.use_gtk_printing);
484 g_key_file_set_boolean(config, "printing", "print_line_numbers", printing_prefs.print_line_numbers);
485 g_key_file_set_boolean(config, "printing", "print_page_numbers", printing_prefs.print_page_numbers);
486 g_key_file_set_boolean(config, "printing", "print_page_header", printing_prefs.print_page_header);
487 g_key_file_set_boolean(config, "printing", "page_header_basename", printing_prefs.page_header_basename);
488 g_key_file_set_string(config, "printing", "page_header_datefmt", printing_prefs.page_header_datefmt);
490 /* VTE */
491 #ifdef HAVE_VTE
492 g_key_file_set_boolean(config, "VTE", "load_vte", vte_info.load_vte);
493 if (vte_info.have_vte)
495 gchar *tmp_string;
497 if (!g_key_file_has_key(config, "VTE", "emulation", NULL)) /* hidden */
498 g_key_file_set_string(config, "VTE", "emulation", vc->emulation);
499 if (!g_key_file_has_key(config, "VTE", "send_selection_unsafe", NULL)) /* hidden */
500 g_key_file_set_boolean(config, "VTE", "send_selection_unsafe",
501 vc->send_selection_unsafe);
502 g_key_file_set_string(config, "VTE", "font", vc->font);
503 g_key_file_set_boolean(config, "VTE", "scroll_on_key", vc->scroll_on_key);
504 g_key_file_set_boolean(config, "VTE", "scroll_on_out", vc->scroll_on_out);
505 g_key_file_set_boolean(config, "VTE", "enable_bash_keys", vc->enable_bash_keys);
506 g_key_file_set_boolean(config, "VTE", "ignore_menu_bar_accel", vc->ignore_menu_bar_accel);
507 g_key_file_set_boolean(config, "VTE", "follow_path", vc->follow_path);
508 g_key_file_set_boolean(config, "VTE", "run_in_vte", vc->run_in_vte);
509 g_key_file_set_boolean(config, "VTE", "skip_run_script", vc->skip_run_script);
510 g_key_file_set_boolean(config, "VTE", "cursor_blinks", vc->cursor_blinks);
511 g_key_file_set_integer(config, "VTE", "scrollback_lines", vc->scrollback_lines);
512 g_key_file_set_string(config, "VTE", "font", vc->font);
513 g_key_file_set_string(config, "VTE", "shell", vc->shell);
514 tmp_string = utils_get_hex_from_color(vc->colour_fore);
515 g_key_file_set_string(config, "VTE", "colour_fore", tmp_string);
516 g_free(tmp_string);
517 tmp_string = utils_get_hex_from_color(vc->colour_back);
518 g_key_file_set_string(config, "VTE", "colour_back", tmp_string);
519 g_free(tmp_string);
521 #endif
525 static void save_ui_prefs(GKeyFile *config)
527 g_key_file_set_boolean(config, PACKAGE, "sidebar_visible", ui_prefs.sidebar_visible);
528 g_key_file_set_boolean(config, PACKAGE, "statusbar_visible", interface_prefs.statusbar_visible);
529 g_key_file_set_boolean(config, PACKAGE, "msgwindow_visible", ui_prefs.msgwindow_visible);
530 g_key_file_set_boolean(config, PACKAGE, "fullscreen", ui_prefs.fullscreen);
532 /* get the text from the scribble textview */
534 GtkTextBuffer *buffer;
535 GtkTextIter start, end, iter;
536 GtkTextMark *mark;
538 buffer = gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
539 gtk_text_buffer_get_bounds(buffer, &start, &end);
540 scribble_text = gtk_text_buffer_get_text(buffer, &start, &end, FALSE);
541 g_key_file_set_string(config, PACKAGE, "scribble_text", scribble_text);
542 g_free(scribble_text);
544 mark = gtk_text_buffer_get_insert(buffer);
545 gtk_text_buffer_get_iter_at_mark(buffer, &iter, mark);
546 scribble_pos = gtk_text_iter_get_offset(&iter);
547 g_key_file_set_integer(config, PACKAGE, "scribble_pos", scribble_pos);
550 if (prefs.save_winpos)
552 g_key_file_set_integer(config, PACKAGE, "treeview_position",
553 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1"))));
554 g_key_file_set_integer(config, PACKAGE, "msgwindow_position",
555 gtk_paned_get_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1"))));
557 gtk_window_get_position(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[0], &ui_prefs.geometry[1]);
558 gtk_window_get_size(GTK_WINDOW(main_widgets.window), &ui_prefs.geometry[2], &ui_prefs.geometry[3]);
559 if (gdk_window_get_state(main_widgets.window->window) & GDK_WINDOW_STATE_MAXIMIZED)
560 ui_prefs.geometry[4] = 1;
561 else
562 ui_prefs.geometry[4] = 0;
564 g_key_file_set_integer_list(config, PACKAGE, "geometry", ui_prefs.geometry, 5);
567 g_key_file_set_string(config, PACKAGE, "custom_date_format", ui_prefs.custom_date_format);
568 if (ui_prefs.custom_commands != NULL)
570 g_key_file_set_string_list(config, PACKAGE, "custom_commands",
571 (const gchar**) ui_prefs.custom_commands, g_strv_length(ui_prefs.custom_commands));
576 void configuration_save(void)
578 GKeyFile *config = g_key_file_new();
579 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
580 gchar *data;
582 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
584 /* this signal can be used e.g. to prepare any settings before Stash code reads them below */
585 g_signal_emit_by_name(geany_object, "save-settings", config);
587 save_dialog_prefs(config);
588 save_ui_prefs(config);
589 project_save_prefs(config); /* save project filename, etc. */
590 save_recent_files(config, ui_prefs.recent_queue, "recent_files");
591 save_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
593 if (cl_options.load_session)
594 configuration_save_session_files(config);
595 #ifdef HAVE_VTE
596 else if (vte_info.have_vte)
598 vte_get_working_directory(); /* refresh vte_info.dir */
599 g_key_file_set_string(config, "VTE", "last_dir", vte_info.dir);
601 #endif
603 /* write the file */
604 data = g_key_file_to_data(config, NULL, NULL);
605 utils_write_file(configfile, data);
606 g_free(data);
608 g_key_file_free(config);
609 g_free(configfile);
613 static void load_recent_files(GKeyFile *config, GQueue *queue, const gchar *key)
615 gchar **recent_files;
616 gsize i, len = 0;
618 recent_files = g_key_file_get_string_list(config, "files", key, &len, NULL);
619 if (recent_files != NULL)
621 for (i = 0; (i < len) && (i < file_prefs.mru_length); i++)
623 gchar *filename = g_strdup(recent_files[i]);
624 g_queue_push_tail(queue, filename);
626 g_strfreev(recent_files);
632 * Load session list from the given keyfile, and store it in the global
633 * session_files variable for later file loading
634 * */
635 void configuration_load_session_files(GKeyFile *config, gboolean read_recent_files)
637 guint i;
638 gboolean have_session_files;
639 gchar entry[16];
640 gchar **tmp_array;
641 GError *error = NULL;
643 session_notebook_page = utils_get_setting_integer(config, "files", "current_page", -1);
645 if (read_recent_files)
647 load_recent_files(config, ui_prefs.recent_queue, "recent_files");
648 load_recent_files(config, ui_prefs.recent_projects_queue, "recent_projects");
651 /* the project may load another list than the main setting */
652 if (session_files != NULL)
653 g_ptr_array_free(session_files, TRUE);
655 session_files = g_ptr_array_new();
656 have_session_files = TRUE;
657 i = 0;
658 while (have_session_files)
660 g_snprintf(entry, sizeof(entry), "FILE_NAME_%d", i);
661 tmp_array = g_key_file_get_string_list(config, "files", entry, NULL, &error);
662 if (! tmp_array || error)
664 g_error_free(error);
665 error = NULL;
666 have_session_files = FALSE;
668 g_ptr_array_add(session_files, tmp_array);
669 i++;
672 #ifdef HAVE_VTE
673 /* BUG: after loading project at startup, closing project doesn't restore old VTE path */
674 if (vte_info.have_vte)
676 gchar *tmp_string = utils_get_setting_string(config, "VTE", "last_dir", NULL);
677 vte_cwd(tmp_string,TRUE);
678 g_free(tmp_string);
680 #endif
684 /* note: new settings should be added in init_pref_groups() */
685 static void load_dialog_prefs(GKeyFile *config)
687 gchar *tmp_string, *tmp_string2;
688 const gchar *default_charset = NULL;
690 /* compatibility with Geany 0.20 */
691 if (!g_key_file_has_key(config, PACKAGE, atomic_file_saving_key, NULL))
693 g_key_file_set_boolean(config, PACKAGE, atomic_file_saving_key,
694 utils_get_setting_boolean(config, PACKAGE, "use_safe_file_saving", FALSE));
697 /* read stash prefs */
698 settings_action(config, SETTING_READ);
700 /* general */
701 prefs.confirm_exit = utils_get_setting_boolean(config, PACKAGE, "pref_main_confirm_exit", FALSE);
702 prefs.suppress_status_messages = utils_get_setting_boolean(config, PACKAGE, "pref_main_suppress_status_messages", FALSE);
703 prefs.load_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_load_session", TRUE);
704 project_prefs.project_session = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_session", TRUE);
705 project_prefs.project_file_in_basedir = utils_get_setting_boolean(config, PACKAGE, "pref_main_project_file_in_basedir", FALSE);
706 prefs.save_winpos = utils_get_setting_boolean(config, PACKAGE, "pref_main_save_winpos", TRUE);
707 prefs.beep_on_errors = utils_get_setting_boolean(config, PACKAGE, "beep_on_errors", TRUE);
708 prefs.switch_to_status = utils_get_setting_boolean(config, PACKAGE, "switch_msgwin_pages", FALSE);
709 prefs.auto_focus = utils_get_setting_boolean(config, PACKAGE, "auto_focus", FALSE);
711 /* interface */
712 interface_prefs.tab_pos_editor = utils_get_setting_integer(config, PACKAGE, "tab_pos_editor", GTK_POS_TOP);
713 interface_prefs.tab_pos_msgwin = utils_get_setting_integer(config, PACKAGE, "tab_pos_msgwin",GTK_POS_LEFT);
714 interface_prefs.sidebar_symbol_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_symbol_visible", TRUE);
715 interface_prefs.sidebar_openfiles_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_openfiles_visible", TRUE);
716 interface_prefs.statusbar_visible = utils_get_setting_boolean(config, PACKAGE, "statusbar_visible", TRUE);
717 file_prefs.tab_order_ltr = utils_get_setting_boolean(config, PACKAGE, "tab_order_ltr", TRUE);
718 file_prefs.tab_order_beside = utils_get_setting_boolean(config, PACKAGE, "tab_order_beside", FALSE);
719 interface_prefs.show_notebook_tabs = utils_get_setting_boolean(config, PACKAGE, "show_notebook_tabs", TRUE);
720 file_prefs.show_tab_cross = utils_get_setting_boolean(config, PACKAGE, "show_tab_cross", TRUE);
721 interface_prefs.editor_font = utils_get_setting_string(config, PACKAGE, "editor_font", GEANY_DEFAULT_FONT_EDITOR);
722 interface_prefs.tagbar_font = utils_get_setting_string(config, PACKAGE, "tagbar_font", GEANY_DEFAULT_FONT_SYMBOL_LIST);
723 interface_prefs.msgwin_font = utils_get_setting_string(config, PACKAGE, "msgwin_font", GEANY_DEFAULT_FONT_MSG_WINDOW);
724 interface_prefs.use_native_windows_dialogs = utils_get_setting_boolean(config, PACKAGE, "use_native_windows_dialogs", FALSE);
726 /* display, editor */
727 editor_prefs.long_line_enabled = utils_get_setting_boolean(config, PACKAGE, "long_line_enabled", TRUE);
728 editor_prefs.long_line_type = utils_get_setting_integer(config, PACKAGE, "long_line_type", 0);
729 if (editor_prefs.long_line_type == 2) /* backward compatibility */
731 editor_prefs.long_line_type = 0;
732 editor_prefs.long_line_enabled = FALSE;
734 editor_prefs.long_line_color = utils_get_setting_string(config, PACKAGE, "long_line_color", "#C2EBC2");
735 editor_prefs.long_line_column = utils_get_setting_integer(config, PACKAGE, "long_line_column", 72);
736 editor_prefs.symbolcompletion_min_chars = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_min_chars", GEANY_MIN_SYMBOLLIST_CHARS);
737 editor_prefs.symbolcompletion_max_height = utils_get_setting_integer(config, PACKAGE, "symbolcompletion_max_height", GEANY_MAX_SYMBOLLIST_HEIGHT);
738 editor_prefs.line_wrapping = utils_get_setting_boolean(config, PACKAGE, "line_wrapping", FALSE); /* default is off for better performance */
739 editor_prefs.use_indicators = utils_get_setting_boolean(config, PACKAGE, "use_indicators", TRUE);
740 editor_prefs.show_indent_guide = utils_get_setting_boolean(config, PACKAGE, "show_indent_guide", FALSE);
741 editor_prefs.show_white_space = utils_get_setting_boolean(config, PACKAGE, "show_white_space", FALSE);
742 editor_prefs.show_line_endings = utils_get_setting_boolean(config, PACKAGE, "show_line_endings", FALSE);
743 editor_prefs.scroll_stop_at_last_line = utils_get_setting_boolean(config, PACKAGE, "scroll_stop_at_last_line", TRUE);
744 editor_prefs.auto_close_xml_tags = utils_get_setting_boolean(config, PACKAGE, "auto_close_xml_tags", TRUE);
745 editor_prefs.complete_snippets = utils_get_setting_boolean(config, PACKAGE, "complete_snippets", TRUE);
746 editor_prefs.auto_complete_symbols = utils_get_setting_boolean(config, PACKAGE, "auto_complete_symbols", TRUE);
747 editor_prefs.folding = utils_get_setting_boolean(config, PACKAGE, "use_folding", TRUE);
748 editor_prefs.unfold_all_children = utils_get_setting_boolean(config, PACKAGE, "unfold_all_children", FALSE);
749 editor_prefs.show_markers_margin = utils_get_setting_boolean(config, PACKAGE, "show_markers_margin", TRUE);
750 editor_prefs.show_linenumber_margin = utils_get_setting_boolean(config, PACKAGE, "show_linenumber_margin", TRUE);
751 editor_prefs.disable_dnd = utils_get_setting_boolean(config, PACKAGE, "pref_editor_disable_dnd", FALSE);
752 editor_prefs.smart_home_key = utils_get_setting_boolean(config, PACKAGE, "pref_editor_smart_home_key", TRUE);
753 editor_prefs.newline_strip = utils_get_setting_boolean(config, PACKAGE, "pref_editor_newline_strip", FALSE);
754 editor_prefs.line_break_column = utils_get_setting_integer(config, PACKAGE, "line_break_column", 72);
755 editor_prefs.auto_continue_multiline = utils_get_setting_boolean(config, PACKAGE, "auto_continue_multiline", TRUE);
756 editor_prefs.comment_toggle_mark = utils_get_setting_string(config, PACKAGE, "comment_toggle_mark", GEANY_TOGGLE_MARK);
757 editor_prefs.autoclose_chars = utils_get_setting_integer(config, PACKAGE, "autoclose_chars", 0);
759 /* Files
760 * use current locale encoding as default for new files (should be UTF-8 in most cases) */
761 g_get_charset(&default_charset);
762 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_new_encoding",
763 default_charset);
764 if (tmp_string)
766 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
767 if (enc != NULL)
768 file_prefs.default_new_encoding = enc->idx;
769 else
770 file_prefs.default_new_encoding = GEANY_ENCODING_UTF_8;
772 g_free(tmp_string);
774 tmp_string = utils_get_setting_string(config, PACKAGE, "pref_editor_default_open_encoding",
775 "none");
776 if (tmp_string)
778 const GeanyEncoding *enc = encodings_get_from_charset(tmp_string);
779 if (enc != NULL)
780 file_prefs.default_open_encoding = enc->idx;
781 else
782 file_prefs.default_open_encoding = -1;
784 g_free(tmp_string);
786 file_prefs.default_eol_character = utils_get_setting_integer(config, PACKAGE, "default_eol_character", GEANY_DEFAULT_EOL_CHARACTER);
787 file_prefs.replace_tabs = utils_get_setting_boolean(config, PACKAGE, "pref_editor_replace_tabs", FALSE);
788 file_prefs.ensure_convert_new_lines = utils_get_setting_boolean(config, PACKAGE, "pref_editor_ensure_convert_line_endings", FALSE);
789 file_prefs.final_new_line = utils_get_setting_boolean(config, PACKAGE, "pref_editor_new_line", TRUE);
790 file_prefs.strip_trailing_spaces = utils_get_setting_boolean(config, PACKAGE, "pref_editor_trail_space", FALSE);
792 /* toolbar */
793 toolbar_prefs.visible = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_show", TRUE);
794 toolbar_prefs.append_to_menu = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_append_to_menu", FALSE);
796 toolbar_prefs.use_gtk_default_style = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_style", TRUE);
797 if (! toolbar_prefs.use_gtk_default_style)
798 toolbar_prefs.icon_style = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_style", GTK_TOOLBAR_ICONS);
800 toolbar_prefs.use_gtk_default_icon = utils_get_setting_boolean(config, PACKAGE, "pref_toolbar_use_gtk_default_icon", TRUE);
801 if (! toolbar_prefs.use_gtk_default_icon)
802 toolbar_prefs.icon_size = utils_get_setting_integer(config, PACKAGE, "pref_toolbar_icon_size", GTK_ICON_SIZE_LARGE_TOOLBAR);
805 /* VTE */
806 #ifdef HAVE_VTE
807 vte_info.load_vte = utils_get_setting_boolean(config, "VTE", "load_vte", TRUE);
808 if (vte_info.load_vte)
810 struct passwd *pw = getpwuid(getuid());
811 const gchar *shell = (pw != NULL) ? pw->pw_shell : "/bin/sh";
813 vc = g_new0(VteConfig, 1);
814 vte_info.dir = utils_get_setting_string(config, "VTE", "last_dir", NULL);
815 if ((vte_info.dir == NULL || utils_str_equal(vte_info.dir, "")) && pw != NULL)
816 /* last dir is not set, fallback to user's home directory */
817 vte_info.dir = g_strdup(pw->pw_dir);
818 else if (vte_info.dir == NULL && pw == NULL)
819 /* fallback to root */
820 vte_info.dir = g_strdup("/");
822 vc->emulation = utils_get_setting_string(config, "VTE", "emulation", "xterm");
823 vc->send_selection_unsafe = utils_get_setting_boolean(config, "VTE",
824 "send_selection_unsafe", FALSE);
825 vc->shell = utils_get_setting_string(config, "VTE", "shell", shell);
826 vc->font = utils_get_setting_string(config, "VTE", "font", "Monospace 10");
827 vc->scroll_on_key = utils_get_setting_boolean(config, "VTE", "scroll_on_key", TRUE);
828 vc->scroll_on_out = utils_get_setting_boolean(config, "VTE", "scroll_on_out", TRUE);
829 vc->enable_bash_keys = utils_get_setting_boolean(config, "VTE", "enable_bash_keys", TRUE);
830 vc->ignore_menu_bar_accel = utils_get_setting_boolean(config, "VTE", "ignore_menu_bar_accel", FALSE);
831 vc->follow_path = utils_get_setting_boolean(config, "VTE", "follow_path", FALSE);
832 vc->run_in_vte = utils_get_setting_boolean(config, "VTE", "run_in_vte", FALSE);
833 vc->skip_run_script = utils_get_setting_boolean(config, "VTE", "skip_run_script", FALSE);
834 vc->cursor_blinks = utils_get_setting_boolean(config, "VTE", "cursor_blinks", FALSE);
835 vc->scrollback_lines = utils_get_setting_integer(config, "VTE", "scrollback_lines", 500);
836 vc->colour_fore = g_new0(GdkColor, 1);
837 vc->colour_back = g_new0(GdkColor, 1);
838 tmp_string = utils_get_setting_string(config, "VTE", "colour_fore", "#ffffff");
839 gdk_color_parse(tmp_string, vc->colour_fore);
840 g_free(tmp_string);
841 tmp_string = utils_get_setting_string(config, "VTE", "colour_back", "#000000");
842 gdk_color_parse(tmp_string, vc->colour_back);
843 g_free(tmp_string);
845 #endif
846 /* templates */
847 template_prefs.developer = utils_get_setting_string(config, PACKAGE, "pref_template_developer", g_get_real_name());
848 template_prefs.company = utils_get_setting_string(config, PACKAGE, "pref_template_company", "");
849 tmp_string = utils_get_initials(template_prefs.developer);
850 template_prefs.initials = utils_get_setting_string(config, PACKAGE, "pref_template_initial", tmp_string);
851 g_free(tmp_string);
853 template_prefs.version = utils_get_setting_string(config, PACKAGE, "pref_template_version", "1.0");
855 tmp_string2 = utils_get_hostname();
856 tmp_string = g_strdup_printf("%s@%s", g_get_user_name(), tmp_string2);
857 template_prefs.mail = utils_get_setting_string(config, PACKAGE, "pref_template_mail", tmp_string);
858 g_free(tmp_string);
859 g_free(tmp_string2);
860 template_prefs.year_format = utils_get_setting_string(config, PACKAGE, "pref_template_year", "%Y");
861 template_prefs.date_format = utils_get_setting_string(config, PACKAGE, "pref_template_date", "%Y-%m-%d");
862 template_prefs.datetime_format = utils_get_setting_string(config, PACKAGE, "pref_template_datetime", "%d.%m.%Y %H:%M:%S %Z");
864 /* tools */
865 tool_prefs.term_cmd = utils_get_setting_string(config, "tools", "term_cmd", GEANY_DEFAULT_TOOLS_TERMINAL);
866 tool_prefs.browser_cmd = utils_get_setting_string(config, "tools", "browser_cmd", GEANY_DEFAULT_TOOLS_BROWSER);
867 tool_prefs.grep_cmd = utils_get_setting_string(config, "tools", "grep_cmd", GEANY_DEFAULT_TOOLS_GREP);
869 tool_prefs.context_action_cmd = utils_get_setting_string(config, PACKAGE, "context_action_cmd", "");
871 /* build menu */
872 build_set_group_count(GEANY_GBG_FT, build_menu_prefs.number_ft_menu_items);
873 build_set_group_count(GEANY_GBG_NON_FT, build_menu_prefs.number_non_ft_menu_items);
874 build_set_group_count(GEANY_GBG_EXEC, build_menu_prefs.number_exec_menu_items);
875 build_load_menu(config, GEANY_BCS_PREF, NULL);
877 /* printing */
878 tmp_string2 = g_find_program_in_path(GEANY_DEFAULT_TOOLS_PRINTCMD);
879 #ifdef G_OS_WIN32
880 /* single quote paths on Win32 for g_spawn_command_line_async */
881 tmp_string = g_strconcat("'", tmp_string2, "' '%f'", NULL);
882 #else
883 tmp_string = g_strconcat(tmp_string2, " %f", NULL);
884 #endif
885 printing_prefs.external_print_cmd = utils_get_setting_string(config, "printing", "print_cmd", tmp_string);
886 g_free(tmp_string);
887 g_free(tmp_string2);
889 printing_prefs.use_gtk_printing = utils_get_setting_boolean(config, "printing", "use_gtk_printing", TRUE);
890 printing_prefs.print_line_numbers = utils_get_setting_boolean(config, "printing", "print_line_numbers", TRUE);
891 printing_prefs.print_page_numbers = utils_get_setting_boolean(config, "printing", "print_page_numbers", TRUE);
892 printing_prefs.print_page_header = utils_get_setting_boolean(config, "printing", "print_page_header", TRUE);
893 printing_prefs.page_header_basename = utils_get_setting_boolean(config, "printing", "page_header_basename", FALSE);
894 printing_prefs.page_header_datefmt = utils_get_setting_string(config, "printing", "page_header_datefmt", "%c");
898 static void load_ui_prefs(GKeyFile *config)
900 gint *geo;
901 GError *error = NULL;
903 ui_prefs.sidebar_visible = utils_get_setting_boolean(config, PACKAGE, "sidebar_visible", TRUE);
904 ui_prefs.msgwindow_visible = utils_get_setting_boolean(config, PACKAGE, "msgwindow_visible", TRUE);
905 ui_prefs.fullscreen = utils_get_setting_boolean(config, PACKAGE, "fullscreen", FALSE);
906 ui_prefs.custom_date_format = utils_get_setting_string(config, PACKAGE, "custom_date_format", "");
907 ui_prefs.custom_commands = g_key_file_get_string_list(config, PACKAGE, "custom_commands", NULL, NULL);
909 scribble_text = utils_get_setting_string(config, PACKAGE, "scribble_text",
910 _("Type here what you want, use it as a notice/scratch board"));
911 scribble_pos = utils_get_setting_integer(config, PACKAGE, "scribble_pos", -1);
913 geo = g_key_file_get_integer_list(config, PACKAGE, "geometry", NULL, &error);
914 if (error)
916 ui_prefs.geometry[0] = -1;
917 g_error_free(error);
918 error = NULL;
920 else
922 gint i;
924 ui_prefs.geometry[0] = geo[0];
925 ui_prefs.geometry[1] = geo[1];
926 ui_prefs.geometry[2] = geo[2];
927 ui_prefs.geometry[3] = geo[3];
928 ui_prefs.geometry[4] = geo[4];
930 /* don't use insane values but when main windows was maximized last time, pos might be
931 * negative (due to differences in root window and window decorations) */
932 if (ui_prefs.geometry[4] != 1)
934 for (i = 2; i < 4; i++)
936 if (ui_prefs.geometry[i] < -1)
937 ui_prefs.geometry[i] = -1;
941 hpan_position = utils_get_setting_integer(config, PACKAGE, "treeview_position", 156);
942 vpan_position = utils_get_setting_integer(config, PACKAGE, "msgwindow_position", (geo) ?
943 (GEANY_MSGWIN_HEIGHT + geo[3] - 440) :
944 (GEANY_MSGWIN_HEIGHT + GEANY_WINDOW_DEFAULT_HEIGHT - 440));
946 g_free(geo);
951 * Save current session in default configuration file
953 void configuration_save_default_session(void)
955 gchar *configfile = g_strconcat(app->configdir, G_DIR_SEPARATOR_S, "geany.conf", NULL);
956 gchar *data;
957 GKeyFile *config = g_key_file_new();
959 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
961 if (cl_options.load_session)
962 configuration_save_session_files(config);
964 /* write the file */
965 data = g_key_file_to_data(config, NULL, NULL);
966 utils_write_file(configfile, data);
967 g_free(data);
969 g_key_file_free(config);
970 g_free(configfile);
975 * Only reload the session part of the default configuration
977 void configuration_reload_default_session(void)
979 const gchar *configfile = utils_build_path(app->configdir, "geany.conf", NULL);
980 GKeyFile *config = g_key_file_new();
982 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
984 configuration_load_session_files(config, FALSE);
986 g_key_file_free(config);
990 gboolean configuration_load(void)
992 gchar *configfile = utils_build_path(app->configdir, "geany.conf", NULL);
993 GKeyFile *config = g_key_file_new();
995 if (! g_file_test(configfile, G_FILE_TEST_IS_REGULAR))
996 { /* config file does not (yet) exist, so try to load a global config file which may be */
997 /* created by distributors */
998 geany_debug("No user config file found, trying to use global configuration.");
999 setptr(configfile, utils_build_path(app->datadir, "geany.conf", NULL));
1001 g_key_file_load_from_file(config, configfile, G_KEY_FILE_NONE, NULL);
1002 g_free(configfile);
1004 load_dialog_prefs(config);
1005 load_ui_prefs(config);
1006 project_load_prefs(config);
1007 configuration_load_session_files(config, TRUE);
1009 /* this signal can be used e.g. to delay building UI elements until settings have been read */
1010 g_signal_emit_by_name(geany_object, "load-settings", config);
1012 g_key_file_free(config);
1013 return TRUE;
1017 static gboolean open_session_file(gchar **tmp, guint len)
1019 guint pos;
1020 const gchar *ft_name;
1021 gchar *locale_filename;
1022 gint enc_idx, indent_type;
1023 gboolean ro, auto_indent, line_wrapping;
1024 /** TODO when we have a global pref for line breaking, use its value */
1025 gboolean line_breaking = FALSE;
1026 gboolean ret = FALSE;
1028 pos = atoi(tmp[0]);
1029 ft_name = tmp[1];
1030 ro = atoi(tmp[2]);
1031 enc_idx = atoi(tmp[3]);
1032 indent_type = atoi(tmp[4]);
1033 auto_indent = atoi(tmp[5]);
1034 line_wrapping = atoi(tmp[6]);
1035 /* try to get the locale equivalent for the filename */
1036 locale_filename = utils_get_locale_from_utf8(tmp[7]);
1037 /* replace ':' back with ';' (see get_session_file_string for details) */
1038 g_strdelimit((gchar*) utils_path_skip_root(locale_filename), ":", ';');
1040 if (len > 8)
1041 line_breaking = atoi(tmp[8]);
1043 if (g_file_test(locale_filename, G_FILE_TEST_IS_REGULAR))
1045 GeanyFiletype *ft = filetypes_lookup_by_name(ft_name);
1046 GeanyDocument *doc = document_open_file_full(
1047 NULL, locale_filename, pos, ro, ft,
1048 (enc_idx >= 0 && enc_idx < GEANY_ENCODINGS_MAX) ?
1049 encodings[enc_idx].charset : NULL);
1051 if (doc)
1053 gint indent_width = doc->editor->indent_width;
1055 if (len > 9)
1056 indent_width = atoi(tmp[9]);
1057 editor_set_indent(doc->editor, indent_type, indent_width);
1058 editor_set_line_wrapping(doc->editor, line_wrapping);
1059 doc->editor->line_breaking = line_breaking;
1060 doc->editor->auto_indent = auto_indent;
1061 ret = TRUE;
1064 else
1066 geany_debug("Could not find file '%s'.", tmp[7]);
1069 g_free(locale_filename);
1070 return ret;
1074 /* Open session files
1075 * Note: notebook page switch handler and adding to recent files list is always disabled
1076 * for all files opened within this function */
1077 void configuration_open_files(void)
1079 gint i;
1080 gboolean failure = FALSE;
1082 /* necessary to set it to TRUE for project session support */
1083 main_status.opening_session_files = TRUE;
1085 i = file_prefs.tab_order_ltr ? 0 : (session_files->len - 1);
1086 while (TRUE)
1088 gchar **tmp = g_ptr_array_index(session_files, i);
1089 guint len;
1091 if (tmp != NULL && (len = g_strv_length(tmp)) >= 8)
1093 if (! open_session_file(tmp, len))
1094 failure = TRUE;
1096 g_strfreev(tmp);
1098 if (file_prefs.tab_order_ltr)
1100 i++;
1101 if (i >= (gint)session_files->len)
1102 break;
1104 else
1106 i--;
1107 if (i < 0)
1108 break;
1112 g_ptr_array_free(session_files, TRUE);
1113 session_files = NULL;
1115 if (failure)
1116 ui_set_statusbar(TRUE, _("Failed to load one or more session files."));
1117 else if (session_notebook_page >= 0)
1119 /* explicitly allow notebook switch page callback to be called for window title,
1120 * encoding settings and so other things */
1121 main_status.opening_session_files = FALSE;
1122 /** TODO if session_notebook_page is equal to the current notebook tab(the last opened)
1123 ** the notebook switch page callback isn't triggered and e.g. menu items are not updated */
1124 gtk_notebook_set_current_page(GTK_NOTEBOOK(main_widgets.notebook), session_notebook_page);
1126 main_status.opening_session_files = FALSE;
1130 /* set some settings which are already read from the config file, but need other things, like the
1131 * realisation of the main window */
1132 void configuration_apply_settings(void)
1134 if (scribble_text)
1135 { /* update the scribble widget, because now it's realized */
1136 GtkTextIter iter;
1137 GtkTextBuffer *buffer =
1138 gtk_text_view_get_buffer(GTK_TEXT_VIEW(msgwindow.scribble));
1140 gtk_text_buffer_set_text(buffer, scribble_text, -1);
1141 gtk_text_buffer_get_iter_at_offset(buffer, &iter, scribble_pos);
1142 gtk_text_buffer_place_cursor(buffer, &iter);
1144 g_free(scribble_text);
1146 /* set the position of the hpaned and vpaned */
1147 if (prefs.save_winpos)
1149 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "hpaned1")), hpan_position);
1150 gtk_paned_set_position(GTK_PANED(ui_lookup_widget(main_widgets.window, "vpaned1")), vpan_position);
1153 /* set fullscreen after initial draw so that returning to normal view is the right size.
1154 * fullscreen mode is disabled by default, so act only if it is true */
1155 if (ui_prefs.fullscreen)
1157 gtk_check_menu_item_set_active(GTK_CHECK_MENU_ITEM(ui_lookup_widget(main_widgets.window, "menu_fullscreen1")), TRUE);
1158 ui_prefs.fullscreen = TRUE;
1159 ui_set_fullscreen();
1162 msgwin_show_hide_tabs();
1166 void configuration_init(void)
1168 keyfile_groups = g_ptr_array_new();
1169 pref_groups = g_ptr_array_new();
1170 init_pref_groups();
1174 void configuration_finalize(void)
1176 guint i;
1177 StashGroup *group;
1179 foreach_ptr_array(group, i, keyfile_groups)
1180 stash_group_free(group);
1182 g_ptr_array_free(keyfile_groups, TRUE);
1183 g_ptr_array_free(pref_groups, TRUE);