2 Routines invoked by a function key
3 They normally operate on the current panel.
5 Copyright (C) 1994-2016
6 Free Software Foundation, Inc.
9 Andrew Borodin <aborodin@vmail.ru>, 2013-2015
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 * \brief Source: routines invoked by a function key
30 * They normally operate on the current panel.
39 #include <sys/types.h>
53 #include "lib/global.h"
55 #include "lib/tty/tty.h" /* LINES, tty_touch_screen() */
56 #include "lib/tty/key.h" /* ALT() macro */
57 #include "lib/tty/win.h" /* do_enter_ca_mode() */
58 #include "lib/mcconfig.h"
59 #include "lib/filehighlight.h" /* MC_FHL_INI_FILE */
60 #include "lib/vfs/vfs.h"
61 #include "lib/fileloc.h"
62 #include "lib/strutil.h"
64 #include "lib/widget.h"
65 #include "lib/keybind.h" /* CK_Down, CK_History */
66 #include "lib/event.h" /* mc_event_raise() */
68 #include "src/viewer/mcviewer.h"
69 #include "src/setup.h"
70 #include "src/execute.h" /* toggle_panels() */
71 #include "src/history.h"
72 #include "src/util.h" /* check_for_default() */
74 #ifdef USE_INTERNAL_EDIT
75 #include "src/editor/edit.h"
79 #include "src/diffviewer/ydiff.h"
82 #include "fileopctx.h"
83 #include "file.h" /* file operation routines */
84 #include "find.h" /* find_file() */
86 #include "hotlist.h" /* hotlist_show() */
87 #include "panel.h" /* WPanel */
88 #include "tree.h" /* tree_chdir() */
89 #include "midnight.h" /* change_panel() */
90 #include "usermenu.h" /* MC_GLOBAL_MENU */
91 #include "command.h" /* cmdline */
92 #include "layout.h" /* get_current_type() */
93 #include "ext.h" /* regex_command() */
94 #include "boxes.h" /* cd_dialog() */
97 #include "cmd.h" /* Our definitions */
99 /*** global variables ****************************************************************************/
101 /*** file scope macro definitions ****************************************************************/
107 /*** file scope type declarations ****************************************************************/
111 compare_quick
, compare_size_only
, compare_thourough
114 /*** file scope variables ************************************************************************/
116 #ifdef ENABLE_VFS_NET
117 static const char *machine_str
= N_("Enter machine name (F1 for details):");
118 #endif /* ENABLE_VFS_NET */
120 /* --------------------------------------------------------------------------------------------- */
121 /*** file scope functions ************************************************************************/
122 /* --------------------------------------------------------------------------------------------- */
124 * Run viewer (internal or external) on the currently selected file.
125 * If normal is TRUE, force internal viewer and raw mode (used for F13).
128 do_view_cmd (gboolean normal
)
130 /* Directories are viewed by changing to them */
131 if (S_ISDIR (selection (current_panel
)->st
.st_mode
) || link_isdir (selection (current_panel
)))
133 vfs_path_t
*fname_vpath
;
135 if (confirm_view_dir
&& (current_panel
->marked
|| current_panel
->dirs_marked
))
138 (_("Confirmation"), _("Files tagged, want to cd?"), D_NORMAL
, 2,
139 _("&Yes"), _("&No")) != 0)
144 fname_vpath
= vfs_path_from_str (selection (current_panel
)->fname
);
145 if (!do_cd (fname_vpath
, cd_exact
))
146 message (D_ERROR
, MSG_ERROR
, _("Cannot change directory"));
147 vfs_path_free (fname_vpath
);
152 vfs_path_t
*filename_vpath
;
154 file_idx
= current_panel
->selected
;
155 filename_vpath
= vfs_path_from_str (current_panel
->dir
.list
[file_idx
].fname
);
156 view_file (filename_vpath
, normal
, use_internal_view
!= 0);
157 vfs_path_free (filename_vpath
);
163 /* --------------------------------------------------------------------------------------------- */
166 do_edit (const vfs_path_t
* what_vpath
)
168 edit_file_at_line (what_vpath
, use_internal_edit
!= 0, 0);
171 /* --------------------------------------------------------------------------------------------- */
174 set_panel_filter_to (WPanel
* p
, char *allocated_filter_string
)
179 if (!(allocated_filter_string
[0] == '*' && allocated_filter_string
[1] == 0))
180 p
->filter
= allocated_filter_string
;
182 g_free (allocated_filter_string
);
186 /* --------------------------------------------------------------------------------------------- */
187 /** Set a given panel filter expression */
190 set_panel_filter (WPanel
* p
)
195 x
= p
->filter
? p
->filter
: easy_patterns
? "*" : ".";
197 reg_exp
= input_dialog_help (_("Filter"),
198 _("Set expression for filtering filenames"),
199 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER
, x
, FALSE
,
200 INPUT_COMPLETE_FILENAMES
);
203 set_panel_filter_to (p
, reg_exp
);
206 /* --------------------------------------------------------------------------------------------- */
209 compare_files (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
, off_t size
)
212 int result
= -1; /* Different by default */
217 file1
= open (vfs_path_as_str (vpath1
), O_RDONLY
);
222 file2
= open (vfs_path_as_str (vpath2
), O_RDONLY
);
229 data1
= mmap (0, size
, PROT_READ
, MAP_FILE
| MAP_PRIVATE
, file1
, 0);
230 if (data1
!= (char *) -1)
234 data2
= mmap (0, size
, PROT_READ
, MAP_FILE
| MAP_PRIVATE
, file2
, 0);
235 if (data2
!= (char *) -1)
238 result
= memcmp (data1
, data2
, size
);
239 munmap (data2
, size
);
241 munmap (data1
, size
);
244 /* Don't have mmap() :( Even more ugly :) */
245 char buf1
[BUFSIZ
], buf2
[BUFSIZ
];
250 while ((n1
= read (file1
, buf1
, sizeof (buf1
))) == -1 && errno
== EINTR
)
252 while ((n2
= read (file2
, buf2
, sizeof (buf2
))) == -1 && errno
== EINTR
)
255 while (n1
== n2
&& n1
== sizeof (buf1
) && memcmp (buf1
, buf2
, sizeof (buf1
)) == 0);
256 result
= (n1
!= n2
) || memcmp (buf1
, buf2
, n1
);
257 #endif /* !HAVE_MMAP */
267 /* --------------------------------------------------------------------------------------------- */
270 compare_dir (WPanel
* panel
, WPanel
* other
, enum CompareMode mode
)
274 /* No marks by default */
277 panel
->dirs_marked
= 0;
279 /* Handle all files in the panel */
280 for (i
= 0; i
< panel
->dir
.len
; i
++)
282 file_entry_t
*source
= &panel
->dir
.list
[i
];
284 /* Default: unmarked */
285 file_mark (panel
, i
, 0);
287 /* Skip directories */
288 if (S_ISDIR (source
->st
.st_mode
))
291 /* Search the corresponding entry from the other panel */
292 for (j
= 0; j
< other
->dir
.len
; j
++)
294 if (strcmp (source
->fname
, other
->dir
.list
[j
].fname
) == 0)
297 if (j
>= other
->dir
.len
)
298 /* Not found -> mark */
299 do_file_mark (panel
, i
, 1);
303 file_entry_t
*target
= &other
->dir
.list
[j
];
305 if (mode
!= compare_size_only
)
307 /* Older version is not marked */
308 if (source
->st
.st_mtime
< target
->st
.st_mtime
)
312 /* Newer version with different size is marked */
313 if (source
->st
.st_size
!= target
->st
.st_size
)
315 do_file_mark (panel
, i
, 1);
319 if (mode
== compare_size_only
)
322 if (mode
== compare_quick
)
324 /* Thorough compare off, compare only time stamps */
325 /* Mark newer version, don't mark version with the same date */
326 if (source
->st
.st_mtime
> target
->st
.st_mtime
)
328 do_file_mark (panel
, i
, 1);
333 /* Thorough compare on, do byte-by-byte comparison */
335 vfs_path_t
*src_name
, *dst_name
;
337 src_name
= vfs_path_append_new (panel
->cwd_vpath
, source
->fname
, NULL
);
338 dst_name
= vfs_path_append_new (other
->cwd_vpath
, target
->fname
, NULL
);
339 if (compare_files (src_name
, dst_name
, source
->st
.st_size
))
340 do_file_mark (panel
, i
, 1);
341 vfs_path_free (src_name
);
342 vfs_path_free (dst_name
);
348 /* --------------------------------------------------------------------------------------------- */
351 do_link (link_type_t link_type
, const char *fname
)
353 char *dest
= NULL
, *src
= NULL
;
354 vfs_path_t
*fname_vpath
, *dest_vpath
= NULL
;
356 fname_vpath
= vfs_path_from_str (fname
);
357 if (link_type
== LINK_HARDLINK
)
359 src
= g_strdup_printf (_("Link %s to:"), str_trunc (fname
, 46));
361 input_expand_dialog (_("Link"), src
, MC_HISTORY_FM_LINK
, "", INPUT_COMPLETE_FILENAMES
);
365 dest_vpath
= vfs_path_from_str (dest
);
366 if (-1 == mc_link (fname_vpath
, dest_vpath
))
367 message (D_ERROR
, MSG_ERROR
, _("link: %s"), unix_error_string (errno
));
373 /* suggest the full path for symlink, and either the full or
374 relative path to the file it points to */
375 s
= vfs_path_append_new (current_panel
->cwd_vpath
, fname
, NULL
);
377 if (get_other_type () == view_listing
)
378 d
= vfs_path_append_new (other_panel
->cwd_vpath
, fname
, NULL
);
380 d
= vfs_path_from_str (fname
);
382 if (link_type
== LINK_SYMLINK_RELATIVE
)
386 s_str
= diff_two_paths (other_panel
->cwd_vpath
, s
);
388 s
= vfs_path_from_str_flags (s_str
, VPF_NO_CANON
);
392 symlink_dialog (s
, d
, &dest
, &src
);
396 if (!dest
|| !*dest
|| !src
|| !*src
)
400 dest_vpath
= vfs_path_from_str_flags (dest
, VPF_NO_CANON
);
402 s
= vfs_path_from_str (src
);
403 if (mc_symlink (dest_vpath
, s
) == -1)
404 message (D_ERROR
, MSG_ERROR
, _("symlink: %s"), unix_error_string (errno
));
408 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
412 vfs_path_free (fname_vpath
);
413 vfs_path_free (dest_vpath
);
418 /* --------------------------------------------------------------------------------------------- */
420 #if defined(ENABLE_VFS_UNDELFS) || defined(ENABLE_VFS_NET)
422 nice_cd (const char *text
, const char *xtext
, const char *help
,
423 const char *history_name
, const char *prefix
, int to_home
, gboolean strip_password
)
428 if (!SELECTED_IS_PANEL
)
432 input_dialog_help (text
, xtext
, help
, history_name
, INPUT_LAST_TEXT
, strip_password
,
433 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
| INPUT_COMPLETE_HOSTNAMES
|
434 INPUT_COMPLETE_USERNAMES
);
438 to_home
= 0; /* FIXME: how to solve going to home nicely? /~/ is
439 ugly as hell and leads to problems in vfs layer */
441 if (strncmp (prefix
, machine
, strlen (prefix
)) == 0)
442 cd_path
= g_strconcat (machine
, to_home
? "/~/" : (char *) NULL
, (char *) NULL
);
444 cd_path
= g_strconcat (prefix
, machine
, to_home
? "/~/" : (char *) NULL
, (char *) NULL
);
448 if (!IS_PATH_SEP (*cd_path
))
452 cd_path
= g_strconcat (PATH_SEP_STR
, tmp
, (char *) NULL
);
457 vfs_path_t
*cd_vpath
;
459 cd_vpath
= vfs_path_from_str_flags (cd_path
, VPF_NO_CANON
);
460 if (!do_panel_cd (MENU_PANEL
, cd_vpath
, cd_parse_command
))
461 message (D_ERROR
, MSG_ERROR
, _("Cannot chdir to \"%s\""), cd_path
);
462 vfs_path_free (cd_vpath
);
466 #endif /* ENABLE_VFS_UNDELFS || ENABLE_VFS_NET */
468 /* --------------------------------------------------------------------------------------------- */
471 configure_panel_listing (WPanel
* p
, int list_type
, int brief_cols
, int use_msformat
, char **user
,
474 p
->user_mini_status
= use_msformat
;
475 p
->list_type
= list_type
;
477 if (list_type
== list_brief
)
478 p
->brief_cols
= brief_cols
;
480 if (list_type
== list_user
|| use_msformat
)
482 g_free (p
->user_format
);
483 p
->user_format
= *user
;
486 g_free (p
->user_status_format
[list_type
]);
487 p
->user_status_format
[list_type
] = *status
;
490 set_panel_formats (p
);
493 set_panel_formats (p
);
497 /* --------------------------------------------------------------------------------------------- */
500 switch_to_listing (int panel_index
)
502 if (get_display_type (panel_index
) != view_listing
)
503 set_display_type (panel_index
, view_listing
);
508 p
= PANEL (get_panel_widget (panel_index
));
511 p
->is_panelized
= FALSE
;
517 /* --------------------------------------------------------------------------------------------- */
518 /** Handle the tree internal listing modes switching */
521 set_basic_panel_listing_to (int panel_index
, int listing_mode
)
526 p
= PANEL (get_panel_widget (panel_index
));
527 switch_to_listing (panel_index
);
528 p
->list_type
= listing_mode
;
530 ok
= set_panel_formats (p
) == 0;
538 /* --------------------------------------------------------------------------------------------- */
539 /*** public functions ****************************************************************************/
540 /* --------------------------------------------------------------------------------------------- */
543 view_file_at_line (const vfs_path_t
* filename_vpath
, gboolean plain_view
, gboolean internal
,
544 long start_line
, off_t search_start
, off_t search_end
)
550 int changed_hex_mode
= 0;
551 int changed_nroff_flag
= 0;
552 int changed_magic_flag
= 0;
554 mcview_altered_hex_mode
= 0;
555 mcview_altered_nroff_flag
= 0;
556 mcview_altered_magic_flag
= 0;
557 if (mcview_default_hex_mode
)
558 changed_hex_mode
= 1;
559 if (mcview_default_nroff_flag
)
560 changed_nroff_flag
= 1;
561 if (mcview_default_magic_flag
)
562 changed_magic_flag
= 1;
563 mcview_default_hex_mode
= 0;
564 mcview_default_nroff_flag
= 0;
565 mcview_default_magic_flag
= 0;
567 ret
= mcview_viewer (NULL
, filename_vpath
, start_line
, search_start
, search_end
);
569 if (changed_hex_mode
&& !mcview_altered_hex_mode
)
570 mcview_default_hex_mode
= 1;
571 if (changed_nroff_flag
&& !mcview_altered_nroff_flag
)
572 mcview_default_nroff_flag
= 1;
573 if (changed_magic_flag
&& !mcview_altered_magic_flag
)
574 mcview_default_magic_flag
= 1;
576 dialog_switch_process_pending ();
580 char view_entry
[BUF_TINY
];
583 g_snprintf (view_entry
, sizeof (view_entry
), "View:%ld", start_line
);
585 strcpy (view_entry
, "View");
587 ret
= (regex_command (filename_vpath
, view_entry
) == 0);
590 ret
= mcview_viewer (NULL
, filename_vpath
, start_line
, search_start
, search_end
);
591 dialog_switch_process_pending ();
596 static const char *viewer
= NULL
;
600 viewer
= getenv ("VIEWER");
602 viewer
= getenv ("PAGER");
607 execute_external_editor_or_viewer (viewer
, filename_vpath
, start_line
);
613 /* --------------------------------------------------------------------------------------------- */
614 /** view_file (filename, plain_view, internal)
617 * filename_vpath: The file name to view
618 * plain_view: If set does not do any fancy pre-processing (no filtering) and
619 * always invokes the internal viewer.
620 * internal: If set uses the internal viewer, otherwise an external viewer.
624 view_file (const vfs_path_t
* filename_vpath
, gboolean plain_view
, gboolean internal
)
626 return view_file_at_line (filename_vpath
, plain_view
, internal
, 0, 0, 0);
630 /* --------------------------------------------------------------------------------------------- */
631 /** Run user's preferred viewer on the currently selected file */
639 /* --------------------------------------------------------------------------------------------- */
640 /** Ask for file and run user's preferred viewer on it */
649 input_expand_dialog (_("View file"), _("Filename:"),
650 MC_HISTORY_FM_VIEW_FILE
, selection (current_panel
)->fname
,
651 INPUT_COMPLETE_FILENAMES
);
652 if (filename
== NULL
)
655 vpath
= vfs_path_from_str (filename
);
657 view_file (vpath
, FALSE
, use_internal_view
!= 0);
658 vfs_path_free (vpath
);
661 /* --------------------------------------------------------------------------------------------- */
662 /** Run plain internal viewer on the currently selected file */
669 /* --------------------------------------------------------------------------------------------- */
672 view_filtered_cmd (void)
675 const char *initial_command
;
677 if (cmdline
->buffer
[0] == '\0')
678 initial_command
= selection (current_panel
)->fname
;
680 initial_command
= cmdline
->buffer
;
683 input_dialog (_("Filtered view"),
684 _("Filter command and arguments:"),
685 MC_HISTORY_FM_FILTERED_VIEW
, initial_command
,
686 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_COMMANDS
);
690 mcview_viewer (command
, NULL
, 0, 0, 0);
692 dialog_switch_process_pending ();
696 /* --------------------------------------------------------------------------------------------- */
699 edit_file_at_line (const vfs_path_t
* what_vpath
, gboolean internal
, long start_line
)
702 #ifdef USE_INTERNAL_EDIT
704 edit_file (what_vpath
, start_line
);
706 #endif /* USE_INTERNAL_EDIT */
708 static const char *editor
= NULL
;
714 editor
= getenv ("EDITOR");
716 editor
= get_default_editor ();
719 execute_external_editor_or_viewer (editor
, what_vpath
, start_line
);
722 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
723 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
725 #ifdef USE_INTERNAL_EDIT
726 if (use_internal_edit
)
727 dialog_switch_process_pending ();
729 #endif /* USE_INTERNAL_EDIT */
733 /* --------------------------------------------------------------------------------------------- */
740 fname
= vfs_path_from_str (selection (current_panel
)->fname
);
741 if (regex_command (fname
, "Edit") == 0)
743 vfs_path_free (fname
);
746 /* --------------------------------------------------------------------------------------------- */
748 #ifdef USE_INTERNAL_EDIT
750 edit_cmd_force_internal (void)
754 fname
= vfs_path_from_str (selection (current_panel
)->fname
);
755 if (regex_command (fname
, "Edit") == 0)
756 edit_file_at_line (fname
, TRUE
, 1);
757 vfs_path_free (fname
);
761 /* --------------------------------------------------------------------------------------------- */
766 vfs_path_t
*fname_vpath
= NULL
;
768 if (editor_ask_filename_before_edit
)
772 fname
= input_expand_dialog (_("Edit file"), _("Enter file name:"),
773 MC_HISTORY_EDIT_LOAD
, "", INPUT_COMPLETE_FILENAMES
);
778 fname_vpath
= vfs_path_from_str (fname
);
784 mc_global
.source_codepage
= default_source_codepage
;
786 do_edit (fname_vpath
);
788 vfs_path_free (fname_vpath
);
791 /* --------------------------------------------------------------------------------------------- */
792 /** Invoked by F5. Copy, default to the other panel. */
798 if (panel_operate (current_panel
, OP_COPY
, FALSE
))
800 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
805 /* --------------------------------------------------------------------------------------------- */
806 /** Invoked by F6. Move/rename, default to the other panel, ignore marks. */
812 if (panel_operate (current_panel
, OP_MOVE
, FALSE
))
814 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
819 /* --------------------------------------------------------------------------------------------- */
820 /** Invoked by F15. Copy, default to the same panel, ignore marks. */
823 copy_cmd_local (void)
826 if (panel_operate (current_panel
, OP_COPY
, TRUE
))
828 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
833 /* --------------------------------------------------------------------------------------------- */
834 /** Invoked by F16. Move/rename, default to the same panel. */
837 rename_cmd_local (void)
840 if (panel_operate (current_panel
, OP_MOVE
, TRUE
))
842 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
847 /* --------------------------------------------------------------------------------------------- */
853 const char *name
= "";
855 /* If 'on' then automatically fills name with current selected item name */
856 if (auto_fill_mkdir_name
&& !DIR_IS_DOTDOT (selection (current_panel
)->fname
))
857 name
= selection (current_panel
)->fname
;
860 input_expand_dialog (_("Create a new Directory"),
861 _("Enter directory name:"), MC_HISTORY_FM_MKDIR
, name
,
862 INPUT_COMPLETE_FILENAMES
);
864 if (dir
!= NULL
&& *dir
!= '\0')
868 if (IS_PATH_SEP (dir
[0]) || dir
[0] == '~')
869 absdir
= vfs_path_from_str (dir
);
872 /* possible escaped '~' */
873 /* allow create directory with name '~' */
876 if (dir
[0] == '\\' && dir
[1] == '~')
879 absdir
= vfs_path_append_new (current_panel
->cwd_vpath
, tmpdir
, NULL
);
883 if (my_mkdir (absdir
, 0777) == 0)
885 update_panels (UP_OPTIMIZE
, dir
);
887 select_item (current_panel
);
891 message (D_ERROR
, MSG_ERROR
, "%s", unix_error_string (errno
));
893 vfs_path_free (absdir
);
898 /* --------------------------------------------------------------------------------------------- */
905 if (panel_operate (current_panel
, OP_DELETE
, FALSE
))
907 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
912 /* --------------------------------------------------------------------------------------------- */
913 /** Invoked by F18. Remove selected file, regardless of marked files. */
916 delete_cmd_local (void)
920 if (panel_operate (current_panel
, OP_DELETE
, TRUE
))
922 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
927 /* --------------------------------------------------------------------------------------------- */
935 /* --------------------------------------------------------------------------------------------- */
936 /** Invoked from the left/right menus */
943 if (!SELECTED_IS_PANEL
)
947 set_panel_filter (p
);
950 /* --------------------------------------------------------------------------------------------- */
955 panel_update_flags_t flag
= UP_ONLY_CURRENT
;
957 if (get_current_type () == view_listing
&& get_other_type () == view_listing
&&
958 vfs_path_equal (current_panel
->cwd_vpath
, other_panel
->cwd_vpath
))
961 update_panels (UP_RELOAD
| flag
, UP_KEEPSEL
);
965 /* --------------------------------------------------------------------------------------------- */
970 vfs_path_t
*extdir_vpath
;
976 dir
= query_dialog (_("Extension file edit"),
977 _("Which extension file you want to edit?"), D_NORMAL
, 2,
978 _("&User"), _("&System Wide"));
980 extdir_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_LIB_EXT
, NULL
);
984 vfs_path_t
*buffer_vpath
;
986 buffer_vpath
= mc_config_get_full_vpath (MC_FILEBIND_FILE
);
987 check_for_default (extdir_vpath
, buffer_vpath
);
988 do_edit (buffer_vpath
);
989 vfs_path_free (buffer_vpath
);
993 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
995 vfs_path_free (extdir_vpath
);
996 extdir_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, MC_LIB_EXT
, NULL
);
998 do_edit (extdir_vpath
);
1000 vfs_path_free (extdir_vpath
);
1001 flush_extension_file ();
1004 /* --------------------------------------------------------------------------------------------- */
1005 /** edit file menu for mc */
1008 edit_mc_menu_cmd (void)
1010 vfs_path_t
*buffer_vpath
;
1011 vfs_path_t
*menufile_vpath
;
1015 dir
= query_dialog (_("Menu edit"),
1016 _("Which menu file do you want to edit?"),
1017 D_NORMAL
, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
1019 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_GLOBAL_MENU
, NULL
);
1021 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
1023 vfs_path_free (menufile_vpath
);
1024 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, MC_GLOBAL_MENU
, NULL
);
1030 buffer_vpath
= vfs_path_from_str (MC_LOCAL_MENU
);
1031 check_for_default (menufile_vpath
, buffer_vpath
);
1032 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
1036 buffer_vpath
= mc_config_get_full_vpath (MC_USERMENU_FILE
);
1037 check_for_default (menufile_vpath
, buffer_vpath
);
1041 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_GLOBAL_MENU
, NULL
);
1042 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
1044 vfs_path_free (buffer_vpath
);
1045 buffer_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, MC_GLOBAL_MENU
, NULL
);
1050 vfs_path_free (menufile_vpath
);
1054 do_edit (buffer_vpath
);
1056 vfs_path_free (buffer_vpath
);
1057 vfs_path_free (menufile_vpath
);
1060 /* --------------------------------------------------------------------------------------------- */
1065 vfs_path_t
*fhlfile_vpath
= NULL
;
1070 if (geteuid () == 0)
1072 dir
= query_dialog (_("Highlighting groups file edit"),
1073 _("Which highlighting file you want to edit?"), D_NORMAL
, 2,
1074 _("&User"), _("&System Wide"));
1076 fhlfile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_FHL_INI_FILE
, NULL
);
1080 vfs_path_t
*buffer_vpath
;
1082 buffer_vpath
= mc_config_get_full_vpath (MC_FHL_INI_FILE
);
1083 check_for_default (fhlfile_vpath
, buffer_vpath
);
1084 do_edit (buffer_vpath
);
1085 vfs_path_free (buffer_vpath
);
1089 if (!exist_file (vfs_path_get_last_path_str (fhlfile_vpath
)))
1091 vfs_path_free (fhlfile_vpath
);
1093 vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_FHL_INI_FILE
, NULL
);
1095 do_edit (fhlfile_vpath
);
1097 vfs_path_free (fhlfile_vpath
);
1099 /* refresh highlighting rules */
1100 mc_fhl_free (&mc_filehighlight
);
1101 mc_filehighlight
= mc_fhl_new (TRUE
);
1104 /* --------------------------------------------------------------------------------------------- */
1111 target
= hotlist_show (LIST_HOTLIST
);
1115 if (get_current_type () == view_tree
)
1116 tree_chdir (the_tree
, target
);
1119 vfs_path_t
*deprecated_vpath
;
1122 deprecated_vpath
= vfs_path_from_str_flags (target
, VPF_USE_DEPRECATED_PARSER
);
1123 cmd
= g_strconcat ("cd ", vfs_path_as_str (deprecated_vpath
), (char *) NULL
);
1124 vfs_path_free (deprecated_vpath
);
1126 do_cd_command (cmd
);
1137 vfs_path_t
*target_vpath
;
1139 target
= hotlist_show (LIST_VFSLIST
);
1143 target_vpath
= vfs_path_from_str (target
);
1144 if (!do_cd (target_vpath
, cd_exact
))
1145 message (D_ERROR
, MSG_ERROR
, _("Cannot change directory"));
1146 vfs_path_free (target_vpath
);
1149 #endif /* ENABLE_VFS */
1151 /* --------------------------------------------------------------------------------------------- */
1154 compare_dirs_cmd (void)
1157 enum CompareMode thorough_flag
;
1160 query_dialog (_("Compare directories"),
1161 _("Select compare method:"), D_NORMAL
, 4,
1162 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
1164 if (choice
< 0 || choice
> 2)
1167 thorough_flag
= choice
;
1169 if (get_current_type () == view_listing
&& get_other_type () == view_listing
)
1171 compare_dir (current_panel
, other_panel
, thorough_flag
);
1172 compare_dir (other_panel
, current_panel
, thorough_flag
);
1176 message (D_ERROR
, MSG_ERROR
,
1177 _("Both panels should be in the listing mode\nto use this command"));
1181 /* --------------------------------------------------------------------------------------------- */
1183 #ifdef USE_DIFF_VIEW
1185 diff_view_cmd (void)
1187 /* both panels must be in the list mode */
1188 if (get_current_type () != view_listing
|| get_other_type () != view_listing
)
1191 if (get_current_index () == 0)
1192 dview_diff_cmd (current_panel
, other_panel
);
1194 dview_diff_cmd (other_panel
, current_panel
);
1196 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
1197 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
1199 dialog_switch_process_pending ();
1203 /* --------------------------------------------------------------------------------------------- */
1209 tty_touch_screen ();
1213 /* --------------------------------------------------------------------------------------------- */
1216 view_other_cmd (void)
1218 static int message_flag
= TRUE
;
1220 if (!mc_global
.tty
.xterm_flag
&& mc_global
.tty
.console_flag
== '\0'
1221 && !mc_global
.tty
.use_subshell
&& !output_starts_shell
)
1224 message (D_ERROR
, MSG_ERROR
,
1225 _("Not an xterm or Linux console;\nthe panels cannot be toggled."));
1226 message_flag
= FALSE
;
1234 /* --------------------------------------------------------------------------------------------- */
1237 link_cmd (link_type_t link_type
)
1239 char *filename
= selection (current_panel
)->fname
;
1241 if (filename
!= NULL
)
1242 do_link (link_type
, filename
);
1245 /* --------------------------------------------------------------------------------------------- */
1248 edit_symlink_cmd (void)
1250 if (S_ISLNK (selection (current_panel
)->st
.st_mode
))
1252 char buffer
[MC_MAXPATHLEN
];
1256 vfs_path_t
*p_vpath
;
1258 p
= selection (current_panel
)->fname
;
1259 p_vpath
= vfs_path_from_str (p
);
1261 q
= g_strdup_printf (_("Symlink '%s\' points to:"), str_trunc (p
, 32));
1263 i
= readlink (p
, buffer
, MC_MAXPATHLEN
- 1);
1270 input_expand_dialog (_("Edit symlink"), q
, MC_HISTORY_FM_EDIT_LINK
, buffer
,
1271 INPUT_COMPLETE_FILENAMES
);
1274 if (*dest
&& strcmp (buffer
, dest
))
1277 if (mc_unlink (p_vpath
) == -1)
1279 message (D_ERROR
, MSG_ERROR
, _("edit symlink, unable to remove %s: %s"),
1280 p
, unix_error_string (errno
));
1284 vfs_path_t
*dest_vpath
;
1286 dest_vpath
= vfs_path_from_str_flags (dest
, VPF_NO_CANON
);
1287 if (mc_symlink (dest_vpath
, p_vpath
) == -1)
1288 message (D_ERROR
, MSG_ERROR
, _("edit symlink: %s"),
1289 unix_error_string (errno
));
1290 vfs_path_free (dest_vpath
);
1292 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
1299 vfs_path_free (p_vpath
);
1303 message (D_ERROR
, MSG_ERROR
, _("'%s' is not a symbolic link"),
1304 selection (current_panel
)->fname
);
1308 /* --------------------------------------------------------------------------------------------- */
1313 ev_help_t event_data
= { NULL
, NULL
};
1315 if (current_panel
->searching
)
1316 event_data
.node
= "[Quick search]";
1318 event_data
.node
= "[main]";
1320 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1323 /* --------------------------------------------------------------------------------------------- */
1326 user_file_menu_cmd (void)
1328 (void) user_menu_cmd (NULL
, NULL
, -1);
1331 /* --------------------------------------------------------------------------------------------- */
1333 * Return a random hint. If force is not 0, ignore the timeout.
1337 get_random_hint (gboolean force
)
1339 char *data
, *result
= NULL
, *eop
;
1341 static int last_sec
;
1342 static struct timeval tv
;
1345 /* Do not change hints more often than one minute */
1346 gettimeofday (&tv
, NULL
);
1347 if (!force
&& !(tv
.tv_sec
> last_sec
+ 60))
1348 return g_strdup ("");
1349 last_sec
= tv
.tv_sec
;
1351 data
= load_mc_home_file (mc_global
.share_data_dir
, MC_HINT
, NULL
, &len
);
1355 /* get a random entry */
1357 start
= ((size_t) rand ()) % (len
- 1);
1359 /* Search the start of paragraph */
1360 for (; start
!= 0; start
--)
1361 if (data
[start
] == '\n' && data
[start
+ 1] == '\n')
1367 /* Search the end of paragraph */
1368 for (eop
= data
+ start
; *eop
!= '\0'; eop
++)
1370 if (*eop
== '\n' && *(eop
+ 1) == '\n')
1379 /* hint files are stored in utf-8 */
1380 /* try convert hint file from utf-8 to terminal encoding */
1381 conv
= str_crt_conv_from ("UTF-8");
1382 if (conv
!= INVALID_CONV
)
1386 buffer
= g_string_sized_new (len
- start
);
1387 if (str_convert (conv
, &data
[start
], buffer
) != ESTR_FAILURE
)
1388 result
= g_string_free (buffer
, FALSE
);
1390 g_string_free (buffer
, TRUE
);
1391 str_close_conv (conv
);
1394 result
= g_strndup (data
+ start
, len
- start
);
1400 /* --------------------------------------------------------------------------------------------- */
1402 #ifdef ENABLE_VFS_FTP
1406 nice_cd (_("FTP to machine"), _(machine_str
),
1407 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "ftp://", 1, TRUE
);
1409 #endif /* ENABLE_VFS_FTP */
1411 /* --------------------------------------------------------------------------------------------- */
1413 #ifdef ENABLE_VFS_SFTP
1417 nice_cd (_("SFTP to machine"), _(machine_str
),
1418 "[SFTP (SSH File Transfer Protocol) filesystem]",
1419 ":sftplink_cmd: SFTP to machine ", "sftp://", 1, TRUE
);
1421 #endif /* ENABLE_VFS_SFTP */
1423 /* --------------------------------------------------------------------------------------------- */
1425 #ifdef ENABLE_VFS_FISH
1429 nice_cd (_("Shell link to machine"), _(machine_str
),
1430 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1433 #endif /* ENABLE_VFS_FISH */
1435 /* --------------------------------------------------------------------------------------------- */
1437 #ifdef ENABLE_VFS_SMB
1441 nice_cd (_("SMB link to machine"), _(machine_str
),
1442 "[SMB File System]", ":smblink_cmd: SMB link to machine ", "smb://", 0, TRUE
);
1444 #endif /* ENABLE_VFS_SMB */
1446 /* --------------------------------------------------------------------------------------------- */
1448 #ifdef ENABLE_VFS_UNDELFS
1452 nice_cd (_("Undelete files on an ext2 file system"),
1453 _("Enter device (without /dev/) to undelete\nfiles on: (F1 for details)"),
1454 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "undel://", 0, FALSE
);
1456 #endif /* ENABLE_VFS_UNDELFS */
1458 /* --------------------------------------------------------------------------------------------- */
1463 char *p
= cd_dialog ();
1467 char *q
= g_strconcat ("cd ", p
, (char *) NULL
);
1475 /* --------------------------------------------------------------------------------------------- */
1477 \brief calculate dirs sizes
1479 calculate dirs sizes and resort panel:
1480 dirs_selected = show size for selected dirs,
1481 otherwise = show size for dir under cursor:
1482 dir under cursor ".." = show size for all dirs,
1483 otherwise = show size for dir under cursor
1487 smart_dirsize_cmd (void)
1489 WPanel
*panel
= current_panel
;
1490 file_entry_t
*entry
;
1492 entry
= &(panel
->dir
.list
[panel
->selected
]);
1493 if ((S_ISDIR (entry
->st
.st_mode
) && DIR_IS_DOTDOT (entry
->fname
)) || panel
->dirs_marked
)
1496 single_dirsize_cmd ();
1499 /* --------------------------------------------------------------------------------------------- */
1502 single_dirsize_cmd (void)
1504 WPanel
*panel
= current_panel
;
1505 file_entry_t
*entry
;
1507 entry
= &(panel
->dir
.list
[panel
->selected
]);
1508 if (S_ISDIR (entry
->st
.st_mode
) && !DIR_IS_DOTDOT (entry
->fname
))
1510 size_t dir_count
= 0;
1512 uintmax_t total
= 0;
1513 dirsize_status_msg_t dsm
;
1516 p
= vfs_path_from_str (entry
->fname
);
1518 memset (&dsm
, 0, sizeof (dsm
));
1519 status_msg_init (STATUS_MSG (&dsm
), _("Directory scanning"), 0, dirsize_status_init_cb
,
1520 dirsize_status_update_cb
, dirsize_status_deinit_cb
);
1522 if (compute_dir_size (p
, &dsm
, &dir_count
, &count
, &total
, TRUE
) == FILE_CONT
)
1524 entry
->st
.st_size
= (off_t
) total
;
1525 entry
->f
.dir_size_computed
= 1;
1530 status_msg_deinit (STATUS_MSG (&dsm
));
1533 if (panels_options
.mark_moves_down
)
1534 send_message (panel
, NULL
, MSG_ACTION
, CK_Down
, NULL
);
1536 recalculate_panel_summary (panel
);
1538 if (current_panel
->sort_field
->sort_routine
== (GCompareFunc
) sort_size
)
1539 panel_re_sort (panel
);
1544 /* --------------------------------------------------------------------------------------------- */
1549 WPanel
*panel
= current_panel
;
1551 dirsize_status_msg_t dsm
;
1553 memset (&dsm
, 0, sizeof (dsm
));
1554 status_msg_init (STATUS_MSG (&dsm
), _("Directory scanning"), 0, dirsize_status_init_cb
,
1555 dirsize_status_update_cb
, dirsize_status_deinit_cb
);
1557 for (i
= 0; i
< panel
->dir
.len
; i
++)
1558 if (S_ISDIR (panel
->dir
.list
[i
].st
.st_mode
)
1559 && ((panel
->dirs_marked
&& panel
->dir
.list
[i
].f
.marked
)
1560 || !panel
->dirs_marked
) && !DIR_IS_DOTDOT (panel
->dir
.list
[i
].fname
))
1563 size_t dir_count
= 0;
1565 uintmax_t total
= 0;
1568 p
= vfs_path_from_str (panel
->dir
.list
[i
].fname
);
1569 ok
= compute_dir_size (p
, &dsm
, &dir_count
, &count
, &total
, TRUE
) != FILE_CONT
;
1574 panel
->dir
.list
[i
].st
.st_size
= (off_t
) total
;
1575 panel
->dir
.list
[i
].f
.dir_size_computed
= 1;
1578 status_msg_deinit (STATUS_MSG (&dsm
));
1580 recalculate_panel_summary (panel
);
1582 if (current_panel
->sort_field
->sort_routine
== (GCompareFunc
) sort_size
)
1583 panel_re_sort (panel
);
1588 /* --------------------------------------------------------------------------------------------- */
1591 save_setup_cmd (void)
1596 vpath
= vfs_path_from_str_flags (mc_config_get_path (), VPF_STRIP_HOME
);
1597 path
= vfs_path_as_str (vpath
);
1599 if (save_setup (TRUE
, TRUE
))
1600 message (D_NORMAL
, _("Setup"), _("Setup saved to %s"), path
);
1602 message (D_ERROR
, _("Setup"), _("Unable to save setup to %s"), path
);
1604 vfs_path_free (vpath
);
1607 /* --------------------------------------------------------------------------------------------- */
1610 info_cmd_no_menu (void)
1612 if (get_display_type (0) == view_info
)
1613 set_display_type (0, view_listing
);
1614 else if (get_display_type (1) == view_info
)
1615 set_display_type (1, view_listing
);
1617 set_display_type (current_panel
== left_panel
? 1 : 0, view_info
);
1620 /* --------------------------------------------------------------------------------------------- */
1623 quick_cmd_no_menu (void)
1625 if (get_display_type (0) == view_quick
)
1626 set_display_type (0, view_listing
);
1627 else if (get_display_type (1) == view_quick
)
1628 set_display_type (1, view_listing
);
1630 set_display_type (current_panel
== left_panel
? 1 : 0, view_quick
);
1633 /* --------------------------------------------------------------------------------------------- */
1638 switch_to_listing (MENU_PANEL_IDX
);
1641 /* --------------------------------------------------------------------------------------------- */
1644 change_listing_cmd (void)
1647 int use_msformat
, brief_cols
;
1648 char *user
, *status
;
1651 if (SELECTED_IS_PANEL
)
1652 p
= MENU_PANEL_IDX
== 0 ? left_panel
: right_panel
;
1654 list_type
= panel_listing_box (p
, MENU_PANEL_IDX
, &user
, &status
, &use_msformat
, &brief_cols
);
1655 if (list_type
!= -1)
1657 switch_to_listing (MENU_PANEL_IDX
);
1658 p
= MENU_PANEL_IDX
== 0 ? left_panel
: right_panel
;
1659 configure_panel_listing (p
, list_type
, brief_cols
, use_msformat
, &user
, &status
);
1665 /* --------------------------------------------------------------------------------------------- */
1668 panel_tree_cmd (void)
1670 set_display_type (MENU_PANEL_IDX
, view_tree
);
1673 /* --------------------------------------------------------------------------------------------- */
1678 set_display_type (MENU_PANEL_IDX
, view_info
);
1681 /* --------------------------------------------------------------------------------------------- */
1684 quick_view_cmd (void)
1686 if (PANEL (get_panel_widget (MENU_PANEL_IDX
)) == current_panel
)
1688 set_display_type (MENU_PANEL_IDX
, view_quick
);
1691 /* --------------------------------------------------------------------------------------------- */
1694 toggle_listing_cmd (void)
1699 current
= get_current_index ();
1700 p
= PANEL (get_panel_widget (current
));
1702 set_basic_panel_listing_to (current
, (p
->list_type
+ 1) % LIST_TYPES
);
1705 /* --------------------------------------------------------------------------------------------- */
1711 if (SELECTED_IS_PANEL
)
1712 panel_change_encoding (MENU_PANEL
);
1716 /* --------------------------------------------------------------------------------------------- */