Support of multiple editors and viewers.
[midnight-commander.git] / src / cmd.c
blob57196fda4931884c81c370bd6000b070b37c112a
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 "dialog-switch.h"
71 #include "wtools.h" /* message() */
72 #include "main.h" /* change_panel() */
73 #include "panel.h" /* current_panel */
74 #include "help.h" /* interactive_display() */
75 #include "user.h" /* MC_GLOBAL_MENU */
76 #include "command.h" /* cmdline */
77 #include "layout.h" /* get_current_type() */
78 #include "ext.h" /* regex_command() */
79 #include "boxes.h" /* cd_dialog() */
80 #include "setup.h"
81 #include "execute.h" /* toggle_panels() */
82 #include "history.h"
83 #include "dir.h"
84 #include "cmddef.h" /* CK_InputHistoryShow */
86 #ifndef MAP_FILE
87 # define MAP_FILE 0
88 #endif
90 #ifdef USE_INTERNAL_EDIT
91 # include "src/editor/edit.h"
92 #endif
94 #ifdef USE_DIFF_VIEW
95 # include "src/diffviewer/ydiff.h"
96 #endif
100 /* If set and you don't have subshell support,then C-o will give you a shell */
101 int output_starts_shell = 0;
103 /* If set, use the builtin editor */
104 int use_internal_edit = 1;
106 /* Automatically fills name with current selected item name on mkdir */
107 int auto_fill_mkdir_name = 1;
109 /* selection flags */
110 typedef enum
112 SELECT_FILES_ONLY = 1 << 0,
113 SELECT_MATCH_CASE = 1 << 1,
114 SELECT_SHELL_PATTERNS = 1 << 2
115 } select_flags_t;
117 int select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
120 view_file_at_line (const char *filename, int plain_view, int internal, int start_line)
122 static const char *viewer = NULL;
123 int move_dir = 0;
125 if (plain_view)
127 int changed_hex_mode = 0;
128 int changed_nroff_flag = 0;
129 int changed_magic_flag = 0;
131 mcview_altered_hex_mode = 0;
132 mcview_altered_nroff_flag = 0;
133 mcview_altered_magic_flag = 0;
134 if (mcview_default_hex_mode)
135 changed_hex_mode = 1;
136 if (mcview_default_nroff_flag)
137 changed_nroff_flag = 1;
138 if (mcview_default_magic_flag)
139 changed_magic_flag = 1;
140 mcview_default_hex_mode = 0;
141 mcview_default_nroff_flag = 0;
142 mcview_default_magic_flag = 0;
144 switch (mcview_viewer (NULL, filename, start_line))
146 case MCVIEW_WANT_NEXT:
147 move_dir = 1;
148 break;
149 case MCVIEW_WANT_PREV:
150 move_dir = -1;
151 break;
152 default:
153 move_dir = 0;
156 if (changed_hex_mode && !mcview_altered_hex_mode)
157 mcview_default_hex_mode = 1;
158 if (changed_nroff_flag && !mcview_altered_nroff_flag)
159 mcview_default_nroff_flag = 1;
160 if (changed_magic_flag && !mcview_altered_magic_flag)
161 mcview_default_magic_flag = 1;
163 dialog_switch_process_pending ();
165 else if (internal)
167 char view_entry[BUF_TINY];
169 if (start_line != 0)
170 g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line);
171 else
172 strcpy (view_entry, "View");
174 if (regex_command (filename, view_entry, &move_dir) == 0)
176 switch (mcview_viewer (NULL, filename, start_line))
178 case MCVIEW_WANT_NEXT:
179 move_dir = 1;
180 break;
181 case MCVIEW_WANT_PREV:
182 move_dir = -1;
183 break;
184 default:
185 move_dir = 0;
188 dialog_switch_process_pending ();
191 else
193 if (viewer == NULL)
195 viewer = getenv ("VIEWER");
196 if (viewer == NULL)
197 viewer = getenv ("PAGER");
198 if (viewer == NULL)
199 viewer = "view";
202 execute_with_vfs_arg (viewer, filename);
205 return move_dir;
208 /* view_file (filename, plain_view, internal)
210 * Inputs:
211 * filename: The file name to view
212 * plain_view: If set does not do any fancy pre-processing (no filtering) and
213 * always invokes the internal viewer.
214 * internal: If set uses the internal viewer, otherwise an external viewer.
217 view_file (const char *filename, int plain_view, int internal)
219 return view_file_at_line (filename, plain_view, internal, 0);
222 /* scan_for_file (panel, idx, direction)
224 * Inputs:
225 * panel: pointer to the panel on which we operate
226 * idx: starting file.
227 * direction: 1, or -1
229 static int
230 scan_for_file (WPanel * panel, int idx, int direction)
232 int i = idx + direction;
234 while (i != idx)
236 if (i < 0)
237 i = panel->count - 1;
238 if (i == panel->count)
239 i = 0;
240 if (!S_ISDIR (panel->dir.list[i].st.st_mode))
241 return i;
242 i += direction;
244 return i;
248 * Run viewer (internal or external) on the currently selected file.
249 * If normal is 1, force internal viewer and raw mode (used for F13).
251 static void
252 do_view_cmd (int normal)
254 /* Directories are viewed by changing to them */
255 if (S_ISDIR (selection (current_panel)->st.st_mode) || link_isdir (selection (current_panel)))
257 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked))
259 if (query_dialog
260 (_("Confirmation"), _("Files tagged, want to cd?"), D_NORMAL, 2,
261 _("&Yes"), _("&No")) != 0)
263 return;
266 if (!do_cd (selection (current_panel)->fname, cd_exact))
267 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
269 else
271 int dir, file_idx;
272 char *filename;
274 file_idx = current_panel->selected;
275 while (1)
277 filename = current_panel->dir.list[file_idx].fname;
279 dir = view_file (filename, normal, use_internal_view);
280 if (dir == 0)
281 break;
282 file_idx = scan_for_file (current_panel, file_idx, dir);
286 repaint_screen ();
289 /* Run user's preferred viewer on the currently selected file */
290 void
291 view_cmd (void)
293 do_view_cmd (0);
296 /* Ask for file and run user's preferred viewer on it */
297 void
298 view_file_cmd (void)
300 char *filename;
302 filename =
303 input_expand_dialog (_("View file"), _("Filename:"),
304 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
305 if (!filename)
306 return;
308 view_file (filename, 0, use_internal_view);
309 g_free (filename);
312 /* Run plain internal viewer on the currently selected file */
313 void
314 view_simple_cmd (void)
316 do_view_cmd (1);
319 void
320 filtered_view_cmd (void)
322 char *command;
324 command =
325 input_dialog (_("Filtered view"),
326 _("Filter command and arguments:"),
327 MC_HISTORY_FM_FILTERED_VIEW, selection (current_panel)->fname);
329 if (command != NULL)
331 mcview_viewer (command, "", 0);
332 g_free (command);
333 dialog_switch_process_pending ();
337 void
338 do_edit_at_line (const char *what, int start_line)
340 static const char *editor = NULL;
342 #ifdef USE_INTERNAL_EDIT
343 if (use_internal_edit)
344 edit_file (what, start_line);
345 else
346 #else
347 (void) start_line;
348 #endif /* USE_INTERNAL_EDIT */
350 if (editor == NULL)
352 editor = getenv ("EDITOR");
353 if (editor == NULL)
354 editor = get_default_editor ();
356 execute_with_vfs_arg (editor, what);
359 if (mc_run_mode == MC_RUN_FULL)
360 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
362 #ifdef USE_INTERNAL_EDIT
363 if (use_internal_edit)
364 dialog_switch_process_pending ();
365 else
366 #endif /* USE_INTERNAL_EDIT */
367 repaint_screen ();
370 static void
371 do_edit (const char *what)
373 do_edit_at_line (what, 0);
376 void
377 edit_cmd (void)
379 if (regex_command (selection (current_panel)->fname, "Edit", NULL) == 0)
380 do_edit (selection (current_panel)->fname);
383 void
384 edit_cmd_new (void)
386 #if HAVE_CHARSET
387 source_codepage = default_source_codepage;
388 #endif
389 do_edit (NULL);
392 /* Invoked by F5. Copy, default to the other panel. */
393 void
394 copy_cmd (void)
396 save_cwds_stat ();
397 if (panel_operate (current_panel, OP_COPY, FALSE))
399 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
400 repaint_screen ();
404 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
405 void
406 rename_cmd (void)
408 save_cwds_stat ();
409 if (panel_operate (current_panel, OP_MOVE, FALSE))
411 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
412 repaint_screen ();
416 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
417 void
418 copy_cmd_local (void)
420 save_cwds_stat ();
421 if (panel_operate (current_panel, OP_COPY, TRUE))
423 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
424 repaint_screen ();
428 /* Invoked by F16. Move/rename, default to the same panel. */
429 void
430 rename_cmd_local (void)
432 save_cwds_stat ();
433 if (panel_operate (current_panel, OP_MOVE, TRUE))
435 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
436 repaint_screen ();
440 void
441 mkdir_cmd (void)
443 char *dir, *absdir;
444 const char *name = "";
446 /* If 'on' then automatically fills name with current selected item name */
447 if (auto_fill_mkdir_name && strcmp (selection (current_panel)->fname, "..") != 0)
448 name = selection (current_panel)->fname;
450 dir =
451 input_expand_dialog (_("Create a new Directory"),
452 _("Enter directory name:"), MC_HISTORY_FM_MKDIR, name);
454 if (!dir)
455 return;
457 if (*dir)
459 if (dir[0] == '/' || dir[0] == '~')
460 absdir = g_strdup (dir);
461 else
462 absdir = concat_dir_and_file (current_panel->cwd, dir);
464 save_cwds_stat ();
465 if (my_mkdir (absdir, 0777) == 0)
467 update_panels (UP_OPTIMIZE, dir);
468 repaint_screen ();
469 select_item (current_panel);
471 else
473 message (D_ERROR, MSG_ERROR, "%s", unix_error_string (errno));
475 g_free (absdir);
477 g_free (dir);
480 void
481 delete_cmd (void)
483 save_cwds_stat ();
485 if (panel_operate (current_panel, OP_DELETE, FALSE))
487 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
488 repaint_screen ();
492 /* Invoked by F18. Remove selected file, regardless of marked files. */
493 void
494 delete_cmd_local (void)
496 save_cwds_stat ();
498 if (panel_operate (current_panel, OP_DELETE, TRUE))
500 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
501 repaint_screen ();
505 void
506 find_cmd (void)
508 do_find ();
511 static void
512 set_panel_filter_to (WPanel * p, char *allocated_filter_string)
514 g_free (p->filter);
515 p->filter = 0;
517 if (!(allocated_filter_string[0] == '*' && allocated_filter_string[1] == 0))
518 p->filter = allocated_filter_string;
519 else
520 g_free (allocated_filter_string);
521 reread_cmd ();
524 /* Set a given panel filter expression */
525 static void
526 set_panel_filter (WPanel * p)
528 char *reg_exp;
529 const char *x;
531 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
533 reg_exp = input_dialog_help (_("Filter"),
534 _("Set expression for filtering filenames"),
535 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
536 if (!reg_exp)
537 return;
538 set_panel_filter_to (p, reg_exp);
541 /* Invoked from the left/right menus */
542 void
543 filter_cmd (void)
545 WPanel *p;
547 if (!SELECTED_IS_PANEL)
548 return;
550 p = MENU_PANEL;
551 set_panel_filter (p);
554 void
555 reread_cmd (void)
557 int flag;
559 if (get_current_type () == view_listing && get_other_type () == view_listing)
560 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
561 else
562 flag = UP_ONLY_CURRENT;
564 update_panels (UP_RELOAD | flag, UP_KEEPSEL);
565 repaint_screen ();
568 void
569 reverse_selection_cmd (void)
571 int i;
572 file_entry *file;
574 for (i = 0; i < current_panel->count; i++)
576 file = &current_panel->dir.list[i];
577 if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
578 do_file_mark (current_panel, i, !file->f.marked);
582 static void
583 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
585 /* dialog sizes */
586 const int DX = 50;
587 const int DY = 7;
589 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
590 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
591 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
593 char *reg_exp;
594 mc_search_t *search;
595 int i;
597 QuickWidget quick_widgets[] = {
598 QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns),
599 QUICK_CHECKBOX (DX / 2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens),
600 QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only),
601 QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, &reg_exp),
602 QUICK_END
605 QuickDialog quick_dlg = {
606 DX, DY, -1, -1, title,
607 "[Select/Unselect Files]", quick_widgets, FALSE
610 if (quick_dialog (&quick_dlg) == B_CANCEL)
611 return;
613 if (!reg_exp)
614 return;
615 if (!*reg_exp)
617 g_free (reg_exp);
618 return;
620 search = mc_search_new (reg_exp, -1);
621 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
622 search->is_entire_line = TRUE;
623 search->is_case_sensitive = case_sens != 0;
625 for (i = 0; i < current_panel->count; i++)
627 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
628 continue;
629 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
630 continue;
632 if (mc_search_run (search, current_panel->dir.list[i].fname,
633 0, current_panel->dir.list[i].fnamelen, NULL))
634 do_file_mark (current_panel, i, do_select);
637 mc_search_free (search);
638 g_free (reg_exp);
640 /* result flags */
641 select_flags = 0;
642 if (case_sens != 0)
643 select_flags |= SELECT_MATCH_CASE;
644 if (files_only != 0)
645 select_flags |= SELECT_FILES_ONLY;
646 if (shell_patterns != 0)
647 select_flags |= SELECT_SHELL_PATTERNS;
650 void
651 select_cmd (void)
653 select_unselect_cmd (_("Select"), ":select_cmd: Select ", TRUE);
656 void
657 unselect_cmd (void)
659 select_unselect_cmd (_("Unselect"), ":unselect_cmd: Unselect ", FALSE);
662 void
663 ext_cmd (void)
665 char *buffer;
666 char *extdir;
667 int dir;
669 dir = 0;
670 if (geteuid () == 0)
672 dir = query_dialog (_("Extension file edit"),
673 _("Which extension file you want to edit?"), D_NORMAL, 2,
674 _("&User"), _("&System Wide"));
676 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
678 if (dir == 0)
680 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
681 check_for_default (extdir, buffer);
682 do_edit (buffer);
683 g_free (buffer);
685 else if (dir == 1)
687 if (!exist_file (extdir))
689 g_free (extdir);
690 extdir = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
692 do_edit (extdir);
694 g_free (extdir);
695 flush_extension_file ();
698 /* edit file menu for mc */
699 void
700 edit_mc_menu_cmd (void)
702 char *buffer;
703 char *menufile;
704 int dir = 0;
706 dir = query_dialog (_("Menu edit"),
707 _("Which menu file do you want to edit?"),
708 D_NORMAL, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
710 menufile = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
712 if (!exist_file (menufile))
714 g_free (menufile);
715 menufile = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
718 switch (dir)
720 case 0:
721 buffer = g_strdup (MC_LOCAL_MENU);
722 check_for_default (menufile, buffer);
723 chmod (buffer, 0600);
724 break;
726 case 1:
727 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_USERMENU_FILE, NULL);
728 check_for_default (menufile, buffer);
729 break;
731 case 2:
732 buffer = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
733 if (!exist_file (buffer))
735 g_free (buffer);
736 buffer = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
738 break;
740 default:
741 g_free (menufile);
742 return;
745 do_edit (buffer);
747 g_free (buffer);
748 g_free (menufile);
751 void
752 edit_fhl_cmd (void)
754 char *buffer = NULL;
755 char *fhlfile = NULL;
757 int dir;
759 dir = 0;
760 if (geteuid () == 0)
762 dir = query_dialog (_("Highlighting groups file edit"),
763 _("Which highlighting file you want to edit?"), D_NORMAL, 2,
764 _("&User"), _("&System Wide"));
766 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
768 if (dir == 0)
770 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL);
771 check_for_default (fhlfile, buffer);
772 do_edit (buffer);
773 g_free (buffer);
775 else if (dir == 1)
777 if (!exist_file (fhlfile))
779 g_free (fhlfile);
780 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
782 do_edit (fhlfile);
784 g_free (fhlfile);
786 /* refresh highlighting rules */
787 mc_fhl_free (&mc_filehighlight);
788 mc_filehighlight = mc_fhl_new (TRUE);
791 void
792 quick_chdir_cmd (void)
794 char *target;
796 target = hotlist_cmd (LIST_HOTLIST);
797 if (!target)
798 return;
800 if (get_current_type () == view_tree)
801 tree_chdir (the_tree, target);
802 else
804 char *cmd = g_strconcat ("cd ", target, (char *) NULL);
805 do_cd_command (cmd);
806 g_free (cmd);
808 g_free (target);
811 #ifdef ENABLE_VFS
812 void
813 reselect_vfs (void)
815 char *target;
817 target = hotlist_cmd (LIST_VFSLIST);
818 if (!target)
819 return;
821 if (!do_cd (target, cd_exact))
822 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
823 g_free (target);
825 #endif /* ENABLE_VFS */
827 static int
828 compare_files (char *name1, char *name2, off_t size)
830 int file1, file2;
831 int result = -1; /* Different by default */
833 if (size == 0)
834 return 0;
836 file1 = open (name1, O_RDONLY);
837 if (file1 >= 0)
839 file2 = open (name2, O_RDONLY);
840 if (file2 >= 0)
842 #ifdef HAVE_MMAP
843 char *data1, *data2;
844 /* Ugly if jungle */
845 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
846 if (data1 != (char *) -1)
848 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
849 if (data2 != (char *) -1)
851 rotate_dash ();
852 result = memcmp (data1, data2, size);
853 munmap (data2, size);
855 munmap (data1, size);
857 #else
858 /* Don't have mmap() :( Even more ugly :) */
859 char buf1[BUFSIZ], buf2[BUFSIZ];
860 int n1, n2;
861 rotate_dash ();
864 while ((n1 = read (file1, buf1, BUFSIZ)) == -1 && errno == EINTR);
865 while ((n2 = read (file2, buf2, BUFSIZ)) == -1 && errno == EINTR);
867 while (n1 == n2 && n1 == BUFSIZ && !memcmp (buf1, buf2, BUFSIZ));
868 result = (n1 != n2) || memcmp (buf1, buf2, n1);
869 #endif /* !HAVE_MMAP */
870 close (file2);
872 close (file1);
874 return result;
877 enum CompareMode
879 compare_quick, compare_size_only, compare_thourough
882 static void
883 compare_dir (WPanel * panel, WPanel * other, enum CompareMode mode)
885 int i, j;
886 char *src_name, *dst_name;
888 /* No marks by default */
889 panel->marked = 0;
890 panel->total = 0;
891 panel->dirs_marked = 0;
893 /* Handle all files in the panel */
894 for (i = 0; i < panel->count; i++)
896 file_entry *source = &panel->dir.list[i];
898 /* Default: unmarked */
899 file_mark (panel, i, 0);
901 /* Skip directories */
902 if (S_ISDIR (source->st.st_mode))
903 continue;
905 /* Search the corresponding entry from the other panel */
906 for (j = 0; j < other->count; j++)
908 if (strcmp (source->fname, other->dir.list[j].fname) == 0)
909 break;
911 if (j >= other->count)
912 /* Not found -> mark */
913 do_file_mark (panel, i, 1);
914 else
916 /* Found */
917 file_entry *target = &other->dir.list[j];
919 if (mode != compare_size_only)
921 /* Older version is not marked */
922 if (source->st.st_mtime < target->st.st_mtime)
923 continue;
926 /* Newer version with different size is marked */
927 if (source->st.st_size != target->st.st_size)
929 do_file_mark (panel, i, 1);
930 continue;
933 if (mode == compare_size_only)
934 continue;
936 if (mode == compare_quick)
938 /* Thorough compare off, compare only time stamps */
939 /* Mark newer version, don't mark version with the same date */
940 if (source->st.st_mtime > target->st.st_mtime)
942 do_file_mark (panel, i, 1);
944 continue;
947 /* Thorough compare on, do byte-by-byte comparison */
948 src_name = concat_dir_and_file (panel->cwd, source->fname);
949 dst_name = concat_dir_and_file (other->cwd, target->fname);
950 if (compare_files (src_name, dst_name, source->st.st_size))
951 do_file_mark (panel, i, 1);
952 g_free (src_name);
953 g_free (dst_name);
955 } /* for (i ...) */
958 void
959 compare_dirs_cmd (void)
961 int choice;
962 enum CompareMode thorough_flag;
964 choice =
965 query_dialog (_("Compare directories"),
966 _("Select compare method:"), D_NORMAL, 4,
967 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
969 if (choice < 0 || choice > 2)
970 return;
972 thorough_flag = choice;
974 if (get_current_type () == view_listing && get_other_type () == view_listing)
976 compare_dir (current_panel, other_panel, thorough_flag);
977 compare_dir (other_panel, current_panel, thorough_flag);
979 else
981 message (D_ERROR, MSG_ERROR,
982 _("Both panels should be in the listing mode\nto use this command"));
986 #ifdef USE_DIFF_VIEW
987 void
988 diff_view_cmd (void)
990 dview_diff_cmd ();
992 if (mc_run_mode == MC_RUN_FULL)
993 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
995 dialog_switch_process_pending ();
997 #endif
999 void
1000 history_cmd (void)
1002 /* show the history of command line widget */
1003 send_message (&cmdline->widget, WIDGET_COMMAND, CK_InputHistoryShow);
1006 void
1007 swap_cmd (void)
1009 swap_panels ();
1010 tty_touch_screen ();
1011 repaint_screen ();
1014 void
1015 view_other_cmd (void)
1017 static int message_flag = TRUE;
1019 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell)
1021 if (message_flag)
1022 message (D_ERROR, MSG_ERROR,
1023 _("Not an xterm or Linux console;\nthe panels cannot be toggled."));
1024 message_flag = FALSE;
1026 else
1028 toggle_panels ();
1032 static void
1033 do_link (link_type_t link_type, const char *fname)
1035 char *dest = NULL, *src = NULL;
1037 if (link_type == LINK_HARDLINK)
1039 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
1040 dest = input_expand_dialog (_("Link"), src, MC_HISTORY_FM_LINK, "");
1041 if (!dest || !*dest)
1042 goto cleanup;
1043 save_cwds_stat ();
1044 if (-1 == mc_link (fname, dest))
1045 message (D_ERROR, MSG_ERROR, _("link: %s"), unix_error_string (errno));
1047 else
1049 char *s;
1050 char *d;
1052 /* suggest the full path for symlink, and either the full or
1053 relative path to the file it points to */
1054 s = concat_dir_and_file (current_panel->cwd, fname);
1056 if (get_other_type () == view_listing)
1057 d = concat_dir_and_file (other_panel->cwd, fname);
1058 else
1059 d = g_strdup (fname);
1061 if (link_type == LINK_SYMLINK_RELATIVE)
1062 s = diff_two_paths (other_panel->cwd, s);
1064 symlink_dialog (s, d, &dest, &src);
1065 g_free (d);
1066 g_free (s);
1068 if (!dest || !*dest || !src || !*src)
1069 goto cleanup;
1070 save_cwds_stat ();
1071 if (-1 == mc_symlink (dest, src))
1072 message (D_ERROR, MSG_ERROR, _("symlink: %s"), unix_error_string (errno));
1075 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1076 repaint_screen ();
1078 cleanup:
1079 g_free (src);
1080 g_free (dest);
1083 void
1084 link_cmd (link_type_t link_type)
1086 char *filename = selection (current_panel)->fname;
1088 if (filename != NULL)
1089 do_link (link_type, filename);
1092 void
1093 edit_symlink_cmd (void)
1095 if (S_ISLNK (selection (current_panel)->st.st_mode))
1097 char buffer[MC_MAXPATHLEN];
1098 char *p = NULL;
1099 int i;
1100 char *dest, *q;
1102 p = selection (current_panel)->fname;
1104 q = g_strdup_printf (_("Symlink `%s\' points to:"), str_trunc (p, 32));
1106 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
1107 if (i > 0)
1109 buffer[i] = 0;
1110 dest = input_expand_dialog (_("Edit symlink"), q, MC_HISTORY_FM_EDIT_LINK, buffer);
1111 if (dest)
1113 if (*dest && strcmp (buffer, dest))
1115 save_cwds_stat ();
1116 if (-1 == mc_unlink (p))
1118 message (D_ERROR, MSG_ERROR, _("edit symlink, unable to remove %s: %s"),
1119 p, unix_error_string (errno));
1121 else
1123 if (-1 == mc_symlink (dest, p))
1124 message (D_ERROR, MSG_ERROR, _("edit symlink: %s"),
1125 unix_error_string (errno));
1127 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1128 repaint_screen ();
1130 g_free (dest);
1133 g_free (q);
1135 else
1137 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1138 selection (current_panel)->fname);
1142 void
1143 help_cmd (void)
1145 if (current_panel->searching)
1146 interactive_display (NULL, "[Quick search]");
1147 else
1148 interactive_display (NULL, "[main]");
1151 void
1152 user_file_menu_cmd (void)
1154 user_menu_cmd (NULL);
1158 * Return a random hint. If force is not 0, ignore the timeout.
1160 char *
1161 get_random_hint (int force)
1163 char *data, *result = NULL, *eol;
1164 int len;
1165 int start;
1166 static int last_sec;
1167 static struct timeval tv;
1168 GIConv conv;
1169 GString *buffer;
1171 /* Do not change hints more often than one minute */
1172 gettimeofday (&tv, NULL);
1173 if (!force && !(tv.tv_sec > last_sec + 60))
1174 return g_strdup ("");
1175 last_sec = tv.tv_sec;
1177 data = load_mc_home_file (mc_home, mc_home_alt, MC_HINT, NULL);
1178 if (!data)
1179 return 0;
1181 /* get a random entry */
1182 srand (tv.tv_sec);
1183 len = strlen (data);
1184 start = rand () % len;
1186 for (; start; start--)
1188 if (data[start] == '\n')
1190 start++;
1191 break;
1194 eol = strchr (&data[start], '\n');
1195 if (eol)
1196 *eol = 0;
1198 /* hint files are stored in utf-8 */
1199 /* try convert hint file from utf-8 to terminal encoding */
1200 conv = str_crt_conv_from ("UTF-8");
1201 if (conv != INVALID_CONV)
1203 buffer = g_string_new ("");
1204 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1206 result = g_strdup (buffer->str);
1208 g_string_free (buffer, TRUE);
1209 str_close_conv (conv);
1212 g_free (data);
1213 return result;
1216 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1217 static void
1218 nice_cd (const char *text, const char *xtext, const char *help,
1219 const char *history_name, const char *prefix, int to_home)
1221 char *machine;
1222 char *cd_path;
1224 if (!SELECTED_IS_PANEL)
1225 return;
1227 machine = input_dialog_help (text, xtext, help, history_name, "");
1228 if (!machine)
1229 return;
1231 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1232 ugly as hell and leads to problems in vfs layer */
1234 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1235 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1236 else
1237 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1239 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1240 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1241 else
1242 message (D_ERROR, MSG_ERROR, _("Cannot chdir to %s"), cd_path);
1243 g_free (cd_path);
1244 g_free (machine);
1246 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1249 #ifdef USE_NETCODE
1251 static const char *machine_str = N_("Enter machine name (F1 for details):");
1253 #ifdef ENABLE_VFS_MCFS
1254 void
1255 netlink_cmd (void)
1257 nice_cd (_("Link to a remote machine"), _(machine_str),
1258 "[Network File System]", ":netlink_cmd: Link to a remote ", "/#mc:", 1);
1260 #endif /* ENABLE_VFS_MCFS */
1262 void
1263 ftplink_cmd (void)
1265 nice_cd (_("FTP to machine"), _(machine_str),
1266 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1269 void
1270 fishlink_cmd (void)
1272 nice_cd (_("Shell link to machine"), _(machine_str),
1273 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1274 "/#sh:", 1);
1277 #ifdef ENABLE_VFS_SMB
1278 void
1279 smblink_cmd (void)
1281 nice_cd (_("SMB link to machine"), _(machine_str),
1282 "[SMB File System]", ":smblink_cmd: SMB link to machine ", "/#smb:", 0);
1284 #endif /* ENABLE_VFS_SMB */
1285 #endif /* USE_NETCODE */
1287 #ifdef USE_EXT2FSLIB
1288 void
1289 undelete_cmd (void)
1291 nice_cd (_("Undelete files on an ext2 file system"),
1292 _("Enter device (without /dev/) to undelete\nfiles on: (F1 for details)"),
1293 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "/#undel:", 0);
1295 #endif /* USE_EXT2FSLIB */
1297 void
1298 quick_cd_cmd (void)
1300 char *p = cd_dialog ();
1302 if (p && *p)
1304 char *q = g_strconcat ("cd ", p, (char *) NULL);
1306 do_cd_command (q);
1307 g_free (q);
1309 g_free (p);
1314 \brief calculate dirs sizes
1316 calculate dirs sizes and resort panel:
1317 dirs_selected = show size for selected dirs,
1318 otherwise = show size for dir under cursor:
1319 dir under cursor ".." = show size for all dirs,
1320 otherwise = show size for dir under cursor
1322 void
1323 smart_dirsize_cmd (void)
1325 WPanel *panel = current_panel;
1326 file_entry *entry;
1328 entry = &(panel->dir.list[panel->selected]);
1329 if ((S_ISDIR (entry->st.st_mode) && (strcmp (entry->fname, "..") == 0)) || panel->dirs_marked)
1330 dirsizes_cmd ();
1331 else
1332 single_dirsize_cmd ();
1335 void
1336 single_dirsize_cmd (void)
1338 WPanel *panel = current_panel;
1339 file_entry *entry;
1340 off_t marked;
1341 double total;
1342 ComputeDirSizeUI *ui;
1344 entry = &(panel->dir.list[panel->selected]);
1345 if (S_ISDIR (entry->st.st_mode) && strcmp (entry->fname, "..") != 0)
1347 ui = compute_dir_size_create_ui ();
1349 total = 0.0;
1351 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1352 &marked, &total, TRUE) == FILE_CONT)
1354 entry->st.st_size = (off_t) total;
1355 entry->f.dir_size_computed = 1;
1358 compute_dir_size_destroy_ui (ui);
1361 if (panels_options.mark_moves_down)
1362 send_message (&panel->widget, WIDGET_KEY, KEY_DOWN);
1364 recalculate_panel_summary (panel);
1366 if (current_panel->current_sort_field->sort_routine == (sortfn *) sort_size)
1367 panel_re_sort (panel);
1369 panel->dirty = 1;
1372 void
1373 dirsizes_cmd (void)
1375 WPanel *panel = current_panel;
1376 int i;
1377 off_t marked;
1378 double total;
1379 ComputeDirSizeUI *ui;
1381 ui = compute_dir_size_create_ui ();
1383 for (i = 0; i < panel->count; i++)
1384 if (S_ISDIR (panel->dir.list[i].st.st_mode)
1385 && ((panel->dirs_marked && panel->dir.list[i].f.marked)
1386 || !panel->dirs_marked) && strcmp (panel->dir.list[i].fname, "..") != 0)
1388 total = 0.0l;
1390 if (compute_dir_size (panel->dir.list[i].fname,
1391 ui, compute_dir_size_update_ui, &marked, &total, TRUE) != FILE_CONT)
1392 break;
1394 panel->dir.list[i].st.st_size = (off_t) total;
1395 panel->dir.list[i].f.dir_size_computed = 1;
1398 compute_dir_size_destroy_ui (ui);
1400 recalculate_panel_summary (panel);
1402 if (current_panel->current_sort_field->sort_routine == (sortfn *) sort_size)
1403 panel_re_sort (panel);
1405 panel->dirty = 1;
1408 void
1409 save_setup_cmd (void)
1411 if (!save_setup ())
1412 return;
1413 message (D_NORMAL, _("Setup"), _("Setup saved to ~/%s"),
1414 MC_USERCONF_DIR PATH_SEP_STR MC_CONFIG_FILE);
1417 static void
1418 configure_panel_listing (WPanel * p, int list_type, int use_msformat, char *user, char *status)
1420 p->user_mini_status = use_msformat;
1421 p->list_type = list_type;
1423 if (list_type == list_user || use_msformat)
1425 g_free (p->user_format);
1426 p->user_format = user;
1428 g_free (p->user_status_format[list_type]);
1429 p->user_status_format[list_type] = status;
1431 set_panel_formats (p);
1433 else
1435 g_free (user);
1436 g_free (status);
1439 set_panel_formats (p);
1440 do_refresh ();
1443 void
1444 info_cmd_no_menu (void)
1446 if (get_display_type (0) == view_info)
1447 set_display_type (0, view_listing);
1448 else if (get_display_type (1) == view_info)
1449 set_display_type (1, view_listing);
1450 else
1451 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1454 void
1455 quick_cmd_no_menu (void)
1457 if (get_display_type (0) == view_quick)
1458 set_display_type (0, view_listing);
1459 else if (get_display_type (1) == view_quick)
1460 set_display_type (1, view_listing);
1461 else
1462 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1465 static void
1466 switch_to_listing (int panel_index)
1468 if (get_display_type (panel_index) != view_listing)
1469 set_display_type (panel_index, view_listing);
1472 void
1473 listing_cmd (void)
1475 switch_to_listing (MENU_PANEL_IDX);
1478 void
1479 change_listing_cmd (void)
1481 int list_type;
1482 int use_msformat;
1483 char *user, *status;
1484 WPanel *p = NULL;
1486 if (get_display_type (MENU_PANEL_IDX) == view_listing)
1487 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1489 list_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1491 if (list_type != -1)
1493 switch_to_listing (MENU_PANEL_IDX);
1494 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1495 configure_panel_listing (p, list_type, use_msformat, user, status);
1499 void
1500 tree_cmd (void)
1502 set_display_type (MENU_PANEL_IDX, view_tree);
1505 void
1506 info_cmd (void)
1508 set_display_type (MENU_PANEL_IDX, view_info);
1511 void
1512 quick_view_cmd (void)
1514 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1515 change_panel ();
1516 set_display_type (MENU_PANEL_IDX, view_quick);
1519 /* Handle the tree internal listing modes switching */
1520 static gboolean
1521 set_basic_panel_listing_to (int panel_index, int listing_mode)
1523 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1524 gboolean ok;
1526 switch_to_listing (panel_index);
1527 p->list_type = listing_mode;
1529 ok = set_panel_formats (p) == 0;
1531 if (ok)
1532 do_refresh ();
1534 return ok;
1537 void
1538 toggle_listing_cmd (void)
1540 int current = get_current_index ();
1541 WPanel *p = (WPanel *) get_panel_widget (current);
1543 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1546 void
1547 encoding_cmd (void)
1549 if (SELECTED_IS_PANEL)
1550 set_panel_encoding (MENU_PANEL);