Merge branch '54_dlg_mouse_handling'
[midnight-commander.git] / src / cmd.c
blob58a22dfaa745194d2935793da3ee2cb02d4e91c8
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 "global.h"
50 #include "../src/tty/tty.h" /* LINES, tty_touch_screen() */
51 #include "../src/tty/key.h" /* ALT() macro */
52 #include "../src/tty/win.h" /* do_enter_ca_mode() */
54 #include "../src/mcconfig/mcconfig.h"
55 #include "../src/search/search.h"
56 #include "../src/viewer/mcviewer.h"
57 #include "../src/filehighlight/fhl.h" /* MC_FHL_INI_FILE */
59 #include "cmd.h" /* Our definitions */
60 #include "fileopctx.h"
61 #include "file.h" /* file operation routines */
62 #include "find.h" /* do_find() */
63 #include "hotlist.h" /* hotlist_cmd() */
64 #include "tree.h" /* tree_chdir() */
65 #include "subshell.h" /* use_subshell */
66 #include "cons.saver.h" /* console_flag */
67 #include "dialog.h" /* Widget */
68 #include "wtools.h" /* message() */
69 #include "widget.h" /* push_history() */
70 #include "main.h" /* change_panel() */
71 #include "panel.h" /* current_panel */
72 #include "help.h" /* interactive_display() */
73 #include "user.h" /* MC_GLOBAL_MENU */
74 #include "command.h" /* cmdline */
75 #include "layout.h" /* get_current_type() */
76 #include "ext.h" /* regex_command() */
77 #include "boxes.h" /* cd_dialog() */
78 #include "setup.h"
79 #include "execute.h" /* toggle_panels() */
80 #include "history.h"
81 #include "strutil.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 "../edit/edit.h"
91 #endif
93 /* If set and you don't have subshell support,then C-o will give you a shell */
94 int output_starts_shell = 0;
96 /* If set, use the builtin editor */
97 int use_internal_edit = 1;
99 /* Automatically fills name with current selected item name on mkdir */
100 int auto_fill_mkdir_name = 1;
102 /* if set, only selection of files is inverted */
103 int reverse_files_only = 1;
105 /* selection flags */
106 typedef enum {
107 SELECT_FILES_ONLY = 1 << 0,
108 SELECT_MATCH_CASE = 1 << 1,
109 SELECT_SHELL_PATTERNS = 1 << 2
110 } select_flags_t;
112 static select_flags_t select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
115 view_file_at_line (const char *filename, int plain_view, int internal,
116 int start_line)
118 static const char *viewer = NULL;
119 int move_dir = 0;
122 if (plain_view) {
123 int changed_hex_mode = 0;
124 int changed_nroff_flag = 0;
125 int changed_magic_flag = 0;
127 mcview_altered_hex_mode = 0;
128 mcview_altered_nroff_flag = 0;
129 mcview_altered_magic_flag = 0;
130 if (mcview_default_hex_mode)
131 changed_hex_mode = 1;
132 if (mcview_default_nroff_flag)
133 changed_nroff_flag = 1;
134 if (mcview_default_magic_flag)
135 changed_magic_flag = 1;
136 mcview_default_hex_mode = 0;
137 mcview_default_nroff_flag = 0;
138 mcview_default_magic_flag = 0;
139 mcview_viewer (NULL, filename, &move_dir, start_line);
140 if (changed_hex_mode && !mcview_altered_hex_mode)
141 mcview_default_hex_mode = 1;
142 if (changed_nroff_flag && !mcview_altered_nroff_flag)
143 mcview_default_nroff_flag = 1;
144 if (changed_magic_flag && !mcview_altered_magic_flag)
145 mcview_default_magic_flag = 1;
146 repaint_screen ();
147 return move_dir;
149 if (internal) {
150 char view_entry[BUF_TINY];
152 if (start_line != 0)
153 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
154 start_line);
155 else
156 strcpy (view_entry, "View");
158 if (regex_command (filename, view_entry, &move_dir) == 0) {
159 mcview_viewer (NULL, filename, &move_dir, start_line);
160 repaint_screen ();
162 } else {
163 if (!viewer) {
164 viewer = getenv ("VIEWER");
165 if (!viewer)
166 viewer = getenv ("PAGER");
167 if (!viewer)
168 viewer = "view";
170 execute_with_vfs_arg (viewer, filename);
172 return move_dir;
175 /* view_file (filename, plain_view, internal)
177 * Inputs:
178 * filename: The file name to view
179 * plain_view: If set does not do any fancy pre-processing (no filtering) and
180 * always invokes the internal viewer.
181 * internal: If set uses the internal viewer, otherwise an external viewer.
184 view_file (const char *filename, int plain_view, int internal)
186 return view_file_at_line (filename, plain_view, internal, 0);
189 /* scan_for_file (panel, idx, direction)
191 * Inputs:
192 * panel: pointer to the panel on which we operate
193 * idx: starting file.
194 * direction: 1, or -1
196 static int scan_for_file (WPanel *panel, int idx, int direction)
198 int i = idx + direction;
200 while (i != idx){
201 if (i < 0)
202 i = panel->count - 1;
203 if (i == panel->count)
204 i = 0;
205 if (!S_ISDIR (panel->dir.list [i].st.st_mode))
206 return i;
207 i += direction;
209 return i;
213 * Run viewer (internal or external) on the currently selected file.
214 * If normal is 1, force internal viewer and raw mode (used for F13).
216 static void
217 do_view_cmd (int normal)
219 /* Directories are viewed by changing to them */
220 if (S_ISDIR (selection (current_panel)->st.st_mode)
221 || link_isdir (selection (current_panel))) {
222 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked)) {
223 if (query_dialog
224 (_(" Confirmation "), _("Files tagged, want to cd?"), D_NORMAL, 2,
225 _("&Yes"), _("&No")) != 0) {
226 return;
229 if (!do_cd (selection (current_panel)->fname, cd_exact))
230 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
231 } else {
232 int dir, file_idx;
233 char *filename;
235 file_idx = current_panel->selected;
236 while (1) {
237 filename = current_panel->dir.list[file_idx].fname;
239 dir = view_file (filename, normal, use_internal_view);
240 if (dir == 0)
241 break;
242 file_idx = scan_for_file (current_panel, file_idx, dir);
246 repaint_screen();
249 /* Run user's preferred viewer on the currently selected file */
250 void
251 view_cmd (void)
253 do_view_cmd (0);
256 /* Ask for file and run user's preferred viewer on it */
257 void
258 view_file_cmd (void)
260 char *filename;
262 filename =
263 input_expand_dialog (_(" View file "), _(" Filename:"),
264 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
265 if (!filename)
266 return;
268 view_file (filename, 0, use_internal_view);
269 g_free (filename);
272 /* Run plain internal viewer on the currently selected file */
273 void
274 view_simple_cmd (void)
276 do_view_cmd (1);
279 void
280 filtered_view_cmd (void)
282 char *command;
284 command =
285 input_dialog (_(" Filtered view "),
286 _(" Filter command and arguments:"),
287 MC_HISTORY_FM_FILTERED_VIEW,
288 selection (current_panel)->fname);
289 if (!command)
290 return;
292 mcview_viewer (command, "", NULL, 0);
294 g_free (command);
297 void
298 do_edit_at_line (const char *what, int start_line)
300 static const char *editor = NULL;
302 #ifdef USE_INTERNAL_EDIT
303 if (use_internal_edit)
304 edit_file (what, start_line);
305 else
306 #endif /* USE_INTERNAL_EDIT */
308 if (editor == NULL) {
309 editor = getenv ("EDITOR");
310 if (editor == NULL)
311 editor = get_default_editor ();
313 execute_with_vfs_arg (editor, what);
315 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
316 repaint_screen ();
319 static void
320 do_edit (const char *what)
322 do_edit_at_line (what, 0);
325 void
326 edit_cmd (void)
328 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
329 do_edit (selection (current_panel)->fname);
332 void
333 edit_cmd_new (void)
335 do_edit (NULL);
338 /* Invoked by F5. Copy, default to the other panel. */
339 void
340 copy_cmd (void)
342 save_cwds_stat ();
343 if (panel_operate (current_panel, OP_COPY, 0)) {
344 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
345 repaint_screen ();
349 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
350 void ren_cmd (void)
352 save_cwds_stat ();
353 if (panel_operate (current_panel, OP_MOVE, 0)){
354 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
355 repaint_screen ();
359 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
360 void copy_cmd_local (void)
362 save_cwds_stat ();
363 if (panel_operate (current_panel, OP_COPY, 1)){
364 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
365 repaint_screen ();
369 /* Invoked by F16. Move/rename, default to the same panel. */
370 void ren_cmd_local (void)
372 save_cwds_stat ();
373 if (panel_operate (current_panel, OP_MOVE, 1)){
374 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
375 repaint_screen ();
379 void
380 mkdir_cmd (void)
382 char *dir, *absdir;
383 const char *name = "";
385 /* If 'on' then automatically fills name with current selected item name */
386 if (auto_fill_mkdir_name)
387 name = selection (current_panel)->fname;
389 dir =
390 input_expand_dialog (_("Create a new Directory"),
391 _(" Enter directory name:"),
392 MC_HISTORY_FM_MKDIR,
393 name);
395 if (!dir)
396 return;
398 if (dir[0] == '/' || dir[0] == '~')
399 absdir = g_strdup (dir);
400 else
401 absdir = concat_dir_and_file (current_panel->cwd, dir);
403 save_cwds_stat ();
404 if (my_mkdir (absdir, 0777) == 0) {
405 update_panels (UP_OPTIMIZE, dir);
406 repaint_screen ();
407 select_item (current_panel);
408 } else {
409 message (D_ERROR, MSG_ERROR, " %s ", unix_error_string (errno));
412 g_free (absdir);
413 g_free (dir);
416 void delete_cmd (void)
418 save_cwds_stat ();
420 if (panel_operate (current_panel, OP_DELETE, 0)){
421 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
422 repaint_screen ();
426 /* Invoked by F18. Remove selected file, regardless of marked files. */
427 void delete_cmd_local (void)
429 save_cwds_stat ();
431 if (panel_operate (current_panel, OP_DELETE, 1)){
432 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
433 repaint_screen ();
437 void find_cmd (void)
439 do_find ();
442 static void
443 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
445 g_free (p->filter);
446 p->filter = 0;
448 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
449 p->filter = allocated_filter_string;
450 else
451 g_free (allocated_filter_string);
452 reread_cmd ();
455 /* Set a given panel filter expression */
456 static void
457 set_panel_filter (WPanel *p)
459 char *reg_exp;
460 const char *x;
462 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
464 reg_exp = input_dialog_help (_(" Filter "),
465 _(" Set expression for filtering filenames"),
466 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
467 if (!reg_exp)
468 return;
469 set_panel_filter_to (p, reg_exp);
472 /* Invoked from the left/right menus */
473 void filter_cmd (void)
475 WPanel *p;
477 if (!SELECTED_IS_PANEL)
478 return;
480 p = MENU_PANEL;
481 set_panel_filter (p);
484 void reread_cmd (void)
486 int flag;
488 if (get_current_type () == view_listing &&
489 get_other_type () == view_listing)
490 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
491 else
492 flag = UP_ONLY_CURRENT;
494 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
495 repaint_screen ();
498 void
499 reverse_selection_cmd (void)
501 int i;
502 file_entry *file;
504 for (i = 0; i < current_panel->count; i++) {
505 file = &current_panel->dir.list [i];
506 if ((reverse_files_only == 0) || !S_ISDIR (file->st.st_mode))
507 do_file_mark (current_panel, i, !file->f.marked);
511 static void
512 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
514 /* dialog sizes */
515 const int DX = 50;
516 const int DY = 7;
518 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
519 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
520 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
522 char *reg_exp;
523 mc_search_t *search;
524 int i;
526 QuickWidget quick_widgets[] =
528 QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns),
529 QUICK_CHECKBOX (DX/2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens),
530 QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only),
531 QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, &reg_exp),
532 QUICK_END
535 QuickDialog quick_dlg =
537 DX, DY, -1, -1, title,
538 "[Select/Unselect Files]", quick_widgets, FALSE
541 if (quick_dialog (&quick_dlg) == B_CANCEL)
542 return;
544 if (!reg_exp)
545 return;
546 if (!*reg_exp) {
547 g_free (reg_exp);
548 return;
550 search = mc_search_new (reg_exp, -1);
551 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
552 search->is_entire_line = TRUE;
553 search->is_case_sentitive = case_sens != 0;
555 for (i = 0; i < current_panel->count; i++) {
556 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
557 continue;
558 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
559 continue;
561 if (mc_search_run (search, current_panel->dir.list[i].fname,
562 0, current_panel->dir.list[i].fnamelen, NULL))
563 do_file_mark (current_panel, i, do_select);
566 mc_search_free (search);
567 g_free (reg_exp);
569 /* result flags */
570 select_flags = 0;
571 if (case_sens != 0)
572 select_flags |= SELECT_MATCH_CASE;
573 if (files_only != 0)
574 select_flags |= SELECT_FILES_ONLY;
575 if (shell_patterns != 0)
576 select_flags |= SELECT_SHELL_PATTERNS;
579 void select_cmd (void)
581 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", TRUE);
584 void unselect_cmd (void)
586 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", FALSE);
589 void ext_cmd (void)
591 char *buffer;
592 char *extdir;
593 int dir;
595 dir = 0;
596 if (geteuid () == 0){
597 dir = query_dialog (_("Extension file edit"),
598 _(" Which extension file you want to edit? "), D_NORMAL, 2,
599 _("&User"), _("&System Wide"));
601 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
603 if (dir == 0){
604 buffer = concat_dir_and_file (home_dir, MC_USER_EXT);
605 check_for_default (extdir, buffer);
606 do_edit (buffer);
607 g_free (buffer);
608 } else if (dir == 1) {
609 if (!exist_file(extdir)) {
610 g_free (extdir);
611 extdir = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
613 do_edit (extdir);
615 g_free (extdir);
616 flush_extension_file ();
619 /* edit file menu for mc */
620 void
621 edit_mc_menu_cmd (void)
623 char *buffer;
624 char *menufile;
625 int dir = 0;
627 dir = query_dialog (
628 _(" Menu edit "),
629 _(" Which menu file do you want to edit? "),
630 D_NORMAL, geteuid() ? 2 : 3,
631 _("&Local"), _("&User"), _("&System Wide")
634 menufile = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
636 if (!exist_file(menufile)) {
637 g_free (menufile);
638 menufile = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
641 switch (dir) {
642 case 0:
643 buffer = g_strdup (MC_LOCAL_MENU);
644 check_for_default (menufile, buffer);
645 chmod (buffer, 0600);
646 break;
648 case 1:
649 buffer = concat_dir_and_file (home_dir, MC_HOME_MENU);
650 check_for_default (menufile, buffer);
651 break;
653 case 2:
654 buffer = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
655 if (!exist_file(buffer)) {
656 g_free (buffer);
657 buffer = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
659 break;
661 default:
662 g_free (menufile);
663 return;
666 do_edit (buffer);
668 g_free (buffer);
669 g_free (menufile);
672 void edit_fhl_cmd (void)
674 char *buffer = NULL;
675 char *fhlfile = NULL;
676 char *user_mc_dir;
678 int dir;
680 dir = 0;
681 if (geteuid () == 0){
682 dir = query_dialog (_("Highlighting groups file edit"),
683 _(" Which highlighting file you want to edit? "), D_NORMAL, 2,
684 _("&User"), _("&System Wide"));
686 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
688 if (dir == 0){
689 user_mc_dir = concat_dir_and_file (home_dir, MC_BASE);
690 buffer = concat_dir_and_file (user_mc_dir, MC_FHL_INI_FILE);
691 g_free(user_mc_dir);
692 check_for_default (fhlfile, buffer);
693 do_edit (buffer);
694 g_free (buffer);
695 } else if (dir == 1) {
696 if (!exist_file(fhlfile)) {
697 g_free (fhlfile);
698 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
700 do_edit (fhlfile);
702 g_free (fhlfile);
704 /* refresh highlighting rules */
705 mc_fhl_free (&mc_filehighlight);
706 mc_filehighlight = mc_fhl_new (TRUE);
709 void quick_chdir_cmd (void)
711 char *target;
713 target = hotlist_cmd (LIST_HOTLIST);
714 if (!target)
715 return;
717 if (get_current_type () == view_tree)
718 tree_chdir (the_tree, target);
719 else {
720 char *cmd = g_strconcat ("cd ", target, (char *) NULL);
721 do_cd_command (cmd);
722 g_free (cmd);
724 g_free (target);
727 #ifdef USE_VFS
728 void reselect_vfs (void)
730 char *target;
732 target = hotlist_cmd (LIST_VFSLIST);
733 if (!target)
734 return;
736 if (!do_cd (target, cd_exact))
737 message (D_ERROR, MSG_ERROR, _("Cannot change directory") );
738 g_free (target);
740 #endif /* USE_VFS */
742 static int compare_files (char *name1, char *name2, off_t size)
744 int file1, file2;
745 int result = -1; /* Different by default */
747 if (size == 0)
748 return 0;
750 file1 = open (name1, O_RDONLY);
751 if (file1 >= 0){
752 file2 = open (name2, O_RDONLY);
753 if (file2 >= 0){
754 #ifdef HAVE_MMAP
755 char *data1, *data2;
756 /* Ugly if jungle */
757 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
758 if (data1 != (char*) -1){
759 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
760 if (data2 != (char*) -1){
761 rotate_dash ();
762 result = memcmp (data1, data2, size);
763 munmap (data2, size);
765 munmap (data1, size);
767 #else
768 /* Don't have mmap() :( Even more ugly :) */
769 char buf1[BUFSIZ], buf2[BUFSIZ];
770 int n1, n2;
771 rotate_dash ();
774 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
775 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
776 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
777 result = (n1 != n2) || memcmp(buf1,buf2,n1);
778 #endif /* !HAVE_MMAP */
779 close (file2);
781 close (file1);
783 return result;
786 enum CompareMode {
787 compare_quick, compare_size_only, compare_thourough
790 static void
791 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
793 int i, j;
794 char *src_name, *dst_name;
796 /* No marks by default */
797 panel->marked = 0;
798 panel->total = 0;
799 panel->dirs_marked = 0;
801 /* Handle all files in the panel */
802 for (i = 0; i < panel->count; i++){
803 file_entry *source = &panel->dir.list[i];
805 /* Default: unmarked */
806 file_mark (panel, i, 0);
808 /* Skip directories */
809 if (S_ISDIR (source->st.st_mode))
810 continue;
812 /* Search the corresponding entry from the other panel */
813 for (j = 0; j < other->count; j++){
814 if (strcmp (source->fname,
815 other->dir.list[j].fname) == 0)
816 break;
818 if (j >= other->count)
819 /* Not found -> mark */
820 do_file_mark (panel, i, 1);
821 else {
822 /* Found */
823 file_entry *target = &other->dir.list[j];
825 if (mode != compare_size_only){
826 /* Older version is not marked */
827 if (source->st.st_mtime < target->st.st_mtime)
828 continue;
831 /* Newer version with different size is marked */
832 if (source->st.st_size != target->st.st_size){
833 do_file_mark (panel, i, 1);
834 continue;
837 if (mode == compare_size_only)
838 continue;
840 if (mode == compare_quick){
841 /* Thorough compare off, compare only time stamps */
842 /* Mark newer version, don't mark version with the same date */
843 if (source->st.st_mtime > target->st.st_mtime){
844 do_file_mark (panel, i, 1);
846 continue;
849 /* Thorough compare on, do byte-by-byte comparison */
850 src_name = concat_dir_and_file (panel->cwd, source->fname);
851 dst_name = concat_dir_and_file (other->cwd, target->fname);
852 if (compare_files (src_name, dst_name, source->st.st_size))
853 do_file_mark (panel, i, 1);
854 g_free (src_name);
855 g_free (dst_name);
857 } /* for (i ...) */
860 void
861 compare_dirs_cmd (void)
863 int choice;
864 enum CompareMode thorough_flag;
866 choice =
867 query_dialog (_(" Compare directories "),
868 _(" Select compare method: "), D_NORMAL, 4,
869 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
871 if (choice < 0 || choice > 2)
872 return;
873 else
874 thorough_flag = choice;
876 if (get_current_type () == view_listing
877 && get_other_type () == view_listing) {
878 compare_dir (current_panel, other_panel, thorough_flag);
879 compare_dir (other_panel, current_panel, thorough_flag);
880 } else {
881 message (D_ERROR, MSG_ERROR,
882 _(" Both panels should be in the "
883 "listing mode to use this command "));
887 void
888 history_cmd (void)
890 /* show the history of command line widget */
891 send_message (&cmdline->widget, WIDGET_COMMAND, CK_InputHistoryShow);
894 void swap_cmd (void)
896 swap_panels ();
897 tty_touch_screen ();
898 repaint_screen ();
901 void
902 view_other_cmd (void)
904 static int message_flag = TRUE;
906 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell) {
907 if (message_flag)
908 message (D_ERROR, MSG_ERROR,
909 _(" Not an xterm or Linux console; \n"
910 " the panels cannot be toggled. "));
911 message_flag = FALSE;
912 } else {
913 toggle_panels ();
917 static void
918 do_link (int symbolic_link, const char *fname)
920 char *dest = NULL, *src = NULL;
922 if (!symbolic_link) {
923 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
924 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
925 if (!dest || !*dest)
926 goto cleanup;
927 save_cwds_stat ();
928 if (-1 == mc_link (fname, dest))
929 message (D_ERROR, MSG_ERROR, _(" link: %s "),
930 unix_error_string (errno));
931 } else {
932 char *s;
933 char *d;
935 /* suggest the full path for symlink */
936 s = concat_dir_and_file (current_panel->cwd, fname);
938 if (get_other_type () == view_listing) {
939 d = concat_dir_and_file (other_panel->cwd, fname);
940 } else {
941 d = g_strdup (fname);
944 symlink_dialog (s, d, &dest, &src);
945 g_free (d);
946 g_free (s);
948 if (!dest || !*dest || !src || !*src)
949 goto cleanup;
950 save_cwds_stat ();
951 if (-1 == mc_symlink (dest, src))
952 message (D_ERROR, MSG_ERROR, _(" symlink: %s "),
953 unix_error_string (errno));
955 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
956 repaint_screen ();
958 cleanup:
959 g_free (src);
960 g_free (dest);
963 void link_cmd (void)
965 do_link (0, selection (current_panel)->fname);
968 void symlink_cmd (void)
970 char *filename = NULL;
971 filename = selection (current_panel)->fname;
973 if (filename) {
974 do_link (1, filename);
978 void edit_symlink_cmd (void)
980 if (S_ISLNK (selection (current_panel)->st.st_mode)) {
981 char buffer [MC_MAXPATHLEN];
982 char *p = NULL;
983 int i;
984 char *dest, *q;
986 p = selection (current_panel)->fname;
988 q = g_strdup_printf (_(" Symlink `%s\' points to: "), str_trunc (p, 32));
990 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
991 if (i > 0) {
992 buffer [i] = 0;
993 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
994 if (dest) {
995 if (*dest && strcmp (buffer, dest)) {
996 save_cwds_stat ();
997 if (-1 == mc_unlink (p)){
998 message (D_ERROR, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
999 p, unix_error_string (errno));
1000 } else {
1001 if (-1 == mc_symlink (dest, p))
1002 message (D_ERROR, MSG_ERROR, _(" edit symlink: %s "),
1003 unix_error_string (errno));
1005 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1006 repaint_screen ();
1008 g_free (dest);
1011 g_free (q);
1012 } else {
1013 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1014 selection (current_panel)->fname);
1018 void help_cmd (void)
1020 interactive_display (NULL, "[main]");
1023 void
1024 user_file_menu_cmd (void)
1026 user_menu_cmd (NULL);
1029 /* partly taken from dcigettext.c, returns "" for default locale */
1030 /* value should be freed by calling function g_free() */
1031 char *guess_message_value (void)
1033 static const char * const var[] = {
1034 /* The highest priority value is the `LANGUAGE' environment
1035 variable. This is a GNU extension. */
1036 "LANGUAGE",
1037 /* Setting of LC_ALL overwrites all other. */
1038 "LC_ALL",
1039 /* Next comes the name of the desired category. */
1040 "LC_MESSAGES",
1041 /* Last possibility is the LANG environment variable. */
1042 "LANG",
1043 /* NULL exit loops */
1044 NULL
1047 unsigned i = 0;
1048 const char *locale = NULL;
1050 while (var[i] != NULL) {
1051 locale = getenv (var[i]);
1052 if (locale != NULL && locale[0] != '\0')
1053 break;
1054 i++;
1057 if (locale == NULL)
1058 locale = "";
1060 return g_strdup (locale);
1064 * Return a random hint. If force is not 0, ignore the timeout.
1066 char *
1067 get_random_hint (int force)
1069 char *data, *result = NULL, *eol;
1070 int len;
1071 int start;
1072 static int last_sec;
1073 static struct timeval tv;
1074 GIConv conv;
1075 GString *buffer;
1077 /* Do not change hints more often than one minute */
1078 gettimeofday (&tv, NULL);
1079 if (!force && !(tv.tv_sec > last_sec + 60))
1080 return g_strdup ("");
1081 last_sec = tv.tv_sec;
1083 data = load_mc_home_file (MC_HINT, NULL);
1084 if (!data)
1085 return 0;
1087 /* get a random entry */
1088 srand (tv.tv_sec);
1089 len = strlen (data);
1090 start = rand () % len;
1092 for (; start; start--) {
1093 if (data[start] == '\n') {
1094 start++;
1095 break;
1098 eol = strchr (&data[start], '\n');
1099 if (eol)
1100 *eol = 0;
1102 /* hint files are stored in utf-8 */
1103 /* try convert hint file from utf-8 to terminal encoding */
1104 conv = str_crt_conv_from ("UTF-8");
1105 if (conv != INVALID_CONV) {
1106 buffer = g_string_new ("");
1107 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE) {
1108 result = g_strdup (buffer->str);
1110 g_string_free (buffer, TRUE);
1111 str_close_conv (conv);
1114 g_free (data);
1115 return result;
1118 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1119 static void
1120 nice_cd (const char *text, const char *xtext, const char *help,
1121 const char *history_name, const char *prefix, int to_home)
1123 char *machine;
1124 char *cd_path;
1126 if (!SELECTED_IS_PANEL)
1127 return;
1129 machine = input_dialog_help (text, xtext, help, history_name, "");
1130 if (!machine)
1131 return;
1133 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1134 ugly as hell and leads to problems in vfs layer */
1136 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1137 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1138 else
1139 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1141 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1142 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1143 else
1144 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1145 g_free (cd_path);
1146 g_free (machine);
1148 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1151 #ifdef USE_NETCODE
1153 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1155 #ifdef ENABLE_VFS_MCFS
1156 void netlink_cmd (void)
1158 nice_cd (_(" Link to a remote machine "), _(machine_str),
1159 "[Network File System]", ":netlink_cmd: Link to a remote ",
1160 "/#mc:", 1);
1162 #endif /* ENABLE_VFS_MCFS */
1164 void ftplink_cmd (void)
1166 nice_cd (_(" FTP to machine "), _(machine_str),
1167 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1170 void fishlink_cmd (void)
1172 nice_cd (_(" Shell link to machine "), _(machine_str),
1173 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1174 "/#sh:", 1);
1177 #ifdef WITH_SMBFS
1178 void smblink_cmd (void)
1180 nice_cd (_(" SMB link to machine "), _(machine_str),
1181 "[SMB File System]", ":smblink_cmd: SMB link to machine ",
1182 "/#smb:", 0);
1184 #endif /* WITH_SMBFS */
1185 #endif /* USE_NETCODE */
1187 #ifdef USE_EXT2FSLIB
1188 void undelete_cmd (void)
1190 nice_cd (_(" Undelete files on an ext2 file system "),
1191 _(" Enter device (without /dev/) to undelete\n "
1192 " files on: (F1 for details)"),
1193 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ",
1194 "/#undel:", 0);
1196 #endif /* USE_EXT2FSLIB */
1198 void quick_cd_cmd (void)
1200 char *p = cd_dialog ();
1202 if (p && *p) {
1203 char *q = g_strconcat ("cd ", p, (char *) NULL);
1205 do_cd_command (q);
1206 g_free (q);
1208 g_free (p);
1213 \brief calculate dirs sizes
1215 calculate dirs sizes and resort panel:
1216 dirs_selected = show size for selected dirs,
1217 otherwise = show size for dir under cursor:
1218 dir under cursor ".." = show size for all dirs,
1219 otherwise = show size for dir under cursor
1221 void
1222 smart_dirsize_cmd (void)
1224 WPanel *panel = current_panel;
1225 file_entry *entry;
1227 entry = &(panel->dir.list[panel->selected]);
1228 if ( ( S_ISDIR (entry->st.st_mode) && ( strcmp(entry->fname, "..") == 0 ) )
1229 || panel->dirs_marked )
1230 dirsizes_cmd ();
1231 else
1232 single_dirsize_cmd ();
1235 void
1236 single_dirsize_cmd (void)
1238 WPanel *panel = current_panel;
1239 file_entry *entry;
1240 off_t marked;
1241 double total;
1242 ComputeDirSizeUI *ui;
1244 entry = &(panel->dir.list[panel->selected]);
1245 if (S_ISDIR (entry->st.st_mode) && strcmp(entry->fname, "..") != 0) {
1246 ui = compute_dir_size_create_ui ();
1248 total = 0.0;
1250 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1251 &marked, &total) == FILE_CONT) {
1252 entry->st.st_size = (off_t) total;
1253 entry->f.dir_size_computed = 1;
1256 compute_dir_size_destroy_ui (ui);
1259 if (mark_moves_down)
1260 send_message (&(panel->widget), WIDGET_KEY, KEY_DOWN);
1262 recalculate_panel_summary (panel);
1264 if ( current_panel->sort_type == (sortfn *) sort_size )
1265 panel_re_sort (panel);
1267 panel->dirty = 1;
1270 void
1271 dirsizes_cmd (void)
1273 WPanel *panel = current_panel;
1274 int i;
1275 off_t marked;
1276 double total;
1277 ComputeDirSizeUI *ui;
1279 ui = compute_dir_size_create_ui ();
1281 for (i = 0; i < panel->count; i++)
1282 if (S_ISDIR (panel->dir.list [i].st.st_mode)
1283 && ((panel->dirs_marked && panel->dir.list [i].f.marked)
1284 || !panel->dirs_marked)
1285 && strcmp (panel->dir.list [i].fname, "..") != 0) {
1286 total = 0.0l;
1288 if (compute_dir_size (panel->dir.list [i].fname,
1289 ui, compute_dir_size_update_ui,
1290 &marked, &total) != FILE_CONT)
1291 break;
1293 panel->dir.list [i].st.st_size = (off_t) total;
1294 panel->dir.list [i].f.dir_size_computed = 1;
1297 compute_dir_size_destroy_ui (ui);
1299 recalculate_panel_summary (panel);
1301 if ( current_panel->sort_type == (sortfn *) sort_size )
1302 panel_re_sort (panel);
1304 panel->dirty = 1;
1307 void
1308 save_setup_cmd (void)
1310 save_setup ();
1311 message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"), PROFILE_NAME);
1314 static void
1315 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1317 p->user_mini_status = use_msformat;
1318 p->list_type = view_type;
1320 if (view_type == list_user || use_msformat){
1321 g_free (p->user_format);
1322 p->user_format = user;
1324 g_free (p->user_status_format [view_type]);
1325 p->user_status_format [view_type] = status;
1327 set_panel_formats (p);
1329 else {
1330 g_free (user);
1331 g_free (status);
1334 set_panel_formats (p);
1335 do_refresh ();
1338 void
1339 info_cmd_no_menu (void)
1341 if (get_display_type (0) == view_info)
1342 set_display_type (0, view_listing);
1343 else if (get_display_type (1) == view_info)
1344 set_display_type (1, view_listing);
1345 else
1346 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1349 void
1350 quick_cmd_no_menu (void)
1352 if (get_display_type (0) == view_quick)
1353 set_display_type (0, view_listing);
1354 else if (get_display_type (1) == view_quick)
1355 set_display_type (1, view_listing);
1356 else
1357 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1360 static void
1361 switch_to_listing (int panel_index)
1363 if (get_display_type (panel_index) != view_listing)
1364 set_display_type (panel_index, view_listing);
1367 void
1368 listing_cmd (void)
1370 int view_type, use_msformat;
1371 char *user, *status;
1372 WPanel *p;
1373 int display_type;
1375 display_type = get_display_type (MENU_PANEL_IDX);
1376 if (display_type == view_listing)
1377 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1378 else
1379 p = 0;
1381 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1383 if (view_type == -1)
1384 return;
1386 switch_to_listing (MENU_PANEL_IDX);
1388 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1390 configure_panel_listing (p, view_type, use_msformat, user, status);
1393 void
1394 tree_cmd (void)
1396 set_display_type (MENU_PANEL_IDX, view_tree);
1399 void
1400 info_cmd (void)
1402 set_display_type (MENU_PANEL_IDX, view_info);
1405 void
1406 quick_view_cmd (void)
1408 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1409 change_panel ();
1410 set_display_type (MENU_PANEL_IDX, view_quick);
1413 /* Handle the tree internal listing modes switching */
1414 static int
1415 set_basic_panel_listing_to (int panel_index, int listing_mode)
1417 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1419 switch_to_listing (panel_index);
1420 p->list_type = listing_mode;
1421 if (set_panel_formats (p))
1422 return 0;
1424 do_refresh ();
1425 return 1;
1428 void
1429 toggle_listing_cmd (void)
1431 int current = get_current_index ();
1432 WPanel *p = (WPanel *) get_panel_widget (current);
1434 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1437 void
1438 encoding_cmd (void)
1440 WPanel *panel;
1442 if (!SELECTED_IS_PANEL)
1443 return;
1445 panel = MENU_PANEL;
1446 set_panel_encoding (panel);