tar: avoid strtoul().
[midnight-commander.git] / src / setup.c
blobeb729c603bec2723c48691cac398a7ce19035cc8
1 /*
2 Setup loading/saving.
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
7 This file is part of the Midnight Commander.
9 The Midnight Commander is free software: you can redistribute it
10 and/or modify it under the terms of the GNU General Public License as
11 published by the Free Software Foundation, either version 3 of the License,
12 or (at your option) any later version.
14 The Midnight Commander is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program. If not, see <http://www.gnu.org/licenses/>.
23 /** \file setup.c
24 * \brief Source: setup loading/saving
27 #include <config.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <stdio.h>
32 #include <sys/types.h>
33 #include <sys/stat.h>
35 #include "lib/global.h"
37 #include "lib/tty/tty.h"
38 #include "lib/tty/key.h"
39 #include "lib/mcconfig.h" /* num_history_items_recorded */
40 #include "lib/fileloc.h"
41 #include "lib/timefmt.h"
42 #include "lib/util.h"
44 #ifdef ENABLE_VFS_FTP
45 #include "src/vfs/ftpfs/ftpfs.h"
46 #endif
47 #ifdef ENABLE_VFS_SHELL
48 #include "src/vfs/shell/shell.h"
49 #endif
51 #ifdef HAVE_CHARSET
52 #include "lib/charsets.h"
53 #endif
55 #include "filemanager/dir.h"
56 #include "filemanager/filemanager.h"
57 #include "filemanager/tree.h" /* xtree_mode */
58 #include "filemanager/hotlist.h" /* load/save/done hotlist */
59 #include "filemanager/panelize.h" /* load/save/done panelize */
60 #include "filemanager/layout.h"
61 #include "filemanager/cmd.h"
63 #include "args.h"
64 #include "execute.h" /* pause_after_run */
65 #include "clipboard.h"
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 /* Only used at program boot */
82 gboolean boot_current_is_left = TRUE;
84 /* If on, default for "No" in delete operations */
85 gboolean safe_delete = FALSE;
86 /* If on, default for "No" in overwrite files */
87 gboolean safe_overwrite = FALSE;
89 /* Controls screen clearing before an exec */
90 gboolean clear_before_exec = TRUE;
92 /* Asks for confirmation before deleting a file */
93 gboolean confirm_delete = TRUE;
94 /* Asks for confirmation before deleting a hotlist entry */
95 gboolean confirm_directory_hotlist_delete = FALSE;
96 /* Asks for confirmation before overwriting a file */
97 gboolean confirm_overwrite = TRUE;
98 /* Asks for confirmation before executing a program by pressing enter */
99 gboolean confirm_execute = FALSE;
100 /* Asks for confirmation before leaving the program */
101 gboolean confirm_exit = FALSE;
103 /* If true, at startup the user-menu is invoked */
104 gboolean auto_menu = FALSE;
105 /* This flag indicates if the pull down menus by default drop down */
106 gboolean drop_menus = FALSE;
108 /* Asks for confirmation when using F3 to view a directory and there
109 are tagged files */
110 gboolean confirm_view_dir = FALSE;
112 /* Ask file name before start the editor */
113 gboolean editor_ask_filename_before_edit = FALSE;
115 panel_view_mode_t startup_left_mode;
116 panel_view_mode_t startup_right_mode;
118 gboolean copymove_persistent_attr = TRUE;
120 /* Tab size */
121 int option_tab_spacing = DEFAULT_TAB_SPACING;
123 /* Ugly hack to allow panel_save_setup to work as a place holder for */
124 /* default panel values */
125 int saving_setup;
127 panels_options_t panels_options = {
128 .show_mini_info = TRUE,
129 .kilobyte_si = FALSE,
130 .mix_all_files = FALSE,
131 .show_backups = TRUE,
132 .show_dot_files = TRUE,
133 .fast_reload = FALSE,
134 .fast_reload_msg_shown = FALSE,
135 .mark_moves_down = TRUE,
136 .reverse_files_only = TRUE,
137 .auto_save_setup = FALSE,
138 .navigate_with_arrows = FALSE,
139 .scroll_pages = TRUE,
140 .scroll_center = FALSE,
141 .mouse_move_pages = TRUE,
142 .filetype_mode = TRUE,
143 .permission_mode = FALSE,
144 .qsearch_mode = QSEARCH_PANEL_CASE,
145 .torben_fj_mode = FALSE,
146 .select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS
149 gboolean easy_patterns = TRUE;
151 /* It true saves the setup when quitting */
152 gboolean auto_save_setup = TRUE;
154 /* If true, then the +, - and \ keys have their special meaning only if the
155 * command line is empty, otherwise they behave like regular letters
157 gboolean only_leading_plus_minus = TRUE;
159 /* Automatically fills name with current selected item name on mkdir */
160 gboolean auto_fill_mkdir_name = TRUE;
162 /* If set and you don't have subshell support, then C-o will give you a shell */
163 gboolean output_starts_shell = FALSE;
165 #ifdef USE_FILE_CMD
166 /* If set, we execute the file command to check the file type */
167 gboolean use_file_to_check_type = TRUE;
168 #endif
170 gboolean verbose = TRUE;
173 * Whether the Midnight Commander tries to provide more
174 * information about copy/move sizes and bytes transferred
175 * at the expense of some speed
177 gboolean file_op_compute_totals = TRUE;
179 /* If true use the internal viewer */
180 gboolean use_internal_view = TRUE;
181 /* If set, use the builtin editor */
182 gboolean use_internal_edit = TRUE;
184 #ifdef HAVE_CHARSET
185 /* Numbers of (file I/O) and (input/display) codepages. -1 if not selected */
186 int default_source_codepage = -1;
187 char *autodetect_codeset = NULL;
188 gboolean is_autodetect_codeset_enabled = FALSE;
189 #endif /* !HAVE_CHARSET */
191 #ifdef HAVE_ASPELL
192 char *spell_language = NULL;
193 #endif
195 /* Value of "other_dir" key in ini file */
196 char *saved_other_dir = NULL;
198 /* If set, then print to the given file the last directory we were at */
199 char *last_wd_string = NULL;
201 /* Set when main loop should be terminated */
202 int quit = 0;
204 /* Set to TRUE to suppress printing the last directory */
205 int print_last_revert = FALSE;
207 #ifdef USE_INTERNAL_EDIT
208 /* index to record_macro_buf[], -1 if not recording a macro */
209 int macro_index = -1;
211 /* macro stuff */
212 struct macro_action_t record_macro_buf[MAX_MACRO_LENGTH];
214 GArray *macros_list;
215 #endif /* USE_INTERNAL_EDIT */
217 /*** file scope macro definitions ****************************************************************/
219 /* In order to use everywhere the same setup for the locale we use defines */
220 #define FMTYEAR _("%b %e %Y")
221 #define FMTTIME _("%b %e %H:%M")
223 /*** file scope type declarations ****************************************************************/
225 /*** forward declarations (file scope functions) *************************************************/
227 /*** file scope variables ************************************************************************/
229 static char *profile_name = NULL; /* ${XDG_CONFIG_HOME}/mc/ini */
230 static char *panels_profile_name = NULL; /* ${XDG_CONFIG_HOME}/mc/panels.ini */
232 /* *INDENT-OFF* */
233 static const struct
235 const char *key;
236 int list_format;
237 } list_formats [] = {
238 { "full", list_full },
239 { "brief", list_brief },
240 { "long", list_long },
241 { "user", list_user },
242 { NULL, 0 }
245 static const struct
247 const char *opt_name;
248 panel_view_mode_t opt_type;
249 } panel_types [] = {
250 { "listing", view_listing },
251 { "quickview", view_quick },
252 { "info", view_info },
253 { "tree", view_tree },
254 { NULL, view_listing }
257 static const struct
259 const char *opt_name;
260 int *opt_addr;
261 } layout_int_options [] = {
262 { "output_lines", &output_lines },
263 { "left_panel_size", &panels_layout.left_panel_size },
264 { "top_panel_size", &panels_layout.top_panel_size },
265 { NULL, NULL }
268 static const struct
270 const char *opt_name;
271 gboolean *opt_addr;
272 } layout_bool_options [] = {
273 { "message_visible", &mc_global.message_visible },
274 { "keybar_visible", &mc_global.keybar_visible },
275 { "xterm_title", &xterm_title },
276 { "command_prompt", &command_prompt },
277 { "menubar_visible", &menubar_visible },
278 { "free_space", &free_space },
279 { "horizontal_split", &panels_layout.horizontal_split },
280 { "vertical_equal", &panels_layout.vertical_equal },
281 { "horizontal_equal", &panels_layout.horizontal_equal },
282 { NULL, NULL }
285 static const struct
287 const char *opt_name;
288 gboolean *opt_addr;
289 } bool_options [] = {
290 { "verbose", &verbose },
291 { "shell_patterns", &easy_patterns },
292 { "auto_save_setup", &auto_save_setup },
293 { "preallocate_space", &mc_global.vfs.preallocate_space },
294 { "auto_menu", &auto_menu },
295 { "use_internal_view", &use_internal_view },
296 { "use_internal_edit", &use_internal_edit },
297 { "clear_before_exec", &clear_before_exec },
298 { "confirm_delete", &confirm_delete },
299 { "confirm_overwrite", &confirm_overwrite },
300 { "confirm_execute", &confirm_execute },
301 { "confirm_history_cleanup", &mc_global.widget.confirm_history_cleanup },
302 { "confirm_exit", &confirm_exit },
303 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
304 { "confirm_view_dir", &confirm_view_dir },
305 { "safe_delete", &safe_delete },
306 { "safe_overwrite", &safe_overwrite },
307 #ifndef HAVE_CHARSET
308 { "eight_bit_clean", &mc_global.eight_bit_clean },
309 { "full_eight_bits", &mc_global.full_eight_bits },
310 #endif /* !HAVE_CHARSET */
311 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
312 { "mouse_move_pages_viewer", &mcview_mouse_move_pages },
313 { "mouse_close_dialog", &mouse_close_dialog},
314 { "fast_refresh", &fast_refresh },
315 { "drop_menus", &drop_menus },
316 { "wrap_mode", &mcview_global_flags.wrap },
317 { "old_esc_mode", &old_esc_mode },
318 { "cd_symlinks", &mc_global.vfs.cd_symlinks },
319 { "show_all_if_ambiguous", &mc_global.widget.show_all_if_ambiguous },
320 #ifdef USE_FILE_CMD
321 { "use_file_to_guess_type", &use_file_to_check_type },
322 #endif
323 { "alternate_plus_minus", &mc_global.tty.alternate_plus_minus },
324 { "only_leading_plus_minus", &only_leading_plus_minus },
325 { "show_output_starts_shell", &output_starts_shell },
326 { "xtree_mode", &xtree_mode },
327 { "file_op_compute_totals", &file_op_compute_totals },
328 { "classic_progressbar", &classic_progressbar },
329 #ifdef ENABLE_VFS
330 #ifdef ENABLE_VFS_FTP
331 { "use_netrc", &ftpfs_use_netrc },
332 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
333 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
334 { "ftpfs_use_passive_connections_over_proxy", &ftpfs_use_passive_connections_over_proxy },
335 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
336 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
337 { "ignore_ftp_chattr_errors", & ftpfs_ignore_chattr_errors} ,
338 #endif /* ENABLE_VFS_FTP */
339 #endif /* ENABLE_VFS */
340 #ifdef USE_INTERNAL_EDIT
341 { "editor_fill_tabs_with_spaces", &edit_options.fill_tabs_with_spaces },
342 { "editor_return_does_auto_indent", &edit_options.return_does_auto_indent },
343 { "editor_backspace_through_tabs", &edit_options.backspace_through_tabs },
344 { "editor_fake_half_tabs", &edit_options.fake_half_tabs },
345 { "editor_option_save_position", &edit_options.save_position },
346 { "editor_option_auto_para_formatting", &edit_options.auto_para_formatting },
347 { "editor_option_typewriter_wrap", &edit_options.typewriter_wrap },
348 { "editor_edit_confirm_save", &edit_options.confirm_save },
349 { "editor_syntax_highlighting", &edit_options.syntax_highlighting },
350 { "editor_persistent_selections", &edit_options.persistent_selections },
351 { "editor_drop_selection_on_copy", &edit_options.drop_selection_on_copy },
352 { "editor_cursor_beyond_eol", &edit_options.cursor_beyond_eol },
353 { "editor_cursor_after_inserted_block", &edit_options.cursor_after_inserted_block },
354 { "editor_visible_tabs", &edit_options.visible_tabs },
355 { "editor_visible_spaces", &edit_options.visible_tws },
356 { "editor_line_state", &edit_options.line_state },
357 { "editor_simple_statusbar", &edit_options.simple_statusbar },
358 { "editor_check_new_line", &edit_options.check_nl_at_eof },
359 { "editor_show_right_margin", &edit_options.show_right_margin },
360 { "editor_group_undo", &edit_options.group_undo },
361 { "editor_state_full_filename", &edit_options.state_full_filename },
362 #endif /* USE_INTERNAL_EDIT */
363 { "editor_ask_filename_before_edit", &editor_ask_filename_before_edit },
364 { "nice_rotating_dash", &nice_rotating_dash },
365 { "shadows", &mc_global.tty.shadows },
366 { "mcview_remember_file_position", &mcview_remember_file_position },
367 { "auto_fill_mkdir_name", &auto_fill_mkdir_name },
368 { "copymove_persistent_attr", &copymove_persistent_attr },
369 { NULL, NULL }
372 static const struct
374 const char *opt_name;
375 int *opt_addr;
376 } int_options [] = {
377 { "pause_after_run", &pause_after_run },
378 { "mouse_repeat_rate", &mou_auto_repeat },
379 { "double_click_speed", &double_click_speed },
380 { "old_esc_mode_timeout", &old_esc_mode_timeout },
381 { "max_dirt_limit", &mcview_max_dirt_limit },
382 { "num_history_items_recorded", &num_history_items_recorded },
383 #ifdef ENABLE_VFS
384 { "vfs_timeout", &vfs_timeout },
385 #ifdef ENABLE_VFS_FTP
386 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
387 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
388 #endif /* ENABLE_VFS_FTP */
389 #ifdef ENABLE_VFS_SHELL
390 { "shell_directory_timeout", &shell_directory_timeout },
391 #endif /* ENABLE_VFS_SHELL */
392 #endif /* ENABLE_VFS */
393 /* option_tab_spacing is used in internal viewer */
394 { "editor_tab_spacing", &option_tab_spacing },
395 #ifdef USE_INTERNAL_EDIT
396 { "editor_word_wrap_line_length", &edit_options.word_wrap_line_length },
397 { "editor_option_save_mode", &edit_options.save_mode },
398 #endif /* USE_INTERNAL_EDIT */
399 { NULL, NULL }
402 static const struct
404 const char *opt_name;
405 char **opt_addr;
406 const char *opt_defval;
407 } str_options[] = {
408 #ifdef USE_INTERNAL_EDIT
409 { "editor_backup_extension", &edit_options.backup_ext, "~" },
410 { "editor_filesize_threshold", &edit_options.filesize_threshold, "64M" },
411 { "editor_stop_format_chars", &edit_options.stop_format_chars, "-+*\\,.;:&>" },
412 #endif
413 { "mcview_eof", &mcview_show_eof, "" },
414 { NULL, NULL, NULL }
417 static const struct
419 const char *opt_name;
420 gboolean *opt_addr;
421 } panels_ini_options[] = {
422 { "show_mini_info", &panels_options.show_mini_info },
423 { "kilobyte_si", &panels_options.kilobyte_si },
424 { "mix_all_files", &panels_options.mix_all_files },
425 { "show_backups", &panels_options.show_backups },
426 { "show_dot_files", &panels_options.show_dot_files },
427 { "fast_reload", &panels_options.fast_reload },
428 { "fast_reload_msg_shown", &panels_options.fast_reload_msg_shown },
429 { "mark_moves_down", &panels_options.mark_moves_down },
430 { "reverse_files_only", &panels_options.reverse_files_only },
431 { "auto_save_setup_panels", &panels_options.auto_save_setup },
432 { "navigate_with_arrows", &panels_options.navigate_with_arrows },
433 { "panel_scroll_pages", &panels_options.scroll_pages },
434 { "panel_scroll_center", &panels_options.scroll_center },
435 { "mouse_move_pages", &panels_options.mouse_move_pages },
436 { "filetype_mode", &panels_options.filetype_mode },
437 { "permission_mode", &panels_options.permission_mode },
438 { "torben_fj_mode", &panels_options.torben_fj_mode },
439 { NULL, NULL }
441 /* *INDENT-ON* */
443 /* --------------------------------------------------------------------------------------------- */
444 /*** file scope functions ************************************************************************/
445 /* --------------------------------------------------------------------------------------------- */
447 static const char *
448 setup__is_cfg_group_must_panel_config (const char *grp)
450 return (!strcasecmp ("Dirs", grp) ||
451 !strcasecmp ("Temporal:New Right Panel", grp) ||
452 !strcasecmp ("Temporal:New Left Panel", grp) ||
453 !strcasecmp ("New Left Panel", grp) || !strcasecmp ("New Right Panel", grp))
454 ? grp : NULL;
457 /* --------------------------------------------------------------------------------------------- */
459 static void
460 setup__move_panels_config_into_separate_file (const char *profile)
462 mc_config_t *tmp_cfg;
463 char **groups, **curr_grp;
465 if (!exist_file (profile))
466 return;
468 tmp_cfg = mc_config_init (profile, FALSE);
469 if (tmp_cfg == NULL)
470 return;
472 groups = mc_config_get_groups (tmp_cfg, NULL);
473 if (*groups == NULL)
475 g_strfreev (groups);
476 mc_config_deinit (tmp_cfg);
477 return;
480 for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
481 if (setup__is_cfg_group_must_panel_config (*curr_grp) == NULL)
482 mc_config_del_group (tmp_cfg, *curr_grp);
484 mc_config_save_to_file (tmp_cfg, panels_profile_name, NULL);
485 mc_config_deinit (tmp_cfg);
487 tmp_cfg = mc_config_init (profile, FALSE);
488 if (tmp_cfg == NULL)
490 g_strfreev (groups);
491 return;
494 for (curr_grp = groups; *curr_grp != NULL; curr_grp++)
496 const char *need_grp;
498 need_grp = setup__is_cfg_group_must_panel_config (*curr_grp);
499 if (need_grp != NULL)
500 mc_config_del_group (tmp_cfg, need_grp);
502 g_strfreev (groups);
504 mc_config_save_file (tmp_cfg, NULL);
505 mc_config_deinit (tmp_cfg);
508 /* --------------------------------------------------------------------------------------------- */
510 static void
511 load_config (void)
513 size_t i;
514 const char *kt;
516 /* Load boolean options */
517 for (i = 0; bool_options[i].opt_name != NULL; i++)
518 *bool_options[i].opt_addr =
519 mc_config_get_bool (mc_global.main_config, CONFIG_APP_SECTION, bool_options[i].opt_name,
520 *bool_options[i].opt_addr);
522 /* Load integer options */
523 for (i = 0; int_options[i].opt_name != NULL; i++)
524 *int_options[i].opt_addr =
525 mc_config_get_int (mc_global.main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
526 *int_options[i].opt_addr);
528 /* Load string options */
529 for (i = 0; str_options[i].opt_name != NULL; i++)
530 *str_options[i].opt_addr =
531 mc_config_get_string (mc_global.main_config, CONFIG_APP_SECTION,
532 str_options[i].opt_name, str_options[i].opt_defval);
534 /* Overwrite some options */
535 #ifdef USE_INTERNAL_EDIT
536 if (edit_options.word_wrap_line_length <= 0)
537 edit_options.word_wrap_line_length = DEFAULT_WRAP_LINE_LENGTH;
538 #else
539 /* Reset forced in case of build without internal editor */
540 use_internal_edit = FALSE;
541 #endif /* USE_INTERNAL_EDIT */
543 if (option_tab_spacing <= 0)
544 option_tab_spacing = DEFAULT_TAB_SPACING;
546 kt = getenv ("KEYBOARD_KEY_TIMEOUT_US");
547 if (kt != NULL && kt[0] != '\0')
548 old_esc_mode_timeout = atoi (kt);
551 /* --------------------------------------------------------------------------------------------- */
553 static panel_view_mode_t
554 setup__load_panel_state (const char *section)
556 char *buffer;
557 size_t i;
558 panel_view_mode_t mode = view_listing;
560 /* Load the display mode */
561 buffer = mc_config_get_string (mc_global.panels_config, section, "display", "listing");
563 for (i = 0; panel_types[i].opt_name != NULL; i++)
564 if (g_ascii_strcasecmp (panel_types[i].opt_name, buffer) == 0)
566 mode = panel_types[i].opt_type;
567 break;
570 g_free (buffer);
572 return mode;
575 /* --------------------------------------------------------------------------------------------- */
577 static void
578 load_layout (void)
580 size_t i;
582 /* actual options override legacy ones */
583 for (i = 0; layout_int_options[i].opt_name != NULL; i++)
584 *layout_int_options[i].opt_addr =
585 mc_config_get_int (mc_global.main_config, CONFIG_LAYOUT_SECTION,
586 layout_int_options[i].opt_name, *layout_int_options[i].opt_addr);
588 for (i = 0; layout_bool_options[i].opt_name != NULL; i++)
589 *layout_bool_options[i].opt_addr =
590 mc_config_get_bool (mc_global.main_config, CONFIG_LAYOUT_SECTION,
591 layout_bool_options[i].opt_name, *layout_bool_options[i].opt_addr);
593 startup_left_mode = setup__load_panel_state ("New Left Panel");
594 startup_right_mode = setup__load_panel_state ("New Right Panel");
596 /* At least one of the panels is a listing panel */
597 if (startup_left_mode != view_listing && startup_right_mode != view_listing)
598 startup_left_mode = view_listing;
600 boot_current_is_left =
601 mc_config_get_bool (mc_global.panels_config, "Dirs", "current_is_left", TRUE);
604 /* --------------------------------------------------------------------------------------------- */
606 static void
607 load_keys_from_section (const char *terminal, mc_config_t *cfg)
609 char *section_name;
610 gchar **profile_keys, **keys;
611 char *valcopy, *value;
612 long key_code;
614 if (terminal == NULL)
615 return;
617 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
618 keys = mc_config_get_keys (cfg, section_name, NULL);
620 for (profile_keys = keys; *profile_keys != NULL; profile_keys++)
622 /* copy=other causes all keys from [terminal:other] to be loaded. */
623 if (g_ascii_strcasecmp (*profile_keys, "copy") == 0)
625 valcopy = mc_config_get_string (cfg, section_name, *profile_keys, "");
626 load_keys_from_section (valcopy, cfg);
627 g_free (valcopy);
628 continue;
631 key_code = tty_keyname_to_keycode (*profile_keys, NULL);
632 if (key_code != 0)
634 gchar **values;
636 values = mc_config_get_string_list (cfg, section_name, *profile_keys, NULL);
637 if (values != NULL)
639 gchar **curr_values;
641 for (curr_values = values; *curr_values != NULL; curr_values++)
643 valcopy = convert_controls (*curr_values);
644 define_sequence (key_code, valcopy, MCKEY_NOACTION);
645 g_free (valcopy);
648 g_strfreev (values);
650 else
652 value = mc_config_get_string (cfg, section_name, *profile_keys, "");
653 valcopy = convert_controls (value);
654 define_sequence (key_code, valcopy, MCKEY_NOACTION);
655 g_free (valcopy);
656 g_free (value);
660 g_strfreev (keys);
661 g_free (section_name);
664 /* --------------------------------------------------------------------------------------------- */
666 static void
667 panel_save_type (const char *section, panel_view_mode_t type)
669 size_t i;
671 for (i = 0; panel_types[i].opt_name != NULL; i++)
672 if (panel_types[i].opt_type == type)
674 mc_config_set_string (mc_global.panels_config, section, "display",
675 panel_types[i].opt_name);
676 break;
680 /* --------------------------------------------------------------------------------------------- */
683 * Load panels options from [Panels] section.
685 static void
686 panels_load_options (void)
688 if (mc_config_has_group (mc_global.main_config, CONFIG_PANELS_SECTION))
690 size_t i;
691 int qmode;
693 for (i = 0; panels_ini_options[i].opt_name != NULL; i++)
694 *panels_ini_options[i].opt_addr =
695 mc_config_get_bool (mc_global.main_config, CONFIG_PANELS_SECTION,
696 panels_ini_options[i].opt_name,
697 *panels_ini_options[i].opt_addr);
699 qmode = mc_config_get_int (mc_global.main_config, CONFIG_PANELS_SECTION,
700 "quick_search_mode", (int) panels_options.qsearch_mode);
701 if (qmode < 0)
702 panels_options.qsearch_mode = QSEARCH_CASE_INSENSITIVE;
703 else if (qmode >= QSEARCH_NUM)
704 panels_options.qsearch_mode = QSEARCH_PANEL_CASE;
705 else
706 panels_options.qsearch_mode = (qsearch_mode_t) qmode;
708 panels_options.select_flags =
709 mc_config_get_int (mc_global.main_config, CONFIG_PANELS_SECTION, "select_flags",
710 (int) panels_options.select_flags);
714 /* --------------------------------------------------------------------------------------------- */
717 * Save panels options in [Panels] section.
719 static void
720 panels_save_options (void)
722 size_t i;
724 for (i = 0; panels_ini_options[i].opt_name != NULL; i++)
725 mc_config_set_bool (mc_global.main_config, CONFIG_PANELS_SECTION,
726 panels_ini_options[i].opt_name, *panels_ini_options[i].opt_addr);
728 mc_config_set_int (mc_global.main_config, CONFIG_PANELS_SECTION,
729 "quick_search_mode", (int) panels_options.qsearch_mode);
730 mc_config_set_int (mc_global.main_config, CONFIG_PANELS_SECTION,
731 "select_flags", (int) panels_options.select_flags);
734 /* --------------------------------------------------------------------------------------------- */
736 static void
737 save_config (void)
739 size_t i;
741 /* Save boolean options */
742 for (i = 0; bool_options[i].opt_name != NULL; i++)
743 mc_config_set_bool (mc_global.main_config, CONFIG_APP_SECTION, bool_options[i].opt_name,
744 *bool_options[i].opt_addr);
746 /* Save integer options */
747 for (i = 0; int_options[i].opt_name != NULL; i++)
748 mc_config_set_int (mc_global.main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
749 *int_options[i].opt_addr);
751 /* Save string options */
752 for (i = 0; str_options[i].opt_name != NULL; i++)
753 mc_config_set_string (mc_global.main_config, CONFIG_APP_SECTION, str_options[i].opt_name,
754 *str_options[i].opt_addr);
757 /* --------------------------------------------------------------------------------------------- */
759 static void
760 save_layout (void)
762 size_t i;
764 /* Save integer options */
765 for (i = 0; layout_int_options[i].opt_name != NULL; i++)
766 mc_config_set_int (mc_global.main_config, CONFIG_LAYOUT_SECTION,
767 layout_int_options[i].opt_name, *layout_int_options[i].opt_addr);
769 /* Save boolean options */
770 for (i = 0; layout_bool_options[i].opt_name != NULL; i++)
771 mc_config_set_bool (mc_global.main_config, CONFIG_LAYOUT_SECTION,
772 layout_bool_options[i].opt_name, *layout_bool_options[i].opt_addr);
775 /* --------------------------------------------------------------------------------------------- */
777 /* save panels.ini */
778 static void
779 save_panel_types (void)
781 panel_view_mode_t type;
783 if (mc_global.mc_run_mode != MC_RUN_FULL)
784 return;
786 type = get_panel_type (0);
787 panel_save_type ("New Left Panel", type);
788 if (type == view_listing)
789 panel_save_setup (left_panel, left_panel->name);
790 type = get_panel_type (1);
791 panel_save_type ("New Right Panel", type);
792 if (type == view_listing)
793 panel_save_setup (right_panel, right_panel->name);
796 char *dirs;
798 dirs = get_panel_dir_for (other_panel);
799 mc_config_set_string (mc_global.panels_config, "Dirs", "other_dir", dirs);
800 g_free (dirs);
803 if (current_panel != NULL)
804 mc_config_set_bool (mc_global.panels_config, "Dirs", "current_is_left",
805 get_current_index () == 0);
807 if (mc_global.panels_config->ini_path == NULL)
808 mc_global.panels_config->ini_path = g_strdup (panels_profile_name);
810 mc_config_del_group (mc_global.panels_config, "Temporal:New Left Panel");
811 mc_config_del_group (mc_global.panels_config, "Temporal:New Right Panel");
813 mc_config_save_file (mc_global.panels_config, NULL);
816 /* --------------------------------------------------------------------------------------------- */
817 /*** public functions ****************************************************************************/
818 /* --------------------------------------------------------------------------------------------- */
820 const char *
821 setup_init (void)
823 if (profile_name == NULL)
825 char *profile;
827 profile = mc_config_get_full_path (MC_CONFIG_FILE);
828 if (!exist_file (profile))
830 char *inifile;
832 inifile = mc_build_filename (mc_global.sysconfig_dir, "mc.ini", (char *) NULL);
833 if (exist_file (inifile))
835 g_free (profile);
836 profile = inifile;
838 else
840 g_free (inifile);
841 inifile = mc_build_filename (mc_global.share_data_dir, "mc.ini", (char *) NULL);
842 if (!exist_file (inifile))
843 g_free (inifile);
844 else
846 g_free (profile);
847 profile = inifile;
852 profile_name = profile;
855 return profile_name;
858 /* --------------------------------------------------------------------------------------------- */
860 void
861 load_setup (void)
863 const char *profile;
865 #ifdef HAVE_CHARSET
866 const char *cbuffer;
868 load_codepages_list ();
869 #endif /* HAVE_CHARSET */
871 profile = setup_init ();
873 /* mc.lib is common for all users, but has priority lower than
874 ${XDG_CONFIG_HOME}/mc/ini. FIXME: it's only used for keys and treestore now */
875 mc_global.profile_name =
876 g_build_filename (mc_global.sysconfig_dir, MC_GLOBAL_CONFIG_FILE, (char *) NULL);
877 if (!exist_file (mc_global.profile_name))
879 g_free (mc_global.profile_name);
880 mc_global.profile_name =
881 g_build_filename (mc_global.share_data_dir, MC_GLOBAL_CONFIG_FILE, (char *) NULL);
884 panels_profile_name = mc_config_get_full_path (MC_PANELS_FILE);
886 mc_global.main_config = mc_config_init (profile, FALSE);
888 if (!exist_file (panels_profile_name))
889 setup__move_panels_config_into_separate_file (profile);
891 mc_global.panels_config = mc_config_init (panels_profile_name, FALSE);
893 load_config ();
894 load_layout ();
895 panels_load_options ();
896 external_panelize_load ();
898 /* Load time formats */
899 user_recent_timeformat =
900 mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "timeformat_recent",
901 FMTTIME);
902 user_old_timeformat =
903 mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "timeformat_old",
904 FMTYEAR);
906 #ifdef ENABLE_VFS_FTP
907 ftpfs_proxy_host =
908 mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "ftp_proxy_host", "gate");
909 ftpfs_init_passwd ();
910 #endif /* ENABLE_VFS_FTP */
912 /* The default color and the terminal dependent color */
913 mc_global.tty.setup_color_string =
914 mc_config_get_string (mc_global.main_config, "Colors", "base_color", "");
915 mc_global.tty.term_color_string =
916 mc_config_get_string (mc_global.main_config, "Colors", getenv ("TERM"), "");
917 mc_global.tty.color_terminal_string =
918 mc_config_get_string (mc_global.main_config, "Colors", "color_terminals", "");
920 /* Load the directory history */
921 /* directory_history_load (); */
922 /* Remove the temporal entries */
924 #ifdef HAVE_CHARSET
925 if (codepages->len > 1)
927 char *buffer;
929 buffer =
930 mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "display_codepage",
931 "");
932 if (buffer[0] != '\0')
934 mc_global.display_codepage = get_codepage_index (buffer);
935 cp_display = get_codepage_id (mc_global.display_codepage);
937 g_free (buffer);
938 buffer =
939 mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "source_codepage",
940 "");
941 if (buffer[0] != '\0')
943 default_source_codepage = get_codepage_index (buffer);
944 mc_global.source_codepage = default_source_codepage; /* May be source_codepage doesn't need this */
945 cp_source = get_codepage_id (mc_global.source_codepage);
947 g_free (buffer);
950 autodetect_codeset =
951 mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "autodetect_codeset", "");
952 if ((autodetect_codeset[0] != '\0') && (strcmp (autodetect_codeset, "off") != 0))
953 is_autodetect_codeset_enabled = TRUE;
955 g_free (init_translation_table (mc_global.source_codepage, mc_global.display_codepage));
956 cbuffer = get_codepage_id (mc_global.display_codepage);
957 if (cbuffer != NULL)
958 mc_global.utf8_display = str_isutf8 (cbuffer);
959 #endif /* HAVE_CHARSET */
961 #ifdef HAVE_ASPELL
962 spell_language =
963 mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "spell_language", "en");
964 #endif /* HAVE_ASPELL */
966 clipboard_store_path =
967 mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_store", "");
968 clipboard_paste_path =
969 mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_paste", "");
972 /* --------------------------------------------------------------------------------------------- */
974 gboolean
975 save_setup (gboolean save_options, gboolean save_panel_options)
977 gboolean ret = TRUE;
979 saving_setup = 1;
981 save_hotlist ();
983 if (save_panel_options)
984 save_panel_types ();
986 if (save_options)
988 char *tmp_profile;
990 save_config ();
991 save_layout ();
992 panels_save_options ();
993 external_panelize_save ();
994 /* directory_history_save (); */
996 #ifdef ENABLE_VFS_FTP
997 mc_config_set_string (mc_global.main_config, CONFIG_MISC_SECTION, "ftpfs_password",
998 ftpfs_anonymous_passwd);
999 if (ftpfs_proxy_host)
1000 mc_config_set_string (mc_global.main_config, CONFIG_MISC_SECTION, "ftp_proxy_host",
1001 ftpfs_proxy_host);
1002 #endif /* ENABLE_VFS_FTP */
1004 #ifdef HAVE_CHARSET
1005 mc_config_set_string (mc_global.main_config, CONFIG_MISC_SECTION, "display_codepage",
1006 get_codepage_id (mc_global.display_codepage));
1007 mc_config_set_string (mc_global.main_config, CONFIG_MISC_SECTION, "source_codepage",
1008 get_codepage_id (default_source_codepage));
1009 mc_config_set_string (mc_global.main_config, CONFIG_MISC_SECTION, "autodetect_codeset",
1010 autodetect_codeset);
1011 #endif /* HAVE_CHARSET */
1013 #ifdef HAVE_ASPELL
1014 mc_config_set_string (mc_global.main_config, CONFIG_MISC_SECTION, "spell_language",
1015 spell_language);
1016 #endif /* HAVE_ASPELL */
1018 mc_config_set_string (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_store",
1019 clipboard_store_path);
1020 mc_config_set_string (mc_global.main_config, CONFIG_MISC_SECTION, "clipboard_paste",
1021 clipboard_paste_path);
1023 tmp_profile = mc_config_get_full_path (MC_CONFIG_FILE);
1024 ret = mc_config_save_to_file (mc_global.main_config, tmp_profile, NULL);
1025 g_free (tmp_profile);
1028 saving_setup = 0;
1030 return ret;
1033 /* --------------------------------------------------------------------------------------------- */
1035 void
1036 done_setup (void)
1038 size_t i;
1040 g_free (clipboard_store_path);
1041 g_free (clipboard_paste_path);
1042 g_free (mc_global.profile_name);
1043 g_free (mc_global.tty.color_terminal_string);
1044 g_free (mc_global.tty.term_color_string);
1045 g_free (mc_global.tty.setup_color_string);
1046 g_free (profile_name);
1047 g_free (panels_profile_name);
1048 mc_config_deinit (mc_global.main_config);
1049 mc_config_deinit (mc_global.panels_config);
1051 g_free (user_recent_timeformat);
1052 g_free (user_old_timeformat);
1054 for (i = 0; str_options[i].opt_name != NULL; i++)
1055 g_free (*str_options[i].opt_addr);
1057 done_hotlist ();
1058 external_panelize_free ();
1059 /* directory_history_free (); */
1061 #ifdef HAVE_CHARSET
1062 g_free (autodetect_codeset);
1063 free_codepages_list ();
1064 #endif
1066 #ifdef HAVE_ASPELL
1067 g_free (spell_language);
1068 #endif /* HAVE_ASPELL */
1072 /* --------------------------------------------------------------------------------------------- */
1074 void
1075 setup_save_config_show_error (const char *filename, GError **mcerror)
1077 if (mcerror != NULL && *mcerror != NULL)
1079 message (D_ERROR, MSG_ERROR, _("Cannot save file %s:\n%s"), filename, (*mcerror)->message);
1080 g_error_free (*mcerror);
1081 *mcerror = NULL;
1085 /* --------------------------------------------------------------------------------------------- */
1087 void
1088 load_key_defs (void)
1091 * Load keys from mc.lib before ${XDG_CONFIG_HOME}/mc/ini, so that the user
1092 * definitions override global settings.
1094 mc_config_t *mc_global_config;
1096 mc_global_config = mc_config_init (mc_global.profile_name, FALSE);
1097 if (mc_global_config != NULL)
1099 load_keys_from_section ("general", mc_global_config);
1100 load_keys_from_section (getenv ("TERM"), mc_global_config);
1101 mc_config_deinit (mc_global_config);
1104 load_keys_from_section ("general", mc_global.main_config);
1105 load_keys_from_section (getenv ("TERM"), mc_global.main_config);
1108 /* --------------------------------------------------------------------------------------------- */
1110 #ifdef ENABLE_VFS_FTP
1111 char *
1112 load_anon_passwd (void)
1114 char *buffer;
1116 buffer =
1117 mc_config_get_string (mc_global.main_config, CONFIG_MISC_SECTION, "ftpfs_password", "");
1119 if ((buffer != NULL) && (buffer[0] != '\0'))
1120 return buffer;
1122 g_free (buffer);
1123 return NULL;
1125 #endif /* ENABLE_VFS_FTP */
1127 /* --------------------------------------------------------------------------------------------- */
1129 void
1130 panel_load_setup (WPanel *panel, const char *section)
1132 size_t i;
1133 char *buffer;
1135 panel->sort_info.reverse =
1136 mc_config_get_bool (mc_global.panels_config, section, "reverse", FALSE);
1137 panel->sort_info.case_sensitive =
1138 mc_config_get_bool (mc_global.panels_config, section, "case_sensitive",
1139 OS_SORT_CASE_SENSITIVE_DEFAULT);
1140 panel->sort_info.exec_first =
1141 mc_config_get_bool (mc_global.panels_config, section, "exec_first", FALSE);
1143 /* Load sort order */
1144 buffer = mc_config_get_string (mc_global.panels_config, section, "sort_order", "name");
1145 panel->sort_field = panel_get_field_by_id (buffer);
1146 if (panel->sort_field == NULL)
1147 panel->sort_field = panel_get_field_by_id ("name");
1149 g_free (buffer);
1151 /* Load the listing format */
1152 buffer = mc_config_get_string (mc_global.panels_config, section, "list_format", NULL);
1153 if (buffer == NULL)
1155 /* fallback to old option */
1156 buffer = mc_config_get_string (mc_global.panels_config, section, "list_mode", "full");
1158 panel->list_format = list_full;
1159 for (i = 0; list_formats[i].key != NULL; i++)
1160 if (g_ascii_strcasecmp (list_formats[i].key, buffer) == 0)
1162 panel->list_format = list_formats[i].list_format;
1163 break;
1165 g_free (buffer);
1167 panel->brief_cols = mc_config_get_int (mc_global.panels_config, section, "brief_cols", 2);
1169 /* User formats */
1170 g_free (panel->user_format);
1171 panel->user_format =
1172 mc_config_get_string (mc_global.panels_config, section, "user_format", NULL);
1174 for (i = 0; i < LIST_FORMATS; i++)
1176 char buffer2[BUF_TINY];
1178 g_free (panel->user_status_format[i]);
1179 g_snprintf (buffer2, sizeof (buffer2), "user_status%lld", (long long) i);
1180 panel->user_status_format[i] =
1181 mc_config_get_string (mc_global.panels_config, section, buffer2, NULL);
1184 panel->user_mini_status =
1185 mc_config_get_bool (mc_global.panels_config, section, "user_mini_status", FALSE);
1187 panel->filter.value =
1188 mc_config_get_string (mc_global.panels_config, section, "filter_value", NULL);
1189 panel->filter.flags =
1190 mc_config_get_int (mc_global.panels_config, section, "filter_flags",
1191 (int) FILE_FILTER_DEFAULT_FLAGS);
1194 /* --------------------------------------------------------------------------------------------- */
1196 void
1197 panel_save_setup (WPanel *panel, const char *section)
1199 char buffer[BUF_TINY];
1200 size_t i;
1202 mc_config_set_bool (mc_global.panels_config, section, "reverse", panel->sort_info.reverse);
1203 mc_config_set_bool (mc_global.panels_config, section, "case_sensitive",
1204 panel->sort_info.case_sensitive);
1205 mc_config_set_bool (mc_global.panels_config, section, "exec_first",
1206 panel->sort_info.exec_first);
1208 mc_config_set_string (mc_global.panels_config, section, "sort_order", panel->sort_field->id);
1210 for (i = 0; list_formats[i].key != NULL; i++)
1211 if (list_formats[i].list_format == (int) panel->list_format)
1213 mc_config_set_string (mc_global.panels_config, section, "list_format",
1214 list_formats[i].key);
1215 break;
1218 mc_config_set_int (mc_global.panels_config, section, "brief_cols", panel->brief_cols);
1220 mc_config_set_string (mc_global.panels_config, section, "user_format", panel->user_format);
1222 for (i = 0; i < LIST_FORMATS; i++)
1224 g_snprintf (buffer, sizeof (buffer), "user_status%lld", (long long) i);
1225 mc_config_set_string (mc_global.panels_config, section, buffer,
1226 panel->user_status_format[i]);
1229 mc_config_set_bool (mc_global.panels_config, section, "user_mini_status",
1230 panel->user_mini_status);
1232 /* do not save the default filter */
1233 if (panel->filter.handler != NULL)
1234 mc_config_set_string (mc_global.panels_config, section, "filter_value",
1235 panel->filter.value);
1236 else
1237 mc_config_del_key (mc_global.panels_config, section, "filter_value");
1238 mc_config_set_int (mc_global.panels_config, section, "filter_flags", (int) panel->filter.flags);
1241 /* --------------------------------------------------------------------------------------------- */