Manual pages: mcmcdiffmceditmcview

root/src/filemanager/filemanager.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. stop_dialogs
  2. treebox_cmd
  3. listmode_cmd
  4. create_panel_menu
  5. create_file_menu
  6. create_command_menu
  7. create_options_menu
  8. init_menu
  9. menu_last_selected_cmd
  10. menu_cmd
  11. sort_cmd
  12. midnight_get_shortcut
  13. midnight_get_title
  14. toggle_panels_split
  15. check_panel_timestamp
  16. check_current_panel_timestamp
  17. check_other_panel_timestamp
  18. print_vfs_message
  19. create_panels
  20. midnight_put_panel_path
  21. put_link
  22. put_current_link
  23. put_other_link
  24. put_current_selected
  25. put_tagged
  26. put_current_tagged
  27. put_other_tagged
  28. setup_mc
  29. setup_dummy_mc
  30. done_mc
  31. create_file_manager
  32. prepend_cwd_on_local
  33. mc_maybe_editor_or_viewer
  34. show_editor_viewer_history
  35. quit_cmd_internal
  36. quit_cmd
  37. update_dirty_panels
  38. toggle_show_hidden
  39. midnight_execute_cmd
  40. is_cmdline_mute
  41. handle_cmdline_enter
  42. midnight_callback
  43. update_menu
  44. midnight_set_buttonbar
  45. get_random_hint
  46. load_hint
  47. change_panel
  48. save_cwds_stat
  49. quiet_quit_cmd
  50. do_nc

   1 /*
   2    Main dialog (file panels) of the Midnight Commander
   3 
   4    Copyright (C) 1994-2025
   5    Free Software Foundation, Inc.
   6 
   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-2022
  12    Slava Zanko <slavazanko@gmail.com>, 2013
  13 
  14    This file is part of the Midnight Commander.
  15 
  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.
  20 
  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.
  25 
  26    You should have received a copy of the GNU General Public License
  27    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  28  */
  29 
  30 /** \file filemanager.c
  31  *  \brief Source: main dialog (file panels) of the Midnight Commander
  32  */
  33 
  34 #include <config.h>
  35 
  36 #include <ctype.h>
  37 #include <locale.h>
  38 #include <stdio.h>
  39 #include <stdlib.h>
  40 #include <string.h>
  41 #include <sys/types.h>
  42 #include <sys/stat.h>
  43 #include <sys/wait.h>
  44 #include <pwd.h>  // for username in xterm title
  45 
  46 #include "lib/global.h"
  47 #include "lib/fileloc.h"  // MC_HINT, MC_FILEPOS_FILE
  48 #include "lib/tty/tty.h"
  49 #include "lib/tty/key.h"  // KEY_M_* masks
  50 #include "lib/skin.h"
  51 #include "lib/util.h"
  52 #include "lib/vfs/vfs.h"
  53 
  54 #include "src/args.h"
  55 #ifdef ENABLE_SUBSHELL
  56 #include "src/subshell/subshell.h"
  57 #endif
  58 #include "src/execute.h"  // toggle_subshell
  59 #include "src/setup.h"    // variables
  60 #include "src/learn.h"    // learn_keys()
  61 #include "src/keymap.h"
  62 #include "src/usermenu.h"  // user_file_menu_cmd()
  63 
  64 #include "lib/keybind.h"
  65 #include "lib/event.h"
  66 
  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()
  75 
  76 #ifdef USE_INTERNAL_EDIT
  77 #include "src/editor/edit.h"
  78 #endif
  79 
  80 #ifdef USE_DIFF_VIEW
  81 #include "src/diffviewer/ydiff.h"
  82 #endif
  83 
  84 #include "src/consaver/cons.saver.h"  // show_console_contents
  85 #include "src/file_history.h"         // show_file_history()
  86 
  87 #include "filemanager.h"
  88 
  89 /*** global variables ****************************************************************************/
  90 
  91 /* When the modes are active, left_panel, right_panel and tree_panel */
  92 /* point to a proper data structure.  You should check with the functions */
  93 /* get_current_type and get_other_type the types of the panels before using */
  94 /* this pointer variables */
  95 
  96 /* The structures for the panels */
  97 WPanel *left_panel = NULL;
  98 WPanel *right_panel = NULL;
  99 /* Pointer to the selected and unselected panel */
 100 WPanel *current_panel = NULL;
 101 
 102 /* The Menubar */
 103 WMenuBar *the_menubar = NULL;
 104 /* The widget where we draw the prompt */
 105 WLabel *the_prompt;
 106 /* The hint bar */
 107 WLabel *the_hint;
 108 /* The button bar */
 109 WButtonBar *the_bar;
 110 
 111 /* The prompt */
 112 const char *mc_prompt = NULL;
 113 
 114 /*** file scope macro definitions ****************************************************************/
 115 
 116 /*** file scope type declarations ****************************************************************/
 117 
 118 /*** forward declarations (file scope functions) *************************************************/
 119 
 120 /*** file scope variables ************************************************************************/
 121 
 122 static menu_t *left_menu, *right_menu;
 123 
 124 /* --------------------------------------------------------------------------------------------- */
 125 /*** file scope functions ************************************************************************/
 126 /* --------------------------------------------------------------------------------------------- */
 127 
 128 /** Stop MC main dialog and the current dialog if it exists.
 129  * Needed to provide fast exit from MC viewer or editor on shell exit */
 130 static void
 131 stop_dialogs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 132 {
 133     dlg_close (filemanager);
 134 
 135     if (top_dlg != NULL)
 136         dlg_close (DIALOG (top_dlg->data));
 137 }
 138 
 139 /* --------------------------------------------------------------------------------------------- */
 140 
 141 static void
 142 treebox_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 143 {
 144     const file_entry_t *fe;
 145     char *sel_dir;
 146 
 147     fe = panel_current_entry (current_panel);
 148     if (fe == NULL)
 149         return;
 150 
 151     sel_dir = tree_box (fe->fname->str);
 152     if (sel_dir != NULL)
 153     {
 154         vfs_path_t *sel_vdir;
 155 
 156         sel_vdir = vfs_path_from_str (sel_dir);
 157         panel_cd (current_panel, sel_vdir, cd_exact);
 158         vfs_path_free (sel_vdir, TRUE);
 159         g_free (sel_dir);
 160     }
 161 }
 162 
 163 /* --------------------------------------------------------------------------------------------- */
 164 
 165 #ifdef LISTMODE_EDITOR
 166 static void
 167 listmode_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 168 {
 169     char *newmode;
 170 
 171     if (get_current_type () != view_listing)
 172         return;
 173 
 174     newmode = listmode_edit (current_panel->user_format);
 175     if (!newmode)
 176         return;
 177 
 178     g_free (current_panel->user_format);
 179     current_panel->list_format = list_user;
 180     current_panel->user_format = newmode;
 181     set_panel_formats (current_panel);
 182 
 183     do_refresh ();
 184 }
 185 #endif
 186 
 187 /* --------------------------------------------------------------------------------------------- */
 188 
 189 static GList *
 190 create_panel_menu (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 191 {
 192     GList *entries = NULL;
 193 
 194     entries = g_list_prepend (entries, menu_entry_new (_ ("File listin&g"), CK_PanelListing));
 195     entries = g_list_prepend (entries, menu_entry_new (_ ("&Quick view"), CK_PanelQuickView));
 196     entries = g_list_prepend (entries, menu_entry_new (_ ("&Info"), CK_PanelInfo));
 197     entries = g_list_prepend (entries, menu_entry_new (_ ("&Tree"), CK_PanelTree));
 198     entries = g_list_prepend (entries, menu_separator_new ());
 199     entries =
 200         g_list_prepend (entries, menu_entry_new (_ ("&Listing format..."), CK_SetupListingFormat));
 201     entries = g_list_prepend (entries, menu_entry_new (_ ("&Sort order..."), CK_Sort));
 202     entries = g_list_prepend (entries, menu_entry_new (_ ("&Filter..."), CK_Filter));
 203     entries = g_list_prepend (entries, menu_entry_new (_ ("&Encoding..."), CK_SelectCodepage));
 204     entries = g_list_prepend (entries, menu_separator_new ());
 205 #ifdef ENABLE_VFS_FTP
 206     entries = g_list_prepend (entries, menu_entry_new (_ ("FT&P link..."), CK_ConnectFtp));
 207 #endif
 208 #ifdef ENABLE_VFS_SHELL
 209     entries = g_list_prepend (entries, menu_entry_new (_ ("S&hell link..."), CK_ConnectShell));
 210 #endif
 211 #ifdef ENABLE_VFS_SFTP
 212     entries = g_list_prepend (entries, menu_entry_new (_ ("SFTP li&nk..."), CK_ConnectSftp));
 213 #endif
 214     entries = g_list_prepend (entries, menu_entry_new (_ ("Paneli&ze"), CK_Panelize));
 215     entries = g_list_prepend (entries, menu_separator_new ());
 216     entries = g_list_prepend (entries, menu_entry_new (_ ("&Rescan"), CK_Reread));
 217 
 218     return g_list_reverse (entries);
 219 }
 220 
 221 /* --------------------------------------------------------------------------------------------- */
 222 
 223 static GList *
 224 create_file_menu (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 225 {
 226     GList *entries = NULL;
 227 
 228     entries = g_list_prepend (entries, menu_entry_new (_ ("&View"), CK_View));
 229     entries = g_list_prepend (entries, menu_entry_new (_ ("Vie&w file..."), CK_ViewFile));
 230     entries = g_list_prepend (entries, menu_entry_new (_ ("&Filtered view"), CK_ViewFiltered));
 231     entries = g_list_prepend (entries, menu_entry_new (_ ("&Edit"), CK_Edit));
 232     entries = g_list_prepend (entries, menu_entry_new (_ ("&Copy"), CK_Copy));
 233     entries = g_list_prepend (entries, menu_entry_new (_ ("C&hmod"), CK_ChangeMode));
 234     entries = g_list_prepend (entries, menu_entry_new (_ ("&Link"), CK_Link));
 235     entries = g_list_prepend (entries, menu_entry_new (_ ("&Symlink"), CK_LinkSymbolic));
 236     entries =
 237         g_list_prepend (entries, menu_entry_new (_ ("Relative symlin&k"), CK_LinkSymbolicRelative));
 238     entries = g_list_prepend (entries, menu_entry_new (_ ("Edit s&ymlink"), CK_LinkSymbolicEdit));
 239     entries = g_list_prepend (entries, menu_entry_new (_ ("Ch&own"), CK_ChangeOwn));
 240     entries =
 241         g_list_prepend (entries, menu_entry_new (_ ("&Advanced chown"), CK_ChangeOwnAdvanced));
 242 #ifdef ENABLE_EXT2FS_ATTR
 243     entries = g_list_prepend (entries, menu_entry_new (_ ("Cha&ttr"), CK_ChangeAttributes));
 244 #endif
 245     entries = g_list_prepend (entries, menu_entry_new (_ ("&Rename/Move"), CK_Move));
 246     entries = g_list_prepend (entries, menu_entry_new (_ ("&Mkdir"), CK_MakeDir));
 247     entries = g_list_prepend (entries, menu_entry_new (_ ("&Delete"), CK_Delete));
 248     entries = g_list_prepend (entries, menu_entry_new (_ ("&Quick cd"), CK_CdQuick));
 249     entries = g_list_prepend (entries, menu_separator_new ());
 250     entries = g_list_prepend (entries, menu_entry_new (_ ("Select &group"), CK_Select));
 251     entries = g_list_prepend (entries, menu_entry_new (_ ("U&nselect group"), CK_Unselect));
 252     entries = g_list_prepend (entries, menu_entry_new (_ ("&Invert selection"), CK_SelectInvert));
 253     entries = g_list_prepend (entries, menu_separator_new ());
 254     entries = g_list_prepend (entries, menu_entry_new (_ ("E&xit"), CK_Quit));
 255 
 256     return g_list_reverse (entries);
 257 }
 258 
 259 /* --------------------------------------------------------------------------------------------- */
 260 
 261 static GList *
 262 create_command_menu (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 263 {
 264     /* I know, I'm lazy, but the tree widget when it's not running
 265      * as a panel still has some problems, I have not yet finished
 266      * the WTree widget port, sorry.
 267      */
 268     GList *entries = NULL;
 269 
 270     entries = g_list_prepend (entries, menu_entry_new (_ ("&User menu"), CK_UserMenu));
 271     entries = g_list_prepend (entries, menu_entry_new (_ ("&Directory tree"), CK_Tree));
 272     entries = g_list_prepend (entries, menu_entry_new (_ ("&Find file"), CK_Find));
 273     entries = g_list_prepend (entries, menu_entry_new (_ ("S&wap panels"), CK_Swap));
 274     entries = g_list_prepend (entries, menu_entry_new (_ ("Switch &panels on/off"), CK_Shell));
 275     entries = g_list_prepend (entries, menu_entry_new (_ ("&Compare directories"), CK_CompareDirs));
 276 #ifdef USE_DIFF_VIEW
 277     entries = g_list_prepend (entries, menu_entry_new (_ ("C&ompare files"), CK_CompareFiles));
 278 #endif
 279     entries =
 280         g_list_prepend (entries, menu_entry_new (_ ("E&xternal panelize"), CK_ExternalPanelize));
 281     entries = g_list_prepend (entries, menu_entry_new (_ ("Show directory s&izes"), CK_DirSize));
 282     entries = g_list_prepend (entries, menu_separator_new ());
 283     entries = g_list_prepend (entries, menu_entry_new (_ ("Command &history"), CK_History));
 284     entries = g_list_prepend (
 285         entries, menu_entry_new (_ ("Viewed/edited files hi&story"), CK_EditorViewerHistory));
 286     entries = g_list_prepend (entries, menu_entry_new (_ ("Di&rectory hotlist"), CK_HotList));
 287 #ifdef ENABLE_VFS
 288     entries = g_list_prepend (entries, menu_entry_new (_ ("&Active VFS list"), CK_VfsList));
 289 #endif
 290 #ifdef ENABLE_BACKGROUND
 291     entries = g_list_prepend (entries, menu_entry_new (_ ("&Background jobs"), CK_Jobs));
 292 #endif
 293     entries = g_list_prepend (entries, menu_entry_new (_ ("Screen lis&t"), CK_ScreenList));
 294     entries = g_list_prepend (entries, menu_separator_new ());
 295 #ifdef ENABLE_VFS_UNDELFS
 296     entries =
 297         g_list_prepend (entries, menu_entry_new (_ ("&Undelete files (ext2fs only)"), CK_Undelete));
 298 #endif
 299 #ifdef LISTMODE_EDITOR
 300     entries = g_list_prepend (entries, menu_entry_new (_ ("&Listing format edit"), CK_ListMode));
 301 #endif
 302 #if defined(ENABLE_VFS_UNDELFS) || defined(LISTMODE_EDITOR)
 303     entries = g_list_prepend (entries, menu_separator_new ());
 304 #endif
 305     entries = g_list_prepend (entries,
 306                               menu_entry_new (_ ("Edit &extension file"), CK_EditExtensionsFile));
 307     entries = g_list_prepend (entries, menu_entry_new (_ ("Edit &menu file"), CK_EditUserMenu));
 308     entries = g_list_prepend (
 309         entries, menu_entry_new (_ ("Edit hi&ghlighting group file"), CK_EditFileHighlightFile));
 310 
 311     return g_list_reverse (entries);
 312 }
 313 
 314 /* --------------------------------------------------------------------------------------------- */
 315 
 316 static GList *
 317 create_options_menu (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 318 {
 319     GList *entries = NULL;
 320 
 321     entries = g_list_prepend (entries, menu_entry_new (_ ("&Configuration..."), CK_Options));
 322     entries = g_list_prepend (entries, menu_entry_new (_ ("&Layout..."), CK_OptionsLayout));
 323     entries = g_list_prepend (entries, menu_entry_new (_ ("&Panel options..."), CK_OptionsPanel));
 324     entries = g_list_prepend (entries, menu_entry_new (_ ("C&onfirmation..."), CK_OptionsConfirm));
 325     entries = g_list_prepend (entries, menu_entry_new (_ ("&Appearance..."), CK_OptionsAppearance));
 326     entries = g_list_prepend (entries, menu_entry_new (_ ("Learn &keys..."), CK_LearnKeys));
 327 #ifdef ENABLE_VFS
 328     entries = g_list_prepend (entries, menu_entry_new (_ ("&Virtual FS..."), CK_OptionsVfs));
 329 #endif
 330     entries = g_list_prepend (entries, menu_separator_new ());
 331     entries = g_list_prepend (entries, menu_entry_new (_ ("&Save setup"), CK_SaveSetup));
 332 
 333     return g_list_reverse (entries);
 334 }
 335 
 336 /* --------------------------------------------------------------------------------------------- */
 337 
 338 static void
 339 init_menu (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 340 {
 341     left_menu = menu_new ("", create_panel_menu (), "[Left and Right Menus]");
 342     menubar_add_menu (the_menubar, left_menu);
 343     menubar_add_menu (the_menubar, menu_new (_ ("&File"), create_file_menu (), "[File Menu]"));
 344     menubar_add_menu (the_menubar,
 345                       menu_new (_ ("&Command"), create_command_menu (), "[Command Menu]"));
 346     menubar_add_menu (the_menubar,
 347                       menu_new (_ ("&Options"), create_options_menu (), "[Options Menu]"));
 348     right_menu = menu_new ("", create_panel_menu (), "[Left and Right Menus]");
 349     menubar_add_menu (the_menubar, right_menu);
 350     update_menu ();
 351 }
 352 
 353 /* --------------------------------------------------------------------------------------------- */
 354 
 355 static void
 356 menu_last_selected_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 357 {
 358     menubar_activate (the_menubar, drop_menus, -1);
 359 }
 360 
 361 /* --------------------------------------------------------------------------------------------- */
 362 
 363 static void
 364 menu_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 365 {
 366     int selected;
 367 
 368     if ((get_current_index () == 0) == current_panel->active)
 369         selected = 0;
 370     else
 371         selected = g_list_length (the_menubar->menu) - 1;
 372 
 373     menubar_activate (the_menubar, drop_menus, selected);
 374 }
 375 
 376 /* --------------------------------------------------------------------------------------------- */
 377 
 378 static void
 379 sort_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 380 {
 381     WPanel *p;
 382     const panel_field_t *sort_order;
 383 
 384     if (!SELECTED_IS_PANEL)
 385         return;
 386 
 387     p = MENU_PANEL;
 388     sort_order = sort_box (&p->sort_info, p->sort_field);
 389     panel_set_sort_order (p, sort_order);
 390 }
 391 
 392 /* --------------------------------------------------------------------------------------------- */
 393 
 394 static char *
 395 midnight_get_shortcut (long command)
     /* [previous][next][first][last][top][bottom][index][help]  */
 396 {
 397     const char *ext_map;
 398     const char *shortcut = NULL;
 399 
 400     shortcut = keybind_lookup_keymap_shortcut (filemanager_map, command);
 401     if (shortcut != NULL)
 402         return g_strdup (shortcut);
 403 
 404     shortcut = keybind_lookup_keymap_shortcut (panel_map, command);
 405     if (shortcut != NULL)
 406         return g_strdup (shortcut);
 407 
 408     ext_map = keybind_lookup_keymap_shortcut (filemanager_map, CK_ExtendedKeyMap);
 409     if (ext_map != NULL)
 410         shortcut = keybind_lookup_keymap_shortcut (filemanager_x_map, command);
 411     if (shortcut != NULL)
 412         return g_strdup_printf ("%s %s", ext_map, shortcut);
 413 
 414     return NULL;
 415 }
 416 
 417 /* --------------------------------------------------------------------------------------------- */
 418 
 419 static char *
 420 midnight_get_title (const WDialog *h, size_t len)
     /* [previous][next][first][last][top][bottom][index][help]  */
 421 {
 422     char *path;
 423     char *login;
 424     char *p;
 425 
 426     (void) h;
 427 
 428     title_path_prepare (&path, &login);
 429 
 430     p = g_strdup_printf ("%s [%s]:%s", _ ("Panels:"), login, path);
 431     g_free (path);
 432     g_free (login);
 433     path = g_strdup (str_trunc (p, len - 4));
 434     g_free (p);
 435 
 436     return path;
 437 }
 438 
 439 /* --------------------------------------------------------------------------------------------- */
 440 
 441 static void
 442 toggle_panels_split (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 443 {
 444     panels_layout.horizontal_split = !panels_layout.horizontal_split;
 445     layout_change ();
 446     do_refresh ();
 447 }
 448 
 449 /* --------------------------------------------------------------------------------------------- */
 450 
 451 #ifdef ENABLE_VFS
 452 /* event helper */
 453 static gboolean
 454 check_panel_timestamp (const WPanel *panel, panel_view_mode_t mode, const struct vfs_class *vclass,
     /* [previous][next][first][last][top][bottom][index][help]  */
 455                        const vfsid id)
 456 {
 457     return (mode != view_listing
 458             || (vfs_path_get_last_path_vfs (panel->cwd_vpath) == vclass
 459                 && vfs_getid (panel->cwd_vpath) == id));
 460 }
 461 
 462 /* --------------------------------------------------------------------------------------------- */
 463 
 464 /* event callback */
 465 static gboolean
 466 check_current_panel_timestamp (const gchar *event_group_name, const gchar *event_name,
     /* [previous][next][first][last][top][bottom][index][help]  */
 467                                gpointer init_data, gpointer data)
 468 {
 469     ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
 470 
 471     (void) event_group_name;
 472     (void) event_name;
 473     (void) init_data;
 474 
 475     event_data->ret = check_panel_timestamp (current_panel, get_current_type (), event_data->vclass,
 476                                              event_data->id);
 477     return !event_data->ret;
 478 }
 479 
 480 /* --------------------------------------------------------------------------------------------- */
 481 
 482 /* event callback */
 483 static gboolean
 484 check_other_panel_timestamp (const gchar *event_group_name, const gchar *event_name,
     /* [previous][next][first][last][top][bottom][index][help]  */
 485                              gpointer init_data, gpointer data)
 486 {
 487     ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
 488 
 489     (void) event_group_name;
 490     (void) event_name;
 491     (void) init_data;
 492 
 493     event_data->ret =
 494         check_panel_timestamp (other_panel, get_other_type (), event_data->vclass, event_data->id);
 495     return !event_data->ret;
 496 }
 497 #endif
 498 
 499 /* --------------------------------------------------------------------------------------------- */
 500 
 501 /* event callback */
 502 static gboolean
 503 print_vfs_message (const gchar *event_group_name, const gchar *event_name, gpointer init_data,
     /* [previous][next][first][last][top][bottom][index][help]  */
 504                    gpointer data)
 505 {
 506     ev_vfs_print_message_t *event_data = (ev_vfs_print_message_t *) data;
 507 
 508     (void) event_group_name;
 509     (void) event_name;
 510     (void) init_data;
 511 
 512     if (mc_global.midnight_shutdown)
 513         goto ret;
 514 
 515     if (!mc_global.message_visible || the_hint == NULL || WIDGET (the_hint)->owner == NULL)
 516     {
 517         int col, row;
 518 
 519         if (!nice_rotating_dash || (ok_to_refresh <= 0))
 520             goto ret;
 521 
 522         // Preserve current cursor position
 523         tty_getyx (&row, &col);
 524 
 525         tty_gotoyx (0, 0);
 526         tty_setcolor (NORMAL_COLOR);
 527         tty_print_string (str_fit_to_term (event_data->msg, COLS - 1, J_LEFT));
 528 
 529         // Restore cursor position
 530         tty_gotoyx (row, col);
 531         mc_refresh ();
 532         goto ret;
 533     }
 534 
 535     if (mc_global.message_visible)
 536         set_hintbar (event_data->msg);
 537 
 538 ret:
 539     MC_PTR_FREE (event_data->msg);
 540     return TRUE;
 541 }
 542 
 543 /* --------------------------------------------------------------------------------------------- */
 544 
 545 static void
 546 create_panels (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 547 {
 548     int current_index, other_index;
 549     panel_view_mode_t current_mode, other_mode;
 550     char *current_dir, *other_dir;
 551     vfs_path_t *original_dir;
 552 
 553     /*
 554      * Following cases from command line are possible:
 555      * 'mc' (no arguments):            mc_run_param0 == NULL, mc_run_param1 == NULL
 556      *                                 active panel uses current directory
 557      *                                 passive panel uses "other_dir" from panels.ini
 558      *
 559      * 'mc dir1 dir2' (two arguments): mc_run_param0 != NULL, mc_run_param1 != NULL
 560      *                                 active panel uses mc_run_param0
 561      *                                 passive panel uses mc_run_param1
 562      *
 563      * 'mc dir1' (single argument):    mc_run_param0 != NULL, mc_run_param1 == NULL
 564      *                                 active panel uses mc_run_param0
 565      *                                 passive panel uses "other_dir" from panels.ini
 566      */
 567 
 568     // Set up panel directories
 569     if (boot_current_is_left)
 570     {
 571         // left panel is active
 572         current_index = 0;
 573         other_index = 1;
 574         current_mode = startup_left_mode;
 575         other_mode = startup_right_mode;
 576 
 577         if (mc_run_param0 == NULL && mc_run_param1 == NULL)
 578         {
 579             // no arguments
 580             current_dir = NULL;           // assume current dir
 581             other_dir = saved_other_dir;  // from ini
 582         }
 583         else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
 584         {
 585             // two arguments
 586             current_dir = (char *) mc_run_param0;
 587             other_dir = mc_run_param1;
 588         }
 589         else  // mc_run_param0 != NULL && mc_run_param1 == NULL
 590         {
 591             // one argument
 592             current_dir = (char *) mc_run_param0;
 593             other_dir = saved_other_dir;  // from ini
 594         }
 595     }
 596     else
 597     {
 598         // right panel is active
 599         current_index = 1;
 600         other_index = 0;
 601         current_mode = startup_right_mode;
 602         other_mode = startup_left_mode;
 603 
 604         if (mc_run_param0 == NULL && mc_run_param1 == NULL)
 605         {
 606             // no arguments
 607             current_dir = NULL;           // assume current dir
 608             other_dir = saved_other_dir;  // from ini
 609         }
 610         else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
 611         {
 612             // two arguments
 613             current_dir = (char *) mc_run_param0;
 614             other_dir = mc_run_param1;
 615         }
 616         else  // mc_run_param0 != NULL && mc_run_param1 == NULL
 617         {
 618             // one argument
 619             current_dir = (char *) mc_run_param0;
 620             other_dir = saved_other_dir;  // from ini
 621         }
 622     }
 623 
 624     // 1. Get current dir
 625     original_dir = vfs_path_clone (vfs_get_raw_current_dir ());
 626 
 627     // 2. Create passive panel
 628     if (other_dir != NULL)
 629     {
 630         vfs_path_t *vpath;
 631 
 632         if (g_path_is_absolute (other_dir))
 633             vpath = vfs_path_from_str (other_dir);
 634         else
 635             vpath = vfs_path_append_new (original_dir, other_dir, (char *) NULL);
 636         mc_chdir (vpath);
 637         vfs_path_free (vpath, TRUE);
 638     }
 639     create_panel (other_index, other_mode);
 640 
 641     // 3. Create active panel
 642     if (current_dir == NULL)
 643         mc_chdir (original_dir);
 644     else
 645     {
 646         vfs_path_t *vpath;
 647 
 648         if (g_path_is_absolute (current_dir))
 649             vpath = vfs_path_from_str (current_dir);
 650         else
 651             vpath = vfs_path_append_new (original_dir, current_dir, (char *) NULL);
 652         mc_chdir (vpath);
 653         vfs_path_free (vpath, TRUE);
 654     }
 655     create_panel (current_index, current_mode);
 656 
 657     if (startup_left_mode == view_listing)
 658         current_panel = left_panel;
 659     else if (right_panel != NULL)
 660         current_panel = right_panel;
 661     else
 662         current_panel = left_panel;
 663 
 664     vfs_path_free (original_dir, TRUE);
 665 
 666 #ifdef ENABLE_VFS
 667     mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_other_panel_timestamp, NULL, NULL);
 668     mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_current_panel_timestamp, NULL, NULL);
 669 #endif
 670 
 671     mc_event_add (MCEVENT_GROUP_CORE, "vfs_print_message", print_vfs_message, NULL, NULL);
 672 }
 673 
 674 /* --------------------------------------------------------------------------------------------- */
 675 
 676 static void
 677 midnight_put_panel_path (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
 678 {
 679     vfs_path_t *cwd_vpath;
 680     const char *cwd_vpath_str;
 681 
 682     if (!command_prompt)
 683         return;
 684 
 685     cwd_vpath = remove_encoding_from_path (panel->cwd_vpath);
 686     cwd_vpath_str = vfs_path_as_str (cwd_vpath);
 687 
 688     command_insert (cmdline, cwd_vpath_str, FALSE);
 689 
 690     if (!IS_PATH_SEP (cwd_vpath_str[strlen (cwd_vpath_str) - 1]))
 691         command_insert (cmdline, PATH_SEP_STR, FALSE);
 692 
 693     vfs_path_free (cwd_vpath, TRUE);
 694 }
 695 
 696 /* --------------------------------------------------------------------------------------------- */
 697 
 698 static void
 699 put_link (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
 700 {
 701     const file_entry_t *fe;
 702 
 703     if (!command_prompt)
 704         return;
 705 
 706     fe = panel_current_entry (panel);
 707 
 708     if (fe != NULL && S_ISLNK (fe->st.st_mode))
 709     {
 710         char buffer[MC_MAXPATHLEN];
 711         vfs_path_t *vpath;
 712         int i;
 713 
 714         vpath = vfs_path_append_new (panel->cwd_vpath, fe->fname->str, (char *) NULL);
 715         i = mc_readlink (vpath, buffer, sizeof (buffer) - 1);
 716         vfs_path_free (vpath, TRUE);
 717 
 718         if (i > 0)
 719         {
 720             buffer[i] = '\0';
 721             command_insert (cmdline, buffer, TRUE);
 722         }
 723     }
 724 }
 725 
 726 /* --------------------------------------------------------------------------------------------- */
 727 
 728 static void
 729 put_current_link (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 730 {
 731     put_link (current_panel);
 732 }
 733 
 734 /* --------------------------------------------------------------------------------------------- */
 735 
 736 static void
 737 put_other_link (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 738 {
 739     if (get_other_type () == view_listing)
 740         put_link (other_panel);
 741 }
 742 
 743 /* --------------------------------------------------------------------------------------------- */
 744 
 745 /** Insert the selected file name into the input line */
 746 static void
 747 put_current_selected (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 748 {
 749     if (!command_prompt)
 750         return;
 751 
 752     if (get_current_type () == view_tree)
 753     {
 754         WTree *tree;
 755         const vfs_path_t *selected_name;
 756 
 757         tree = (WTree *) get_panel_widget (get_current_index ());
 758         selected_name = tree_selected_name (tree);
 759         command_insert (cmdline, vfs_path_as_str (selected_name), TRUE);
 760     }
 761     else
 762     {
 763         const file_entry_t *fe;
 764 
 765         fe = panel_current_entry (current_panel);
 766         if (fe != NULL)
 767             command_insert (cmdline, fe->fname->str, TRUE);
 768     }
 769 }
 770 
 771 /* --------------------------------------------------------------------------------------------- */
 772 
 773 static void
 774 put_tagged (WPanel *panel)
     /* [previous][next][first][last][top][bottom][index][help]  */
 775 {
 776     if (!command_prompt)
 777         return;
 778 
 779     input_disable_update (cmdline);
 780 
 781     if (panel->marked == 0)
 782     {
 783         const file_entry_t *fe;
 784 
 785         fe = panel_current_entry (current_panel);
 786         if (fe != NULL)
 787             command_insert (cmdline, fe->fname->str, TRUE);
 788     }
 789     else
 790     {
 791         int i;
 792 
 793         for (i = 0; i < panel->dir.len; i++)
 794             if (panel->dir.list[i].f.marked != 0)
 795                 command_insert (cmdline, panel->dir.list[i].fname->str, TRUE);
 796     }
 797 
 798     input_enable_update (cmdline);
 799 }
 800 
 801 /* --------------------------------------------------------------------------------------------- */
 802 
 803 static void
 804 put_current_tagged (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 805 {
 806     put_tagged (current_panel);
 807 }
 808 
 809 /* --------------------------------------------------------------------------------------------- */
 810 
 811 static void
 812 put_other_tagged (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 813 {
 814     if (get_other_type () == view_listing)
 815         put_tagged (other_panel);
 816 }
 817 
 818 /* --------------------------------------------------------------------------------------------- */
 819 
 820 static void
 821 setup_mc (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 822 {
 823     tty_display_8bit (TRUE);
 824 
 825     const int baudrate = tty_baudrate ();
 826     if ((baudrate > 0 && baudrate < 9600) || mc_global.tty.slow_terminal)
 827         verbose = FALSE;
 828 }
 829 
 830 /* --------------------------------------------------------------------------------------------- */
 831 
 832 static void
 833 setup_dummy_mc (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 834 {
 835     vfs_path_t *vpath;
 836     char *d;
 837     int ret;
 838 
 839     d = vfs_get_cwd ();
 840     setup_mc ();
 841     vpath = vfs_path_from_str (d);
 842     ret = mc_chdir (vpath);
 843     (void) ret;
 844     vfs_path_free (vpath, TRUE);
 845     g_free (d);
 846 }
 847 
 848 /* --------------------------------------------------------------------------------------------- */
 849 
 850 static void
 851 done_mc (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 852 {
 853     /* Setup shutdown
 854      *
 855      * We sync the profiles since the hotlist may have changed, while
 856      * we only change the setup data if we have the auto save feature set
 857      */
 858 
 859     save_setup (auto_save_setup, panels_options.auto_save_setup);
 860 
 861     vfs_stamp_path (vfs_get_raw_current_dir ());
 862 }
 863 
 864 /* --------------------------------------------------------------------------------------------- */
 865 
 866 static void
 867 create_file_manager (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 868 {
 869     Widget *w = WIDGET (filemanager);
 870     WGroup *g = GROUP (filemanager);
 871 
 872     w->keymap = filemanager_map;
 873     w->ext_keymap = filemanager_x_map;
 874 
 875     filemanager->get_shortcut = midnight_get_shortcut;
 876     filemanager->get_title = midnight_get_title;
 877     // allow rebind tab
 878     widget_want_tab (w, TRUE);
 879 
 880     the_menubar = menubar_new (NULL);
 881     group_add_widget (g, the_menubar);
 882     init_menu ();
 883 
 884     create_panels ();
 885     group_add_widget (g, get_panel_widget (0));
 886     group_add_widget (g, get_panel_widget (1));
 887 
 888     the_hint = label_new (0, 0, NULL);
 889     the_hint->transparent = TRUE;
 890     the_hint->auto_adjust_cols = 0;
 891     WIDGET (the_hint)->rect.cols = COLS;
 892     group_add_widget (g, the_hint);
 893 
 894     cmdline = command_new (0, 0, 0);
 895     group_add_widget (g, cmdline);
 896 
 897     the_prompt = label_new (0, 0, mc_prompt);
 898     the_prompt->transparent = TRUE;
 899     group_add_widget (g, the_prompt);
 900 
 901     the_bar = buttonbar_new ();
 902     group_add_widget (g, the_bar);
 903     midnight_set_buttonbar (the_bar);
 904 
 905 #ifdef ENABLE_SUBSHELL
 906     /* Must be done after creation of cmdline and prompt widgets to avoid potential
 907        NULL dereference in load_prompt() -> ... -> setup_cmdline() -> label_set_text(). */
 908     if (mc_global.tty.use_subshell)
 909         add_select_channel (mc_global.tty.subshell_pty, load_prompt, NULL);
 910 #endif
 911 }
 912 
 913 /* --------------------------------------------------------------------------------------------- */
 914 
 915 /** result must be free'd (I think this should go in util.c) */
 916 static vfs_path_t *
 917 prepend_cwd_on_local (const char *filename)
     /* [previous][next][first][last][top][bottom][index][help]  */
 918 {
 919     vfs_path_t *vpath;
 920 
 921     vpath = vfs_path_from_str (filename);
 922     if (!vfs_file_is_local (vpath) || g_path_is_absolute (filename))
 923         return vpath;
 924 
 925     vfs_path_free (vpath, TRUE);
 926 
 927     return vfs_path_append_new (vfs_get_raw_current_dir (), filename, (char *) NULL);
 928 }
 929 
 930 /* --------------------------------------------------------------------------------------------- */
 931 
 932 /** Invoke the internal view/edit routine with:
 933  * the default processing and forcing the internal viewer/editor
 934  */
 935 static gboolean
 936 mc_maybe_editor_or_viewer (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 937 {
 938     gboolean ret;
 939 
 940     switch (mc_global.mc_run_mode)
 941     {
 942 #ifdef USE_INTERNAL_EDIT
 943     case MC_RUN_EDITOR:
 944         ret = edit_files ((GList *) mc_run_param0);
 945         break;
 946 #endif
 947     case MC_RUN_VIEWER:
 948     {
 949         vfs_path_t *vpath = NULL;
 950 
 951         if (mc_run_param0 != NULL && *(char *) mc_run_param0 != '\0')
 952             vpath = prepend_cwd_on_local ((char *) mc_run_param0);
 953 
 954         ret = view_file (vpath, FALSE, TRUE);
 955         vfs_path_free (vpath, TRUE);
 956         break;
 957     }
 958 #ifdef USE_DIFF_VIEW
 959     case MC_RUN_DIFFVIEWER:
 960         ret = dview_diff_cmd (mc_run_param0, mc_run_param1);
 961         break;
 962 #endif
 963     default:
 964         ret = FALSE;
 965     }
 966 
 967     return ret;
 968 }
 969 
 970 /* --------------------------------------------------------------------------------------------- */
 971 
 972 static void
 973 show_editor_viewer_history (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 974 {
 975     char *s;
 976     int act;
 977 
 978     s = show_file_history (WIDGET (filemanager), &act);
 979     if (s != NULL)
 980     {
 981         vfs_path_t *s_vpath;
 982 
 983         switch (act)
 984         {
 985         case CK_Edit:
 986             s_vpath = vfs_path_from_str (s);
 987             edit_file_at_line (s_vpath, use_internal_edit, 0);
 988             break;
 989 
 990         case CK_View:
 991             s_vpath = vfs_path_from_str (s);
 992             view_file (s_vpath, use_internal_view, FALSE);
 993             break;
 994 
 995         default:
 996         {
 997             char *d;
 998 
 999             d = g_path_get_dirname (s);
1000             s_vpath = vfs_path_from_str (d);
1001             panel_cd (current_panel, s_vpath, cd_exact);
1002             panel_set_current_by_name (current_panel, s);
1003             g_free (d);
1004         }
1005         }
1006 
1007         g_free (s);
1008         vfs_path_free (s_vpath, TRUE);
1009     }
1010 }
1011 
1012 /* --------------------------------------------------------------------------------------------- */
1013 
1014 static gboolean
1015 quit_cmd_internal (int quiet)
     /* [previous][next][first][last][top][bottom][index][help]  */
1016 {
1017     int q = quit;
1018     size_t n;
1019 
1020     n = dialog_switch_num () - 1;
1021     if (n != 0)
1022     {
1023         char msg[BUF_MEDIUM];
1024 
1025         g_snprintf (msg, sizeof (msg),
1026                     ngettext ("You have %zu opened screen. Quit anyway?",
1027                               "You have %zu opened screens. Quit anyway?", n),
1028                     n);
1029 
1030         if (query_dialog (_ ("The Midnight Commander"), msg, D_NORMAL, 2, _ ("&Yes"), _ ("&No"))
1031             != 0)
1032             return FALSE;
1033         q = 1;
1034     }
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?"), D_NORMAL, 2,
1039                            _ ("&Yes"), _ ("&No"))
1040              == 0)
1041         q = 1;
1042 
1043     if (q != 0)
1044     {
1045 #ifdef ENABLE_SUBSHELL
1046         if (!mc_global.tty.use_subshell)
1047             stop_dialogs ();
1048         else if ((q = exit_subshell () ? 1 : 0) != 0)
1049 #endif
1050             stop_dialogs ();
1051     }
1052 
1053     if (q != 0)
1054         quit |= 1;
1055     return (quit != 0);
1056 }
1057 
1058 /* --------------------------------------------------------------------------------------------- */
1059 
1060 static gboolean
1061 quit_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1062 {
1063     return quit_cmd_internal (0);
1064 }
1065 
1066 /* --------------------------------------------------------------------------------------------- */
1067 
1068 /**
1069  * Repaint the contents of the panels without frames.  To schedule panel
1070  * for repainting, set panel->dirty to TRUE.  There are many reasons why
1071  * the panels need to be repainted, and this is a costly operation, so
1072  * it's done once per event.
1073  */
1074 
1075 static void
1076 update_dirty_panels (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1077 {
1078     if (get_current_type () == view_listing && current_panel->dirty)
1079         widget_draw (WIDGET (current_panel));
1080 
1081     if (get_other_type () == view_listing && other_panel->dirty)
1082         widget_draw (WIDGET (other_panel));
1083 }
1084 
1085 /* --------------------------------------------------------------------------------------------- */
1086 
1087 static void
1088 toggle_show_hidden (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1089 {
1090     panels_options.show_dot_files = !panels_options.show_dot_files;
1091     update_panels (UP_RELOAD, UP_KEEPSEL);
1092     // redraw panels forced
1093     update_dirty_panels ();
1094 }
1095 
1096 /* --------------------------------------------------------------------------------------------- */
1097 
1098 static cb_ret_t
1099 midnight_execute_cmd (Widget *sender, long command)
     /* [previous][next][first][last][top][bottom][index][help]  */
1100 {
1101     cb_ret_t res = MSG_HANDLED;
1102 
1103     (void) sender;
1104 
1105     // stop quick search before executing any command
1106     send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1107 
1108     switch (command)
1109     {
1110     case CK_ChangePanel:
1111         (void) change_panel ();
1112         break;
1113     case CK_HotListAdd:
1114         add2hotlist_cmd (current_panel);
1115         break;
1116     case CK_SetupListingFormat:
1117         setup_listing_format_cmd ();
1118         break;
1119     case CK_ChangeMode:
1120         chmod_cmd (current_panel);
1121         break;
1122     case CK_ChangeOwn:
1123         chown_cmd (current_panel);
1124         break;
1125     case CK_ChangeOwnAdvanced:
1126         advanced_chown_cmd (current_panel);
1127         break;
1128 #ifdef ENABLE_EXT2FS_ATTR
1129     case CK_ChangeAttributes:
1130         chattr_cmd (current_panel);
1131         break;
1132 #endif
1133     case CK_CompareDirs:
1134         compare_dirs_cmd ();
1135         break;
1136     case CK_Options:
1137         configure_box ();
1138         break;
1139 #ifdef ENABLE_VFS
1140     case CK_OptionsVfs:
1141         configure_vfs_box ();
1142         break;
1143 #endif
1144     case CK_OptionsConfirm:
1145         confirm_box ();
1146         break;
1147     case CK_Copy:
1148         copy_cmd (current_panel);
1149         break;
1150     case CK_PutCurrentPath:
1151         midnight_put_panel_path (current_panel);
1152         break;
1153     case CK_PutCurrentSelected:
1154         put_current_selected ();
1155         break;
1156     case CK_PutCurrentFullSelected:
1157         midnight_put_panel_path (current_panel);
1158         put_current_selected ();
1159         break;
1160     case CK_PutCurrentLink:
1161         put_current_link ();
1162         break;
1163     case CK_PutCurrentTagged:
1164         put_current_tagged ();
1165         break;
1166     case CK_PutOtherPath:
1167         if (get_other_type () == view_listing)
1168             midnight_put_panel_path (other_panel);
1169         break;
1170     case CK_PutOtherLink:
1171         put_other_link ();
1172         break;
1173     case CK_PutOtherTagged:
1174         put_other_tagged ();
1175         break;
1176     case CK_Delete:
1177         delete_cmd (current_panel);
1178         break;
1179     case CK_ScreenList:
1180         dialog_switch_list ();
1181         break;
1182 #ifdef USE_DIFF_VIEW
1183     case CK_CompareFiles:
1184         diff_view_cmd ();
1185         break;
1186 #endif
1187     case CK_Edit:
1188         edit_cmd (current_panel);
1189         break;
1190 #ifdef USE_INTERNAL_EDIT
1191     case CK_EditForceInternal:
1192         edit_cmd_force_internal (current_panel);
1193         break;
1194 #endif
1195     case CK_EditExtensionsFile:
1196         ext_cmd ();
1197         break;
1198     case CK_EditFileHighlightFile:
1199         edit_fhl_cmd ();
1200         break;
1201     case CK_EditUserMenu:
1202         edit_mc_menu_cmd ();
1203         break;
1204     case CK_LinkSymbolicEdit:
1205         edit_symlink_cmd ();
1206         break;
1207     case CK_ExternalPanelize:
1208         external_panelize_cmd ();
1209         break;
1210     case CK_ViewFiltered:
1211         view_filtered_cmd (current_panel);
1212         break;
1213     case CK_Find:
1214         find_cmd (current_panel);
1215         break;
1216 #ifdef ENABLE_VFS_SHELL
1217     case CK_ConnectShell:
1218         shelllink_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     case CK_Panelize:
1232         panel_panelize_cd ();
1233         break;
1234     case CK_Help:
1235         help_cmd ();
1236         break;
1237     case CK_History:
1238         // show the history of command line widget
1239         send_message (cmdline, NULL, MSG_ACTION, CK_History, NULL);
1240         break;
1241     case CK_PanelInfo:
1242         if (sender == WIDGET (the_menubar))
1243             info_cmd ();  // menu
1244         else
1245             info_cmd_no_menu ();  // shortcut or buttonbar
1246         break;
1247 #ifdef ENABLE_BACKGROUND
1248     case CK_Jobs:
1249         jobs_box ();
1250         break;
1251 #endif
1252     case CK_OptionsLayout:
1253         layout_box ();
1254         break;
1255     case CK_OptionsAppearance:
1256         appearance_box ();
1257         break;
1258     case CK_LearnKeys:
1259         learn_keys ();
1260         break;
1261     case CK_Link:
1262         link_cmd (LINK_HARDLINK);
1263         break;
1264     case CK_PanelListing:
1265         listing_cmd ();
1266         break;
1267 #ifdef LISTMODE_EDITOR
1268     case CK_ListMode:
1269         listmode_cmd ();
1270         break;
1271 #endif
1272     case CK_Menu:
1273         menu_cmd ();
1274         break;
1275     case CK_MenuLastSelected:
1276         menu_last_selected_cmd ();
1277         break;
1278     case CK_MakeDir:
1279         mkdir_cmd (current_panel);
1280         break;
1281     case CK_OptionsPanel:
1282         panel_options_box ();
1283         break;
1284     case CK_SelectCodepage:
1285         encoding_cmd ();
1286         break;
1287     case CK_CdQuick:
1288         quick_cd_cmd (current_panel);
1289         break;
1290     case CK_HotList:
1291         hotlist_cmd (current_panel);
1292         break;
1293     case CK_PanelQuickView:
1294         if (sender == WIDGET (the_menubar))
1295             quick_view_cmd ();  // menu
1296         else
1297             quick_cmd_no_menu ();  // shortcut or buttonabr
1298         break;
1299     case CK_QuitQuiet:
1300         quiet_quit_cmd ();
1301         break;
1302     case CK_Quit:
1303         quit_cmd ();
1304         break;
1305     case CK_LinkSymbolicRelative:
1306         link_cmd (LINK_SYMLINK_RELATIVE);
1307         break;
1308     case CK_Move:
1309         rename_cmd (current_panel);
1310         break;
1311     case CK_Reread:
1312         reread_cmd ();
1313         break;
1314 #ifdef ENABLE_VFS
1315     case CK_VfsList:
1316         vfs_list (current_panel);
1317         break;
1318 #endif
1319     case CK_SaveSetup:
1320         save_setup_cmd ();
1321         break;
1322     case CK_Select:
1323     case CK_Unselect:
1324     case CK_SelectInvert:
1325     case CK_Filter:
1326         res = send_message (current_panel, filemanager, MSG_ACTION, command, NULL);
1327         break;
1328     case CK_Shell:
1329         toggle_subshell ();
1330         break;
1331     case CK_DirSize:
1332         smart_dirsize_cmd (current_panel);
1333         break;
1334     case CK_Sort:
1335         sort_cmd ();
1336         break;
1337     case CK_ExtendedKeyMap:
1338         WIDGET (filemanager)->ext_mode = TRUE;
1339         break;
1340     case CK_Suspend:
1341         mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
1342         break;
1343     case CK_Swap:
1344         swap_cmd ();
1345         break;
1346     case CK_LinkSymbolic:
1347         link_cmd (LINK_SYMLINK_ABSOLUTE);
1348         break;
1349     case CK_ShowHidden:
1350         toggle_show_hidden ();
1351         break;
1352     case CK_SplitVertHoriz:
1353         toggle_panels_split ();
1354         break;
1355     case CK_SplitEqual:
1356         panels_split_equal ();
1357         break;
1358     case CK_SplitMore:
1359         panels_split_more ();
1360         break;
1361     case CK_SplitLess:
1362         panels_split_less ();
1363         break;
1364     case CK_PanelTree:
1365         panel_tree_cmd ();
1366         break;
1367     case CK_Tree:
1368         treebox_cmd ();
1369         break;
1370 #ifdef ENABLE_VFS_UNDELFS
1371     case CK_Undelete:
1372         undelete_cmd ();
1373         break;
1374 #endif
1375     case CK_UserMenu:
1376         user_file_menu_cmd ();
1377         break;
1378     case CK_View:
1379         view_cmd (current_panel);
1380         break;
1381     case CK_ViewFile:
1382         view_file_cmd (current_panel);
1383         break;
1384     case CK_EditorViewerHistory:
1385         show_editor_viewer_history ();
1386         break;
1387     case CK_Cancel:
1388         // don't close panels due to SIGINT
1389         break;
1390     default:
1391         res = MSG_NOT_HANDLED;
1392     }
1393 
1394     return res;
1395 }
1396 
1397 /* --------------------------------------------------------------------------------------------- */
1398 
1399 /**
1400  * Whether the command-line should not respond to key events.
1401  *
1402  * This is TRUE if a QuickView or TreeView have the focus, as they're going
1403  * to consume some keys and there's no sense in passing to the command-line
1404  * just the leftovers.
1405  */
1406 static gboolean
1407 is_cmdline_mute (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1408 {
1409     /* When one of panels is other than view_listing,
1410        current_panel points to view_listing panel all time independently of
1411        it's activity. Thus, we can't use get_current_type() here.
1412        current_panel should point to actually current active panel
1413        independently of it's type. */
1414     return (!current_panel->active
1415             && (get_other_type () == view_quick || get_other_type () == view_tree));
1416 }
1417 
1418 /* --------------------------------------------------------------------------------------------- */
1419 
1420 /**
1421  * Handles the Enter key on the command-line.
1422  *
1423  * Returns TRUE if non-whitespace was indeed processed.
1424  */
1425 static gboolean
1426 handle_cmdline_enter (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1427 {
1428     const char *s;
1429 
1430     for (s = input_get_ctext (cmdline); *s != '\0' && whitespace (*s); s++)
1431         ;
1432 
1433     if (*s != '\0')
1434     {
1435         send_message (cmdline, NULL, MSG_KEY, '\n', NULL);
1436         return TRUE;
1437     }
1438 
1439     input_insert (cmdline, "", FALSE);
1440     cmdline->point = 0;
1441 
1442     return FALSE;
1443 }
1444 
1445 /* --------------------------------------------------------------------------------------------- */
1446 
1447 static cb_ret_t
1448 midnight_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
     /* [previous][next][first][last][top][bottom][index][help]  */
1449 {
1450     long command;
1451 
1452     switch (msg)
1453     {
1454     case MSG_INIT:
1455         panel_init ();
1456         setup_panels ();
1457         return MSG_HANDLED;
1458 
1459     case MSG_DRAW:
1460         load_hint (TRUE);
1461         group_default_callback (w, NULL, MSG_DRAW, 0, NULL);
1462         // We handle the special case of the output lines
1463         if (mc_global.tty.console_flag != '\0' && output_lines != 0)
1464         {
1465             unsigned char end_line;
1466 
1467             end_line = LINES - (mc_global.keybar_visible ? 1 : 0) - 1;
1468             show_console_contents (output_start_y, end_line - output_lines, end_line);
1469         }
1470         return MSG_HANDLED;
1471 
1472     case MSG_RESIZE:
1473         widget_adjust_position (w->pos_flags, &w->rect);
1474         setup_panels ();
1475         menubar_arrange (the_menubar);
1476         return MSG_HANDLED;
1477 
1478     case MSG_IDLE:
1479         // We only need the first idle event to show user menu after start
1480         widget_idle (w, FALSE);
1481 
1482         if (boot_current_is_left)
1483             widget_select (get_panel_widget (0));
1484         else
1485             widget_select (get_panel_widget (1));
1486 
1487         if (auto_menu)
1488             midnight_execute_cmd (NULL, CK_UserMenu);
1489         return MSG_HANDLED;
1490 
1491     case MSG_KEY:
1492         if (w->ext_mode)
1493         {
1494             command = widget_lookup_key (w, parm);
1495             if (command != CK_IgnoreKey)
1496                 return midnight_execute_cmd (NULL, command);
1497         }
1498 
1499         // FIXME: should handle all menu shortcuts before this point
1500         if (widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1501             return MSG_NOT_HANDLED;
1502 
1503         if (parm == '\n' && !is_cmdline_mute ())
1504         {
1505             if (handle_cmdline_enter ())
1506                 return MSG_HANDLED;
1507             // Else: the panel will handle it.
1508         }
1509 
1510         if ((!mc_global.tty.alternate_plus_minus
1511              || !(mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag))
1512             && !quote && !current_panel->quick_search.active)
1513         {
1514             if (!only_leading_plus_minus)
1515             {
1516                 // Special treatment, since the input line will eat them
1517                 if (parm == '+')
1518                     return send_message (current_panel, filemanager, MSG_ACTION, CK_Select, NULL);
1519 
1520                 if (parm == '\\' || parm == '-')
1521                     return send_message (current_panel, filemanager, MSG_ACTION, CK_Unselect, NULL);
1522 
1523                 if (parm == '*')
1524                     return send_message (current_panel, filemanager, MSG_ACTION, CK_SelectInvert,
1525                                          NULL);
1526             }
1527             else if (!command_prompt || input_is_empty (cmdline))
1528             {
1529                 /* Special treatment '+', '-', '\', '*' only when this is
1530                  * first char on input line
1531                  */
1532                 if (parm == '+')
1533                     return send_message (current_panel, filemanager, MSG_ACTION, CK_Select, NULL);
1534 
1535                 if (parm == '\\' || parm == '-')
1536                     return send_message (current_panel, filemanager, MSG_ACTION, CK_Unselect, NULL);
1537 
1538                 if (parm == '*')
1539                     return send_message (current_panel, filemanager, MSG_ACTION, CK_SelectInvert,
1540                                          NULL);
1541             }
1542         }
1543         return MSG_NOT_HANDLED;
1544 
1545     case MSG_HOTKEY_HANDLED:
1546         if ((get_current_type () == view_listing) && current_panel->quick_search.active)
1547         {
1548             current_panel->dirty = TRUE;  // FIXME: unneeded?
1549             send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1550         }
1551         return MSG_HANDLED;
1552 
1553     case MSG_UNHANDLED_KEY:
1554     {
1555         cb_ret_t v = MSG_NOT_HANDLED;
1556 
1557         command = widget_lookup_key (w, parm);
1558         if (command != CK_IgnoreKey)
1559             v = midnight_execute_cmd (NULL, command);
1560 
1561         if (v == MSG_NOT_HANDLED && command_prompt && !is_cmdline_mute ())
1562             v = send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1563 
1564         return v;
1565     }
1566 
1567     case MSG_POST_KEY:
1568         if (!widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1569             update_dirty_panels ();
1570         return MSG_HANDLED;
1571 
1572     case MSG_ACTION:
1573         // Handle shortcuts, menu, and buttonbar.
1574         return midnight_execute_cmd (sender, parm);
1575 
1576     case MSG_DESTROY:
1577         panel_deinit ();
1578         return MSG_HANDLED;
1579 
1580     default:
1581         return dlg_default_callback (w, sender, msg, parm, data);
1582     }
1583 }
1584 
1585 /* --------------------------------------------------------------------------------------------- */
1586 /*** public functions ****************************************************************************/
1587 /* --------------------------------------------------------------------------------------------- */
1588 
1589 void
1590 update_menu (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1591 {
1592     menu_set_name (left_menu, panels_layout.horizontal_split ? _ ("&Above") : _ ("&Left"));
1593     menu_set_name (right_menu, panels_layout.horizontal_split ? _ ("&Below") : _ ("&Right"));
1594     menubar_arrange (the_menubar);
1595     widget_set_visibility (WIDGET (the_menubar), menubar_visible);
1596 }
1597 
1598 /* --------------------------------------------------------------------------------------------- */
1599 
1600 void
1601 midnight_set_buttonbar (WButtonBar *b)
     /* [previous][next][first][last][top][bottom][index][help]  */
1602 {
1603     Widget *w = WIDGET (filemanager);
1604 
1605     buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), w->keymap, NULL);
1606     buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), w->keymap, NULL);
1607     buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), w->keymap, NULL);
1608     buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), w->keymap, NULL);
1609     buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), w->keymap, NULL);
1610     buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), w->keymap, NULL);
1611     buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), w->keymap, NULL);
1612     buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), w->keymap, NULL);
1613     buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), w->keymap, NULL);
1614     buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), w->keymap, NULL);
1615 }
1616 
1617 /* --------------------------------------------------------------------------------------------- */
1618 /**
1619  * Return a random hint.  If force is TRUE, ignore the timeout.
1620  */
1621 
1622 char *
1623 get_random_hint (gboolean force)
     /* [previous][next][first][last][top][bottom][index][help]  */
1624 {
1625     static const gint64 update_period = 60 * G_USEC_PER_SEC;
1626     static gint64 tv = 0;
1627 
1628     char *data, *result, *eop;
1629     size_t len, start;
1630     GIConv conv;
1631 
1632     // Do not change hints more often than one minute
1633     if (!force && !mc_time_elapsed (&tv, update_period))
1634         return g_strdup ("");
1635 
1636     data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
1637     if (data == NULL)
1638         return NULL;
1639 
1640     // get a random entry
1641     srand ((unsigned int) (tv / G_USEC_PER_SEC));
1642     start = ((size_t) rand ()) % (len - 1);
1643 
1644     // Search the start of paragraph
1645     for (; start != 0; start--)
1646         if (data[start] == '\n' && data[start + 1] == '\n')
1647         {
1648             start += 2;
1649             break;
1650         }
1651 
1652     // Search the end of paragraph
1653     for (eop = data + start; *eop != '\0'; eop++)
1654     {
1655         if (*eop == '\n' && *(eop + 1) == '\n')
1656         {
1657             *eop = '\0';
1658             break;
1659         }
1660         if (*eop == '\n')
1661             *eop = ' ';
1662     }
1663 
1664     // hint files are stored in utf-8
1665     // try convert hint file from utf-8 to terminal encoding
1666     conv = str_crt_conv_from ("UTF-8");
1667     if (conv == INVALID_CONV)
1668         result = g_strndup (data + start, len - start);
1669     else
1670     {
1671         GString *buffer;
1672         gboolean nok;
1673 
1674         buffer = g_string_sized_new (len - start);
1675         nok = (str_convert (conv, data + start, buffer) == ESTR_FAILURE);
1676         result = g_string_free (buffer, nok);
1677         str_close_conv (conv);
1678     }
1679 
1680     g_free (data);
1681     return result;
1682 }
1683 
1684 /* --------------------------------------------------------------------------------------------- */
1685 /**
1686  * Load new hint and display it.
1687  * IF force is not 0, ignore the timeout.
1688  */
1689 
1690 void
1691 load_hint (gboolean force)
     /* [previous][next][first][last][top][bottom][index][help]  */
1692 {
1693     char *hint;
1694 
1695     if (WIDGET (the_hint)->owner == NULL)
1696         return;
1697 
1698     if (!mc_global.message_visible)
1699     {
1700         label_set_text (the_hint, NULL);
1701         return;
1702     }
1703 
1704     hint = get_random_hint (force);
1705 
1706     if (hint != NULL)
1707     {
1708         if (*hint != '\0')
1709             set_hintbar (hint);
1710         g_free (hint);
1711     }
1712     else
1713     {
1714         char text[BUF_SMALL];
1715 
1716         g_snprintf (text, sizeof (text), PACKAGE_NAME " %s\n", mc_global.mc_version);
1717         set_hintbar (text);
1718     }
1719 }
1720 
1721 /* --------------------------------------------------------------------------------------------- */
1722 /**
1723  * Change current panel in the file manager.
1724  *
1725  * @return current_panel
1726  */
1727 
1728 WPanel *
1729 change_panel (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1730 {
1731     input_complete_free (cmdline);
1732     group_select_next_widget (GROUP (filemanager));
1733     return current_panel;
1734 }
1735 
1736 /* --------------------------------------------------------------------------------------------- */
1737 
1738 /** Save current stat of directories to avoid reloading the panels
1739  * when no modifications have taken place
1740  */
1741 void
1742 save_cwds_stat (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1743 {
1744     if (panels_options.fast_reload)
1745     {
1746         mc_stat (current_panel->cwd_vpath, &(current_panel->dir_stat));
1747         if (get_other_type () == view_listing)
1748             mc_stat (other_panel->cwd_vpath, &(other_panel->dir_stat));
1749     }
1750 }
1751 
1752 /* --------------------------------------------------------------------------------------------- */
1753 
1754 gboolean
1755 quiet_quit_cmd (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1756 {
1757     print_last_revert = TRUE;
1758     return quit_cmd_internal (1);
1759 }
1760 
1761 /* --------------------------------------------------------------------------------------------- */
1762 
1763 /** Run the main dialog that occupies the whole screen */
1764 gboolean
1765 do_nc (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
1766 {
1767     gboolean ret;
1768 
1769 #ifdef USE_INTERNAL_EDIT
1770     edit_stack_init ();
1771 #endif
1772 
1773     filemanager = dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, dialog_colors,
1774                               midnight_callback, NULL, "[main]", NULL);
1775 
1776     // Check if we were invoked as an editor or file viewer
1777     if (mc_global.mc_run_mode != MC_RUN_FULL)
1778     {
1779         setup_dummy_mc ();
1780         ret = mc_maybe_editor_or_viewer ();
1781     }
1782     else
1783     {
1784         // We only need the first idle event to show user menu after start
1785         widget_idle (WIDGET (filemanager), TRUE);
1786 
1787         setup_mc ();
1788         mc_filehighlight = mc_fhl_new (TRUE);
1789 
1790         create_file_manager ();
1791         (void) dlg_run (filemanager);
1792 
1793         mc_fhl_free (&mc_filehighlight);
1794 
1795         ret = TRUE;
1796 
1797         // widget_destroy destroys even current_panel->cwd_vpath, so we have to save a copy :)
1798         if (mc_args__last_wd_file != NULL && vfs_current_is_local ())
1799             last_wd_str = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
1800 
1801         // don't handle VFS timestamps for dirs opened in panels
1802         mc_event_destroy (MCEVENT_GROUP_CORE, "vfs_timestamp");
1803     }
1804 
1805     // Program end
1806     mc_global.midnight_shutdown = TRUE;
1807     dialog_switch_shutdown ();
1808     done_mc ();
1809     widget_destroy (WIDGET (filemanager));
1810     current_panel = NULL;
1811 
1812 #ifdef USE_INTERNAL_EDIT
1813     edit_stack_free ();
1814 #endif
1815 
1816     if ((quit & SUBSHELL_EXIT) == 0)
1817         tty_clear_screen ();
1818 
1819     return ret;
1820 }
1821 
1822 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */