Split file src/keybind.[ch] to lib/keybind.[ch] and src/keybind-defaults.[ch].
[midnight-commander.git] / src / cmd.c
blob918bef6b2799ce7afc5ac97d221fed6983a891d3
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 ENABLE_VFS_NET
39 #include <netdb.h>
40 #endif
41 #include <unistd.h>
42 #include <stdlib.h>
43 #include <fcntl.h>
44 #include <pwd.h>
45 #include <grp.h>
46 #include <sys/time.h>
48 #include "lib/global.h"
50 #include "lib/tty/tty.h" /* LINES, tty_touch_screen() */
51 #include "lib/tty/key.h" /* ALT() macro */
52 #include "lib/tty/win.h" /* do_enter_ca_mode() */
53 #include "lib/mcconfig.h"
54 #include "lib/search.h"
55 #include "lib/filehighlight.h" /* MC_FHL_INI_FILE */
56 #include "lib/vfs/mc-vfs/vfs.h"
57 #include "lib/fileloc.h"
58 #include "lib/strutil.h"
59 #include "lib/util.h"
60 #include "lib/widget.h"
62 #include "cmd.h" /* Our definitions */
63 #include "fileopctx.h"
64 #include "file.h" /* file operation routines */
65 #include "find.h" /* do_find() */
66 #include "hotlist.h" /* hotlist_cmd() */
67 #include "tree.h" /* tree_chdir() */
68 #include "subshell.h" /* use_subshell */
69 #include "consaver/cons.saver.h" /* console_flag */
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 "dir.h"
82 #include "keybind-defaults.h" /* CK_InputHistoryShow */
83 #include "viewer/mcviewer.h"
86 #ifdef USE_INTERNAL_EDIT
87 #include "src/editor/edit.h"
88 #endif
90 #ifdef USE_DIFF_VIEW
91 #include "src/diffviewer/ydiff.h"
92 #endif
94 /*** global variables ****************************************************************************/
96 /* If set and you don't have subshell support,then C-o will give you a shell */
97 int output_starts_shell = 0;
99 /* If set, use the builtin editor */
100 int use_internal_edit = 1;
102 /* Automatically fills name with current selected item name on mkdir */
103 int auto_fill_mkdir_name = 1;
105 int select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
107 /*** file scope macro definitions ****************************************************************/
109 #ifndef MAP_FILE
110 #define MAP_FILE 0
111 #endif
113 /*** file scope type declarations ****************************************************************/
115 enum CompareMode
117 compare_quick, compare_size_only, compare_thourough
120 /*** file scope variables ************************************************************************/
122 #ifdef ENABLE_VFS_NET
123 static const char *machine_str = N_("Enter machine name (F1 for details):");
124 #endif /* ENABLE_VFS_NET */
126 /*** file scope functions ************************************************************************/
127 /* --------------------------------------------------------------------------------------------- */
129 /** scan_for_file (panel, idx, direction)
131 * Inputs:
132 * panel: pointer to the panel on which we operate
133 * idx: starting file.
134 * direction: 1, or -1
137 static int
138 scan_for_file (WPanel * panel, int idx, int direction)
140 int i = idx + direction;
142 while (i != idx)
144 if (i < 0)
145 i = panel->count - 1;
146 if (i == panel->count)
147 i = 0;
148 if (!S_ISDIR (panel->dir.list[i].st.st_mode))
149 return i;
150 i += direction;
152 return i;
155 /* --------------------------------------------------------------------------------------------- */
157 * Run viewer (internal or external) on the currently selected file.
158 * If normal is 1, force internal viewer and raw mode (used for F13).
160 static void
161 do_view_cmd (int normal)
163 /* Directories are viewed by changing to them */
164 if (S_ISDIR (selection (current_panel)->st.st_mode) || link_isdir (selection (current_panel)))
166 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked))
168 if (query_dialog
169 (_("Confirmation"), _("Files tagged, want to cd?"), D_NORMAL, 2,
170 _("&Yes"), _("&No")) != 0)
172 return;
175 if (!do_cd (selection (current_panel)->fname, cd_exact))
176 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
178 else
180 int dir, file_idx;
181 char *filename;
183 file_idx = current_panel->selected;
184 while (1)
186 filename = current_panel->dir.list[file_idx].fname;
188 dir = view_file (filename, normal, use_internal_view);
189 if (dir == 0)
190 break;
191 file_idx = scan_for_file (current_panel, file_idx, dir);
195 repaint_screen ();
198 /* --------------------------------------------------------------------------------------------- */
200 static inline void
201 do_edit (const char *what)
203 do_edit_at_line (what, use_internal_edit, 0);
206 /* --------------------------------------------------------------------------------------------- */
208 static void
209 set_panel_filter_to (WPanel * p, char *allocated_filter_string)
211 g_free (p->filter);
212 p->filter = 0;
214 if (!(allocated_filter_string[0] == '*' && allocated_filter_string[1] == 0))
215 p->filter = allocated_filter_string;
216 else
217 g_free (allocated_filter_string);
218 reread_cmd ();
221 /* --------------------------------------------------------------------------------------------- */
222 /** Set a given panel filter expression */
224 static void
225 set_panel_filter (WPanel * p)
227 char *reg_exp;
228 const char *x;
230 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
232 reg_exp = input_dialog_help (_("Filter"),
233 _("Set expression for filtering filenames"),
234 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
235 if (!reg_exp)
236 return;
237 set_panel_filter_to (p, reg_exp);
240 /* --------------------------------------------------------------------------------------------- */
242 static void
243 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
245 /* dialog sizes */
246 const int DX = 50;
247 const int DY = 7;
249 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
250 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
251 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
253 char *reg_exp;
254 mc_search_t *search;
255 int i;
257 QuickWidget quick_widgets[] = {
258 QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns),
259 QUICK_CHECKBOX (DX / 2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens),
260 QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only),
261 QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, &reg_exp),
262 QUICK_END
265 QuickDialog quick_dlg = {
266 DX, DY, -1, -1, title,
267 "[Select/Unselect Files]", quick_widgets, NULL, FALSE
270 if (quick_dialog (&quick_dlg) == B_CANCEL)
271 return;
273 if (!reg_exp)
274 return;
275 if (!*reg_exp)
277 g_free (reg_exp);
278 return;
280 search = mc_search_new (reg_exp, -1);
281 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
282 search->is_entire_line = TRUE;
283 search->is_case_sensitive = case_sens != 0;
285 for (i = 0; i < current_panel->count; i++)
287 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
288 continue;
289 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
290 continue;
292 if (mc_search_run (search, current_panel->dir.list[i].fname,
293 0, current_panel->dir.list[i].fnamelen, NULL))
294 do_file_mark (current_panel, i, do_select);
297 mc_search_free (search);
298 g_free (reg_exp);
300 /* result flags */
301 select_flags = 0;
302 if (case_sens != 0)
303 select_flags |= SELECT_MATCH_CASE;
304 if (files_only != 0)
305 select_flags |= SELECT_FILES_ONLY;
306 if (shell_patterns != 0)
307 select_flags |= SELECT_SHELL_PATTERNS;
310 /* --------------------------------------------------------------------------------------------- */
312 static int
313 compare_files (char *name1, char *name2, off_t size)
315 int file1, file2;
316 int result = -1; /* Different by default */
318 if (size == 0)
319 return 0;
321 file1 = open (name1, O_RDONLY);
322 if (file1 >= 0)
324 file2 = open (name2, O_RDONLY);
325 if (file2 >= 0)
327 #ifdef HAVE_MMAP
328 char *data1, *data2;
329 /* Ugly if jungle */
330 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
331 if (data1 != (char *) -1)
333 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
334 if (data2 != (char *) -1)
336 rotate_dash ();
337 result = memcmp (data1, data2, size);
338 munmap (data2, size);
340 munmap (data1, size);
342 #else
343 /* Don't have mmap() :( Even more ugly :) */
344 char buf1[BUFSIZ], buf2[BUFSIZ];
345 int n1, n2;
346 rotate_dash ();
349 while ((n1 = read (file1, buf1, BUFSIZ)) == -1 && errno == EINTR);
350 while ((n2 = read (file2, buf2, BUFSIZ)) == -1 && errno == EINTR);
352 while (n1 == n2 && n1 == BUFSIZ && !memcmp (buf1, buf2, BUFSIZ));
353 result = (n1 != n2) || memcmp (buf1, buf2, n1);
354 #endif /* !HAVE_MMAP */
355 close (file2);
357 close (file1);
359 return result;
362 /* --------------------------------------------------------------------------------------------- */
364 static void
365 compare_dir (WPanel * panel, WPanel * other, enum CompareMode mode)
367 int i, j;
368 char *src_name, *dst_name;
370 /* No marks by default */
371 panel->marked = 0;
372 panel->total = 0;
373 panel->dirs_marked = 0;
375 /* Handle all files in the panel */
376 for (i = 0; i < panel->count; i++)
378 file_entry *source = &panel->dir.list[i];
380 /* Default: unmarked */
381 file_mark (panel, i, 0);
383 /* Skip directories */
384 if (S_ISDIR (source->st.st_mode))
385 continue;
387 /* Search the corresponding entry from the other panel */
388 for (j = 0; j < other->count; j++)
390 if (strcmp (source->fname, other->dir.list[j].fname) == 0)
391 break;
393 if (j >= other->count)
394 /* Not found -> mark */
395 do_file_mark (panel, i, 1);
396 else
398 /* Found */
399 file_entry *target = &other->dir.list[j];
401 if (mode != compare_size_only)
403 /* Older version is not marked */
404 if (source->st.st_mtime < target->st.st_mtime)
405 continue;
408 /* Newer version with different size is marked */
409 if (source->st.st_size != target->st.st_size)
411 do_file_mark (panel, i, 1);
412 continue;
415 if (mode == compare_size_only)
416 continue;
418 if (mode == compare_quick)
420 /* Thorough compare off, compare only time stamps */
421 /* Mark newer version, don't mark version with the same date */
422 if (source->st.st_mtime > target->st.st_mtime)
424 do_file_mark (panel, i, 1);
426 continue;
429 /* Thorough compare on, do byte-by-byte comparison */
430 src_name = concat_dir_and_file (panel->cwd, source->fname);
431 dst_name = concat_dir_and_file (other->cwd, target->fname);
432 if (compare_files (src_name, dst_name, source->st.st_size))
433 do_file_mark (panel, i, 1);
434 g_free (src_name);
435 g_free (dst_name);
437 } /* for (i ...) */
440 /* --------------------------------------------------------------------------------------------- */
442 static void
443 do_link (link_type_t link_type, const char *fname)
445 char *dest = NULL, *src = NULL;
447 if (link_type == LINK_HARDLINK)
449 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
450 dest = input_expand_dialog (_("Link"), src, MC_HISTORY_FM_LINK, "");
451 if (!dest || !*dest)
452 goto cleanup;
453 save_cwds_stat ();
454 if (-1 == mc_link (fname, dest))
455 message (D_ERROR, MSG_ERROR, _("link: %s"), unix_error_string (errno));
457 else
459 char *s;
460 char *d;
462 /* suggest the full path for symlink, and either the full or
463 relative path to the file it points to */
464 s = concat_dir_and_file (current_panel->cwd, fname);
466 if (get_other_type () == view_listing)
467 d = concat_dir_and_file (other_panel->cwd, fname);
468 else
469 d = g_strdup (fname);
471 if (link_type == LINK_SYMLINK_RELATIVE)
472 s = diff_two_paths (other_panel->cwd, s);
474 symlink_dialog (s, d, &dest, &src);
475 g_free (d);
476 g_free (s);
478 if (!dest || !*dest || !src || !*src)
479 goto cleanup;
480 save_cwds_stat ();
481 if (-1 == mc_symlink (dest, src))
482 message (D_ERROR, MSG_ERROR, _("symlink: %s"), unix_error_string (errno));
485 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
486 repaint_screen ();
488 cleanup:
489 g_free (src);
490 g_free (dest);
493 /* --------------------------------------------------------------------------------------------- */
495 #if defined(ENABLE_VFS_UNDELFS) || defined(ENABLE_VFS_NET)
496 static void
497 nice_cd (const char *text, const char *xtext, const char *help,
498 const char *history_name, const char *prefix, int to_home)
500 char *machine;
501 char *cd_path;
503 if (!SELECTED_IS_PANEL)
504 return;
506 machine = input_dialog_help (text, xtext, help, history_name, "");
507 if (!machine)
508 return;
510 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
511 ugly as hell and leads to problems in vfs layer */
513 if (strncmp (prefix, machine, strlen (prefix)) == 0)
514 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
515 else
516 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
518 if (!do_panel_cd (MENU_PANEL, cd_path, cd_parse_command))
519 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
520 g_free (cd_path);
521 g_free (machine);
523 #endif /* ENABLE_VFS_UNDELFS || ENABLE_VFS_NET */
525 /* --------------------------------------------------------------------------------------------- */
527 static void
528 configure_panel_listing (WPanel * p, int list_type, int use_msformat, char *user, char *status)
530 p->user_mini_status = use_msformat;
531 p->list_type = list_type;
533 if (list_type == list_user || use_msformat)
535 g_free (p->user_format);
536 p->user_format = user;
538 g_free (p->user_status_format[list_type]);
539 p->user_status_format[list_type] = status;
541 set_panel_formats (p);
543 else
545 g_free (user);
546 g_free (status);
549 set_panel_formats (p);
550 do_refresh ();
553 /* --------------------------------------------------------------------------------------------- */
555 static void
556 switch_to_listing (int panel_index)
558 if (get_display_type (panel_index) != view_listing)
559 set_display_type (panel_index, view_listing);
562 /* --------------------------------------------------------------------------------------------- */
563 /** Handle the tree internal listing modes switching */
565 static gboolean
566 set_basic_panel_listing_to (int panel_index, int listing_mode)
568 WPanel *p = (WPanel *) get_panel_widget (panel_index);
569 gboolean ok;
571 switch_to_listing (panel_index);
572 p->list_type = listing_mode;
574 ok = set_panel_formats (p) == 0;
576 if (ok)
577 do_refresh ();
579 return ok;
582 /* --------------------------------------------------------------------------------------------- */
583 /*** public functions ****************************************************************************/
584 /* --------------------------------------------------------------------------------------------- */
587 view_file_at_line (const char *filename, int plain_view, int internal, int start_line)
589 static const char *viewer = NULL;
590 int move_dir = 0;
592 if (plain_view)
594 int changed_hex_mode = 0;
595 int changed_nroff_flag = 0;
596 int changed_magic_flag = 0;
598 mcview_altered_hex_mode = 0;
599 mcview_altered_nroff_flag = 0;
600 mcview_altered_magic_flag = 0;
601 if (mcview_default_hex_mode)
602 changed_hex_mode = 1;
603 if (mcview_default_nroff_flag)
604 changed_nroff_flag = 1;
605 if (mcview_default_magic_flag)
606 changed_magic_flag = 1;
607 mcview_default_hex_mode = 0;
608 mcview_default_nroff_flag = 0;
609 mcview_default_magic_flag = 0;
611 switch (mcview_viewer (NULL, filename, start_line))
613 case MCVIEW_WANT_NEXT:
614 move_dir = 1;
615 break;
616 case MCVIEW_WANT_PREV:
617 move_dir = -1;
618 break;
619 default:
620 move_dir = 0;
623 if (changed_hex_mode && !mcview_altered_hex_mode)
624 mcview_default_hex_mode = 1;
625 if (changed_nroff_flag && !mcview_altered_nroff_flag)
626 mcview_default_nroff_flag = 1;
627 if (changed_magic_flag && !mcview_altered_magic_flag)
628 mcview_default_magic_flag = 1;
630 dialog_switch_process_pending ();
632 else if (internal)
634 char view_entry[BUF_TINY];
636 if (start_line != 0)
637 g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line);
638 else
639 strcpy (view_entry, "View");
641 if (regex_command (filename, view_entry, &move_dir) == 0)
643 switch (mcview_viewer (NULL, filename, start_line))
645 case MCVIEW_WANT_NEXT:
646 move_dir = 1;
647 break;
648 case MCVIEW_WANT_PREV:
649 move_dir = -1;
650 break;
651 default:
652 move_dir = 0;
655 dialog_switch_process_pending ();
658 else
660 if (viewer == NULL)
662 viewer = getenv ("VIEWER");
663 if (viewer == NULL)
664 viewer = getenv ("PAGER");
665 if (viewer == NULL)
666 viewer = "view";
669 execute_with_vfs_arg (viewer, filename);
672 return move_dir;
675 /* --------------------------------------------------------------------------------------------- */
676 /** view_file (filename, plain_view, internal)
678 * Inputs:
679 * filename: The file name to view
680 * plain_view: If set does not do any fancy pre-processing (no filtering) and
681 * always invokes the internal viewer.
682 * internal: If set uses the internal viewer, otherwise an external viewer.
686 view_file (const char *filename, int plain_view, int internal)
688 return view_file_at_line (filename, plain_view, internal, 0);
692 /* --------------------------------------------------------------------------------------------- */
693 /** Run user's preferred viewer on the currently selected file */
695 void
696 view_cmd (void)
698 do_view_cmd (0);
701 /* --------------------------------------------------------------------------------------------- */
702 /** Ask for file and run user's preferred viewer on it */
704 void
705 view_file_cmd (void)
707 char *filename;
709 filename =
710 input_expand_dialog (_("View file"), _("Filename:"),
711 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
712 if (!filename)
713 return;
715 view_file (filename, 0, use_internal_view);
716 g_free (filename);
719 /* --------------------------------------------------------------------------------------------- */
720 /** Run plain internal viewer on the currently selected file */
721 void
722 view_simple_cmd (void)
724 do_view_cmd (1);
727 /* --------------------------------------------------------------------------------------------- */
729 void
730 filtered_view_cmd (void)
732 char *command;
734 command =
735 input_dialog (_("Filtered view"),
736 _("Filter command and arguments:"),
737 MC_HISTORY_FM_FILTERED_VIEW, selection (current_panel)->fname);
739 if (command != NULL)
741 mcview_viewer (command, "", 0);
742 g_free (command);
743 dialog_switch_process_pending ();
747 /* --------------------------------------------------------------------------------------------- */
749 void
750 do_edit_at_line (const char *what, gboolean internal, int start_line)
752 static const char *editor = NULL;
754 #ifdef USE_INTERNAL_EDIT
755 if (internal)
756 edit_file (what, start_line);
757 else
758 #else
759 (void) start_line;
760 #endif /* USE_INTERNAL_EDIT */
762 if (editor == NULL)
764 editor = getenv ("EDITOR");
765 if (editor == NULL)
766 editor = get_default_editor ();
768 execute_with_vfs_arg (editor, what);
771 if (mc_run_mode == MC_RUN_FULL)
772 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
774 #ifdef USE_INTERNAL_EDIT
775 if (use_internal_edit)
776 dialog_switch_process_pending ();
777 else
778 #endif /* USE_INTERNAL_EDIT */
779 repaint_screen ();
782 /* --------------------------------------------------------------------------------------------- */
784 void
785 edit_cmd (void)
787 if (regex_command (selection (current_panel)->fname, "Edit", NULL) == 0)
788 do_edit (selection (current_panel)->fname);
791 /* --------------------------------------------------------------------------------------------- */
793 #ifdef USE_INTERNAL_EDIT
794 void
795 edit_cmd_force_internal (void)
797 if (regex_command (selection (current_panel)->fname, "Edit", NULL) == 0)
798 do_edit_at_line (selection (current_panel)->fname, TRUE, 0);
800 #endif
802 /* --------------------------------------------------------------------------------------------- */
804 void
805 edit_cmd_new (void)
807 #if HAVE_CHARSET
808 source_codepage = default_source_codepage;
809 #endif
810 do_edit (NULL);
813 /* --------------------------------------------------------------------------------------------- */
814 /** Invoked by F5. Copy, default to the other panel. */
816 void
817 copy_cmd (void)
819 save_cwds_stat ();
820 if (panel_operate (current_panel, OP_COPY, FALSE))
822 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
823 repaint_screen ();
827 /* --------------------------------------------------------------------------------------------- */
828 /** Invoked by F6. Move/rename, default to the other panel, ignore marks. */
830 void
831 rename_cmd (void)
833 save_cwds_stat ();
834 if (panel_operate (current_panel, OP_MOVE, FALSE))
836 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
837 repaint_screen ();
841 /* --------------------------------------------------------------------------------------------- */
842 /** Invoked by F15. Copy, default to the same panel, ignore marks. */
844 void
845 copy_cmd_local (void)
847 save_cwds_stat ();
848 if (panel_operate (current_panel, OP_COPY, TRUE))
850 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
851 repaint_screen ();
855 /* --------------------------------------------------------------------------------------------- */
856 /** Invoked by F16. Move/rename, default to the same panel. */
858 void
859 rename_cmd_local (void)
861 save_cwds_stat ();
862 if (panel_operate (current_panel, OP_MOVE, TRUE))
864 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
865 repaint_screen ();
869 /* --------------------------------------------------------------------------------------------- */
871 void
872 mkdir_cmd (void)
874 char *dir, *absdir;
875 const char *name = "";
877 /* If 'on' then automatically fills name with current selected item name */
878 if (auto_fill_mkdir_name && strcmp (selection (current_panel)->fname, "..") != 0)
879 name = selection (current_panel)->fname;
881 dir =
882 input_expand_dialog (_("Create a new Directory"),
883 _("Enter directory name:"), MC_HISTORY_FM_MKDIR, name);
885 if (!dir)
886 return;
888 if (*dir)
890 if (dir[0] == '/' || dir[0] == '~')
891 absdir = g_strdup (dir);
892 else
893 absdir = concat_dir_and_file (current_panel->cwd, dir);
895 save_cwds_stat ();
896 if (my_mkdir (absdir, 0777) == 0)
898 update_panels (UP_OPTIMIZE, dir);
899 repaint_screen ();
900 select_item (current_panel);
902 else
904 message (D_ERROR, MSG_ERROR, "%s", unix_error_string (errno));
906 g_free (absdir);
908 g_free (dir);
911 /* --------------------------------------------------------------------------------------------- */
913 void
914 delete_cmd (void)
916 save_cwds_stat ();
918 if (panel_operate (current_panel, OP_DELETE, FALSE))
920 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
921 repaint_screen ();
925 /* --------------------------------------------------------------------------------------------- */
926 /** Invoked by F18. Remove selected file, regardless of marked files. */
928 void
929 delete_cmd_local (void)
931 save_cwds_stat ();
933 if (panel_operate (current_panel, OP_DELETE, TRUE))
935 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
936 repaint_screen ();
940 /* --------------------------------------------------------------------------------------------- */
942 void
943 find_cmd (void)
945 do_find ();
948 /* --------------------------------------------------------------------------------------------- */
949 /** Invoked from the left/right menus */
951 void
952 filter_cmd (void)
954 WPanel *p;
956 if (!SELECTED_IS_PANEL)
957 return;
959 p = MENU_PANEL;
960 set_panel_filter (p);
963 /* --------------------------------------------------------------------------------------------- */
965 void
966 reread_cmd (void)
968 int flag;
970 if (get_current_type () == view_listing && get_other_type () == view_listing)
971 flag = strcmp (current_panel->cwd, other_panel->cwd) ? UP_ONLY_CURRENT : 0;
972 else
973 flag = UP_ONLY_CURRENT;
975 update_panels (UP_RELOAD | flag, UP_KEEPSEL);
976 repaint_screen ();
979 /* --------------------------------------------------------------------------------------------- */
981 void
982 reverse_selection_cmd (void)
984 int i;
985 file_entry *file;
987 for (i = 0; i < current_panel->count; i++)
989 file = &current_panel->dir.list[i];
990 if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
991 do_file_mark (current_panel, i, !file->f.marked);
995 /* --------------------------------------------------------------------------------------------- */
997 void
998 select_cmd (void)
1000 select_unselect_cmd (_("Select"), ":select_cmd: Select ", TRUE);
1003 /* --------------------------------------------------------------------------------------------- */
1005 void
1006 unselect_cmd (void)
1008 select_unselect_cmd (_("Unselect"), ":unselect_cmd: Unselect ", FALSE);
1011 /* --------------------------------------------------------------------------------------------- */
1013 void
1014 ext_cmd (void)
1016 char *buffer;
1017 char *extdir;
1018 int dir;
1020 dir = 0;
1021 if (geteuid () == 0)
1023 dir = query_dialog (_("Extension file edit"),
1024 _("Which extension file you want to edit?"), D_NORMAL, 2,
1025 _("&User"), _("&System Wide"));
1027 extdir = concat_dir_and_file (mc_home, MC_LIB_EXT);
1029 if (dir == 0)
1031 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FILEBIND_FILE, NULL);
1032 check_for_default (extdir, buffer);
1033 do_edit (buffer);
1034 g_free (buffer);
1036 else if (dir == 1)
1038 if (!exist_file (extdir))
1040 g_free (extdir);
1041 extdir = concat_dir_and_file (mc_home_alt, MC_LIB_EXT);
1043 do_edit (extdir);
1045 g_free (extdir);
1046 flush_extension_file ();
1049 /* --------------------------------------------------------------------------------------------- */
1050 /** edit file menu for mc */
1052 void
1053 edit_mc_menu_cmd (void)
1055 char *buffer;
1056 char *menufile;
1057 int dir = 0;
1059 dir = query_dialog (_("Menu edit"),
1060 _("Which menu file do you want to edit?"),
1061 D_NORMAL, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
1063 menufile = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
1065 if (!exist_file (menufile))
1067 g_free (menufile);
1068 menufile = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
1071 switch (dir)
1073 case 0:
1074 buffer = g_strdup (MC_LOCAL_MENU);
1075 check_for_default (menufile, buffer);
1076 chmod (buffer, 0600);
1077 break;
1079 case 1:
1080 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_USERMENU_FILE, NULL);
1081 check_for_default (menufile, buffer);
1082 break;
1084 case 2:
1085 buffer = concat_dir_and_file (mc_home, MC_GLOBAL_MENU);
1086 if (!exist_file (buffer))
1088 g_free (buffer);
1089 buffer = concat_dir_and_file (mc_home_alt, MC_GLOBAL_MENU);
1091 break;
1093 default:
1094 g_free (menufile);
1095 return;
1098 do_edit (buffer);
1100 g_free (buffer);
1101 g_free (menufile);
1104 /* --------------------------------------------------------------------------------------------- */
1106 void
1107 edit_fhl_cmd (void)
1109 char *buffer = NULL;
1110 char *fhlfile = NULL;
1112 int dir;
1114 dir = 0;
1115 if (geteuid () == 0)
1117 dir = query_dialog (_("Highlighting groups file edit"),
1118 _("Which highlighting file you want to edit?"), D_NORMAL, 2,
1119 _("&User"), _("&System Wide"));
1121 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
1123 if (dir == 0)
1125 buffer = g_build_filename (home_dir, MC_USERCONF_DIR, MC_FHL_INI_FILE, NULL);
1126 check_for_default (fhlfile, buffer);
1127 do_edit (buffer);
1128 g_free (buffer);
1130 else if (dir == 1)
1132 if (!exist_file (fhlfile))
1134 g_free (fhlfile);
1135 fhlfile = concat_dir_and_file (mc_home, MC_FHL_INI_FILE);
1137 do_edit (fhlfile);
1139 g_free (fhlfile);
1141 /* refresh highlighting rules */
1142 mc_fhl_free (&mc_filehighlight);
1143 mc_filehighlight = mc_fhl_new (TRUE);
1146 /* --------------------------------------------------------------------------------------------- */
1148 void
1149 quick_chdir_cmd (void)
1151 char *target;
1153 target = hotlist_cmd (LIST_HOTLIST);
1154 if (!target)
1155 return;
1157 if (get_current_type () == view_tree)
1158 tree_chdir (the_tree, target);
1159 else
1161 char *cmd = g_strconcat ("cd ", target, (char *) NULL);
1162 do_cd_command (cmd);
1163 g_free (cmd);
1165 g_free (target);
1168 #ifdef ENABLE_VFS
1169 void
1170 reselect_vfs (void)
1172 char *target;
1174 target = hotlist_cmd (LIST_VFSLIST);
1175 if (!target)
1176 return;
1178 if (!do_cd (target, cd_exact))
1179 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
1180 g_free (target);
1182 #endif /* ENABLE_VFS */
1184 /* --------------------------------------------------------------------------------------------- */
1186 void
1187 compare_dirs_cmd (void)
1189 int choice;
1190 enum CompareMode thorough_flag;
1192 choice =
1193 query_dialog (_("Compare directories"),
1194 _("Select compare method:"), D_NORMAL, 4,
1195 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
1197 if (choice < 0 || choice > 2)
1198 return;
1200 thorough_flag = choice;
1202 if (get_current_type () == view_listing && get_other_type () == view_listing)
1204 compare_dir (current_panel, other_panel, thorough_flag);
1205 compare_dir (other_panel, current_panel, thorough_flag);
1207 else
1209 message (D_ERROR, MSG_ERROR,
1210 _("Both panels should be in the listing mode\nto use this command"));
1214 /* --------------------------------------------------------------------------------------------- */
1216 #ifdef USE_DIFF_VIEW
1217 void
1218 diff_view_cmd (void)
1220 dview_diff_cmd ();
1222 if (mc_run_mode == MC_RUN_FULL)
1223 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1225 dialog_switch_process_pending ();
1227 #endif
1229 /* --------------------------------------------------------------------------------------------- */
1231 void
1232 history_cmd (void)
1234 /* show the history of command line widget */
1235 send_message (&cmdline->widget, WIDGET_COMMAND, CK_InputHistoryShow);
1238 /* --------------------------------------------------------------------------------------------- */
1240 void
1241 swap_cmd (void)
1243 swap_panels ();
1244 tty_touch_screen ();
1245 repaint_screen ();
1248 /* --------------------------------------------------------------------------------------------- */
1250 void
1251 view_other_cmd (void)
1253 static int message_flag = TRUE;
1255 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell)
1257 if (message_flag)
1258 message (D_ERROR, MSG_ERROR,
1259 _("Not an xterm or Linux console;\nthe panels cannot be toggled."));
1260 message_flag = FALSE;
1262 else
1264 toggle_panels ();
1268 /* --------------------------------------------------------------------------------------------- */
1270 void
1271 link_cmd (link_type_t link_type)
1273 char *filename = selection (current_panel)->fname;
1275 if (filename != NULL)
1276 do_link (link_type, filename);
1279 /* --------------------------------------------------------------------------------------------- */
1281 void
1282 edit_symlink_cmd (void)
1284 if (S_ISLNK (selection (current_panel)->st.st_mode))
1286 char buffer[MC_MAXPATHLEN];
1287 char *p = NULL;
1288 int i;
1289 char *dest, *q;
1291 p = selection (current_panel)->fname;
1293 q = g_strdup_printf (_("Symlink `%s\' points to:"), str_trunc (p, 32));
1295 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
1296 if (i > 0)
1298 buffer[i] = 0;
1299 dest = input_expand_dialog (_("Edit symlink"), q, MC_HISTORY_FM_EDIT_LINK, buffer);
1300 if (dest)
1302 if (*dest && strcmp (buffer, dest))
1304 save_cwds_stat ();
1305 if (-1 == mc_unlink (p))
1307 message (D_ERROR, MSG_ERROR, _("edit symlink, unable to remove %s: %s"),
1308 p, unix_error_string (errno));
1310 else
1312 if (-1 == mc_symlink (dest, p))
1313 message (D_ERROR, MSG_ERROR, _("edit symlink: %s"),
1314 unix_error_string (errno));
1316 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1317 repaint_screen ();
1319 g_free (dest);
1322 g_free (q);
1324 else
1326 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1327 selection (current_panel)->fname);
1331 /* --------------------------------------------------------------------------------------------- */
1333 void
1334 help_cmd (void)
1336 if (current_panel->searching)
1337 interactive_display (NULL, "[Quick search]");
1338 else
1339 interactive_display (NULL, "[main]");
1342 /* --------------------------------------------------------------------------------------------- */
1344 void
1345 user_file_menu_cmd (void)
1347 user_menu_cmd (NULL);
1350 /* --------------------------------------------------------------------------------------------- */
1352 * Return a random hint. If force is not 0, ignore the timeout.
1355 char *
1356 get_random_hint (int force)
1358 char *data, *result = NULL, *eol;
1359 int len;
1360 int start;
1361 static int last_sec;
1362 static struct timeval tv;
1363 GIConv conv;
1364 GString *buffer;
1366 /* Do not change hints more often than one minute */
1367 gettimeofday (&tv, NULL);
1368 if (!force && !(tv.tv_sec > last_sec + 60))
1369 return g_strdup ("");
1370 last_sec = tv.tv_sec;
1372 data = load_mc_home_file (mc_home_alt, MC_HINT, NULL);
1373 if (data == NULL)
1374 return NULL;
1376 /* get a random entry */
1377 srand (tv.tv_sec);
1378 len = strlen (data);
1379 start = rand () % len;
1381 for (; start != 0; start--)
1382 if (data[start] == '\n')
1384 start++;
1385 break;
1388 eol = strchr (data + start, '\n');
1389 if (eol != NULL)
1390 *eol = '\0';
1392 /* hint files are stored in utf-8 */
1393 /* try convert hint file from utf-8 to terminal encoding */
1394 conv = str_crt_conv_from ("UTF-8");
1395 if (conv != INVALID_CONV)
1397 buffer = g_string_new ("");
1398 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1399 result = g_strdup (buffer->str);
1400 g_string_free (buffer, TRUE);
1401 str_close_conv (conv);
1404 g_free (data);
1405 return result;
1408 /* --------------------------------------------------------------------------------------------- */
1410 #ifdef ENABLE_VFS_NET
1411 #ifdef ENABLE_VFS_FTP
1412 void
1413 ftplink_cmd (void)
1415 nice_cd (_("FTP to machine"), _(machine_str),
1416 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1418 #endif /* ENABLE_VFS_FTP */
1420 /* --------------------------------------------------------------------------------------------- */
1422 #ifdef ENABLE_VFS_FISH
1423 void
1424 fishlink_cmd (void)
1426 nice_cd (_("Shell link to machine"), _(machine_str),
1427 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1428 "/#sh:", 1);
1430 #endif /* ENABLE_VFS_FISH */
1432 /* --------------------------------------------------------------------------------------------- */
1434 #ifdef ENABLE_VFS_SMB
1435 void
1436 smblink_cmd (void)
1438 nice_cd (_("SMB link to machine"), _(machine_str),
1439 "[SMB File System]", ":smblink_cmd: SMB link to machine ", "/#smb:", 0);
1441 #endif /* ENABLE_VFS_SMB */
1442 #endif /* ENABLE_VFS_NET */
1444 /* --------------------------------------------------------------------------------------------- */
1446 #ifdef ENABLE_VFS_UNDELFS
1447 void
1448 undelete_cmd (void)
1450 nice_cd (_("Undelete files on an ext2 file system"),
1451 _("Enter device (without /dev/) to undelete\nfiles on: (F1 for details)"),
1452 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "/#undel:", 0);
1454 #endif /* ENABLE_VFS_UNDELFS */
1456 /* --------------------------------------------------------------------------------------------- */
1458 void
1459 quick_cd_cmd (void)
1461 char *p = cd_dialog ();
1463 if (p && *p)
1465 char *q = g_strconcat ("cd ", p, (char *) NULL);
1467 do_cd_command (q);
1468 g_free (q);
1470 g_free (p);
1473 /* --------------------------------------------------------------------------------------------- */
1475 \brief calculate dirs sizes
1477 calculate dirs sizes and resort panel:
1478 dirs_selected = show size for selected dirs,
1479 otherwise = show size for dir under cursor:
1480 dir under cursor ".." = show size for all dirs,
1481 otherwise = show size for dir under cursor
1484 void
1485 smart_dirsize_cmd (void)
1487 WPanel *panel = current_panel;
1488 file_entry *entry;
1490 entry = &(panel->dir.list[panel->selected]);
1491 if ((S_ISDIR (entry->st.st_mode) && (strcmp (entry->fname, "..") == 0)) || panel->dirs_marked)
1492 dirsizes_cmd ();
1493 else
1494 single_dirsize_cmd ();
1497 /* --------------------------------------------------------------------------------------------- */
1499 void
1500 single_dirsize_cmd (void)
1502 WPanel *panel = current_panel;
1503 file_entry *entry;
1504 off_t marked;
1505 double total;
1506 ComputeDirSizeUI *ui;
1508 entry = &(panel->dir.list[panel->selected]);
1509 if (S_ISDIR (entry->st.st_mode) && strcmp (entry->fname, "..") != 0)
1511 ui = compute_dir_size_create_ui ();
1513 total = 0.0;
1515 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1516 &marked, &total, TRUE) == FILE_CONT)
1518 entry->st.st_size = (off_t) total;
1519 entry->f.dir_size_computed = 1;
1522 compute_dir_size_destroy_ui (ui);
1525 if (panels_options.mark_moves_down)
1526 send_message (&panel->widget, WIDGET_KEY, KEY_DOWN);
1528 recalculate_panel_summary (panel);
1530 if (current_panel->current_sort_field->sort_routine == (sortfn *) sort_size)
1531 panel_re_sort (panel);
1533 panel->dirty = 1;
1536 /* --------------------------------------------------------------------------------------------- */
1538 void
1539 dirsizes_cmd (void)
1541 WPanel *panel = current_panel;
1542 int i;
1543 off_t marked;
1544 double total;
1545 ComputeDirSizeUI *ui;
1547 ui = compute_dir_size_create_ui ();
1549 for (i = 0; i < panel->count; i++)
1550 if (S_ISDIR (panel->dir.list[i].st.st_mode)
1551 && ((panel->dirs_marked && panel->dir.list[i].f.marked)
1552 || !panel->dirs_marked) && strcmp (panel->dir.list[i].fname, "..") != 0)
1554 total = 0.0l;
1556 if (compute_dir_size (panel->dir.list[i].fname,
1557 ui, compute_dir_size_update_ui, &marked, &total,
1558 TRUE) != FILE_CONT)
1559 break;
1561 panel->dir.list[i].st.st_size = (off_t) total;
1562 panel->dir.list[i].f.dir_size_computed = 1;
1565 compute_dir_size_destroy_ui (ui);
1567 recalculate_panel_summary (panel);
1569 if (current_panel->current_sort_field->sort_routine == (sortfn *) sort_size)
1570 panel_re_sort (panel);
1572 panel->dirty = 1;
1575 /* --------------------------------------------------------------------------------------------- */
1577 void
1578 save_setup_cmd (void)
1580 if (!save_setup ())
1581 return;
1582 message (D_NORMAL, _("Setup"), _("Setup saved to ~/%s"),
1583 MC_USERCONF_DIR PATH_SEP_STR MC_CONFIG_FILE);
1586 /* --------------------------------------------------------------------------------------------- */
1588 void
1589 info_cmd_no_menu (void)
1591 if (get_display_type (0) == view_info)
1592 set_display_type (0, view_listing);
1593 else if (get_display_type (1) == view_info)
1594 set_display_type (1, view_listing);
1595 else
1596 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1599 /* --------------------------------------------------------------------------------------------- */
1601 void
1602 quick_cmd_no_menu (void)
1604 if (get_display_type (0) == view_quick)
1605 set_display_type (0, view_listing);
1606 else if (get_display_type (1) == view_quick)
1607 set_display_type (1, view_listing);
1608 else
1609 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1612 /* --------------------------------------------------------------------------------------------- */
1614 void
1615 listing_cmd (void)
1617 switch_to_listing (MENU_PANEL_IDX);
1620 /* --------------------------------------------------------------------------------------------- */
1622 void
1623 change_listing_cmd (void)
1625 int list_type;
1626 int use_msformat;
1627 char *user, *status;
1628 WPanel *p = NULL;
1630 if (get_display_type (MENU_PANEL_IDX) == view_listing)
1631 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1633 list_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1635 if (list_type != -1)
1637 switch_to_listing (MENU_PANEL_IDX);
1638 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1639 configure_panel_listing (p, list_type, use_msformat, user, status);
1643 /* --------------------------------------------------------------------------------------------- */
1645 void
1646 tree_cmd (void)
1648 set_display_type (MENU_PANEL_IDX, view_tree);
1651 /* --------------------------------------------------------------------------------------------- */
1653 void
1654 info_cmd (void)
1656 set_display_type (MENU_PANEL_IDX, view_info);
1659 /* --------------------------------------------------------------------------------------------- */
1661 void
1662 quick_view_cmd (void)
1664 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1665 change_panel ();
1666 set_display_type (MENU_PANEL_IDX, view_quick);
1669 /* --------------------------------------------------------------------------------------------- */
1671 void
1672 toggle_listing_cmd (void)
1674 int current = get_current_index ();
1675 WPanel *p = (WPanel *) get_panel_widget (current);
1677 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1680 /* --------------------------------------------------------------------------------------------- */
1682 void
1683 encoding_cmd (void)
1685 if (SELECTED_IS_PANEL)
1686 panel_change_encoding (MENU_PANEL);
1689 /* --------------------------------------------------------------------------------------------- */