Optimization of history load.
[midnight-commander.git] / src / setup.c
blob8a775c712ea4bff4384b266707ba40800ab07d87
1 /* Setup loading/saving.
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2009, 2010 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 "lib/global.h"
33 #include "lib/tty/tty.h"
34 #include "lib/tty/key.h"
35 #include "lib/mcconfig.h"
36 #include "lib/fileloc.h"
37 #include "lib/timefmt.h"
38 #include "lib/util.h"
39 #include "lib/widget.h"
41 #include "lib/vfs/vfs.h"
43 #ifdef ENABLE_VFS_FTP
44 #include "src/vfs/ftpfs/ftpfs.h"
45 #endif
46 #ifdef ENABLE_VFS_FISH
47 #include "src/vfs/fish/fish.h"
48 #endif
50 #ifdef HAVE_CHARSET
51 #include "lib/charsets.h"
52 #endif
54 #include "filemanager/dir.h"
55 #include "filemanager/midnight.h"
56 #include "filemanager/tree.h" /* xtree_mode */
57 #include "filemanager/hotlist.h" /* load/save/done hotlist */
58 #include "filemanager/panelize.h" /* load/save/done panelize */
59 #include "filemanager/layout.h"
60 #include "filemanager/cmd.h"
62 #include "args.h"
63 #include "execute.h" /* pause_after_run */
64 #include "clipboard.h"
65 #include "keybind-defaults.h" /* keybind_lookup_action */
67 #ifdef HAVE_CHARSET
68 #include "selcodepage.h"
69 #endif
71 #ifdef USE_INTERNAL_EDIT
72 #include "src/editor/edit.h"
73 #endif
75 #include "src/viewer/mcviewer.h" /* For the externs */
77 #include "setup.h"
79 /*** global variables ****************************************************************************/
81 char *profile_name; /* ${XDG_CONFIG_HOME}/mc/ini */
82 char *global_profile_name; /* mc.lib */
84 /* Only used at program boot */
85 gboolean boot_current_is_left = TRUE;
87 /* If on, default for "No" in delete operations */
88 int safe_delete = 0;
90 /* Controls screen clearing before an exec */
91 int clear_before_exec = 1;
93 /* Asks for confirmation before deleting a file */
94 int confirm_delete = 1;
95 /* Asks for confirmation before deleting a hotlist entry */
96 int confirm_directory_hotlist_delete = 1;
97 /* Asks for confirmation before overwriting a file */
98 int confirm_overwrite = 1;
99 /* Asks for confirmation before executing a program by pressing enter */
100 int confirm_execute = 0;
101 /* Asks for confirmation before leaving the program */
102 int confirm_exit = 1;
104 /* If true, at startup the user-menu is invoked */
105 int auto_menu = 0;
106 /* This flag indicates if the pull down menus by default drop down */
107 int drop_menus = 0;
109 /* Asks for confirmation when using F3 to view a directory and there
110 are tagged files */
111 int confirm_view_dir = 0;
113 panel_view_mode_t startup_left_mode;
114 panel_view_mode_t startup_right_mode;
116 int setup_copymove_persistent_attr = 1;
118 /* Tab size */
119 int option_tab_spacing = DEFAULT_TAB_SPACING;
121 /* Ugly hack to allow panel_save_setup to work as a place holder for */
122 /* default panel values */
123 int saving_setup;
125 panels_options_t panels_options = {
126 .show_mini_info = TRUE,
127 .kilobyte_si = FALSE,
128 .mix_all_files = FALSE,
129 .show_backups = TRUE,
130 .show_dot_files = TRUE,
131 .fast_reload = FALSE,
132 .fast_reload_msg_shown = FALSE,
133 .mark_moves_down = TRUE,
134 .reverse_files_only = TRUE,
135 .auto_save_setup = FALSE,
136 .navigate_with_arrows = FALSE,
137 .scroll_pages = TRUE,
138 .mouse_move_pages = TRUE,
139 .filetype_mode = TRUE,
140 .permission_mode = FALSE,
141 .qsearch_mode = QSEARCH_PANEL_CASE,
142 .torben_fj_mode = FALSE
145 int easy_patterns = 1;
147 /* It true saves the setup when quitting */
148 int auto_save_setup = 1;
150 /* If true, then the +, - and \ keys have their special meaning only if the
151 * command line is emtpy, otherwise they behave like regular letters
153 int only_leading_plus_minus = 1;
155 /* Automatically fills name with current selected item name on mkdir */
156 int auto_fill_mkdir_name = 1;
158 /* If set and you don't have subshell support,then C-o will give you a shell */
159 int output_starts_shell = 0;
161 /* If set, we execute the file command to check the file type */
162 int use_file_to_check_type = 1;
164 int verbose = 1;
167 * Whether the Midnight Commander tries to provide more
168 * information about copy/move sizes and bytes transfered
169 * at the expense of some speed
171 int file_op_compute_totals = 1;
173 /*** file scope macro definitions ****************************************************************/
175 /* In order to use everywhere the same setup for the locale we use defines */
176 #define FMTYEAR _("%b %e %Y")
177 #define FMTTIME _("%b %e %H:%M")
179 /*** file scope type declarations ****************************************************************/
181 /*** file scope variables ************************************************************************/
183 static char *panels_profile_name = NULL; /* ${XDG_CACHE_HOME}/mc/panels.ini */
185 /* *INDENT-OFF* */
186 static const struct
188 const char *key;
189 int list_type;
190 } list_types [] = {
191 { "full", list_full },
192 { "brief", list_brief },
193 { "long", list_long },
194 { "user", list_user },
195 { NULL, 0 }
198 static const struct
200 const char *opt_name;
201 panel_view_mode_t opt_type;
202 } panel_types [] = {
203 { "listing", view_listing },
204 { "quickview", view_quick },
205 { "info", view_info },
206 { "tree", view_tree },
207 { NULL, view_listing }
210 static const struct
212 const char *opt_name;
213 int *opt_addr;
214 } layout [] = {
215 { "equal_split", &equal_split },
216 { "first_panel_size", &first_panel_size },
217 { "message_visible", &mc_global.message_visible },
218 { "keybar_visible", &mc_global.keybar_visible },
219 { "xterm_title", &xterm_title },
220 { "output_lines", &output_lines },
221 { "command_prompt", &command_prompt },
222 { "menubar_visible", &menubar_visible },
223 { "free_space", &free_space },
224 { NULL, NULL }
227 static const struct
229 const char *opt_name;
230 int *opt_addr;
231 } int_options [] = {
232 { "verbose", &verbose },
233 { "pause_after_run", &pause_after_run },
234 { "shell_patterns", &easy_patterns },
235 { "auto_save_setup", &auto_save_setup },
236 { "auto_menu", &auto_menu },
237 { "use_internal_view", &use_internal_view },
238 { "use_internal_edit", &use_internal_edit },
239 { "clear_before_exec", &clear_before_exec },
240 { "confirm_delete", &confirm_delete },
241 { "confirm_overwrite", &confirm_overwrite },
242 { "confirm_execute", &confirm_execute },
243 { "confirm_history_cleanup", &mc_global.widget.confirm_history_cleanup },
244 { "confirm_exit", &confirm_exit },
245 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
246 { "safe_delete", &safe_delete },
247 { "mouse_repeat_rate", &mou_auto_repeat },
248 { "double_click_speed", &double_click_speed },
249 #ifndef HAVE_CHARSET
250 { "eight_bit_clean", &mc_global.eight_bit_clean },
251 { "full_eight_bits", &mc_global.full_eight_bits },
252 #endif /* !HAVE_CHARSET */
253 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
254 { "confirm_view_dir", &confirm_view_dir },
255 { "mouse_move_pages_viewer", &mcview_mouse_move_pages },
256 { "mouse_close_dialog", &mouse_close_dialog},
257 { "fast_refresh", &fast_refresh },
258 { "drop_menus", &drop_menus },
259 { "wrap_mode", &mcview_global_wrap_mode},
260 { "old_esc_mode", &old_esc_mode },
261 { "old_esc_mode_timeout", &old_esc_mode_timeout },
262 { "cd_symlinks", &mc_global.vfs.cd_symlinks },
263 { "show_all_if_ambiguous", &mc_global.widget.show_all_if_ambiguous },
264 { "max_dirt_limit", &mcview_max_dirt_limit },
265 { "use_file_to_guess_type", &use_file_to_check_type },
266 { "alternate_plus_minus", &alternate_plus_minus },
267 { "only_leading_plus_minus", &only_leading_plus_minus },
268 { "show_output_starts_shell", &output_starts_shell },
269 { "xtree_mode", &xtree_mode },
270 { "num_history_items_recorded", &num_history_items_recorded },
271 { "file_op_compute_totals", &file_op_compute_totals },
272 { "classic_progressbar", &classic_progressbar},
273 #ifdef ENABLE_VFS
274 { "vfs_timeout", &vfs_timeout },
275 #ifdef ENABLE_VFS_FTP
276 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
277 { "use_netrc", &ftpfs_use_netrc },
278 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
279 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
280 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
281 { "ftpfs_use_passive_connections_over_proxy", &ftpfs_use_passive_connections_over_proxy },
282 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
283 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
284 #endif /* ENABLE_VFS_FTP */
285 #ifdef ENABLE_VFS_FISH
286 { "fish_directory_timeout", &fish_directory_timeout },
287 #endif /* ENABLE_VFS_FISH */
288 #endif /* ENABLE_VFS */
289 /* option_tab_spacing is used in internal viewer */
290 { "editor_tab_spacing", &option_tab_spacing },
291 #ifdef USE_INTERNAL_EDIT
292 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
293 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
294 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
295 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
296 { "editor_fake_half_tabs", &option_fake_half_tabs },
297 { "editor_option_save_mode", &option_save_mode },
298 { "editor_option_save_position", &option_save_position },
299 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
300 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
301 { "editor_edit_confirm_save", &edit_confirm_save },
302 { "editor_syntax_highlighting", &option_syntax_highlighting },
303 { "editor_persistent_selections", &option_persistent_selections },
304 { "editor_cursor_beyond_eol", &option_cursor_beyond_eol },
305 { "editor_visible_tabs", &visible_tabs },
306 { "editor_visible_spaces", &visible_tws },
307 { "editor_line_state", &option_line_state },
308 { "editor_simple_statusbar", &simple_statusbar },
309 { "editor_check_new_line", &option_check_nl_at_eof },
310 { "editor_show_right_margin", &show_right_margin },
311 { "editor_group_undo", &option_group_undo },
312 #endif /* USE_INTERNAL_EDIT */
313 { "nice_rotating_dash", &nice_rotating_dash },
314 { "horizontal_split", &horizontal_split },
315 { "mcview_remember_file_position", &mcview_remember_file_position },
316 { "auto_fill_mkdir_name", &auto_fill_mkdir_name },
317 { "copymove_persistent_attr", &setup_copymove_persistent_attr },
318 { "select_flags", &select_flags },
319 { NULL, NULL }
322 static const struct
324 const char *opt_name;
325 char **opt_addr;
326 const char *opt_defval;
327 } str_options[] = {
328 #ifdef USE_INTERNAL_EDIT
329 { "editor_backup_extension", &option_backup_ext, "~" },
330 #endif
331 { "mcview_eof", &mcview_show_eof, "" },
332 { NULL, NULL, NULL }
335 static const struct
337 const char *opt_name;
338 gboolean *opt_addr;
339 } panels_ini_options[] = {
340 { "show_mini_info", &panels_options.show_mini_info },
341 { "kilobyte_si", &panels_options.kilobyte_si },
342 { "mix_all_files", &panels_options.mix_all_files },
343 { "show_backups", &panels_options.show_backups },
344 { "show_dot_files", &panels_options.show_dot_files },
345 { "fast_reload", &panels_options.fast_reload },
346 { "fast_reload_msg_shown", &panels_options.fast_reload_msg_shown },
347 { "mark_moves_down", &panels_options.mark_moves_down },
348 { "reverse_files_only", &panels_options.reverse_files_only },
349 { "auto_save_setup_panels", &panels_options.auto_save_setup },
350 { "navigate_with_arrows", &panels_options.navigate_with_arrows },
351 { "panel_scroll_pages", &panels_options.scroll_pages },
352 { "mouse_move_pages", &panels_options.mouse_move_pages },
353 { "filetype_mode", &panels_options.filetype_mode },
354 { "permission_mode", &panels_options.permission_mode },
355 { "torben_fj_mode", &panels_options.torben_fj_mode },
356 { NULL, NULL }
358 /* *INDENT-ON* */
360 /*** file scope functions ************************************************************************/
361 /* --------------------------------------------------------------------------------------------- */
364 Get name of config file.
366 \param subdir
367 if not NULL, then config also search into specified subdir.
369 \param config_file_name
370 If specified filename is relative, then will search in standart patches.
372 \return
373 Newly allocated path to config name or NULL if file not found.
375 If config_file_name is a relative path, then search config in stantart paths.
377 static char *
378 load_setup_get_full_config_name (const char *subdir, const char *config_file_name)
381 TODO: IMHO, in future this function must be placed into mc_config module.
383 char *lc_basename, *ret;
385 if (config_file_name == NULL)
386 return NULL;
388 if (g_path_is_absolute (config_file_name))
389 return g_strdup (config_file_name);
392 lc_basename = g_path_get_basename (config_file_name);
393 if (lc_basename == NULL)
394 return NULL;
396 if (subdir != NULL)
397 ret = g_build_filename (mc_config_get_path (), subdir, lc_basename, NULL);
398 else
399 ret = g_build_filename (mc_config_get_path (), lc_basename, NULL);
401 if (exist_file (ret))
403 g_free (lc_basename);
404 return ret;
406 g_free (ret);
408 if (subdir != NULL)
409 ret = g_build_filename (mc_global.sysconfig_dir, subdir, lc_basename, NULL);
410 else
411 ret = g_build_filename (mc_global.sysconfig_dir, lc_basename, NULL);
413 if (exist_file (ret))
415 g_free (lc_basename);
416 return ret;
418 g_free (ret);
420 if (subdir != NULL)
421 ret = g_build_filename (mc_global.share_data_dir, subdir, lc_basename, NULL);
422 else
423 ret = g_build_filename (mc_global.share_data_dir, lc_basename, NULL);
425 g_free (lc_basename);
427 if (exist_file (ret))
428 return ret;
430 g_free (ret);
431 return NULL;
435 /* --------------------------------------------------------------------------------------------- */
437 static const char *
438 setup__is_cfg_group_must_panel_config (const char *grp)
440 return (!strcasecmp ("Dirs", grp) ||
441 !strcasecmp ("Temporal:New Right Panel", grp) ||
442 !strcasecmp ("Temporal:New Left Panel", grp) ||
443 !strcasecmp ("New Left Panel", grp) || !strcasecmp ("New Right Panel", grp))
444 ? grp : NULL;
447 /* --------------------------------------------------------------------------------------------- */
449 static void
450 setup__move_panels_config_into_separate_file (const char *profile)
452 mc_config_t *tmp_cfg;
453 char **groups, **curr_grp;
454 const char *need_grp;
456 if (!exist_file (profile))
457 return;
459 tmp_cfg = mc_config_init (profile);
460 if (!tmp_cfg)
461 return;
463 curr_grp = groups = mc_config_get_groups (tmp_cfg, NULL);
464 if (!groups)
466 mc_config_deinit (tmp_cfg);
467 return;
470 while (*curr_grp)
472 if (setup__is_cfg_group_must_panel_config (*curr_grp) == NULL)
473 mc_config_del_group (tmp_cfg, *curr_grp);
474 curr_grp++;
477 mc_config_save_to_file (tmp_cfg, panels_profile_name, NULL);
478 mc_config_deinit (tmp_cfg);
480 tmp_cfg = mc_config_init (profile);
481 if (!tmp_cfg)
483 g_strfreev (groups);
484 return;
487 curr_grp = groups;
489 while (*curr_grp)
491 need_grp = setup__is_cfg_group_must_panel_config (*curr_grp);
492 if (need_grp != NULL)
494 mc_config_del_group (tmp_cfg, need_grp);
496 curr_grp++;
498 g_strfreev (groups);
500 mc_config_save_file (tmp_cfg, NULL);
501 mc_config_deinit (tmp_cfg);
504 /* --------------------------------------------------------------------------------------------- */
506 Create new mc_config object from specified ini-file or
507 append data to existing mc_config object from ini-file
510 static void
511 load_setup_init_config_from_file (mc_config_t ** config, const char *fname)
514 TODO: IMHO, in future this function must be placed into mc_config module.
516 if (exist_file (fname))
518 if (*config != NULL)
519 mc_config_read_file (*config, fname, TRUE);
520 else
521 *config = mc_config_init (fname);
525 /* --------------------------------------------------------------------------------------------- */
527 static void
528 load_layout (void)
530 size_t i;
532 for (i = 0; layout[i].opt_name != NULL; i++)
533 *layout[i].opt_addr = mc_config_get_int (mc_main_config, "Layout",
534 layout[i].opt_name, *layout[i].opt_addr);
537 /* --------------------------------------------------------------------------------------------- */
539 static void
540 load_keys_from_section (const char *terminal, mc_config_t * cfg)
542 char *section_name;
543 gchar **profile_keys, **keys;
544 gchar **values, **curr_values;
545 char *valcopy, *value;
546 long key_code;
547 gsize len, values_len;
549 if (terminal == NULL)
550 return;
552 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
553 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
555 while (*profile_keys != NULL)
557 /* copy=other causes all keys from [terminal:other] to be loaded. */
558 if (g_ascii_strcasecmp (*profile_keys, "copy") == 0)
560 valcopy = mc_config_get_string (cfg, section_name, *profile_keys, "");
561 load_keys_from_section (valcopy, cfg);
562 g_free (valcopy);
563 profile_keys++;
564 continue;
567 curr_values = values =
568 mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
570 key_code = lookup_key (*profile_keys, NULL);
572 if (key_code != 0)
574 if (curr_values != NULL)
576 while (*curr_values != NULL)
578 valcopy = convert_controls (*curr_values);
579 define_sequence (key_code, valcopy, MCKEY_NOACTION);
580 g_free (valcopy);
581 curr_values++;
584 else
586 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
587 valcopy = convert_controls (value);
588 define_sequence (key_code, valcopy, MCKEY_NOACTION);
589 g_free (valcopy);
590 g_free (value);
594 profile_keys++;
595 g_strfreev (values);
597 g_strfreev (keys);
598 g_free (section_name);
601 /* --------------------------------------------------------------------------------------------- */
603 static void
604 load_keymap_from_section (const char *section_name, GArray * keymap, mc_config_t * cfg)
606 gchar **profile_keys, **keys;
607 gchar **values, **curr_values;
608 char *valcopy, *value;
609 int action;
610 gsize len, values_len;
612 if (section_name == NULL)
613 return;
615 profile_keys = keys = mc_config_get_keys (cfg, section_name, &len);
617 while (*profile_keys != NULL)
619 curr_values = values =
620 mc_config_get_string_list (cfg, section_name, *profile_keys, &values_len);
622 action = keybind_lookup_action (*profile_keys);
624 if (action > 0)
626 if (curr_values != NULL)
628 while (*curr_values != NULL)
630 valcopy = convert_controls (*curr_values);
631 keybind_cmd_bind (keymap, valcopy, action);
632 g_free (valcopy);
633 curr_values++;
636 else
638 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
639 valcopy = convert_controls (value);
640 /* define_sequence (key_code, valcopy, MCKEY_NOACTION); */
641 g_free (valcopy);
642 g_free (value);
646 profile_keys++;
647 g_strfreev (values);
649 g_strfreev (keys);
652 /* --------------------------------------------------------------------------------------------- */
654 static mc_config_t *
655 load_setup_get_keymap_profile_config (gboolean load_from_file)
658 TODO: IMHO, in future this function must be placed into mc_config module.
660 mc_config_t *keymap_config;
661 char *fname, *fname2;
663 /* 0) Create default keymap */
664 keymap_config = create_default_keymap ();
665 if (!load_from_file)
666 return keymap_config;
668 /* 1) /usr/share/mc (mc_global.share_data_dir) */
669 fname = g_build_filename (mc_global.share_data_dir, GLOBAL_KEYMAP_FILE, NULL);
670 load_setup_init_config_from_file (&keymap_config, fname);
671 g_free (fname);
673 /* 2) /etc/mc (mc_global.sysconfig_dir) */
674 fname = g_build_filename (mc_global.sysconfig_dir, GLOBAL_KEYMAP_FILE, NULL);
675 load_setup_init_config_from_file (&keymap_config, fname);
676 g_free (fname);
678 /* 3) ${XDG_CONFIG_HOME}/mc */
679 fname = g_build_filename (mc_config_get_path (), GLOBAL_KEYMAP_FILE, NULL);
680 load_setup_init_config_from_file (&keymap_config, fname);
681 g_free (fname);
683 /* 4) main config; [Midnight Commander] -> keymap */
684 fname2 =
685 mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, "keymap", GLOBAL_KEYMAP_FILE);
686 fname = load_setup_get_full_config_name (NULL, fname2);
687 if (fname != NULL)
689 load_setup_init_config_from_file (&keymap_config, fname);
690 g_free (fname);
692 g_free (fname2);
694 /* 5) getenv("MC_KEYMAP") */
695 fname = load_setup_get_full_config_name (NULL, g_getenv ("MC_KEYMAP"));
696 if (fname != NULL)
698 load_setup_init_config_from_file (&keymap_config, fname);
699 g_free (fname);
702 /* 6) --keymap=<keymap> */
703 fname = load_setup_get_full_config_name (NULL, mc_args__keymap_file);
704 if (fname != NULL)
706 load_setup_init_config_from_file (&keymap_config, fname);
707 g_free (fname);
710 return keymap_config;
713 /* --------------------------------------------------------------------------------------------- */
715 static panel_view_mode_t
716 setup__load_panel_state (const char *section)
718 char *buffer;
719 size_t i;
720 panel_view_mode_t mode = view_listing;
722 /* Load the display mode */
723 buffer = mc_config_get_string (mc_panels_config, section, "display", "listing");
725 for (i = 0; panel_types[i].opt_name != NULL; i++)
726 if (g_ascii_strcasecmp (panel_types[i].opt_name, buffer) == 0)
728 mode = panel_types[i].opt_type;
729 break;
732 g_free (buffer);
734 return mode;
737 /* --------------------------------------------------------------------------------------------- */
739 static void
740 panel_save_type (const char *section, panel_view_mode_t type)
742 size_t i;
744 for (i = 0; panel_types[i].opt_name != NULL; i++)
745 if (panel_types[i].opt_type == type)
747 mc_config_set_string (mc_panels_config, section, "display", panel_types[i].opt_name);
748 break;
752 /* --------------------------------------------------------------------------------------------- */
754 /* save panels.ini */
755 static void
756 save_panel_types (void)
758 panel_view_mode_t type;
760 if (mc_global.mc_run_mode != MC_RUN_FULL)
761 return;
763 type = get_display_type (0);
764 panel_save_type ("New Left Panel", type);
765 if (type == view_listing)
766 panel_save_setup (left_panel, left_panel->panel_name);
767 type = get_display_type (1);
768 panel_save_type ("New Right Panel", type);
769 if (type == view_listing)
770 panel_save_setup (right_panel, right_panel->panel_name);
772 mc_config_set_string (mc_panels_config, "Dirs", "other_dir", get_panel_dir_for (other_panel));
774 if (current_panel != NULL)
775 mc_config_set_string (mc_panels_config, "Dirs", "current_is_left",
776 get_current_index () == 0 ? "1" : "0");
778 if (mc_panels_config->ini_path == NULL)
779 mc_panels_config->ini_path = g_strdup (panels_profile_name);
781 mc_config_del_group (mc_panels_config, "Temporal:New Left Panel");
782 mc_config_del_group (mc_panels_config, "Temporal:New Right Panel");
784 mc_config_save_file (mc_panels_config, NULL);
787 /* --------------------------------------------------------------------------------------------- */
788 /*** public functions ****************************************************************************/
789 /* --------------------------------------------------------------------------------------------- */
791 char *
792 setup_init (void)
794 char *profile;
795 char *inifile;
797 if (profile_name != NULL)
798 return profile_name;
800 profile = g_build_filename (mc_config_get_path (), MC_CONFIG_FILE, NULL);
801 if (!exist_file (profile))
803 inifile = concat_dir_and_file (mc_global.sysconfig_dir, "mc.ini");
804 if (exist_file (inifile))
806 g_free (profile);
807 profile = inifile;
809 else
811 g_free (inifile);
812 inifile = concat_dir_and_file (mc_global.share_data_dir, "mc.ini");
813 if (exist_file (inifile))
815 g_free (profile);
816 profile = inifile;
818 else
819 g_free (inifile);
823 profile_name = profile;
825 return profile;
828 /* --------------------------------------------------------------------------------------------- */
830 void
831 load_setup (void)
833 char *profile;
834 size_t i;
835 char *buffer;
836 const char *kt;
838 #ifdef HAVE_CHARSET
839 load_codepages_list ();
840 #endif /* HAVE_CHARSET */
842 profile = setup_init ();
844 /* mc.lib is common for all users, but has priority lower than
845 ${XDG_CONFIG_HOME}/mc/ini. FIXME: it's only used for keys and treestore now */
846 global_profile_name =
847 g_build_filename (mc_global.sysconfig_dir, MC_GLOBAL_CONFIG_FILE, (char *) NULL);
848 if (!exist_file (global_profile_name))
850 g_free (global_profile_name);
851 global_profile_name =
852 g_build_filename (mc_global.share_data_dir, MC_GLOBAL_CONFIG_FILE, (char *) NULL);
855 panels_profile_name = g_build_filename (mc_config_get_cache_path (), MC_PANELS_FILE, NULL);
857 mc_main_config = mc_config_init (profile);
859 if (!exist_file (panels_profile_name))
860 setup__move_panels_config_into_separate_file (profile);
862 mc_panels_config = mc_config_init (panels_profile_name);
864 /* Load integer boolean options */
865 for (i = 0; int_options[i].opt_name != NULL; i++)
866 *int_options[i].opt_addr =
867 mc_config_get_int (mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
868 *int_options[i].opt_addr);
870 if (option_tab_spacing <= 0)
871 option_tab_spacing = DEFAULT_TAB_SPACING;
873 #ifdef USE_INTERNAL_EDIT
874 if (option_word_wrap_line_length <= 0)
875 option_word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
876 #endif /* USE_INTERNAL_EDIT */
878 /* overwrite old_esc_mode_timeout */
879 kt = getenv ("KEYBOARD_KEY_TIMEOUT_US");
880 if ((kt != NULL) && (kt[0] != '\0'))
881 old_esc_mode_timeout = atoi (kt);
883 /* Load string options */
884 for (i = 0; str_options[i].opt_name != NULL; i++)
885 *str_options[i].opt_addr =
886 mc_config_get_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name,
887 str_options[i].opt_defval);
889 load_layout ();
890 panels_load_options ();
891 load_panelize ();
893 startup_left_mode = setup__load_panel_state ("New Left Panel");
894 startup_right_mode = setup__load_panel_state ("New Right Panel");
896 /* At least one of the panels is a listing panel */
897 if (startup_left_mode != view_listing && startup_right_mode != view_listing)
898 startup_left_mode = view_listing;
900 if (mc_run_param1 == NULL)
902 buffer = mc_config_get_string (mc_panels_config, "Dirs", "other_dir", ".");
903 if (vfs_file_is_local (buffer))
904 mc_run_param1 = buffer;
905 else
906 g_free (buffer);
909 boot_current_is_left = mc_config_get_bool (mc_panels_config, "Dirs", "current_is_left", TRUE);
911 /* Load time formats */
912 user_recent_timeformat =
913 mc_config_get_string (mc_main_config, "Misc", "timeformat_recent", FMTTIME);
914 user_old_timeformat = mc_config_get_string (mc_main_config, "Misc", "timeformat_old", FMTYEAR);
916 #ifdef ENABLE_VFS_FTP
917 ftpfs_proxy_host = mc_config_get_string (mc_main_config, "Misc", "ftp_proxy_host", "gate");
918 ftpfs_ignore_chattr_errors = mc_config_get_bool (mc_main_config, CONFIG_APP_SECTION,
919 "ignore_ftp_chattr_errors", TRUE);
920 ftpfs_init_passwd ();
921 #endif /* ENABLE_VFS_FTP */
923 /* The default color and the terminal dependent color */
924 mc_global.tty.setup_color_string =
925 mc_config_get_string (mc_main_config, "Colors", "base_color", "");
926 mc_global.tty.term_color_string =
927 mc_config_get_string (mc_main_config, "Colors", getenv ("TERM"), "");
928 mc_global.tty.color_terminal_string =
929 mc_config_get_string (mc_main_config, "Colors", "color_terminals", "");
931 /* Load the directory history */
932 /* directory_history_load (); */
933 /* Remove the temporal entries */
935 #ifdef HAVE_CHARSET
936 if (codepages->len > 1)
938 buffer = mc_config_get_string (mc_main_config, "Misc", "display_codepage", "");
939 if (buffer[0] != '\0')
941 mc_global.display_codepage = get_codepage_index (buffer);
942 cp_display = get_codepage_id (mc_global.display_codepage);
944 g_free (buffer);
945 buffer = mc_config_get_string (mc_main_config, "Misc", "source_codepage", "");
946 if (buffer[0] != '\0')
948 default_source_codepage = get_codepage_index (buffer);
949 mc_global.source_codepage = default_source_codepage; /* May be source_codepage doesn't need this */
950 cp_source = get_codepage_id (mc_global.source_codepage);
952 g_free (buffer);
955 autodetect_codeset = mc_config_get_string (mc_main_config, "Misc", "autodetect_codeset", "");
956 if ((autodetect_codeset[0] != '\0') && (strcmp (autodetect_codeset, "off") != 0))
957 is_autodetect_codeset_enabled = TRUE;
959 g_free (init_translation_table (mc_global.source_codepage, mc_global.display_codepage));
960 buffer = (char *) get_codepage_id (mc_global.display_codepage);
961 if (buffer != NULL)
962 mc_global.utf8_display = str_isutf8 (buffer);
963 #endif /* HAVE_CHARSET */
965 clipboard_store_path = mc_config_get_string (mc_main_config, "Misc", "clipboard_store", "");
966 clipboard_paste_path = mc_config_get_string (mc_main_config, "Misc", "clipboard_paste", "");
969 /* --------------------------------------------------------------------------------------------- */
971 gboolean
972 save_setup (gboolean save_options, gboolean save_panel_options)
974 gboolean ret = TRUE;
976 saving_setup = 1;
978 save_hotlist ();
980 if (save_panel_options)
981 save_panel_types ();
983 if (save_options)
985 char *tmp_profile;
987 save_config ();
988 save_layout ();
989 panels_save_options ();
990 save_panelize ();
991 /* directory_history_save (); */
993 #ifdef ENABLE_VFS_FTP
994 mc_config_set_string (mc_main_config, "Misc", "ftpfs_password", ftpfs_anonymous_passwd);
995 if (ftpfs_proxy_host)
996 mc_config_set_string (mc_main_config, "Misc", "ftp_proxy_host", ftpfs_proxy_host);
997 #endif /* ENABLE_VFS_FTP */
999 #ifdef HAVE_CHARSET
1000 mc_config_set_string (mc_main_config, "Misc", "display_codepage",
1001 get_codepage_id (mc_global.display_codepage));
1002 mc_config_set_string (mc_main_config, "Misc", "source_codepage",
1003 get_codepage_id (default_source_codepage));
1004 mc_config_set_string (mc_main_config, "Misc", "autodetect_codeset", autodetect_codeset);
1005 #endif /* HAVE_CHARSET */
1006 mc_config_set_string (mc_main_config, "Misc", "clipboard_store", clipboard_store_path);
1007 mc_config_set_string (mc_main_config, "Misc", "clipboard_paste", clipboard_paste_path);
1009 tmp_profile = g_build_filename (mc_config_get_path (), MC_CONFIG_FILE, NULL);
1010 ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL);
1011 g_free (tmp_profile);
1014 saving_setup = 0;
1016 return ret;
1019 /* --------------------------------------------------------------------------------------------- */
1021 void
1022 done_setup (void)
1024 size_t i;
1026 g_free (clipboard_store_path);
1027 g_free (clipboard_paste_path);
1028 g_free (profile_name);
1029 g_free (global_profile_name);
1030 g_free (mc_global.tty.color_terminal_string);
1031 g_free (mc_global.tty.term_color_string);
1032 g_free (mc_global.tty.setup_color_string);
1033 g_free (panels_profile_name);
1034 mc_config_deinit (mc_main_config);
1035 mc_config_deinit (mc_panels_config);
1037 g_free (user_recent_timeformat);
1038 g_free (user_old_timeformat);
1040 for (i = 0; str_options[i].opt_name != NULL; i++)
1041 g_free (*str_options[i].opt_addr);
1043 done_hotlist ();
1044 done_panelize ();
1045 /* directory_history_free (); */
1047 #ifdef HAVE_CHARSET
1048 g_free (autodetect_codeset);
1049 free_codepages_list ();
1050 #endif
1053 /* --------------------------------------------------------------------------------------------- */
1055 void
1056 save_config (void)
1058 size_t i;
1060 /* Save integer options */
1061 for (i = 0; int_options[i].opt_name != NULL; i++)
1062 mc_config_set_int (mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
1063 *int_options[i].opt_addr);
1065 /* Save string options */
1066 for (i = 0; str_options[i].opt_name != NULL; i++)
1067 mc_config_set_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name,
1068 *str_options[i].opt_addr);
1071 /* --------------------------------------------------------------------------------------------- */
1073 void
1074 setup_save_config_show_error (const char *filename, GError ** error)
1076 if (error != NULL && *error != NULL)
1078 message (D_ERROR, MSG_ERROR, _("Cannot save file %s:\n%s"), filename, (*error)->message);
1079 g_error_free (*error);
1080 *error = NULL;
1084 /* --------------------------------------------------------------------------------------------- */
1086 void
1087 save_layout (void)
1089 size_t i;
1091 /* Save integer options */
1092 for (i = 0; layout[i].opt_name != NULL; i++)
1093 mc_config_set_int (mc_main_config, "Layout", layout[i].opt_name, *layout[i].opt_addr);
1096 /* --------------------------------------------------------------------------------------------- */
1098 void
1099 load_key_defs (void)
1102 * Load keys from mc.lib before ${XDG_CONFIG_HOME}/mc/ini, so that the user
1103 * definitions override global settings.
1105 mc_config_t *mc_global_config;
1107 mc_global_config = mc_config_init (global_profile_name);
1108 if (mc_global_config != NULL)
1110 load_keys_from_section ("general", mc_global_config);
1111 load_keys_from_section (getenv ("TERM"), mc_global_config);
1112 mc_config_deinit (mc_global_config);
1115 load_keys_from_section ("general", mc_main_config);
1116 load_keys_from_section (getenv ("TERM"), mc_main_config);
1119 /* --------------------------------------------------------------------------------------------- */
1121 #ifdef ENABLE_VFS_FTP
1122 char *
1123 load_anon_passwd (void)
1125 char *buffer;
1127 buffer = mc_config_get_string (mc_main_config, "Misc", "ftpfs_password", "");
1129 if ((buffer != NULL) && (buffer[0] != '\0'))
1130 return buffer;
1132 g_free (buffer);
1133 return NULL;
1135 #endif /* ENABLE_VFS_FTP */
1137 /* --------------------------------------------------------------------------------------------- */
1139 void
1140 load_keymap_defs (gboolean load_from_file)
1143 * Load keymap from GLOBAL_KEYMAP_FILE before ${XDG_DATA_HOME}/mc/keymap, so that the user
1144 * definitions override global settings.
1146 mc_config_t *mc_global_keymap;
1148 mc_global_keymap = load_setup_get_keymap_profile_config (load_from_file);
1150 if (mc_global_keymap != NULL)
1152 main_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1153 load_keymap_from_section (KEYMAP_SECTION_MAIN, main_keymap, mc_global_keymap);
1154 main_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1155 load_keymap_from_section (KEYMAP_SECTION_MAIN_EXT, main_x_keymap, mc_global_keymap);
1157 panel_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1158 load_keymap_from_section (KEYMAP_SECTION_PANEL, panel_keymap, mc_global_keymap);
1160 dialog_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1161 load_keymap_from_section (KEYMAP_SECTION_DIALOG, dialog_keymap, mc_global_keymap);
1163 input_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1164 load_keymap_from_section (KEYMAP_SECTION_INPUT, input_keymap, mc_global_keymap);
1166 listbox_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1167 load_keymap_from_section (KEYMAP_SECTION_LISTBOX, listbox_keymap, mc_global_keymap);
1169 tree_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1170 load_keymap_from_section (KEYMAP_SECTION_TREE, tree_keymap, mc_global_keymap);
1172 help_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1173 load_keymap_from_section (KEYMAP_SECTION_HELP, help_keymap, mc_global_keymap);
1175 #ifdef USE_INTERNAL_EDIT
1176 editor_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1177 load_keymap_from_section (KEYMAP_SECTION_EDITOR, editor_keymap, mc_global_keymap);
1178 editor_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1179 load_keymap_from_section (KEYMAP_SECTION_EDITOR_EXT, editor_x_keymap, mc_global_keymap);
1180 #endif
1182 viewer_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1183 load_keymap_from_section (KEYMAP_SECTION_VIEWER, viewer_keymap, mc_global_keymap);
1184 viewer_hex_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1185 load_keymap_from_section (KEYMAP_SECTION_VIEWER_HEX, viewer_hex_keymap, mc_global_keymap);
1187 #ifdef USE_DIFF_VIEW
1188 diff_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1189 load_keymap_from_section (KEYMAP_SECTION_DIFFVIEWER, diff_keymap, mc_global_keymap);
1190 #endif
1192 mc_config_deinit (mc_global_keymap);
1195 main_map = (global_keymap_t *) main_keymap->data;
1196 main_x_map = (global_keymap_t *) main_x_keymap->data;
1197 panel_map = (global_keymap_t *) panel_keymap->data;
1198 dialog_map = (global_keymap_t *) dialog_keymap->data;
1199 input_map = (global_keymap_t *) input_keymap->data;
1200 listbox_map = (global_keymap_t *) listbox_keymap->data;
1201 tree_map = (global_keymap_t *) tree_keymap->data;
1202 help_map = (global_keymap_t *) help_keymap->data;
1203 #ifdef USE_INTERNAL_EDIT
1204 editor_map = (global_keymap_t *) editor_keymap->data;
1205 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
1206 #endif
1207 viewer_map = (global_keymap_t *) viewer_keymap->data;
1208 viewer_hex_map = (global_keymap_t *) viewer_hex_keymap->data;
1209 #ifdef USE_DIFF_VIEW
1210 diff_map = (global_keymap_t *) diff_keymap->data;
1211 #endif
1214 /* --------------------------------------------------------------------------------------------- */
1216 void
1217 free_keymap_defs (void)
1219 if (main_keymap != NULL)
1220 g_array_free (main_keymap, TRUE);
1221 if (main_x_keymap != NULL)
1222 g_array_free (main_x_keymap, TRUE);
1223 if (panel_keymap != NULL)
1224 g_array_free (panel_keymap, TRUE);
1225 if (dialog_keymap != NULL)
1226 g_array_free (dialog_keymap, TRUE);
1227 if (input_keymap != NULL)
1228 g_array_free (input_keymap, TRUE);
1229 if (listbox_keymap != NULL)
1230 g_array_free (listbox_keymap, TRUE);
1231 if (tree_keymap != NULL)
1232 g_array_free (tree_keymap, TRUE);
1233 if (help_keymap != NULL)
1234 g_array_free (help_keymap, TRUE);
1235 #ifdef USE_INTERNAL_EDIT
1236 if (editor_keymap != NULL)
1237 g_array_free (editor_keymap, TRUE);
1238 if (editor_x_keymap != NULL)
1239 g_array_free (editor_x_keymap, TRUE);
1240 #endif
1241 if (viewer_keymap != NULL)
1242 g_array_free (viewer_keymap, TRUE);
1243 if (viewer_hex_keymap != NULL)
1244 g_array_free (viewer_hex_keymap, TRUE);
1245 #ifdef USE_DIFF_VIEW
1246 if (diff_keymap != NULL)
1247 g_array_free (diff_keymap, TRUE);
1248 #endif
1251 /* --------------------------------------------------------------------------------------------- */
1253 void
1254 panel_load_setup (WPanel * panel, const char *section)
1256 size_t i;
1257 char *buffer, buffer2[BUF_TINY];
1259 panel->sort_info.reverse = mc_config_get_int (mc_panels_config, section, "reverse", 0);
1260 panel->sort_info.case_sensitive =
1261 mc_config_get_int (mc_panels_config, section, "case_sensitive",
1262 OS_SORT_CASE_SENSITIVE_DEFAULT);
1263 panel->sort_info.exec_first = mc_config_get_int (mc_panels_config, section, "exec_first", 0);
1265 /* Load sort order */
1266 buffer = mc_config_get_string (mc_panels_config, section, "sort_order", "name");
1267 panel->sort_info.sort_field = panel_get_field_by_id (buffer);
1268 if (panel->sort_info.sort_field == NULL)
1269 panel->sort_info.sort_field = panel_get_field_by_id ("name");
1271 g_free (buffer);
1273 /* Load the listing mode */
1274 buffer = mc_config_get_string (mc_panels_config, section, "list_mode", "full");
1275 panel->list_type = list_full;
1276 for (i = 0; list_types[i].key != NULL; i++)
1277 if (g_ascii_strcasecmp (list_types[i].key, buffer) == 0)
1279 panel->list_type = list_types[i].list_type;
1280 break;
1282 g_free (buffer);
1284 /* User formats */
1285 g_free (panel->user_format);
1286 panel->user_format =
1287 mc_config_get_string (mc_panels_config, section, "user_format", DEFAULT_USER_FORMAT);
1289 for (i = 0; i < LIST_TYPES; i++)
1291 g_free (panel->user_status_format[i]);
1292 g_snprintf (buffer2, BUF_TINY, "user_status%lld", (long long) i);
1293 panel->user_status_format[i] =
1294 mc_config_get_string (mc_panels_config, section, buffer2, DEFAULT_USER_FORMAT);
1297 panel->user_mini_status = mc_config_get_int (mc_panels_config, section, "user_mini_status", 0);
1300 /* --------------------------------------------------------------------------------------------- */
1302 void
1303 panel_save_setup (struct WPanel *panel, const char *section)
1305 char buffer[BUF_TINY];
1306 size_t i;
1308 mc_config_set_int (mc_panels_config, section, "reverse", panel->sort_info.reverse);
1309 mc_config_set_int (mc_panels_config, section, "case_sensitive",
1310 panel->sort_info.case_sensitive);
1311 mc_config_set_int (mc_panels_config, section, "exec_first", panel->sort_info.exec_first);
1313 mc_config_set_string (mc_panels_config, section, "sort_order", panel->sort_info.sort_field->id);
1315 for (i = 0; list_types[i].key != NULL; i++)
1316 if (list_types[i].list_type == panel->list_type)
1318 mc_config_set_string (mc_panels_config, section, "list_mode", list_types[i].key);
1319 break;
1322 mc_config_set_string (mc_panels_config, section, "user_format", panel->user_format);
1324 for (i = 0; i < LIST_TYPES; i++)
1326 g_snprintf (buffer, BUF_TINY, "user_status%lld", (long long) i);
1327 mc_config_set_string (mc_panels_config, section, buffer, panel->user_status_format[i]);
1330 mc_config_set_int (mc_panels_config, section, "user_mini_status", panel->user_mini_status);
1333 /* --------------------------------------------------------------------------------------------- */
1336 Load panels options from [Panels] section.
1338 void
1339 panels_load_options (void)
1341 if (mc_config_has_group (mc_main_config, CONFIG_PANELS_SECTION))
1343 size_t i;
1344 int qmode;
1346 for (i = 0; panels_ini_options[i].opt_name != NULL; i++)
1347 *panels_ini_options[i].opt_addr =
1348 mc_config_get_bool (mc_main_config, CONFIG_PANELS_SECTION,
1349 panels_ini_options[i].opt_name,
1350 *panels_ini_options[i].opt_addr);
1352 qmode = mc_config_get_int (mc_main_config, CONFIG_PANELS_SECTION,
1353 "quick_search_mode", (int) panels_options.qsearch_mode);
1354 if (qmode < 0)
1355 panels_options.qsearch_mode = QSEARCH_CASE_INSENSITIVE;
1356 else if (qmode >= QSEARCH_NUM)
1357 panels_options.qsearch_mode = QSEARCH_PANEL_CASE;
1358 else
1359 panels_options.qsearch_mode = (qsearch_mode_t) qmode;
1363 /* --------------------------------------------------------------------------------------------- */
1366 Save panels options in [Panels] section.
1368 void
1369 panels_save_options (void)
1371 size_t i;
1373 for (i = 0; panels_ini_options[i].opt_name != NULL; i++)
1374 mc_config_set_bool (mc_main_config, CONFIG_PANELS_SECTION,
1375 panels_ini_options[i].opt_name, *panels_ini_options[i].opt_addr);
1377 mc_config_set_int (mc_main_config, CONFIG_PANELS_SECTION,
1378 "quick_search_mode", (int) panels_options.qsearch_mode);
1381 /* --------------------------------------------------------------------------------------------- */