Updated italian translation.
[midnight-commander.git] / src / cmd.c
blobba7b792fca5a53a3235d213a29db856cb113268c
1 /* Routines invoked by a function key
2 They normally operate on the current panel.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
7 This program is free software; you can redistribute it and/or modify
8 it under the terms of the GNU General Public License as published by
9 the Free Software Foundation; either version 2 of the License, or
10 (at your option) any later version.
12 This program is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 GNU General Public License for more details.
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
21 /** \file cmd.c
22 * \brief Source: routines invoked by a function key
24 * They normally operate on the current panel.
27 #include <config.h>
29 #include <errno.h>
30 #include <stdio.h>
31 #include <string.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #ifdef HAVE_MMAP
36 # include <sys/mman.h>
37 #endif
38 #ifdef USE_NETCODE
39 #include <netdb.h>
40 #endif
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <fcntl.h>
44 #include <pwd.h>
45 #include <grp.h>
46 #include <sys/time.h>
48 #include "lib/global.h"
50 #include "lib/tty/tty.h" /* LINES, tty_touch_screen() */
51 #include "lib/tty/key.h" /* ALT() macro */
52 #include "lib/tty/win.h" /* do_enter_ca_mode() */
53 #include "lib/mcconfig.h"
54 #include "lib/search.h"
55 #include "src/viewer/mcviewer.h"
56 #include "lib/filehighlight.h" /* MC_FHL_INI_FILE */
57 #include "lib/vfs/mc-vfs/vfs.h"
58 #include "lib/fileloc.h"
59 #include "lib/strutil.h"
61 #include "cmd.h" /* Our definitions */
62 #include "fileopctx.h"
63 #include "file.h" /* file operation routines */
64 #include "find.h" /* do_find() */
65 #include "hotlist.h" /* hotlist_cmd() */
66 #include "tree.h" /* tree_chdir() */
67 #include "subshell.h" /* use_subshell */
68 #include "consaver/cons.saver.h" /* console_flag */
69 #include "dialog.h" /* Widget */
70 #include "wtools.h" /* message() */
71 #include "main.h" /* change_panel() */
72 #include "panel.h" /* current_panel */
73 #include "help.h" /* interactive_display() */
74 #include "user.h" /* MC_GLOBAL_MENU */
75 #include "command.h" /* cmdline */
76 #include "layout.h" /* get_current_type() */
77 #include "ext.h" /* regex_command() */
78 #include "boxes.h" /* cd_dialog() */
79 #include "setup.h"
80 #include "execute.h" /* toggle_panels() */
81 #include "history.h"
82 #include "dir.h"
83 #include "cmddef.h" /* CK_InputHistoryShow */
85 #ifndef MAP_FILE
86 # define MAP_FILE 0
87 #endif
89 #ifdef USE_INTERNAL_EDIT
90 # include "src/editor/edit.h"
91 #endif
93 /* If set and you don't have subshell support,then C-o will give you a shell */
94 int output_starts_shell = 0;
96 /* If set, use the builtin editor */
97 int use_internal_edit = 1;
99 /* Automatically fills name with current selected item name on mkdir */
100 int auto_fill_mkdir_name = 1;
102 /* if set, only selection of files is inverted */
103 int reverse_files_only = 1;
105 /* selection flags */
106 typedef enum {
107 SELECT_FILES_ONLY = 1 << 0,
108 SELECT_MATCH_CASE = 1 << 1,
109 SELECT_SHELL_PATTERNS = 1 << 2
110 } select_flags_t;
112 int select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
115 view_file_at_line (const char *filename, int plain_view, int internal,
116 int start_line)
118 static const char *viewer = NULL;
119 int move_dir = 0;
122 if (plain_view) {
123 int changed_hex_mode = 0;
124 int changed_nroff_flag = 0;
125 int changed_magic_flag = 0;
127 mcview_altered_hex_mode = 0;
128 mcview_altered_nroff_flag = 0;
129 mcview_altered_magic_flag = 0;
130 if (mcview_default_hex_mode)
131 changed_hex_mode = 1;
132 if (mcview_default_nroff_flag)
133 changed_nroff_flag = 1;
134 if (mcview_default_magic_flag)
135 changed_magic_flag = 1;
136 mcview_default_hex_mode = 0;
137 mcview_default_nroff_flag = 0;
138 mcview_default_magic_flag = 0;
139 mcview_viewer (NULL, filename, &move_dir, start_line);
140 if (changed_hex_mode && !mcview_altered_hex_mode)
141 mcview_default_hex_mode = 1;
142 if (changed_nroff_flag && !mcview_altered_nroff_flag)
143 mcview_default_nroff_flag = 1;
144 if (changed_magic_flag && !mcview_altered_magic_flag)
145 mcview_default_magic_flag = 1;
146 repaint_screen ();
147 return move_dir;
149 if (internal) {
150 char view_entry[BUF_TINY];
152 if (start_line != 0)
153 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
154 start_line);
155 else
156 strcpy (view_entry, "View");
158 if (regex_command (filename, view_entry, &move_dir) == 0) {
159 mcview_viewer (NULL, filename, &move_dir, start_line);
160 repaint_screen ();
162 } else {
163 if (!viewer) {
164 viewer = getenv ("VIEWER");
165 if (!viewer)
166 viewer = getenv ("PAGER");
167 if (!viewer)
168 viewer = "view";
170 execute_with_vfs_arg (viewer, filename);
172 return move_dir;
175 /* view_file (filename, plain_view, internal)
177 * Inputs:
178 * filename: The file name to view
179 * plain_view: If set does not do any fancy pre-processing (no filtering) and
180 * always invokes the internal viewer.
181 * internal: If set uses the internal viewer, otherwise an external viewer.
184 view_file (const char *filename, int plain_view, int internal)
186 return view_file_at_line (filename, plain_view, internal, 0);
189 /* scan_for_file (panel, idx, direction)
191 * Inputs:
192 * panel: pointer to the panel on which we operate
193 * idx: starting file.
194 * direction: 1, or -1
196 static int scan_for_file (WPanel *panel, int idx, int direction)
198 int i = idx + direction;
200 while (i != idx){
201 if (i < 0)
202 i = panel->count - 1;
203 if (i == panel->count)
204 i = 0;
205 if (!S_ISDIR (panel->dir.list [i].st.st_mode))
206 return i;
207 i += direction;
209 return i;
213 * Run viewer (internal or external) on the currently selected file.
214 * If normal is 1, force internal viewer and raw mode (used for F13).
216 static void
217 do_view_cmd (int normal)
219 /* Directories are viewed by changing to them */
220 if (S_ISDIR (selection (current_panel)->st.st_mode)
221 || link_isdir (selection (current_panel))) {
222 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked)) {
223 if (query_dialog
224 (_(" Confirmation "), _("Files tagged, want to cd?"), D_NORMAL, 2,
225 _("&Yes"), _("&No")) != 0) {
226 return;
229 if (!do_cd (selection (current_panel)->fname, cd_exact))
230 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
231 } else {
232 int dir, file_idx;
233 char *filename;
235 file_idx = current_panel->selected;
236 while (1) {
237 filename = current_panel->dir.list[file_idx].fname;
239 dir = view_file (filename, normal, use_internal_view);
240 if (dir == 0)
241 break;
242 file_idx = scan_for_file (current_panel, file_idx, dir);
246 repaint_screen();
249 /* Run user's preferred viewer on the currently selected file */
250 void
251 view_cmd (void)
253 do_view_cmd (0);
256 /* Ask for file and run user's preferred viewer on it */
257 void
258 view_file_cmd (void)
260 char *filename;
262 filename =
263 input_expand_dialog (_(" View file "), _(" Filename:"),
264 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
265 if (!filename)
266 return;
268 view_file (filename, 0, use_internal_view);
269 g_free (filename);
272 /* Run plain internal viewer on the currently selected file */
273 void
274 view_simple_cmd (void)
276 do_view_cmd (1);
279 void
280 filtered_view_cmd (void)
282 char *command;
284 command =
285 input_dialog (_(" Filtered view "),
286 _(" Filter command and arguments:"),
287 MC_HISTORY_FM_FILTERED_VIEW,
288 selection (current_panel)->fname);
289 if (!command)
290 return;
292 mcview_viewer (command, "", NULL, 0);
294 g_free (command);
297 void
298 do_edit_at_line (const char *what, int start_line)
300 static const char *editor = NULL;
302 #ifdef USE_INTERNAL_EDIT
303 if (use_internal_edit)
304 edit_file (what, start_line);
305 else
306 #else
307 (void) start_line;
308 #endif /* USE_INTERNAL_EDIT */
310 if (editor == NULL) {
311 editor = getenv ("EDITOR");
312 if (editor == NULL)
313 editor = get_default_editor ();
315 execute_with_vfs_arg (editor, what);
317 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
318 repaint_screen ();
321 static void
322 do_edit (const char *what)
324 do_edit_at_line (what, 0);
327 void
328 edit_cmd (void)
330 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
331 do_edit (selection (current_panel)->fname);
334 void
335 edit_cmd_new (void)
337 #if HAVE_CHARSET
338 source_codepage = default_source_codepage;
339 #endif
340 do_edit (NULL);
343 /* Invoked by F5. Copy, default to the other panel. */
344 void
345 copy_cmd (void)
347 save_cwds_stat ();
348 if (panel_operate (current_panel, OP_COPY, 0)) {
349 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
350 repaint_screen ();
354 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
355 void rename_cmd (void)
357 save_cwds_stat ();
358 if (panel_operate (current_panel, OP_MOVE, 0)){
359 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
360 repaint_screen ();
364 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
365 void copy_cmd_local (void)
367 save_cwds_stat ();
368 if (panel_operate (current_panel, OP_COPY, 1)){
369 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
370 repaint_screen ();
374 /* Invoked by F16. Move/rename, default to the same panel. */
375 void rename_cmd_local (void)
377 save_cwds_stat ();
378 if (panel_operate (current_panel, OP_MOVE, 1)){
379 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
380 repaint_screen ();
384 void
385 mkdir_cmd (void)
387 char *dir, *absdir;
388 const char *name = "";
390 /* If 'on' then automatically fills name with current selected item name */
391 if (auto_fill_mkdir_name)
392 name = selection (current_panel)->fname;
394 dir =
395 input_expand_dialog (_("Create a new Directory"),
396 _(" Enter directory name:"),
397 MC_HISTORY_FM_MKDIR,
398 name);
400 if (!dir)
401 return;
403 if (dir[0] == '/' || dir[0] == '~')
404 absdir = g_strdup (dir);
405 else
406 absdir = concat_dir_and_file (current_panel->cwd, dir);
408 save_cwds_stat ();
409 if (my_mkdir (absdir, 0777) == 0) {
410 update_panels (UP_OPTIMIZE, dir);
411 repaint_screen ();
412 select_item (current_panel);
413 } else {
414 message (D_ERROR, MSG_ERROR, " %s ", unix_error_string (errno));
417 g_free (absdir);
418 g_free (dir);
421 void delete_cmd (void)
423 save_cwds_stat ();
425 if (panel_operate (current_panel, OP_DELETE, 0)){
426 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
427 repaint_screen ();
431 /* Invoked by F18. Remove selected file, regardless of marked files. */
432 void delete_cmd_local (void)
434 save_cwds_stat ();
436 if (panel_operate (current_panel, OP_DELETE, 1)){
437 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
438 repaint_screen ();
442 void find_cmd (void)
444 do_find ();
447 static void
448 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
450 g_free (p->filter);
451 p->filter = 0;
453 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
454 p->filter = allocated_filter_string;
455 else
456 g_free (allocated_filter_string);
457 reread_cmd ();
460 /* Set a given panel filter expression */
461 static void
462 set_panel_filter (WPanel *p)
464 char *reg_exp;
465 const char *x;
467 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
469 reg_exp = input_dialog_help (_(" Filter "),
470 _(" Set expression for filtering filenames"),
471 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
472 if (!reg_exp)
473 return;
474 set_panel_filter_to (p, reg_exp);
477 /* Invoked from the left/right menus */
478 void filter_cmd (void)
480 WPanel *p;
482 if (!SELECTED_IS_PANEL)
483 return;
485 p = MENU_PANEL;
486 set_panel_filter (p);
489 void reread_cmd (void)
491 int flag;
493 if (get_current_type () == view_listing &&
494 get_other_type () == view_listing)
495 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
496 else
497 flag = UP_ONLY_CURRENT;
499 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
500 repaint_screen ();
503 void
504 reverse_selection_cmd (void)
506 int i;
507 file_entry *file;
509 for (i = 0; i < current_panel->count; i++) {
510 file = &current_panel->dir.list [i];
511 if ((reverse_files_only == 0) || !S_ISDIR (file->st.st_mode))
512 do_file_mark (current_panel, i, !file->f.marked);
516 static void
517 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
519 /* dialog sizes */
520 const int DX = 50;
521 const int DY = 7;
523 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
524 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
525 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
527 char *reg_exp;
528 mc_search_t *search;
529 int i;
531 QuickWidget quick_widgets[] =
533 QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns),
534 QUICK_CHECKBOX (DX/2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens),
535 QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only),
536 QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, &reg_exp),
537 QUICK_END
540 QuickDialog quick_dlg =
542 DX, DY, -1, -1, title,
543 "[Select/Unselect Files]", quick_widgets, FALSE
546 if (quick_dialog (&quick_dlg) == B_CANCEL)
547 return;
549 if (!reg_exp)
550 return;
551 if (!*reg_exp) {
552 g_free (reg_exp);
553 return;
555 search = mc_search_new (reg_exp, -1);
556 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
557 search->is_entire_line = TRUE;
558 search->is_case_sentitive = case_sens != 0;
560 for (i = 0; i < current_panel->count; i++) {
561 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
562 continue;
563 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
564 continue;
566 if (mc_search_run (search, current_panel->dir.list[i].fname,
567 0, current_panel->dir.list[i].fnamelen, NULL))
568 do_file_mark (current_panel, i, do_select);
571 mc_search_free (search);
572 g_free (reg_exp);
574 /* result flags */
575 select_flags = 0;
576 if (case_sens != 0)
577 select_flags |= SELECT_MATCH_CASE;
578 if (files_only != 0)
579 select_flags |= SELECT_FILES_ONLY;
580 if (shell_patterns != 0)
581 select_flags |= SELECT_SHELL_PATTERNS;
584 void select_cmd (void)
586 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", TRUE);
589 void unselect_cmd (void)
591 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", FALSE);
594 void ext_cmd (void)
596 char *buffer;
597 char *extdir;
598 int dir;
600 dir = 0;
601 if (geteuid () == 0){
602 dir = query_dialog (_("Extension file edit"),
603 _(" Which extension file you want to edit? "), D_NORMAL, 2,
604 _("&User"), _("&System Wide"));
606 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
608 if (dir == 0){
609 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
610 check_for_default (extdir, buffer);
611 do_edit (buffer);
612 g_free (buffer);
613 } else if (dir == 1) {
614 if (!exist_file(extdir)) {
615 g_free (extdir);
616 extdir = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
618 do_edit (extdir);
620 g_free (extdir);
621 flush_extension_file ();
624 /* edit file menu for mc */
625 void
626 edit_mc_menu_cmd (void)
628 char *buffer;
629 char *menufile;
630 int dir = 0;
632 dir = query_dialog (
633 _(" Menu edit "),
634 _(" Which menu file do you want to edit? "),
635 D_NORMAL, geteuid() ? 2 : 3,
636 _("&Local"), _("&User"), _("&System Wide")
639 menufile = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
641 if (!exist_file(menufile)) {
642 g_free (menufile);
643 menufile = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
646 switch (dir) {
647 case 0:
648 buffer = g_strdup (MC_LOCAL_MENU);
649 check_for_default (menufile, buffer);
650 chmod (buffer, 0600);
651 break;
653 case 1:
654 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_USERMENU_FILE, NULL);
655 check_for_default (menufile, buffer);
656 break;
658 case 2:
659 buffer = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
660 if (!exist_file(buffer)) {
661 g_free (buffer);
662 buffer = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
664 break;
666 default:
667 g_free (menufile);
668 return;
671 do_edit (buffer);
673 g_free (buffer);
674 g_free (menufile);
677 void edit_fhl_cmd (void)
679 char *buffer = NULL;
680 char *fhlfile = NULL;
682 int dir;
684 dir = 0;
685 if (geteuid () == 0){
686 dir = query_dialog (_("Highlighting groups file edit"),
687 _(" Which highlighting file you want to edit? "), D_NORMAL, 2,
688 _("&User"), _("&System Wide"));
690 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
692 if (dir == 0){
693 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL);
694 check_for_default (fhlfile, buffer);
695 do_edit (buffer);
696 g_free (buffer);
697 } else if (dir == 1) {
698 if (!exist_file(fhlfile)) {
699 g_free (fhlfile);
700 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
702 do_edit (fhlfile);
704 g_free (fhlfile);
706 /* refresh highlighting rules */
707 mc_fhl_free (&mc_filehighlight);
708 mc_filehighlight = mc_fhl_new (TRUE);
711 void quick_chdir_cmd (void)
713 char *target;
715 target = hotlist_cmd (LIST_HOTLIST);
716 if (!target)
717 return;
719 if (get_current_type () == view_tree)
720 tree_chdir (the_tree, target);
721 else {
722 char *cmd = g_strconcat ("cd ", target, (char *) NULL);
723 do_cd_command (cmd);
724 g_free (cmd);
726 g_free (target);
729 #ifdef ENABLE_VFS
730 void reselect_vfs (void)
732 char *target;
734 target = hotlist_cmd (LIST_VFSLIST);
735 if (!target)
736 return;
738 if (!do_cd (target, cd_exact))
739 message (D_ERROR, MSG_ERROR, _("Cannot change directory") );
740 g_free (target);
742 #endif /* ENABLE_VFS */
744 static int compare_files (char *name1, char *name2, off_t size)
746 int file1, file2;
747 int result = -1; /* Different by default */
749 if (size == 0)
750 return 0;
752 file1 = open (name1, O_RDONLY);
753 if (file1 >= 0){
754 file2 = open (name2, O_RDONLY);
755 if (file2 >= 0){
756 #ifdef HAVE_MMAP
757 char *data1, *data2;
758 /* Ugly if jungle */
759 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
760 if (data1 != (char*) -1){
761 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
762 if (data2 != (char*) -1){
763 rotate_dash ();
764 result = memcmp (data1, data2, size);
765 munmap (data2, size);
767 munmap (data1, size);
769 #else
770 /* Don't have mmap() :( Even more ugly :) */
771 char buf1[BUFSIZ], buf2[BUFSIZ];
772 int n1, n2;
773 rotate_dash ();
776 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
777 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
778 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
779 result = (n1 != n2) || memcmp(buf1,buf2,n1);
780 #endif /* !HAVE_MMAP */
781 close (file2);
783 close (file1);
785 return result;
788 enum CompareMode {
789 compare_quick, compare_size_only, compare_thourough
792 static void
793 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
795 int i, j;
796 char *src_name, *dst_name;
798 /* No marks by default */
799 panel->marked = 0;
800 panel->total = 0;
801 panel->dirs_marked = 0;
803 /* Handle all files in the panel */
804 for (i = 0; i < panel->count; i++){
805 file_entry *source = &panel->dir.list[i];
807 /* Default: unmarked */
808 file_mark (panel, i, 0);
810 /* Skip directories */
811 if (S_ISDIR (source->st.st_mode))
812 continue;
814 /* Search the corresponding entry from the other panel */
815 for (j = 0; j < other->count; j++){
816 if (strcmp (source->fname,
817 other->dir.list[j].fname) == 0)
818 break;
820 if (j >= other->count)
821 /* Not found -> mark */
822 do_file_mark (panel, i, 1);
823 else {
824 /* Found */
825 file_entry *target = &other->dir.list[j];
827 if (mode != compare_size_only){
828 /* Older version is not marked */
829 if (source->st.st_mtime < target->st.st_mtime)
830 continue;
833 /* Newer version with different size is marked */
834 if (source->st.st_size != target->st.st_size){
835 do_file_mark (panel, i, 1);
836 continue;
839 if (mode == compare_size_only)
840 continue;
842 if (mode == compare_quick){
843 /* Thorough compare off, compare only time stamps */
844 /* Mark newer version, don't mark version with the same date */
845 if (source->st.st_mtime > target->st.st_mtime){
846 do_file_mark (panel, i, 1);
848 continue;
851 /* Thorough compare on, do byte-by-byte comparison */
852 src_name = concat_dir_and_file (panel->cwd, source->fname);
853 dst_name = concat_dir_and_file (other->cwd, target->fname);
854 if (compare_files (src_name, dst_name, source->st.st_size))
855 do_file_mark (panel, i, 1);
856 g_free (src_name);
857 g_free (dst_name);
859 } /* for (i ...) */
862 void
863 compare_dirs_cmd (void)
865 int choice;
866 enum CompareMode thorough_flag;
868 choice =
869 query_dialog (_(" Compare directories "),
870 _(" Select compare method: "), D_NORMAL, 4,
871 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
873 if (choice < 0 || choice > 2)
874 return;
876 thorough_flag = choice;
878 if (get_current_type () == view_listing
879 && get_other_type () == view_listing) {
880 compare_dir (current_panel, other_panel, thorough_flag);
881 compare_dir (other_panel, current_panel, thorough_flag);
882 } else {
883 message (D_ERROR, MSG_ERROR,
884 _(" Both panels should be in the "
885 "listing mode to use this command "));
889 void
890 history_cmd (void)
892 /* show the history of command line widget */
893 send_message (&cmdline->widget, WIDGET_COMMAND, CK_InputHistoryShow);
896 void swap_cmd (void)
898 swap_panels ();
899 tty_touch_screen ();
900 repaint_screen ();
903 void
904 view_other_cmd (void)
906 static int message_flag = TRUE;
908 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell) {
909 if (message_flag)
910 message (D_ERROR, MSG_ERROR,
911 _(" Not an xterm or Linux console; \n"
912 " the panels cannot be toggled. "));
913 message_flag = FALSE;
914 } else {
915 toggle_panels ();
919 static void
920 do_link (int symbolic_link, const char *fname)
922 char *dest = NULL, *src = NULL;
924 if (!symbolic_link) {
925 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
926 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
927 if (!dest || !*dest)
928 goto cleanup;
929 save_cwds_stat ();
930 if (-1 == mc_link (fname, dest))
931 message (D_ERROR, MSG_ERROR, _(" link: %s "),
932 unix_error_string (errno));
933 } else {
934 char *s;
935 char *d;
937 /* suggest the full path for symlink */
938 s = concat_dir_and_file (current_panel->cwd, fname);
940 if (get_other_type () == view_listing) {
941 d = concat_dir_and_file (other_panel->cwd, fname);
942 } else {
943 d = g_strdup (fname);
946 symlink_dialog (s, d, &dest, &src);
947 g_free (d);
948 g_free (s);
950 if (!dest || !*dest || !src || !*src)
951 goto cleanup;
952 save_cwds_stat ();
953 if (-1 == mc_symlink (dest, src))
954 message (D_ERROR, MSG_ERROR, _(" symlink: %s "),
955 unix_error_string (errno));
957 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
958 repaint_screen ();
960 cleanup:
961 g_free (src);
962 g_free (dest);
965 void link_cmd (void)
967 do_link (0, selection (current_panel)->fname);
970 void symlink_cmd (void)
972 char *filename = NULL;
973 filename = selection (current_panel)->fname;
975 if (filename) {
976 do_link (1, filename);
980 void edit_symlink_cmd (void)
982 if (S_ISLNK (selection (current_panel)->st.st_mode)) {
983 char buffer [MC_MAXPATHLEN];
984 char *p = NULL;
985 int i;
986 char *dest, *q;
988 p = selection (current_panel)->fname;
990 q = g_strdup_printf (_(" Symlink `%s\' points to: "), str_trunc (p, 32));
992 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
993 if (i > 0) {
994 buffer [i] = 0;
995 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
996 if (dest) {
997 if (*dest && strcmp (buffer, dest)) {
998 save_cwds_stat ();
999 if (-1 == mc_unlink (p)){
1000 message (D_ERROR, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
1001 p, unix_error_string (errno));
1002 } else {
1003 if (-1 == mc_symlink (dest, p))
1004 message (D_ERROR, MSG_ERROR, _(" edit symlink: %s "),
1005 unix_error_string (errno));
1007 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1008 repaint_screen ();
1010 g_free (dest);
1013 g_free (q);
1014 } else {
1015 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1016 selection (current_panel)->fname);
1020 void help_cmd (void)
1022 interactive_display (NULL, "[main]");
1025 void
1026 user_file_menu_cmd (void)
1028 user_menu_cmd (NULL);
1032 * Return a random hint. If force is not 0, ignore the timeout.
1034 char *
1035 get_random_hint (int force)
1037 char *data, *result = NULL, *eol;
1038 int len;
1039 int start;
1040 static int last_sec;
1041 static struct timeval tv;
1042 GIConv conv;
1043 GString *buffer;
1045 /* Do not change hints more often than one minute */
1046 gettimeofday (&tv, NULL);
1047 if (!force && !(tv.tv_sec > last_sec + 60))
1048 return g_strdup ("");
1049 last_sec = tv.tv_sec;
1051 data = load_mc_home_file (mc_home, mc_home_alt, MC_HINT, NULL);
1052 if (!data)
1053 return 0;
1055 /* get a random entry */
1056 srand (tv.tv_sec);
1057 len = strlen (data);
1058 start = rand () % len;
1060 for (; start; start--) {
1061 if (data[start] == '\n') {
1062 start++;
1063 break;
1066 eol = strchr (&data[start], '\n');
1067 if (eol)
1068 *eol = 0;
1070 /* hint files are stored in utf-8 */
1071 /* try convert hint file from utf-8 to terminal encoding */
1072 conv = str_crt_conv_from ("UTF-8");
1073 if (conv != INVALID_CONV) {
1074 buffer = g_string_new ("");
1075 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE) {
1076 result = g_strdup (buffer->str);
1078 g_string_free (buffer, TRUE);
1079 str_close_conv (conv);
1082 g_free (data);
1083 return result;
1086 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1087 static void
1088 nice_cd (const char *text, const char *xtext, const char *help,
1089 const char *history_name, const char *prefix, int to_home)
1091 char *machine;
1092 char *cd_path;
1094 if (!SELECTED_IS_PANEL)
1095 return;
1097 machine = input_dialog_help (text, xtext, help, history_name, "");
1098 if (!machine)
1099 return;
1101 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1102 ugly as hell and leads to problems in vfs layer */
1104 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1105 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1106 else
1107 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1109 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1110 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1111 else
1112 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1113 g_free (cd_path);
1114 g_free (machine);
1116 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1119 #ifdef USE_NETCODE
1121 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1123 #ifdef ENABLE_VFS_MCFS
1124 void netlink_cmd (void)
1126 nice_cd (_(" Link to a remote machine "), _(machine_str),
1127 "[Network File System]", ":netlink_cmd: Link to a remote ",
1128 "/#mc:", 1);
1130 #endif /* ENABLE_VFS_MCFS */
1132 void ftplink_cmd (void)
1134 nice_cd (_(" FTP to machine "), _(machine_str),
1135 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1138 void fishlink_cmd (void)
1140 nice_cd (_(" Shell link to machine "), _(machine_str),
1141 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1142 "/#sh:", 1);
1145 #ifdef ENABLE_VFS_SMB
1146 void smblink_cmd (void)
1148 nice_cd (_(" SMB link to machine "), _(machine_str),
1149 "[SMB File System]", ":smblink_cmd: SMB link to machine ",
1150 "/#smb:", 0);
1152 #endif /* ENABLE_VFS_SMB */
1153 #endif /* USE_NETCODE */
1155 #ifdef USE_EXT2FSLIB
1156 void undelete_cmd (void)
1158 nice_cd (_(" Undelete files on an ext2 file system "),
1159 _(" Enter device (without /dev/) to undelete\n "
1160 " files on: (F1 for details)"),
1161 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ",
1162 "/#undel:", 0);
1164 #endif /* USE_EXT2FSLIB */
1166 void quick_cd_cmd (void)
1168 char *p = cd_dialog ();
1170 if (p && *p) {
1171 char *q = g_strconcat ("cd ", p, (char *) NULL);
1173 do_cd_command (q);
1174 g_free (q);
1176 g_free (p);
1181 \brief calculate dirs sizes
1183 calculate dirs sizes and resort panel:
1184 dirs_selected = show size for selected dirs,
1185 otherwise = show size for dir under cursor:
1186 dir under cursor ".." = show size for all dirs,
1187 otherwise = show size for dir under cursor
1189 void
1190 smart_dirsize_cmd (void)
1192 WPanel *panel = current_panel;
1193 file_entry *entry;
1195 entry = &(panel->dir.list[panel->selected]);
1196 if ( ( S_ISDIR (entry->st.st_mode) && ( strcmp(entry->fname, "..") == 0 ) )
1197 || panel->dirs_marked )
1198 dirsizes_cmd ();
1199 else
1200 single_dirsize_cmd ();
1203 void
1204 single_dirsize_cmd (void)
1206 WPanel *panel = current_panel;
1207 file_entry *entry;
1208 off_t marked;
1209 double total;
1210 ComputeDirSizeUI *ui;
1212 entry = &(panel->dir.list[panel->selected]);
1213 if (S_ISDIR (entry->st.st_mode) && strcmp(entry->fname, "..") != 0) {
1214 ui = compute_dir_size_create_ui ();
1216 total = 0.0;
1218 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1219 &marked, &total) == FILE_CONT) {
1220 entry->st.st_size = (off_t) total;
1221 entry->f.dir_size_computed = 1;
1224 compute_dir_size_destroy_ui (ui);
1227 if (mark_moves_down)
1228 send_message (&(panel->widget), WIDGET_KEY, KEY_DOWN);
1230 recalculate_panel_summary (panel);
1232 if ( current_panel->current_sort_field->sort_routine == (sortfn *) sort_size )
1233 panel_re_sort (panel);
1235 panel->dirty = 1;
1238 void
1239 dirsizes_cmd (void)
1241 WPanel *panel = current_panel;
1242 int i;
1243 off_t marked;
1244 double total;
1245 ComputeDirSizeUI *ui;
1247 ui = compute_dir_size_create_ui ();
1249 for (i = 0; i < panel->count; i++)
1250 if (S_ISDIR (panel->dir.list [i].st.st_mode)
1251 && ((panel->dirs_marked && panel->dir.list [i].f.marked)
1252 || !panel->dirs_marked)
1253 && strcmp (panel->dir.list [i].fname, "..") != 0) {
1254 total = 0.0l;
1256 if (compute_dir_size (panel->dir.list [i].fname,
1257 ui, compute_dir_size_update_ui,
1258 &marked, &total) != FILE_CONT)
1259 break;
1261 panel->dir.list [i].st.st_size = (off_t) total;
1262 panel->dir.list [i].f.dir_size_computed = 1;
1265 compute_dir_size_destroy_ui (ui);
1267 recalculate_panel_summary (panel);
1269 if ( current_panel->current_sort_field->sort_routine == (sortfn *) sort_size )
1270 panel_re_sort (panel);
1272 panel->dirty = 1;
1275 void
1276 save_setup_cmd (void)
1278 if (! save_setup ())
1279 return;
1280 message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"),
1281 MC_USERCONF_DIR PATH_SEP_STR MC_CONFIG_FILE);
1284 static void
1285 configure_panel_listing (WPanel *p, int list_type, int use_msformat, char *user, char *status)
1287 p->user_mini_status = use_msformat;
1288 p->list_type = list_type;
1290 if (list_type == list_user || use_msformat) {
1291 g_free (p->user_format);
1292 p->user_format = user;
1294 g_free (p->user_status_format [list_type]);
1295 p->user_status_format [list_type] = status;
1297 set_panel_formats (p);
1298 } else {
1299 g_free (user);
1300 g_free (status);
1303 set_panel_formats (p);
1304 do_refresh ();
1307 void
1308 info_cmd_no_menu (void)
1310 if (get_display_type (0) == view_info)
1311 set_display_type (0, view_listing);
1312 else if (get_display_type (1) == view_info)
1313 set_display_type (1, view_listing);
1314 else
1315 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1318 void
1319 quick_cmd_no_menu (void)
1321 if (get_display_type (0) == view_quick)
1322 set_display_type (0, view_listing);
1323 else if (get_display_type (1) == view_quick)
1324 set_display_type (1, view_listing);
1325 else
1326 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1329 static void
1330 switch_to_listing (int panel_index)
1332 if (get_display_type (panel_index) != view_listing)
1333 set_display_type (panel_index, view_listing);
1336 void
1337 listing_cmd (void)
1339 int list_type;
1340 int use_msformat;
1341 char *user, *status;
1342 WPanel *p = NULL;
1344 if (get_display_type (MENU_PANEL_IDX) == view_listing)
1345 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1347 list_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1349 if (list_type != -1) {
1350 switch_to_listing (MENU_PANEL_IDX);
1351 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1352 configure_panel_listing (p, list_type, use_msformat, user, status);
1356 void
1357 tree_cmd (void)
1359 set_display_type (MENU_PANEL_IDX, view_tree);
1362 void
1363 info_cmd (void)
1365 set_display_type (MENU_PANEL_IDX, view_info);
1368 void
1369 quick_view_cmd (void)
1371 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1372 change_panel ();
1373 set_display_type (MENU_PANEL_IDX, view_quick);
1376 /* Handle the tree internal listing modes switching */
1377 static gboolean
1378 set_basic_panel_listing_to (int panel_index, int listing_mode)
1380 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1381 gboolean ok;
1383 switch_to_listing (panel_index);
1384 p->list_type = listing_mode;
1386 ok = set_panel_formats (p) == 0;
1388 if (ok)
1389 do_refresh ();
1391 return ok;
1394 void
1395 toggle_listing_cmd (void)
1397 int current = get_current_index ();
1398 WPanel *p = (WPanel *) get_panel_widget (current);
1400 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1403 void
1404 encoding_cmd (void)
1406 if (SELECTED_IS_PANEL)
1407 set_panel_encoding (MENU_PANEL);