Changes for correctly work after rebase to master (master with HACK_tty).
[midnight-commander.git] / src / cmd.c
blob8705a0bd0b66c9bbf81b2a00e64f9ef5bab9317a
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 */
51 #include "../src/tty/key.h" /* ALT() macro */
52 #include "../src/tty/win.h" /* do_enter_ca_mode() */
54 #include "../src/search/search.h"
56 #include "../src/mcconfig/mcconfig.h"
58 #include "cmd.h" /* Our definitions */
59 #include "fileopctx.h"
60 #include "file.h" /* file operation routines */
61 #include "find.h" /* do_find() */
62 #include "hotlist.h" /* hotlist_cmd() */
63 #include "tree.h" /* tree_chdir() */
64 #include "subshell.h" /* use_subshell */
65 #include "cons.saver.h" /* console_flag */
66 #include "dialog.h" /* Widget */
67 #include "wtools.h" /* message() */
68 #include "widget.h" /* push_history() */
69 #include "main.h" /* change_panel() */
70 #include "panel.h" /* current_panel */
71 #include "help.h" /* interactive_display() */
72 #include "user.h" /* MC_GLOBAL_MENU */
73 #include "command.h" /* cmdline */
74 #include "layout.h" /* get_current_type() */
75 #include "ext.h" /* regex_command() */
76 #include "boxes.h" /* cd_dialog() */
77 #include "setup.h"
78 #include "execute.h" /* toggle_panels() */
79 #include "history.h"
80 #include "strutil.h"
81 #include "dir.h"
82 #include "../src/viewer/mcviewer.h"
84 #ifndef MAP_FILE
85 # define MAP_FILE 0
86 #endif
88 #ifdef USE_INTERNAL_EDIT
89 # include "../edit/edit.h"
90 #endif
92 /* If set and you don't have subshell support,then C-o will give you a shell */
93 int output_starts_shell = 0;
95 /* If set, use the builtin editor */
96 int use_internal_edit = 1;
98 /* Automatically fills name with current selected item name on mkdir */
99 int auto_fill_mkdir_name = 1;
101 /* if set, only selection of files is inverted */
102 int reverse_files_only = 1;
104 /* selection flags */
105 typedef enum {
106 SELECT_FILES_ONLY = 1 << 0,
107 SELECT_MATCH_CASE = 1 << 1,
108 SELECT_SHELL_PATTERNS = 1 << 2
109 } select_flags_t;
111 static select_flags_t select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
114 view_file_at_line (const char *filename, int plain_view, int internal,
115 int start_line)
117 static const char *viewer = NULL;
118 int move_dir = 0;
121 if (plain_view) {
122 int changed_hex_mode = 0;
123 int changed_nroff_flag = 0;
124 int changed_magic_flag = 0;
126 mcview_altered_hex_mode = 0;
127 mcview_altered_nroff_flag = 0;
128 mcview_altered_magic_flag = 0;
129 if (mcview_default_hex_mode)
130 changed_hex_mode = 1;
131 if (mcview_default_nroff_flag)
132 changed_nroff_flag = 1;
133 if (mcview_default_magic_flag)
134 changed_magic_flag = 1;
135 mcview_default_hex_mode = 0;
136 mcview_default_nroff_flag = 0;
137 mcview_default_magic_flag = 0;
138 mcview_viewer (NULL, filename, &move_dir, start_line);
139 if (changed_hex_mode && !mcview_altered_hex_mode)
140 mcview_default_hex_mode = 1;
141 if (changed_nroff_flag && !mcview_altered_nroff_flag)
142 mcview_default_nroff_flag = 1;
143 if (changed_magic_flag && !mcview_altered_magic_flag)
144 mcview_default_magic_flag = 1;
145 repaint_screen ();
146 return move_dir;
148 if (internal) {
149 char view_entry[BUF_TINY];
151 if (start_line != 0)
152 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
153 start_line);
154 else
155 strcpy (view_entry, "View");
157 if (regex_command (filename, view_entry, &move_dir) == 0) {
158 mcview_viewer (NULL, filename, &move_dir, start_line);
159 repaint_screen ();
161 } else {
162 if (!viewer) {
163 viewer = getenv ("VIEWER");
164 if (!viewer)
165 viewer = getenv ("PAGER");
166 if (!viewer)
167 viewer = "view";
169 execute_with_vfs_arg (viewer, filename);
171 return move_dir;
174 /* view_file (filename, plain_view, internal)
176 * Inputs:
177 * filename: The file name to view
178 * plain_view: If set does not do any fancy pre-processing (no filtering) and
179 * always invokes the internal viewer.
180 * internal: If set uses the internal viewer, otherwise an external viewer.
183 view_file (const char *filename, int plain_view, int internal)
185 return view_file_at_line (filename, plain_view, internal, 0);
188 /* scan_for_file (panel, idx, direction)
190 * Inputs:
191 * panel: pointer to the panel on which we operate
192 * idx: starting file.
193 * direction: 1, or -1
195 static int scan_for_file (WPanel *panel, int idx, int direction)
197 int i = idx + direction;
199 while (i != idx){
200 if (i < 0)
201 i = panel->count - 1;
202 if (i == panel->count)
203 i = 0;
204 if (!S_ISDIR (panel->dir.list [i].st.st_mode))
205 return i;
206 i += direction;
208 return i;
212 * Run viewer (internal or external) on the currently selected file.
213 * If normal is 1, force internal viewer and raw mode (used for F13).
215 static void
216 do_view_cmd (int normal)
218 /* Directories are viewed by changing to them */
219 if (S_ISDIR (selection (current_panel)->st.st_mode)
220 || link_isdir (selection (current_panel))) {
221 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked)) {
222 if (query_dialog
223 (_(" Confirmation "), _("Files tagged, want to cd?"), D_NORMAL, 2,
224 _("&Yes"), _("&No")) != 0) {
225 return;
228 if (!do_cd (selection (current_panel)->fname, cd_exact))
229 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
230 } else {
231 int dir, file_idx;
232 char *filename;
234 file_idx = current_panel->selected;
235 while (1) {
236 filename = current_panel->dir.list[file_idx].fname;
238 dir = view_file (filename, normal, use_internal_view);
239 if (dir == 0)
240 break;
241 file_idx = scan_for_file (current_panel, file_idx, dir);
245 repaint_screen();
248 /* Run user's preferred viewer on the currently selected file */
249 void
250 view_cmd (void)
252 do_view_cmd (0);
255 /* Ask for file and run user's preferred viewer on it */
256 void
257 view_file_cmd (void)
259 char *filename;
261 filename =
262 input_expand_dialog (_(" View file "), _(" Filename:"),
263 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
264 if (!filename)
265 return;
267 view_file (filename, 0, use_internal_view);
268 g_free (filename);
271 /* Run plain internal viewer on the currently selected file */
272 void
273 view_simple_cmd (void)
275 do_view_cmd (1);
278 void
279 filtered_view_cmd (void)
281 char *command;
283 command =
284 input_dialog (_(" Filtered view "),
285 _(" Filter command and arguments:"),
286 MC_HISTORY_FM_FILTERED_VIEW,
287 selection (current_panel)->fname);
288 if (!command)
289 return;
291 mcview_viewer (command, "", NULL, 0);
293 g_free (command);
296 void
297 do_edit_at_line (const char *what, int start_line)
299 static const char *editor = NULL;
301 #ifdef USE_INTERNAL_EDIT
302 if (use_internal_edit)
303 edit_file (what, start_line);
304 else
305 #endif /* USE_INTERNAL_EDIT */
307 if (editor == NULL) {
308 editor = getenv ("EDITOR");
309 if (editor == NULL)
310 editor = get_default_editor ();
312 execute_with_vfs_arg (editor, what);
314 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
315 repaint_screen ();
318 static void
319 do_edit (const char *what)
321 do_edit_at_line (what, 0);
324 void
325 edit_cmd (void)
327 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
328 do_edit (selection (current_panel)->fname);
331 void
332 edit_cmd_new (void)
334 do_edit (NULL);
337 /* Invoked by F5. Copy, default to the other panel. */
338 void
339 copy_cmd (void)
341 save_cwds_stat ();
342 if (panel_operate (current_panel, OP_COPY, 0)) {
343 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
344 repaint_screen ();
348 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
349 void ren_cmd (void)
351 save_cwds_stat ();
352 if (panel_operate (current_panel, OP_MOVE, 0)){
353 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
354 repaint_screen ();
358 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
359 void copy_cmd_local (void)
361 save_cwds_stat ();
362 if (panel_operate (current_panel, OP_COPY, 1)){
363 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
364 repaint_screen ();
368 /* Invoked by F16. Move/rename, default to the same panel. */
369 void ren_cmd_local (void)
371 save_cwds_stat ();
372 if (panel_operate (current_panel, OP_MOVE, 1)){
373 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
374 repaint_screen ();
378 void
379 mkdir_cmd (void)
381 char *dir, *absdir;
382 const char *name = "";
384 /* If 'on' then automatically fills name with current selected item name */
385 if (auto_fill_mkdir_name)
386 name = selection (current_panel)->fname;
388 dir =
389 input_expand_dialog (_("Create a new Directory"),
390 _(" Enter directory name:"),
391 MC_HISTORY_FM_MKDIR,
392 name);
394 if (!dir)
395 return;
397 if (dir[0] == '/' || dir[0] == '~')
398 absdir = g_strdup (dir);
399 else
400 absdir = concat_dir_and_file (current_panel->cwd, dir);
402 save_cwds_stat ();
403 if (my_mkdir (absdir, 0777) == 0) {
404 update_panels (UP_OPTIMIZE, dir);
405 repaint_screen ();
406 select_item (current_panel);
407 } else {
408 message (D_ERROR, MSG_ERROR, " %s ", unix_error_string (errno));
411 g_free (absdir);
412 g_free (dir);
415 void delete_cmd (void)
417 save_cwds_stat ();
419 if (panel_operate (current_panel, OP_DELETE, 0)){
420 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
421 repaint_screen ();
425 /* Invoked by F18. Remove selected file, regardless of marked files. */
426 void delete_cmd_local (void)
428 save_cwds_stat ();
430 if (panel_operate (current_panel, OP_DELETE, 1)){
431 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
432 repaint_screen ();
436 void find_cmd (void)
438 do_find ();
441 static void
442 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
444 g_free (p->filter);
445 p->filter = 0;
447 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
448 p->filter = allocated_filter_string;
449 else
450 g_free (allocated_filter_string);
451 reread_cmd ();
454 /* Set a given panel filter expression */
455 static void
456 set_panel_filter (WPanel *p)
458 char *reg_exp;
459 const char *x;
461 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
463 reg_exp = input_dialog_help (_(" Filter "),
464 _(" Set expression for filtering filenames"),
465 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
466 if (!reg_exp)
467 return;
468 set_panel_filter_to (p, reg_exp);
471 /* Invoked from the left/right menus */
472 void filter_cmd (void)
474 WPanel *p;
476 if (!SELECTED_IS_PANEL)
477 return;
479 p = MENU_PANEL;
480 set_panel_filter (p);
483 void reread_cmd (void)
485 int flag;
487 if (get_current_type () == view_listing &&
488 get_other_type () == view_listing)
489 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
490 else
491 flag = UP_ONLY_CURRENT;
493 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
494 repaint_screen ();
497 void
498 reverse_selection_cmd (void)
500 int i;
501 file_entry *file;
503 for (i = 0; i < current_panel->count; i++) {
504 file = &current_panel->dir.list [i];
505 if ((reverse_files_only == 0) || !S_ISDIR (file->st.st_mode))
506 do_file_mark (current_panel, i, !file->f.marked);
510 static void
511 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
513 /* dialog sizes */
514 const int DX = 50;
515 const int DY = 7;
517 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
518 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
519 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
521 char *reg_exp;
522 mc_search_t *search;
523 int i;
525 QuickWidget quick_widgets[] = {
526 { quick_checkbox, 3, DX, DY - 3, DY, N_("&Using shell patterns"), 0, 0, &shell_patterns, NULL, NULL },
527 { quick_checkbox, DX/2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), 0, 0, &case_sens, NULL, NULL },
528 { quick_checkbox, 3, DX, DY - 4, DY, N_("&Files only"), 0, 0, &files_only, NULL, NULL },
529 { quick_input, 3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, NULL, &reg_exp, history_name },
530 NULL_QuickWidget
533 QuickDialog quick_dlg = {
534 DX, DY, -1, -1, title, "[Select/Unselect Files]", quick_widgets, 0
537 if (quick_dialog (&quick_dlg) == B_CANCEL)
538 return;
540 if (!reg_exp)
541 return;
542 if (!*reg_exp) {
543 g_free (reg_exp);
544 return;
546 search = mc_search_new (reg_exp, -1);
547 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
548 search->is_entire_line = TRUE;
549 search->is_case_sentitive = case_sens != 0;
551 for (i = 0; i < current_panel->count; i++) {
552 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
553 continue;
554 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
555 continue;
557 if (mc_search_run (search, current_panel->dir.list[i].fname,
558 0, current_panel->dir.list[i].fnamelen, NULL))
559 do_file_mark (current_panel, i, do_select);
562 mc_search_free (search);
563 g_free (reg_exp);
565 /* result flags */
566 select_flags = 0;
567 if (case_sens != 0)
568 select_flags |= SELECT_MATCH_CASE;
569 if (files_only != 0)
570 select_flags |= SELECT_FILES_ONLY;
571 if (shell_patterns != 0)
572 select_flags |= SELECT_SHELL_PATTERNS;
575 void select_cmd (void)
577 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", TRUE);
580 void unselect_cmd (void)
582 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", FALSE);
585 void ext_cmd (void)
587 char *buffer;
588 char *extdir;
589 int dir;
591 dir = 0;
592 if (geteuid () == 0){
593 dir = query_dialog (_("Extension file edit"),
594 _(" Which extension file you want to edit? "), D_NORMAL, 2,
595 _("&User"), _("&System Wide"));
597 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
599 if (dir == 0){
600 buffer = concat_dir_and_file (home_dir, MC_USER_EXT);
601 check_for_default (extdir, buffer);
602 do_edit (buffer);
603 g_free (buffer);
604 } else if (dir == 1) {
605 if (!exist_file(extdir)) {
606 g_free (extdir);
607 extdir = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
609 do_edit (extdir);
611 g_free (extdir);
612 flush_extension_file ();
615 /* edit file menu for mc */
616 void
617 edit_mc_menu_cmd (void)
619 char *buffer;
620 char *menufile;
621 int dir = 0;
623 dir = query_dialog (
624 _(" Menu edit "),
625 _(" Which menu file do you want to edit? "),
626 D_NORMAL, geteuid() ? 2 : 3,
627 _("&Local"), _("&User"), _("&System Wide")
630 menufile = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
632 if (!exist_file(menufile)) {
633 g_free (menufile);
634 menufile = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
637 switch (dir) {
638 case 0:
639 buffer = g_strdup (MC_LOCAL_MENU);
640 check_for_default (menufile, buffer);
641 chmod (buffer, 0600);
642 break;
644 case 1:
645 buffer = concat_dir_and_file (home_dir, MC_HOME_MENU);
646 check_for_default (menufile, buffer);
647 break;
649 case 2:
650 buffer = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
651 if (!exist_file(buffer)) {
652 g_free (buffer);
653 buffer = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
655 break;
657 default:
658 g_free (menufile);
659 return;
662 do_edit (buffer);
664 g_free (buffer);
665 g_free (menufile);
668 void quick_chdir_cmd (void)
670 char *target;
672 target = hotlist_cmd (LIST_HOTLIST);
673 if (!target)
674 return;
676 if (get_current_type () == view_tree)
677 tree_chdir (the_tree, target);
678 else
679 if (!do_cd (target, cd_exact))
680 message (D_ERROR, MSG_ERROR, _("Cannot change directory") );
681 g_free (target);
684 #ifdef USE_VFS
685 void reselect_vfs (void)
687 char *target;
689 target = hotlist_cmd (LIST_VFSLIST);
690 if (!target)
691 return;
693 if (!do_cd (target, cd_exact))
694 message (D_ERROR, MSG_ERROR, _("Cannot change directory") );
695 g_free (target);
697 #endif /* USE_VFS */
699 static int compare_files (char *name1, char *name2, off_t size)
701 int file1, file2;
702 int result = -1; /* Different by default */
704 if (size == 0)
705 return 0;
707 file1 = open (name1, O_RDONLY);
708 if (file1 >= 0){
709 file2 = open (name2, O_RDONLY);
710 if (file2 >= 0){
711 #ifdef HAVE_MMAP
712 char *data1, *data2;
713 /* Ugly if jungle */
714 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
715 if (data1 != (char*) -1){
716 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
717 if (data2 != (char*) -1){
718 rotate_dash ();
719 result = memcmp (data1, data2, size);
720 munmap (data2, size);
722 munmap (data1, size);
724 #else
725 /* Don't have mmap() :( Even more ugly :) */
726 char buf1[BUFSIZ], buf2[BUFSIZ];
727 int n1, n2;
728 rotate_dash ();
731 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
732 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
733 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
734 result = (n1 != n2) || memcmp(buf1,buf2,n1);
735 #endif /* !HAVE_MMAP */
736 close (file2);
738 close (file1);
740 return result;
743 enum CompareMode {
744 compare_quick, compare_size_only, compare_thourough
747 static void
748 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
750 int i, j;
751 char *src_name, *dst_name;
753 /* No marks by default */
754 panel->marked = 0;
755 panel->total = 0;
756 panel->dirs_marked = 0;
758 /* Handle all files in the panel */
759 for (i = 0; i < panel->count; i++){
760 file_entry *source = &panel->dir.list[i];
762 /* Default: unmarked */
763 file_mark (panel, i, 0);
765 /* Skip directories */
766 if (S_ISDIR (source->st.st_mode))
767 continue;
769 /* Search the corresponding entry from the other panel */
770 for (j = 0; j < other->count; j++){
771 if (strcmp (source->fname,
772 other->dir.list[j].fname) == 0)
773 break;
775 if (j >= other->count)
776 /* Not found -> mark */
777 do_file_mark (panel, i, 1);
778 else {
779 /* Found */
780 file_entry *target = &other->dir.list[j];
782 if (mode != compare_size_only){
783 /* Older version is not marked */
784 if (source->st.st_mtime < target->st.st_mtime)
785 continue;
788 /* Newer version with different size is marked */
789 if (source->st.st_size != target->st.st_size){
790 do_file_mark (panel, i, 1);
791 continue;
794 if (mode == compare_size_only)
795 continue;
797 if (mode == compare_quick){
798 /* Thorough compare off, compare only time stamps */
799 /* Mark newer version, don't mark version with the same date */
800 if (source->st.st_mtime > target->st.st_mtime){
801 do_file_mark (panel, i, 1);
803 continue;
806 /* Thorough compare on, do byte-by-byte comparison */
807 src_name = concat_dir_and_file (panel->cwd, source->fname);
808 dst_name = concat_dir_and_file (other->cwd, target->fname);
809 if (compare_files (src_name, dst_name, source->st.st_size))
810 do_file_mark (panel, i, 1);
811 g_free (src_name);
812 g_free (dst_name);
814 } /* for (i ...) */
817 void
818 compare_dirs_cmd (void)
820 int choice;
821 enum CompareMode thorough_flag;
823 choice =
824 query_dialog (_(" Compare directories "),
825 _(" Select compare method: "), D_NORMAL, 4,
826 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
828 if (choice < 0 || choice > 2)
829 return;
830 else
831 thorough_flag = choice;
833 if (get_current_type () == view_listing
834 && get_other_type () == view_listing) {
835 compare_dir (current_panel, other_panel, thorough_flag);
836 compare_dir (other_panel, current_panel, thorough_flag);
837 } else {
838 message (D_ERROR, MSG_ERROR,
839 _(" Both panels should be in the "
840 "listing mode to use this command "));
844 void
845 history_cmd (void)
847 /* show the history of command line widget */
848 send_message (&cmdline->widget, WIDGET_KEY, ALT ('h'));
851 void swap_cmd (void)
853 swap_panels ();
854 repaint_screen ();
857 void
858 view_other_cmd (void)
860 static int message_flag = TRUE;
862 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell) {
863 if (message_flag)
864 message (D_ERROR, MSG_ERROR,
865 _(" Not an xterm or Linux console; \n"
866 " the panels cannot be toggled. "));
867 message_flag = FALSE;
868 } else {
869 toggle_panels ();
873 static void
874 do_link (int symbolic_link, const char *fname)
876 char *dest = NULL, *src = NULL;
878 if (!symbolic_link) {
879 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
880 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
881 if (!dest || !*dest)
882 goto cleanup;
883 save_cwds_stat ();
884 if (-1 == mc_link (fname, dest))
885 message (D_ERROR, MSG_ERROR, _(" link: %s "),
886 unix_error_string (errno));
887 } else {
888 char *s;
889 char *d;
891 /* suggest the full path for symlink */
892 s = concat_dir_and_file (current_panel->cwd, fname);
894 if (get_other_type () == view_listing) {
895 d = concat_dir_and_file (other_panel->cwd, fname);
896 } else {
897 d = g_strdup (fname);
900 symlink_dialog (s, d, &dest, &src);
901 g_free (d);
902 g_free (s);
904 if (!dest || !*dest || !src || !*src)
905 goto cleanup;
906 save_cwds_stat ();
907 if (-1 == mc_symlink (dest, src))
908 message (D_ERROR, MSG_ERROR, _(" symlink: %s "),
909 unix_error_string (errno));
911 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
912 repaint_screen ();
914 cleanup:
915 g_free (src);
916 g_free (dest);
919 void link_cmd (void)
921 do_link (0, selection (current_panel)->fname);
924 void symlink_cmd (void)
926 char *filename = NULL;
927 filename = selection (current_panel)->fname;
929 if (filename) {
930 do_link (1, filename);
934 void edit_symlink_cmd (void)
936 if (S_ISLNK (selection (current_panel)->st.st_mode)) {
937 char buffer [MC_MAXPATHLEN];
938 char *p = NULL;
939 int i;
940 char *dest, *q;
942 p = selection (current_panel)->fname;
944 q = g_strdup_printf (_(" Symlink `%s\' points to: "), str_trunc (p, 32));
946 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
947 if (i > 0) {
948 buffer [i] = 0;
949 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
950 if (dest) {
951 if (*dest && strcmp (buffer, dest)) {
952 save_cwds_stat ();
953 if (-1 == mc_unlink (p)){
954 message (D_ERROR, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
955 p, unix_error_string (errno));
956 } else {
957 if (-1 == mc_symlink (dest, p))
958 message (D_ERROR, MSG_ERROR, _(" edit symlink: %s "),
959 unix_error_string (errno));
961 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
962 repaint_screen ();
964 g_free (dest);
967 g_free (q);
968 } else {
969 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
970 selection (current_panel)->fname);
974 void help_cmd (void)
976 interactive_display (NULL, "[main]");
979 void
980 user_file_menu_cmd (void)
982 user_menu_cmd (NULL);
985 /* partly taken from dcigettext.c, returns "" for default locale */
986 /* value should be freed by calling function g_free() */
987 char *guess_message_value (void)
989 static const char * const var[] = {
990 /* The highest priority value is the `LANGUAGE' environment
991 variable. This is a GNU extension. */
992 "LANGUAGE",
993 /* Setting of LC_ALL overwrites all other. */
994 "LC_ALL",
995 /* Next comes the name of the desired category. */
996 "LC_MESSAGES",
997 /* Last possibility is the LANG environment variable. */
998 "LANG",
999 /* NULL exit loops */
1000 NULL
1003 unsigned i = 0;
1004 const char *locale = NULL;
1006 while (var[i] != NULL) {
1007 locale = getenv (var[i]);
1008 if (locale != NULL && locale[0] != '\0')
1009 break;
1010 i++;
1013 if (locale == NULL)
1014 locale = "";
1016 return g_strdup (locale);
1020 * Return a random hint. If force is not 0, ignore the timeout.
1022 char *
1023 get_random_hint (int force)
1025 char *data, *result = NULL, *eol;
1026 int len;
1027 int start;
1028 static int last_sec;
1029 static struct timeval tv;
1030 GIConv conv;
1031 GString *buffer;
1033 /* Do not change hints more often than one minute */
1034 gettimeofday (&tv, NULL);
1035 if (!force && !(tv.tv_sec > last_sec + 60))
1036 return g_strdup ("");
1037 last_sec = tv.tv_sec;
1039 data = load_mc_home_file (MC_HINT, NULL);
1040 if (!data)
1041 return 0;
1043 /* get a random entry */
1044 srand (tv.tv_sec);
1045 len = strlen (data);
1046 start = rand () % len;
1048 for (; start; start--) {
1049 if (data[start] == '\n') {
1050 start++;
1051 break;
1054 eol = strchr (&data[start], '\n');
1055 if (eol)
1056 *eol = 0;
1058 /* hint files are stored in utf-8 */
1059 /* try convert hint file from utf-8 to terminal encoding */
1060 conv = str_crt_conv_from ("UTF-8");
1061 if (conv != INVALID_CONV) {
1062 buffer = g_string_new ("");
1063 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE) {
1064 result = g_strdup (buffer->str);
1066 g_string_free (buffer, TRUE);
1067 str_close_conv (conv);
1070 g_free (data);
1071 return result;
1074 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1075 static void
1076 nice_cd (const char *text, const char *xtext, const char *help,
1077 const char *history_name, const char *prefix, int to_home)
1079 char *machine;
1080 char *cd_path;
1082 if (!SELECTED_IS_PANEL)
1083 return;
1085 machine = input_dialog_help (text, xtext, help, history_name, "");
1086 if (!machine)
1087 return;
1089 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1090 ugly as hell and leads to problems in vfs layer */
1092 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1093 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1094 else
1095 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1097 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1098 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1099 else
1100 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1101 g_free (cd_path);
1102 g_free (machine);
1104 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1107 #ifdef USE_NETCODE
1109 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1111 #ifdef ENABLE_VFS_MCFS
1112 void netlink_cmd (void)
1114 nice_cd (_(" Link to a remote machine "), _(machine_str),
1115 "[Network File System]", ":netlink_cmd: Link to a remote ",
1116 "/#mc:", 1);
1118 #endif /* ENABLE_VFS_MCFS */
1120 void ftplink_cmd (void)
1122 nice_cd (_(" FTP to machine "), _(machine_str),
1123 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1126 void fishlink_cmd (void)
1128 nice_cd (_(" Shell link to machine "), _(machine_str),
1129 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1130 "/#sh:", 1);
1133 #ifdef WITH_SMBFS
1134 void smblink_cmd (void)
1136 nice_cd (_(" SMB link to machine "), _(machine_str),
1137 "[SMB File System]", ":smblink_cmd: SMB link to machine ",
1138 "/#smb:", 0);
1140 #endif /* WITH_SMBFS */
1141 #endif /* USE_NETCODE */
1143 #ifdef USE_EXT2FSLIB
1144 void undelete_cmd (void)
1146 nice_cd (_(" Undelete files on an ext2 file system "),
1147 _(" Enter device (without /dev/) to undelete\n "
1148 " files on: (F1 for details)"),
1149 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ",
1150 "/#undel:", 0);
1152 #endif /* USE_EXT2FSLIB */
1154 void quick_cd_cmd (void)
1156 char *p = cd_dialog ();
1158 if (p && *p) {
1159 char *q = g_strconcat ("cd ", p, (char *) NULL);
1161 do_cd_command (q);
1162 g_free (q);
1164 g_free (p);
1169 \brief calculate dirs sizes
1171 calculate dirs sizes and resort panel:
1172 dirs_selected = show size for selected dirs,
1173 otherwise = show size for dir under cursor:
1174 dir under cursor ".." = show size for all dirs,
1175 otherwise = show size for dir under cursor
1177 void
1178 smart_dirsize_cmd (void)
1180 WPanel *panel = current_panel;
1181 file_entry *entry;
1183 entry = &(panel->dir.list[panel->selected]);
1184 if ( ( S_ISDIR (entry->st.st_mode) && ( strcmp(entry->fname, "..") == 0 ) )
1185 || panel->dirs_marked )
1186 dirsizes_cmd ();
1187 else
1188 single_dirsize_cmd ();
1191 void
1192 single_dirsize_cmd (void)
1194 WPanel *panel = current_panel;
1195 file_entry *entry;
1196 off_t marked;
1197 double total;
1198 ComputeDirSizeUI *ui;
1200 entry = &(panel->dir.list[panel->selected]);
1201 if (S_ISDIR (entry->st.st_mode) && strcmp(entry->fname, "..") != 0) {
1202 ui = compute_dir_size_create_ui ();
1204 total = 0.0;
1206 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1207 &marked, &total) == FILE_CONT) {
1208 entry->st.st_size = (off_t) total;
1209 entry->f.dir_size_computed = 1;
1212 compute_dir_size_destroy_ui (ui);
1215 if (mark_moves_down)
1216 send_message (&(panel->widget), WIDGET_KEY, KEY_DOWN);
1218 recalculate_panel_summary (panel);
1220 if ( current_panel->sort_type == (sortfn *) sort_size )
1221 panel_re_sort (panel);
1223 panel->dirty = 1;
1226 void
1227 dirsizes_cmd (void)
1229 WPanel *panel = current_panel;
1230 int i;
1231 off_t marked;
1232 double total;
1233 ComputeDirSizeUI *ui;
1235 ui = compute_dir_size_create_ui ();
1237 for (i = 0; i < panel->count; i++)
1238 if (S_ISDIR (panel->dir.list [i].st.st_mode)
1239 && ((panel->dirs_marked && panel->dir.list [i].f.marked)
1240 || !panel->dirs_marked)
1241 && strcmp (panel->dir.list [i].fname, "..") != 0) {
1242 total = 0.0l;
1244 if (compute_dir_size (panel->dir.list [i].fname,
1245 ui, compute_dir_size_update_ui,
1246 &marked, &total) != FILE_CONT)
1247 break;
1249 panel->dir.list [i].st.st_size = (off_t) total;
1250 panel->dir.list [i].f.dir_size_computed = 1;
1253 compute_dir_size_destroy_ui (ui);
1255 recalculate_panel_summary (panel);
1257 if ( current_panel->sort_type == (sortfn *) sort_size )
1258 panel_re_sort (panel);
1260 panel->dirty = 1;
1263 void
1264 save_setup_cmd (void)
1266 save_setup ();
1267 message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"), PROFILE_NAME);
1270 static void
1271 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1273 p->user_mini_status = use_msformat;
1274 p->list_type = view_type;
1276 if (view_type == list_user || use_msformat){
1277 g_free (p->user_format);
1278 p->user_format = user;
1280 g_free (p->user_status_format [view_type]);
1281 p->user_status_format [view_type] = status;
1283 set_panel_formats (p);
1285 else {
1286 g_free (user);
1287 g_free (status);
1290 set_panel_formats (p);
1291 do_refresh ();
1294 void
1295 info_cmd_no_menu (void)
1297 if (get_display_type (0) == view_info)
1298 set_display_type (0, view_listing);
1299 else if (get_display_type (1) == view_info)
1300 set_display_type (1, view_listing);
1301 else
1302 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1305 void
1306 quick_cmd_no_menu (void)
1308 if (get_display_type (0) == view_quick)
1309 set_display_type (0, view_listing);
1310 else if (get_display_type (1) == view_quick)
1311 set_display_type (1, view_listing);
1312 else
1313 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1316 static void
1317 switch_to_listing (int panel_index)
1319 if (get_display_type (panel_index) != view_listing)
1320 set_display_type (panel_index, view_listing);
1323 void
1324 listing_cmd (void)
1326 int view_type, use_msformat;
1327 char *user, *status;
1328 WPanel *p;
1329 int display_type;
1331 display_type = get_display_type (MENU_PANEL_IDX);
1332 if (display_type == view_listing)
1333 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1334 else
1335 p = 0;
1337 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1339 if (view_type == -1)
1340 return;
1342 switch_to_listing (MENU_PANEL_IDX);
1344 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1346 configure_panel_listing (p, view_type, use_msformat, user, status);
1349 void
1350 tree_cmd (void)
1352 set_display_type (MENU_PANEL_IDX, view_tree);
1355 void
1356 info_cmd (void)
1358 set_display_type (MENU_PANEL_IDX, view_info);
1361 void
1362 quick_view_cmd (void)
1364 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1365 change_panel ();
1366 set_display_type (MENU_PANEL_IDX, view_quick);
1369 /* Handle the tree internal listing modes switching */
1370 static int
1371 set_basic_panel_listing_to (int panel_index, int listing_mode)
1373 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1375 switch_to_listing (panel_index);
1376 p->list_type = listing_mode;
1377 if (set_panel_formats (p))
1378 return 0;
1380 do_refresh ();
1381 return 1;
1384 void
1385 toggle_listing_cmd (void)
1387 int current = get_current_index ();
1388 WPanel *p = (WPanel *) get_panel_widget (current);
1390 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1393 void
1394 encoding_cmd (void)
1396 WPanel *panel;
1398 if (!SELECTED_IS_PANEL)
1399 return;
1401 panel = MENU_PANEL;
1402 set_panel_encoding (panel);