root/src/filemanager/midnight.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. ctl_x_cmd
  29. setup_mc
  30. setup_dummy_mc
  31. done_mc
  32. create_file_manager
  33. prepend_cwd_on_local
  34. mc_maybe_editor_or_viewer
  35. show_editor_viewer_history
  36. quit_cmd_internal
  37. quit_cmd
  38. update_dirty_panels
  39. toggle_show_hidden
  40. midnight_execute_cmd
  41. is_cmdline_mute
  42. handle_cmdline_enter
  43. midnight_callback
  44. update_menu
  45. midnight_set_buttonbar
  46. get_random_hint
  47. load_hint
  48. change_panel
  49. save_cwds_stat
  50. quiet_quit_cmd
  51. do_nc

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

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