Ticket #275: panelization fixes.
[midnight-commander.git] / src / filemanager / cmd.c
blobdf756f247681b193421f4361801fc35f5690bb86
1 /*
2 Routines invoked by a function key
3 They normally operate on the current panel.
5 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
6 2005, 2006, 2007, 2009, 2011
7 The Free Software Foundation, Inc.
9 This file is part of the Midnight Commander.
11 The Midnight Commander is free software: you can redistribute it
12 and/or modify it under the terms of the GNU General Public License as
13 published by the Free Software Foundation, either version 3 of the License,
14 or (at your option) any later version.
16 The Midnight Commander is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 /** \file cmd.c
26 * \brief Source: routines invoked by a function key
28 * They normally operate on the current panel.
31 #include <config.h>
33 #include <errno.h>
34 #include <stdio.h>
35 #include <string.h>
37 #include <sys/types.h>
38 #include <sys/stat.h>
39 #ifdef HAVE_MMAP
40 #include <sys/mman.h>
41 #endif
42 #ifdef ENABLE_VFS_NET
43 #include <netdb.h>
44 #endif
45 #include <unistd.h>
46 #include <stdlib.h>
47 #include <fcntl.h>
48 #include <pwd.h>
49 #include <grp.h>
50 #include <sys/time.h>
52 #include "lib/global.h"
54 #include "lib/tty/tty.h" /* LINES, tty_touch_screen() */
55 #include "lib/tty/key.h" /* ALT() macro */
56 #include "lib/tty/win.h" /* do_enter_ca_mode() */
57 #include "lib/mcconfig.h"
58 #include "lib/search.h"
59 #include "lib/filehighlight.h" /* MC_FHL_INI_FILE */
60 #include "lib/vfs/vfs.h"
61 #include "lib/fileloc.h"
62 #include "lib/strutil.h"
63 #include "lib/util.h"
64 #include "lib/widget.h"
65 #include "lib/keybind.h" /* CK_Down, CK_History */
66 #include "lib/event.h" /* mc_event_raise() */
68 #include "src/viewer/mcviewer.h"
69 #include "src/setup.h"
70 #include "src/execute.h" /* toggle_panels() */
71 #include "src/history.h"
72 #include "src/util.h" /* check_for_default() */
74 #ifdef USE_INTERNAL_EDIT
75 #include "src/editor/edit.h"
76 #endif
78 #ifdef USE_DIFF_VIEW
79 #include "src/diffviewer/ydiff.h"
80 #endif
82 #include "fileopctx.h"
83 #include "file.h" /* file operation routines */
84 #include "find.h" /* find_file() */
85 #include "hotlist.h" /* hotlist_show() */
86 #include "panel.h" /* WPanel */
87 #include "tree.h" /* tree_chdir() */
88 #include "midnight.h" /* change_panel() */
89 #include "usermenu.h" /* MC_GLOBAL_MENU */
90 #include "command.h" /* cmdline */
91 #include "layout.h" /* get_current_type() */
92 #include "ext.h" /* regex_command() */
93 #include "boxes.h" /* cd_dialog() */
94 #include "dir.h"
96 #include "cmd.h" /* Our definitions */
98 /*** global variables ****************************************************************************/
100 int select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
102 /*** file scope macro definitions ****************************************************************/
104 #ifndef MAP_FILE
105 #define MAP_FILE 0
106 #endif
108 /*** file scope type declarations ****************************************************************/
110 enum CompareMode
112 compare_quick, compare_size_only, compare_thourough
115 /*** file scope variables ************************************************************************/
117 #ifdef ENABLE_VFS_NET
118 static const char *machine_str = N_("Enter machine name (F1 for details):");
119 #endif /* ENABLE_VFS_NET */
121 /*** file scope functions ************************************************************************/
122 /* --------------------------------------------------------------------------------------------- */
124 /** scan_for_file (panel, idx, direction)
126 * Inputs:
127 * panel: pointer to the panel on which we operate
128 * idx: starting file.
129 * direction: 1, or -1
132 static int
133 scan_for_file (WPanel * panel, int idx, int direction)
135 int i = idx + direction;
137 while (i != idx)
139 if (i < 0)
140 i = panel->count - 1;
141 if (i == panel->count)
142 i = 0;
143 if (!S_ISDIR (panel->dir.list[i].st.st_mode))
144 return i;
145 i += direction;
147 return i;
150 /* --------------------------------------------------------------------------------------------- */
152 * Run viewer (internal or external) on the currently selected file.
153 * If normal is TRUE, force internal viewer and raw mode (used for F13).
155 static void
156 do_view_cmd (gboolean normal)
158 /* Directories are viewed by changing to them */
159 if (S_ISDIR (selection (current_panel)->st.st_mode) || link_isdir (selection (current_panel)))
161 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked))
163 if (query_dialog
164 (_("Confirmation"), _("Files tagged, want to cd?"), D_NORMAL, 2,
165 _("&Yes"), _("&No")) != 0)
167 return;
170 if (!do_cd (selection (current_panel)->fname, cd_exact))
171 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
173 else
175 int file_idx;
177 file_idx = current_panel->selected;
179 while (TRUE)
181 char *filename;
182 int dir;
184 filename = current_panel->dir.list[file_idx].fname;
185 dir = view_file (filename, normal, use_internal_view);
186 if (dir == 0)
187 break;
188 file_idx = scan_for_file (current_panel, file_idx, dir);
192 repaint_screen ();
195 /* --------------------------------------------------------------------------------------------- */
197 static inline void
198 do_edit (const char *what)
200 do_edit_at_line (what, use_internal_edit, 0);
203 /* --------------------------------------------------------------------------------------------- */
205 static void
206 set_panel_filter_to (WPanel * p, char *allocated_filter_string)
208 g_free (p->filter);
209 p->filter = 0;
211 if (!(allocated_filter_string[0] == '*' && allocated_filter_string[1] == 0))
212 p->filter = allocated_filter_string;
213 else
214 g_free (allocated_filter_string);
215 reread_cmd ();
218 /* --------------------------------------------------------------------------------------------- */
219 /** Set a given panel filter expression */
221 static void
222 set_panel_filter (WPanel * p)
224 char *reg_exp;
225 const char *x;
227 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
229 reg_exp = input_dialog_help (_("Filter"),
230 _("Set expression for filtering filenames"),
231 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
232 if (!reg_exp)
233 return;
234 set_panel_filter_to (p, reg_exp);
237 /* --------------------------------------------------------------------------------------------- */
239 static void
240 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
242 /* dialog sizes */
243 const int DX = 50;
244 const int DY = 7;
246 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
247 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
248 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
250 char *reg_exp;
251 mc_search_t *search;
252 int i;
254 QuickWidget quick_widgets[] = {
255 QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns),
256 QUICK_CHECKBOX (DX / 2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens),
257 QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only),
258 QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, &reg_exp),
259 QUICK_END
262 QuickDialog quick_dlg = {
263 DX, DY, -1, -1, title,
264 "[Select/Unselect Files]", quick_widgets, NULL, FALSE
267 if (quick_dialog (&quick_dlg) == B_CANCEL)
268 return;
270 if (!reg_exp)
271 return;
272 if (!*reg_exp)
274 g_free (reg_exp);
275 return;
277 search = mc_search_new (reg_exp, -1);
278 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
279 search->is_entire_line = TRUE;
280 search->is_case_sensitive = case_sens != 0;
282 for (i = 0; i < current_panel->count; i++)
284 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
285 continue;
286 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
287 continue;
289 if (mc_search_run (search, current_panel->dir.list[i].fname,
290 0, current_panel->dir.list[i].fnamelen, NULL))
291 do_file_mark (current_panel, i, do_select);
294 mc_search_free (search);
295 g_free (reg_exp);
297 /* result flags */
298 select_flags = 0;
299 if (case_sens != 0)
300 select_flags |= SELECT_MATCH_CASE;
301 if (files_only != 0)
302 select_flags |= SELECT_FILES_ONLY;
303 if (shell_patterns != 0)
304 select_flags |= SELECT_SHELL_PATTERNS;
307 /* --------------------------------------------------------------------------------------------- */
309 static int
310 compare_files (char *name1, char *name2, off_t size)
312 int file1, file2;
313 int result = -1; /* Different by default */
315 if (size == 0)
316 return 0;
318 file1 = open (name1, O_RDONLY);
319 if (file1 >= 0)
321 file2 = open (name2, O_RDONLY);
322 if (file2 >= 0)
324 #ifdef HAVE_MMAP
325 char *data1, *data2;
326 /* Ugly if jungle */
327 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
328 if (data1 != (char *) -1)
330 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
331 if (data2 != (char *) -1)
333 rotate_dash ();
334 result = memcmp (data1, data2, size);
335 munmap (data2, size);
337 munmap (data1, size);
339 #else
340 /* Don't have mmap() :( Even more ugly :) */
341 char buf1[BUFSIZ], buf2[BUFSIZ];
342 int n1, n2;
343 rotate_dash ();
346 while ((n1 = read (file1, buf1, BUFSIZ)) == -1 && errno == EINTR);
347 while ((n2 = read (file2, buf2, BUFSIZ)) == -1 && errno == EINTR);
349 while (n1 == n2 && n1 == BUFSIZ && !memcmp (buf1, buf2, BUFSIZ));
350 result = (n1 != n2) || memcmp (buf1, buf2, n1);
351 #endif /* !HAVE_MMAP */
352 close (file2);
354 close (file1);
356 return result;
359 /* --------------------------------------------------------------------------------------------- */
361 static void
362 compare_dir (WPanel * panel, WPanel * other, enum CompareMode mode)
364 int i, j;
365 char *src_name, *dst_name;
367 /* No marks by default */
368 panel->marked = 0;
369 panel->total = 0;
370 panel->dirs_marked = 0;
372 /* Handle all files in the panel */
373 for (i = 0; i < panel->count; i++)
375 file_entry *source = &panel->dir.list[i];
377 /* Default: unmarked */
378 file_mark (panel, i, 0);
380 /* Skip directories */
381 if (S_ISDIR (source->st.st_mode))
382 continue;
384 /* Search the corresponding entry from the other panel */
385 for (j = 0; j < other->count; j++)
387 if (strcmp (source->fname, other->dir.list[j].fname) == 0)
388 break;
390 if (j >= other->count)
391 /* Not found -> mark */
392 do_file_mark (panel, i, 1);
393 else
395 /* Found */
396 file_entry *target = &other->dir.list[j];
398 if (mode != compare_size_only)
400 /* Older version is not marked */
401 if (source->st.st_mtime < target->st.st_mtime)
402 continue;
405 /* Newer version with different size is marked */
406 if (source->st.st_size != target->st.st_size)
408 do_file_mark (panel, i, 1);
409 continue;
412 if (mode == compare_size_only)
413 continue;
415 if (mode == compare_quick)
417 /* Thorough compare off, compare only time stamps */
418 /* Mark newer version, don't mark version with the same date */
419 if (source->st.st_mtime > target->st.st_mtime)
421 do_file_mark (panel, i, 1);
423 continue;
426 /* Thorough compare on, do byte-by-byte comparison */
427 src_name = concat_dir_and_file (panel->cwd, source->fname);
428 dst_name = concat_dir_and_file (other->cwd, target->fname);
429 if (compare_files (src_name, dst_name, source->st.st_size))
430 do_file_mark (panel, i, 1);
431 g_free (src_name);
432 g_free (dst_name);
434 } /* for (i ...) */
437 /* --------------------------------------------------------------------------------------------- */
439 static void
440 do_link (link_type_t link_type, const char *fname)
442 char *dest = NULL, *src = NULL;
444 if (link_type == LINK_HARDLINK)
446 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
447 dest = input_expand_dialog (_("Link"), src, MC_HISTORY_FM_LINK, "");
448 if (!dest || !*dest)
449 goto cleanup;
450 save_cwds_stat ();
451 if (-1 == mc_link (fname, dest))
452 message (D_ERROR, MSG_ERROR, _("link: %s"), unix_error_string (errno));
454 else
456 char *s;
457 char *d;
459 /* suggest the full path for symlink, and either the full or
460 relative path to the file it points to */
461 s = concat_dir_and_file (current_panel->cwd, fname);
463 if (get_other_type () == view_listing)
464 d = concat_dir_and_file (other_panel->cwd, fname);
465 else
466 d = g_strdup (fname);
468 if (link_type == LINK_SYMLINK_RELATIVE)
469 s = diff_two_paths (other_panel->cwd, s);
471 symlink_dialog (s, d, &dest, &src);
472 g_free (d);
473 g_free (s);
475 if (!dest || !*dest || !src || !*src)
476 goto cleanup;
477 save_cwds_stat ();
478 if (-1 == mc_symlink (dest, src))
479 message (D_ERROR, MSG_ERROR, _("symlink: %s"), unix_error_string (errno));
482 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
483 repaint_screen ();
485 cleanup:
486 g_free (src);
487 g_free (dest);
490 /* --------------------------------------------------------------------------------------------- */
492 #if defined(ENABLE_VFS_UNDELFS) || defined(ENABLE_VFS_NET)
494 static const char *
495 transform_prefix (const char *prefix)
497 static char buffer[BUF_TINY];
498 size_t prefix_len = strlen (prefix);
500 if (prefix_len < 3)
501 return prefix;
503 strcpy (buffer, prefix + 2);
504 strcpy (buffer + prefix_len - 3, VFS_PATH_URL_DELIMITER);
505 return buffer;
508 /* --------------------------------------------------------------------------------------------- */
510 static void
511 nice_cd (const char *text, const char *xtext, const char *help,
512 const char *history_name, const char *prefix, int to_home)
514 char *machine;
515 char *cd_path;
517 if (!SELECTED_IS_PANEL)
518 return;
520 machine = input_dialog_help (text, xtext, help, history_name, "");
521 if (!machine)
522 return;
524 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
525 ugly as hell and leads to problems in vfs layer */
527 /* default prefix in old-style format. */
528 if (strncmp (prefix, machine, strlen (prefix)) != 0)
529 prefix = transform_prefix (prefix); /* Convert prefix to URL-style format */
531 if (strncmp (prefix, machine, strlen (prefix)) == 0)
532 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
533 else
534 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
536 if (*cd_path != PATH_SEP)
538 char *tmp = cd_path;
539 cd_path = g_strconcat (PATH_SEP_STR, tmp, (char *) NULL);
540 g_free (tmp);
543 if (!do_panel_cd (MENU_PANEL, cd_path, cd_parse_command))
544 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
545 g_free (cd_path);
546 g_free (machine);
548 #endif /* ENABLE_VFS_UNDELFS || ENABLE_VFS_NET */
550 /* --------------------------------------------------------------------------------------------- */
552 static void
553 configure_panel_listing (WPanel * p, int list_type, int use_msformat, char *user, char *status)
555 p->user_mini_status = use_msformat;
556 p->list_type = list_type;
558 if (list_type == list_user || use_msformat)
560 g_free (p->user_format);
561 p->user_format = user;
563 g_free (p->user_status_format[list_type]);
564 p->user_status_format[list_type] = status;
566 set_panel_formats (p);
568 else
570 g_free (user);
571 g_free (status);
574 set_panel_formats (p);
575 do_refresh ();
578 /* --------------------------------------------------------------------------------------------- */
580 static void
581 switch_to_listing (int panel_index)
583 if (get_display_type (panel_index) != view_listing)
584 set_display_type (panel_index, view_listing);
585 else
587 WPanel *p;
589 p = (WPanel *) get_panel_widget (panel_index);
590 if (p->is_panelized)
592 p->is_panelized = 0;
593 panel_reload (p);
598 /* --------------------------------------------------------------------------------------------- */
599 /** Handle the tree internal listing modes switching */
601 static gboolean
602 set_basic_panel_listing_to (int panel_index, int listing_mode)
604 WPanel *p = (WPanel *) get_panel_widget (panel_index);
605 gboolean ok;
607 switch_to_listing (panel_index);
608 p->list_type = listing_mode;
610 ok = set_panel_formats (p) == 0;
612 if (ok)
613 do_refresh ();
615 return ok;
618 /* --------------------------------------------------------------------------------------------- */
619 /*** public functions ****************************************************************************/
620 /* --------------------------------------------------------------------------------------------- */
623 view_file_at_line (const char *filename, int plain_view, int internal, int start_line)
625 static const char *viewer = NULL;
626 int move_dir = 0;
628 if (plain_view)
630 int changed_hex_mode = 0;
631 int changed_nroff_flag = 0;
632 int changed_magic_flag = 0;
634 mcview_altered_hex_mode = 0;
635 mcview_altered_nroff_flag = 0;
636 mcview_altered_magic_flag = 0;
637 if (mcview_default_hex_mode)
638 changed_hex_mode = 1;
639 if (mcview_default_nroff_flag)
640 changed_nroff_flag = 1;
641 if (mcview_default_magic_flag)
642 changed_magic_flag = 1;
643 mcview_default_hex_mode = 0;
644 mcview_default_nroff_flag = 0;
645 mcview_default_magic_flag = 0;
647 switch (mcview_viewer (NULL, filename, start_line))
649 case MCVIEW_WANT_NEXT:
650 move_dir = 1;
651 break;
652 case MCVIEW_WANT_PREV:
653 move_dir = -1;
654 break;
655 default:
656 move_dir = 0;
659 if (changed_hex_mode && !mcview_altered_hex_mode)
660 mcview_default_hex_mode = 1;
661 if (changed_nroff_flag && !mcview_altered_nroff_flag)
662 mcview_default_nroff_flag = 1;
663 if (changed_magic_flag && !mcview_altered_magic_flag)
664 mcview_default_magic_flag = 1;
666 dialog_switch_process_pending ();
668 else if (internal)
670 char view_entry[BUF_TINY];
672 if (start_line != 0)
673 g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line);
674 else
675 strcpy (view_entry, "View");
677 if (regex_command (filename, view_entry, &move_dir) == 0)
679 switch (mcview_viewer (NULL, filename, start_line))
681 case MCVIEW_WANT_NEXT:
682 move_dir = 1;
683 break;
684 case MCVIEW_WANT_PREV:
685 move_dir = -1;
686 break;
687 default:
688 move_dir = 0;
691 dialog_switch_process_pending ();
694 else
696 if (viewer == NULL)
698 viewer = getenv ("VIEWER");
699 if (viewer == NULL)
700 viewer = getenv ("PAGER");
701 if (viewer == NULL)
702 viewer = "view";
705 execute_with_vfs_arg (viewer, filename);
708 return move_dir;
711 /* --------------------------------------------------------------------------------------------- */
712 /** view_file (filename, plain_view, internal)
714 * Inputs:
715 * filename: The file name to view
716 * plain_view: If set does not do any fancy pre-processing (no filtering) and
717 * always invokes the internal viewer.
718 * internal: If set uses the internal viewer, otherwise an external viewer.
722 view_file (const char *filename, int plain_view, int internal)
724 return view_file_at_line (filename, plain_view, internal, 0);
728 /* --------------------------------------------------------------------------------------------- */
729 /** Run user's preferred viewer on the currently selected file */
731 void
732 view_cmd (void)
734 do_view_cmd (FALSE);
737 /* --------------------------------------------------------------------------------------------- */
738 /** Ask for file and run user's preferred viewer on it */
740 void
741 view_file_cmd (void)
743 char *filename;
745 filename =
746 input_expand_dialog (_("View file"), _("Filename:"),
747 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
748 if (!filename)
749 return;
751 view_file (filename, 0, use_internal_view);
752 g_free (filename);
755 /* --------------------------------------------------------------------------------------------- */
756 /** Run plain internal viewer on the currently selected file */
757 void
758 view_raw_cmd (void)
760 do_view_cmd (TRUE);
763 /* --------------------------------------------------------------------------------------------- */
765 void
766 view_filtered_cmd (void)
768 char *command;
769 const char *initial_command;
771 if (cmdline->buffer[0] == '\0')
772 initial_command = selection (current_panel)->fname;
773 else
774 initial_command = cmdline->buffer;
776 command =
777 input_dialog (_("Filtered view"),
778 _("Filter command and arguments:"),
779 MC_HISTORY_FM_FILTERED_VIEW, initial_command);
781 if (command != NULL)
783 mcview_viewer (command, "", 0);
784 g_free (command);
785 dialog_switch_process_pending ();
789 /* --------------------------------------------------------------------------------------------- */
791 void
792 do_edit_at_line (const char *what, gboolean internal, int start_line)
794 static const char *editor = NULL;
796 #ifdef USE_INTERNAL_EDIT
797 if (internal)
798 edit_file (what, start_line);
799 else
800 #else
801 (void) start_line;
802 #endif /* USE_INTERNAL_EDIT */
804 if (editor == NULL)
806 editor = getenv ("EDITOR");
807 if (editor == NULL)
808 editor = get_default_editor ();
810 execute_with_vfs_arg (editor, what);
813 if (mc_global.mc_run_mode == MC_RUN_FULL)
814 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
816 #ifdef USE_INTERNAL_EDIT
817 if (use_internal_edit)
818 dialog_switch_process_pending ();
819 else
820 #endif /* USE_INTERNAL_EDIT */
821 repaint_screen ();
824 /* --------------------------------------------------------------------------------------------- */
826 void
827 edit_cmd (void)
829 if (regex_command (selection (current_panel)->fname, "Edit", NULL) == 0)
830 do_edit (selection (current_panel)->fname);
833 /* --------------------------------------------------------------------------------------------- */
835 #ifdef USE_INTERNAL_EDIT
836 void
837 edit_cmd_force_internal (void)
839 if (regex_command (selection (current_panel)->fname, "Edit", NULL) == 0)
840 do_edit_at_line (selection (current_panel)->fname, TRUE, 0);
842 #endif
844 /* --------------------------------------------------------------------------------------------- */
846 void
847 edit_cmd_new (void)
849 #if HAVE_CHARSET
850 mc_global.source_codepage = default_source_codepage;
851 #endif
852 do_edit (NULL);
855 /* --------------------------------------------------------------------------------------------- */
856 /** Invoked by F5. Copy, default to the other panel. */
858 void
859 copy_cmd (void)
861 save_cwds_stat ();
862 if (panel_operate (current_panel, OP_COPY, FALSE))
864 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
865 repaint_screen ();
869 /* --------------------------------------------------------------------------------------------- */
870 /** Invoked by F6. Move/rename, default to the other panel, ignore marks. */
872 void
873 rename_cmd (void)
875 save_cwds_stat ();
876 if (panel_operate (current_panel, OP_MOVE, FALSE))
878 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
879 repaint_screen ();
883 /* --------------------------------------------------------------------------------------------- */
884 /** Invoked by F15. Copy, default to the same panel, ignore marks. */
886 void
887 copy_cmd_local (void)
889 save_cwds_stat ();
890 if (panel_operate (current_panel, OP_COPY, TRUE))
892 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
893 repaint_screen ();
897 /* --------------------------------------------------------------------------------------------- */
898 /** Invoked by F16. Move/rename, default to the same panel. */
900 void
901 rename_cmd_local (void)
903 save_cwds_stat ();
904 if (panel_operate (current_panel, OP_MOVE, TRUE))
906 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
907 repaint_screen ();
911 /* --------------------------------------------------------------------------------------------- */
913 void
914 mkdir_cmd (void)
916 char *dir, *absdir;
917 const char *name = "";
919 /* If 'on' then automatically fills name with current selected item name */
920 if (auto_fill_mkdir_name && strcmp (selection (current_panel)->fname, "..") != 0)
921 name = selection (current_panel)->fname;
923 dir =
924 input_expand_dialog (_("Create a new Directory"),
925 _("Enter directory name:"), MC_HISTORY_FM_MKDIR, name);
927 if (!dir)
928 return;
930 if (*dir)
932 if (dir[0] == '/' || dir[0] == '~')
933 absdir = g_strdup (dir);
934 else
935 absdir = concat_dir_and_file (current_panel->cwd, dir);
937 save_cwds_stat ();
938 if (my_mkdir (absdir, 0777) == 0)
940 update_panels (UP_OPTIMIZE, dir);
941 repaint_screen ();
942 select_item (current_panel);
944 else
946 message (D_ERROR, MSG_ERROR, "%s", unix_error_string (errno));
948 g_free (absdir);
950 g_free (dir);
953 /* --------------------------------------------------------------------------------------------- */
955 void
956 delete_cmd (void)
958 save_cwds_stat ();
960 if (panel_operate (current_panel, OP_DELETE, FALSE))
962 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
963 repaint_screen ();
967 /* --------------------------------------------------------------------------------------------- */
968 /** Invoked by F18. Remove selected file, regardless of marked files. */
970 void
971 delete_cmd_local (void)
973 save_cwds_stat ();
975 if (panel_operate (current_panel, OP_DELETE, TRUE))
977 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
978 repaint_screen ();
982 /* --------------------------------------------------------------------------------------------- */
984 void
985 find_cmd (void)
987 find_file ();
990 /* --------------------------------------------------------------------------------------------- */
991 /** Invoked from the left/right menus */
993 void
994 filter_cmd (void)
996 WPanel *p;
998 if (!SELECTED_IS_PANEL)
999 return;
1001 p = MENU_PANEL;
1002 set_panel_filter (p);
1005 /* --------------------------------------------------------------------------------------------- */
1007 void
1008 reread_cmd (void)
1010 panel_update_flags_t flag = UP_ONLY_CURRENT;
1012 if (get_current_type () == view_listing && get_other_type () == view_listing
1013 && strcmp (current_panel->cwd, other_panel->cwd) == 0)
1014 flag = UP_OPTIMIZE;
1016 update_panels (UP_RELOAD | flag, UP_KEEPSEL);
1017 repaint_screen ();
1020 /* --------------------------------------------------------------------------------------------- */
1022 void
1023 select_invert_cmd (void)
1025 int i;
1026 file_entry *file;
1028 for (i = 0; i < current_panel->count; i++)
1030 file = &current_panel->dir.list[i];
1031 if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
1032 do_file_mark (current_panel, i, !file->f.marked);
1036 /* --------------------------------------------------------------------------------------------- */
1038 void
1039 select_cmd (void)
1041 select_unselect_cmd (_("Select"), ":select_cmd: Select ", TRUE);
1044 /* --------------------------------------------------------------------------------------------- */
1046 void
1047 unselect_cmd (void)
1049 select_unselect_cmd (_("Unselect"), ":unselect_cmd: Unselect ", FALSE);
1052 /* --------------------------------------------------------------------------------------------- */
1054 void
1055 ext_cmd (void)
1057 char *buffer;
1058 char *extdir;
1059 int dir;
1061 dir = 0;
1062 if (geteuid () == 0)
1064 dir = query_dialog (_("Extension file edit"),
1065 _("Which extension file you want to edit?"), D_NORMAL, 2,
1066 _("&User"), _("&System Wide"));
1068 extdir = concat_dir_and_file (mc_global.sysconfig_dir, MC_LIB_EXT);
1070 if (dir == 0)
1072 buffer = g_build_filename (mc_config_get_data_path (), MC_FILEBIND_FILE, NULL);
1073 check_for_default (extdir, buffer);
1074 do_edit (buffer);
1075 g_free (buffer);
1077 else if (dir == 1)
1079 if (!exist_file (extdir))
1081 g_free (extdir);
1082 extdir = concat_dir_and_file (mc_global.share_data_dir, MC_LIB_EXT);
1084 do_edit (extdir);
1086 g_free (extdir);
1087 flush_extension_file ();
1090 /* --------------------------------------------------------------------------------------------- */
1091 /** edit file menu for mc */
1093 void
1094 edit_mc_menu_cmd (void)
1096 char *buffer;
1097 char *menufile;
1098 int dir = 0;
1100 dir = query_dialog (_("Menu edit"),
1101 _("Which menu file do you want to edit?"),
1102 D_NORMAL, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
1104 menufile = concat_dir_and_file (mc_global.sysconfig_dir, MC_GLOBAL_MENU);
1106 if (!exist_file (menufile))
1108 g_free (menufile);
1109 menufile = concat_dir_and_file (mc_global.share_data_dir, MC_GLOBAL_MENU);
1112 switch (dir)
1114 case 0:
1115 buffer = g_strdup (MC_LOCAL_MENU);
1116 check_for_default (menufile, buffer);
1117 chmod (buffer, 0600);
1118 break;
1120 case 1:
1121 buffer = g_build_filename (mc_config_get_data_path (), MC_USERMENU_FILE, NULL);
1122 check_for_default (menufile, buffer);
1123 break;
1125 case 2:
1126 buffer = concat_dir_and_file (mc_global.sysconfig_dir, MC_GLOBAL_MENU);
1127 if (!exist_file (buffer))
1129 g_free (buffer);
1130 buffer = concat_dir_and_file (mc_global.share_data_dir, MC_GLOBAL_MENU);
1132 break;
1134 default:
1135 g_free (menufile);
1136 return;
1139 do_edit (buffer);
1141 g_free (buffer);
1142 g_free (menufile);
1145 /* --------------------------------------------------------------------------------------------- */
1147 void
1148 edit_fhl_cmd (void)
1150 char *buffer = NULL;
1151 char *fhlfile = NULL;
1153 int dir;
1155 dir = 0;
1156 if (geteuid () == 0)
1158 dir = query_dialog (_("Highlighting groups file edit"),
1159 _("Which highlighting file you want to edit?"), D_NORMAL, 2,
1160 _("&User"), _("&System Wide"));
1162 fhlfile = concat_dir_and_file (mc_global.sysconfig_dir, MC_FHL_INI_FILE);
1164 if (dir == 0)
1166 buffer = g_build_filename (mc_config_get_path (), MC_FHL_INI_FILE, NULL);
1167 check_for_default (fhlfile, buffer);
1168 do_edit (buffer);
1169 g_free (buffer);
1171 else if (dir == 1)
1173 if (!exist_file (fhlfile))
1175 g_free (fhlfile);
1176 fhlfile = concat_dir_and_file (mc_global.sysconfig_dir, MC_FHL_INI_FILE);
1178 do_edit (fhlfile);
1180 g_free (fhlfile);
1182 /* refresh highlighting rules */
1183 mc_fhl_free (&mc_filehighlight);
1184 mc_filehighlight = mc_fhl_new (TRUE);
1187 /* --------------------------------------------------------------------------------------------- */
1189 void
1190 hotlist_cmd (void)
1192 char *target;
1194 target = hotlist_show (LIST_HOTLIST);
1195 if (!target)
1196 return;
1198 if (get_current_type () == view_tree)
1199 tree_chdir (the_tree, target);
1200 else
1202 char *cmd = g_strconcat ("cd ", target, (char *) NULL);
1203 do_cd_command (cmd);
1204 g_free (cmd);
1206 g_free (target);
1209 #ifdef ENABLE_VFS
1210 void
1211 vfs_list (void)
1213 char *target;
1215 target = hotlist_show (LIST_VFSLIST);
1216 if (!target)
1217 return;
1219 if (!do_cd (target, cd_exact))
1220 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
1221 g_free (target);
1223 #endif /* ENABLE_VFS */
1225 /* --------------------------------------------------------------------------------------------- */
1227 void
1228 compare_dirs_cmd (void)
1230 int choice;
1231 enum CompareMode thorough_flag;
1233 choice =
1234 query_dialog (_("Compare directories"),
1235 _("Select compare method:"), D_NORMAL, 4,
1236 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
1238 if (choice < 0 || choice > 2)
1239 return;
1241 thorough_flag = choice;
1243 if (get_current_type () == view_listing && get_other_type () == view_listing)
1245 compare_dir (current_panel, other_panel, thorough_flag);
1246 compare_dir (other_panel, current_panel, thorough_flag);
1248 else
1250 message (D_ERROR, MSG_ERROR,
1251 _("Both panels should be in the listing mode\nto use this command"));
1255 /* --------------------------------------------------------------------------------------------- */
1257 #ifdef USE_DIFF_VIEW
1258 void
1259 diff_view_cmd (void)
1261 dview_diff_cmd ();
1263 if (mc_global.mc_run_mode == MC_RUN_FULL)
1264 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1266 dialog_switch_process_pending ();
1268 #endif
1270 /* --------------------------------------------------------------------------------------------- */
1272 void
1273 swap_cmd (void)
1275 swap_panels ();
1276 tty_touch_screen ();
1277 repaint_screen ();
1280 /* --------------------------------------------------------------------------------------------- */
1282 void
1283 view_other_cmd (void)
1285 static int message_flag = TRUE;
1287 if (!mc_global.tty.xterm_flag && mc_global.tty.console_flag == '\0'
1288 && !mc_global.tty.use_subshell && !output_starts_shell)
1290 if (message_flag)
1291 message (D_ERROR, MSG_ERROR,
1292 _("Not an xterm or Linux console;\nthe panels cannot be toggled."));
1293 message_flag = FALSE;
1295 else
1297 toggle_panels ();
1301 /* --------------------------------------------------------------------------------------------- */
1303 void
1304 link_cmd (link_type_t link_type)
1306 char *filename = selection (current_panel)->fname;
1308 if (filename != NULL)
1309 do_link (link_type, filename);
1312 /* --------------------------------------------------------------------------------------------- */
1314 void
1315 edit_symlink_cmd (void)
1317 if (S_ISLNK (selection (current_panel)->st.st_mode))
1319 char buffer[MC_MAXPATHLEN];
1320 char *p = NULL;
1321 int i;
1322 char *dest, *q;
1324 p = selection (current_panel)->fname;
1326 q = g_strdup_printf (_("Symlink `%s\' points to:"), str_trunc (p, 32));
1328 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
1329 if (i > 0)
1331 buffer[i] = 0;
1332 dest = input_expand_dialog (_("Edit symlink"), q, MC_HISTORY_FM_EDIT_LINK, buffer);
1333 if (dest)
1335 if (*dest && strcmp (buffer, dest))
1337 save_cwds_stat ();
1338 if (-1 == mc_unlink (p))
1340 message (D_ERROR, MSG_ERROR, _("edit symlink, unable to remove %s: %s"),
1341 p, unix_error_string (errno));
1343 else
1345 if (-1 == mc_symlink (dest, p))
1346 message (D_ERROR, MSG_ERROR, _("edit symlink: %s"),
1347 unix_error_string (errno));
1349 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1350 repaint_screen ();
1352 g_free (dest);
1355 g_free (q);
1357 else
1359 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1360 selection (current_panel)->fname);
1364 /* --------------------------------------------------------------------------------------------- */
1366 void
1367 help_cmd (void)
1369 ev_help_t event_data = { NULL, NULL };
1371 if (current_panel->searching)
1372 event_data.node = "[Quick search]";
1373 else
1374 event_data.node = "[main]";
1376 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1379 /* --------------------------------------------------------------------------------------------- */
1381 void
1382 user_file_menu_cmd (void)
1384 (void) user_menu_cmd (NULL, NULL, -1);
1387 /* --------------------------------------------------------------------------------------------- */
1389 * Return a random hint. If force is not 0, ignore the timeout.
1392 char *
1393 get_random_hint (int force)
1395 char *data, *result = NULL, *eol;
1396 int len;
1397 int start;
1398 static int last_sec;
1399 static struct timeval tv;
1400 GIConv conv;
1401 GString *buffer;
1403 /* Do not change hints more often than one minute */
1404 gettimeofday (&tv, NULL);
1405 if (!force && !(tv.tv_sec > last_sec + 60))
1406 return g_strdup ("");
1407 last_sec = tv.tv_sec;
1409 data = load_mc_home_file (mc_global.share_data_dir, MC_HINT, NULL);
1410 if (data == NULL)
1411 return NULL;
1413 /* get a random entry */
1414 srand (tv.tv_sec);
1415 len = strlen (data);
1416 start = rand () % len;
1418 for (; start != 0; start--)
1419 if (data[start] == '\n')
1421 start++;
1422 break;
1425 eol = strchr (data + start, '\n');
1426 if (eol != NULL)
1427 *eol = '\0';
1429 /* hint files are stored in utf-8 */
1430 /* try convert hint file from utf-8 to terminal encoding */
1431 conv = str_crt_conv_from ("UTF-8");
1432 if (conv != INVALID_CONV)
1434 buffer = g_string_new ("");
1435 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1436 result = g_strdup (buffer->str);
1437 g_string_free (buffer, TRUE);
1438 str_close_conv (conv);
1441 g_free (data);
1442 return result;
1445 /* --------------------------------------------------------------------------------------------- */
1447 #ifdef ENABLE_VFS_NET
1448 #ifdef ENABLE_VFS_FTP
1449 void
1450 ftplink_cmd (void)
1452 nice_cd (_("FTP to machine"), _(machine_str),
1453 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1455 #endif /* ENABLE_VFS_FTP */
1457 /* --------------------------------------------------------------------------------------------- */
1459 #ifdef ENABLE_VFS_FISH
1460 void
1461 fishlink_cmd (void)
1463 nice_cd (_("Shell link to machine"), _(machine_str),
1464 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1465 "/#sh:", 1);
1467 #endif /* ENABLE_VFS_FISH */
1469 /* --------------------------------------------------------------------------------------------- */
1471 #ifdef ENABLE_VFS_SMB
1472 void
1473 smblink_cmd (void)
1475 nice_cd (_("SMB link to machine"), _(machine_str),
1476 "[SMB File System]", ":smblink_cmd: SMB link to machine ", "/#smb:", 0);
1478 #endif /* ENABLE_VFS_SMB */
1479 #endif /* ENABLE_VFS_NET */
1481 /* --------------------------------------------------------------------------------------------- */
1483 #ifdef ENABLE_VFS_UNDELFS
1484 void
1485 undelete_cmd (void)
1487 nice_cd (_("Undelete files on an ext2 file system"),
1488 _("Enter device (without /dev/) to undelete\nfiles on: (F1 for details)"),
1489 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "/#undel:", 0);
1491 #endif /* ENABLE_VFS_UNDELFS */
1493 /* --------------------------------------------------------------------------------------------- */
1495 void
1496 quick_cd_cmd (void)
1498 char *p = cd_dialog ();
1500 if (p && *p)
1502 char *q = g_strconcat ("cd ", p, (char *) NULL);
1504 do_cd_command (q);
1505 g_free (q);
1507 g_free (p);
1510 /* --------------------------------------------------------------------------------------------- */
1512 \brief calculate dirs sizes
1514 calculate dirs sizes and resort panel:
1515 dirs_selected = show size for selected dirs,
1516 otherwise = show size for dir under cursor:
1517 dir under cursor ".." = show size for all dirs,
1518 otherwise = show size for dir under cursor
1521 void
1522 smart_dirsize_cmd (void)
1524 WPanel *panel = current_panel;
1525 file_entry *entry;
1527 entry = &(panel->dir.list[panel->selected]);
1528 if ((S_ISDIR (entry->st.st_mode) && (strcmp (entry->fname, "..") == 0)) || panel->dirs_marked)
1529 dirsizes_cmd ();
1530 else
1531 single_dirsize_cmd ();
1534 /* --------------------------------------------------------------------------------------------- */
1536 void
1537 single_dirsize_cmd (void)
1539 WPanel *panel = current_panel;
1540 file_entry *entry;
1542 entry = &(panel->dir.list[panel->selected]);
1543 if (S_ISDIR (entry->st.st_mode) && strcmp (entry->fname, "..") != 0)
1545 size_t marked = 0;
1546 uintmax_t total = 0;
1547 ComputeDirSizeUI *ui;
1549 ui = compute_dir_size_create_ui ();
1551 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1552 &marked, &total, TRUE) == FILE_CONT)
1554 entry->st.st_size = (off_t) total;
1555 entry->f.dir_size_computed = 1;
1558 compute_dir_size_destroy_ui (ui);
1561 if (panels_options.mark_moves_down)
1562 send_message ((Widget *) panel, WIDGET_COMMAND, CK_Down);
1564 recalculate_panel_summary (panel);
1566 if (current_panel->sort_info.sort_field->sort_routine == (sortfn *) sort_size)
1567 panel_re_sort (panel);
1569 panel->dirty = 1;
1572 /* --------------------------------------------------------------------------------------------- */
1574 void
1575 dirsizes_cmd (void)
1577 WPanel *panel = current_panel;
1578 int i;
1579 ComputeDirSizeUI *ui;
1581 ui = compute_dir_size_create_ui ();
1583 for (i = 0; i < panel->count; i++)
1584 if (S_ISDIR (panel->dir.list[i].st.st_mode)
1585 && ((panel->dirs_marked && panel->dir.list[i].f.marked)
1586 || !panel->dirs_marked) && strcmp (panel->dir.list[i].fname, "..") != 0)
1588 size_t marked = 0;
1589 uintmax_t total = 0;
1591 if (compute_dir_size (panel->dir.list[i].fname,
1592 ui, compute_dir_size_update_ui, &marked, &total,
1593 TRUE) != FILE_CONT)
1594 break;
1596 panel->dir.list[i].st.st_size = (off_t) total;
1597 panel->dir.list[i].f.dir_size_computed = 1;
1600 compute_dir_size_destroy_ui (ui);
1602 recalculate_panel_summary (panel);
1604 if (current_panel->sort_info.sort_field->sort_routine == (sortfn *) sort_size)
1605 panel_re_sort (panel);
1607 panel->dirty = 1;
1610 /* --------------------------------------------------------------------------------------------- */
1612 void
1613 save_setup_cmd (void)
1615 char *d1;
1616 const char *d2;
1618 d1 = g_build_filename (mc_config_get_path (), MC_CONFIG_FILE, (char *) NULL);
1619 d2 = strip_home_and_password (d1);
1620 g_free (d1);
1622 if (save_setup (TRUE, TRUE))
1623 message (D_NORMAL, _("Setup"), _("Setup saved to %s"), d2);
1624 else
1625 message (D_ERROR, _("Setup"), _("Unable to save setup to %s"), d2);
1628 /* --------------------------------------------------------------------------------------------- */
1630 void
1631 info_cmd_no_menu (void)
1633 if (get_display_type (0) == view_info)
1634 set_display_type (0, view_listing);
1635 else if (get_display_type (1) == view_info)
1636 set_display_type (1, view_listing);
1637 else
1638 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1641 /* --------------------------------------------------------------------------------------------- */
1643 void
1644 quick_cmd_no_menu (void)
1646 if (get_display_type (0) == view_quick)
1647 set_display_type (0, view_listing);
1648 else if (get_display_type (1) == view_quick)
1649 set_display_type (1, view_listing);
1650 else
1651 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1654 /* --------------------------------------------------------------------------------------------- */
1656 void
1657 listing_cmd (void)
1659 switch_to_listing (MENU_PANEL_IDX);
1662 /* --------------------------------------------------------------------------------------------- */
1664 void
1665 change_listing_cmd (void)
1667 int list_type;
1668 int use_msformat;
1669 char *user, *status;
1670 WPanel *p = NULL;
1672 if (get_display_type (MENU_PANEL_IDX) == view_listing)
1673 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1675 list_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1677 if (list_type != -1)
1679 switch_to_listing (MENU_PANEL_IDX);
1680 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1681 configure_panel_listing (p, list_type, use_msformat, user, status);
1685 /* --------------------------------------------------------------------------------------------- */
1687 void
1688 panel_tree_cmd (void)
1690 set_display_type (MENU_PANEL_IDX, view_tree);
1693 /* --------------------------------------------------------------------------------------------- */
1695 void
1696 info_cmd (void)
1698 set_display_type (MENU_PANEL_IDX, view_info);
1701 /* --------------------------------------------------------------------------------------------- */
1703 void
1704 quick_view_cmd (void)
1706 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1707 change_panel ();
1708 set_display_type (MENU_PANEL_IDX, view_quick);
1711 /* --------------------------------------------------------------------------------------------- */
1713 void
1714 toggle_listing_cmd (void)
1716 int current = get_current_index ();
1717 WPanel *p = (WPanel *) get_panel_widget (current);
1719 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1722 /* --------------------------------------------------------------------------------------------- */
1724 void
1725 encoding_cmd (void)
1727 if (SELECTED_IS_PANEL)
1728 panel_change_encoding (MENU_PANEL);
1731 /* --------------------------------------------------------------------------------------------- */