Fixed search for first symbol in string.
[midnight-commander.git] / src / setup.c
blob41f4317585004c226dd3ea7bc59a1195e0472c83
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 vfs_path_t *vpath;
903 buffer = mc_config_get_string (mc_panels_config, "Dirs", "other_dir", ".");
904 vpath = vfs_path_from_str (buffer);
905 if (vfs_file_is_local (vpath))
906 mc_run_param1 = buffer;
907 else
908 g_free (buffer);
909 vfs_path_free (vpath);
912 boot_current_is_left = mc_config_get_bool (mc_panels_config, "Dirs", "current_is_left", TRUE);
914 /* Load time formats */
915 user_recent_timeformat =
916 mc_config_get_string (mc_main_config, "Misc", "timeformat_recent", FMTTIME);
917 user_old_timeformat = mc_config_get_string (mc_main_config, "Misc", "timeformat_old", FMTYEAR);
919 #ifdef ENABLE_VFS_FTP
920 ftpfs_proxy_host = mc_config_get_string (mc_main_config, "Misc", "ftp_proxy_host", "gate");
921 ftpfs_ignore_chattr_errors = mc_config_get_bool (mc_main_config, CONFIG_APP_SECTION,
922 "ignore_ftp_chattr_errors", TRUE);
923 ftpfs_init_passwd ();
924 #endif /* ENABLE_VFS_FTP */
926 /* The default color and the terminal dependent color */
927 mc_global.tty.setup_color_string =
928 mc_config_get_string (mc_main_config, "Colors", "base_color", "");
929 mc_global.tty.term_color_string =
930 mc_config_get_string (mc_main_config, "Colors", getenv ("TERM"), "");
931 mc_global.tty.color_terminal_string =
932 mc_config_get_string (mc_main_config, "Colors", "color_terminals", "");
934 /* Load the directory history */
935 /* directory_history_load (); */
936 /* Remove the temporal entries */
938 #ifdef HAVE_CHARSET
939 if (codepages->len > 1)
941 buffer = mc_config_get_string (mc_main_config, "Misc", "display_codepage", "");
942 if (buffer[0] != '\0')
944 mc_global.display_codepage = get_codepage_index (buffer);
945 cp_display = get_codepage_id (mc_global.display_codepage);
947 g_free (buffer);
948 buffer = mc_config_get_string (mc_main_config, "Misc", "source_codepage", "");
949 if (buffer[0] != '\0')
951 default_source_codepage = get_codepage_index (buffer);
952 mc_global.source_codepage = default_source_codepage; /* May be source_codepage doesn't need this */
953 cp_source = get_codepage_id (mc_global.source_codepage);
955 g_free (buffer);
958 autodetect_codeset = mc_config_get_string (mc_main_config, "Misc", "autodetect_codeset", "");
959 if ((autodetect_codeset[0] != '\0') && (strcmp (autodetect_codeset, "off") != 0))
960 is_autodetect_codeset_enabled = TRUE;
962 g_free (init_translation_table (mc_global.source_codepage, mc_global.display_codepage));
963 buffer = (char *) get_codepage_id (mc_global.display_codepage);
964 if (buffer != NULL)
965 mc_global.utf8_display = str_isutf8 (buffer);
966 #endif /* HAVE_CHARSET */
968 clipboard_store_path = mc_config_get_string (mc_main_config, "Misc", "clipboard_store", "");
969 clipboard_paste_path = mc_config_get_string (mc_main_config, "Misc", "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 save_panelize ();
994 /* directory_history_save (); */
996 #ifdef ENABLE_VFS_FTP
997 mc_config_set_string (mc_main_config, "Misc", "ftpfs_password", ftpfs_anonymous_passwd);
998 if (ftpfs_proxy_host)
999 mc_config_set_string (mc_main_config, "Misc", "ftp_proxy_host", ftpfs_proxy_host);
1000 #endif /* ENABLE_VFS_FTP */
1002 #ifdef HAVE_CHARSET
1003 mc_config_set_string (mc_main_config, "Misc", "display_codepage",
1004 get_codepage_id (mc_global.display_codepage));
1005 mc_config_set_string (mc_main_config, "Misc", "source_codepage",
1006 get_codepage_id (default_source_codepage));
1007 mc_config_set_string (mc_main_config, "Misc", "autodetect_codeset", autodetect_codeset);
1008 #endif /* HAVE_CHARSET */
1009 mc_config_set_string (mc_main_config, "Misc", "clipboard_store", clipboard_store_path);
1010 mc_config_set_string (mc_main_config, "Misc", "clipboard_paste", clipboard_paste_path);
1012 tmp_profile = g_build_filename (mc_config_get_path (), MC_CONFIG_FILE, NULL);
1013 ret = mc_config_save_to_file (mc_main_config, tmp_profile, NULL);
1014 g_free (tmp_profile);
1017 saving_setup = 0;
1019 return ret;
1022 /* --------------------------------------------------------------------------------------------- */
1024 void
1025 done_setup (void)
1027 size_t i;
1029 g_free (clipboard_store_path);
1030 g_free (clipboard_paste_path);
1031 g_free (profile_name);
1032 g_free (global_profile_name);
1033 g_free (mc_global.tty.color_terminal_string);
1034 g_free (mc_global.tty.term_color_string);
1035 g_free (mc_global.tty.setup_color_string);
1036 g_free (panels_profile_name);
1037 mc_config_deinit (mc_main_config);
1038 mc_config_deinit (mc_panels_config);
1040 g_free (user_recent_timeformat);
1041 g_free (user_old_timeformat);
1043 for (i = 0; str_options[i].opt_name != NULL; i++)
1044 g_free (*str_options[i].opt_addr);
1046 done_hotlist ();
1047 done_panelize ();
1048 /* directory_history_free (); */
1050 #ifdef HAVE_CHARSET
1051 g_free (autodetect_codeset);
1052 free_codepages_list ();
1053 #endif
1056 /* --------------------------------------------------------------------------------------------- */
1058 void
1059 save_config (void)
1061 size_t i;
1063 /* Save integer options */
1064 for (i = 0; int_options[i].opt_name != NULL; i++)
1065 mc_config_set_int (mc_main_config, CONFIG_APP_SECTION, int_options[i].opt_name,
1066 *int_options[i].opt_addr);
1068 /* Save string options */
1069 for (i = 0; str_options[i].opt_name != NULL; i++)
1070 mc_config_set_string (mc_main_config, CONFIG_APP_SECTION, str_options[i].opt_name,
1071 *str_options[i].opt_addr);
1074 /* --------------------------------------------------------------------------------------------- */
1076 void
1077 setup_save_config_show_error (const char *filename, GError ** error)
1079 if (error != NULL && *error != NULL)
1081 message (D_ERROR, MSG_ERROR, _("Cannot save file %s:\n%s"), filename, (*error)->message);
1082 g_error_free (*error);
1083 *error = NULL;
1087 /* --------------------------------------------------------------------------------------------- */
1089 void
1090 save_layout (void)
1092 size_t i;
1094 /* Save integer options */
1095 for (i = 0; layout[i].opt_name != NULL; i++)
1096 mc_config_set_int (mc_main_config, "Layout", layout[i].opt_name, *layout[i].opt_addr);
1099 /* --------------------------------------------------------------------------------------------- */
1101 void
1102 load_key_defs (void)
1105 * Load keys from mc.lib before ${XDG_CONFIG_HOME}/mc/ini, so that the user
1106 * definitions override global settings.
1108 mc_config_t *mc_global_config;
1110 mc_global_config = mc_config_init (global_profile_name);
1111 if (mc_global_config != NULL)
1113 load_keys_from_section ("general", mc_global_config);
1114 load_keys_from_section (getenv ("TERM"), mc_global_config);
1115 mc_config_deinit (mc_global_config);
1118 load_keys_from_section ("general", mc_main_config);
1119 load_keys_from_section (getenv ("TERM"), mc_main_config);
1122 /* --------------------------------------------------------------------------------------------- */
1124 #ifdef ENABLE_VFS_FTP
1125 char *
1126 load_anon_passwd (void)
1128 char *buffer;
1130 buffer = mc_config_get_string (mc_main_config, "Misc", "ftpfs_password", "");
1132 if ((buffer != NULL) && (buffer[0] != '\0'))
1133 return buffer;
1135 g_free (buffer);
1136 return NULL;
1138 #endif /* ENABLE_VFS_FTP */
1140 /* --------------------------------------------------------------------------------------------- */
1142 void
1143 load_keymap_defs (gboolean load_from_file)
1146 * Load keymap from GLOBAL_KEYMAP_FILE before ${XDG_DATA_HOME}/mc/keymap, so that the user
1147 * definitions override global settings.
1149 mc_config_t *mc_global_keymap;
1151 mc_global_keymap = load_setup_get_keymap_profile_config (load_from_file);
1153 if (mc_global_keymap != NULL)
1155 main_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1156 load_keymap_from_section (KEYMAP_SECTION_MAIN, main_keymap, mc_global_keymap);
1157 main_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1158 load_keymap_from_section (KEYMAP_SECTION_MAIN_EXT, main_x_keymap, mc_global_keymap);
1160 panel_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1161 load_keymap_from_section (KEYMAP_SECTION_PANEL, panel_keymap, mc_global_keymap);
1163 dialog_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1164 load_keymap_from_section (KEYMAP_SECTION_DIALOG, dialog_keymap, mc_global_keymap);
1166 input_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1167 load_keymap_from_section (KEYMAP_SECTION_INPUT, input_keymap, mc_global_keymap);
1169 listbox_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1170 load_keymap_from_section (KEYMAP_SECTION_LISTBOX, listbox_keymap, mc_global_keymap);
1172 tree_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1173 load_keymap_from_section (KEYMAP_SECTION_TREE, tree_keymap, mc_global_keymap);
1175 help_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1176 load_keymap_from_section (KEYMAP_SECTION_HELP, help_keymap, mc_global_keymap);
1178 #ifdef USE_INTERNAL_EDIT
1179 editor_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1180 load_keymap_from_section (KEYMAP_SECTION_EDITOR, editor_keymap, mc_global_keymap);
1181 editor_x_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1182 load_keymap_from_section (KEYMAP_SECTION_EDITOR_EXT, editor_x_keymap, mc_global_keymap);
1183 #endif
1185 viewer_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1186 load_keymap_from_section (KEYMAP_SECTION_VIEWER, viewer_keymap, mc_global_keymap);
1187 viewer_hex_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1188 load_keymap_from_section (KEYMAP_SECTION_VIEWER_HEX, viewer_hex_keymap, mc_global_keymap);
1190 #ifdef USE_DIFF_VIEW
1191 diff_keymap = g_array_new (TRUE, FALSE, sizeof (global_keymap_t));
1192 load_keymap_from_section (KEYMAP_SECTION_DIFFVIEWER, diff_keymap, mc_global_keymap);
1193 #endif
1195 mc_config_deinit (mc_global_keymap);
1198 main_map = (global_keymap_t *) main_keymap->data;
1199 main_x_map = (global_keymap_t *) main_x_keymap->data;
1200 panel_map = (global_keymap_t *) panel_keymap->data;
1201 dialog_map = (global_keymap_t *) dialog_keymap->data;
1202 input_map = (global_keymap_t *) input_keymap->data;
1203 listbox_map = (global_keymap_t *) listbox_keymap->data;
1204 tree_map = (global_keymap_t *) tree_keymap->data;
1205 help_map = (global_keymap_t *) help_keymap->data;
1206 #ifdef USE_INTERNAL_EDIT
1207 editor_map = (global_keymap_t *) editor_keymap->data;
1208 editor_x_map = (global_keymap_t *) editor_x_keymap->data;
1209 #endif
1210 viewer_map = (global_keymap_t *) viewer_keymap->data;
1211 viewer_hex_map = (global_keymap_t *) viewer_hex_keymap->data;
1212 #ifdef USE_DIFF_VIEW
1213 diff_map = (global_keymap_t *) diff_keymap->data;
1214 #endif
1217 /* --------------------------------------------------------------------------------------------- */
1219 void
1220 free_keymap_defs (void)
1222 if (main_keymap != NULL)
1223 g_array_free (main_keymap, TRUE);
1224 if (main_x_keymap != NULL)
1225 g_array_free (main_x_keymap, TRUE);
1226 if (panel_keymap != NULL)
1227 g_array_free (panel_keymap, TRUE);
1228 if (dialog_keymap != NULL)
1229 g_array_free (dialog_keymap, TRUE);
1230 if (input_keymap != NULL)
1231 g_array_free (input_keymap, TRUE);
1232 if (listbox_keymap != NULL)
1233 g_array_free (listbox_keymap, TRUE);
1234 if (tree_keymap != NULL)
1235 g_array_free (tree_keymap, TRUE);
1236 if (help_keymap != NULL)
1237 g_array_free (help_keymap, TRUE);
1238 #ifdef USE_INTERNAL_EDIT
1239 if (editor_keymap != NULL)
1240 g_array_free (editor_keymap, TRUE);
1241 if (editor_x_keymap != NULL)
1242 g_array_free (editor_x_keymap, TRUE);
1243 #endif
1244 if (viewer_keymap != NULL)
1245 g_array_free (viewer_keymap, TRUE);
1246 if (viewer_hex_keymap != NULL)
1247 g_array_free (viewer_hex_keymap, TRUE);
1248 #ifdef USE_DIFF_VIEW
1249 if (diff_keymap != NULL)
1250 g_array_free (diff_keymap, TRUE);
1251 #endif
1254 /* --------------------------------------------------------------------------------------------- */
1256 void
1257 panel_load_setup (WPanel * panel, const char *section)
1259 size_t i;
1260 char *buffer, buffer2[BUF_TINY];
1262 panel->sort_info.reverse = mc_config_get_int (mc_panels_config, section, "reverse", 0);
1263 panel->sort_info.case_sensitive =
1264 mc_config_get_int (mc_panels_config, section, "case_sensitive",
1265 OS_SORT_CASE_SENSITIVE_DEFAULT);
1266 panel->sort_info.exec_first = mc_config_get_int (mc_panels_config, section, "exec_first", 0);
1268 /* Load sort order */
1269 buffer = mc_config_get_string (mc_panels_config, section, "sort_order", "name");
1270 panel->sort_info.sort_field = panel_get_field_by_id (buffer);
1271 if (panel->sort_info.sort_field == NULL)
1272 panel->sort_info.sort_field = panel_get_field_by_id ("name");
1274 g_free (buffer);
1276 /* Load the listing mode */
1277 buffer = mc_config_get_string (mc_panels_config, section, "list_mode", "full");
1278 panel->list_type = list_full;
1279 for (i = 0; list_types[i].key != NULL; i++)
1280 if (g_ascii_strcasecmp (list_types[i].key, buffer) == 0)
1282 panel->list_type = list_types[i].list_type;
1283 break;
1285 g_free (buffer);
1287 /* User formats */
1288 g_free (panel->user_format);
1289 panel->user_format =
1290 mc_config_get_string (mc_panels_config, section, "user_format", DEFAULT_USER_FORMAT);
1292 for (i = 0; i < LIST_TYPES; i++)
1294 g_free (panel->user_status_format[i]);
1295 g_snprintf (buffer2, BUF_TINY, "user_status%lld", (long long) i);
1296 panel->user_status_format[i] =
1297 mc_config_get_string (mc_panels_config, section, buffer2, DEFAULT_USER_FORMAT);
1300 panel->user_mini_status = mc_config_get_int (mc_panels_config, section, "user_mini_status", 0);
1303 /* --------------------------------------------------------------------------------------------- */
1305 void
1306 panel_save_setup (struct WPanel *panel, const char *section)
1308 char buffer[BUF_TINY];
1309 size_t i;
1311 mc_config_set_int (mc_panels_config, section, "reverse", panel->sort_info.reverse);
1312 mc_config_set_int (mc_panels_config, section, "case_sensitive",
1313 panel->sort_info.case_sensitive);
1314 mc_config_set_int (mc_panels_config, section, "exec_first", panel->sort_info.exec_first);
1316 mc_config_set_string (mc_panels_config, section, "sort_order", panel->sort_info.sort_field->id);
1318 for (i = 0; list_types[i].key != NULL; i++)
1319 if (list_types[i].list_type == panel->list_type)
1321 mc_config_set_string (mc_panels_config, section, "list_mode", list_types[i].key);
1322 break;
1325 mc_config_set_string (mc_panels_config, section, "user_format", panel->user_format);
1327 for (i = 0; i < LIST_TYPES; i++)
1329 g_snprintf (buffer, BUF_TINY, "user_status%lld", (long long) i);
1330 mc_config_set_string (mc_panels_config, section, buffer, panel->user_status_format[i]);
1333 mc_config_set_int (mc_panels_config, section, "user_mini_status", panel->user_mini_status);
1336 /* --------------------------------------------------------------------------------------------- */
1339 Load panels options from [Panels] section.
1341 void
1342 panels_load_options (void)
1344 if (mc_config_has_group (mc_main_config, CONFIG_PANELS_SECTION))
1346 size_t i;
1347 int qmode;
1349 for (i = 0; panels_ini_options[i].opt_name != NULL; i++)
1350 *panels_ini_options[i].opt_addr =
1351 mc_config_get_bool (mc_main_config, CONFIG_PANELS_SECTION,
1352 panels_ini_options[i].opt_name,
1353 *panels_ini_options[i].opt_addr);
1355 qmode = mc_config_get_int (mc_main_config, CONFIG_PANELS_SECTION,
1356 "quick_search_mode", (int) panels_options.qsearch_mode);
1357 if (qmode < 0)
1358 panels_options.qsearch_mode = QSEARCH_CASE_INSENSITIVE;
1359 else if (qmode >= QSEARCH_NUM)
1360 panels_options.qsearch_mode = QSEARCH_PANEL_CASE;
1361 else
1362 panels_options.qsearch_mode = (qsearch_mode_t) qmode;
1366 /* --------------------------------------------------------------------------------------------- */
1369 Save panels options in [Panels] section.
1371 void
1372 panels_save_options (void)
1374 size_t i;
1376 for (i = 0; panels_ini_options[i].opt_name != NULL; i++)
1377 mc_config_set_bool (mc_main_config, CONFIG_PANELS_SECTION,
1378 panels_ini_options[i].opt_name, *panels_ini_options[i].opt_addr);
1380 mc_config_set_int (mc_main_config, CONFIG_PANELS_SECTION,
1381 "quick_search_mode", (int) panels_options.qsearch_mode);
1384 /* --------------------------------------------------------------------------------------------- */