Ticket #4536: skins: add root variant of julia256 skin.
[midnight-commander.git] / src / filemanager / panel.c
blobe6ead3567a1a4a41019092e447f574dc7c3199ba
1 /*
2 Panel managing.
4 Copyright (C) 1994-2024
5 Free Software Foundation, Inc.
7 Written by:
8 Miguel de Icaza, 1995
9 Timur Bakeyev, 1997, 1999
10 Slava Zanko <slavazanko@gmail.com>, 2013
11 Andrew Borodin <aborodin@vmail.ru>, 2013-2023
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file panel.c
30 * \brief Source: panel managin module
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
39 #include "lib/global.h"
41 #include "lib/tty/tty.h"
42 #include "lib/tty/key.h" /* XCTRL and ALT macros */
43 #include "lib/skin.h"
44 #include "lib/strutil.h"
45 #include "lib/mcconfig.h"
46 #include "lib/vfs/vfs.h"
47 #include "lib/unixcompat.h"
48 #include "lib/search.h"
49 #include "lib/timefmt.h" /* file_date() */
50 #include "lib/util.h"
51 #include "lib/widget.h"
52 #ifdef HAVE_CHARSET
53 #include "lib/charsets.h" /* get_codepage_id () */
54 #endif
55 #include "lib/event.h"
57 #include "src/setup.h" /* For loading/saving panel options */
58 #include "src/execute.h"
59 #ifdef HAVE_CHARSET
60 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
61 #endif
62 #include "src/keymap.h" /* global_keymap_t */
63 #include "src/history.h"
64 #ifdef ENABLE_SUBSHELL
65 #include "src/subshell/subshell.h" /* do_subshell_chdir() */
66 #endif
68 #include "src/usermenu.h"
70 #include "dir.h"
71 #include "boxes.h"
72 #include "tree.h"
73 #include "ext.h" /* regexp_command */
74 #include "layout.h" /* Most layout variables are here */
75 #include "cmd.h"
76 #include "command.h" /* cmdline */
77 #include "filemanager.h"
78 #include "mountlist.h" /* my_statfs */
79 #include "cd.h" /* cd_error_message() */
81 #include "panel.h"
83 /*** global variables ****************************************************************************/
85 /* The hook list for the select file function */
86 hook_t *select_file_hook = NULL;
88 mc_fhl_t *mc_filehighlight = NULL;
90 /*** file scope macro definitions ****************************************************************/
92 typedef enum
94 FATTR_NORMAL = 0,
95 FATTR_CURRENT,
96 FATTR_MARKED,
97 FATTR_MARKED_CURRENT,
98 FATTR_STATUS
99 } file_attr_t;
101 /* select/unselect dialog results */
102 #define SELECT_RESET ((mc_search_t *)(-1))
103 #define SELECT_ERROR ((mc_search_t *)(-2))
105 /* mouse position relative to file list */
106 #define MOUSE_UPPER_FILE_LIST (-1)
107 #define MOUSE_BELOW_FILE_LIST (-2)
108 #define MOUSE_AFTER_LAST_FILE (-3)
110 /*** file scope type declarations ****************************************************************/
112 typedef enum
114 MARK_DONT_MOVE = 0,
115 MARK_DOWN = 1,
116 MARK_FORCE_DOWN = 2,
117 MARK_FORCE_UP = 3
118 } mark_act_t;
121 * This describes a format item. The parse_display_format routine parses
122 * the user specified format and creates a linked list of format_item_t structures.
124 typedef struct format_item_t
126 int requested_field_len;
127 int field_len;
128 align_crt_t just_mode;
129 gboolean expand;
130 const char *(*string_fn) (file_entry_t *, int len);
131 char *title;
132 const char *id;
133 } format_item_t;
135 /* File name scroll states */
136 typedef enum
138 FILENAME_NOSCROLL = 1,
139 FILENAME_SCROLL_LEFT = 2,
140 FILENAME_SCROLL_RIGHT = 4
141 } filename_scroll_flag_t;
143 /*** forward declarations (file scope functions) *************************************************/
145 static const char *string_file_name (file_entry_t * fe, int len);
146 static const char *string_file_size (file_entry_t * fe, int len);
147 static const char *string_file_size_brief (file_entry_t * fe, int len);
148 static const char *string_file_type (file_entry_t * fe, int len);
149 static const char *string_file_mtime (file_entry_t * fe, int len);
150 static const char *string_file_atime (file_entry_t * fe, int len);
151 static const char *string_file_ctime (file_entry_t * fe, int len);
152 static const char *string_file_permission (file_entry_t * fe, int len);
153 static const char *string_file_perm_octal (file_entry_t * fe, int len);
154 static const char *string_file_nlinks (file_entry_t * fe, int len);
155 static const char *string_inode (file_entry_t * fe, int len);
156 static const char *string_file_nuid (file_entry_t * fe, int len);
157 static const char *string_file_ngid (file_entry_t * fe, int len);
158 static const char *string_file_owner (file_entry_t * fe, int len);
159 static const char *string_file_group (file_entry_t * fe, int len);
160 static const char *string_marked (file_entry_t * fe, int len);
161 static const char *string_space (file_entry_t * fe, int len);
162 static const char *string_dot (file_entry_t * fe, int len);
164 /*** file scope variables ************************************************************************/
166 /* *INDENT-OFF* */
167 static panel_field_t panel_fields[] = {
169 "unsorted", 12, TRUE, J_LEFT_FIT,
170 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
171 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
172 N_("sort|u"),
173 N_("&Unsorted"), TRUE, FALSE,
174 string_file_name,
175 (GCompareFunc) unsorted
179 "name", 12, TRUE, J_LEFT_FIT,
180 /* TRANSLATORS: one single character to represent 'name' sort mode */
181 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
182 N_("sort|n"),
183 N_("&Name"), TRUE, TRUE,
184 string_file_name,
185 (GCompareFunc) sort_name
189 "version", 12, TRUE, J_LEFT_FIT,
190 /* TRANSLATORS: one single character to represent 'version' sort mode */
191 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
192 N_("sort|v"),
193 N_("&Version"), TRUE, FALSE,
194 string_file_name,
195 (GCompareFunc) sort_vers
199 "extension", 12, TRUE, J_LEFT_FIT,
200 /* TRANSLATORS: one single character to represent 'extension' sort mode */
201 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
202 N_("sort|e"),
203 N_("E&xtension"), TRUE, FALSE,
204 string_file_name, /* TODO: string_file_ext */
205 (GCompareFunc) sort_ext
209 "size", 7, FALSE, J_RIGHT,
210 /* TRANSLATORS: one single character to represent 'size' sort mode */
211 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
212 N_("sort|s"),
213 N_("&Size"), TRUE, TRUE,
214 string_file_size,
215 (GCompareFunc) sort_size
219 "bsize", 7, FALSE, J_RIGHT,
221 N_("Block Size"), FALSE, FALSE,
222 string_file_size_brief,
223 (GCompareFunc) sort_size
227 "type", 1, FALSE, J_LEFT,
229 "", FALSE, TRUE,
230 string_file_type,
231 NULL
235 "mtime", 12, FALSE, J_RIGHT,
236 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
237 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
238 N_("sort|m"),
239 N_("&Modify time"), TRUE, TRUE,
240 string_file_mtime,
241 (GCompareFunc) sort_time
245 "atime", 12, FALSE, J_RIGHT,
246 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
247 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
248 N_("sort|a"),
249 N_("&Access time"), TRUE, TRUE,
250 string_file_atime,
251 (GCompareFunc) sort_atime
255 "ctime", 12, FALSE, J_RIGHT,
256 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
257 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
258 N_("sort|h"),
259 N_("C&hange time"), TRUE, TRUE,
260 string_file_ctime,
261 (GCompareFunc) sort_ctime
265 "perm", 10, FALSE, J_LEFT,
267 N_("Permission"), FALSE, TRUE,
268 string_file_permission,
269 NULL
273 "mode", 6, FALSE, J_RIGHT,
275 N_("Perm"), FALSE, TRUE,
276 string_file_perm_octal,
277 NULL
281 "nlink", 2, FALSE, J_RIGHT,
283 N_("Nl"), FALSE, TRUE,
284 string_file_nlinks, NULL
288 "inode", 5, FALSE, J_RIGHT,
289 /* TRANSLATORS: one single character to represent 'inode' sort mode */
290 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
291 N_("sort|i"),
292 N_("&Inode"), TRUE, TRUE,
293 string_inode,
294 (GCompareFunc) sort_inode
298 "nuid", 5, FALSE, J_RIGHT,
300 N_("UID"), FALSE, FALSE,
301 string_file_nuid,
302 NULL
306 "ngid", 5, FALSE, J_RIGHT,
308 N_("GID"), FALSE, FALSE,
309 string_file_ngid,
310 NULL
314 "owner", 8, FALSE, J_LEFT_FIT,
316 N_("Owner"), FALSE, TRUE,
317 string_file_owner,
318 NULL
322 "group", 8, FALSE, J_LEFT_FIT,
324 N_("Group"), FALSE, TRUE,
325 string_file_group,
326 NULL
330 "mark", 1, FALSE, J_RIGHT,
332 " ", FALSE, TRUE,
333 string_marked,
334 NULL
338 "|", 1, FALSE, J_RIGHT,
340 " ", FALSE, TRUE,
341 NULL,
342 NULL
346 "space", 1, FALSE, J_RIGHT,
348 " ", FALSE, TRUE,
349 string_space,
350 NULL
354 "dot", 1, FALSE, J_RIGHT,
356 " ", FALSE, FALSE,
357 string_dot,
358 NULL
362 NULL, 0, FALSE, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
365 /* *INDENT-ON* */
367 static char *panel_sort_up_char = NULL;
368 static char *panel_sort_down_char = NULL;
370 static char *panel_hiddenfiles_show_char = NULL;
371 static char *panel_hiddenfiles_hide_char = NULL;
372 static char *panel_history_prev_item_char = NULL;
373 static char *panel_history_next_item_char = NULL;
374 static char *panel_history_show_list_char = NULL;
375 static char *panel_filename_scroll_left_char = NULL;
376 static char *panel_filename_scroll_right_char = NULL;
378 /* Panel that selection started */
379 static WPanel *mouse_mark_panel = NULL;
381 static gboolean mouse_marking = FALSE;
382 static int state_mark = 0;
384 static GString *string_file_name_buffer;
386 /* --------------------------------------------------------------------------------------------- */
387 /*** file scope functions ************************************************************************/
388 /* --------------------------------------------------------------------------------------------- */
390 static panelized_descr_t *
391 panelized_descr_new (void)
393 panelized_descr_t *p;
395 p = g_new0 (panelized_descr_t, 1);
396 p->list.len = -1;
398 return p;
401 /* --------------------------------------------------------------------------------------------- */
403 static void
404 panelized_descr_free (panelized_descr_t * p)
406 if (p != NULL)
408 dir_list_free_list (&p->list);
409 vfs_path_free (p->root_vpath, TRUE);
410 g_free (p);
414 /* --------------------------------------------------------------------------------------------- */
416 static void
417 set_colors (const WPanel * panel)
419 (void) panel;
421 tty_set_normal_attrs ();
422 tty_setcolor (NORMAL_COLOR);
425 /* --------------------------------------------------------------------------------------------- */
426 /** Delete format_item_t object */
428 static void
429 format_item_free (format_item_t * format)
431 g_free (format->title);
432 g_free (format);
435 /* --------------------------------------------------------------------------------------------- */
436 /** Extract the number of available lines in a panel */
438 static int
439 panel_lines (const WPanel * p)
441 /* 3 lines are: top frame, column header, button frame */
442 return (CONST_WIDGET (p)->rect.lines - 3 - (panels_options.show_mini_info ? 2 : 0));
445 /* --------------------------------------------------------------------------------------------- */
446 /** This code relies on the default justification!!! */
448 static void
449 add_permission_string (const char *dest, int width, file_entry_t * fe, file_attr_t attr, int color,
450 gboolean is_octal)
452 int i, r, l;
454 l = get_user_permissions (&fe->st);
456 if (is_octal)
458 /* Place of the access bit in octal mode */
459 l = width + l - 3;
460 r = l + 1;
462 else
464 /* The same to the triplet in string mode */
465 l = l * 3 + 1;
466 r = l + 3;
469 for (i = 0; i < width; i++)
471 if (i >= l && i < r)
473 if (attr == FATTR_CURRENT || attr == FATTR_MARKED_CURRENT)
474 tty_setcolor (MARKED_SELECTED_COLOR);
475 else
476 tty_setcolor (MARKED_COLOR);
478 else if (color >= 0)
479 tty_setcolor (color);
480 else
481 tty_lowlevel_setcolor (-color);
483 tty_print_char (dest[i]);
487 /* --------------------------------------------------------------------------------------------- */
488 /** String representations of various file attributes name */
490 static const char *
491 string_file_name (file_entry_t * fe, int len)
493 (void) len;
495 mc_g_string_copy (string_file_name_buffer, fe->fname);
497 return string_file_name_buffer->str;
500 /* --------------------------------------------------------------------------------------------- */
502 static unsigned int
503 ilog10 (dev_t n)
505 unsigned int digits = 0;
509 digits++;
510 n /= 10;
512 while (n != 0);
514 return digits;
517 /* --------------------------------------------------------------------------------------------- */
519 static void
520 format_device_number (char *buf, size_t bufsize, dev_t dev)
522 dev_t major_dev, minor_dev;
523 unsigned int major_digits, minor_digits;
525 major_dev = major (dev);
526 major_digits = ilog10 (major_dev);
528 minor_dev = minor (dev);
529 minor_digits = ilog10 (minor_dev);
531 g_assert (bufsize >= 1);
533 if (major_digits + 1 + minor_digits + 1 <= bufsize)
534 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
535 else
536 g_strlcpy (buf, _("[dev]"), bufsize);
539 /* --------------------------------------------------------------------------------------------- */
540 /** size */
542 static const char *
543 string_file_size (file_entry_t * fe, int len)
545 static char buffer[BUF_TINY];
547 /* Don't ever show size of ".." since we don't calculate it */
548 if (DIR_IS_DOTDOT (fe->fname->str))
549 return _("UP--DIR");
551 #ifdef HAVE_STRUCT_STAT_ST_RDEV
552 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
553 format_device_number (buffer, len + 1, fe->st.st_rdev);
554 else
555 #endif
556 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
558 return buffer;
561 /* --------------------------------------------------------------------------------------------- */
562 /** bsize */
564 static const char *
565 string_file_size_brief (file_entry_t * fe, int len)
567 if (S_ISLNK (fe->st.st_mode) && !link_isdir (fe))
568 return _("SYMLINK");
570 if ((S_ISDIR (fe->st.st_mode) || link_isdir (fe)) && !DIR_IS_DOTDOT (fe->fname->str))
571 return _("SUB-DIR");
573 return string_file_size (fe, len);
576 /* --------------------------------------------------------------------------------------------- */
577 /** This functions return a string representation of a file entry type */
579 static const char *
580 string_file_type (file_entry_t * fe, int len)
582 static char buffer[2];
584 (void) len;
586 if (S_ISDIR (fe->st.st_mode))
587 buffer[0] = PATH_SEP;
588 else if (S_ISLNK (fe->st.st_mode))
590 if (link_isdir (fe))
591 buffer[0] = '~';
592 else if (fe->f.stale_link != 0)
593 buffer[0] = '!';
594 else
595 buffer[0] = '@';
597 else if (S_ISCHR (fe->st.st_mode))
598 buffer[0] = '-';
599 else if (S_ISSOCK (fe->st.st_mode))
600 buffer[0] = '=';
601 else if (S_ISDOOR (fe->st.st_mode))
602 buffer[0] = '>';
603 else if (S_ISBLK (fe->st.st_mode))
604 buffer[0] = '+';
605 else if (S_ISFIFO (fe->st.st_mode))
606 buffer[0] = '|';
607 else if (S_ISNAM (fe->st.st_mode))
608 buffer[0] = '#';
609 else if (!S_ISREG (fe->st.st_mode))
610 buffer[0] = '?'; /* non-regular of unknown kind */
611 else if (is_exe (fe->st.st_mode))
612 buffer[0] = '*';
613 else
614 buffer[0] = ' ';
615 buffer[1] = '\0';
616 return buffer;
619 /* --------------------------------------------------------------------------------------------- */
620 /** mtime */
622 static const char *
623 string_file_mtime (file_entry_t * fe, int len)
625 (void) len;
627 return file_date (fe->st.st_mtime);
630 /* --------------------------------------------------------------------------------------------- */
631 /** atime */
633 static const char *
634 string_file_atime (file_entry_t * fe, int len)
636 (void) len;
638 return file_date (fe->st.st_atime);
641 /* --------------------------------------------------------------------------------------------- */
642 /** ctime */
644 static const char *
645 string_file_ctime (file_entry_t * fe, int len)
647 (void) len;
649 return file_date (fe->st.st_ctime);
652 /* --------------------------------------------------------------------------------------------- */
653 /** perm */
655 static const char *
656 string_file_permission (file_entry_t * fe, int len)
658 (void) len;
660 return string_perm (fe->st.st_mode);
663 /* --------------------------------------------------------------------------------------------- */
664 /** mode */
666 static const char *
667 string_file_perm_octal (file_entry_t * fe, int len)
669 static char buffer[10];
671 (void) len;
673 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
674 return buffer;
677 /* --------------------------------------------------------------------------------------------- */
678 /** nlink */
680 static const char *
681 string_file_nlinks (file_entry_t * fe, int len)
683 static char buffer[BUF_TINY];
685 (void) len;
687 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
688 return buffer;
691 /* --------------------------------------------------------------------------------------------- */
692 /** inode */
694 static const char *
695 string_inode (file_entry_t * fe, int len)
697 static char buffer[10];
699 (void) len;
701 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
702 return buffer;
705 /* --------------------------------------------------------------------------------------------- */
706 /** nuid */
708 static const char *
709 string_file_nuid (file_entry_t * fe, int len)
711 static char buffer[10];
713 (void) len;
715 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
716 return buffer;
719 /* --------------------------------------------------------------------------------------------- */
720 /** ngid */
722 static const char *
723 string_file_ngid (file_entry_t * fe, int len)
725 static char buffer[10];
727 (void) len;
729 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
730 return buffer;
733 /* --------------------------------------------------------------------------------------------- */
734 /** owner */
736 static const char *
737 string_file_owner (file_entry_t * fe, int len)
739 (void) len;
741 return get_owner (fe->st.st_uid);
744 /* --------------------------------------------------------------------------------------------- */
745 /** group */
747 static const char *
748 string_file_group (file_entry_t * fe, int len)
750 (void) len;
752 return get_group (fe->st.st_gid);
755 /* --------------------------------------------------------------------------------------------- */
756 /** mark */
758 static const char *
759 string_marked (file_entry_t * fe, int len)
761 (void) len;
763 return fe->f.marked != 0 ? "*" : " ";
766 /* --------------------------------------------------------------------------------------------- */
767 /** space */
769 static const char *
770 string_space (file_entry_t * fe, int len)
772 (void) fe;
773 (void) len;
775 return " ";
778 /* --------------------------------------------------------------------------------------------- */
779 /** dot */
781 static const char *
782 string_dot (file_entry_t * fe, int len)
784 (void) fe;
785 (void) len;
787 return ".";
790 /* --------------------------------------------------------------------------------------------- */
792 static int
793 file_compute_color (file_attr_t attr, file_entry_t * fe)
795 switch (attr)
797 case FATTR_CURRENT:
798 return (SELECTED_COLOR);
799 case FATTR_MARKED:
800 return (MARKED_COLOR);
801 case FATTR_MARKED_CURRENT:
802 return (MARKED_SELECTED_COLOR);
803 case FATTR_STATUS:
804 return (NORMAL_COLOR);
805 case FATTR_NORMAL:
806 default:
807 if (!panels_options.filetype_mode)
808 return (NORMAL_COLOR);
811 return mc_fhl_get_color (mc_filehighlight, fe);
814 /* --------------------------------------------------------------------------------------------- */
815 /** Returns the number of items in the given panel */
817 static int
818 panel_items (const WPanel * p)
820 return panel_lines (p) * p->list_cols;
823 /* --------------------------------------------------------------------------------------------- */
824 /** Formats the file number file_index of panel in the buffer dest */
826 static filename_scroll_flag_t
827 format_file (WPanel * panel, int file_index, int width, file_attr_t attr, gboolean isstatus,
828 int *field_length)
830 int color = NORMAL_COLOR;
831 int length = 0;
832 GSList *format, *home;
833 file_entry_t *fe = NULL;
834 filename_scroll_flag_t res = FILENAME_NOSCROLL;
836 *field_length = 0;
838 if (file_index < panel->dir.len)
840 fe = &panel->dir.list[file_index];
841 color = file_compute_color (attr, fe);
844 home = isstatus ? panel->status_format : panel->format;
846 for (format = home; format != NULL && length != width; format = g_slist_next (format))
848 format_item_t *fi = (format_item_t *) format->data;
850 if (fi->string_fn != NULL)
852 const char *txt = " ";
853 int len, perm = 0;
854 const char *prepared_text;
855 int name_offset = 0;
857 if (fe != NULL)
858 txt = fi->string_fn (fe, fi->field_len);
860 len = fi->field_len;
861 if (len + length > width)
862 len = width - length;
863 if (len <= 0)
864 break;
866 if (!isstatus && panel->content_shift > -1 && strcmp (fi->id, "name") == 0)
868 int str_len;
869 int i;
871 *field_length = len + 1;
873 str_len = str_length (txt);
874 i = MAX (0, str_len - len);
875 panel->max_shift = MAX (panel->max_shift, i);
876 i = MIN (panel->content_shift, i);
878 if (i > -1)
880 name_offset = str_offset_to_pos (txt, i);
881 if (str_len > len)
883 res = FILENAME_SCROLL_LEFT;
884 if (str_length (txt + name_offset) > len)
885 res |= FILENAME_SCROLL_RIGHT;
890 if (panels_options.permission_mode)
892 if (strcmp (fi->id, "perm") == 0)
893 perm = 1;
894 else if (strcmp (fi->id, "mode") == 0)
895 perm = 2;
898 if (color >= 0)
899 tty_setcolor (color);
900 else
901 tty_lowlevel_setcolor (-color);
903 if (!isstatus && panel->content_shift > -1)
904 prepared_text = str_fit_to_term (txt + name_offset, len, HIDE_FIT (fi->just_mode));
905 else
906 prepared_text = str_fit_to_term (txt, len, fi->just_mode);
908 if (perm != 0 && fe != NULL)
909 add_permission_string (prepared_text, fi->field_len, fe, attr, color, perm != 1);
910 else
911 tty_print_string (prepared_text);
913 length += len;
915 else
917 if (attr == FATTR_CURRENT || attr == FATTR_MARKED_CURRENT)
918 tty_setcolor (SELECTED_COLOR);
919 else
920 tty_setcolor (NORMAL_COLOR);
921 tty_print_one_vline (TRUE);
922 length++;
926 if (length < width)
928 int y, x;
930 tty_getyx (&y, &x);
931 tty_draw_hline (y, x, ' ', width - length);
934 return res;
937 /* --------------------------------------------------------------------------------------------- */
939 static void
940 repaint_file (WPanel * panel, int file_index, file_attr_t attr)
942 Widget *w = WIDGET (panel);
944 int nth_column = 0;
945 int width;
946 int offset = 0;
947 filename_scroll_flag_t ret_frm;
948 int ypos = 0;
949 gboolean panel_is_split;
950 int fln = 0;
952 panel_is_split = panel->list_cols > 1;
953 width = w->rect.cols - 2;
955 if (panel_is_split)
957 nth_column = (file_index - panel->top) / panel_lines (panel);
958 width /= panel->list_cols;
960 offset = width * nth_column;
962 if (nth_column + 1 >= panel->list_cols)
963 width = w->rect.cols - offset - 2;
966 /* Nothing to paint */
967 if (width <= 0)
968 return;
970 ypos = file_index - panel->top;
972 if (panel_is_split)
973 ypos %= panel_lines (panel);
975 ypos += 2; /* top frame and header */
976 widget_gotoyx (w, ypos, offset + 1);
978 ret_frm = format_file (panel, file_index, width, attr, FALSE, &fln);
980 if (panel_is_split && nth_column + 1 < panel->list_cols)
982 tty_setcolor (NORMAL_COLOR);
983 tty_print_one_vline (TRUE);
986 if (ret_frm != FILENAME_NOSCROLL)
988 if (!panel_is_split && fln > 0)
990 if (panel->list_format != list_long)
991 width = fln;
992 else
994 offset = width - fln + 1;
995 width = fln - 1;
999 widget_gotoyx (w, ypos, offset);
1000 tty_setcolor (NORMAL_COLOR);
1001 tty_print_string (panel_filename_scroll_left_char);
1003 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
1005 offset += width;
1006 if (nth_column + 1 >= panel->list_cols)
1007 offset++;
1009 widget_gotoyx (w, ypos, offset);
1010 tty_setcolor (NORMAL_COLOR);
1011 tty_print_string (panel_filename_scroll_right_char);
1016 /* --------------------------------------------------------------------------------------------- */
1018 static void
1019 repaint_status (WPanel * panel)
1021 int width;
1023 width = WIDGET (panel)->rect.cols - 2;
1024 if (width > 0)
1026 int fln = 0;
1028 (void) format_file (panel, panel->current, width, FATTR_STATUS, TRUE, &fln);
1032 /* --------------------------------------------------------------------------------------------- */
1034 static void
1035 display_mini_info (WPanel * panel)
1037 Widget *w = WIDGET (panel);
1038 const file_entry_t *fe;
1040 if (!panels_options.show_mini_info || panel->current < 0)
1041 return;
1043 widget_gotoyx (w, panel_lines (panel) + 3, 1);
1045 if (panel->quick_search.active)
1047 tty_setcolor (INPUT_COLOR);
1048 tty_print_char ('/');
1049 tty_print_string (str_fit_to_term
1050 (panel->quick_search.buffer->str, w->rect.cols - 3, J_LEFT));
1051 return;
1054 /* Status resolves links and show them */
1055 set_colors (panel);
1057 fe = panel_current_entry (panel);
1059 if (S_ISLNK (fe->st.st_mode))
1061 char link_target[MC_MAXPATHLEN];
1062 vfs_path_t *lc_link_vpath;
1063 int len;
1065 lc_link_vpath = vfs_path_append_new (panel->cwd_vpath, fe->fname->str, (char *) NULL);
1066 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
1067 vfs_path_free (lc_link_vpath, TRUE);
1068 if (len > 0)
1070 link_target[len] = 0;
1071 tty_print_string ("-> ");
1072 tty_print_string (str_fit_to_term (link_target, w->rect.cols - 5, J_LEFT_FIT));
1074 else
1075 tty_print_string (str_fit_to_term (_("<readlink failed>"), w->rect.cols - 2, J_LEFT));
1077 else if (DIR_IS_DOTDOT (fe->fname->str))
1079 /* FIXME:
1080 * while loading directory (dir_list_load() and dir_list_reload()),
1081 * the actual stat info about ".." directory isn't got;
1082 * so just don't display incorrect info about ".." directory */
1083 tty_print_string (str_fit_to_term (_("UP--DIR"), w->rect.cols - 2, J_LEFT));
1085 else
1086 /* Default behavior */
1087 repaint_status (panel);
1090 /* --------------------------------------------------------------------------------------------- */
1092 static void
1093 paint_dir (WPanel * panel)
1095 int i;
1096 int items; /* Number of items */
1098 items = panel_items (panel);
1099 /* reset max len of filename because we have the new max length for the new file list */
1100 panel->max_shift = -1;
1102 for (i = 0; i < items; i++)
1104 file_attr_t attr = FATTR_NORMAL; /* Color value of the line */
1105 int n;
1106 gboolean marked;
1108 n = i + panel->top;
1109 marked = (panel->dir.list[n].f.marked != 0);
1111 if (n < panel->dir.len)
1113 if (panel->current == n && panel->active)
1114 attr = marked ? FATTR_MARKED_CURRENT : FATTR_CURRENT;
1115 else if (marked)
1116 attr = FATTR_MARKED;
1119 repaint_file (panel, n, attr);
1122 tty_set_normal_attrs ();
1125 /* --------------------------------------------------------------------------------------------- */
1127 static void
1128 display_total_marked_size (const WPanel * panel, int y, int x, gboolean size_only)
1130 const Widget *w = CONST_WIDGET (panel);
1132 char buffer[BUF_SMALL], b_bytes[BUF_SMALL];
1133 const char *buf;
1134 int cols;
1136 if (panel->marked <= 0)
1137 return;
1139 buf = size_only ? b_bytes : buffer;
1140 cols = w->rect.cols - 2;
1142 g_strlcpy (b_bytes, size_trunc_sep (panel->total, panels_options.kilobyte_si),
1143 sizeof (b_bytes));
1145 if (!size_only)
1146 g_snprintf (buffer, sizeof (buffer),
1147 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1148 b_bytes, panel->marked);
1150 /* don't forget spaces around buffer content */
1151 buf = str_trunc (buf, cols - 4);
1153 if (x < 0)
1154 /* center in panel */
1155 x = (w->rect.cols - str_term_width1 (buf)) / 2 - 1;
1158 * y == panel_lines (panel) + 2 for mini_info_separator
1159 * y == w->lines - 1 for panel bottom frame
1161 widget_gotoyx (w, y, x);
1162 tty_setcolor (MARKED_COLOR);
1163 tty_printf (" %s ", buf);
1166 /* --------------------------------------------------------------------------------------------- */
1168 static void
1169 mini_info_separator (const WPanel * panel)
1171 if (panels_options.show_mini_info)
1173 const Widget *w = CONST_WIDGET (panel);
1174 int y;
1176 y = panel_lines (panel) + 2;
1178 tty_setcolor (NORMAL_COLOR);
1179 tty_draw_hline (w->rect.y + y, w->rect.x + 1, ACS_HLINE, w->rect.cols - 2);
1180 /* Status displays total marked size.
1181 * Centered in panel, full format. */
1182 display_total_marked_size (panel, y, -1, FALSE);
1186 /* --------------------------------------------------------------------------------------------- */
1188 static void
1189 show_free_space (const WPanel * panel)
1191 /* Used to figure out how many free space we have */
1192 static struct my_statfs myfs_stats;
1193 /* Old current working directory for displaying free space */
1194 static char *old_cwd = NULL;
1196 /* Don't try to stat non-local fs */
1197 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1198 return;
1200 if (old_cwd == NULL || strcmp (old_cwd, vfs_path_as_str (panel->cwd_vpath)) != 0)
1202 char rpath[PATH_MAX];
1204 init_my_statfs ();
1205 g_free (old_cwd);
1206 old_cwd = g_strdup (vfs_path_as_str (panel->cwd_vpath));
1208 if (mc_realpath (old_cwd, rpath) == NULL)
1209 return;
1211 my_statfs (&myfs_stats, rpath);
1214 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1216 const Widget *w = CONST_WIDGET (panel);
1217 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1219 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1220 panels_options.kilobyte_si);
1221 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1222 panels_options.kilobyte_si);
1223 g_snprintf (tmp, sizeof (tmp), " %s / %s (%d%%) ", buffer1, buffer2,
1224 myfs_stats.total == 0 ? 0 :
1225 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1226 widget_gotoyx (w, w->rect.lines - 1, w->rect.cols - 2 - (int) strlen (tmp));
1227 tty_setcolor (NORMAL_COLOR);
1228 tty_print_string (tmp);
1232 /* --------------------------------------------------------------------------------------------- */
1234 * Prepare path string for showing in panel's header.
1235 * Passwords will removed, also home dir will replaced by ~
1237 * @param panel WPanel object
1239 * @return newly allocated string.
1242 static char *
1243 panel_correct_path_to_show (const WPanel * panel)
1245 vfs_path_t *last_vpath;
1246 const vfs_path_element_t *path_element;
1247 char *return_path;
1248 int elements_count;
1250 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1252 /* get last path element */
1253 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1255 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1256 strcmp (path_element->class->name, "extfs") == 0 ||
1257 strcmp (path_element->class->name, "tarfs") == 0))
1259 const char *archive_name;
1260 const vfs_path_element_t *prev_path_element;
1262 /* get previous path element for catching archive name */
1263 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1264 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1265 if (archive_name != NULL)
1266 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1267 else
1269 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1270 last_vpath->relative = TRUE;
1273 else
1274 last_vpath = vfs_path_new (TRUE);
1276 vfs_path_add_element (last_vpath, path_element);
1277 return_path =
1278 vfs_path_to_str_flags (last_vpath, 0,
1279 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1280 vfs_path_free (last_vpath, TRUE);
1282 return return_path;
1285 /* --------------------------------------------------------------------------------------------- */
1287 * Get Current path element encoding
1289 * @param panel WPanel object
1291 * @return newly allocated string or NULL if path charset is same as system charset
1294 #ifdef HAVE_CHARSET
1295 static char *
1296 panel_get_encoding_info_str (const WPanel * panel)
1298 char *ret_str = NULL;
1299 const vfs_path_element_t *path_element;
1301 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1302 if (path_element->encoding != NULL)
1303 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1305 return ret_str;
1307 #endif
1309 /* --------------------------------------------------------------------------------------------- */
1311 static void
1312 show_dir (const WPanel * panel)
1314 const Widget *w = CONST_WIDGET (panel);
1315 gchar *tmp;
1317 set_colors (panel);
1318 tty_draw_box (w->rect.y, w->rect.x, w->rect.lines, w->rect.cols, FALSE);
1320 if (panels_options.show_mini_info)
1322 int y;
1324 y = panel_lines (panel) + 2;
1326 widget_gotoyx (w, y, 0);
1327 tty_print_alt_char (ACS_LTEE, FALSE);
1328 widget_gotoyx (w, y, w->rect.cols - 1);
1329 tty_print_alt_char (ACS_RTEE, FALSE);
1332 widget_gotoyx (w, 0, 1);
1333 tty_print_string (panel_history_prev_item_char);
1335 tmp = panels_options.show_dot_files ? panel_hiddenfiles_show_char : panel_hiddenfiles_hide_char;
1336 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_char,
1337 panel_history_next_item_char);
1339 widget_gotoyx (w, 0, w->rect.cols - 6);
1340 tty_print_string (tmp);
1342 g_free (tmp);
1344 widget_gotoyx (w, 0, 3);
1346 if (panel->is_panelized)
1347 tty_printf (" %s ", _("Panelize"));
1348 #ifdef HAVE_CHARSET
1349 else
1351 tmp = panel_get_encoding_info_str (panel);
1352 if (tmp != NULL)
1354 tty_printf ("%s", tmp);
1355 widget_gotoyx (w, 0, 3 + strlen (tmp));
1356 g_free (tmp);
1359 #endif
1361 if (panel->active)
1362 tty_setcolor (REVERSE_COLOR);
1364 tmp = panel_correct_path_to_show (panel);
1365 tty_printf (" %s ", str_term_trim (tmp, MIN (MAX (w->rect.cols - 12, 0), w->rect.cols)));
1366 g_free (tmp);
1368 if (!panels_options.show_mini_info)
1370 if (panel->marked == 0)
1372 const file_entry_t *fe;
1374 fe = panel_current_entry (panel);
1376 /* Show size of curret file in the bottom of panel */
1377 if (S_ISREG (fe->st.st_mode))
1379 char buffer[BUF_SMALL];
1381 g_snprintf (buffer, sizeof (buffer), " %s ",
1382 size_trunc_sep (fe->st.st_size, panels_options.kilobyte_si));
1383 tty_setcolor (NORMAL_COLOR);
1384 widget_gotoyx (w, w->rect.lines - 1, 4);
1385 tty_print_string (buffer);
1388 else
1390 /* Show total size of marked files
1391 * In the bottom of panel, display size only. */
1392 display_total_marked_size (panel, w->rect.lines - 1, 2, TRUE);
1396 show_free_space (panel);
1398 if (panel->active)
1399 tty_set_normal_attrs ();
1402 /* --------------------------------------------------------------------------------------------- */
1404 static void
1405 adjust_top_file (WPanel * panel)
1407 int items;
1409 /* Update panel->current to avoid out of range in panel->dir.list[panel->current]
1410 * when panel is redrawing when directory is reloading, for example in path:
1411 * dir_list_reload() -> mc_refresh() -> dialog_change_screen_size() ->
1412 * midnight_callback (MSG_RESIZE) -> setup_panels() -> panel_callback(MSG_DRAW) ->
1413 * display_mini_info()
1415 panel->current = CLAMP (panel->current, 0, panel->dir.len - 1);
1417 items = panel_items (panel);
1419 if (panel->dir.len <= items)
1421 /* If all files fit, show them all. */
1422 panel->top = 0;
1424 else
1426 int i;
1428 /* top_file has to be in the range [current-items+1, current] so that
1429 the current file is visible.
1430 top_file should be in the range [0, count-items] so that there's
1431 no empty space wasted.
1432 Within these ranges, adjust it by as little as possible. */
1434 if (panel->top < 0)
1435 panel->top = 0;
1437 i = panel->current - items + 1;
1438 if (panel->top < i)
1439 panel->top = i;
1441 i = panel->dir.len - items;
1442 if (panel->top > i)
1443 panel->top = i;
1445 if (panel->top > panel->current)
1446 panel->top = panel->current;
1450 /* --------------------------------------------------------------------------------------------- */
1451 /** add "#enc:encoding" to end of path */
1452 /* if path ends width a previous #enc:, only encoding is changed no additional
1453 * #enc: is appended
1454 * return new string
1457 static char *
1458 panel_save_name (WPanel * panel)
1460 /* If the program is shutting down */
1461 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1462 return g_strdup (panel->name);
1464 return g_strconcat ("Temporal:", panel->name, (char *) NULL);
1467 /* --------------------------------------------------------------------------------------------- */
1469 static void
1470 directory_history_add (WPanel * panel, const vfs_path_t * vpath)
1472 char *tmp;
1474 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1475 panel->dir_history.list = list_append_unique (panel->dir_history.list, tmp);
1476 panel->dir_history.current = panel->dir_history.list;
1479 /* --------------------------------------------------------------------------------------------- */
1481 /* "history_load" event handler */
1482 static gboolean
1483 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1484 gpointer init_data, gpointer data)
1486 WPanel *p = PANEL (init_data);
1487 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1489 (void) event_group_name;
1490 (void) event_name;
1492 if (ev->receiver == NULL || ev->receiver == WIDGET (p))
1494 if (ev->cfg != NULL)
1495 p->dir_history.list = mc_config_history_load (ev->cfg, p->dir_history.name);
1496 else
1497 p->dir_history.list = mc_config_history_get (p->dir_history.name);
1499 directory_history_add (p, p->cwd_vpath);
1502 return TRUE;
1505 /* --------------------------------------------------------------------------------------------- */
1507 /* "history_save" event handler */
1508 static gboolean
1509 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1510 gpointer init_data, gpointer data)
1512 WPanel *p = PANEL (init_data);
1514 (void) event_group_name;
1515 (void) event_name;
1517 if (p->dir_history.list != NULL)
1519 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1521 mc_config_history_save (ev->cfg, p->dir_history.name, p->dir_history.list);
1524 return TRUE;
1527 /* --------------------------------------------------------------------------------------------- */
1529 static void
1530 panel_destroy (WPanel * p)
1532 size_t i;
1534 if (panels_options.auto_save_setup)
1536 char *name;
1538 name = panel_save_name (p);
1539 panel_save_setup (p, name);
1540 g_free (name);
1543 panel_clean_dir (p);
1545 /* clean history */
1546 if (p->dir_history.list != NULL)
1548 /* directory history is already saved before this moment */
1549 p->dir_history.list = g_list_first (p->dir_history.list);
1550 g_list_free_full (p->dir_history.list, g_free);
1552 g_free (p->dir_history.name);
1554 file_filter_clear (&p->filter);
1556 g_slist_free_full (p->format, (GDestroyNotify) format_item_free);
1557 g_slist_free_full (p->status_format, (GDestroyNotify) format_item_free);
1559 g_free (p->user_format);
1560 for (i = 0; i < LIST_FORMATS; i++)
1561 g_free (p->user_status_format[i]);
1563 g_free (p->name);
1565 panelized_descr_free (p->panelized_descr);
1567 g_string_free (p->quick_search.buffer, TRUE);
1568 g_string_free (p->quick_search.prev_buffer, TRUE);
1570 vfs_path_free (p->lwd_vpath, TRUE);
1571 vfs_path_free (p->cwd_vpath, TRUE);
1574 /* --------------------------------------------------------------------------------------------- */
1576 static void
1577 panel_paint_sort_info (const WPanel * panel)
1579 if (*panel->sort_field->hotkey != '\0')
1581 const char *sort_sign =
1582 panel->sort_info.reverse ? panel_sort_up_char : panel_sort_down_char;
1583 char *str;
1585 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_field->hotkey));
1586 widget_gotoyx (panel, 1, 1);
1587 tty_print_string (str);
1588 g_free (str);
1592 /* --------------------------------------------------------------------------------------------- */
1594 static const char *
1595 panel_get_title_without_hotkey (const char *title)
1597 static char translated_title[BUF_TINY];
1599 if (title == NULL || title[0] == '\0')
1600 translated_title[0] = '\0';
1601 else
1603 char *hkey;
1605 g_snprintf (translated_title, sizeof (translated_title), "%s", _(title));
1607 hkey = strchr (translated_title, '&');
1608 if (hkey != NULL && hkey[1] != '\0')
1609 memmove (hkey, hkey + 1, strlen (hkey));
1612 return translated_title;
1615 /* --------------------------------------------------------------------------------------------- */
1617 static void
1618 panel_print_header (const WPanel * panel)
1620 const Widget *w = CONST_WIDGET (panel);
1622 int y, x;
1623 int i;
1624 GString *format_txt;
1626 widget_gotoyx (w, 1, 1);
1627 tty_getyx (&y, &x);
1628 tty_setcolor (NORMAL_COLOR);
1629 tty_draw_hline (y, x, ' ', w->rect.cols - 2);
1631 format_txt = g_string_new ("");
1633 for (i = 0; i < panel->list_cols; i++)
1635 GSList *format;
1637 for (format = panel->format; format != NULL; format = g_slist_next (format))
1639 format_item_t *fi = (format_item_t *) format->data;
1641 if (fi->string_fn != NULL)
1643 g_string_set_size (format_txt, 0);
1645 if (panel->list_format == list_long && strcmp (fi->id, panel->sort_field->id) == 0)
1646 g_string_append (format_txt,
1647 panel->sort_info.reverse
1648 ? panel_sort_up_char : panel_sort_down_char);
1650 g_string_append (format_txt, fi->title);
1652 if (panel->filter.handler != NULL && strcmp (fi->id, "name") == 0)
1654 g_string_append (format_txt, " [");
1655 g_string_append (format_txt, panel->filter.value);
1656 g_string_append (format_txt, "]");
1659 tty_setcolor (HEADER_COLOR);
1660 tty_print_string (str_fit_to_term (format_txt->str, fi->field_len, J_CENTER_LEFT));
1662 else
1664 tty_setcolor (NORMAL_COLOR);
1665 tty_print_one_vline (TRUE);
1669 if (i < panel->list_cols - 1)
1671 tty_setcolor (NORMAL_COLOR);
1672 tty_print_one_vline (TRUE);
1676 g_string_free (format_txt, TRUE);
1678 if (panel->list_format != list_long)
1679 panel_paint_sort_info (panel);
1682 /* --------------------------------------------------------------------------------------------- */
1684 static const char *
1685 parse_panel_size (WPanel * panel, const char *format, gboolean isstatus)
1687 panel_display_t frame = frame_half;
1689 format = skip_separators (format);
1691 if (strncmp (format, "full", 4) == 0)
1693 frame = frame_full;
1694 format += 4;
1696 else if (strncmp (format, "half", 4) == 0)
1698 frame = frame_half;
1699 format += 4;
1702 if (!isstatus)
1704 panel->frame_size = frame;
1705 panel->list_cols = 1;
1708 /* Now, the optional column specifier */
1709 format = skip_separators (format);
1711 if (g_ascii_isdigit (*format))
1713 if (!isstatus)
1715 panel->list_cols = g_ascii_digit_value (*format);
1716 if (panel->list_cols < 1)
1717 panel->list_cols = 1;
1720 format++;
1723 if (!isstatus)
1724 panel_update_cols (WIDGET (panel), panel->frame_size);
1726 return skip_separators (format);
1729 /* --------------------------------------------------------------------------------------------- */
1731 /* *INDENT-OFF* */
1732 /* Format is:
1734 all := panel_format? format
1735 panel_format := [full|half] [1-9]
1736 format := one_format_item_t
1737 | format , one_format_item_t
1739 one_format_item_t := just format.id [opt_size]
1740 just := [<=>]
1741 opt_size := : size [opt_expand]
1742 size := [0-9]+
1743 opt_expand := +
1746 /* *INDENT-ON* */
1748 static GSList *
1749 parse_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus,
1750 int *res_total_cols)
1752 GSList *home = NULL; /* The formats we return */
1753 int total_cols = 0; /* Used columns by the format */
1754 size_t i;
1756 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1758 *error = NULL;
1760 if (i18n_timelength == 0)
1762 i18n_timelength = i18n_checktimelength (); /* Mustn't be 0 */
1764 for (i = 0; panel_fields[i].id != NULL; i++)
1765 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1766 panel_fields[i].min_size = i18n_timelength;
1770 * This makes sure that the panel and mini status full/half mode
1771 * setting is equal
1773 format = parse_panel_size (panel, format, isstatus);
1775 while (*format != '\0')
1776 { /* format can be an empty string */
1777 format_item_t *darr;
1778 align_crt_t justify; /* Which mode. */
1779 gboolean set_justify = TRUE; /* flag: set justification mode? */
1780 gboolean found = FALSE;
1781 size_t klen = 0;
1783 darr = g_new0 (format_item_t, 1);
1784 home = g_slist_append (home, darr);
1786 format = skip_separators (format);
1788 switch (*format)
1790 case '<':
1791 justify = J_LEFT;
1792 format = skip_separators (format + 1);
1793 break;
1794 case '=':
1795 justify = J_CENTER;
1796 format = skip_separators (format + 1);
1797 break;
1798 case '>':
1799 justify = J_RIGHT;
1800 format = skip_separators (format + 1);
1801 break;
1802 default:
1803 justify = J_LEFT;
1804 set_justify = FALSE;
1805 break;
1808 for (i = 0; !found && panel_fields[i].id != NULL; i++)
1810 klen = strlen (panel_fields[i].id);
1811 found = strncmp (format, panel_fields[i].id, klen) == 0;
1814 if (found)
1816 i--;
1817 format += klen;
1819 darr->requested_field_len = panel_fields[i].min_size;
1820 darr->string_fn = panel_fields[i].string_fn;
1821 darr->title = g_strdup (panel_get_title_without_hotkey (panel_fields[i].title_hotkey));
1822 darr->id = panel_fields[i].id;
1823 darr->expand = panel_fields[i].expands;
1824 darr->just_mode = panel_fields[i].default_just;
1826 if (set_justify)
1828 if (IS_FIT (darr->just_mode))
1829 darr->just_mode = MAKE_FIT (justify);
1830 else
1831 darr->just_mode = justify;
1834 format = skip_separators (format);
1836 /* If we have a size specifier */
1837 if (*format == ':')
1839 int req_length;
1841 /* If the size was specified, we don't want
1842 * auto-expansion by default
1844 darr->expand = FALSE;
1845 format++;
1846 req_length = atoi (format);
1847 darr->requested_field_len = req_length;
1849 format = skip_numbers (format);
1851 /* Now, if they insist on expansion */
1852 if (*format == '+')
1854 darr->expand = TRUE;
1855 format++;
1859 else
1861 size_t pos;
1862 char *tmp_format;
1864 pos = strlen (format);
1865 if (pos > 8)
1866 pos = 8;
1868 tmp_format = g_strndup (format, pos);
1869 g_slist_free_full (home, (GDestroyNotify) format_item_free);
1870 *error =
1871 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1872 g_free (tmp_format);
1874 return NULL;
1877 total_cols += darr->requested_field_len;
1880 *res_total_cols = total_cols;
1881 return home;
1884 /* --------------------------------------------------------------------------------------------- */
1886 static GSList *
1887 use_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus)
1889 #define MAX_EXPAND 4
1890 int expand_top = 0; /* Max used element in expand */
1891 int usable_columns; /* Usable columns in the panel */
1892 int total_cols = 0;
1893 GSList *darr, *home;
1895 if (format == NULL)
1896 format = DEFAULT_USER_FORMAT;
1898 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1900 if (*error != NULL)
1901 return NULL;
1903 panel->dirty = TRUE;
1905 usable_columns = WIDGET (panel)->rect.cols - 2;
1906 /* Status needn't to be split */
1907 if (!isstatus)
1909 usable_columns /= panel->list_cols;
1910 if (panel->list_cols > 1)
1911 usable_columns--;
1914 /* Look for the expandable fields and set field_len based on the requested field len */
1915 for (darr = home; darr != NULL && expand_top < MAX_EXPAND; darr = g_slist_next (darr))
1917 format_item_t *fi = (format_item_t *) darr->data;
1919 fi->field_len = fi->requested_field_len;
1920 if (fi->expand)
1921 expand_top++;
1924 /* If we used more columns than the available columns, adjust that */
1925 if (total_cols > usable_columns)
1927 int dif;
1928 int pdif = 0;
1930 dif = total_cols - usable_columns;
1932 while (dif != 0 && pdif != dif)
1934 pdif = dif;
1936 for (darr = home; darr != NULL; darr = g_slist_next (darr))
1938 format_item_t *fi = (format_item_t *) darr->data;
1940 if (dif != 0 && fi->field_len != 1)
1942 fi->field_len--;
1943 dif--;
1948 total_cols = usable_columns; /* give up, the rest should be truncated */
1951 /* Expand the available space */
1952 if (usable_columns > total_cols && expand_top != 0)
1954 int i;
1955 int spaces;
1957 spaces = (usable_columns - total_cols) / expand_top;
1959 for (i = 0, darr = home; darr != NULL && i < expand_top; darr = g_slist_next (darr))
1961 format_item_t *fi = (format_item_t *) darr->data;
1963 if (fi->expand)
1965 fi->field_len += spaces;
1966 if (i == 0)
1967 fi->field_len += (usable_columns - total_cols) % expand_top;
1968 i++;
1973 return home;
1976 /* --------------------------------------------------------------------------------------------- */
1977 /** Given the panel->view_type returns the format string to be parsed */
1979 static const char *
1980 panel_format (WPanel * panel)
1982 switch (panel->list_format)
1984 case list_long:
1985 return "full perm space nlink space owner space group space size space mtime space name";
1987 case list_brief:
1989 static char format[BUF_TINY];
1990 int brief_cols = panel->brief_cols;
1992 if (brief_cols < 1)
1993 brief_cols = 2;
1995 if (brief_cols > 9)
1996 brief_cols = 9;
1998 g_snprintf (format, sizeof (format), "half %d type name", brief_cols);
1999 return format;
2002 case list_user:
2003 return panel->user_format;
2005 default:
2006 case list_full:
2007 return "half type name | size | mtime";
2011 /* --------------------------------------------------------------------------------------------- */
2013 static const char *
2014 mini_status_format (WPanel * panel)
2016 if (panel->user_mini_status)
2017 return panel->user_status_format[panel->list_format];
2019 switch (panel->list_format)
2021 case list_long:
2022 return "full perm space nlink space owner space group space size space mtime space name";
2024 case list_brief:
2025 return "half type name space bsize space perm space";
2027 case list_full:
2028 return "half type name";
2030 default:
2031 case list_user:
2032 return panel->user_format;
2036 /* */
2037 /* Panel operation commands */
2038 /* */
2040 /* --------------------------------------------------------------------------------------------- */
2042 static void
2043 cd_up_dir (WPanel * panel)
2045 vfs_path_t *up_dir;
2047 up_dir = vfs_path_from_str ("..");
2048 panel_cd (panel, up_dir, cd_exact);
2049 vfs_path_free (up_dir, TRUE);
2052 /* --------------------------------------------------------------------------------------------- */
2053 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
2055 static cb_ret_t
2056 maybe_cd (WPanel * panel, gboolean move_up_dir)
2058 if (panels_options.navigate_with_arrows && input_is_empty (cmdline))
2060 const file_entry_t *fe;
2062 if (move_up_dir)
2064 cd_up_dir (panel);
2065 return MSG_HANDLED;
2068 fe = panel_current_entry (panel);
2070 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe))
2072 vfs_path_t *vpath;
2074 vpath = vfs_path_from_str (fe->fname->str);
2075 panel_cd (panel, vpath, cd_exact);
2076 vfs_path_free (vpath, TRUE);
2077 return MSG_HANDLED;
2081 return MSG_NOT_HANDLED;
2084 /* --------------------------------------------------------------------------------------------- */
2086 /* if command line is empty then do 'cd ..' */
2087 static cb_ret_t
2088 force_maybe_cd (WPanel * panel)
2090 if (input_is_empty (cmdline))
2092 cd_up_dir (panel);
2093 return MSG_HANDLED;
2096 return MSG_NOT_HANDLED;
2099 /* --------------------------------------------------------------------------------------------- */
2101 static inline void
2102 unselect_item (WPanel * panel)
2104 repaint_file (panel, panel->current,
2105 panel_current_entry (panel)->f.marked != 0 ? FATTR_MARKED : FATTR_NORMAL);
2108 /* --------------------------------------------------------------------------------------------- */
2109 /** Select/unselect all the files like a current file by extension */
2111 static void
2112 panel_select_ext_cmd (WPanel * panel)
2114 const file_entry_t *fe;
2115 GString *filename;
2116 gboolean do_select;
2117 char *reg_exp, *cur_file_ext;
2118 mc_search_t *search;
2119 int i;
2121 fe = panel_current_entry (panel);
2123 filename = fe->fname;
2124 if (filename == NULL)
2125 return;
2127 do_select = (fe->f.marked == 0);
2129 cur_file_ext = str_regex_escape (extension (filename->str));
2130 if (cur_file_ext[0] != '\0')
2131 reg_exp = g_strconcat ("^.*\\.", cur_file_ext, "$", (char *) NULL);
2132 else
2133 reg_exp = g_strdup ("^[^\\.]+$");
2135 g_free (cur_file_ext);
2137 search = mc_search_new (reg_exp, NULL);
2138 search->search_type = MC_SEARCH_T_REGEX;
2139 search->is_case_sensitive = FALSE;
2141 for (i = 0; i < panel->dir.len; i++)
2143 fe = &panel->dir.list[i];
2145 if (DIR_IS_DOTDOT (fe->fname->str) || S_ISDIR (fe->st.st_mode))
2146 continue;
2148 if (!mc_search_run (search, fe->fname->str, 0, fe->fname->len, NULL))
2149 continue;
2151 do_file_mark (panel, i, do_select ? 1 : 0);
2154 mc_search_free (search);
2155 g_free (reg_exp);
2158 /* --------------------------------------------------------------------------------------------- */
2160 static int
2161 panel_current_at_half (const WPanel * panel)
2163 int lines, top;
2165 lines = panel_lines (panel);
2167 /* define top file of column */
2168 top = panel->top;
2169 if (panel->list_cols > 1)
2170 top += lines * ((panel->current - top) / lines);
2172 return (panel->current - top - lines / 2);
2175 /* --------------------------------------------------------------------------------------------- */
2177 static void
2178 move_down (WPanel * panel)
2180 int items;
2182 if (panel->current + 1 == panel->dir.len)
2183 return;
2185 unselect_item (panel);
2186 panel->current++;
2188 items = panel_items (panel);
2190 if (panels_options.scroll_pages && panel->current - panel->top == items)
2192 /* Scroll window half screen */
2193 panel->top += items / 2;
2194 if (panel->top > panel->dir.len - items)
2195 panel->top = panel->dir.len - items;
2196 paint_dir (panel);
2198 else if (panels_options.scroll_center && panel_current_at_half (panel) > 0)
2200 /* Scroll window when cursor is halfway down */
2201 panel->top++;
2202 if (panel->top > panel->dir.len - items)
2203 panel->top = panel->dir.len - items;
2205 select_item (panel);
2208 /* --------------------------------------------------------------------------------------------- */
2210 static void
2211 move_up (WPanel * panel)
2213 if (panel->current == 0)
2214 return;
2216 unselect_item (panel);
2217 panel->current--;
2219 if (panels_options.scroll_pages && panel->current < panel->top)
2221 /* Scroll window half screen */
2222 panel->top -= panel_items (panel) / 2;
2223 if (panel->top < 0)
2224 panel->top = 0;
2225 paint_dir (panel);
2227 else if (panels_options.scroll_center && panel_current_at_half (panel) < 0)
2229 /* Scroll window when cursor is halfway up */
2230 panel->top--;
2231 if (panel->top < 0)
2232 panel->top = 0;
2234 select_item (panel);
2237 /* --------------------------------------------------------------------------------------------- */
2238 /** Changes the current by lines (may be negative) */
2240 static void
2241 panel_move_current (WPanel * panel, int lines)
2243 int new_pos;
2244 gboolean adjust = FALSE;
2246 new_pos = panel->current + lines;
2247 if (new_pos >= panel->dir.len)
2248 new_pos = panel->dir.len - 1;
2250 if (new_pos < 0)
2251 new_pos = 0;
2253 unselect_item (panel);
2254 panel->current = new_pos;
2256 if (panel->current - panel->top >= panel_items (panel))
2258 panel->top += lines;
2259 adjust = TRUE;
2262 if (panel->current - panel->top < 0)
2264 panel->top += lines;
2265 adjust = TRUE;
2268 if (adjust)
2270 if (panel->top > panel->current)
2271 panel->top = panel->current;
2272 if (panel->top < 0)
2273 panel->top = 0;
2274 paint_dir (panel);
2276 select_item (panel);
2279 /* --------------------------------------------------------------------------------------------- */
2281 static cb_ret_t
2282 move_left (WPanel * panel)
2284 if (panel->list_cols > 1)
2286 panel_move_current (panel, -panel_lines (panel));
2287 return MSG_HANDLED;
2290 return maybe_cd (panel, TRUE); /* cd .. */
2293 /* --------------------------------------------------------------------------------------------- */
2295 static cb_ret_t
2296 move_right (WPanel * panel)
2298 if (panel->list_cols > 1)
2300 panel_move_current (panel, panel_lines (panel));
2301 return MSG_HANDLED;
2304 return maybe_cd (panel, FALSE); /* cd (current) */
2307 /* --------------------------------------------------------------------------------------------- */
2309 static void
2310 prev_page (WPanel * panel)
2312 int items;
2314 if (panel->current == 0 && panel->top == 0)
2315 return;
2317 unselect_item (panel);
2318 items = panel_items (panel);
2319 if (panel->top < items)
2320 items = panel->top;
2321 if (items == 0)
2322 panel->current = 0;
2323 else
2324 panel->current -= items;
2325 panel->top -= items;
2327 select_item (panel);
2328 paint_dir (panel);
2331 /* --------------------------------------------------------------------------------------------- */
2333 static void
2334 goto_parent_dir (WPanel * panel)
2336 if (!panel->is_panelized)
2337 cd_up_dir (panel);
2338 else
2340 GString *fname;
2341 const char *bname;
2342 vfs_path_t *dname_vpath;
2344 fname = panel_current_entry (panel)->fname;
2346 if (g_path_is_absolute (fname->str))
2347 fname = mc_g_string_dup (fname);
2348 else
2350 char *fname2;
2352 fname2 =
2353 mc_build_filename (vfs_path_as_str (panel->panelized_descr->root_vpath), fname->str,
2354 (char *) NULL);
2356 fname = g_string_new_take (fname2);
2359 bname = x_basename (fname->str);
2361 if (bname == fname->str)
2362 dname_vpath = vfs_path_from_str (".");
2363 else
2365 g_string_truncate (fname, bname - fname->str);
2366 dname_vpath = vfs_path_from_str (fname->str);
2369 panel_cd (panel, dname_vpath, cd_exact);
2370 panel_set_current_by_name (panel, bname);
2372 vfs_path_free (dname_vpath, TRUE);
2373 g_string_free (fname, TRUE);
2377 /* --------------------------------------------------------------------------------------------- */
2379 static void
2380 next_page (WPanel * panel)
2382 int items;
2384 if (panel->current == panel->dir.len - 1)
2385 return;
2387 unselect_item (panel);
2388 items = panel_items (panel);
2389 if (panel->top > panel->dir.len - 2 * items)
2390 items = panel->dir.len - items - panel->top;
2391 if (panel->top + items < 0)
2392 items = -panel->top;
2393 if (items == 0)
2394 panel->current = panel->dir.len - 1;
2395 else
2396 panel->current += items;
2397 panel->top += items;
2399 select_item (panel);
2400 paint_dir (panel);
2403 /* --------------------------------------------------------------------------------------------- */
2405 static void
2406 goto_child_dir (WPanel * panel)
2408 const file_entry_t *fe;
2410 fe = panel_current_entry (panel);
2412 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe))
2414 vfs_path_t *vpath;
2416 vpath = vfs_path_from_str (fe->fname->str);
2417 panel_cd (panel, vpath, cd_exact);
2418 vfs_path_free (vpath, TRUE);
2422 /* --------------------------------------------------------------------------------------------- */
2424 static void
2425 goto_top_file (WPanel * panel)
2427 unselect_item (panel);
2428 panel->current = panel->top;
2429 select_item (panel);
2432 /* --------------------------------------------------------------------------------------------- */
2434 static void
2435 goto_middle_file (WPanel * panel)
2437 unselect_item (panel);
2438 panel->current = panel->top + panel_items (panel) / 2;
2439 select_item (panel);
2442 /* --------------------------------------------------------------------------------------------- */
2444 static void
2445 goto_bottom_file (WPanel * panel)
2447 unselect_item (panel);
2448 panel->current = panel->top + panel_items (panel) - 1;
2449 select_item (panel);
2452 /* --------------------------------------------------------------------------------------------- */
2454 static void
2455 move_home (WPanel * panel)
2457 if (panel->current == 0)
2458 return;
2460 unselect_item (panel);
2462 if (panels_options.torben_fj_mode)
2464 int middle_pos;
2466 middle_pos = panel->top + panel_items (panel) / 2;
2468 if (panel->current > middle_pos)
2470 goto_middle_file (panel);
2471 return;
2473 if (panel->current != panel->top)
2475 goto_top_file (panel);
2476 return;
2480 panel->top = 0;
2481 panel->current = 0;
2483 paint_dir (panel);
2484 select_item (panel);
2487 /* --------------------------------------------------------------------------------------------- */
2489 static void
2490 move_end (WPanel * panel)
2492 if (panel->current == panel->dir.len - 1)
2493 return;
2495 unselect_item (panel);
2497 if (panels_options.torben_fj_mode)
2499 int items, middle_pos;
2501 items = panel_items (panel);
2502 middle_pos = panel->top + items / 2;
2504 if (panel->current < middle_pos)
2506 goto_middle_file (panel);
2507 return;
2509 if (panel->current != panel->top + items - 1)
2511 goto_bottom_file (panel);
2512 return;
2516 panel->current = panel->dir.len - 1;
2517 paint_dir (panel);
2518 select_item (panel);
2521 /* --------------------------------------------------------------------------------------------- */
2523 static void
2524 do_mark_file (WPanel * panel, mark_act_t do_move)
2526 do_file_mark (panel, panel->current, panel_current_entry (panel)->f.marked ? 0 : 1);
2528 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2529 move_down (panel);
2530 else if (do_move == MARK_FORCE_UP)
2531 move_up (panel);
2534 /* --------------------------------------------------------------------------------------------- */
2536 static inline void
2537 mark_file (WPanel * panel)
2539 do_mark_file (panel, MARK_DOWN);
2542 /* --------------------------------------------------------------------------------------------- */
2544 static inline void
2545 mark_file_up (WPanel * panel)
2547 do_mark_file (panel, MARK_FORCE_UP);
2550 /* --------------------------------------------------------------------------------------------- */
2552 static inline void
2553 mark_file_down (WPanel * panel)
2555 do_mark_file (panel, MARK_FORCE_DOWN);
2558 /* --------------------------------------------------------------------------------------------- */
2560 static void
2561 mark_file_right (WPanel * panel)
2563 int lines;
2565 if (state_mark < 0)
2566 state_mark = panel_current_entry (panel)->f.marked ? 0 : 1;
2568 lines = panel_lines (panel);
2569 lines = MIN (lines, panel->dir.len - panel->current - 1);
2570 for (; lines != 0; lines--)
2572 do_file_mark (panel, panel->current, state_mark);
2573 move_down (panel);
2575 do_file_mark (panel, panel->current, state_mark);
2578 /* --------------------------------------------------------------------------------------------- */
2580 static void
2581 mark_file_left (WPanel * panel)
2583 int lines;
2585 if (state_mark < 0)
2586 state_mark = panel_current_entry (panel)->f.marked ? 0 : 1;
2588 lines = panel_lines (panel);
2589 lines = MIN (lines, panel->current + 1);
2590 for (; lines != 0; lines--)
2592 do_file_mark (panel, panel->current, state_mark);
2593 move_up (panel);
2595 do_file_mark (panel, panel->current, state_mark);
2598 /* --------------------------------------------------------------------------------------------- */
2600 static mc_search_t *
2601 panel_select_unselect_files_dialog (select_flags_t * flags, const char *title,
2602 const char *history_name, const char *help_section, char **str)
2604 gboolean files_only = (*flags & SELECT_FILES_ONLY) != 0;
2605 gboolean case_sens = (*flags & SELECT_MATCH_CASE) != 0;
2606 gboolean shell_patterns = (*flags & SELECT_SHELL_PATTERNS) != 0;
2608 char *reg_exp;
2609 mc_search_t *search;
2611 quick_widget_t quick_widgets[] = {
2612 /* *INDENT-OFF* */
2613 QUICK_INPUT (INPUT_LAST_TEXT, history_name, &reg_exp, NULL,
2614 FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
2615 QUICK_START_COLUMNS,
2616 QUICK_CHECKBOX (N_("&Files only"), &files_only, NULL),
2617 QUICK_CHECKBOX (N_("&Using shell patterns"), &shell_patterns, NULL),
2618 QUICK_NEXT_COLUMN,
2619 QUICK_CHECKBOX (N_("&Case sensitive"), &case_sens, NULL),
2620 QUICK_STOP_COLUMNS,
2621 QUICK_END
2622 /* *INDENT-ON* */
2625 WRect r = { -1, -1, 0, 50 };
2627 quick_dialog_t qdlg = {
2628 r, title, help_section,
2629 quick_widgets, NULL, NULL
2632 if (quick_dialog (&qdlg) == B_CANCEL)
2633 return NULL;
2635 if (*reg_exp == '\0')
2637 g_free (reg_exp);
2638 if (str != NULL)
2639 *str = NULL;
2640 return SELECT_RESET;
2643 search = mc_search_new (reg_exp, NULL);
2644 search->search_type = shell_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
2645 search->is_entire_line = TRUE;
2646 search->is_case_sensitive = case_sens;
2648 if (str != NULL)
2649 *str = reg_exp;
2650 else
2651 g_free (reg_exp);
2653 if (!mc_search_prepare (search))
2655 message (D_ERROR, MSG_ERROR, _("Malformed regular expression"));
2656 mc_search_free (search);
2657 return SELECT_ERROR;
2660 /* result flags */
2661 *flags = 0;
2662 if (case_sens)
2663 *flags |= SELECT_MATCH_CASE;
2664 if (files_only)
2665 *flags |= SELECT_FILES_ONLY;
2666 if (shell_patterns)
2667 *flags |= SELECT_SHELL_PATTERNS;
2669 return search;
2672 /* --------------------------------------------------------------------------------------------- */
2674 static void
2675 panel_select_unselect_files (WPanel * panel, const char *title, const char *history_name,
2676 const char *help_section, gboolean do_select)
2678 mc_search_t *search;
2679 gboolean files_only;
2680 int i;
2682 search = panel_select_unselect_files_dialog (&panels_options.select_flags, title, history_name,
2683 help_section, NULL);
2684 if (search == NULL || search == SELECT_RESET || search == SELECT_ERROR)
2685 return;
2687 files_only = (panels_options.select_flags & SELECT_FILES_ONLY) != 0;
2689 for (i = 0; i < panel->dir.len; i++)
2691 if (DIR_IS_DOTDOT (panel->dir.list[i].fname->str))
2692 continue;
2693 if (S_ISDIR (panel->dir.list[i].st.st_mode) && files_only)
2694 continue;
2696 if (mc_search_run
2697 (search, panel->dir.list[i].fname->str, 0, panel->dir.list[i].fname->len, NULL))
2698 do_file_mark (panel, i, do_select ? 1 : 0);
2701 mc_search_free (search);
2704 /* --------------------------------------------------------------------------------------------- */
2706 static void
2707 panel_select_files (WPanel * panel)
2709 panel_select_unselect_files (panel, _("Select"), MC_HISTORY_FM_PANEL_SELECT,
2710 "[Select/Unselect Files]", TRUE);
2713 /* --------------------------------------------------------------------------------------------- */
2715 static void
2716 panel_unselect_files (WPanel * panel)
2718 panel_select_unselect_files (panel, _("Unselect"), MC_HISTORY_FM_PANEL_UNSELECT,
2719 "[Select/Unselect Files]", FALSE);
2722 /* --------------------------------------------------------------------------------------------- */
2724 static void
2725 panel_select_invert_files (WPanel * panel)
2727 int i;
2729 for (i = 0; i < panel->dir.len; i++)
2731 file_entry_t *file = &panel->dir.list[i];
2733 if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
2734 do_file_mark (panel, i, file->f.marked ? 0 : 1);
2738 /* --------------------------------------------------------------------------------------------- */
2740 static void
2741 panel_do_set_filter (WPanel * panel)
2743 /* *INDENT-OFF* */
2744 file_filter_t ff = { .value = NULL, .handler = NULL, .flags = panel->filter.flags };
2745 /* *INDENT-ON* */
2747 ff.handler =
2748 panel_select_unselect_files_dialog (&ff.flags, _("Filter"), MC_HISTORY_FM_PANEL_FILTER,
2749 "[Filter...]", &ff.value);
2751 if (ff.handler == NULL || ff.handler == SELECT_ERROR)
2752 return;
2754 if (ff.handler == SELECT_RESET)
2755 ff.handler = NULL;
2757 panel_set_filter (panel, &ff);
2760 /* --------------------------------------------------------------------------------------------- */
2761 /** Incremental search of a file name in the panel.
2762 * @param panel instance of WPanel structure
2763 * @param c_code key code
2766 static void
2767 do_search (WPanel * panel, int c_code)
2769 int curr;
2770 int i;
2771 gboolean wrapped = FALSE;
2772 char *act;
2773 mc_search_t *search;
2774 char *reg_exp, *esc_str;
2775 gboolean is_found = FALSE;
2777 if (c_code == KEY_BACKSPACE)
2779 if (panel->quick_search.buffer->len != 0)
2781 act = panel->quick_search.buffer->str + panel->quick_search.buffer->len;
2782 str_prev_noncomb_char (&act, panel->quick_search.buffer->str);
2783 g_string_set_size (panel->quick_search.buffer, act - panel->quick_search.buffer->str);
2785 panel->quick_search.chpoint = 0;
2787 else
2789 if (c_code != 0 && (gsize) panel->quick_search.chpoint < sizeof (panel->quick_search.ch))
2791 panel->quick_search.ch[panel->quick_search.chpoint] = c_code;
2792 panel->quick_search.chpoint++;
2795 if (panel->quick_search.chpoint > 0)
2797 switch (str_is_valid_char (panel->quick_search.ch, panel->quick_search.chpoint))
2799 case -2:
2800 return;
2801 case -1:
2802 panel->quick_search.chpoint = 0;
2803 return;
2804 default:
2805 g_string_append_len (panel->quick_search.buffer, panel->quick_search.ch,
2806 panel->quick_search.chpoint);
2807 panel->quick_search.chpoint = 0;
2812 reg_exp = g_strdup_printf ("%s*", panel->quick_search.buffer->str);
2813 esc_str = str_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2814 search = mc_search_new (esc_str, NULL);
2815 search->search_type = MC_SEARCH_T_GLOB;
2816 search->is_entire_line = TRUE;
2818 switch (panels_options.qsearch_mode)
2820 case QSEARCH_CASE_SENSITIVE:
2821 search->is_case_sensitive = TRUE;
2822 break;
2823 case QSEARCH_CASE_INSENSITIVE:
2824 search->is_case_sensitive = FALSE;
2825 break;
2826 default:
2827 search->is_case_sensitive = panel->sort_info.case_sensitive;
2828 break;
2831 curr = panel->current;
2833 for (i = panel->current; !wrapped || i != panel->current; i++)
2835 if (i >= panel->dir.len)
2837 i = 0;
2838 if (wrapped)
2839 break;
2840 wrapped = TRUE;
2842 if (mc_search_run
2843 (search, panel->dir.list[i].fname->str, 0, panel->dir.list[i].fname->len, NULL))
2845 curr = i;
2846 is_found = TRUE;
2847 break;
2850 if (is_found)
2852 unselect_item (panel);
2853 panel->current = curr;
2854 select_item (panel);
2855 widget_draw (WIDGET (panel));
2857 else if (c_code != KEY_BACKSPACE)
2859 act = panel->quick_search.buffer->str + panel->quick_search.buffer->len;
2860 str_prev_noncomb_char (&act, panel->quick_search.buffer->str);
2861 g_string_set_size (panel->quick_search.buffer, act - panel->quick_search.buffer->str);
2863 mc_search_free (search);
2864 g_free (reg_exp);
2865 g_free (esc_str);
2868 /* --------------------------------------------------------------------------------------------- */
2869 /** Start new search.
2870 * @param panel instance of WPanel structure
2873 static void
2874 start_search (WPanel * panel)
2876 if (panel->quick_search.active)
2878 if (panel->current == panel->dir.len - 1)
2879 panel->current = 0;
2880 else
2881 move_down (panel);
2883 /* in case if there was no search string we need to recall
2884 previous string, with which we ended previous searching */
2885 if (panel->quick_search.buffer->len == 0)
2886 mc_g_string_copy (panel->quick_search.buffer, panel->quick_search.prev_buffer);
2888 do_search (panel, 0);
2890 else
2892 panel->quick_search.active = TRUE;
2893 g_string_set_size (panel->quick_search.buffer, 0);
2894 panel->quick_search.ch[0] = '\0';
2895 panel->quick_search.chpoint = 0;
2896 display_mini_info (panel);
2900 /* --------------------------------------------------------------------------------------------- */
2902 static void
2903 stop_search (WPanel * panel)
2905 if (!panel->quick_search.active)
2906 return;
2908 panel->quick_search.active = FALSE;
2910 /* if user overrdied search string, we need to store it
2911 to the quick_search.prev_buffer */
2912 if (panel->quick_search.buffer->len != 0)
2913 mc_g_string_copy (panel->quick_search.prev_buffer, panel->quick_search.buffer);
2915 display_mini_info (panel);
2918 /* --------------------------------------------------------------------------------------------- */
2919 /** Return TRUE if the Enter key has been processed, FALSE otherwise */
2921 static gboolean
2922 do_enter_on_file_entry (WPanel * panel, file_entry_t * fe)
2924 const char *fname = fe->fname->str;
2925 char *fname_quoted;
2926 vfs_path_t *full_name_vpath;
2927 gboolean ok;
2930 * Directory or link to directory - change directory.
2931 * Try the same for the entries on which mc_lstat() has failed.
2933 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2935 vfs_path_t *fname_vpath;
2937 fname_vpath = vfs_path_from_str (fname);
2938 if (!panel_cd (panel, fname_vpath, cd_exact))
2939 cd_error_message (fname);
2940 vfs_path_free (fname_vpath, TRUE);
2941 return TRUE;
2944 full_name_vpath = vfs_path_append_new (panel->cwd_vpath, fname, (char *) NULL);
2946 /* Try associated command */
2947 ok = regex_command (full_name_vpath, "Open") != 0;
2948 vfs_path_free (full_name_vpath, TRUE);
2949 if (ok)
2950 return TRUE;
2952 /* Check if the file is executable */
2953 full_name_vpath = vfs_path_append_new (panel->cwd_vpath, fname, (char *) NULL);
2954 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2955 vfs_path_free (full_name_vpath, TRUE);
2956 if (!ok)
2957 return FALSE;
2959 if (confirm_execute
2960 && query_dialog (_("The Midnight Commander"), _("Do you really want to execute?"), D_NORMAL,
2961 2, _("&Yes"), _("&No")) != 0)
2962 return TRUE;
2964 if (!vfs_current_is_local ())
2966 int ret;
2967 vfs_path_t *tmp_vpath;
2969 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fname, (char *) NULL);
2970 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2971 vfs_path_free (tmp_vpath, TRUE);
2972 /* We took action only if the dialog was shown or the execution was successful */
2973 return confirm_execute || (ret == 0);
2976 fname_quoted = name_quote (fname, FALSE);
2977 if (fname_quoted != NULL)
2979 char *cmd;
2981 cmd = g_strconcat ("." PATH_SEP_STR, fname_quoted, (char *) NULL);
2982 g_free (fname_quoted);
2984 shell_execute (cmd, 0);
2985 g_free (cmd);
2988 #ifdef HAVE_CHARSET
2989 mc_global.source_codepage = default_source_codepage;
2990 #endif
2992 return TRUE;
2995 /* --------------------------------------------------------------------------------------------- */
2997 static inline gboolean
2998 do_enter (WPanel * panel)
3000 return do_enter_on_file_entry (panel, panel_current_entry (panel));
3003 /* --------------------------------------------------------------------------------------------- */
3005 static void
3006 panel_cycle_listing_format (WPanel * panel)
3008 panel->list_format = (panel->list_format + 1) % LIST_FORMATS;
3010 if (set_panel_formats (panel) == 0)
3011 do_refresh ();
3014 /* --------------------------------------------------------------------------------------------- */
3016 static void
3017 chdir_other_panel (WPanel * panel)
3019 const file_entry_t *entry;
3020 vfs_path_t *new_dir_vpath;
3021 char *curr_entry = NULL;
3022 WPanel *p;
3024 entry = panel_current_entry (panel);
3026 if (get_other_type () != view_listing)
3027 create_panel (get_other_index (), view_listing);
3029 if (S_ISDIR (entry->st.st_mode) || link_isdir (entry))
3030 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname->str, (char *) NULL);
3031 else
3033 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
3034 curr_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
3037 p = change_panel ();
3038 panel_cd (p, new_dir_vpath, cd_exact);
3039 vfs_path_free (new_dir_vpath, TRUE);
3041 if (curr_entry != NULL)
3042 panel_set_current_by_name (p, curr_entry);
3043 (void) change_panel ();
3045 move_down (panel);
3048 /* --------------------------------------------------------------------------------------------- */
3050 * Make the current directory of the current panel also the current
3051 * directory of the other panel. Put the other panel to the listing
3052 * mode if needed. If the current panel is panelized, the other panel
3053 * doesn't become panelized.
3056 static void
3057 panel_sync_other (const WPanel * panel)
3059 if (get_other_type () != view_listing)
3060 create_panel (get_other_index (), view_listing);
3062 panel_do_cd (other_panel, panel->cwd_vpath, cd_exact);
3064 /* try to set current filename on the other panel */
3065 if (!panel->is_panelized)
3066 panel_set_current_by_name (other_panel, panel_current_entry (panel)->fname->str);
3069 /* --------------------------------------------------------------------------------------------- */
3071 static void
3072 chdir_to_readlink (WPanel * panel)
3074 const file_entry_t *fe;
3075 vfs_path_t *new_dir_vpath;
3076 char buffer[MC_MAXPATHLEN];
3077 int i;
3078 struct stat st;
3079 vfs_path_t *panel_fname_vpath;
3080 gboolean ok;
3081 WPanel *cpanel;
3083 if (get_other_type () != view_listing)
3084 return;
3086 fe = panel_current_entry (panel);
3088 if (!S_ISLNK (fe->st.st_mode))
3089 return;
3091 i = readlink (fe->fname->str, buffer, MC_MAXPATHLEN - 1);
3092 if (i < 0)
3093 return;
3095 panel_fname_vpath = vfs_path_from_str (fe->fname->str);
3096 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
3097 vfs_path_free (panel_fname_vpath, TRUE);
3098 if (!ok)
3099 return;
3101 buffer[i] = '\0';
3102 if (!S_ISDIR (st.st_mode))
3104 char *p;
3106 p = strrchr (buffer, PATH_SEP);
3107 if (p != NULL && p[1] == '\0')
3109 *p = '\0';
3110 p = strrchr (buffer, PATH_SEP);
3112 if (p == NULL)
3113 return;
3115 p[1] = '\0';
3117 if (IS_PATH_SEP (*buffer))
3118 new_dir_vpath = vfs_path_from_str (buffer);
3119 else
3120 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, (char *) NULL);
3122 cpanel = change_panel ();
3123 panel_cd (cpanel, new_dir_vpath, cd_exact);
3124 vfs_path_free (new_dir_vpath, TRUE);
3125 (void) change_panel ();
3127 move_down (panel);
3130 /* --------------------------------------------------------------------------------------------- */
3132 function return 0 if not found and REAL_INDEX+1 if found
3135 static gsize
3136 panel_get_format_field_index_by_name (const WPanel * panel, const char *name)
3138 GSList *format;
3139 gsize lc_index;
3141 for (lc_index = 1, format = panel->format;
3142 format != NULL && strcmp (((format_item_t *) format->data)->title, name) != 0;
3143 format = g_slist_next (format), lc_index++)
3146 if (format == NULL)
3147 lc_index = 0;
3149 return lc_index;
3152 /* --------------------------------------------------------------------------------------------- */
3154 static const panel_field_t *
3155 panel_get_sortable_field_by_format (const WPanel * panel, gsize lc_index)
3157 const panel_field_t *pfield;
3158 const format_item_t *format;
3160 format = (const format_item_t *) g_slist_nth_data (panel->format, lc_index);
3161 if (format == NULL)
3162 return NULL;
3164 pfield = panel_get_field_by_title (format->title);
3165 if (pfield == NULL)
3166 return NULL;
3167 if (pfield->sort_routine == NULL)
3168 return NULL;
3169 return pfield;
3172 /* --------------------------------------------------------------------------------------------- */
3174 static void
3175 panel_toggle_sort_order_prev (WPanel * panel)
3177 gsize lc_index, i;
3178 const char *title;
3179 const panel_field_t *pfield = NULL;
3181 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3182 lc_index = panel_get_format_field_index_by_name (panel, title);
3184 if (lc_index > 1)
3186 /* search for prev sortable column in panel format */
3187 for (i = lc_index - 1;
3188 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3192 if (pfield == NULL)
3194 /* Sortable field not found. Try to search in each array */
3195 for (i = g_slist_length (panel->format);
3196 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3200 if (pfield != NULL)
3202 panel->sort_field = pfield;
3203 panel_set_sort_order (panel, pfield);
3207 /* --------------------------------------------------------------------------------------------- */
3209 static void
3210 panel_toggle_sort_order_next (WPanel * panel)
3212 gsize lc_index, i;
3213 const panel_field_t *pfield = NULL;
3214 gsize format_field_count;
3215 const char *title;
3217 format_field_count = g_slist_length (panel->format);
3218 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3219 lc_index = panel_get_format_field_index_by_name (panel, title);
3221 if (lc_index != 0 && lc_index != format_field_count)
3223 /* search for prev sortable column in panel format */
3224 for (i = lc_index;
3225 i != format_field_count
3226 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
3230 if (pfield == NULL)
3232 /* Sortable field not found. Try to search in each array */
3233 for (i = 0;
3234 i != format_field_count
3235 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
3239 if (pfield != NULL)
3241 panel->sort_field = pfield;
3242 panel_set_sort_order (panel, pfield);
3246 /* --------------------------------------------------------------------------------------------- */
3248 static void
3249 panel_select_sort_order (WPanel * panel)
3251 const panel_field_t *sort_order;
3253 sort_order = sort_box (&panel->sort_info, panel->sort_field);
3254 if (sort_order != NULL)
3256 panel->sort_field = sort_order;
3257 panel_set_sort_order (panel, sort_order);
3261 /* --------------------------------------------------------------------------------------------- */
3264 * panel_content_scroll_left:
3265 * @param panel the pointer to the panel on which we operate
3267 * scroll long filename to the left (decrement scroll pointer)
3271 static void
3272 panel_content_scroll_left (WPanel * panel)
3274 if (panel->content_shift > -1)
3276 if (panel->content_shift > panel->max_shift)
3277 panel->content_shift = panel->max_shift;
3279 panel->content_shift--;
3280 show_dir (panel);
3281 paint_dir (panel);
3285 /* --------------------------------------------------------------------------------------------- */
3287 * panel_content_scroll_right:
3288 * @param panel the pointer to the panel on which we operate
3290 * scroll long filename to the right (increment scroll pointer)
3294 static void
3295 panel_content_scroll_right (WPanel * panel)
3297 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
3299 panel->content_shift++;
3300 show_dir (panel);
3301 paint_dir (panel);
3305 /* --------------------------------------------------------------------------------------------- */
3307 static void
3308 panel_set_sort_type_by_id (WPanel * panel, const char *name)
3310 if (strcmp (panel->sort_field->id, name) == 0)
3311 panel->sort_info.reverse = !panel->sort_info.reverse;
3312 else
3314 const panel_field_t *sort_order;
3316 sort_order = panel_get_field_by_id (name);
3317 if (sort_order == NULL)
3318 return;
3320 panel->sort_field = sort_order;
3323 panel_set_sort_order (panel, panel->sort_field);
3326 /* --------------------------------------------------------------------------------------------- */
3328 * If we moved to the parent directory move the 'current' pointer to
3329 * the old directory name; If we leave VFS dir, remove FS specificator.
3331 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
3334 static const char *
3335 get_parent_dir_name (const vfs_path_t * cwd_vpath, const vfs_path_t * lwd_vpath)
3337 size_t llen, clen;
3338 const char *p, *lwd;
3340 llen = vfs_path_len (lwd_vpath);
3341 clen = vfs_path_len (cwd_vpath);
3343 if (llen <= clen)
3344 return NULL;
3346 lwd = vfs_path_as_str (lwd_vpath);
3348 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
3350 if (p == NULL)
3352 const char *cwd;
3354 cwd = vfs_path_as_str (cwd_vpath);
3356 p = strrchr (lwd, PATH_SEP);
3358 if (p != NULL && strncmp (cwd, lwd, (size_t) (p - lwd)) == 0
3359 && (clen == (size_t) (p - lwd) || (p == lwd && IS_PATH_SEP (cwd[0]) && cwd[1] == '\0')))
3360 return (p + 1);
3362 return NULL;
3365 /* skip VFS prefix */
3366 while (--p > lwd && !IS_PATH_SEP (*p))
3368 /* get last component */
3369 while (--p > lwd && !IS_PATH_SEP (*p))
3372 /* return last component */
3373 return (p != lwd || IS_PATH_SEP (*p)) ? p + 1 : p;
3376 /* --------------------------------------------------------------------------------------------- */
3377 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3379 static void
3380 subshell_chdir (const vfs_path_t * vpath)
3382 #ifdef ENABLE_SUBSHELL
3383 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3384 do_subshell_chdir (vpath, FALSE);
3385 #else /* ENABLE_SUBSHELL */
3386 (void) vpath;
3387 #endif /* ENABLE_SUBSHELL */
3390 /* --------------------------------------------------------------------------------------------- */
3392 * Changes the current directory of the panel.
3393 * Don't record change in the directory history.
3396 static gboolean
3397 panel_do_cd_int (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3399 vfs_path_t *olddir_vpath;
3401 /* Convert *new_path to a suitable pathname, handle ~user */
3402 if (cd_type == cd_parse_command)
3404 const vfs_path_element_t *element;
3406 element = vfs_path_get_by_index (new_dir_vpath, 0);
3407 if (strcmp (element->path, "-") == 0)
3408 new_dir_vpath = panel->lwd_vpath;
3411 if (mc_chdir (new_dir_vpath) == -1)
3412 return FALSE;
3414 /* Success: save previous directory, shutdown status of previous dir */
3415 olddir_vpath = vfs_path_clone (panel->cwd_vpath);
3416 panel_set_lwd (panel, panel->cwd_vpath);
3417 input_complete_free (cmdline);
3419 vfs_path_free (panel->cwd_vpath, TRUE);
3420 vfs_setup_cwd ();
3421 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3423 vfs_release_path (olddir_vpath);
3425 subshell_chdir (panel->cwd_vpath);
3427 /* Reload current panel */
3428 panel_clean_dir (panel);
3430 if (!dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
3431 &panel->sort_info, &panel->filter))
3432 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
3434 panel_set_current_by_name (panel, get_parent_dir_name (panel->cwd_vpath, olddir_vpath));
3436 load_hint (FALSE);
3437 panel->dirty = TRUE;
3438 update_xterm_title_path ();
3439 update_terminal_cwd ();
3441 vfs_path_free (olddir_vpath, TRUE);
3443 return TRUE;
3446 /* --------------------------------------------------------------------------------------------- */
3448 static void
3449 directory_history_next (WPanel * panel)
3451 gboolean ok;
3455 GList *next;
3457 ok = TRUE;
3458 next = g_list_next (panel->dir_history.current);
3459 if (next != NULL)
3461 vfs_path_t *data_vpath;
3463 data_vpath = vfs_path_from_str ((char *) next->data);
3464 ok = panel_do_cd_int (panel, data_vpath, cd_exact);
3465 vfs_path_free (data_vpath, TRUE);
3466 panel->dir_history.current = next;
3468 /* skip directories that present in history but absent in file system */
3470 while (!ok);
3473 /* --------------------------------------------------------------------------------------------- */
3475 static void
3476 directory_history_prev (WPanel * panel)
3478 gboolean ok;
3482 GList *prev;
3484 ok = TRUE;
3485 prev = g_list_previous (panel->dir_history.current);
3486 if (prev != NULL)
3488 vfs_path_t *data_vpath;
3490 data_vpath = vfs_path_from_str ((char *) prev->data);
3491 ok = panel_do_cd_int (panel, data_vpath, cd_exact);
3492 vfs_path_free (data_vpath, TRUE);
3493 panel->dir_history.current = prev;
3495 /* skip directories that present in history but absent in file system */
3497 while (!ok);
3500 /* --------------------------------------------------------------------------------------------- */
3502 static void
3503 directory_history_list (WPanel * panel)
3505 history_descriptor_t hd;
3506 gboolean ok = FALSE;
3507 size_t pos;
3509 pos = g_list_position (panel->dir_history.current, panel->dir_history.list);
3511 history_descriptor_init (&hd, WIDGET (panel)->rect.y, WIDGET (panel)->rect.x,
3512 panel->dir_history.list, (int) pos);
3513 history_show (&hd);
3515 panel->dir_history.list = hd.list;
3516 if (hd.text != NULL)
3518 vfs_path_t *s_vpath;
3520 s_vpath = vfs_path_from_str (hd.text);
3521 ok = panel_do_cd_int (panel, s_vpath, cd_exact);
3522 if (ok)
3523 directory_history_add (panel, panel->cwd_vpath);
3524 else
3525 cd_error_message (hd.text);
3526 vfs_path_free (s_vpath, TRUE);
3527 g_free (hd.text);
3530 if (!ok)
3532 /* Since history is fully modified in history_show(), panel->dir_history actually
3533 * points to the invalid place. Try restore current position here. */
3535 size_t i;
3537 panel->dir_history.current = panel->dir_history.list;
3539 for (i = 0; i <= pos; i++)
3541 GList *prev;
3543 prev = g_list_previous (panel->dir_history.current);
3544 if (prev == NULL)
3545 break;
3547 panel->dir_history.current = prev;
3552 /* --------------------------------------------------------------------------------------------- */
3554 static cb_ret_t
3555 panel_execute_cmd (WPanel * panel, long command)
3557 int res = MSG_HANDLED;
3559 if (command != CK_Search)
3560 stop_search (panel);
3562 switch (command)
3564 case CK_Up:
3565 case CK_Down:
3566 case CK_Left:
3567 case CK_Right:
3568 case CK_Bottom:
3569 case CK_Top:
3570 case CK_PageDown:
3571 case CK_PageUp:
3572 /* reset state of marks flag */
3573 state_mark = -1;
3574 break;
3575 default:
3576 break;
3579 switch (command)
3581 case CK_CycleListingFormat:
3582 panel_cycle_listing_format (panel);
3583 break;
3584 case CK_PanelOtherCd:
3585 chdir_other_panel (panel);
3586 break;
3587 case CK_PanelOtherCdLink:
3588 chdir_to_readlink (panel);
3589 break;
3590 case CK_CopySingle:
3591 copy_cmd_local (panel);
3592 break;
3593 case CK_DeleteSingle:
3594 delete_cmd_local (panel);
3595 break;
3596 case CK_Enter:
3597 do_enter (panel);
3598 break;
3599 case CK_ViewRaw:
3600 view_raw_cmd (panel);
3601 break;
3602 case CK_EditNew:
3603 edit_cmd_new ();
3604 break;
3605 case CK_MoveSingle:
3606 rename_cmd_local (panel);
3607 break;
3608 case CK_SelectInvert:
3609 panel_select_invert_files (panel);
3610 break;
3611 case CK_Select:
3612 panel_select_files (panel);
3613 break;
3614 case CK_SelectExt:
3615 panel_select_ext_cmd (panel);
3616 break;
3617 case CK_Unselect:
3618 panel_unselect_files (panel);
3619 break;
3620 case CK_Filter:
3621 panel_do_set_filter (panel);
3622 break;
3623 case CK_PageDown:
3624 next_page (panel);
3625 break;
3626 case CK_PageUp:
3627 prev_page (panel);
3628 break;
3629 case CK_CdChild:
3630 goto_child_dir (panel);
3631 break;
3632 case CK_CdParent:
3633 goto_parent_dir (panel);
3634 break;
3635 case CK_History:
3636 directory_history_list (panel);
3637 break;
3638 case CK_HistoryNext:
3639 directory_history_next (panel);
3640 break;
3641 case CK_HistoryPrev:
3642 directory_history_prev (panel);
3643 break;
3644 case CK_BottomOnScreen:
3645 goto_bottom_file (panel);
3646 break;
3647 case CK_MiddleOnScreen:
3648 goto_middle_file (panel);
3649 break;
3650 case CK_TopOnScreen:
3651 goto_top_file (panel);
3652 break;
3653 case CK_Mark:
3654 mark_file (panel);
3655 break;
3656 case CK_MarkUp:
3657 mark_file_up (panel);
3658 break;
3659 case CK_MarkDown:
3660 mark_file_down (panel);
3661 break;
3662 case CK_MarkLeft:
3663 mark_file_left (panel);
3664 break;
3665 case CK_MarkRight:
3666 mark_file_right (panel);
3667 break;
3668 case CK_CdParentSmart:
3669 res = force_maybe_cd (panel);
3670 break;
3671 case CK_Up:
3672 move_up (panel);
3673 break;
3674 case CK_Down:
3675 move_down (panel);
3676 break;
3677 case CK_Left:
3678 res = move_left (panel);
3679 break;
3680 case CK_Right:
3681 res = move_right (panel);
3682 break;
3683 case CK_Bottom:
3684 move_end (panel);
3685 break;
3686 case CK_Top:
3687 move_home (panel);
3688 break;
3689 #ifdef HAVE_CHARSET
3690 case CK_SelectCodepage:
3691 panel_change_encoding (panel);
3692 break;
3693 #endif
3694 case CK_ScrollLeft:
3695 panel_content_scroll_left (panel);
3696 break;
3697 case CK_ScrollRight:
3698 panel_content_scroll_right (panel);
3699 break;
3700 case CK_Search:
3701 start_search (panel);
3702 break;
3703 case CK_SearchStop:
3704 break;
3705 case CK_PanelOtherSync:
3706 panel_sync_other (panel);
3707 break;
3708 case CK_Sort:
3709 panel_select_sort_order (panel);
3710 break;
3711 case CK_SortPrev:
3712 panel_toggle_sort_order_prev (panel);
3713 break;
3714 case CK_SortNext:
3715 panel_toggle_sort_order_next (panel);
3716 break;
3717 case CK_SortReverse:
3718 panel->sort_info.reverse = !panel->sort_info.reverse;
3719 panel_set_sort_order (panel, panel->sort_field);
3720 break;
3721 case CK_SortByName:
3722 panel_set_sort_type_by_id (panel, "name");
3723 break;
3724 case CK_SortByExt:
3725 panel_set_sort_type_by_id (panel, "extension");
3726 break;
3727 case CK_SortBySize:
3728 panel_set_sort_type_by_id (panel, "size");
3729 break;
3730 case CK_SortByMTime:
3731 panel_set_sort_type_by_id (panel, "mtime");
3732 break;
3733 default:
3734 res = MSG_NOT_HANDLED;
3735 break;
3738 return res;
3741 /* --------------------------------------------------------------------------------------------- */
3743 static cb_ret_t
3744 panel_key (WPanel * panel, int key)
3746 long command;
3748 if (is_abort_char (key))
3750 stop_search (panel);
3751 return MSG_HANDLED;
3754 if (panel->quick_search.active && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3756 do_search (panel, key);
3757 return MSG_HANDLED;
3760 command = widget_lookup_key (WIDGET (panel), key);
3761 if (command != CK_IgnoreKey)
3762 return panel_execute_cmd (panel, command);
3764 if (panels_options.torben_fj_mode && key == ALT ('h'))
3766 goto_middle_file (panel);
3767 return MSG_HANDLED;
3770 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3772 start_search (panel);
3773 do_search (panel, key);
3774 return MSG_HANDLED;
3777 return MSG_NOT_HANDLED;
3780 /* --------------------------------------------------------------------------------------------- */
3782 static cb_ret_t
3783 panel_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3785 WPanel *panel = PANEL (w);
3786 WDialog *h = DIALOG (w->owner);
3787 WButtonBar *bb;
3789 switch (msg)
3791 case MSG_INIT:
3792 /* subscribe to "history_load" event */
3793 mc_event_add (h->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3794 /* subscribe to "history_save" event */
3795 mc_event_add (h->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3796 return MSG_HANDLED;
3798 case MSG_DRAW:
3799 /* Repaint everything, including frame and separator */
3800 widget_erase (w);
3801 show_dir (panel);
3802 panel_print_header (panel);
3803 adjust_top_file (panel);
3804 paint_dir (panel);
3805 mini_info_separator (panel);
3806 display_mini_info (panel);
3807 panel->dirty = FALSE;
3808 return MSG_HANDLED;
3810 case MSG_FOCUS:
3811 state_mark = -1;
3812 current_panel = panel;
3813 panel->active = TRUE;
3815 if (mc_chdir (panel->cwd_vpath) != 0)
3817 char *cwd;
3819 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3820 cd_error_message (cwd);
3821 g_free (cwd);
3823 else
3824 subshell_chdir (panel->cwd_vpath);
3826 update_xterm_title_path ();
3827 update_terminal_cwd ();
3828 select_item (panel);
3830 bb = buttonbar_find (h);
3831 midnight_set_buttonbar (bb);
3832 widget_draw (WIDGET (bb));
3833 return MSG_HANDLED;
3835 case MSG_UNFOCUS:
3836 /* Janne: look at this for the multiple panel options */
3837 stop_search (panel);
3838 panel->active = FALSE;
3839 unselect_item (panel);
3840 return MSG_HANDLED;
3842 case MSG_KEY:
3843 return panel_key (panel, parm);
3845 case MSG_ACTION:
3846 return panel_execute_cmd (panel, parm);
3848 case MSG_DESTROY:
3849 vfs_stamp_path (panel->cwd_vpath);
3850 /* unsubscribe from "history_load" event */
3851 mc_event_del (h->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3852 /* unsubscribe from "history_save" event */
3853 mc_event_del (h->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3854 panel_destroy (panel);
3855 free_my_statfs ();
3856 return MSG_HANDLED;
3858 default:
3859 return widget_default_callback (w, sender, msg, parm, data);
3863 /* --------------------------------------------------------------------------------------------- */
3864 /* */
3865 /* Panel mouse events support routines */
3866 /* */
3868 static void
3869 mouse_toggle_mark (WPanel * panel)
3871 do_mark_file (panel, MARK_DONT_MOVE);
3872 mouse_marking = (panel_current_entry (panel)->f.marked != 0);
3873 mouse_mark_panel = current_panel;
3876 /* --------------------------------------------------------------------------------------------- */
3878 static void
3879 mouse_set_mark (WPanel * panel)
3881 if (mouse_mark_panel == panel)
3883 const file_entry_t *fe;
3885 fe = panel_current_entry (panel);
3887 if (mouse_marking && fe->f.marked == 0)
3888 do_mark_file (panel, MARK_DONT_MOVE);
3889 else if (!mouse_marking && fe->f.marked != 0)
3890 do_mark_file (panel, MARK_DONT_MOVE);
3894 /* --------------------------------------------------------------------------------------------- */
3896 static void
3897 mark_if_marking (WPanel * panel, const mouse_event_t * event, int previous_current)
3899 if ((event->buttons & GPM_B_RIGHT) == 0)
3900 return;
3902 if (event->msg == MSG_MOUSE_DOWN)
3903 mouse_toggle_mark (panel);
3904 else
3906 int pcurr, curr1, curr2;
3908 pcurr = panel->current;
3909 curr1 = MIN (previous_current, panel->current);
3910 curr2 = MAX (previous_current, panel->current);
3912 for (; curr1 <= curr2; curr1++)
3914 panel->current = curr1;
3915 mouse_set_mark (panel);
3918 panel->current = pcurr;
3922 /* --------------------------------------------------------------------------------------------- */
3923 /** Determine which column was clicked, and sort the panel on
3924 * that column, or reverse sort on that column if already
3925 * sorted on that column.
3928 static void
3929 mouse_sort_col (WPanel * panel, int x)
3931 int i = 0;
3932 GSList *format;
3933 const char *lc_sort_name = NULL;
3934 panel_field_t *col_sort_format = NULL;
3936 for (format = panel->format; format != NULL; format = g_slist_next (format))
3938 format_item_t *fi = (format_item_t *) format->data;
3940 i += fi->field_len;
3941 if (x < i + 1)
3943 /* found column */
3944 lc_sort_name = fi->title;
3945 break;
3949 if (lc_sort_name == NULL)
3950 return;
3952 for (i = 0; panel_fields[i].id != NULL; i++)
3954 const char *title;
3956 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3957 if (panel_fields[i].sort_routine != NULL && strcmp (title, lc_sort_name) == 0)
3959 col_sort_format = &panel_fields[i];
3960 break;
3964 if (col_sort_format != NULL)
3966 if (panel->sort_field == col_sort_format)
3967 /* reverse the sort if clicked column is already the sorted column */
3968 panel->sort_info.reverse = !panel->sort_info.reverse;
3969 else
3970 /* new sort is forced to be ascending */
3971 panel->sort_info.reverse = FALSE;
3973 panel_set_sort_order (panel, col_sort_format);
3977 /* --------------------------------------------------------------------------------------------- */
3979 static int
3980 panel_mouse_is_on_item (const WPanel * panel, int y, int x)
3982 int lines, col_width, col;
3984 if (y < 0)
3985 return MOUSE_UPPER_FILE_LIST;
3987 lines = panel_lines (panel);
3988 if (y >= lines)
3989 return MOUSE_BELOW_FILE_LIST;
3991 col_width = (CONST_WIDGET (panel)->rect.cols - 2) / panel->list_cols;
3992 /* column where mouse is */
3993 col = x / col_width;
3995 y += panel->top + lines * col;
3997 /* are we below or in the next column of last file? */
3998 if (y > panel->dir.len)
3999 return MOUSE_AFTER_LAST_FILE;
4001 /* we are on item of the file file; return an index to select a file */
4002 return y;
4005 /* --------------------------------------------------------------------------------------------- */
4007 static void
4008 panel_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
4010 WPanel *panel = PANEL (w);
4011 gboolean is_active;
4013 is_active = widget_is_active (w);
4015 switch (msg)
4017 case MSG_MOUSE_DOWN:
4018 if (event->y == 0)
4020 /* top frame */
4021 if (event->x == 1)
4022 /* "<" button */
4023 directory_history_prev (panel);
4024 else if (event->x == w->rect.cols - 2)
4025 /* ">" button */
4026 directory_history_next (panel);
4027 else if (event->x >= w->rect.cols - 5 && event->x <= w->rect.cols - 3)
4028 /* "^" button */
4029 directory_history_list (panel);
4030 else if (event->x == w->rect.cols - 6)
4031 /* "." button show/hide hidden files */
4032 send_message (filemanager, NULL, MSG_ACTION, CK_ShowHidden, NULL);
4033 else
4035 /* no other events on 1st line, return MOU_UNHANDLED */
4036 event->result.abort = TRUE;
4037 /* avoid extra panel redraw */
4038 panel->dirty = FALSE;
4040 break;
4043 if (event->y == 1)
4045 /* sort on clicked column */
4046 mouse_sort_col (panel, event->x + 1);
4047 break;
4050 if (!is_active)
4051 (void) change_panel ();
4052 MC_FALLTHROUGH;
4054 case MSG_MOUSE_DRAG:
4056 int my_index;
4057 int previous_current;
4059 my_index = panel_mouse_is_on_item (panel, event->y - 2, event->x);
4060 previous_current = panel->current;
4062 switch (my_index)
4064 case MOUSE_UPPER_FILE_LIST:
4065 move_up (panel);
4066 mark_if_marking (panel, event, previous_current);
4067 break;
4069 case MOUSE_BELOW_FILE_LIST:
4070 move_down (panel);
4071 mark_if_marking (panel, event, previous_current);
4072 break;
4074 case MOUSE_AFTER_LAST_FILE:
4075 break; /* do nothing */
4077 default:
4078 if (my_index != panel->current)
4080 unselect_item (panel);
4081 panel->current = my_index;
4082 select_item (panel);
4085 mark_if_marking (panel, event, previous_current);
4086 break;
4089 break;
4091 case MSG_MOUSE_UP:
4092 break;
4094 case MSG_MOUSE_CLICK:
4095 if ((event->count & GPM_DOUBLE) != 0 && (event->buttons & GPM_B_LEFT) != 0 &&
4096 panel_mouse_is_on_item (panel, event->y - 2, event->x) >= 0)
4097 do_enter (panel);
4098 break;
4100 case MSG_MOUSE_MOVE:
4101 break;
4103 case MSG_MOUSE_SCROLL_UP:
4104 if (is_active)
4106 if (panels_options.mouse_move_pages && panel->top > 0)
4107 prev_page (panel);
4108 else /* We are in first page */
4109 move_up (panel);
4111 break;
4113 case MSG_MOUSE_SCROLL_DOWN:
4114 if (is_active)
4116 if (panels_options.mouse_move_pages
4117 && panel->top + panel_items (panel) < panel->dir.len)
4118 next_page (panel);
4119 else /* We are in last page */
4120 move_down (panel);
4122 break;
4124 default:
4125 break;
4128 if (panel->dirty)
4129 widget_draw (w);
4132 /* --------------------------------------------------------------------------------------------- */
4134 static void
4135 reload_panelized (WPanel * panel)
4137 int i, j;
4138 dir_list *list = &panel->dir;
4140 /* refresh current VFS directory required for vfs_path_from_str() */
4141 (void) mc_chdir (panel->cwd_vpath);
4143 for (i = 0, j = 0; i < list->len; i++)
4145 vfs_path_t *vpath;
4147 vpath = vfs_path_from_str (list->list[i].fname->str);
4148 if (mc_lstat (vpath, &list->list[i].st) != 0)
4149 g_string_free (list->list[i].fname, TRUE);
4150 else
4152 if (j != i)
4153 list->list[j] = list->list[i];
4154 j++;
4156 vfs_path_free (vpath, TRUE);
4158 if (j == 0)
4159 dir_list_init (list);
4160 else
4161 list->len = j;
4163 recalculate_panel_summary (panel);
4165 if (panel != current_panel)
4166 (void) mc_chdir (current_panel->cwd_vpath);
4169 /* --------------------------------------------------------------------------------------------- */
4171 static void
4172 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
4174 gboolean free_pointer;
4175 char *my_current_file = NULL;
4177 if ((flags & UP_RELOAD) != 0)
4179 panel->is_panelized = FALSE;
4180 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, NULL);
4181 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4184 /* If current_file == -1 (an invalid pointer) then preserve current */
4185 free_pointer = current_file == UP_KEEPSEL;
4187 if (free_pointer)
4189 const GString *fname;
4191 fname = panel_current_entry (panel)->fname;
4192 my_current_file = g_strndup (fname->str, fname->len);
4193 current_file = my_current_file;
4196 if (panel->is_panelized)
4197 reload_panelized (panel);
4198 else
4199 panel_reload (panel);
4201 panel_set_current_by_name (panel, current_file);
4202 panel->dirty = TRUE;
4204 if (free_pointer)
4205 g_free (my_current_file);
4208 /* --------------------------------------------------------------------------------------------- */
4210 static void
4211 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
4213 if (get_panel_type (which) == view_listing)
4215 WPanel *panel;
4217 panel = PANEL (get_panel_widget (which));
4218 if (panel->is_panelized)
4219 flags &= ~UP_RELOAD;
4220 update_one_panel_widget (panel, flags, current_file);
4224 /* --------------------------------------------------------------------------------------------- */
4226 static void
4227 panel_set_current (WPanel * panel, int i)
4229 if (i != panel->current)
4231 panel->dirty = TRUE;
4232 panel->current = i;
4233 panel->top = panel->current - (WIDGET (panel)->rect.lines - 2) / 2;
4234 if (panel->top < 0)
4235 panel->top = 0;
4239 /* --------------------------------------------------------------------------------------------- */
4241 /* event callback */
4242 static gboolean
4243 event_update_panels (const gchar * event_group_name, const gchar * event_name,
4244 gpointer init_data, gpointer data)
4246 (void) event_group_name;
4247 (void) event_name;
4248 (void) init_data;
4249 (void) data;
4251 update_panels (UP_RELOAD, UP_KEEPSEL);
4253 return TRUE;
4256 /* --------------------------------------------------------------------------------------------- */
4258 /* event callback */
4259 static gboolean
4260 panel_save_current_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
4261 gpointer init_data, gpointer data)
4263 (void) event_group_name;
4264 (void) event_name;
4265 (void) init_data;
4266 (void) data;
4268 if (current_panel->marked == 0)
4269 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
4270 (gpointer) panel_current_entry (current_panel)->fname->str);
4271 else
4273 int i;
4274 gboolean first = TRUE;
4275 char *flist = NULL;
4277 for (i = 0; i < current_panel->dir.len; i++)
4279 const file_entry_t *fe = &current_panel->dir.list[i];
4281 if (fe->f.marked != 0)
4282 { /* Skip the unmarked ones */
4283 if (first)
4285 flist = g_strndup (fe->fname->str, fe->fname->len);
4286 first = FALSE;
4288 else
4290 /* Add empty lines after the file */
4291 char *tmp;
4293 tmp = g_strconcat (flist, "\n", fe->fname->str, (char *) NULL);
4294 g_free (flist);
4295 flist = tmp;
4300 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
4301 g_free (flist);
4303 return TRUE;
4306 /* --------------------------------------------------------------------------------------------- */
4308 static vfs_path_t *
4309 panel_recursive_cd_to_parent (const vfs_path_t * vpath)
4311 vfs_path_t *cwd_vpath;
4313 cwd_vpath = vfs_path_clone (vpath);
4315 while (mc_chdir (cwd_vpath) < 0)
4317 const char *panel_cwd_path;
4318 vfs_path_t *tmp_vpath;
4320 /* check if path contains only '/' */
4321 panel_cwd_path = vfs_path_as_str (cwd_vpath);
4322 if (panel_cwd_path != NULL && IS_PATH_SEP (panel_cwd_path[0]) && panel_cwd_path[1] == '\0')
4324 vfs_path_free (cwd_vpath, TRUE);
4325 return NULL;
4328 tmp_vpath = vfs_path_vtokens_get (cwd_vpath, 0, -1);
4329 vfs_path_free (cwd_vpath, TRUE);
4330 cwd_vpath =
4331 vfs_path_build_filename (PATH_SEP_STR, vfs_path_as_str (tmp_vpath), (char *) NULL);
4332 vfs_path_free (tmp_vpath, TRUE);
4335 return cwd_vpath;
4338 /* --------------------------------------------------------------------------------------------- */
4340 static void
4341 panel_dir_list_callback (dir_list_cb_state_t state, void *data)
4343 static int count = 0;
4345 (void) data;
4347 switch (state)
4349 case DIR_OPEN:
4350 count = 0;
4351 break;
4353 case DIR_READ:
4354 count++;
4355 if ((count & 15) == 0)
4356 rotate_dash (TRUE);
4357 break;
4359 case DIR_CLOSE:
4360 rotate_dash (FALSE);
4361 break;
4363 default:
4364 g_assert_not_reached ();
4368 /* --------------------------------------------------------------------------------------------- */
4369 /*** public functions ****************************************************************************/
4370 /* --------------------------------------------------------------------------------------------- */
4372 void
4373 panel_set_current_by_name (WPanel * panel, const char *name)
4375 int i;
4376 char *subdir;
4378 if (name == NULL)
4380 panel_set_current (panel, 0);
4381 return;
4384 /* We only want the last component of the directory,
4385 * and from this only the name without suffix.
4386 * Cut prefix if the panel is not panelized */
4387 if (panel->is_panelized)
4388 subdir = vfs_strip_suffix_from_filename (name);
4389 else
4390 subdir = vfs_strip_suffix_from_filename (x_basename (name));
4392 /* Search that subdir or filename without prefix (if not panelized panel),
4393 make it current if found */
4394 for (i = 0; i < panel->dir.len; i++)
4395 if (strcmp (subdir, panel->dir.list[i].fname->str) == 0)
4397 panel_set_current (panel, i);
4398 g_free (subdir);
4399 return;
4402 /* Make current near the filee that is missing */
4403 if (panel->current >= panel->dir.len)
4404 panel_set_current (panel, panel->dir.len - 1);
4405 g_free (subdir);
4407 select_item (panel);
4410 /* --------------------------------------------------------------------------------------------- */
4412 void
4413 panel_clean_dir (WPanel * panel)
4415 panel->top = 0;
4416 panel->current = 0;
4417 panel->marked = 0;
4418 panel->dirs_marked = 0;
4419 panel->total = 0;
4420 panel->quick_search.active = FALSE;
4421 panel->is_panelized = FALSE;
4422 panel->dirty = TRUE;
4423 panel->content_shift = -1;
4424 panel->max_shift = -1;
4426 dir_list_free_list (&panel->dir);
4429 /* --------------------------------------------------------------------------------------------- */
4431 * Set Up panel's current dir object
4433 * @param panel panel object
4434 * @param path_str string contain path
4437 void
4438 panel_set_cwd (WPanel * panel, const vfs_path_t * vpath)
4440 if (vpath != panel->cwd_vpath) /* check if new vpath is not the panel->cwd_vpath object */
4442 vfs_path_free (panel->cwd_vpath, TRUE);
4443 panel->cwd_vpath = vfs_path_clone (vpath);
4447 /* --------------------------------------------------------------------------------------------- */
4449 * Set Up panel's last working dir object
4451 * @param panel panel object
4452 * @param path_str string contain path
4455 void
4456 panel_set_lwd (WPanel * panel, const vfs_path_t * vpath)
4458 if (vpath != panel->lwd_vpath) /* check if new vpath is not the panel->lwd_vpath object */
4460 vfs_path_free (panel->lwd_vpath, TRUE);
4461 panel->lwd_vpath = vfs_path_clone (vpath);
4465 /* --------------------------------------------------------------------------------------------- */
4467 * Creatie an empty panel with specified size.
4469 * @param panel_name name of panel for setup receiving
4470 * @param r panel areaa
4472 * @return new instance of WPanel
4475 WPanel *
4476 panel_sized_empty_new (const char *panel_name, const WRect * r)
4478 WPanel *panel;
4479 Widget *w;
4480 char *section;
4481 int i, err;
4483 panel = g_new0 (WPanel, 1);
4484 w = WIDGET (panel);
4485 widget_init (w, r, panel_callback, panel_mouse_callback);
4486 w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
4487 w->keymap = panel_map;
4489 panel->dir.size = DIR_LIST_MIN_SIZE;
4490 panel->dir.list = g_new (file_entry_t, panel->dir.size);
4491 panel->dir.len = 0;
4492 panel->dir.callback = panel_dir_list_callback;
4494 panel->list_cols = 1;
4495 panel->brief_cols = 2;
4496 panel->dirty = TRUE;
4497 panel->content_shift = -1;
4498 panel->max_shift = -1;
4500 panel->list_format = list_full;
4501 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4503 panel->filter.flags = FILE_FILTER_DEFAULT_FLAGS;
4505 for (i = 0; i < LIST_FORMATS; i++)
4506 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4508 #ifdef HAVE_CHARSET
4509 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4510 #endif
4512 panel->frame_size = frame_half;
4514 panel->quick_search.buffer = g_string_sized_new (MC_MAXFILENAMELEN);
4515 panel->quick_search.prev_buffer = g_string_sized_new (MC_MAXFILENAMELEN);
4517 panel->name = g_strdup (panel_name);
4518 panel->dir_history.name = g_strconcat ("Dir Hist ", panel->name, (char *) NULL);
4519 /* directories history will be get later */
4521 section = g_strconcat ("Temporal:", panel->name, (char *) NULL);
4522 if (!mc_config_has_group (mc_global.main_config, section))
4524 g_free (section);
4525 section = g_strdup (panel->name);
4527 panel_load_setup (panel, section);
4528 g_free (section);
4530 if (panel->filter.value != NULL)
4532 gboolean case_sens = (panel->filter.flags & SELECT_MATCH_CASE) != 0;
4533 gboolean shell_patterns = (panel->filter.flags & SELECT_SHELL_PATTERNS) != 0;
4535 panel->filter.handler = mc_search_new (panel->filter.value, NULL);
4536 panel->filter.handler->search_type = shell_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
4537 panel->filter.handler->is_entire_line = TRUE;
4538 panel->filter.handler->is_case_sensitive = case_sens;
4540 /* FIXME: silent check -- do not display an error message */
4541 if (!mc_search_prepare (panel->filter.handler))
4542 file_filter_clear (&panel->filter);
4545 /* Load format strings */
4546 err = set_panel_formats (panel);
4547 if (err != 0)
4548 set_panel_formats (panel);
4550 return panel;
4553 /* --------------------------------------------------------------------------------------------- */
4555 * Panel creation for specified size and directory.
4557 * @param panel_name name of panel for setup retrieving
4558 * @param r panel areaa
4559 * @param vpath working panel directory. If NULL then current directory is used
4561 * @return new instance of WPanel
4564 WPanel *
4565 panel_sized_with_dir_new (const char *panel_name, const WRect * r, const vfs_path_t * vpath)
4567 WPanel *panel;
4568 char *curdir = NULL;
4569 #ifdef HAVE_CHARSET
4570 const vfs_path_element_t *path_element;
4571 #endif
4573 panel = panel_sized_empty_new (panel_name, r);
4575 if (vpath != NULL)
4577 curdir = vfs_get_cwd ();
4578 panel_set_cwd (panel, vpath);
4580 else
4582 vfs_setup_cwd ();
4583 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4586 panel_set_lwd (panel, vfs_get_raw_current_dir ());
4588 #ifdef HAVE_CHARSET
4589 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4590 if (path_element->encoding != NULL)
4591 panel->codepage = get_codepage_index (path_element->encoding);
4592 #endif
4594 if (mc_chdir (panel->cwd_vpath) != 0)
4596 #ifdef HAVE_CHARSET
4597 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4598 #endif
4599 vfs_setup_cwd ();
4600 vfs_path_free (panel->cwd_vpath, TRUE);
4601 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4604 /* Load the default format */
4605 if (!dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4606 &panel->sort_info, &panel->filter))
4607 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
4609 /* Restore old right path */
4610 if (curdir != NULL)
4612 vfs_path_t *tmp_vpath;
4613 int err;
4615 tmp_vpath = vfs_path_from_str (curdir);
4616 mc_chdir (tmp_vpath);
4617 vfs_path_free (tmp_vpath, TRUE);
4618 (void) err;
4620 g_free (curdir);
4622 return panel;
4625 /* --------------------------------------------------------------------------------------------- */
4627 void
4628 panel_reload (WPanel * panel)
4630 struct stat current_stat;
4631 vfs_path_t *cwd_vpath;
4633 if (panels_options.fast_reload && stat (vfs_path_as_str (panel->cwd_vpath), &current_stat) == 0
4634 && current_stat.st_ctime == panel->dir_stat.st_ctime
4635 && current_stat.st_mtime == panel->dir_stat.st_mtime)
4636 return;
4638 cwd_vpath = panel_recursive_cd_to_parent (panel->cwd_vpath);
4639 vfs_path_free (panel->cwd_vpath, TRUE);
4641 if (cwd_vpath == NULL)
4643 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4644 panel_clean_dir (panel);
4645 dir_list_init (&panel->dir);
4646 return;
4649 panel->cwd_vpath = cwd_vpath;
4650 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4651 show_dir (panel);
4653 if (!dir_list_reload (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4654 &panel->sort_info, &panel->filter))
4655 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
4657 panel->dirty = TRUE;
4658 if (panel->current >= panel->dir.len)
4659 panel_set_current (panel, panel->dir.len - 1);
4661 recalculate_panel_summary (panel);
4664 /* --------------------------------------------------------------------------------------------- */
4665 /* Switches the panel to the mode specified in the format */
4666 /* Setting up both format and status string. Return: 0 - on success; */
4667 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4670 set_panel_formats (WPanel * p)
4672 GSList *form;
4673 char *err = NULL;
4674 int retcode = 0;
4676 form = use_display_format (p, panel_format (p), &err, FALSE);
4678 if (err != NULL)
4680 g_free (err);
4681 retcode = 1;
4683 else
4685 g_slist_free_full (p->format, (GDestroyNotify) format_item_free);
4686 p->format = form;
4689 if (panels_options.show_mini_info)
4691 form = use_display_format (p, mini_status_format (p), &err, TRUE);
4693 if (err != NULL)
4695 g_free (err);
4696 retcode += 2;
4698 else
4700 g_slist_free_full (p->status_format, (GDestroyNotify) format_item_free);
4701 p->status_format = form;
4705 panel_update_cols (WIDGET (p), p->frame_size);
4707 if (retcode)
4708 message (D_ERROR, _("Warning"),
4709 _("User supplied format looks invalid, reverting to default."));
4710 if (retcode & 0x01)
4712 g_free (p->user_format);
4713 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4715 if (retcode & 0x02)
4717 g_free (p->user_status_format[p->list_format]);
4718 p->user_status_format[p->list_format] = g_strdup (DEFAULT_USER_FORMAT);
4721 return retcode;
4724 /* --------------------------------------------------------------------------------------------- */
4726 void
4727 panel_set_filter (WPanel * panel, const file_filter_t * filter)
4729 MC_PTR_FREE (panel->filter.value);
4730 mc_search_free (panel->filter.handler);
4731 panel->filter.handler = NULL;
4733 /* NULL to clear filter */
4734 if (filter != NULL)
4735 panel->filter = *filter;
4737 reread_cmd ();
4740 /* --------------------------------------------------------------------------------------------- */
4742 /* Select current item and readjust the panel */
4743 void
4744 select_item (WPanel * panel)
4746 adjust_top_file (panel);
4748 panel->dirty = TRUE;
4750 execute_hooks (select_file_hook);
4753 /* --------------------------------------------------------------------------------------------- */
4754 /** Clears all files in the panel, used only when one file was marked */
4755 void
4756 unmark_files (WPanel * panel)
4758 if (panel->marked != 0)
4760 int i;
4762 for (i = 0; i < panel->dir.len; i++)
4763 file_mark (panel, i, 0);
4765 panel->dirs_marked = 0;
4766 panel->marked = 0;
4767 panel->total = 0;
4771 /* --------------------------------------------------------------------------------------------- */
4772 /** Recalculate the panels summary information, used e.g. when marked
4773 files might have been removed by an external command */
4775 void
4776 recalculate_panel_summary (WPanel * panel)
4778 int i;
4780 panel->marked = 0;
4781 panel->dirs_marked = 0;
4782 panel->total = 0;
4784 for (i = 0; i < panel->dir.len; i++)
4785 if (panel->dir.list[i].f.marked != 0)
4787 /* do_file_mark will return immediately if newmark == oldmark.
4788 So we have to first unmark it to get panel's summary information
4789 updated. (Norbert) */
4790 panel->dir.list[i].f.marked = 0;
4791 do_file_mark (panel, i, 1);
4795 /* --------------------------------------------------------------------------------------------- */
4796 /** This routine marks a file or a directory */
4798 void
4799 do_file_mark (WPanel * panel, int idx, int mark)
4801 if (panel->dir.list[idx].f.marked == mark)
4802 return;
4804 /* Only '..' can't be marked, '.' isn't visible */
4805 if (DIR_IS_DOTDOT (panel->dir.list[idx].fname->str))
4806 return;
4808 file_mark (panel, idx, mark);
4809 if (panel->dir.list[idx].f.marked != 0)
4811 panel->marked++;
4813 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4815 if (panel->dir.list[idx].f.dir_size_computed != 0)
4816 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4817 panel->dirs_marked++;
4819 else
4820 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4822 set_colors (panel);
4824 else
4826 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4828 if (panel->dir.list[idx].f.dir_size_computed != 0)
4829 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4830 panel->dirs_marked--;
4832 else
4833 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4835 panel->marked--;
4839 /* --------------------------------------------------------------------------------------------- */
4841 * Changes the current directory of the panel.
4842 * Record change in the directory history.
4844 gboolean
4845 panel_do_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4847 gboolean r;
4849 r = panel_do_cd_int (panel, new_dir_vpath, cd_type);
4850 if (r)
4851 directory_history_add (panel, panel->cwd_vpath);
4852 return r;
4855 /* --------------------------------------------------------------------------------------------- */
4857 void
4858 file_mark (WPanel * panel, int lc_index, int val)
4860 if (panel->dir.list[lc_index].f.marked != val)
4862 panel->dir.list[lc_index].f.marked = val;
4863 panel->dirty = TRUE;
4867 /* --------------------------------------------------------------------------------------------- */
4869 void
4870 panel_re_sort (WPanel * panel)
4872 char *filename;
4873 const file_entry_t *fe;
4874 int i;
4876 if (panel == NULL)
4877 return;
4879 fe = panel_current_entry (panel);
4880 filename = g_strndup (fe->fname->str, fe->fname->len);
4881 unselect_item (panel);
4882 dir_list_sort (&panel->dir, panel->sort_field->sort_routine, &panel->sort_info);
4883 panel->current = -1;
4885 for (i = panel->dir.len; i != 0; i--)
4886 if (strcmp (panel->dir.list[i - 1].fname->str, filename) == 0)
4888 panel->current = i - 1;
4889 break;
4892 g_free (filename);
4893 panel->top = panel->current - panel_items (panel) / 2;
4894 select_item (panel);
4895 panel->dirty = TRUE;
4898 /* --------------------------------------------------------------------------------------------- */
4900 void
4901 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4903 if (sort_order == NULL)
4904 return;
4906 panel->sort_field = sort_order;
4908 /* The directory is already sorted, we have to load the unsorted stuff */
4909 if (sort_order->sort_routine == (GCompareFunc) unsorted)
4911 char *current_file;
4912 const GString *fname;
4914 fname = panel_current_entry (panel)->fname;
4915 current_file = g_strndup (fname->str, fname->len);
4916 panel_reload (panel);
4917 panel_set_current_by_name (panel, current_file);
4918 g_free (current_file);
4920 panel_re_sort (panel);
4923 /* --------------------------------------------------------------------------------------------- */
4925 #ifdef HAVE_CHARSET
4928 * Change panel encoding.
4929 * @param panel WPanel object
4932 void
4933 panel_change_encoding (WPanel * panel)
4935 const char *encoding = NULL;
4936 char *errmsg;
4937 int r;
4939 r = select_charset (-1, -1, panel->codepage, FALSE);
4941 if (r == SELECT_CHARSET_CANCEL)
4942 return; /* Cancel */
4944 panel->codepage = r;
4946 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4948 /* No translation */
4949 vfs_path_t *cd_path_vpath;
4951 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4952 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4953 panel_do_cd (panel, cd_path_vpath, cd_parse_command);
4954 show_dir (panel);
4955 vfs_path_free (cd_path_vpath, TRUE);
4956 return;
4959 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4960 if (errmsg != NULL)
4962 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4963 g_free (errmsg);
4964 return;
4967 encoding = get_codepage_id (panel->codepage);
4968 if (encoding != NULL)
4970 vfs_path_change_encoding (panel->cwd_vpath, encoding);
4972 if (!panel_do_cd (panel, panel->cwd_vpath, cd_parse_command))
4973 cd_error_message (vfs_path_as_str (panel->cwd_vpath));
4977 /* --------------------------------------------------------------------------------------------- */
4980 * Remove encode info from last path element.
4983 vfs_path_t *
4984 remove_encoding_from_path (const vfs_path_t * vpath)
4986 vfs_path_t *ret_vpath;
4987 GString *tmp_conv;
4988 int indx;
4990 ret_vpath = vfs_path_new (FALSE);
4992 tmp_conv = g_string_new ("");
4994 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4996 GIConv converter;
4997 vfs_path_element_t *path_element;
4999 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
5001 if (path_element->encoding == NULL)
5003 vfs_path_add_element (ret_vpath, path_element);
5004 continue;
5007 converter = str_crt_conv_to (path_element->encoding);
5008 if (converter == INVALID_CONV)
5010 vfs_path_add_element (ret_vpath, path_element);
5011 continue;
5014 MC_PTR_FREE (path_element->encoding);
5016 str_vfs_convert_from (converter, path_element->path, tmp_conv);
5018 g_free (path_element->path);
5019 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
5021 g_string_set_size (tmp_conv, 0);
5023 str_close_conv (converter);
5024 str_close_conv (path_element->dir.converter);
5025 path_element->dir.converter = INVALID_CONV;
5026 vfs_path_add_element (ret_vpath, path_element);
5028 g_string_free (tmp_conv, TRUE);
5029 return ret_vpath;
5031 #endif /* HAVE_CHARSET */
5033 /* --------------------------------------------------------------------------------------------- */
5036 * This routine reloads the directory in both panels. It tries to
5037 * select current_file in current_panel and other_file in other_panel.
5038 * If current_file == -1 then it automatically sets current_file and
5039 * other_file to the current files in the panels.
5041 * If flags has the UP_ONLY_CURRENT bit toggled on, then it
5042 * will not reload the other panel.
5044 * @param flags for reload panel
5045 * @param current_file name of the current file
5048 void
5049 update_panels (panel_update_flags_t flags, const char *current_file)
5051 WPanel *panel;
5053 /* first, update other panel... */
5054 if ((flags & UP_ONLY_CURRENT) == 0)
5055 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
5056 /* ...then current one */
5057 update_one_panel (get_current_index (), flags, current_file);
5059 if (get_current_type () == view_listing)
5060 panel = PANEL (get_panel_widget (get_current_index ()));
5061 else
5062 panel = PANEL (get_panel_widget (get_other_index ()));
5064 if (!panel->is_panelized)
5065 (void) mc_chdir (panel->cwd_vpath);
5068 /* --------------------------------------------------------------------------------------------- */
5070 gsize
5071 panel_get_num_of_sortable_fields (void)
5073 gsize ret = 0, lc_index;
5075 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5076 if (panel_fields[lc_index].is_user_choice)
5077 ret++;
5078 return ret;
5081 /* --------------------------------------------------------------------------------------------- */
5083 char **
5084 panel_get_sortable_fields (gsize * array_size)
5086 char **ret;
5087 gsize lc_index, i;
5089 lc_index = panel_get_num_of_sortable_fields ();
5091 ret = g_try_new0 (char *, lc_index + 1);
5092 if (ret == NULL)
5093 return NULL;
5095 if (array_size != NULL)
5096 *array_size = lc_index;
5098 lc_index = 0;
5100 for (i = 0; panel_fields[i].id != NULL; i++)
5101 if (panel_fields[i].is_user_choice)
5102 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
5104 return ret;
5107 /* --------------------------------------------------------------------------------------------- */
5109 const panel_field_t *
5110 panel_get_field_by_id (const char *name)
5112 gsize lc_index;
5114 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5115 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
5116 return &panel_fields[lc_index];
5118 return NULL;
5121 /* --------------------------------------------------------------------------------------------- */
5123 const panel_field_t *
5124 panel_get_field_by_title_hotkey (const char *name)
5126 gsize lc_index;
5128 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5129 if (panel_fields[lc_index].title_hotkey != NULL &&
5130 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
5131 return &panel_fields[lc_index];
5133 return NULL;
5136 /* --------------------------------------------------------------------------------------------- */
5138 const panel_field_t *
5139 panel_get_field_by_title (const char *name)
5141 gsize lc_index;
5143 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5145 const char *title;
5147 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
5148 if (strcmp (title, name) == 0)
5149 return &panel_fields[lc_index];
5152 return NULL;
5155 /* --------------------------------------------------------------------------------------------- */
5157 gsize
5158 panel_get_num_of_user_possible_fields (void)
5160 gsize ret = 0, lc_index;
5162 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5163 if (panel_fields[lc_index].use_in_user_format)
5164 ret++;
5166 return ret;
5169 /* --------------------------------------------------------------------------------------------- */
5171 char **
5172 panel_get_user_possible_fields (gsize * array_size)
5174 char **ret;
5175 gsize lc_index, i;
5177 lc_index = panel_get_num_of_user_possible_fields ();
5179 ret = g_try_new0 (char *, lc_index + 1);
5180 if (ret == NULL)
5181 return NULL;
5183 if (array_size != NULL)
5184 *array_size = lc_index;
5186 lc_index = 0;
5188 for (i = 0; panel_fields[i].id != NULL; i++)
5189 if (panel_fields[i].use_in_user_format)
5190 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
5192 return ret;
5195 /* --------------------------------------------------------------------------------------------- */
5197 void
5198 panel_panelize_cd (void)
5200 WPanel *panel;
5201 int i;
5202 dir_list *list;
5203 panelized_descr_t *pdescr;
5204 dir_list *plist;
5205 gboolean panelized_same;
5207 if (!SELECTED_IS_PANEL)
5208 create_panel (MENU_PANEL_IDX, view_listing);
5210 panel = PANEL (get_panel_widget (MENU_PANEL_IDX));
5212 dir_list_clean (&panel->dir);
5214 if (panel->panelized_descr == NULL)
5215 panel->panelized_descr = panelized_descr_new ();
5217 pdescr = panel->panelized_descr;
5218 plist = &pdescr->list;
5220 if (pdescr->root_vpath == NULL)
5221 panel_panelize_change_root (panel, panel->cwd_vpath);
5223 if (plist->len < 1)
5224 dir_list_init (plist);
5225 else if (plist->len > panel->dir.size)
5226 dir_list_grow (&panel->dir, plist->len - panel->dir.size);
5228 list = &panel->dir;
5229 list->len = plist->len;
5231 panelized_same = vfs_path_equal (pdescr->root_vpath, panel->cwd_vpath);
5233 for (i = 0; i < plist->len; i++)
5235 if (panelized_same || DIR_IS_DOTDOT (plist->list[i].fname->str))
5236 list->list[i].fname = mc_g_string_dup (plist->list[i].fname);
5237 else
5239 vfs_path_t *tmp_vpath;
5241 tmp_vpath =
5242 vfs_path_append_new (pdescr->root_vpath, plist->list[i].fname->str, (char *) NULL);
5243 list->list[i].fname = g_string_new_take (vfs_path_free (tmp_vpath, FALSE));
5245 list->list[i].f.link_to_dir = plist->list[i].f.link_to_dir;
5246 list->list[i].f.stale_link = plist->list[i].f.stale_link;
5247 list->list[i].f.dir_size_computed = plist->list[i].f.dir_size_computed;
5248 list->list[i].f.marked = plist->list[i].f.marked;
5249 list->list[i].st = plist->list[i].st;
5250 list->list[i].name_sort_key = plist->list[i].name_sort_key;
5251 list->list[i].extension_sort_key = plist->list[i].extension_sort_key;
5254 panel->is_panelized = TRUE;
5255 panel_panelize_absolutize_if_needed (panel);
5257 panel_set_current_by_name (panel, NULL);
5260 /* --------------------------------------------------------------------------------------------- */
5263 * Change root directory of panelized content.
5264 * @param panel file panel
5265 * @param new_root new path
5267 void
5268 panel_panelize_change_root (WPanel * panel, const vfs_path_t * new_root)
5270 if (panel->panelized_descr == NULL)
5271 panel->panelized_descr = panelized_descr_new ();
5272 else
5273 vfs_path_free (panel->panelized_descr->root_vpath, TRUE);
5275 panel->panelized_descr->root_vpath = vfs_path_clone (new_root);
5278 /* --------------------------------------------------------------------------------------------- */
5281 * Conditionally switches a panel's directory to "/" (root).
5283 * If a panelized panel's listing contain absolute paths, this function
5284 * sets the panel's directory to "/". Otherwise it does nothing.
5286 * Rationale:
5288 * This makes tokenized strings like "%d/%p" work. This also makes other
5289 * places work where such naive concatenation is done in code (e.g., when
5290 * pressing ctrl+shift+enter, for CK_PutCurrentFullSelected).
5292 * When to call:
5294 * You should always call this function after you populate the listing
5295 * of a panelized panel.
5297 void
5298 panel_panelize_absolutize_if_needed (WPanel * panel)
5300 const dir_list *const list = &panel->dir;
5302 /* Note: We don't support mixing of absolute and relative paths, which is
5303 * why it's ok for us to check only the 1st entry. */
5304 if (list->len > 1 && g_path_is_absolute (list->list[1].fname->str))
5306 vfs_path_t *root;
5308 root = vfs_path_from_str (PATH_SEP_STR);
5309 panel_set_cwd (panel, root);
5310 if (panel == current_panel)
5311 mc_chdir (root);
5312 vfs_path_free (root, TRUE);
5316 /* --------------------------------------------------------------------------------------------- */
5318 void
5319 panel_panelize_save (WPanel * panel)
5321 int i;
5322 dir_list *list = &panel->dir;
5323 dir_list *plist;
5325 panel_panelize_change_root (panel, panel->cwd_vpath);
5327 plist = &panel->panelized_descr->list;
5329 if (plist->len > 0)
5330 dir_list_clean (plist);
5331 if (panel->dir.len == 0)
5332 return;
5334 if (panel->dir.len > plist->size)
5335 dir_list_grow (plist, panel->dir.len - plist->size);
5336 plist->len = panel->dir.len;
5338 for (i = 0; i < panel->dir.len; i++)
5340 plist->list[i].fname = mc_g_string_dup (list->list[i].fname);
5341 plist->list[i].f.link_to_dir = list->list[i].f.link_to_dir;
5342 plist->list[i].f.stale_link = list->list[i].f.stale_link;
5343 plist->list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
5344 plist->list[i].f.marked = list->list[i].f.marked;
5345 plist->list[i].st = list->list[i].st;
5346 plist->list[i].name_sort_key = list->list[i].name_sort_key;
5347 plist->list[i].extension_sort_key = list->list[i].extension_sort_key;
5351 /* --------------------------------------------------------------------------------------------- */
5353 void
5354 panel_init (void)
5356 panel_sort_up_char = mc_skin_get ("widget-panel", "sort-up-char", "'");
5357 panel_sort_down_char = mc_skin_get ("widget-panel", "sort-down-char", ".");
5358 panel_hiddenfiles_show_char = mc_skin_get ("widget-panel", "hiddenfiles-show-char", ".");
5359 panel_hiddenfiles_hide_char = mc_skin_get ("widget-panel", "hiddenfiles-hide-char", ".");
5360 panel_history_prev_item_char = mc_skin_get ("widget-panel", "history-prev-item-char", "<");
5361 panel_history_next_item_char = mc_skin_get ("widget-panel", "history-next-item-char", ">");
5362 panel_history_show_list_char = mc_skin_get ("widget-panel", "history-show-list-char", "^");
5363 panel_filename_scroll_left_char =
5364 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
5365 panel_filename_scroll_right_char =
5366 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
5368 string_file_name_buffer = g_string_sized_new (MC_MAXFILENAMELEN);
5370 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
5371 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_current_file_to_clip_file",
5372 panel_save_current_file_to_clip_file, NULL, NULL);
5375 /* --------------------------------------------------------------------------------------------- */
5377 void
5378 panel_deinit (void)
5380 g_free (panel_sort_up_char);
5381 g_free (panel_sort_down_char);
5382 g_free (panel_hiddenfiles_show_char);
5383 g_free (panel_hiddenfiles_hide_char);
5384 g_free (panel_history_prev_item_char);
5385 g_free (panel_history_next_item_char);
5386 g_free (panel_history_show_list_char);
5387 g_free (panel_filename_scroll_left_char);
5388 g_free (panel_filename_scroll_right_char);
5389 g_string_free (string_file_name_buffer, TRUE);
5392 /* --------------------------------------------------------------------------------------------- */
5394 gboolean
5395 panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum exact)
5397 gboolean res;
5398 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
5400 if (panel->is_panelized)
5402 size_t new_vpath_len;
5404 new_vpath_len = vfs_path_len (new_dir_vpath);
5405 if (vfs_path_equal_len (new_dir_vpath, panel->panelized_descr->root_vpath, new_vpath_len))
5406 _new_dir_vpath = panel->panelized_descr->root_vpath;
5409 res = panel_do_cd (panel, _new_dir_vpath, exact);
5411 #ifdef HAVE_CHARSET
5412 if (res)
5414 const vfs_path_element_t *path_element;
5416 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
5417 if (path_element->encoding != NULL)
5418 panel->codepage = get_codepage_index (path_element->encoding);
5419 else
5420 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
5422 #endif /* HAVE_CHARSET */
5424 return res;
5427 /* --------------------------------------------------------------------------------------------- */