changed interface of function mc_setctl() for handle vfs_path_t object as parameter
[midnight-commander.git] / src / filemanager / panel.c
blobe217692067fd35172841fbedc5a23ae0b21fc3c7
1 /*
2 Panel managing.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Miguel de Icaza, 1995
10 Timur Bakeyev, 1997, 1999
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file panel.c
29 * \brief Source: panel managin module
32 #include <config.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h" /* For Gpm_Event */
44 #include "lib/tty/key.h" /* XCTRL and ALT macros */
45 #include "lib/skin.h"
46 #include "lib/strescape.h"
47 #include "lib/filehighlight.h"
48 #include "lib/mcconfig.h"
49 #include "lib/vfs/vfs.h"
50 #include "lib/unixcompat.h"
51 #include "lib/timefmt.h"
52 #include "lib/util.h"
53 #include "lib/widget.h"
54 #ifdef HAVE_CHARSET
55 #include "lib/charsets.h" /* get_codepage_id () */
56 #endif
57 #include "lib/event.h"
59 #include "src/setup.h" /* For loading/saving panel options */
60 #include "src/execute.h"
61 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
62 #include "src/keybind-defaults.h" /* global_keymap_t */
63 #include "src/subshell.h" /* do_subshell_chdir() */
65 #include "dir.h"
66 #include "boxes.h"
67 #include "tree.h"
68 #include "ext.h" /* regexp_command */
69 #include "layout.h" /* Most layout variables are here */
70 #include "cmd.h"
71 #include "command.h" /* cmdline */
72 #include "usermenu.h"
73 #include "midnight.h"
74 #include "mountlist.h" /* my_statfs */
76 #include "panel.h"
78 /*** global variables ****************************************************************************/
80 /* The hook list for the select file function */
81 hook_t *select_file_hook = NULL;
83 panelized_panel_t panelized_panel = { { NULL, 0 }, -1, { '\0' } };
85 static const char *string_file_name (file_entry *, int);
86 static const char *string_file_size (file_entry *, int);
87 static const char *string_file_size_brief (file_entry *, int);
88 static const char *string_file_type (file_entry *, int);
89 static const char *string_file_mtime (file_entry *, int);
90 static const char *string_file_atime (file_entry *, int);
91 static const char *string_file_ctime (file_entry *, int);
92 static const char *string_file_permission (file_entry *, int);
93 static const char *string_file_perm_octal (file_entry *, int);
94 static const char *string_file_nlinks (file_entry *, int);
95 static const char *string_inode (file_entry *, int);
96 static const char *string_file_nuid (file_entry *, int);
97 static const char *string_file_ngid (file_entry *, int);
98 static const char *string_file_owner (file_entry *, int);
99 static const char *string_file_group (file_entry *, int);
100 static const char *string_marked (file_entry *, int);
101 static const char *string_space (file_entry *, int);
102 static const char *string_dot (file_entry *, int);
104 /* *INDENT-OFF* */
105 panel_field_t panel_fields[] = {
107 "unsorted", 12, 1, J_LEFT_FIT,
108 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
109 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
110 N_("sort|u"),
111 N_("&Unsorted"), TRUE, FALSE,
112 string_file_name,
113 (sortfn *) unsorted
117 "name", 12, 1, J_LEFT_FIT,
118 /* TRANSLATORS: one single character to represent 'name' sort mode */
119 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
120 N_("sort|n"),
121 N_("&Name"), TRUE, TRUE,
122 string_file_name,
123 (sortfn *) sort_name
127 "version", 12, 1, J_LEFT_FIT,
128 /* TRANSLATORS: one single character to represent 'version' sort mode */
129 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
130 N_("sort|v"),
131 N_("&Version"), TRUE, FALSE,
132 string_file_name,
133 (sortfn *) sort_vers
137 "extension", 12, 1, J_LEFT_FIT,
138 /* TRANSLATORS: one single character to represent 'extension' sort mode */
139 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
140 N_("sort|e"),
141 N_("&Extension"), TRUE, FALSE,
142 string_file_name, /* TODO: string_file_ext */
143 (sortfn *) sort_ext
147 "size", 7, 0, J_RIGHT,
148 /* TRANSLATORS: one single character to represent 'size' sort mode */
149 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
150 N_("sort|s"),
151 N_("&Size"), TRUE, TRUE,
152 string_file_size,
153 (sortfn *) sort_size
157 "bsize", 7, 0, J_RIGHT,
159 N_("Block Size"), FALSE, FALSE,
160 string_file_size_brief,
161 (sortfn *) sort_size
165 "type", 1, 0, J_LEFT,
167 "", FALSE, TRUE,
168 string_file_type,
169 NULL
173 "mtime", 12, 0, J_RIGHT,
174 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
175 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
176 N_("sort|m"),
177 N_("&Modify time"), TRUE, TRUE,
178 string_file_mtime,
179 (sortfn *) sort_time
183 "atime", 12, 0, J_RIGHT,
184 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
185 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
186 N_("sort|a"),
187 N_("&Access time"), TRUE, TRUE,
188 string_file_atime,
189 (sortfn *) sort_atime
193 "ctime", 12, 0, J_RIGHT,
194 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
195 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
196 N_("sort|h"),
197 N_("C&hange time"), TRUE, TRUE,
198 string_file_ctime,
199 (sortfn *) sort_ctime
203 "perm", 10, 0, J_LEFT,
205 N_("Permission"), FALSE, TRUE,
206 string_file_permission,
207 NULL
211 "mode", 6, 0, J_RIGHT,
213 N_("Perm"), FALSE, TRUE,
214 string_file_perm_octal,
215 NULL
219 "nlink", 2, 0, J_RIGHT,
221 N_("Nl"), FALSE, TRUE,
222 string_file_nlinks, NULL
226 "inode", 5, 0, J_RIGHT,
227 /* TRANSLATORS: one single character to represent 'inode' sort mode */
228 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
229 N_("sort|i"),
230 N_("&Inode"), TRUE, TRUE,
231 string_inode,
232 (sortfn *) sort_inode
236 "nuid", 5, 0, J_RIGHT,
238 N_("UID"), FALSE, FALSE,
239 string_file_nuid,
240 NULL
244 "ngid", 5, 0, J_RIGHT,
246 N_("GID"), FALSE, FALSE,
247 string_file_ngid,
248 NULL
252 "owner", 8, 0, J_LEFT_FIT,
254 N_("Owner"), FALSE, TRUE,
255 string_file_owner,
256 NULL
260 "group", 8, 0, J_LEFT_FIT,
262 N_("Group"), FALSE, TRUE,
263 string_file_group,
264 NULL
268 "mark", 1, 0, J_RIGHT,
270 " ", FALSE, TRUE,
271 string_marked,
272 NULL
276 "|", 1, 0, J_RIGHT,
278 " ", FALSE, TRUE,
279 NULL,
280 NULL
284 "space", 1, 0, J_RIGHT,
286 " ", FALSE, TRUE,
287 string_space,
288 NULL
292 "dot", 1, 0, J_RIGHT,
294 " ", FALSE, FALSE,
295 string_dot,
296 NULL
300 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
303 /* *INDENT-ON* */
305 extern int saving_setup;
307 /*** file scope macro definitions ****************************************************************/
309 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
311 #define NORMAL 0
312 #define SELECTED 1
313 #define MARKED 2
314 #define MARKED_SELECTED 3
315 #define STATUS 5
317 /* This macro extracts the number of available lines in a panel */
318 #define llines(p) (p->widget.lines - 3 - (panels_options.show_mini_info ? 2 : 0))
320 /*** file scope type declarations ****************************************************************/
322 typedef enum
324 MARK_DONT_MOVE = 0,
325 MARK_DOWN = 1,
326 MARK_FORCE_DOWN = 2,
327 MARK_FORCE_UP = 3
328 } mark_act_t;
331 * This describes a format item. The parse_display_format routine parses
332 * the user specified format and creates a linked list of format_e structures.
334 typedef struct format_e
336 struct format_e *next;
337 int requested_field_len;
338 int field_len;
339 align_crt_t just_mode;
340 int expand;
341 const char *(*string_fn) (file_entry *, int len);
342 char *title;
343 const char *id;
344 } format_e;
346 /*** file scope variables ************************************************************************/
348 static char *panel_sort_up_sign = NULL;
349 static char *panel_sort_down_sign = NULL;
351 static char *panel_hiddenfiles_sign_show = NULL;
352 static char *panel_hiddenfiles_sign_hide = NULL;
353 static char *panel_history_prev_item_sign = NULL;
354 static char *panel_history_next_item_sign = NULL;
355 static char *panel_history_show_list_sign = NULL;
357 /* Panel that selection started */
358 static WPanel *mouse_mark_panel = NULL;
360 static int mouse_marking = 0;
361 static int state_mark = 0;
362 /*** file scope functions ************************************************************************/
363 /* --------------------------------------------------------------------------------------------- */
365 static void
366 set_colors (WPanel * panel)
368 (void) panel;
369 tty_set_normal_attrs ();
370 tty_setcolor (NORMAL_COLOR);
373 /* --------------------------------------------------------------------------------------------- */
374 /** Delete format string, it is a linked list */
376 static void
377 delete_format (format_e * format)
379 while (format != NULL)
381 format_e *next = format->next;
382 g_free (format->title);
383 g_free (format);
384 format = next;
388 /* --------------------------------------------------------------------------------------------- */
389 /** This code relies on the default justification!!! */
391 static void
392 add_permission_string (char *dest, int width, file_entry * fe, int attr, int color, int is_octal)
394 int i, r, l;
396 l = get_user_permissions (&fe->st);
398 if (is_octal)
400 /* Place of the access bit in octal mode */
401 l = width + l - 3;
402 r = l + 1;
404 else
406 /* The same to the triplet in string mode */
407 l = l * 3 + 1;
408 r = l + 3;
411 for (i = 0; i < width; i++)
413 if (i >= l && i < r)
415 if (attr == SELECTED || attr == MARKED_SELECTED)
416 tty_setcolor (MARKED_SELECTED_COLOR);
417 else
418 tty_setcolor (MARKED_COLOR);
420 else if (color >= 0)
421 tty_setcolor (color);
422 else
423 tty_lowlevel_setcolor (-color);
425 tty_print_char (dest[i]);
429 /* --------------------------------------------------------------------------------------------- */
430 /** String representations of various file attributes name */
432 static const char *
433 string_file_name (file_entry * fe, int len)
435 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
437 (void) len;
438 g_strlcpy (buffer, fe->fname, sizeof (buffer));
439 return buffer;
442 /* --------------------------------------------------------------------------------------------- */
444 static unsigned int
445 ilog10 (dev_t n)
447 unsigned int digits = 0;
450 digits++, n /= 10;
452 while (n != 0);
453 return digits;
456 /* --------------------------------------------------------------------------------------------- */
458 static void
459 format_device_number (char *buf, size_t bufsize, dev_t dev)
461 dev_t major_dev = major (dev);
462 dev_t minor_dev = minor (dev);
463 unsigned int major_digits = ilog10 (major_dev);
464 unsigned int minor_digits = ilog10 (minor_dev);
466 g_assert (bufsize >= 1);
467 if (major_digits + 1 + minor_digits + 1 <= bufsize)
469 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
471 else
473 g_strlcpy (buf, _("[dev]"), bufsize);
477 /* --------------------------------------------------------------------------------------------- */
478 /** size */
480 static const char *
481 string_file_size (file_entry * fe, int len)
483 static char buffer[BUF_TINY];
485 /* Don't ever show size of ".." since we don't calculate it */
486 if (!strcmp (fe->fname, ".."))
488 return _("UP--DIR");
491 #ifdef HAVE_STRUCT_STAT_ST_RDEV
492 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
493 format_device_number (buffer, len + 1, fe->st.st_rdev);
494 else
495 #endif
497 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
499 return buffer;
502 /* --------------------------------------------------------------------------------------------- */
503 /** bsize */
505 static const char *
506 string_file_size_brief (file_entry * fe, int len)
508 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
510 return _("SYMLINK");
513 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
515 return _("SUB-DIR");
518 return string_file_size (fe, len);
521 /* --------------------------------------------------------------------------------------------- */
522 /** This functions return a string representation of a file entry type */
524 static const char *
525 string_file_type (file_entry * fe, int len)
527 static char buffer[2];
529 (void) len;
530 if (S_ISDIR (fe->st.st_mode))
531 buffer[0] = PATH_SEP;
532 else if (S_ISLNK (fe->st.st_mode))
534 if (fe->f.link_to_dir)
535 buffer[0] = '~';
536 else if (fe->f.stale_link)
537 buffer[0] = '!';
538 else
539 buffer[0] = '@';
541 else if (S_ISCHR (fe->st.st_mode))
542 buffer[0] = '-';
543 else if (S_ISSOCK (fe->st.st_mode))
544 buffer[0] = '=';
545 else if (S_ISDOOR (fe->st.st_mode))
546 buffer[0] = '>';
547 else if (S_ISBLK (fe->st.st_mode))
548 buffer[0] = '+';
549 else if (S_ISFIFO (fe->st.st_mode))
550 buffer[0] = '|';
551 else if (S_ISNAM (fe->st.st_mode))
552 buffer[0] = '#';
553 else if (!S_ISREG (fe->st.st_mode))
554 buffer[0] = '?'; /* non-regular of unknown kind */
555 else if (is_exe (fe->st.st_mode))
556 buffer[0] = '*';
557 else
558 buffer[0] = ' ';
559 buffer[1] = '\0';
560 return buffer;
563 /* --------------------------------------------------------------------------------------------- */
564 /** mtime */
566 static const char *
567 string_file_mtime (file_entry * fe, int len)
569 (void) len;
570 return file_date (fe->st.st_mtime);
573 /* --------------------------------------------------------------------------------------------- */
574 /** atime */
576 static const char *
577 string_file_atime (file_entry * fe, int len)
579 (void) len;
580 return file_date (fe->st.st_atime);
583 /* --------------------------------------------------------------------------------------------- */
584 /** ctime */
586 static const char *
587 string_file_ctime (file_entry * fe, int len)
589 (void) len;
590 return file_date (fe->st.st_ctime);
593 /* --------------------------------------------------------------------------------------------- */
594 /** perm */
596 static const char *
597 string_file_permission (file_entry * fe, int len)
599 (void) len;
600 return string_perm (fe->st.st_mode);
603 /* --------------------------------------------------------------------------------------------- */
604 /** mode */
606 static const char *
607 string_file_perm_octal (file_entry * fe, int len)
609 static char buffer[10];
611 (void) len;
612 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
613 return buffer;
616 /* --------------------------------------------------------------------------------------------- */
617 /** nlink */
619 static const char *
620 string_file_nlinks (file_entry * fe, int len)
622 static char buffer[BUF_TINY];
624 (void) len;
625 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
626 return buffer;
629 /* --------------------------------------------------------------------------------------------- */
630 /** inode */
632 static const char *
633 string_inode (file_entry * fe, int len)
635 static char buffer[10];
637 (void) len;
638 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
639 return buffer;
642 /* --------------------------------------------------------------------------------------------- */
643 /** nuid */
645 static const char *
646 string_file_nuid (file_entry * fe, int len)
648 static char buffer[10];
650 (void) len;
651 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
652 return buffer;
655 /* --------------------------------------------------------------------------------------------- */
656 /** ngid */
658 static const char *
659 string_file_ngid (file_entry * fe, int len)
661 static char buffer[10];
663 (void) len;
664 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
665 return buffer;
668 /* --------------------------------------------------------------------------------------------- */
669 /** owner */
671 static const char *
672 string_file_owner (file_entry * fe, int len)
674 (void) len;
675 return get_owner (fe->st.st_uid);
678 /* --------------------------------------------------------------------------------------------- */
679 /** group */
681 static const char *
682 string_file_group (file_entry * fe, int len)
684 (void) len;
685 return get_group (fe->st.st_gid);
688 /* --------------------------------------------------------------------------------------------- */
689 /** mark */
691 static const char *
692 string_marked (file_entry * fe, int len)
694 (void) len;
695 return fe->f.marked ? "*" : " ";
698 /* --------------------------------------------------------------------------------------------- */
699 /** space */
701 static const char *
702 string_space (file_entry * fe, int len)
704 (void) fe;
705 (void) len;
706 return " ";
709 /* --------------------------------------------------------------------------------------------- */
710 /** dot */
712 static const char *
713 string_dot (file_entry * fe, int len)
715 (void) fe;
716 (void) len;
717 return ".";
720 /* --------------------------------------------------------------------------------------------- */
722 static int
723 file_compute_color (int attr, file_entry * fe)
725 switch (attr)
727 case SELECTED:
728 return (SELECTED_COLOR);
729 case MARKED:
730 return (MARKED_COLOR);
731 case MARKED_SELECTED:
732 return (MARKED_SELECTED_COLOR);
733 case STATUS:
734 return (NORMAL_COLOR);
735 case NORMAL:
736 default:
737 if (!panels_options.filetype_mode)
738 return (NORMAL_COLOR);
741 return mc_fhl_get_color (mc_filehighlight, fe);
744 /* --------------------------------------------------------------------------------------------- */
745 /** Formats the file number file_index of panel in the buffer dest */
747 static void
748 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
749 int isstatus)
751 int color, length, empty_line;
752 const char *txt;
753 format_e *format, *home;
754 file_entry *fe;
756 (void) dest;
757 (void) limit;
758 length = 0;
759 empty_line = (file_index >= panel->count);
760 home = (isstatus) ? panel->status_format : panel->format;
761 fe = &panel->dir.list[file_index];
763 if (!empty_line)
764 color = file_compute_color (attr, fe);
765 else
766 color = NORMAL_COLOR;
768 for (format = home; format; format = format->next)
770 if (length == width)
771 break;
773 if (format->string_fn)
775 int len, perm;
776 char *preperad_text;
778 if (empty_line)
779 txt = " ";
780 else
781 txt = (*format->string_fn) (fe, format->field_len);
783 len = format->field_len;
784 if (len + length > width)
785 len = width - length;
786 if (len <= 0)
787 break;
789 perm = 0;
790 if (panels_options.permission_mode)
792 if (!strcmp (format->id, "perm"))
793 perm = 1;
794 else if (!strcmp (format->id, "mode"))
795 perm = 2;
798 if (color >= 0)
799 tty_setcolor (color);
800 else
801 tty_lowlevel_setcolor (-color);
803 preperad_text = (char *) str_fit_to_term (txt, len, format->just_mode);
805 if (perm)
806 add_permission_string (preperad_text, format->field_len, fe, attr, color, perm - 1);
807 else
808 tty_print_string (preperad_text);
810 length += len;
812 else
814 if (attr == SELECTED || attr == MARKED_SELECTED)
815 tty_setcolor (SELECTED_COLOR);
816 else
817 tty_setcolor (NORMAL_COLOR);
818 tty_print_one_vline (TRUE);
819 length++;
823 if (length < width)
824 tty_draw_hline (-1, -1, ' ', width - length);
827 /* --------------------------------------------------------------------------------------------- */
829 static void
830 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
832 int second_column = 0;
833 int width;
834 int offset = 0;
835 char buffer[BUF_MEDIUM];
837 gboolean panel_is_split = !isstatus && panel->split;
839 width = panel->widget.cols - 2;
841 if (panel_is_split)
843 second_column = (file_index - panel->top_file) / llines (panel);
844 width = width / 2 - 1;
846 if (second_column != 0)
848 offset = 1 + width;
849 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
850 width = panel->widget.cols - offset - 2;
854 /* Nothing to paint */
855 if (width <= 0)
856 return;
858 if (mv)
860 if (panel_is_split)
861 widget_move (&panel->widget,
862 (file_index - panel->top_file) % llines (panel) + 2, offset + 1);
863 else
864 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
867 format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus);
869 if (panel_is_split)
871 if (second_column)
872 tty_print_char (' ');
873 else
875 tty_setcolor (NORMAL_COLOR);
876 tty_print_one_vline (TRUE);
881 /* --------------------------------------------------------------------------------------------- */
883 static void
884 display_mini_info (WPanel * panel)
886 if (!panels_options.show_mini_info)
887 return;
889 widget_move (&panel->widget, llines (panel) + 3, 1);
891 if (panel->searching)
893 tty_setcolor (INPUT_COLOR);
894 tty_print_char ('/');
895 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
896 return;
899 /* Status resolves links and show them */
900 set_colors (panel);
902 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
904 char *lc_link, link_target[MC_MAXPATHLEN];
905 int len;
907 lc_link = concat_dir_and_file (panel->cwd, panel->dir.list[panel->selected].fname);
908 len = mc_readlink (lc_link, link_target, MC_MAXPATHLEN - 1);
909 g_free (lc_link);
910 if (len > 0)
912 link_target[len] = 0;
913 tty_print_string ("-> ");
914 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
916 else
917 tty_print_string (str_fit_to_term (_("<readlink failed>"),
918 panel->widget.cols - 2, J_LEFT));
920 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
922 /* FIXME:
923 * while loading directory (do_load_dir() and do_reload_dir()),
924 * the actual stat info about ".." directory isn't got;
925 * so just don't display incorrect info about ".." directory */
926 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
928 else
929 /* Default behavior */
930 repaint_file (panel, panel->selected, 0, STATUS, 1);
933 /* --------------------------------------------------------------------------------------------- */
935 static void
936 paint_dir (WPanel * panel)
938 int i;
939 int color; /* Color value of the line */
940 int items; /* Number of items */
942 items = llines (panel) * (panel->split ? 2 : 1);
944 for (i = 0; i < items; i++)
946 if (i + panel->top_file >= panel->count)
947 color = 0;
948 else
950 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
951 color += (panel->selected == i + panel->top_file && panel->active);
953 repaint_file (panel, i + panel->top_file, 1, color, 0);
955 tty_set_normal_attrs ();
958 /* --------------------------------------------------------------------------------------------- */
960 static void
961 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
963 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
964 int cols;
966 if (panel->marked <= 0)
967 return;
969 buf = size_only ? b_bytes : buffer;
970 cols = panel->widget.cols - 2;
973 * This is a trick to use two ngettext() calls in one sentence.
974 * First make "N bytes", then insert it into "X in M files".
976 g_snprintf (b_bytes, sizeof (b_bytes),
977 ngettext ("%s byte", "%s bytes", panel->total),
978 size_trunc_sep (panel->total, panels_options.kilobyte_si));
979 if (!size_only)
980 g_snprintf (buffer, sizeof (buffer),
981 ngettext ("%s in %d file", "%s in %d files", panel->marked),
982 b_bytes, panel->marked);
984 /* don't forget spaces around buffer content */
985 buf = (char *) str_trunc (buf, cols - 4);
987 if (x < 0)
988 /* center in panel */
989 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
992 * y == llines (panel) + 2 for mini_info_separator
993 * y == panel->widget.lines - 1 for panel bottom frame
995 widget_move (&panel->widget, y, x);
996 tty_setcolor (MARKED_COLOR);
997 tty_printf (" %s ", buf);
1000 /* --------------------------------------------------------------------------------------------- */
1002 static void
1003 mini_info_separator (WPanel * panel)
1005 if (panels_options.show_mini_info)
1007 const int y = llines (panel) + 2;
1009 tty_setcolor (NORMAL_COLOR);
1010 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1011 ACS_HLINE, panel->widget.cols - 2);
1012 /* Status displays total marked size.
1013 * Centered in panel, full format. */
1014 display_total_marked_size (panel, y, -1, FALSE);
1018 /* --------------------------------------------------------------------------------------------- */
1020 static void
1021 show_free_space (WPanel * panel)
1023 /* Used to figure out how many free space we have */
1024 static struct my_statfs myfs_stats;
1025 /* Old current working directory for displaying free space */
1026 static char *old_cwd = NULL;
1027 vfs_path_t *vpath = vfs_path_from_str (panel->cwd);
1029 /* Don't try to stat non-local fs */
1030 if (!vfs_file_is_local (vpath) || !free_space)
1032 vfs_path_free (vpath);
1033 return;
1035 vfs_path_free (vpath);
1037 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0)
1039 char rpath[PATH_MAX];
1041 init_my_statfs ();
1042 g_free (old_cwd);
1043 old_cwd = g_strdup (panel->cwd);
1045 if (mc_realpath (panel->cwd, rpath) == NULL)
1046 return;
1048 my_statfs (&myfs_stats, rpath);
1051 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1053 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1054 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1055 panels_options.kilobyte_si);
1056 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1057 panels_options.kilobyte_si);
1058 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1059 myfs_stats.total == 0 ? 0 :
1060 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1061 widget_move (&panel->widget, panel->widget.lines - 1,
1062 panel->widget.cols - 2 - (int) strlen (tmp));
1063 tty_setcolor (NORMAL_COLOR);
1064 tty_print_string (tmp);
1068 /* --------------------------------------------------------------------------------------------- */
1070 static void
1071 show_dir (WPanel * panel)
1073 gchar *tmp;
1074 set_colors (panel);
1075 draw_box (panel->widget.owner,
1076 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1078 if (panels_options.show_mini_info)
1080 widget_move (&panel->widget, llines (panel) + 2, 0);
1081 tty_print_alt_char (ACS_LTEE, FALSE);
1082 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1083 tty_print_alt_char (ACS_RTEE, FALSE);
1086 widget_move (&panel->widget, 0, 1);
1087 tty_print_string (panel_history_prev_item_sign);
1089 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1090 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1091 panel_history_next_item_sign);
1093 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1094 tty_print_string (tmp);
1096 g_free (tmp);
1098 if (panel->active)
1099 tty_setcolor (REVERSE_COLOR);
1101 widget_move (&panel->widget, 0, 3);
1103 if (panel->is_panelized)
1104 tty_printf (" %s ", _("Panelize"));
1105 else
1106 tty_printf (" %s ",
1107 str_term_trim (strip_home_and_password (panel->cwd),
1108 min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1110 if (!panels_options.show_mini_info)
1112 if (panel->marked == 0)
1114 /* Show size of curret file in the bottom of panel */
1115 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1117 char buffer[BUF_SMALL];
1119 g_snprintf (buffer, sizeof (buffer), " %s ",
1120 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1121 panels_options.kilobyte_si));
1122 tty_setcolor (NORMAL_COLOR);
1123 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1124 tty_print_string (buffer);
1127 else
1129 /* Show total size of marked files
1130 * In the bottom of panel, display size only. */
1131 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1135 show_free_space (panel);
1137 if (panel->active)
1138 tty_set_normal_attrs ();
1141 /* --------------------------------------------------------------------------------------------- */
1143 /* Returns the number of items in the given panel */
1144 static int
1145 ITEMS (WPanel * p)
1147 if (p->split)
1148 return llines (p) * 2;
1149 else
1150 return llines (p);
1153 /* --------------------------------------------------------------------------------------------- */
1155 static void
1156 adjust_top_file (WPanel * panel)
1158 int items = ITEMS (panel);
1160 if (panel->count <= items)
1162 /* If all files fit, show them all. */
1163 panel->top_file = 0;
1165 else
1167 int i;
1169 /* top_file has to be in the range [selected-items+1, selected] so that
1170 the selected file is visible.
1171 top_file should be in the range [0, count-items] so that there's
1172 no empty space wasted.
1173 Within these ranges, adjust it by as little as possible. */
1175 if (panel->top_file < 0)
1176 panel->top_file = 0;
1178 i = panel->selected - items + 1;
1179 if (panel->top_file < i)
1180 panel->top_file = i;
1182 i = panel->count - items;
1183 if (panel->top_file > i)
1184 panel->top_file = i;
1186 if (panel->top_file > panel->selected)
1187 panel->top_file = panel->selected;
1191 /* --------------------------------------------------------------------------------------------- */
1192 /** add "#enc:encodning" to end of path */
1193 /* if path end width a previous #enc:, only encoding is changed no additional
1194 * #enc: is appended
1195 * retun new string
1198 static char *
1199 panel_save_name (WPanel * panel)
1201 /* If the program is shuting down */
1202 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1203 return g_strdup (panel->panel_name);
1204 else
1205 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1208 /* --------------------------------------------------------------------------------------------- */
1210 /* "history_load" event handler */
1211 static gboolean
1212 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1213 gpointer init_data, gpointer data)
1215 WPanel *p = (WPanel *) init_data;
1216 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1218 (void) event_group_name;
1219 (void) event_name;
1221 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1223 if (ev->cfg != NULL)
1224 p->dir_history = history_load (ev->cfg, p->hist_name);
1225 else
1226 p->dir_history = history_get (p->hist_name);
1228 directory_history_add (p, p->cwd);
1231 return TRUE;
1234 /* --------------------------------------------------------------------------------------------- */
1236 /* "history_save" event handler */
1237 static gboolean
1238 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1239 gpointer init_data, gpointer data)
1241 WPanel *p = (WPanel *) init_data;
1243 (void) event_group_name;
1244 (void) event_name;
1246 if (p->dir_history != NULL)
1248 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1250 history_save (ev->cfg, p->hist_name, p->dir_history);
1253 return TRUE;
1256 /* --------------------------------------------------------------------------------------------- */
1258 static void
1259 panel_destroy (WPanel * p)
1261 size_t i;
1263 if (panels_options.auto_save_setup)
1265 char *name;
1267 name = panel_save_name (p);
1268 panel_save_setup (p, name);
1269 g_free (name);
1272 panel_clean_dir (p);
1274 /* clean history */
1275 if (p->dir_history != NULL)
1277 /* directory history is already saved before this moment */
1278 p->dir_history = g_list_first (p->dir_history);
1279 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1280 g_list_free (p->dir_history);
1282 g_free (p->hist_name);
1284 delete_format (p->format);
1285 delete_format (p->status_format);
1287 g_free (p->user_format);
1288 for (i = 0; i < LIST_TYPES; i++)
1289 g_free (p->user_status_format[i]);
1290 g_free (p->dir.list);
1291 g_free (p->panel_name);
1294 /* --------------------------------------------------------------------------------------------- */
1296 static void
1297 panel_format_modified (WPanel * panel)
1299 panel->format_modified = 1;
1302 /* --------------------------------------------------------------------------------------------- */
1304 static void
1305 panel_paint_sort_info (WPanel * panel)
1307 if (*panel->sort_info.sort_field->hotkey != '\0')
1309 const char *sort_sign =
1310 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1311 char *str;
1313 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1314 widget_move (&panel->widget, 1, 1);
1315 tty_print_string (str);
1316 g_free (str);
1320 /* --------------------------------------------------------------------------------------------- */
1322 static gchar *
1323 panel_get_title_without_hotkey (const char *title)
1325 char *translated_title;
1326 char *hkey;
1328 if (title == NULL)
1329 return NULL;
1330 if (title[0] == '\0')
1331 return g_strdup ("");
1333 translated_title = g_strdup (_(title));
1335 hkey = strchr (translated_title, '&');
1336 if ((hkey != NULL) && (hkey[1] != '\0'))
1337 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1339 return translated_title;
1342 /* --------------------------------------------------------------------------------------------- */
1344 static void
1345 paint_frame (WPanel * panel)
1347 int side, width;
1348 GString *format_txt;
1350 adjust_top_file (panel);
1352 widget_erase (&panel->widget);
1353 show_dir (panel);
1355 widget_move (&panel->widget, 1, 1);
1357 for (side = 0; side <= panel->split; side++)
1359 format_e *format;
1361 if (side)
1363 tty_setcolor (NORMAL_COLOR);
1364 tty_print_one_vline (TRUE);
1365 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1367 else if (panel->split)
1368 width = panel->widget.cols / 2 - 3;
1369 else
1370 width = panel->widget.cols - 2;
1372 format_txt = g_string_new ("");
1373 for (format = panel->format; format; format = format->next)
1375 if (format->string_fn)
1377 g_string_set_size (format_txt, 0);
1379 if (panel->list_type == list_long
1380 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1381 g_string_append (format_txt,
1382 panel->sort_info.reverse
1383 ? panel_sort_down_sign : panel_sort_up_sign);
1385 g_string_append (format_txt, format->title);
1386 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1388 g_string_append (format_txt, " [");
1389 g_string_append (format_txt, panel->filter);
1390 g_string_append (format_txt, "]");
1393 tty_setcolor (HEADER_COLOR);
1394 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1395 J_CENTER_LEFT));
1396 width -= format->field_len;
1398 else
1400 tty_setcolor (NORMAL_COLOR);
1401 tty_print_one_vline (TRUE);
1402 width--;
1405 g_string_free (format_txt, TRUE);
1407 if (width > 0)
1408 tty_draw_hline (-1, -1, ' ', width);
1411 if (panel->list_type != list_long)
1412 panel_paint_sort_info (panel);
1415 /* --------------------------------------------------------------------------------------------- */
1417 static const char *
1418 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1420 panel_display_t frame = frame_half;
1421 format = skip_separators (format);
1423 if (!strncmp (format, "full", 4))
1425 frame = frame_full;
1426 format += 4;
1428 else if (!strncmp (format, "half", 4))
1430 frame = frame_half;
1431 format += 4;
1434 if (!isstatus)
1436 panel->frame_size = frame;
1437 panel->split = 0;
1440 /* Now, the optional column specifier */
1441 format = skip_separators (format);
1443 if (*format == '1' || *format == '2')
1445 if (!isstatus)
1446 panel->split = *format == '2';
1447 format++;
1450 if (!isstatus)
1451 panel_update_cols (&(panel->widget), panel->frame_size);
1453 return skip_separators (format);
1456 /* Format is:
1458 all := panel_format? format
1459 panel_format := [full|half] [1|2]
1460 format := one_format_e
1461 | format , one_format_e
1463 one_format_e := just format.id [opt_size]
1464 just := [<=>]
1465 opt_size := : size [opt_expand]
1466 size := [0-9]+
1467 opt_expand := +
1471 /* --------------------------------------------------------------------------------------------- */
1473 static format_e *
1474 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1475 int *res_total_cols)
1477 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1478 int total_cols = 0; /* Used columns by the format */
1479 int set_justify; /* flag: set justification mode? */
1480 align_crt_t justify = J_LEFT; /* Which mode. */
1481 size_t i;
1483 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1485 *error = 0;
1487 if (i18n_timelength == 0)
1489 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1491 for (i = 0; panel_fields[i].id != NULL; i++)
1492 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1493 panel_fields[i].min_size = i18n_timelength;
1497 * This makes sure that the panel and mini status full/half mode
1498 * setting is equal
1500 format = parse_panel_size (panel, format, isstatus);
1502 while (*format)
1503 { /* format can be an empty string */
1504 int found = 0;
1506 darr = g_new0 (format_e, 1);
1508 /* I'm so ugly, don't look at me :-) */
1509 if (!home)
1510 home = old = darr;
1512 old->next = darr;
1513 darr->next = 0;
1514 old = darr;
1516 format = skip_separators (format);
1518 if (strchr ("<=>", *format))
1520 set_justify = 1;
1521 switch (*format)
1523 case '<':
1524 justify = J_LEFT;
1525 break;
1526 case '=':
1527 justify = J_CENTER;
1528 break;
1529 case '>':
1530 default:
1531 justify = J_RIGHT;
1532 break;
1534 format = skip_separators (format + 1);
1536 else
1537 set_justify = 0;
1539 for (i = 0; panel_fields[i].id != NULL; i++)
1541 size_t klen = strlen (panel_fields[i].id);
1543 if (strncmp (format, panel_fields[i].id, klen) != 0)
1544 continue;
1546 format += klen;
1548 darr->requested_field_len = panel_fields[i].min_size;
1549 darr->string_fn = panel_fields[i].string_fn;
1550 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1552 darr->id = panel_fields[i].id;
1553 darr->expand = panel_fields[i].expands;
1554 darr->just_mode = panel_fields[i].default_just;
1556 if (set_justify)
1558 if (IS_FIT (darr->just_mode))
1559 darr->just_mode = MAKE_FIT (justify);
1560 else
1561 darr->just_mode = justify;
1563 found = 1;
1565 format = skip_separators (format);
1567 /* If we have a size specifier */
1568 if (*format == ':')
1570 int req_length;
1572 /* If the size was specified, we don't want
1573 * auto-expansion by default
1575 darr->expand = 0;
1576 format++;
1577 req_length = atoi (format);
1578 darr->requested_field_len = req_length;
1580 format = skip_numbers (format);
1582 /* Now, if they insist on expansion */
1583 if (*format == '+')
1585 darr->expand = 1;
1586 format++;
1591 break;
1593 if (!found)
1595 char *tmp_format = g_strdup (format);
1597 int pos = min (8, strlen (format));
1598 delete_format (home);
1599 tmp_format[pos] = 0;
1600 *error =
1601 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1602 g_free (tmp_format);
1603 return 0;
1605 total_cols += darr->requested_field_len;
1608 *res_total_cols = total_cols;
1609 return home;
1612 /* --------------------------------------------------------------------------------------------- */
1614 static format_e *
1615 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1617 #define MAX_EXPAND 4
1618 int expand_top = 0; /* Max used element in expand */
1619 int usable_columns; /* Usable columns in the panel */
1620 int total_cols = 0;
1621 int i;
1622 format_e *darr, *home;
1624 if (!format)
1625 format = DEFAULT_USER_FORMAT;
1627 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1629 if (*error)
1630 return 0;
1632 panel->dirty = 1;
1634 /* Status needn't to be split */
1635 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1637 : (panel->split + 1))) - (!isstatus
1638 && panel->split);
1640 /* Look for the expandable fields and set field_len based on the requested field len */
1641 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1643 darr->field_len = darr->requested_field_len;
1644 if (darr->expand)
1645 expand_top++;
1648 /* If we used more columns than the available columns, adjust that */
1649 if (total_cols > usable_columns)
1651 int pdif, dif = total_cols - usable_columns;
1653 while (dif)
1655 pdif = dif;
1656 for (darr = home; darr; darr = darr->next)
1658 if (dif && darr->field_len - 1)
1660 darr->field_len--;
1661 dif--;
1665 /* avoid endless loop if num fields > 40 */
1666 if (pdif == dif)
1667 break;
1669 total_cols = usable_columns; /* give up, the rest should be truncated */
1672 /* Expand the available space */
1673 if ((usable_columns > total_cols) && expand_top)
1675 int spaces = (usable_columns - total_cols) / expand_top;
1676 int extra = (usable_columns - total_cols) % expand_top;
1678 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1679 if (darr->expand)
1681 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1682 i++;
1685 return home;
1688 /* --------------------------------------------------------------------------------------------- */
1689 /** Given the panel->view_type returns the format string to be parsed */
1691 static const char *
1692 panel_format (WPanel * panel)
1694 switch (panel->list_type)
1696 case list_long:
1697 return "full perm space nlink space owner space group space size space mtime space name";
1699 case list_brief:
1700 return "half 2 type name";
1702 case list_user:
1703 return panel->user_format;
1705 default:
1706 case list_full:
1707 return "half type name | size | mtime";
1711 /* --------------------------------------------------------------------------------------------- */
1713 static const char *
1714 mini_status_format (WPanel * panel)
1716 if (panel->user_mini_status)
1717 return panel->user_status_format[panel->list_type];
1719 switch (panel->list_type)
1722 case list_long:
1723 return "full perm space nlink space owner space group space size space mtime space name";
1725 case list_brief:
1726 return "half type name space bsize space perm space";
1728 case list_full:
1729 return "half type name";
1731 default:
1732 case list_user:
1733 return panel->user_format;
1737 /* */
1738 /* Panel operation commands */
1739 /* */
1741 /* --------------------------------------------------------------------------------------------- */
1742 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1744 static cb_ret_t
1745 maybe_cd (int move_up_dir)
1747 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1749 if (move_up_dir)
1751 do_cd ("..", cd_exact);
1752 return MSG_HANDLED;
1755 if (S_ISDIR (selection (current_panel)->st.st_mode)
1756 || link_isdir (selection (current_panel)))
1758 do_cd (selection (current_panel)->fname, cd_exact);
1759 return MSG_HANDLED;
1762 return MSG_NOT_HANDLED;
1765 /* --------------------------------------------------------------------------------------------- */
1767 /* if command line is empty then do 'cd ..' */
1768 static cb_ret_t
1769 force_maybe_cd (void)
1771 if (cmdline->buffer[0] == '\0')
1773 do_cd ("..", cd_exact);
1774 return MSG_HANDLED;
1776 return MSG_NOT_HANDLED;
1779 /* --------------------------------------------------------------------------------------------- */
1781 static void
1782 unselect_item (WPanel * panel)
1784 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1787 /* --------------------------------------------------------------------------------------------- */
1789 static void
1790 move_down (WPanel * panel)
1792 if (panel->selected + 1 == panel->count)
1793 return;
1795 unselect_item (panel);
1796 panel->selected++;
1797 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1799 /* Scroll window half screen */
1800 panel->top_file += ITEMS (panel) / 2;
1801 if (panel->top_file > panel->count - ITEMS (panel))
1802 panel->top_file = panel->count - ITEMS (panel);
1803 paint_dir (panel);
1805 select_item (panel);
1808 /* --------------------------------------------------------------------------------------------- */
1810 static void
1811 move_up (WPanel * panel)
1813 if (panel->selected == 0)
1814 return;
1816 unselect_item (panel);
1817 panel->selected--;
1818 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1820 /* Scroll window half screen */
1821 panel->top_file -= ITEMS (panel) / 2;
1822 if (panel->top_file < 0)
1823 panel->top_file = 0;
1824 paint_dir (panel);
1826 select_item (panel);
1829 /* --------------------------------------------------------------------------------------------- */
1830 /** Changes the selection by lines (may be negative) */
1832 static void
1833 move_selection (WPanel * panel, int lines)
1835 int new_pos;
1836 int adjust = 0;
1838 new_pos = panel->selected + lines;
1839 if (new_pos >= panel->count)
1840 new_pos = panel->count - 1;
1842 if (new_pos < 0)
1843 new_pos = 0;
1845 unselect_item (panel);
1846 panel->selected = new_pos;
1848 if (panel->selected - panel->top_file >= ITEMS (panel))
1850 panel->top_file += lines;
1851 adjust = 1;
1854 if (panel->selected - panel->top_file < 0)
1856 panel->top_file += lines;
1857 adjust = 1;
1860 if (adjust)
1862 if (panel->top_file > panel->selected)
1863 panel->top_file = panel->selected;
1864 if (panel->top_file < 0)
1865 panel->top_file = 0;
1866 paint_dir (panel);
1868 select_item (panel);
1871 /* --------------------------------------------------------------------------------------------- */
1873 static cb_ret_t
1874 move_left (WPanel * panel)
1876 if (panel->split)
1878 move_selection (panel, -llines (panel));
1879 return MSG_HANDLED;
1881 else
1882 return maybe_cd (1); /* cd .. */
1885 /* --------------------------------------------------------------------------------------------- */
1887 static cb_ret_t
1888 move_right (WPanel * panel)
1890 if (panel->split)
1892 move_selection (panel, llines (panel));
1893 return MSG_HANDLED;
1895 else
1896 return maybe_cd (0); /* cd (selection) */
1899 /* --------------------------------------------------------------------------------------------- */
1901 static void
1902 prev_page (WPanel * panel)
1904 int items;
1906 if (!panel->selected && !panel->top_file)
1907 return;
1908 unselect_item (panel);
1909 items = ITEMS (panel);
1910 if (panel->top_file < items)
1911 items = panel->top_file;
1912 if (!items)
1913 panel->selected = 0;
1914 else
1915 panel->selected -= items;
1916 panel->top_file -= items;
1918 select_item (panel);
1919 paint_dir (panel);
1922 /* --------------------------------------------------------------------------------------------- */
1924 static void
1925 goto_parent_dir (WPanel * panel)
1927 if (!panel->is_panelized)
1928 do_cd ("..", cd_exact);
1929 else
1931 char *fname = panel->dir.list[panel->selected].fname;
1932 const char *bname;
1933 char *dname;
1935 if (g_path_is_absolute (fname))
1936 fname = g_strdup (fname);
1937 else
1938 fname = mc_build_filename (panelized_panel.root, fname, (char *) NULL);
1940 bname = x_basename (fname);
1942 if (bname == fname)
1943 dname = g_strdup (".");
1944 else
1945 dname = g_strndup (fname, bname - fname);
1947 do_cd (dname, cd_exact);
1948 try_to_select (panel, bname);
1950 g_free (dname);
1951 g_free (fname);
1955 /* --------------------------------------------------------------------------------------------- */
1957 static void
1958 next_page (WPanel * panel)
1960 int items;
1962 if (panel->selected == panel->count - 1)
1963 return;
1964 unselect_item (panel);
1965 items = ITEMS (panel);
1966 if (panel->top_file > panel->count - 2 * items)
1967 items = panel->count - items - panel->top_file;
1968 if (panel->top_file + items < 0)
1969 items = -panel->top_file;
1970 if (!items)
1971 panel->selected = panel->count - 1;
1972 else
1973 panel->selected += items;
1974 panel->top_file += items;
1976 select_item (panel);
1977 paint_dir (panel);
1980 /* --------------------------------------------------------------------------------------------- */
1982 static void
1983 goto_child_dir (WPanel * panel)
1985 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
1987 do_cd (selection (panel)->fname, cd_exact);
1991 /* --------------------------------------------------------------------------------------------- */
1993 static void
1994 goto_top_file (WPanel * panel)
1996 unselect_item (panel);
1997 panel->selected = panel->top_file;
1998 select_item (panel);
2001 /* --------------------------------------------------------------------------------------------- */
2003 static void
2004 goto_middle_file (WPanel * panel)
2006 unselect_item (panel);
2007 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2008 select_item (panel);
2011 /* --------------------------------------------------------------------------------------------- */
2013 static void
2014 goto_bottom_file (WPanel * panel)
2016 unselect_item (panel);
2017 panel->selected = panel->top_file + ITEMS (panel) - 1;
2018 select_item (panel);
2021 /* --------------------------------------------------------------------------------------------- */
2023 static void
2024 move_home (WPanel * panel)
2026 if (panel->selected == 0)
2027 return;
2029 unselect_item (panel);
2031 if (panels_options.torben_fj_mode)
2033 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2035 if (panel->selected > middle_pos)
2037 goto_middle_file (panel);
2038 return;
2040 if (panel->selected != panel->top_file)
2042 goto_top_file (panel);
2043 return;
2047 panel->top_file = 0;
2048 panel->selected = 0;
2050 paint_dir (panel);
2051 select_item (panel);
2054 /* --------------------------------------------------------------------------------------------- */
2056 static void
2057 move_end (WPanel * panel)
2059 if (panel->selected == panel->count - 1)
2060 return;
2062 unselect_item (panel);
2064 if (panels_options.torben_fj_mode)
2066 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2068 if (panel->selected < middle_pos)
2070 goto_middle_file (panel);
2071 return;
2073 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2075 goto_bottom_file (panel);
2076 return;
2080 panel->selected = panel->count - 1;
2081 paint_dir (panel);
2082 select_item (panel);
2085 /* --------------------------------------------------------------------------------------------- */
2087 static void
2088 do_mark_file (WPanel * panel, mark_act_t do_move)
2090 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2091 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2092 move_down (panel);
2093 else if (do_move == MARK_FORCE_UP)
2094 move_up (panel);
2097 /* --------------------------------------------------------------------------------------------- */
2099 static void
2100 mark_file (WPanel * panel)
2102 do_mark_file (panel, MARK_DOWN);
2105 /* --------------------------------------------------------------------------------------------- */
2107 static void
2108 mark_file_up (WPanel * panel)
2110 do_mark_file (panel, MARK_FORCE_UP);
2113 /* --------------------------------------------------------------------------------------------- */
2115 static void
2116 mark_file_down (WPanel * panel)
2118 do_mark_file (panel, MARK_FORCE_DOWN);
2121 /* --------------------------------------------------------------------------------------------- */
2123 static void
2124 mark_file_right (WPanel * panel)
2126 int lines = llines (panel);
2128 if (state_mark < 0)
2129 state_mark = selection (panel)->f.marked ? 0 : 1;
2131 lines = min (lines, panel->count - panel->selected - 1);
2132 for (; lines != 0; lines--)
2134 do_file_mark (panel, panel->selected, state_mark);
2135 move_down (panel);
2137 do_file_mark (panel, panel->selected, state_mark);
2140 /* --------------------------------------------------------------------------------------------- */
2142 static void
2143 mark_file_left (WPanel * panel)
2145 int lines = llines (panel);
2147 if (state_mark < 0)
2148 state_mark = selection (panel)->f.marked ? 0 : 1;
2150 lines = min (lines, panel->selected + 1);
2151 for (; lines != 0; lines--)
2153 do_file_mark (panel, panel->selected, state_mark);
2154 move_up (panel);
2156 do_file_mark (panel, panel->selected, state_mark);
2159 /* --------------------------------------------------------------------------------------------- */
2160 /** Incremental search of a file name in the panel.
2161 * @param panel instance of WPanel structure
2162 * @param c_code key code
2165 static void
2166 do_search (WPanel * panel, int c_code)
2168 size_t l;
2169 int i, sel;
2170 gboolean wrapped = FALSE;
2171 char *act;
2172 mc_search_t *search;
2173 char *reg_exp, *esc_str;
2174 gboolean is_found = FALSE;
2176 l = strlen (panel->search_buffer);
2177 if (c_code == KEY_BACKSPACE)
2179 if (l != 0)
2181 act = panel->search_buffer + l;
2182 str_prev_noncomb_char (&act, panel->search_buffer);
2183 act[0] = '\0';
2185 panel->search_chpoint = 0;
2187 else
2189 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2191 panel->search_char[panel->search_chpoint] = c_code;
2192 panel->search_chpoint++;
2195 if (panel->search_chpoint > 0)
2197 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2199 case -2:
2200 return;
2201 case -1:
2202 panel->search_chpoint = 0;
2203 return;
2204 default:
2205 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2207 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2208 l += panel->search_chpoint;
2209 *(panel->search_buffer + l) = '\0';
2210 panel->search_chpoint = 0;
2216 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2217 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2218 search = mc_search_new (esc_str, -1);
2219 search->search_type = MC_SEARCH_T_GLOB;
2220 search->is_entire_line = TRUE;
2221 switch (panels_options.qsearch_mode)
2223 case QSEARCH_CASE_SENSITIVE:
2224 search->is_case_sensitive = TRUE;
2225 break;
2226 case QSEARCH_CASE_INSENSITIVE:
2227 search->is_case_sensitive = FALSE;
2228 break;
2229 default:
2230 search->is_case_sensitive = panel->sort_info.case_sensitive;
2231 break;
2233 sel = panel->selected;
2234 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2236 if (i >= panel->count)
2238 i = 0;
2239 if (wrapped)
2240 break;
2241 wrapped = TRUE;
2243 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2245 sel = i;
2246 is_found = TRUE;
2247 break;
2250 if (is_found)
2252 unselect_item (panel);
2253 panel->selected = sel;
2254 select_item (panel);
2255 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2257 else if (c_code != KEY_BACKSPACE)
2259 act = panel->search_buffer + l;
2260 str_prev_noncomb_char (&act, panel->search_buffer);
2261 act[0] = '\0';
2263 mc_search_free (search);
2264 g_free (reg_exp);
2265 g_free (esc_str);
2268 /* --------------------------------------------------------------------------------------------- */
2269 /** Start new search.
2270 * @param panel instance of WPanel structure
2273 static void
2274 start_search (WPanel * panel)
2276 if (panel->searching)
2278 if (panel->selected + 1 == panel->count)
2279 panel->selected = 0;
2280 else
2281 move_down (panel);
2283 /* in case if there was no search string we need to recall
2284 previous string, with which we ended previous searching */
2285 if (panel->search_buffer[0] == '\0')
2286 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2287 sizeof (panel->search_buffer));
2289 do_search (panel, 0);
2291 else
2293 panel->searching = TRUE;
2294 panel->search_buffer[0] = '\0';
2295 panel->search_char[0] = '\0';
2296 panel->search_chpoint = 0;
2297 display_mini_info (panel);
2298 mc_refresh ();
2302 /* --------------------------------------------------------------------------------------------- */
2304 static void
2305 stop_search (WPanel * panel)
2307 panel->searching = FALSE;
2309 /* if user had overrdied search string, we need to store it
2310 to the previous_search_buffer */
2311 if (panel->search_buffer[0] != '\0')
2312 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2313 sizeof (panel->prev_search_buffer));
2315 display_mini_info (panel);
2318 /* --------------------------------------------------------------------------------------------- */
2319 /** Return 1 if the Enter key has been processed, 0 otherwise */
2321 static int
2322 do_enter_on_file_entry (file_entry * fe)
2324 char *full_name;
2327 * Directory or link to directory - change directory.
2328 * Try the same for the entries on which mc_lstat() has failed.
2330 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2332 if (!do_cd (fe->fname, cd_exact))
2333 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2334 return 1;
2337 /* Try associated command */
2338 if (regex_command (fe->fname, "Open", NULL) != 0)
2339 return 1;
2341 /* Check if the file is executable */
2342 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2343 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe))
2345 g_free (full_name);
2346 return 0;
2348 g_free (full_name);
2350 if (confirm_execute)
2352 if (query_dialog
2353 (_("The Midnight Commander"),
2354 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2355 return 1;
2358 if (!vfs_current_is_local ())
2360 int ret;
2361 vfs_path_t *tmp_vpath;
2363 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2364 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2365 vfs_path_free (tmp_vpath);
2366 /* We took action only if the dialog was shown or the execution
2367 * was successful */
2368 return confirm_execute || (ret == 0);
2372 char *tmp = name_quote (fe->fname, 0);
2373 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2374 g_free (tmp);
2375 shell_execute (cmd, 0);
2376 g_free (cmd);
2379 #ifdef HAVE_CHARSET
2380 mc_global.source_codepage = default_source_codepage;
2381 #endif
2383 return 1;
2386 /* --------------------------------------------------------------------------------------------- */
2388 static int
2389 do_enter (WPanel * panel)
2391 return do_enter_on_file_entry (selection (panel));
2394 /* --------------------------------------------------------------------------------------------- */
2396 static void
2397 chdir_other_panel (WPanel * panel)
2399 const file_entry *entry = &panel->dir.list[panel->selected];
2401 char *new_dir;
2402 char *sel_entry = NULL;
2404 if (get_other_type () != view_listing)
2406 set_display_type (get_other_index (), view_listing);
2409 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2410 new_dir = mc_build_filename (panel->cwd, entry->fname, (char *) NULL);
2411 else
2413 new_dir = mc_build_filename (panel->cwd, "..", (char *) NULL);
2414 sel_entry = strrchr (panel->cwd, PATH_SEP);
2417 change_panel ();
2418 do_cd (new_dir, cd_exact);
2419 if (sel_entry)
2420 try_to_select (current_panel, sel_entry);
2421 change_panel ();
2423 move_down (panel);
2425 g_free (new_dir);
2428 /* --------------------------------------------------------------------------------------------- */
2430 * Make the current directory of the current panel also the current
2431 * directory of the other panel. Put the other panel to the listing
2432 * mode if needed. If the current panel is panelized, the other panel
2433 * doesn't become panelized.
2436 static void
2437 panel_sync_other (const WPanel * panel)
2439 if (get_other_type () != view_listing)
2441 set_display_type (get_other_index (), view_listing);
2444 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2446 /* try to select current filename on the other panel */
2447 if (!panel->is_panelized)
2449 try_to_select (other_panel, selection (panel)->fname);
2453 /* --------------------------------------------------------------------------------------------- */
2455 static void
2456 chdir_to_readlink (WPanel * panel)
2458 char *new_dir;
2460 if (get_other_type () != view_listing)
2461 return;
2463 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2465 char buffer[MC_MAXPATHLEN], *p;
2466 int i;
2467 struct stat st;
2468 vfs_path_t *panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2470 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2471 if (i < 0)
2473 vfs_path_free (panel_fname_vpath);
2474 return;
2476 if (mc_stat (panel_fname_vpath, &st) < 0)
2478 vfs_path_free (panel_fname_vpath);
2479 return;
2481 vfs_path_free (panel_fname_vpath);
2482 buffer[i] = 0;
2483 if (!S_ISDIR (st.st_mode))
2485 p = strrchr (buffer, PATH_SEP);
2486 if (p && !p[1])
2488 *p = 0;
2489 p = strrchr (buffer, PATH_SEP);
2491 if (!p)
2492 return;
2493 p[1] = 0;
2495 if (*buffer == PATH_SEP)
2496 new_dir = g_strdup (buffer);
2497 else
2498 new_dir = concat_dir_and_file (panel->cwd, buffer);
2500 change_panel ();
2501 do_cd (new_dir, cd_exact);
2502 change_panel ();
2504 move_down (panel);
2506 g_free (new_dir);
2510 /* --------------------------------------------------------------------------------------------- */
2512 static gsize
2513 panel_get_format_field_count (WPanel * panel)
2515 format_e *format;
2516 gsize lc_index;
2517 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2518 return lc_index;
2521 /* --------------------------------------------------------------------------------------------- */
2523 function return 0 if not found and REAL_INDEX+1 if found
2526 static gsize
2527 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2529 format_e *format;
2530 gsize lc_index;
2532 for (lc_index = 1, format = panel->format;
2533 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2534 if (format == NULL)
2535 lc_index = 0;
2537 return lc_index;
2540 /* --------------------------------------------------------------------------------------------- */
2542 static format_e *
2543 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2545 format_e *format;
2546 for (format = panel->format;
2547 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2548 return format;
2551 /* --------------------------------------------------------------------------------------------- */
2553 static const panel_field_t *
2554 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2556 const panel_field_t *pfield;
2557 format_e *format;
2559 format = panel_get_format_field_by_index (panel, lc_index);
2560 if (format == NULL)
2561 return NULL;
2562 pfield = panel_get_field_by_title (format->title);
2563 if (pfield == NULL)
2564 return NULL;
2565 if (pfield->sort_routine == NULL)
2566 return NULL;
2567 return pfield;
2570 /* --------------------------------------------------------------------------------------------- */
2572 static void
2573 panel_toggle_sort_order_prev (WPanel * panel)
2575 gsize lc_index, i;
2576 gchar *title;
2578 const panel_field_t *pfield = NULL;
2580 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2581 lc_index = panel_get_format_field_index_by_name (panel, title);
2582 g_free (title);
2584 if (lc_index > 1)
2586 /* search for prev sortable column in panel format */
2587 for (i = lc_index - 1;
2588 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2591 if (pfield == NULL)
2593 /* Sortable field not found. Try to search in each array */
2594 for (i = panel_get_format_field_count (panel);
2595 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2598 if (pfield != NULL)
2600 panel->sort_info.sort_field = pfield;
2601 panel_set_sort_order (panel, pfield);
2605 /* --------------------------------------------------------------------------------------------- */
2607 static void
2608 panel_toggle_sort_order_next (WPanel * panel)
2610 gsize lc_index, i;
2611 const panel_field_t *pfield = NULL;
2612 gsize format_field_count;
2613 gchar *title;
2615 format_field_count = panel_get_format_field_count (panel);
2616 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2617 lc_index = panel_get_format_field_index_by_name (panel, title);
2618 g_free (title);
2620 if (lc_index != 0 && lc_index != format_field_count)
2622 /* search for prev sortable column in panel format */
2623 for (i = lc_index;
2624 i != format_field_count
2625 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2628 if (pfield == NULL)
2630 /* Sortable field not found. Try to search in each array */
2631 for (i = 0;
2632 i != format_field_count
2633 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2636 if (pfield != NULL)
2638 panel->sort_info.sort_field = pfield;
2639 panel_set_sort_order (panel, pfield);
2643 /* --------------------------------------------------------------------------------------------- */
2645 static void
2646 panel_select_sort_order (WPanel * panel)
2648 const panel_field_t *sort_order;
2650 sort_order = sort_box (&panel->sort_info);
2651 if (sort_order != NULL)
2653 panel->sort_info.sort_field = sort_order;
2654 panel_set_sort_order (panel, sort_order);
2658 /* --------------------------------------------------------------------------------------------- */
2660 static void
2661 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2663 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2665 const panel_field_t *sort_order;
2667 sort_order = panel_get_field_by_id (name);
2668 if (sort_order == NULL)
2669 return;
2670 panel->sort_info.sort_field = sort_order;
2672 else
2673 panel->sort_info.reverse = !panel->sort_info.reverse;
2675 panel_set_sort_order (panel, panel->sort_info.sort_field);
2678 /* --------------------------------------------------------------------------------------------- */
2680 * If we moved to the parent directory move the selection pointer to
2681 * the old directory name; If we leave VFS dir, remove FS specificator.
2683 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2686 static const char *
2687 get_parent_dir_name (const char *cwd, const char *lwd)
2689 size_t llen, clen;
2690 const char *p;
2692 llen = strlen (lwd);
2693 clen = strlen (cwd);
2695 if (llen <= clen)
2696 return NULL;
2698 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2700 if (p == NULL)
2702 p = strrchr (lwd, PATH_SEP);
2704 if ((p != NULL)
2705 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2706 && (clen == (size_t) (p - lwd)
2707 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2708 return (p + 1);
2710 return NULL;
2713 /* skip VFS prefix */
2714 while (--p > lwd && *p != PATH_SEP)
2716 /* get last component */
2717 while (--p > lwd && *p != PATH_SEP)
2720 /* return last component */
2721 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
2724 /* --------------------------------------------------------------------------------------------- */
2725 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2727 static void
2728 subshell_chdir (const char *directory)
2730 #ifdef HAVE_SUBSHELL_SUPPORT
2731 if (mc_global.tty.use_subshell && vfs_current_is_local ())
2732 do_subshell_chdir (directory, FALSE, TRUE);
2733 #endif /* HAVE_SUBSHELL_SUPPORT */
2736 /* --------------------------------------------------------------------------------------------- */
2738 * Changes the current directory of the panel.
2739 * Don't record change in the directory history.
2742 static gboolean
2743 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
2745 vfs_path_t *vpath;
2746 char *olddir;
2747 char temp[MC_MAXPATHLEN];
2749 if (cd_type == cd_parse_command)
2751 while (*new_dir == ' ')
2752 new_dir++;
2755 olddir = g_strdup (panel->cwd);
2757 /* Convert *new_path to a suitable pathname, handle ~user */
2759 if (cd_type == cd_parse_command)
2761 if (!strcmp (new_dir, "-"))
2763 strcpy (temp, panel->lwd);
2764 new_dir = temp;
2767 vpath = vfs_path_from_str (*new_dir ? new_dir : mc_config_get_home_dir ());
2769 if (mc_chdir (vpath) == -1)
2771 strcpy (panel->cwd, olddir);
2772 g_free (olddir);
2773 vfs_path_free (vpath);
2774 return FALSE;
2776 vfs_path_free (vpath);
2778 /* Success: save previous directory, shutdown status of previous dir */
2779 strcpy (panel->lwd, olddir);
2780 input_free_completions (cmdline);
2782 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
2784 vfs_release_path (olddir);
2786 subshell_chdir (panel->cwd);
2788 /* Reload current panel */
2789 panel_clean_dir (panel);
2790 panel->count =
2791 do_load_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
2792 panel->sort_info.reverse, panel->sort_info.case_sensitive,
2793 panel->sort_info.exec_first, panel->filter);
2794 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
2795 load_hint (0);
2796 panel->dirty = 1;
2797 update_xterm_title_path ();
2799 g_free (olddir);
2801 return TRUE;
2804 /* --------------------------------------------------------------------------------------------- */
2806 static void
2807 directory_history_next (WPanel * panel)
2809 GList *nextdir;
2811 nextdir = g_list_next (panel->dir_history);
2813 if ((nextdir != NULL) && (_do_panel_cd (panel, (char *) nextdir->data, cd_exact)))
2814 panel->dir_history = nextdir;
2817 /* --------------------------------------------------------------------------------------------- */
2819 static void
2820 directory_history_prev (WPanel * panel)
2822 GList *prevdir;
2824 prevdir = g_list_previous (panel->dir_history);
2826 if ((prevdir != NULL) && (_do_panel_cd (panel, (char *) prevdir->data, cd_exact)))
2827 panel->dir_history = prevdir;
2830 /* --------------------------------------------------------------------------------------------- */
2832 static void
2833 directory_history_list (WPanel * panel)
2835 char *s;
2837 s = history_show (&panel->dir_history, &panel->widget);
2839 if (s != NULL)
2841 if (_do_panel_cd (panel, s, cd_exact))
2842 directory_history_add (panel, panel->cwd);
2843 else
2844 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2845 g_free (s);
2849 /* --------------------------------------------------------------------------------------------- */
2851 static cb_ret_t
2852 panel_execute_cmd (WPanel * panel, unsigned long command)
2854 int res = MSG_HANDLED;
2856 if (command != CK_Search)
2857 stop_search (panel);
2860 switch (command)
2862 case CK_Up:
2863 case CK_Down:
2864 case CK_Left:
2865 case CK_Right:
2866 case CK_Bottom:
2867 case CK_Top:
2868 case CK_PageDown:
2869 case CK_PageUp:
2870 /* reset state of marks flag */
2871 state_mark = -1;
2872 break;
2874 switch (command)
2876 case CK_PanelOtherCd:
2877 chdir_other_panel (panel);
2878 break;
2879 case CK_PanelOtherCdLink:
2880 chdir_to_readlink (panel);
2881 break;
2882 case CK_CopySingle:
2883 copy_cmd_local ();
2884 break;
2885 case CK_DeleteSingle:
2886 delete_cmd_local ();
2887 break;
2888 case CK_Enter:
2889 do_enter (panel);
2890 break;
2891 case CK_ViewRaw:
2892 view_raw_cmd ();
2893 break;
2894 case CK_EditNew:
2895 edit_cmd_new ();
2896 break;
2897 case CK_MoveSingle:
2898 rename_cmd_local ();
2899 break;
2900 case CK_SelectInvert:
2901 select_invert_cmd ();
2902 break;
2903 case CK_Select:
2904 select_cmd ();
2905 break;
2906 case CK_Unselect:
2907 unselect_cmd ();
2908 break;
2909 case CK_PageDown:
2910 next_page (panel);
2911 break;
2912 case CK_PageUp:
2913 prev_page (panel);
2914 break;
2915 case CK_CdChild:
2916 goto_child_dir (panel);
2917 break;
2918 case CK_CdParent:
2919 goto_parent_dir (panel);
2920 break;
2921 case CK_History:
2922 directory_history_list (panel);
2923 break;
2924 case CK_HistoryNext:
2925 directory_history_next (panel);
2926 break;
2927 case CK_HistoryPrev:
2928 directory_history_prev (panel);
2929 break;
2930 case CK_BottomOnScreen:
2931 goto_bottom_file (panel);
2932 break;
2933 case CK_MiddleOnScreen:
2934 goto_middle_file (panel);
2935 break;
2936 case CK_TopOnScreen:
2937 goto_top_file (panel);
2938 break;
2939 case CK_Mark:
2940 mark_file (panel);
2941 break;
2942 case CK_MarkUp:
2943 mark_file_up (panel);
2944 break;
2945 case CK_MarkDown:
2946 mark_file_down (panel);
2947 break;
2948 case CK_MarkLeft:
2949 mark_file_left (panel);
2950 break;
2951 case CK_MarkRight:
2952 mark_file_right (panel);
2953 break;
2954 case CK_CdParentSmart:
2955 res = force_maybe_cd ();
2956 break;
2957 case CK_Up:
2958 move_up (panel);
2959 break;
2960 case CK_Down:
2961 move_down (panel);
2962 break;
2963 case CK_Left:
2964 res = move_left (panel);
2965 break;
2966 case CK_Right:
2967 res = move_right (panel);
2968 break;
2969 case CK_Bottom:
2970 move_end (panel);
2971 break;
2972 case CK_Top:
2973 move_home (panel);
2974 break;
2975 #ifdef HAVE_CHARSET
2976 case CK_SelectCodepage:
2977 panel_change_encoding (panel);
2978 break;
2979 #endif
2980 case CK_Search:
2981 start_search (panel);
2982 break;
2983 case CK_SearchStop:
2984 break;
2985 case CK_PanelOtherSync:
2986 panel_sync_other (panel);
2987 break;
2988 case CK_Sort:
2989 panel_select_sort_order (panel);
2990 break;
2991 case CK_SortPrev:
2992 panel_toggle_sort_order_prev (panel);
2993 break;
2994 case CK_SortNext:
2995 panel_toggle_sort_order_next (panel);
2996 break;
2997 case CK_SortReverse:
2998 panel->sort_info.reverse = !panel->sort_info.reverse;
2999 panel_set_sort_order (panel, panel->sort_info.sort_field);
3000 break;
3001 case CK_SortByName:
3002 panel_set_sort_type_by_id (panel, "name");
3003 break;
3004 case CK_SortByExt:
3005 panel_set_sort_type_by_id (panel, "extension");
3006 break;
3007 case CK_SortBySize:
3008 panel_set_sort_type_by_id (panel, "size");
3009 break;
3010 case CK_SortByMTime:
3011 panel_set_sort_type_by_id (panel, "mtime");
3012 break;
3014 return res;
3017 /* --------------------------------------------------------------------------------------------- */
3019 static cb_ret_t
3020 panel_key (WPanel * panel, int key)
3022 size_t i;
3024 if (is_abort_char (key))
3026 stop_search (panel);
3027 return MSG_HANDLED;
3030 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3032 do_search (panel, key);
3033 return MSG_HANDLED;
3036 for (i = 0; panel_map[i].key != 0; i++)
3037 if (key == panel_map[i].key)
3038 return panel_execute_cmd (panel, panel_map[i].command);
3040 if (panels_options.torben_fj_mode && key == ALT ('h'))
3042 goto_middle_file (panel);
3043 return MSG_HANDLED;
3046 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3048 start_search (panel);
3049 do_search (panel, key);
3050 return MSG_HANDLED;
3053 return MSG_NOT_HANDLED;
3056 /* --------------------------------------------------------------------------------------------- */
3058 static cb_ret_t
3059 panel_callback (Widget * w, widget_msg_t msg, int parm)
3061 vfs_path_t *vpath;
3062 WPanel *panel = (WPanel *) w;
3063 WButtonBar *bb;
3065 switch (msg)
3067 case WIDGET_INIT:
3068 /* subscribe to "history_load" event */
3069 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3070 /* subscribe to "history_save" event */
3071 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3072 return MSG_HANDLED;
3074 case WIDGET_DRAW:
3075 /* Repaint everything, including frame and separator */
3076 paint_frame (panel); /* including show_dir */
3077 paint_dir (panel);
3078 mini_info_separator (panel);
3079 display_mini_info (panel);
3080 panel->dirty = 0;
3081 return MSG_HANDLED;
3083 case WIDGET_FOCUS:
3084 state_mark = -1;
3085 current_panel = panel;
3086 panel->active = 1;
3087 vpath = vfs_path_from_str (panel->cwd);
3088 if (mc_chdir (vpath) != 0)
3090 char *cwd = strip_password (g_strdup (panel->cwd), 1);
3091 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3092 cwd, unix_error_string (errno));
3093 g_free (cwd);
3095 else
3096 subshell_chdir (panel->cwd);
3098 vfs_path_free (vpath);
3099 update_xterm_title_path ();
3100 select_item (panel);
3101 show_dir (panel);
3102 paint_dir (panel);
3103 panel->dirty = 0;
3105 bb = find_buttonbar (panel->widget.owner);
3106 midnight_set_buttonbar (bb);
3107 buttonbar_redraw (bb);
3108 return MSG_HANDLED;
3110 case WIDGET_UNFOCUS:
3111 /* Janne: look at this for the multiple panel options */
3112 stop_search (panel);
3113 panel->active = 0;
3114 show_dir (panel);
3115 unselect_item (panel);
3116 return MSG_HANDLED;
3118 case WIDGET_KEY:
3119 return panel_key (panel, parm);
3121 case WIDGET_COMMAND:
3122 return panel_execute_cmd (panel, parm);
3124 case WIDGET_DESTROY:
3125 /* unsubscribe from "history_load" event */
3126 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3127 /* unsubscribe from "history_save" event */
3128 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3129 panel_destroy (panel);
3130 free_my_statfs ();
3131 return MSG_HANDLED;
3133 default:
3134 return default_proc (msg, parm);
3138 /* --------------------------------------------------------------------------------------------- */
3139 /* */
3140 /* Panel mouse events support routines */
3141 /* */
3143 static void
3144 mouse_toggle_mark (WPanel * panel)
3146 do_mark_file (panel, MARK_DONT_MOVE);
3147 mouse_marking = selection (panel)->f.marked;
3148 mouse_mark_panel = current_panel;
3151 /* --------------------------------------------------------------------------------------------- */
3153 static void
3154 mouse_set_mark (WPanel * panel)
3157 if (mouse_mark_panel == panel)
3159 if (mouse_marking && !(selection (panel)->f.marked))
3160 do_mark_file (panel, MARK_DONT_MOVE);
3161 else if (!mouse_marking && (selection (panel)->f.marked))
3162 do_mark_file (panel, MARK_DONT_MOVE);
3166 /* --------------------------------------------------------------------------------------------- */
3168 static int
3169 mark_if_marking (WPanel * panel, Gpm_Event * event)
3171 if (event->buttons & GPM_B_RIGHT)
3173 if (event->type & GPM_DOWN)
3174 mouse_toggle_mark (panel);
3175 else
3176 mouse_set_mark (panel);
3177 return 1;
3179 return 0;
3182 /* --------------------------------------------------------------------------------------------- */
3183 /** Determine which column was clicked, and sort the panel on
3184 * that column, or reverse sort on that column if already
3185 * sorted on that column.
3188 static void
3189 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3191 int i;
3192 const char *lc_sort_name = NULL;
3193 panel_field_t *col_sort_format = NULL;
3194 format_e *format;
3195 gchar *title;
3197 for (i = 0, format = panel->format; format != NULL; format = format->next)
3199 i += format->field_len;
3200 if (event->x < i + 1)
3202 /* found column */
3203 lc_sort_name = format->title;
3204 break;
3208 if (lc_sort_name == NULL)
3209 return;
3211 for (i = 0; panel_fields[i].id != NULL; i++)
3213 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3214 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3216 col_sort_format = &panel_fields[i];
3217 g_free (title);
3218 break;
3220 g_free (title);
3223 if (col_sort_format == NULL)
3224 return;
3226 if (panel->sort_info.sort_field == col_sort_format)
3228 /* reverse the sort if clicked column is already the sorted column */
3229 panel->sort_info.reverse = !panel->sort_info.reverse;
3231 else
3233 /* new sort is forced to be ascending */
3234 panel->sort_info.reverse = FALSE;
3236 panel_set_sort_order (panel, col_sort_format);
3240 /* --------------------------------------------------------------------------------------------- */
3242 * Mouse callback of the panel minus repainting.
3243 * If the event is redirected to the menu, *redir is set to TRUE.
3245 static int
3246 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3248 const int lines = llines (panel);
3249 const gboolean is_active = dlg_widget_active (panel);
3250 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3252 *redir = FALSE;
3254 /* 1st line */
3255 if (mouse_down && event->y == 1)
3257 /* "<" button */
3258 if (event->x == 2)
3260 directory_history_prev (panel);
3261 return MOU_NORMAL;
3264 /* "." button show/hide hidden files */
3265 if (event->x == panel->widget.cols - 5)
3267 panel->widget.owner->callback (panel->widget.owner, NULL,
3268 DLG_ACTION, CK_ShowHidden, NULL);
3269 repaint_screen ();
3270 return MOU_NORMAL;
3273 /* ">" button */
3274 if (event->x == panel->widget.cols - 1)
3276 directory_history_next (panel);
3277 return MOU_NORMAL;
3280 /* "^" button */
3281 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3283 directory_history_list (panel);
3284 return MOU_NORMAL;
3287 /* rest of the upper frame, the menu is invisible - call menu */
3288 if (!menubar_visible)
3290 *redir = TRUE;
3291 event->x += panel->widget.x;
3292 return the_menubar->widget.mouse (event, the_menubar);
3295 /* no other events on 1st line */
3296 return MOU_NORMAL;
3299 /* sort on clicked column; don't handle wheel events */
3300 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3302 mouse_sort_col (event, panel);
3303 return MOU_NORMAL;
3306 /* Mouse wheel events */
3307 if (mouse_down && (event->buttons & GPM_B_UP))
3309 if (is_active)
3311 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3312 prev_page (panel);
3313 else /* We are in first page */
3314 move_up (panel);
3316 return MOU_NORMAL;
3319 if (mouse_down && (event->buttons & GPM_B_DOWN))
3321 if (is_active)
3323 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3324 next_page (panel);
3325 else /* We are in last page */
3326 move_down (panel);
3328 return MOU_NORMAL;
3331 event->y -= 2;
3332 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3334 int my_index;
3336 if (!is_active)
3337 change_panel ();
3339 if (panel->top_file + event->y > panel->count)
3340 my_index = panel->count - 1;
3341 else
3343 my_index = panel->top_file + event->y - 1;
3344 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3345 my_index += llines (panel);
3347 if (my_index >= panel->count)
3348 my_index = panel->count - 1;
3351 if (my_index != panel->selected)
3353 unselect_item (panel);
3354 panel->selected = my_index;
3355 select_item (panel);
3358 /* This one is new */
3359 mark_if_marking (panel, event);
3361 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3363 if (event->y > 0 && event->y <= lines)
3364 do_enter (panel);
3366 return MOU_NORMAL;
3369 /* --------------------------------------------------------------------------------------------- */
3370 /** Mouse callback of the panel */
3372 static int
3373 panel_event (Gpm_Event * event, void *data)
3375 WPanel *panel = data;
3376 int ret;
3377 gboolean redir;
3379 ret = do_panel_event (event, panel, &redir);
3380 if (!redir)
3381 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3383 return ret;
3386 /* --------------------------------------------------------------------------------------------- */
3388 static void
3389 reload_panelized (WPanel * panel)
3391 int i, j;
3392 dir_list *list = &panel->dir;
3393 vfs_path_t *vpath;
3395 if (panel != current_panel)
3397 int ret;
3399 vpath = vfs_path_from_str (panel->cwd);
3400 ret = mc_chdir (vpath);
3401 vfs_path_free (vpath);
3404 for (i = 0, j = 0; i < panel->count; i++)
3406 if (list->list[i].f.marked)
3408 /* Unmark the file in advance. In case the following mc_lstat
3409 * fails we are done, else we have to mark the file again
3410 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3411 * IMO that's the best way to update the panel's summary status
3412 * -- Norbert
3414 do_file_mark (panel, i, 0);
3416 vpath = vfs_path_from_str (list->list[i].fname);
3417 if (mc_lstat (vpath, &list->list[i].st))
3419 g_free (list->list[i].fname);
3420 vfs_path_free (vpath);
3421 continue;
3423 if (list->list[i].f.marked)
3424 do_file_mark (panel, i, 1);
3425 if (j != i)
3426 list->list[j] = list->list[i];
3427 j++;
3428 vfs_path_free (vpath);
3430 if (j == 0)
3431 panel->count = set_zero_dir (list) ? 1 : 0;
3432 else
3433 panel->count = j;
3435 if (panel != current_panel)
3437 int ret;
3439 vpath = vfs_path_from_str (current_panel->cwd);
3440 ret = mc_chdir (vpath);
3441 vfs_path_free (vpath);
3445 /* --------------------------------------------------------------------------------------------- */
3447 static void
3448 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3450 gboolean free_pointer;
3451 char *my_current_file = NULL;
3453 if ((flags & UP_RELOAD) != 0)
3455 vfs_path_t *tmp_vpath;
3457 tmp_vpath = vfs_path_from_str (panel->cwd);
3458 panel->is_panelized = FALSE;
3459 mc_setctl (tmp_vpath, VFS_SETCTL_FLUSH, 0);
3460 vfs_path_free (tmp_vpath);
3461 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3464 /* If current_file == -1 (an invalid pointer) then preserve selection */
3465 free_pointer = current_file == UP_KEEPSEL;
3467 if (free_pointer)
3469 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3470 current_file = my_current_file;
3473 if (panel->is_panelized)
3474 reload_panelized (panel);
3475 else
3476 panel_reload (panel);
3478 try_to_select (panel, current_file);
3479 panel->dirty = 1;
3481 if (free_pointer)
3482 g_free (my_current_file);
3485 /* --------------------------------------------------------------------------------------------- */
3487 static void
3488 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3490 if (get_display_type (which) == view_listing)
3492 WPanel *panel;
3494 panel = (WPanel *) get_panel_widget (which);
3495 if (panel->is_panelized)
3496 flags &= ~UP_RELOAD;
3497 update_one_panel_widget (panel, flags, current_file);
3501 /* --------------------------------------------------------------------------------------------- */
3502 /*** public functions ****************************************************************************/
3503 /* --------------------------------------------------------------------------------------------- */
3505 char *
3506 remove_encoding_from_path (const char *path)
3508 GString *ret;
3509 GString *tmp_path, *tmp_conv;
3510 char *tmp;
3512 ret = g_string_new ("");
3513 tmp_conv = g_string_new ("");
3514 tmp_path = g_string_new (path);
3516 while ((tmp = g_strrstr (tmp_path->str, PATH_SEP_STR VFS_ENCODING_PREFIX)) != NULL)
3518 GIConv converter;
3519 char *tmp2;
3521 vfs_path_t *vpath = vfs_path_from_str (tmp);
3522 vfs_path_element_t *path_element = vfs_path_get_by_index (vpath, -1);
3524 converter =
3525 path_element->encoding !=
3526 NULL ? str_crt_conv_to (path_element->encoding) : str_cnv_to_term;
3527 vfs_path_free (vpath);
3529 if (converter == INVALID_CONV)
3530 converter = str_cnv_to_term;
3532 tmp2 = tmp + 1;
3533 while (*tmp2 != '\0' && *tmp2 != PATH_SEP)
3534 tmp2++;
3536 if (*tmp2 != '\0')
3538 str_vfs_convert_from (converter, tmp2, tmp_conv);
3539 g_string_prepend (ret, tmp_conv->str);
3540 g_string_set_size (tmp_conv, 0);
3543 g_string_set_size (tmp_path, tmp - tmp_path->str);
3544 str_close_conv (converter);
3547 g_string_prepend (ret, tmp_path->str);
3548 g_string_free (tmp_path, TRUE);
3549 g_string_free (tmp_conv, TRUE);
3551 return g_string_free (ret, FALSE);
3554 /* --------------------------------------------------------------------------------------------- */
3556 static void
3557 do_select (WPanel * panel, int i)
3559 if (i != panel->selected)
3561 panel->dirty = 1;
3562 panel->selected = i;
3563 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3564 if (panel->top_file < 0)
3565 panel->top_file = 0;
3569 /* --------------------------------------------------------------------------------------------- */
3571 static void
3572 do_try_to_select (WPanel * panel, const char *name)
3574 int i;
3575 char *subdir;
3577 if (!name)
3579 do_select (panel, 0);
3580 return;
3583 /* We only want the last component of the directory,
3584 * and from this only the name without suffix. */
3585 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3587 /* Search that subdirectory, if found select it */
3588 for (i = 0; i < panel->count; i++)
3590 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3592 do_select (panel, i);
3593 g_free (subdir);
3594 return;
3598 /* Try to select a file near the file that is missing */
3599 if (panel->selected >= panel->count)
3600 do_select (panel, panel->count - 1);
3601 g_free (subdir);
3604 /* --------------------------------------------------------------------------------------------- */
3606 /* event callback */
3607 static gboolean
3608 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3609 gpointer init_data, gpointer data)
3611 (void) event_group_name;
3612 (void) event_name;
3613 (void) init_data;
3614 (void) data;
3616 update_panels (UP_RELOAD, UP_KEEPSEL);
3618 return TRUE;
3621 /* --------------------------------------------------------------------------------------------- */
3623 /* event callback */
3624 static gboolean
3625 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3626 gpointer init_data, gpointer data)
3628 (void) event_group_name;
3629 (void) event_name;
3630 (void) init_data;
3631 (void) data;
3633 if (current_panel->marked == 0)
3634 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3635 (gpointer) selection (current_panel)->fname);
3636 else
3638 int i;
3639 gboolean first = TRUE;
3640 char *flist = NULL;
3642 for (i = 0; i < current_panel->count; i++)
3643 if (current_panel->dir.list[i].f.marked != 0)
3644 { /* Skip the unmarked ones */
3645 if (first)
3647 flist = g_strdup (current_panel->dir.list[i].fname);
3648 first = FALSE;
3650 else
3652 /* Add empty lines after the file */
3653 char *tmp;
3655 tmp =
3656 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3657 g_free (flist);
3658 flist = tmp;
3662 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3663 g_free (flist);
3665 return TRUE;
3668 /* --------------------------------------------------------------------------------------------- */
3669 /*** public functions ****************************************************************************/
3670 /* --------------------------------------------------------------------------------------------- */
3672 void
3673 try_to_select (WPanel * panel, const char *name)
3675 do_try_to_select (panel, name);
3676 select_item (panel);
3679 /* --------------------------------------------------------------------------------------------- */
3681 void
3682 panel_clean_dir (WPanel * panel)
3684 int count = panel->count;
3686 panel->count = 0;
3687 panel->top_file = 0;
3688 panel->selected = 0;
3689 panel->marked = 0;
3690 panel->dirs_marked = 0;
3691 panel->total = 0;
3692 panel->searching = FALSE;
3693 panel->is_panelized = FALSE;
3694 panel->dirty = 1;
3696 clean_dir (&panel->dir, count);
3699 /* --------------------------------------------------------------------------------------------- */
3700 /** Panel creation.
3701 * @param panel_name the name of the panel for setup retieving
3702 * @returns new instance of WPanel
3705 WPanel *
3706 panel_new (const char *panel_name)
3708 return panel_new_with_dir (panel_name, NULL);
3711 /* --------------------------------------------------------------------------------------------- */
3712 /** Panel creation for specified directory.
3713 * @param panel_name specifies the name of the panel for setup retieving
3714 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3715 * @returns new instance of WPanel
3718 WPanel *
3719 panel_new_with_dir (const char *panel_name, const char *wpath)
3721 WPanel *panel;
3722 char *section;
3723 int i, err;
3724 char curdir[MC_MAXPATHLEN] = "\0";
3726 panel = g_new0 (WPanel, 1);
3728 /* No know sizes of the panel at startup */
3729 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3731 /* We do not want the cursor */
3732 widget_want_cursor (panel->widget, 0);
3734 if (wpath != NULL)
3736 g_strlcpy (panel->cwd, wpath, sizeof (panel->cwd));
3737 mc_get_current_wd (curdir, sizeof (curdir) - 2);
3739 else
3740 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3742 strcpy (panel->lwd, ".");
3744 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3745 /* directories history will be get later */
3747 panel->dir.list = g_new (file_entry, MIN_FILES);
3748 panel->dir.size = MIN_FILES;
3749 panel->active = 0;
3750 panel->filter = 0;
3751 panel->split = 0;
3752 panel->top_file = 0;
3753 panel->selected = 0;
3754 panel->marked = 0;
3755 panel->total = 0;
3756 panel->dirty = 1;
3757 panel->searching = FALSE;
3758 panel->dirs_marked = 0;
3759 panel->is_panelized = FALSE;
3760 panel->format = 0;
3761 panel->status_format = 0;
3762 panel->format_modified = 1;
3764 panel->panel_name = g_strdup (panel_name);
3765 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3767 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3769 for (i = 0; i < LIST_TYPES; i++)
3770 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3772 panel->search_buffer[0] = '\0';
3773 panel->prev_search_buffer[0] = '\0';
3774 panel->frame_size = frame_half;
3776 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3777 if (!mc_config_has_group (mc_main_config, section))
3779 g_free (section);
3780 section = g_strdup (panel->panel_name);
3782 panel_load_setup (panel, section);
3783 g_free (section);
3785 /* Load format strings */
3786 err = set_panel_formats (panel);
3787 if (err != 0)
3788 set_panel_formats (panel);
3790 #ifdef HAVE_CHARSET
3792 vfs_path_t *vpath = vfs_path_from_str (panel->cwd);
3793 vfs_path_element_t *path_element = vfs_path_get_by_index (vpath, -1);
3795 if (path_element->encoding != NULL)
3796 panel->codepage = get_codepage_index (path_element->encoding);
3798 vfs_path_free (vpath);
3800 #endif
3803 vfs_path_t *vpath;
3805 vpath = vfs_path_from_str (panel->cwd);
3806 if (mc_chdir (vpath) != 0)
3808 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3809 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3811 vfs_path_free (vpath);
3814 /* Load the default format */
3815 panel->count =
3816 do_load_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
3817 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3818 panel->sort_info.exec_first, panel->filter);
3820 /* Restore old right path */
3821 if (curdir[0] != '\0')
3823 vfs_path_t *vpath;
3825 vpath = vfs_path_from_str (curdir);
3826 err = mc_chdir (vpath);
3827 vfs_path_free (vpath);
3830 return panel;
3833 /* --------------------------------------------------------------------------------------------- */
3835 void
3836 panel_reload (WPanel * panel)
3838 struct stat current_stat;
3840 if (panels_options.fast_reload && !stat (panel->cwd, &current_stat)
3841 && current_stat.st_ctime == panel->dir_stat.st_ctime
3842 && current_stat.st_mtime == panel->dir_stat.st_mtime)
3843 return;
3847 vfs_path_t *vpath;
3848 gboolean ok;
3849 char *last_slash;
3851 vpath = vfs_path_from_str (panel->cwd);
3852 ok = (mc_chdir (vpath) != -1);
3853 vfs_path_free (vpath);
3854 if (ok)
3855 break;
3857 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0)
3859 panel_clean_dir (panel);
3860 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
3861 return;
3863 last_slash = strrchr (panel->cwd, PATH_SEP);
3864 if (!last_slash || last_slash == panel->cwd)
3865 strcpy (panel->cwd, PATH_SEP_STR);
3866 else
3867 *last_slash = 0;
3868 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3869 show_dir (panel);
3871 while (TRUE);
3873 panel->count =
3874 do_reload_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
3875 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
3876 panel->sort_info.exec_first, panel->filter);
3878 panel->dirty = 1;
3879 if (panel->selected >= panel->count)
3880 do_select (panel, panel->count - 1);
3882 recalculate_panel_summary (panel);
3885 /* --------------------------------------------------------------------------------------------- */
3886 /* Switches the panel to the mode specified in the format */
3887 /* Seting up both format and status string. Return: 0 - on success; */
3888 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
3891 set_panel_formats (WPanel * p)
3893 format_e *form;
3894 char *err = NULL;
3895 int retcode = 0;
3897 form = use_display_format (p, panel_format (p), &err, 0);
3899 if (err != NULL)
3901 g_free (err);
3902 retcode = 1;
3904 else
3906 delete_format (p->format);
3907 p->format = form;
3910 if (panels_options.show_mini_info)
3912 form = use_display_format (p, mini_status_format (p), &err, 1);
3914 if (err != NULL)
3916 g_free (err);
3917 retcode += 2;
3919 else
3921 delete_format (p->status_format);
3922 p->status_format = form;
3926 panel_format_modified (p);
3927 panel_update_cols (&(p->widget), p->frame_size);
3929 if (retcode)
3930 message (D_ERROR, _("Warning"),
3931 _("User supplied format looks invalid, reverting to default."));
3932 if (retcode & 0x01)
3934 g_free (p->user_format);
3935 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
3937 if (retcode & 0x02)
3939 g_free (p->user_status_format[p->list_type]);
3940 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
3943 return retcode;
3946 /* --------------------------------------------------------------------------------------------- */
3948 void
3949 panel_update_cols (Widget * widget, panel_display_t frame_size)
3951 int cols, origin;
3953 /* don't touch panel if it is not in dialog yet */
3954 /* if panel is not in dialog it is not in widgets list
3955 and cannot be compared with get_panel_widget() result */
3956 if (widget->owner == NULL)
3957 return;
3959 if (panels_layout.horizontal_split)
3961 widget->cols = COLS;
3962 return;
3965 if (frame_size == frame_full)
3967 cols = COLS;
3968 origin = 0;
3970 else if (widget == get_panel_widget (0))
3972 cols = panels_layout.left_panel_size;
3973 origin = 0;
3975 else
3977 cols = COLS - panels_layout.left_panel_size;
3978 origin = panels_layout.left_panel_size;
3981 widget->cols = cols;
3982 widget->x = origin;
3985 /* --------------------------------------------------------------------------------------------- */
3987 /* Select current item and readjust the panel */
3988 void
3989 select_item (WPanel * panel)
3991 /* Although currently all over the code we set the selection and
3992 top file to decent values before calling select_item, I could
3993 forget it someday, so it's better to do the actual fitting here */
3995 if (panel->selected < 0)
3996 panel->selected = 0;
3998 if (panel->selected > panel->count - 1)
3999 panel->selected = panel->count - 1;
4001 adjust_top_file (panel);
4003 panel->dirty = 1;
4005 execute_hooks (select_file_hook);
4008 /* --------------------------------------------------------------------------------------------- */
4009 /** Clears all files in the panel, used only when one file was marked */
4010 void
4011 unmark_files (WPanel * panel)
4013 int i;
4015 if (!panel->marked)
4016 return;
4017 for (i = 0; i < panel->count; i++)
4018 file_mark (panel, i, 0);
4020 panel->dirs_marked = 0;
4021 panel->marked = 0;
4022 panel->total = 0;
4025 /* --------------------------------------------------------------------------------------------- */
4026 /** Recalculate the panels summary information, used e.g. when marked
4027 files might have been removed by an external command */
4029 void
4030 recalculate_panel_summary (WPanel * panel)
4032 int i;
4034 panel->marked = 0;
4035 panel->dirs_marked = 0;
4036 panel->total = 0;
4038 for (i = 0; i < panel->count; i++)
4039 if (panel->dir.list[i].f.marked)
4041 /* do_file_mark will return immediately if newmark == oldmark.
4042 So we have to first unmark it to get panel's summary information
4043 updated. (Norbert) */
4044 panel->dir.list[i].f.marked = 0;
4045 do_file_mark (panel, i, 1);
4049 /* --------------------------------------------------------------------------------------------- */
4050 /** This routine marks a file or a directory */
4052 void
4053 do_file_mark (WPanel * panel, int idx, int mark)
4055 if (panel->dir.list[idx].f.marked == mark)
4056 return;
4058 /* Only '..' can't be marked, '.' isn't visible */
4059 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4060 return;
4062 file_mark (panel, idx, mark);
4063 if (panel->dir.list[idx].f.marked)
4065 panel->marked++;
4066 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4068 if (panel->dir.list[idx].f.dir_size_computed)
4069 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4070 panel->dirs_marked++;
4072 else
4073 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4074 set_colors (panel);
4076 else
4078 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4080 if (panel->dir.list[idx].f.dir_size_computed)
4081 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4082 panel->dirs_marked--;
4084 else
4085 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4086 panel->marked--;
4090 /* --------------------------------------------------------------------------------------------- */
4092 * Changes the current directory of the panel.
4093 * Record change in the directory history.
4095 gboolean
4096 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
4098 gboolean r;
4100 r = _do_panel_cd (panel, new_dir, cd_type);
4101 if (r)
4102 directory_history_add (panel, panel->cwd);
4103 return r;
4106 /* --------------------------------------------------------------------------------------------- */
4108 void
4109 file_mark (WPanel * panel, int lc_index, int val)
4111 if (panel->dir.list[lc_index].f.marked != val)
4113 panel->dir.list[lc_index].f.marked = val;
4114 panel->dirty = 1;
4118 /* --------------------------------------------------------------------------------------------- */
4120 void
4121 panel_re_sort (WPanel * panel)
4123 char *filename;
4124 int i;
4126 if (panel == NULL)
4127 return;
4129 filename = g_strdup (selection (panel)->fname);
4130 unselect_item (panel);
4131 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4132 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4133 panel->sort_info.exec_first);
4134 panel->selected = -1;
4135 for (i = panel->count; i; i--)
4137 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4139 panel->selected = i - 1;
4140 break;
4143 g_free (filename);
4144 panel->top_file = panel->selected - ITEMS (panel) / 2;
4145 select_item (panel);
4146 panel->dirty = 1;
4149 /* --------------------------------------------------------------------------------------------- */
4151 void
4152 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4154 if (sort_order == NULL)
4155 return;
4157 panel->sort_info.sort_field = sort_order;
4159 /* The directory is already sorted, we have to load the unsorted stuff */
4160 if (sort_order->sort_routine == (sortfn *) unsorted)
4162 char *current_file;
4164 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4165 panel_reload (panel);
4166 try_to_select (panel, current_file);
4167 g_free (current_file);
4169 panel_re_sort (panel);
4172 /* --------------------------------------------------------------------------------------------- */
4174 * Change panel encoding.
4175 * @param panel WPanel object
4178 void
4179 panel_change_encoding (WPanel * panel)
4181 const char *encoding = NULL;
4182 char *cd_path;
4183 #ifdef HAVE_CHARSET
4184 char *errmsg;
4185 int r;
4187 r = select_charset (-1, -1, panel->codepage, FALSE);
4189 if (r == SELECT_CHARSET_CANCEL)
4190 return; /* Cancel */
4192 panel->codepage = r;
4194 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4196 /* No translation */
4197 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4198 cd_path = remove_encoding_from_path (panel->cwd);
4199 do_panel_cd (panel, cd_path, cd_parse_command);
4200 g_free (cd_path);
4201 return;
4204 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4205 if (errmsg != NULL)
4207 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4208 g_free (errmsg);
4209 return;
4212 encoding = get_codepage_id (panel->codepage);
4213 #endif
4214 if (encoding != NULL)
4216 vfs_path_t *vpath = vfs_path_from_str (panel->cwd);
4218 vfs_change_encoding (vpath, encoding);
4220 cd_path = vfs_path_to_str (vpath);
4221 if (!do_panel_cd (panel, cd_path, cd_parse_command))
4222 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4223 g_free (cd_path);
4225 vfs_path_free (vpath);
4229 /* --------------------------------------------------------------------------------------------- */
4231 * This routine reloads the directory in both panels. It tries to
4232 * select current_file in current_panel and other_file in other_panel.
4233 * If current_file == -1 then it automatically sets current_file and
4234 * other_file to the currently selected files in the panels.
4236 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4237 * will not reload the other panel.
4240 void
4241 update_panels (panel_update_flags_t flags, const char *current_file)
4243 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4244 WPanel *panel;
4245 vfs_path_t *vpath;
4246 int ret;
4248 update_one_panel (get_current_index (), flags, current_file);
4249 if (reload_other)
4250 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4252 if (get_current_type () == view_listing)
4253 panel = (WPanel *) get_panel_widget (get_current_index ());
4254 else
4255 panel = (WPanel *) get_panel_widget (get_other_index ());
4257 if (!panel->is_panelized)
4259 vpath = vfs_path_from_str (panel->cwd);
4260 ret = mc_chdir (vpath);
4261 vfs_path_free (vpath);
4265 /* --------------------------------------------------------------------------------------------- */
4267 void
4268 directory_history_add (struct WPanel *panel, const char *dir)
4270 char *tmp;
4272 tmp = g_strdup (dir);
4273 strip_password (tmp, 1);
4275 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4278 /* --------------------------------------------------------------------------------------------- */
4280 gsize
4281 panel_get_num_of_sortable_fields (void)
4283 gsize ret = 0, lc_index;
4285 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4286 if (panel_fields[lc_index].is_user_choice)
4287 ret++;
4288 return ret;
4291 /* --------------------------------------------------------------------------------------------- */
4293 const char **
4294 panel_get_sortable_fields (gsize * array_size)
4296 char **ret;
4297 gsize lc_index, i;
4299 lc_index = panel_get_num_of_sortable_fields ();
4301 ret = g_try_new0 (char *, lc_index + 1);
4302 if (ret == NULL)
4303 return NULL;
4305 if (array_size != NULL)
4306 *array_size = lc_index;
4308 lc_index = 0;
4310 for (i = 0; panel_fields[i].id != NULL; i++)
4311 if (panel_fields[i].is_user_choice)
4312 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4313 return (const char **) ret;
4316 /* --------------------------------------------------------------------------------------------- */
4318 const panel_field_t *
4319 panel_get_field_by_id (const char *name)
4321 gsize lc_index;
4322 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4323 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4324 return &panel_fields[lc_index];
4325 return NULL;
4328 /* --------------------------------------------------------------------------------------------- */
4330 const panel_field_t *
4331 panel_get_field_by_title_hotkey (const char *name)
4333 gsize lc_index;
4334 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4335 if (panel_fields[lc_index].title_hotkey != NULL &&
4336 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4337 return &panel_fields[lc_index];
4338 return NULL;
4341 /* --------------------------------------------------------------------------------------------- */
4343 const panel_field_t *
4344 panel_get_field_by_title (const char *name)
4346 gsize lc_index;
4347 gchar *title = NULL;
4349 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4351 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4352 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4354 g_free (title);
4355 return &panel_fields[lc_index];
4358 g_free (title);
4359 return NULL;
4362 /* --------------------------------------------------------------------------------------------- */
4364 gsize
4365 panel_get_num_of_user_possible_fields (void)
4367 gsize ret = 0, lc_index;
4369 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4370 if (panel_fields[lc_index].use_in_user_format)
4371 ret++;
4372 return ret;
4375 /* --------------------------------------------------------------------------------------------- */
4377 const char **
4378 panel_get_user_possible_fields (gsize * array_size)
4380 char **ret;
4381 gsize lc_index, i;
4383 lc_index = panel_get_num_of_user_possible_fields ();
4385 ret = g_try_new0 (char *, lc_index + 1);
4386 if (ret == NULL)
4387 return NULL;
4389 if (array_size != NULL)
4390 *array_size = lc_index;
4392 lc_index = 0;
4394 for (i = 0; panel_fields[i].id != NULL; i++)
4395 if (panel_fields[i].use_in_user_format)
4396 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4397 return (const char **) ret;
4400 /* --------------------------------------------------------------------------------------------- */
4402 void
4403 panel_init (void)
4405 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4406 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4408 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4409 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4410 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4411 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4412 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4414 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4415 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4416 panel_save_curent_file_to_clip_file, NULL, NULL);
4420 /* --------------------------------------------------------------------------------------------- */
4422 void
4423 panel_deinit (void)
4425 g_free (panel_sort_up_sign);
4426 g_free (panel_sort_down_sign);
4428 g_free (panel_hiddenfiles_sign_show);
4429 g_free (panel_hiddenfiles_sign_hide);
4430 g_free (panel_history_prev_item_sign);
4431 g_free (panel_history_next_item_sign);
4432 g_free (panel_history_show_list_sign);
4436 /* --------------------------------------------------------------------------------------------- */