Original patch as attached on the bugreport
[midnight-commander.git] / src / setup.c
blobef4249692067865134cbc7bc09f1328c95e0b185
1 /* Setup loading/saving.
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2006, 2007 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 #include <config.h>
21 #include <string.h>
22 #include <stdio.h>
24 #include <sys/types.h>
25 #include <sys/stat.h>
27 #include "global.h"
28 #include "tty.h"
29 #include "dir.h"
30 #include "panel.h"
31 #include "main.h"
32 #include "tree.h" /* xtree_mode */
33 #include "profile.h"
34 #include "setup.h"
35 #include "mouse.h" /* To make view.h happy */
36 #include "view.h" /* For the externs */
37 #include "key.h" /* For the externs */
38 #include "hotlist.h" /* load/save/done hotlist */
39 #include "panelize.h" /* load/save/done panelize */
40 #include "layout.h"
41 #include "menu.h" /* menubar_visible declaration */
42 #include "win.h" /* lookup_key */
43 #include "cmd.h"
44 #include "file.h" /* safe_delete */
46 #ifdef USE_VFS
47 #include "../vfs/gc.h"
48 #endif
50 #ifdef HAVE_CHARSET
51 #include "charsets.h"
52 #endif
54 #ifdef USE_NETCODE
55 # include "../vfs/ftpfs.h"
56 # include "../vfs/fish.h"
57 #endif
59 #ifdef USE_INTERNAL_EDIT
60 # include "../edit/edit.h"
61 #endif
64 extern char *find_ignore_dirs;
66 extern int num_history_items_recorded;
68 char *profile_name; /* .mc/ini */
69 char *global_profile_name; /* mc.lib */
71 char setup_color_string [4096];
72 char term_color_string [4096];
73 char color_terminal_string [512];
75 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
76 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
77 #define save_string WritePrivateProfileString
79 int startup_left_mode;
80 int startup_right_mode;
82 /* Ugly hack to allow panel_save_setup to work as a place holder for */
83 /* default panel values */
84 int saving_setup;
86 static const struct {
87 const char *key;
88 sortfn *sort_type;
89 } sort_names [] = {
90 { "name", (sortfn *) sort_name },
91 { "extension", (sortfn *) sort_ext },
92 { "time", (sortfn *) sort_time },
93 { "atime", (sortfn *) sort_atime },
94 { "ctime", (sortfn *) sort_ctime },
95 { "size", (sortfn *) sort_size },
96 { "inode", (sortfn *) sort_inode },
97 { "unsorted", (sortfn *) unsorted },
98 { 0, 0 }
101 static const struct {
102 const char *key;
103 int list_type;
104 } list_types [] = {
105 { "full", list_full },
106 { "brief", list_brief },
107 { "long", list_long },
108 { "user", list_user },
109 { 0, 0 }
112 static const struct {
113 const char *opt_name;
114 int opt_type;
115 } panel_types [] = {
116 { "listing", view_listing },
117 { "quickview", view_quick },
118 { "info", view_info },
119 { "tree", view_tree },
120 { 0, 0 }
123 static const struct {
124 const char *opt_name;
125 int *opt_addr;
126 } layout [] = {
127 { "equal_split", &equal_split },
128 { "first_panel_size", &first_panel_size },
129 { "message_visible", &message_visible },
130 { "keybar_visible", &keybar_visible },
131 { "xterm_title", &xterm_title },
132 { "output_lines", &output_lines },
133 { "command_prompt", &command_prompt },
134 { "menubar_visible", &menubar_visible },
135 { "show_mini_info", &show_mini_info },
136 { "permission_mode", &permission_mode },
137 { "filetype_mode", &filetype_mode },
138 { 0, 0 }
141 static const struct {
142 const char *opt_name;
143 int *opt_addr;
144 } int_options [] = {
145 { "show_backups", &show_backups },
146 { "show_dot_files", &show_dot_files },
147 { "verbose", &verbose },
148 { "mark_moves_down", &mark_moves_down },
149 { "pause_after_run", &pause_after_run },
150 { "shell_patterns", &easy_patterns },
151 { "auto_save_setup", &auto_save_setup },
152 { "auto_menu", &auto_menu },
153 { "use_internal_view", &use_internal_view },
154 { "use_internal_edit", &use_internal_edit },
155 { "clear_before_exec", &clear_before_exec },
156 { "mix_all_files", &mix_all_files },
157 { "fast_reload", &fast_reload },
158 { "fast_reload_msg_shown", &fast_reload_w },
159 { "confirm_delete", &confirm_delete },
160 { "confirm_overwrite", &confirm_overwrite },
161 { "confirm_execute", &confirm_execute },
162 { "confirm_exit", &confirm_exit },
163 { "confirm_directory_hotlist_delete", &confirm_directory_hotlist_delete },
164 { "safe_delete", &safe_delete },
165 { "mouse_repeat_rate", &mou_auto_repeat },
166 { "double_click_speed", &double_click_speed },
167 #ifndef HAVE_CHARSET
168 { "eight_bit_clean", &eight_bit_clean },
169 { "full_eight_bits", &full_eight_bits },
170 #endif /* !HAVE_CHARSET */
171 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
172 { "confirm_view_dir", &confirm_view_dir },
173 { "mouse_move_pages", &mouse_move_pages },
174 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
175 { "fast_refresh", &fast_refresh },
176 { "navigate_with_arrows", &navigate_with_arrows },
177 { "drop_menus", &drop_menus },
178 { "wrap_mode", &global_wrap_mode},
179 { "old_esc_mode", &old_esc_mode },
180 { "cd_symlinks", &cd_symlinks },
181 { "show_all_if_ambiguous", &show_all_if_ambiguous },
182 { "max_dirt_limit", &max_dirt_limit },
183 { "torben_fj_mode", &torben_fj_mode },
184 { "use_file_to_guess_type", &use_file_to_check_type },
185 { "alternate_plus_minus", &alternate_plus_minus },
186 { "only_leading_plus_minus", &only_leading_plus_minus },
187 { "show_output_starts_shell", &output_starts_shell },
188 { "panel_scroll_pages", &panel_scroll_pages },
189 { "xtree_mode", &xtree_mode },
190 { "num_history_items_recorded", &num_history_items_recorded },
191 { "file_op_compute_totals", &file_op_compute_totals },
192 #ifdef USE_VFS
193 { "vfs_timeout", &vfs_timeout },
194 #ifdef USE_NETCODE
195 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
196 { "use_netrc", &use_netrc },
197 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
198 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
199 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
200 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
201 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
202 { "fish_directory_timeout", &fish_directory_timeout },
203 #endif /* USE_NETCODE */
204 #endif /* USE_VFS */
205 #ifdef USE_INTERNAL_EDIT
206 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
207 { "editor_key_emulation", &edit_key_emulation },
208 { "editor_tab_spacing", &option_tab_spacing },
209 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
210 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
211 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
212 { "editor_fake_half_tabs", &option_fake_half_tabs },
213 { "editor_option_save_mode", &option_save_mode },
214 { "editor_option_save_position", &option_save_position },
215 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
216 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
217 { "editor_edit_confirm_save", &edit_confirm_save },
218 { "editor_syntax_highlighting", &option_syntax_highlighting },
219 #endif /* USE_INTERNAL_EDIT */
221 { "nice_rotating_dash", &nice_rotating_dash },
222 { "horizontal_split", &horizontal_split },
223 { "mcview_remember_file_position", &mcview_remember_file_position },
224 { 0, 0 }
227 static const struct {
228 const char *opt_name;
229 char **opt_addr;
230 const char *opt_defval;
231 } str_options [] = {
232 #ifdef USE_INTERNAL_EDIT
233 { "editor_backup_extension", &option_backup_ext, "~" },
234 #endif
235 { NULL, NULL }
238 void
239 panel_save_setup (struct WPanel *panel, const char *section)
241 char buffer [BUF_TINY];
242 int i;
244 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
245 save_string (section, "reverse", buffer, profile_name);
246 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
247 save_string (section, "case_sensitive", buffer, profile_name);
248 g_snprintf (buffer, sizeof (buffer), "%d", panel->exec_first);
249 save_string (section, "exec_first", buffer, profile_name);
250 for (i = 0; sort_names [i].key; i++)
251 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
252 save_string (section, "sort_order",
253 sort_names [i].key, profile_name);
254 break;
257 for (i = 0; list_types [i].key; i++)
258 if (list_types [i].list_type == panel->list_type){
259 save_string (section, "list_mode", list_types [i].key, profile_name);
260 break;
263 save_string (section, "user_format",
264 panel->user_format, profile_name);
266 for (i = 0; i < LIST_TYPES; i++){
267 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
268 save_string (section, buffer,
269 panel->user_status_format [i], profile_name);
272 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
273 save_string (section, "user_mini_status", buffer,
274 profile_name);
277 void
278 save_layout (void)
280 char *profile;
281 int i;
282 char buffer [BUF_TINY];
284 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
286 /* Save integer options */
287 for (i = 0; layout [i].opt_name; i++){
288 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
289 save_string ("Layout", layout [i].opt_name, buffer, profile);
292 g_free (profile);
295 void
296 save_configure (void)
298 char *profile;
299 int i;
301 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
303 /* Save integer options */
304 for (i = 0; int_options[i].opt_name; i++)
305 set_int (profile, int_options[i].opt_name, *int_options[i].opt_addr);
307 /* Save string options */
308 for (i = 0; str_options[i].opt_name != NULL; i++)
309 set_config_string (profile, str_options[i].opt_name,
310 *str_options[i].opt_addr);
312 g_free (profile);
315 static void
316 panel_save_type (const char *section, int type)
318 int i;
320 for (i = 0; panel_types [i].opt_name; i++)
321 if (panel_types [i].opt_type == type){
322 save_string (section, "display", panel_types [i].opt_name,
323 profile_name);
324 break;
328 static void
329 save_panel_types (void)
331 int type;
333 type = get_display_type (0);
334 panel_save_type ("New Left Panel", type);
335 if (type == view_listing)
336 panel_save_setup (left_panel, left_panel->panel_name);
337 type = get_display_type (1);
338 panel_save_type ("New Right Panel", type);
339 if (type == view_listing)
340 panel_save_setup (right_panel, right_panel->panel_name);
343 void
344 save_setup (void)
346 char *profile;
348 saving_setup = 1;
349 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
351 save_configure ();
353 save_layout ();
354 save_string ("Dirs", "other_dir",
355 get_other_type () == view_listing
356 ? other_panel->cwd : ".", profile);
357 if (current_panel != NULL)
358 WritePrivateProfileString ("Dirs", "current_is_left",
359 get_current_index () == 0 ? "1" : "0", profile);
360 save_hotlist ();
362 save_panelize ();
363 save_panel_types ();
364 /* directory_history_save (); */
366 #if defined(USE_VFS) && defined (USE_NETCODE)
367 WritePrivateProfileString ("Misc", "ftpfs_password",
368 ftpfs_anonymous_passwd, profile);
369 if (ftpfs_proxy_host)
370 WritePrivateProfileString ("Misc", "ftp_proxy_host",
371 ftpfs_proxy_host, profile);
372 #endif /* USE_VFS && USE_NETCODE */
374 #ifdef HAVE_CHARSET
375 save_string( "Misc", "display_codepage",
376 get_codepage_id( display_codepage ), profile_name );
377 #endif /* HAVE_CHARSET */
379 g_free (profile);
380 saving_setup = 0;
383 void
384 panel_load_setup (WPanel *panel, const char *section)
386 int i;
387 char buffer [BUF_TINY];
389 panel->reverse = load_int (section, "reverse", 0);
390 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
391 panel->exec_first = load_int (section, "exec_first", 0);
393 /* Load sort order */
394 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
395 panel->sort_type = (sortfn *) sort_name;
396 for (i = 0; sort_names [i].key; i++)
397 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
398 panel->sort_type = sort_names [i].sort_type;
399 break;
402 /* Load the listing mode */
403 load_string (section, "list_mode", "full", buffer, sizeof (buffer));
404 panel->list_type = list_full;
405 for (i = 0; list_types [i].key; i++)
406 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
407 panel->list_type = list_types [i].list_type;
408 break;
411 /* User formats */
412 g_free (panel->user_format);
413 panel->user_format = g_strdup (get_profile_string (section, "user_format",
414 DEFAULT_USER_FORMAT,
415 profile_name));
416 for (i = 0; i < LIST_TYPES; i++){
417 g_free (panel->user_status_format [i]);
418 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
419 panel->user_status_format [i] =
420 g_strdup (get_profile_string (section, buffer,
421 DEFAULT_USER_FORMAT, profile_name));
424 panel->user_mini_status =
425 load_int (section, "user_mini_status", 0);
429 static void
430 load_layout (char *profile_name)
432 int i;
434 for (i = 0; layout [i].opt_name; i++)
435 *layout [i].opt_addr =
436 load_int ("Layout", layout [i].opt_name,
437 *layout [i].opt_addr);
440 static int
441 load_mode (const char *section)
443 char buffer [20];
444 int i;
446 int mode = view_listing;
448 /* Load the display mode */
449 load_string (section, "display", "listing", buffer, sizeof (buffer));
451 for (i = 0; panel_types [i].opt_name; i++)
452 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
453 mode = panel_types [i].opt_type;
454 break;
457 return mode;
460 #ifdef USE_NETCODE
461 static char *
462 do_load_string (const char *s, const char *ss, const char *def)
464 char *buffer = g_malloc (BUF_SMALL);
465 char *p;
467 load_string (s, ss, def, buffer, BUF_SMALL);
469 p = g_strdup (buffer);
470 g_free (buffer);
471 return p;
473 #endif /* !USE_NETCODE */
475 char *
476 setup_init (void)
478 char *profile;
479 char *inifile;
481 if (profile_name)
482 return profile_name;
484 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
485 if (!exist_file (profile)){
486 inifile = concat_dir_and_file (mc_home, "mc.ini");
487 if (exist_file (inifile)){
488 g_free (profile);
489 profile = inifile;
490 } else
491 g_free (inifile);
494 profile_name = profile;
496 return profile;
499 void
500 load_setup (void)
502 char *profile;
503 int i;
505 profile = setup_init ();
507 /* mc.lib is common for all users, but has priority lower than
508 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
509 global_profile_name = concat_dir_and_file (mc_home, "mc.lib");
511 /* Load integer boolean options */
512 for (i = 0; int_options[i].opt_name; i++)
513 *int_options[i].opt_addr =
514 get_int (profile, int_options[i].opt_name, *int_options[i].opt_addr);
516 /* Load string options */
517 for (i = 0; str_options[i].opt_name != NULL; i++)
518 *str_options[i].opt_addr = get_config_string (profile,
519 str_options[i].opt_name, str_options[i].opt_defval);
521 load_layout (profile);
523 load_panelize ();
525 startup_left_mode = load_mode ("New Left Panel");
526 startup_right_mode = load_mode ("New Right Panel");
528 /* At least one of the panels is a listing panel */
529 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
530 startup_left_mode = view_listing;
532 if (!other_dir){
533 char *buffer;
535 buffer = (char*) g_malloc (MC_MAXPATHLEN);
536 load_string ("Dirs", "other_dir", ".", buffer,
537 MC_MAXPATHLEN);
538 if (vfs_file_is_local (buffer))
539 other_dir = buffer;
540 else
541 g_free (buffer);
544 boot_current_is_left =
545 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
547 #ifdef USE_NETCODE
548 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
549 #endif
551 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
552 sizeof (setup_color_string));
553 if (setup_color_string [0])
554 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", (char *) NULL);
556 /* The default color and the terminal dependent color */
557 load_string ("Colors", "base_color", "", setup_color_string,
558 sizeof (setup_color_string));
559 load_string ("Colors", getenv ("TERM"), "",
560 term_color_string, sizeof (term_color_string));
561 load_string ("Colors", "color_terminals", "",
562 color_terminal_string, sizeof (color_terminal_string));
564 /* Load the directory history */
565 /* directory_history_load (); */
566 /* Remove the temporal entries */
567 profile_clean_section ("Temporal:New Left Panel", profile_name);
568 profile_clean_section ("Temporal:New Right Panel", profile_name);
569 #if defined(USE_VFS) && defined (USE_NETCODE)
570 ftpfs_init_passwd ();
571 #endif /* USE_VFS && USE_NETCODE */
573 #ifdef HAVE_CHARSET
574 if ( load_codepages_list() > 0 ) {
575 char cpname[128];
576 load_string( "Misc", "display_codepage", "",
577 cpname, sizeof(cpname) );
578 if ( cpname[0] != '\0' )
579 display_codepage = get_codepage_index( cpname );
582 init_translation_table( source_codepage, display_codepage );
583 #endif /* HAVE_CHARSET */
586 #if defined(USE_VFS) && defined (USE_NETCODE)
587 char *
588 load_anon_passwd ()
590 char buffer [255];
592 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
593 if (buffer [0])
594 return g_strdup (buffer);
595 else
596 return 0;
598 #endif /* USE_VFS && USE_NETCODE */
600 void done_setup (void)
602 g_free (profile_name);
603 g_free (global_profile_name);
604 done_hotlist ();
605 done_panelize ();
606 /* directory_history_free (); */
609 static void
610 load_keys_from_section (const char *terminal, const char *profile_name)
612 char *section_name;
613 void *profile_keys;
614 char *key, *value, *valcopy;
615 int key_code;
617 if (!terminal)
618 return;
620 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
621 profile_keys = profile_init_iterator (section_name, profile_name);
622 g_free (section_name);
623 while (profile_keys){
624 profile_keys = profile_iterator_next (profile_keys, &key, &value);
626 /* copy=other causes all keys from [terminal:other] to be loaded. */
627 if (g_strcasecmp (key, "copy") == 0) {
628 load_keys_from_section (value, profile_name);
629 continue;
632 key_code = lookup_key (key);
633 if (key_code){
634 valcopy = convert_controls (value);
635 define_sequence (key_code, valcopy, MCKEY_NOACTION);
636 g_free (valcopy);
641 void load_key_defs (void)
644 * Load keys from mc.lib before ~/.mc/ini, so that the user
645 * definitions override global settings.
647 load_keys_from_section ("general", global_profile_name);
648 load_keys_from_section (getenv ("TERM"), global_profile_name);
649 load_keys_from_section ("general", profile_name);
650 load_keys_from_section (getenv ("TERM"), profile_name);
652 /* We don't want a huge database loaded in core */
653 free_profile_name (global_profile_name);