1 /* Setup loading/saving.
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007, 2009 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program; if not, write to the Free Software
17 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
20 * \brief Source: setup loading/saving
28 #include <sys/types.h>
33 #include "../src/tty/tty.h"
34 #include "../src/tty/key.h"
35 #include "../src/tty/mouse.h" /* To make view.h happy */
40 #include "tree.h" /* xtree_mode */
41 #include "../src/mcconfig/mcconfig.h"
43 #include "../src/viewer/mcviewer.h" /* For the externs */
44 #include "hotlist.h" /* load/save/done hotlist */
45 #include "panelize.h" /* load/save/done panelize */
47 #include "menu.h" /* menubar_visible declaration */
49 #include "file.h" /* safe_delete */
52 #include "../vfs/gc.h"
60 # include "../vfs/ftpfs.h"
61 # include "../vfs/fish.h"
64 #ifdef USE_INTERNAL_EDIT
65 # include "../edit/edit.h"
68 #include "../src/strutil.h" /* str_isutf8 () */
71 extern char *find_ignore_dirs
;
73 extern int num_history_items_recorded
;
75 char *profile_name
; /* .mc/ini */
76 char *global_profile_name
; /* mc.lib */
77 char *panels_profile_name
; /* .mc/panels.ini */
79 char *setup_color_string
;
80 char *term_color_string
;
81 char *color_terminal_string
;
83 int startup_left_mode
;
84 int startup_right_mode
;
86 /* Ugly hack to allow panel_save_setup to work as a place holder for */
87 /* default panel values */
94 { "name", (sortfn
*) sort_name
},
95 { "extension", (sortfn
*) sort_ext
},
96 { "time", (sortfn
*) sort_time
},
97 { "atime", (sortfn
*) sort_atime
},
98 { "ctime", (sortfn
*) sort_ctime
},
99 { "size", (sortfn
*) sort_size
},
100 { "inode", (sortfn
*) sort_inode
},
101 { "unsorted", (sortfn
*) unsorted
},
105 static const struct {
109 { "full", list_full
},
110 { "brief", list_brief
},
111 { "long", list_long
},
112 { "user", list_user
},
116 static const struct {
117 const char *opt_name
;
120 { "listing", view_listing
},
121 { "quickview", view_quick
},
122 { "info", view_info
},
123 { "tree", view_tree
},
127 static const struct {
128 const char *opt_name
;
131 { "equal_split", &equal_split
},
132 { "first_panel_size", &first_panel_size
},
133 { "message_visible", &message_visible
},
134 { "keybar_visible", &keybar_visible
},
135 { "xterm_title", &xterm_title
},
136 { "output_lines", &output_lines
},
137 { "command_prompt", &command_prompt
},
138 { "menubar_visible", &menubar_visible
},
139 { "show_mini_info", &show_mini_info
},
140 { "permission_mode", &permission_mode
},
141 { "filetype_mode", &filetype_mode
},
142 { "free_space", &free_space
},
146 static const struct {
147 const char *opt_name
;
150 { "show_backups", &show_backups
},
151 { "kilobyte_si", &kilobyte_si
},
152 { "show_dot_files", &show_dot_files
},
153 { "verbose", &verbose
},
154 { "mark_moves_down", &mark_moves_down
},
155 { "pause_after_run", &pause_after_run
},
156 { "shell_patterns", &easy_patterns
},
157 { "auto_save_setup", &auto_save_setup
},
158 { "auto_menu", &auto_menu
},
159 { "use_internal_view", &use_internal_view
},
160 { "use_internal_edit", &use_internal_edit
},
161 { "clear_before_exec", &clear_before_exec
},
162 { "mix_all_files", &mix_all_files
},
163 { "fast_reload", &fast_reload
},
164 { "fast_reload_msg_shown", &fast_reload_w
},
165 { "confirm_delete", &confirm_delete
},
166 { "confirm_overwrite", &confirm_overwrite
},
167 { "confirm_execute", &confirm_execute
},
168 { "confirm_exit", &confirm_exit
},
169 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete
},
170 { "safe_delete", &safe_delete
},
171 { "mouse_repeat_rate", &mou_auto_repeat
},
172 { "double_click_speed", &double_click_speed
},
174 { "eight_bit_clean", &eight_bit_clean
},
175 { "full_eight_bits", &full_eight_bits
},
176 #endif /* !HAVE_CHARSET */
177 { "use_8th_bit_as_meta", &use_8th_bit_as_meta
},
178 { "confirm_view_dir", &confirm_view_dir
},
179 { "mouse_move_pages", &mouse_move_pages
},
180 { "mouse_move_pages_viewer", &mcview_mouse_move_pages
},
181 { "mouse_close_dialog", &mouse_close_dialog
},
182 { "fast_refresh", &fast_refresh
},
183 { "navigate_with_arrows", &navigate_with_arrows
},
184 { "drop_menus", &drop_menus
},
185 { "wrap_mode", &mcview_global_wrap_mode
},
186 { "old_esc_mode", &old_esc_mode
},
187 { "cd_symlinks", &cd_symlinks
},
188 { "show_all_if_ambiguous", &show_all_if_ambiguous
},
189 { "max_dirt_limit", &mcview_max_dirt_limit
},
190 { "torben_fj_mode", &torben_fj_mode
},
191 { "use_file_to_guess_type", &use_file_to_check_type
},
192 { "alternate_plus_minus", &alternate_plus_minus
},
193 { "only_leading_plus_minus", &only_leading_plus_minus
},
194 { "show_output_starts_shell", &output_starts_shell
},
195 { "panel_scroll_pages", &panel_scroll_pages
},
196 { "xtree_mode", &xtree_mode
},
197 { "num_history_items_recorded", &num_history_items_recorded
},
198 { "file_op_compute_totals", &file_op_compute_totals
},
199 { "skip_check_codeset", &skip_check_codeset
},
201 { "vfs_timeout", &vfs_timeout
},
203 { "ftpfs_directory_timeout", &ftpfs_directory_timeout
},
204 { "use_netrc", &use_netrc
},
205 { "ftpfs_retry_seconds", &ftpfs_retry_seconds
},
206 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy
},
207 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections
},
208 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options
},
209 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls
},
210 { "fish_directory_timeout", &fish_directory_timeout
},
211 #endif /* USE_NETCODE */
213 #ifdef USE_INTERNAL_EDIT
214 { "editor_word_wrap_line_length", &option_word_wrap_line_length
},
215 { "editor_key_emulation", &edit_key_emulation
},
216 { "editor_tab_spacing", &option_tab_spacing
},
217 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces
},
218 { "editor_return_does_auto_indent", &option_return_does_auto_indent
},
219 { "editor_backspace_through_tabs", &option_backspace_through_tabs
},
220 { "editor_fake_half_tabs", &option_fake_half_tabs
},
221 { "editor_option_save_mode", &option_save_mode
},
222 { "editor_option_save_position", &option_save_position
},
223 { "editor_option_auto_para_formatting", &option_auto_para_formatting
},
224 { "editor_option_typewriter_wrap", &option_typewriter_wrap
},
225 { "editor_edit_confirm_save", &edit_confirm_save
},
226 { "editor_syntax_highlighting", &option_syntax_highlighting
},
227 { "editor_persistent_selections", &option_persistent_selections
},
228 { "editor_cursor_beyond_eol", &option_cursor_beyond_eol
},
229 { "editor_visible_tabs", &visible_tabs
},
230 { "editor_visible_spaces", &visible_tws
},
231 { "editor_line_state", &option_line_state
},
232 { "editor_simple_statusbar", &simple_statusbar
},
233 #endif /* USE_INTERNAL_EDIT */
235 { "nice_rotating_dash", &nice_rotating_dash
},
236 { "horizontal_split", &horizontal_split
},
237 { "mcview_remember_file_position", &mcview_remember_file_position
},
238 { "auto_fill_mkdir_name", &auto_fill_mkdir_name
},
239 { "reverse_files_only", &reverse_files_only
},
243 static const struct {
244 const char *opt_name
;
246 const char *opt_defval
;
248 #ifdef USE_INTERNAL_EDIT
249 { "editor_backup_extension", &option_backup_ext
, "~" },
255 panel_save_setup (struct WPanel
*panel
, const char *section
)
260 mc_config_set_int(mc_panels_config
, section
, "reverse", panel
->reverse
);
261 mc_config_set_int(mc_panels_config
, section
, "case_sensitive", panel
->case_sensitive
);
262 mc_config_set_int(mc_panels_config
, section
, "exec_first", panel
->exec_first
);
265 for (i
= 0; sort_names
[i
].key
; i
++)
266 if (sort_names
[i
].sort_type
== (sortfn
*) panel
->sort_type
){
267 mc_config_set_string(mc_panels_config
, section
, "sort_order", sort_names
[i
].key
);
271 for (i
= 0; list_types
[i
].key
; i
++)
272 if (list_types
[i
].list_type
== panel
->list_type
){
273 mc_config_set_string(mc_panels_config
, section
, "list_mode", list_types
[i
].key
);
277 mc_config_set_string(mc_panels_config
, section
, "user_format", panel
->user_format
);
279 for (i
= 0; i
< LIST_TYPES
; i
++){
280 buffer
= g_strdup_printf("user_status%d", i
);
281 mc_config_set_string(mc_panels_config
, section
, buffer
, panel
->user_status_format
[i
]);
285 mc_config_set_int(mc_panels_config
, section
, "user_mini_status", panel
->user_mini_status
);
294 profile
= concat_dir_and_file (home_dir
, PROFILE_NAME
);
296 /* Save integer options */
297 for (i
= 0; layout
[i
].opt_name
; i
++){
298 mc_config_set_int(mc_main_config
, "Layout", layout
[i
].opt_name
, *layout
[i
].opt_addr
);
300 mc_config_save_to_file (mc_main_config
, profile
);
306 save_configure (void)
311 profile
= concat_dir_and_file (home_dir
, PROFILE_NAME
);
313 /* Save integer options */
314 for (i
= 0; int_options
[i
].opt_name
; i
++)
315 mc_config_set_int(mc_main_config
, CONFIG_APP_SECTION
, int_options
[i
].opt_name
, *int_options
[i
].opt_addr
);
317 /* Save string options */
318 for (i
= 0; str_options
[i
].opt_name
!= NULL
; i
++)
319 mc_config_set_string(mc_main_config
, CONFIG_APP_SECTION
, str_options
[i
].opt_name
, *str_options
[i
].opt_addr
);
321 mc_config_save_to_file (mc_main_config
, profile
);
326 panel_save_type (const char *section
, int type
)
330 for (i
= 0; panel_types
[i
].opt_name
; i
++)
331 if (panel_types
[i
].opt_type
== type
){
332 mc_config_set_string(mc_panels_config
, section
,
333 "display", panel_types
[i
].opt_name
);
339 save_panel_types (void)
343 type
= get_display_type (0);
344 panel_save_type ("New Left Panel", type
);
345 if (type
== view_listing
)
346 panel_save_setup (left_panel
, left_panel
->panel_name
);
347 type
= get_display_type (1);
348 panel_save_type ("New Right Panel", type
);
349 if (type
== view_listing
)
350 panel_save_setup (right_panel
, right_panel
->panel_name
);
352 mc_config_set_string(mc_panels_config
, "Dirs" , "other_dir",
353 get_other_type () == view_listing
354 ? other_panel
->cwd
: ".");
355 if (current_panel
!= NULL
)
356 mc_config_set_string(mc_panels_config
, "Dirs" , "current_is_left",
357 get_current_index () == 0 ? "1" : "0");
359 if (mc_panels_config
->ini_path
== NULL
)
360 mc_panels_config
->ini_path
= g_strdup(panels_profile_name
);
362 mc_config_del_group (mc_panels_config
, "Temporal:New Left Panel");
363 mc_config_del_group (mc_panels_config
, "Temporal:New Right Panel");
365 mc_config_save_file (mc_panels_config
);
383 /* directory_history_save (); */
385 #if defined(USE_VFS) && defined (USE_NETCODE)
386 mc_config_set_string(mc_main_config
, "Misc" , "ftpfs_password",
387 ftpfs_anonymous_passwd
);
388 if (ftpfs_proxy_host
)
389 mc_config_set_string(mc_main_config
, "Misc" , "ftp_proxy_host",
391 #endif /* USE_VFS && USE_NETCODE */
394 mc_config_set_string(mc_main_config
, "Misc" , "display_codepage",
395 get_codepage_id( display_codepage
));
396 mc_config_set_string(mc_main_config
, "Misc" , "source_codepage",
397 get_codepage_id( source_codepage
));
398 #endif /* HAVE_CHARSET */
399 tmp_profile
= concat_dir_and_file (home_dir
, PROFILE_NAME
);
400 mc_config_save_to_file (mc_main_config
, tmp_profile
);
401 g_free (tmp_profile
);
406 panel_load_setup (WPanel
*panel
, const char *section
)
411 panel
->reverse
= mc_config_get_int(mc_panels_config
, section
, "reverse", 0);
412 panel
->case_sensitive
= mc_config_get_int(mc_panels_config
, section
, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT
);
413 panel
->exec_first
= mc_config_get_int(mc_panels_config
, section
, "exec_first", 0);
415 /* Load sort order */
416 buffer
= mc_config_get_string(mc_panels_config
, section
, "sort_order", "name");
418 panel
->sort_type
= (sortfn
*) sort_name
;
419 for (i
= 0; sort_names
[i
].key
; i
++)
420 if ( g_strcasecmp (sort_names
[i
].key
, buffer
) == 0){
421 panel
->sort_type
= sort_names
[i
].sort_type
;
425 /* Load the listing mode */
426 buffer
= mc_config_get_string(mc_panels_config
, section
, "list_mode", "full");
427 panel
->list_type
= list_full
;
428 for (i
= 0; list_types
[i
].key
; i
++)
429 if ( g_strcasecmp (list_types
[i
].key
, buffer
) == 0){
430 panel
->list_type
= list_types
[i
].list_type
;
436 g_free (panel
->user_format
);
437 panel
->user_format
= mc_config_get_string(mc_panels_config
, section
, "user_format", DEFAULT_USER_FORMAT
);
439 for (i
= 0; i
< LIST_TYPES
; i
++){
440 g_free (panel
->user_status_format
[i
]);
441 buffer
= g_strdup_printf("user_status%d",i
);
442 panel
->user_status_format
[i
] =
443 mc_config_get_string(mc_panels_config
, section
, buffer
, DEFAULT_USER_FORMAT
);
447 panel
->user_mini_status
=
448 mc_config_get_int(mc_panels_config
, section
, "user_mini_status", 0);
457 for (i
= 0; layout
[i
].opt_name
; i
++)
458 *layout
[i
].opt_addr
=
459 mc_config_get_int(mc_main_config
,"Layout", layout
[i
].opt_name
, *layout
[i
].opt_addr
);
463 setup__load_panel_state (const char *section
)
468 int mode
= view_listing
;
470 /* Load the display mode */
471 buffer
= mc_config_get_string(mc_panels_config
, section
, "display", "listing");
473 for (i
= 0; panel_types
[i
].opt_name
; i
++)
474 if ( g_strcasecmp (panel_types
[i
].opt_name
, buffer
) == 0){
475 mode
= panel_types
[i
].opt_type
;
483 setup__is_cfg_group_must_panel_config(const char *grp
)
486 ! strcasecmp("Dirs",grp
) ||
487 ! strcasecmp("Temporal:New Right Panel",grp
) ||
488 ! strcasecmp("Temporal:New Left Panel",grp
) ||
489 ! strcasecmp("New Left Panel",grp
) ||
490 ! strcasecmp("New Right Panel",grp
)
497 setup__move_panels_config_into_separate_file(const char*profile
)
499 mc_config_t
*tmp_cfg
;
500 char **groups
, **curr_grp
;
501 const char *need_grp
;
504 if (!exist_file(profile
))
507 tmp_cfg
= mc_config_init(profile
);
511 curr_grp
= groups
= mc_config_get_groups (tmp_cfg
, &groups_count
);
514 mc_config_deinit(tmp_cfg
);
519 if ( setup__is_cfg_group_must_panel_config(*curr_grp
) == NULL
)
521 mc_config_del_group (tmp_cfg
, *curr_grp
);
526 mc_config_save_to_file (tmp_cfg
, panels_profile_name
);
527 mc_config_deinit(tmp_cfg
);
529 tmp_cfg
= mc_config_init(profile
);
540 need_grp
= setup__is_cfg_group_must_panel_config(*curr_grp
);
541 if ( need_grp
!= NULL
)
543 mc_config_del_group (tmp_cfg
, need_grp
);
548 mc_config_save_file (tmp_cfg
);
549 mc_config_deinit(tmp_cfg
);
562 profile
= concat_dir_and_file (home_dir
, PROFILE_NAME
);
563 if (!exist_file (profile
)){
564 inifile
= concat_dir_and_file (mc_home
, "mc.ini");
565 if (exist_file (inifile
)){
570 inifile
= concat_dir_and_file (mc_home_alt
, "mc.ini");
571 if (exist_file (inifile
)) {
579 profile_name
= profile
;
591 profile
= setup_init ();
593 /* mc.lib is common for all users, but has priority lower than
594 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
595 global_profile_name
= concat_dir_and_file (mc_home
, "mc.lib");
597 if (!exist_file(global_profile_name
)) {
598 g_free (global_profile_name
);
599 global_profile_name
= concat_dir_and_file (mc_home_alt
, "mc.lib");
601 panels_profile_name
= concat_dir_and_file (home_dir
, PANELS_PROFILE_NAME
);
603 mc_main_config
= mc_config_init(profile
);
605 if (!exist_file(panels_profile_name
))
606 setup__move_panels_config_into_separate_file(profile
);
608 mc_panels_config
= mc_config_init(panels_profile_name
);
610 /* Load integer boolean options */
611 for (i
= 0; int_options
[i
].opt_name
; i
++)
612 *int_options
[i
].opt_addr
=
613 mc_config_get_int(mc_main_config
, CONFIG_APP_SECTION
, int_options
[i
].opt_name
, *int_options
[i
].opt_addr
);
615 /* Load string options */
616 for (i
= 0; str_options
[i
].opt_name
!= NULL
; i
++)
617 *str_options
[i
].opt_addr
=
618 mc_config_get_string (mc_main_config
, CONFIG_APP_SECTION
, str_options
[i
].opt_name
, str_options
[i
].opt_defval
);
624 startup_left_mode
= setup__load_panel_state ("New Left Panel");
625 startup_right_mode
= setup__load_panel_state ("New Right Panel");
627 /* At least one of the panels is a listing panel */
628 if (startup_left_mode
!= view_listing
&& startup_right_mode
!=view_listing
)
629 startup_left_mode
= view_listing
;
632 buffer
= mc_config_get_string(mc_panels_config
, "Dirs", "other_dir", ".");
633 if (vfs_file_is_local (buffer
))
639 boot_current_is_left
=
640 mc_config_get_int(mc_panels_config
, "Dirs", "current_is_left", 1);
643 ftpfs_proxy_host
= mc_config_get_string(mc_main_config
, "Misc", "ftp_proxy_host", "gate");
645 setup_color_string
= mc_config_get_string(mc_main_config
, "Misc", "find_ignore_dirs", "");
646 if (setup_color_string
[0])
647 find_ignore_dirs
= g_strconcat (":", setup_color_string
, ":", (char *) NULL
);
648 g_free(setup_color_string
);
650 /* The default color and the terminal dependent color */
651 setup_color_string
= mc_config_get_string(mc_main_config
, "Colors", "base_color", "");
652 term_color_string
= mc_config_get_string(mc_main_config
, "Colors", getenv ("TERM"), "");
653 color_terminal_string
= mc_config_get_string(mc_main_config
, "Colors", "color_terminals", "");
655 /* Load the directory history */
656 /* directory_history_load (); */
657 /* Remove the temporal entries */
658 #if defined(USE_VFS) && defined (USE_NETCODE)
659 ftpfs_init_passwd ();
660 #endif /* USE_VFS && USE_NETCODE */
663 if ( load_codepages_list() > 0 ) {
664 buffer
= mc_config_get_string(mc_main_config
, "Misc", "display_codepage", "");
665 if ( buffer
[0] != '\0' )
667 display_codepage
= get_codepage_index( buffer
);
668 cp_display
= get_codepage_id (display_codepage
);
671 buffer
= mc_config_get_string(mc_main_config
, "Misc", "source_codepage", "");
672 if ( buffer
[0] != '\0' )
674 source_codepage
= get_codepage_index( buffer
);
675 cp_source
= get_codepage_id (source_codepage
);
679 init_translation_table( source_codepage
, display_codepage
);
680 if ( get_codepage_id( display_codepage
) )
681 utf8_display
= str_isutf8 (get_codepage_id( display_codepage
));
682 #endif /* HAVE_CHARSET */
685 #if defined(USE_VFS) && defined (USE_NETCODE)
691 buffer
= mc_config_get_string(mc_main_config
, "Misc", "ftpfs_password", "");
698 #endif /* USE_VFS && USE_NETCODE */
700 void done_setup (void)
702 g_free (profile_name
);
703 g_free (global_profile_name
);
704 g_free(color_terminal_string
);
705 g_free(term_color_string
);
706 g_free(setup_color_string
);
707 mc_config_deinit(mc_main_config
);
708 mc_config_deinit(mc_panels_config
);
711 /* directory_history_free (); */
715 load_keys_from_section (const char *terminal
, mc_config_t
*cfg
)
718 gchar
**profile_keys
, **keys
;
719 gchar
**values
, **curr_values
;
720 char *valcopy
, *value
;
722 gsize len
, values_len
;
727 section_name
= g_strconcat ("terminal:", terminal
, (char *) NULL
);
728 profile_keys
= keys
= mc_config_get_keys (cfg
, section_name
, &len
);
730 while (*profile_keys
){
732 /* copy=other causes all keys from [terminal:other] to be loaded. */
733 if (g_strcasecmp (*profile_keys
, "copy") == 0) {
734 valcopy
= mc_config_get_string (cfg
, section_name
, *profile_keys
, "");
735 load_keys_from_section (valcopy
, cfg
);
740 curr_values
= values
= mc_config_get_string_list (cfg
, section_name
, *profile_keys
, &values_len
);
742 key_code
= lookup_key (*profile_keys
);
745 while (*curr_values
){
746 valcopy
= convert_controls (*curr_values
);
747 define_sequence (key_code
, valcopy
, MCKEY_NOACTION
);
752 value
= mc_config_get_string (cfg
, section_name
, *profile_keys
, "");
753 valcopy
= convert_controls (value
);
754 define_sequence (key_code
, valcopy
, MCKEY_NOACTION
);
764 g_free (section_name
);
767 void load_key_defs (void)
770 * Load keys from mc.lib before ~/.mc/ini, so that the user
771 * definitions override global settings.
773 mc_config_t
*mc_global_config
;
775 mc_global_config
= mc_config_init(global_profile_name
);
776 if (mc_global_config
!= NULL
)
778 load_keys_from_section ("general", mc_global_config
);
779 load_keys_from_section (getenv ("TERM"), mc_global_config
);
780 mc_config_deinit(mc_global_config
);
782 load_keys_from_section ("general", mc_main_config
);
783 load_keys_from_section (getenv ("TERM"), mc_main_config
);