Refactoring: handle panel listing mode switch in the panel level
[midnight-commander.git] / src / filemanager / midnight.c
blob33498bacf93be685f933b08ad0e42300657a20dd
1 /*
2 Main dialog (file panels) of the Midnight Commander
4 Copyright (C) 1994-2017
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) && (top_dlg->data != 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_type = 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, menu_entry_create (_("&Listing mode..."), CK_PanelListingChange));
204 entries = g_list_prepend (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
205 entries = g_list_prepend (entries, menu_entry_create (_("&Filter..."), CK_Filter));
206 #ifdef HAVE_CHARSET
207 entries = g_list_prepend (entries, menu_entry_create (_("&Encoding..."), CK_SelectCodepage));
208 #endif
209 entries = g_list_prepend (entries, menu_separator_create ());
210 #ifdef ENABLE_VFS_FTP
211 entries = g_list_prepend (entries, menu_entry_create (_("FT&P link..."), CK_ConnectFtp));
212 #endif
213 #ifdef ENABLE_VFS_FISH
214 entries = g_list_prepend (entries, menu_entry_create (_("S&hell link..."), CK_ConnectFish));
215 #endif
216 #ifdef ENABLE_VFS_SFTP
217 entries = g_list_prepend (entries, menu_entry_create (_("S&FTP link..."), CK_ConnectSftp));
218 #endif
219 #ifdef ENABLE_VFS_SMB
220 entries = g_list_prepend (entries, menu_entry_create (_("SM&B link..."), CK_ConnectSmb));
221 #endif
222 entries = g_list_prepend (entries, menu_entry_create (_("Paneli&ze"), CK_Panelize));
223 entries = g_list_prepend (entries, menu_separator_create ());
224 entries = g_list_prepend (entries, menu_entry_create (_("&Rescan"), CK_Reread));
226 return g_list_reverse (entries);
229 /* --------------------------------------------------------------------------------------------- */
231 static GList *
232 create_file_menu (void)
234 GList *entries = NULL;
236 entries = g_list_prepend (entries, menu_entry_create (_("&View"), CK_View));
237 entries = g_list_prepend (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFile));
238 entries = g_list_prepend (entries, menu_entry_create (_("&Filtered view"), CK_ViewFiltered));
239 entries = g_list_prepend (entries, menu_entry_create (_("&Edit"), CK_Edit));
240 entries = g_list_prepend (entries, menu_entry_create (_("&Copy"), CK_Copy));
241 entries = g_list_prepend (entries, menu_entry_create (_("C&hmod"), CK_ChangeMode));
242 entries = g_list_prepend (entries, menu_entry_create (_("&Link"), CK_Link));
243 entries = g_list_prepend (entries, menu_entry_create (_("&Symlink"), CK_LinkSymbolic));
244 entries =
245 g_list_prepend (entries,
246 menu_entry_create (_("Relative symlin&k"), CK_LinkSymbolicRelative));
247 entries = g_list_prepend (entries, menu_entry_create (_("Edit s&ymlink"), CK_LinkSymbolicEdit));
248 entries = g_list_prepend (entries, menu_entry_create (_("Ch&own"), CK_ChangeOwn));
249 entries =
250 g_list_prepend (entries, menu_entry_create (_("&Advanced chown"), CK_ChangeOwnAdvanced));
251 entries = g_list_prepend (entries, menu_entry_create (_("&Rename/Move"), CK_Move));
252 entries = g_list_prepend (entries, menu_entry_create (_("&Mkdir"), CK_MakeDir));
253 entries = g_list_prepend (entries, menu_entry_create (_("&Delete"), CK_Delete));
254 entries = g_list_prepend (entries, menu_entry_create (_("&Quick cd"), CK_CdQuick));
255 entries = g_list_prepend (entries, menu_separator_create ());
256 entries = g_list_prepend (entries, menu_entry_create (_("Select &group"), CK_Select));
257 entries = g_list_prepend (entries, menu_entry_create (_("U&nselect group"), CK_Unselect));
258 entries = g_list_prepend (entries, menu_entry_create (_("&Invert selection"), CK_SelectInvert));
259 entries = g_list_prepend (entries, menu_separator_create ());
260 entries = g_list_prepend (entries, menu_entry_create (_("E&xit"), CK_Quit));
262 return g_list_reverse (entries);
265 /* --------------------------------------------------------------------------------------------- */
267 static GList *
268 create_command_menu (void)
270 /* I know, I'm lazy, but the tree widget when it's not running
271 * as a panel still has some problems, I have not yet finished
272 * the WTree widget port, sorry.
274 GList *entries = NULL;
276 entries = g_list_prepend (entries, menu_entry_create (_("&User menu"), CK_UserMenu));
277 entries = g_list_prepend (entries, menu_entry_create (_("&Directory tree"), CK_Tree));
278 entries = g_list_prepend (entries, menu_entry_create (_("&Find file"), CK_Find));
279 entries = g_list_prepend (entries, menu_entry_create (_("S&wap panels"), CK_Swap));
280 entries = g_list_prepend (entries, menu_entry_create (_("Switch &panels on/off"), CK_Shell));
281 entries =
282 g_list_prepend (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirs));
283 #ifdef USE_DIFF_VIEW
284 entries = g_list_prepend (entries, menu_entry_create (_("C&ompare files"), CK_CompareFiles));
285 #endif
286 entries =
287 g_list_prepend (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
288 entries = g_list_prepend (entries, menu_entry_create (_("Show directory s&izes"), CK_DirSize));
289 entries = g_list_prepend (entries, menu_separator_create ());
290 entries = g_list_prepend (entries, menu_entry_create (_("Command &history"), CK_History));
291 entries = g_list_prepend (entries, menu_entry_create (_("Di&rectory hotlist"), CK_HotList));
292 #ifdef ENABLE_VFS
293 entries = g_list_prepend (entries, menu_entry_create (_("&Active VFS list"), CK_VfsList));
294 #endif
295 #ifdef ENABLE_BACKGROUND
296 entries = g_list_prepend (entries, menu_entry_create (_("&Background jobs"), CK_Jobs));
297 #endif
298 entries = g_list_prepend (entries, menu_entry_create (_("Screen lis&t"), CK_ScreenList));
299 entries = g_list_prepend (entries, menu_separator_create ());
300 #ifdef ENABLE_VFS_UNDELFS
301 entries =
302 g_list_prepend (entries,
303 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_Undelete));
304 #endif
305 #ifdef LISTMODE_EDITOR
306 entries = g_list_prepend (entries, menu_entry_create (_("&Listing format edit"), CK_ListMode));
307 #endif
308 #if defined (ENABLE_VFS_UNDELFS) || defined (LISTMODE_EDITOR)
309 entries = g_list_prepend (entries, menu_separator_create ());
310 #endif
311 entries =
312 g_list_prepend (entries,
313 menu_entry_create (_("Edit &extension file"), CK_EditExtensionsFile));
314 entries = g_list_prepend (entries, menu_entry_create (_("Edit &menu file"), CK_EditUserMenu));
315 entries =
316 g_list_prepend (entries,
317 menu_entry_create (_("Edit hi&ghlighting group file"),
318 CK_EditFileHighlightFile));
320 return g_list_reverse (entries);
323 /* --------------------------------------------------------------------------------------------- */
325 static GList *
326 create_options_menu (void)
328 GList *entries = NULL;
330 entries = g_list_prepend (entries, menu_entry_create (_("&Configuration..."), CK_Options));
331 entries = g_list_prepend (entries, menu_entry_create (_("&Layout..."), CK_OptionsLayout));
332 entries = g_list_prepend (entries, menu_entry_create (_("&Panel options..."), CK_OptionsPanel));
333 entries =
334 g_list_prepend (entries, menu_entry_create (_("C&onfirmation..."), CK_OptionsConfirm));
335 entries =
336 g_list_prepend (entries, menu_entry_create (_("&Appearance..."), CK_OptionsAppearance));
337 entries =
338 g_list_prepend (entries, menu_entry_create (_("&Display bits..."), CK_OptionsDisplayBits));
339 entries = g_list_prepend (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
340 #ifdef ENABLE_VFS
341 entries = g_list_prepend (entries, menu_entry_create (_("&Virtual FS..."), CK_OptionsVfs));
342 #endif
343 entries = g_list_prepend (entries, menu_separator_create ());
344 entries = g_list_prepend (entries, menu_entry_create (_("&Save setup"), CK_SaveSetup));
346 return g_list_reverse (entries);
349 /* --------------------------------------------------------------------------------------------- */
351 static void
352 init_menu (void)
354 left_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
355 menubar_add_menu (the_menubar, left_menu);
356 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
357 menubar_add_menu (the_menubar,
358 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
359 menubar_add_menu (the_menubar,
360 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
361 right_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
362 menubar_add_menu (the_menubar, right_menu);
363 update_menu ();
366 /* --------------------------------------------------------------------------------------------- */
368 static void
369 menu_last_selected_cmd (void)
371 menubar_activate (the_menubar, drop_menus, -1);
374 /* --------------------------------------------------------------------------------------------- */
376 static void
377 menu_cmd (void)
379 int selected;
381 if ((get_current_index () == 0) == (current_panel->active != 0))
382 selected = 0;
383 else
384 selected = g_list_length (the_menubar->menu) - 1;
386 menubar_activate (the_menubar, drop_menus, selected);
389 /* --------------------------------------------------------------------------------------------- */
391 static void
392 sort_cmd (void)
394 WPanel *p;
395 const panel_field_t *sort_order;
397 if (!SELECTED_IS_PANEL)
398 return;
400 p = MENU_PANEL;
401 sort_order = sort_box (&p->sort_info, p->sort_field);
402 panel_set_sort_order (p, sort_order);
405 /* --------------------------------------------------------------------------------------------- */
407 static char *
408 midnight_get_shortcut (long command)
410 const char *ext_map;
411 const char *shortcut = NULL;
413 shortcut = keybind_lookup_keymap_shortcut (main_map, command);
414 if (shortcut != NULL)
415 return g_strdup (shortcut);
417 shortcut = keybind_lookup_keymap_shortcut (panel_map, command);
418 if (shortcut != NULL)
419 return g_strdup (shortcut);
421 ext_map = keybind_lookup_keymap_shortcut (main_map, CK_ExtendedKeyMap);
422 if (ext_map != NULL)
423 shortcut = keybind_lookup_keymap_shortcut (main_x_map, command);
424 if (shortcut != NULL)
425 return g_strdup_printf ("%s %s", ext_map, shortcut);
427 return NULL;
430 /* --------------------------------------------------------------------------------------------- */
432 static char *
433 midnight_get_title (const WDialog * h, size_t len)
435 char *path;
436 char *login;
437 char *p;
439 (void) h;
441 title_path_prepare (&path, &login);
443 p = g_strdup_printf ("%s [%s]:%s", _("Panels:"), login, path);
444 g_free (path);
445 g_free (login);
446 path = g_strdup (str_trunc (p, len - 4));
447 g_free (p);
449 return path;
452 /* --------------------------------------------------------------------------------------------- */
454 static void
455 toggle_panels_split (void)
457 panels_layout.horizontal_split = !panels_layout.horizontal_split;
458 layout_change ();
459 do_refresh ();
462 /* --------------------------------------------------------------------------------------------- */
464 #ifdef ENABLE_VFS
465 /* event helper */
466 static gboolean
467 check_panel_timestamp (const WPanel * panel, panel_view_mode_t mode, struct vfs_class *vclass,
468 vfsid id)
470 if (mode == view_listing)
472 const vfs_path_element_t *path_element;
474 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
476 if (path_element->class != vclass)
477 return FALSE;
479 if (vfs_getid (panel->cwd_vpath) != id)
480 return FALSE;
482 return TRUE;
485 /* --------------------------------------------------------------------------------------------- */
487 /* event callback */
488 static gboolean
489 check_current_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
490 gpointer init_data, gpointer data)
492 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
494 (void) event_group_name;
495 (void) event_name;
496 (void) init_data;
498 event_data->ret =
499 check_panel_timestamp (current_panel, get_current_type (), event_data->vclass,
500 event_data->id);
501 return !event_data->ret;
504 /* --------------------------------------------------------------------------------------------- */
506 /* event callback */
507 static gboolean
508 check_other_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
509 gpointer init_data, gpointer data)
511 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
513 (void) event_group_name;
514 (void) event_name;
515 (void) init_data;
517 event_data->ret =
518 check_panel_timestamp (other_panel, get_other_type (), event_data->vclass, event_data->id);
519 return !event_data->ret;
521 #endif /* ENABLE_VFS */
523 /* --------------------------------------------------------------------------------------------- */
525 /* event callback */
526 static gboolean
527 print_vfs_message (const gchar * event_group_name, const gchar * event_name,
528 gpointer init_data, gpointer data)
530 ev_vfs_print_message_t *event_data = (ev_vfs_print_message_t *) data;
532 (void) event_group_name;
533 (void) event_name;
534 (void) init_data;
536 if (mc_global.midnight_shutdown)
537 goto ret;
539 if (!mc_global.message_visible || the_hint == NULL || WIDGET (the_hint)->owner == NULL)
541 int col, row;
543 if (!nice_rotating_dash || (ok_to_refresh <= 0))
544 goto ret;
546 /* Preserve current cursor position */
547 tty_getyx (&row, &col);
549 tty_gotoyx (0, 0);
550 tty_setcolor (NORMAL_COLOR);
551 tty_print_string (str_fit_to_term (event_data->msg, COLS - 1, J_LEFT));
553 /* Restore cursor position */
554 tty_gotoyx (row, col);
555 mc_refresh ();
556 goto ret;
559 if (mc_global.message_visible)
560 set_hintbar (event_data->msg);
562 ret:
563 MC_PTR_FREE (event_data->msg);
564 return TRUE;
567 /* --------------------------------------------------------------------------------------------- */
569 static void
570 create_panels (void)
572 int current_index, other_index;
573 panel_view_mode_t current_mode, other_mode;
574 char *current_dir, *other_dir;
575 vfs_path_t *original_dir;
578 * Following cases from command line are possible:
579 * 'mc' (no arguments): mc_run_param0 == NULL, mc_run_param1 == NULL
580 * active panel uses current directory
581 * passive panel uses "other_dir" from panels.ini
583 * 'mc dir1 dir2' (two arguments): mc_run_param0 != NULL, mc_run_param1 != NULL
584 * active panel uses mc_run_param0
585 * passive panel uses mc_run_param1
587 * 'mc dir1' (single argument): mc_run_param0 != NULL, mc_run_param1 == NULL
588 * active panel uses mc_run_param0
589 * passive panel uses "other_dir" from panels.ini
592 /* Set up panel directories */
593 if (boot_current_is_left)
595 /* left panel is active */
596 current_index = 0;
597 other_index = 1;
598 current_mode = startup_left_mode;
599 other_mode = startup_right_mode;
601 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
603 /* no arguments */
604 current_dir = NULL; /* assume current dir */
605 other_dir = saved_other_dir; /* from ini */
607 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
609 /* two arguments */
610 current_dir = (char *) mc_run_param0;
611 other_dir = mc_run_param1;
613 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
615 /* one argument */
616 current_dir = (char *) mc_run_param0;
617 other_dir = saved_other_dir; /* from ini */
620 else
622 /* right panel is active */
623 current_index = 1;
624 other_index = 0;
625 current_mode = startup_right_mode;
626 other_mode = startup_left_mode;
628 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
630 /* no arguments */
631 current_dir = NULL; /* assume current dir */
632 other_dir = saved_other_dir; /* from ini */
634 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
636 /* two arguments */
637 current_dir = (char *) mc_run_param0;
638 other_dir = mc_run_param1;
640 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
642 /* one argument */
643 current_dir = (char *) mc_run_param0;
644 other_dir = saved_other_dir; /* from ini */
648 /* 1. Get current dir */
649 original_dir = vfs_path_clone (vfs_get_raw_current_dir ());
651 /* 2. Create passive panel */
652 if (other_dir != NULL)
654 vfs_path_t *vpath;
656 if (g_path_is_absolute (other_dir))
657 vpath = vfs_path_from_str (other_dir);
658 else
659 vpath = vfs_path_append_new (original_dir, other_dir, (char *) NULL);
660 mc_chdir (vpath);
661 vfs_path_free (vpath);
663 set_display_type (other_index, other_mode);
665 /* 3. Create active panel */
666 if (current_dir == NULL)
667 mc_chdir (original_dir);
668 else
670 vfs_path_t *vpath;
672 if (g_path_is_absolute (current_dir))
673 vpath = vfs_path_from_str (current_dir);
674 else
675 vpath = vfs_path_append_new (original_dir, current_dir, (char *) NULL);
676 mc_chdir (vpath);
677 vfs_path_free (vpath);
679 set_display_type (current_index, current_mode);
681 if (startup_left_mode == view_listing)
682 current_panel = left_panel;
683 else if (right_panel != NULL)
684 current_panel = right_panel;
685 else
686 current_panel = left_panel;
688 vfs_path_free (original_dir);
690 #ifdef ENABLE_VFS
691 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_other_panel_timestamp, NULL, NULL);
692 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_current_panel_timestamp, NULL, NULL);
693 #endif /* ENABLE_VFS */
695 mc_event_add (MCEVENT_GROUP_CORE, "vfs_print_message", print_vfs_message, NULL, NULL);
697 /* Create the nice widgets */
698 cmdline = command_new (0, 0, 0);
699 the_prompt = label_new (0, 0, mc_prompt);
700 the_prompt->transparent = 1;
701 the_bar = buttonbar_new (mc_global.keybar_visible);
703 the_hint = label_new (0, 0, 0);
704 the_hint->transparent = 1;
705 the_hint->auto_adjust_cols = 0;
706 WIDGET (the_hint)->cols = COLS;
708 the_menubar = menubar_new (0, 0, COLS, NULL, menubar_visible);
711 /* --------------------------------------------------------------------------------------------- */
713 static void
714 midnight_put_panel_path (WPanel * panel)
716 vfs_path_t *cwd_vpath;
717 const char *cwd_vpath_str;
719 if (!command_prompt)
720 return;
722 #ifdef HAVE_CHARSET
723 cwd_vpath = remove_encoding_from_path (panel->cwd_vpath);
724 #else
725 cwd_vpath = vfs_path_clone (panel->cwd_vpath);
726 #endif
728 cwd_vpath_str = vfs_path_as_str (cwd_vpath);
730 command_insert (cmdline, cwd_vpath_str, FALSE);
732 if (!IS_PATH_SEP (cwd_vpath_str[strlen (cwd_vpath_str) - 1]))
733 command_insert (cmdline, PATH_SEP_STR, FALSE);
735 vfs_path_free (cwd_vpath);
738 /* --------------------------------------------------------------------------------------------- */
740 static void
741 put_link (WPanel * panel)
743 if (!command_prompt)
744 return;
745 if (S_ISLNK (selection (panel)->st.st_mode))
747 char buffer[MC_MAXPATHLEN];
748 vfs_path_t *vpath;
749 int i;
751 vpath = vfs_path_append_new (panel->cwd_vpath, selection (panel)->fname, (char *) NULL);
752 i = mc_readlink (vpath, buffer, sizeof (buffer) - 1);
753 vfs_path_free (vpath);
755 if (i > 0)
757 buffer[i] = '\0';
758 command_insert (cmdline, buffer, TRUE);
763 /* --------------------------------------------------------------------------------------------- */
765 static void
766 put_current_link (void)
768 put_link (current_panel);
771 /* --------------------------------------------------------------------------------------------- */
773 static void
774 put_other_link (void)
776 if (get_other_type () == view_listing)
777 put_link (other_panel);
780 /* --------------------------------------------------------------------------------------------- */
782 /** Insert the selected file name into the input line */
783 static void
784 put_current_selected (void)
786 const char *tmp;
788 if (!command_prompt)
789 return;
791 if (get_current_type () == view_tree)
793 WTree *tree;
794 const vfs_path_t *selected_name;
796 tree = (WTree *) get_panel_widget (get_current_index ());
797 selected_name = tree_selected_name (tree);
798 tmp = vfs_path_as_str (selected_name);
800 else
801 tmp = selection (current_panel)->fname;
803 command_insert (cmdline, tmp, TRUE);
806 /* --------------------------------------------------------------------------------------------- */
808 static void
809 put_tagged (WPanel * panel)
811 if (!command_prompt)
812 return;
813 input_disable_update (cmdline);
814 if (panel->marked)
816 int i;
818 for (i = 0; i < panel->dir.len; i++)
820 if (panel->dir.list[i].f.marked)
821 command_insert (cmdline, panel->dir.list[i].fname, TRUE);
824 else
826 command_insert (cmdline, panel->dir.list[panel->selected].fname, TRUE);
828 input_enable_update (cmdline);
831 /* --------------------------------------------------------------------------------------------- */
833 static void
834 put_current_tagged (void)
836 put_tagged (current_panel);
839 /* --------------------------------------------------------------------------------------------- */
841 static void
842 put_other_tagged (void)
844 if (get_other_type () == view_listing)
845 put_tagged (other_panel);
848 /* --------------------------------------------------------------------------------------------- */
850 static void
851 ctl_x_cmd (void)
853 ctl_x_map_enabled = TRUE;
856 /* --------------------------------------------------------------------------------------------- */
858 static void
859 setup_mc (void)
861 #ifdef HAVE_SLANG
862 #ifdef HAVE_CHARSET
863 tty_display_8bit (TRUE);
864 #else
865 tty_display_8bit (mc_global.full_eight_bits);
866 #endif /* HAVE_CHARSET */
868 #else /* HAVE_SLANG */
870 #ifdef HAVE_CHARSET
871 tty_display_8bit (TRUE);
872 #else
873 tty_display_8bit (mc_global.eight_bit_clean);
874 #endif /* HAVE_CHARSET */
875 #endif /* HAVE_SLANG */
877 #ifdef ENABLE_SUBSHELL
878 if (mc_global.tty.use_subshell)
879 add_select_channel (mc_global.tty.subshell_pty, load_prompt, 0);
880 #endif /* !ENABLE_SUBSHELL */
882 if ((tty_baudrate () < 9600) || mc_global.tty.slow_terminal)
883 verbose = FALSE;
886 /* --------------------------------------------------------------------------------------------- */
888 static void
889 setup_dummy_mc (void)
891 vfs_path_t *vpath;
892 char *d;
893 int ret;
895 d = _vfs_get_cwd ();
896 setup_mc ();
897 vpath = vfs_path_from_str (d);
898 ret = mc_chdir (vpath);
899 (void) ret;
900 vfs_path_free (vpath);
901 g_free (d);
904 /* --------------------------------------------------------------------------------------------- */
906 static void
907 done_mc (void)
909 /* Setup shutdown
911 * We sync the profiles since the hotlist may have changed, while
912 * we only change the setup data if we have the auto save feature set
914 const char *curr_dir;
916 save_setup (auto_save_setup, panels_options.auto_save_setup);
918 curr_dir = vfs_get_current_dir ();
919 vfs_stamp_path (curr_dir);
921 if ((current_panel != NULL) && (get_current_type () == view_listing))
922 vfs_stamp_path (vfs_path_as_str (current_panel->cwd_vpath));
924 if ((other_panel != NULL) && (get_other_type () == view_listing))
925 vfs_stamp_path (vfs_path_as_str (other_panel->cwd_vpath));
928 /* --------------------------------------------------------------------------------------------- */
930 static void
931 create_panels_and_run_mc (void)
933 midnight_dlg->get_shortcut = midnight_get_shortcut;
934 midnight_dlg->get_title = midnight_get_title;
935 /* allow rebind tab */
936 widget_want_tab (WIDGET (midnight_dlg), TRUE);
938 create_panels ();
940 add_widget (midnight_dlg, the_menubar);
941 init_menu ();
943 add_widget (midnight_dlg, get_panel_widget (0));
944 add_widget (midnight_dlg, get_panel_widget (1));
946 add_widget (midnight_dlg, the_hint);
947 add_widget (midnight_dlg, cmdline);
948 add_widget (midnight_dlg, the_prompt);
950 add_widget (midnight_dlg, the_bar);
951 midnight_set_buttonbar (the_bar);
953 /* Run the Midnight Commander if no file was specified in the command line */
954 dlg_run (midnight_dlg);
957 /* --------------------------------------------------------------------------------------------- */
959 /** result must be free'd (I think this should go in util.c) */
960 static vfs_path_t *
961 prepend_cwd_on_local (const char *filename)
963 vfs_path_t *vpath;
965 vpath = vfs_path_from_str (filename);
966 if (!vfs_file_is_local (vpath) || g_path_is_absolute (filename))
967 return vpath;
969 vfs_path_free (vpath);
971 return vfs_path_append_new (vfs_get_raw_current_dir (), filename, (char *) NULL);
974 /* --------------------------------------------------------------------------------------------- */
976 /** Invoke the internal view/edit routine with:
977 * the default processing and forcing the internal viewer/editor
979 static gboolean
980 mc_maybe_editor_or_viewer (void)
982 gboolean ret;
984 switch (mc_global.mc_run_mode)
986 #ifdef USE_INTERNAL_EDIT
987 case MC_RUN_EDITOR:
988 ret = edit_files ((GList *) mc_run_param0);
989 break;
990 #endif /* USE_INTERNAL_EDIT */
991 case MC_RUN_VIEWER:
993 vfs_path_t *vpath = NULL;
995 if (mc_run_param0 != NULL && *(char *) mc_run_param0 != '\0')
996 vpath = prepend_cwd_on_local ((char *) mc_run_param0);
998 ret = view_file (vpath, FALSE, TRUE);
999 vfs_path_free (vpath);
1000 break;
1002 #ifdef USE_DIFF_VIEW
1003 case MC_RUN_DIFFVIEWER:
1004 ret = dview_diff_cmd (mc_run_param0, mc_run_param1);
1005 break;
1006 #endif /* USE_DIFF_VIEW */
1007 default:
1008 ret = FALSE;
1011 return ret;
1014 /* --------------------------------------------------------------------------------------------- */
1016 static gboolean
1017 quit_cmd_internal (int quiet)
1019 int q = quit;
1020 size_t n;
1022 n = dialog_switch_num () - 1;
1023 if (n != 0)
1025 char msg[BUF_MEDIUM];
1027 g_snprintf (msg, sizeof (msg),
1028 ngettext ("You have %zu opened screen. Quit anyway?",
1029 "You have %zu opened screens. Quit anyway?", n), n);
1031 if (query_dialog (_("The Midnight Commander"), msg, D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
1032 return FALSE;
1033 q = 1;
1035 else if (quiet || !confirm_exit)
1036 q = 1;
1037 else if (query_dialog (_("The Midnight Commander"),
1038 _("Do you really want to quit the Midnight Commander?"),
1039 D_NORMAL, 2, _("&Yes"), _("&No")) == 0)
1040 q = 1;
1042 if (q != 0)
1044 #ifdef ENABLE_SUBSHELL
1045 if (!mc_global.tty.use_subshell)
1046 stop_dialogs ();
1047 else if ((q = exit_subshell ()? 1 : 0) != 0)
1048 #endif
1049 stop_dialogs ();
1052 if (q != 0)
1053 quit |= 1;
1054 return (quit != 0);
1057 /* --------------------------------------------------------------------------------------------- */
1059 static gboolean
1060 quit_cmd (void)
1062 return quit_cmd_internal (0);
1065 /* --------------------------------------------------------------------------------------------- */
1068 * Repaint the contents of the panels without frames. To schedule panel
1069 * for repainting, set panel->dirty to 1. There are many reasons why
1070 * the panels need to be repainted, and this is a costly operation, so
1071 * it's done once per event.
1074 static void
1075 update_dirty_panels (void)
1077 if (get_current_type () == view_listing && current_panel->dirty)
1078 widget_redraw (WIDGET (current_panel));
1080 if (get_other_type () == view_listing && other_panel->dirty)
1081 widget_redraw (WIDGET (other_panel));
1084 /* --------------------------------------------------------------------------------------------- */
1086 static void
1087 toggle_show_hidden (void)
1089 panels_options.show_dot_files = !panels_options.show_dot_files;
1090 update_panels (UP_RELOAD, UP_KEEPSEL);
1091 /* redraw panels forced */
1092 update_dirty_panels ();
1095 /* --------------------------------------------------------------------------------------------- */
1097 static cb_ret_t
1098 midnight_execute_cmd (Widget * sender, long command)
1100 cb_ret_t res = MSG_HANDLED;
1102 (void) sender;
1104 /* stop quick search before executing any command */
1105 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1107 switch (command)
1109 case CK_ChangePanel:
1110 change_panel ();
1111 break;
1112 case CK_HotListAdd:
1113 add2hotlist_cmd ();
1114 break;
1115 case CK_PanelListingChange:
1116 change_listing_cmd ();
1117 break;
1118 case CK_ChangeMode:
1119 chmod_cmd ();
1120 break;
1121 case CK_ChangeOwn:
1122 chown_cmd ();
1123 break;
1124 case CK_ChangeOwnAdvanced:
1125 chown_advanced_cmd ();
1126 break;
1127 case CK_CompareDirs:
1128 compare_dirs_cmd ();
1129 break;
1130 case CK_Options:
1131 configure_box ();
1132 break;
1133 #ifdef ENABLE_VFS
1134 case CK_OptionsVfs:
1135 configure_vfs ();
1136 break;
1137 #endif
1138 case CK_OptionsConfirm:
1139 confirm_box ();
1140 break;
1141 case CK_Copy:
1142 copy_cmd ();
1143 break;
1144 case CK_PutCurrentPath:
1145 midnight_put_panel_path (current_panel);
1146 break;
1147 case CK_PutCurrentSelected:
1148 put_current_selected ();
1149 break;
1150 case CK_PutCurrentFullSelected:
1151 midnight_put_panel_path (current_panel);
1152 put_current_selected ();
1153 break;
1154 case CK_PutCurrentLink:
1155 put_current_link ();
1156 break;
1157 case CK_PutCurrentTagged:
1158 put_current_tagged ();
1159 break;
1160 case CK_PutOtherPath:
1161 if (get_other_type () == view_listing)
1162 midnight_put_panel_path (other_panel);
1163 break;
1164 case CK_PutOtherLink:
1165 put_other_link ();
1166 break;
1167 case CK_PutOtherTagged:
1168 put_other_tagged ();
1169 break;
1170 case CK_Delete:
1171 delete_cmd ();
1172 break;
1173 case CK_ScreenList:
1174 dialog_switch_list ();
1175 break;
1176 #ifdef USE_DIFF_VIEW
1177 case CK_CompareFiles:
1178 diff_view_cmd ();
1179 break;
1180 #endif
1181 case CK_OptionsDisplayBits:
1182 display_bits_box ();
1183 break;
1184 case CK_Edit:
1185 edit_cmd ();
1186 break;
1187 #ifdef USE_INTERNAL_EDIT
1188 case CK_EditForceInternal:
1189 edit_cmd_force_internal ();
1190 break;
1191 #endif
1192 case CK_EditExtensionsFile:
1193 ext_cmd ();
1194 break;
1195 case CK_EditFileHighlightFile:
1196 edit_fhl_cmd ();
1197 break;
1198 case CK_EditUserMenu:
1199 edit_mc_menu_cmd ();
1200 break;
1201 case CK_LinkSymbolicEdit:
1202 edit_symlink_cmd ();
1203 break;
1204 case CK_ExternalPanelize:
1205 external_panelize ();
1206 break;
1207 case CK_Filter:
1208 filter_cmd ();
1209 break;
1210 case CK_ViewFiltered:
1211 view_filtered_cmd ();
1212 break;
1213 case CK_Find:
1214 find_cmd ();
1215 break;
1216 #ifdef ENABLE_VFS_FISH
1217 case CK_ConnectFish:
1218 fishlink_cmd ();
1219 break;
1220 #endif
1221 #ifdef ENABLE_VFS_FTP
1222 case CK_ConnectFtp:
1223 ftplink_cmd ();
1224 break;
1225 #endif
1226 #ifdef ENABLE_VFS_SFTP
1227 case CK_ConnectSftp:
1228 sftplink_cmd ();
1229 break;
1230 #endif
1231 #ifdef ENABLE_VFS_SMB
1232 case CK_ConnectSmb:
1233 smblink_cmd ();
1234 break;
1235 #endif /* ENABLE_VFS_SMB */
1236 case CK_Panelize:
1237 cd_panelize_cmd ();
1238 break;
1239 case CK_Help:
1240 help_cmd ();
1241 break;
1242 case CK_History:
1243 /* show the history of command line widget */
1244 send_message (cmdline, NULL, MSG_ACTION, CK_History, NULL);
1245 break;
1246 case CK_PanelInfo:
1247 if (sender == WIDGET (the_menubar))
1248 info_cmd (); /* menu */
1249 else
1250 info_cmd_no_menu (); /* shortcut or buttonbar */
1251 break;
1252 #ifdef ENABLE_BACKGROUND
1253 case CK_Jobs:
1254 jobs_cmd ();
1255 break;
1256 #endif
1257 case CK_OptionsLayout:
1258 layout_box ();
1259 break;
1260 case CK_OptionsAppearance:
1261 appearance_box ();
1262 break;
1263 case CK_LearnKeys:
1264 learn_keys ();
1265 break;
1266 case CK_Link:
1267 link_cmd (LINK_HARDLINK);
1268 break;
1269 case CK_PanelListing:
1270 listing_cmd ();
1271 break;
1272 #ifdef LISTMODE_EDITOR
1273 case CK_ListMode:
1274 listmode_cmd ();
1275 break;
1276 #endif
1277 case CK_Menu:
1278 menu_cmd ();
1279 break;
1280 case CK_MenuLastSelected:
1281 menu_last_selected_cmd ();
1282 break;
1283 case CK_MakeDir:
1284 mkdir_cmd ();
1285 break;
1286 case CK_OptionsPanel:
1287 panel_options_box ();
1288 break;
1289 #ifdef HAVE_CHARSET
1290 case CK_SelectCodepage:
1291 encoding_cmd ();
1292 break;
1293 #endif
1294 case CK_CdQuick:
1295 quick_cd_cmd ();
1296 break;
1297 case CK_HotList:
1298 hotlist_cmd ();
1299 break;
1300 case CK_PanelQuickView:
1301 if (sender == WIDGET (the_menubar))
1302 quick_view_cmd (); /* menu */
1303 else
1304 quick_cmd_no_menu (); /* shortcut or buttonabr */
1305 break;
1306 case CK_QuitQuiet:
1307 quiet_quit_cmd ();
1308 break;
1309 case CK_Quit:
1310 quit_cmd ();
1311 break;
1312 case CK_LinkSymbolicRelative:
1313 link_cmd (LINK_SYMLINK_RELATIVE);
1314 break;
1315 case CK_Move:
1316 rename_cmd ();
1317 break;
1318 case CK_Reread:
1319 reread_cmd ();
1320 break;
1321 #ifdef ENABLE_VFS
1322 case CK_VfsList:
1323 vfs_list ();
1324 break;
1325 #endif
1326 case CK_SaveSetup:
1327 save_setup_cmd ();
1328 break;
1329 case CK_Select:
1330 case CK_Unselect:
1331 case CK_SelectInvert:
1332 res = send_message (current_panel, midnight_dlg, MSG_ACTION, command, NULL);
1333 break;
1334 case CK_Shell:
1335 view_other_cmd ();
1336 break;
1337 case CK_DirSize:
1338 smart_dirsize_cmd ();
1339 break;
1340 case CK_Sort:
1341 sort_cmd ();
1342 break;
1343 case CK_ExtendedKeyMap:
1344 ctl_x_cmd ();
1345 break;
1346 case CK_Suspend:
1347 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
1348 break;
1349 case CK_Swap:
1350 swap_cmd ();
1351 break;
1352 case CK_LinkSymbolic:
1353 link_cmd (LINK_SYMLINK_ABSOLUTE);
1354 break;
1355 case CK_ShowHidden:
1356 toggle_show_hidden ();
1357 break;
1358 case CK_SplitVertHoriz:
1359 toggle_panels_split ();
1360 break;
1361 case CK_SplitEqual:
1362 panels_split_equal ();
1363 break;
1364 case CK_SplitMore:
1365 panels_split_more ();
1366 break;
1367 case CK_SplitLess:
1368 panels_split_less ();
1369 break;
1370 case CK_PanelTree:
1371 panel_tree_cmd ();
1372 break;
1373 case CK_Tree:
1374 treebox_cmd ();
1375 break;
1376 #ifdef ENABLE_VFS_UNDELFS
1377 case CK_Undelete:
1378 undelete_cmd ();
1379 break;
1380 #endif
1381 case CK_UserMenu:
1382 user_file_menu_cmd ();
1383 break;
1384 case CK_View:
1385 view_cmd ();
1386 break;
1387 case CK_ViewFile:
1388 view_file_cmd ();
1389 break;
1390 case CK_Cancel:
1391 /* don't close panels due to SIGINT */
1392 break;
1393 default:
1394 res = MSG_NOT_HANDLED;
1397 return res;
1400 /* --------------------------------------------------------------------------------------------- */
1402 static cb_ret_t
1403 midnight_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
1405 long command;
1407 switch (msg)
1409 case MSG_INIT:
1410 panel_init ();
1411 setup_panels ();
1412 return MSG_HANDLED;
1414 case MSG_DRAW:
1415 load_hint (TRUE);
1416 /* We handle the special case of the output lines */
1417 if (mc_global.tty.console_flag != '\0' && output_lines)
1418 show_console_contents (output_start_y,
1419 LINES - output_lines - mc_global.keybar_visible -
1420 1, LINES - mc_global.keybar_visible - 1);
1421 return MSG_HANDLED;
1423 case MSG_RESIZE:
1424 /* dlg_set_size() is surplus for this case */
1425 w->lines = LINES;
1426 w->cols = COLS;
1427 setup_panels ();
1428 menubar_arrange (the_menubar);
1429 return MSG_HANDLED;
1431 case MSG_IDLE:
1432 /* We only need the first idle event to show user menu after start */
1433 widget_idle (w, FALSE);
1435 if (boot_current_is_left)
1436 widget_select (get_panel_widget (0));
1437 else
1438 widget_select (get_panel_widget (1));
1440 if (auto_menu)
1441 midnight_execute_cmd (NULL, CK_UserMenu);
1442 return MSG_HANDLED;
1444 case MSG_KEY:
1445 if (ctl_x_map_enabled)
1447 ctl_x_map_enabled = FALSE;
1448 command = keybind_lookup_keymap_command (main_x_map, parm);
1449 if (command != CK_IgnoreKey)
1450 return midnight_execute_cmd (NULL, command);
1453 /* FIXME: should handle all menu shortcuts before this point */
1454 if (widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1455 return MSG_NOT_HANDLED;
1457 if (parm == '\n')
1459 size_t i;
1461 /* HACK: don't execute command in the command line if Enter was pressed
1462 in the quick viewer panel. */
1463 /* TODO: currently, when one of panels is other than view_listing,
1464 current_panel points to view_listing panel all time independently of
1465 it's activity. Thus, we can't use get_current_type() here.
1466 current_panel should point to actualy current active panel
1467 independently of it's type. */
1468 if (current_panel->active == 0 && get_other_type () == view_quick)
1469 return MSG_NOT_HANDLED;
1471 for (i = 0; cmdline->buffer[i] != '\0' && whitespace (cmdline->buffer[i]); i++)
1474 if (cmdline->buffer[i] != '\0')
1476 send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1477 return MSG_HANDLED;
1480 input_insert (cmdline, "", FALSE);
1481 cmdline->point = 0;
1484 if ((!mc_global.tty.alternate_plus_minus
1485 || !(mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)) && !quote
1486 && !current_panel->searching)
1488 if (!only_leading_plus_minus)
1490 /* Special treatement, since the input line will eat them */
1491 if (parm == '+')
1492 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1494 if (parm == '\\' || parm == '-')
1495 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1496 NULL);
1498 if (parm == '*')
1499 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1500 NULL);
1502 else if (!command_prompt || cmdline->buffer[0] == '\0')
1504 /* Special treatement '+', '-', '\', '*' only when this is
1505 * first char on input line
1507 if (parm == '+')
1508 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1510 if (parm == '\\' || parm == '-')
1511 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1512 NULL);
1514 if (parm == '*')
1515 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1516 NULL);
1519 return MSG_NOT_HANDLED;
1521 case MSG_HOTKEY_HANDLED:
1522 if ((get_current_type () == view_listing) && current_panel->searching)
1524 current_panel->dirty = 1; /* FIXME: unneeded? */
1525 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1527 return MSG_HANDLED;
1529 case MSG_UNHANDLED_KEY:
1531 cb_ret_t v = MSG_NOT_HANDLED;
1533 if (ctl_x_map_enabled)
1535 ctl_x_map_enabled = FALSE;
1536 command = keybind_lookup_keymap_command (main_x_map, parm);
1538 else
1539 command = keybind_lookup_keymap_command (main_map, parm);
1541 if (command != CK_IgnoreKey)
1542 v = midnight_execute_cmd (NULL, command);
1544 if (v == MSG_NOT_HANDLED && command_prompt)
1545 v = send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1547 return v;
1550 case MSG_POST_KEY:
1551 if (!widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1552 update_dirty_panels ();
1553 return MSG_HANDLED;
1555 case MSG_ACTION:
1556 /* Handle shortcuts, menu, and buttonbar. */
1557 return midnight_execute_cmd (sender, parm);
1559 case MSG_END:
1560 panel_deinit ();
1561 return MSG_HANDLED;
1563 default:
1564 return dlg_default_callback (w, sender, msg, parm, data);
1568 /* --------------------------------------------------------------------------------------------- */
1569 /*** public functions ****************************************************************************/
1570 /* --------------------------------------------------------------------------------------------- */
1572 void
1573 update_menu (void)
1575 menu_set_name (left_menu, panels_layout.horizontal_split ? _("&Above") : _("&Left"));
1576 menu_set_name (right_menu, panels_layout.horizontal_split ? _("&Below") : _("&Right"));
1577 menubar_arrange (the_menubar);
1578 menubar_set_visible (the_menubar, menubar_visible);
1581 /* --------------------------------------------------------------------------------------------- */
1583 void
1584 midnight_set_buttonbar (WButtonBar * b)
1586 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1587 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1588 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1589 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1590 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1591 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1592 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1593 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1594 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1595 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1598 /* --------------------------------------------------------------------------------------------- */
1600 * Return a random hint. If force is TRUE, ignore the timeout.
1603 char *
1604 get_random_hint (gboolean force)
1606 static const guint64 update_period = 60 * G_USEC_PER_SEC;
1607 static guint64 tv = 0;
1609 char *data, *result = NULL, *eop;
1610 size_t len, start;
1611 GIConv conv;
1613 /* Do not change hints more often than one minute */
1614 if (!force && !mc_time_elapsed (&tv, update_period))
1615 return g_strdup ("");
1617 data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
1618 if (data == NULL)
1619 return NULL;
1621 /* get a random entry */
1622 srand ((unsigned int) (tv / G_USEC_PER_SEC));
1623 start = ((size_t) rand ()) % (len - 1);
1625 /* Search the start of paragraph */
1626 for (; start != 0; start--)
1627 if (data[start] == '\n' && data[start + 1] == '\n')
1629 start += 2;
1630 break;
1633 /* Search the end of paragraph */
1634 for (eop = data + start; *eop != '\0'; eop++)
1636 if (*eop == '\n' && *(eop + 1) == '\n')
1638 *eop = '\0';
1639 break;
1641 if (*eop == '\n')
1642 *eop = ' ';
1645 /* hint files are stored in utf-8 */
1646 /* try convert hint file from utf-8 to terminal encoding */
1647 conv = str_crt_conv_from ("UTF-8");
1648 if (conv != INVALID_CONV)
1650 GString *buffer;
1652 buffer = g_string_sized_new (len - start);
1653 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1654 result = g_string_free (buffer, FALSE);
1655 else
1656 g_string_free (buffer, TRUE);
1657 str_close_conv (conv);
1659 else
1660 result = g_strndup (data + start, len - start);
1662 g_free (data);
1663 return result;
1667 /* --------------------------------------------------------------------------------------------- */
1669 * Load new hint and display it.
1670 * IF force is not 0, ignore the timeout.
1673 void
1674 load_hint (gboolean force)
1676 char *hint;
1678 if (WIDGET (the_hint)->owner == NULL)
1679 return;
1681 if (!mc_global.message_visible)
1683 label_set_text (the_hint, NULL);
1684 return;
1687 hint = get_random_hint (force);
1689 if (hint != NULL)
1691 if (*hint != '\0')
1692 set_hintbar (hint);
1693 g_free (hint);
1695 else
1697 char text[BUF_SMALL];
1699 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1700 set_hintbar (text);
1704 /* --------------------------------------------------------------------------------------------- */
1706 void
1707 change_panel (void)
1709 input_free_completions (cmdline);
1710 dlg_select_next_widget (midnight_dlg);
1713 /* --------------------------------------------------------------------------------------------- */
1715 /** Save current stat of directories to avoid reloading the panels
1716 * when no modifications have taken place
1718 void
1719 save_cwds_stat (void)
1721 if (panels_options.fast_reload)
1723 mc_stat (current_panel->cwd_vpath, &(current_panel->dir_stat));
1724 if (get_other_type () == view_listing)
1725 mc_stat (other_panel->cwd_vpath, &(other_panel->dir_stat));
1729 /* --------------------------------------------------------------------------------------------- */
1731 gboolean
1732 quiet_quit_cmd (void)
1734 print_last_revert = TRUE;
1735 return quit_cmd_internal (1);
1738 /* --------------------------------------------------------------------------------------------- */
1740 /** Run the main dialog that occupies the whole screen */
1741 gboolean
1742 do_nc (void)
1744 gboolean ret;
1746 #ifdef USE_INTERNAL_EDIT
1747 edit_stack_init ();
1748 #endif
1750 midnight_dlg = dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, dialog_colors,
1751 midnight_callback, NULL, "[main]", NULL);
1753 /* Check if we were invoked as an editor or file viewer */
1754 if (mc_global.mc_run_mode != MC_RUN_FULL)
1756 setup_dummy_mc ();
1757 ret = mc_maybe_editor_or_viewer ();
1759 else
1761 /* We only need the first idle event to show user menu after start */
1762 widget_idle (WIDGET (midnight_dlg), TRUE);
1764 setup_mc ();
1765 mc_filehighlight = mc_fhl_new (TRUE);
1766 create_panels_and_run_mc ();
1767 mc_fhl_free (&mc_filehighlight);
1769 ret = TRUE;
1771 /* dlg_destroy destroys even current_panel->cwd_vpath, so we have to save a copy :) */
1772 if (mc_args__last_wd_file != NULL && vfs_current_is_local ())
1773 last_wd_string = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
1775 /* don't handle VFS timestamps for dirs opened in panels */
1776 mc_event_destroy (MCEVENT_GROUP_CORE, "vfs_timestamp");
1778 dir_list_clean (&panelized_panel.list);
1781 /* Program end */
1782 mc_global.midnight_shutdown = TRUE;
1783 dialog_switch_shutdown ();
1784 done_mc ();
1785 dlg_destroy (midnight_dlg);
1786 current_panel = NULL;
1788 #ifdef USE_INTERNAL_EDIT
1789 edit_stack_free ();
1790 #endif
1792 if ((quit & SUBSHELL_EXIT) == 0)
1793 clr_scr ();
1795 return ret;
1798 /* --------------------------------------------------------------------------------------------- */