small typo
[midnight-commander.git] / src / setup.c
blob5fbd1ba2b93f6b2666c638ff14f3e70a7cdebde4
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 #ifdef HAVE_CHARSET
45 #include "charsets.h"
46 #endif
48 #include "../vfs/vfs.h"
49 #ifdef USE_NETCODE
50 # include "../vfs/ftpfs.h"
51 #endif
53 #ifdef USE_INTERNAL_EDIT
54 # include "../edit/edit.h"
55 #endif
58 /* "$Id$" */
60 extern char *find_ignore_dirs;
62 extern int num_history_items_recorded;
64 char *profile_name; /* .mc/ini */
65 char *global_profile_name; /* mc.lib */
67 char setup_color_string [4096];
68 char term_color_string [4096];
69 char color_terminal_string [512];
71 #define load_int(a,b,c) GetPrivateProfileInt(a,b,c,profile_name)
72 #define load_string(a,b,c,d,e) GetPrivateProfileString(a,b,c,d,e,profile_name)
73 #define save_string WritePrivateProfileString
75 int startup_left_mode;
76 int startup_right_mode;
78 /* Ugly hack to allow panel_save_setup to work as a place holder for */
79 /* default panel values */
80 int saving_setup;
82 static const struct {
83 char *key;
84 sortfn *sort_type;
85 } sort_names [] = {
86 { "name", (sortfn *) sort_name },
87 { "extension", (sortfn *) sort_ext },
88 { "time", (sortfn *) sort_time },
89 { "atime", (sortfn *) sort_atime },
90 { "ctime", (sortfn *) sort_ctime },
91 { "size", (sortfn *) sort_size },
92 { "inode", (sortfn *) sort_inode },
93 { "unsorted", (sortfn *) unsorted },
94 { 0, 0 }
97 static const struct {
98 char *key;
99 int list_type;
100 } list_types [] = {
101 { "full", list_full },
102 { "brief", list_brief },
103 { "long", list_long },
104 { "user", list_user },
105 { "icons", list_icons },
106 { 0, 0 }
109 static const struct {
110 char *opt_name;
111 int opt_type;
112 } panel_types [] = {
113 { "listing", view_listing },
114 { "quickview", view_quick },
115 { "info", view_info },
116 { "tree", view_tree },
117 { 0, 0 }
120 static const struct {
121 char *opt_name;
122 int *opt_addr;
123 } layout [] = {
124 { "equal_split", &equal_split },
125 { "first_panel_size", &first_panel_size },
126 { "message_visible", &message_visible },
127 { "keybar_visible", &keybar_visible },
128 { "xterm_hintbar", &xterm_hintbar },
129 { "output_lines", &output_lines },
130 { "command_prompt", &command_prompt },
131 { "menubar_visible", &menubar_visible },
132 { "show_mini_info", &show_mini_info },
133 { "permission_mode", &permission_mode },
134 { "filetype_mode", &filetype_mode },
135 { 0, 0 }
139 #undef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
141 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
142 extern int preserve_uidgid;
143 #endif
145 static const struct {
146 char *opt_name;
147 int *opt_addr;
148 } options [] = {
149 { "show_backups", &show_backups },
150 { "show_dot_files", &show_dot_files },
151 { "verbose", &verbose },
152 { "mark_moves_down", &mark_moves_down },
153 { "pause_after_run", &pause_after_run },
154 { "shell_patterns", &easy_patterns },
155 { "auto_save_setup", &auto_save_setup },
156 { "auto_menu", &auto_menu },
157 { "use_internal_view", &use_internal_view },
158 { "use_internal_edit", &use_internal_edit },
159 { "clear_before_exec", &clear_before_exec },
160 { "mix_all_files", &mix_all_files },
161 { "fast_reload", &fast_reload },
162 { "fast_reload_msg_shown", &fast_reload_w },
163 { "confirm_delete", &confirm_delete },
164 { "confirm_overwrite", &confirm_overwrite },
165 { "confirm_execute", &confirm_execute },
166 { "confirm_exit", &confirm_exit },
167 { "safe_delete", &know_not_what_am_i_doing },
168 { "mouse_repeat_rate", &mou_auto_repeat },
169 { "double_click_speed", &double_click_speed },
170 #ifndef HAVE_CHARSET
171 { "eight_bit_clean", &eight_bit_clean },
172 { "full_eight_bits", &full_eight_bits },
173 #endif /* !HAVE_CHARSET */
174 { "use_8th_bit_as_meta", &use_8th_bit_as_meta },
175 { "confirm_view_dir", &confirm_view_dir },
176 { "mouse_move_pages", &mouse_move_pages },
177 { "mouse_move_pages_viewer", &mouse_move_pages_viewer },
178 { "fast_refresh", &fast_refresh },
179 { "navigate_with_arrows", &navigate_with_arrows },
180 { "drop_menus", &drop_menus },
181 { "wrap_mode", &global_wrap_mode},
182 { "old_esc_mode", &old_esc_mode },
183 { "cd_symlinks", &cd_symlinks },
184 { "show_all_if_ambiguous", &show_all_if_ambiguous },
185 { "have_fast_cpu", &have_fast_cpu },
186 { "torben_fj_mode", &torben_fj_mode },
187 { "use_file_to_guess_type", &use_file_to_check_type },
188 { "alternate_plus_minus", &alternate_plus_minus },
189 { "only_leading_plus_minus", &only_leading_plus_minus },
190 { "show_output_starts_shell", &output_starts_shell },
191 { "panel_scroll_pages", &panel_scroll_pages },
192 { "xtree_mode", &xtree_mode },
193 { "num_history_items_recorded", &num_history_items_recorded },
194 { "file_op_compute_totals", &file_op_compute_totals },
195 #ifdef SAVE_CHANGES_OUTSIDE_OPTIONS_MENU
196 { "dive_into_subdirs", &dive_into_subdirs },
197 { "preserve_uidgid", &preserve_uidgid },
198 /* What about the other two options in the copy dialog
199 (follow links, stable symlinks) -Norbert */
200 { "tree_navigation_flag", &tree_navigation_flag },
201 #endif /* SAVE_CHANGES_OUTSIDE_OPTIONS_MENU */
202 #ifdef USE_VFS
203 { "vfs_timeout", &vfs_timeout },
204 #ifdef USE_NETCODE
205 { "ftpfs_directory_timeout", &ftpfs_directory_timeout },
206 { "use_netrc", &use_netrc },
207 { "ftpfs_retry_seconds", &ftpfs_retry_seconds },
208 { "ftpfs_always_use_proxy", &ftpfs_always_use_proxy },
209 { "ftpfs_use_passive_connections", &ftpfs_use_passive_connections },
210 { "ftpfs_use_unix_list_options", &ftpfs_use_unix_list_options },
211 { "ftpfs_first_cd_then_ls", &ftpfs_first_cd_then_ls },
212 #endif /* USE_NETCODE */
213 #endif /* USE_VFS */
214 #ifdef USE_INTERNAL_EDIT
215 { "editor_word_wrap_line_length", &option_word_wrap_line_length },
216 { "editor_key_emulation", &edit_key_emulation },
217 { "editor_tab_spacing", &option_tab_spacing },
218 { "editor_fill_tabs_with_spaces", &option_fill_tabs_with_spaces },
219 { "editor_return_does_auto_indent", &option_return_does_auto_indent },
220 { "editor_backspace_through_tabs", &option_backspace_through_tabs },
221 { "editor_fake_half_tabs", &option_fake_half_tabs },
222 { "editor_option_save_mode", &option_save_mode },
223 { "editor_option_backup_ext_int", &option_backup_ext_int },
224 { "editor_option_auto_para_formatting", &option_auto_para_formatting },
225 { "editor_option_typewriter_wrap", &option_typewriter_wrap },
226 { "editor_edit_confirm_save", &edit_confirm_save },
227 { "editor_syntax_highlighting", &option_syntax_highlighting },
228 #endif /* USE_INTERNAL_EDIT */
230 { "nice_rotating_dash", &nice_rotating_dash },
231 { "horizontal_split", &horizontal_split },
232 { 0, 0 }
235 void
236 panel_save_setup (WPanel *panel, char *section)
238 char buffer [BUF_TINY];
239 int i;
241 g_snprintf (buffer, sizeof (buffer), "%d", panel->reverse);
242 save_string (section, "reverse", buffer, profile_name);
243 g_snprintf (buffer, sizeof (buffer), "%d", panel->case_sensitive);
244 save_string (section, "case_sensitive", buffer, profile_name);
245 for (i = 0; sort_names [i].key; i++)
246 if (sort_names [i].sort_type == (sortfn *) panel->sort_type){
247 save_string (section, "sort_order",
248 sort_names [i].key, profile_name);
249 break;
252 for (i = 0; list_types [i].key; i++)
253 if (list_types [i].list_type == panel->list_type){
254 save_string (section, "list_mode", list_types [i].key, profile_name);
255 break;
258 save_string (section, "user_format",
259 panel->user_format, profile_name);
261 for (i = 0; i < LIST_TYPES; i++){
262 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
263 save_string (section, buffer,
264 panel->user_status_format [i], profile_name);
267 g_snprintf (buffer, sizeof (buffer), "%d", panel->user_mini_status);
268 save_string (section, "user_mini_status", buffer,
269 profile_name);
272 void
273 save_layout (void)
275 char *profile;
276 int i;
277 char buffer [BUF_TINY];
279 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
281 /* Save integer options */
282 for (i = 0; layout [i].opt_name; i++){
283 g_snprintf (buffer, sizeof (buffer), "%d", *layout [i].opt_addr);
284 save_string ("Layout", layout [i].opt_name, buffer, profile);
287 g_free (profile);
290 void
291 save_configure (void)
293 char *profile;
294 int i;
296 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
298 /* Save integer options */
299 for (i = 0; options [i].opt_name; i++)
300 set_int (profile, options [i].opt_name, *options [i].opt_addr);
302 g_free (profile);
305 static void
306 panel_save_type (char *section, int type)
308 int i;
310 for (i = 0; panel_types [i].opt_name; i++)
311 if (panel_types [i].opt_type == type){
312 save_string (section, "display", panel_types [i].opt_name,
313 profile_name);
314 break;
318 void
319 save_panel_types ()
321 int type;
323 type = get_display_type (0);
324 panel_save_type ("New Left Panel", type);
325 if (type == view_listing)
326 panel_save_setup (left_panel, left_panel->panel_name);
327 type = get_display_type (1);
328 panel_save_type ("New Right Panel", type);
329 if (type == view_listing)
330 panel_save_setup (right_panel, right_panel->panel_name);
333 void
334 save_setup (void)
336 char *profile;
338 saving_setup = 1;
339 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
341 save_configure ();
343 save_layout ();
344 save_string ("Dirs", "other_dir",
345 get_other_type () == view_listing
346 ? opanel->cwd : ".", profile);
347 if (get_current_panel () != NULL)
348 WritePrivateProfileString ("Dirs", "current_is_left",
349 get_current_index () == 0 ? "1" : "0", profile);
350 save_hotlist ();
352 save_panelize ();
353 save_panel_types ();
354 /* directory_history_save (); */
356 #if defined(USE_VFS) && defined (USE_NETCODE)
357 WritePrivateProfileString ("Misc", "ftpfs_password",
358 ftpfs_anonymous_passwd, profile);
359 if (ftpfs_proxy_host)
360 WritePrivateProfileString ("Misc", "ftp_proxy_host",
361 ftpfs_proxy_host, profile);
362 #endif /* USE_VFS && USE_NETCODE */
364 #ifdef HAVE_CHARSET
365 save_string( "Misc", "display_codepage",
366 get_codepage_id( display_codepage ), profile_name );
367 #endif /* HAVE_CHARSET */
369 g_free (profile);
370 saving_setup = 0;
373 void
374 panel_load_setup (WPanel *panel, char *section)
376 int i;
377 char buffer [BUF_TINY];
379 panel->reverse = load_int (section, "reverse", 0);
380 panel->case_sensitive = load_int (section, "case_sensitive", OS_SORT_CASE_SENSITIVE_DEFAULT);
382 /* Load sort order */
383 load_string (section, "sort_order", "name", buffer, sizeof (buffer));
384 panel->sort_type = (sortfn *) sort_name;
385 for (i = 0; sort_names [i].key; i++)
386 if ( g_strcasecmp (sort_names [i].key, buffer) == 0){
387 panel->sort_type = sort_names [i].sort_type;
388 break;
391 /* Load the listing mode */
392 load_string (section, "list_mode", "full", buffer, sizeof (buffer));
393 panel->list_type = list_full;
394 for (i = 0; list_types [i].key; i++)
395 if ( g_strcasecmp (list_types [i].key, buffer) == 0){
396 panel->list_type = list_types [i].list_type;
397 break;
400 if (panel->list_type == list_icons)
401 panel->list_type = list_full;
403 /* User formats */
404 if (panel->user_format){
405 g_free (panel->user_format);
406 panel->user_format = 0;
408 panel->user_format = g_strdup (get_profile_string (section, "user_format",
409 DEFAULT_USER_FORMAT,
410 profile_name));
411 for (i = 0; i < LIST_TYPES; i++){
412 if (panel->user_status_format [i])
413 g_free (panel->user_status_format [i]);
414 g_snprintf (buffer, sizeof (buffer), "user_status%d", i);
415 panel->user_status_format [i] =
416 g_strdup (get_profile_string (section, buffer,
417 DEFAULT_USER_FORMAT, profile_name));
420 panel->user_mini_status =
421 load_int (section, "user_mini_status", 0);
425 static void
426 load_layout (char *profile_name)
428 int i;
430 for (i = 0; layout [i].opt_name; i++)
431 *layout [i].opt_addr =
432 load_int ("Layout", layout [i].opt_name,
433 *layout [i].opt_addr);
436 static int
437 load_mode (char *section)
439 char buffer [20];
440 int i;
442 int mode = view_listing;
444 /* Load the display mode */
445 load_string (section, "display", "listing", buffer, sizeof (buffer));
447 for (i = 0; panel_types [i].opt_name; i++)
448 if ( g_strcasecmp (panel_types [i].opt_name, buffer) == 0){
449 mode = panel_types [i].opt_type;
450 break;
453 return mode;
456 #ifdef USE_NETCODE
457 static char *
458 do_load_string (char *s, char *ss, char *def)
460 char *buffer = g_malloc (BUF_SMALL);
461 char *p;
463 load_string (s, ss, def, buffer, BUF_SMALL);
465 p = g_strdup (buffer);
466 g_free (buffer);
467 return p;
469 #endif /* !USE_NETCODE */
471 char *
472 setup_init (void)
474 char *profile;
475 char *inifile;
477 if (profile_name)
478 return profile_name;
480 profile = concat_dir_and_file (home_dir, PROFILE_NAME);
481 if (!exist_file (profile)){
482 inifile = concat_dir_and_file (mc_home, "mc.ini");
483 if (exist_file (inifile)){
484 g_free (profile);
485 profile = inifile;
486 } else
487 g_free (inifile);
490 profile_name = profile;
492 return profile;
495 void
496 load_setup (void)
498 char *profile;
499 int i;
501 profile = setup_init ();
503 /* mc.lib is common for all users, but has priority lower than
504 ~/.mc/ini. FIXME: it's only used for keys and treestore now */
505 global_profile_name = concat_dir_and_file (mc_home, "mc.lib");
507 /* Load integer boolean options */
508 for (i = 0; options [i].opt_name; i++)
509 *options [i].opt_addr =
510 get_int (profile, options [i].opt_name, *options [i].opt_addr);
512 load_layout (profile);
514 load_panelize ();
516 startup_left_mode = load_mode ("New Left Panel");
517 startup_right_mode = load_mode ("New Right Panel");
519 /* At least one of the panels is a listing panel */
520 if (startup_left_mode != view_listing && startup_right_mode!=view_listing)
521 startup_left_mode = view_listing;
523 if (!other_dir){
524 char *buffer;
526 buffer = (char*) g_malloc (MC_MAXPATHLEN);
527 load_string ("Dirs", "other_dir", ".", buffer,
528 MC_MAXPATHLEN);
529 if (vfs_file_is_local (buffer))
530 other_dir = buffer;
531 else
532 g_free (buffer);
535 boot_current_is_left =
536 GetPrivateProfileInt ("Dirs", "current_is_left", 1, profile);
538 #ifdef USE_NETCODE
539 ftpfs_proxy_host = do_load_string ("Misc", "ftp_proxy_host", "gate");
540 #endif
542 load_string ("Misc", "find_ignore_dirs", "", setup_color_string,
543 sizeof (setup_color_string));
544 if (setup_color_string [0])
545 find_ignore_dirs = g_strconcat (":", setup_color_string, ":", NULL);
547 /* The default color and the terminal dependent color */
548 load_string ("Colors", "base_color", "", setup_color_string,
549 sizeof (setup_color_string));
550 load_string ("Colors", getenv ("TERM"), "",
551 term_color_string, sizeof (term_color_string));
552 load_string ("Colors", "color_terminals", "",
553 color_terminal_string, sizeof (color_terminal_string));
555 /* Load the directory history */
556 /* directory_history_load (); */
557 /* Remove the temporal entries */
558 profile_clean_section ("Temporal:New Left Panel", profile_name);
559 profile_clean_section ("Temporal:New Right Panel", profile_name);
560 #if defined(USE_VFS) && defined (USE_NETCODE)
561 ftpfs_init_passwd ();
562 #endif /* USE_VFS && USE_NETCODE */
564 #ifdef HAVE_CHARSET
565 if ( load_codepages_list() > 0 ) {
566 char cpname[128];
567 load_string( "Misc", "display_codepage", "",
568 cpname, sizeof(cpname) );
569 if ( cpname[0] != '\0' )
570 display_codepage = get_codepage_index( cpname );
573 init_translation_table( source_codepage, display_codepage );
574 #endif /* HAVE_CHARSET */
577 #if defined(USE_VFS) && defined (USE_NETCODE)
578 char *
579 load_anon_passwd ()
581 char buffer [255];
583 load_string ("Misc", "ftpfs_password", "", buffer, sizeof (buffer));
584 if (buffer [0])
585 return g_strdup (buffer);
586 else
587 return 0;
589 #endif /* USE_VFS && USE_NETCODE */
591 void done_setup (void)
593 g_free (profile_name);
594 g_free (global_profile_name);
595 done_hotlist ();
596 done_panelize ();
597 /* directory_history_free (); */
600 static void
601 load_keys_from_section (char *terminal, char *profile_name)
603 char *section_name;
604 void *profile_keys;
605 char *key, *value, *valcopy;
606 int key_code;
608 if (!terminal)
609 return;
611 section_name = g_strconcat ("terminal:", terminal, NULL);
612 profile_keys = profile_init_iterator (section_name, profile_name);
613 if (!profile_keys){
614 g_free (section_name);
615 return;
618 while (profile_keys){
619 profile_keys = profile_iterator_next (profile_keys, &key, &value);
620 key_code = lookup_key (key);
621 if (key_code){
622 valcopy = convert_controls (value);
623 define_sequence (key_code, valcopy, MCKEY_NOACTION);
624 g_free (valcopy);
627 g_free (section_name);
628 return;
631 void load_key_defs (void)
634 * Load keys from mc.lib before ~/.mc/ini, so that the user
635 * definitions override global settings.
637 load_keys_from_section ("general", global_profile_name);
638 load_keys_from_section (getenv ("TERM"), global_profile_name);
639 load_keys_from_section ("general", profile_name);
640 load_keys_from_section (getenv ("TERM"), profile_name);
642 /* We don't want a huge database loaded in core */
643 free_profile_name (global_profile_name);