Completely removed MHL stuff
[midnight-commander.git] / src / cmd.c
blob84f6e071aef7866171d0ee573cf18341c2045541
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 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 #include <config.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <string.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29 #ifdef HAVE_MMAP
30 # include <sys/mman.h>
31 #endif
32 #ifdef USE_NETCODE
33 #include <netdb.h>
34 #endif
35 #include <unistd.h>
37 #include "global.h"
38 #include "cmd.h" /* Our definitions */
39 #include "fileopctx.h" /* file_op_context_new() */
40 #include "file.h" /* copy_file_file() */
41 #include "find.h" /* do_find() */
42 #include "hotlist.h" /* hotlist_cmd() */
43 #include "tree.h" /* tree_chdir() */
44 #include "subshell.h" /* use_subshell */
45 #include "cons.saver.h" /* console_flag */
46 #include "tty.h" /* LINES */
47 #include "dialog.h" /* Widget */
48 #include "view.h" /* mc_internal_viewer() */
49 #include "wtools.h" /* message() */
50 #include "widget.h" /* push_history() */
51 #include "key.h" /* application_keypad_mode() */
52 #include "win.h" /* do_enter_ca_mode() */
53 #include "main.h" /* change_panel() */
54 #include "panel.h" /* current_panel */
55 #include "help.h" /* interactive_display() */
56 #include "user.h" /* MC_GLOBAL_MENU */
57 #include "command.h" /* cmdline */
58 #include "layout.h" /* get_current_type() */
59 #include "ext.h" /* regex_command() */
60 #include "boxes.h" /* cd_dialog() */
61 #include "setup.h" /* save_setup() */
62 #include "profile.h" /* PROFILE_NAME */
63 #include "execute.h" /* toggle_panels() */
64 #include "history.h"
66 #ifndef MAP_FILE
67 # define MAP_FILE 0
68 #endif
70 #ifdef USE_INTERNAL_EDIT
71 # include "../edit/edit.h"
72 #endif
74 /* If set and you don't have subshell support,then C-o will give you a shell */
75 int output_starts_shell = 0;
77 /* If set, use the builtin editor */
78 int use_internal_edit = 1;
80 /* Automatically fills name with current selected item name on mkdir */
81 int auto_fill_mkdir_name = 1;
83 int
84 view_file_at_line (const char *filename, int plain_view, int internal,
85 int start_line)
87 static const char *viewer = NULL;
88 int move_dir = 0;
91 if (plain_view) {
92 int changed_hex_mode = 0;
93 int changed_nroff_flag = 0;
94 int changed_magic_flag = 0;
96 altered_hex_mode = 0;
97 altered_nroff_flag = 0;
98 altered_magic_flag = 0;
99 if (default_hex_mode)
100 changed_hex_mode = 1;
101 if (default_nroff_flag)
102 changed_nroff_flag = 1;
103 if (default_magic_flag)
104 changed_magic_flag = 1;
105 default_hex_mode = 0;
106 default_nroff_flag = 0;
107 default_magic_flag = 0;
108 mc_internal_viewer (NULL, filename, &move_dir, start_line);
109 if (changed_hex_mode && !altered_hex_mode)
110 default_hex_mode = 1;
111 if (changed_nroff_flag && !altered_nroff_flag)
112 default_nroff_flag = 1;
113 if (changed_magic_flag && !altered_magic_flag)
114 default_magic_flag = 1;
115 repaint_screen ();
116 return move_dir;
118 if (internal) {
119 char view_entry[BUF_TINY];
121 if (start_line != 0)
122 g_snprintf (view_entry, sizeof (view_entry), "View:%d",
123 start_line);
124 else
125 strcpy (view_entry, "View");
127 if (regex_command (filename, view_entry, &move_dir) == 0) {
128 mc_internal_viewer (NULL, filename, &move_dir, start_line);
129 repaint_screen ();
131 } else {
132 if (!viewer) {
133 viewer = getenv ("VIEWER");
134 if (!viewer)
135 viewer = getenv ("PAGER");
136 if (!viewer)
137 viewer = "view";
139 execute_with_vfs_arg (viewer, filename);
141 return move_dir;
144 /* view_file (filename, plain_view, internal)
146 * Inputs:
147 * filename: The file name to view
148 * plain_view: If set does not do any fancy pre-processing (no filtering) and
149 * always invokes the internal viewer.
150 * internal: If set uses the internal viewer, otherwise an external viewer.
153 view_file (const char *filename, int plain_view, int internal)
155 return view_file_at_line (filename, plain_view, internal, 0);
158 /* scan_for_file (panel, idx, direction)
160 * Inputs:
161 * panel: pointer to the panel on which we operate
162 * idx: starting file.
163 * direction: 1, or -1
165 static int scan_for_file (WPanel *panel, int idx, int direction)
167 int i = idx + direction;
169 while (i != idx){
170 if (i < 0)
171 i = panel->count - 1;
172 if (i == panel->count)
173 i = 0;
174 if (!S_ISDIR (panel->dir.list [i].st.st_mode))
175 return i;
176 i += direction;
178 return i;
182 * Run viewer (internal or external) on the currently selected file.
183 * If normal is 1, force internal viewer and raw mode (used for F13).
185 static void
186 do_view_cmd (int normal)
188 int dir, file_idx;
190 /* Directories are viewed by changing to them */
191 if (S_ISDIR (selection (current_panel)->st.st_mode)
192 || link_isdir (selection (current_panel))) {
193 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked)) {
194 if (query_dialog
195 (_(" Confirmation "), _("Files tagged, want to cd?"), D_NORMAL, 2,
196 _("&Yes"), _("&No")) != 0) {
197 return;
200 if (!do_cd (selection (current_panel)->fname, cd_exact))
201 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
203 repaint_screen();
204 return;
207 file_idx = current_panel->selected;
208 while (1) {
209 char *filename;
211 filename = current_panel->dir.list[file_idx].fname;
213 dir = view_file (filename, normal, use_internal_view);
214 if (dir == 0)
215 break;
216 file_idx = scan_for_file (current_panel, file_idx, dir);
219 repaint_screen();
222 /* Run user's preferred viewer on the currently selected file */
223 void
224 view_cmd (void)
226 do_view_cmd (0);
229 /* Ask for file and run user's preferred viewer on it */
230 void
231 view_file_cmd (void)
233 char *filename;
235 filename =
236 input_expand_dialog (_(" View file "), _(" Filename:"),
237 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
238 if (!filename)
239 return;
241 view_file (filename, 0, use_internal_view);
242 g_free (filename);
245 /* Run plain internal viewer on the currently selected file */
246 void
247 view_simple_cmd (void)
249 do_view_cmd (1);
252 void
253 filtered_view_cmd (void)
255 char *command;
257 command =
258 input_dialog (_(" Filtered view "),
259 _(" Filter command and arguments:"),
260 MC_HISTORY_FM_FILTERED_VIEW,
261 selection (current_panel)->fname);
262 if (!command)
263 return;
265 mc_internal_viewer (command, "", NULL, 0);
267 g_free (command);
270 void do_edit_at_line (const char *what, int start_line)
272 static const char *editor = NULL;
274 #ifdef USE_INTERNAL_EDIT
275 if (use_internal_edit){
276 edit_file (what, start_line);
277 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
278 repaint_screen ();
279 return;
281 #endif /* USE_INTERNAL_EDIT */
282 if (!editor){
283 editor = getenv ("EDITOR");
284 if (!editor)
285 editor = get_default_editor ();
287 execute_with_vfs_arg (editor, what);
288 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
289 repaint_screen ();
292 static void
293 do_edit (const char *what)
295 do_edit_at_line (what, 0);
298 void
299 edit_cmd (void)
301 if (regex_command (selection (current_panel)->fname, "Edit", 0) == 0)
302 do_edit (selection (current_panel)->fname);
305 void
306 edit_cmd_new (void)
308 do_edit (NULL);
311 /* Invoked by F5. Copy, default to the other panel. */
312 void
313 copy_cmd (void)
315 save_cwds_stat ();
316 if (panel_operate (current_panel, OP_COPY, 0)) {
317 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
318 repaint_screen ();
322 /* Invoked by F6. Move/rename, default to the other panel, ignore marks. */
323 void ren_cmd (void)
325 save_cwds_stat ();
326 if (panel_operate (current_panel, OP_MOVE, 0)){
327 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
328 repaint_screen ();
332 /* Invoked by F15. Copy, default to the same panel, ignore marks. */
333 void copy_cmd_local (void)
335 save_cwds_stat ();
336 if (panel_operate (current_panel, OP_COPY, 1)){
337 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
338 repaint_screen ();
342 /* Invoked by F16. Move/rename, default to the same panel. */
343 void ren_cmd_local (void)
345 save_cwds_stat ();
346 if (panel_operate (current_panel, OP_MOVE, 1)){
347 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
348 repaint_screen ();
352 void
353 mkdir_cmd (void)
355 char *dir, *absdir;
356 char *name = "";
358 /* If 'on' then automatically fills name with current selected item name */
359 if (auto_fill_mkdir_name)
360 name = selection (current_panel)->fname;
362 dir =
363 input_expand_dialog (_("Create a new Directory"),
364 _(" Enter directory name:"),
365 MC_HISTORY_FM_MKDIR,
366 name);
368 if (!dir)
369 return;
371 if (dir[0] == '/' || dir[0] == '~')
372 absdir = g_strdup (dir);
373 else
374 absdir = concat_dir_and_file (current_panel->cwd, dir);
376 save_cwds_stat ();
377 if (my_mkdir (absdir, 0777) == 0) {
378 update_panels (UP_OPTIMIZE, dir);
379 repaint_screen ();
380 select_item (current_panel);
381 } else {
382 message (D_ERROR, MSG_ERROR, " %s ", unix_error_string (errno));
385 g_free (absdir);
386 g_free (dir);
389 void delete_cmd (void)
391 save_cwds_stat ();
393 if (panel_operate (current_panel, OP_DELETE, 0)){
394 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
395 repaint_screen ();
399 /* Invoked by F18. Remove selected file, regardless of marked files. */
400 void delete_cmd_local (void)
402 save_cwds_stat ();
404 if (panel_operate (current_panel, OP_DELETE, 1)){
405 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
406 repaint_screen ();
410 void find_cmd (void)
412 do_find ();
415 static void
416 set_panel_filter_to (WPanel *p, char *allocated_filter_string)
418 g_free (p->filter);
419 p->filter = 0;
421 if (!(allocated_filter_string [0] == '*' && allocated_filter_string [1] == 0))
422 p->filter = allocated_filter_string;
423 else
424 g_free (allocated_filter_string);
425 reread_cmd ();
428 /* Set a given panel filter expression */
429 static void
430 set_panel_filter (WPanel *p)
432 char *reg_exp;
433 const char *x;
435 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
437 reg_exp = input_dialog_help (_(" Filter "),
438 _(" Set expression for filtering filenames"),
439 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
440 if (!reg_exp)
441 return;
442 set_panel_filter_to (p, reg_exp);
445 /* Invoked from the left/right menus */
446 void filter_cmd (void)
448 WPanel *p;
450 if (!SELECTED_IS_PANEL)
451 return;
453 p = MENU_PANEL;
454 set_panel_filter (p);
457 void reread_cmd (void)
459 int flag;
461 if (get_current_type () == view_listing &&
462 get_other_type () == view_listing)
463 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
464 else
465 flag = UP_ONLY_CURRENT;
467 update_panels (UP_RELOAD|flag, UP_KEEPSEL);
468 repaint_screen ();
471 void reverse_selection_cmd (void)
473 file_entry *file;
474 int i;
476 for (i = 0; i < current_panel->count; i++){
477 file = &current_panel->dir.list [i];
478 if (S_ISDIR (file->st.st_mode))
479 continue;
480 do_file_mark (current_panel, i, !file->f.marked);
484 static void
485 select_unselect_cmd (const char *title, const char *history_name, int cmd)
487 char *reg_exp, *reg_exp_t;
488 int i;
489 int c;
490 int dirflag = 0;
492 reg_exp = input_dialog (title, "", history_name, easy_patterns ? "*" : ".");
493 if (!reg_exp)
494 return;
495 if (!*reg_exp) {
496 g_free (reg_exp);
497 return;
500 reg_exp_t = reg_exp;
502 /* Check if they specified a directory */
503 if (*reg_exp_t == PATH_SEP) {
504 dirflag = 1;
505 reg_exp_t++;
507 if (reg_exp_t[strlen (reg_exp_t) - 1] == PATH_SEP) {
508 dirflag = 1;
509 reg_exp_t[strlen (reg_exp_t) - 1] = 0;
512 for (i = 0; i < current_panel->count; i++) {
513 if (!strcmp (current_panel->dir.list[i].fname, ".."))
514 continue;
515 if (S_ISDIR (current_panel->dir.list[i].st.st_mode)) {
516 if (!dirflag)
517 continue;
518 } else {
519 if (dirflag)
520 continue;
522 c = regexp_match (reg_exp_t, current_panel->dir.list[i].fname,
523 match_file);
524 if (c == -1) {
525 message (D_ERROR, MSG_ERROR, _(" Malformed regular expression "));
526 g_free (reg_exp);
527 return;
529 if (c) {
530 do_file_mark (current_panel, i, cmd);
533 g_free (reg_exp);
536 void select_cmd (void)
538 select_unselect_cmd (_(" Select "), ":select_cmd: Select ", 1);
541 void unselect_cmd (void)
543 select_unselect_cmd (_(" Unselect "), ":unselect_cmd: Unselect ", 0);
546 /* Check if the file exists */
547 /* If not copy the default */
548 static int
549 check_for_default(char *default_file, char *file)
551 struct stat s;
552 off_t count = 0;
553 double bytes = 0;
554 FileOpContext *ctx;
556 if (mc_stat (file, &s)){
557 if (mc_stat (default_file, &s)){
558 return -1;
560 ctx = file_op_context_new (OP_COPY);
561 file_op_context_create_ui (ctx, 0);
562 copy_file_file (ctx, default_file, file, 1, &count, &bytes, 1);
563 file_op_context_destroy (ctx);
565 return 0;
568 void ext_cmd (void)
570 char *buffer;
571 char *extdir;
572 int dir;
574 dir = 0;
575 if (geteuid () == 0){
576 dir = query_dialog (_("Extension file edit"),
577 _(" Which extension file you want to edit? "), D_NORMAL, 2,
578 _("&User"), _("&System Wide"));
580 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
582 if (dir == 0){
583 buffer = concat_dir_and_file (home_dir, MC_USER_EXT);
584 check_for_default (extdir, buffer);
585 do_edit (buffer);
586 g_free (buffer);
587 } else if (dir == 1)
588 do_edit (extdir);
590 g_free (extdir);
591 flush_extension_file ();
594 /* where = 0 - do edit file menu for mc */
595 /* where = 1 - do edit file menu for mcedit */
596 static void
597 menu_edit_cmd (int where)
599 char *buffer;
600 char *menufile;
601 int dir = 0;
603 dir = query_dialog (
604 _(" Menu edit "),
605 _(" Which menu file do you want to edit? "),
606 D_NORMAL, geteuid() ? 2 : 3,
607 _("&Local"), _("&User"), _("&System Wide")
610 menufile = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
612 switch (dir) {
613 case 0:
614 buffer = g_strdup (where ? CEDIT_LOCAL_MENU : MC_LOCAL_MENU);
615 check_for_default (menufile, buffer);
616 break;
618 case 1:
619 buffer = concat_dir_and_file (home_dir, where ? CEDIT_HOME_MENU : MC_HOME_MENU);
620 check_for_default (menufile, buffer);
621 break;
623 case 2:
624 buffer = concat_dir_and_file (mc_home, where ? CEDIT_GLOBAL_MENU : MC_GLOBAL_MENU);
625 break;
627 default:
628 g_free (menufile);
629 return;
631 do_edit (buffer);
632 if (dir == 0)
633 chmod(buffer, 0600);
634 g_free (buffer);
635 g_free (menufile);
638 void quick_chdir_cmd (void)
640 char *target;
642 target = hotlist_cmd (LIST_HOTLIST);
643 if (!target)
644 return;
646 if (get_current_type () == view_tree)
647 tree_chdir (the_tree, target);
648 else
649 if (!do_cd (target, cd_exact))
650 message (D_ERROR, MSG_ERROR, _("Cannot change directory") );
651 g_free (target);
654 /* edit file menu for mc */
655 void
656 edit_mc_menu_cmd (void)
658 menu_edit_cmd (0);
661 #ifdef USE_INTERNAL_EDIT
662 /* edit file menu for mcedit */
663 void
664 edit_user_menu_cmd (void)
666 menu_edit_cmd (1);
669 /* edit syntax file for mcedit */
670 void
671 edit_syntax_cmd (void)
673 char *buffer;
674 char *extdir;
675 int dir = 0;
677 if (geteuid () == 0) {
678 dir =
679 query_dialog (_("Syntax file edit"),
680 _(" Which syntax file you want to edit? "), D_NORMAL, 2,
681 _("&User"), _("&System Wide"));
683 extdir = concat_dir_and_file (mc_home, "syntax" PATH_SEP_STR "Syntax");
685 if (dir == 0) {
686 buffer = concat_dir_and_file (home_dir, SYNTAX_FILE);
687 check_for_default (extdir, buffer);
688 do_edit (buffer);
689 g_free (buffer);
690 } else if (dir == 1)
691 do_edit (extdir);
693 g_free (extdir);
695 #endif
697 #ifdef USE_VFS
698 void reselect_vfs (void)
700 char *target;
702 target = hotlist_cmd (LIST_VFSLIST);
703 if (!target)
704 return;
706 if (!do_cd (target, cd_exact))
707 message (D_ERROR, MSG_ERROR, _("Cannot change directory") );
708 g_free (target);
710 #endif /* USE_VFS */
712 static int compare_files (char *name1, char *name2, off_t size)
714 int file1, file2;
715 int result = -1; /* Different by default */
717 file1 = open (name1, O_RDONLY);
718 if (file1 >= 0){
719 file2 = open (name2, O_RDONLY);
720 if (file2 >= 0){
721 #ifdef HAVE_MMAP
722 char *data1, *data2;
723 /* Ugly if jungle */
724 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
725 if (data1 != (char*) -1){
726 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
727 if (data2 != (char*) -1){
728 rotate_dash ();
729 result = memcmp (data1, data2, size);
730 munmap (data2, size);
732 munmap (data1, size);
734 #else
735 /* Don't have mmap() :( Even more ugly :) */
736 char buf1[BUFSIZ], buf2[BUFSIZ];
737 int n1, n2;
738 rotate_dash ();
741 while((n1 = read(file1,buf1,BUFSIZ)) == -1 && errno == EINTR);
742 while((n2 = read(file2,buf2,BUFSIZ)) == -1 && errno == EINTR);
743 } while (n1 == n2 && n1 == BUFSIZ && !memcmp(buf1,buf2,BUFSIZ));
744 result = (n1 != n2) || memcmp(buf1,buf2,n1);
745 #endif /* !HAVE_MMAP */
746 close (file2);
748 close (file1);
750 return result;
753 enum CompareMode {
754 compare_quick, compare_size_only, compare_thourough
757 static void
758 compare_dir (WPanel *panel, WPanel *other, enum CompareMode mode)
760 int i, j;
761 char *src_name, *dst_name;
763 /* No marks by default */
764 panel->marked = 0;
765 panel->total = 0;
766 panel->dirs_marked = 0;
768 /* Handle all files in the panel */
769 for (i = 0; i < panel->count; i++){
770 file_entry *source = &panel->dir.list[i];
772 /* Default: unmarked */
773 file_mark (panel, i, 0);
775 /* Skip directories */
776 if (S_ISDIR (source->st.st_mode))
777 continue;
779 /* Search the corresponding entry from the other panel */
780 for (j = 0; j < other->count; j++){
781 if (strcmp (source->fname,
782 other->dir.list[j].fname) == 0)
783 break;
785 if (j >= other->count)
786 /* Not found -> mark */
787 do_file_mark (panel, i, 1);
788 else {
789 /* Found */
790 file_entry *target = &other->dir.list[j];
792 if (mode != compare_size_only){
793 /* Older version is not marked */
794 if (source->st.st_mtime < target->st.st_mtime)
795 continue;
798 /* Newer version with different size is marked */
799 if (source->st.st_size != target->st.st_size){
800 do_file_mark (panel, i, 1);
801 continue;
804 if (mode == compare_size_only)
805 continue;
807 if (mode == compare_quick){
808 /* Thorough compare off, compare only time stamps */
809 /* Mark newer version, don't mark version with the same date */
810 if (source->st.st_mtime > target->st.st_mtime){
811 do_file_mark (panel, i, 1);
813 continue;
816 /* Thorough compare on, do byte-by-byte comparison */
817 src_name = concat_dir_and_file (panel->cwd, source->fname);
818 dst_name = concat_dir_and_file (other->cwd, target->fname);
819 if (compare_files (src_name, dst_name, source->st.st_size))
820 do_file_mark (panel, i, 1);
821 g_free (src_name);
822 g_free (dst_name);
824 } /* for (i ...) */
827 void
828 compare_dirs_cmd (void)
830 int choice;
831 enum CompareMode thorough_flag;
833 choice =
834 query_dialog (_(" Compare directories "),
835 _(" Select compare method: "), 0, D_NORMAL,
836 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
838 if (choice < 0 || choice > 2)
839 return;
840 else
841 thorough_flag = choice;
843 if (get_current_type () == view_listing
844 && get_other_type () == view_listing) {
845 compare_dir (current_panel, other_panel, thorough_flag);
846 compare_dir (other_panel, current_panel, thorough_flag);
847 } else {
848 message (D_ERROR, MSG_ERROR,
849 _(" Both panels should be in the "
850 "listing mode to use this command "));
854 void
855 history_cmd (void)
857 Listbox *listbox;
858 GList *current;
860 if (cmdline->need_push) {
861 if (push_history (cmdline, cmdline->buffer) == 2)
862 cmdline->need_push = 0;
864 if (!cmdline->history) {
865 message (D_ERROR, MSG_ERROR, _(" The command history is empty "));
866 return;
868 current = g_list_first (cmdline->history);
869 listbox = create_listbox_window (60, 10, _(" Command history "),
870 "[Command Menu]");
871 while (current) {
872 LISTBOX_APPEND_TEXT (listbox, 0, (char *) current->data, current);
873 current = g_list_next(current);
875 run_dlg (listbox->dlg);
876 if (listbox->dlg->ret_value == B_CANCEL)
877 current = NULL;
878 else
879 current = listbox->list->current->data;
880 destroy_dlg (listbox->dlg);
881 g_free (listbox);
883 if (!current)
884 return;
885 cmdline->history = current;
886 assign_text (cmdline, (char *) current->data);
887 update_input (cmdline, 1);
890 void swap_cmd (void)
892 swap_panels ();
893 touchwin (stdscr);
894 repaint_screen ();
897 void
898 view_other_cmd (void)
900 static int message_flag = TRUE;
902 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell) {
903 if (message_flag)
904 message (D_ERROR, MSG_ERROR,
905 _(" Not an xterm or Linux console; \n"
906 " the panels cannot be toggled. "));
907 message_flag = FALSE;
908 } else {
909 toggle_panels ();
913 static void
914 do_link (int symbolic_link, const char *fname)
916 char *dest = NULL, *src = NULL;
918 if (!symbolic_link) {
919 src = g_strdup_printf (_("Link %s to:"), name_trunc (fname, 46));
920 dest = input_expand_dialog (_(" Link "), src, MC_HISTORY_FM_LINK, "");
921 if (!dest || !*dest)
922 goto cleanup;
923 save_cwds_stat ();
924 if (-1 == mc_link (fname, dest))
925 message (D_ERROR, MSG_ERROR, _(" link: %s "),
926 unix_error_string (errno));
927 } else {
928 char *s;
929 char *d;
931 /* suggest the full path for symlink */
932 s = concat_dir_and_file (current_panel->cwd, fname);
934 if (get_other_type () == view_listing) {
935 d = concat_dir_and_file (other_panel->cwd, fname);
936 } else {
937 d = g_strdup (fname);
940 symlink_dialog (s, d, &dest, &src);
941 g_free (d);
942 g_free (s);
944 if (!dest || !*dest || !src || !*src)
945 goto cleanup;
946 save_cwds_stat ();
947 if (-1 == mc_symlink (dest, src))
948 message (D_ERROR, MSG_ERROR, _(" symlink: %s "),
949 unix_error_string (errno));
951 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
952 repaint_screen ();
954 cleanup:
955 g_free (src);
956 g_free (dest);
959 void link_cmd (void)
961 do_link (0, selection (current_panel)->fname);
964 void symlink_cmd (void)
966 char *filename = NULL;
967 filename = selection (current_panel)->fname;
969 if (filename) {
970 do_link (1, filename);
974 void edit_symlink_cmd (void)
976 if (S_ISLNK (selection (current_panel)->st.st_mode)) {
977 char buffer [MC_MAXPATHLEN];
978 char *p = NULL;
979 int i;
980 char *dest, *q;
982 p = selection (current_panel)->fname;
984 q = g_strdup_printf (_(" Symlink `%s\' points to: "), name_trunc (p, 32));
986 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
987 if (i > 0) {
988 buffer [i] = 0;
989 dest = input_expand_dialog (_(" Edit symlink "), q, MC_HISTORY_FM_EDIT_LINK, buffer);
990 if (dest) {
991 if (*dest && strcmp (buffer, dest)) {
992 save_cwds_stat ();
993 if (-1 == mc_unlink (p)){
994 message (D_ERROR, MSG_ERROR, _(" edit symlink, unable to remove %s: %s "),
995 p, unix_error_string (errno));
996 } else {
997 if (-1 == mc_symlink (dest, p))
998 message (D_ERROR, MSG_ERROR, _(" edit symlink: %s "),
999 unix_error_string (errno));
1001 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1002 repaint_screen ();
1004 g_free (dest);
1007 g_free (q);
1008 } else {
1009 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1010 selection (current_panel)->fname);
1014 void help_cmd (void)
1016 interactive_display (NULL, "[main]");
1019 void
1020 user_file_menu_cmd (void)
1022 user_menu_cmd (NULL);
1025 /* partly taken from dcigettext.c, returns "" for default locale */
1026 /* value should be freed by calling function g_free() */
1027 char *guess_message_value (void)
1029 static const char * const var[] = {
1030 /* The highest priority value is the `LANGUAGE' environment
1031 variable. This is a GNU extension. */
1032 "LANGUAGE",
1033 /* Setting of LC_ALL overwrites all other. */
1034 "LC_ALL",
1035 /* Next comes the name of the desired category. */
1036 "LC_MESSAGES",
1037 /* Last possibility is the LANG environment variable. */
1038 "LANG",
1039 /* NULL exit loops */
1040 NULL
1043 unsigned i = 0;
1044 const char *locale = NULL;
1046 while (var[i] != NULL) {
1047 locale = getenv (var[i]);
1048 if (locale != NULL && locale[0] != '\0')
1049 break;
1050 i++;
1053 if (locale == NULL)
1054 locale = "";
1056 return g_strdup (locale);
1060 * Return a random hint. If force is not 0, ignore the timeout.
1062 char *
1063 get_random_hint (int force)
1065 char *data, *result, *eol;
1066 int len;
1067 int start;
1068 static int last_sec;
1069 static struct timeval tv;
1071 /* Do not change hints more often than one minute */
1072 gettimeofday (&tv, NULL);
1073 if (!force && !(tv.tv_sec > last_sec + 60))
1074 return g_strdup ("");
1075 last_sec = tv.tv_sec;
1077 data = load_mc_home_file (MC_HINT, NULL);
1078 if (!data)
1079 return 0;
1081 /* get a random entry */
1082 srand (tv.tv_sec);
1083 len = strlen (data);
1084 start = rand () % len;
1086 for (; start; start--) {
1087 if (data[start] == '\n') {
1088 start++;
1089 break;
1092 eol = strchr (&data[start], '\n');
1093 if (eol)
1094 *eol = 0;
1095 result = g_strdup (&data[start]);
1096 g_free (data);
1097 return result;
1100 #if defined(USE_NETCODE) || defined(USE_EXT2FSLIB)
1101 static void
1102 nice_cd (const char *text, const char *xtext, const char *help,
1103 const char *history_name, const char *prefix, int to_home)
1105 char *machine;
1106 char *cd_path;
1108 if (!SELECTED_IS_PANEL)
1109 return;
1111 machine = input_dialog_help (text, xtext, help, history_name, "");
1112 if (!machine)
1113 return;
1115 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
1116 ugly as hell and leads to problems in vfs layer */
1118 if (strncmp (prefix, machine, strlen (prefix)) == 0)
1119 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1120 else
1121 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
1123 if (do_panel_cd (MENU_PANEL, cd_path, 0))
1124 directory_history_add (MENU_PANEL, (MENU_PANEL)->cwd);
1125 else
1126 message (D_ERROR, MSG_ERROR, _(" Cannot chdir to %s "), cd_path);
1127 g_free (cd_path);
1128 g_free (machine);
1130 #endif /* USE_NETCODE || USE_EXT2FSLIB */
1133 #ifdef USE_NETCODE
1135 static const char *machine_str = N_(" Enter machine name (F1 for details): ");
1137 #ifdef WITH_MCFS
1138 void netlink_cmd (void)
1140 nice_cd (_(" Link to a remote machine "), _(machine_str),
1141 "[Network File System]", ":netlink_cmd: Link to a remote ",
1142 "/#mc:", 1);
1144 #endif /* WITH_MCFS */
1146 void ftplink_cmd (void)
1148 nice_cd (_(" FTP to machine "), _(machine_str),
1149 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1152 void fishlink_cmd (void)
1154 nice_cd (_(" Shell link to machine "), _(machine_str),
1155 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1156 "/#sh:", 1);
1159 #ifdef WITH_SMBFS
1160 void smblink_cmd (void)
1162 nice_cd (_(" SMB link to machine "), _(machine_str),
1163 "[SMB File System]", ":smblink_cmd: SMB link to machine ",
1164 "/#smb:", 0);
1166 #endif /* WITH_SMBFS */
1167 #endif /* USE_NETCODE */
1169 #ifdef USE_EXT2FSLIB
1170 void undelete_cmd (void)
1172 nice_cd (_(" Undelete files on an ext2 file system "),
1173 _(" Enter device (without /dev/) to undelete\n "
1174 " files on: (F1 for details)"),
1175 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ",
1176 "/#undel:", 0);
1178 #endif /* USE_EXT2FSLIB */
1180 void quick_cd_cmd (void)
1182 char *p = cd_dialog ();
1184 if (p && *p) {
1185 char *q = g_strconcat ("cd ", p, (char *) NULL);
1187 do_cd_command (q);
1188 g_free (q);
1190 g_free (p);
1193 void
1194 single_dirsize_cmd (void)
1196 WPanel *panel = current_panel;
1197 file_entry *entry;
1198 off_t marked;
1199 double total;
1201 entry = &(panel->dir.list[panel->selected]);
1202 if (S_ISDIR (entry->st.st_mode) && strcmp(entry->fname, "..") != 0) {
1203 total = 0.0;
1204 compute_dir_size (entry->fname, &marked, &total);
1205 entry->st.st_size = (off_t) total;
1206 entry->f.dir_size_computed = 1;
1209 if (mark_moves_down)
1210 send_message (&(panel->widget), WIDGET_KEY, KEY_DOWN);
1212 recalculate_panel_summary (panel);
1213 panel->dirty = 1;
1216 void
1217 dirsizes_cmd (void)
1219 WPanel *panel = current_panel;
1220 int i;
1221 off_t marked;
1222 double total;
1224 for (i = 0; i < panel->count; i++)
1225 if (S_ISDIR (panel->dir.list [i].st.st_mode) &&
1226 ((panel->dirs_marked && panel->dir.list [i].f.marked) ||
1227 !panel->dirs_marked) &&
1228 strcmp (panel->dir.list [i].fname, "..") != 0) {
1229 total = 0.0l;
1230 compute_dir_size (panel->dir.list [i].fname, &marked, &total);
1231 panel->dir.list [i].st.st_size = (off_t) total;
1232 panel->dir.list [i].f.dir_size_computed = 1;
1235 recalculate_panel_summary (panel);
1236 panel_re_sort (panel);
1237 panel->dirty = 1;
1240 void
1241 save_setup_cmd (void)
1243 save_setup ();
1244 sync_profiles ();
1246 message (D_NORMAL, _(" Setup "), _(" Setup saved to ~/%s"), PROFILE_NAME);
1249 static void
1250 configure_panel_listing (WPanel *p, int view_type, int use_msformat, char *user, char *status)
1252 p->user_mini_status = use_msformat;
1253 p->list_type = view_type;
1255 if (view_type == list_user || use_msformat){
1256 g_free (p->user_format);
1257 p->user_format = user;
1259 g_free (p->user_status_format [view_type]);
1260 p->user_status_format [view_type] = status;
1262 set_panel_formats (p);
1264 else {
1265 g_free (user);
1266 g_free (status);
1269 set_panel_formats (p);
1270 do_refresh ();
1273 void
1274 info_cmd_no_menu (void)
1276 if (get_display_type (0) == view_info)
1277 set_display_type (0, view_listing);
1278 else if (get_display_type (1) == view_info)
1279 set_display_type (1, view_listing);
1280 else
1281 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1284 void
1285 quick_cmd_no_menu (void)
1287 if (get_display_type (0) == view_quick)
1288 set_display_type (0, view_listing);
1289 else if (get_display_type (1) == view_quick)
1290 set_display_type (1, view_listing);
1291 else
1292 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1295 static void
1296 switch_to_listing (int panel_index)
1298 if (get_display_type (panel_index) != view_listing)
1299 set_display_type (panel_index, view_listing);
1302 void
1303 listing_cmd (void)
1305 int view_type, use_msformat;
1306 char *user, *status;
1307 WPanel *p;
1308 int display_type;
1310 display_type = get_display_type (MENU_PANEL_IDX);
1311 if (display_type == view_listing)
1312 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1313 else
1314 p = 0;
1316 view_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1318 if (view_type == -1)
1319 return;
1321 switch_to_listing (MENU_PANEL_IDX);
1323 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1325 configure_panel_listing (p, view_type, use_msformat, user, status);
1328 void
1329 tree_cmd (void)
1331 set_display_type (MENU_PANEL_IDX, view_tree);
1334 void
1335 info_cmd (void)
1337 set_display_type (MENU_PANEL_IDX, view_info);
1340 void
1341 quick_view_cmd (void)
1343 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1344 change_panel ();
1345 set_display_type (MENU_PANEL_IDX, view_quick);
1348 /* Handle the tree internal listing modes switching */
1349 static int
1350 set_basic_panel_listing_to (int panel_index, int listing_mode)
1352 WPanel *p = (WPanel *) get_panel_widget (panel_index);
1354 switch_to_listing (panel_index);
1355 p->list_type = listing_mode;
1356 if (set_panel_formats (p))
1357 return 0;
1359 do_refresh ();
1360 return 1;
1363 void
1364 toggle_listing_cmd (void)
1366 int current = get_current_index ();
1367 WPanel *p = (WPanel *) get_panel_widget (current);
1369 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);