(user_file_menu_cmd): move to src/usermenu.h and make inline.
[midnight-commander.git] / src / filemanager / cmd.c
blob73cd904a7c5bd416d616fc2046974a64e5d8c8c5
1 /*
2 Routines invoked by a function key
3 They normally operate on the current panel.
5 Copyright (C) 1994-2024
6 Free Software Foundation, Inc.
8 Written by:
9 Andrew Borodin <aborodin@vmail.ru>, 2013-2022
11 This file is part of the Midnight Commander.
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 /** \file cmd.c
28 * \brief Source: routines invoked by a function key
30 * They normally operate on the current panel.
33 #include <config.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <string.h>
39 #include <sys/types.h>
40 #include <sys/stat.h>
41 #ifdef HAVE_MMAP
42 #include <sys/mman.h>
43 #endif
44 #ifdef ENABLE_VFS_NET
45 #include <netdb.h>
46 #endif
47 #include <unistd.h>
48 #include <stdlib.h>
49 #include <pwd.h>
50 #include <grp.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/mcconfig.h"
57 #include "lib/filehighlight.h" /* MC_FHL_INI_FILE */
58 #include "lib/vfs/vfs.h"
59 #include "lib/fileloc.h"
60 #include "lib/strutil.h"
61 #include "lib/file-entry.h"
62 #include "lib/util.h"
63 #include "lib/widget.h"
64 #include "lib/keybind.h" /* CK_Down, CK_History */
65 #include "lib/event.h" /* mc_event_raise() */
67 #include "src/setup.h"
68 #include "src/execute.h" /* toggle_panels() */
69 #include "src/history.h"
70 #include "src/util.h" /* check_for_default() */
72 #include "src/viewer/mcviewer.h"
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 "filenot.h"
84 #include "hotlist.h" /* hotlist_show() */
85 #include "tree.h" /* tree_chdir() */
86 #include "filemanager.h" /* change_panel() */
87 #include "command.h" /* cmdline */
88 #include "layout.h" /* get_current_type() */
89 #include "ext.h" /* regex_command() */
90 #include "boxes.h" /* cd_box() */
91 #include "dir.h"
92 #include "cd.h"
93 #include "ioblksize.h" /* IO_BUFSIZE */
95 #include "cmd.h" /* Our definitions */
97 /*** global variables ****************************************************************************/
99 /*** file scope macro definitions ****************************************************************/
101 #ifdef HAVE_MMAP
102 #ifndef MAP_FILE
103 #define MAP_FILE 0
104 #endif
105 #endif /* HAVE_MMAP */
107 /*** file scope type declarations ****************************************************************/
109 enum CompareMode
111 compare_quick = 0,
112 compare_size_only,
113 compare_thourough
116 /*** forward declarations (file scope functions) *************************************************/
118 /*** file scope variables ************************************************************************/
120 #ifdef ENABLE_VFS_NET
121 static const char *machine_str = N_("Enter machine name (F1 for details):");
122 #endif /* ENABLE_VFS_NET */
124 /* --------------------------------------------------------------------------------------------- */
125 /*** file scope functions ************************************************************************/
126 /* --------------------------------------------------------------------------------------------- */
128 * Run viewer (internal or external) on the current file.
129 * If @plain_view is TRUE, force internal viewer and raw mode (used for F13).
131 static void
132 do_view_cmd (WPanel *panel, gboolean plain_view)
134 const file_entry_t *fe;
136 fe = panel_current_entry (panel);
138 /* Directories are viewed by changing to them */
139 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe))
141 vfs_path_t *fname_vpath;
143 if (confirm_view_dir && (panel->marked != 0 || panel->dirs_marked != 0) &&
144 query_dialog (_("Confirmation"), _("Files tagged, want to cd?"), D_NORMAL, 2,
145 _("&Yes"), _("&No")) != 0)
146 return;
148 fname_vpath = vfs_path_from_str (fe->fname->str);
149 if (!panel_cd (panel, fname_vpath, cd_exact))
150 cd_error_message (fe->fname->str);
151 vfs_path_free (fname_vpath, TRUE);
153 else
155 vfs_path_t *filename_vpath;
157 filename_vpath = vfs_path_from_str (fe->fname->str);
158 view_file (filename_vpath, plain_view, use_internal_view);
159 vfs_path_free (filename_vpath, TRUE);
162 repaint_screen ();
165 /* --------------------------------------------------------------------------------------------- */
167 static inline void
168 do_edit (const vfs_path_t *what_vpath)
170 edit_file_at_line (what_vpath, use_internal_edit, 0);
173 /* --------------------------------------------------------------------------------------------- */
175 static int
176 compare_files (const vfs_path_t *vpath1, const vfs_path_t *vpath2, off_t size)
178 int file1;
179 int result = -1; /* Different by default */
181 if (size == 0)
182 return 0;
184 file1 = open (vfs_path_as_str (vpath1), O_RDONLY);
185 if (file1 >= 0)
187 int file2;
189 file2 = open (vfs_path_as_str (vpath2), O_RDONLY);
190 if (file2 >= 0)
192 char buf1[IO_BUFSIZE], buf2[IO_BUFSIZE];
193 ssize_t n1, n2;
195 rotate_dash (TRUE);
198 while ((n1 = read (file1, buf1, sizeof (buf1))) == -1 && errno == EINTR)
200 while ((n2 = read (file2, buf2, sizeof (buf2))) == -1 && errno == EINTR)
203 while (n1 == n2 && n1 == sizeof (buf1) && memcmp (buf1, buf2, sizeof (buf1)) == 0);
204 result = (n1 != n2) || memcmp (buf1, buf2, n1);
205 rotate_dash (FALSE);
207 close (file2);
209 close (file1);
212 return result;
215 /* --------------------------------------------------------------------------------------------- */
217 static void
218 compare_dir (WPanel *panel, const WPanel *other, enum CompareMode mode)
220 int i, j;
222 /* No marks by default */
223 panel->marked = 0;
224 panel->total = 0;
225 panel->dirs_marked = 0;
227 /* Handle all files in the panel */
228 for (i = 0; i < panel->dir.len; i++)
230 file_entry_t *source = &panel->dir.list[i];
231 const char *source_fname;
233 /* Default: unmarked */
234 file_mark (panel, i, 0);
236 /* Skip directories */
237 if (S_ISDIR (source->st.st_mode))
238 continue;
240 source_fname = source->fname->str;
241 if (panel->is_panelized)
242 source_fname = x_basename (source_fname);
244 /* Search the corresponding entry from the other panel */
245 for (j = 0; j < other->dir.len; j++)
247 const char *other_fname;
249 other_fname = other->dir.list[j].fname->str;
250 if (other->is_panelized)
251 other_fname = x_basename (other_fname);
253 if (strcmp (source_fname, other_fname) == 0)
254 break;
257 if (j >= other->dir.len)
258 /* Not found -> mark */
259 do_file_mark (panel, i, 1);
260 else
262 /* Found */
263 file_entry_t *target = &other->dir.list[j];
265 if (mode != compare_size_only)
266 /* Older version is not marked */
267 if (source->st.st_mtime < target->st.st_mtime)
268 continue;
270 /* Newer version with different size is marked */
271 if (source->st.st_size != target->st.st_size)
273 do_file_mark (panel, i, 1);
274 continue;
277 if (mode == compare_size_only)
278 continue;
280 if (mode == compare_quick)
282 /* Thorough compare off, compare only time stamps */
283 /* Mark newer version, don't mark version with the same date */
284 if (source->st.st_mtime > target->st.st_mtime)
285 do_file_mark (panel, i, 1);
287 continue;
290 /* Thorough compare on, do byte-by-byte comparison */
292 vfs_path_t *src_name, *dst_name;
294 src_name =
295 vfs_path_append_new (panel->cwd_vpath, source->fname->str, (char *) NULL);
296 dst_name =
297 vfs_path_append_new (other->cwd_vpath, target->fname->str, (char *) NULL);
298 if (compare_files (src_name, dst_name, source->st.st_size))
299 do_file_mark (panel, i, 1);
300 vfs_path_free (src_name, TRUE);
301 vfs_path_free (dst_name, TRUE);
304 } /* for (i ...) */
307 /* --------------------------------------------------------------------------------------------- */
309 static void
310 do_link (link_type_t link_type, const char *fname)
312 char *dest = NULL, *src = NULL;
313 vfs_path_t *dest_vpath = NULL;
315 if (link_type == LINK_HARDLINK)
317 vfs_path_t *fname_vpath;
319 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
320 dest =
321 input_expand_dialog (_("Link"), src, MC_HISTORY_FM_LINK, "", INPUT_COMPLETE_FILENAMES);
322 if (dest == NULL || *dest == '\0')
323 goto cleanup;
325 save_cwds_stat ();
327 fname_vpath = vfs_path_from_str (fname);
328 dest_vpath = vfs_path_from_str (dest);
329 if (mc_link (fname_vpath, dest_vpath) == -1)
330 message (D_ERROR, MSG_ERROR, _("link: %s"), unix_error_string (errno));
331 vfs_path_free (fname_vpath, TRUE);
333 else
335 vfs_path_t *s, *d;
337 /* suggest the full path for symlink, and either the full or
338 relative path to the file it points to */
339 s = vfs_path_append_new (current_panel->cwd_vpath, fname, (char *) NULL);
341 if (get_other_type () == view_listing)
342 d = vfs_path_append_new (other_panel->cwd_vpath, fname, (char *) NULL);
343 else
344 d = vfs_path_from_str (fname);
346 if (link_type == LINK_SYMLINK_RELATIVE)
348 char *s_str;
350 s_str = diff_two_paths (other_panel->cwd_vpath, s);
351 vfs_path_free (s, TRUE);
352 s = vfs_path_from_str_flags (s_str, VPF_NO_CANON);
353 g_free (s_str);
356 symlink_box (s, d, &dest, &src);
357 vfs_path_free (d, TRUE);
358 vfs_path_free (s, TRUE);
360 if (dest == NULL || *dest == '\0' || src == NULL || *src == '\0')
361 goto cleanup;
363 save_cwds_stat ();
365 dest_vpath = vfs_path_from_str_flags (dest, VPF_NO_CANON);
367 s = vfs_path_from_str (src);
368 if (mc_symlink (dest_vpath, s) == -1)
369 message (D_ERROR, MSG_ERROR, _("symlink: %s"), unix_error_string (errno));
370 vfs_path_free (s, TRUE);
373 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
374 repaint_screen ();
376 cleanup:
377 vfs_path_free (dest_vpath, TRUE);
378 g_free (src);
379 g_free (dest);
382 /* --------------------------------------------------------------------------------------------- */
384 #if defined(ENABLE_VFS_UNDELFS) || defined(ENABLE_VFS_NET)
385 static void
386 nice_cd (const char *text, const char *xtext, const char *help,
387 const char *history_name, const char *prefix, int to_home, gboolean strip_password)
389 char *machine;
390 char *cd_path;
392 machine =
393 input_dialog_help (text, xtext, help, history_name, INPUT_LAST_TEXT, strip_password,
394 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_CD | INPUT_COMPLETE_HOSTNAMES |
395 INPUT_COMPLETE_USERNAMES);
396 if (machine == NULL)
397 return;
399 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
400 ugly as hell and leads to problems in vfs layer */
402 if (strncmp (prefix, machine, strlen (prefix)) == 0)
403 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
404 else
405 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
407 g_free (machine);
409 if (!IS_PATH_SEP (*cd_path))
411 char *tmp = cd_path;
413 cd_path = g_strconcat (PATH_SEP_STR, tmp, (char *) NULL);
414 g_free (tmp);
418 panel_view_mode_t save_type;
419 vfs_path_t *cd_vpath;
421 save_type = get_panel_type (MENU_PANEL_IDX);
423 if (save_type != view_listing)
424 create_panel (MENU_PANEL_IDX, view_listing);
426 cd_vpath = vfs_path_from_str_flags (cd_path, VPF_NO_CANON);
427 if (!panel_do_cd (MENU_PANEL, cd_vpath, cd_parse_command))
429 cd_error_message (cd_path);
431 if (save_type != view_listing)
432 create_panel (MENU_PANEL_IDX, save_type);
434 vfs_path_free (cd_vpath, TRUE);
436 g_free (cd_path);
438 /* In case of passive panel, restore current VFS directory that was changed in panel_do_cd() */
439 if (MENU_PANEL != current_panel)
440 (void) mc_chdir (current_panel->cwd_vpath);
442 #endif /* ENABLE_VFS_UNDELFS || ENABLE_VFS_NET */
444 /* --------------------------------------------------------------------------------------------- */
446 static void
447 configure_panel_listing (WPanel *p, int list_format, int brief_cols, gboolean use_msformat,
448 char **user, char **status)
450 p->user_mini_status = use_msformat;
451 p->list_format = list_format;
453 if (list_format == list_brief)
454 p->brief_cols = brief_cols;
456 if (list_format == list_user || use_msformat)
458 g_free (p->user_format);
459 p->user_format = *user;
460 *user = NULL;
462 g_free (p->user_status_format[list_format]);
463 p->user_status_format[list_format] = *status;
464 *status = NULL;
466 set_panel_formats (p);
469 set_panel_formats (p);
470 do_refresh ();
473 /* --------------------------------------------------------------------------------------------- */
475 static void
476 switch_to_listing (int panel_index)
478 if (get_panel_type (panel_index) != view_listing)
479 create_panel (panel_index, view_listing);
482 /* --------------------------------------------------------------------------------------------- */
483 /*** public functions ****************************************************************************/
484 /* --------------------------------------------------------------------------------------------- */
486 gboolean
487 view_file_at_line (const vfs_path_t *filename_vpath, gboolean plain_view, gboolean internal,
488 long start_line, off_t search_start, off_t search_end)
490 gboolean ret = TRUE;
492 if (plain_view)
494 mcview_mode_flags_t changed_flags;
496 mcview_clear_mode_flags (&changed_flags);
497 mcview_altered_flags.hex = FALSE;
498 mcview_altered_flags.magic = FALSE;
499 mcview_altered_flags.nroff = FALSE;
500 if (mcview_global_flags.hex)
501 changed_flags.hex = TRUE;
502 if (mcview_global_flags.magic)
503 changed_flags.magic = TRUE;
504 if (mcview_global_flags.nroff)
505 changed_flags.nroff = TRUE;
506 mcview_global_flags.hex = FALSE;
507 mcview_global_flags.magic = FALSE;
508 mcview_global_flags.nroff = FALSE;
510 ret = mcview_viewer (NULL, filename_vpath, start_line, search_start, search_end);
512 if (changed_flags.hex && !mcview_altered_flags.hex)
513 mcview_global_flags.hex = TRUE;
514 if (changed_flags.magic && !mcview_altered_flags.magic)
515 mcview_global_flags.magic = TRUE;
516 if (changed_flags.nroff && !mcview_altered_flags.nroff)
517 mcview_global_flags.nroff = TRUE;
519 dialog_switch_process_pending ();
521 else if (internal)
523 char view_entry[BUF_TINY];
525 if (start_line > 0)
526 g_snprintf (view_entry, sizeof (view_entry), "View:%ld", start_line);
527 else
528 strcpy (view_entry, "View");
530 ret = (regex_command (filename_vpath, view_entry) == 0);
531 if (ret)
533 ret = mcview_viewer (NULL, filename_vpath, start_line, search_start, search_end);
534 dialog_switch_process_pending ();
537 else
539 static const char *viewer = NULL;
541 if (viewer == NULL)
543 viewer = getenv ("VIEWER");
544 if (viewer == NULL)
545 viewer = getenv ("PAGER");
546 if (viewer == NULL)
547 viewer = "view";
550 execute_external_editor_or_viewer (viewer, filename_vpath, start_line);
553 return ret;
556 /* --------------------------------------------------------------------------------------------- */
557 /** view_file (filename, plain_view, internal)
559 * Inputs:
560 * filename_vpath: The file name to view
561 * plain_view: If set does not do any fancy pre-processing (no filtering) and
562 * always invokes the internal viewer.
563 * internal: If set uses the internal viewer, otherwise an external viewer.
566 gboolean
567 view_file (const vfs_path_t *filename_vpath, gboolean plain_view, gboolean internal)
569 return view_file_at_line (filename_vpath, plain_view, internal, 0, 0, 0);
573 /* --------------------------------------------------------------------------------------------- */
574 /** Run user's preferred viewer on the current file */
576 void
577 view_cmd (WPanel *panel)
579 do_view_cmd (panel, FALSE);
582 /* --------------------------------------------------------------------------------------------- */
583 /** Ask for file and run user's preferred viewer on it */
585 void
586 view_file_cmd (const WPanel *panel)
588 char *filename;
589 vfs_path_t *vpath;
591 filename =
592 input_expand_dialog (_("View file"), _("Filename:"),
593 MC_HISTORY_FM_VIEW_FILE, panel_current_entry (panel)->fname->str,
594 INPUT_COMPLETE_FILENAMES);
595 if (filename == NULL)
596 return;
598 vpath = vfs_path_from_str (filename);
599 g_free (filename);
600 view_file (vpath, FALSE, use_internal_view);
601 vfs_path_free (vpath, TRUE);
604 /* --------------------------------------------------------------------------------------------- */
605 /** Run plain internal viewer on the current file */
606 void
607 view_raw_cmd (WPanel *panel)
609 do_view_cmd (panel, TRUE);
612 /* --------------------------------------------------------------------------------------------- */
614 void
615 view_filtered_cmd (const WPanel *panel)
617 char *command;
618 const char *initial_command;
620 if (input_is_empty (cmdline))
621 initial_command = panel_current_entry (panel)->fname->str;
622 else
623 initial_command = input_get_ctext (cmdline);
625 command =
626 input_dialog (_("Filtered view"),
627 _("Filter command and arguments:"),
628 MC_HISTORY_FM_FILTERED_VIEW, initial_command,
629 INPUT_COMPLETE_FILENAMES | INPUT_COMPLETE_COMMANDS);
631 if (command != NULL)
633 mcview_viewer (command, NULL, 0, 0, 0);
634 g_free (command);
635 dialog_switch_process_pending ();
639 /* --------------------------------------------------------------------------------------------- */
641 void
642 edit_file_at_line (const vfs_path_t *what_vpath, gboolean internal, long start_line)
645 #ifdef USE_INTERNAL_EDIT
646 if (internal)
648 const edit_arg_t arg = { (vfs_path_t *) what_vpath, start_line };
650 edit_file (&arg);
652 else
653 #endif /* USE_INTERNAL_EDIT */
655 static const char *editor = NULL;
657 (void) internal;
659 if (editor == NULL)
661 editor = getenv ("EDITOR");
662 if (editor == NULL)
663 editor = get_default_editor ();
666 execute_external_editor_or_viewer (editor, what_vpath, start_line);
669 if (mc_global.mc_run_mode == MC_RUN_FULL)
670 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
672 #ifdef USE_INTERNAL_EDIT
673 if (use_internal_edit)
674 dialog_switch_process_pending ();
675 else
676 #endif /* USE_INTERNAL_EDIT */
677 repaint_screen ();
680 /* --------------------------------------------------------------------------------------------- */
682 void
683 edit_cmd (const WPanel *panel)
685 vfs_path_t *fname;
687 fname = vfs_path_from_str (panel_current_entry (panel)->fname->str);
688 if (regex_command (fname, "Edit") == 0)
689 do_edit (fname);
690 vfs_path_free (fname, TRUE);
693 /* --------------------------------------------------------------------------------------------- */
695 #ifdef USE_INTERNAL_EDIT
696 void
697 edit_cmd_force_internal (const WPanel *panel)
699 vfs_path_t *fname;
701 fname = vfs_path_from_str (panel_current_entry (panel)->fname->str);
702 if (regex_command (fname, "Edit") == 0)
703 edit_file_at_line (fname, TRUE, 1);
704 vfs_path_free (fname, TRUE);
706 #endif
708 /* --------------------------------------------------------------------------------------------- */
710 void
711 edit_cmd_new (void)
713 vfs_path_t *fname_vpath = NULL;
715 if (editor_ask_filename_before_edit)
717 char *fname;
719 fname = input_expand_dialog (_("Edit file"), _("Enter file name:"),
720 MC_HISTORY_EDIT_LOAD, "", INPUT_COMPLETE_FILENAMES);
721 if (fname == NULL)
722 return;
724 if (*fname != '\0')
725 fname_vpath = vfs_path_from_str (fname);
727 g_free (fname);
730 #ifdef HAVE_CHARSET
731 mc_global.source_codepage = default_source_codepage;
732 #endif
733 do_edit (fname_vpath);
735 vfs_path_free (fname_vpath, TRUE);
738 /* --------------------------------------------------------------------------------------------- */
740 void
741 mkdir_cmd (WPanel *panel)
743 const file_entry_t *fe;
744 char *dir;
745 const char *name = "";
747 fe = panel_current_entry (panel);
749 /* If 'on' then automatically fills name with current item name */
750 if (auto_fill_mkdir_name && !DIR_IS_DOTDOT (fe->fname->str))
751 name = fe->fname->str;
753 dir =
754 input_expand_dialog (_("Create a new Directory"),
755 _("Enter directory name:"), MC_HISTORY_FM_MKDIR, name,
756 INPUT_COMPLETE_FILENAMES);
758 if (dir != NULL && *dir != '\0')
760 vfs_path_t *absdir;
762 if (IS_PATH_SEP (dir[0]) || dir[0] == '~')
763 absdir = vfs_path_from_str (dir);
764 else
766 /* possible escaped '~' */
767 /* allow create directory with name '~' */
768 char *tmpdir = dir;
770 if (dir[0] == '\\' && dir[1] == '~')
771 tmpdir = dir + 1;
773 absdir = vfs_path_append_new (panel->cwd_vpath, tmpdir, (char *) NULL);
776 save_cwds_stat ();
778 if (my_mkdir (absdir, 0777) != 0)
779 message (D_ERROR, MSG_ERROR, "%s", unix_error_string (errno));
780 else
782 update_panels (UP_OPTIMIZE, dir);
783 repaint_screen ();
784 select_item (panel);
787 vfs_path_free (absdir, TRUE);
789 g_free (dir);
792 /* --------------------------------------------------------------------------------------------- */
794 void
795 reread_cmd (void)
797 panel_update_flags_t flag = UP_ONLY_CURRENT;
799 if (get_current_type () == view_listing && get_other_type () == view_listing &&
800 vfs_path_equal (current_panel->cwd_vpath, other_panel->cwd_vpath))
801 flag = UP_OPTIMIZE;
803 update_panels (UP_RELOAD | flag, UP_KEEPSEL);
804 repaint_screen ();
807 /* --------------------------------------------------------------------------------------------- */
809 void
810 ext_cmd (void)
812 vfs_path_t *extdir_vpath;
813 int dir = 0;
815 if (geteuid () == 0)
816 dir = query_dialog (_("Extension file edit"),
817 _("Which extension file you want to edit?"), D_NORMAL, 2,
818 _("&User"), _("&System Wide"));
820 extdir_vpath = vfs_path_build_filename (mc_global.sysconfig_dir, MC_EXT_FILE, (char *) NULL);
822 if (dir == 0)
824 vfs_path_t *buffer_vpath;
826 buffer_vpath = mc_config_get_full_vpath (MC_EXT_FILE);
827 check_for_default (extdir_vpath, buffer_vpath);
828 do_edit (buffer_vpath);
829 vfs_path_free (buffer_vpath, TRUE);
831 else if (dir == 1)
833 if (!exist_file (vfs_path_get_last_path_str (extdir_vpath)))
835 vfs_path_free (extdir_vpath, TRUE);
836 extdir_vpath =
837 vfs_path_build_filename (mc_global.share_data_dir, MC_EXT_FILE, (char *) NULL);
839 do_edit (extdir_vpath);
842 vfs_path_free (extdir_vpath, TRUE);
843 flush_extension_file ();
846 /* --------------------------------------------------------------------------------------------- */
847 /** edit file menu for mc */
849 void
850 edit_mc_menu_cmd (void)
852 vfs_path_t *buffer_vpath;
853 vfs_path_t *menufile_vpath;
854 int dir = 0;
856 query_set_sel (1);
857 dir = query_dialog (_("Menu edit"),
858 _("Which menu file do you want to edit?"),
859 D_NORMAL, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
861 menufile_vpath =
862 vfs_path_build_filename (mc_global.sysconfig_dir, MC_GLOBAL_MENU, (char *) NULL);
864 if (!exist_file (vfs_path_get_last_path_str (menufile_vpath)))
866 vfs_path_free (menufile_vpath, TRUE);
867 menufile_vpath =
868 vfs_path_build_filename (mc_global.share_data_dir, MC_GLOBAL_MENU, (char *) NULL);
871 switch (dir)
873 case 0:
874 buffer_vpath = vfs_path_from_str (MC_LOCAL_MENU);
875 check_for_default (menufile_vpath, buffer_vpath);
876 chmod (vfs_path_get_last_path_str (buffer_vpath), 0600);
877 break;
879 case 1:
880 buffer_vpath = mc_config_get_full_vpath (MC_USERMENU_FILE);
881 check_for_default (menufile_vpath, buffer_vpath);
882 break;
884 case 2:
885 buffer_vpath =
886 vfs_path_build_filename (mc_global.sysconfig_dir, MC_GLOBAL_MENU, (char *) NULL);
887 if (!exist_file (vfs_path_get_last_path_str (buffer_vpath)))
889 vfs_path_free (buffer_vpath, TRUE);
890 buffer_vpath =
891 vfs_path_build_filename (mc_global.share_data_dir, MC_GLOBAL_MENU, (char *) NULL);
893 break;
895 default:
896 vfs_path_free (menufile_vpath, TRUE);
897 return;
900 do_edit (buffer_vpath);
902 vfs_path_free (buffer_vpath, TRUE);
903 vfs_path_free (menufile_vpath, TRUE);
906 /* --------------------------------------------------------------------------------------------- */
908 void
909 edit_fhl_cmd (void)
911 vfs_path_t *fhlfile_vpath = NULL;
912 int dir = 0;
914 if (geteuid () == 0)
915 dir = query_dialog (_("Highlighting groups file edit"),
916 _("Which highlighting file you want to edit?"), D_NORMAL, 2,
917 _("&User"), _("&System Wide"));
919 fhlfile_vpath =
920 vfs_path_build_filename (mc_global.sysconfig_dir, MC_FHL_INI_FILE, (char *) NULL);
922 if (dir == 0)
924 vfs_path_t *buffer_vpath;
926 buffer_vpath = mc_config_get_full_vpath (MC_FHL_INI_FILE);
927 check_for_default (fhlfile_vpath, buffer_vpath);
928 do_edit (buffer_vpath);
929 vfs_path_free (buffer_vpath, TRUE);
931 else if (dir == 1)
933 if (!exist_file (vfs_path_get_last_path_str (fhlfile_vpath)))
935 vfs_path_free (fhlfile_vpath, TRUE);
936 fhlfile_vpath =
937 vfs_path_build_filename (mc_global.sysconfig_dir, MC_FHL_INI_FILE, (char *) NULL);
939 do_edit (fhlfile_vpath);
942 vfs_path_free (fhlfile_vpath, TRUE);
943 /* refresh highlighting rules */
944 mc_fhl_free (&mc_filehighlight);
945 mc_filehighlight = mc_fhl_new (TRUE);
948 /* --------------------------------------------------------------------------------------------- */
950 void
951 hotlist_cmd (WPanel *panel)
953 char *target;
955 target = hotlist_show (LIST_HOTLIST, panel);
956 if (target == NULL)
957 return;
959 if (get_current_type () == view_tree)
961 vfs_path_t *vpath;
963 vpath = vfs_path_from_str (target);
964 tree_chdir (the_tree, vpath);
965 vfs_path_free (vpath, TRUE);
967 else
969 vfs_path_t *deprecated_vpath;
970 const char *deprecated_path;
972 deprecated_vpath = vfs_path_from_str_flags (target, VPF_USE_DEPRECATED_PARSER);
973 deprecated_path = vfs_path_as_str (deprecated_vpath);
974 cd_to (deprecated_path);
975 vfs_path_free (deprecated_vpath, TRUE);
978 g_free (target);
981 /* --------------------------------------------------------------------------------------------- */
983 #ifdef ENABLE_VFS
984 void
985 vfs_list (WPanel *panel)
987 char *target;
988 vfs_path_t *target_vpath;
990 target = hotlist_show (LIST_VFSLIST, panel);
991 if (target == NULL)
992 return;
994 target_vpath = vfs_path_from_str (target);
995 if (!panel_cd (current_panel, target_vpath, cd_exact))
996 cd_error_message (target);
997 vfs_path_free (target_vpath, TRUE);
998 g_free (target);
1000 #endif /* ENABLE_VFS */
1002 /* --------------------------------------------------------------------------------------------- */
1004 void
1005 compare_dirs_cmd (void)
1007 int choice;
1008 enum CompareMode thorough_flag;
1010 choice =
1011 query_dialog (_("Compare directories"),
1012 _("Select compare method:"), D_NORMAL, 4,
1013 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
1015 if (choice < 0 || choice > 2)
1016 return;
1018 thorough_flag = choice;
1020 if (get_current_type () == view_listing && get_other_type () == view_listing)
1022 compare_dir (current_panel, other_panel, thorough_flag);
1023 compare_dir (other_panel, current_panel, thorough_flag);
1025 else
1026 message (D_ERROR, MSG_ERROR,
1027 _("Both panels should be in the listing mode\nto use this command"));
1030 /* --------------------------------------------------------------------------------------------- */
1032 #ifdef USE_DIFF_VIEW
1033 void
1034 diff_view_cmd (void)
1036 /* both panels must be in the list mode */
1037 if (get_current_type () == view_listing && get_other_type () == view_listing)
1039 if (get_current_index () == 0)
1040 dview_diff_cmd (current_panel, other_panel);
1041 else
1042 dview_diff_cmd (other_panel, current_panel);
1044 if (mc_global.mc_run_mode == MC_RUN_FULL)
1045 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1047 dialog_switch_process_pending ();
1050 #endif
1052 /* --------------------------------------------------------------------------------------------- */
1054 void
1055 swap_cmd (void)
1057 swap_panels ();
1058 tty_touch_screen ();
1059 repaint_screen ();
1062 /* --------------------------------------------------------------------------------------------- */
1064 void
1065 link_cmd (link_type_t link_type)
1067 const char *filename;
1069 filename = panel_current_entry (current_panel)->fname->str;
1070 if (filename != NULL)
1071 do_link (link_type, filename);
1074 /* --------------------------------------------------------------------------------------------- */
1076 void
1077 edit_symlink_cmd (void)
1079 const file_entry_t *fe;
1080 const char *p;
1082 fe = panel_current_entry (current_panel);
1083 p = fe->fname->str;
1085 if (!S_ISLNK (fe->st.st_mode))
1086 message (D_ERROR, MSG_ERROR, _("'%s' is not a symbolic link"), p);
1087 else
1089 char buffer[MC_MAXPATHLEN];
1090 int i;
1092 i = readlink (p, buffer, sizeof (buffer) - 1);
1093 if (i > 0)
1095 char *q, *dest;
1097 buffer[i] = '\0';
1099 q = g_strdup_printf (_("Symlink '%s\' points to:"), str_trunc (p, 32));
1100 dest =
1101 input_expand_dialog (_("Edit symlink"), q, MC_HISTORY_FM_EDIT_LINK, buffer,
1102 INPUT_COMPLETE_FILENAMES);
1103 g_free (q);
1105 if (dest != NULL && *dest != '\0' && strcmp (buffer, dest) != 0)
1107 vfs_path_t *p_vpath;
1109 p_vpath = vfs_path_from_str (p);
1111 save_cwds_stat ();
1113 if (mc_unlink (p_vpath) == -1)
1114 message (D_ERROR, MSG_ERROR, _("edit symlink, unable to remove %s: %s"), p,
1115 unix_error_string (errno));
1116 else
1118 vfs_path_t *dest_vpath;
1120 dest_vpath = vfs_path_from_str_flags (dest, VPF_NO_CANON);
1121 if (mc_symlink (dest_vpath, p_vpath) == -1)
1122 message (D_ERROR, MSG_ERROR, _("edit symlink: %s"),
1123 unix_error_string (errno));
1124 vfs_path_free (dest_vpath, TRUE);
1127 vfs_path_free (p_vpath, TRUE);
1129 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1130 repaint_screen ();
1133 g_free (dest);
1138 /* --------------------------------------------------------------------------------------------- */
1140 void
1141 help_cmd (void)
1143 ev_help_t event_data = { NULL, NULL };
1145 if (current_panel->quick_search.active)
1146 event_data.node = "[Quick search]";
1147 else
1148 event_data.node = "[main]";
1150 mc_event_raise (MCEVENT_GROUP_CORE, "help", &event_data);
1153 /* --------------------------------------------------------------------------------------------- */
1155 #ifdef ENABLE_VFS_FTP
1156 void
1157 ftplink_cmd (void)
1159 nice_cd (_("FTP to machine"), _(machine_str),
1160 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "ftp://", 1, TRUE);
1162 #endif /* ENABLE_VFS_FTP */
1164 /* --------------------------------------------------------------------------------------------- */
1166 #ifdef ENABLE_VFS_SFTP
1167 void
1168 sftplink_cmd (void)
1170 nice_cd (_("SFTP to machine"), _(machine_str),
1171 "[SFTP (SSH File Transfer Protocol) filesystem]",
1172 ":sftplink_cmd: SFTP to machine ", "sftp://", 1, TRUE);
1174 #endif /* ENABLE_VFS_SFTP */
1176 /* --------------------------------------------------------------------------------------------- */
1178 #ifdef ENABLE_VFS_SHELL
1179 void
1180 shelllink_cmd (void)
1182 nice_cd (_("Shell link to machine"), _(machine_str),
1183 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1184 "sh://", 1, TRUE);
1186 #endif /* ENABLE_VFS_SHELL */
1188 /* --------------------------------------------------------------------------------------------- */
1190 #ifdef ENABLE_VFS_UNDELFS
1191 void
1192 undelete_cmd (void)
1194 nice_cd (_("Undelete files on an ext2 file system"),
1195 _("Enter device (without /dev/) to undelete\nfiles on: (F1 for details)"),
1196 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "undel://", 0, FALSE);
1198 #endif /* ENABLE_VFS_UNDELFS */
1200 /* --------------------------------------------------------------------------------------------- */
1202 void
1203 quick_cd_cmd (WPanel *panel)
1205 char *p;
1207 p = cd_box (panel);
1208 if (p != NULL && *p != '\0')
1209 cd_to (p);
1210 g_free (p);
1213 /* --------------------------------------------------------------------------------------------- */
1215 \brief calculate dirs sizes
1217 calculate dirs sizes and resort panel:
1218 dirs_selected = show size for selected dirs,
1219 otherwise = show size for dir under cursor:
1220 dir under cursor ".." = show size for all dirs,
1221 otherwise = show size for dir under cursor
1224 void
1225 smart_dirsize_cmd (WPanel *panel)
1227 const file_entry_t *entry;
1229 entry = panel_current_entry (panel);
1230 if ((S_ISDIR (entry->st.st_mode) && DIR_IS_DOTDOT (entry->fname->str)) || panel->dirs_marked)
1231 dirsizes_cmd (panel);
1232 else
1233 single_dirsize_cmd (panel);
1236 /* --------------------------------------------------------------------------------------------- */
1238 void
1239 single_dirsize_cmd (WPanel *panel)
1241 file_entry_t *entry;
1243 entry = panel_current_entry (panel);
1245 if (S_ISDIR (entry->st.st_mode) && !DIR_IS_DOTDOT (entry->fname->str))
1247 size_t dir_count = 0;
1248 size_t count = 0;
1249 uintmax_t total = 0;
1250 dirsize_status_msg_t dsm;
1251 vfs_path_t *p;
1253 p = vfs_path_from_str (entry->fname->str);
1255 memset (&dsm, 0, sizeof (dsm));
1256 status_msg_init (STATUS_MSG (&dsm), _("Directory scanning"), 0, dirsize_status_init_cb,
1257 dirsize_status_update_cb, dirsize_status_deinit_cb);
1259 if (compute_dir_size (p, &dsm, &dir_count, &count, &total, FALSE) == FILE_CONT)
1261 entry->st.st_size = (off_t) total;
1262 entry->f.dir_size_computed = 1;
1265 vfs_path_free (p, TRUE);
1267 status_msg_deinit (STATUS_MSG (&dsm));
1270 if (panels_options.mark_moves_down)
1271 send_message (panel, NULL, MSG_ACTION, CK_Down, NULL);
1273 recalculate_panel_summary (panel);
1275 if (panel->sort_field->sort_routine == (GCompareFunc) sort_size)
1276 panel_re_sort (panel);
1278 panel->dirty = TRUE;
1281 /* --------------------------------------------------------------------------------------------- */
1283 void
1284 dirsizes_cmd (WPanel *panel)
1286 int i;
1287 dirsize_status_msg_t dsm;
1289 memset (&dsm, 0, sizeof (dsm));
1290 status_msg_init (STATUS_MSG (&dsm), _("Directory scanning"), 0, dirsize_status_init_cb,
1291 dirsize_status_update_cb, dirsize_status_deinit_cb);
1293 for (i = 0; i < panel->dir.len; i++)
1294 if (S_ISDIR (panel->dir.list[i].st.st_mode)
1295 && ((panel->dirs_marked != 0 && panel->dir.list[i].f.marked != 0)
1296 || panel->dirs_marked == 0) && !DIR_IS_DOTDOT (panel->dir.list[i].fname->str))
1298 vfs_path_t *p;
1299 size_t dir_count = 0;
1300 size_t count = 0;
1301 uintmax_t total = 0;
1302 gboolean ok;
1304 p = vfs_path_from_str (panel->dir.list[i].fname->str);
1305 ok = compute_dir_size (p, &dsm, &dir_count, &count, &total, FALSE) != FILE_CONT;
1306 vfs_path_free (p, TRUE);
1307 if (ok)
1308 break;
1310 panel->dir.list[i].st.st_size = (off_t) total;
1311 panel->dir.list[i].f.dir_size_computed = 1;
1314 status_msg_deinit (STATUS_MSG (&dsm));
1316 recalculate_panel_summary (panel);
1318 if (panel->sort_field->sort_routine == (GCompareFunc) sort_size)
1319 panel_re_sort (panel);
1321 panel->dirty = TRUE;
1324 /* --------------------------------------------------------------------------------------------- */
1326 void
1327 save_setup_cmd (void)
1329 vfs_path_t *vpath;
1330 const char *path;
1332 vpath = vfs_path_from_str_flags (mc_config_get_path (), VPF_STRIP_HOME);
1333 path = vfs_path_as_str (vpath);
1335 if (save_setup (TRUE, TRUE))
1336 message (D_NORMAL, _("Setup"), _("Setup saved to %s"), path);
1337 else
1338 message (D_ERROR, _("Setup"), _("Unable to save setup to %s"), path);
1340 vfs_path_free (vpath, TRUE);
1343 /* --------------------------------------------------------------------------------------------- */
1345 void
1346 info_cmd_no_menu (void)
1348 if (get_panel_type (0) == view_info)
1349 create_panel (0, view_listing);
1350 else if (get_panel_type (1) == view_info)
1351 create_panel (1, view_listing);
1352 else
1353 create_panel (current_panel == left_panel ? 1 : 0, view_info);
1356 /* --------------------------------------------------------------------------------------------- */
1358 void
1359 quick_cmd_no_menu (void)
1361 if (get_panel_type (0) == view_quick)
1362 create_panel (0, view_listing);
1363 else if (get_panel_type (1) == view_quick)
1364 create_panel (1, view_listing);
1365 else
1366 create_panel (current_panel == left_panel ? 1 : 0, view_quick);
1369 /* --------------------------------------------------------------------------------------------- */
1371 void
1372 listing_cmd (void)
1374 WPanel *p;
1376 switch_to_listing (MENU_PANEL_IDX);
1378 p = PANEL (get_panel_widget (MENU_PANEL_IDX));
1380 p->is_panelized = FALSE;
1381 panel_set_filter (p, NULL); /* including panel reload */
1384 /* --------------------------------------------------------------------------------------------- */
1386 void
1387 setup_listing_format_cmd (void)
1389 int list_format;
1390 gboolean use_msformat;
1391 int brief_cols;
1392 char *user, *status;
1393 WPanel *p = NULL;
1395 if (SELECTED_IS_PANEL)
1396 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1398 list_format = panel_listing_box (p, MENU_PANEL_IDX, &user, &status, &use_msformat, &brief_cols);
1399 if (list_format != -1)
1401 switch_to_listing (MENU_PANEL_IDX);
1402 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1403 configure_panel_listing (p, list_format, brief_cols, use_msformat, &user, &status);
1404 g_free (user);
1405 g_free (status);
1409 /* --------------------------------------------------------------------------------------------- */
1411 void
1412 panel_tree_cmd (void)
1414 create_panel (MENU_PANEL_IDX, view_tree);
1417 /* --------------------------------------------------------------------------------------------- */
1419 void
1420 info_cmd (void)
1422 create_panel (MENU_PANEL_IDX, view_info);
1425 /* --------------------------------------------------------------------------------------------- */
1427 void
1428 quick_view_cmd (void)
1430 if (PANEL (get_panel_widget (MENU_PANEL_IDX)) == current_panel)
1431 (void) change_panel ();
1432 create_panel (MENU_PANEL_IDX, view_quick);
1435 /* --------------------------------------------------------------------------------------------- */
1437 #ifdef HAVE_CHARSET
1438 void
1439 encoding_cmd (void)
1441 if (SELECTED_IS_PANEL)
1442 panel_change_encoding (MENU_PANEL);
1444 #endif
1446 /* --------------------------------------------------------------------------------------------- */