2 Routines invoked by a function key
3 They normally operate on the current panel.
5 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 2005, 2006, 2007, 2009, 2011, 2013
7 The Free Software Foundation, Inc.
10 Andrew Borodin <aborodin@vmail.ru>, 2013
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 * \brief Source: routines invoked by a function key
31 * They normally operate on the current panel.
40 #include <sys/types.h>
55 #include "lib/global.h"
57 #include "lib/tty/tty.h" /* LINES, tty_touch_screen() */
58 #include "lib/tty/key.h" /* ALT() macro */
59 #include "lib/tty/win.h" /* do_enter_ca_mode() */
60 #include "lib/mcconfig.h"
61 #include "lib/search.h"
62 #include "lib/filehighlight.h" /* MC_FHL_INI_FILE */
63 #include "lib/vfs/vfs.h"
64 #include "lib/fileloc.h"
65 #include "lib/strutil.h"
67 #include "lib/widget.h"
68 #include "lib/keybind.h" /* CK_Down, CK_History */
69 #include "lib/event.h" /* mc_event_raise() */
71 #include "src/viewer/mcviewer.h"
72 #include "src/setup.h"
73 #include "src/execute.h" /* toggle_panels() */
74 #include "src/history.h"
75 #include "src/util.h" /* check_for_default() */
77 #ifdef USE_INTERNAL_EDIT
78 #include "src/editor/edit.h"
82 #include "src/diffviewer/ydiff.h"
85 #include "fileopctx.h"
86 #include "file.h" /* file operation routines */
87 #include "find.h" /* find_file() */
89 #include "hotlist.h" /* hotlist_show() */
90 #include "panel.h" /* WPanel */
91 #include "tree.h" /* tree_chdir() */
92 #include "midnight.h" /* change_panel() */
93 #include "usermenu.h" /* MC_GLOBAL_MENU */
94 #include "command.h" /* cmdline */
95 #include "layout.h" /* get_current_type() */
96 #include "ext.h" /* regex_command() */
97 #include "boxes.h" /* cd_dialog() */
100 #include "cmd.h" /* Our definitions */
102 /*** global variables ****************************************************************************/
104 int select_flags
= SELECT_MATCH_CASE
| SELECT_SHELL_PATTERNS
;
106 /*** file scope macro definitions ****************************************************************/
112 /*** file scope type declarations ****************************************************************/
116 compare_quick
, compare_size_only
, compare_thourough
119 /*** file scope variables ************************************************************************/
121 #ifdef ENABLE_VFS_NET
122 static const char *machine_str
= N_("Enter machine name (F1 for details):");
123 #endif /* ENABLE_VFS_NET */
125 /*** file scope functions ************************************************************************/
126 /* --------------------------------------------------------------------------------------------- */
128 * Run viewer (internal or external) on the currently selected file.
129 * If normal is TRUE, force internal viewer and raw mode (used for F13).
132 do_view_cmd (gboolean normal
)
134 /* Directories are viewed by changing to them */
135 if (S_ISDIR (selection (current_panel
)->st
.st_mode
) || link_isdir (selection (current_panel
)))
137 vfs_path_t
*fname_vpath
;
139 if (confirm_view_dir
&& (current_panel
->marked
|| current_panel
->dirs_marked
))
142 (_("Confirmation"), _("Files tagged, want to cd?"), D_NORMAL
, 2,
143 _("&Yes"), _("&No")) != 0)
148 fname_vpath
= vfs_path_from_str (selection (current_panel
)->fname
);
149 if (!do_cd (fname_vpath
, cd_exact
))
150 message (D_ERROR
, MSG_ERROR
, _("Cannot change directory"));
151 vfs_path_free (fname_vpath
);
156 vfs_path_t
*filename_vpath
;
158 file_idx
= current_panel
->selected
;
159 filename_vpath
= vfs_path_from_str (current_panel
->dir
.list
[file_idx
].fname
);
160 view_file (filename_vpath
, normal
, use_internal_view
);
161 vfs_path_free (filename_vpath
);
167 /* --------------------------------------------------------------------------------------------- */
170 do_edit (const vfs_path_t
* what_vpath
)
174 if (!use_internal_edit
)
179 if (what_vpath
!= NULL
&& *(vfs_path_get_by_index (what_vpath
, 0)->path
) != '\0')
180 load_file_position (what_vpath
, &line
, &column
, &offset
, NULL
);
182 do_edit_at_line (what_vpath
, use_internal_edit
, line
);
185 /* --------------------------------------------------------------------------------------------- */
188 set_panel_filter_to (WPanel
* p
, char *allocated_filter_string
)
193 if (!(allocated_filter_string
[0] == '*' && allocated_filter_string
[1] == 0))
194 p
->filter
= allocated_filter_string
;
196 g_free (allocated_filter_string
);
200 /* --------------------------------------------------------------------------------------------- */
201 /** Set a given panel filter expression */
204 set_panel_filter (WPanel
* p
)
209 x
= p
->filter
? p
->filter
: easy_patterns
? "*" : ".";
211 reg_exp
= input_dialog_help (_("Filter"),
212 _("Set expression for filtering filenames"),
213 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER
, x
, FALSE
,
214 INPUT_COMPLETE_FILENAMES
);
217 set_panel_filter_to (p
, reg_exp
);
220 /* --------------------------------------------------------------------------------------------- */
223 select_unselect_cmd (const char *title
, const char *history_name
, gboolean do_select
)
225 int files_only
= (select_flags
& SELECT_FILES_ONLY
) != 0;
226 int case_sens
= (select_flags
& SELECT_MATCH_CASE
) != 0;
227 int shell_patterns
= (select_flags
& SELECT_SHELL_PATTERNS
) != 0;
233 quick_widget_t quick_widgets
[] = {
235 QUICK_INPUT (INPUT_LAST_TEXT
, history_name
, ®_exp
, NULL
,
236 FALSE
, FALSE
, INPUT_COMPLETE_FILENAMES
),
238 QUICK_CHECKBOX (N_("&Files only"), &files_only
, NULL
),
239 QUICK_CHECKBOX (N_("&Using shell patterns"), &shell_patterns
, NULL
),
241 QUICK_CHECKBOX (N_("&Case sensitive"), &case_sens
, NULL
),
247 quick_dialog_t qdlg
= {
249 title
, "[Select/Unselect Files]",
250 quick_widgets
, NULL
, NULL
253 if (quick_dialog (&qdlg
) == B_CANCEL
)
256 if (reg_exp
== NULL
|| *reg_exp
== '\0')
262 search
= mc_search_new (reg_exp
, -1, NULL
);
263 search
->search_type
= (shell_patterns
!= 0) ? MC_SEARCH_T_GLOB
: MC_SEARCH_T_REGEX
;
264 search
->is_entire_line
= TRUE
;
265 search
->is_case_sensitive
= case_sens
!= 0;
267 for (i
= 0; i
< current_panel
->dir
.len
; i
++)
269 if (DIR_IS_DOTDOT (current_panel
->dir
.list
[i
].fname
))
271 if (S_ISDIR (current_panel
->dir
.list
[i
].st
.st_mode
) && files_only
!= 0)
274 if (mc_search_run (search
, current_panel
->dir
.list
[i
].fname
,
275 0, current_panel
->dir
.list
[i
].fnamelen
, NULL
))
276 do_file_mark (current_panel
, i
, do_select
);
279 mc_search_free (search
);
285 select_flags
|= SELECT_MATCH_CASE
;
287 select_flags
|= SELECT_FILES_ONLY
;
288 if (shell_patterns
!= 0)
289 select_flags
|= SELECT_SHELL_PATTERNS
;
292 /* --------------------------------------------------------------------------------------------- */
295 compare_files (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
, off_t size
)
298 int result
= -1; /* Different by default */
303 file1
= open (vfs_path_as_str (vpath1
), O_RDONLY
);
308 file2
= open (vfs_path_as_str (vpath2
), O_RDONLY
);
315 data1
= mmap (0, size
, PROT_READ
, MAP_FILE
| MAP_PRIVATE
, file1
, 0);
316 if (data1
!= (char *) -1)
320 data2
= mmap (0, size
, PROT_READ
, MAP_FILE
| MAP_PRIVATE
, file2
, 0);
321 if (data2
!= (char *) -1)
324 result
= memcmp (data1
, data2
, size
);
325 munmap (data2
, size
);
327 munmap (data1
, size
);
330 /* Don't have mmap() :( Even more ugly :) */
331 char buf1
[BUFSIZ
], buf2
[BUFSIZ
];
336 while ((n1
= read (file1
, buf1
, BUFSIZ
)) == -1 && errno
== EINTR
);
337 while ((n2
= read (file2
, buf2
, BUFSIZ
)) == -1 && errno
== EINTR
);
339 while (n1
== n2
&& n1
== BUFSIZ
&& !memcmp (buf1
, buf2
, BUFSIZ
));
340 result
= (n1
!= n2
) || memcmp (buf1
, buf2
, n1
);
341 #endif /* !HAVE_MMAP */
351 /* --------------------------------------------------------------------------------------------- */
354 compare_dir (WPanel
* panel
, WPanel
* other
, enum CompareMode mode
)
358 /* No marks by default */
361 panel
->dirs_marked
= 0;
363 /* Handle all files in the panel */
364 for (i
= 0; i
< panel
->dir
.len
; i
++)
366 file_entry_t
*source
= &panel
->dir
.list
[i
];
368 /* Default: unmarked */
369 file_mark (panel
, i
, 0);
371 /* Skip directories */
372 if (S_ISDIR (source
->st
.st_mode
))
375 /* Search the corresponding entry from the other panel */
376 for (j
= 0; j
< other
->dir
.len
; j
++)
378 if (strcmp (source
->fname
, other
->dir
.list
[j
].fname
) == 0)
381 if (j
>= other
->dir
.len
)
382 /* Not found -> mark */
383 do_file_mark (panel
, i
, 1);
387 file_entry_t
*target
= &other
->dir
.list
[j
];
389 if (mode
!= compare_size_only
)
391 /* Older version is not marked */
392 if (source
->st
.st_mtime
< target
->st
.st_mtime
)
396 /* Newer version with different size is marked */
397 if (source
->st
.st_size
!= target
->st
.st_size
)
399 do_file_mark (panel
, i
, 1);
403 if (mode
== compare_size_only
)
406 if (mode
== compare_quick
)
408 /* Thorough compare off, compare only time stamps */
409 /* Mark newer version, don't mark version with the same date */
410 if (source
->st
.st_mtime
> target
->st
.st_mtime
)
412 do_file_mark (panel
, i
, 1);
417 /* Thorough compare on, do byte-by-byte comparison */
419 vfs_path_t
*src_name
, *dst_name
;
421 src_name
= vfs_path_append_new (panel
->cwd_vpath
, source
->fname
, NULL
);
422 dst_name
= vfs_path_append_new (other
->cwd_vpath
, target
->fname
, NULL
);
423 if (compare_files (src_name
, dst_name
, source
->st
.st_size
))
424 do_file_mark (panel
, i
, 1);
425 vfs_path_free (src_name
);
426 vfs_path_free (dst_name
);
432 /* --------------------------------------------------------------------------------------------- */
435 do_link (link_type_t link_type
, const char *fname
)
437 char *dest
= NULL
, *src
= NULL
;
438 vfs_path_t
*fname_vpath
, *dest_vpath
= NULL
;
440 fname_vpath
= vfs_path_from_str (fname
);
441 if (link_type
== LINK_HARDLINK
)
443 src
= g_strdup_printf (_("Link %s to:"), str_trunc (fname
, 46));
445 input_expand_dialog (_("Link"), src
, MC_HISTORY_FM_LINK
, "", INPUT_COMPLETE_FILENAMES
);
449 dest_vpath
= vfs_path_from_str (dest
);
450 if (-1 == mc_link (fname_vpath
, dest_vpath
))
451 message (D_ERROR
, MSG_ERROR
, _("link: %s"), unix_error_string (errno
));
457 /* suggest the full path for symlink, and either the full or
458 relative path to the file it points to */
459 s
= vfs_path_append_new (current_panel
->cwd_vpath
, fname
, NULL
);
461 if (get_other_type () == view_listing
)
462 d
= vfs_path_append_new (other_panel
->cwd_vpath
, fname
, NULL
);
464 d
= vfs_path_from_str (fname
);
466 if (link_type
== LINK_SYMLINK_RELATIVE
)
470 s_str
= diff_two_paths (other_panel
->cwd_vpath
, s
);
472 s
= vfs_path_from_str_flags (s_str
, VPF_NO_CANON
);
476 symlink_dialog (s
, d
, &dest
, &src
);
480 if (!dest
|| !*dest
|| !src
|| !*src
)
484 dest_vpath
= vfs_path_from_str_flags (dest
, VPF_NO_CANON
);
486 s
= vfs_path_from_str (src
);
487 if (mc_symlink (dest_vpath
, s
) == -1)
488 message (D_ERROR
, MSG_ERROR
, _("symlink: %s"), unix_error_string (errno
));
492 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
496 vfs_path_free (fname_vpath
);
497 vfs_path_free (dest_vpath
);
502 /* --------------------------------------------------------------------------------------------- */
504 #if defined(ENABLE_VFS_UNDELFS) || defined(ENABLE_VFS_NET)
506 nice_cd (const char *text
, const char *xtext
, const char *help
,
507 const char *history_name
, const char *prefix
, int to_home
, gboolean strip_password
)
512 if (!SELECTED_IS_PANEL
)
516 input_dialog_help (text
, xtext
, help
, history_name
, "", strip_password
,
517 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_CD
| INPUT_COMPLETE_HOSTNAMES
|
518 INPUT_COMPLETE_USERNAMES
);
522 to_home
= 0; /* FIXME: how to solve going to home nicely? /~/ is
523 ugly as hell and leads to problems in vfs layer */
525 if (strncmp (prefix
, machine
, strlen (prefix
)) == 0)
526 cd_path
= g_strconcat (machine
, to_home
? "/~/" : (char *) NULL
, (char *) NULL
);
528 cd_path
= g_strconcat (prefix
, machine
, to_home
? "/~/" : (char *) NULL
, (char *) NULL
);
532 if (*cd_path
!= PATH_SEP
)
536 cd_path
= g_strconcat (PATH_SEP_STR
, tmp
, (char *) NULL
);
541 vfs_path_t
*cd_vpath
;
543 cd_vpath
= vfs_path_from_str_flags (cd_path
, VPF_NO_CANON
);
544 if (!do_panel_cd (MENU_PANEL
, cd_vpath
, cd_parse_command
))
545 message (D_ERROR
, MSG_ERROR
, _("Cannot chdir to \"%s\""), cd_path
);
546 vfs_path_free (cd_vpath
);
550 #endif /* ENABLE_VFS_UNDELFS || ENABLE_VFS_NET */
552 /* --------------------------------------------------------------------------------------------- */
555 configure_panel_listing (WPanel
* p
, int list_type
, int use_msformat
, char *user
, char *status
)
557 p
->user_mini_status
= use_msformat
;
558 p
->list_type
= list_type
;
560 if (list_type
== list_user
|| use_msformat
)
562 g_free (p
->user_format
);
563 p
->user_format
= user
;
565 g_free (p
->user_status_format
[list_type
]);
566 p
->user_status_format
[list_type
] = status
;
568 set_panel_formats (p
);
576 set_panel_formats (p
);
580 /* --------------------------------------------------------------------------------------------- */
583 switch_to_listing (int panel_index
)
585 if (get_display_type (panel_index
) != view_listing
)
586 set_display_type (panel_index
, view_listing
);
591 p
= (WPanel
*) get_panel_widget (panel_index
);
594 p
->is_panelized
= FALSE
;
600 /* --------------------------------------------------------------------------------------------- */
601 /** Handle the tree internal listing modes switching */
604 set_basic_panel_listing_to (int panel_index
, int listing_mode
)
606 WPanel
*p
= (WPanel
*) get_panel_widget (panel_index
);
609 switch_to_listing (panel_index
);
610 p
->list_type
= listing_mode
;
612 ok
= set_panel_formats (p
) == 0;
620 /* --------------------------------------------------------------------------------------------- */
621 /*** public functions ****************************************************************************/
622 /* --------------------------------------------------------------------------------------------- */
625 view_file_at_line (const vfs_path_t
* filename_vpath
, int plain_view
, int internal
, long start_line
)
631 int changed_hex_mode
= 0;
632 int changed_nroff_flag
= 0;
633 int changed_magic_flag
= 0;
635 mcview_altered_hex_mode
= 0;
636 mcview_altered_nroff_flag
= 0;
637 mcview_altered_magic_flag
= 0;
638 if (mcview_default_hex_mode
)
639 changed_hex_mode
= 1;
640 if (mcview_default_nroff_flag
)
641 changed_nroff_flag
= 1;
642 if (mcview_default_magic_flag
)
643 changed_magic_flag
= 1;
644 mcview_default_hex_mode
= 0;
645 mcview_default_nroff_flag
= 0;
646 mcview_default_magic_flag
= 0;
648 ret
= mcview_viewer (NULL
, filename_vpath
, start_line
);
650 if (changed_hex_mode
&& !mcview_altered_hex_mode
)
651 mcview_default_hex_mode
= 1;
652 if (changed_nroff_flag
&& !mcview_altered_nroff_flag
)
653 mcview_default_nroff_flag
= 1;
654 if (changed_magic_flag
&& !mcview_altered_magic_flag
)
655 mcview_default_magic_flag
= 1;
657 dialog_switch_process_pending ();
661 char view_entry
[BUF_TINY
];
664 g_snprintf (view_entry
, sizeof (view_entry
), "View:%ld", start_line
);
666 strcpy (view_entry
, "View");
668 ret
= (regex_command (filename_vpath
, view_entry
) == 0);
671 ret
= mcview_viewer (NULL
, filename_vpath
, start_line
);
672 dialog_switch_process_pending ();
677 static const char *viewer
= NULL
;
681 viewer
= getenv ("VIEWER");
683 viewer
= getenv ("PAGER");
688 execute_external_editor_or_viewer (viewer
, filename_vpath
, start_line
);
694 /* --------------------------------------------------------------------------------------------- */
695 /** view_file (filename, plain_view, internal)
698 * filename_vpath: The file name to view
699 * plain_view: If set does not do any fancy pre-processing (no filtering) and
700 * always invokes the internal viewer.
701 * internal: If set uses the internal viewer, otherwise an external viewer.
705 view_file (const vfs_path_t
* filename_vpath
, int plain_view
, int internal
)
714 if (filename_vpath
!= NULL
&& *(vfs_path_get_by_index (filename_vpath
, 0)->path
) != '\0')
715 load_file_position (filename_vpath
, &line
, &column
, &offset
, NULL
);
718 return view_file_at_line (filename_vpath
, plain_view
, internal
, line
);
722 /* --------------------------------------------------------------------------------------------- */
723 /** Run user's preferred viewer on the currently selected file */
731 /* --------------------------------------------------------------------------------------------- */
732 /** Ask for file and run user's preferred viewer on it */
741 input_expand_dialog (_("View file"), _("Filename:"),
742 MC_HISTORY_FM_VIEW_FILE
, selection (current_panel
)->fname
,
743 INPUT_COMPLETE_FILENAMES
);
747 vpath
= vfs_path_from_str (filename
);
749 view_file (vpath
, 0, use_internal_view
);
750 vfs_path_free (vpath
);
753 /* --------------------------------------------------------------------------------------------- */
754 /** Run plain internal viewer on the currently selected file */
761 /* --------------------------------------------------------------------------------------------- */
764 view_filtered_cmd (void)
767 const char *initial_command
;
769 if (cmdline
->buffer
[0] == '\0')
770 initial_command
= selection (current_panel
)->fname
;
772 initial_command
= cmdline
->buffer
;
775 input_dialog (_("Filtered view"),
776 _("Filter command and arguments:"),
777 MC_HISTORY_FM_FILTERED_VIEW
, initial_command
,
778 INPUT_COMPLETE_FILENAMES
| INPUT_COMPLETE_COMMANDS
);
782 mcview_viewer (command
, NULL
, 0);
784 dialog_switch_process_pending ();
788 /* --------------------------------------------------------------------------------------------- */
791 do_edit_at_line (const vfs_path_t
* what_vpath
, gboolean internal
, long start_line
)
794 #ifdef USE_INTERNAL_EDIT
796 edit_file (what_vpath
, start_line
);
798 #endif /* USE_INTERNAL_EDIT */
800 static const char *editor
= NULL
;
806 editor
= getenv ("EDITOR");
808 editor
= get_default_editor ();
814 execute_external_editor_or_viewer (editor
, what_vpath
, start_line
);
817 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
818 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
820 #ifdef USE_INTERNAL_EDIT
821 if (use_internal_edit
)
822 dialog_switch_process_pending ();
824 #endif /* USE_INTERNAL_EDIT */
828 /* --------------------------------------------------------------------------------------------- */
835 fname
= vfs_path_from_str (selection (current_panel
)->fname
);
836 if (regex_command (fname
, "Edit") == 0)
838 vfs_path_free (fname
);
841 /* --------------------------------------------------------------------------------------------- */
843 #ifdef USE_INTERNAL_EDIT
845 edit_cmd_force_internal (void)
849 fname
= vfs_path_from_str (selection (current_panel
)->fname
);
850 if (regex_command (fname
, "Edit") == 0)
851 do_edit_at_line (fname
, TRUE
, 0);
852 vfs_path_free (fname
);
856 /* --------------------------------------------------------------------------------------------- */
861 vfs_path_t
*fname_vpath
= NULL
;
863 if (editor_ask_filename_before_edit
)
867 fname
= input_expand_dialog (_("Edit file"), _("Enter file name:"),
868 MC_HISTORY_EDIT_LOAD
, "", INPUT_COMPLETE_FILENAMES
);
873 fname_vpath
= vfs_path_from_str (fname
);
879 mc_global
.source_codepage
= default_source_codepage
;
881 do_edit (fname_vpath
);
883 vfs_path_free (fname_vpath
);
886 /* --------------------------------------------------------------------------------------------- */
887 /** Invoked by F5. Copy, default to the other panel. */
893 if (panel_operate (current_panel
, OP_COPY
, FALSE
))
895 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
900 /* --------------------------------------------------------------------------------------------- */
901 /** Invoked by F6. Move/rename, default to the other panel, ignore marks. */
907 if (panel_operate (current_panel
, OP_MOVE
, FALSE
))
909 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
914 /* --------------------------------------------------------------------------------------------- */
915 /** Invoked by F15. Copy, default to the same panel, ignore marks. */
918 copy_cmd_local (void)
921 if (panel_operate (current_panel
, OP_COPY
, TRUE
))
923 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
928 /* --------------------------------------------------------------------------------------------- */
929 /** Invoked by F16. Move/rename, default to the same panel. */
932 rename_cmd_local (void)
935 if (panel_operate (current_panel
, OP_MOVE
, TRUE
))
937 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
942 /* --------------------------------------------------------------------------------------------- */
948 const char *name
= "";
950 /* If 'on' then automatically fills name with current selected item name */
951 if (auto_fill_mkdir_name
&& !DIR_IS_DOTDOT (selection (current_panel
)->fname
))
952 name
= selection (current_panel
)->fname
;
955 input_expand_dialog (_("Create a new Directory"),
956 _("Enter directory name:"), MC_HISTORY_FM_MKDIR
, name
,
957 INPUT_COMPLETE_FILENAMES
);
959 if (dir
!= NULL
&& dir
!= '\0')
963 if (dir
[0] == '/' || dir
[0] == '~')
964 absdir
= vfs_path_from_str (dir
);
967 /* possible escaped '~' */
968 /* allow create directory with name '~' */
971 if (dir
[0] == '\\' && dir
[1] == '~')
974 absdir
= vfs_path_append_new (current_panel
->cwd_vpath
, tmpdir
, NULL
);
978 if (my_mkdir (absdir
, 0777) == 0)
980 update_panels (UP_OPTIMIZE
, dir
);
982 select_item (current_panel
);
986 message (D_ERROR
, MSG_ERROR
, "%s", unix_error_string (errno
));
988 vfs_path_free (absdir
);
993 /* --------------------------------------------------------------------------------------------- */
1000 if (panel_operate (current_panel
, OP_DELETE
, FALSE
))
1002 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
1007 /* --------------------------------------------------------------------------------------------- */
1008 /** Invoked by F18. Remove selected file, regardless of marked files. */
1011 delete_cmd_local (void)
1015 if (panel_operate (current_panel
, OP_DELETE
, TRUE
))
1017 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
1022 /* --------------------------------------------------------------------------------------------- */
1030 /* --------------------------------------------------------------------------------------------- */
1031 /** Invoked from the left/right menus */
1038 if (!SELECTED_IS_PANEL
)
1042 set_panel_filter (p
);
1045 /* --------------------------------------------------------------------------------------------- */
1050 panel_update_flags_t flag
= UP_ONLY_CURRENT
;
1052 if (get_current_type () == view_listing
&& get_other_type () == view_listing
&&
1053 vfs_path_equal (current_panel
->cwd_vpath
, other_panel
->cwd_vpath
))
1056 update_panels (UP_RELOAD
| flag
, UP_KEEPSEL
);
1060 /* --------------------------------------------------------------------------------------------- */
1063 select_invert_cmd (void)
1067 for (i
= 0; i
< current_panel
->dir
.len
; i
++)
1069 file_entry_t
*file
= ¤t_panel
->dir
.list
[i
];
1071 if (!panels_options
.reverse_files_only
|| !S_ISDIR (file
->st
.st_mode
))
1072 do_file_mark (current_panel
, i
, !file
->f
.marked
);
1076 /* --------------------------------------------------------------------------------------------- */
1081 select_unselect_cmd (_("Select"), ":select_cmd: Select ", TRUE
);
1084 /* --------------------------------------------------------------------------------------------- */
1089 select_unselect_cmd (_("Unselect"), ":unselect_cmd: Unselect ", FALSE
);
1092 /* --------------------------------------------------------------------------------------------- */
1097 vfs_path_t
*extdir_vpath
;
1101 if (geteuid () == 0)
1103 dir
= query_dialog (_("Extension file edit"),
1104 _("Which extension file you want to edit?"), D_NORMAL
, 2,
1105 _("&User"), _("&System Wide"));
1107 extdir_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_LIB_EXT
, NULL
);
1111 vfs_path_t
*buffer_vpath
;
1113 buffer_vpath
= mc_config_get_full_vpath (MC_FILEBIND_FILE
);
1114 check_for_default (extdir_vpath
, buffer_vpath
);
1115 do_edit (buffer_vpath
);
1116 vfs_path_free (buffer_vpath
);
1120 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
1122 vfs_path_free (extdir_vpath
);
1123 extdir_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, MC_LIB_EXT
, NULL
);
1125 do_edit (extdir_vpath
);
1127 vfs_path_free (extdir_vpath
);
1128 flush_extension_file ();
1131 /* --------------------------------------------------------------------------------------------- */
1132 /** edit file menu for mc */
1135 edit_mc_menu_cmd (void)
1137 vfs_path_t
*buffer_vpath
;
1138 vfs_path_t
*menufile_vpath
;
1141 dir
= query_dialog (_("Menu edit"),
1142 _("Which menu file do you want to edit?"),
1143 D_NORMAL
, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
1145 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_GLOBAL_MENU
, NULL
);
1147 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
1149 vfs_path_free (menufile_vpath
);
1150 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, MC_GLOBAL_MENU
, NULL
);
1156 buffer_vpath
= vfs_path_from_str (MC_LOCAL_MENU
);
1157 check_for_default (menufile_vpath
, buffer_vpath
);
1158 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
1162 buffer_vpath
= mc_config_get_full_vpath (MC_USERMENU_FILE
);
1163 check_for_default (menufile_vpath
, buffer_vpath
);
1167 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_GLOBAL_MENU
, NULL
);
1168 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
1170 vfs_path_free (buffer_vpath
);
1171 buffer_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, MC_GLOBAL_MENU
, NULL
);
1176 vfs_path_free (menufile_vpath
);
1180 do_edit (buffer_vpath
);
1182 vfs_path_free (buffer_vpath
);
1183 vfs_path_free (menufile_vpath
);
1186 /* --------------------------------------------------------------------------------------------- */
1191 vfs_path_t
*fhlfile_vpath
= NULL
;
1196 if (geteuid () == 0)
1198 dir
= query_dialog (_("Highlighting groups file edit"),
1199 _("Which highlighting file you want to edit?"), D_NORMAL
, 2,
1200 _("&User"), _("&System Wide"));
1202 fhlfile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_FHL_INI_FILE
, NULL
);
1206 vfs_path_t
*buffer_vpath
;
1208 buffer_vpath
= mc_config_get_full_vpath (MC_FHL_INI_FILE
);
1209 check_for_default (fhlfile_vpath
, buffer_vpath
);
1210 do_edit (buffer_vpath
);
1211 vfs_path_free (buffer_vpath
);
1215 if (!exist_file (vfs_path_get_last_path_str (fhlfile_vpath
)))
1217 vfs_path_free (fhlfile_vpath
);
1219 vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_FHL_INI_FILE
, NULL
);
1221 do_edit (fhlfile_vpath
);
1223 vfs_path_free (fhlfile_vpath
);
1225 /* refresh highlighting rules */
1226 mc_fhl_free (&mc_filehighlight
);
1227 mc_filehighlight
= mc_fhl_new (TRUE
);
1230 /* --------------------------------------------------------------------------------------------- */
1237 target
= hotlist_show (LIST_HOTLIST
);
1241 if (get_current_type () == view_tree
)
1242 tree_chdir (the_tree
, target
);
1245 vfs_path_t
*deprecated_vpath
;
1248 deprecated_vpath
= vfs_path_from_str_flags (target
, VPF_USE_DEPRECATED_PARSER
);
1249 cmd
= g_strconcat ("cd ", vfs_path_as_str (deprecated_vpath
), (char *) NULL
);
1250 vfs_path_free (deprecated_vpath
);
1252 do_cd_command (cmd
);
1263 vfs_path_t
*target_vpath
;
1265 target
= hotlist_show (LIST_VFSLIST
);
1269 target_vpath
= vfs_path_from_str (target
);
1270 if (!do_cd (target_vpath
, cd_exact
))
1271 message (D_ERROR
, MSG_ERROR
, _("Cannot change directory"));
1272 vfs_path_free (target_vpath
);
1275 #endif /* ENABLE_VFS */
1277 /* --------------------------------------------------------------------------------------------- */
1280 compare_dirs_cmd (void)
1283 enum CompareMode thorough_flag
;
1286 query_dialog (_("Compare directories"),
1287 _("Select compare method:"), D_NORMAL
, 4,
1288 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
1290 if (choice
< 0 || choice
> 2)
1293 thorough_flag
= choice
;
1295 if (get_current_type () == view_listing
&& get_other_type () == view_listing
)
1297 compare_dir (current_panel
, other_panel
, thorough_flag
);
1298 compare_dir (other_panel
, current_panel
, thorough_flag
);
1302 message (D_ERROR
, MSG_ERROR
,
1303 _("Both panels should be in the listing mode\nto use this command"));
1307 /* --------------------------------------------------------------------------------------------- */
1309 #ifdef USE_DIFF_VIEW
1311 diff_view_cmd (void)
1313 /* both panels must be in the list mode */
1314 if (get_current_type () != view_listing
|| get_other_type () != view_listing
)
1317 if (get_current_index () == 0)
1318 dview_diff_cmd (current_panel
, other_panel
);
1320 dview_diff_cmd (other_panel
, current_panel
);
1322 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
1323 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
1325 dialog_switch_process_pending ();
1329 /* --------------------------------------------------------------------------------------------- */
1335 tty_touch_screen ();
1339 /* --------------------------------------------------------------------------------------------- */
1342 view_other_cmd (void)
1344 static int message_flag
= TRUE
;
1346 if (!mc_global
.tty
.xterm_flag
&& mc_global
.tty
.console_flag
== '\0'
1347 && !mc_global
.tty
.use_subshell
&& !output_starts_shell
)
1350 message (D_ERROR
, MSG_ERROR
,
1351 _("Not an xterm or Linux console;\nthe panels cannot be toggled."));
1352 message_flag
= FALSE
;
1360 /* --------------------------------------------------------------------------------------------- */
1363 link_cmd (link_type_t link_type
)
1365 char *filename
= selection (current_panel
)->fname
;
1367 if (filename
!= NULL
)
1368 do_link (link_type
, filename
);
1371 /* --------------------------------------------------------------------------------------------- */
1374 edit_symlink_cmd (void)
1376 if (S_ISLNK (selection (current_panel
)->st
.st_mode
))
1378 char buffer
[MC_MAXPATHLEN
];
1382 vfs_path_t
*p_vpath
;
1384 p
= selection (current_panel
)->fname
;
1385 p_vpath
= vfs_path_from_str (p
);
1387 q
= g_strdup_printf (_("Symlink '%s\' points to:"), str_trunc (p
, 32));
1389 i
= readlink (p
, buffer
, MC_MAXPATHLEN
- 1);
1396 input_expand_dialog (_("Edit symlink"), q
, MC_HISTORY_FM_EDIT_LINK
, buffer
,
1397 INPUT_COMPLETE_FILENAMES
);
1400 if (*dest
&& strcmp (buffer
, dest
))
1403 if (mc_unlink (p_vpath
) == -1)
1405 message (D_ERROR
, MSG_ERROR
, _("edit symlink, unable to remove %s: %s"),
1406 p
, unix_error_string (errno
));
1410 vfs_path_t
*dest_vpath
;
1412 dest_vpath
= vfs_path_from_str_flags (dest
, VPF_NO_CANON
);
1413 if (mc_symlink (dest_vpath
, p_vpath
) == -1)
1414 message (D_ERROR
, MSG_ERROR
, _("edit symlink: %s"),
1415 unix_error_string (errno
));
1416 vfs_path_free (dest_vpath
);
1418 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
1425 vfs_path_free (p_vpath
);
1429 message (D_ERROR
, MSG_ERROR
, _("'%s' is not a symbolic link"),
1430 selection (current_panel
)->fname
);
1434 /* --------------------------------------------------------------------------------------------- */
1439 ev_help_t event_data
= { NULL
, NULL
};
1441 if (current_panel
->searching
)
1442 event_data
.node
= "[Quick search]";
1444 event_data
.node
= "[main]";
1446 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1449 /* --------------------------------------------------------------------------------------------- */
1452 user_file_menu_cmd (void)
1454 (void) user_menu_cmd (NULL
, NULL
, -1);
1457 /* --------------------------------------------------------------------------------------------- */
1459 * Return a random hint. If force is not 0, ignore the timeout.
1463 get_random_hint (int force
)
1465 char *data
, *result
= NULL
, *eop
;
1468 static int last_sec
;
1469 static struct timeval tv
;
1472 /* Do not change hints more often than one minute */
1473 gettimeofday (&tv
, NULL
);
1474 if (!force
&& !(tv
.tv_sec
> last_sec
+ 60))
1475 return g_strdup ("");
1476 last_sec
= tv
.tv_sec
;
1478 data
= load_mc_home_file (mc_global
.share_data_dir
, MC_HINT
, NULL
);
1482 /* get a random entry */
1484 len
= strlen (data
);
1485 start
= rand () % (len
- 1);
1487 /* Search the start of paragraph */
1488 for (; start
!= 0; start
--)
1489 if (data
[start
] == '\n' && data
[start
+ 1] == '\n')
1495 /* Search the end of paragraph */
1496 for (eop
= data
+ start
; *eop
!= '\0'; eop
++)
1498 if (*eop
== '\n' && *(eop
+ 1) == '\n')
1507 /* hint files are stored in utf-8 */
1508 /* try convert hint file from utf-8 to terminal encoding */
1509 conv
= str_crt_conv_from ("UTF-8");
1510 if (conv
!= INVALID_CONV
)
1514 buffer
= g_string_new ("");
1515 if (str_convert (conv
, &data
[start
], buffer
) != ESTR_FAILURE
)
1516 result
= g_string_free (buffer
, FALSE
);
1518 g_string_free (buffer
, TRUE
);
1519 str_close_conv (conv
);
1522 result
= g_strdup (&data
[start
]);
1528 /* --------------------------------------------------------------------------------------------- */
1530 #ifdef ENABLE_VFS_FTP
1534 nice_cd (_("FTP to machine"), _(machine_str
),
1535 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "ftp://", 1, TRUE
);
1537 #endif /* ENABLE_VFS_FTP */
1539 /* --------------------------------------------------------------------------------------------- */
1541 #ifdef ENABLE_VFS_SFTP
1545 nice_cd (_("SFTP to machine"), _(machine_str
),
1546 "[SFTP (SSH File Transfer Protocol) filesystem]",
1547 ":sftplink_cmd: SFTP to machine ", "sftp://", 1, TRUE
);
1549 #endif /* ENABLE_VFS_SFTP */
1551 /* --------------------------------------------------------------------------------------------- */
1553 #ifdef ENABLE_VFS_FISH
1557 nice_cd (_("Shell link to machine"), _(machine_str
),
1558 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1561 #endif /* ENABLE_VFS_FISH */
1563 /* --------------------------------------------------------------------------------------------- */
1565 #ifdef ENABLE_VFS_SMB
1569 nice_cd (_("SMB link to machine"), _(machine_str
),
1570 "[SMB File System]", ":smblink_cmd: SMB link to machine ", "smb://", 0, TRUE
);
1572 #endif /* ENABLE_VFS_SMB */
1574 /* --------------------------------------------------------------------------------------------- */
1576 #ifdef ENABLE_VFS_UNDELFS
1580 nice_cd (_("Undelete files on an ext2 file system"),
1581 _("Enter device (without /dev/) to undelete\nfiles on: (F1 for details)"),
1582 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "undel://", 0, FALSE
);
1584 #endif /* ENABLE_VFS_UNDELFS */
1586 /* --------------------------------------------------------------------------------------------- */
1591 char *p
= cd_dialog ();
1595 char *q
= g_strconcat ("cd ", p
, (char *) NULL
);
1603 /* --------------------------------------------------------------------------------------------- */
1605 \brief calculate dirs sizes
1607 calculate dirs sizes and resort panel:
1608 dirs_selected = show size for selected dirs,
1609 otherwise = show size for dir under cursor:
1610 dir under cursor ".." = show size for all dirs,
1611 otherwise = show size for dir under cursor
1615 smart_dirsize_cmd (void)
1617 WPanel
*panel
= current_panel
;
1618 file_entry_t
*entry
;
1620 entry
= &(panel
->dir
.list
[panel
->selected
]);
1621 if ((S_ISDIR (entry
->st
.st_mode
) && DIR_IS_DOTDOT (entry
->fname
)) || panel
->dirs_marked
)
1624 single_dirsize_cmd ();
1627 /* --------------------------------------------------------------------------------------------- */
1630 single_dirsize_cmd (void)
1632 WPanel
*panel
= current_panel
;
1633 file_entry_t
*entry
;
1635 entry
= &(panel
->dir
.list
[panel
->selected
]);
1636 if (S_ISDIR (entry
->st
.st_mode
) && !DIR_IS_DOTDOT (entry
->fname
))
1639 uintmax_t total
= 0;
1640 ComputeDirSizeUI
*ui
;
1643 ui
= compute_dir_size_create_ui (FALSE
);
1644 p
= vfs_path_from_str (entry
->fname
);
1646 if (compute_dir_size (p
, ui
, compute_dir_size_update_ui
, &count
, &total
, TRUE
) == FILE_CONT
)
1648 entry
->st
.st_size
= (off_t
) total
;
1649 entry
->f
.dir_size_computed
= 1;
1653 compute_dir_size_destroy_ui (ui
);
1656 if (panels_options
.mark_moves_down
)
1657 send_message (panel
, NULL
, MSG_ACTION
, CK_Down
, NULL
);
1659 recalculate_panel_summary (panel
);
1661 if (current_panel
->sort_field
->sort_routine
== (GCompareFunc
) sort_size
)
1662 panel_re_sort (panel
);
1667 /* --------------------------------------------------------------------------------------------- */
1672 WPanel
*panel
= current_panel
;
1674 ComputeDirSizeUI
*ui
;
1676 ui
= compute_dir_size_create_ui (FALSE
);
1678 for (i
= 0; i
< panel
->dir
.len
; i
++)
1679 if (S_ISDIR (panel
->dir
.list
[i
].st
.st_mode
)
1680 && ((panel
->dirs_marked
&& panel
->dir
.list
[i
].f
.marked
)
1681 || !panel
->dirs_marked
) && !DIR_IS_DOTDOT (panel
->dir
.list
[i
].fname
))
1685 uintmax_t total
= 0;
1688 p
= vfs_path_from_str (panel
->dir
.list
[i
].fname
);
1689 ok
= compute_dir_size (p
, ui
, compute_dir_size_update_ui
, &count
, &total
,
1696 panel
->dir
.list
[i
].st
.st_size
= (off_t
) total
;
1697 panel
->dir
.list
[i
].f
.dir_size_computed
= 1;
1700 compute_dir_size_destroy_ui (ui
);
1702 recalculate_panel_summary (panel
);
1704 if (current_panel
->sort_field
->sort_routine
== (GCompareFunc
) sort_size
)
1705 panel_re_sort (panel
);
1710 /* --------------------------------------------------------------------------------------------- */
1713 save_setup_cmd (void)
1718 vpath
= vfs_path_from_str_flags (mc_config_get_path (), VPF_STRIP_HOME
);
1719 path
= vfs_path_as_str (vpath
);
1721 if (save_setup (TRUE
, TRUE
))
1722 message (D_NORMAL
, _("Setup"), _("Setup saved to %s"), path
);
1724 message (D_ERROR
, _("Setup"), _("Unable to save setup to %s"), path
);
1726 vfs_path_free (vpath
);
1729 /* --------------------------------------------------------------------------------------------- */
1732 info_cmd_no_menu (void)
1734 if (get_display_type (0) == view_info
)
1735 set_display_type (0, view_listing
);
1736 else if (get_display_type (1) == view_info
)
1737 set_display_type (1, view_listing
);
1739 set_display_type (current_panel
== left_panel
? 1 : 0, view_info
);
1742 /* --------------------------------------------------------------------------------------------- */
1745 quick_cmd_no_menu (void)
1747 if (get_display_type (0) == view_quick
)
1748 set_display_type (0, view_listing
);
1749 else if (get_display_type (1) == view_quick
)
1750 set_display_type (1, view_listing
);
1752 set_display_type (current_panel
== left_panel
? 1 : 0, view_quick
);
1755 /* --------------------------------------------------------------------------------------------- */
1760 switch_to_listing (MENU_PANEL_IDX
);
1763 /* --------------------------------------------------------------------------------------------- */
1766 change_listing_cmd (void)
1770 char *user
, *status
;
1773 if (SELECTED_IS_PANEL
)
1774 p
= MENU_PANEL_IDX
== 0 ? left_panel
: right_panel
;
1776 list_type
= panel_listing_box (p
, &user
, &status
, &use_msformat
, MENU_PANEL_IDX
);
1777 if (list_type
!= -1)
1779 switch_to_listing (MENU_PANEL_IDX
);
1780 p
= MENU_PANEL_IDX
== 0 ? left_panel
: right_panel
;
1781 configure_panel_listing (p
, list_type
, use_msformat
, user
, status
);
1785 /* --------------------------------------------------------------------------------------------- */
1788 panel_tree_cmd (void)
1790 set_display_type (MENU_PANEL_IDX
, view_tree
);
1793 /* --------------------------------------------------------------------------------------------- */
1798 set_display_type (MENU_PANEL_IDX
, view_info
);
1801 /* --------------------------------------------------------------------------------------------- */
1804 quick_view_cmd (void)
1806 if ((WPanel
*) get_panel_widget (MENU_PANEL_IDX
) == current_panel
)
1808 set_display_type (MENU_PANEL_IDX
, view_quick
);
1811 /* --------------------------------------------------------------------------------------------- */
1814 toggle_listing_cmd (void)
1816 int current
= get_current_index ();
1817 WPanel
*p
= (WPanel
*) get_panel_widget (current
);
1819 set_basic_panel_listing_to (current
, (p
->list_type
+ 1) % LIST_TYPES
);
1822 /* --------------------------------------------------------------------------------------------- */
1828 if (SELECTED_IS_PANEL
)
1829 panel_change_encoding (MENU_PANEL
);
1833 /* --------------------------------------------------------------------------------------------- */