Update translations from Transifex
[midnight-commander.git] / src / filemanager / midnight.c
blob3b92959d74c21c6091b3643937f876236873c1d5
1 /*
2 Main dialog (file panels) of the Midnight Commander
4 Copyright (C) 1994-2019
5 Free Software Foundation, Inc.
7 Written by:
8 Miguel de Icaza, 1994, 1995, 1996, 1997
9 Janne Kukonlehto, 1994, 1995
10 Norbert Warmuth, 1997
11 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2012, 2013
12 Slava Zanko <slavazanko@gmail.com>, 2013
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 /** \file main.c
31 * \brief Source: main dialog (file panels) of the Midnight Commander
34 #include <config.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <locale.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/wait.h>
45 #include <pwd.h> /* for username in xterm title */
47 #include "lib/global.h"
48 #include "lib/fileloc.h" /* MC_HINT */
50 #include "lib/tty/tty.h"
51 #include "lib/tty/key.h" /* KEY_M_* masks */
52 #include "lib/skin.h"
53 #include "lib/util.h"
55 #include "lib/vfs/vfs.h"
57 #include "src/args.h"
58 #ifdef ENABLE_SUBSHELL
59 #include "src/subshell/subshell.h"
60 #endif
61 #include "src/setup.h" /* variables */
62 #include "src/learn.h" /* learn_keys() */
63 #include "src/keybind-defaults.h"
64 #include "lib/keybind.h"
65 #include "lib/event.h"
67 #include "tree.h"
68 #include "boxes.h" /* sort_box(), tree_box() */
69 #include "layout.h"
70 #include "cmd.h" /* commands */
71 #include "hotlist.h"
72 #include "panelize.h"
73 #include "command.h" /* cmdline */
74 #include "dir.h" /* dir_list_clean() */
76 #include "chmod.h"
77 #include "chown.h"
78 #include "achown.h"
80 #ifdef USE_INTERNAL_EDIT
81 #include "src/editor/edit.h"
82 #endif
84 #ifdef USE_DIFF_VIEW
85 #include "src/diffviewer/ydiff.h"
86 #endif
88 #include "src/consaver/cons.saver.h" /* show_console_contents */
90 #include "midnight.h"
92 /*** global variables ****************************************************************************/
94 /* When the modes are active, left_panel, right_panel and tree_panel */
95 /* point to a proper data structure. You should check with the functions */
96 /* get_current_type and get_other_type the types of the panels before using */
97 /* this pointer variables */
99 /* The structures for the panels */
100 WPanel *left_panel = NULL;
101 WPanel *right_panel = NULL;
102 /* Pointer to the selected and unselected panel */
103 WPanel *current_panel = NULL;
105 /* The Menubar */
106 WMenuBar *the_menubar = NULL;
107 /* The widget where we draw the prompt */
108 WLabel *the_prompt;
109 /* The hint bar */
110 WLabel *the_hint;
111 /* The button bar */
112 WButtonBar *the_bar;
114 /* The prompt */
115 const char *mc_prompt = NULL;
117 /*** file scope macro definitions ****************************************************************/
119 #ifdef HAVE_CHARSET
121 * Don't restrict the output on the screen manager level,
122 * the translation tables take care of it.
124 #endif /* !HAVE_CHARSET */
126 /*** file scope type declarations ****************************************************************/
128 /*** file scope variables ************************************************************************/
130 static menu_t *left_menu, *right_menu;
132 static gboolean ctl_x_map_enabled = FALSE;
134 /*** file scope functions ************************************************************************/
136 /** Stop MC main dialog and the current dialog if it exists.
137 * Needed to provide fast exit from MC viewer or editor on shell exit */
138 static void
139 stop_dialogs (void)
141 dlg_stop (midnight_dlg);
143 if (top_dlg != NULL)
144 dlg_stop (DIALOG (top_dlg->data));
147 /* --------------------------------------------------------------------------------------------- */
149 static void
150 treebox_cmd (void)
152 char *sel_dir;
154 sel_dir = tree_box (selection (current_panel)->fname);
155 if (sel_dir != NULL)
157 vfs_path_t *sel_vdir;
159 sel_vdir = vfs_path_from_str (sel_dir);
160 do_cd (sel_vdir, cd_exact);
161 vfs_path_free (sel_vdir);
162 g_free (sel_dir);
166 /* --------------------------------------------------------------------------------------------- */
168 #ifdef LISTMODE_EDITOR
169 static void
170 listmode_cmd (void)
172 char *newmode;
174 if (get_current_type () != view_listing)
175 return;
177 newmode = listmode_edit (current_panel->user_format);
178 if (!newmode)
179 return;
181 g_free (current_panel->user_format);
182 current_panel->list_format = list_user;
183 current_panel->user_format = newmode;
184 set_panel_formats (current_panel);
186 do_refresh ();
188 #endif /* LISTMODE_EDITOR */
190 /* --------------------------------------------------------------------------------------------- */
192 static GList *
193 create_panel_menu (void)
195 GList *entries = NULL;
197 entries = g_list_prepend (entries, menu_entry_create (_("File listin&g"), CK_PanelListing));
198 entries = g_list_prepend (entries, menu_entry_create (_("&Quick view"), CK_PanelQuickView));
199 entries = g_list_prepend (entries, menu_entry_create (_("&Info"), CK_PanelInfo));
200 entries = g_list_prepend (entries, menu_entry_create (_("&Tree"), CK_PanelTree));
201 entries = g_list_prepend (entries, menu_separator_create ());
202 entries =
203 g_list_prepend (entries,
204 menu_entry_create (_("&Listing format..."), CK_SetupListingFormat));
205 entries = g_list_prepend (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
206 entries = g_list_prepend (entries, menu_entry_create (_("&Filter..."), CK_Filter));
207 #ifdef HAVE_CHARSET
208 entries = g_list_prepend (entries, menu_entry_create (_("&Encoding..."), CK_SelectCodepage));
209 #endif
210 entries = g_list_prepend (entries, menu_separator_create ());
211 #ifdef ENABLE_VFS_FTP
212 entries = g_list_prepend (entries, menu_entry_create (_("FT&P link..."), CK_ConnectFtp));
213 #endif
214 #ifdef ENABLE_VFS_FISH
215 entries = g_list_prepend (entries, menu_entry_create (_("S&hell link..."), CK_ConnectFish));
216 #endif
217 #ifdef ENABLE_VFS_SFTP
218 entries = g_list_prepend (entries, menu_entry_create (_("S&FTP link..."), CK_ConnectSftp));
219 #endif
220 #ifdef ENABLE_VFS_SMB
221 entries = g_list_prepend (entries, menu_entry_create (_("SM&B link..."), CK_ConnectSmb));
222 #endif
223 entries = g_list_prepend (entries, menu_entry_create (_("Paneli&ze"), CK_Panelize));
224 entries = g_list_prepend (entries, menu_separator_create ());
225 entries = g_list_prepend (entries, menu_entry_create (_("&Rescan"), CK_Reread));
227 return g_list_reverse (entries);
230 /* --------------------------------------------------------------------------------------------- */
232 static GList *
233 create_file_menu (void)
235 GList *entries = NULL;
237 entries = g_list_prepend (entries, menu_entry_create (_("&View"), CK_View));
238 entries = g_list_prepend (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFile));
239 entries = g_list_prepend (entries, menu_entry_create (_("&Filtered view"), CK_ViewFiltered));
240 entries = g_list_prepend (entries, menu_entry_create (_("&Edit"), CK_Edit));
241 entries = g_list_prepend (entries, menu_entry_create (_("&Copy"), CK_Copy));
242 entries = g_list_prepend (entries, menu_entry_create (_("C&hmod"), CK_ChangeMode));
243 entries = g_list_prepend (entries, menu_entry_create (_("&Link"), CK_Link));
244 entries = g_list_prepend (entries, menu_entry_create (_("&Symlink"), CK_LinkSymbolic));
245 entries =
246 g_list_prepend (entries,
247 menu_entry_create (_("Relative symlin&k"), CK_LinkSymbolicRelative));
248 entries = g_list_prepend (entries, menu_entry_create (_("Edit s&ymlink"), CK_LinkSymbolicEdit));
249 entries = g_list_prepend (entries, menu_entry_create (_("Ch&own"), CK_ChangeOwn));
250 entries =
251 g_list_prepend (entries, menu_entry_create (_("&Advanced chown"), CK_ChangeOwnAdvanced));
252 entries = g_list_prepend (entries, menu_entry_create (_("&Rename/Move"), CK_Move));
253 entries = g_list_prepend (entries, menu_entry_create (_("&Mkdir"), CK_MakeDir));
254 entries = g_list_prepend (entries, menu_entry_create (_("&Delete"), CK_Delete));
255 entries = g_list_prepend (entries, menu_entry_create (_("&Quick cd"), CK_CdQuick));
256 entries = g_list_prepend (entries, menu_separator_create ());
257 entries = g_list_prepend (entries, menu_entry_create (_("Select &group"), CK_Select));
258 entries = g_list_prepend (entries, menu_entry_create (_("U&nselect group"), CK_Unselect));
259 entries = g_list_prepend (entries, menu_entry_create (_("&Invert selection"), CK_SelectInvert));
260 entries = g_list_prepend (entries, menu_separator_create ());
261 entries = g_list_prepend (entries, menu_entry_create (_("E&xit"), CK_Quit));
263 return g_list_reverse (entries);
266 /* --------------------------------------------------------------------------------------------- */
268 static GList *
269 create_command_menu (void)
271 /* I know, I'm lazy, but the tree widget when it's not running
272 * as a panel still has some problems, I have not yet finished
273 * the WTree widget port, sorry.
275 GList *entries = NULL;
277 entries = g_list_prepend (entries, menu_entry_create (_("&User menu"), CK_UserMenu));
278 entries = g_list_prepend (entries, menu_entry_create (_("&Directory tree"), CK_Tree));
279 entries = g_list_prepend (entries, menu_entry_create (_("&Find file"), CK_Find));
280 entries = g_list_prepend (entries, menu_entry_create (_("S&wap panels"), CK_Swap));
281 entries = g_list_prepend (entries, menu_entry_create (_("Switch &panels on/off"), CK_Shell));
282 entries =
283 g_list_prepend (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirs));
284 #ifdef USE_DIFF_VIEW
285 entries = g_list_prepend (entries, menu_entry_create (_("C&ompare files"), CK_CompareFiles));
286 #endif
287 entries =
288 g_list_prepend (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
289 entries = g_list_prepend (entries, menu_entry_create (_("Show directory s&izes"), CK_DirSize));
290 entries = g_list_prepend (entries, menu_separator_create ());
291 entries = g_list_prepend (entries, menu_entry_create (_("Command &history"), CK_History));
292 entries = g_list_prepend (entries, menu_entry_create (_("Di&rectory hotlist"), CK_HotList));
293 #ifdef ENABLE_VFS
294 entries = g_list_prepend (entries, menu_entry_create (_("&Active VFS list"), CK_VfsList));
295 #endif
296 #ifdef ENABLE_BACKGROUND
297 entries = g_list_prepend (entries, menu_entry_create (_("&Background jobs"), CK_Jobs));
298 #endif
299 entries = g_list_prepend (entries, menu_entry_create (_("Screen lis&t"), CK_ScreenList));
300 entries = g_list_prepend (entries, menu_separator_create ());
301 #ifdef ENABLE_VFS_UNDELFS
302 entries =
303 g_list_prepend (entries,
304 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_Undelete));
305 #endif
306 #ifdef LISTMODE_EDITOR
307 entries = g_list_prepend (entries, menu_entry_create (_("&Listing format edit"), CK_ListMode));
308 #endif
309 #if defined (ENABLE_VFS_UNDELFS) || defined (LISTMODE_EDITOR)
310 entries = g_list_prepend (entries, menu_separator_create ());
311 #endif
312 entries =
313 g_list_prepend (entries,
314 menu_entry_create (_("Edit &extension file"), CK_EditExtensionsFile));
315 entries = g_list_prepend (entries, menu_entry_create (_("Edit &menu file"), CK_EditUserMenu));
316 entries =
317 g_list_prepend (entries,
318 menu_entry_create (_("Edit hi&ghlighting group file"),
319 CK_EditFileHighlightFile));
321 return g_list_reverse (entries);
324 /* --------------------------------------------------------------------------------------------- */
326 static GList *
327 create_options_menu (void)
329 GList *entries = NULL;
331 entries = g_list_prepend (entries, menu_entry_create (_("&Configuration..."), CK_Options));
332 entries = g_list_prepend (entries, menu_entry_create (_("&Layout..."), CK_OptionsLayout));
333 entries = g_list_prepend (entries, menu_entry_create (_("&Panel options..."), CK_OptionsPanel));
334 entries =
335 g_list_prepend (entries, menu_entry_create (_("C&onfirmation..."), CK_OptionsConfirm));
336 entries =
337 g_list_prepend (entries, menu_entry_create (_("&Appearance..."), CK_OptionsAppearance));
338 entries =
339 g_list_prepend (entries, menu_entry_create (_("&Display bits..."), CK_OptionsDisplayBits));
340 entries = g_list_prepend (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
341 #ifdef ENABLE_VFS
342 entries = g_list_prepend (entries, menu_entry_create (_("&Virtual FS..."), CK_OptionsVfs));
343 #endif
344 entries = g_list_prepend (entries, menu_separator_create ());
345 entries = g_list_prepend (entries, menu_entry_create (_("&Save setup"), CK_SaveSetup));
347 return g_list_reverse (entries);
350 /* --------------------------------------------------------------------------------------------- */
352 static void
353 init_menu (void)
355 left_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
356 menubar_add_menu (the_menubar, left_menu);
357 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
358 menubar_add_menu (the_menubar,
359 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
360 menubar_add_menu (the_menubar,
361 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
362 right_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
363 menubar_add_menu (the_menubar, right_menu);
364 update_menu ();
367 /* --------------------------------------------------------------------------------------------- */
369 static void
370 menu_last_selected_cmd (void)
372 menubar_activate (the_menubar, drop_menus, -1);
375 /* --------------------------------------------------------------------------------------------- */
377 static void
378 menu_cmd (void)
380 int selected;
382 if ((get_current_index () == 0) == (current_panel->active != 0))
383 selected = 0;
384 else
385 selected = g_list_length (the_menubar->menu) - 1;
387 menubar_activate (the_menubar, drop_menus, selected);
390 /* --------------------------------------------------------------------------------------------- */
392 static void
393 sort_cmd (void)
395 WPanel *p;
396 const panel_field_t *sort_order;
398 if (!SELECTED_IS_PANEL)
399 return;
401 p = MENU_PANEL;
402 sort_order = sort_box (&p->sort_info, p->sort_field);
403 panel_set_sort_order (p, sort_order);
406 /* --------------------------------------------------------------------------------------------- */
408 static char *
409 midnight_get_shortcut (long command)
411 const char *ext_map;
412 const char *shortcut = NULL;
414 shortcut = keybind_lookup_keymap_shortcut (main_map, command);
415 if (shortcut != NULL)
416 return g_strdup (shortcut);
418 shortcut = keybind_lookup_keymap_shortcut (panel_map, command);
419 if (shortcut != NULL)
420 return g_strdup (shortcut);
422 ext_map = keybind_lookup_keymap_shortcut (main_map, CK_ExtendedKeyMap);
423 if (ext_map != NULL)
424 shortcut = keybind_lookup_keymap_shortcut (main_x_map, command);
425 if (shortcut != NULL)
426 return g_strdup_printf ("%s %s", ext_map, shortcut);
428 return NULL;
431 /* --------------------------------------------------------------------------------------------- */
433 static char *
434 midnight_get_title (const WDialog * h, size_t len)
436 char *path;
437 char *login;
438 char *p;
440 (void) h;
442 title_path_prepare (&path, &login);
444 p = g_strdup_printf ("%s [%s]:%s", _("Panels:"), login, path);
445 g_free (path);
446 g_free (login);
447 path = g_strdup (str_trunc (p, len - 4));
448 g_free (p);
450 return path;
453 /* --------------------------------------------------------------------------------------------- */
455 static void
456 toggle_panels_split (void)
458 panels_layout.horizontal_split = !panels_layout.horizontal_split;
459 layout_change ();
460 do_refresh ();
463 /* --------------------------------------------------------------------------------------------- */
465 #ifdef ENABLE_VFS
466 /* event helper */
467 static gboolean
468 check_panel_timestamp (const WPanel * panel, panel_view_mode_t mode, struct vfs_class *vclass,
469 vfsid id)
471 if (mode == view_listing)
473 const vfs_path_element_t *path_element;
475 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
477 if (path_element->class != vclass)
478 return FALSE;
480 if (vfs_getid (panel->cwd_vpath) != id)
481 return FALSE;
483 return TRUE;
486 /* --------------------------------------------------------------------------------------------- */
488 /* event callback */
489 static gboolean
490 check_current_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
491 gpointer init_data, gpointer data)
493 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
495 (void) event_group_name;
496 (void) event_name;
497 (void) init_data;
499 event_data->ret =
500 check_panel_timestamp (current_panel, get_current_type (), event_data->vclass,
501 event_data->id);
502 return !event_data->ret;
505 /* --------------------------------------------------------------------------------------------- */
507 /* event callback */
508 static gboolean
509 check_other_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
510 gpointer init_data, gpointer data)
512 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
514 (void) event_group_name;
515 (void) event_name;
516 (void) init_data;
518 event_data->ret =
519 check_panel_timestamp (other_panel, get_other_type (), event_data->vclass, event_data->id);
520 return !event_data->ret;
522 #endif /* ENABLE_VFS */
524 /* --------------------------------------------------------------------------------------------- */
526 /* event callback */
527 static gboolean
528 print_vfs_message (const gchar * event_group_name, const gchar * event_name,
529 gpointer init_data, gpointer data)
531 ev_vfs_print_message_t *event_data = (ev_vfs_print_message_t *) data;
533 (void) event_group_name;
534 (void) event_name;
535 (void) init_data;
537 if (mc_global.midnight_shutdown)
538 goto ret;
540 if (!mc_global.message_visible || the_hint == NULL || WIDGET (the_hint)->owner == NULL)
542 int col, row;
544 if (!nice_rotating_dash || (ok_to_refresh <= 0))
545 goto ret;
547 /* Preserve current cursor position */
548 tty_getyx (&row, &col);
550 tty_gotoyx (0, 0);
551 tty_setcolor (NORMAL_COLOR);
552 tty_print_string (str_fit_to_term (event_data->msg, COLS - 1, J_LEFT));
554 /* Restore cursor position */
555 tty_gotoyx (row, col);
556 mc_refresh ();
557 goto ret;
560 if (mc_global.message_visible)
561 set_hintbar (event_data->msg);
563 ret:
564 MC_PTR_FREE (event_data->msg);
565 return TRUE;
568 /* --------------------------------------------------------------------------------------------- */
570 static void
571 create_panels (void)
573 int current_index, other_index;
574 panel_view_mode_t current_mode, other_mode;
575 char *current_dir, *other_dir;
576 vfs_path_t *original_dir;
579 * Following cases from command line are possible:
580 * 'mc' (no arguments): mc_run_param0 == NULL, mc_run_param1 == NULL
581 * active panel uses current directory
582 * passive panel uses "other_dir" from panels.ini
584 * 'mc dir1 dir2' (two arguments): mc_run_param0 != NULL, mc_run_param1 != NULL
585 * active panel uses mc_run_param0
586 * passive panel uses mc_run_param1
588 * 'mc dir1' (single argument): mc_run_param0 != NULL, mc_run_param1 == NULL
589 * active panel uses mc_run_param0
590 * passive panel uses "other_dir" from panels.ini
593 /* Set up panel directories */
594 if (boot_current_is_left)
596 /* left panel is active */
597 current_index = 0;
598 other_index = 1;
599 current_mode = startup_left_mode;
600 other_mode = startup_right_mode;
602 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
604 /* no arguments */
605 current_dir = NULL; /* assume current dir */
606 other_dir = saved_other_dir; /* from ini */
608 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
610 /* two arguments */
611 current_dir = (char *) mc_run_param0;
612 other_dir = mc_run_param1;
614 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
616 /* one argument */
617 current_dir = (char *) mc_run_param0;
618 other_dir = saved_other_dir; /* from ini */
621 else
623 /* right panel is active */
624 current_index = 1;
625 other_index = 0;
626 current_mode = startup_right_mode;
627 other_mode = startup_left_mode;
629 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
631 /* no arguments */
632 current_dir = NULL; /* assume current dir */
633 other_dir = saved_other_dir; /* from ini */
635 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
637 /* two arguments */
638 current_dir = (char *) mc_run_param0;
639 other_dir = mc_run_param1;
641 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
643 /* one argument */
644 current_dir = (char *) mc_run_param0;
645 other_dir = saved_other_dir; /* from ini */
649 /* 1. Get current dir */
650 original_dir = vfs_path_clone (vfs_get_raw_current_dir ());
652 /* 2. Create passive panel */
653 if (other_dir != NULL)
655 vfs_path_t *vpath;
657 if (g_path_is_absolute (other_dir))
658 vpath = vfs_path_from_str (other_dir);
659 else
660 vpath = vfs_path_append_new (original_dir, other_dir, (char *) NULL);
661 mc_chdir (vpath);
662 vfs_path_free (vpath);
664 create_panel (other_index, other_mode);
666 /* 3. Create active panel */
667 if (current_dir == NULL)
668 mc_chdir (original_dir);
669 else
671 vfs_path_t *vpath;
673 if (g_path_is_absolute (current_dir))
674 vpath = vfs_path_from_str (current_dir);
675 else
676 vpath = vfs_path_append_new (original_dir, current_dir, (char *) NULL);
677 mc_chdir (vpath);
678 vfs_path_free (vpath);
680 create_panel (current_index, current_mode);
682 if (startup_left_mode == view_listing)
683 current_panel = left_panel;
684 else if (right_panel != NULL)
685 current_panel = right_panel;
686 else
687 current_panel = left_panel;
689 vfs_path_free (original_dir);
691 #ifdef ENABLE_VFS
692 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_other_panel_timestamp, NULL, NULL);
693 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_current_panel_timestamp, NULL, NULL);
694 #endif /* ENABLE_VFS */
696 mc_event_add (MCEVENT_GROUP_CORE, "vfs_print_message", print_vfs_message, NULL, NULL);
699 /* --------------------------------------------------------------------------------------------- */
701 static void
702 midnight_put_panel_path (WPanel * panel)
704 vfs_path_t *cwd_vpath;
705 const char *cwd_vpath_str;
707 if (!command_prompt)
708 return;
710 #ifdef HAVE_CHARSET
711 cwd_vpath = remove_encoding_from_path (panel->cwd_vpath);
712 #else
713 cwd_vpath = vfs_path_clone (panel->cwd_vpath);
714 #endif
716 cwd_vpath_str = vfs_path_as_str (cwd_vpath);
718 command_insert (cmdline, cwd_vpath_str, FALSE);
720 if (!IS_PATH_SEP (cwd_vpath_str[strlen (cwd_vpath_str) - 1]))
721 command_insert (cmdline, PATH_SEP_STR, FALSE);
723 vfs_path_free (cwd_vpath);
726 /* --------------------------------------------------------------------------------------------- */
728 static void
729 put_link (WPanel * panel)
731 if (!command_prompt)
732 return;
733 if (S_ISLNK (selection (panel)->st.st_mode))
735 char buffer[MC_MAXPATHLEN];
736 vfs_path_t *vpath;
737 int i;
739 vpath = vfs_path_append_new (panel->cwd_vpath, selection (panel)->fname, (char *) NULL);
740 i = mc_readlink (vpath, buffer, sizeof (buffer) - 1);
741 vfs_path_free (vpath);
743 if (i > 0)
745 buffer[i] = '\0';
746 command_insert (cmdline, buffer, TRUE);
751 /* --------------------------------------------------------------------------------------------- */
753 static void
754 put_current_link (void)
756 put_link (current_panel);
759 /* --------------------------------------------------------------------------------------------- */
761 static void
762 put_other_link (void)
764 if (get_other_type () == view_listing)
765 put_link (other_panel);
768 /* --------------------------------------------------------------------------------------------- */
770 /** Insert the selected file name into the input line */
771 static void
772 put_current_selected (void)
774 const char *tmp;
776 if (!command_prompt)
777 return;
779 if (get_current_type () == view_tree)
781 WTree *tree;
782 const vfs_path_t *selected_name;
784 tree = (WTree *) get_panel_widget (get_current_index ());
785 selected_name = tree_selected_name (tree);
786 tmp = vfs_path_as_str (selected_name);
788 else
789 tmp = selection (current_panel)->fname;
791 command_insert (cmdline, tmp, TRUE);
794 /* --------------------------------------------------------------------------------------------- */
796 static void
797 put_tagged (WPanel * panel)
799 if (!command_prompt)
800 return;
801 input_disable_update (cmdline);
802 if (panel->marked)
804 int i;
806 for (i = 0; i < panel->dir.len; i++)
808 if (panel->dir.list[i].f.marked)
809 command_insert (cmdline, panel->dir.list[i].fname, TRUE);
812 else
814 command_insert (cmdline, panel->dir.list[panel->selected].fname, TRUE);
816 input_enable_update (cmdline);
819 /* --------------------------------------------------------------------------------------------- */
821 static void
822 put_current_tagged (void)
824 put_tagged (current_panel);
827 /* --------------------------------------------------------------------------------------------- */
829 static void
830 put_other_tagged (void)
832 if (get_other_type () == view_listing)
833 put_tagged (other_panel);
836 /* --------------------------------------------------------------------------------------------- */
838 static void
839 ctl_x_cmd (void)
841 ctl_x_map_enabled = TRUE;
844 /* --------------------------------------------------------------------------------------------- */
846 static void
847 setup_mc (void)
849 #ifdef HAVE_SLANG
850 #ifdef HAVE_CHARSET
851 tty_display_8bit (TRUE);
852 #else
853 tty_display_8bit (mc_global.full_eight_bits);
854 #endif /* HAVE_CHARSET */
856 #else /* HAVE_SLANG */
858 #ifdef HAVE_CHARSET
859 tty_display_8bit (TRUE);
860 #else
861 tty_display_8bit (mc_global.eight_bit_clean);
862 #endif /* HAVE_CHARSET */
863 #endif /* HAVE_SLANG */
865 #ifdef ENABLE_SUBSHELL
866 if (mc_global.tty.use_subshell)
867 add_select_channel (mc_global.tty.subshell_pty, load_prompt, NULL);
868 #endif /* !ENABLE_SUBSHELL */
870 if ((tty_baudrate () < 9600) || mc_global.tty.slow_terminal)
871 verbose = FALSE;
874 /* --------------------------------------------------------------------------------------------- */
876 static void
877 setup_dummy_mc (void)
879 vfs_path_t *vpath;
880 char *d;
881 int ret;
883 d = _vfs_get_cwd ();
884 setup_mc ();
885 vpath = vfs_path_from_str (d);
886 ret = mc_chdir (vpath);
887 (void) ret;
888 vfs_path_free (vpath);
889 g_free (d);
892 /* --------------------------------------------------------------------------------------------- */
894 static void
895 done_mc (void)
897 /* Setup shutdown
899 * We sync the profiles since the hotlist may have changed, while
900 * we only change the setup data if we have the auto save feature set
903 save_setup (auto_save_setup, panels_options.auto_save_setup);
905 vfs_stamp_path (vfs_get_raw_current_dir ());
908 /* --------------------------------------------------------------------------------------------- */
910 static void
911 create_file_manager (void)
913 midnight_dlg->get_shortcut = midnight_get_shortcut;
914 midnight_dlg->get_title = midnight_get_title;
915 /* allow rebind tab */
916 widget_want_tab (WIDGET (midnight_dlg), TRUE);
918 the_menubar = menubar_new (0, 0, COLS, NULL, menubar_visible);
919 add_widget (midnight_dlg, the_menubar);
920 init_menu ();
922 create_panels ();
923 add_widget (midnight_dlg, get_panel_widget (0));
924 add_widget (midnight_dlg, get_panel_widget (1));
926 the_hint = label_new (0, 0, 0);
927 the_hint->transparent = 1;
928 the_hint->auto_adjust_cols = 0;
929 WIDGET (the_hint)->cols = COLS;
930 add_widget (midnight_dlg, the_hint);
932 cmdline = command_new (0, 0, 0);
933 add_widget (midnight_dlg, cmdline);
935 the_prompt = label_new (0, 0, mc_prompt);
936 the_prompt->transparent = TRUE;
937 add_widget (midnight_dlg, the_prompt);
939 the_bar = buttonbar_new (mc_global.keybar_visible);
940 add_widget (midnight_dlg, the_bar);
941 midnight_set_buttonbar (the_bar);
944 /* --------------------------------------------------------------------------------------------- */
946 /** result must be free'd (I think this should go in util.c) */
947 static vfs_path_t *
948 prepend_cwd_on_local (const char *filename)
950 vfs_path_t *vpath;
952 vpath = vfs_path_from_str (filename);
953 if (!vfs_file_is_local (vpath) || g_path_is_absolute (filename))
954 return vpath;
956 vfs_path_free (vpath);
958 return vfs_path_append_new (vfs_get_raw_current_dir (), filename, (char *) NULL);
961 /* --------------------------------------------------------------------------------------------- */
963 /** Invoke the internal view/edit routine with:
964 * the default processing and forcing the internal viewer/editor
966 static gboolean
967 mc_maybe_editor_or_viewer (void)
969 gboolean ret;
971 switch (mc_global.mc_run_mode)
973 #ifdef USE_INTERNAL_EDIT
974 case MC_RUN_EDITOR:
975 ret = edit_files ((GList *) mc_run_param0);
976 break;
977 #endif /* USE_INTERNAL_EDIT */
978 case MC_RUN_VIEWER:
980 vfs_path_t *vpath = NULL;
982 if (mc_run_param0 != NULL && *(char *) mc_run_param0 != '\0')
983 vpath = prepend_cwd_on_local ((char *) mc_run_param0);
985 ret = view_file (vpath, FALSE, TRUE);
986 vfs_path_free (vpath);
987 break;
989 #ifdef USE_DIFF_VIEW
990 case MC_RUN_DIFFVIEWER:
991 ret = dview_diff_cmd (mc_run_param0, mc_run_param1);
992 break;
993 #endif /* USE_DIFF_VIEW */
994 default:
995 ret = FALSE;
998 return ret;
1001 /* --------------------------------------------------------------------------------------------- */
1003 static gboolean
1004 quit_cmd_internal (int quiet)
1006 int q = quit;
1007 size_t n;
1009 n = dialog_switch_num () - 1;
1010 if (n != 0)
1012 char msg[BUF_MEDIUM];
1014 g_snprintf (msg, sizeof (msg),
1015 ngettext ("You have %zu opened screen. Quit anyway?",
1016 "You have %zu opened screens. Quit anyway?", n), n);
1018 if (query_dialog (_("The Midnight Commander"), msg, D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
1019 return FALSE;
1020 q = 1;
1022 else if (quiet || !confirm_exit)
1023 q = 1;
1024 else if (query_dialog (_("The Midnight Commander"),
1025 _("Do you really want to quit the Midnight Commander?"),
1026 D_NORMAL, 2, _("&Yes"), _("&No")) == 0)
1027 q = 1;
1029 if (q != 0)
1031 #ifdef ENABLE_SUBSHELL
1032 if (!mc_global.tty.use_subshell)
1033 stop_dialogs ();
1034 else if ((q = exit_subshell ()? 1 : 0) != 0)
1035 #endif
1036 stop_dialogs ();
1039 if (q != 0)
1040 quit |= 1;
1041 return (quit != 0);
1044 /* --------------------------------------------------------------------------------------------- */
1046 static gboolean
1047 quit_cmd (void)
1049 return quit_cmd_internal (0);
1052 /* --------------------------------------------------------------------------------------------- */
1055 * Repaint the contents of the panels without frames. To schedule panel
1056 * for repainting, set panel->dirty to 1. There are many reasons why
1057 * the panels need to be repainted, and this is a costly operation, so
1058 * it's done once per event.
1061 static void
1062 update_dirty_panels (void)
1064 if (get_current_type () == view_listing && current_panel->dirty)
1065 widget_redraw (WIDGET (current_panel));
1067 if (get_other_type () == view_listing && other_panel->dirty)
1068 widget_redraw (WIDGET (other_panel));
1071 /* --------------------------------------------------------------------------------------------- */
1073 static void
1074 toggle_show_hidden (void)
1076 panels_options.show_dot_files = !panels_options.show_dot_files;
1077 update_panels (UP_RELOAD, UP_KEEPSEL);
1078 /* redraw panels forced */
1079 update_dirty_panels ();
1082 /* --------------------------------------------------------------------------------------------- */
1084 static cb_ret_t
1085 midnight_execute_cmd (Widget * sender, long command)
1087 cb_ret_t res = MSG_HANDLED;
1089 (void) sender;
1091 /* stop quick search before executing any command */
1092 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1094 switch (command)
1096 case CK_ChangePanel:
1097 change_panel ();
1098 break;
1099 case CK_HotListAdd:
1100 add2hotlist_cmd ();
1101 break;
1102 case CK_SetupListingFormat:
1103 setup_listing_format_cmd ();
1104 break;
1105 case CK_ChangeMode:
1106 chmod_cmd ();
1107 break;
1108 case CK_ChangeOwn:
1109 chown_cmd ();
1110 break;
1111 case CK_ChangeOwnAdvanced:
1112 advanced_chown_cmd ();
1113 break;
1114 case CK_CompareDirs:
1115 compare_dirs_cmd ();
1116 break;
1117 case CK_Options:
1118 configure_box ();
1119 break;
1120 #ifdef ENABLE_VFS
1121 case CK_OptionsVfs:
1122 configure_vfs ();
1123 break;
1124 #endif
1125 case CK_OptionsConfirm:
1126 confirm_box ();
1127 break;
1128 case CK_Copy:
1129 copy_cmd ();
1130 break;
1131 case CK_PutCurrentPath:
1132 midnight_put_panel_path (current_panel);
1133 break;
1134 case CK_PutCurrentSelected:
1135 put_current_selected ();
1136 break;
1137 case CK_PutCurrentFullSelected:
1138 midnight_put_panel_path (current_panel);
1139 put_current_selected ();
1140 break;
1141 case CK_PutCurrentLink:
1142 put_current_link ();
1143 break;
1144 case CK_PutCurrentTagged:
1145 put_current_tagged ();
1146 break;
1147 case CK_PutOtherPath:
1148 if (get_other_type () == view_listing)
1149 midnight_put_panel_path (other_panel);
1150 break;
1151 case CK_PutOtherLink:
1152 put_other_link ();
1153 break;
1154 case CK_PutOtherTagged:
1155 put_other_tagged ();
1156 break;
1157 case CK_Delete:
1158 delete_cmd ();
1159 break;
1160 case CK_ScreenList:
1161 dialog_switch_list ();
1162 break;
1163 #ifdef USE_DIFF_VIEW
1164 case CK_CompareFiles:
1165 diff_view_cmd ();
1166 break;
1167 #endif
1168 case CK_OptionsDisplayBits:
1169 display_bits_box ();
1170 break;
1171 case CK_Edit:
1172 edit_cmd ();
1173 break;
1174 #ifdef USE_INTERNAL_EDIT
1175 case CK_EditForceInternal:
1176 edit_cmd_force_internal ();
1177 break;
1178 #endif
1179 case CK_EditExtensionsFile:
1180 ext_cmd ();
1181 break;
1182 case CK_EditFileHighlightFile:
1183 edit_fhl_cmd ();
1184 break;
1185 case CK_EditUserMenu:
1186 edit_mc_menu_cmd ();
1187 break;
1188 case CK_LinkSymbolicEdit:
1189 edit_symlink_cmd ();
1190 break;
1191 case CK_ExternalPanelize:
1192 external_panelize ();
1193 break;
1194 case CK_Filter:
1195 filter_cmd ();
1196 break;
1197 case CK_ViewFiltered:
1198 view_filtered_cmd ();
1199 break;
1200 case CK_Find:
1201 find_cmd ();
1202 break;
1203 #ifdef ENABLE_VFS_FISH
1204 case CK_ConnectFish:
1205 fishlink_cmd ();
1206 break;
1207 #endif
1208 #ifdef ENABLE_VFS_FTP
1209 case CK_ConnectFtp:
1210 ftplink_cmd ();
1211 break;
1212 #endif
1213 #ifdef ENABLE_VFS_SFTP
1214 case CK_ConnectSftp:
1215 sftplink_cmd ();
1216 break;
1217 #endif
1218 #ifdef ENABLE_VFS_SMB
1219 case CK_ConnectSmb:
1220 smblink_cmd ();
1221 break;
1222 #endif /* ENABLE_VFS_SMB */
1223 case CK_Panelize:
1224 cd_panelize_cmd ();
1225 break;
1226 case CK_Help:
1227 help_cmd ();
1228 break;
1229 case CK_History:
1230 /* show the history of command line widget */
1231 send_message (cmdline, NULL, MSG_ACTION, CK_History, NULL);
1232 break;
1233 case CK_PanelInfo:
1234 if (sender == WIDGET (the_menubar))
1235 info_cmd (); /* menu */
1236 else
1237 info_cmd_no_menu (); /* shortcut or buttonbar */
1238 break;
1239 #ifdef ENABLE_BACKGROUND
1240 case CK_Jobs:
1241 jobs_cmd ();
1242 break;
1243 #endif
1244 case CK_OptionsLayout:
1245 layout_box ();
1246 break;
1247 case CK_OptionsAppearance:
1248 appearance_box ();
1249 break;
1250 case CK_LearnKeys:
1251 learn_keys ();
1252 break;
1253 case CK_Link:
1254 link_cmd (LINK_HARDLINK);
1255 break;
1256 case CK_PanelListing:
1257 listing_cmd ();
1258 break;
1259 #ifdef LISTMODE_EDITOR
1260 case CK_ListMode:
1261 listmode_cmd ();
1262 break;
1263 #endif
1264 case CK_Menu:
1265 menu_cmd ();
1266 break;
1267 case CK_MenuLastSelected:
1268 menu_last_selected_cmd ();
1269 break;
1270 case CK_MakeDir:
1271 mkdir_cmd ();
1272 break;
1273 case CK_OptionsPanel:
1274 panel_options_box ();
1275 break;
1276 #ifdef HAVE_CHARSET
1277 case CK_SelectCodepage:
1278 encoding_cmd ();
1279 break;
1280 #endif
1281 case CK_CdQuick:
1282 quick_cd_cmd ();
1283 break;
1284 case CK_HotList:
1285 hotlist_cmd ();
1286 break;
1287 case CK_PanelQuickView:
1288 if (sender == WIDGET (the_menubar))
1289 quick_view_cmd (); /* menu */
1290 else
1291 quick_cmd_no_menu (); /* shortcut or buttonabr */
1292 break;
1293 case CK_QuitQuiet:
1294 quiet_quit_cmd ();
1295 break;
1296 case CK_Quit:
1297 quit_cmd ();
1298 break;
1299 case CK_LinkSymbolicRelative:
1300 link_cmd (LINK_SYMLINK_RELATIVE);
1301 break;
1302 case CK_Move:
1303 rename_cmd ();
1304 break;
1305 case CK_Reread:
1306 reread_cmd ();
1307 break;
1308 #ifdef ENABLE_VFS
1309 case CK_VfsList:
1310 vfs_list ();
1311 break;
1312 #endif
1313 case CK_SaveSetup:
1314 save_setup_cmd ();
1315 break;
1316 case CK_Select:
1317 case CK_Unselect:
1318 case CK_SelectInvert:
1319 res = send_message (current_panel, midnight_dlg, MSG_ACTION, command, NULL);
1320 break;
1321 case CK_Shell:
1322 view_other_cmd ();
1323 break;
1324 case CK_DirSize:
1325 smart_dirsize_cmd ();
1326 break;
1327 case CK_Sort:
1328 sort_cmd ();
1329 break;
1330 case CK_ExtendedKeyMap:
1331 ctl_x_cmd ();
1332 break;
1333 case CK_Suspend:
1334 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
1335 break;
1336 case CK_Swap:
1337 swap_cmd ();
1338 break;
1339 case CK_LinkSymbolic:
1340 link_cmd (LINK_SYMLINK_ABSOLUTE);
1341 break;
1342 case CK_ShowHidden:
1343 toggle_show_hidden ();
1344 break;
1345 case CK_SplitVertHoriz:
1346 toggle_panels_split ();
1347 break;
1348 case CK_SplitEqual:
1349 panels_split_equal ();
1350 break;
1351 case CK_SplitMore:
1352 panels_split_more ();
1353 break;
1354 case CK_SplitLess:
1355 panels_split_less ();
1356 break;
1357 case CK_PanelTree:
1358 panel_tree_cmd ();
1359 break;
1360 case CK_Tree:
1361 treebox_cmd ();
1362 break;
1363 #ifdef ENABLE_VFS_UNDELFS
1364 case CK_Undelete:
1365 undelete_cmd ();
1366 break;
1367 #endif
1368 case CK_UserMenu:
1369 user_file_menu_cmd ();
1370 break;
1371 case CK_View:
1372 view_cmd ();
1373 break;
1374 case CK_ViewFile:
1375 view_file_cmd ();
1376 break;
1377 case CK_Cancel:
1378 /* don't close panels due to SIGINT */
1379 break;
1380 default:
1381 res = MSG_NOT_HANDLED;
1384 return res;
1387 /* --------------------------------------------------------------------------------------------- */
1390 * Whether the command-line should not respond to key events.
1392 * This is TRUE if a QuickView or TreeView have the focus, as they're going
1393 * to consume some keys and there's no sense in passing to the command-line
1394 * just the leftovers.
1396 static gboolean
1397 is_cmdline_mute (void)
1399 /* When one of panels is other than view_listing,
1400 current_panel points to view_listing panel all time independently of
1401 it's activity. Thus, we can't use get_current_type() here.
1402 current_panel should point to actualy current active panel
1403 independently of it's type. */
1404 return (current_panel->active == 0
1405 && (get_other_type () == view_quick || get_other_type () == view_tree));
1408 /* --------------------------------------------------------------------------------------------- */
1411 * Handles the Enter key on the command-line.
1413 * Returns TRUE if non-whitespace was indeed processed.
1415 static gboolean
1416 handle_cmdline_enter (void)
1418 size_t i;
1420 for (i = 0; cmdline->buffer[i] != '\0' && whitespace (cmdline->buffer[i]); i++)
1423 if (cmdline->buffer[i] != '\0')
1425 send_message (cmdline, NULL, MSG_KEY, '\n', NULL);
1426 return TRUE;
1429 input_insert (cmdline, "", FALSE);
1430 cmdline->point = 0;
1432 return FALSE;
1435 /* --------------------------------------------------------------------------------------------- */
1437 static cb_ret_t
1438 midnight_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
1440 long command;
1442 switch (msg)
1444 case MSG_INIT:
1445 panel_init ();
1446 setup_panels ();
1447 return MSG_HANDLED;
1449 case MSG_DRAW:
1450 load_hint (TRUE);
1451 /* We handle the special case of the output lines */
1452 if (mc_global.tty.console_flag != '\0' && output_lines)
1453 show_console_contents (output_start_y,
1454 LINES - output_lines - mc_global.keybar_visible -
1455 1, LINES - mc_global.keybar_visible - 1);
1456 return MSG_HANDLED;
1458 case MSG_RESIZE:
1459 /* dlg_set_size() is surplus for this case */
1460 w->lines = LINES;
1461 w->cols = COLS;
1462 setup_panels ();
1463 menubar_arrange (the_menubar);
1464 return MSG_HANDLED;
1466 case MSG_IDLE:
1467 /* We only need the first idle event to show user menu after start */
1468 widget_idle (w, FALSE);
1470 if (boot_current_is_left)
1471 widget_select (get_panel_widget (0));
1472 else
1473 widget_select (get_panel_widget (1));
1475 if (auto_menu)
1476 midnight_execute_cmd (NULL, CK_UserMenu);
1477 return MSG_HANDLED;
1479 case MSG_KEY:
1480 if (ctl_x_map_enabled)
1482 ctl_x_map_enabled = FALSE;
1483 command = keybind_lookup_keymap_command (main_x_map, parm);
1484 if (command != CK_IgnoreKey)
1485 return midnight_execute_cmd (NULL, command);
1488 /* FIXME: should handle all menu shortcuts before this point */
1489 if (widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1490 return MSG_NOT_HANDLED;
1492 if (parm == '\n' && !is_cmdline_mute ())
1494 if (handle_cmdline_enter ())
1495 return MSG_HANDLED;
1496 /* Else: the panel will handle it. */
1499 if ((!mc_global.tty.alternate_plus_minus
1500 || !(mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)) && !quote
1501 && !current_panel->searching)
1503 if (!only_leading_plus_minus)
1505 /* Special treatement, since the input line will eat them */
1506 if (parm == '+')
1507 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1509 if (parm == '\\' || parm == '-')
1510 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1511 NULL);
1513 if (parm == '*')
1514 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1515 NULL);
1517 else if (!command_prompt || input_is_empty (cmdline))
1519 /* Special treatement '+', '-', '\', '*' only when this is
1520 * first char on input line
1522 if (parm == '+')
1523 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1525 if (parm == '\\' || parm == '-')
1526 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1527 NULL);
1529 if (parm == '*')
1530 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1531 NULL);
1534 return MSG_NOT_HANDLED;
1536 case MSG_HOTKEY_HANDLED:
1537 if ((get_current_type () == view_listing) && current_panel->searching)
1539 current_panel->dirty = 1; /* FIXME: unneeded? */
1540 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1542 return MSG_HANDLED;
1544 case MSG_UNHANDLED_KEY:
1546 cb_ret_t v = MSG_NOT_HANDLED;
1548 if (ctl_x_map_enabled)
1550 ctl_x_map_enabled = FALSE;
1551 command = keybind_lookup_keymap_command (main_x_map, parm);
1553 else
1554 command = keybind_lookup_keymap_command (main_map, parm);
1556 if (command != CK_IgnoreKey)
1557 v = midnight_execute_cmd (NULL, command);
1559 if (v == MSG_NOT_HANDLED && command_prompt && !is_cmdline_mute ())
1560 v = send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1562 return v;
1565 case MSG_POST_KEY:
1566 if (!widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1567 update_dirty_panels ();
1568 return MSG_HANDLED;
1570 case MSG_ACTION:
1571 /* Handle shortcuts, menu, and buttonbar. */
1572 return midnight_execute_cmd (sender, parm);
1574 case MSG_END:
1575 panel_deinit ();
1576 return MSG_HANDLED;
1578 default:
1579 return dlg_default_callback (w, sender, msg, parm, data);
1583 /* --------------------------------------------------------------------------------------------- */
1584 /*** public functions ****************************************************************************/
1585 /* --------------------------------------------------------------------------------------------- */
1587 void
1588 update_menu (void)
1590 menu_set_name (left_menu, panels_layout.horizontal_split ? _("&Above") : _("&Left"));
1591 menu_set_name (right_menu, panels_layout.horizontal_split ? _("&Below") : _("&Right"));
1592 menubar_arrange (the_menubar);
1593 menubar_set_visible (the_menubar, menubar_visible);
1596 /* --------------------------------------------------------------------------------------------- */
1598 void
1599 midnight_set_buttonbar (WButtonBar * b)
1601 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1602 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1603 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1604 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1605 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1606 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1607 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1608 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1609 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1610 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1613 /* --------------------------------------------------------------------------------------------- */
1615 * Return a random hint. If force is TRUE, ignore the timeout.
1618 char *
1619 get_random_hint (gboolean force)
1621 static const guint64 update_period = 60 * G_USEC_PER_SEC;
1622 static guint64 tv = 0;
1624 char *data, *result = NULL, *eop;
1625 size_t len, start;
1626 GIConv conv;
1628 /* Do not change hints more often than one minute */
1629 if (!force && !mc_time_elapsed (&tv, update_period))
1630 return g_strdup ("");
1632 data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
1633 if (data == NULL)
1634 return NULL;
1636 /* get a random entry */
1637 srand ((unsigned int) (tv / G_USEC_PER_SEC));
1638 start = ((size_t) rand ()) % (len - 1);
1640 /* Search the start of paragraph */
1641 for (; start != 0; start--)
1642 if (data[start] == '\n' && data[start + 1] == '\n')
1644 start += 2;
1645 break;
1648 /* Search the end of paragraph */
1649 for (eop = data + start; *eop != '\0'; eop++)
1651 if (*eop == '\n' && *(eop + 1) == '\n')
1653 *eop = '\0';
1654 break;
1656 if (*eop == '\n')
1657 *eop = ' ';
1660 /* hint files are stored in utf-8 */
1661 /* try convert hint file from utf-8 to terminal encoding */
1662 conv = str_crt_conv_from ("UTF-8");
1663 if (conv != INVALID_CONV)
1665 GString *buffer;
1667 buffer = g_string_sized_new (len - start);
1668 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1669 result = g_string_free (buffer, FALSE);
1670 else
1671 g_string_free (buffer, TRUE);
1672 str_close_conv (conv);
1674 else
1675 result = g_strndup (data + start, len - start);
1677 g_free (data);
1678 return result;
1682 /* --------------------------------------------------------------------------------------------- */
1684 * Load new hint and display it.
1685 * IF force is not 0, ignore the timeout.
1688 void
1689 load_hint (gboolean force)
1691 char *hint;
1693 if (WIDGET (the_hint)->owner == NULL)
1694 return;
1696 if (!mc_global.message_visible)
1698 label_set_text (the_hint, NULL);
1699 return;
1702 hint = get_random_hint (force);
1704 if (hint != NULL)
1706 if (*hint != '\0')
1707 set_hintbar (hint);
1708 g_free (hint);
1710 else
1712 char text[BUF_SMALL];
1714 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1715 set_hintbar (text);
1719 /* --------------------------------------------------------------------------------------------- */
1721 void
1722 change_panel (void)
1724 input_free_completions (cmdline);
1725 dlg_select_next_widget (midnight_dlg);
1728 /* --------------------------------------------------------------------------------------------- */
1730 /** Save current stat of directories to avoid reloading the panels
1731 * when no modifications have taken place
1733 void
1734 save_cwds_stat (void)
1736 if (panels_options.fast_reload)
1738 mc_stat (current_panel->cwd_vpath, &(current_panel->dir_stat));
1739 if (get_other_type () == view_listing)
1740 mc_stat (other_panel->cwd_vpath, &(other_panel->dir_stat));
1744 /* --------------------------------------------------------------------------------------------- */
1746 gboolean
1747 quiet_quit_cmd (void)
1749 print_last_revert = TRUE;
1750 return quit_cmd_internal (1);
1753 /* --------------------------------------------------------------------------------------------- */
1755 /** Run the main dialog that occupies the whole screen */
1756 gboolean
1757 do_nc (void)
1759 gboolean ret;
1761 #ifdef USE_INTERNAL_EDIT
1762 edit_stack_init ();
1763 #endif
1765 midnight_dlg = dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, dialog_colors,
1766 midnight_callback, NULL, "[main]", NULL);
1768 /* Check if we were invoked as an editor or file viewer */
1769 if (mc_global.mc_run_mode != MC_RUN_FULL)
1771 setup_dummy_mc ();
1772 ret = mc_maybe_editor_or_viewer ();
1774 else
1776 /* We only need the first idle event to show user menu after start */
1777 widget_idle (WIDGET (midnight_dlg), TRUE);
1779 setup_mc ();
1780 mc_filehighlight = mc_fhl_new (TRUE);
1782 create_file_manager ();
1783 (void) dlg_run (midnight_dlg);
1785 mc_fhl_free (&mc_filehighlight);
1787 ret = TRUE;
1789 /* dlg_destroy destroys even current_panel->cwd_vpath, so we have to save a copy :) */
1790 if (mc_args__last_wd_file != NULL && vfs_current_is_local ())
1791 last_wd_string = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
1793 /* don't handle VFS timestamps for dirs opened in panels */
1794 mc_event_destroy (MCEVENT_GROUP_CORE, "vfs_timestamp");
1796 dir_list_free_list (&panelized_panel.list);
1799 /* Program end */
1800 mc_global.midnight_shutdown = TRUE;
1801 dialog_switch_shutdown ();
1802 done_mc ();
1803 dlg_destroy (midnight_dlg);
1804 current_panel = NULL;
1806 #ifdef USE_INTERNAL_EDIT
1807 edit_stack_free ();
1808 #endif
1810 if ((quit & SUBSHELL_EXIT) == 0)
1811 clr_scr ();
1813 return ret;
1816 /* --------------------------------------------------------------------------------------------- */