Merge branch '2499_mcedit_select_cur_word'
[midnight-commander/borarpet.git] / src / filemanager / cmd.c
bloba5d504e7ff26172119b48c85f997482ada777f10
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"
61 #include "lib/keybind.h" /* CK_PanelMoveDown, CK_InputHistoryShow */
63 #include "src/subshell.h" /* use_subshell */
64 #include "src/consaver/cons.saver.h" /* console_flag */
65 #include "src/viewer/mcviewer.h"
66 #include "src/help.h" /* interactive_display() */
67 #include "src/setup.h"
68 #include "src/execute.h" /* toggle_panels() */
69 #include "src/history.h"
71 #ifdef USE_INTERNAL_EDIT
72 #include "src/editor/edit.h"
73 #endif
75 #ifdef USE_DIFF_VIEW
76 #include "src/diffviewer/ydiff.h"
77 #endif
79 #include "fileopctx.h"
80 #include "file.h" /* file operation routines */
81 #include "find.h" /* do_find() */
82 #include "hotlist.h" /* hotlist_cmd() */
83 #include "tree.h" /* tree_chdir() */
84 #include "midnight.h" /* change_panel() */
85 #include "usermenu.h" /* MC_GLOBAL_MENU */
86 #include "command.h" /* cmdline */
87 #include "layout.h" /* get_current_type() */
88 #include "ext.h" /* regex_command() */
89 #include "boxes.h" /* cd_dialog() */
90 #include "dir.h"
92 #include "cmd.h" /* Our definitions */
94 /*** global variables ****************************************************************************/
96 int select_flags = SELECT_MATCH_CASE | SELECT_SHELL_PATTERNS;
98 /*** file scope macro definitions ****************************************************************/
100 #ifndef MAP_FILE
101 #define MAP_FILE 0
102 #endif
104 /*** file scope type declarations ****************************************************************/
106 enum CompareMode
108 compare_quick, compare_size_only, compare_thourough
111 /*** file scope variables ************************************************************************/
113 #ifdef ENABLE_VFS_NET
114 static const char *machine_str = N_("Enter machine name (F1 for details):");
115 #endif /* ENABLE_VFS_NET */
117 /*** file scope functions ************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 /** scan_for_file (panel, idx, direction)
122 * Inputs:
123 * panel: pointer to the panel on which we operate
124 * idx: starting file.
125 * direction: 1, or -1
128 static int
129 scan_for_file (WPanel * panel, int idx, int direction)
131 int i = idx + direction;
133 while (i != idx)
135 if (i < 0)
136 i = panel->count - 1;
137 if (i == panel->count)
138 i = 0;
139 if (!S_ISDIR (panel->dir.list[i].st.st_mode))
140 return i;
141 i += direction;
143 return i;
146 /* --------------------------------------------------------------------------------------------- */
148 * Run viewer (internal or external) on the currently selected file.
149 * If normal is 1, force internal viewer and raw mode (used for F13).
151 static void
152 do_view_cmd (int normal)
154 /* Directories are viewed by changing to them */
155 if (S_ISDIR (selection (current_panel)->st.st_mode) || link_isdir (selection (current_panel)))
157 if (confirm_view_dir && (current_panel->marked || current_panel->dirs_marked))
159 if (query_dialog
160 (_("Confirmation"), _("Files tagged, want to cd?"), D_NORMAL, 2,
161 _("&Yes"), _("&No")) != 0)
163 return;
166 if (!do_cd (selection (current_panel)->fname, cd_exact))
167 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
169 else
171 int dir, file_idx;
172 char *filename;
174 file_idx = current_panel->selected;
175 while (1)
177 filename = current_panel->dir.list[file_idx].fname;
179 dir = view_file (filename, normal, use_internal_view);
180 if (dir == 0)
181 break;
182 file_idx = scan_for_file (current_panel, file_idx, dir);
186 repaint_screen ();
189 /* --------------------------------------------------------------------------------------------- */
191 static inline void
192 do_edit (const char *what)
194 do_edit_at_line (what, use_internal_edit, 0);
197 /* --------------------------------------------------------------------------------------------- */
199 static void
200 set_panel_filter_to (WPanel * p, char *allocated_filter_string)
202 g_free (p->filter);
203 p->filter = 0;
205 if (!(allocated_filter_string[0] == '*' && allocated_filter_string[1] == 0))
206 p->filter = allocated_filter_string;
207 else
208 g_free (allocated_filter_string);
209 reread_cmd ();
212 /* --------------------------------------------------------------------------------------------- */
213 /** Set a given panel filter expression */
215 static void
216 set_panel_filter (WPanel * p)
218 char *reg_exp;
219 const char *x;
221 x = p->filter ? p->filter : easy_patterns ? "*" : ".";
223 reg_exp = input_dialog_help (_("Filter"),
224 _("Set expression for filtering filenames"),
225 "[Filter...]", MC_HISTORY_FM_PANEL_FILTER, x);
226 if (!reg_exp)
227 return;
228 set_panel_filter_to (p, reg_exp);
231 /* --------------------------------------------------------------------------------------------- */
233 static void
234 select_unselect_cmd (const char *title, const char *history_name, gboolean do_select)
236 /* dialog sizes */
237 const int DX = 50;
238 const int DY = 7;
240 int files_only = (select_flags & SELECT_FILES_ONLY) != 0;
241 int case_sens = (select_flags & SELECT_MATCH_CASE) != 0;
242 int shell_patterns = (select_flags & SELECT_SHELL_PATTERNS) != 0;
244 char *reg_exp;
245 mc_search_t *search;
246 int i;
248 QuickWidget quick_widgets[] = {
249 QUICK_CHECKBOX (3, DX, DY - 3, DY, N_("&Using shell patterns"), &shell_patterns),
250 QUICK_CHECKBOX (DX / 2 + 1, DX, DY - 4, DY, N_("&Case sensitive"), &case_sens),
251 QUICK_CHECKBOX (3, DX, DY - 4, DY, N_("&Files only"), &files_only),
252 QUICK_INPUT (3, DX, DY - 5, DY, INPUT_LAST_TEXT, DX - 6, 0, history_name, &reg_exp),
253 QUICK_END
256 QuickDialog quick_dlg = {
257 DX, DY, -1, -1, title,
258 "[Select/Unselect Files]", quick_widgets, NULL, FALSE
261 if (quick_dialog (&quick_dlg) == B_CANCEL)
262 return;
264 if (!reg_exp)
265 return;
266 if (!*reg_exp)
268 g_free (reg_exp);
269 return;
271 search = mc_search_new (reg_exp, -1);
272 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
273 search->is_entire_line = TRUE;
274 search->is_case_sensitive = case_sens != 0;
276 for (i = 0; i < current_panel->count; i++)
278 if (strcmp (current_panel->dir.list[i].fname, "..") == 0)
279 continue;
280 if (S_ISDIR (current_panel->dir.list[i].st.st_mode) && files_only != 0)
281 continue;
283 if (mc_search_run (search, current_panel->dir.list[i].fname,
284 0, current_panel->dir.list[i].fnamelen, NULL))
285 do_file_mark (current_panel, i, do_select);
288 mc_search_free (search);
289 g_free (reg_exp);
291 /* result flags */
292 select_flags = 0;
293 if (case_sens != 0)
294 select_flags |= SELECT_MATCH_CASE;
295 if (files_only != 0)
296 select_flags |= SELECT_FILES_ONLY;
297 if (shell_patterns != 0)
298 select_flags |= SELECT_SHELL_PATTERNS;
301 /* --------------------------------------------------------------------------------------------- */
303 static int
304 compare_files (char *name1, char *name2, off_t size)
306 int file1, file2;
307 int result = -1; /* Different by default */
309 if (size == 0)
310 return 0;
312 file1 = open (name1, O_RDONLY);
313 if (file1 >= 0)
315 file2 = open (name2, O_RDONLY);
316 if (file2 >= 0)
318 #ifdef HAVE_MMAP
319 char *data1, *data2;
320 /* Ugly if jungle */
321 data1 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file1, 0);
322 if (data1 != (char *) -1)
324 data2 = mmap (0, size, PROT_READ, MAP_FILE | MAP_PRIVATE, file2, 0);
325 if (data2 != (char *) -1)
327 rotate_dash ();
328 result = memcmp (data1, data2, size);
329 munmap (data2, size);
331 munmap (data1, size);
333 #else
334 /* Don't have mmap() :( Even more ugly :) */
335 char buf1[BUFSIZ], buf2[BUFSIZ];
336 int n1, n2;
337 rotate_dash ();
340 while ((n1 = read (file1, buf1, BUFSIZ)) == -1 && errno == EINTR);
341 while ((n2 = read (file2, buf2, BUFSIZ)) == -1 && errno == EINTR);
343 while (n1 == n2 && n1 == BUFSIZ && !memcmp (buf1, buf2, BUFSIZ));
344 result = (n1 != n2) || memcmp (buf1, buf2, n1);
345 #endif /* !HAVE_MMAP */
346 close (file2);
348 close (file1);
350 return result;
353 /* --------------------------------------------------------------------------------------------- */
355 static void
356 compare_dir (WPanel * panel, WPanel * other, enum CompareMode mode)
358 int i, j;
359 char *src_name, *dst_name;
361 /* No marks by default */
362 panel->marked = 0;
363 panel->total = 0;
364 panel->dirs_marked = 0;
366 /* Handle all files in the panel */
367 for (i = 0; i < panel->count; i++)
369 file_entry *source = &panel->dir.list[i];
371 /* Default: unmarked */
372 file_mark (panel, i, 0);
374 /* Skip directories */
375 if (S_ISDIR (source->st.st_mode))
376 continue;
378 /* Search the corresponding entry from the other panel */
379 for (j = 0; j < other->count; j++)
381 if (strcmp (source->fname, other->dir.list[j].fname) == 0)
382 break;
384 if (j >= other->count)
385 /* Not found -> mark */
386 do_file_mark (panel, i, 1);
387 else
389 /* Found */
390 file_entry *target = &other->dir.list[j];
392 if (mode != compare_size_only)
394 /* Older version is not marked */
395 if (source->st.st_mtime < target->st.st_mtime)
396 continue;
399 /* Newer version with different size is marked */
400 if (source->st.st_size != target->st.st_size)
402 do_file_mark (panel, i, 1);
403 continue;
406 if (mode == compare_size_only)
407 continue;
409 if (mode == compare_quick)
411 /* Thorough compare off, compare only time stamps */
412 /* Mark newer version, don't mark version with the same date */
413 if (source->st.st_mtime > target->st.st_mtime)
415 do_file_mark (panel, i, 1);
417 continue;
420 /* Thorough compare on, do byte-by-byte comparison */
421 src_name = concat_dir_and_file (panel->cwd, source->fname);
422 dst_name = concat_dir_and_file (other->cwd, target->fname);
423 if (compare_files (src_name, dst_name, source->st.st_size))
424 do_file_mark (panel, i, 1);
425 g_free (src_name);
426 g_free (dst_name);
428 } /* for (i ...) */
431 /* --------------------------------------------------------------------------------------------- */
433 static void
434 do_link (link_type_t link_type, const char *fname)
436 char *dest = NULL, *src = NULL;
438 if (link_type == LINK_HARDLINK)
440 src = g_strdup_printf (_("Link %s to:"), str_trunc (fname, 46));
441 dest = input_expand_dialog (_("Link"), src, MC_HISTORY_FM_LINK, "");
442 if (!dest || !*dest)
443 goto cleanup;
444 save_cwds_stat ();
445 if (-1 == mc_link (fname, dest))
446 message (D_ERROR, MSG_ERROR, _("link: %s"), unix_error_string (errno));
448 else
450 char *s;
451 char *d;
453 /* suggest the full path for symlink, and either the full or
454 relative path to the file it points to */
455 s = concat_dir_and_file (current_panel->cwd, fname);
457 if (get_other_type () == view_listing)
458 d = concat_dir_and_file (other_panel->cwd, fname);
459 else
460 d = g_strdup (fname);
462 if (link_type == LINK_SYMLINK_RELATIVE)
463 s = diff_two_paths (other_panel->cwd, s);
465 symlink_dialog (s, d, &dest, &src);
466 g_free (d);
467 g_free (s);
469 if (!dest || !*dest || !src || !*src)
470 goto cleanup;
471 save_cwds_stat ();
472 if (-1 == mc_symlink (dest, src))
473 message (D_ERROR, MSG_ERROR, _("symlink: %s"), unix_error_string (errno));
476 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
477 repaint_screen ();
479 cleanup:
480 g_free (src);
481 g_free (dest);
484 /* --------------------------------------------------------------------------------------------- */
486 #if defined(ENABLE_VFS_UNDELFS) || defined(ENABLE_VFS_NET)
487 static void
488 nice_cd (const char *text, const char *xtext, const char *help,
489 const char *history_name, const char *prefix, int to_home)
491 char *machine;
492 char *cd_path;
494 if (!SELECTED_IS_PANEL)
495 return;
497 machine = input_dialog_help (text, xtext, help, history_name, "");
498 if (!machine)
499 return;
501 to_home = 0; /* FIXME: how to solve going to home nicely? /~/ is
502 ugly as hell and leads to problems in vfs layer */
504 if (strncmp (prefix, machine, strlen (prefix)) == 0)
505 cd_path = g_strconcat (machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
506 else
507 cd_path = g_strconcat (prefix, machine, to_home ? "/~/" : (char *) NULL, (char *) NULL);
509 if (!do_panel_cd (MENU_PANEL, cd_path, cd_parse_command))
510 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
511 g_free (cd_path);
512 g_free (machine);
514 #endif /* ENABLE_VFS_UNDELFS || ENABLE_VFS_NET */
516 /* --------------------------------------------------------------------------------------------- */
518 static void
519 configure_panel_listing (WPanel * p, int list_type, int use_msformat, char *user, char *status)
521 p->user_mini_status = use_msformat;
522 p->list_type = list_type;
524 if (list_type == list_user || use_msformat)
526 g_free (p->user_format);
527 p->user_format = user;
529 g_free (p->user_status_format[list_type]);
530 p->user_status_format[list_type] = status;
532 set_panel_formats (p);
534 else
536 g_free (user);
537 g_free (status);
540 set_panel_formats (p);
541 do_refresh ();
544 /* --------------------------------------------------------------------------------------------- */
546 static void
547 switch_to_listing (int panel_index)
549 if (get_display_type (panel_index) != view_listing)
550 set_display_type (panel_index, view_listing);
553 /* --------------------------------------------------------------------------------------------- */
554 /** Handle the tree internal listing modes switching */
556 static gboolean
557 set_basic_panel_listing_to (int panel_index, int listing_mode)
559 WPanel *p = (WPanel *) get_panel_widget (panel_index);
560 gboolean ok;
562 switch_to_listing (panel_index);
563 p->list_type = listing_mode;
565 ok = set_panel_formats (p) == 0;
567 if (ok)
568 do_refresh ();
570 return ok;
573 /* --------------------------------------------------------------------------------------------- */
574 /*** public functions ****************************************************************************/
575 /* --------------------------------------------------------------------------------------------- */
578 view_file_at_line (const char *filename, int plain_view, int internal, int start_line)
580 static const char *viewer = NULL;
581 int move_dir = 0;
583 if (plain_view)
585 int changed_hex_mode = 0;
586 int changed_nroff_flag = 0;
587 int changed_magic_flag = 0;
589 mcview_altered_hex_mode = 0;
590 mcview_altered_nroff_flag = 0;
591 mcview_altered_magic_flag = 0;
592 if (mcview_default_hex_mode)
593 changed_hex_mode = 1;
594 if (mcview_default_nroff_flag)
595 changed_nroff_flag = 1;
596 if (mcview_default_magic_flag)
597 changed_magic_flag = 1;
598 mcview_default_hex_mode = 0;
599 mcview_default_nroff_flag = 0;
600 mcview_default_magic_flag = 0;
602 switch (mcview_viewer (NULL, filename, start_line))
604 case MCVIEW_WANT_NEXT:
605 move_dir = 1;
606 break;
607 case MCVIEW_WANT_PREV:
608 move_dir = -1;
609 break;
610 default:
611 move_dir = 0;
614 if (changed_hex_mode && !mcview_altered_hex_mode)
615 mcview_default_hex_mode = 1;
616 if (changed_nroff_flag && !mcview_altered_nroff_flag)
617 mcview_default_nroff_flag = 1;
618 if (changed_magic_flag && !mcview_altered_magic_flag)
619 mcview_default_magic_flag = 1;
621 dialog_switch_process_pending ();
623 else if (internal)
625 char view_entry[BUF_TINY];
627 if (start_line != 0)
628 g_snprintf (view_entry, sizeof (view_entry), "View:%d", start_line);
629 else
630 strcpy (view_entry, "View");
632 if (regex_command (filename, view_entry, &move_dir) == 0)
634 switch (mcview_viewer (NULL, filename, start_line))
636 case MCVIEW_WANT_NEXT:
637 move_dir = 1;
638 break;
639 case MCVIEW_WANT_PREV:
640 move_dir = -1;
641 break;
642 default:
643 move_dir = 0;
646 dialog_switch_process_pending ();
649 else
651 if (viewer == NULL)
653 viewer = getenv ("VIEWER");
654 if (viewer == NULL)
655 viewer = getenv ("PAGER");
656 if (viewer == NULL)
657 viewer = "view";
660 execute_with_vfs_arg (viewer, filename);
663 return move_dir;
666 /* --------------------------------------------------------------------------------------------- */
667 /** view_file (filename, plain_view, internal)
669 * Inputs:
670 * filename: The file name to view
671 * plain_view: If set does not do any fancy pre-processing (no filtering) and
672 * always invokes the internal viewer.
673 * internal: If set uses the internal viewer, otherwise an external viewer.
677 view_file (const char *filename, int plain_view, int internal)
679 return view_file_at_line (filename, plain_view, internal, 0);
683 /* --------------------------------------------------------------------------------------------- */
684 /** Run user's preferred viewer on the currently selected file */
686 void
687 view_cmd (void)
689 do_view_cmd (0);
692 /* --------------------------------------------------------------------------------------------- */
693 /** Ask for file and run user's preferred viewer on it */
695 void
696 view_file_cmd (void)
698 char *filename;
700 filename =
701 input_expand_dialog (_("View file"), _("Filename:"),
702 MC_HISTORY_FM_VIEW_FILE, selection (current_panel)->fname);
703 if (!filename)
704 return;
706 view_file (filename, 0, use_internal_view);
707 g_free (filename);
710 /* --------------------------------------------------------------------------------------------- */
711 /** Run plain internal viewer on the currently selected file */
712 void
713 view_simple_cmd (void)
715 do_view_cmd (1);
718 /* --------------------------------------------------------------------------------------------- */
720 void
721 filtered_view_cmd (void)
723 char *command;
724 const char *initial_command;
726 if (cmdline->buffer[0] == '\0')
727 initial_command = selection (current_panel)->fname;
728 else
729 initial_command = cmdline->buffer;
731 command =
732 input_dialog (_("Filtered view"),
733 _("Filter command and arguments:"),
734 MC_HISTORY_FM_FILTERED_VIEW, initial_command);
736 if (command != NULL)
738 mcview_viewer (command, "", 0);
739 g_free (command);
740 dialog_switch_process_pending ();
744 /* --------------------------------------------------------------------------------------------- */
746 void
747 do_edit_at_line (const char *what, gboolean internal, int start_line)
749 static const char *editor = NULL;
751 #ifdef USE_INTERNAL_EDIT
752 if (internal)
753 edit_file (what, start_line);
754 else
755 #else
756 (void) start_line;
757 #endif /* USE_INTERNAL_EDIT */
759 if (editor == NULL)
761 editor = getenv ("EDITOR");
762 if (editor == NULL)
763 editor = get_default_editor ();
765 execute_with_vfs_arg (editor, what);
768 if (mc_run_mode == MC_RUN_FULL)
769 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
771 #ifdef USE_INTERNAL_EDIT
772 if (use_internal_edit)
773 dialog_switch_process_pending ();
774 else
775 #endif /* USE_INTERNAL_EDIT */
776 repaint_screen ();
779 /* --------------------------------------------------------------------------------------------- */
781 void
782 edit_cmd (void)
784 if (regex_command (selection (current_panel)->fname, "Edit", NULL) == 0)
785 do_edit (selection (current_panel)->fname);
788 /* --------------------------------------------------------------------------------------------- */
790 #ifdef USE_INTERNAL_EDIT
791 void
792 edit_cmd_force_internal (void)
794 if (regex_command (selection (current_panel)->fname, "Edit", NULL) == 0)
795 do_edit_at_line (selection (current_panel)->fname, TRUE, 0);
797 #endif
799 /* --------------------------------------------------------------------------------------------- */
801 void
802 edit_cmd_new (void)
804 #if HAVE_CHARSET
805 source_codepage = default_source_codepage;
806 #endif
807 do_edit (NULL);
810 /* --------------------------------------------------------------------------------------------- */
811 /** Invoked by F5. Copy, default to the other panel. */
813 void
814 copy_cmd (void)
816 save_cwds_stat ();
817 if (panel_operate (current_panel, OP_COPY, FALSE))
819 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
820 repaint_screen ();
824 /* --------------------------------------------------------------------------------------------- */
825 /** Invoked by F6. Move/rename, default to the other panel, ignore marks. */
827 void
828 rename_cmd (void)
830 save_cwds_stat ();
831 if (panel_operate (current_panel, OP_MOVE, FALSE))
833 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
834 repaint_screen ();
838 /* --------------------------------------------------------------------------------------------- */
839 /** Invoked by F15. Copy, default to the same panel, ignore marks. */
841 void
842 copy_cmd_local (void)
844 save_cwds_stat ();
845 if (panel_operate (current_panel, OP_COPY, TRUE))
847 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
848 repaint_screen ();
852 /* --------------------------------------------------------------------------------------------- */
853 /** Invoked by F16. Move/rename, default to the same panel. */
855 void
856 rename_cmd_local (void)
858 save_cwds_stat ();
859 if (panel_operate (current_panel, OP_MOVE, TRUE))
861 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
862 repaint_screen ();
866 /* --------------------------------------------------------------------------------------------- */
868 void
869 mkdir_cmd (void)
871 char *dir, *absdir;
872 const char *name = "";
874 /* If 'on' then automatically fills name with current selected item name */
875 if (auto_fill_mkdir_name && strcmp (selection (current_panel)->fname, "..") != 0)
876 name = selection (current_panel)->fname;
878 dir =
879 input_expand_dialog (_("Create a new Directory"),
880 _("Enter directory name:"), MC_HISTORY_FM_MKDIR, name);
882 if (!dir)
883 return;
885 if (*dir)
887 if (dir[0] == '/' || dir[0] == '~')
888 absdir = g_strdup (dir);
889 else
890 absdir = concat_dir_and_file (current_panel->cwd, dir);
892 save_cwds_stat ();
893 if (my_mkdir (absdir, 0777) == 0)
895 update_panels (UP_OPTIMIZE, dir);
896 repaint_screen ();
897 select_item (current_panel);
899 else
901 message (D_ERROR, MSG_ERROR, "%s", unix_error_string (errno));
903 g_free (absdir);
905 g_free (dir);
908 /* --------------------------------------------------------------------------------------------- */
910 void
911 delete_cmd (void)
913 save_cwds_stat ();
915 if (panel_operate (current_panel, OP_DELETE, FALSE))
917 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
918 repaint_screen ();
922 /* --------------------------------------------------------------------------------------------- */
923 /** Invoked by F18. Remove selected file, regardless of marked files. */
925 void
926 delete_cmd_local (void)
928 save_cwds_stat ();
930 if (panel_operate (current_panel, OP_DELETE, TRUE))
932 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
933 repaint_screen ();
937 /* --------------------------------------------------------------------------------------------- */
939 void
940 find_cmd (void)
942 do_find ();
945 /* --------------------------------------------------------------------------------------------- */
946 /** Invoked from the left/right menus */
948 void
949 filter_cmd (void)
951 WPanel *p;
953 if (!SELECTED_IS_PANEL)
954 return;
956 p = MENU_PANEL;
957 set_panel_filter (p);
960 /* --------------------------------------------------------------------------------------------- */
962 void
963 reread_cmd (void)
965 panel_update_flags_t flag = UP_ONLY_CURRENT;
967 if (get_current_type () == view_listing && get_other_type () == view_listing
968 && strcmp (current_panel->cwd, other_panel->cwd) == 0)
969 flag = UP_OPTIMIZE;
971 update_panels (UP_RELOAD | flag, UP_KEEPSEL);
972 repaint_screen ();
975 /* --------------------------------------------------------------------------------------------- */
977 void
978 reverse_selection_cmd (void)
980 int i;
981 file_entry *file;
983 for (i = 0; i < current_panel->count; i++)
985 file = &current_panel->dir.list[i];
986 if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
987 do_file_mark (current_panel, i, !file->f.marked);
991 /* --------------------------------------------------------------------------------------------- */
993 void
994 select_cmd (void)
996 select_unselect_cmd (_("Select"), ":select_cmd: Select ", TRUE);
999 /* --------------------------------------------------------------------------------------------- */
1001 void
1002 unselect_cmd (void)
1004 select_unselect_cmd (_("Unselect"), ":unselect_cmd: Unselect ", FALSE);
1007 /* --------------------------------------------------------------------------------------------- */
1009 void
1010 ext_cmd (void)
1012 char *buffer;
1013 char *extdir;
1014 int dir;
1016 dir = 0;
1017 if (geteuid () == 0)
1019 dir = query_dialog (_("Extension file edit"),
1020 _("Which extension file you want to edit?"), D_NORMAL, 2,
1021 _("&User"), _("&System Wide"));
1023 extdir = concat_dir_and_file (mc_sysconfig_dir, MC_LIB_EXT);
1025 if (dir == 0)
1027 buffer = g_build_filename (mc_config_get_data_path (), MC_FILEBIND_FILE, NULL);
1028 check_for_default (extdir, buffer);
1029 do_edit (buffer);
1030 g_free (buffer);
1032 else if (dir == 1)
1034 if (!exist_file (extdir))
1036 g_free (extdir);
1037 extdir = concat_dir_and_file (mc_share_data_dir, MC_LIB_EXT);
1039 do_edit (extdir);
1041 g_free (extdir);
1042 flush_extension_file ();
1045 /* --------------------------------------------------------------------------------------------- */
1046 /** edit file menu for mc */
1048 void
1049 edit_mc_menu_cmd (void)
1051 char *buffer;
1052 char *menufile;
1053 int dir = 0;
1055 dir = query_dialog (_("Menu edit"),
1056 _("Which menu file do you want to edit?"),
1057 D_NORMAL, geteuid ()? 2 : 3, _("&Local"), _("&User"), _("&System Wide"));
1059 menufile = concat_dir_and_file (mc_sysconfig_dir, MC_GLOBAL_MENU);
1061 if (!exist_file (menufile))
1063 g_free (menufile);
1064 menufile = concat_dir_and_file (mc_share_data_dir, MC_GLOBAL_MENU);
1067 switch (dir)
1069 case 0:
1070 buffer = g_strdup (MC_LOCAL_MENU);
1071 check_for_default (menufile, buffer);
1072 chmod (buffer, 0600);
1073 break;
1075 case 1:
1076 buffer = g_build_filename (mc_config_get_data_path (), MC_USERMENU_FILE, NULL);
1077 check_for_default (menufile, buffer);
1078 break;
1080 case 2:
1081 buffer = concat_dir_and_file (mc_sysconfig_dir, MC_GLOBAL_MENU);
1082 if (!exist_file (buffer))
1084 g_free (buffer);
1085 buffer = concat_dir_and_file (mc_share_data_dir, MC_GLOBAL_MENU);
1087 break;
1089 default:
1090 g_free (menufile);
1091 return;
1094 do_edit (buffer);
1096 g_free (buffer);
1097 g_free (menufile);
1100 /* --------------------------------------------------------------------------------------------- */
1102 void
1103 edit_fhl_cmd (void)
1105 char *buffer = NULL;
1106 char *fhlfile = NULL;
1108 int dir;
1110 dir = 0;
1111 if (geteuid () == 0)
1113 dir = query_dialog (_("Highlighting groups file edit"),
1114 _("Which highlighting file you want to edit?"), D_NORMAL, 2,
1115 _("&User"), _("&System Wide"));
1117 fhlfile = concat_dir_and_file (mc_sysconfig_dir, MC_FHL_INI_FILE);
1119 if (dir == 0)
1121 buffer = g_build_filename (mc_config_get_path (), MC_FHL_INI_FILE, NULL);
1122 check_for_default (fhlfile, buffer);
1123 do_edit (buffer);
1124 g_free (buffer);
1126 else if (dir == 1)
1128 if (!exist_file (fhlfile))
1130 g_free (fhlfile);
1131 fhlfile = concat_dir_and_file (mc_sysconfig_dir, MC_FHL_INI_FILE);
1133 do_edit (fhlfile);
1135 g_free (fhlfile);
1137 /* refresh highlighting rules */
1138 mc_fhl_free (&mc_filehighlight);
1139 mc_filehighlight = mc_fhl_new (TRUE);
1142 /* --------------------------------------------------------------------------------------------- */
1144 void
1145 quick_chdir_cmd (void)
1147 char *target;
1149 target = hotlist_cmd (LIST_HOTLIST);
1150 if (!target)
1151 return;
1153 if (get_current_type () == view_tree)
1154 tree_chdir (the_tree, target);
1155 else
1157 char *cmd = g_strconcat ("cd ", target, (char *) NULL);
1158 do_cd_command (cmd);
1159 g_free (cmd);
1161 g_free (target);
1164 #ifdef ENABLE_VFS
1165 void
1166 reselect_vfs (void)
1168 char *target;
1170 target = hotlist_cmd (LIST_VFSLIST);
1171 if (!target)
1172 return;
1174 if (!do_cd (target, cd_exact))
1175 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
1176 g_free (target);
1178 #endif /* ENABLE_VFS */
1180 /* --------------------------------------------------------------------------------------------- */
1182 void
1183 compare_dirs_cmd (void)
1185 int choice;
1186 enum CompareMode thorough_flag;
1188 choice =
1189 query_dialog (_("Compare directories"),
1190 _("Select compare method:"), D_NORMAL, 4,
1191 _("&Quick"), _("&Size only"), _("&Thorough"), _("&Cancel"));
1193 if (choice < 0 || choice > 2)
1194 return;
1196 thorough_flag = choice;
1198 if (get_current_type () == view_listing && get_other_type () == view_listing)
1200 compare_dir (current_panel, other_panel, thorough_flag);
1201 compare_dir (other_panel, current_panel, thorough_flag);
1203 else
1205 message (D_ERROR, MSG_ERROR,
1206 _("Both panels should be in the listing mode\nto use this command"));
1210 /* --------------------------------------------------------------------------------------------- */
1212 #ifdef USE_DIFF_VIEW
1213 void
1214 diff_view_cmd (void)
1216 dview_diff_cmd ();
1218 if (mc_run_mode == MC_RUN_FULL)
1219 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1221 dialog_switch_process_pending ();
1223 #endif
1225 /* --------------------------------------------------------------------------------------------- */
1227 void
1228 history_cmd (void)
1230 /* show the history of command line widget */
1231 send_message (&cmdline->widget, WIDGET_COMMAND, CK_InputHistoryShow);
1234 /* --------------------------------------------------------------------------------------------- */
1236 void
1237 swap_cmd (void)
1239 swap_panels ();
1240 tty_touch_screen ();
1241 repaint_screen ();
1244 /* --------------------------------------------------------------------------------------------- */
1246 void
1247 view_other_cmd (void)
1249 static int message_flag = TRUE;
1251 if (!xterm_flag && !console_flag && !use_subshell && !output_starts_shell)
1253 if (message_flag)
1254 message (D_ERROR, MSG_ERROR,
1255 _("Not an xterm or Linux console;\nthe panels cannot be toggled."));
1256 message_flag = FALSE;
1258 else
1260 toggle_panels ();
1264 /* --------------------------------------------------------------------------------------------- */
1266 void
1267 link_cmd (link_type_t link_type)
1269 char *filename = selection (current_panel)->fname;
1271 if (filename != NULL)
1272 do_link (link_type, filename);
1275 /* --------------------------------------------------------------------------------------------- */
1277 void
1278 edit_symlink_cmd (void)
1280 if (S_ISLNK (selection (current_panel)->st.st_mode))
1282 char buffer[MC_MAXPATHLEN];
1283 char *p = NULL;
1284 int i;
1285 char *dest, *q;
1287 p = selection (current_panel)->fname;
1289 q = g_strdup_printf (_("Symlink `%s\' points to:"), str_trunc (p, 32));
1291 i = readlink (p, buffer, MC_MAXPATHLEN - 1);
1292 if (i > 0)
1294 buffer[i] = 0;
1295 dest = input_expand_dialog (_("Edit symlink"), q, MC_HISTORY_FM_EDIT_LINK, buffer);
1296 if (dest)
1298 if (*dest && strcmp (buffer, dest))
1300 save_cwds_stat ();
1301 if (-1 == mc_unlink (p))
1303 message (D_ERROR, MSG_ERROR, _("edit symlink, unable to remove %s: %s"),
1304 p, unix_error_string (errno));
1306 else
1308 if (-1 == mc_symlink (dest, p))
1309 message (D_ERROR, MSG_ERROR, _("edit symlink: %s"),
1310 unix_error_string (errno));
1312 update_panels (UP_OPTIMIZE, UP_KEEPSEL);
1313 repaint_screen ();
1315 g_free (dest);
1318 g_free (q);
1320 else
1322 message (D_ERROR, MSG_ERROR, _("`%s' is not a symbolic link"),
1323 selection (current_panel)->fname);
1327 /* --------------------------------------------------------------------------------------------- */
1329 void
1330 help_cmd (void)
1332 if (current_panel->searching)
1333 interactive_display (NULL, "[Quick search]");
1334 else
1335 interactive_display (NULL, "[main]");
1338 /* --------------------------------------------------------------------------------------------- */
1340 void
1341 user_file_menu_cmd (void)
1343 (void) user_menu_cmd (NULL);
1346 /* --------------------------------------------------------------------------------------------- */
1348 * Return a random hint. If force is not 0, ignore the timeout.
1351 char *
1352 get_random_hint (int force)
1354 char *data, *result = NULL, *eol;
1355 int len;
1356 int start;
1357 static int last_sec;
1358 static struct timeval tv;
1359 GIConv conv;
1360 GString *buffer;
1362 /* Do not change hints more often than one minute */
1363 gettimeofday (&tv, NULL);
1364 if (!force && !(tv.tv_sec > last_sec + 60))
1365 return g_strdup ("");
1366 last_sec = tv.tv_sec;
1368 data = load_mc_home_file (mc_share_data_dir, MC_HINT, NULL);
1369 if (data == NULL)
1370 return NULL;
1372 /* get a random entry */
1373 srand (tv.tv_sec);
1374 len = strlen (data);
1375 start = rand () % len;
1377 for (; start != 0; start--)
1378 if (data[start] == '\n')
1380 start++;
1381 break;
1384 eol = strchr (data + start, '\n');
1385 if (eol != NULL)
1386 *eol = '\0';
1388 /* hint files are stored in utf-8 */
1389 /* try convert hint file from utf-8 to terminal encoding */
1390 conv = str_crt_conv_from ("UTF-8");
1391 if (conv != INVALID_CONV)
1393 buffer = g_string_new ("");
1394 if (str_convert (conv, &data[start], buffer) != ESTR_FAILURE)
1395 result = g_strdup (buffer->str);
1396 g_string_free (buffer, TRUE);
1397 str_close_conv (conv);
1400 g_free (data);
1401 return result;
1404 /* --------------------------------------------------------------------------------------------- */
1406 #ifdef ENABLE_VFS_NET
1407 #ifdef ENABLE_VFS_FTP
1408 void
1409 ftplink_cmd (void)
1411 nice_cd (_("FTP to machine"), _(machine_str),
1412 "[FTP File System]", ":ftplink_cmd: FTP to machine ", "/#ftp:", 1);
1414 #endif /* ENABLE_VFS_FTP */
1416 /* --------------------------------------------------------------------------------------------- */
1418 #ifdef ENABLE_VFS_FISH
1419 void
1420 fishlink_cmd (void)
1422 nice_cd (_("Shell link to machine"), _(machine_str),
1423 "[FIle transfer over SHell filesystem]", ":fishlink_cmd: Shell link to machine ",
1424 "/#sh:", 1);
1426 #endif /* ENABLE_VFS_FISH */
1428 /* --------------------------------------------------------------------------------------------- */
1430 #ifdef ENABLE_VFS_SMB
1431 void
1432 smblink_cmd (void)
1434 nice_cd (_("SMB link to machine"), _(machine_str),
1435 "[SMB File System]", ":smblink_cmd: SMB link to machine ", "/#smb:", 0);
1437 #endif /* ENABLE_VFS_SMB */
1438 #endif /* ENABLE_VFS_NET */
1440 /* --------------------------------------------------------------------------------------------- */
1442 #ifdef ENABLE_VFS_UNDELFS
1443 void
1444 undelete_cmd (void)
1446 nice_cd (_("Undelete files on an ext2 file system"),
1447 _("Enter device (without /dev/) to undelete\nfiles on: (F1 for details)"),
1448 "[Undelete File System]", ":undelete_cmd: Undel on ext2 fs ", "/#undel:", 0);
1450 #endif /* ENABLE_VFS_UNDELFS */
1452 /* --------------------------------------------------------------------------------------------- */
1454 void
1455 quick_cd_cmd (void)
1457 char *p = cd_dialog ();
1459 if (p && *p)
1461 char *q = g_strconcat ("cd ", p, (char *) NULL);
1463 do_cd_command (q);
1464 g_free (q);
1466 g_free (p);
1469 /* --------------------------------------------------------------------------------------------- */
1471 \brief calculate dirs sizes
1473 calculate dirs sizes and resort panel:
1474 dirs_selected = show size for selected dirs,
1475 otherwise = show size for dir under cursor:
1476 dir under cursor ".." = show size for all dirs,
1477 otherwise = show size for dir under cursor
1480 void
1481 smart_dirsize_cmd (void)
1483 WPanel *panel = current_panel;
1484 file_entry *entry;
1486 entry = &(panel->dir.list[panel->selected]);
1487 if ((S_ISDIR (entry->st.st_mode) && (strcmp (entry->fname, "..") == 0)) || panel->dirs_marked)
1488 dirsizes_cmd ();
1489 else
1490 single_dirsize_cmd ();
1493 /* --------------------------------------------------------------------------------------------- */
1495 void
1496 single_dirsize_cmd (void)
1498 WPanel *panel = current_panel;
1499 file_entry *entry;
1501 entry = &(panel->dir.list[panel->selected]);
1502 if (S_ISDIR (entry->st.st_mode) && strcmp (entry->fname, "..") != 0)
1504 size_t marked = 0;
1505 uintmax_t total = 0;
1506 ComputeDirSizeUI *ui;
1508 ui = compute_dir_size_create_ui ();
1510 if (compute_dir_size (entry->fname, ui, compute_dir_size_update_ui,
1511 &marked, &total, TRUE) == FILE_CONT)
1513 entry->st.st_size = (off_t) total;
1514 entry->f.dir_size_computed = 1;
1517 compute_dir_size_destroy_ui (ui);
1520 if (panels_options.mark_moves_down)
1521 send_message ((Widget *) panel, WIDGET_COMMAND, CK_PanelMoveDown);
1523 recalculate_panel_summary (panel);
1525 if (current_panel->sort_info.sort_field->sort_routine == (sortfn *) sort_size)
1526 panel_re_sort (panel);
1528 panel->dirty = 1;
1531 /* --------------------------------------------------------------------------------------------- */
1533 void
1534 dirsizes_cmd (void)
1536 WPanel *panel = current_panel;
1537 int i;
1538 ComputeDirSizeUI *ui;
1540 ui = compute_dir_size_create_ui ();
1542 for (i = 0; i < panel->count; i++)
1543 if (S_ISDIR (panel->dir.list[i].st.st_mode)
1544 && ((panel->dirs_marked && panel->dir.list[i].f.marked)
1545 || !panel->dirs_marked) && strcmp (panel->dir.list[i].fname, "..") != 0)
1547 size_t marked = 0;
1548 uintmax_t total = 0;
1550 if (compute_dir_size (panel->dir.list[i].fname,
1551 ui, compute_dir_size_update_ui, &marked, &total,
1552 TRUE) != FILE_CONT)
1553 break;
1555 panel->dir.list[i].st.st_size = (off_t) total;
1556 panel->dir.list[i].f.dir_size_computed = 1;
1559 compute_dir_size_destroy_ui (ui);
1561 recalculate_panel_summary (panel);
1563 if (current_panel->sort_info.sort_field->sort_routine == (sortfn *) sort_size)
1564 panel_re_sort (panel);
1566 panel->dirty = 1;
1569 /* --------------------------------------------------------------------------------------------- */
1571 void
1572 save_setup_cmd (void)
1574 if (save_setup (TRUE, TRUE))
1575 message (D_NORMAL, _("Setup"), _("Setup saved to %s%s%s"),
1576 mc_config_get_path (), PATH_SEP_STR, MC_CONFIG_FILE);
1579 /* --------------------------------------------------------------------------------------------- */
1581 void
1582 info_cmd_no_menu (void)
1584 if (get_display_type (0) == view_info)
1585 set_display_type (0, view_listing);
1586 else if (get_display_type (1) == view_info)
1587 set_display_type (1, view_listing);
1588 else
1589 set_display_type (current_panel == left_panel ? 1 : 0, view_info);
1592 /* --------------------------------------------------------------------------------------------- */
1594 void
1595 quick_cmd_no_menu (void)
1597 if (get_display_type (0) == view_quick)
1598 set_display_type (0, view_listing);
1599 else if (get_display_type (1) == view_quick)
1600 set_display_type (1, view_listing);
1601 else
1602 set_display_type (current_panel == left_panel ? 1 : 0, view_quick);
1605 /* --------------------------------------------------------------------------------------------- */
1607 void
1608 listing_cmd (void)
1610 switch_to_listing (MENU_PANEL_IDX);
1613 /* --------------------------------------------------------------------------------------------- */
1615 void
1616 change_listing_cmd (void)
1618 int list_type;
1619 int use_msformat;
1620 char *user, *status;
1621 WPanel *p = NULL;
1623 if (get_display_type (MENU_PANEL_IDX) == view_listing)
1624 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1626 list_type = display_box (p, &user, &status, &use_msformat, MENU_PANEL_IDX);
1628 if (list_type != -1)
1630 switch_to_listing (MENU_PANEL_IDX);
1631 p = MENU_PANEL_IDX == 0 ? left_panel : right_panel;
1632 configure_panel_listing (p, list_type, use_msformat, user, status);
1636 /* --------------------------------------------------------------------------------------------- */
1638 void
1639 tree_cmd (void)
1641 set_display_type (MENU_PANEL_IDX, view_tree);
1644 /* --------------------------------------------------------------------------------------------- */
1646 void
1647 info_cmd (void)
1649 set_display_type (MENU_PANEL_IDX, view_info);
1652 /* --------------------------------------------------------------------------------------------- */
1654 void
1655 quick_view_cmd (void)
1657 if ((WPanel *) get_panel_widget (MENU_PANEL_IDX) == current_panel)
1658 change_panel ();
1659 set_display_type (MENU_PANEL_IDX, view_quick);
1662 /* --------------------------------------------------------------------------------------------- */
1664 void
1665 toggle_listing_cmd (void)
1667 int current = get_current_index ();
1668 WPanel *p = (WPanel *) get_panel_widget (current);
1670 set_basic_panel_listing_to (current, (p->list_type + 1) % LIST_TYPES);
1673 /* --------------------------------------------------------------------------------------------- */
1675 void
1676 encoding_cmd (void)
1678 if (SELECTED_IS_PANEL)
1679 panel_change_encoding (MENU_PANEL);
1682 /* --------------------------------------------------------------------------------------------- */