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
7 The Free Software Foundation, Inc.
9 This file is part of the Midnight Commander.
11 The Midnight Commander is free software: you can redistribute it
12 and/or modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation, either version 3 of the License,
14 or (at your option) any later version.
16 The Midnight Commander is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
26 * \brief Source: routines invoked by a function key
28 * They normally operate on the current panel.
37 #include <sys/types.h>
52 #include "lib/global.h"
54 #include "lib/tty/tty.h" /* LINES, tty_touch_screen() */
55 #include "lib/tty/key.h" /* ALT() macro */
56 #include "lib/tty/win.h" /* do_enter_ca_mode() */
57 #include "lib/mcconfig.h"
58 #include "lib/search.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 int select_flags
= SELECT_MATCH_CASE
| SELECT_SHELL_PATTERNS
;
103 /*** file scope macro definitions ****************************************************************/
109 /*** file scope type declarations ****************************************************************/
113 compare_quick
, compare_size_only
, compare_thourough
116 /*** file scope variables ************************************************************************/
118 #ifdef ENABLE_VFS_NET
119 static const char *machine_str
= N_("Enter machine name (F1 for details):");
120 #endif /* ENABLE_VFS_NET */
122 /*** file scope functions ************************************************************************/
123 /* --------------------------------------------------------------------------------------------- */
125 /** scan_for_file (panel, idx, direction)
128 * panel: pointer to the panel on which we operate
129 * idx: starting file.
130 * direction: 1, or -1
134 scan_for_file (WPanel
* panel
, int idx
, int direction
)
136 int i
= idx
+ direction
;
141 i
= panel
->count
- 1;
142 if (i
== panel
->count
)
144 if (!S_ISDIR (panel
->dir
.list
[i
].st
.st_mode
))
151 /* --------------------------------------------------------------------------------------------- */
153 * Run viewer (internal or external) on the currently selected file.
154 * If normal is TRUE, force internal viewer and raw mode (used for F13).
157 do_view_cmd (gboolean normal
)
159 /* Directories are viewed by changing to them */
160 if (S_ISDIR (selection (current_panel
)->st
.st_mode
) || link_isdir (selection (current_panel
)))
162 vfs_path_t
*fname_vpath
;
164 if (confirm_view_dir
&& (current_panel
->marked
|| current_panel
->dirs_marked
))
167 (_("Confirmation"), _("Files tagged, want to cd?"), D_NORMAL
, 2,
168 _("&Yes"), _("&No")) != 0)
173 fname_vpath
= vfs_path_from_str (selection (current_panel
)->fname
);
174 if (!do_cd (fname_vpath
, cd_exact
))
175 message (D_ERROR
, MSG_ERROR
, _("Cannot change directory"));
176 vfs_path_free (fname_vpath
);
182 file_idx
= current_panel
->selected
;
186 vfs_path_t
*filename_vpath
;
189 filename_vpath
= vfs_path_from_str (current_panel
->dir
.list
[file_idx
].fname
);
190 dir
= view_file (filename_vpath
, normal
, use_internal_view
);
191 vfs_path_free (filename_vpath
);
194 file_idx
= scan_for_file (current_panel
, file_idx
, dir
);
201 /* --------------------------------------------------------------------------------------------- */
204 do_edit (const vfs_path_t
* what_vpath
)
206 do_edit_at_line (what_vpath
, use_internal_edit
, 0);
209 /* --------------------------------------------------------------------------------------------- */
212 set_panel_filter_to (WPanel
* p
, char *allocated_filter_string
)
217 if (!(allocated_filter_string
[0] == '*' && allocated_filter_string
[1] == 0))
218 p
->filter
= allocated_filter_string
;
220 g_free (allocated_filter_string
);
224 /* --------------------------------------------------------------------------------------------- */
225 /** Set a given panel filter expression */
228 set_panel_filter (WPanel
* p
)
233 x
= p
->filter
? p
->filter
: easy_patterns
? "*" : ".";
235 reg_exp
= input_dialog_help (_("Filter"),
236 _("Set expression for filtering filenames"),
237 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER
, x
, FALSE
);
240 set_panel_filter_to (p
, reg_exp
);
243 /* --------------------------------------------------------------------------------------------- */
246 select_unselect_cmd (const char *title
, const char *history_name
, gboolean do_select
)
252 int files_only
= (select_flags
& SELECT_FILES_ONLY
) != 0;
253 int case_sens
= (select_flags
& SELECT_MATCH_CASE
) != 0;
254 int shell_patterns
= (select_flags
& SELECT_SHELL_PATTERNS
) != 0;
260 QuickWidget quick_widgets
[] = {
261 QUICK_CHECKBOX (3, DX
, DY
- 3, DY
, N_("&Using shell patterns"), &shell_patterns
),
262 QUICK_CHECKBOX (DX
/ 2 + 1, DX
, DY
- 4, DY
, N_("&Case sensitive"), &case_sens
),
263 QUICK_CHECKBOX (3, DX
, DY
- 4, DY
, N_("&Files only"), &files_only
),
264 QUICK_INPUT (3, DX
, DY
- 5, DY
, INPUT_LAST_TEXT
, DX
- 6, 0, history_name
, ®_exp
),
268 QuickDialog quick_dlg
= {
269 DX
, DY
, -1, -1, title
,
270 "[Select/Unselect Files]", quick_widgets
, NULL
, FALSE
273 if (quick_dialog (&quick_dlg
) == B_CANCEL
)
283 search
= mc_search_new (reg_exp
, -1);
284 search
->search_type
= (shell_patterns
!= 0) ? MC_SEARCH_T_GLOB
: MC_SEARCH_T_REGEX
;
285 search
->is_entire_line
= TRUE
;
286 search
->is_case_sensitive
= case_sens
!= 0;
288 for (i
= 0; i
< current_panel
->count
; i
++)
290 if (strcmp (current_panel
->dir
.list
[i
].fname
, "..") == 0)
292 if (S_ISDIR (current_panel
->dir
.list
[i
].st
.st_mode
) && files_only
!= 0)
295 if (mc_search_run (search
, current_panel
->dir
.list
[i
].fname
,
296 0, current_panel
->dir
.list
[i
].fnamelen
, NULL
))
297 do_file_mark (current_panel
, i
, do_select
);
300 mc_search_free (search
);
306 select_flags
|= SELECT_MATCH_CASE
;
308 select_flags
|= SELECT_FILES_ONLY
;
309 if (shell_patterns
!= 0)
310 select_flags
|= SELECT_SHELL_PATTERNS
;
313 /* --------------------------------------------------------------------------------------------- */
316 compare_files (const vfs_path_t
* vpath1
, const vfs_path_t
* vpath2
, off_t size
)
320 int result
= -1; /* Different by default */
325 name
= vfs_path_to_str (vpath1
);
326 file1
= open (name
, O_RDONLY
);
330 name
= vfs_path_to_str (vpath2
);
331 file2
= open (name
, O_RDONLY
);
338 data1
= mmap (0, size
, PROT_READ
, MAP_FILE
| MAP_PRIVATE
, file1
, 0);
339 if (data1
!= (char *) -1)
341 data2
= mmap (0, size
, PROT_READ
, MAP_FILE
| MAP_PRIVATE
, file2
, 0);
342 if (data2
!= (char *) -1)
345 result
= memcmp (data1
, data2
, size
);
346 munmap (data2
, size
);
348 munmap (data1
, size
);
351 /* Don't have mmap() :( Even more ugly :) */
352 char buf1
[BUFSIZ
], buf2
[BUFSIZ
];
357 while ((n1
= read (file1
, buf1
, BUFSIZ
)) == -1 && errno
== EINTR
);
358 while ((n2
= read (file2
, buf2
, BUFSIZ
)) == -1 && errno
== EINTR
);
360 while (n1
== n2
&& n1
== BUFSIZ
&& !memcmp (buf1
, buf2
, BUFSIZ
));
361 result
= (n1
!= n2
) || memcmp (buf1
, buf2
, n1
);
362 #endif /* !HAVE_MMAP */
370 /* --------------------------------------------------------------------------------------------- */
373 compare_dir (WPanel
* panel
, WPanel
* other
, enum CompareMode mode
)
377 /* No marks by default */
380 panel
->dirs_marked
= 0;
382 /* Handle all files in the panel */
383 for (i
= 0; i
< panel
->count
; i
++)
385 file_entry
*source
= &panel
->dir
.list
[i
];
387 /* Default: unmarked */
388 file_mark (panel
, i
, 0);
390 /* Skip directories */
391 if (S_ISDIR (source
->st
.st_mode
))
394 /* Search the corresponding entry from the other panel */
395 for (j
= 0; j
< other
->count
; j
++)
397 if (strcmp (source
->fname
, other
->dir
.list
[j
].fname
) == 0)
400 if (j
>= other
->count
)
401 /* Not found -> mark */
402 do_file_mark (panel
, i
, 1);
406 file_entry
*target
= &other
->dir
.list
[j
];
408 if (mode
!= compare_size_only
)
410 /* Older version is not marked */
411 if (source
->st
.st_mtime
< target
->st
.st_mtime
)
415 /* Newer version with different size is marked */
416 if (source
->st
.st_size
!= target
->st
.st_size
)
418 do_file_mark (panel
, i
, 1);
422 if (mode
== compare_size_only
)
425 if (mode
== compare_quick
)
427 /* Thorough compare off, compare only time stamps */
428 /* Mark newer version, don't mark version with the same date */
429 if (source
->st
.st_mtime
> target
->st
.st_mtime
)
431 do_file_mark (panel
, i
, 1);
436 /* Thorough compare on, do byte-by-byte comparison */
438 vfs_path_t
*src_name
, *dst_name
;
440 src_name
= vfs_path_append_new (panel
->cwd_vpath
, source
->fname
, NULL
);
441 dst_name
= vfs_path_append_new (other
->cwd_vpath
, target
->fname
, NULL
);
442 if (compare_files (src_name
, dst_name
, source
->st
.st_size
))
443 do_file_mark (panel
, i
, 1);
444 vfs_path_free (src_name
);
445 vfs_path_free (dst_name
);
451 /* --------------------------------------------------------------------------------------------- */
454 do_link (link_type_t link_type
, const char *fname
)
456 char *dest
= NULL
, *src
= NULL
;
457 vfs_path_t
*fname_vpath
, *dest_vpath
= NULL
;
459 fname_vpath
= vfs_path_from_str (fname
);
460 if (link_type
== LINK_HARDLINK
)
462 src
= g_strdup_printf (_("Link %s to:"), str_trunc (fname
, 46));
463 dest
= input_expand_dialog (_("Link"), src
, MC_HISTORY_FM_LINK
, "");
467 dest_vpath
= vfs_path_from_str (dest
);
468 if (-1 == mc_link (fname_vpath
, dest_vpath
))
469 message (D_ERROR
, MSG_ERROR
, _("link: %s"), unix_error_string (errno
));
475 /* suggest the full path for symlink, and either the full or
476 relative path to the file it points to */
477 s
= vfs_path_append_new (current_panel
->cwd_vpath
, fname
, NULL
);
479 if (get_other_type () == view_listing
)
480 d
= vfs_path_append_new (other_panel
->cwd_vpath
, fname
, NULL
);
482 d
= vfs_path_from_str (fname
);
484 if (link_type
== LINK_SYMLINK_RELATIVE
)
488 s_str
= diff_two_paths (other_panel
->cwd_vpath
, s
);
490 s
= vfs_path_from_str_flags (s_str
, VPF_NO_CANON
);
494 symlink_dialog (s
, d
, &dest
, &src
);
498 if (!dest
|| !*dest
|| !src
|| !*src
)
502 dest_vpath
= vfs_path_from_str_flags (dest
, VPF_NO_CANON
);
504 s
= vfs_path_from_str (src
);
505 if (mc_symlink (dest_vpath
, s
) == -1)
506 message (D_ERROR
, MSG_ERROR
, _("symlink: %s"), unix_error_string (errno
));
510 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
514 vfs_path_free (fname_vpath
);
515 vfs_path_free (dest_vpath
);
520 /* --------------------------------------------------------------------------------------------- */
522 #if defined(ENABLE_VFS_UNDELFS) || defined(ENABLE_VFS_NET)
524 nice_cd (const char *text
, const char *xtext
, const char *help
,
525 const char *history_name
, const char *prefix
, int to_home
, gboolean strip_password
)
530 if (!SELECTED_IS_PANEL
)
533 machine
= input_dialog_help (text
, xtext
, help
, history_name
, "", strip_password
);
537 to_home
= 0; /* FIXME: how to solve going to home nicely? /~/ is
538 ugly as hell and leads to problems in vfs layer */
540 if (strncmp (prefix
, machine
, strlen (prefix
)) == 0)
541 cd_path
= g_strconcat (machine
, to_home
? "/~/" : (char *) NULL
, (char *) NULL
);
543 cd_path
= g_strconcat (prefix
, machine
, to_home
? "/~/" : (char *) NULL
, (char *) NULL
);
547 if (*cd_path
!= PATH_SEP
)
551 cd_path
= g_strconcat (PATH_SEP_STR
, tmp
, (char *) NULL
);
556 vfs_path_t
*cd_vpath
;
558 cd_vpath
= vfs_path_from_str_flags (cd_path
, VPF_NO_CANON
);
559 if (!do_panel_cd (MENU_PANEL
, cd_vpath
, cd_parse_command
))
560 message (D_ERROR
, MSG_ERROR
, _("Cannot chdir to \"%s\""), cd_path
);
561 vfs_path_free (cd_vpath
);
565 #endif /* ENABLE_VFS_UNDELFS || ENABLE_VFS_NET */
567 /* --------------------------------------------------------------------------------------------- */
570 configure_panel_listing (WPanel
* p
, int list_type
, int use_msformat
, char *user
, char *status
)
572 p
->user_mini_status
= use_msformat
;
573 p
->list_type
= list_type
;
575 if (list_type
== list_user
|| use_msformat
)
577 g_free (p
->user_format
);
578 p
->user_format
= user
;
580 g_free (p
->user_status_format
[list_type
]);
581 p
->user_status_format
[list_type
] = status
;
583 set_panel_formats (p
);
591 set_panel_formats (p
);
595 /* --------------------------------------------------------------------------------------------- */
598 switch_to_listing (int panel_index
)
600 if (get_display_type (panel_index
) != view_listing
)
601 set_display_type (panel_index
, view_listing
);
606 p
= (WPanel
*) get_panel_widget (panel_index
);
609 p
->is_panelized
= FALSE
;
615 /* --------------------------------------------------------------------------------------------- */
616 /** Handle the tree internal listing modes switching */
619 set_basic_panel_listing_to (int panel_index
, int listing_mode
)
621 WPanel
*p
= (WPanel
*) get_panel_widget (panel_index
);
624 switch_to_listing (panel_index
);
625 p
->list_type
= listing_mode
;
627 ok
= set_panel_formats (p
) == 0;
635 /* --------------------------------------------------------------------------------------------- */
636 /*** public functions ****************************************************************************/
637 /* --------------------------------------------------------------------------------------------- */
640 view_file_at_line (const vfs_path_t
* filename_vpath
, int plain_view
, int internal
, int start_line
)
642 static const char *viewer
= NULL
;
647 int changed_hex_mode
= 0;
648 int changed_nroff_flag
= 0;
649 int changed_magic_flag
= 0;
651 mcview_altered_hex_mode
= 0;
652 mcview_altered_nroff_flag
= 0;
653 mcview_altered_magic_flag
= 0;
654 if (mcview_default_hex_mode
)
655 changed_hex_mode
= 1;
656 if (mcview_default_nroff_flag
)
657 changed_nroff_flag
= 1;
658 if (mcview_default_magic_flag
)
659 changed_magic_flag
= 1;
660 mcview_default_hex_mode
= 0;
661 mcview_default_nroff_flag
= 0;
662 mcview_default_magic_flag
= 0;
664 switch (mcview_viewer (NULL
, filename_vpath
, start_line
))
666 case MCVIEW_WANT_NEXT
:
669 case MCVIEW_WANT_PREV
:
676 if (changed_hex_mode
&& !mcview_altered_hex_mode
)
677 mcview_default_hex_mode
= 1;
678 if (changed_nroff_flag
&& !mcview_altered_nroff_flag
)
679 mcview_default_nroff_flag
= 1;
680 if (changed_magic_flag
&& !mcview_altered_magic_flag
)
681 mcview_default_magic_flag
= 1;
683 dialog_switch_process_pending ();
687 char view_entry
[BUF_TINY
];
690 g_snprintf (view_entry
, sizeof (view_entry
), "View:%d", start_line
);
692 strcpy (view_entry
, "View");
694 if (regex_command (filename_vpath
, view_entry
, &move_dir
) == 0)
696 switch (mcview_viewer (NULL
, filename_vpath
, start_line
))
698 case MCVIEW_WANT_NEXT
:
701 case MCVIEW_WANT_PREV
:
708 dialog_switch_process_pending ();
715 viewer
= getenv ("VIEWER");
717 viewer
= getenv ("PAGER");
722 execute_with_vfs_arg (viewer
, filename_vpath
);
728 /* --------------------------------------------------------------------------------------------- */
729 /** view_file (filename, plain_view, internal)
732 * filename_vpath: The file name to view
733 * plain_view: If set does not do any fancy pre-processing (no filtering) and
734 * always invokes the internal viewer.
735 * internal: If set uses the internal viewer, otherwise an external viewer.
739 view_file (const vfs_path_t
* filename_vpath
, int plain_view
, int internal
)
741 return view_file_at_line (filename_vpath
, plain_view
, internal
, 0);
745 /* --------------------------------------------------------------------------------------------- */
746 /** Run user's preferred viewer on the currently selected file */
754 /* --------------------------------------------------------------------------------------------- */
755 /** Ask for file and run user's preferred viewer on it */
764 input_expand_dialog (_("View file"), _("Filename:"),
765 MC_HISTORY_FM_VIEW_FILE
, selection (current_panel
)->fname
);
769 vpath
= vfs_path_from_str (filename
);
771 view_file (vpath
, 0, use_internal_view
);
772 vfs_path_free (vpath
);
775 /* --------------------------------------------------------------------------------------------- */
776 /** Run plain internal viewer on the currently selected file */
783 /* --------------------------------------------------------------------------------------------- */
786 view_filtered_cmd (void)
789 const char *initial_command
;
791 if (cmdline
->buffer
[0] == '\0')
792 initial_command
= selection (current_panel
)->fname
;
794 initial_command
= cmdline
->buffer
;
797 input_dialog (_("Filtered view"),
798 _("Filter command and arguments:"),
799 MC_HISTORY_FM_FILTERED_VIEW
, initial_command
);
803 mcview_viewer (command
, NULL
, 0);
805 dialog_switch_process_pending ();
809 /* --------------------------------------------------------------------------------------------- */
812 do_edit_at_line (const vfs_path_t
* what_vpath
, gboolean internal
, int start_line
)
814 static const char *editor
= NULL
;
816 #ifdef USE_INTERNAL_EDIT
818 edit_file (what_vpath
, start_line
);
822 #endif /* USE_INTERNAL_EDIT */
826 editor
= getenv ("EDITOR");
828 editor
= get_default_editor ();
830 execute_with_vfs_arg (editor
, what_vpath
);
833 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
834 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
836 #ifdef USE_INTERNAL_EDIT
837 if (use_internal_edit
)
838 dialog_switch_process_pending ();
840 #endif /* USE_INTERNAL_EDIT */
844 /* --------------------------------------------------------------------------------------------- */
851 fname
= vfs_path_from_str (selection (current_panel
)->fname
);
852 if (regex_command (fname
, "Edit", NULL
) == 0)
854 vfs_path_free (fname
);
857 /* --------------------------------------------------------------------------------------------- */
859 #ifdef USE_INTERNAL_EDIT
861 edit_cmd_force_internal (void)
865 fname
= vfs_path_from_str (selection (current_panel
)->fname
);
866 if (regex_command (fname
, "Edit", NULL
) == 0)
867 do_edit_at_line (fname
, TRUE
, 0);
868 vfs_path_free (fname
);
872 /* --------------------------------------------------------------------------------------------- */
878 mc_global
.source_codepage
= default_source_codepage
;
883 /* --------------------------------------------------------------------------------------------- */
884 /** Invoked by F5. Copy, default to the other panel. */
890 if (panel_operate (current_panel
, OP_COPY
, FALSE
))
892 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
897 /* --------------------------------------------------------------------------------------------- */
898 /** Invoked by F6. Move/rename, default to the other panel, ignore marks. */
904 if (panel_operate (current_panel
, OP_MOVE
, FALSE
))
906 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
911 /* --------------------------------------------------------------------------------------------- */
912 /** Invoked by F15. Copy, default to the same panel, ignore marks. */
915 copy_cmd_local (void)
918 if (panel_operate (current_panel
, OP_COPY
, TRUE
))
920 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
925 /* --------------------------------------------------------------------------------------------- */
926 /** Invoked by F16. Move/rename, default to the same panel. */
929 rename_cmd_local (void)
932 if (panel_operate (current_panel
, OP_MOVE
, TRUE
))
934 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
939 /* --------------------------------------------------------------------------------------------- */
945 const char *name
= "";
947 /* If 'on' then automatically fills name with current selected item name */
948 if (auto_fill_mkdir_name
&& strcmp (selection (current_panel
)->fname
, "..") != 0)
949 name
= selection (current_panel
)->fname
;
952 input_expand_dialog (_("Create a new Directory"),
953 _("Enter directory name:"), MC_HISTORY_FM_MKDIR
, name
);
955 if (dir
!= NULL
&& dir
!= '\0')
959 if (dir
[0] == '/' || dir
[0] == '~')
960 absdir
= vfs_path_from_str (dir
);
963 /* possible escaped '~' */
964 /* allow create directory with name '~' */
967 if (dir
[0] == '\\' && dir
[1] == '~')
970 absdir
= vfs_path_append_new (current_panel
->cwd_vpath
, tmpdir
, NULL
);
974 if (my_mkdir (absdir
, 0777) == 0)
976 update_panels (UP_OPTIMIZE
, dir
);
978 select_item (current_panel
);
982 message (D_ERROR
, MSG_ERROR
, "%s", unix_error_string (errno
));
984 vfs_path_free (absdir
);
989 /* --------------------------------------------------------------------------------------------- */
996 if (panel_operate (current_panel
, OP_DELETE
, FALSE
))
998 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
1003 /* --------------------------------------------------------------------------------------------- */
1004 /** Invoked by F18. Remove selected file, regardless of marked files. */
1007 delete_cmd_local (void)
1011 if (panel_operate (current_panel
, OP_DELETE
, TRUE
))
1013 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
1018 /* --------------------------------------------------------------------------------------------- */
1026 /* --------------------------------------------------------------------------------------------- */
1027 /** Invoked from the left/right menus */
1034 if (!SELECTED_IS_PANEL
)
1038 set_panel_filter (p
);
1041 /* --------------------------------------------------------------------------------------------- */
1046 panel_update_flags_t flag
= UP_ONLY_CURRENT
;
1048 if (get_current_type () == view_listing
&& get_other_type () == view_listing
&&
1049 vfs_path_cmp (current_panel
->cwd_vpath
, other_panel
->cwd_vpath
) == 0)
1052 update_panels (UP_RELOAD
| flag
, UP_KEEPSEL
);
1056 /* --------------------------------------------------------------------------------------------- */
1059 select_invert_cmd (void)
1064 for (i
= 0; i
< current_panel
->count
; i
++)
1066 file
= ¤t_panel
->dir
.list
[i
];
1067 if (!panels_options
.reverse_files_only
|| !S_ISDIR (file
->st
.st_mode
))
1068 do_file_mark (current_panel
, i
, !file
->f
.marked
);
1072 /* --------------------------------------------------------------------------------------------- */
1077 select_unselect_cmd (_("Select"), ":select_cmd: Select ", TRUE
);
1080 /* --------------------------------------------------------------------------------------------- */
1085 select_unselect_cmd (_("Unselect"), ":unselect_cmd: Unselect ", FALSE
);
1088 /* --------------------------------------------------------------------------------------------- */
1093 vfs_path_t
*buffer_vpath
;
1094 vfs_path_t
*extdir_vpath
;
1098 if (geteuid () == 0)
1100 dir
= query_dialog (_("Extension file edit"),
1101 _("Which extension file you want to edit?"), D_NORMAL
, 2,
1102 _("&User"), _("&System Wide"));
1104 extdir_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_LIB_EXT
, NULL
);
1108 buffer_vpath
= mc_config_get_full_vpath (MC_FILEBIND_FILE
);
1109 check_for_default (extdir_vpath
, buffer_vpath
);
1110 do_edit (buffer_vpath
);
1111 vfs_path_free (buffer_vpath
);
1115 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath
)))
1117 vfs_path_free (extdir_vpath
);
1118 extdir_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, MC_LIB_EXT
, NULL
);
1120 do_edit (extdir_vpath
);
1122 vfs_path_free (extdir_vpath
);
1123 flush_extension_file ();
1126 /* --------------------------------------------------------------------------------------------- */
1127 /** edit file menu for mc */
1130 edit_mc_menu_cmd (void)
1132 vfs_path_t
*buffer_vpath
;
1133 vfs_path_t
*menufile_vpath
;
1136 dir
= query_dialog (_("Menu edit"),
1137 _("Which menu file do you want to edit?"),
1138 D_NORMAL
, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
1140 menufile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_GLOBAL_MENU
, NULL
);
1142 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath
)))
1144 vfs_path_free (menufile_vpath
);
1145 menufile_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, MC_GLOBAL_MENU
, NULL
);
1151 buffer_vpath
= vfs_path_from_str (MC_LOCAL_MENU
);
1152 check_for_default (menufile_vpath
, buffer_vpath
);
1153 chmod (vfs_path_get_last_path_str (buffer_vpath
), 0600);
1157 buffer_vpath
= mc_config_get_full_vpath (MC_USERMENU_FILE
);
1158 check_for_default (menufile_vpath
, buffer_vpath
);
1162 buffer_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_GLOBAL_MENU
, NULL
);
1163 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath
)))
1165 vfs_path_free (buffer_vpath
);
1166 buffer_vpath
= vfs_path_build_filename (mc_global
.share_data_dir
, MC_GLOBAL_MENU
, NULL
);
1171 vfs_path_free (menufile_vpath
);
1175 do_edit (buffer_vpath
);
1177 vfs_path_free (buffer_vpath
);
1178 vfs_path_free (menufile_vpath
);
1181 /* --------------------------------------------------------------------------------------------- */
1186 vfs_path_t
*buffer_vpath
= NULL
;
1187 vfs_path_t
*fhlfile_vpath
= NULL
;
1192 if (geteuid () == 0)
1194 dir
= query_dialog (_("Highlighting groups file edit"),
1195 _("Which highlighting file you want to edit?"), D_NORMAL
, 2,
1196 _("&User"), _("&System Wide"));
1198 fhlfile_vpath
= vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_FHL_INI_FILE
, NULL
);
1202 buffer_vpath
= mc_config_get_full_vpath (MC_FHL_INI_FILE
);
1203 check_for_default (fhlfile_vpath
, buffer_vpath
);
1204 do_edit (buffer_vpath
);
1205 vfs_path_free (buffer_vpath
);
1209 if (!exist_file (vfs_path_get_last_path_str (fhlfile_vpath
)))
1211 vfs_path_free (fhlfile_vpath
);
1213 vfs_path_build_filename (mc_global
.sysconfig_dir
, MC_FHL_INI_FILE
, NULL
);
1215 do_edit (fhlfile_vpath
);
1217 vfs_path_free (fhlfile_vpath
);
1219 /* refresh highlighting rules */
1220 mc_fhl_free (&mc_filehighlight
);
1221 mc_filehighlight
= mc_fhl_new (TRUE
);
1224 /* --------------------------------------------------------------------------------------------- */
1231 target
= hotlist_show (LIST_HOTLIST
);
1235 if (get_current_type () == view_tree
)
1236 tree_chdir (the_tree
, target
);
1239 vfs_path_t
*deprecated_vpath
;
1240 char *cmd
, *normalized_target
;
1242 deprecated_vpath
= vfs_path_from_str_flags (target
, VPF_USE_DEPRECATED_PARSER
);
1243 normalized_target
= vfs_path_to_str (deprecated_vpath
);
1244 cmd
= g_strconcat ("cd ", normalized_target
, (char *) NULL
);
1245 g_free (normalized_target
);
1246 vfs_path_free (deprecated_vpath
);
1248 do_cd_command (cmd
);
1259 vfs_path_t
*target_vpath
;
1261 target
= hotlist_show (LIST_VFSLIST
);
1265 target_vpath
= vfs_path_from_str (target
);
1266 if (!do_cd (target_vpath
, cd_exact
))
1267 message (D_ERROR
, MSG_ERROR
, _("Cannot change directory"));
1268 vfs_path_free (target_vpath
);
1271 #endif /* ENABLE_VFS */
1273 /* --------------------------------------------------------------------------------------------- */
1276 compare_dirs_cmd (void)
1279 enum CompareMode thorough_flag
;
1282 query_dialog (_("Compare directories"),
1283 _("Select compare method:"), D_NORMAL
, 4,
1284 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
1286 if (choice
< 0 || choice
> 2)
1289 thorough_flag
= choice
;
1291 if (get_current_type () == view_listing
&& get_other_type () == view_listing
)
1293 compare_dir (current_panel
, other_panel
, thorough_flag
);
1294 compare_dir (other_panel
, current_panel
, thorough_flag
);
1298 message (D_ERROR
, MSG_ERROR
,
1299 _("Both panels should be in the listing mode\nto use this command"));
1303 /* --------------------------------------------------------------------------------------------- */
1305 #ifdef USE_DIFF_VIEW
1307 diff_view_cmd (void)
1309 /* both panels must be in the list mode */
1310 if (get_current_type () != view_listing
|| get_other_type () != view_listing
)
1313 if (get_current_index () == 0)
1314 dview_diff_cmd (current_panel
, other_panel
);
1316 dview_diff_cmd (other_panel
, current_panel
);
1318 if (mc_global
.mc_run_mode
== MC_RUN_FULL
)
1319 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
1321 dialog_switch_process_pending ();
1325 /* --------------------------------------------------------------------------------------------- */
1331 tty_touch_screen ();
1335 /* --------------------------------------------------------------------------------------------- */
1338 view_other_cmd (void)
1340 static int message_flag
= TRUE
;
1342 if (!mc_global
.tty
.xterm_flag
&& mc_global
.tty
.console_flag
== '\0'
1343 && !mc_global
.tty
.use_subshell
&& !output_starts_shell
)
1346 message (D_ERROR
, MSG_ERROR
,
1347 _("Not an xterm or Linux console;\nthe panels cannot be toggled."));
1348 message_flag
= FALSE
;
1356 /* --------------------------------------------------------------------------------------------- */
1359 link_cmd (link_type_t link_type
)
1361 char *filename
= selection (current_panel
)->fname
;
1363 if (filename
!= NULL
)
1364 do_link (link_type
, filename
);
1367 /* --------------------------------------------------------------------------------------------- */
1370 edit_symlink_cmd (void)
1372 if (S_ISLNK (selection (current_panel
)->st
.st_mode
))
1374 char buffer
[MC_MAXPATHLEN
];
1378 vfs_path_t
*p_vpath
;
1380 p
= selection (current_panel
)->fname
;
1381 p_vpath
= vfs_path_from_str (p
);
1383 q
= g_strdup_printf (_("Symlink `%s\' points to:"), str_trunc (p
, 32));
1385 i
= readlink (p
, buffer
, MC_MAXPATHLEN
- 1);
1389 dest
= input_expand_dialog (_("Edit symlink"), q
, MC_HISTORY_FM_EDIT_LINK
, buffer
);
1392 if (*dest
&& strcmp (buffer
, dest
))
1395 if (mc_unlink (p_vpath
) == -1)
1397 message (D_ERROR
, MSG_ERROR
, _("edit symlink, unable to remove %s: %s"),
1398 p
, unix_error_string (errno
));
1402 vfs_path_t
*dest_vpath
;
1404 dest_vpath
= vfs_path_from_str_flags (dest
, VPF_NO_CANON
);
1405 if (mc_symlink (dest_vpath
, p_vpath
) == -1)
1406 message (D_ERROR
, MSG_ERROR
, _("edit symlink: %s"),
1407 unix_error_string (errno
));
1408 vfs_path_free (dest_vpath
);
1410 update_panels (UP_OPTIMIZE
, UP_KEEPSEL
);
1417 vfs_path_free (p_vpath
);
1421 message (D_ERROR
, MSG_ERROR
, _("`%s' is not a symbolic link"),
1422 selection (current_panel
)->fname
);
1426 /* --------------------------------------------------------------------------------------------- */
1431 ev_help_t event_data
= { NULL
, NULL
};
1433 if (current_panel
->searching
)
1434 event_data
.node
= "[Quick search]";
1436 event_data
.node
= "[main]";
1438 mc_event_raise (MCEVENT_GROUP_CORE
, "help", &event_data
);
1441 /* --------------------------------------------------------------------------------------------- */
1444 user_file_menu_cmd (void)
1446 (void) user_menu_cmd (NULL
, NULL
, -1);
1449 /* --------------------------------------------------------------------------------------------- */
1451 * Return a random hint. If force is not 0, ignore the timeout.
1455 get_random_hint (int force
)
1457 char *data
, *result
= NULL
, *eol
;
1460 static int last_sec
;
1461 static struct timeval tv
;
1464 /* Do not change hints more often than one minute */
1465 gettimeofday (&tv
, NULL
);
1466 if (!force
&& !(tv
.tv_sec
> last_sec
+ 60))
1467 return g_strdup ("");
1468 last_sec
= tv
.tv_sec
;
1470 data
= load_mc_home_file (mc_global
.share_data_dir
, MC_HINT
, NULL
);
1474 /* get a random entry */
1476 len
= strlen (data
);
1477 start
= rand () % len
;
1479 for (; start
!= 0; start
--)
1480 if (data
[start
] == '\n')
1486 eol
= strchr (data
+ start
, '\n');
1490 /* hint files are stored in utf-8 */
1491 /* try convert hint file from utf-8 to terminal encoding */
1492 conv
= str_crt_conv_from ("UTF-8");
1493 if (conv
!= INVALID_CONV
)
1497 buffer
= g_string_new ("");
1498 if (str_convert (conv
, &data
[start
], buffer
) != ESTR_FAILURE
)
1499 result
= g_string_free (buffer
, FALSE
);
1501 g_string_free (buffer
, TRUE
);
1502 str_close_conv (conv
);
1509 /* --------------------------------------------------------------------------------------------- */
1511 #ifdef ENABLE_VFS_FTP
1515 nice_cd (_("FTP to machine"), _(machine_str
),
1516 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "ftp://", 1, TRUE
);
1518 #endif /* ENABLE_VFS_FTP */
1520 /* --------------------------------------------------------------------------------------------- */
1522 #ifdef ENABLE_VFS_FISH
1526 nice_cd (_("Shell link to machine"), _(machine_str
),
1527 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1530 #endif /* ENABLE_VFS_FISH */
1532 /* --------------------------------------------------------------------------------------------- */
1534 #ifdef ENABLE_VFS_SMB
1538 nice_cd (_("SMB link to machine"), _(machine_str
),
1539 "[SMB File System]", ":smblink_cmd: SMB link to machine ", "smb://", 0, TRUE
);
1541 #endif /* ENABLE_VFS_SMB */
1543 /* --------------------------------------------------------------------------------------------- */
1545 #ifdef ENABLE_VFS_UNDELFS
1549 nice_cd (_("Undelete files on an ext2 file system"),
1550 _("Enter device (without /dev/) to undelete\nfiles on: (F1 for details)"),
1551 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "undel://", 0, FALSE
);
1553 #endif /* ENABLE_VFS_UNDELFS */
1555 /* --------------------------------------------------------------------------------------------- */
1560 char *p
= cd_dialog ();
1564 char *q
= g_strconcat ("cd ", p
, (char *) NULL
);
1572 /* --------------------------------------------------------------------------------------------- */
1574 \brief calculate dirs sizes
1576 calculate dirs sizes and resort panel:
1577 dirs_selected = show size for selected dirs,
1578 otherwise = show size for dir under cursor:
1579 dir under cursor ".." = show size for all dirs,
1580 otherwise = show size for dir under cursor
1584 smart_dirsize_cmd (void)
1586 WPanel
*panel
= current_panel
;
1589 entry
= &(panel
->dir
.list
[panel
->selected
]);
1590 if ((S_ISDIR (entry
->st
.st_mode
) && (strcmp (entry
->fname
, "..") == 0)) || panel
->dirs_marked
)
1593 single_dirsize_cmd ();
1596 /* --------------------------------------------------------------------------------------------- */
1599 single_dirsize_cmd (void)
1601 WPanel
*panel
= current_panel
;
1604 entry
= &(panel
->dir
.list
[panel
->selected
]);
1605 if (S_ISDIR (entry
->st
.st_mode
) && strcmp (entry
->fname
, "..") != 0)
1608 uintmax_t total
= 0;
1609 ComputeDirSizeUI
*ui
;
1612 ui
= compute_dir_size_create_ui ();
1613 p
= vfs_path_from_str (entry
->fname
);
1615 if (compute_dir_size (p
, ui
, compute_dir_size_update_ui
, &marked
, &total
, TRUE
) ==
1618 entry
->st
.st_size
= (off_t
) total
;
1619 entry
->f
.dir_size_computed
= 1;
1623 compute_dir_size_destroy_ui (ui
);
1626 if (panels_options
.mark_moves_down
)
1627 send_message ((Widget
*) panel
, WIDGET_COMMAND
, CK_Down
);
1629 recalculate_panel_summary (panel
);
1631 if (current_panel
->sort_info
.sort_field
->sort_routine
== (sortfn
*) sort_size
)
1632 panel_re_sort (panel
);
1637 /* --------------------------------------------------------------------------------------------- */
1642 WPanel
*panel
= current_panel
;
1644 ComputeDirSizeUI
*ui
;
1646 ui
= compute_dir_size_create_ui ();
1648 for (i
= 0; i
< panel
->count
; i
++)
1649 if (S_ISDIR (panel
->dir
.list
[i
].st
.st_mode
)
1650 && ((panel
->dirs_marked
&& panel
->dir
.list
[i
].f
.marked
)
1651 || !panel
->dirs_marked
) && strcmp (panel
->dir
.list
[i
].fname
, "..") != 0)
1655 uintmax_t total
= 0;
1658 p
= vfs_path_from_str (panel
->dir
.list
[i
].fname
);
1659 ok
= compute_dir_size (p
, ui
, compute_dir_size_update_ui
, &marked
, &total
,
1666 panel
->dir
.list
[i
].st
.st_size
= (off_t
) total
;
1667 panel
->dir
.list
[i
].f
.dir_size_computed
= 1;
1670 compute_dir_size_destroy_ui (ui
);
1672 recalculate_panel_summary (panel
);
1674 if (current_panel
->sort_info
.sort_field
->sort_routine
== (sortfn
*) sort_size
)
1675 panel_re_sort (panel
);
1680 /* --------------------------------------------------------------------------------------------- */
1683 save_setup_cmd (void)
1688 vpath
= mc_config_get_full_vpath (MC_CONFIG_FILE
);
1689 path
= vfs_path_to_str_flags (vpath
, 0, VPF_STRIP_HOME
);
1690 vfs_path_free (vpath
);
1692 if (save_setup (TRUE
, TRUE
))
1693 message (D_NORMAL
, _("Setup"), _("Setup saved to %s"), path
);
1695 message (D_ERROR
, _("Setup"), _("Unable to save setup to %s"), path
);
1699 /* --------------------------------------------------------------------------------------------- */
1702 info_cmd_no_menu (void)
1704 if (get_display_type (0) == view_info
)
1705 set_display_type (0, view_listing
);
1706 else if (get_display_type (1) == view_info
)
1707 set_display_type (1, view_listing
);
1709 set_display_type (current_panel
== left_panel
? 1 : 0, view_info
);
1712 /* --------------------------------------------------------------------------------------------- */
1715 quick_cmd_no_menu (void)
1717 if (get_display_type (0) == view_quick
)
1718 set_display_type (0, view_listing
);
1719 else if (get_display_type (1) == view_quick
)
1720 set_display_type (1, view_listing
);
1722 set_display_type (current_panel
== left_panel
? 1 : 0, view_quick
);
1725 /* --------------------------------------------------------------------------------------------- */
1730 switch_to_listing (MENU_PANEL_IDX
);
1733 /* --------------------------------------------------------------------------------------------- */
1736 change_listing_cmd (void)
1740 char *user
, *status
;
1743 if (get_display_type (MENU_PANEL_IDX
) == view_listing
)
1744 p
= MENU_PANEL_IDX
== 0 ? left_panel
: right_panel
;
1746 list_type
= display_box (p
, &user
, &status
, &use_msformat
, MENU_PANEL_IDX
);
1748 if (list_type
!= -1)
1750 switch_to_listing (MENU_PANEL_IDX
);
1751 p
= MENU_PANEL_IDX
== 0 ? left_panel
: right_panel
;
1752 configure_panel_listing (p
, list_type
, use_msformat
, user
, status
);
1756 /* --------------------------------------------------------------------------------------------- */
1759 panel_tree_cmd (void)
1761 set_display_type (MENU_PANEL_IDX
, view_tree
);
1764 /* --------------------------------------------------------------------------------------------- */
1769 set_display_type (MENU_PANEL_IDX
, view_info
);
1772 /* --------------------------------------------------------------------------------------------- */
1775 quick_view_cmd (void)
1777 if ((WPanel
*) get_panel_widget (MENU_PANEL_IDX
) == current_panel
)
1779 set_display_type (MENU_PANEL_IDX
, view_quick
);
1782 /* --------------------------------------------------------------------------------------------- */
1785 toggle_listing_cmd (void)
1787 int current
= get_current_index ();
1788 WPanel
*p
= (WPanel
*) get_panel_widget (current
);
1790 set_basic_panel_listing_to (current
, (p
->list_type
+ 1) % LIST_TYPES
);
1793 /* --------------------------------------------------------------------------------------------- */
1799 if (SELECTED_IS_PANEL
)
1800 panel_change_encoding (MENU_PANEL
);
1804 /* --------------------------------------------------------------------------------------------- */