Merge branch '4600_filter_segfault'
[midnight-commander.git] / src / filemanager / filemanager.c
blob7291b896eaaa0075e6e2cd81aae4717bec5eee35
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 "src/usermenu.h" /* user_file_menu_cmd() */
65 #include "lib/fileloc.h" /* MC_FILEPOS_FILE */
66 #include "lib/keybind.h"
67 #include "lib/event.h"
69 #include "tree.h"
70 #include "boxes.h" /* sort_box(), tree_box() */
71 #include "layout.h"
72 #include "cmd.h" /* commands */
73 #include "hotlist.h"
74 #include "panelize.h"
75 #include "command.h" /* cmdline */
76 #include "dir.h" /* dir_list_clean() */
78 #ifdef USE_INTERNAL_EDIT
79 #include "src/editor/edit.h"
80 #endif
82 #ifdef USE_DIFF_VIEW
83 #include "src/diffviewer/ydiff.h"
84 #endif
86 #include "src/consaver/cons.saver.h" /* show_console_contents */
87 #include "src/file_history.h" /* show_file_history() */
89 #include "filemanager.h"
91 /*** global variables ****************************************************************************/
93 /* When the modes are active, left_panel, right_panel and tree_panel */
94 /* point to a proper data structure. You should check with the functions */
95 /* get_current_type and get_other_type the types of the panels before using */
96 /* this pointer variables */
98 /* The structures for the panels */
99 WPanel *left_panel = NULL;
100 WPanel *right_panel = NULL;
101 /* Pointer to the selected and unselected panel */
102 WPanel *current_panel = NULL;
104 /* The Menubar */
105 WMenuBar *the_menubar = NULL;
106 /* The widget where we draw the prompt */
107 WLabel *the_prompt;
108 /* The hint bar */
109 WLabel *the_hint;
110 /* The button bar */
111 WButtonBar *the_bar;
113 /* The prompt */
114 const char *mc_prompt = NULL;
116 /*** file scope macro definitions ****************************************************************/
118 #ifdef HAVE_CHARSET
120 * Don't restrict the output on the screen manager level,
121 * the translation tables take care of it.
123 #endif /* !HAVE_CHARSET */
125 /*** file scope type declarations ****************************************************************/
127 /*** forward declarations (file scope functions) *************************************************/
129 /*** file scope variables ************************************************************************/
131 static menu_t *left_menu, *right_menu;
133 /* --------------------------------------------------------------------------------------------- */
134 /*** file scope functions ************************************************************************/
135 /* --------------------------------------------------------------------------------------------- */
137 /** Stop MC main dialog and the current dialog if it exists.
138 * Needed to provide fast exit from MC viewer or editor on shell exit */
139 static void
140 stop_dialogs (void)
142 dlg_close (filemanager);
144 if (top_dlg != NULL)
145 dlg_close (DIALOG (top_dlg->data));
148 /* --------------------------------------------------------------------------------------------- */
150 static void
151 treebox_cmd (void)
153 const file_entry_t *fe;
154 char *sel_dir;
156 fe = panel_current_entry (current_panel);
157 if (fe == NULL)
158 return;
160 sel_dir = tree_box (fe->fname->str);
161 if (sel_dir != NULL)
163 vfs_path_t *sel_vdir;
165 sel_vdir = vfs_path_from_str (sel_dir);
166 panel_cd (current_panel, sel_vdir, cd_exact);
167 vfs_path_free (sel_vdir, TRUE);
168 g_free (sel_dir);
172 /* --------------------------------------------------------------------------------------------- */
174 #ifdef LISTMODE_EDITOR
175 static void
176 listmode_cmd (void)
178 char *newmode;
180 if (get_current_type () != view_listing)
181 return;
183 newmode = listmode_edit (current_panel->user_format);
184 if (!newmode)
185 return;
187 g_free (current_panel->user_format);
188 current_panel->list_format = list_user;
189 current_panel->user_format = newmode;
190 set_panel_formats (current_panel);
192 do_refresh ();
194 #endif /* LISTMODE_EDITOR */
196 /* --------------------------------------------------------------------------------------------- */
198 static GList *
199 create_panel_menu (void)
201 GList *entries = NULL;
203 entries = g_list_prepend (entries, menu_entry_new (_("File listin&g"), CK_PanelListing));
204 entries = g_list_prepend (entries, menu_entry_new (_("&Quick view"), CK_PanelQuickView));
205 entries = g_list_prepend (entries, menu_entry_new (_("&Info"), CK_PanelInfo));
206 entries = g_list_prepend (entries, menu_entry_new (_("&Tree"), CK_PanelTree));
207 entries = g_list_prepend (entries, menu_separator_new ());
208 entries =
209 g_list_prepend (entries, menu_entry_new (_("&Listing format..."), CK_SetupListingFormat));
210 entries = g_list_prepend (entries, menu_entry_new (_("&Sort order..."), CK_Sort));
211 entries = g_list_prepend (entries, menu_entry_new (_("&Filter..."), CK_Filter));
212 #ifdef HAVE_CHARSET
213 entries = g_list_prepend (entries, menu_entry_new (_("&Encoding..."), CK_SelectCodepage));
214 #endif
215 entries = g_list_prepend (entries, menu_separator_new ());
216 #ifdef ENABLE_VFS_FTP
217 entries = g_list_prepend (entries, menu_entry_new (_("FT&P link..."), CK_ConnectFtp));
218 #endif
219 #ifdef ENABLE_VFS_SHELL
220 entries = g_list_prepend (entries, menu_entry_new (_("S&hell link..."), CK_ConnectShell));
221 #endif
222 #ifdef ENABLE_VFS_SFTP
223 entries = g_list_prepend (entries, menu_entry_new (_("SFTP li&nk..."), CK_ConnectSftp));
224 #endif
225 entries = g_list_prepend (entries, menu_entry_new (_("Paneli&ze"), CK_Panelize));
226 entries = g_list_prepend (entries, menu_separator_new ());
227 entries = g_list_prepend (entries, menu_entry_new (_("&Rescan"), CK_Reread));
229 return g_list_reverse (entries);
232 /* --------------------------------------------------------------------------------------------- */
234 static GList *
235 create_file_menu (void)
237 GList *entries = NULL;
239 entries = g_list_prepend (entries, menu_entry_new (_("&View"), CK_View));
240 entries = g_list_prepend (entries, menu_entry_new (_("Vie&w file..."), CK_ViewFile));
241 entries = g_list_prepend (entries, menu_entry_new (_("&Filtered view"), CK_ViewFiltered));
242 entries = g_list_prepend (entries, menu_entry_new (_("&Edit"), CK_Edit));
243 entries = g_list_prepend (entries, menu_entry_new (_("&Copy"), CK_Copy));
244 entries = g_list_prepend (entries, menu_entry_new (_("C&hmod"), CK_ChangeMode));
245 entries = g_list_prepend (entries, menu_entry_new (_("&Link"), CK_Link));
246 entries = g_list_prepend (entries, menu_entry_new (_("&Symlink"), CK_LinkSymbolic));
247 entries =
248 g_list_prepend (entries, menu_entry_new (_("Relative symlin&k"), CK_LinkSymbolicRelative));
249 entries = g_list_prepend (entries, menu_entry_new (_("Edit s&ymlink"), CK_LinkSymbolicEdit));
250 entries = g_list_prepend (entries, menu_entry_new (_("Ch&own"), CK_ChangeOwn));
251 entries = g_list_prepend (entries, menu_entry_new (_("&Advanced chown"), CK_ChangeOwnAdvanced));
252 #ifdef ENABLE_EXT2FS_ATTR
253 entries = g_list_prepend (entries, menu_entry_new (_("Cha&ttr"), CK_ChangeAttributes));
254 #endif
255 entries = g_list_prepend (entries, menu_entry_new (_("&Rename/Move"), CK_Move));
256 entries = g_list_prepend (entries, menu_entry_new (_("&Mkdir"), CK_MakeDir));
257 entries = g_list_prepend (entries, menu_entry_new (_("&Delete"), CK_Delete));
258 entries = g_list_prepend (entries, menu_entry_new (_("&Quick cd"), CK_CdQuick));
259 entries = g_list_prepend (entries, menu_separator_new ());
260 entries = g_list_prepend (entries, menu_entry_new (_("Select &group"), CK_Select));
261 entries = g_list_prepend (entries, menu_entry_new (_("U&nselect group"), CK_Unselect));
262 entries = g_list_prepend (entries, menu_entry_new (_("&Invert selection"), CK_SelectInvert));
263 entries = g_list_prepend (entries, menu_separator_new ());
264 entries = g_list_prepend (entries, menu_entry_new (_("E&xit"), CK_Quit));
266 return g_list_reverse (entries);
269 /* --------------------------------------------------------------------------------------------- */
271 static GList *
272 create_command_menu (void)
274 /* I know, I'm lazy, but the tree widget when it's not running
275 * as a panel still has some problems, I have not yet finished
276 * the WTree widget port, sorry.
278 GList *entries = NULL;
280 entries = g_list_prepend (entries, menu_entry_new (_("&User menu"), CK_UserMenu));
281 entries = g_list_prepend (entries, menu_entry_new (_("&Directory tree"), CK_Tree));
282 entries = g_list_prepend (entries, menu_entry_new (_("&Find file"), CK_Find));
283 entries = g_list_prepend (entries, menu_entry_new (_("S&wap panels"), CK_Swap));
284 entries = g_list_prepend (entries, menu_entry_new (_("Switch &panels on/off"), CK_Shell));
285 entries = g_list_prepend (entries, menu_entry_new (_("&Compare directories"), CK_CompareDirs));
286 #ifdef USE_DIFF_VIEW
287 entries = g_list_prepend (entries, menu_entry_new (_("C&ompare files"), CK_CompareFiles));
288 #endif
289 entries =
290 g_list_prepend (entries, menu_entry_new (_("E&xternal panelize"), CK_ExternalPanelize));
291 entries = g_list_prepend (entries, menu_entry_new (_("Show directory s&izes"), CK_DirSize));
292 entries = g_list_prepend (entries, menu_separator_new ());
293 entries = g_list_prepend (entries, menu_entry_new (_("Command &history"), CK_History));
294 entries =
295 g_list_prepend (entries,
296 menu_entry_new (_("Viewed/edited files hi&story"), CK_EditorViewerHistory));
297 entries = g_list_prepend (entries, menu_entry_new (_("Di&rectory hotlist"), CK_HotList));
298 #ifdef ENABLE_VFS
299 entries = g_list_prepend (entries, menu_entry_new (_("&Active VFS list"), CK_VfsList));
300 #endif
301 #ifdef ENABLE_BACKGROUND
302 entries = g_list_prepend (entries, menu_entry_new (_("&Background jobs"), CK_Jobs));
303 #endif
304 entries = g_list_prepend (entries, menu_entry_new (_("Screen lis&t"), CK_ScreenList));
305 entries = g_list_prepend (entries, menu_separator_new ());
306 #ifdef ENABLE_VFS_UNDELFS
307 entries =
308 g_list_prepend (entries, menu_entry_new (_("&Undelete files (ext2fs only)"), CK_Undelete));
309 #endif
310 #ifdef LISTMODE_EDITOR
311 entries = g_list_prepend (entries, menu_entry_new (_("&Listing format edit"), CK_ListMode));
312 #endif
313 #if defined (ENABLE_VFS_UNDELFS) || defined (LISTMODE_EDITOR)
314 entries = g_list_prepend (entries, menu_separator_new ());
315 #endif
316 entries =
317 g_list_prepend (entries, menu_entry_new (_("Edit &extension file"), CK_EditExtensionsFile));
318 entries = g_list_prepend (entries, menu_entry_new (_("Edit &menu file"), CK_EditUserMenu));
319 entries =
320 g_list_prepend (entries,
321 menu_entry_new (_("Edit hi&ghlighting group file"),
322 CK_EditFileHighlightFile));
324 return g_list_reverse (entries);
327 /* --------------------------------------------------------------------------------------------- */
329 static GList *
330 create_options_menu (void)
332 GList *entries = NULL;
334 entries = g_list_prepend (entries, menu_entry_new (_("&Configuration..."), CK_Options));
335 entries = g_list_prepend (entries, menu_entry_new (_("&Layout..."), CK_OptionsLayout));
336 entries = g_list_prepend (entries, menu_entry_new (_("&Panel options..."), CK_OptionsPanel));
337 entries = g_list_prepend (entries, menu_entry_new (_("C&onfirmation..."), CK_OptionsConfirm));
338 entries = g_list_prepend (entries, menu_entry_new (_("&Appearance..."), CK_OptionsAppearance));
339 entries =
340 g_list_prepend (entries, menu_entry_new (_("&Display bits..."), CK_OptionsDisplayBits));
341 entries = g_list_prepend (entries, menu_entry_new (_("Learn &keys..."), CK_LearnKeys));
342 #ifdef ENABLE_VFS
343 entries = g_list_prepend (entries, menu_entry_new (_("&Virtual FS..."), CK_OptionsVfs));
344 #endif
345 entries = g_list_prepend (entries, menu_separator_new ());
346 entries = g_list_prepend (entries, menu_entry_new (_("&Save setup"), CK_SaveSetup));
348 return g_list_reverse (entries);
351 /* --------------------------------------------------------------------------------------------- */
353 static void
354 init_menu (void)
356 left_menu = menu_new ("", create_panel_menu (), "[Left and Right Menus]");
357 menubar_add_menu (the_menubar, left_menu);
358 menubar_add_menu (the_menubar, menu_new (_("&File"), create_file_menu (), "[File Menu]"));
359 menubar_add_menu (the_menubar,
360 menu_new (_("&Command"), create_command_menu (), "[Command Menu]"));
361 menubar_add_menu (the_menubar,
362 menu_new (_("&Options"), create_options_menu (), "[Options Menu]"));
363 right_menu = menu_new ("", create_panel_menu (), "[Left and Right Menus]");
364 menubar_add_menu (the_menubar, right_menu);
365 update_menu ();
368 /* --------------------------------------------------------------------------------------------- */
370 static void
371 menu_last_selected_cmd (void)
373 menubar_activate (the_menubar, drop_menus, -1);
376 /* --------------------------------------------------------------------------------------------- */
378 static void
379 menu_cmd (void)
381 int selected;
383 if ((get_current_index () == 0) == current_panel->active)
384 selected = 0;
385 else
386 selected = g_list_length (the_menubar->menu) - 1;
388 menubar_activate (the_menubar, drop_menus, selected);
391 /* --------------------------------------------------------------------------------------------- */
393 static void
394 sort_cmd (void)
396 WPanel *p;
397 const panel_field_t *sort_order;
399 if (!SELECTED_IS_PANEL)
400 return;
402 p = MENU_PANEL;
403 sort_order = sort_box (&p->sort_info, p->sort_field);
404 panel_set_sort_order (p, sort_order);
407 /* --------------------------------------------------------------------------------------------- */
409 static char *
410 midnight_get_shortcut (long command)
412 const char *ext_map;
413 const char *shortcut = NULL;
415 shortcut = keybind_lookup_keymap_shortcut (filemanager_map, command);
416 if (shortcut != NULL)
417 return g_strdup (shortcut);
419 shortcut = keybind_lookup_keymap_shortcut (panel_map, command);
420 if (shortcut != NULL)
421 return g_strdup (shortcut);
423 ext_map = keybind_lookup_keymap_shortcut (filemanager_map, CK_ExtendedKeyMap);
424 if (ext_map != NULL)
425 shortcut = keybind_lookup_keymap_shortcut (filemanager_x_map, command);
426 if (shortcut != NULL)
427 return g_strdup_printf ("%s %s", ext_map, shortcut);
429 return NULL;
432 /* --------------------------------------------------------------------------------------------- */
434 static char *
435 midnight_get_title (const WDialog *h, size_t len)
437 char *path;
438 char *login;
439 char *p;
441 (void) h;
443 title_path_prepare (&path, &login);
445 p = g_strdup_printf ("%s [%s]:%s", _("Panels:"), login, path);
446 g_free (path);
447 g_free (login);
448 path = g_strdup (str_trunc (p, len - 4));
449 g_free (p);
451 return path;
454 /* --------------------------------------------------------------------------------------------- */
456 static void
457 toggle_panels_split (void)
459 panels_layout.horizontal_split = !panels_layout.horizontal_split;
460 layout_change ();
461 do_refresh ();
464 /* --------------------------------------------------------------------------------------------- */
466 #ifdef ENABLE_VFS
467 /* event helper */
468 static gboolean
469 check_panel_timestamp (const WPanel *panel, panel_view_mode_t mode, const struct vfs_class *vclass,
470 const vfsid id)
472 return (mode != view_listing || (vfs_path_get_last_path_vfs (panel->cwd_vpath) == vclass
473 && vfs_getid (panel->cwd_vpath) == id));
476 /* --------------------------------------------------------------------------------------------- */
478 /* event callback */
479 static gboolean
480 check_current_panel_timestamp (const gchar *event_group_name, const gchar *event_name,
481 gpointer init_data, gpointer data)
483 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
485 (void) event_group_name;
486 (void) event_name;
487 (void) init_data;
489 event_data->ret =
490 check_panel_timestamp (current_panel, get_current_type (), event_data->vclass,
491 event_data->id);
492 return !event_data->ret;
495 /* --------------------------------------------------------------------------------------------- */
497 /* event callback */
498 static gboolean
499 check_other_panel_timestamp (const gchar *event_group_name, const gchar *event_name,
500 gpointer init_data, gpointer data)
502 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
504 (void) event_group_name;
505 (void) event_name;
506 (void) init_data;
508 event_data->ret =
509 check_panel_timestamp (other_panel, get_other_type (), event_data->vclass, event_data->id);
510 return !event_data->ret;
512 #endif /* ENABLE_VFS */
514 /* --------------------------------------------------------------------------------------------- */
516 /* event callback */
517 static gboolean
518 print_vfs_message (const gchar *event_group_name, const gchar *event_name,
519 gpointer init_data, gpointer data)
521 ev_vfs_print_message_t *event_data = (ev_vfs_print_message_t *) data;
523 (void) event_group_name;
524 (void) event_name;
525 (void) init_data;
527 if (mc_global.midnight_shutdown)
528 goto ret;
530 if (!mc_global.message_visible || the_hint == NULL || WIDGET (the_hint)->owner == NULL)
532 int col, row;
534 if (!nice_rotating_dash || (ok_to_refresh <= 0))
535 goto ret;
537 /* Preserve current cursor position */
538 tty_getyx (&row, &col);
540 tty_gotoyx (0, 0);
541 tty_setcolor (NORMAL_COLOR);
542 tty_print_string (str_fit_to_term (event_data->msg, COLS - 1, J_LEFT));
544 /* Restore cursor position */
545 tty_gotoyx (row, col);
546 mc_refresh ();
547 goto ret;
550 if (mc_global.message_visible)
551 set_hintbar (event_data->msg);
553 ret:
554 MC_PTR_FREE (event_data->msg);
555 return TRUE;
558 /* --------------------------------------------------------------------------------------------- */
560 static void
561 create_panels (void)
563 int current_index, other_index;
564 panel_view_mode_t current_mode, other_mode;
565 char *current_dir, *other_dir;
566 vfs_path_t *original_dir;
569 * Following cases from command line are possible:
570 * 'mc' (no arguments): mc_run_param0 == NULL, mc_run_param1 == NULL
571 * active panel uses current directory
572 * passive panel uses "other_dir" from panels.ini
574 * 'mc dir1 dir2' (two arguments): mc_run_param0 != NULL, mc_run_param1 != NULL
575 * active panel uses mc_run_param0
576 * passive panel uses mc_run_param1
578 * 'mc dir1' (single argument): mc_run_param0 != NULL, mc_run_param1 == NULL
579 * active panel uses mc_run_param0
580 * passive panel uses "other_dir" from panels.ini
583 /* Set up panel directories */
584 if (boot_current_is_left)
586 /* left panel is active */
587 current_index = 0;
588 other_index = 1;
589 current_mode = startup_left_mode;
590 other_mode = startup_right_mode;
592 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
594 /* no arguments */
595 current_dir = NULL; /* assume current dir */
596 other_dir = saved_other_dir; /* from ini */
598 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
600 /* two arguments */
601 current_dir = (char *) mc_run_param0;
602 other_dir = mc_run_param1;
604 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
606 /* one argument */
607 current_dir = (char *) mc_run_param0;
608 other_dir = saved_other_dir; /* from ini */
611 else
613 /* right panel is active */
614 current_index = 1;
615 other_index = 0;
616 current_mode = startup_right_mode;
617 other_mode = startup_left_mode;
619 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
621 /* no arguments */
622 current_dir = NULL; /* assume current dir */
623 other_dir = saved_other_dir; /* from ini */
625 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
627 /* two arguments */
628 current_dir = (char *) mc_run_param0;
629 other_dir = mc_run_param1;
631 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
633 /* one argument */
634 current_dir = (char *) mc_run_param0;
635 other_dir = saved_other_dir; /* from ini */
639 /* 1. Get current dir */
640 original_dir = vfs_path_clone (vfs_get_raw_current_dir ());
642 /* 2. Create passive panel */
643 if (other_dir != NULL)
645 vfs_path_t *vpath;
647 if (g_path_is_absolute (other_dir))
648 vpath = vfs_path_from_str (other_dir);
649 else
650 vpath = vfs_path_append_new (original_dir, other_dir, (char *) NULL);
651 mc_chdir (vpath);
652 vfs_path_free (vpath, TRUE);
654 create_panel (other_index, other_mode);
656 /* 3. Create active panel */
657 if (current_dir == NULL)
658 mc_chdir (original_dir);
659 else
661 vfs_path_t *vpath;
663 if (g_path_is_absolute (current_dir))
664 vpath = vfs_path_from_str (current_dir);
665 else
666 vpath = vfs_path_append_new (original_dir, current_dir, (char *) NULL);
667 mc_chdir (vpath);
668 vfs_path_free (vpath, TRUE);
670 create_panel (current_index, current_mode);
672 if (startup_left_mode == view_listing)
673 current_panel = left_panel;
674 else if (right_panel != NULL)
675 current_panel = right_panel;
676 else
677 current_panel = left_panel;
679 vfs_path_free (original_dir, TRUE);
681 #ifdef ENABLE_VFS
682 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_other_panel_timestamp, NULL, NULL);
683 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_current_panel_timestamp, NULL, NULL);
684 #endif /* ENABLE_VFS */
686 mc_event_add (MCEVENT_GROUP_CORE, "vfs_print_message", print_vfs_message, NULL, NULL);
689 /* --------------------------------------------------------------------------------------------- */
691 static void
692 midnight_put_panel_path (WPanel *panel)
694 vfs_path_t *cwd_vpath;
695 const char *cwd_vpath_str;
697 if (!command_prompt)
698 return;
700 #ifdef HAVE_CHARSET
701 cwd_vpath = remove_encoding_from_path (panel->cwd_vpath);
702 #else
703 cwd_vpath = vfs_path_clone (panel->cwd_vpath);
704 #endif
706 cwd_vpath_str = vfs_path_as_str (cwd_vpath);
708 command_insert (cmdline, cwd_vpath_str, FALSE);
710 if (!IS_PATH_SEP (cwd_vpath_str[strlen (cwd_vpath_str) - 1]))
711 command_insert (cmdline, PATH_SEP_STR, FALSE);
713 vfs_path_free (cwd_vpath, TRUE);
716 /* --------------------------------------------------------------------------------------------- */
718 static void
719 put_link (WPanel *panel)
721 const file_entry_t *fe;
723 if (!command_prompt)
724 return;
726 fe = panel_current_entry (panel);
728 if (fe != NULL && S_ISLNK (fe->st.st_mode))
730 char buffer[MC_MAXPATHLEN];
731 vfs_path_t *vpath;
732 int i;
734 vpath = vfs_path_append_new (panel->cwd_vpath, fe->fname->str, (char *) NULL);
735 i = mc_readlink (vpath, buffer, sizeof (buffer) - 1);
736 vfs_path_free (vpath, TRUE);
738 if (i > 0)
740 buffer[i] = '\0';
741 command_insert (cmdline, buffer, TRUE);
746 /* --------------------------------------------------------------------------------------------- */
748 static void
749 put_current_link (void)
751 put_link (current_panel);
754 /* --------------------------------------------------------------------------------------------- */
756 static void
757 put_other_link (void)
759 if (get_other_type () == view_listing)
760 put_link (other_panel);
763 /* --------------------------------------------------------------------------------------------- */
765 /** Insert the selected file name into the input line */
766 static void
767 put_current_selected (void)
769 if (!command_prompt)
770 return;
772 if (get_current_type () == view_tree)
774 WTree *tree;
775 const vfs_path_t *selected_name;
777 tree = (WTree *) get_panel_widget (get_current_index ());
778 selected_name = tree_selected_name (tree);
779 command_insert (cmdline, vfs_path_as_str (selected_name), TRUE);
781 else
783 const file_entry_t *fe;
785 fe = panel_current_entry (current_panel);
786 if (fe != NULL)
787 command_insert (cmdline, fe->fname->str, TRUE);
791 /* --------------------------------------------------------------------------------------------- */
793 static void
794 put_tagged (WPanel *panel)
796 if (!command_prompt)
797 return;
799 input_disable_update (cmdline);
801 if (panel->marked == 0)
803 const file_entry_t *fe;
805 fe = panel_current_entry (current_panel);
806 if (fe != NULL)
807 command_insert (cmdline, fe->fname->str, TRUE);
809 else
811 int i;
813 for (i = 0; i < panel->dir.len; i++)
814 if (panel->dir.list[i].f.marked != 0)
815 command_insert (cmdline, panel->dir.list[i].fname->str, TRUE);
818 input_enable_update (cmdline);
821 /* --------------------------------------------------------------------------------------------- */
823 static void
824 put_current_tagged (void)
826 put_tagged (current_panel);
829 /* --------------------------------------------------------------------------------------------- */
831 static void
832 put_other_tagged (void)
834 if (get_other_type () == view_listing)
835 put_tagged (other_panel);
838 /* --------------------------------------------------------------------------------------------- */
840 static void
841 setup_mc (void)
843 #ifdef HAVE_SLANG
844 #ifdef HAVE_CHARSET
845 tty_display_8bit (TRUE);
846 #else
847 tty_display_8bit (mc_global.full_eight_bits);
848 #endif /* HAVE_CHARSET */
850 #else /* HAVE_SLANG */
852 #ifdef HAVE_CHARSET
853 tty_display_8bit (TRUE);
854 #else
855 tty_display_8bit (mc_global.eight_bit_clean);
856 #endif /* HAVE_CHARSET */
857 #endif /* HAVE_SLANG */
859 if ((tty_baudrate () < 9600) || mc_global.tty.slow_terminal)
860 verbose = FALSE;
863 /* --------------------------------------------------------------------------------------------- */
865 static void
866 setup_dummy_mc (void)
868 vfs_path_t *vpath;
869 char *d;
870 int ret;
872 d = vfs_get_cwd ();
873 setup_mc ();
874 vpath = vfs_path_from_str (d);
875 ret = mc_chdir (vpath);
876 (void) ret;
877 vfs_path_free (vpath, TRUE);
878 g_free (d);
881 /* --------------------------------------------------------------------------------------------- */
883 static void
884 done_mc (void)
886 /* Setup shutdown
888 * We sync the profiles since the hotlist may have changed, while
889 * we only change the setup data if we have the auto save feature set
892 save_setup (auto_save_setup, panels_options.auto_save_setup);
894 vfs_stamp_path (vfs_get_raw_current_dir ());
897 /* --------------------------------------------------------------------------------------------- */
899 static void
900 create_file_manager (void)
902 Widget *w = WIDGET (filemanager);
903 WGroup *g = GROUP (filemanager);
905 w->keymap = filemanager_map;
906 w->ext_keymap = filemanager_x_map;
908 filemanager->get_shortcut = midnight_get_shortcut;
909 filemanager->get_title = midnight_get_title;
910 /* allow rebind tab */
911 widget_want_tab (w, TRUE);
913 the_menubar = menubar_new (NULL);
914 group_add_widget (g, the_menubar);
915 init_menu ();
917 create_panels ();
918 group_add_widget (g, get_panel_widget (0));
919 group_add_widget (g, get_panel_widget (1));
921 the_hint = label_new (0, 0, NULL);
922 the_hint->transparent = TRUE;
923 the_hint->auto_adjust_cols = 0;
924 WIDGET (the_hint)->rect.cols = COLS;
925 group_add_widget (g, the_hint);
927 cmdline = command_new (0, 0, 0);
928 group_add_widget (g, cmdline);
930 the_prompt = label_new (0, 0, mc_prompt);
931 the_prompt->transparent = TRUE;
932 group_add_widget (g, the_prompt);
934 the_bar = buttonbar_new ();
935 group_add_widget (g, the_bar);
936 midnight_set_buttonbar (the_bar);
938 #ifdef ENABLE_SUBSHELL
939 /* Must be done after creation of cmdline and prompt widgets to avoid potential
940 NULL dereference in load_prompt() -> ... -> setup_cmdline() -> label_set_text(). */
941 if (mc_global.tty.use_subshell)
942 add_select_channel (mc_global.tty.subshell_pty, load_prompt, NULL);
943 #endif /* !ENABLE_SUBSHELL */
946 /* --------------------------------------------------------------------------------------------- */
948 /** result must be free'd (I think this should go in util.c) */
949 static vfs_path_t *
950 prepend_cwd_on_local (const char *filename)
952 vfs_path_t *vpath;
954 vpath = vfs_path_from_str (filename);
955 if (!vfs_file_is_local (vpath) || g_path_is_absolute (filename))
956 return vpath;
958 vfs_path_free (vpath, TRUE);
960 return vfs_path_append_new (vfs_get_raw_current_dir (), filename, (char *) NULL);
963 /* --------------------------------------------------------------------------------------------- */
965 /** Invoke the internal view/edit routine with:
966 * the default processing and forcing the internal viewer/editor
968 static gboolean
969 mc_maybe_editor_or_viewer (void)
971 gboolean ret;
973 switch (mc_global.mc_run_mode)
975 #ifdef USE_INTERNAL_EDIT
976 case MC_RUN_EDITOR:
977 ret = edit_files ((GList *) mc_run_param0);
978 break;
979 #endif /* USE_INTERNAL_EDIT */
980 case MC_RUN_VIEWER:
982 vfs_path_t *vpath = NULL;
984 if (mc_run_param0 != NULL && *(char *) mc_run_param0 != '\0')
985 vpath = prepend_cwd_on_local ((char *) mc_run_param0);
987 ret = view_file (vpath, FALSE, TRUE);
988 vfs_path_free (vpath, TRUE);
989 break;
991 #ifdef USE_DIFF_VIEW
992 case MC_RUN_DIFFVIEWER:
993 ret = dview_diff_cmd (mc_run_param0, mc_run_param1);
994 break;
995 #endif /* USE_DIFF_VIEW */
996 default:
997 ret = FALSE;
1000 return ret;
1003 /* --------------------------------------------------------------------------------------------- */
1005 static void
1006 show_editor_viewer_history (void)
1008 char *s;
1009 int act;
1011 s = show_file_history (WIDGET (filemanager), &act);
1012 if (s != NULL)
1014 vfs_path_t *s_vpath;
1016 switch (act)
1018 case CK_Edit:
1019 s_vpath = vfs_path_from_str (s);
1020 edit_file_at_line (s_vpath, use_internal_edit, 0);
1021 break;
1023 case CK_View:
1024 s_vpath = vfs_path_from_str (s);
1025 view_file (s_vpath, use_internal_view, FALSE);
1026 break;
1028 default:
1030 char *d;
1032 d = g_path_get_dirname (s);
1033 s_vpath = vfs_path_from_str (d);
1034 panel_cd (current_panel, s_vpath, cd_exact);
1035 panel_set_current_by_name (current_panel, s);
1036 g_free (d);
1040 g_free (s);
1041 vfs_path_free (s_vpath, TRUE);
1045 /* --------------------------------------------------------------------------------------------- */
1047 static gboolean
1048 quit_cmd_internal (int quiet)
1050 int q = quit;
1051 size_t n;
1053 n = dialog_switch_num () - 1;
1054 if (n != 0)
1056 char msg[BUF_MEDIUM];
1058 g_snprintf (msg, sizeof (msg),
1059 ngettext ("You have %zu opened screen. Quit anyway?",
1060 "You have %zu opened screens. Quit anyway?", n), n);
1062 if (query_dialog (_("The Midnight Commander"), msg, D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
1063 return FALSE;
1064 q = 1;
1066 else if (quiet || !confirm_exit)
1067 q = 1;
1068 else if (query_dialog (_("The Midnight Commander"),
1069 _("Do you really want to quit the Midnight Commander?"),
1070 D_NORMAL, 2, _("&Yes"), _("&No")) == 0)
1071 q = 1;
1073 if (q != 0)
1075 #ifdef ENABLE_SUBSHELL
1076 if (!mc_global.tty.use_subshell)
1077 stop_dialogs ();
1078 else if ((q = exit_subshell ()? 1 : 0) != 0)
1079 #endif
1080 stop_dialogs ();
1083 if (q != 0)
1084 quit |= 1;
1085 return (quit != 0);
1088 /* --------------------------------------------------------------------------------------------- */
1090 static gboolean
1091 quit_cmd (void)
1093 return quit_cmd_internal (0);
1096 /* --------------------------------------------------------------------------------------------- */
1099 * Repaint the contents of the panels without frames. To schedule panel
1100 * for repainting, set panel->dirty to TRUE. There are many reasons why
1101 * the panels need to be repainted, and this is a costly operation, so
1102 * it's done once per event.
1105 static void
1106 update_dirty_panels (void)
1108 if (get_current_type () == view_listing && current_panel->dirty)
1109 widget_draw (WIDGET (current_panel));
1111 if (get_other_type () == view_listing && other_panel->dirty)
1112 widget_draw (WIDGET (other_panel));
1115 /* --------------------------------------------------------------------------------------------- */
1117 static void
1118 toggle_show_hidden (void)
1120 panels_options.show_dot_files = !panels_options.show_dot_files;
1121 update_panels (UP_RELOAD, UP_KEEPSEL);
1122 /* redraw panels forced */
1123 update_dirty_panels ();
1126 /* --------------------------------------------------------------------------------------------- */
1128 static cb_ret_t
1129 midnight_execute_cmd (Widget *sender, long command)
1131 cb_ret_t res = MSG_HANDLED;
1133 (void) sender;
1135 /* stop quick search before executing any command */
1136 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1138 switch (command)
1140 case CK_ChangePanel:
1141 (void) change_panel ();
1142 break;
1143 case CK_HotListAdd:
1144 add2hotlist_cmd (current_panel);
1145 break;
1146 case CK_SetupListingFormat:
1147 setup_listing_format_cmd ();
1148 break;
1149 case CK_ChangeMode:
1150 chmod_cmd (current_panel);
1151 break;
1152 case CK_ChangeOwn:
1153 chown_cmd (current_panel);
1154 break;
1155 case CK_ChangeOwnAdvanced:
1156 advanced_chown_cmd (current_panel);
1157 break;
1158 #ifdef ENABLE_EXT2FS_ATTR
1159 case CK_ChangeAttributes:
1160 chattr_cmd (current_panel);
1161 break;
1162 #endif
1163 case CK_CompareDirs:
1164 compare_dirs_cmd ();
1165 break;
1166 case CK_Options:
1167 configure_box ();
1168 break;
1169 #ifdef ENABLE_VFS
1170 case CK_OptionsVfs:
1171 configure_vfs_box ();
1172 break;
1173 #endif
1174 case CK_OptionsConfirm:
1175 confirm_box ();
1176 break;
1177 case CK_Copy:
1178 copy_cmd (current_panel);
1179 break;
1180 case CK_PutCurrentPath:
1181 midnight_put_panel_path (current_panel);
1182 break;
1183 case CK_PutCurrentSelected:
1184 put_current_selected ();
1185 break;
1186 case CK_PutCurrentFullSelected:
1187 midnight_put_panel_path (current_panel);
1188 put_current_selected ();
1189 break;
1190 case CK_PutCurrentLink:
1191 put_current_link ();
1192 break;
1193 case CK_PutCurrentTagged:
1194 put_current_tagged ();
1195 break;
1196 case CK_PutOtherPath:
1197 if (get_other_type () == view_listing)
1198 midnight_put_panel_path (other_panel);
1199 break;
1200 case CK_PutOtherLink:
1201 put_other_link ();
1202 break;
1203 case CK_PutOtherTagged:
1204 put_other_tagged ();
1205 break;
1206 case CK_Delete:
1207 delete_cmd (current_panel);
1208 break;
1209 case CK_ScreenList:
1210 dialog_switch_list ();
1211 break;
1212 #ifdef USE_DIFF_VIEW
1213 case CK_CompareFiles:
1214 diff_view_cmd ();
1215 break;
1216 #endif
1217 case CK_OptionsDisplayBits:
1218 display_bits_box ();
1219 break;
1220 case CK_Edit:
1221 edit_cmd (current_panel);
1222 break;
1223 #ifdef USE_INTERNAL_EDIT
1224 case CK_EditForceInternal:
1225 edit_cmd_force_internal (current_panel);
1226 break;
1227 #endif
1228 case CK_EditExtensionsFile:
1229 ext_cmd ();
1230 break;
1231 case CK_EditFileHighlightFile:
1232 edit_fhl_cmd ();
1233 break;
1234 case CK_EditUserMenu:
1235 edit_mc_menu_cmd ();
1236 break;
1237 case CK_LinkSymbolicEdit:
1238 edit_symlink_cmd ();
1239 break;
1240 case CK_ExternalPanelize:
1241 external_panelize_cmd ();
1242 break;
1243 case CK_ViewFiltered:
1244 view_filtered_cmd (current_panel);
1245 break;
1246 case CK_Find:
1247 find_cmd (current_panel);
1248 break;
1249 #ifdef ENABLE_VFS_SHELL
1250 case CK_ConnectShell:
1251 shelllink_cmd ();
1252 break;
1253 #endif
1254 #ifdef ENABLE_VFS_FTP
1255 case CK_ConnectFtp:
1256 ftplink_cmd ();
1257 break;
1258 #endif
1259 #ifdef ENABLE_VFS_SFTP
1260 case CK_ConnectSftp:
1261 sftplink_cmd ();
1262 break;
1263 #endif
1264 case CK_Panelize:
1265 panel_panelize_cd ();
1266 break;
1267 case CK_Help:
1268 help_cmd ();
1269 break;
1270 case CK_History:
1271 /* show the history of command line widget */
1272 send_message (cmdline, NULL, MSG_ACTION, CK_History, NULL);
1273 break;
1274 case CK_PanelInfo:
1275 if (sender == WIDGET (the_menubar))
1276 info_cmd (); /* menu */
1277 else
1278 info_cmd_no_menu (); /* shortcut or buttonbar */
1279 break;
1280 #ifdef ENABLE_BACKGROUND
1281 case CK_Jobs:
1282 jobs_box ();
1283 break;
1284 #endif
1285 case CK_OptionsLayout:
1286 layout_box ();
1287 break;
1288 case CK_OptionsAppearance:
1289 appearance_box ();
1290 break;
1291 case CK_LearnKeys:
1292 learn_keys ();
1293 break;
1294 case CK_Link:
1295 link_cmd (LINK_HARDLINK);
1296 break;
1297 case CK_PanelListing:
1298 listing_cmd ();
1299 break;
1300 #ifdef LISTMODE_EDITOR
1301 case CK_ListMode:
1302 listmode_cmd ();
1303 break;
1304 #endif
1305 case CK_Menu:
1306 menu_cmd ();
1307 break;
1308 case CK_MenuLastSelected:
1309 menu_last_selected_cmd ();
1310 break;
1311 case CK_MakeDir:
1312 mkdir_cmd (current_panel);
1313 break;
1314 case CK_OptionsPanel:
1315 panel_options_box ();
1316 break;
1317 #ifdef HAVE_CHARSET
1318 case CK_SelectCodepage:
1319 encoding_cmd ();
1320 break;
1321 #endif
1322 case CK_CdQuick:
1323 quick_cd_cmd (current_panel);
1324 break;
1325 case CK_HotList:
1326 hotlist_cmd (current_panel);
1327 break;
1328 case CK_PanelQuickView:
1329 if (sender == WIDGET (the_menubar))
1330 quick_view_cmd (); /* menu */
1331 else
1332 quick_cmd_no_menu (); /* shortcut or buttonabr */
1333 break;
1334 case CK_QuitQuiet:
1335 quiet_quit_cmd ();
1336 break;
1337 case CK_Quit:
1338 quit_cmd ();
1339 break;
1340 case CK_LinkSymbolicRelative:
1341 link_cmd (LINK_SYMLINK_RELATIVE);
1342 break;
1343 case CK_Move:
1344 rename_cmd (current_panel);
1345 break;
1346 case CK_Reread:
1347 reread_cmd ();
1348 break;
1349 #ifdef ENABLE_VFS
1350 case CK_VfsList:
1351 vfs_list (current_panel);
1352 break;
1353 #endif
1354 case CK_SaveSetup:
1355 save_setup_cmd ();
1356 break;
1357 case CK_Select:
1358 case CK_Unselect:
1359 case CK_SelectInvert:
1360 case CK_Filter:
1361 res = send_message (current_panel, filemanager, MSG_ACTION, command, NULL);
1362 break;
1363 case CK_Shell:
1364 toggle_subshell ();
1365 break;
1366 case CK_DirSize:
1367 smart_dirsize_cmd (current_panel);
1368 break;
1369 case CK_Sort:
1370 sort_cmd ();
1371 break;
1372 case CK_ExtendedKeyMap:
1373 WIDGET (filemanager)->ext_mode = TRUE;
1374 break;
1375 case CK_Suspend:
1376 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
1377 break;
1378 case CK_Swap:
1379 swap_cmd ();
1380 break;
1381 case CK_LinkSymbolic:
1382 link_cmd (LINK_SYMLINK_ABSOLUTE);
1383 break;
1384 case CK_ShowHidden:
1385 toggle_show_hidden ();
1386 break;
1387 case CK_SplitVertHoriz:
1388 toggle_panels_split ();
1389 break;
1390 case CK_SplitEqual:
1391 panels_split_equal ();
1392 break;
1393 case CK_SplitMore:
1394 panels_split_more ();
1395 break;
1396 case CK_SplitLess:
1397 panels_split_less ();
1398 break;
1399 case CK_PanelTree:
1400 panel_tree_cmd ();
1401 break;
1402 case CK_Tree:
1403 treebox_cmd ();
1404 break;
1405 #ifdef ENABLE_VFS_UNDELFS
1406 case CK_Undelete:
1407 undelete_cmd ();
1408 break;
1409 #endif
1410 case CK_UserMenu:
1411 user_file_menu_cmd ();
1412 break;
1413 case CK_View:
1414 view_cmd (current_panel);
1415 break;
1416 case CK_ViewFile:
1417 view_file_cmd (current_panel);
1418 break;
1419 case CK_EditorViewerHistory:
1420 show_editor_viewer_history ();
1421 break;
1422 case CK_Cancel:
1423 /* don't close panels due to SIGINT */
1424 break;
1425 default:
1426 res = MSG_NOT_HANDLED;
1429 return res;
1432 /* --------------------------------------------------------------------------------------------- */
1435 * Whether the command-line should not respond to key events.
1437 * This is TRUE if a QuickView or TreeView have the focus, as they're going
1438 * to consume some keys and there's no sense in passing to the command-line
1439 * just the leftovers.
1441 static gboolean
1442 is_cmdline_mute (void)
1444 /* When one of panels is other than view_listing,
1445 current_panel points to view_listing panel all time independently of
1446 it's activity. Thus, we can't use get_current_type() here.
1447 current_panel should point to actually current active panel
1448 independently of it's type. */
1449 return (!current_panel->active
1450 && (get_other_type () == view_quick || get_other_type () == view_tree));
1453 /* --------------------------------------------------------------------------------------------- */
1456 * Handles the Enter key on the command-line.
1458 * Returns TRUE if non-whitespace was indeed processed.
1460 static gboolean
1461 handle_cmdline_enter (void)
1463 const char *s;
1465 for (s = input_get_ctext (cmdline); *s != '\0' && whitespace (*s); s++)
1468 if (*s != '\0')
1470 send_message (cmdline, NULL, MSG_KEY, '\n', NULL);
1471 return TRUE;
1474 input_insert (cmdline, "", FALSE);
1475 cmdline->point = 0;
1477 return FALSE;
1480 /* --------------------------------------------------------------------------------------------- */
1482 static cb_ret_t
1483 midnight_callback (Widget *w, Widget *sender, widget_msg_t msg, int parm, void *data)
1485 long command;
1487 switch (msg)
1489 case MSG_INIT:
1490 panel_init ();
1491 setup_panels ();
1492 return MSG_HANDLED;
1494 case MSG_DRAW:
1495 load_hint (TRUE);
1496 group_default_callback (w, NULL, MSG_DRAW, 0, NULL);
1497 /* We handle the special case of the output lines */
1498 if (mc_global.tty.console_flag != '\0' && output_lines != 0)
1500 unsigned char end_line;
1502 end_line = LINES - (mc_global.keybar_visible ? 1 : 0) - 1;
1503 show_console_contents (output_start_y, end_line - output_lines, end_line);
1505 return MSG_HANDLED;
1507 case MSG_RESIZE:
1508 widget_adjust_position (w->pos_flags, &w->rect);
1509 setup_panels ();
1510 menubar_arrange (the_menubar);
1511 return MSG_HANDLED;
1513 case MSG_IDLE:
1514 /* We only need the first idle event to show user menu after start */
1515 widget_idle (w, FALSE);
1517 if (boot_current_is_left)
1518 widget_select (get_panel_widget (0));
1519 else
1520 widget_select (get_panel_widget (1));
1522 if (auto_menu)
1523 midnight_execute_cmd (NULL, CK_UserMenu);
1524 return MSG_HANDLED;
1526 case MSG_KEY:
1527 if (w->ext_mode)
1529 command = widget_lookup_key (w, parm);
1530 if (command != CK_IgnoreKey)
1531 return midnight_execute_cmd (NULL, command);
1534 /* FIXME: should handle all menu shortcuts before this point */
1535 if (widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1536 return MSG_NOT_HANDLED;
1538 if (parm == '\n' && !is_cmdline_mute ())
1540 if (handle_cmdline_enter ())
1541 return MSG_HANDLED;
1542 /* Else: the panel will handle it. */
1545 if ((!mc_global.tty.alternate_plus_minus
1546 || !(mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)) && !quote
1547 && !current_panel->quick_search.active)
1549 if (!only_leading_plus_minus)
1551 /* Special treatment, since the input line will eat them */
1552 if (parm == '+')
1553 return send_message (current_panel, filemanager, MSG_ACTION, CK_Select, NULL);
1555 if (parm == '\\' || parm == '-')
1556 return send_message (current_panel, filemanager, MSG_ACTION, CK_Unselect, NULL);
1558 if (parm == '*')
1559 return send_message (current_panel, filemanager, MSG_ACTION, CK_SelectInvert,
1560 NULL);
1562 else if (!command_prompt || input_is_empty (cmdline))
1564 /* Special treatment '+', '-', '\', '*' only when this is
1565 * first char on input line
1567 if (parm == '+')
1568 return send_message (current_panel, filemanager, MSG_ACTION, CK_Select, NULL);
1570 if (parm == '\\' || parm == '-')
1571 return send_message (current_panel, filemanager, MSG_ACTION, CK_Unselect, NULL);
1573 if (parm == '*')
1574 return send_message (current_panel, filemanager, MSG_ACTION, CK_SelectInvert,
1575 NULL);
1578 return MSG_NOT_HANDLED;
1580 case MSG_HOTKEY_HANDLED:
1581 if ((get_current_type () == view_listing) && current_panel->quick_search.active)
1583 current_panel->dirty = TRUE; /* FIXME: unneeded? */
1584 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1586 return MSG_HANDLED;
1588 case MSG_UNHANDLED_KEY:
1590 cb_ret_t v = MSG_NOT_HANDLED;
1592 command = widget_lookup_key (w, parm);
1593 if (command != CK_IgnoreKey)
1594 v = midnight_execute_cmd (NULL, command);
1596 if (v == MSG_NOT_HANDLED && command_prompt && !is_cmdline_mute ())
1597 v = send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1599 return v;
1602 case MSG_POST_KEY:
1603 if (!widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1604 update_dirty_panels ();
1605 return MSG_HANDLED;
1607 case MSG_ACTION:
1608 /* Handle shortcuts, menu, and buttonbar. */
1609 return midnight_execute_cmd (sender, parm);
1611 case MSG_DESTROY:
1612 panel_deinit ();
1613 return MSG_HANDLED;
1615 default:
1616 return dlg_default_callback (w, sender, msg, parm, data);
1620 /* --------------------------------------------------------------------------------------------- */
1621 /*** public functions ****************************************************************************/
1622 /* --------------------------------------------------------------------------------------------- */
1624 void
1625 update_menu (void)
1627 menu_set_name (left_menu, panels_layout.horizontal_split ? _("&Above") : _("&Left"));
1628 menu_set_name (right_menu, panels_layout.horizontal_split ? _("&Below") : _("&Right"));
1629 menubar_arrange (the_menubar);
1630 widget_set_visibility (WIDGET (the_menubar), menubar_visible);
1633 /* --------------------------------------------------------------------------------------------- */
1635 void
1636 midnight_set_buttonbar (WButtonBar *b)
1638 Widget *w = WIDGET (filemanager);
1640 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), w->keymap, NULL);
1641 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), w->keymap, NULL);
1642 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), w->keymap, NULL);
1643 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), w->keymap, NULL);
1644 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), w->keymap, NULL);
1645 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), w->keymap, NULL);
1646 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), w->keymap, NULL);
1647 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), w->keymap, NULL);
1648 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), w->keymap, NULL);
1649 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), w->keymap, NULL);
1652 /* --------------------------------------------------------------------------------------------- */
1654 * Return a random hint. If force is TRUE, ignore the timeout.
1657 char *
1658 get_random_hint (gboolean force)
1660 static const gint64 update_period = 60 * G_USEC_PER_SEC;
1661 static gint64 tv = 0;
1663 char *data, *result, *eop;
1664 size_t len, start;
1665 GIConv conv;
1667 /* Do not change hints more often than one minute */
1668 if (!force && !mc_time_elapsed (&tv, update_period))
1669 return g_strdup ("");
1671 data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
1672 if (data == NULL)
1673 return NULL;
1675 /* get a random entry */
1676 srand ((unsigned int) (tv / G_USEC_PER_SEC));
1677 start = ((size_t) rand ()) % (len - 1);
1679 /* Search the start of paragraph */
1680 for (; start != 0; start--)
1681 if (data[start] == '\n' && data[start + 1] == '\n')
1683 start += 2;
1684 break;
1687 /* Search the end of paragraph */
1688 for (eop = data + start; *eop != '\0'; eop++)
1690 if (*eop == '\n' && *(eop + 1) == '\n')
1692 *eop = '\0';
1693 break;
1695 if (*eop == '\n')
1696 *eop = ' ';
1699 /* hint files are stored in utf-8 */
1700 /* try convert hint file from utf-8 to terminal encoding */
1701 conv = str_crt_conv_from ("UTF-8");
1702 if (conv == INVALID_CONV)
1703 result = g_strndup (data + start, len - start);
1704 else
1706 GString *buffer;
1707 gboolean nok;
1709 buffer = g_string_sized_new (len - start);
1710 nok = (str_convert (conv, data + start, buffer) == ESTR_FAILURE);
1711 result = g_string_free (buffer, nok);
1712 str_close_conv (conv);
1715 g_free (data);
1716 return result;
1720 /* --------------------------------------------------------------------------------------------- */
1722 * Load new hint and display it.
1723 * IF force is not 0, ignore the timeout.
1726 void
1727 load_hint (gboolean force)
1729 char *hint;
1731 if (WIDGET (the_hint)->owner == NULL)
1732 return;
1734 if (!mc_global.message_visible)
1736 label_set_text (the_hint, NULL);
1737 return;
1740 hint = get_random_hint (force);
1742 if (hint != NULL)
1744 if (*hint != '\0')
1745 set_hintbar (hint);
1746 g_free (hint);
1748 else
1750 char text[BUF_SMALL];
1752 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), mc_global.mc_version);
1753 set_hintbar (text);
1757 /* --------------------------------------------------------------------------------------------- */
1759 * Change current panel in the file manager.
1761 * @return current_panel
1764 WPanel *
1765 change_panel (void)
1767 input_complete_free (cmdline);
1768 group_select_next_widget (GROUP (filemanager));
1769 return current_panel;
1772 /* --------------------------------------------------------------------------------------------- */
1774 /** Save current stat of directories to avoid reloading the panels
1775 * when no modifications have taken place
1777 void
1778 save_cwds_stat (void)
1780 if (panels_options.fast_reload)
1782 mc_stat (current_panel->cwd_vpath, &(current_panel->dir_stat));
1783 if (get_other_type () == view_listing)
1784 mc_stat (other_panel->cwd_vpath, &(other_panel->dir_stat));
1788 /* --------------------------------------------------------------------------------------------- */
1790 gboolean
1791 quiet_quit_cmd (void)
1793 print_last_revert = TRUE;
1794 return quit_cmd_internal (1);
1797 /* --------------------------------------------------------------------------------------------- */
1799 /** Run the main dialog that occupies the whole screen */
1800 gboolean
1801 do_nc (void)
1803 gboolean ret;
1805 #ifdef USE_INTERNAL_EDIT
1806 edit_stack_init ();
1807 #endif
1809 filemanager = dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, dialog_colors,
1810 midnight_callback, NULL, "[main]", NULL);
1812 /* Check if we were invoked as an editor or file viewer */
1813 if (mc_global.mc_run_mode != MC_RUN_FULL)
1815 setup_dummy_mc ();
1816 ret = mc_maybe_editor_or_viewer ();
1818 else
1820 /* We only need the first idle event to show user menu after start */
1821 widget_idle (WIDGET (filemanager), TRUE);
1823 setup_mc ();
1824 mc_filehighlight = mc_fhl_new (TRUE);
1826 create_file_manager ();
1827 (void) dlg_run (filemanager);
1829 mc_fhl_free (&mc_filehighlight);
1831 ret = TRUE;
1833 /* widget_destroy destroys even current_panel->cwd_vpath, so we have to save a copy :) */
1834 if (mc_args__last_wd_file != NULL && vfs_current_is_local ())
1835 last_wd_string = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
1837 /* don't handle VFS timestamps for dirs opened in panels */
1838 mc_event_destroy (MCEVENT_GROUP_CORE, "vfs_timestamp");
1841 /* Program end */
1842 mc_global.midnight_shutdown = TRUE;
1843 dialog_switch_shutdown ();
1844 done_mc ();
1845 widget_destroy (WIDGET (filemanager));
1846 current_panel = NULL;
1848 #ifdef USE_INTERNAL_EDIT
1849 edit_stack_free ();
1850 #endif
1852 if ((quit & SUBSHELL_EXIT) == 0)
1853 tty_clear_screen ();
1855 return ret;
1858 /* --------------------------------------------------------------------------------------------- */