Changes into src directory:
[midnight-commander.git] / src / cmd.c
blob1f642b0a123094c043e703c167455d01d0421030
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 #ifdef USE_DIFF_VIEW
94 # include "src/diffviewer/ydiff.h"
95 #endif
99 /* If set and you don't have subshell support,then C-o will give you a shell */
100 int output_starts_shell = 0;
102 /* If set, use the builtin editor */
103 int use_internal_edit = 1;
105 /* Automatically fills name with current selected item name on mkdir */
106 int auto_fill_mkdir_name = 1;
108 /* if set, only selection of files is inverted */
109 int reverse_files_only = 1;
111 /* selection flags */
112 typedef enum
114 SELECT_FILES_ONLY = 1 << 0,
115 SELECT_MATCH_CASE = 1 << 1,
116 SELECT_SHELL_PATTERNS = 1 << 2
117 } select_flags_t;
119 int select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
122 view_file_at_line (const char *filename, int plain_view, int internal, int start_line)
124 static const char *viewer = NULL;
125 int move_dir = 0;
128 if (plain_view)
130 int changed_hex_mode = 0;
131 int changed_nroff_flag = 0;
132 int changed_magic_flag = 0;
134 mcview_altered_hex_mode = 0;
135 mcview_altered_nroff_flag = 0;
136 mcview_altered_magic_flag = 0;
137 if (mcview_default_hex_mode)
138 changed_hex_mode = 1;
139 if (mcview_default_nroff_flag)
140 changed_nroff_flag = 1;
141 if (mcview_default_magic_flag)
142 changed_magic_flag = 1;
143 mcview_default_hex_mode = 0;
144 mcview_default_nroff_flag = 0;
145 mcview_default_magic_flag = 0;
146 mcview_viewer (NULL, filename, &move_dir, start_line);
147 if (changed_hex_mode && !mcview_altered_hex_mode)
148 mcview_default_hex_mode = 1;
149 if (changed_nroff_flag && !mcview_altered_nroff_flag)
150 mcview_default_nroff_flag = 1;
151 if (changed_magic_flag && !mcview_altered_magic_flag)
152 mcview_default_magic_flag = 1;
153 repaint_screen ();
154 return move_dir;
156 if (internal)
158 char view_entry[BUF_TINY];
160 if (start_line != 0)
161 g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line);
162 else
163 strcpy (view_entry, "View");
165 if (regex_command (filename, view_entry, &move_dir) == 0)
167 mcview_viewer (NULL, filename, &move_dir, start_line);
168 repaint_screen ();
171 else
173 if (!viewer)
175 viewer = getenv ("VIEWER");
176 if (!viewer)
177 viewer = getenv ("PAGER");
178 if (!viewer)
179 viewer = "view";
181 execute_with_vfs_arg (viewer, filename);
183 return move_dir;
186 /* view_file (filename, plain_view, internal)
188 * Inputs:
189 * filename: The file name to view
190 * plain_view: If set does not do any fancy pre-processing (no filtering) and
191 * always invokes the internal viewer.
192 * internal: If set uses the internal viewer, otherwise an external viewer.
195 view_file (const char *filename, int plain_view, int internal)
197 return view_file_at_line (filename, plain_view, internal, 0);
200 /* scan_for_file (panel, idx, direction)
202 * Inputs:
203 * panel: pointer to the panel on which we operate
204 * idx: starting file.
205 * direction: 1, or -1
207 static int
208 scan_for_file (WPanel * panel, int idx, int direction)
210 int i = idx + direction;
212 while (i != idx)
214 if (i < 0)
215 i = panel->count - 1;
216 if (i == panel->count)
217 i = 0;
218 if (!S_ISDIR (panel->dir.list[i].st.st_mode))
219 return i;
220 i += direction;
222 return i;
226 * Run viewer (internal or external) on the currently selected file.
227 * If normal is 1, force internal viewer and raw mode (used for F13).
229 static void
230 do_view_cmd (int normal)
232 /* Directories are viewed by changing to them */
233 if (S_ISDIR (selection (current_panel)->st.st_mode) || link_isdir (selection (current_panel)))
235 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked))
237 if (query_dialog
238 (_(" Confirmation "), _("Files tagged, want to cd?"), D_NORMAL, 2,
239 _("&Yes"), _("&No")) != 0)
241 return;
244 if (!do_cd (selection (current_panel)->fname, cd_exact))
245 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
247 else
249 int dir, file_idx;
250 char *filename;
252 file_idx = current_panel->selected;
253 while (1)
255 filename = current_panel->dir.list[file_idx].fname;
257 dir = view_file (filename, normal, use_internal_view);
258 if (dir == 0)
259 break;
260 file_idx = scan_for_file (current_panel, file_idx, dir);
264 repaint_screen ();
267 /* Run user's preferred viewer on the currently selected file */
268 void
269 view_cmd (void)
271 do_view_cmd (0);
274 /* Ask for file and run user's preferred viewer on it */
275 void
276 view_file_cmd (void)
278 char *filename;
280 filename =
281 input_expand_dialog (_(" View file "), _(" Filename:"),
282 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
283 if (!filename)
284 return;
286 view_file (filename, 0, use_internal_view);
287 g_free (filename);
290 /* Run plain internal viewer on the currently selected file */
291 void
292 view_simple_cmd (void)
294 do_view_cmd (1);
297 void
298 filtered_view_cmd (void)
300 char *command;
302 command =
303 input_dialog (_(" Filtered view "),
304 _(" Filter command and arguments:"),
305 MC_HISTORY_FM_FILTERED_VIEW, selection (current_panel)->fname);
306 if (!command)
307 return;
309 mcview_viewer (command, "", NULL, 0);
311 g_free (command);
314 void
315 do_edit_at_line (const char *what, int start_line)
317 static const char *editor = NULL;
319 #ifdef USE_INTERNAL_EDIT
320 if (use_internal_edit)
321 edit_file (what, start_line);
322 else
323 #else
324 (void) start_line;
325 #endif /* USE_INTERNAL_EDIT */
327 if (editor == NULL)
329 editor = getenv ("EDITOR");
330 if (editor == NULL)
331 editor = get_default_editor ();
333 execute_with_vfs_arg (editor, what);
335 if (mc_run_mode == MC_RUN_FULL)
337 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
338 repaint_screen ();
342 static void
343 do_edit (const char *what)
345 do_edit_at_line (what, 0);
348 void
349 edit_cmd (void)
351 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
352 do_edit (selection (current_panel)->fname);
355 void
356 edit_cmd_new (void)
358 #if HAVE_CHARSET
359 source_codepage = default_source_codepage;
360 #endif
361 do_edit (NULL);
364 /* Invoked by F5. Copy, default to the other panel. */
365 void
366 copy_cmd (void)
368 save_cwds_stat ();
369 if (panel_operate (current_panel, OP_COPY, FALSE))
371 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
372 repaint_screen ();
376 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
377 void
378 rename_cmd (void)
380 save_cwds_stat ();
381 if (panel_operate (current_panel, OP_MOVE, FALSE))
383 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
384 repaint_screen ();
388 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
389 void
390 copy_cmd_local (void)
392 save_cwds_stat ();
393 if (panel_operate (current_panel, OP_COPY, TRUE))
395 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
396 repaint_screen ();
400 /* Invoked by F16. Move/rename, default to the same panel. */
401 void
402 rename_cmd_local (void)
404 save_cwds_stat ();
405 if (panel_operate (current_panel, OP_MOVE, TRUE))
407 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
408 repaint_screen ();
412 void
413 mkdir_cmd (void)
415 char *dir, *absdir;
416 const char *name = "";
418 /* If 'on' then automatically fills name with current selected item name */
419 if (auto_fill_mkdir_name && strcmp (selection (current_panel)->fname, "..") != 0)
420 name = selection (current_panel)->fname;
422 dir =
423 input_expand_dialog (_("Create a new Directory"),
424 _(" Enter directory name:"), MC_HISTORY_FM_MKDIR, name);
426 if (!dir)
427 return;
429 if (*dir)
431 if (dir[0] == '/' || dir[0] == '~')
432 absdir = g_strdup (dir);
433 else
434 absdir = concat_dir_and_file (current_panel->cwd, dir);
436 save_cwds_stat ();
437 if (my_mkdir (absdir, 0777) == 0)
439 update_panels (UP_OPTIMIZE, dir);
440 repaint_screen ();
441 select_item (current_panel);
443 else
445 message (D_ERROR, MSG_ERROR, " %s ", unix_error_string (errno));
447 g_free (absdir);
449 g_free (dir);
452 void
453 delete_cmd (void)
455 save_cwds_stat ();
457 if (panel_operate (current_panel, OP_DELETE, FALSE))
459 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
460 repaint_screen ();
464 /* Invoked by F18. Remove selected file, regardless of marked files. */
465 void
466 delete_cmd_local (void)
468 save_cwds_stat ();
470 if (panel_operate (current_panel, OP_DELETE, TRUE))
472 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
473 repaint_screen ();
477 void
478 find_cmd (void)
480 do_find ();
483 static void
484 set_panel_filter_to (WPanel * p, char *allocated_filter_string)
486 g_free (p->filter);
487 p->filter = 0;
489 if (!(allocated_filter_string[0] == '*' && allocated_filter_string[1] == 0))
490 p->filter = allocated_filter_string;
491 else
492 g_free (allocated_filter_string);
493 reread_cmd ();
496 /* Set a given panel filter expression */
497 static void
498 set_panel_filter (WPanel * p)
500 char *reg_exp;
501 const char *x;
503 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
505 reg_exp = input_dialog_help (_(" Filter "),
506 _(" Set expression for filtering filenames"),
507 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
508 if (!reg_exp)
509 return;
510 set_panel_filter_to (p, reg_exp);
513 /* Invoked from the left/right menus */
514 void
515 filter_cmd (void)
517 WPanel *p;
519 if (!SELECTED_IS_PANEL)
520 return;
522 p = MENU_PANEL;
523 set_panel_filter (p);
526 void
527 reread_cmd (void)
529 int flag;
531 if (get_current_type () == view_listing && get_other_type () == view_listing)
532 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
533 else
534 flag = UP_ONLY_CURRENT;
536 update_panels (UP_RELOAD | flag, UP_KEEPSEL);
537 repaint_screen ();
540 void
541 reverse_selection_cmd (void)
543 int i;
544 file_entry *file;
546 for (i = 0; i < current_panel->count; i++)
548 file = &current_panel->dir.list[i];
549 if ((reverse_files_only == 0) || !S_ISDIR (file->st.st_mode))
550 do_file_mark (current_panel, i, !file->f.marked);
554 static void
555 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
557 /* dialog sizes */
558 const int DX = 50;
559 const int DY = 7;
561 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
562 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
563 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
565 char *reg_exp;
566 mc_search_t *search;
567 int i;
569 QuickWidget quick_widgets[] = {
570 QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns),
571 QUICK_CHECKBOX (DX / 2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens),
572 QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only),
573 QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, &reg_exp),
574 QUICK_END
577 QuickDialog quick_dlg = {
578 DX, DY, -1, -1, title,
579 "[Select/Unselect Files]", quick_widgets, FALSE
582 if (quick_dialog (&quick_dlg) == B_CANCEL)
583 return;
585 if (!reg_exp)
586 return;
587 if (!*reg_exp)
589 g_free (reg_exp);
590 return;
592 search = mc_search_new (reg_exp, -1);
593 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
594 search->is_entire_line = TRUE;
595 search->is_case_sentitive = case_sens != 0;
597 for (i = 0; i < current_panel->count; i++)
599 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
600 continue;
601 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
602 continue;
604 if (mc_search_run (search, current_panel->dir.list[i].fname,
605 0, current_panel->dir.list[i].fnamelen, NULL))
606 do_file_mark (current_panel, i, do_select);
609 mc_search_free (search);
610 g_free (reg_exp);
612 /* result flags */
613 select_flags = 0;
614 if (case_sens != 0)
615 select_flags |= SELECT_MATCH_CASE;
616 if (files_only != 0)
617 select_flags |= SELECT_FILES_ONLY;
618 if (shell_patterns != 0)
619 select_flags |= SELECT_SHELL_PATTERNS;
622 void
623 select_cmd (void)
625 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", TRUE);
628 void
629 unselect_cmd (void)
631 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", FALSE);
634 void
635 ext_cmd (void)
637 char *buffer;
638 char *extdir;
639 int dir;
641 dir = 0;
642 if (geteuid () == 0)
644 dir = query_dialog (_("Extension file edit"),
645 _(" Which extension file you want to edit? "), D_NORMAL, 2,
646 _("&User"), _("&System Wide"));
648 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
650 if (dir == 0)
652 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
653 check_for_default (extdir, buffer);
654 do_edit (buffer);
655 g_free (buffer);
657 else if (dir == 1)
659 if (!exist_file (extdir))
661 g_free (extdir);
662 extdir = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
664 do_edit (extdir);
666 g_free (extdir);
667 flush_extension_file ();
670 /* edit file menu for mc */
671 void
672 edit_mc_menu_cmd (void)
674 char *buffer;
675 char *menufile;
676 int dir = 0;
678 dir = query_dialog (_(" Menu edit "),
679 _(" Which menu file do you want to edit? "),
680 D_NORMAL, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
682 menufile = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
684 if (!exist_file (menufile))
686 g_free (menufile);
687 menufile = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
690 switch (dir)
692 case 0:
693 buffer = g_strdup (MC_LOCAL_MENU);
694 check_for_default (menufile, buffer);
695 chmod (buffer, 0600);
696 break;
698 case 1:
699 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_USERMENU_FILE, NULL);
700 check_for_default (menufile, buffer);
701 break;
703 case 2:
704 buffer = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
705 if (!exist_file (buffer))
707 g_free (buffer);
708 buffer = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
710 break;
712 default:
713 g_free (menufile);
714 return;
717 do_edit (buffer);
719 g_free (buffer);
720 g_free (menufile);
723 void
724 edit_fhl_cmd (void)
726 char *buffer = NULL;
727 char *fhlfile = NULL;
729 int dir;
731 dir = 0;
732 if (geteuid () == 0)
734 dir = query_dialog (_("Highlighting groups file edit"),
735 _(" Which highlighting file you want to edit? "), D_NORMAL, 2,
736 _("&User"), _("&System Wide"));
738 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
740 if (dir == 0)
742 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL);
743 check_for_default (fhlfile, buffer);
744 do_edit (buffer);
745 g_free (buffer);
747 else if (dir == 1)
749 if (!exist_file (fhlfile))
751 g_free (fhlfile);
752 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
754 do_edit (fhlfile);
756 g_free (fhlfile);
758 /* refresh highlighting rules */
759 mc_fhl_free (&mc_filehighlight);
760 mc_filehighlight = mc_fhl_new (TRUE);
763 void
764 quick_chdir_cmd (void)
766 char *target;
768 target = hotlist_cmd (LIST_HOTLIST);
769 if (!target)
770 return;
772 if (get_current_type () == view_tree)
773 tree_chdir (the_tree, target);
774 else
776 char *cmd = g_strconcat ("cd ", target, (char *) NULL);
777 do_cd_command (cmd);
778 g_free (cmd);
780 g_free (target);
783 #ifdef ENABLE_VFS
784 void
785 reselect_vfs (void)
787 char *target;
789 target = hotlist_cmd (LIST_VFSLIST);
790 if (!target)
791 return;
793 if (!do_cd (target, cd_exact))
794 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
795 g_free (target);
797 #endif /* ENABLE_VFS */
799 static int
800 compare_files (char *name1, char *name2, off_t size)
802 int file1, file2;
803 int result = -1; /* Different by default */
805 if (size == 0)
806 return 0;
808 file1 = open (name1, O_RDONLY);
809 if (file1 >= 0)
811 file2 = open (name2, O_RDONLY);
812 if (file2 >= 0)
814 #ifdef HAVE_MMAP
815 char *data1, *data2;
816 /* Ugly if jungle */
817 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
818 if (data1 != (char *) -1)
820 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
821 if (data2 != (char *) -1)
823 rotate_dash ();
824 result = memcmp (data1, data2, size);
825 munmap (data2, size);
827 munmap (data1, size);
829 #else
830 /* Don't have mmap() :( Even more ugly :) */
831 char buf1[BUFSIZ], buf2[BUFSIZ];
832 int n1, n2;
833 rotate_dash ();
836 while ((n1 = read (file1, buf1, BUFSIZ)) == -1 && errno == EINTR);
837 while ((n2 = read (file2, buf2, BUFSIZ)) == -1 && errno == EINTR);
839 while (n1 == n2 && n1 == BUFSIZ && !memcmp (buf1, buf2, BUFSIZ));
840 result = (n1 != n2) || memcmp (buf1, buf2, n1);
841 #endif /* !HAVE_MMAP */
842 close (file2);
844 close (file1);
846 return result;
849 enum CompareMode
851 compare_quick, compare_size_only, compare_thourough
854 static void
855 compare_dir (WPanel * panel, WPanel * other, enum CompareMode mode)
857 int i, j;
858 char *src_name, *dst_name;
860 /* No marks by default */
861 panel->marked = 0;
862 panel->total = 0;
863 panel->dirs_marked = 0;
865 /* Handle all files in the panel */
866 for (i = 0; i < panel->count; i++)
868 file_entry *source = &panel->dir.list[i];
870 /* Default: unmarked */
871 file_mark (panel, i, 0);
873 /* Skip directories */
874 if (S_ISDIR (source->st.st_mode))
875 continue;
877 /* Search the corresponding entry from the other panel */
878 for (j = 0; j < other->count; j++)
880 if (strcmp (source->fname, other->dir.list[j].fname) == 0)
881 break;
883 if (j >= other->count)
884 /* Not found -> mark */
885 do_file_mark (panel, i, 1);
886 else
888 /* Found */
889 file_entry *target = &other->dir.list[j];
891 if (mode != compare_size_only)
893 /* Older version is not marked */
894 if (source->st.st_mtime < target->st.st_mtime)
895 continue;
898 /* Newer version with different size is marked */
899 if (source->st.st_size != target->st.st_size)
901 do_file_mark (panel, i, 1);
902 continue;
905 if (mode == compare_size_only)
906 continue;
908 if (mode == compare_quick)
910 /* Thorough compare off, compare only time stamps */
911 /* Mark newer version, don't mark version with the same date */
912 if (source->st.st_mtime > target->st.st_mtime)
914 do_file_mark (panel, i, 1);
916 continue;
919 /* Thorough compare on, do byte-by-byte comparison */
920 src_name = concat_dir_and_file (panel->cwd, source->fname);
921 dst_name = concat_dir_and_file (other->cwd, target->fname);
922 if (compare_files (src_name, dst_name, source->st.st_size))
923 do_file_mark (panel, i, 1);
924 g_free (src_name);
925 g_free (dst_name);
927 } /* for (i ...) */
930 void
931 compare_dirs_cmd (void)
933 int choice;
934 enum CompareMode thorough_flag;
936 choice =
937 query_dialog (_(" Compare directories "),
938 _(" Select compare method: "), D_NORMAL, 4,
939 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
941 if (choice < 0 || choice > 2)
942 return;
944 thorough_flag = choice;
946 if (get_current_type () == view_listing && get_other_type () == view_listing)
948 compare_dir (current_panel, other_panel, thorough_flag);
949 compare_dir (other_panel, current_panel, thorough_flag);
951 else
953 message (D_ERROR, MSG_ERROR,
954 _(" Both panels should be in the " "listing mode to use this command "));
958 #ifdef USE_DIFF_VIEW
959 void
960 diff_view_cmd (void)
962 dview_diff_cmd ();
964 if (mc_run_mode == MC_RUN_FULL)
966 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
967 repaint_screen ();
970 #endif
972 void
973 history_cmd (void)
975 /* show the history of command line widget */
976 send_message (&cmdline->widget, WIDGET_COMMAND, CK_InputHistoryShow);
979 void
980 swap_cmd (void)
982 swap_panels ();
983 tty_touch_screen ();
984 repaint_screen ();
987 void
988 view_other_cmd (void)
990 static int message_flag = TRUE;
992 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell)
994 if (message_flag)
995 message (D_ERROR, MSG_ERROR,
996 _(" Not an xterm or Linux console; \n" " the panels cannot be toggled. "));
997 message_flag = FALSE;
999 else
1001 toggle_panels ();
1005 static void
1006 do_link (int symbolic_link, const char *fname)
1008 char *dest = NULL, *src = NULL;
1010 if (!symbolic_link)
1012 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
1013 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
1014 if (!dest || !*dest)
1015 goto cleanup;
1016 save_cwds_stat ();
1017 if (-1 == mc_link (fname, dest))
1018 message (D_ERROR, MSG_ERROR, _(" link: %s "), unix_error_string (errno));
1020 else
1022 char *s;
1023 char *d;
1025 /* suggest the full path for symlink */
1026 s = concat_dir_and_file (current_panel->cwd, fname);
1028 if (get_other_type () == view_listing)
1030 d = concat_dir_and_file (other_panel->cwd, fname);
1032 else
1034 d = g_strdup (fname);
1037 symlink_dialog (s, d, &dest, &src);
1038 g_free (d);
1039 g_free (s);
1041 if (!dest || !*dest || !src || !*src)
1042 goto cleanup;
1043 save_cwds_stat ();
1044 if (-1 == mc_symlink (dest, src))
1045 message (D_ERROR, MSG_ERROR, _(" symlink: %s "), unix_error_string (errno));
1047 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1048 repaint_screen ();
1050 cleanup:
1051 g_free (src);
1052 g_free (dest);
1055 void
1056 link_cmd (void)
1058 do_link (0, selection (current_panel)->fname);
1061 void
1062 symlink_cmd (void)
1064 char *filename = NULL;
1065 filename = selection (current_panel)->fname;
1067 if (filename)
1069 do_link (1, filename);
1073 void
1074 edit_symlink_cmd (void)
1076 if (S_ISLNK (selection (current_panel)->st.st_mode))
1078 char buffer[MC_MAXPATHLEN];
1079 char *p = NULL;
1080 int i;
1081 char *dest, *q;
1083 p = selection (current_panel)->fname;
1085 q = g_strdup_printf (_(" Symlink `%s\' points to: "), str_trunc (p, 32));
1087 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
1088 if (i > 0)
1090 buffer[i] = 0;
1091 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
1092 if (dest)
1094 if (*dest && strcmp (buffer, dest))
1096 save_cwds_stat ();
1097 if (-1 == mc_unlink (p))
1099 message (D_ERROR, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
1100 p, unix_error_string (errno));
1102 else
1104 if (-1 == mc_symlink (dest, p))
1105 message (D_ERROR, MSG_ERROR, _(" edit symlink: %s "),
1106 unix_error_string (errno));
1108 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1109 repaint_screen ();
1111 g_free (dest);
1114 g_free (q);
1116 else
1118 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1119 selection (current_panel)->fname);
1123 void
1124 help_cmd (void)
1126 if (current_panel->searching)
1127 interactive_display (NULL, "[Quick search]");
1128 else
1129 interactive_display (NULL, "[main]");
1132 void
1133 user_file_menu_cmd (void)
1135 user_menu_cmd (NULL);
1139 * Return a random hint. If force is not 0, ignore the timeout.
1141 char *
1142 get_random_hint (int force)
1144 char *data, *result = NULL, *eol;
1145 int len;
1146 int start;
1147 static int last_sec;
1148 static struct timeval tv;
1149 GIConv conv;
1150 GString *buffer;
1152 /* Do not change hints more often than one minute */
1153 gettimeofday (&tv, NULL);
1154 if (!force && !(tv.tv_sec > last_sec + 60))
1155 return g_strdup ("");
1156 last_sec = tv.tv_sec;
1158 data = load_mc_home_file (mc_home, mc_home_alt, MC_HINT, NULL);
1159 if (!data)
1160 return 0;
1162 /* get a random entry */
1163 srand (tv.tv_sec);
1164 len = strlen (data);
1165 start = rand () % len;
1167 for (; start; start--)
1169 if (data[start] == '\n')
1171 start++;
1172 break;
1175 eol = strchr (&data[start], '\n');
1176 if (eol)
1177 *eol = 0;
1179 /* hint files are stored in utf-8 */
1180 /* try convert hint file from utf-8 to terminal encoding */
1181 conv = str_crt_conv_from ("UTF-8");
1182 if (conv != INVALID_CONV)
1184 buffer = g_string_new ("");
1185 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1187 result = g_strdup (buffer->str);
1189 g_string_free (buffer, TRUE);
1190 str_close_conv (conv);
1193 g_free (data);
1194 return result;
1197 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1198 static void
1199 nice_cd (const char *text, const char *xtext, const char *help,
1200 const char *history_name, const char *prefix, int to_home)
1202 char *machine;
1203 char *cd_path;
1205 if (!SELECTED_IS_PANEL)
1206 return;
1208 machine = input_dialog_help (text, xtext, help, history_name, "");
1209 if (!machine)
1210 return;
1212 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1213 ugly as hell and leads to problems in vfs layer */
1215 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1216 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1217 else
1218 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1220 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1221 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1222 else
1223 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1224 g_free (cd_path);
1225 g_free (machine);
1227 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1230 #ifdef USE_NETCODE
1232 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1234 #ifdef ENABLE_VFS_MCFS
1235 void
1236 netlink_cmd (void)
1238 nice_cd (_(" Link to a remote machine "), _(machine_str),
1239 "[Network File System]", ":netlink_cmd: Link to a remote ", "/#mc:", 1);
1241 #endif /* ENABLE_VFS_MCFS */
1243 void
1244 ftplink_cmd (void)
1246 nice_cd (_(" FTP to machine "), _(machine_str),
1247 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1250 void
1251 fishlink_cmd (void)
1253 nice_cd (_(" Shell link to machine "), _(machine_str),
1254 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1255 "/#sh:", 1);
1258 #ifdef ENABLE_VFS_SMB
1259 void
1260 smblink_cmd (void)
1262 nice_cd (_(" SMB link to machine "), _(machine_str),
1263 "[SMB File System]", ":smblink_cmd: SMB link to machine ", "/#smb:", 0);
1265 #endif /* ENABLE_VFS_SMB */
1266 #endif /* USE_NETCODE */
1268 #ifdef USE_EXT2FSLIB
1269 void
1270 undelete_cmd (void)
1272 nice_cd (_(" Undelete files on an ext2 file system "),
1273 _(" Enter device (without /dev/) to undelete\n "
1274 " files on: (F1 for details)"),
1275 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "/#undel:", 0);
1277 #endif /* USE_EXT2FSLIB */
1279 void
1280 quick_cd_cmd (void)
1282 char *p = cd_dialog ();
1284 if (p && *p)
1286 char *q = g_strconcat ("cd ", p, (char *) NULL);
1288 do_cd_command (q);
1289 g_free (q);
1291 g_free (p);
1296 \brief calculate dirs sizes
1298 calculate dirs sizes and resort panel:
1299 dirs_selected = show size for selected dirs,
1300 otherwise = show size for dir under cursor:
1301 dir under cursor ".." = show size for all dirs,
1302 otherwise = show size for dir under cursor
1304 void
1305 smart_dirsize_cmd (void)
1307 WPanel *panel = current_panel;
1308 file_entry *entry;
1310 entry = &(panel->dir.list[panel->selected]);
1311 if ((S_ISDIR (entry->st.st_mode) && (strcmp (entry->fname, "..") == 0)) || panel->dirs_marked)
1312 dirsizes_cmd ();
1313 else
1314 single_dirsize_cmd ();
1317 void
1318 single_dirsize_cmd (void)
1320 WPanel *panel = current_panel;
1321 file_entry *entry;
1322 off_t marked;
1323 double total;
1324 ComputeDirSizeUI *ui;
1326 entry = &(panel->dir.list[panel->selected]);
1327 if (S_ISDIR (entry->st.st_mode) && strcmp (entry->fname, "..") != 0)
1329 ui = compute_dir_size_create_ui ();
1331 total = 0.0;
1333 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1334 &marked, &total, TRUE) == FILE_CONT)
1336 entry->st.st_size = (off_t) total;
1337 entry->f.dir_size_computed = 1;
1340 compute_dir_size_destroy_ui (ui);
1343 if (mark_moves_down)
1344 send_message (&(panel->widget), WIDGET_KEY, KEY_DOWN);
1346 recalculate_panel_summary (panel);
1348 if (current_panel->current_sort_field->sort_routine == (sortfn *) sort_size)
1349 panel_re_sort (panel);
1351 panel->dirty = 1;
1354 void
1355 dirsizes_cmd (void)
1357 WPanel *panel = current_panel;
1358 int i;
1359 off_t marked;
1360 double total;
1361 ComputeDirSizeUI *ui;
1363 ui = compute_dir_size_create_ui ();
1365 for (i = 0; i < panel->count; i++)
1366 if (S_ISDIR (panel->dir.list[i].st.st_mode)
1367 && ((panel->dirs_marked && panel->dir.list[i].f.marked)
1368 || !panel->dirs_marked) && strcmp (panel->dir.list[i].fname, "..") != 0)
1370 total = 0.0l;
1372 if (compute_dir_size (panel->dir.list[i].fname,
1373 ui, compute_dir_size_update_ui, &marked, &total, TRUE) != FILE_CONT)
1374 break;
1376 panel->dir.list[i].st.st_size = (off_t) total;
1377 panel->dir.list[i].f.dir_size_computed = 1;
1380 compute_dir_size_destroy_ui (ui);
1382 recalculate_panel_summary (panel);
1384 if (current_panel->current_sort_field->sort_routine == (sortfn *) sort_size)
1385 panel_re_sort (panel);
1387 panel->dirty = 1;
1390 void
1391 save_setup_cmd (void)
1393 if (!save_setup ())
1394 return;
1395 message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"),
1396 MC_USERCONF_DIR PATH_SEP_STR MC_CONFIG_FILE);
1399 static void
1400 configure_panel_listing (WPanel * p, int list_type, int use_msformat, char *user, char *status)
1402 p->user_mini_status = use_msformat;
1403 p->list_type = list_type;
1405 if (list_type == list_user || use_msformat)
1407 g_free (p->user_format);
1408 p->user_format = user;
1410 g_free (p->user_status_format[list_type]);
1411 p->user_status_format[list_type] = status;
1413 set_panel_formats (p);
1415 else
1417 g_free (user);
1418 g_free (status);
1421 set_panel_formats (p);
1422 do_refresh ();
1425 void
1426 info_cmd_no_menu (void)
1428 if (get_display_type (0) == view_info)
1429 set_display_type (0, view_listing);
1430 else if (get_display_type (1) == view_info)
1431 set_display_type (1, view_listing);
1432 else
1433 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1436 void
1437 quick_cmd_no_menu (void)
1439 if (get_display_type (0) == view_quick)
1440 set_display_type (0, view_listing);
1441 else if (get_display_type (1) == view_quick)
1442 set_display_type (1, view_listing);
1443 else
1444 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1447 static void
1448 switch_to_listing (int panel_index)
1450 if (get_display_type (panel_index) != view_listing)
1451 set_display_type (panel_index, view_listing);
1454 void
1455 listing_cmd (void)
1457 int list_type;
1458 int use_msformat;
1459 char *user, *status;
1460 WPanel *p = NULL;
1462 if (get_display_type (MENU_PANEL_IDX) == view_listing)
1463 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1465 list_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1467 if (list_type != -1)
1469 switch_to_listing (MENU_PANEL_IDX);
1470 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1471 configure_panel_listing (p, list_type, use_msformat, user, status);
1475 void
1476 tree_cmd (void)
1478 set_display_type (MENU_PANEL_IDX, view_tree);
1481 void
1482 info_cmd (void)
1484 set_display_type (MENU_PANEL_IDX, view_info);
1487 void
1488 quick_view_cmd (void)
1490 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1491 change_panel ();
1492 set_display_type (MENU_PANEL_IDX, view_quick);
1495 /* Handle the tree internal listing modes switching */
1496 static gboolean
1497 set_basic_panel_listing_to (int panel_index, int listing_mode)
1499 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1500 gboolean ok;
1502 switch_to_listing (panel_index);
1503 p->list_type = listing_mode;
1505 ok = set_panel_formats (p) == 0;
1507 if (ok)
1508 do_refresh ();
1510 return ok;
1513 void
1514 toggle_listing_cmd (void)
1516 int current = get_current_index ();
1517 WPanel *p = (WPanel *) get_panel_widget (current);
1519 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1522 void
1523 encoding_cmd (void)
1525 if (SELECTED_IS_PANEL)
1526 set_panel_encoding (MENU_PANEL);