Merge commit '315633ecce31cd5af92112ab9903f6e7e8ae8df7'
[free-mc.git] / src / setup.c
blob026ac02d7593a15e85b3386d84aa8ab9de07d898
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 for (i = 0; sort_names [i].key; i++)
249 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
250 save_string (section, "sort_order",
251 sort_names [i].key, profile_name);
252 break;
255 for (i = 0; list_types [i].key; i++)
256 if (list_types [i].list_type == panel->list_type){
257 save_string (section, "list_mode", list_types [i].key, profile_name);
258 break;
261 save_string (section, "user_format",
262 panel->user_format, profile_name);
264 for (i = 0; i < LIST_TYPES; i++){
265 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
266 save_string (section, buffer,
267 panel->user_status_format [i], profile_name);
270 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
271 save_string (section, "user_mini_status", buffer,
272 profile_name);
275 void
276 save_layout (void)
278 char *profile;
279 int i;
280 char buffer [BUF_TINY];
282 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
284 /* Save integer options */
285 for (i = 0; layout [i].opt_name; i++){
286 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
287 save_string ("Layout", layout [i].opt_name, buffer, profile);
290 g_free (profile);
293 void
294 save_configure (void)
296 char *profile;
297 int i;
299 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
301 /* Save integer options */
302 for (i = 0; int_options[i].opt_name; i++)
303 set_int (profile, int_options[i].opt_name, *int_options[i].opt_addr);
305 /* Save string options */
306 for (i = 0; str_options[i].opt_name != NULL; i++)
307 set_config_string (profile, str_options[i].opt_name,
308 *str_options[i].opt_addr);
310 g_free (profile);
313 static void
314 panel_save_type (const char *section, int type)
316 int i;
318 for (i = 0; panel_types [i].opt_name; i++)
319 if (panel_types [i].opt_type == type){
320 save_string (section, "display", panel_types [i].opt_name,
321 profile_name);
322 break;
326 static void
327 save_panel_types (void)
329 int type;
331 type = get_display_type (0);
332 panel_save_type ("New Left Panel", type);
333 if (type == view_listing)
334 panel_save_setup (left_panel, left_panel->panel_name);
335 type = get_display_type (1);
336 panel_save_type ("New Right Panel", type);
337 if (type == view_listing)
338 panel_save_setup (right_panel, right_panel->panel_name);
341 void
342 save_setup (void)
344 char *profile;
346 saving_setup = 1;
347 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
349 save_configure ();
351 save_layout ();
352 save_string ("Dirs", "other_dir",
353 get_other_type () == view_listing
354 ? other_panel->cwd : ".", profile);
355 if (current_panel != NULL)
356 WritePrivateProfileString ("Dirs", "current_is_left",
357 get_current_index () == 0 ? "1" : "0", profile);
358 save_hotlist ();
360 save_panelize ();
361 save_panel_types ();
362 /* directory_history_save (); */
364 #if defined(USE_VFS) && defined (USE_NETCODE)
365 WritePrivateProfileString ("Misc", "ftpfs_password",
366 ftpfs_anonymous_passwd, profile);
367 if (ftpfs_proxy_host)
368 WritePrivateProfileString ("Misc", "ftp_proxy_host",
369 ftpfs_proxy_host, profile);
370 #endif /* USE_VFS && USE_NETCODE */
372 #ifdef HAVE_CHARSET
373 save_string( "Misc", "display_codepage",
374 get_codepage_id( display_codepage ), profile_name );
375 #endif /* HAVE_CHARSET */
377 g_free (profile);
378 saving_setup = 0;
381 void
382 panel_load_setup (WPanel *panel, const char *section)
384 int i;
385 char buffer [BUF_TINY];
387 panel->reverse = load_int (section, "reverse", 0);
388 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
390 /* Load sort order */
391 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
392 panel->sort_type = (sortfn *) sort_name;
393 for (i = 0; sort_names [i].key; i++)
394 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
395 panel->sort_type = sort_names [i].sort_type;
396 break;
399 /* Load the listing mode */
400 load_string (section, "list_mode", "full", buffer, sizeof (buffer));
401 panel->list_type = list_full;
402 for (i = 0; list_types [i].key; i++)
403 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
404 panel->list_type = list_types [i].list_type;
405 break;
408 /* User formats */
409 g_free (panel->user_format);
410 panel->user_format = g_strdup (get_profile_string (section, "user_format",
411 DEFAULT_USER_FORMAT,
412 profile_name));
413 for (i = 0; i < LIST_TYPES; i++){
414 g_free (panel->user_status_format [i]);
415 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
416 panel->user_status_format [i] =
417 g_strdup (get_profile_string (section, buffer,
418 DEFAULT_USER_FORMAT, profile_name));
421 panel->user_mini_status =
422 load_int (section, "user_mini_status", 0);
426 static void
427 load_layout (char *profile_name)
429 int i;
431 for (i = 0; layout [i].opt_name; i++)
432 *layout [i].opt_addr =
433 load_int ("Layout", layout [i].opt_name,
434 *layout [i].opt_addr);
437 static int
438 load_mode (const char *section)
440 char buffer [20];
441 int i;
443 int mode = view_listing;
445 /* Load the display mode */
446 load_string (section, "display", "listing", buffer, sizeof (buffer));
448 for (i = 0; panel_types [i].opt_name; i++)
449 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
450 mode = panel_types [i].opt_type;
451 break;
454 return mode;
457 #ifdef USE_NETCODE
458 static char *
459 do_load_string (const char *s, const char *ss, const char *def)
461 char *buffer = g_malloc (BUF_SMALL);
462 char *p;
464 load_string (s, ss, def, buffer, BUF_SMALL);
466 p = g_strdup (buffer);
467 g_free (buffer);
468 return p;
470 #endif /* !USE_NETCODE */
472 char *
473 setup_init (void)
475 char *profile;
476 char *inifile;
478 if (profile_name)
479 return profile_name;
481 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
482 if (!exist_file (profile)){
483 inifile = concat_dir_and_file (mc_home, "mc.ini");
484 if (exist_file (inifile)){
485 g_free (profile);
486 profile = inifile;
487 } else
488 g_free (inifile);
491 profile_name = profile;
493 return profile;
496 void
497 load_setup (void)
499 char *profile;
500 int i;
502 profile = setup_init ();
504 /* mc.lib is common for all users, but has priority lower than
505 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
506 global_profile_name = concat_dir_and_file (mc_home, "mc.lib");
508 /* Load integer boolean options */
509 for (i = 0; int_options[i].opt_name; i++)
510 *int_options[i].opt_addr =
511 get_int (profile, int_options[i].opt_name, *int_options[i].opt_addr);
513 /* Load string options */
514 for (i = 0; str_options[i].opt_name != NULL; i++)
515 *str_options[i].opt_addr = get_config_string (profile,
516 str_options[i].opt_name, str_options[i].opt_defval);
518 load_layout (profile);
520 load_panelize ();
522 startup_left_mode = load_mode ("New Left Panel");
523 startup_right_mode = load_mode ("New Right Panel");
525 /* At least one of the panels is a listing panel */
526 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
527 startup_left_mode = view_listing;
529 if (!other_dir){
530 char *buffer;
532 buffer = (char*) g_malloc (MC_MAXPATHLEN);
533 load_string ("Dirs", "other_dir", ".", buffer,
534 MC_MAXPATHLEN);
535 if (vfs_file_is_local (buffer))
536 other_dir = buffer;
537 else
538 g_free (buffer);
541 boot_current_is_left =
542 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
544 #ifdef USE_NETCODE
545 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
546 #endif
548 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
549 sizeof (setup_color_string));
550 if (setup_color_string [0])
551 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", (char *) NULL);
553 /* The default color and the terminal dependent color */
554 load_string ("Colors", "base_color", "", setup_color_string,
555 sizeof (setup_color_string));
556 load_string ("Colors", getenv ("TERM"), "",
557 term_color_string, sizeof (term_color_string));
558 load_string ("Colors", "color_terminals", "",
559 color_terminal_string, sizeof (color_terminal_string));
561 /* Load the directory history */
562 /* directory_history_load (); */
563 /* Remove the temporal entries */
564 profile_clean_section ("Temporal:New Left Panel", profile_name);
565 profile_clean_section ("Temporal:New Right Panel", profile_name);
566 #if defined(USE_VFS) && defined (USE_NETCODE)
567 ftpfs_init_passwd ();
568 #endif /* USE_VFS && USE_NETCODE */
570 #ifdef HAVE_CHARSET
571 if ( load_codepages_list() > 0 ) {
572 char cpname[128];
573 load_string( "Misc", "display_codepage", "",
574 cpname, sizeof(cpname) );
575 if ( cpname[0] != '\0' )
576 display_codepage = get_codepage_index( cpname );
579 init_translation_table( source_codepage, display_codepage );
580 #endif /* HAVE_CHARSET */
583 #if defined(USE_VFS) && defined (USE_NETCODE)
584 char *
585 load_anon_passwd ()
587 char buffer [255];
589 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
590 if (buffer [0])
591 return g_strdup (buffer);
592 else
593 return 0;
595 #endif /* USE_VFS && USE_NETCODE */
597 void done_setup (void)
599 g_free (profile_name);
600 g_free (global_profile_name);
601 done_hotlist ();
602 done_panelize ();
603 /* directory_history_free (); */
606 static void
607 load_keys_from_section (const char *terminal, const char *profile_name)
609 char *section_name;
610 void *profile_keys;
611 char *key, *value, *valcopy;
612 int key_code;
614 if (!terminal)
615 return;
617 section_name = g_strconcat ("terminal:", terminal, (char *) NULL);
618 profile_keys = profile_init_iterator (section_name, profile_name);
619 g_free (section_name);
620 while (profile_keys){
621 profile_keys = profile_iterator_next (profile_keys, &key, &value);
623 /* copy=other causes all keys from [terminal:other] to be loaded. */
624 if (g_strcasecmp (key, "copy") == 0) {
625 load_keys_from_section (value, profile_name);
626 continue;
629 key_code = lookup_key (key);
630 if (key_code){
631 valcopy = convert_controls (value);
632 define_sequence (key_code, valcopy, MCKEY_NOACTION);
633 g_free (valcopy);
638 void load_key_defs (void)
641 * Load keys from mc.lib before ~/.mc/ini, so that the user
642 * definitions override global settings.
644 load_keys_from_section ("general", global_profile_name);
645 load_keys_from_section (getenv ("TERM"), global_profile_name);
646 load_keys_from_section ("general", profile_name);
647 load_keys_from_section (getenv ("TERM"), profile_name);
649 /* We don't want a huge database loaded in core */
650 free_profile_name (global_profile_name);