Ticket #4536: skins: add root variant of julia256 skin.
[midnight-commander.git] / src / filemanager / filemanager.c
blob2e8baca784472ac5e650d765805a00ed57eed19c
1 /*
2 Main dialog (file panels) of the Midnight Commander
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Miguel de Icaza, 1994, 1995, 1996, 1997
9 Janne Kukonlehto, 1994, 1995
10 Norbert Warmuth, 1997
11 Andrew Borodin <aborodin@vmail.ru>, 2009-2022
12 Slava Zanko <slavazanko@gmail.com>, 2013
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 /** \file filemanager.c
31 * \brief Source: main dialog (file panels) of the Midnight Commander
34 #include <config.h>
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 */
46 #include "lib/global.h"
47 #include "lib/fileloc.h" /* MC_HINT */
49 #include "lib/tty/tty.h"
50 #include "lib/tty/key.h" /* KEY_M_* masks */
51 #include "lib/skin.h"
52 #include "lib/util.h"
54 #include "lib/vfs/vfs.h"
56 #include "src/args.h"
57 #ifdef ENABLE_SUBSHELL
58 #include "src/subshell/subshell.h"
59 #endif
60 #include "src/execute.h" /* toggle_subshell */
61 #include "src/setup.h" /* variables */
62 #include "src/learn.h" /* learn_keys() */
63 #include "src/keymap.h"
64 #include "lib/fileloc.h" /* MC_FILEPOS_FILE */
65 #include "lib/keybind.h"
66 #include "lib/event.h"
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() */
77 #ifdef USE_INTERNAL_EDIT
78 #include "src/editor/edit.h"
79 #endif
81 #ifdef USE_DIFF_VIEW
82 #include "src/diffviewer/ydiff.h"
83 #endif
85 #include "src/consaver/cons.saver.h" /* show_console_contents */
86 #include "src/file_history.h" /* show_file_history() */
88 #include "filemanager.h"
90 /*** global variables ****************************************************************************/
92 /* When the modes are active, left_panel, right_panel and tree_panel */
93 /* point to a proper data structure. You should check with the functions */
94 /* get_current_type and get_other_type the types of the panels before using */
95 /* this pointer variables */
97 /* The structures for the panels */
98 WPanel *left_panel = NULL;
99 WPanel *right_panel = NULL;
100 /* Pointer to the selected and unselected panel */
101 WPanel *current_panel = NULL;
103 /* The Menubar */
104 WMenuBar *the_menubar = NULL;
105 /* The widget where we draw the prompt */
106 WLabel *the_prompt;
107 /* The hint bar */
108 WLabel *the_hint;
109 /* The button bar */
110 WButtonBar *the_bar;
112 /* The prompt */
113 const char *mc_prompt = NULL;
115 /*** file scope macro definitions ****************************************************************/
117 #ifdef HAVE_CHARSET
119 * Don't restrict the output on the screen manager level,
120 * the translation tables take care of it.
122 #endif /* !HAVE_CHARSET */
124 /*** file scope type declarations ****************************************************************/
126 /*** forward declarations (file scope functions) *************************************************/
128 /*** file scope variables ************************************************************************/
130 static menu_t *left_menu, *right_menu;
132 /* --------------------------------------------------------------------------------------------- */
133 /*** file scope functions ************************************************************************/
134 /* --------------------------------------------------------------------------------------------- */
136 /** Stop MC main dialog and the current dialog if it exists.
137 * Needed to provide fast exit from MC viewer or editor on shell exit */
138 static void
139 stop_dialogs (void)
141 dlg_close (filemanager);
143 if (top_dlg != NULL)
144 dlg_close (DIALOG (top_dlg->data));
147 /* --------------------------------------------------------------------------------------------- */
149 static void
150 treebox_cmd (void)
152 char *sel_dir;
154 sel_dir = tree_box (panel_current_entry (current_panel)->fname->str);
155 if (sel_dir != NULL)
157 vfs_path_t *sel_vdir;
159 sel_vdir = vfs_path_from_str (sel_dir);
160 panel_cd (current_panel, sel_vdir, cd_exact);
161 vfs_path_free (sel_vdir, TRUE);
162 g_free (sel_dir);
166 /* --------------------------------------------------------------------------------------------- */
168 #ifdef LISTMODE_EDITOR
169 static void
170 listmode_cmd (void)
172 char *newmode;
174 if (get_current_type () != view_listing)
175 return;
177 newmode = listmode_edit (current_panel->user_format);
178 if (!newmode)
179 return;
181 g_free (current_panel->user_format);
182 current_panel->list_format = list_user;
183 current_panel->user_format = newmode;
184 set_panel_formats (current_panel);
186 do_refresh ();
188 #endif /* LISTMODE_EDITOR */
190 /* --------------------------------------------------------------------------------------------- */
192 static GList *
193 create_panel_menu (void)
195 GList *entries = NULL;
197 entries = g_list_prepend (entries, menu_entry_new (_("File listin&g"), CK_PanelListing));
198 entries = g_list_prepend (entries, menu_entry_new (_("&Quick view"), CK_PanelQuickView));
199 entries = g_list_prepend (entries, menu_entry_new (_("&Info"), CK_PanelInfo));
200 entries = g_list_prepend (entries, menu_entry_new (_("&Tree"), CK_PanelTree));
201 entries = g_list_prepend (entries, menu_separator_new ());
202 entries =
203 g_list_prepend (entries, menu_entry_new (_("&Listing format..."), CK_SetupListingFormat));
204 entries = g_list_prepend (entries, menu_entry_new (_("&Sort order..."), CK_Sort));
205 entries = g_list_prepend (entries, menu_entry_new (_("&Filter..."), CK_Filter));
206 #ifdef HAVE_CHARSET
207 entries = g_list_prepend (entries, menu_entry_new (_("&Encoding..."), CK_SelectCodepage));
208 #endif
209 entries = g_list_prepend (entries, menu_separator_new ());
210 #ifdef ENABLE_VFS_FTP
211 entries = g_list_prepend (entries, menu_entry_new (_("FT&P link..."), CK_ConnectFtp));
212 #endif
213 #ifdef ENABLE_VFS_SHELL
214 entries = g_list_prepend (entries, menu_entry_new (_("S&hell link..."), CK_ConnectShell));
215 #endif
216 #ifdef ENABLE_VFS_SFTP
217 entries = g_list_prepend (entries, menu_entry_new (_("SFTP li&nk..."), CK_ConnectSftp));
218 #endif
219 entries = g_list_prepend (entries, menu_entry_new (_("Paneli&ze"), CK_Panelize));
220 entries = g_list_prepend (entries, menu_separator_new ());
221 entries = g_list_prepend (entries, menu_entry_new (_("&Rescan"), CK_Reread));
223 return g_list_reverse (entries);
226 /* --------------------------------------------------------------------------------------------- */
228 static GList *
229 create_file_menu (void)
231 GList *entries = NULL;
233 entries = g_list_prepend (entries, menu_entry_new (_("&View"), CK_View));
234 entries = g_list_prepend (entries, menu_entry_new (_("Vie&w file..."), CK_ViewFile));
235 entries = g_list_prepend (entries, menu_entry_new (_("&Filtered view"), CK_ViewFiltered));
236 entries = g_list_prepend (entries, menu_entry_new (_("&Edit"), CK_Edit));
237 entries = g_list_prepend (entries, menu_entry_new (_("&Copy"), CK_Copy));
238 entries = g_list_prepend (entries, menu_entry_new (_("C&hmod"), CK_ChangeMode));
239 entries = g_list_prepend (entries, menu_entry_new (_("&Link"), CK_Link));
240 entries = g_list_prepend (entries, menu_entry_new (_("&Symlink"), CK_LinkSymbolic));
241 entries =
242 g_list_prepend (entries, menu_entry_new (_("Relative symlin&k"), CK_LinkSymbolicRelative));
243 entries = g_list_prepend (entries, menu_entry_new (_("Edit s&ymlink"), CK_LinkSymbolicEdit));
244 entries = g_list_prepend (entries, menu_entry_new (_("Ch&own"), CK_ChangeOwn));
245 entries = g_list_prepend (entries, menu_entry_new (_("&Advanced chown"), CK_ChangeOwnAdvanced));
246 #ifdef ENABLE_EXT2FS_ATTR
247 entries = g_list_prepend (entries, menu_entry_new (_("Cha&ttr"), CK_ChangeAttributes));
248 #endif
249 entries = g_list_prepend (entries, menu_entry_new (_("&Rename/Move"), CK_Move));
250 entries = g_list_prepend (entries, menu_entry_new (_("&Mkdir"), CK_MakeDir));
251 entries = g_list_prepend (entries, menu_entry_new (_("&Delete"), CK_Delete));
252 entries = g_list_prepend (entries, menu_entry_new (_("&Quick cd"), CK_CdQuick));
253 entries = g_list_prepend (entries, menu_separator_new ());
254 entries = g_list_prepend (entries, menu_entry_new (_("Select &group"), CK_Select));
255 entries = g_list_prepend (entries, menu_entry_new (_("U&nselect group"), CK_Unselect));
256 entries = g_list_prepend (entries, menu_entry_new (_("&Invert selection"), CK_SelectInvert));
257 entries = g_list_prepend (entries, menu_separator_new ());
258 entries = g_list_prepend (entries, menu_entry_new (_("E&xit"), CK_Quit));
260 return g_list_reverse (entries);
263 /* --------------------------------------------------------------------------------------------- */
265 static GList *
266 create_command_menu (void)
268 /* I know, I'm lazy, but the tree widget when it's not running
269 * as a panel still has some problems, I have not yet finished
270 * the WTree widget port, sorry.
272 GList *entries = NULL;
274 entries = g_list_prepend (entries, menu_entry_new (_("&User menu"), CK_UserMenu));
275 entries = g_list_prepend (entries, menu_entry_new (_("&Directory tree"), CK_Tree));
276 entries = g_list_prepend (entries, menu_entry_new (_("&Find file"), CK_Find));
277 entries = g_list_prepend (entries, menu_entry_new (_("S&wap panels"), CK_Swap));
278 entries = g_list_prepend (entries, menu_entry_new (_("Switch &panels on/off"), CK_Shell));
279 entries = g_list_prepend (entries, menu_entry_new (_("&Compare directories"), CK_CompareDirs));
280 #ifdef USE_DIFF_VIEW
281 entries = g_list_prepend (entries, menu_entry_new (_("C&ompare files"), CK_CompareFiles));
282 #endif
283 entries =
284 g_list_prepend (entries, menu_entry_new (_("E&xternal panelize"), CK_ExternalPanelize));
285 entries = g_list_prepend (entries, menu_entry_new (_("Show directory s&izes"), CK_DirSize));
286 entries = g_list_prepend (entries, menu_separator_new ());
287 entries = g_list_prepend (entries, menu_entry_new (_("Command &history"), CK_History));
288 entries =
289 g_list_prepend (entries,
290 menu_entry_new (_("Viewed/edited files hi&story"), CK_EditorViewerHistory));
291 entries = g_list_prepend (entries, menu_entry_new (_("Di&rectory hotlist"), CK_HotList));
292 #ifdef ENABLE_VFS
293 entries = g_list_prepend (entries, menu_entry_new (_("&Active VFS list"), CK_VfsList));
294 #endif
295 #ifdef ENABLE_BACKGROUND
296 entries = g_list_prepend (entries, menu_entry_new (_("&Background jobs"), CK_Jobs));
297 #endif
298 entries = g_list_prepend (entries, menu_entry_new (_("Screen lis&t"), CK_ScreenList));
299 entries = g_list_prepend (entries, menu_separator_new ());
300 #ifdef ENABLE_VFS_UNDELFS
301 entries =
302 g_list_prepend (entries, menu_entry_new (_("&Undelete files (ext2fs only)"), CK_Undelete));
303 #endif
304 #ifdef LISTMODE_EDITOR
305 entries = g_list_prepend (entries, menu_entry_new (_("&Listing format edit"), CK_ListMode));
306 #endif
307 #if defined (ENABLE_VFS_UNDELFS) || defined (LISTMODE_EDITOR)
308 entries = g_list_prepend (entries, menu_separator_new ());
309 #endif
310 entries =
311 g_list_prepend (entries, menu_entry_new (_("Edit &extension file"), CK_EditExtensionsFile));
312 entries = g_list_prepend (entries, menu_entry_new (_("Edit &menu file"), CK_EditUserMenu));
313 entries =
314 g_list_prepend (entries,
315 menu_entry_new (_("Edit hi&ghlighting group file"),
316 CK_EditFileHighlightFile));
318 return g_list_reverse (entries);
321 /* --------------------------------------------------------------------------------------------- */
323 static GList *
324 create_options_menu (void)
326 GList *entries = NULL;
328 entries = g_list_prepend (entries, menu_entry_new (_("&Configuration..."), CK_Options));
329 entries = g_list_prepend (entries, menu_entry_new (_("&Layout..."), CK_OptionsLayout));
330 entries = g_list_prepend (entries, menu_entry_new (_("&Panel options..."), CK_OptionsPanel));
331 entries = g_list_prepend (entries, menu_entry_new (_("C&onfirmation..."), CK_OptionsConfirm));
332 entries = g_list_prepend (entries, menu_entry_new (_("&Appearance..."), CK_OptionsAppearance));
333 entries =
334 g_list_prepend (entries, menu_entry_new (_("&Display bits..."), CK_OptionsDisplayBits));
335 entries = g_list_prepend (entries, menu_entry_new (_("Learn &keys..."), CK_LearnKeys));
336 #ifdef ENABLE_VFS
337 entries = g_list_prepend (entries, menu_entry_new (_("&Virtual FS..."), CK_OptionsVfs));
338 #endif
339 entries = g_list_prepend (entries, menu_separator_new ());
340 entries = g_list_prepend (entries, menu_entry_new (_("&Save setup"), CK_SaveSetup));
342 return g_list_reverse (entries);
345 /* --------------------------------------------------------------------------------------------- */
347 static void
348 init_menu (void)
350 left_menu = menu_new ("", create_panel_menu (), "[Left and Right Menus]");
351 menubar_add_menu (the_menubar, left_menu);
352 menubar_add_menu (the_menubar, menu_new (_("&File"), create_file_menu (), "[File Menu]"));
353 menubar_add_menu (the_menubar,
354 menu_new (_("&Command"), create_command_menu (), "[Command Menu]"));
355 menubar_add_menu (the_menubar,
356 menu_new (_("&Options"), create_options_menu (), "[Options Menu]"));
357 right_menu = menu_new ("", create_panel_menu (), "[Left and Right Menus]");
358 menubar_add_menu (the_menubar, right_menu);
359 update_menu ();
362 /* --------------------------------------------------------------------------------------------- */
364 static void
365 menu_last_selected_cmd (void)
367 menubar_activate (the_menubar, drop_menus, -1);
370 /* --------------------------------------------------------------------------------------------- */
372 static void
373 menu_cmd (void)
375 int selected;
377 if ((get_current_index () == 0) == current_panel->active)
378 selected = 0;
379 else
380 selected = g_list_length (the_menubar->menu) - 1;
382 menubar_activate (the_menubar, drop_menus, selected);
385 /* --------------------------------------------------------------------------------------------- */
387 static void
388 sort_cmd (void)
390 WPanel *p;
391 const panel_field_t *sort_order;
393 if (!SELECTED_IS_PANEL)
394 return;
396 p = MENU_PANEL;
397 sort_order = sort_box (&p->sort_info, p->sort_field);
398 panel_set_sort_order (p, sort_order);
401 /* --------------------------------------------------------------------------------------------- */
403 static char *
404 midnight_get_shortcut (long command)
406 const char *ext_map;
407 const char *shortcut = NULL;
409 shortcut = keybind_lookup_keymap_shortcut (filemanager_map, command);
410 if (shortcut != NULL)
411 return g_strdup (shortcut);
413 shortcut = keybind_lookup_keymap_shortcut (panel_map, command);
414 if (shortcut != NULL)
415 return g_strdup (shortcut);
417 ext_map = keybind_lookup_keymap_shortcut (filemanager_map, CK_ExtendedKeyMap);
418 if (ext_map != NULL)
419 shortcut = keybind_lookup_keymap_shortcut (filemanager_x_map, command);
420 if (shortcut != NULL)
421 return g_strdup_printf ("%s %s", ext_map, shortcut);
423 return NULL;
426 /* --------------------------------------------------------------------------------------------- */
428 static char *
429 midnight_get_title (const WDialog * h, size_t len)
431 char *path;
432 char *login;
433 char *p;
435 (void) h;
437 title_path_prepare (&path, &login);
439 p = g_strdup_printf ("%s [%s]:%s", _("Panels:"), login, path);
440 g_free (path);
441 g_free (login);
442 path = g_strdup (str_trunc (p, len - 4));
443 g_free (p);
445 return path;
448 /* --------------------------------------------------------------------------------------------- */
450 static void
451 toggle_panels_split (void)
453 panels_layout.horizontal_split = !panels_layout.horizontal_split;
454 layout_change ();
455 do_refresh ();
458 /* --------------------------------------------------------------------------------------------- */
460 #ifdef ENABLE_VFS
461 /* event helper */
462 static gboolean
463 check_panel_timestamp (const WPanel * panel, panel_view_mode_t mode, const struct vfs_class *vclass,
464 const vfsid id)
466 return (mode != view_listing || (vfs_path_get_last_path_vfs (panel->cwd_vpath) == vclass
467 && vfs_getid (panel->cwd_vpath) == id));
470 /* --------------------------------------------------------------------------------------------- */
472 /* event callback */
473 static gboolean
474 check_current_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
475 gpointer init_data, gpointer data)
477 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
479 (void) event_group_name;
480 (void) event_name;
481 (void) init_data;
483 event_data->ret =
484 check_panel_timestamp (current_panel, get_current_type (), event_data->vclass,
485 event_data->id);
486 return !event_data->ret;
489 /* --------------------------------------------------------------------------------------------- */
491 /* event callback */
492 static gboolean
493 check_other_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
494 gpointer init_data, gpointer data)
496 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
498 (void) event_group_name;
499 (void) event_name;
500 (void) init_data;
502 event_data->ret =
503 check_panel_timestamp (other_panel, get_other_type (), event_data->vclass, event_data->id);
504 return !event_data->ret;
506 #endif /* ENABLE_VFS */
508 /* --------------------------------------------------------------------------------------------- */
510 /* event callback */
511 static gboolean
512 print_vfs_message (const gchar * event_group_name, const gchar * event_name,
513 gpointer init_data, gpointer data)
515 ev_vfs_print_message_t *event_data = (ev_vfs_print_message_t *) data;
517 (void) event_group_name;
518 (void) event_name;
519 (void) init_data;
521 if (mc_global.midnight_shutdown)
522 goto ret;
524 if (!mc_global.message_visible || the_hint == NULL || WIDGET (the_hint)->owner == NULL)
526 int col, row;
528 if (!nice_rotating_dash || (ok_to_refresh <= 0))
529 goto ret;
531 /* Preserve current cursor position */
532 tty_getyx (&row, &col);
534 tty_gotoyx (0, 0);
535 tty_setcolor (NORMAL_COLOR);
536 tty_print_string (str_fit_to_term (event_data->msg, COLS - 1, J_LEFT));
538 /* Restore cursor position */
539 tty_gotoyx (row, col);
540 mc_refresh ();
541 goto ret;
544 if (mc_global.message_visible)
545 set_hintbar (event_data->msg);
547 ret:
548 MC_PTR_FREE (event_data->msg);
549 return TRUE;
552 /* --------------------------------------------------------------------------------------------- */
554 static void
555 create_panels (void)
557 int current_index, other_index;
558 panel_view_mode_t current_mode, other_mode;
559 char *current_dir, *other_dir;
560 vfs_path_t *original_dir;
563 * Following cases from command line are possible:
564 * 'mc' (no arguments): mc_run_param0 == NULL, mc_run_param1 == NULL
565 * active panel uses current directory
566 * passive panel uses "other_dir" from panels.ini
568 * 'mc dir1 dir2' (two arguments): mc_run_param0 != NULL, mc_run_param1 != NULL
569 * active panel uses mc_run_param0
570 * passive panel uses mc_run_param1
572 * 'mc dir1' (single argument): mc_run_param0 != NULL, mc_run_param1 == NULL
573 * active panel uses mc_run_param0
574 * passive panel uses "other_dir" from panels.ini
577 /* Set up panel directories */
578 if (boot_current_is_left)
580 /* left panel is active */
581 current_index = 0;
582 other_index = 1;
583 current_mode = startup_left_mode;
584 other_mode = startup_right_mode;
586 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
588 /* no arguments */
589 current_dir = NULL; /* assume current dir */
590 other_dir = saved_other_dir; /* from ini */
592 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
594 /* two arguments */
595 current_dir = (char *) mc_run_param0;
596 other_dir = mc_run_param1;
598 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
600 /* one argument */
601 current_dir = (char *) mc_run_param0;
602 other_dir = saved_other_dir; /* from ini */
605 else
607 /* right panel is active */
608 current_index = 1;
609 other_index = 0;
610 current_mode = startup_right_mode;
611 other_mode = startup_left_mode;
613 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
615 /* no arguments */
616 current_dir = NULL; /* assume current dir */
617 other_dir = saved_other_dir; /* from ini */
619 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
621 /* two arguments */
622 current_dir = (char *) mc_run_param0;
623 other_dir = mc_run_param1;
625 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
627 /* one argument */
628 current_dir = (char *) mc_run_param0;
629 other_dir = saved_other_dir; /* from ini */
633 /* 1. Get current dir */
634 original_dir = vfs_path_clone (vfs_get_raw_current_dir ());
636 /* 2. Create passive panel */
637 if (other_dir != NULL)
639 vfs_path_t *vpath;
641 if (g_path_is_absolute (other_dir))
642 vpath = vfs_path_from_str (other_dir);
643 else
644 vpath = vfs_path_append_new (original_dir, other_dir, (char *) NULL);
645 mc_chdir (vpath);
646 vfs_path_free (vpath, TRUE);
648 create_panel (other_index, other_mode);
650 /* 3. Create active panel */
651 if (current_dir == NULL)
652 mc_chdir (original_dir);
653 else
655 vfs_path_t *vpath;
657 if (g_path_is_absolute (current_dir))
658 vpath = vfs_path_from_str (current_dir);
659 else
660 vpath = vfs_path_append_new (original_dir, current_dir, (char *) NULL);
661 mc_chdir (vpath);
662 vfs_path_free (vpath, TRUE);
664 create_panel (current_index, current_mode);
666 if (startup_left_mode == view_listing)
667 current_panel = left_panel;
668 else if (right_panel != NULL)
669 current_panel = right_panel;
670 else
671 current_panel = left_panel;
673 vfs_path_free (original_dir, TRUE);
675 #ifdef ENABLE_VFS
676 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_other_panel_timestamp, NULL, NULL);
677 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_current_panel_timestamp, NULL, NULL);
678 #endif /* ENABLE_VFS */
680 mc_event_add (MCEVENT_GROUP_CORE, "vfs_print_message", print_vfs_message, NULL, NULL);
683 /* --------------------------------------------------------------------------------------------- */
685 static void
686 midnight_put_panel_path (WPanel * panel)
688 vfs_path_t *cwd_vpath;
689 const char *cwd_vpath_str;
691 if (!command_prompt)
692 return;
694 #ifdef HAVE_CHARSET
695 cwd_vpath = remove_encoding_from_path (panel->cwd_vpath);
696 #else
697 cwd_vpath = vfs_path_clone (panel->cwd_vpath);
698 #endif
700 cwd_vpath_str = vfs_path_as_str (cwd_vpath);
702 command_insert (cmdline, cwd_vpath_str, FALSE);
704 if (!IS_PATH_SEP (cwd_vpath_str[strlen (cwd_vpath_str) - 1]))
705 command_insert (cmdline, PATH_SEP_STR, FALSE);
707 vfs_path_free (cwd_vpath, TRUE);
710 /* --------------------------------------------------------------------------------------------- */
712 static void
713 put_link (WPanel * panel)
715 const file_entry_t *fe;
717 if (!command_prompt)
718 return;
720 fe = panel_current_entry (panel);
722 if (S_ISLNK (fe->st.st_mode))
724 char buffer[MC_MAXPATHLEN];
725 vfs_path_t *vpath;
726 int i;
728 vpath = vfs_path_append_new (panel->cwd_vpath, fe->fname->str, (char *) NULL);
729 i = mc_readlink (vpath, buffer, sizeof (buffer) - 1);
730 vfs_path_free (vpath, TRUE);
732 if (i > 0)
734 buffer[i] = '\0';
735 command_insert (cmdline, buffer, TRUE);
740 /* --------------------------------------------------------------------------------------------- */
742 static void
743 put_current_link (void)
745 put_link (current_panel);
748 /* --------------------------------------------------------------------------------------------- */
750 static void
751 put_other_link (void)
753 if (get_other_type () == view_listing)
754 put_link (other_panel);
757 /* --------------------------------------------------------------------------------------------- */
759 /** Insert the selected file name into the input line */
760 static void
761 put_current_selected (void)
763 const char *tmp;
765 if (!command_prompt)
766 return;
768 if (get_current_type () == view_tree)
770 WTree *tree;
771 const vfs_path_t *selected_name;
773 tree = (WTree *) get_panel_widget (get_current_index ());
774 selected_name = tree_selected_name (tree);
775 tmp = vfs_path_as_str (selected_name);
777 else
778 tmp = panel_current_entry (current_panel)->fname->str;
780 command_insert (cmdline, tmp, TRUE);
783 /* --------------------------------------------------------------------------------------------- */
785 static void
786 put_tagged (WPanel * panel)
788 if (!command_prompt)
789 return;
791 input_disable_update (cmdline);
793 if (panel->marked == 0)
794 command_insert (cmdline, panel_current_entry (panel)->fname->str, TRUE);
795 else
797 int i;
799 for (i = 0; i < panel->dir.len; i++)
800 if (panel->dir.list[i].f.marked != 0)
801 command_insert (cmdline, panel->dir.list[i].fname->str, TRUE);
804 input_enable_update (cmdline);
807 /* --------------------------------------------------------------------------------------------- */
809 static void
810 put_current_tagged (void)
812 put_tagged (current_panel);
815 /* --------------------------------------------------------------------------------------------- */
817 static void
818 put_other_tagged (void)
820 if (get_other_type () == view_listing)
821 put_tagged (other_panel);
824 /* --------------------------------------------------------------------------------------------- */
826 static void
827 setup_mc (void)
829 #ifdef HAVE_SLANG
830 #ifdef HAVE_CHARSET
831 tty_display_8bit (TRUE);
832 #else
833 tty_display_8bit (mc_global.full_eight_bits);
834 #endif /* HAVE_CHARSET */
836 #else /* HAVE_SLANG */
838 #ifdef HAVE_CHARSET
839 tty_display_8bit (TRUE);
840 #else
841 tty_display_8bit (mc_global.eight_bit_clean);
842 #endif /* HAVE_CHARSET */
843 #endif /* HAVE_SLANG */
845 if ((tty_baudrate () < 9600) || mc_global.tty.slow_terminal)
846 verbose = FALSE;
849 /* --------------------------------------------------------------------------------------------- */
851 static void
852 setup_dummy_mc (void)
854 vfs_path_t *vpath;
855 char *d;
856 int ret;
858 d = vfs_get_cwd ();
859 setup_mc ();
860 vpath = vfs_path_from_str (d);
861 ret = mc_chdir (vpath);
862 (void) ret;
863 vfs_path_free (vpath, TRUE);
864 g_free (d);
867 /* --------------------------------------------------------------------------------------------- */
869 static void
870 done_mc (void)
872 /* Setup shutdown
874 * We sync the profiles since the hotlist may have changed, while
875 * we only change the setup data if we have the auto save feature set
878 save_setup (auto_save_setup, panels_options.auto_save_setup);
880 vfs_stamp_path (vfs_get_raw_current_dir ());
883 /* --------------------------------------------------------------------------------------------- */
885 static void
886 create_file_manager (void)
888 Widget *w = WIDGET (filemanager);
889 WGroup *g = GROUP (filemanager);
891 w->keymap = filemanager_map;
892 w->ext_keymap = filemanager_x_map;
894 filemanager->get_shortcut = midnight_get_shortcut;
895 filemanager->get_title = midnight_get_title;
896 /* allow rebind tab */
897 widget_want_tab (w, TRUE);
899 the_menubar = menubar_new (NULL);
900 group_add_widget (g, the_menubar);
901 init_menu ();
903 create_panels ();
904 group_add_widget (g, get_panel_widget (0));
905 group_add_widget (g, get_panel_widget (1));
907 the_hint = label_new (0, 0, NULL);
908 the_hint->transparent = TRUE;
909 the_hint->auto_adjust_cols = 0;
910 WIDGET (the_hint)->rect.cols = COLS;
911 group_add_widget (g, the_hint);
913 cmdline = command_new (0, 0, 0);
914 group_add_widget (g, cmdline);
916 the_prompt = label_new (0, 0, mc_prompt);
917 the_prompt->transparent = TRUE;
918 group_add_widget (g, the_prompt);
920 the_bar = buttonbar_new ();
921 group_add_widget (g, the_bar);
922 midnight_set_buttonbar (the_bar);
924 #ifdef ENABLE_SUBSHELL
925 /* Must be done after creation of cmdline and prompt widgets to avoid potential
926 NULL dereference in load_prompt() -> ... -> setup_cmdline() -> label_set_text(). */
927 if (mc_global.tty.use_subshell)
928 add_select_channel (mc_global.tty.subshell_pty, load_prompt, NULL);
929 #endif /* !ENABLE_SUBSHELL */
932 /* --------------------------------------------------------------------------------------------- */
934 /** result must be free'd (I think this should go in util.c) */
935 static vfs_path_t *
936 prepend_cwd_on_local (const char *filename)
938 vfs_path_t *vpath;
940 vpath = vfs_path_from_str (filename);
941 if (!vfs_file_is_local (vpath) || g_path_is_absolute (filename))
942 return vpath;
944 vfs_path_free (vpath, TRUE);
946 return vfs_path_append_new (vfs_get_raw_current_dir (), filename, (char *) NULL);
949 /* --------------------------------------------------------------------------------------------- */
951 /** Invoke the internal view/edit routine with:
952 * the default processing and forcing the internal viewer/editor
954 static gboolean
955 mc_maybe_editor_or_viewer (void)
957 gboolean ret;
959 switch (mc_global.mc_run_mode)
961 #ifdef USE_INTERNAL_EDIT
962 case MC_RUN_EDITOR:
963 ret = edit_files ((GList *) mc_run_param0);
964 break;
965 #endif /* USE_INTERNAL_EDIT */
966 case MC_RUN_VIEWER:
968 vfs_path_t *vpath = NULL;
970 if (mc_run_param0 != NULL && *(char *) mc_run_param0 != '\0')
971 vpath = prepend_cwd_on_local ((char *) mc_run_param0);
973 ret = view_file (vpath, FALSE, TRUE);
974 vfs_path_free (vpath, TRUE);
975 break;
977 #ifdef USE_DIFF_VIEW
978 case MC_RUN_DIFFVIEWER:
979 ret = dview_diff_cmd (mc_run_param0, mc_run_param1);
980 break;
981 #endif /* USE_DIFF_VIEW */
982 default:
983 ret = FALSE;
986 return ret;
989 /* --------------------------------------------------------------------------------------------- */
991 static void
992 show_editor_viewer_history (void)
994 char *s;
995 int act;
997 s = show_file_history (WIDGET (filemanager), &act);
998 if (s != NULL)
1000 vfs_path_t *s_vpath;
1002 switch (act)
1004 case CK_Edit:
1005 s_vpath = vfs_path_from_str (s);
1006 edit_file_at_line (s_vpath, use_internal_edit, 0);
1007 break;
1009 case CK_View:
1010 s_vpath = vfs_path_from_str (s);
1011 view_file (s_vpath, use_internal_view, FALSE);
1012 break;
1014 default:
1016 char *d;
1018 d = g_path_get_dirname (s);
1019 s_vpath = vfs_path_from_str (d);
1020 panel_cd (current_panel, s_vpath, cd_exact);
1021 panel_set_current_by_name (current_panel, s);
1022 g_free (d);
1026 g_free (s);
1027 vfs_path_free (s_vpath, TRUE);
1031 /* --------------------------------------------------------------------------------------------- */
1033 static gboolean
1034 quit_cmd_internal (int quiet)
1036 int q = quit;
1037 size_t n;
1039 n = dialog_switch_num () - 1;
1040 if (n != 0)
1042 char msg[BUF_MEDIUM];
1044 g_snprintf (msg, sizeof (msg),
1045 ngettext ("You have %zu opened screen. Quit anyway?",
1046 "You have %zu opened screens. Quit anyway?", n), n);
1048 if (query_dialog (_("The Midnight Commander"), msg, D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
1049 return FALSE;
1050 q = 1;
1052 else if (quiet || !confirm_exit)
1053 q = 1;
1054 else if (query_dialog (_("The Midnight Commander"),
1055 _("Do you really want to quit the Midnight Commander?"),
1056 D_NORMAL, 2, _("&Yes"), _("&No")) == 0)
1057 q = 1;
1059 if (q != 0)
1061 #ifdef ENABLE_SUBSHELL
1062 if (!mc_global.tty.use_subshell)
1063 stop_dialogs ();
1064 else if ((q = exit_subshell ()? 1 : 0) != 0)
1065 #endif
1066 stop_dialogs ();
1069 if (q != 0)
1070 quit |= 1;
1071 return (quit != 0);
1074 /* --------------------------------------------------------------------------------------------- */
1076 static gboolean
1077 quit_cmd (void)
1079 return quit_cmd_internal (0);
1082 /* --------------------------------------------------------------------------------------------- */
1085 * Repaint the contents of the panels without frames. To schedule panel
1086 * for repainting, set panel->dirty to TRUE. There are many reasons why
1087 * the panels need to be repainted, and this is a costly operation, so
1088 * it's done once per event.
1091 static void
1092 update_dirty_panels (void)
1094 if (get_current_type () == view_listing && current_panel->dirty)
1095 widget_draw (WIDGET (current_panel));
1097 if (get_other_type () == view_listing && other_panel->dirty)
1098 widget_draw (WIDGET (other_panel));
1101 /* --------------------------------------------------------------------------------------------- */
1103 static void
1104 toggle_show_hidden (void)
1106 panels_options.show_dot_files = !panels_options.show_dot_files;
1107 update_panels (UP_RELOAD, UP_KEEPSEL);
1108 /* redraw panels forced */
1109 update_dirty_panels ();
1112 /* --------------------------------------------------------------------------------------------- */
1114 static cb_ret_t
1115 midnight_execute_cmd (Widget * sender, long command)
1117 cb_ret_t res = MSG_HANDLED;
1119 (void) sender;
1121 /* stop quick search before executing any command */
1122 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1124 switch (command)
1126 case CK_ChangePanel:
1127 (void) change_panel ();
1128 break;
1129 case CK_HotListAdd:
1130 add2hotlist_cmd (current_panel);
1131 break;
1132 case CK_SetupListingFormat:
1133 setup_listing_format_cmd ();
1134 break;
1135 case CK_ChangeMode:
1136 chmod_cmd (current_panel);
1137 break;
1138 case CK_ChangeOwn:
1139 chown_cmd (current_panel);
1140 break;
1141 case CK_ChangeOwnAdvanced:
1142 advanced_chown_cmd (current_panel);
1143 break;
1144 #ifdef ENABLE_EXT2FS_ATTR
1145 case CK_ChangeAttributes:
1146 chattr_cmd (current_panel);
1147 break;
1148 #endif
1149 case CK_CompareDirs:
1150 compare_dirs_cmd ();
1151 break;
1152 case CK_Options:
1153 configure_box ();
1154 break;
1155 #ifdef ENABLE_VFS
1156 case CK_OptionsVfs:
1157 configure_vfs_box ();
1158 break;
1159 #endif
1160 case CK_OptionsConfirm:
1161 confirm_box ();
1162 break;
1163 case CK_Copy:
1164 copy_cmd (current_panel);
1165 break;
1166 case CK_PutCurrentPath:
1167 midnight_put_panel_path (current_panel);
1168 break;
1169 case CK_PutCurrentSelected:
1170 put_current_selected ();
1171 break;
1172 case CK_PutCurrentFullSelected:
1173 midnight_put_panel_path (current_panel);
1174 put_current_selected ();
1175 break;
1176 case CK_PutCurrentLink:
1177 put_current_link ();
1178 break;
1179 case CK_PutCurrentTagged:
1180 put_current_tagged ();
1181 break;
1182 case CK_PutOtherPath:
1183 if (get_other_type () == view_listing)
1184 midnight_put_panel_path (other_panel);
1185 break;
1186 case CK_PutOtherLink:
1187 put_other_link ();
1188 break;
1189 case CK_PutOtherTagged:
1190 put_other_tagged ();
1191 break;
1192 case CK_Delete:
1193 delete_cmd (current_panel);
1194 break;
1195 case CK_ScreenList:
1196 dialog_switch_list ();
1197 break;
1198 #ifdef USE_DIFF_VIEW
1199 case CK_CompareFiles:
1200 diff_view_cmd ();
1201 break;
1202 #endif
1203 case CK_OptionsDisplayBits:
1204 display_bits_box ();
1205 break;
1206 case CK_Edit:
1207 edit_cmd (current_panel);
1208 break;
1209 #ifdef USE_INTERNAL_EDIT
1210 case CK_EditForceInternal:
1211 edit_cmd_force_internal (current_panel);
1212 break;
1213 #endif
1214 case CK_EditExtensionsFile:
1215 ext_cmd ();
1216 break;
1217 case CK_EditFileHighlightFile:
1218 edit_fhl_cmd ();
1219 break;
1220 case CK_EditUserMenu:
1221 edit_mc_menu_cmd ();
1222 break;
1223 case CK_LinkSymbolicEdit:
1224 edit_symlink_cmd ();
1225 break;
1226 case CK_ExternalPanelize:
1227 external_panelize_cmd ();
1228 break;
1229 case CK_ViewFiltered:
1230 view_filtered_cmd (current_panel);
1231 break;
1232 case CK_Find:
1233 find_cmd (current_panel);
1234 break;
1235 #ifdef ENABLE_VFS_SHELL
1236 case CK_ConnectShell:
1237 shelllink_cmd ();
1238 break;
1239 #endif
1240 #ifdef ENABLE_VFS_FTP
1241 case CK_ConnectFtp:
1242 ftplink_cmd ();
1243 break;
1244 #endif
1245 #ifdef ENABLE_VFS_SFTP
1246 case CK_ConnectSftp:
1247 sftplink_cmd ();
1248 break;
1249 #endif
1250 case CK_Panelize:
1251 panel_panelize_cd ();
1252 break;
1253 case CK_Help:
1254 help_cmd ();
1255 break;
1256 case CK_History:
1257 /* show the history of command line widget */
1258 send_message (cmdline, NULL, MSG_ACTION, CK_History, NULL);
1259 break;
1260 case CK_PanelInfo:
1261 if (sender == WIDGET (the_menubar))
1262 info_cmd (); /* menu */
1263 else
1264 info_cmd_no_menu (); /* shortcut or buttonbar */
1265 break;
1266 #ifdef ENABLE_BACKGROUND
1267 case CK_Jobs:
1268 jobs_box ();
1269 break;
1270 #endif
1271 case CK_OptionsLayout:
1272 layout_box ();
1273 break;
1274 case CK_OptionsAppearance:
1275 appearance_box ();
1276 break;
1277 case CK_LearnKeys:
1278 learn_keys ();
1279 break;
1280 case CK_Link:
1281 link_cmd (LINK_HARDLINK);
1282 break;
1283 case CK_PanelListing:
1284 listing_cmd ();
1285 break;
1286 #ifdef LISTMODE_EDITOR
1287 case CK_ListMode:
1288 listmode_cmd ();
1289 break;
1290 #endif
1291 case CK_Menu:
1292 menu_cmd ();
1293 break;
1294 case CK_MenuLastSelected:
1295 menu_last_selected_cmd ();
1296 break;
1297 case CK_MakeDir:
1298 mkdir_cmd (current_panel);
1299 break;
1300 case CK_OptionsPanel:
1301 panel_options_box ();
1302 break;
1303 #ifdef HAVE_CHARSET
1304 case CK_SelectCodepage:
1305 encoding_cmd ();
1306 break;
1307 #endif
1308 case CK_CdQuick:
1309 quick_cd_cmd (current_panel);
1310 break;
1311 case CK_HotList:
1312 hotlist_cmd (current_panel);
1313 break;
1314 case CK_PanelQuickView:
1315 if (sender == WIDGET (the_menubar))
1316 quick_view_cmd (); /* menu */
1317 else
1318 quick_cmd_no_menu (); /* shortcut or buttonabr */
1319 break;
1320 case CK_QuitQuiet:
1321 quiet_quit_cmd ();
1322 break;
1323 case CK_Quit:
1324 quit_cmd ();
1325 break;
1326 case CK_LinkSymbolicRelative:
1327 link_cmd (LINK_SYMLINK_RELATIVE);
1328 break;
1329 case CK_Move:
1330 rename_cmd (current_panel);
1331 break;
1332 case CK_Reread:
1333 reread_cmd ();
1334 break;
1335 #ifdef ENABLE_VFS
1336 case CK_VfsList:
1337 vfs_list (current_panel);
1338 break;
1339 #endif
1340 case CK_SaveSetup:
1341 save_setup_cmd ();
1342 break;
1343 case CK_Select:
1344 case CK_Unselect:
1345 case CK_SelectInvert:
1346 case CK_Filter:
1347 res = send_message (current_panel, filemanager, MSG_ACTION, command, NULL);
1348 break;
1349 case CK_Shell:
1350 toggle_subshell ();
1351 break;
1352 case CK_DirSize:
1353 smart_dirsize_cmd (current_panel);
1354 break;
1355 case CK_Sort:
1356 sort_cmd ();
1357 break;
1358 case CK_ExtendedKeyMap:
1359 WIDGET (filemanager)->ext_mode = TRUE;
1360 break;
1361 case CK_Suspend:
1362 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
1363 break;
1364 case CK_Swap:
1365 swap_cmd ();
1366 break;
1367 case CK_LinkSymbolic:
1368 link_cmd (LINK_SYMLINK_ABSOLUTE);
1369 break;
1370 case CK_ShowHidden:
1371 toggle_show_hidden ();
1372 break;
1373 case CK_SplitVertHoriz:
1374 toggle_panels_split ();
1375 break;
1376 case CK_SplitEqual:
1377 panels_split_equal ();
1378 break;
1379 case CK_SplitMore:
1380 panels_split_more ();
1381 break;
1382 case CK_SplitLess:
1383 panels_split_less ();
1384 break;
1385 case CK_PanelTree:
1386 panel_tree_cmd ();
1387 break;
1388 case CK_Tree:
1389 treebox_cmd ();
1390 break;
1391 #ifdef ENABLE_VFS_UNDELFS
1392 case CK_Undelete:
1393 undelete_cmd ();
1394 break;
1395 #endif
1396 case CK_UserMenu:
1397 user_file_menu_cmd ();
1398 break;
1399 case CK_View:
1400 view_cmd (current_panel);
1401 break;
1402 case CK_ViewFile:
1403 view_file_cmd (current_panel);
1404 break;
1405 case CK_EditorViewerHistory:
1406 show_editor_viewer_history ();
1407 break;
1408 case CK_Cancel:
1409 /* don't close panels due to SIGINT */
1410 break;
1411 default:
1412 res = MSG_NOT_HANDLED;
1415 return res;
1418 /* --------------------------------------------------------------------------------------------- */
1421 * Whether the command-line should not respond to key events.
1423 * This is TRUE if a QuickView or TreeView have the focus, as they're going
1424 * to consume some keys and there's no sense in passing to the command-line
1425 * just the leftovers.
1427 static gboolean
1428 is_cmdline_mute (void)
1430 /* When one of panels is other than view_listing,
1431 current_panel points to view_listing panel all time independently of
1432 it's activity. Thus, we can't use get_current_type() here.
1433 current_panel should point to actually current active panel
1434 independently of it's type. */
1435 return (!current_panel->active
1436 && (get_other_type () == view_quick || get_other_type () == view_tree));
1439 /* --------------------------------------------------------------------------------------------- */
1442 * Handles the Enter key on the command-line.
1444 * Returns TRUE if non-whitespace was indeed processed.
1446 static gboolean
1447 handle_cmdline_enter (void)
1449 const char *s;
1451 for (s = input_get_ctext (cmdline); *s != '\0' && whitespace (*s); s++)
1454 if (*s != '\0')
1456 send_message (cmdline, NULL, MSG_KEY, '\n', NULL);
1457 return TRUE;
1460 input_insert (cmdline, "", FALSE);
1461 cmdline->point = 0;
1463 return FALSE;
1466 /* --------------------------------------------------------------------------------------------- */
1468 static cb_ret_t
1469 midnight_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
1471 long command;
1473 switch (msg)
1475 case MSG_INIT:
1476 panel_init ();
1477 setup_panels ();
1478 return MSG_HANDLED;
1480 case MSG_DRAW:
1481 load_hint (TRUE);
1482 group_default_callback (w, NULL, MSG_DRAW, 0, NULL);
1483 /* We handle the special case of the output lines */
1484 if (mc_global.tty.console_flag != '\0' && output_lines != 0)
1486 unsigned char end_line;
1488 end_line = LINES - (mc_global.keybar_visible ? 1 : 0) - 1;
1489 show_console_contents (output_start_y, end_line - output_lines, end_line);
1491 return MSG_HANDLED;
1493 case MSG_RESIZE:
1494 widget_adjust_position (w->pos_flags, &w->rect);
1495 setup_panels ();
1496 menubar_arrange (the_menubar);
1497 return MSG_HANDLED;
1499 case MSG_IDLE:
1500 /* We only need the first idle event to show user menu after start */
1501 widget_idle (w, FALSE);
1503 if (boot_current_is_left)
1504 widget_select (get_panel_widget (0));
1505 else
1506 widget_select (get_panel_widget (1));
1508 if (auto_menu)
1509 midnight_execute_cmd (NULL, CK_UserMenu);
1510 return MSG_HANDLED;
1512 case MSG_KEY:
1513 if (w->ext_mode)
1515 command = widget_lookup_key (w, parm);
1516 if (command != CK_IgnoreKey)
1517 return midnight_execute_cmd (NULL, command);
1520 /* FIXME: should handle all menu shortcuts before this point */
1521 if (widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1522 return MSG_NOT_HANDLED;
1524 if (parm == '\n' && !is_cmdline_mute ())
1526 if (handle_cmdline_enter ())
1527 return MSG_HANDLED;
1528 /* Else: the panel will handle it. */
1531 if ((!mc_global.tty.alternate_plus_minus
1532 || !(mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)) && !quote
1533 && !current_panel->quick_search.active)
1535 if (!only_leading_plus_minus)
1537 /* Special treatment, since the input line will eat them */
1538 if (parm == '+')
1539 return send_message (current_panel, filemanager, MSG_ACTION, CK_Select, NULL);
1541 if (parm == '\\' || parm == '-')
1542 return send_message (current_panel, filemanager, MSG_ACTION, CK_Unselect, NULL);
1544 if (parm == '*')
1545 return send_message (current_panel, filemanager, MSG_ACTION, CK_SelectInvert,
1546 NULL);
1548 else if (!command_prompt || input_is_empty (cmdline))
1550 /* Special treatment '+', '-', '\', '*' only when this is
1551 * first char on input line
1553 if (parm == '+')
1554 return send_message (current_panel, filemanager, MSG_ACTION, CK_Select, NULL);
1556 if (parm == '\\' || parm == '-')
1557 return send_message (current_panel, filemanager, MSG_ACTION, CK_Unselect, NULL);
1559 if (parm == '*')
1560 return send_message (current_panel, filemanager, MSG_ACTION, CK_SelectInvert,
1561 NULL);
1564 return MSG_NOT_HANDLED;
1566 case MSG_HOTKEY_HANDLED:
1567 if ((get_current_type () == view_listing) && current_panel->quick_search.active)
1569 current_panel->dirty = TRUE; /* FIXME: unneeded? */
1570 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1572 return MSG_HANDLED;
1574 case MSG_UNHANDLED_KEY:
1576 cb_ret_t v = MSG_NOT_HANDLED;
1578 command = widget_lookup_key (w, parm);
1579 if (command != CK_IgnoreKey)
1580 v = midnight_execute_cmd (NULL, command);
1582 if (v == MSG_NOT_HANDLED && command_prompt && !is_cmdline_mute ())
1583 v = send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1585 return v;
1588 case MSG_POST_KEY:
1589 if (!widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1590 update_dirty_panels ();
1591 return MSG_HANDLED;
1593 case MSG_ACTION:
1594 /* Handle shortcuts, menu, and buttonbar. */
1595 return midnight_execute_cmd (sender, parm);
1597 case MSG_DESTROY:
1598 panel_deinit ();
1599 return MSG_HANDLED;
1601 default:
1602 return dlg_default_callback (w, sender, msg, parm, data);
1606 /* --------------------------------------------------------------------------------------------- */
1607 /*** public functions ****************************************************************************/
1608 /* --------------------------------------------------------------------------------------------- */
1610 void
1611 update_menu (void)
1613 menu_set_name (left_menu, panels_layout.horizontal_split ? _("&Above") : _("&Left"));
1614 menu_set_name (right_menu, panels_layout.horizontal_split ? _("&Below") : _("&Right"));
1615 menubar_arrange (the_menubar);
1616 widget_set_visibility (WIDGET (the_menubar), menubar_visible);
1619 /* --------------------------------------------------------------------------------------------- */
1621 void
1622 midnight_set_buttonbar (WButtonBar * b)
1624 Widget *w = WIDGET (filemanager);
1626 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), w->keymap, NULL);
1627 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), w->keymap, NULL);
1628 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), w->keymap, NULL);
1629 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), w->keymap, NULL);
1630 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), w->keymap, NULL);
1631 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), w->keymap, NULL);
1632 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), w->keymap, NULL);
1633 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), w->keymap, NULL);
1634 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), w->keymap, NULL);
1635 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), w->keymap, NULL);
1638 /* --------------------------------------------------------------------------------------------- */
1640 * Return a random hint. If force is TRUE, ignore the timeout.
1643 char *
1644 get_random_hint (gboolean force)
1646 static const gint64 update_period = 60 * G_USEC_PER_SEC;
1647 static gint64 tv = 0;
1649 char *data, *result, *eop;
1650 size_t len, start;
1651 GIConv conv;
1653 /* Do not change hints more often than one minute */
1654 if (!force && !mc_time_elapsed (&tv, update_period))
1655 return g_strdup ("");
1657 data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
1658 if (data == NULL)
1659 return NULL;
1661 /* get a random entry */
1662 srand ((unsigned int) (tv / G_USEC_PER_SEC));
1663 start = ((size_t) rand ()) % (len - 1);
1665 /* Search the start of paragraph */
1666 for (; start != 0; start--)
1667 if (data[start] == '\n' && data[start + 1] == '\n')
1669 start += 2;
1670 break;
1673 /* Search the end of paragraph */
1674 for (eop = data + start; *eop != '\0'; eop++)
1676 if (*eop == '\n' && *(eop + 1) == '\n')
1678 *eop = '\0';
1679 break;
1681 if (*eop == '\n')
1682 *eop = ' ';
1685 /* hint files are stored in utf-8 */
1686 /* try convert hint file from utf-8 to terminal encoding */
1687 conv = str_crt_conv_from ("UTF-8");
1688 if (conv == INVALID_CONV)
1689 result = g_strndup (data + start, len - start);
1690 else
1692 GString *buffer;
1693 gboolean nok;
1695 buffer = g_string_sized_new (len - start);
1696 nok = (str_convert (conv, data + start, buffer) == ESTR_FAILURE);
1697 result = g_string_free (buffer, nok);
1698 str_close_conv (conv);
1701 g_free (data);
1702 return result;
1706 /* --------------------------------------------------------------------------------------------- */
1708 * Load new hint and display it.
1709 * IF force is not 0, ignore the timeout.
1712 void
1713 load_hint (gboolean force)
1715 char *hint;
1717 if (WIDGET (the_hint)->owner == NULL)
1718 return;
1720 if (!mc_global.message_visible)
1722 label_set_text (the_hint, NULL);
1723 return;
1726 hint = get_random_hint (force);
1728 if (hint != NULL)
1730 if (*hint != '\0')
1731 set_hintbar (hint);
1732 g_free (hint);
1734 else
1736 char text[BUF_SMALL];
1738 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), mc_global.mc_version);
1739 set_hintbar (text);
1743 /* --------------------------------------------------------------------------------------------- */
1745 * Change current panel in the file manager.
1747 * @return current_panel
1750 WPanel *
1751 change_panel (void)
1753 input_complete_free (cmdline);
1754 group_select_next_widget (GROUP (filemanager));
1755 return current_panel;
1758 /* --------------------------------------------------------------------------------------------- */
1760 /** Save current stat of directories to avoid reloading the panels
1761 * when no modifications have taken place
1763 void
1764 save_cwds_stat (void)
1766 if (panels_options.fast_reload)
1768 mc_stat (current_panel->cwd_vpath, &(current_panel->dir_stat));
1769 if (get_other_type () == view_listing)
1770 mc_stat (other_panel->cwd_vpath, &(other_panel->dir_stat));
1774 /* --------------------------------------------------------------------------------------------- */
1776 gboolean
1777 quiet_quit_cmd (void)
1779 print_last_revert = TRUE;
1780 return quit_cmd_internal (1);
1783 /* --------------------------------------------------------------------------------------------- */
1785 /** Run the main dialog that occupies the whole screen */
1786 gboolean
1787 do_nc (void)
1789 gboolean ret;
1791 #ifdef USE_INTERNAL_EDIT
1792 edit_stack_init ();
1793 #endif
1795 filemanager = dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, dialog_colors,
1796 midnight_callback, NULL, "[main]", NULL);
1798 /* Check if we were invoked as an editor or file viewer */
1799 if (mc_global.mc_run_mode != MC_RUN_FULL)
1801 setup_dummy_mc ();
1802 ret = mc_maybe_editor_or_viewer ();
1804 else
1806 /* We only need the first idle event to show user menu after start */
1807 widget_idle (WIDGET (filemanager), TRUE);
1809 setup_mc ();
1810 mc_filehighlight = mc_fhl_new (TRUE);
1812 create_file_manager ();
1813 (void) dlg_run (filemanager);
1815 mc_fhl_free (&mc_filehighlight);
1817 ret = TRUE;
1819 /* widget_destroy destroys even current_panel->cwd_vpath, so we have to save a copy :) */
1820 if (mc_args__last_wd_file != NULL && vfs_current_is_local ())
1821 last_wd_string = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
1823 /* don't handle VFS timestamps for dirs opened in panels */
1824 mc_event_destroy (MCEVENT_GROUP_CORE, "vfs_timestamp");
1827 /* Program end */
1828 mc_global.midnight_shutdown = TRUE;
1829 dialog_switch_shutdown ();
1830 done_mc ();
1831 widget_destroy (WIDGET (filemanager));
1832 current_panel = NULL;
1834 #ifdef USE_INTERNAL_EDIT
1835 edit_stack_free ();
1836 #endif
1838 if ((quit & SUBSHELL_EXIT) == 0)
1839 tty_clear_screen ();
1841 return ret;
1844 /* --------------------------------------------------------------------------------------------- */