Ticket #2042: added a capability to create relative symlinks.
[midnight-commander.git] / src / cmd.c
blob859dbc8ed3f73ef0573e29c6f92e353e46a09692
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 /* selection flags */
109 typedef enum
111 SELECT_FILES_ONLY = 1 << 0,
112 SELECT_MATCH_CASE = 1 << 1,
113 SELECT_SHELL_PATTERNS = 1 << 2
114 } select_flags_t;
116 int select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
119 view_file_at_line (const char *filename, int plain_view, int internal, int start_line)
121 static const char *viewer = NULL;
122 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;
143 mcview_viewer (NULL, filename, &move_dir, start_line);
144 if (changed_hex_mode && !mcview_altered_hex_mode)
145 mcview_default_hex_mode = 1;
146 if (changed_nroff_flag && !mcview_altered_nroff_flag)
147 mcview_default_nroff_flag = 1;
148 if (changed_magic_flag && !mcview_altered_magic_flag)
149 mcview_default_magic_flag = 1;
150 repaint_screen ();
151 return move_dir;
153 if (internal)
155 char view_entry[BUF_TINY];
157 if (start_line != 0)
158 g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line);
159 else
160 strcpy (view_entry, "View");
162 if (regex_command (filename, view_entry, &move_dir) == 0)
164 mcview_viewer (NULL, filename, &move_dir, start_line);
165 repaint_screen ();
168 else
170 if (!viewer)
172 viewer = getenv ("VIEWER");
173 if (!viewer)
174 viewer = getenv ("PAGER");
175 if (!viewer)
176 viewer = "view";
178 execute_with_vfs_arg (viewer, filename);
180 return move_dir;
183 /* view_file (filename, plain_view, internal)
185 * Inputs:
186 * filename: The file name to view
187 * plain_view: If set does not do any fancy pre-processing (no filtering) and
188 * always invokes the internal viewer.
189 * internal: If set uses the internal viewer, otherwise an external viewer.
192 view_file (const char *filename, int plain_view, int internal)
194 return view_file_at_line (filename, plain_view, internal, 0);
197 /* scan_for_file (panel, idx, direction)
199 * Inputs:
200 * panel: pointer to the panel on which we operate
201 * idx: starting file.
202 * direction: 1, or -1
204 static int
205 scan_for_file (WPanel * panel, int idx, int direction)
207 int i = idx + direction;
209 while (i != idx)
211 if (i < 0)
212 i = panel->count - 1;
213 if (i == panel->count)
214 i = 0;
215 if (!S_ISDIR (panel->dir.list[i].st.st_mode))
216 return i;
217 i += direction;
219 return i;
223 * Run viewer (internal or external) on the currently selected file.
224 * If normal is 1, force internal viewer and raw mode (used for F13).
226 static void
227 do_view_cmd (int normal)
229 /* Directories are viewed by changing to them */
230 if (S_ISDIR (selection (current_panel)->st.st_mode) || link_isdir (selection (current_panel)))
232 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked))
234 if (query_dialog
235 (_(" Confirmation "), _("Files tagged, want to cd?"), D_NORMAL, 2,
236 _("&Yes"), _("&No")) != 0)
238 return;
241 if (!do_cd (selection (current_panel)->fname, cd_exact))
242 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
244 else
246 int dir, file_idx;
247 char *filename;
249 file_idx = current_panel->selected;
250 while (1)
252 filename = current_panel->dir.list[file_idx].fname;
254 dir = view_file (filename, normal, use_internal_view);
255 if (dir == 0)
256 break;
257 file_idx = scan_for_file (current_panel, file_idx, dir);
261 repaint_screen ();
264 /* Run user's preferred viewer on the currently selected file */
265 void
266 view_cmd (void)
268 do_view_cmd (0);
271 /* Ask for file and run user's preferred viewer on it */
272 void
273 view_file_cmd (void)
275 char *filename;
277 filename =
278 input_expand_dialog (_(" View file "), _(" Filename:"),
279 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
280 if (!filename)
281 return;
283 view_file (filename, 0, use_internal_view);
284 g_free (filename);
287 /* Run plain internal viewer on the currently selected file */
288 void
289 view_simple_cmd (void)
291 do_view_cmd (1);
294 void
295 filtered_view_cmd (void)
297 char *command;
299 command =
300 input_dialog (_(" Filtered view "),
301 _(" Filter command and arguments:"),
302 MC_HISTORY_FM_FILTERED_VIEW, selection (current_panel)->fname);
303 if (!command)
304 return;
306 mcview_viewer (command, "", NULL, 0);
308 g_free (command);
311 void
312 do_edit_at_line (const char *what, int start_line)
314 static const char *editor = NULL;
316 #ifdef USE_INTERNAL_EDIT
317 if (use_internal_edit)
318 edit_file (what, start_line);
319 else
320 #else
321 (void) start_line;
322 #endif /* USE_INTERNAL_EDIT */
324 if (editor == NULL)
326 editor = getenv ("EDITOR");
327 if (editor == NULL)
328 editor = get_default_editor ();
330 execute_with_vfs_arg (editor, what);
332 if (mc_run_mode == MC_RUN_FULL)
334 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
335 repaint_screen ();
339 static void
340 do_edit (const char *what)
342 do_edit_at_line (what, 0);
345 void
346 edit_cmd (void)
348 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
349 do_edit (selection (current_panel)->fname);
352 void
353 edit_cmd_new (void)
355 #if HAVE_CHARSET
356 source_codepage = default_source_codepage;
357 #endif
358 do_edit (NULL);
361 /* Invoked by F5. Copy, default to the other panel. */
362 void
363 copy_cmd (void)
365 save_cwds_stat ();
366 if (panel_operate (current_panel, OP_COPY, FALSE))
368 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
369 repaint_screen ();
373 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
374 void
375 rename_cmd (void)
377 save_cwds_stat ();
378 if (panel_operate (current_panel, OP_MOVE, FALSE))
380 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
381 repaint_screen ();
385 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
386 void
387 copy_cmd_local (void)
389 save_cwds_stat ();
390 if (panel_operate (current_panel, OP_COPY, TRUE))
392 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
393 repaint_screen ();
397 /* Invoked by F16. Move/rename, default to the same panel. */
398 void
399 rename_cmd_local (void)
401 save_cwds_stat ();
402 if (panel_operate (current_panel, OP_MOVE, TRUE))
404 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
405 repaint_screen ();
409 void
410 mkdir_cmd (void)
412 char *dir, *absdir;
413 const char *name = "";
415 /* If 'on' then automatically fills name with current selected item name */
416 if (auto_fill_mkdir_name && strcmp (selection (current_panel)->fname, "..") != 0)
417 name = selection (current_panel)->fname;
419 dir =
420 input_expand_dialog (_("Create a new Directory"),
421 _(" Enter directory name:"), MC_HISTORY_FM_MKDIR, name);
423 if (!dir)
424 return;
426 if (*dir)
428 if (dir[0] == '/' || dir[0] == '~')
429 absdir = g_strdup (dir);
430 else
431 absdir = concat_dir_and_file (current_panel->cwd, dir);
433 save_cwds_stat ();
434 if (my_mkdir (absdir, 0777) == 0)
436 update_panels (UP_OPTIMIZE, dir);
437 repaint_screen ();
438 select_item (current_panel);
440 else
442 message (D_ERROR, MSG_ERROR, " %s ", unix_error_string (errno));
444 g_free (absdir);
446 g_free (dir);
449 void
450 delete_cmd (void)
452 save_cwds_stat ();
454 if (panel_operate (current_panel, OP_DELETE, FALSE))
456 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
457 repaint_screen ();
461 /* Invoked by F18. Remove selected file, regardless of marked files. */
462 void
463 delete_cmd_local (void)
465 save_cwds_stat ();
467 if (panel_operate (current_panel, OP_DELETE, TRUE))
469 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
470 repaint_screen ();
474 void
475 find_cmd (void)
477 do_find ();
480 static void
481 set_panel_filter_to (WPanel * p, char *allocated_filter_string)
483 g_free (p->filter);
484 p->filter = 0;
486 if (!(allocated_filter_string[0] == '*' && allocated_filter_string[1] == 0))
487 p->filter = allocated_filter_string;
488 else
489 g_free (allocated_filter_string);
490 reread_cmd ();
493 /* Set a given panel filter expression */
494 static void
495 set_panel_filter (WPanel * p)
497 char *reg_exp;
498 const char *x;
500 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
502 reg_exp = input_dialog_help (_(" Filter "),
503 _(" Set expression for filtering filenames"),
504 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
505 if (!reg_exp)
506 return;
507 set_panel_filter_to (p, reg_exp);
510 /* Invoked from the left/right menus */
511 void
512 filter_cmd (void)
514 WPanel *p;
516 if (!SELECTED_IS_PANEL)
517 return;
519 p = MENU_PANEL;
520 set_panel_filter (p);
523 void
524 reread_cmd (void)
526 int flag;
528 if (get_current_type () == view_listing && get_other_type () == view_listing)
529 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
530 else
531 flag = UP_ONLY_CURRENT;
533 update_panels (UP_RELOAD | flag, UP_KEEPSEL);
534 repaint_screen ();
537 void
538 reverse_selection_cmd (void)
540 int i;
541 file_entry *file;
543 for (i = 0; i < current_panel->count; i++)
545 file = &current_panel->dir.list[i];
546 if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
547 do_file_mark (current_panel, i, !file->f.marked);
551 static void
552 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
554 /* dialog sizes */
555 const int DX = 50;
556 const int DY = 7;
558 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
559 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
560 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
562 char *reg_exp;
563 mc_search_t *search;
564 int i;
566 QuickWidget quick_widgets[] = {
567 QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns),
568 QUICK_CHECKBOX (DX / 2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens),
569 QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only),
570 QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, &reg_exp),
571 QUICK_END
574 QuickDialog quick_dlg = {
575 DX, DY, -1, -1, title,
576 "[Select/Unselect Files]", quick_widgets, FALSE
579 if (quick_dialog (&quick_dlg) == B_CANCEL)
580 return;
582 if (!reg_exp)
583 return;
584 if (!*reg_exp)
586 g_free (reg_exp);
587 return;
589 search = mc_search_new (reg_exp, -1);
590 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
591 search->is_entire_line = TRUE;
592 search->is_case_sensitive = case_sens != 0;
594 for (i = 0; i < current_panel->count; i++)
596 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
597 continue;
598 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
599 continue;
601 if (mc_search_run (search, current_panel->dir.list[i].fname,
602 0, current_panel->dir.list[i].fnamelen, NULL))
603 do_file_mark (current_panel, i, do_select);
606 mc_search_free (search);
607 g_free (reg_exp);
609 /* result flags */
610 select_flags = 0;
611 if (case_sens != 0)
612 select_flags |= SELECT_MATCH_CASE;
613 if (files_only != 0)
614 select_flags |= SELECT_FILES_ONLY;
615 if (shell_patterns != 0)
616 select_flags |= SELECT_SHELL_PATTERNS;
619 void
620 select_cmd (void)
622 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", TRUE);
625 void
626 unselect_cmd (void)
628 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", FALSE);
631 void
632 ext_cmd (void)
634 char *buffer;
635 char *extdir;
636 int dir;
638 dir = 0;
639 if (geteuid () == 0)
641 dir = query_dialog (_("Extension file edit"),
642 _(" Which extension file you want to edit? "), D_NORMAL, 2,
643 _("&User"), _("&System Wide"));
645 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
647 if (dir == 0)
649 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
650 check_for_default (extdir, buffer);
651 do_edit (buffer);
652 g_free (buffer);
654 else if (dir == 1)
656 if (!exist_file (extdir))
658 g_free (extdir);
659 extdir = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
661 do_edit (extdir);
663 g_free (extdir);
664 flush_extension_file ();
667 /* edit file menu for mc */
668 void
669 edit_mc_menu_cmd (void)
671 char *buffer;
672 char *menufile;
673 int dir = 0;
675 dir = query_dialog (_(" Menu edit "),
676 _(" Which menu file do you want to edit? "),
677 D_NORMAL, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
679 menufile = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
681 if (!exist_file (menufile))
683 g_free (menufile);
684 menufile = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
687 switch (dir)
689 case 0:
690 buffer = g_strdup (MC_LOCAL_MENU);
691 check_for_default (menufile, buffer);
692 chmod (buffer, 0600);
693 break;
695 case 1:
696 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_USERMENU_FILE, NULL);
697 check_for_default (menufile, buffer);
698 break;
700 case 2:
701 buffer = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
702 if (!exist_file (buffer))
704 g_free (buffer);
705 buffer = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
707 break;
709 default:
710 g_free (menufile);
711 return;
714 do_edit (buffer);
716 g_free (buffer);
717 g_free (menufile);
720 void
721 edit_fhl_cmd (void)
723 char *buffer = NULL;
724 char *fhlfile = NULL;
726 int dir;
728 dir = 0;
729 if (geteuid () == 0)
731 dir = query_dialog (_("Highlighting groups file edit"),
732 _(" Which highlighting file you want to edit? "), D_NORMAL, 2,
733 _("&User"), _("&System Wide"));
735 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
737 if (dir == 0)
739 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL);
740 check_for_default (fhlfile, buffer);
741 do_edit (buffer);
742 g_free (buffer);
744 else if (dir == 1)
746 if (!exist_file (fhlfile))
748 g_free (fhlfile);
749 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
751 do_edit (fhlfile);
753 g_free (fhlfile);
755 /* refresh highlighting rules */
756 mc_fhl_free (&mc_filehighlight);
757 mc_filehighlight = mc_fhl_new (TRUE);
760 void
761 quick_chdir_cmd (void)
763 char *target;
765 target = hotlist_cmd (LIST_HOTLIST);
766 if (!target)
767 return;
769 if (get_current_type () == view_tree)
770 tree_chdir (the_tree, target);
771 else
773 char *cmd = g_strconcat ("cd ", target, (char *) NULL);
774 do_cd_command (cmd);
775 g_free (cmd);
777 g_free (target);
780 #ifdef ENABLE_VFS
781 void
782 reselect_vfs (void)
784 char *target;
786 target = hotlist_cmd (LIST_VFSLIST);
787 if (!target)
788 return;
790 if (!do_cd (target, cd_exact))
791 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
792 g_free (target);
794 #endif /* ENABLE_VFS */
796 static int
797 compare_files (char *name1, char *name2, off_t size)
799 int file1, file2;
800 int result = -1; /* Different by default */
802 if (size == 0)
803 return 0;
805 file1 = open (name1, O_RDONLY);
806 if (file1 >= 0)
808 file2 = open (name2, O_RDONLY);
809 if (file2 >= 0)
811 #ifdef HAVE_MMAP
812 char *data1, *data2;
813 /* Ugly if jungle */
814 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
815 if (data1 != (char *) -1)
817 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
818 if (data2 != (char *) -1)
820 rotate_dash ();
821 result = memcmp (data1, data2, size);
822 munmap (data2, size);
824 munmap (data1, size);
826 #else
827 /* Don't have mmap() :( Even more ugly :) */
828 char buf1[BUFSIZ], buf2[BUFSIZ];
829 int n1, n2;
830 rotate_dash ();
833 while ((n1 = read (file1, buf1, BUFSIZ)) == -1 && errno == EINTR);
834 while ((n2 = read (file2, buf2, BUFSIZ)) == -1 && errno == EINTR);
836 while (n1 == n2 && n1 == BUFSIZ && !memcmp (buf1, buf2, BUFSIZ));
837 result = (n1 != n2) || memcmp (buf1, buf2, n1);
838 #endif /* !HAVE_MMAP */
839 close (file2);
841 close (file1);
843 return result;
846 enum CompareMode
848 compare_quick, compare_size_only, compare_thourough
851 static void
852 compare_dir (WPanel * panel, WPanel * other, enum CompareMode mode)
854 int i, j;
855 char *src_name, *dst_name;
857 /* No marks by default */
858 panel->marked = 0;
859 panel->total = 0;
860 panel->dirs_marked = 0;
862 /* Handle all files in the panel */
863 for (i = 0; i < panel->count; i++)
865 file_entry *source = &panel->dir.list[i];
867 /* Default: unmarked */
868 file_mark (panel, i, 0);
870 /* Skip directories */
871 if (S_ISDIR (source->st.st_mode))
872 continue;
874 /* Search the corresponding entry from the other panel */
875 for (j = 0; j < other->count; j++)
877 if (strcmp (source->fname, other->dir.list[j].fname) == 0)
878 break;
880 if (j >= other->count)
881 /* Not found -> mark */
882 do_file_mark (panel, i, 1);
883 else
885 /* Found */
886 file_entry *target = &other->dir.list[j];
888 if (mode != compare_size_only)
890 /* Older version is not marked */
891 if (source->st.st_mtime < target->st.st_mtime)
892 continue;
895 /* Newer version with different size is marked */
896 if (source->st.st_size != target->st.st_size)
898 do_file_mark (panel, i, 1);
899 continue;
902 if (mode == compare_size_only)
903 continue;
905 if (mode == compare_quick)
907 /* Thorough compare off, compare only time stamps */
908 /* Mark newer version, don't mark version with the same date */
909 if (source->st.st_mtime > target->st.st_mtime)
911 do_file_mark (panel, i, 1);
913 continue;
916 /* Thorough compare on, do byte-by-byte comparison */
917 src_name = concat_dir_and_file (panel->cwd, source->fname);
918 dst_name = concat_dir_and_file (other->cwd, target->fname);
919 if (compare_files (src_name, dst_name, source->st.st_size))
920 do_file_mark (panel, i, 1);
921 g_free (src_name);
922 g_free (dst_name);
924 } /* for (i ...) */
927 void
928 compare_dirs_cmd (void)
930 int choice;
931 enum CompareMode thorough_flag;
933 choice =
934 query_dialog (_(" Compare directories "),
935 _(" Select compare method: "), D_NORMAL, 4,
936 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
938 if (choice < 0 || choice > 2)
939 return;
941 thorough_flag = choice;
943 if (get_current_type () == view_listing && get_other_type () == view_listing)
945 compare_dir (current_panel, other_panel, thorough_flag);
946 compare_dir (other_panel, current_panel, thorough_flag);
948 else
950 message (D_ERROR, MSG_ERROR,
951 _(" Both panels should be in the " "listing mode to use this command "));
955 #ifdef USE_DIFF_VIEW
956 void
957 diff_view_cmd (void)
959 dview_diff_cmd ();
961 if (mc_run_mode == MC_RUN_FULL)
963 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
964 repaint_screen ();
967 #endif
969 void
970 history_cmd (void)
972 /* show the history of command line widget */
973 send_message (&cmdline->widget, WIDGET_COMMAND, CK_InputHistoryShow);
976 void
977 swap_cmd (void)
979 swap_panels ();
980 tty_touch_screen ();
981 repaint_screen ();
984 void
985 view_other_cmd (void)
987 static int message_flag = TRUE;
989 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell)
991 if (message_flag)
992 message (D_ERROR, MSG_ERROR,
993 _(" Not an xterm or Linux console; \n" " the panels cannot be toggled. "));
994 message_flag = FALSE;
996 else
998 toggle_panels ();
1002 static void
1003 do_link (link_type_t link_type, const char *fname)
1005 char *dest = NULL, *src = NULL;
1007 if (link_type == LINK_HARDLINK)
1009 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
1010 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
1011 if (!dest || !*dest)
1012 goto cleanup;
1013 save_cwds_stat ();
1014 if (-1 == mc_link (fname, dest))
1015 message (D_ERROR, MSG_ERROR, _(" link: %s "), unix_error_string (errno));
1017 else
1019 char *s;
1020 char *d;
1022 /* suggest the full path for symlink, and either the full or
1023 relative path to the file it points to */
1024 s = concat_dir_and_file (current_panel->cwd, fname);
1026 if (get_other_type () == view_listing)
1027 d = concat_dir_and_file (other_panel->cwd, fname);
1028 else
1029 d = g_strdup (fname);
1031 if (link_type == LINK_SYMLINK_RELATIVE)
1032 s = diff_two_paths (other_panel->cwd, s);
1034 symlink_dialog (s, d, &dest, &src);
1035 g_free (d);
1036 g_free (s);
1038 if (!dest || !*dest || !src || !*src)
1039 goto cleanup;
1040 save_cwds_stat ();
1041 if (-1 == mc_symlink (dest, src))
1042 message (D_ERROR, MSG_ERROR, _(" symlink: %s "), unix_error_string (errno));
1045 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1046 repaint_screen ();
1048 cleanup:
1049 g_free (src);
1050 g_free (dest);
1053 void
1054 link_cmd (link_type_t link_type)
1056 char *filename = selection (current_panel)->fname;
1058 if (filename != NULL)
1059 do_link (link_type, filename);
1062 void
1063 edit_symlink_cmd (void)
1065 if (S_ISLNK (selection (current_panel)->st.st_mode))
1067 char buffer[MC_MAXPATHLEN];
1068 char *p = NULL;
1069 int i;
1070 char *dest, *q;
1072 p = selection (current_panel)->fname;
1074 q = g_strdup_printf (_(" Symlink `%s\' points to: "), str_trunc (p, 32));
1076 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
1077 if (i > 0)
1079 buffer[i] = 0;
1080 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
1081 if (dest)
1083 if (*dest && strcmp (buffer, dest))
1085 save_cwds_stat ();
1086 if (-1 == mc_unlink (p))
1088 message (D_ERROR, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
1089 p, unix_error_string (errno));
1091 else
1093 if (-1 == mc_symlink (dest, p))
1094 message (D_ERROR, MSG_ERROR, _(" edit symlink: %s "),
1095 unix_error_string (errno));
1097 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1098 repaint_screen ();
1100 g_free (dest);
1103 g_free (q);
1105 else
1107 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1108 selection (current_panel)->fname);
1112 void
1113 help_cmd (void)
1115 if (current_panel->searching)
1116 interactive_display (NULL, "[Quick search]");
1117 else
1118 interactive_display (NULL, "[main]");
1121 void
1122 user_file_menu_cmd (void)
1124 user_menu_cmd (NULL);
1128 * Return a random hint. If force is not 0, ignore the timeout.
1130 char *
1131 get_random_hint (int force)
1133 char *data, *result = NULL, *eol;
1134 int len;
1135 int start;
1136 static int last_sec;
1137 static struct timeval tv;
1138 GIConv conv;
1139 GString *buffer;
1141 /* Do not change hints more often than one minute */
1142 gettimeofday (&tv, NULL);
1143 if (!force && !(tv.tv_sec > last_sec + 60))
1144 return g_strdup ("");
1145 last_sec = tv.tv_sec;
1147 data = load_mc_home_file (mc_home, mc_home_alt, MC_HINT, NULL);
1148 if (!data)
1149 return 0;
1151 /* get a random entry */
1152 srand (tv.tv_sec);
1153 len = strlen (data);
1154 start = rand () % len;
1156 for (; start; start--)
1158 if (data[start] == '\n')
1160 start++;
1161 break;
1164 eol = strchr (&data[start], '\n');
1165 if (eol)
1166 *eol = 0;
1168 /* hint files are stored in utf-8 */
1169 /* try convert hint file from utf-8 to terminal encoding */
1170 conv = str_crt_conv_from ("UTF-8");
1171 if (conv != INVALID_CONV)
1173 buffer = g_string_new ("");
1174 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1176 result = g_strdup (buffer->str);
1178 g_string_free (buffer, TRUE);
1179 str_close_conv (conv);
1182 g_free (data);
1183 return result;
1186 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1187 static void
1188 nice_cd (const char *text, const char *xtext, const char *help,
1189 const char *history_name, const char *prefix, int to_home)
1191 char *machine;
1192 char *cd_path;
1194 if (!SELECTED_IS_PANEL)
1195 return;
1197 machine = input_dialog_help (text, xtext, help, history_name, "");
1198 if (!machine)
1199 return;
1201 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1202 ugly as hell and leads to problems in vfs layer */
1204 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1205 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1206 else
1207 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1209 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1210 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1211 else
1212 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1213 g_free (cd_path);
1214 g_free (machine);
1216 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1219 #ifdef USE_NETCODE
1221 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1223 #ifdef ENABLE_VFS_MCFS
1224 void
1225 netlink_cmd (void)
1227 nice_cd (_(" Link to a remote machine "), _(machine_str),
1228 "[Network File System]", ":netlink_cmd: Link to a remote ", "/#mc:", 1);
1230 #endif /* ENABLE_VFS_MCFS */
1232 void
1233 ftplink_cmd (void)
1235 nice_cd (_(" FTP to machine "), _(machine_str),
1236 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1239 void
1240 fishlink_cmd (void)
1242 nice_cd (_(" Shell link to machine "), _(machine_str),
1243 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1244 "/#sh:", 1);
1247 #ifdef ENABLE_VFS_SMB
1248 void
1249 smblink_cmd (void)
1251 nice_cd (_(" SMB link to machine "), _(machine_str),
1252 "[SMB File System]", ":smblink_cmd: SMB link to machine ", "/#smb:", 0);
1254 #endif /* ENABLE_VFS_SMB */
1255 #endif /* USE_NETCODE */
1257 #ifdef USE_EXT2FSLIB
1258 void
1259 undelete_cmd (void)
1261 nice_cd (_(" Undelete files on an ext2 file system "),
1262 _(" Enter device (without /dev/) to undelete\n "
1263 " files on: (F1 for details)"),
1264 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "/#undel:", 0);
1266 #endif /* USE_EXT2FSLIB */
1268 void
1269 quick_cd_cmd (void)
1271 char *p = cd_dialog ();
1273 if (p && *p)
1275 char *q = g_strconcat ("cd ", p, (char *) NULL);
1277 do_cd_command (q);
1278 g_free (q);
1280 g_free (p);
1285 \brief calculate dirs sizes
1287 calculate dirs sizes and resort panel:
1288 dirs_selected = show size for selected dirs,
1289 otherwise = show size for dir under cursor:
1290 dir under cursor ".." = show size for all dirs,
1291 otherwise = show size for dir under cursor
1293 void
1294 smart_dirsize_cmd (void)
1296 WPanel *panel = current_panel;
1297 file_entry *entry;
1299 entry = &(panel->dir.list[panel->selected]);
1300 if ((S_ISDIR (entry->st.st_mode) && (strcmp (entry->fname, "..") == 0)) || panel->dirs_marked)
1301 dirsizes_cmd ();
1302 else
1303 single_dirsize_cmd ();
1306 void
1307 single_dirsize_cmd (void)
1309 WPanel *panel = current_panel;
1310 file_entry *entry;
1311 off_t marked;
1312 double total;
1313 ComputeDirSizeUI *ui;
1315 entry = &(panel->dir.list[panel->selected]);
1316 if (S_ISDIR (entry->st.st_mode) && strcmp (entry->fname, "..") != 0)
1318 ui = compute_dir_size_create_ui ();
1320 total = 0.0;
1322 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1323 &marked, &total, TRUE) == FILE_CONT)
1325 entry->st.st_size = (off_t) total;
1326 entry->f.dir_size_computed = 1;
1329 compute_dir_size_destroy_ui (ui);
1332 if (panels_options.mark_moves_down)
1333 send_message (&panel->widget, WIDGET_KEY, KEY_DOWN);
1335 recalculate_panel_summary (panel);
1337 if (current_panel->current_sort_field->sort_routine == (sortfn *) sort_size)
1338 panel_re_sort (panel);
1340 panel->dirty = 1;
1343 void
1344 dirsizes_cmd (void)
1346 WPanel *panel = current_panel;
1347 int i;
1348 off_t marked;
1349 double total;
1350 ComputeDirSizeUI *ui;
1352 ui = compute_dir_size_create_ui ();
1354 for (i = 0; i < panel->count; i++)
1355 if (S_ISDIR (panel->dir.list[i].st.st_mode)
1356 && ((panel->dirs_marked && panel->dir.list[i].f.marked)
1357 || !panel->dirs_marked) && strcmp (panel->dir.list[i].fname, "..") != 0)
1359 total = 0.0l;
1361 if (compute_dir_size (panel->dir.list[i].fname,
1362 ui, compute_dir_size_update_ui, &marked, &total, TRUE) != FILE_CONT)
1363 break;
1365 panel->dir.list[i].st.st_size = (off_t) total;
1366 panel->dir.list[i].f.dir_size_computed = 1;
1369 compute_dir_size_destroy_ui (ui);
1371 recalculate_panel_summary (panel);
1373 if (current_panel->current_sort_field->sort_routine == (sortfn *) sort_size)
1374 panel_re_sort (panel);
1376 panel->dirty = 1;
1379 void
1380 save_setup_cmd (void)
1382 if (!save_setup ())
1383 return;
1384 message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"),
1385 MC_USERCONF_DIR PATH_SEP_STR MC_CONFIG_FILE);
1388 static void
1389 configure_panel_listing (WPanel * p, int list_type, int use_msformat, char *user, char *status)
1391 p->user_mini_status = use_msformat;
1392 p->list_type = list_type;
1394 if (list_type == list_user || use_msformat)
1396 g_free (p->user_format);
1397 p->user_format = user;
1399 g_free (p->user_status_format[list_type]);
1400 p->user_status_format[list_type] = status;
1402 set_panel_formats (p);
1404 else
1406 g_free (user);
1407 g_free (status);
1410 set_panel_formats (p);
1411 do_refresh ();
1414 void
1415 info_cmd_no_menu (void)
1417 if (get_display_type (0) == view_info)
1418 set_display_type (0, view_listing);
1419 else if (get_display_type (1) == view_info)
1420 set_display_type (1, view_listing);
1421 else
1422 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1425 void
1426 quick_cmd_no_menu (void)
1428 if (get_display_type (0) == view_quick)
1429 set_display_type (0, view_listing);
1430 else if (get_display_type (1) == view_quick)
1431 set_display_type (1, view_listing);
1432 else
1433 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1436 static void
1437 switch_to_listing (int panel_index)
1439 if (get_display_type (panel_index) != view_listing)
1440 set_display_type (panel_index, view_listing);
1443 void
1444 listing_cmd (void)
1446 switch_to_listing (MENU_PANEL_IDX);
1449 void
1450 change_listing_cmd (void)
1452 int list_type;
1453 int use_msformat;
1454 char *user, *status;
1455 WPanel *p = NULL;
1457 if (get_display_type (MENU_PANEL_IDX) == view_listing)
1458 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1460 list_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1462 if (list_type != -1)
1464 switch_to_listing (MENU_PANEL_IDX);
1465 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1466 configure_panel_listing (p, list_type, use_msformat, user, status);
1470 void
1471 tree_cmd (void)
1473 set_display_type (MENU_PANEL_IDX, view_tree);
1476 void
1477 info_cmd (void)
1479 set_display_type (MENU_PANEL_IDX, view_info);
1482 void
1483 quick_view_cmd (void)
1485 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1486 change_panel ();
1487 set_display_type (MENU_PANEL_IDX, view_quick);
1490 /* Handle the tree internal listing modes switching */
1491 static gboolean
1492 set_basic_panel_listing_to (int panel_index, int listing_mode)
1494 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1495 gboolean ok;
1497 switch_to_listing (panel_index);
1498 p->list_type = listing_mode;
1500 ok = set_panel_formats (p) == 0;
1502 if (ok)
1503 do_refresh ();
1505 return ok;
1508 void
1509 toggle_listing_cmd (void)
1511 int current = get_current_index ();
1512 WPanel *p = (WPanel *) get_panel_widget (current);
1514 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1517 void
1518 encoding_cmd (void)
1520 if (SELECTED_IS_PANEL)
1521 set_panel_encoding (MENU_PANEL);