Corrected some in-line help strings...
[midnight-commander.git] / src / setup.c
bloba89eff2f8d047d92d10d30f706752dc7cd9ddd26
1 /* Setup loading/saving.
2 Copyright (C) 1994 Miguel de Icaza
4 This program is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 2 of the License, or
7 (at your option) any later version.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. */
18 #include <config.h>
19 #include <sys/types.h> /* Needed to include local .h files */
20 #include <sys/stat.h>
21 #include <string.h>
22 #include <stdio.h>
24 #include "global.h"
25 #include "tty.h"
26 #include "dir.h"
27 #include "panel.h"
28 #include "main.h"
29 #include "tree.h"
30 #include "profile.h"
31 #define WANT_WIDGETS
32 #include "setup.h"
33 #include "mouse.h" /* To make view.h happy */
34 #include "view.h" /* For the externs */
35 #include "key.h" /* For the externs */
36 #include "hotlist.h" /* load/save/done hotlist */
37 #include "panelize.h" /* load/save/done panelize */
38 #include "layout.h"
39 #include "menu.h" /* menubar_visible declaration */
40 #include "win.h" /* lookup_key */
41 #include "cmd.h"
42 #include "file.h"
44 #ifndef PORT_LIST_MODE_NAME
45 # define PORT_LIST_MODE_NAME "list_mode"
46 #endif
48 #ifndef PORT_LIST_MODE_DEFAULT
49 # define PORT_LIST_MODE_DEFAULT "full"
50 #endif
52 #ifdef HAVE_CHARSET
53 #include "charsets.h"
54 #endif
56 #include "../vfs/vfs.h"
57 #ifdef USE_NETCODE
58 # include "../vfs/ftpfs.h"
59 #endif
61 /* "$Id$" */
63 #ifdef USE_VFS
64 extern int vfs_timeout;
65 extern int tar_gzipped_memlimit;
66 #endif
68 extern char *find_ignore_dirs;
70 extern int num_history_items_recorded;
72 char *profile_name;
74 char setup_color_string [4096];
75 char term_color_string [4096];
76 char color_terminal_string [512];
78 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
79 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
80 #define save_string WritePrivateProfileString
82 int startup_left_mode;
83 int startup_right_mode;
85 /* Ugly hack to allow panel_save_setup to work as a place holder for */
86 /* default panel values */
87 int saving_setup;
89 static const struct {
90 char *key;
91 sortfn *sort_type;
92 } sort_names [] = {
93 { "name", (sortfn *) sort_name },
94 { "extension", (sortfn *) sort_ext },
95 { "time", (sortfn *) sort_time },
96 { "atime", (sortfn *) sort_atime },
97 { "ctime", (sortfn *) sort_ctime },
98 { "size", (sortfn *) sort_size },
99 { "inode", (sortfn *) sort_inode },
100 { "unsorted", (sortfn *) unsorted },
101 { 0, 0 }
104 static const struct {
105 char *key;
106 int list_type;
107 } list_types [] = {
108 { "full", list_full },
109 { "brief", list_brief },
110 { "long", list_long },
111 { "user", list_user },
112 { "icons", list_icons },
113 { 0, 0 }
116 static const struct {
117 char *opt_name;
118 int opt_type;
119 } panel_types [] = {
120 { "listing", view_listing },
121 { "quickview", view_quick },
122 { "info", view_info },
123 { "tree", view_tree },
124 { 0, 0 }
127 static const struct {
128 char *opt_name;
129 int *opt_addr;
130 } layout [] = {
131 { "equal_split", &equal_split },
132 { "first_panel_size", &first_panel_size },
133 { "message_visible", &message_visible },
134 { "keybar_visible", &keybar_visible },
135 { "xterm_hintbar", &xterm_hintbar },
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 { 0, 0 }
146 #undef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
148 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
149 extern int preserve_uidgid;
150 #endif
152 static const struct {
153 char *opt_name;
154 int *opt_addr;
155 } options [] = {
156 { "show_backups", &show_backups },
157 { "show_dot_files", &show_dot_files },
158 { "verbose", &verbose },
159 { "mark_moves_down", &mark_moves_down },
160 { "pause_after_run", &pause_after_run },
161 { "shell_patterns", &easy_patterns },
162 { "auto_save_setup", &auto_save_setup },
163 { "auto_menu", &auto_menu },
164 { "use_internal_view", &use_internal_view },
165 { "use_internal_edit", &use_internal_edit },
166 { "clear_before_exec", &clear_before_exec },
167 { "mix_all_files", &mix_all_files },
168 { "fast_reload", &fast_reload },
169 { "fast_reload_msg_shown", &fast_reload_w },
170 { "confirm_delete", &confirm_delete },
171 { "confirm_overwrite", &confirm_overwrite },
172 { "confirm_execute", &confirm_execute },
173 { "confirm_exit", &confirm_exit },
174 { "safe_delete", &know_not_what_am_i_doing },
175 { "mouse_repeat_rate", &mou_auto_repeat },
176 { "double_click_speed", &double_click_speed },
177 #ifndef HAVE_CHARSET
178 { "eight_bit_clean", &eight_bit_clean },
179 { "full_eight_bits", &full_eight_bits },
180 #endif /* !HAVE_CHARSET */
181 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
182 { "confirm_view_dir", &confirm_view_dir },
183 { "mouse_move_pages", &mouse_move_pages },
184 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
185 { "fast_refresh", &fast_refresh },
186 { "navigate_with_arrows", &navigate_with_arrows },
187 { "advanced_chown", &advanced_chfns },
188 { "drop_menus", &drop_menus },
189 { "wrap_mode", &global_wrap_mode},
190 { "old_esc_mode", &old_esc_mode },
191 { "cd_symlinks", &cd_symlinks },
192 { "show_all_if_ambiguous", &show_all_if_ambiguous },
193 { "have_fast_cpu", &have_fast_cpu },
194 { "torben_fj_mode", &torben_fj_mode },
195 { "use_file_to_guess_type", &use_file_to_check_type },
196 { "alternate_plus_minus", &alternate_plus_minus },
197 { "only_leading_plus_minus", &only_leading_plus_minus },
198 { "show_output_starts_shell", &output_starts_shell },
199 { "panel_scroll_pages", &panel_scroll_pages },
200 { "xtree_mode", &xtree_mode },
201 { "num_history_items_recorded", &num_history_items_recorded },
202 { "file_op_compute_totals", &file_op_compute_totals },
203 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
204 { "dive_into_subdirs", &dive_into_subdirs },
205 { "preserve_uidgid", &preserve_uidgid },
206 /* What about the other two options in the copy dialog
207 (follow links, stable symlinks) -Norbert */
208 { "tree_navigation_flag", &tree_navigation_flag },
209 #endif /* SAVE_CHANGES_OUTSIDE_OPTIONS_MENU */
210 #ifdef USE_VFS
211 { "vfs_timeout", &vfs_timeout },
212 #ifdef USE_NETCODE
213 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
214 { "use_netrc", &use_netrc },
215 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
216 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
217 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
218 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
219 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
220 #endif /* USE_NETCODE */
221 #endif /* USE_VFS */
222 #ifdef USE_INTERNAL_EDIT
223 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
224 { "editor_key_emulation", &edit_key_emulation },
225 { "editor_tab_spacing", &option_tab_spacing },
226 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
227 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
228 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
229 { "editor_fake_half_tabs", &option_fake_half_tabs },
230 { "editor_option_save_mode", &option_save_mode },
231 { "editor_option_backup_ext_int", &option_backup_ext_int },
232 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
233 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
234 { "editor_edit_confirm_save", &edit_confirm_save },
235 { "editor_syntax_highlighting", &option_syntax_highlighting },
236 #endif /* USE_INTERNAL_EDIT */
238 { "nice_rotating_dash", &nice_rotating_dash },
239 { "horizontal_split", &horizontal_split },
240 { 0, 0 }
243 void
244 panel_save_setup (WPanel *panel, char *section)
246 char buffer [BUF_TINY];
247 int i;
249 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
250 save_string (section, "reverse", buffer, profile_name);
251 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
252 save_string (section, "case_sensitive", buffer, profile_name);
253 for (i = 0; sort_names [i].key; i++)
254 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
255 save_string (section, "sort_order",
256 sort_names [i].key, profile_name);
257 break;
260 for (i = 0; list_types [i].key; i++)
261 if (list_types [i].list_type == panel->list_type){
262 save_string (section, PORT_LIST_MODE_NAME, list_types [i].key, profile_name);
263 break;
266 save_string (section, "user_format",
267 panel->user_format, profile_name);
269 for (i = 0; i < LIST_TYPES; i++){
270 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
271 save_string (section, buffer,
272 panel->user_status_format [i], profile_name);
275 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
276 save_string (section, "user_mini_status", buffer,
277 profile_name);
280 void
281 save_layout (void)
283 char *profile;
284 int i;
285 char buffer [BUF_TINY];
287 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
289 /* Save integer options */
290 for (i = 0; layout [i].opt_name; i++){
291 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
292 save_string ("Layout", layout [i].opt_name, buffer, profile);
295 g_free (profile);
298 void
299 save_configure (void)
301 char *profile;
302 int i;
304 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
306 /* Save integer options */
307 for (i = 0; options [i].opt_name; i++)
308 set_int (profile, options [i].opt_name, *options [i].opt_addr);
310 g_free (profile);
313 static void
314 panel_save_type (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 void
327 save_panel_types ()
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 ? opanel->cwd : ".", profile);
355 if (get_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, 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, PORT_LIST_MODE_NAME, PORT_LIST_MODE_DEFAULT, 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 if (panel->list_type == list_icons)
409 panel->list_type = list_full;
411 /* User formats */
412 if (panel->user_format){
413 g_free (panel->user_format);
414 panel->user_format = 0;
416 panel->user_format = g_strdup (get_profile_string (section, "user_format",
417 DEFAULT_USER_FORMAT,
418 profile_name));
419 for (i = 0; i < LIST_TYPES; i++){
420 if (panel->user_status_format [i])
421 g_free (panel->user_status_format [i]);
422 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
423 panel->user_status_format [i] =
424 g_strdup (get_profile_string (section, buffer,
425 DEFAULT_USER_FORMAT, profile_name));
428 panel->user_mini_status =
429 load_int (section, "user_mini_status", 0);
433 static void
434 load_layout (char *profile_name)
436 int i;
438 for (i = 0; layout [i].opt_name; i++)
439 *layout [i].opt_addr =
440 load_int ("Layout", layout [i].opt_name,
441 *layout [i].opt_addr);
444 static int
445 load_mode (char *section)
447 char buffer [20];
448 int i;
450 int mode = view_listing;
452 /* Load the display mode */
453 load_string (section, "display", "listing", buffer, sizeof (buffer));
455 for (i = 0; panel_types [i].opt_name; i++)
456 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
457 mode = panel_types [i].opt_type;
458 break;
461 return mode;
464 #ifdef USE_NETCODE
465 static char *
466 do_load_string (char *s, char *ss, char *def)
468 char *buffer = g_malloc (BUF_SMALL);
469 char *p;
471 load_string (s, ss, def, buffer, BUF_SMALL);
473 p = g_strdup (buffer);
474 g_free (buffer);
475 return p;
477 #endif /* !USE_NETCODE */
479 char *
480 setup_init (void)
482 char *profile;
483 char *inifile;
485 if (profile_name)
486 return profile_name;
488 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
489 if (!exist_file (profile)){
490 inifile = concat_dir_and_file (mc_home, "mc.ini");
491 if (exist_file (inifile)){
492 g_free (profile);
493 profile = inifile;
494 } else
495 g_free (inifile);
498 profile_name = profile;
500 return profile;
503 void
504 load_setup (void)
506 char *profile;
507 int i;
509 profile = setup_init ();
510 /* Load integer boolean options */
511 for (i = 0; options [i].opt_name; i++)
512 *options [i].opt_addr =
513 get_int (profile, options [i].opt_name, *options [i].opt_addr);
515 load_layout (profile);
517 load_panelize ();
519 startup_left_mode = load_mode ("New Left Panel");
520 startup_right_mode = load_mode ("New Right Panel");
522 /* At least one of the panels is a listing panel */
523 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
524 startup_left_mode = view_listing;
526 if (!other_dir){
527 char *buffer;
529 buffer = (char*) g_malloc (MC_MAXPATHLEN);
530 load_string ("Dirs", "other_dir", ".", buffer,
531 MC_MAXPATHLEN);
532 if (vfs_file_is_local (buffer))
533 other_dir = buffer;
534 else
535 g_free (buffer);
538 boot_current_is_left =
539 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
541 #ifdef USE_NETCODE
542 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
543 #endif
545 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
546 sizeof (setup_color_string));
547 if (setup_color_string [0])
548 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", NULL);
550 /* The default color and the terminal dependent color */
551 load_string ("Colors", "base_color", "", setup_color_string,
552 sizeof (setup_color_string));
553 load_string ("Colors", getenv ("TERM"), "",
554 term_color_string, sizeof (term_color_string));
555 load_string ("Colors", "color_terminals", "",
556 color_terminal_string, sizeof (color_terminal_string));
558 /* Load the directory history */
559 /* directory_history_load (); */
560 /* Remove the temporal entries */
561 profile_clean_section ("Temporal:New Left Panel", profile_name);
562 profile_clean_section ("Temporal:New Right Panel", profile_name);
563 #if defined(USE_VFS) && defined (USE_NETCODE)
564 ftpfs_init_passwd ();
565 #endif /* USE_VFS && USE_NETCODE */
567 #ifdef HAVE_CHARSET
568 if ( load_codepages_list() > 0 ) {
569 char cpname[128];
570 load_string( "Misc", "display_codepage", "",
571 cpname, sizeof(cpname) );
572 if ( cpname[0] != '\0' )
573 display_codepage = get_codepage_index( cpname );
576 init_translation_table( source_codepage, display_codepage );
577 #endif /* HAVE_CHARSET */
580 #if defined(USE_VFS) && defined (USE_NETCODE)
581 char *
582 load_anon_passwd ()
584 char buffer [255];
586 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
587 if (buffer [0])
588 return g_strdup (buffer);
589 else
590 return 0;
592 #endif /* USE_VFS && USE_NETCODE */
594 void done_setup (void)
596 g_free (profile_name);
597 done_hotlist ();
598 done_panelize ();
599 /* directory_history_free (); */
602 static void
603 load_keys_from_section (char *terminal, char *profile_name)
605 char *section_name;
606 void *profile_keys;
607 char *key, *value, *valcopy;
608 int key_code;
610 if (!terminal)
611 return;
613 section_name = g_strconcat ("terminal:", terminal, NULL);
614 profile_keys = profile_init_iterator (section_name, profile_name);
615 if (!profile_keys){
616 g_free (section_name);
617 return;
620 while (profile_keys){
621 profile_keys = profile_iterator_next (profile_keys, &key, &value);
622 key_code = lookup_key (key);
623 if (key_code){
624 valcopy = convert_controls (value);
625 define_sequence (key_code, valcopy, MCKEY_NOACTION);
626 g_free (valcopy);
629 g_free (section_name);
630 return;
633 void load_key_defs (void)
635 char *libfile = concat_dir_and_file (mc_home, "mc.lib");
638 * Load keys from mc.lib before ~/.mc/ini, so that the user
639 * definitions override global settings.
641 load_keys_from_section ("general", libfile);
642 load_keys_from_section (getenv ("TERM"), libfile);
643 load_keys_from_section ("general", profile_name);
644 load_keys_from_section (getenv ("TERM"), profile_name);
646 /* We don't want a huge database loaded in core */
647 free_profile_name (libfile);
648 g_free (libfile);