Ticket #1396: Fixed logic of processing '--with-search-engine' configure option.
[midnight-commander.git] / src / setup.c
blob7b7cee16fa4190dde24d403b9e47be2a327c2485
1 /* Setup loading/saving.
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2009 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
19 /** \file setup.c
20 * \brief Source: setup loading/saving
23 #include <config.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <stdio.h>
28 #include <sys/types.h>
29 #include <sys/stat.h>
31 #include "global.h"
33 #include "../src/tty/tty.h"
34 #include "../src/tty/key.h"
35 #include "../src/tty/mouse.h" /* To make view.h happy */
37 #include "args.h"
38 #include "dir.h"
39 #include "panel.h"
40 #include "main.h"
41 #include "tree.h" /* xtree_mode */
42 #include "../src/mcconfig/mcconfig.h"
43 #include "setup.h"
44 #include "../src/viewer/mcviewer.h" /* For the externs */
45 #include "hotlist.h" /* load/save/done hotlist */
46 #include "panelize.h" /* load/save/done panelize */
47 #include "layout.h"
48 #include "menu.h" /* menubar_visible declaration */
49 #include "cmd.h"
50 #include "file.h" /* safe_delete */
51 #include "keybind.h" /* lookup_action */
52 #include "fileloc.h"
53 #include "wtools.h"
55 #ifdef USE_VFS
56 #include "../vfs/gc.h"
57 #endif
59 #ifdef HAVE_CHARSET
60 #include "charsets.h"
61 #endif
63 #ifdef USE_NETCODE
64 # include "../vfs/ftpfs.h"
65 # include "../vfs/fish.h"
66 #endif
68 #ifdef USE_INTERNAL_EDIT
69 # include "../edit/edit.h"
70 #endif
72 #include "../src/strutil.h" /* str_isutf8 () */
75 extern char *find_ignore_dirs;
77 extern int num_history_items_recorded;
79 char *profile_name; /* .mc/ini */
80 char *global_profile_name; /* mc.lib */
82 char *setup_color_string;
83 char *term_color_string;
84 char *color_terminal_string;
86 int startup_left_mode;
87 int startup_right_mode;
89 /* Ugly hack to allow panel_save_setup to work as a place holder for */
90 /* default panel values */
91 int saving_setup;
92 int setup_copymove_persistent_attr = 1;
94 static char *panels_profile_name = NULL; /* .mc/panels.ini */
96 static const struct {
97 const char *key;
98 int list_type;
99 } list_types [] = {
100 { "full", list_full },
101 { "brief", list_brief },
102 { "long", list_long },
103 { "user", list_user },
104 { 0, 0 }
107 static const struct {
108 const char *opt_name;
109 int opt_type;
110 } panel_types [] = {
111 { "listing", view_listing },
112 { "quickview", view_quick },
113 { "info", view_info },
114 { "tree", view_tree },
115 { 0, 0 }
118 static const struct {
119 const char *opt_name;
120 int *opt_addr;
121 } layout [] = {
122 { "equal_split", &equal_split },
123 { "first_panel_size", &first_panel_size },
124 { "message_visible", &message_visible },
125 { "keybar_visible", &keybar_visible },
126 { "xterm_title", &xterm_title },
127 { "output_lines", &output_lines },
128 { "command_prompt", &command_prompt },
129 { "menubar_visible", &menubar_visible },
130 { "show_mini_info", &show_mini_info },
131 { "permission_mode", &permission_mode },
132 { "filetype_mode", &filetype_mode },
133 { "free_space", &free_space },
134 { 0, 0 }
137 static const struct {
138 const char *opt_name;
139 int *opt_addr;
140 } int_options [] = {
141 { "show_backups", &show_backups },
142 { "kilobyte_si", &kilobyte_si },
143 { "show_dot_files", &show_dot_files },
144 { "verbose", &verbose },
145 { "mark_moves_down", &mark_moves_down },
146 { "pause_after_run", &pause_after_run },
147 { "shell_patterns", &easy_patterns },
148 { "auto_save_setup", &auto_save_setup },
149 { "auto_menu", &auto_menu },
150 { "use_internal_view", &use_internal_view },
151 { "use_internal_edit", &use_internal_edit },
152 { "clear_before_exec", &clear_before_exec },
153 { "mix_all_files", &mix_all_files },
154 { "fast_reload", &fast_reload },
155 { "fast_reload_msg_shown", &fast_reload_w },
156 { "confirm_delete", &confirm_delete },
157 { "confirm_overwrite", &confirm_overwrite },
158 { "confirm_execute", &confirm_execute },
159 { "confirm_exit", &confirm_exit },
160 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
161 { "safe_delete", &safe_delete },
162 { "mouse_repeat_rate", &mou_auto_repeat },
163 { "double_click_speed", &double_click_speed },
164 #ifndef HAVE_CHARSET
165 { "eight_bit_clean", &eight_bit_clean },
166 { "full_eight_bits", &full_eight_bits },
167 #endif /* !HAVE_CHARSET */
168 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
169 { "confirm_view_dir", &confirm_view_dir },
170 { "mouse_move_pages", &mouse_move_pages },
171 { "mouse_move_pages_viewer", &mcview_mouse_move_pages },
172 { "mouse_close_dialog", &mouse_close_dialog},
173 { "fast_refresh", &fast_refresh },
174 { "navigate_with_arrows", &navigate_with_arrows },
175 { "drop_menus", &drop_menus },
176 { "wrap_mode", &mcview_global_wrap_mode},
177 { "old_esc_mode", &old_esc_mode },
178 { "cd_symlinks", &cd_symlinks },
179 { "show_all_if_ambiguous", &show_all_if_ambiguous },
180 { "max_dirt_limit", &mcview_max_dirt_limit },
181 { "torben_fj_mode", &torben_fj_mode },
182 { "use_file_to_guess_type", &use_file_to_check_type },
183 { "alternate_plus_minus", &alternate_plus_minus },
184 { "only_leading_plus_minus", &only_leading_plus_minus },
185 { "show_output_starts_shell", &output_starts_shell },
186 { "panel_scroll_pages", &panel_scroll_pages },
187 { "xtree_mode", &xtree_mode },
188 { "num_history_items_recorded", &num_history_items_recorded },
189 { "file_op_compute_totals", &file_op_compute_totals },
190 #ifdef USE_VFS
191 { "vfs_timeout", &vfs_timeout },
192 #ifdef USE_NETCODE
193 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
194 { "use_netrc", &use_netrc },
195 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
196 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
197 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
198 { "ftpfs_use_passive_connections_over_proxy", &ftpfs_use_passive_connections_over_proxy },
199 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
200 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
201 { "fish_directory_timeout", &fish_directory_timeout },
202 #endif /* USE_NETCODE */
203 #endif /* USE_VFS */
204 #ifdef USE_INTERNAL_EDIT
205 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
206 { "editor_tab_spacing", &option_tab_spacing },
207 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
208 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
209 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
210 { "editor_fake_half_tabs", &option_fake_half_tabs },
211 { "editor_option_save_mode", &option_save_mode },
212 { "editor_option_save_position", &option_save_position },
213 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
214 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
215 { "editor_edit_confirm_save", &edit_confirm_save },
216 { "editor_syntax_highlighting", &option_syntax_highlighting },
217 { "editor_persistent_selections", &option_persistent_selections },
218 { "editor_cursor_beyond_eol", &option_cursor_beyond_eol },
219 { "editor_visible_tabs", &visible_tabs },
220 { "editor_visible_spaces", &visible_tws },
221 { "editor_line_state", &option_line_state },
222 { "editor_simple_statusbar", &simple_statusbar },
223 { "editor_check_new_line", &option_check_nl_at_eof },
224 #endif /* USE_INTERNAL_EDIT */
226 { "nice_rotating_dash", &nice_rotating_dash },
227 { "horizontal_split", &horizontal_split },
228 { "mcview_remember_file_position", &mcview_remember_file_position },
229 { "auto_fill_mkdir_name", &auto_fill_mkdir_name },
230 { "reverse_files_only", &reverse_files_only },
231 { "copymove_persistent_attr", &setup_copymove_persistent_attr },
232 { "select_flags", &select_flags },
233 { 0, 0 }
236 static const struct {
237 const char *opt_name;
238 char **opt_addr;
239 const char *opt_defval;
240 } str_options [] = {
241 #ifdef USE_INTERNAL_EDIT
242 { "editor_backup_extension", &option_backup_ext, "~" },
243 #endif
244 { NULL, NULL, NULL }
247 void
248 panel_save_setup (struct WPanel *panel, const char *section)
250 char *buffer;
251 int i;
253 mc_config_set_int(mc_panels_config, section, "reverse", panel->reverse);
254 mc_config_set_int(mc_panels_config, section, "case_sensitive", panel->case_sensitive);
255 mc_config_set_int(mc_panels_config, section, "exec_first", panel->exec_first);
258 mc_config_set_string(mc_panels_config, section, "sort_order", panel->current_sort_field->id);
260 for (i = 0; list_types [i].key; i++)
261 if (list_types [i].list_type == panel->list_type){
262 mc_config_set_string(mc_panels_config, section, "list_mode", list_types [i].key);
263 break;
266 mc_config_set_string(mc_panels_config, section, "user_format", panel->user_format);
268 for (i = 0; i < LIST_TYPES; i++){
269 buffer = g_strdup_printf("user_status%d", i);
270 mc_config_set_string(mc_panels_config, section, buffer, panel->user_status_format [i]);
271 g_free(buffer);
274 mc_config_set_int(mc_panels_config, section, "user_mini_status", panel->user_mini_status);
277 void
278 save_layout (void)
280 char *profile;
281 int i;
283 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
285 /* Save integer options */
286 for (i = 0; layout [i].opt_name; i++){
287 mc_config_set_int(mc_main_config, "Layout", layout [i].opt_name, *layout [i].opt_addr);
289 mc_config_save_to_file (mc_main_config, profile, NULL);
291 g_free (profile);
294 void
295 save_configure (void)
297 char *profile;
298 GError *error = NULL;
299 int i;
301 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
303 /* Save integer options */
304 for (i = 0; int_options[i].opt_name; i++)
305 mc_config_set_int(mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name, *int_options[i].opt_addr);
307 /* Save string options */
308 for (i = 0; str_options[i].opt_name != NULL; i++)
309 mc_config_set_string(mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name, *str_options[i].opt_addr);
311 if (! mc_config_save_to_file (mc_main_config, profile, &error))
312 setup_save_config_show_error(profile, &error);
314 g_free (profile);
317 static void
318 panel_save_type (const char *section, int type)
320 int i;
322 for (i = 0; panel_types [i].opt_name; i++)
323 if (panel_types [i].opt_type == type){
324 mc_config_set_string(mc_panels_config, section,
325 "display", panel_types [i].opt_name);
326 break;
330 void
331 save_panel_types (void)
333 int type;
335 type = get_display_type (0);
336 panel_save_type ("New Left Panel", type);
337 if (type == view_listing)
338 panel_save_setup (left_panel, left_panel->panel_name);
339 type = get_display_type (1);
340 panel_save_type ("New Right Panel", type);
341 if (type == view_listing)
342 panel_save_setup (right_panel, right_panel->panel_name);
344 mc_config_set_string (mc_panels_config, "Dirs" , "other_dir",
345 get_panel_dir_for (other_panel));
347 if (current_panel != NULL)
348 mc_config_set_string (mc_panels_config, "Dirs" , "current_is_left",
349 get_current_index () == 0 ? "1" : "0");
351 if (mc_panels_config->ini_path == NULL)
352 mc_panels_config->ini_path = g_strdup(panels_profile_name);
354 mc_config_del_group (mc_panels_config, "Temporal:New Left Panel");
355 mc_config_del_group (mc_panels_config, "Temporal:New Right Panel");
357 mc_config_save_file (mc_panels_config, NULL);
360 gboolean
361 save_setup (void)
363 char *tmp_profile;
364 gboolean ret;
366 saving_setup = 1;
368 save_configure ();
370 save_layout ();
372 save_hotlist ();
374 save_panelize ();
375 save_panel_types ();
376 /* directory_history_save (); */
378 #if defined(USE_VFS) && defined (USE_NETCODE)
379 mc_config_set_string(mc_main_config, "Misc" , "ftpfs_password",
380 ftpfs_anonymous_passwd);
381 if (ftpfs_proxy_host)
382 mc_config_set_string(mc_main_config, "Misc" , "ftp_proxy_host",
383 ftpfs_proxy_host);
384 #endif /* USE_VFS && USE_NETCODE */
386 #ifdef HAVE_CHARSET
387 mc_config_set_string(mc_main_config, "Misc" , "display_codepage",
388 get_codepage_id( display_codepage ));
389 mc_config_set_string(mc_main_config, "Misc" , "source_codepage",
390 get_codepage_id( source_codepage ));
391 #endif /* HAVE_CHARSET */
392 tmp_profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
393 ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL);
395 g_free (tmp_profile);
396 saving_setup = 0;
397 return ret;
400 void
401 panel_load_setup (WPanel *panel, const char *section)
403 int i;
404 char *buffer;
406 panel->reverse = mc_config_get_int(mc_panels_config, section, "reverse", 0);
407 panel->case_sensitive = mc_config_get_int(mc_panels_config, section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
408 panel->exec_first = mc_config_get_int(mc_panels_config, section, "exec_first", 0);
410 /* Load sort order */
411 buffer = mc_config_get_string(mc_panels_config, section, "sort_order", "name");
412 panel->current_sort_field = panel_get_field_by_id(buffer);
413 if (panel->current_sort_field == NULL )
414 panel->current_sort_field = panel_get_field_by_id("name");
416 g_free(buffer);
418 /* Load the listing mode */
419 buffer = mc_config_get_string(mc_panels_config, section, "list_mode", "full");
420 panel->list_type = list_full;
421 for (i = 0; list_types [i].key; i++)
422 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
423 panel->list_type = list_types [i].list_type;
424 break;
426 g_free(buffer);
428 /* User formats */
429 g_free (panel->user_format);
430 panel->user_format = mc_config_get_string(mc_panels_config, section, "user_format", DEFAULT_USER_FORMAT);
432 for (i = 0; i < LIST_TYPES; i++){
433 g_free (panel->user_status_format [i]);
434 buffer = g_strdup_printf("user_status%d",i);
435 panel->user_status_format [i] =
436 mc_config_get_string(mc_panels_config, section, buffer, DEFAULT_USER_FORMAT);
437 g_free(buffer);
440 panel->user_mini_status =
441 mc_config_get_int(mc_panels_config, section, "user_mini_status", 0);
445 static void
446 load_layout ()
448 int i;
450 for (i = 0; layout [i].opt_name; i++)
451 *layout [i].opt_addr =
452 mc_config_get_int(mc_main_config,"Layout", layout [i].opt_name, *layout [i].opt_addr);
455 static int
456 setup__load_panel_state (const char *section)
458 char *buffer;
459 int i;
461 int mode = view_listing;
463 /* Load the display mode */
464 buffer = mc_config_get_string(mc_panels_config, section, "display", "listing");
466 for (i = 0; panel_types [i].opt_name; i++)
467 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
468 mode = panel_types [i].opt_type;
469 break;
471 g_free(buffer);
472 return mode;
475 static const char *
476 setup__is_cfg_group_must_panel_config(const char *grp)
478 if (
479 ! strcasecmp("Dirs",grp) ||
480 ! strcasecmp("Temporal:New Right Panel",grp) ||
481 ! strcasecmp("Temporal:New Left Panel",grp) ||
482 ! strcasecmp("New Left Panel",grp) ||
483 ! strcasecmp("New Right Panel",grp)
485 return grp;
486 return NULL;
489 static void
490 setup__move_panels_config_into_separate_file(const char*profile)
492 mc_config_t *tmp_cfg;
493 char **groups, **curr_grp;
494 const char *need_grp;
496 if (!exist_file(profile))
497 return;
499 tmp_cfg = mc_config_init(profile);
500 if (!tmp_cfg)
501 return;
503 curr_grp = groups = mc_config_get_groups (tmp_cfg, NULL);
504 if (!groups)
506 mc_config_deinit(tmp_cfg);
507 return;
509 while (*curr_grp)
511 if ( setup__is_cfg_group_must_panel_config(*curr_grp) == NULL)
513 mc_config_del_group (tmp_cfg, *curr_grp);
515 curr_grp++;
518 mc_config_save_to_file (tmp_cfg, panels_profile_name, NULL);
519 mc_config_deinit(tmp_cfg);
521 tmp_cfg = mc_config_init(profile);
522 if (!tmp_cfg)
524 g_strfreev(groups);
525 return;
528 curr_grp = groups;
530 while (*curr_grp)
532 need_grp = setup__is_cfg_group_must_panel_config(*curr_grp);
533 if ( need_grp != NULL)
535 mc_config_del_group (tmp_cfg, need_grp);
537 curr_grp++;
539 g_strfreev(groups);
540 mc_config_save_file (tmp_cfg, NULL);
541 mc_config_deinit(tmp_cfg);
546 Get name of config file.
548 \param subdir
549 if not NULL, then config also search into specified subdir
551 \param config_file_name
552 If specified filename is relative, then will search in standart patches.
554 \return
555 Newly allocated path to config name or NULL if file not found
557 If config_file_name is a relative path, then search config in stantart pathes */
558 static char*
559 load_setup_get_full_config_name(const char *subdir, const char *config_file_name)
562 TODO: IMHO, in future this function must be placed into mc_config module.
563 Also, need to rename stupid mc_home and mc_home_alt to mc_sysconfdir and mc_datadir;
564 home_mc => mc_user_homedir
566 char *lc_basename, *ret;
568 if (config_file_name == NULL)
569 return NULL;
571 if (g_path_is_absolute (config_file_name))
572 return g_strdup(config_file_name);
575 lc_basename = g_path_get_basename(config_file_name);
576 if (lc_basename == NULL)
577 return NULL;
580 if (subdir)
581 ret = g_build_filename (home_dir, MC_USERCONF_DIR, subdir, lc_basename, NULL);
582 else
583 ret = g_build_filename (home_dir, MC_USERCONF_DIR, lc_basename, NULL);
585 if (exist_file(ret)) {
586 g_free(lc_basename);
587 return ret;
589 g_free(ret);
592 if (subdir)
593 ret = g_build_filename (mc_home, subdir, lc_basename, NULL);
594 else
595 ret = g_build_filename (mc_home, lc_basename, NULL);
597 if (exist_file(ret)) {
598 g_free(lc_basename);
599 return ret;
601 g_free(ret);
603 if (subdir)
604 ret = g_build_filename (mc_home_alt, subdir, lc_basename, NULL);
605 else
606 ret = g_build_filename (mc_home_alt, lc_basename, NULL);
608 if (exist_file(ret)) {
609 g_free(lc_basename);
610 return ret;
612 g_free(ret);
613 g_free(lc_basename);
614 return NULL;
619 Create new mc_config object from specified ini-file or
620 append data to existing mc_config object from ini-file
623 static void
624 load_setup_init_config_from_file(mc_config_t **config, const char *fname)
627 TODO: IMHO, in future this function must be placed into mc_config module.
629 if (exist_file(fname)) {
630 if (*config)
631 mc_config_read_file( *config, fname);
632 else
633 *config = mc_config_init(fname);
639 static mc_config_t *
640 load_setup_get_keymap_profile_config(void)
643 TODO: IMHO, in future this function must be placed into mc_config module.
645 mc_config_t *keymap_config = NULL ;
647 char *fname, *fname2;
649 /* 1) /usr/share/mc (mc_home_alt) */
650 fname = g_build_filename (mc_home_alt, GLOBAL_KEYMAP_FILE, NULL);
651 load_setup_init_config_from_file( &keymap_config, fname);
652 g_free(fname);
654 /* 2) /etc/mc (mc_home) */
655 fname = g_build_filename (mc_home, GLOBAL_KEYMAP_FILE, NULL);
656 load_setup_init_config_from_file( &keymap_config, fname);
657 g_free(fname);
659 /* 3) ~/.mc (home_dir?) */
660 fname = g_build_filename (home_dir, MC_USERCONF_DIR, GLOBAL_KEYMAP_FILE, NULL);
661 load_setup_init_config_from_file( &keymap_config, fname);
662 g_free(fname);
664 /* 4) main config; [Midnight Commander] -> keymap*/
666 fname2 = mc_config_get_string(mc_main_config, CONFIG_APP_SECTION, "keymap" , GLOBAL_KEYMAP_FILE);
667 fname = load_setup_get_full_config_name(NULL, fname2);
668 if (fname)
670 load_setup_init_config_from_file( &keymap_config, fname);
671 g_free(fname);
673 g_free(fname2);
675 /* 5) getenv("MC_KEYMAP") */
676 fname = load_setup_get_full_config_name(NULL, g_getenv ("MC_KEYMAP"));
677 if (fname)
679 load_setup_init_config_from_file( &keymap_config, fname);
680 g_free(fname);
683 /* 6) --keymap=<keymap> */
684 fname = load_setup_get_full_config_name(NULL, mc_args__keymap_file);
685 if (fname)
687 load_setup_init_config_from_file( &keymap_config, fname);
688 g_free(fname);
691 return keymap_config;
694 char *
695 setup_init (void)
697 char *profile;
698 char *inifile;
700 if (profile_name)
701 return profile_name;
703 profile = g_build_filename (home_dir, MC_USERCONF_DIR, MC_CONFIG_FILE, NULL);
704 if (!exist_file (profile)){
705 inifile = concat_dir_and_file (mc_home, "mc.ini");
706 if (exist_file (inifile)){
707 g_free (profile);
708 profile = inifile;
709 } else {
710 g_free (inifile);
711 inifile = concat_dir_and_file (mc_home_alt, "mc.ini");
712 if (exist_file (inifile)) {
713 g_free (profile);
714 profile = inifile;
715 } else
716 g_free (inifile);
720 profile_name = profile;
722 return profile;
725 void
726 load_setup (void)
728 char *profile;
729 int i;
730 char *buffer;
732 profile = setup_init ();
734 /* mc.lib is common for all users, but has priority lower than
735 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
736 global_profile_name = concat_dir_and_file (mc_home, MC_GLOBAL_CONFIG_FILE);
737 if (!exist_file(global_profile_name)) {
738 g_free (global_profile_name);
739 global_profile_name = concat_dir_and_file (mc_home_alt, MC_GLOBAL_CONFIG_FILE);
742 panels_profile_name = g_build_filename (home_dir, MC_USERCONF_DIR, MC_PANELS_FILE, NULL);
744 mc_main_config = mc_config_init(profile);
746 if (!exist_file(panels_profile_name))
747 setup__move_panels_config_into_separate_file(profile);
749 mc_panels_config = mc_config_init(panels_profile_name);
751 /* Load integer boolean options */
752 for (i = 0; int_options[i].opt_name; i++)
753 *int_options[i].opt_addr =
754 mc_config_get_int(mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name, *int_options[i].opt_addr);
756 /* Load string options */
757 for (i = 0; str_options[i].opt_name != NULL; i++)
758 *str_options[i].opt_addr =
759 mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name, str_options[i].opt_defval);
761 load_layout ();
763 load_panelize ();
765 startup_left_mode = setup__load_panel_state ("New Left Panel");
766 startup_right_mode = setup__load_panel_state ("New Right Panel");
768 /* At least one of the panels is a listing panel */
769 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
770 startup_left_mode = view_listing;
772 if (!other_dir){
773 buffer = mc_config_get_string(mc_panels_config, "Dirs", "other_dir", ".");
774 if (vfs_file_is_local (buffer))
775 other_dir = buffer;
776 else
777 g_free (buffer);
780 boot_current_is_left =
781 mc_config_get_int(mc_panels_config, "Dirs", "current_is_left", 1);
783 #ifdef USE_NETCODE
784 ftpfs_proxy_host = mc_config_get_string(mc_main_config, "Misc", "ftp_proxy_host", "gate");
785 #endif
786 setup_color_string = mc_config_get_string(mc_main_config, "Misc", "find_ignore_dirs", "");
787 if (setup_color_string [0])
788 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", (char *) NULL);
789 g_free(setup_color_string);
791 /* The default color and the terminal dependent color */
792 setup_color_string = mc_config_get_string(mc_main_config, "Colors", "base_color", "");
793 term_color_string = mc_config_get_string(mc_main_config, "Colors", getenv ("TERM"), "");
794 color_terminal_string = mc_config_get_string(mc_main_config, "Colors", "color_terminals", "");
796 /* Load the directory history */
797 /* directory_history_load (); */
798 /* Remove the temporal entries */
799 #if defined(USE_VFS) && defined (USE_NETCODE)
800 ftpfs_init_passwd ();
801 #endif /* USE_VFS && USE_NETCODE */
803 #ifdef HAVE_CHARSET
804 if ( load_codepages_list() > 0 ) {
805 buffer = mc_config_get_string(mc_main_config, "Misc", "display_codepage", "");
806 if ( buffer[0] != '\0' )
808 display_codepage = get_codepage_index( buffer );
809 cp_display = get_codepage_id (display_codepage);
811 g_free(buffer);
812 buffer = mc_config_get_string(mc_main_config, "Misc", "source_codepage", "");
813 if ( buffer[0] != '\0' )
815 source_codepage = get_codepage_index( buffer );
816 cp_source = get_codepage_id (source_codepage);
818 g_free(buffer);
820 init_translation_table( source_codepage, display_codepage );
821 if ( get_codepage_id( display_codepage ) )
822 utf8_display = str_isutf8 (get_codepage_id( display_codepage ));
823 #endif /* HAVE_CHARSET */
826 #if defined(USE_VFS) && defined (USE_NETCODE)
827 char *
828 load_anon_passwd ()
830 char *buffer;
832 buffer = mc_config_get_string(mc_main_config, "Misc", "ftpfs_password", "");
833 if (buffer [0])
834 return buffer;
836 g_free(buffer);
837 return NULL;
839 #endif /* USE_VFS && USE_NETCODE */
841 void
842 done_setup (void)
844 int i;
846 g_free (profile_name);
847 g_free (global_profile_name);
848 g_free (color_terminal_string);
849 g_free (term_color_string);
850 g_free (setup_color_string);
851 g_free (panels_profile_name);
852 mc_config_deinit (mc_main_config);
853 mc_config_deinit (mc_panels_config);
855 for (i = 0; str_options[i].opt_name != NULL; i++)
856 g_free (*str_options[i].opt_addr);
858 done_hotlist ();
859 done_panelize ();
860 /* directory_history_free (); */
863 static void
864 load_keys_from_section (const char *terminal, mc_config_t *cfg)
866 char *section_name;
867 gchar **profile_keys, **keys;
868 gchar **values, **curr_values;
869 char *valcopy, *value;
870 long key_code;
871 gsize len, values_len;
873 if (!terminal)
874 return;
876 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
877 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
879 while (*profile_keys){
881 /* copy=other causes all keys from [terminal:other] to be loaded. */
882 if (g_strcasecmp (*profile_keys, "copy") == 0) {
883 valcopy = mc_config_get_string (cfg, section_name, *profile_keys, "");
884 load_keys_from_section (valcopy, cfg);
885 g_free(valcopy);
886 profile_keys++;
887 continue;
889 curr_values = values = mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
891 key_code = lookup_key (*profile_keys, NULL);
892 if (key_code != 0) {
893 if (curr_values){
894 while (*curr_values){
895 valcopy = convert_controls (*curr_values);
896 define_sequence (key_code, valcopy, MCKEY_NOACTION);
897 g_free (valcopy);
898 curr_values++;
900 } else {
901 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
902 valcopy = convert_controls (value);
903 define_sequence (key_code, valcopy, MCKEY_NOACTION);
904 g_free (valcopy);
905 g_free (value);
908 profile_keys++;
909 if (values)
910 g_strfreev (values);
912 g_strfreev (keys);
913 g_free (section_name);
916 void load_key_defs (void)
919 * Load keys from mc.lib before ~/.mc/ini, so that the user
920 * definitions override global settings.
922 mc_config_t *mc_global_config;
924 mc_global_config = mc_config_init(global_profile_name);
925 if (mc_global_config != NULL)
927 load_keys_from_section ("general", mc_global_config);
928 load_keys_from_section (getenv ("TERM"), mc_global_config);
929 mc_config_deinit(mc_global_config);
931 load_keys_from_section ("general", mc_main_config);
932 load_keys_from_section (getenv ("TERM"), mc_main_config);
936 static void
937 load_keymap_from_section (const char *section_name, GArray *keymap, mc_config_t *cfg)
939 gchar **profile_keys, **keys;
940 gchar **values, **curr_values;
941 char *valcopy, *value;
942 int action;
943 gsize len, values_len;
945 if (!section_name)
946 return;
948 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
950 while (*profile_keys) {
951 curr_values = values = mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
952 action = lookup_action (*profile_keys);
953 if (action>0) {
954 if (curr_values) {
955 while (*curr_values) {
956 valcopy = convert_controls (*curr_values);
957 keybind_cmd_bind (keymap, valcopy, action);
958 g_free (valcopy);
959 curr_values++;
961 } else {
962 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
963 valcopy = convert_controls (value);
964 /* define_sequence (key_code, valcopy, MCKEY_NOACTION); */
965 g_free (valcopy);
966 g_free (value);
969 profile_keys++;
970 if (values)
971 g_strfreev(values);
973 g_strfreev(keys);
976 void
977 load_keymap_defs (void)
980 * Load keymap from GLOBAL_KEYMAP_FILE before ~/.mc/keymap, so that the user
981 * definitions override global settings.
983 mc_config_t *mc_global_keymap;
985 mc_global_keymap = load_setup_get_keymap_profile_config();
987 if (mc_global_keymap != NULL)
989 #ifdef USE_INTERNAL_EDIT
990 editor_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
991 load_keymap_from_section ("editor", editor_keymap, mc_global_keymap);
992 editor_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
993 load_keymap_from_section ("editor:xmap", editor_x_keymap, mc_global_keymap);
994 #endif
996 viewer_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
997 load_keymap_from_section ("viewer", viewer_keymap, mc_global_keymap);
998 viewer_hex_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
999 load_keymap_from_section ("viewer:hex", viewer_hex_keymap, mc_global_keymap);
1001 main_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1002 load_keymap_from_section ("main", main_keymap, mc_global_keymap);
1003 main_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1004 load_keymap_from_section ("main:xmap", main_x_keymap, mc_global_keymap);
1006 panel_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1007 load_keymap_from_section ("panel", panel_keymap, mc_global_keymap);
1009 input_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1010 load_keymap_from_section ("input", input_keymap, mc_global_keymap);
1012 tree_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1013 load_keymap_from_section ("tree", tree_keymap, mc_global_keymap);
1015 help_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1016 load_keymap_from_section ("help", help_keymap, mc_global_keymap);
1018 mc_config_deinit (mc_global_keymap);
1022 void
1023 free_keymap_defs (void)
1025 #ifdef USE_INTERNAL_EDIT
1026 g_array_free (editor_keymap, TRUE);
1027 g_array_free (editor_x_keymap, TRUE);
1028 #endif
1029 g_array_free (viewer_keymap, TRUE);
1030 g_array_free (viewer_hex_keymap, TRUE);
1031 g_array_free (main_keymap, TRUE);
1032 g_array_free (main_x_keymap, TRUE);
1033 g_array_free (panel_keymap, TRUE);
1034 g_array_free (input_keymap, TRUE);
1035 g_array_free (tree_keymap, TRUE);
1036 g_array_free (help_keymap, TRUE);
1039 void
1040 setup_save_config_show_error(const char *filename, GError **error)
1042 if (error == NULL || *error == NULL)
1043 return;
1045 message (D_ERROR, MSG_ERROR, _("Cannot save file %s:\n%s"),
1046 filename, (*error)->message);
1048 g_error_free(*error);
1049 *error = NULL;