(vfs_stamp_path): get rid of path conversion.
[midnight-commander.git] / src / filemanager / midnight.c
blob93b063474d41a933b33393839dfbaae6a723f2f7
1 /*
2 Main dialog (file panels) of the Midnight Commander
4 Copyright (C) 1994-2018
5 Free Software Foundation, Inc.
7 Written by:
8 Miguel de Icaza, 1994, 1995, 1996, 1997
9 Janne Kukonlehto, 1994, 1995
10 Norbert Warmuth, 1997
11 Andrew Borodin <aborodin@vmail.ru>, 2009, 2010, 2012, 2013
12 Slava Zanko <slavazanko@gmail.com>, 2013
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 /** \file main.c
31 * \brief Source: main dialog (file panels) of the Midnight Commander
34 #include <config.h>
36 #include <ctype.h>
37 #include <errno.h>
38 #include <locale.h>
39 #include <stdio.h>
40 #include <stdlib.h>
41 #include <string.h>
42 #include <sys/types.h>
43 #include <sys/stat.h>
44 #include <sys/wait.h>
45 #include <pwd.h> /* for username in xterm title */
47 #include "lib/global.h"
48 #include "lib/fileloc.h" /* MC_HINT */
50 #include "lib/tty/tty.h"
51 #include "lib/tty/key.h" /* KEY_M_* masks */
52 #include "lib/skin.h"
53 #include "lib/util.h"
55 #include "lib/vfs/vfs.h"
57 #include "src/args.h"
58 #ifdef ENABLE_SUBSHELL
59 #include "src/subshell/subshell.h"
60 #endif
61 #include "src/setup.h" /* variables */
62 #include "src/learn.h" /* learn_keys() */
63 #include "src/keybind-defaults.h"
64 #include "lib/keybind.h"
65 #include "lib/event.h"
67 #include "tree.h"
68 #include "boxes.h" /* sort_box(), tree_box() */
69 #include "layout.h"
70 #include "cmd.h" /* commands */
71 #include "hotlist.h"
72 #include "panelize.h"
73 #include "command.h" /* cmdline */
74 #include "dir.h" /* dir_list_clean() */
76 #include "chmod.h"
77 #include "chown.h"
78 #include "achown.h"
80 #ifdef USE_INTERNAL_EDIT
81 #include "src/editor/edit.h"
82 #endif
84 #ifdef USE_DIFF_VIEW
85 #include "src/diffviewer/ydiff.h"
86 #endif
88 #include "src/consaver/cons.saver.h" /* show_console_contents */
90 #include "midnight.h"
92 /*** global variables ****************************************************************************/
94 /* When the modes are active, left_panel, right_panel and tree_panel */
95 /* point to a proper data structure. You should check with the functions */
96 /* get_current_type and get_other_type the types of the panels before using */
97 /* this pointer variables */
99 /* The structures for the panels */
100 WPanel *left_panel = NULL;
101 WPanel *right_panel = NULL;
102 /* Pointer to the selected and unselected panel */
103 WPanel *current_panel = NULL;
105 /* The Menubar */
106 WMenuBar *the_menubar = NULL;
107 /* The widget where we draw the prompt */
108 WLabel *the_prompt;
109 /* The hint bar */
110 WLabel *the_hint;
111 /* The button bar */
112 WButtonBar *the_bar;
114 /* The prompt */
115 const char *mc_prompt = NULL;
117 /*** file scope macro definitions ****************************************************************/
119 #ifdef HAVE_CHARSET
121 * Don't restrict the output on the screen manager level,
122 * the translation tables take care of it.
124 #endif /* !HAVE_CHARSET */
126 /*** file scope type declarations ****************************************************************/
128 /*** file scope variables ************************************************************************/
130 static menu_t *left_menu, *right_menu;
132 static gboolean ctl_x_map_enabled = FALSE;
134 /*** file scope functions ************************************************************************/
136 /** Stop MC main dialog and the current dialog if it exists.
137 * Needed to provide fast exit from MC viewer or editor on shell exit */
138 static void
139 stop_dialogs (void)
141 dlg_stop (midnight_dlg);
143 if ((top_dlg != NULL) && (top_dlg->data != NULL))
144 dlg_stop (DIALOG (top_dlg->data));
147 /* --------------------------------------------------------------------------------------------- */
149 static void
150 treebox_cmd (void)
152 char *sel_dir;
154 sel_dir = tree_box (selection (current_panel)->fname);
155 if (sel_dir != NULL)
157 vfs_path_t *sel_vdir;
159 sel_vdir = vfs_path_from_str (sel_dir);
160 do_cd (sel_vdir, cd_exact);
161 vfs_path_free (sel_vdir);
162 g_free (sel_dir);
166 /* --------------------------------------------------------------------------------------------- */
168 #ifdef LISTMODE_EDITOR
169 static void
170 listmode_cmd (void)
172 char *newmode;
174 if (get_current_type () != view_listing)
175 return;
177 newmode = listmode_edit (current_panel->user_format);
178 if (!newmode)
179 return;
181 g_free (current_panel->user_format);
182 current_panel->list_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_create (_("File listin&g"), CK_PanelListing));
198 entries = g_list_prepend (entries, menu_entry_create (_("&Quick view"), CK_PanelQuickView));
199 entries = g_list_prepend (entries, menu_entry_create (_("&Info"), CK_PanelInfo));
200 entries = g_list_prepend (entries, menu_entry_create (_("&Tree"), CK_PanelTree));
201 entries = g_list_prepend (entries, menu_separator_create ());
202 entries =
203 g_list_prepend (entries,
204 menu_entry_create (_("&Listing format..."), CK_SetupListingFormat));
205 entries = g_list_prepend (entries, menu_entry_create (_("&Sort order..."), CK_Sort));
206 entries = g_list_prepend (entries, menu_entry_create (_("&Filter..."), CK_Filter));
207 #ifdef HAVE_CHARSET
208 entries = g_list_prepend (entries, menu_entry_create (_("&Encoding..."), CK_SelectCodepage));
209 #endif
210 entries = g_list_prepend (entries, menu_separator_create ());
211 #ifdef ENABLE_VFS_FTP
212 entries = g_list_prepend (entries, menu_entry_create (_("FT&P link..."), CK_ConnectFtp));
213 #endif
214 #ifdef ENABLE_VFS_FISH
215 entries = g_list_prepend (entries, menu_entry_create (_("S&hell link..."), CK_ConnectFish));
216 #endif
217 #ifdef ENABLE_VFS_SFTP
218 entries = g_list_prepend (entries, menu_entry_create (_("S&FTP link..."), CK_ConnectSftp));
219 #endif
220 #ifdef ENABLE_VFS_SMB
221 entries = g_list_prepend (entries, menu_entry_create (_("SM&B link..."), CK_ConnectSmb));
222 #endif
223 entries = g_list_prepend (entries, menu_entry_create (_("Paneli&ze"), CK_Panelize));
224 entries = g_list_prepend (entries, menu_separator_create ());
225 entries = g_list_prepend (entries, menu_entry_create (_("&Rescan"), CK_Reread));
227 return g_list_reverse (entries);
230 /* --------------------------------------------------------------------------------------------- */
232 static GList *
233 create_file_menu (void)
235 GList *entries = NULL;
237 entries = g_list_prepend (entries, menu_entry_create (_("&View"), CK_View));
238 entries = g_list_prepend (entries, menu_entry_create (_("Vie&w file..."), CK_ViewFile));
239 entries = g_list_prepend (entries, menu_entry_create (_("&Filtered view"), CK_ViewFiltered));
240 entries = g_list_prepend (entries, menu_entry_create (_("&Edit"), CK_Edit));
241 entries = g_list_prepend (entries, menu_entry_create (_("&Copy"), CK_Copy));
242 entries = g_list_prepend (entries, menu_entry_create (_("C&hmod"), CK_ChangeMode));
243 entries = g_list_prepend (entries, menu_entry_create (_("&Link"), CK_Link));
244 entries = g_list_prepend (entries, menu_entry_create (_("&Symlink"), CK_LinkSymbolic));
245 entries =
246 g_list_prepend (entries,
247 menu_entry_create (_("Relative symlin&k"), CK_LinkSymbolicRelative));
248 entries = g_list_prepend (entries, menu_entry_create (_("Edit s&ymlink"), CK_LinkSymbolicEdit));
249 entries = g_list_prepend (entries, menu_entry_create (_("Ch&own"), CK_ChangeOwn));
250 entries =
251 g_list_prepend (entries, menu_entry_create (_("&Advanced chown"), CK_ChangeOwnAdvanced));
252 entries = g_list_prepend (entries, menu_entry_create (_("&Rename/Move"), CK_Move));
253 entries = g_list_prepend (entries, menu_entry_create (_("&Mkdir"), CK_MakeDir));
254 entries = g_list_prepend (entries, menu_entry_create (_("&Delete"), CK_Delete));
255 entries = g_list_prepend (entries, menu_entry_create (_("&Quick cd"), CK_CdQuick));
256 entries = g_list_prepend (entries, menu_separator_create ());
257 entries = g_list_prepend (entries, menu_entry_create (_("Select &group"), CK_Select));
258 entries = g_list_prepend (entries, menu_entry_create (_("U&nselect group"), CK_Unselect));
259 entries = g_list_prepend (entries, menu_entry_create (_("&Invert selection"), CK_SelectInvert));
260 entries = g_list_prepend (entries, menu_separator_create ());
261 entries = g_list_prepend (entries, menu_entry_create (_("E&xit"), CK_Quit));
263 return g_list_reverse (entries);
266 /* --------------------------------------------------------------------------------------------- */
268 static GList *
269 create_command_menu (void)
271 /* I know, I'm lazy, but the tree widget when it's not running
272 * as a panel still has some problems, I have not yet finished
273 * the WTree widget port, sorry.
275 GList *entries = NULL;
277 entries = g_list_prepend (entries, menu_entry_create (_("&User menu"), CK_UserMenu));
278 entries = g_list_prepend (entries, menu_entry_create (_("&Directory tree"), CK_Tree));
279 entries = g_list_prepend (entries, menu_entry_create (_("&Find file"), CK_Find));
280 entries = g_list_prepend (entries, menu_entry_create (_("S&wap panels"), CK_Swap));
281 entries = g_list_prepend (entries, menu_entry_create (_("Switch &panels on/off"), CK_Shell));
282 entries =
283 g_list_prepend (entries, menu_entry_create (_("&Compare directories"), CK_CompareDirs));
284 #ifdef USE_DIFF_VIEW
285 entries = g_list_prepend (entries, menu_entry_create (_("C&ompare files"), CK_CompareFiles));
286 #endif
287 entries =
288 g_list_prepend (entries, menu_entry_create (_("E&xternal panelize"), CK_ExternalPanelize));
289 entries = g_list_prepend (entries, menu_entry_create (_("Show directory s&izes"), CK_DirSize));
290 entries = g_list_prepend (entries, menu_separator_create ());
291 entries = g_list_prepend (entries, menu_entry_create (_("Command &history"), CK_History));
292 entries = g_list_prepend (entries, menu_entry_create (_("Di&rectory hotlist"), CK_HotList));
293 #ifdef ENABLE_VFS
294 entries = g_list_prepend (entries, menu_entry_create (_("&Active VFS list"), CK_VfsList));
295 #endif
296 #ifdef ENABLE_BACKGROUND
297 entries = g_list_prepend (entries, menu_entry_create (_("&Background jobs"), CK_Jobs));
298 #endif
299 entries = g_list_prepend (entries, menu_entry_create (_("Screen lis&t"), CK_ScreenList));
300 entries = g_list_prepend (entries, menu_separator_create ());
301 #ifdef ENABLE_VFS_UNDELFS
302 entries =
303 g_list_prepend (entries,
304 menu_entry_create (_("&Undelete files (ext2fs only)"), CK_Undelete));
305 #endif
306 #ifdef LISTMODE_EDITOR
307 entries = g_list_prepend (entries, menu_entry_create (_("&Listing format edit"), CK_ListMode));
308 #endif
309 #if defined (ENABLE_VFS_UNDELFS) || defined (LISTMODE_EDITOR)
310 entries = g_list_prepend (entries, menu_separator_create ());
311 #endif
312 entries =
313 g_list_prepend (entries,
314 menu_entry_create (_("Edit &extension file"), CK_EditExtensionsFile));
315 entries = g_list_prepend (entries, menu_entry_create (_("Edit &menu file"), CK_EditUserMenu));
316 entries =
317 g_list_prepend (entries,
318 menu_entry_create (_("Edit hi&ghlighting group file"),
319 CK_EditFileHighlightFile));
321 return g_list_reverse (entries);
324 /* --------------------------------------------------------------------------------------------- */
326 static GList *
327 create_options_menu (void)
329 GList *entries = NULL;
331 entries = g_list_prepend (entries, menu_entry_create (_("&Configuration..."), CK_Options));
332 entries = g_list_prepend (entries, menu_entry_create (_("&Layout..."), CK_OptionsLayout));
333 entries = g_list_prepend (entries, menu_entry_create (_("&Panel options..."), CK_OptionsPanel));
334 entries =
335 g_list_prepend (entries, menu_entry_create (_("C&onfirmation..."), CK_OptionsConfirm));
336 entries =
337 g_list_prepend (entries, menu_entry_create (_("&Appearance..."), CK_OptionsAppearance));
338 entries =
339 g_list_prepend (entries, menu_entry_create (_("&Display bits..."), CK_OptionsDisplayBits));
340 entries = g_list_prepend (entries, menu_entry_create (_("Learn &keys..."), CK_LearnKeys));
341 #ifdef ENABLE_VFS
342 entries = g_list_prepend (entries, menu_entry_create (_("&Virtual FS..."), CK_OptionsVfs));
343 #endif
344 entries = g_list_prepend (entries, menu_separator_create ());
345 entries = g_list_prepend (entries, menu_entry_create (_("&Save setup"), CK_SaveSetup));
347 return g_list_reverse (entries);
350 /* --------------------------------------------------------------------------------------------- */
352 static void
353 init_menu (void)
355 left_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
356 menubar_add_menu (the_menubar, left_menu);
357 menubar_add_menu (the_menubar, create_menu (_("&File"), create_file_menu (), "[File Menu]"));
358 menubar_add_menu (the_menubar,
359 create_menu (_("&Command"), create_command_menu (), "[Command Menu]"));
360 menubar_add_menu (the_menubar,
361 create_menu (_("&Options"), create_options_menu (), "[Options Menu]"));
362 right_menu = create_menu ("", create_panel_menu (), "[Left and Right Menus]");
363 menubar_add_menu (the_menubar, right_menu);
364 update_menu ();
367 /* --------------------------------------------------------------------------------------------- */
369 static void
370 menu_last_selected_cmd (void)
372 menubar_activate (the_menubar, drop_menus, -1);
375 /* --------------------------------------------------------------------------------------------- */
377 static void
378 menu_cmd (void)
380 int selected;
382 if ((get_current_index () == 0) == (current_panel->active != 0))
383 selected = 0;
384 else
385 selected = g_list_length (the_menubar->menu) - 1;
387 menubar_activate (the_menubar, drop_menus, selected);
390 /* --------------------------------------------------------------------------------------------- */
392 static void
393 sort_cmd (void)
395 WPanel *p;
396 const panel_field_t *sort_order;
398 if (!SELECTED_IS_PANEL)
399 return;
401 p = MENU_PANEL;
402 sort_order = sort_box (&p->sort_info, p->sort_field);
403 panel_set_sort_order (p, sort_order);
406 /* --------------------------------------------------------------------------------------------- */
408 static char *
409 midnight_get_shortcut (long command)
411 const char *ext_map;
412 const char *shortcut = NULL;
414 shortcut = keybind_lookup_keymap_shortcut (main_map, command);
415 if (shortcut != NULL)
416 return g_strdup (shortcut);
418 shortcut = keybind_lookup_keymap_shortcut (panel_map, command);
419 if (shortcut != NULL)
420 return g_strdup (shortcut);
422 ext_map = keybind_lookup_keymap_shortcut (main_map, CK_ExtendedKeyMap);
423 if (ext_map != NULL)
424 shortcut = keybind_lookup_keymap_shortcut (main_x_map, command);
425 if (shortcut != NULL)
426 return g_strdup_printf ("%s %s", ext_map, shortcut);
428 return NULL;
431 /* --------------------------------------------------------------------------------------------- */
433 static char *
434 midnight_get_title (const WDialog * h, size_t len)
436 char *path;
437 char *login;
438 char *p;
440 (void) h;
442 title_path_prepare (&path, &login);
444 p = g_strdup_printf ("%s [%s]:%s", _("Panels:"), login, path);
445 g_free (path);
446 g_free (login);
447 path = g_strdup (str_trunc (p, len - 4));
448 g_free (p);
450 return path;
453 /* --------------------------------------------------------------------------------------------- */
455 static void
456 toggle_panels_split (void)
458 panels_layout.horizontal_split = !panels_layout.horizontal_split;
459 layout_change ();
460 do_refresh ();
463 /* --------------------------------------------------------------------------------------------- */
465 #ifdef ENABLE_VFS
466 /* event helper */
467 static gboolean
468 check_panel_timestamp (const WPanel * panel, panel_view_mode_t mode, struct vfs_class *vclass,
469 vfsid id)
471 if (mode == view_listing)
473 const vfs_path_element_t *path_element;
475 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
477 if (path_element->class != vclass)
478 return FALSE;
480 if (vfs_getid (panel->cwd_vpath) != id)
481 return FALSE;
483 return TRUE;
486 /* --------------------------------------------------------------------------------------------- */
488 /* event callback */
489 static gboolean
490 check_current_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
491 gpointer init_data, gpointer data)
493 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
495 (void) event_group_name;
496 (void) event_name;
497 (void) init_data;
499 event_data->ret =
500 check_panel_timestamp (current_panel, get_current_type (), event_data->vclass,
501 event_data->id);
502 return !event_data->ret;
505 /* --------------------------------------------------------------------------------------------- */
507 /* event callback */
508 static gboolean
509 check_other_panel_timestamp (const gchar * event_group_name, const gchar * event_name,
510 gpointer init_data, gpointer data)
512 ev_vfs_stamp_create_t *event_data = (ev_vfs_stamp_create_t *) data;
514 (void) event_group_name;
515 (void) event_name;
516 (void) init_data;
518 event_data->ret =
519 check_panel_timestamp (other_panel, get_other_type (), event_data->vclass, event_data->id);
520 return !event_data->ret;
522 #endif /* ENABLE_VFS */
524 /* --------------------------------------------------------------------------------------------- */
526 /* event callback */
527 static gboolean
528 print_vfs_message (const gchar * event_group_name, const gchar * event_name,
529 gpointer init_data, gpointer data)
531 ev_vfs_print_message_t *event_data = (ev_vfs_print_message_t *) data;
533 (void) event_group_name;
534 (void) event_name;
535 (void) init_data;
537 if (mc_global.midnight_shutdown)
538 goto ret;
540 if (!mc_global.message_visible || the_hint == NULL || WIDGET (the_hint)->owner == NULL)
542 int col, row;
544 if (!nice_rotating_dash || (ok_to_refresh <= 0))
545 goto ret;
547 /* Preserve current cursor position */
548 tty_getyx (&row, &col);
550 tty_gotoyx (0, 0);
551 tty_setcolor (NORMAL_COLOR);
552 tty_print_string (str_fit_to_term (event_data->msg, COLS - 1, J_LEFT));
554 /* Restore cursor position */
555 tty_gotoyx (row, col);
556 mc_refresh ();
557 goto ret;
560 if (mc_global.message_visible)
561 set_hintbar (event_data->msg);
563 ret:
564 MC_PTR_FREE (event_data->msg);
565 return TRUE;
568 /* --------------------------------------------------------------------------------------------- */
570 static void
571 create_panels (void)
573 int current_index, other_index;
574 panel_view_mode_t current_mode, other_mode;
575 char *current_dir, *other_dir;
576 vfs_path_t *original_dir;
579 * Following cases from command line are possible:
580 * 'mc' (no arguments): mc_run_param0 == NULL, mc_run_param1 == NULL
581 * active panel uses current directory
582 * passive panel uses "other_dir" from panels.ini
584 * 'mc dir1 dir2' (two arguments): mc_run_param0 != NULL, mc_run_param1 != NULL
585 * active panel uses mc_run_param0
586 * passive panel uses mc_run_param1
588 * 'mc dir1' (single argument): mc_run_param0 != NULL, mc_run_param1 == NULL
589 * active panel uses mc_run_param0
590 * passive panel uses "other_dir" from panels.ini
593 /* Set up panel directories */
594 if (boot_current_is_left)
596 /* left panel is active */
597 current_index = 0;
598 other_index = 1;
599 current_mode = startup_left_mode;
600 other_mode = startup_right_mode;
602 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
604 /* no arguments */
605 current_dir = NULL; /* assume current dir */
606 other_dir = saved_other_dir; /* from ini */
608 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
610 /* two arguments */
611 current_dir = (char *) mc_run_param0;
612 other_dir = mc_run_param1;
614 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
616 /* one argument */
617 current_dir = (char *) mc_run_param0;
618 other_dir = saved_other_dir; /* from ini */
621 else
623 /* right panel is active */
624 current_index = 1;
625 other_index = 0;
626 current_mode = startup_right_mode;
627 other_mode = startup_left_mode;
629 if (mc_run_param0 == NULL && mc_run_param1 == NULL)
631 /* no arguments */
632 current_dir = NULL; /* assume current dir */
633 other_dir = saved_other_dir; /* from ini */
635 else if (mc_run_param0 != NULL && mc_run_param1 != NULL)
637 /* two arguments */
638 current_dir = (char *) mc_run_param0;
639 other_dir = mc_run_param1;
641 else /* mc_run_param0 != NULL && mc_run_param1 == NULL */
643 /* one argument */
644 current_dir = (char *) mc_run_param0;
645 other_dir = saved_other_dir; /* from ini */
649 /* 1. Get current dir */
650 original_dir = vfs_path_clone (vfs_get_raw_current_dir ());
652 /* 2. Create passive panel */
653 if (other_dir != NULL)
655 vfs_path_t *vpath;
657 if (g_path_is_absolute (other_dir))
658 vpath = vfs_path_from_str (other_dir);
659 else
660 vpath = vfs_path_append_new (original_dir, other_dir, (char *) NULL);
661 mc_chdir (vpath);
662 vfs_path_free (vpath);
664 set_display_type (other_index, other_mode);
666 /* 3. Create active panel */
667 if (current_dir == NULL)
668 mc_chdir (original_dir);
669 else
671 vfs_path_t *vpath;
673 if (g_path_is_absolute (current_dir))
674 vpath = vfs_path_from_str (current_dir);
675 else
676 vpath = vfs_path_append_new (original_dir, current_dir, (char *) NULL);
677 mc_chdir (vpath);
678 vfs_path_free (vpath);
680 set_display_type (current_index, current_mode);
682 if (startup_left_mode == view_listing)
683 current_panel = left_panel;
684 else if (right_panel != NULL)
685 current_panel = right_panel;
686 else
687 current_panel = left_panel;
689 vfs_path_free (original_dir);
691 #ifdef ENABLE_VFS
692 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_other_panel_timestamp, NULL, NULL);
693 mc_event_add (MCEVENT_GROUP_CORE, "vfs_timestamp", check_current_panel_timestamp, NULL, NULL);
694 #endif /* ENABLE_VFS */
696 mc_event_add (MCEVENT_GROUP_CORE, "vfs_print_message", print_vfs_message, NULL, NULL);
699 /* --------------------------------------------------------------------------------------------- */
701 static void
702 midnight_put_panel_path (WPanel * panel)
704 vfs_path_t *cwd_vpath;
705 const char *cwd_vpath_str;
707 if (!command_prompt)
708 return;
710 #ifdef HAVE_CHARSET
711 cwd_vpath = remove_encoding_from_path (panel->cwd_vpath);
712 #else
713 cwd_vpath = vfs_path_clone (panel->cwd_vpath);
714 #endif
716 cwd_vpath_str = vfs_path_as_str (cwd_vpath);
718 command_insert (cmdline, cwd_vpath_str, FALSE);
720 if (!IS_PATH_SEP (cwd_vpath_str[strlen (cwd_vpath_str) - 1]))
721 command_insert (cmdline, PATH_SEP_STR, FALSE);
723 vfs_path_free (cwd_vpath);
726 /* --------------------------------------------------------------------------------------------- */
728 static void
729 put_link (WPanel * panel)
731 if (!command_prompt)
732 return;
733 if (S_ISLNK (selection (panel)->st.st_mode))
735 char buffer[MC_MAXPATHLEN];
736 vfs_path_t *vpath;
737 int i;
739 vpath = vfs_path_append_new (panel->cwd_vpath, selection (panel)->fname, (char *) NULL);
740 i = mc_readlink (vpath, buffer, sizeof (buffer) - 1);
741 vfs_path_free (vpath);
743 if (i > 0)
745 buffer[i] = '\0';
746 command_insert (cmdline, buffer, TRUE);
751 /* --------------------------------------------------------------------------------------------- */
753 static void
754 put_current_link (void)
756 put_link (current_panel);
759 /* --------------------------------------------------------------------------------------------- */
761 static void
762 put_other_link (void)
764 if (get_other_type () == view_listing)
765 put_link (other_panel);
768 /* --------------------------------------------------------------------------------------------- */
770 /** Insert the selected file name into the input line */
771 static void
772 put_current_selected (void)
774 const char *tmp;
776 if (!command_prompt)
777 return;
779 if (get_current_type () == view_tree)
781 WTree *tree;
782 const vfs_path_t *selected_name;
784 tree = (WTree *) get_panel_widget (get_current_index ());
785 selected_name = tree_selected_name (tree);
786 tmp = vfs_path_as_str (selected_name);
788 else
789 tmp = selection (current_panel)->fname;
791 command_insert (cmdline, tmp, TRUE);
794 /* --------------------------------------------------------------------------------------------- */
796 static void
797 put_tagged (WPanel * panel)
799 if (!command_prompt)
800 return;
801 input_disable_update (cmdline);
802 if (panel->marked)
804 int i;
806 for (i = 0; i < panel->dir.len; i++)
808 if (panel->dir.list[i].f.marked)
809 command_insert (cmdline, panel->dir.list[i].fname, TRUE);
812 else
814 command_insert (cmdline, panel->dir.list[panel->selected].fname, TRUE);
816 input_enable_update (cmdline);
819 /* --------------------------------------------------------------------------------------------- */
821 static void
822 put_current_tagged (void)
824 put_tagged (current_panel);
827 /* --------------------------------------------------------------------------------------------- */
829 static void
830 put_other_tagged (void)
832 if (get_other_type () == view_listing)
833 put_tagged (other_panel);
836 /* --------------------------------------------------------------------------------------------- */
838 static void
839 ctl_x_cmd (void)
841 ctl_x_map_enabled = TRUE;
844 /* --------------------------------------------------------------------------------------------- */
846 static void
847 setup_mc (void)
849 #ifdef HAVE_SLANG
850 #ifdef HAVE_CHARSET
851 tty_display_8bit (TRUE);
852 #else
853 tty_display_8bit (mc_global.full_eight_bits);
854 #endif /* HAVE_CHARSET */
856 #else /* HAVE_SLANG */
858 #ifdef HAVE_CHARSET
859 tty_display_8bit (TRUE);
860 #else
861 tty_display_8bit (mc_global.eight_bit_clean);
862 #endif /* HAVE_CHARSET */
863 #endif /* HAVE_SLANG */
865 #ifdef ENABLE_SUBSHELL
866 if (mc_global.tty.use_subshell)
867 add_select_channel (mc_global.tty.subshell_pty, load_prompt, 0);
868 #endif /* !ENABLE_SUBSHELL */
870 if ((tty_baudrate () < 9600) || mc_global.tty.slow_terminal)
871 verbose = FALSE;
874 /* --------------------------------------------------------------------------------------------- */
876 static void
877 setup_dummy_mc (void)
879 vfs_path_t *vpath;
880 char *d;
881 int ret;
883 d = _vfs_get_cwd ();
884 setup_mc ();
885 vpath = vfs_path_from_str (d);
886 ret = mc_chdir (vpath);
887 (void) ret;
888 vfs_path_free (vpath);
889 g_free (d);
892 /* --------------------------------------------------------------------------------------------- */
894 static void
895 done_mc (void)
897 /* Setup shutdown
899 * We sync the profiles since the hotlist may have changed, while
900 * we only change the setup data if we have the auto save feature set
903 save_setup (auto_save_setup, panels_options.auto_save_setup);
905 vfs_stamp_path (vfs_get_raw_current_dir ());
907 if ((current_panel != NULL) && (get_current_type () == view_listing))
908 vfs_stamp_path (current_panel->cwd_vpath);
910 if ((other_panel != NULL) && (get_other_type () == view_listing))
911 vfs_stamp_path (other_panel->cwd_vpath);
914 /* --------------------------------------------------------------------------------------------- */
916 static void
917 create_file_manager (void)
919 midnight_dlg->get_shortcut = midnight_get_shortcut;
920 midnight_dlg->get_title = midnight_get_title;
921 /* allow rebind tab */
922 widget_want_tab (WIDGET (midnight_dlg), TRUE);
924 the_menubar = menubar_new (0, 0, COLS, NULL, menubar_visible);
925 add_widget (midnight_dlg, the_menubar);
926 init_menu ();
928 create_panels ();
929 add_widget (midnight_dlg, get_panel_widget (0));
930 add_widget (midnight_dlg, get_panel_widget (1));
932 the_hint = label_new (0, 0, 0);
933 the_hint->transparent = 1;
934 the_hint->auto_adjust_cols = 0;
935 WIDGET (the_hint)->cols = COLS;
936 add_widget (midnight_dlg, the_hint);
938 cmdline = command_new (0, 0, 0);
939 add_widget (midnight_dlg, cmdline);
941 the_prompt = label_new (0, 0, mc_prompt);
942 the_prompt->transparent = TRUE;
943 add_widget (midnight_dlg, the_prompt);
945 the_bar = buttonbar_new (mc_global.keybar_visible);
946 add_widget (midnight_dlg, the_bar);
947 midnight_set_buttonbar (the_bar);
950 /* --------------------------------------------------------------------------------------------- */
952 /** result must be free'd (I think this should go in util.c) */
953 static vfs_path_t *
954 prepend_cwd_on_local (const char *filename)
956 vfs_path_t *vpath;
958 vpath = vfs_path_from_str (filename);
959 if (!vfs_file_is_local (vpath) || g_path_is_absolute (filename))
960 return vpath;
962 vfs_path_free (vpath);
964 return vfs_path_append_new (vfs_get_raw_current_dir (), filename, (char *) NULL);
967 /* --------------------------------------------------------------------------------------------- */
969 /** Invoke the internal view/edit routine with:
970 * the default processing and forcing the internal viewer/editor
972 static gboolean
973 mc_maybe_editor_or_viewer (void)
975 gboolean ret;
977 switch (mc_global.mc_run_mode)
979 #ifdef USE_INTERNAL_EDIT
980 case MC_RUN_EDITOR:
981 ret = edit_files ((GList *) mc_run_param0);
982 break;
983 #endif /* USE_INTERNAL_EDIT */
984 case MC_RUN_VIEWER:
986 vfs_path_t *vpath = NULL;
988 if (mc_run_param0 != NULL && *(char *) mc_run_param0 != '\0')
989 vpath = prepend_cwd_on_local ((char *) mc_run_param0);
991 ret = view_file (vpath, FALSE, TRUE);
992 vfs_path_free (vpath);
993 break;
995 #ifdef USE_DIFF_VIEW
996 case MC_RUN_DIFFVIEWER:
997 ret = dview_diff_cmd (mc_run_param0, mc_run_param1);
998 break;
999 #endif /* USE_DIFF_VIEW */
1000 default:
1001 ret = FALSE;
1004 return ret;
1007 /* --------------------------------------------------------------------------------------------- */
1009 static gboolean
1010 quit_cmd_internal (int quiet)
1012 int q = quit;
1013 size_t n;
1015 n = dialog_switch_num () - 1;
1016 if (n != 0)
1018 char msg[BUF_MEDIUM];
1020 g_snprintf (msg, sizeof (msg),
1021 ngettext ("You have %zu opened screen. Quit anyway?",
1022 "You have %zu opened screens. Quit anyway?", n), n);
1024 if (query_dialog (_("The Midnight Commander"), msg, D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
1025 return FALSE;
1026 q = 1;
1028 else if (quiet || !confirm_exit)
1029 q = 1;
1030 else if (query_dialog (_("The Midnight Commander"),
1031 _("Do you really want to quit the Midnight Commander?"),
1032 D_NORMAL, 2, _("&Yes"), _("&No")) == 0)
1033 q = 1;
1035 if (q != 0)
1037 #ifdef ENABLE_SUBSHELL
1038 if (!mc_global.tty.use_subshell)
1039 stop_dialogs ();
1040 else if ((q = exit_subshell ()? 1 : 0) != 0)
1041 #endif
1042 stop_dialogs ();
1045 if (q != 0)
1046 quit |= 1;
1047 return (quit != 0);
1050 /* --------------------------------------------------------------------------------------------- */
1052 static gboolean
1053 quit_cmd (void)
1055 return quit_cmd_internal (0);
1058 /* --------------------------------------------------------------------------------------------- */
1061 * Repaint the contents of the panels without frames. To schedule panel
1062 * for repainting, set panel->dirty to 1. There are many reasons why
1063 * the panels need to be repainted, and this is a costly operation, so
1064 * it's done once per event.
1067 static void
1068 update_dirty_panels (void)
1070 if (get_current_type () == view_listing && current_panel->dirty)
1071 widget_redraw (WIDGET (current_panel));
1073 if (get_other_type () == view_listing && other_panel->dirty)
1074 widget_redraw (WIDGET (other_panel));
1077 /* --------------------------------------------------------------------------------------------- */
1079 static void
1080 toggle_show_hidden (void)
1082 panels_options.show_dot_files = !panels_options.show_dot_files;
1083 update_panels (UP_RELOAD, UP_KEEPSEL);
1084 /* redraw panels forced */
1085 update_dirty_panels ();
1088 /* --------------------------------------------------------------------------------------------- */
1090 static cb_ret_t
1091 midnight_execute_cmd (Widget * sender, long command)
1093 cb_ret_t res = MSG_HANDLED;
1095 (void) sender;
1097 /* stop quick search before executing any command */
1098 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1100 switch (command)
1102 case CK_ChangePanel:
1103 change_panel ();
1104 break;
1105 case CK_HotListAdd:
1106 add2hotlist_cmd ();
1107 break;
1108 case CK_SetupListingFormat:
1109 setup_listing_format_cmd ();
1110 break;
1111 case CK_ChangeMode:
1112 chmod_cmd ();
1113 break;
1114 case CK_ChangeOwn:
1115 chown_cmd ();
1116 break;
1117 case CK_ChangeOwnAdvanced:
1118 advanced_chown_cmd ();
1119 break;
1120 case CK_CompareDirs:
1121 compare_dirs_cmd ();
1122 break;
1123 case CK_Options:
1124 configure_box ();
1125 break;
1126 #ifdef ENABLE_VFS
1127 case CK_OptionsVfs:
1128 configure_vfs ();
1129 break;
1130 #endif
1131 case CK_OptionsConfirm:
1132 confirm_box ();
1133 break;
1134 case CK_Copy:
1135 copy_cmd ();
1136 break;
1137 case CK_PutCurrentPath:
1138 midnight_put_panel_path (current_panel);
1139 break;
1140 case CK_PutCurrentSelected:
1141 put_current_selected ();
1142 break;
1143 case CK_PutCurrentFullSelected:
1144 midnight_put_panel_path (current_panel);
1145 put_current_selected ();
1146 break;
1147 case CK_PutCurrentLink:
1148 put_current_link ();
1149 break;
1150 case CK_PutCurrentTagged:
1151 put_current_tagged ();
1152 break;
1153 case CK_PutOtherPath:
1154 if (get_other_type () == view_listing)
1155 midnight_put_panel_path (other_panel);
1156 break;
1157 case CK_PutOtherLink:
1158 put_other_link ();
1159 break;
1160 case CK_PutOtherTagged:
1161 put_other_tagged ();
1162 break;
1163 case CK_Delete:
1164 delete_cmd ();
1165 break;
1166 case CK_ScreenList:
1167 dialog_switch_list ();
1168 break;
1169 #ifdef USE_DIFF_VIEW
1170 case CK_CompareFiles:
1171 diff_view_cmd ();
1172 break;
1173 #endif
1174 case CK_OptionsDisplayBits:
1175 display_bits_box ();
1176 break;
1177 case CK_Edit:
1178 edit_cmd ();
1179 break;
1180 #ifdef USE_INTERNAL_EDIT
1181 case CK_EditForceInternal:
1182 edit_cmd_force_internal ();
1183 break;
1184 #endif
1185 case CK_EditExtensionsFile:
1186 ext_cmd ();
1187 break;
1188 case CK_EditFileHighlightFile:
1189 edit_fhl_cmd ();
1190 break;
1191 case CK_EditUserMenu:
1192 edit_mc_menu_cmd ();
1193 break;
1194 case CK_LinkSymbolicEdit:
1195 edit_symlink_cmd ();
1196 break;
1197 case CK_ExternalPanelize:
1198 external_panelize ();
1199 break;
1200 case CK_Filter:
1201 filter_cmd ();
1202 break;
1203 case CK_ViewFiltered:
1204 view_filtered_cmd ();
1205 break;
1206 case CK_Find:
1207 find_cmd ();
1208 break;
1209 #ifdef ENABLE_VFS_FISH
1210 case CK_ConnectFish:
1211 fishlink_cmd ();
1212 break;
1213 #endif
1214 #ifdef ENABLE_VFS_FTP
1215 case CK_ConnectFtp:
1216 ftplink_cmd ();
1217 break;
1218 #endif
1219 #ifdef ENABLE_VFS_SFTP
1220 case CK_ConnectSftp:
1221 sftplink_cmd ();
1222 break;
1223 #endif
1224 #ifdef ENABLE_VFS_SMB
1225 case CK_ConnectSmb:
1226 smblink_cmd ();
1227 break;
1228 #endif /* ENABLE_VFS_SMB */
1229 case CK_Panelize:
1230 cd_panelize_cmd ();
1231 break;
1232 case CK_Help:
1233 help_cmd ();
1234 break;
1235 case CK_History:
1236 /* show the history of command line widget */
1237 send_message (cmdline, NULL, MSG_ACTION, CK_History, NULL);
1238 break;
1239 case CK_PanelInfo:
1240 if (sender == WIDGET (the_menubar))
1241 info_cmd (); /* menu */
1242 else
1243 info_cmd_no_menu (); /* shortcut or buttonbar */
1244 break;
1245 #ifdef ENABLE_BACKGROUND
1246 case CK_Jobs:
1247 jobs_cmd ();
1248 break;
1249 #endif
1250 case CK_OptionsLayout:
1251 layout_box ();
1252 break;
1253 case CK_OptionsAppearance:
1254 appearance_box ();
1255 break;
1256 case CK_LearnKeys:
1257 learn_keys ();
1258 break;
1259 case CK_Link:
1260 link_cmd (LINK_HARDLINK);
1261 break;
1262 case CK_PanelListing:
1263 listing_cmd ();
1264 break;
1265 #ifdef LISTMODE_EDITOR
1266 case CK_ListMode:
1267 listmode_cmd ();
1268 break;
1269 #endif
1270 case CK_Menu:
1271 menu_cmd ();
1272 break;
1273 case CK_MenuLastSelected:
1274 menu_last_selected_cmd ();
1275 break;
1276 case CK_MakeDir:
1277 mkdir_cmd ();
1278 break;
1279 case CK_OptionsPanel:
1280 panel_options_box ();
1281 break;
1282 #ifdef HAVE_CHARSET
1283 case CK_SelectCodepage:
1284 encoding_cmd ();
1285 break;
1286 #endif
1287 case CK_CdQuick:
1288 quick_cd_cmd ();
1289 break;
1290 case CK_HotList:
1291 hotlist_cmd ();
1292 break;
1293 case CK_PanelQuickView:
1294 if (sender == WIDGET (the_menubar))
1295 quick_view_cmd (); /* menu */
1296 else
1297 quick_cmd_no_menu (); /* shortcut or buttonabr */
1298 break;
1299 case CK_QuitQuiet:
1300 quiet_quit_cmd ();
1301 break;
1302 case CK_Quit:
1303 quit_cmd ();
1304 break;
1305 case CK_LinkSymbolicRelative:
1306 link_cmd (LINK_SYMLINK_RELATIVE);
1307 break;
1308 case CK_Move:
1309 rename_cmd ();
1310 break;
1311 case CK_Reread:
1312 reread_cmd ();
1313 break;
1314 #ifdef ENABLE_VFS
1315 case CK_VfsList:
1316 vfs_list ();
1317 break;
1318 #endif
1319 case CK_SaveSetup:
1320 save_setup_cmd ();
1321 break;
1322 case CK_Select:
1323 case CK_Unselect:
1324 case CK_SelectInvert:
1325 res = send_message (current_panel, midnight_dlg, MSG_ACTION, command, NULL);
1326 break;
1327 case CK_Shell:
1328 view_other_cmd ();
1329 break;
1330 case CK_DirSize:
1331 smart_dirsize_cmd ();
1332 break;
1333 case CK_Sort:
1334 sort_cmd ();
1335 break;
1336 case CK_ExtendedKeyMap:
1337 ctl_x_cmd ();
1338 break;
1339 case CK_Suspend:
1340 mc_event_raise (MCEVENT_GROUP_CORE, "suspend", NULL);
1341 break;
1342 case CK_Swap:
1343 swap_cmd ();
1344 break;
1345 case CK_LinkSymbolic:
1346 link_cmd (LINK_SYMLINK_ABSOLUTE);
1347 break;
1348 case CK_ShowHidden:
1349 toggle_show_hidden ();
1350 break;
1351 case CK_SplitVertHoriz:
1352 toggle_panels_split ();
1353 break;
1354 case CK_SplitEqual:
1355 panels_split_equal ();
1356 break;
1357 case CK_SplitMore:
1358 panels_split_more ();
1359 break;
1360 case CK_SplitLess:
1361 panels_split_less ();
1362 break;
1363 case CK_PanelTree:
1364 panel_tree_cmd ();
1365 break;
1366 case CK_Tree:
1367 treebox_cmd ();
1368 break;
1369 #ifdef ENABLE_VFS_UNDELFS
1370 case CK_Undelete:
1371 undelete_cmd ();
1372 break;
1373 #endif
1374 case CK_UserMenu:
1375 user_file_menu_cmd ();
1376 break;
1377 case CK_View:
1378 view_cmd ();
1379 break;
1380 case CK_ViewFile:
1381 view_file_cmd ();
1382 break;
1383 case CK_Cancel:
1384 /* don't close panels due to SIGINT */
1385 break;
1386 default:
1387 res = MSG_NOT_HANDLED;
1390 return res;
1393 /* --------------------------------------------------------------------------------------------- */
1396 * Whether the command-line should not respond to key events.
1398 * This is TRUE if a QuickView or TreeView have the focus, as they're going
1399 * to consume some keys and there's no sense in passing to the command-line
1400 * just the leftovers.
1402 static gboolean
1403 is_cmdline_mute (void)
1405 /* When one of panels is other than view_listing,
1406 current_panel points to view_listing panel all time independently of
1407 it's activity. Thus, we can't use get_current_type() here.
1408 current_panel should point to actualy current active panel
1409 independently of it's type. */
1410 return (current_panel->active == 0
1411 && (get_other_type () == view_quick || get_other_type () == view_tree));
1414 /* --------------------------------------------------------------------------------------------- */
1417 * Handles the Enter key on the command-line.
1419 * Returns TRUE if non-whitespace was indeed processed.
1421 static gboolean
1422 handle_cmdline_enter (void)
1424 size_t i;
1426 for (i = 0; cmdline->buffer[i] != '\0' && whitespace (cmdline->buffer[i]); i++)
1429 if (cmdline->buffer[i] != '\0')
1431 send_message (cmdline, NULL, MSG_KEY, '\n', NULL);
1432 return TRUE;
1435 input_insert (cmdline, "", FALSE);
1436 cmdline->point = 0;
1438 return FALSE;
1441 /* --------------------------------------------------------------------------------------------- */
1443 static cb_ret_t
1444 midnight_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
1446 long command;
1448 switch (msg)
1450 case MSG_INIT:
1451 panel_init ();
1452 setup_panels ();
1453 return MSG_HANDLED;
1455 case MSG_DRAW:
1456 load_hint (TRUE);
1457 /* We handle the special case of the output lines */
1458 if (mc_global.tty.console_flag != '\0' && output_lines)
1459 show_console_contents (output_start_y,
1460 LINES - output_lines - mc_global.keybar_visible -
1461 1, LINES - mc_global.keybar_visible - 1);
1462 return MSG_HANDLED;
1464 case MSG_RESIZE:
1465 /* dlg_set_size() is surplus for this case */
1466 w->lines = LINES;
1467 w->cols = COLS;
1468 setup_panels ();
1469 menubar_arrange (the_menubar);
1470 return MSG_HANDLED;
1472 case MSG_IDLE:
1473 /* We only need the first idle event to show user menu after start */
1474 widget_idle (w, FALSE);
1476 if (boot_current_is_left)
1477 widget_select (get_panel_widget (0));
1478 else
1479 widget_select (get_panel_widget (1));
1481 if (auto_menu)
1482 midnight_execute_cmd (NULL, CK_UserMenu);
1483 return MSG_HANDLED;
1485 case MSG_KEY:
1486 if (ctl_x_map_enabled)
1488 ctl_x_map_enabled = FALSE;
1489 command = keybind_lookup_keymap_command (main_x_map, parm);
1490 if (command != CK_IgnoreKey)
1491 return midnight_execute_cmd (NULL, command);
1494 /* FIXME: should handle all menu shortcuts before this point */
1495 if (widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1496 return MSG_NOT_HANDLED;
1498 if (parm == '\n' && !is_cmdline_mute ())
1500 if (handle_cmdline_enter ())
1501 return MSG_HANDLED;
1502 /* Else: the panel will handle it. */
1505 if ((!mc_global.tty.alternate_plus_minus
1506 || !(mc_global.tty.console_flag != '\0' || mc_global.tty.xterm_flag)) && !quote
1507 && !current_panel->searching)
1509 if (!only_leading_plus_minus)
1511 /* Special treatement, since the input line will eat them */
1512 if (parm == '+')
1513 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1515 if (parm == '\\' || parm == '-')
1516 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1517 NULL);
1519 if (parm == '*')
1520 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1521 NULL);
1523 else if (!command_prompt || cmdline->buffer[0] == '\0')
1525 /* Special treatement '+', '-', '\', '*' only when this is
1526 * first char on input line
1528 if (parm == '+')
1529 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Select, NULL);
1531 if (parm == '\\' || parm == '-')
1532 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_Unselect,
1533 NULL);
1535 if (parm == '*')
1536 return send_message (current_panel, midnight_dlg, MSG_ACTION, CK_SelectInvert,
1537 NULL);
1540 return MSG_NOT_HANDLED;
1542 case MSG_HOTKEY_HANDLED:
1543 if ((get_current_type () == view_listing) && current_panel->searching)
1545 current_panel->dirty = 1; /* FIXME: unneeded? */
1546 send_message (current_panel, NULL, MSG_ACTION, CK_SearchStop, NULL);
1548 return MSG_HANDLED;
1550 case MSG_UNHANDLED_KEY:
1552 cb_ret_t v = MSG_NOT_HANDLED;
1554 if (ctl_x_map_enabled)
1556 ctl_x_map_enabled = FALSE;
1557 command = keybind_lookup_keymap_command (main_x_map, parm);
1559 else
1560 command = keybind_lookup_keymap_command (main_map, parm);
1562 if (command != CK_IgnoreKey)
1563 v = midnight_execute_cmd (NULL, command);
1565 if (v == MSG_NOT_HANDLED && command_prompt && !is_cmdline_mute ())
1566 v = send_message (cmdline, NULL, MSG_KEY, parm, NULL);
1568 return v;
1571 case MSG_POST_KEY:
1572 if (!widget_get_state (WIDGET (the_menubar), WST_FOCUSED))
1573 update_dirty_panels ();
1574 return MSG_HANDLED;
1576 case MSG_ACTION:
1577 /* Handle shortcuts, menu, and buttonbar. */
1578 return midnight_execute_cmd (sender, parm);
1580 case MSG_END:
1581 panel_deinit ();
1582 return MSG_HANDLED;
1584 default:
1585 return dlg_default_callback (w, sender, msg, parm, data);
1589 /* --------------------------------------------------------------------------------------------- */
1590 /*** public functions ****************************************************************************/
1591 /* --------------------------------------------------------------------------------------------- */
1593 void
1594 update_menu (void)
1596 menu_set_name (left_menu, panels_layout.horizontal_split ? _("&Above") : _("&Left"));
1597 menu_set_name (right_menu, panels_layout.horizontal_split ? _("&Below") : _("&Right"));
1598 menubar_arrange (the_menubar);
1599 menubar_set_visible (the_menubar, menubar_visible);
1602 /* --------------------------------------------------------------------------------------------- */
1604 void
1605 midnight_set_buttonbar (WButtonBar * b)
1607 buttonbar_set_label (b, 1, Q_ ("ButtonBar|Help"), main_map, NULL);
1608 buttonbar_set_label (b, 2, Q_ ("ButtonBar|Menu"), main_map, NULL);
1609 buttonbar_set_label (b, 3, Q_ ("ButtonBar|View"), main_map, NULL);
1610 buttonbar_set_label (b, 4, Q_ ("ButtonBar|Edit"), main_map, NULL);
1611 buttonbar_set_label (b, 5, Q_ ("ButtonBar|Copy"), main_map, NULL);
1612 buttonbar_set_label (b, 6, Q_ ("ButtonBar|RenMov"), main_map, NULL);
1613 buttonbar_set_label (b, 7, Q_ ("ButtonBar|Mkdir"), main_map, NULL);
1614 buttonbar_set_label (b, 8, Q_ ("ButtonBar|Delete"), main_map, NULL);
1615 buttonbar_set_label (b, 9, Q_ ("ButtonBar|PullDn"), main_map, NULL);
1616 buttonbar_set_label (b, 10, Q_ ("ButtonBar|Quit"), main_map, NULL);
1619 /* --------------------------------------------------------------------------------------------- */
1621 * Return a random hint. If force is TRUE, ignore the timeout.
1624 char *
1625 get_random_hint (gboolean force)
1627 static const guint64 update_period = 60 * G_USEC_PER_SEC;
1628 static guint64 tv = 0;
1630 char *data, *result = NULL, *eop;
1631 size_t len, start;
1632 GIConv conv;
1634 /* Do not change hints more often than one minute */
1635 if (!force && !mc_time_elapsed (&tv, update_period))
1636 return g_strdup ("");
1638 data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL, &len);
1639 if (data == NULL)
1640 return NULL;
1642 /* get a random entry */
1643 srand ((unsigned int) (tv / G_USEC_PER_SEC));
1644 start = ((size_t) rand ()) % (len - 1);
1646 /* Search the start of paragraph */
1647 for (; start != 0; start--)
1648 if (data[start] == '\n' && data[start + 1] == '\n')
1650 start += 2;
1651 break;
1654 /* Search the end of paragraph */
1655 for (eop = data + start; *eop != '\0'; eop++)
1657 if (*eop == '\n' && *(eop + 1) == '\n')
1659 *eop = '\0';
1660 break;
1662 if (*eop == '\n')
1663 *eop = ' ';
1666 /* hint files are stored in utf-8 */
1667 /* try convert hint file from utf-8 to terminal encoding */
1668 conv = str_crt_conv_from ("UTF-8");
1669 if (conv != INVALID_CONV)
1671 GString *buffer;
1673 buffer = g_string_sized_new (len - start);
1674 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1675 result = g_string_free (buffer, FALSE);
1676 else
1677 g_string_free (buffer, TRUE);
1678 str_close_conv (conv);
1680 else
1681 result = g_strndup (data + start, len - start);
1683 g_free (data);
1684 return result;
1688 /* --------------------------------------------------------------------------------------------- */
1690 * Load new hint and display it.
1691 * IF force is not 0, ignore the timeout.
1694 void
1695 load_hint (gboolean force)
1697 char *hint;
1699 if (WIDGET (the_hint)->owner == NULL)
1700 return;
1702 if (!mc_global.message_visible)
1704 label_set_text (the_hint, NULL);
1705 return;
1708 hint = get_random_hint (force);
1710 if (hint != NULL)
1712 if (*hint != '\0')
1713 set_hintbar (hint);
1714 g_free (hint);
1716 else
1718 char text[BUF_SMALL];
1720 g_snprintf (text, sizeof (text), _("GNU Midnight Commander %s\n"), VERSION);
1721 set_hintbar (text);
1725 /* --------------------------------------------------------------------------------------------- */
1727 void
1728 change_panel (void)
1730 input_free_completions (cmdline);
1731 dlg_select_next_widget (midnight_dlg);
1734 /* --------------------------------------------------------------------------------------------- */
1736 /** Save current stat of directories to avoid reloading the panels
1737 * when no modifications have taken place
1739 void
1740 save_cwds_stat (void)
1742 if (panels_options.fast_reload)
1744 mc_stat (current_panel->cwd_vpath, &(current_panel->dir_stat));
1745 if (get_other_type () == view_listing)
1746 mc_stat (other_panel->cwd_vpath, &(other_panel->dir_stat));
1750 /* --------------------------------------------------------------------------------------------- */
1752 gboolean
1753 quiet_quit_cmd (void)
1755 print_last_revert = TRUE;
1756 return quit_cmd_internal (1);
1759 /* --------------------------------------------------------------------------------------------- */
1761 /** Run the main dialog that occupies the whole screen */
1762 gboolean
1763 do_nc (void)
1765 gboolean ret;
1767 #ifdef USE_INTERNAL_EDIT
1768 edit_stack_init ();
1769 #endif
1771 midnight_dlg = dlg_create (FALSE, 0, 0, 1, 1, WPOS_FULLSCREEN, FALSE, dialog_colors,
1772 midnight_callback, NULL, "[main]", NULL);
1774 /* Check if we were invoked as an editor or file viewer */
1775 if (mc_global.mc_run_mode != MC_RUN_FULL)
1777 setup_dummy_mc ();
1778 ret = mc_maybe_editor_or_viewer ();
1780 else
1782 /* We only need the first idle event to show user menu after start */
1783 widget_idle (WIDGET (midnight_dlg), TRUE);
1785 setup_mc ();
1786 mc_filehighlight = mc_fhl_new (TRUE);
1788 create_file_manager ();
1789 (void) dlg_run (midnight_dlg);
1791 mc_fhl_free (&mc_filehighlight);
1793 ret = TRUE;
1795 /* dlg_destroy destroys even current_panel->cwd_vpath, so we have to save a copy :) */
1796 if (mc_args__last_wd_file != NULL && vfs_current_is_local ())
1797 last_wd_string = g_strdup (vfs_path_as_str (current_panel->cwd_vpath));
1799 /* don't handle VFS timestamps for dirs opened in panels */
1800 mc_event_destroy (MCEVENT_GROUP_CORE, "vfs_timestamp");
1802 dir_list_free_list (&panelized_panel.list);
1805 /* Program end */
1806 mc_global.midnight_shutdown = TRUE;
1807 dialog_switch_shutdown ();
1808 done_mc ();
1809 dlg_destroy (midnight_dlg);
1810 current_panel = NULL;
1812 #ifdef USE_INTERNAL_EDIT
1813 edit_stack_free ();
1814 #endif
1816 if ((quit & SUBSHELL_EXIT) == 0)
1817 clr_scr ();
1819 return ret;
1822 /* --------------------------------------------------------------------------------------------- */