Use g_string_new_take().
[midnight-commander.git] / src / filemanager / panel.c
blob46631465c524c1f7870a8271ca5f579f8b0785c6
1 /*
2 Panel managing.
4 Copyright (C) 1994-2023
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/strescape.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 = strutils_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 = strutils_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 ();
3440 vfs_path_free (olddir_vpath, TRUE);
3442 return TRUE;
3445 /* --------------------------------------------------------------------------------------------- */
3447 static void
3448 directory_history_next (WPanel * panel)
3450 gboolean ok;
3454 GList *next;
3456 ok = TRUE;
3457 next = g_list_next (panel->dir_history.current);
3458 if (next != NULL)
3460 vfs_path_t *data_vpath;
3462 data_vpath = vfs_path_from_str ((char *) next->data);
3463 ok = panel_do_cd_int (panel, data_vpath, cd_exact);
3464 vfs_path_free (data_vpath, TRUE);
3465 panel->dir_history.current = next;
3467 /* skip directories that present in history but absent in file system */
3469 while (!ok);
3472 /* --------------------------------------------------------------------------------------------- */
3474 static void
3475 directory_history_prev (WPanel * panel)
3477 gboolean ok;
3481 GList *prev;
3483 ok = TRUE;
3484 prev = g_list_previous (panel->dir_history.current);
3485 if (prev != NULL)
3487 vfs_path_t *data_vpath;
3489 data_vpath = vfs_path_from_str ((char *) prev->data);
3490 ok = panel_do_cd_int (panel, data_vpath, cd_exact);
3491 vfs_path_free (data_vpath, TRUE);
3492 panel->dir_history.current = prev;
3494 /* skip directories that present in history but absent in file system */
3496 while (!ok);
3499 /* --------------------------------------------------------------------------------------------- */
3501 static void
3502 directory_history_list (WPanel * panel)
3504 history_descriptor_t hd;
3505 gboolean ok = FALSE;
3506 size_t pos;
3508 pos = g_list_position (panel->dir_history.current, panel->dir_history.list);
3510 history_descriptor_init (&hd, WIDGET (panel)->rect.y, WIDGET (panel)->rect.x,
3511 panel->dir_history.list, (int) pos);
3512 history_show (&hd);
3514 panel->dir_history.list = hd.list;
3515 if (hd.text != NULL)
3517 vfs_path_t *s_vpath;
3519 s_vpath = vfs_path_from_str (hd.text);
3520 ok = panel_do_cd_int (panel, s_vpath, cd_exact);
3521 if (ok)
3522 directory_history_add (panel, panel->cwd_vpath);
3523 else
3524 cd_error_message (hd.text);
3525 vfs_path_free (s_vpath, TRUE);
3526 g_free (hd.text);
3529 if (!ok)
3531 /* Since history is fully modified in history_show(), panel->dir_history actually
3532 * points to the invalid place. Try restore current position here. */
3534 size_t i;
3536 panel->dir_history.current = panel->dir_history.list;
3538 for (i = 0; i <= pos; i++)
3540 GList *prev;
3542 prev = g_list_previous (panel->dir_history.current);
3543 if (prev == NULL)
3544 break;
3546 panel->dir_history.current = prev;
3551 /* --------------------------------------------------------------------------------------------- */
3553 static cb_ret_t
3554 panel_execute_cmd (WPanel * panel, long command)
3556 int res = MSG_HANDLED;
3558 if (command != CK_Search)
3559 stop_search (panel);
3561 switch (command)
3563 case CK_Up:
3564 case CK_Down:
3565 case CK_Left:
3566 case CK_Right:
3567 case CK_Bottom:
3568 case CK_Top:
3569 case CK_PageDown:
3570 case CK_PageUp:
3571 /* reset state of marks flag */
3572 state_mark = -1;
3573 break;
3574 default:
3575 break;
3578 switch (command)
3580 case CK_CycleListingFormat:
3581 panel_cycle_listing_format (panel);
3582 break;
3583 case CK_PanelOtherCd:
3584 chdir_other_panel (panel);
3585 break;
3586 case CK_PanelOtherCdLink:
3587 chdir_to_readlink (panel);
3588 break;
3589 case CK_CopySingle:
3590 copy_cmd_local (panel);
3591 break;
3592 case CK_DeleteSingle:
3593 delete_cmd_local (panel);
3594 break;
3595 case CK_Enter:
3596 do_enter (panel);
3597 break;
3598 case CK_ViewRaw:
3599 view_raw_cmd (panel);
3600 break;
3601 case CK_EditNew:
3602 edit_cmd_new ();
3603 break;
3604 case CK_MoveSingle:
3605 rename_cmd_local (panel);
3606 break;
3607 case CK_SelectInvert:
3608 panel_select_invert_files (panel);
3609 break;
3610 case CK_Select:
3611 panel_select_files (panel);
3612 break;
3613 case CK_SelectExt:
3614 panel_select_ext_cmd (panel);
3615 break;
3616 case CK_Unselect:
3617 panel_unselect_files (panel);
3618 break;
3619 case CK_Filter:
3620 panel_do_set_filter (panel);
3621 break;
3622 case CK_PageDown:
3623 next_page (panel);
3624 break;
3625 case CK_PageUp:
3626 prev_page (panel);
3627 break;
3628 case CK_CdChild:
3629 goto_child_dir (panel);
3630 break;
3631 case CK_CdParent:
3632 goto_parent_dir (panel);
3633 break;
3634 case CK_History:
3635 directory_history_list (panel);
3636 break;
3637 case CK_HistoryNext:
3638 directory_history_next (panel);
3639 break;
3640 case CK_HistoryPrev:
3641 directory_history_prev (panel);
3642 break;
3643 case CK_BottomOnScreen:
3644 goto_bottom_file (panel);
3645 break;
3646 case CK_MiddleOnScreen:
3647 goto_middle_file (panel);
3648 break;
3649 case CK_TopOnScreen:
3650 goto_top_file (panel);
3651 break;
3652 case CK_Mark:
3653 mark_file (panel);
3654 break;
3655 case CK_MarkUp:
3656 mark_file_up (panel);
3657 break;
3658 case CK_MarkDown:
3659 mark_file_down (panel);
3660 break;
3661 case CK_MarkLeft:
3662 mark_file_left (panel);
3663 break;
3664 case CK_MarkRight:
3665 mark_file_right (panel);
3666 break;
3667 case CK_CdParentSmart:
3668 res = force_maybe_cd (panel);
3669 break;
3670 case CK_Up:
3671 move_up (panel);
3672 break;
3673 case CK_Down:
3674 move_down (panel);
3675 break;
3676 case CK_Left:
3677 res = move_left (panel);
3678 break;
3679 case CK_Right:
3680 res = move_right (panel);
3681 break;
3682 case CK_Bottom:
3683 move_end (panel);
3684 break;
3685 case CK_Top:
3686 move_home (panel);
3687 break;
3688 #ifdef HAVE_CHARSET
3689 case CK_SelectCodepage:
3690 panel_change_encoding (panel);
3691 break;
3692 #endif
3693 case CK_ScrollLeft:
3694 panel_content_scroll_left (panel);
3695 break;
3696 case CK_ScrollRight:
3697 panel_content_scroll_right (panel);
3698 break;
3699 case CK_Search:
3700 start_search (panel);
3701 break;
3702 case CK_SearchStop:
3703 break;
3704 case CK_PanelOtherSync:
3705 panel_sync_other (panel);
3706 break;
3707 case CK_Sort:
3708 panel_select_sort_order (panel);
3709 break;
3710 case CK_SortPrev:
3711 panel_toggle_sort_order_prev (panel);
3712 break;
3713 case CK_SortNext:
3714 panel_toggle_sort_order_next (panel);
3715 break;
3716 case CK_SortReverse:
3717 panel->sort_info.reverse = !panel->sort_info.reverse;
3718 panel_set_sort_order (panel, panel->sort_field);
3719 break;
3720 case CK_SortByName:
3721 panel_set_sort_type_by_id (panel, "name");
3722 break;
3723 case CK_SortByExt:
3724 panel_set_sort_type_by_id (panel, "extension");
3725 break;
3726 case CK_SortBySize:
3727 panel_set_sort_type_by_id (panel, "size");
3728 break;
3729 case CK_SortByMTime:
3730 panel_set_sort_type_by_id (panel, "mtime");
3731 break;
3732 default:
3733 res = MSG_NOT_HANDLED;
3734 break;
3737 return res;
3740 /* --------------------------------------------------------------------------------------------- */
3742 static cb_ret_t
3743 panel_key (WPanel * panel, int key)
3745 long command;
3747 if (is_abort_char (key))
3749 stop_search (panel);
3750 return MSG_HANDLED;
3753 if (panel->quick_search.active && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3755 do_search (panel, key);
3756 return MSG_HANDLED;
3759 command = widget_lookup_key (WIDGET (panel), key);
3760 if (command != CK_IgnoreKey)
3761 return panel_execute_cmd (panel, command);
3763 if (panels_options.torben_fj_mode && key == ALT ('h'))
3765 goto_middle_file (panel);
3766 return MSG_HANDLED;
3769 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3771 start_search (panel);
3772 do_search (panel, key);
3773 return MSG_HANDLED;
3776 return MSG_NOT_HANDLED;
3779 /* --------------------------------------------------------------------------------------------- */
3781 static cb_ret_t
3782 panel_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3784 WPanel *panel = PANEL (w);
3785 WDialog *h = DIALOG (w->owner);
3786 WButtonBar *bb;
3788 switch (msg)
3790 case MSG_INIT:
3791 /* subscribe to "history_load" event */
3792 mc_event_add (h->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3793 /* subscribe to "history_save" event */
3794 mc_event_add (h->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3795 return MSG_HANDLED;
3797 case MSG_DRAW:
3798 /* Repaint everything, including frame and separator */
3799 widget_erase (w);
3800 show_dir (panel);
3801 panel_print_header (panel);
3802 adjust_top_file (panel);
3803 paint_dir (panel);
3804 mini_info_separator (panel);
3805 display_mini_info (panel);
3806 panel->dirty = FALSE;
3807 return MSG_HANDLED;
3809 case MSG_FOCUS:
3810 state_mark = -1;
3811 current_panel = panel;
3812 panel->active = TRUE;
3814 if (mc_chdir (panel->cwd_vpath) != 0)
3816 char *cwd;
3818 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3819 cd_error_message (cwd);
3820 g_free (cwd);
3822 else
3823 subshell_chdir (panel->cwd_vpath);
3825 update_xterm_title_path ();
3826 select_item (panel);
3828 bb = buttonbar_find (h);
3829 midnight_set_buttonbar (bb);
3830 widget_draw (WIDGET (bb));
3831 return MSG_HANDLED;
3833 case MSG_UNFOCUS:
3834 /* Janne: look at this for the multiple panel options */
3835 stop_search (panel);
3836 panel->active = FALSE;
3837 unselect_item (panel);
3838 return MSG_HANDLED;
3840 case MSG_KEY:
3841 return panel_key (panel, parm);
3843 case MSG_ACTION:
3844 return panel_execute_cmd (panel, parm);
3846 case MSG_DESTROY:
3847 vfs_stamp_path (panel->cwd_vpath);
3848 /* unsubscribe from "history_load" event */
3849 mc_event_del (h->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3850 /* unsubscribe from "history_save" event */
3851 mc_event_del (h->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3852 panel_destroy (panel);
3853 free_my_statfs ();
3854 return MSG_HANDLED;
3856 default:
3857 return widget_default_callback (w, sender, msg, parm, data);
3861 /* --------------------------------------------------------------------------------------------- */
3862 /* */
3863 /* Panel mouse events support routines */
3864 /* */
3866 static void
3867 mouse_toggle_mark (WPanel * panel)
3869 do_mark_file (panel, MARK_DONT_MOVE);
3870 mouse_marking = (panel_current_entry (panel)->f.marked != 0);
3871 mouse_mark_panel = current_panel;
3874 /* --------------------------------------------------------------------------------------------- */
3876 static void
3877 mouse_set_mark (WPanel * panel)
3879 if (mouse_mark_panel == panel)
3881 const file_entry_t *fe;
3883 fe = panel_current_entry (panel);
3885 if (mouse_marking && fe->f.marked == 0)
3886 do_mark_file (panel, MARK_DONT_MOVE);
3887 else if (!mouse_marking && fe->f.marked != 0)
3888 do_mark_file (panel, MARK_DONT_MOVE);
3892 /* --------------------------------------------------------------------------------------------- */
3894 static void
3895 mark_if_marking (WPanel * panel, const mouse_event_t * event, int previous_current)
3897 if ((event->buttons & GPM_B_RIGHT) == 0)
3898 return;
3900 if (event->msg == MSG_MOUSE_DOWN)
3901 mouse_toggle_mark (panel);
3902 else
3904 int pcurr, curr1, curr2;
3906 pcurr = panel->current;
3907 curr1 = MIN (previous_current, panel->current);
3908 curr2 = MAX (previous_current, panel->current);
3910 for (; curr1 <= curr2; curr1++)
3912 panel->current = curr1;
3913 mouse_set_mark (panel);
3916 panel->current = pcurr;
3920 /* --------------------------------------------------------------------------------------------- */
3921 /** Determine which column was clicked, and sort the panel on
3922 * that column, or reverse sort on that column if already
3923 * sorted on that column.
3926 static void
3927 mouse_sort_col (WPanel * panel, int x)
3929 int i = 0;
3930 GSList *format;
3931 const char *lc_sort_name = NULL;
3932 panel_field_t *col_sort_format = NULL;
3934 for (format = panel->format; format != NULL; format = g_slist_next (format))
3936 format_item_t *fi = (format_item_t *) format->data;
3938 i += fi->field_len;
3939 if (x < i + 1)
3941 /* found column */
3942 lc_sort_name = fi->title;
3943 break;
3947 if (lc_sort_name == NULL)
3948 return;
3950 for (i = 0; panel_fields[i].id != NULL; i++)
3952 const char *title;
3954 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3955 if (panel_fields[i].sort_routine != NULL && strcmp (title, lc_sort_name) == 0)
3957 col_sort_format = &panel_fields[i];
3958 break;
3962 if (col_sort_format != NULL)
3964 if (panel->sort_field == col_sort_format)
3965 /* reverse the sort if clicked column is already the sorted column */
3966 panel->sort_info.reverse = !panel->sort_info.reverse;
3967 else
3968 /* new sort is forced to be ascending */
3969 panel->sort_info.reverse = FALSE;
3971 panel_set_sort_order (panel, col_sort_format);
3975 /* --------------------------------------------------------------------------------------------- */
3977 static int
3978 panel_mouse_is_on_item (const WPanel * panel, int y, int x)
3980 int lines, col_width, col;
3982 if (y < 0)
3983 return MOUSE_UPPER_FILE_LIST;
3985 lines = panel_lines (panel);
3986 if (y >= lines)
3987 return MOUSE_BELOW_FILE_LIST;
3989 col_width = (CONST_WIDGET (panel)->rect.cols - 2) / panel->list_cols;
3990 /* column where mouse is */
3991 col = x / col_width;
3993 y += panel->top + lines * col;
3995 /* are we below or in the next column of last file? */
3996 if (y > panel->dir.len)
3997 return MOUSE_AFTER_LAST_FILE;
3999 /* we are on item of the file file; return an index to select a file */
4000 return y;
4003 /* --------------------------------------------------------------------------------------------- */
4005 static void
4006 panel_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
4008 WPanel *panel = PANEL (w);
4009 gboolean is_active;
4011 is_active = widget_is_active (w);
4013 switch (msg)
4015 case MSG_MOUSE_DOWN:
4016 if (event->y == 0)
4018 /* top frame */
4019 if (event->x == 1)
4020 /* "<" button */
4021 directory_history_prev (panel);
4022 else if (event->x == w->rect.cols - 2)
4023 /* ">" button */
4024 directory_history_next (panel);
4025 else if (event->x >= w->rect.cols - 5 && event->x <= w->rect.cols - 3)
4026 /* "^" button */
4027 directory_history_list (panel);
4028 else if (event->x == w->rect.cols - 6)
4029 /* "." button show/hide hidden files */
4030 send_message (filemanager, NULL, MSG_ACTION, CK_ShowHidden, NULL);
4031 else
4033 /* no other events on 1st line, return MOU_UNHANDLED */
4034 event->result.abort = TRUE;
4035 /* avoid extra panel redraw */
4036 panel->dirty = FALSE;
4038 break;
4041 if (event->y == 1)
4043 /* sort on clicked column */
4044 mouse_sort_col (panel, event->x + 1);
4045 break;
4048 if (!is_active)
4049 (void) change_panel ();
4050 MC_FALLTHROUGH;
4052 case MSG_MOUSE_DRAG:
4054 int my_index;
4055 int previous_current;
4057 my_index = panel_mouse_is_on_item (panel, event->y - 2, event->x);
4058 previous_current = panel->current;
4060 switch (my_index)
4062 case MOUSE_UPPER_FILE_LIST:
4063 move_up (panel);
4064 mark_if_marking (panel, event, previous_current);
4065 break;
4067 case MOUSE_BELOW_FILE_LIST:
4068 move_down (panel);
4069 mark_if_marking (panel, event, previous_current);
4070 break;
4072 case MOUSE_AFTER_LAST_FILE:
4073 break; /* do nothing */
4075 default:
4076 if (my_index != panel->current)
4078 unselect_item (panel);
4079 panel->current = my_index;
4080 select_item (panel);
4083 mark_if_marking (panel, event, previous_current);
4084 break;
4087 break;
4089 case MSG_MOUSE_UP:
4090 break;
4092 case MSG_MOUSE_CLICK:
4093 if ((event->count & GPM_DOUBLE) != 0 && (event->buttons & GPM_B_LEFT) != 0 &&
4094 panel_mouse_is_on_item (panel, event->y - 2, event->x) >= 0)
4095 do_enter (panel);
4096 break;
4098 case MSG_MOUSE_MOVE:
4099 break;
4101 case MSG_MOUSE_SCROLL_UP:
4102 if (is_active)
4104 if (panels_options.mouse_move_pages && panel->top > 0)
4105 prev_page (panel);
4106 else /* We are in first page */
4107 move_up (panel);
4109 break;
4111 case MSG_MOUSE_SCROLL_DOWN:
4112 if (is_active)
4114 if (panels_options.mouse_move_pages
4115 && panel->top + panel_items (panel) < panel->dir.len)
4116 next_page (panel);
4117 else /* We are in last page */
4118 move_down (panel);
4120 break;
4122 default:
4123 break;
4126 if (panel->dirty)
4127 widget_draw (w);
4130 /* --------------------------------------------------------------------------------------------- */
4132 static void
4133 reload_panelized (WPanel * panel)
4135 int i, j;
4136 dir_list *list = &panel->dir;
4138 /* refresh current VFS directory required for vfs_path_from_str() */
4139 (void) mc_chdir (panel->cwd_vpath);
4141 for (i = 0, j = 0; i < list->len; i++)
4143 vfs_path_t *vpath;
4145 vpath = vfs_path_from_str (list->list[i].fname->str);
4146 if (mc_lstat (vpath, &list->list[i].st) != 0)
4147 g_string_free (list->list[i].fname, TRUE);
4148 else
4150 if (j != i)
4151 list->list[j] = list->list[i];
4152 j++;
4154 vfs_path_free (vpath, TRUE);
4156 if (j == 0)
4157 dir_list_init (list);
4158 else
4159 list->len = j;
4161 recalculate_panel_summary (panel);
4163 if (panel != current_panel)
4164 (void) mc_chdir (current_panel->cwd_vpath);
4167 /* --------------------------------------------------------------------------------------------- */
4169 static void
4170 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
4172 gboolean free_pointer;
4173 char *my_current_file = NULL;
4175 if ((flags & UP_RELOAD) != 0)
4177 panel->is_panelized = FALSE;
4178 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, NULL);
4179 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4182 /* If current_file == -1 (an invalid pointer) then preserve current */
4183 free_pointer = current_file == UP_KEEPSEL;
4185 if (free_pointer)
4187 const GString *fname;
4189 fname = panel_current_entry (panel)->fname;
4190 my_current_file = g_strndup (fname->str, fname->len);
4191 current_file = my_current_file;
4194 if (panel->is_panelized)
4195 reload_panelized (panel);
4196 else
4197 panel_reload (panel);
4199 panel_set_current_by_name (panel, current_file);
4200 panel->dirty = TRUE;
4202 if (free_pointer)
4203 g_free (my_current_file);
4206 /* --------------------------------------------------------------------------------------------- */
4208 static void
4209 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
4211 if (get_panel_type (which) == view_listing)
4213 WPanel *panel;
4215 panel = PANEL (get_panel_widget (which));
4216 if (panel->is_panelized)
4217 flags &= ~UP_RELOAD;
4218 update_one_panel_widget (panel, flags, current_file);
4222 /* --------------------------------------------------------------------------------------------- */
4224 static void
4225 panel_set_current (WPanel * panel, int i)
4227 if (i != panel->current)
4229 panel->dirty = TRUE;
4230 panel->current = i;
4231 panel->top = panel->current - (WIDGET (panel)->rect.lines - 2) / 2;
4232 if (panel->top < 0)
4233 panel->top = 0;
4237 /* --------------------------------------------------------------------------------------------- */
4239 /* event callback */
4240 static gboolean
4241 event_update_panels (const gchar * event_group_name, const gchar * event_name,
4242 gpointer init_data, gpointer data)
4244 (void) event_group_name;
4245 (void) event_name;
4246 (void) init_data;
4247 (void) data;
4249 update_panels (UP_RELOAD, UP_KEEPSEL);
4251 return TRUE;
4254 /* --------------------------------------------------------------------------------------------- */
4256 /* event callback */
4257 static gboolean
4258 panel_save_current_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
4259 gpointer init_data, gpointer data)
4261 (void) event_group_name;
4262 (void) event_name;
4263 (void) init_data;
4264 (void) data;
4266 if (current_panel->marked == 0)
4267 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
4268 (gpointer) panel_current_entry (current_panel)->fname->str);
4269 else
4271 int i;
4272 gboolean first = TRUE;
4273 char *flist = NULL;
4275 for (i = 0; i < current_panel->dir.len; i++)
4277 const file_entry_t *fe = &current_panel->dir.list[i];
4279 if (fe->f.marked != 0)
4280 { /* Skip the unmarked ones */
4281 if (first)
4283 flist = g_strndup (fe->fname->str, fe->fname->len);
4284 first = FALSE;
4286 else
4288 /* Add empty lines after the file */
4289 char *tmp;
4291 tmp = g_strconcat (flist, "\n", fe->fname->str, (char *) NULL);
4292 g_free (flist);
4293 flist = tmp;
4298 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
4299 g_free (flist);
4301 return TRUE;
4304 /* --------------------------------------------------------------------------------------------- */
4306 static vfs_path_t *
4307 panel_recursive_cd_to_parent (const vfs_path_t * vpath)
4309 vfs_path_t *cwd_vpath;
4311 cwd_vpath = vfs_path_clone (vpath);
4313 while (mc_chdir (cwd_vpath) < 0)
4315 const char *panel_cwd_path;
4316 vfs_path_t *tmp_vpath;
4318 /* check if path contains only '/' */
4319 panel_cwd_path = vfs_path_as_str (cwd_vpath);
4320 if (panel_cwd_path != NULL && IS_PATH_SEP (panel_cwd_path[0]) && panel_cwd_path[1] == '\0')
4322 vfs_path_free (cwd_vpath, TRUE);
4323 return NULL;
4326 tmp_vpath = vfs_path_vtokens_get (cwd_vpath, 0, -1);
4327 vfs_path_free (cwd_vpath, TRUE);
4328 cwd_vpath =
4329 vfs_path_build_filename (PATH_SEP_STR, vfs_path_as_str (tmp_vpath), (char *) NULL);
4330 vfs_path_free (tmp_vpath, TRUE);
4333 return cwd_vpath;
4336 /* --------------------------------------------------------------------------------------------- */
4338 static void
4339 panel_dir_list_callback (dir_list_cb_state_t state, void *data)
4341 static int count = 0;
4343 (void) data;
4345 switch (state)
4347 case DIR_OPEN:
4348 count = 0;
4349 break;
4351 case DIR_READ:
4352 count++;
4353 if ((count & 15) == 0)
4354 rotate_dash (TRUE);
4355 break;
4357 case DIR_CLOSE:
4358 rotate_dash (FALSE);
4359 break;
4361 default:
4362 g_assert_not_reached ();
4366 /* --------------------------------------------------------------------------------------------- */
4367 /*** public functions ****************************************************************************/
4368 /* --------------------------------------------------------------------------------------------- */
4370 void
4371 panel_set_current_by_name (WPanel * panel, const char *name)
4373 int i;
4374 char *subdir;
4376 if (name == NULL)
4378 panel_set_current (panel, 0);
4379 return;
4382 /* We only want the last component of the directory,
4383 * and from this only the name without suffix.
4384 * Cut prefix if the panel is not panelized */
4385 if (panel->is_panelized)
4386 subdir = vfs_strip_suffix_from_filename (name);
4387 else
4388 subdir = vfs_strip_suffix_from_filename (x_basename (name));
4390 /* Search that subdir or filename without prefix (if not panelized panel),
4391 make it current if found */
4392 for (i = 0; i < panel->dir.len; i++)
4393 if (strcmp (subdir, panel->dir.list[i].fname->str) == 0)
4395 panel_set_current (panel, i);
4396 g_free (subdir);
4397 return;
4400 /* Make current near the filee that is missing */
4401 if (panel->current >= panel->dir.len)
4402 panel_set_current (panel, panel->dir.len - 1);
4403 g_free (subdir);
4405 select_item (panel);
4408 /* --------------------------------------------------------------------------------------------- */
4410 void
4411 panel_clean_dir (WPanel * panel)
4413 panel->top = 0;
4414 panel->current = 0;
4415 panel->marked = 0;
4416 panel->dirs_marked = 0;
4417 panel->total = 0;
4418 panel->quick_search.active = FALSE;
4419 panel->is_panelized = FALSE;
4420 panel->dirty = TRUE;
4421 panel->content_shift = -1;
4422 panel->max_shift = -1;
4424 dir_list_free_list (&panel->dir);
4427 /* --------------------------------------------------------------------------------------------- */
4429 * Set Up panel's current dir object
4431 * @param panel panel object
4432 * @param path_str string contain path
4435 void
4436 panel_set_cwd (WPanel * panel, const vfs_path_t * vpath)
4438 if (vpath != panel->cwd_vpath) /* check if new vpath is not the panel->cwd_vpath object */
4440 vfs_path_free (panel->cwd_vpath, TRUE);
4441 panel->cwd_vpath = vfs_path_clone (vpath);
4445 /* --------------------------------------------------------------------------------------------- */
4447 * Set Up panel's last working dir object
4449 * @param panel panel object
4450 * @param path_str string contain path
4453 void
4454 panel_set_lwd (WPanel * panel, const vfs_path_t * vpath)
4456 if (vpath != panel->lwd_vpath) /* check if new vpath is not the panel->lwd_vpath object */
4458 vfs_path_free (panel->lwd_vpath, TRUE);
4459 panel->lwd_vpath = vfs_path_clone (vpath);
4463 /* --------------------------------------------------------------------------------------------- */
4465 * Creatie an empty panel with specified size.
4467 * @param panel_name name of panel for setup receiving
4469 * @return new instance of WPanel
4472 WPanel *
4473 panel_sized_empty_new (const char *panel_name, int y, int x, int lines, int cols)
4475 WRect r = { y, x, lines, cols };
4476 WPanel *panel;
4477 Widget *w;
4478 char *section;
4479 int i, err;
4481 panel = g_new0 (WPanel, 1);
4482 w = WIDGET (panel);
4483 widget_init (w, &r, panel_callback, panel_mouse_callback);
4484 w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
4485 w->keymap = panel_map;
4487 panel->dir.size = DIR_LIST_MIN_SIZE;
4488 panel->dir.list = g_new (file_entry_t, panel->dir.size);
4489 panel->dir.len = 0;
4490 panel->dir.callback = panel_dir_list_callback;
4492 panel->list_cols = 1;
4493 panel->brief_cols = 2;
4494 panel->dirty = TRUE;
4495 panel->content_shift = -1;
4496 panel->max_shift = -1;
4498 panel->list_format = list_full;
4499 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4501 panel->filter.flags = FILE_FILTER_DEFAULT_FLAGS;
4503 for (i = 0; i < LIST_FORMATS; i++)
4504 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4506 #ifdef HAVE_CHARSET
4507 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4508 #endif
4510 panel->frame_size = frame_half;
4512 panel->quick_search.buffer = g_string_sized_new (MC_MAXFILENAMELEN);
4513 panel->quick_search.prev_buffer = g_string_sized_new (MC_MAXFILENAMELEN);
4515 panel->name = g_strdup (panel_name);
4516 panel->dir_history.name = g_strconcat ("Dir Hist ", panel->name, (char *) NULL);
4517 /* directories history will be get later */
4519 section = g_strconcat ("Temporal:", panel->name, (char *) NULL);
4520 if (!mc_config_has_group (mc_global.main_config, section))
4522 g_free (section);
4523 section = g_strdup (panel->name);
4525 panel_load_setup (panel, section);
4526 g_free (section);
4528 if (panel->filter.value != NULL)
4530 gboolean case_sens = (panel->filter.flags & SELECT_MATCH_CASE) != 0;
4531 gboolean shell_patterns = (panel->filter.flags & SELECT_SHELL_PATTERNS) != 0;
4533 panel->filter.handler = mc_search_new (panel->filter.value, NULL);
4534 panel->filter.handler->search_type = shell_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
4535 panel->filter.handler->is_entire_line = TRUE;
4536 panel->filter.handler->is_case_sensitive = case_sens;
4538 /* FIXME: silent check -- do not display an error message */
4539 if (!mc_search_prepare (panel->filter.handler))
4540 file_filter_clear (&panel->filter);
4543 /* Load format strings */
4544 err = set_panel_formats (panel);
4545 if (err != 0)
4546 set_panel_formats (panel);
4548 return panel;
4551 /* --------------------------------------------------------------------------------------------- */
4553 * Panel creation for specified size and directory.
4555 * @param panel_name name of panel for setup retrieving
4556 * @param y y coordinate of top-left corner
4557 * @param x x coordinate of top-left corner
4558 * @param lines vertical size
4559 * @param cols horizontal size
4560 * @param vpath working panel directory. If NULL then current directory is used
4562 * @return new instance of WPanel
4565 WPanel *
4566 panel_sized_with_dir_new (const char *panel_name, int y, int x, int lines, int cols,
4567 const vfs_path_t * vpath)
4569 WPanel *panel;
4570 char *curdir = NULL;
4571 #ifdef HAVE_CHARSET
4572 const vfs_path_element_t *path_element;
4573 #endif
4575 panel = panel_sized_empty_new (panel_name, y, x, lines, cols);
4577 if (vpath != NULL)
4579 curdir = vfs_get_cwd ();
4580 panel_set_cwd (panel, vpath);
4582 else
4584 vfs_setup_cwd ();
4585 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4588 panel_set_lwd (panel, vfs_get_raw_current_dir ());
4590 #ifdef HAVE_CHARSET
4591 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4592 if (path_element->encoding != NULL)
4593 panel->codepage = get_codepage_index (path_element->encoding);
4594 #endif
4596 if (mc_chdir (panel->cwd_vpath) != 0)
4598 #ifdef HAVE_CHARSET
4599 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4600 #endif
4601 vfs_setup_cwd ();
4602 vfs_path_free (panel->cwd_vpath, TRUE);
4603 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4606 /* Load the default format */
4607 if (!dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4608 &panel->sort_info, &panel->filter))
4609 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
4611 /* Restore old right path */
4612 if (curdir != NULL)
4614 vfs_path_t *tmp_vpath;
4615 int err;
4617 tmp_vpath = vfs_path_from_str (curdir);
4618 mc_chdir (tmp_vpath);
4619 vfs_path_free (tmp_vpath, TRUE);
4620 (void) err;
4622 g_free (curdir);
4624 return panel;
4627 /* --------------------------------------------------------------------------------------------- */
4629 void
4630 panel_reload (WPanel * panel)
4632 struct stat current_stat;
4633 vfs_path_t *cwd_vpath;
4635 if (panels_options.fast_reload && stat (vfs_path_as_str (panel->cwd_vpath), &current_stat) == 0
4636 && current_stat.st_ctime == panel->dir_stat.st_ctime
4637 && current_stat.st_mtime == panel->dir_stat.st_mtime)
4638 return;
4640 cwd_vpath = panel_recursive_cd_to_parent (panel->cwd_vpath);
4641 vfs_path_free (panel->cwd_vpath, TRUE);
4643 if (cwd_vpath == NULL)
4645 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4646 panel_clean_dir (panel);
4647 dir_list_init (&panel->dir);
4648 return;
4651 panel->cwd_vpath = cwd_vpath;
4652 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4653 show_dir (panel);
4655 if (!dir_list_reload (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4656 &panel->sort_info, &panel->filter))
4657 message (D_ERROR, MSG_ERROR, _("Cannot read directory contents"));
4659 panel->dirty = TRUE;
4660 if (panel->current >= panel->dir.len)
4661 panel_set_current (panel, panel->dir.len - 1);
4663 recalculate_panel_summary (panel);
4666 /* --------------------------------------------------------------------------------------------- */
4667 /* Switches the panel to the mode specified in the format */
4668 /* Setting up both format and status string. Return: 0 - on success; */
4669 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4672 set_panel_formats (WPanel * p)
4674 GSList *form;
4675 char *err = NULL;
4676 int retcode = 0;
4678 form = use_display_format (p, panel_format (p), &err, FALSE);
4680 if (err != NULL)
4682 g_free (err);
4683 retcode = 1;
4685 else
4687 g_slist_free_full (p->format, (GDestroyNotify) format_item_free);
4688 p->format = form;
4691 if (panels_options.show_mini_info)
4693 form = use_display_format (p, mini_status_format (p), &err, TRUE);
4695 if (err != NULL)
4697 g_free (err);
4698 retcode += 2;
4700 else
4702 g_slist_free_full (p->status_format, (GDestroyNotify) format_item_free);
4703 p->status_format = form;
4707 panel_update_cols (WIDGET (p), p->frame_size);
4709 if (retcode)
4710 message (D_ERROR, _("Warning"),
4711 _("User supplied format looks invalid, reverting to default."));
4712 if (retcode & 0x01)
4714 g_free (p->user_format);
4715 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4717 if (retcode & 0x02)
4719 g_free (p->user_status_format[p->list_format]);
4720 p->user_status_format[p->list_format] = g_strdup (DEFAULT_USER_FORMAT);
4723 return retcode;
4726 /* --------------------------------------------------------------------------------------------- */
4728 void
4729 panel_set_filter (WPanel * panel, const file_filter_t * filter)
4731 MC_PTR_FREE (panel->filter.value);
4732 mc_search_free (panel->filter.handler);
4733 panel->filter.handler = NULL;
4735 /* NULL to clear filter */
4736 if (filter != NULL)
4737 panel->filter = *filter;
4739 reread_cmd ();
4742 /* --------------------------------------------------------------------------------------------- */
4744 /* Select current item and readjust the panel */
4745 void
4746 select_item (WPanel * panel)
4748 adjust_top_file (panel);
4750 panel->dirty = TRUE;
4752 execute_hooks (select_file_hook);
4755 /* --------------------------------------------------------------------------------------------- */
4756 /** Clears all files in the panel, used only when one file was marked */
4757 void
4758 unmark_files (WPanel * panel)
4760 if (panel->marked != 0)
4762 int i;
4764 for (i = 0; i < panel->dir.len; i++)
4765 file_mark (panel, i, 0);
4767 panel->dirs_marked = 0;
4768 panel->marked = 0;
4769 panel->total = 0;
4773 /* --------------------------------------------------------------------------------------------- */
4774 /** Recalculate the panels summary information, used e.g. when marked
4775 files might have been removed by an external command */
4777 void
4778 recalculate_panel_summary (WPanel * panel)
4780 int i;
4782 panel->marked = 0;
4783 panel->dirs_marked = 0;
4784 panel->total = 0;
4786 for (i = 0; i < panel->dir.len; i++)
4787 if (panel->dir.list[i].f.marked != 0)
4789 /* do_file_mark will return immediately if newmark == oldmark.
4790 So we have to first unmark it to get panel's summary information
4791 updated. (Norbert) */
4792 panel->dir.list[i].f.marked = 0;
4793 do_file_mark (panel, i, 1);
4797 /* --------------------------------------------------------------------------------------------- */
4798 /** This routine marks a file or a directory */
4800 void
4801 do_file_mark (WPanel * panel, int idx, int mark)
4803 if (panel->dir.list[idx].f.marked == mark)
4804 return;
4806 /* Only '..' can't be marked, '.' isn't visible */
4807 if (DIR_IS_DOTDOT (panel->dir.list[idx].fname->str))
4808 return;
4810 file_mark (panel, idx, mark);
4811 if (panel->dir.list[idx].f.marked != 0)
4813 panel->marked++;
4815 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4817 if (panel->dir.list[idx].f.dir_size_computed != 0)
4818 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4819 panel->dirs_marked++;
4821 else
4822 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4824 set_colors (panel);
4826 else
4828 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4830 if (panel->dir.list[idx].f.dir_size_computed != 0)
4831 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4832 panel->dirs_marked--;
4834 else
4835 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4837 panel->marked--;
4841 /* --------------------------------------------------------------------------------------------- */
4843 * Changes the current directory of the panel.
4844 * Record change in the directory history.
4846 gboolean
4847 panel_do_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4849 gboolean r;
4851 r = panel_do_cd_int (panel, new_dir_vpath, cd_type);
4852 if (r)
4853 directory_history_add (panel, panel->cwd_vpath);
4854 return r;
4857 /* --------------------------------------------------------------------------------------------- */
4859 void
4860 file_mark (WPanel * panel, int lc_index, int val)
4862 if (panel->dir.list[lc_index].f.marked != val)
4864 panel->dir.list[lc_index].f.marked = val;
4865 panel->dirty = TRUE;
4869 /* --------------------------------------------------------------------------------------------- */
4871 void
4872 panel_re_sort (WPanel * panel)
4874 char *filename;
4875 const file_entry_t *fe;
4876 int i;
4878 if (panel == NULL)
4879 return;
4881 fe = panel_current_entry (panel);
4882 filename = g_strndup (fe->fname->str, fe->fname->len);
4883 unselect_item (panel);
4884 dir_list_sort (&panel->dir, panel->sort_field->sort_routine, &panel->sort_info);
4885 panel->current = -1;
4887 for (i = panel->dir.len; i != 0; i--)
4888 if (strcmp (panel->dir.list[i - 1].fname->str, filename) == 0)
4890 panel->current = i - 1;
4891 break;
4894 g_free (filename);
4895 panel->top = panel->current - panel_items (panel) / 2;
4896 select_item (panel);
4897 panel->dirty = TRUE;
4900 /* --------------------------------------------------------------------------------------------- */
4902 void
4903 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4905 if (sort_order == NULL)
4906 return;
4908 panel->sort_field = sort_order;
4910 /* The directory is already sorted, we have to load the unsorted stuff */
4911 if (sort_order->sort_routine == (GCompareFunc) unsorted)
4913 char *current_file;
4914 const GString *fname;
4916 fname = panel_current_entry (panel)->fname;
4917 current_file = g_strndup (fname->str, fname->len);
4918 panel_reload (panel);
4919 panel_set_current_by_name (panel, current_file);
4920 g_free (current_file);
4922 panel_re_sort (panel);
4925 /* --------------------------------------------------------------------------------------------- */
4927 #ifdef HAVE_CHARSET
4930 * Change panel encoding.
4931 * @param panel WPanel object
4934 void
4935 panel_change_encoding (WPanel * panel)
4937 const char *encoding = NULL;
4938 char *errmsg;
4939 int r;
4941 r = select_charset (-1, -1, panel->codepage, FALSE);
4943 if (r == SELECT_CHARSET_CANCEL)
4944 return; /* Cancel */
4946 panel->codepage = r;
4948 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4950 /* No translation */
4951 vfs_path_t *cd_path_vpath;
4953 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4954 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4955 panel_do_cd (panel, cd_path_vpath, cd_parse_command);
4956 show_dir (panel);
4957 vfs_path_free (cd_path_vpath, TRUE);
4958 return;
4961 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4962 if (errmsg != NULL)
4964 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4965 g_free (errmsg);
4966 return;
4969 encoding = get_codepage_id (panel->codepage);
4970 if (encoding != NULL)
4972 vfs_path_change_encoding (panel->cwd_vpath, encoding);
4974 if (!panel_do_cd (panel, panel->cwd_vpath, cd_parse_command))
4975 cd_error_message (vfs_path_as_str (panel->cwd_vpath));
4979 /* --------------------------------------------------------------------------------------------- */
4982 * Remove encode info from last path element.
4985 vfs_path_t *
4986 remove_encoding_from_path (const vfs_path_t * vpath)
4988 vfs_path_t *ret_vpath;
4989 GString *tmp_conv;
4990 int indx;
4992 ret_vpath = vfs_path_new (FALSE);
4994 tmp_conv = g_string_new ("");
4996 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4998 GIConv converter;
4999 vfs_path_element_t *path_element;
5001 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
5003 if (path_element->encoding == NULL)
5005 vfs_path_add_element (ret_vpath, path_element);
5006 continue;
5009 converter = str_crt_conv_to (path_element->encoding);
5010 if (converter == INVALID_CONV)
5012 vfs_path_add_element (ret_vpath, path_element);
5013 continue;
5016 MC_PTR_FREE (path_element->encoding);
5018 str_vfs_convert_from (converter, path_element->path, tmp_conv);
5020 g_free (path_element->path);
5021 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
5023 g_string_set_size (tmp_conv, 0);
5025 str_close_conv (converter);
5026 str_close_conv (path_element->dir.converter);
5027 path_element->dir.converter = INVALID_CONV;
5028 vfs_path_add_element (ret_vpath, path_element);
5030 g_string_free (tmp_conv, TRUE);
5031 return ret_vpath;
5033 #endif /* HAVE_CHARSET */
5035 /* --------------------------------------------------------------------------------------------- */
5038 * This routine reloads the directory in both panels. It tries to
5039 * select current_file in current_panel and other_file in other_panel.
5040 * If current_file == -1 then it automatically sets current_file and
5041 * other_file to the current files in the panels.
5043 * If flags has the UP_ONLY_CURRENT bit toggled on, then it
5044 * will not reload the other panel.
5046 * @param flags for reload panel
5047 * @param current_file name of the current file
5050 void
5051 update_panels (panel_update_flags_t flags, const char *current_file)
5053 WPanel *panel;
5055 /* first, update other panel... */
5056 if ((flags & UP_ONLY_CURRENT) == 0)
5057 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
5058 /* ...then current one */
5059 update_one_panel (get_current_index (), flags, current_file);
5061 if (get_current_type () == view_listing)
5062 panel = PANEL (get_panel_widget (get_current_index ()));
5063 else
5064 panel = PANEL (get_panel_widget (get_other_index ()));
5066 if (!panel->is_panelized)
5067 (void) mc_chdir (panel->cwd_vpath);
5070 /* --------------------------------------------------------------------------------------------- */
5072 gsize
5073 panel_get_num_of_sortable_fields (void)
5075 gsize ret = 0, lc_index;
5077 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5078 if (panel_fields[lc_index].is_user_choice)
5079 ret++;
5080 return ret;
5083 /* --------------------------------------------------------------------------------------------- */
5085 char **
5086 panel_get_sortable_fields (gsize * array_size)
5088 char **ret;
5089 gsize lc_index, i;
5091 lc_index = panel_get_num_of_sortable_fields ();
5093 ret = g_try_new0 (char *, lc_index + 1);
5094 if (ret == NULL)
5095 return NULL;
5097 if (array_size != NULL)
5098 *array_size = lc_index;
5100 lc_index = 0;
5102 for (i = 0; panel_fields[i].id != NULL; i++)
5103 if (panel_fields[i].is_user_choice)
5104 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
5106 return ret;
5109 /* --------------------------------------------------------------------------------------------- */
5111 const panel_field_t *
5112 panel_get_field_by_id (const char *name)
5114 gsize lc_index;
5116 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5117 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
5118 return &panel_fields[lc_index];
5120 return NULL;
5123 /* --------------------------------------------------------------------------------------------- */
5125 const panel_field_t *
5126 panel_get_field_by_title_hotkey (const char *name)
5128 gsize lc_index;
5130 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5131 if (panel_fields[lc_index].title_hotkey != NULL &&
5132 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
5133 return &panel_fields[lc_index];
5135 return NULL;
5138 /* --------------------------------------------------------------------------------------------- */
5140 const panel_field_t *
5141 panel_get_field_by_title (const char *name)
5143 gsize lc_index;
5145 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5147 const char *title;
5149 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
5150 if (strcmp (title, name) == 0)
5151 return &panel_fields[lc_index];
5154 return NULL;
5157 /* --------------------------------------------------------------------------------------------- */
5159 gsize
5160 panel_get_num_of_user_possible_fields (void)
5162 gsize ret = 0, lc_index;
5164 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
5165 if (panel_fields[lc_index].use_in_user_format)
5166 ret++;
5168 return ret;
5171 /* --------------------------------------------------------------------------------------------- */
5173 char **
5174 panel_get_user_possible_fields (gsize * array_size)
5176 char **ret;
5177 gsize lc_index, i;
5179 lc_index = panel_get_num_of_user_possible_fields ();
5181 ret = g_try_new0 (char *, lc_index + 1);
5182 if (ret == NULL)
5183 return NULL;
5185 if (array_size != NULL)
5186 *array_size = lc_index;
5188 lc_index = 0;
5190 for (i = 0; panel_fields[i].id != NULL; i++)
5191 if (panel_fields[i].use_in_user_format)
5192 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
5194 return ret;
5197 /* --------------------------------------------------------------------------------------------- */
5199 void
5200 panel_panelize_cd (void)
5202 WPanel *panel;
5203 int i;
5204 dir_list *list;
5205 panelized_descr_t *pdescr;
5206 dir_list *plist;
5207 gboolean panelized_same;
5209 if (!SELECTED_IS_PANEL)
5210 create_panel (MENU_PANEL_IDX, view_listing);
5212 panel = PANEL (get_panel_widget (MENU_PANEL_IDX));
5214 dir_list_clean (&panel->dir);
5216 if (panel->panelized_descr == NULL)
5217 panel->panelized_descr = panelized_descr_new ();
5219 pdescr = panel->panelized_descr;
5220 plist = &pdescr->list;
5222 if (pdescr->root_vpath == NULL)
5223 panel_panelize_change_root (panel, panel->cwd_vpath);
5225 if (plist->len < 1)
5226 dir_list_init (plist);
5227 else if (plist->len > panel->dir.size)
5228 dir_list_grow (&panel->dir, plist->len - panel->dir.size);
5230 list = &panel->dir;
5231 list->len = plist->len;
5233 panelized_same = vfs_path_equal (pdescr->root_vpath, panel->cwd_vpath);
5235 for (i = 0; i < plist->len; i++)
5237 if (panelized_same || DIR_IS_DOTDOT (plist->list[i].fname->str))
5238 list->list[i].fname = mc_g_string_dup (plist->list[i].fname);
5239 else
5241 vfs_path_t *tmp_vpath;
5243 tmp_vpath =
5244 vfs_path_append_new (pdescr->root_vpath, plist->list[i].fname->str, (char *) NULL);
5245 list->list[i].fname = g_string_new_take (vfs_path_free (tmp_vpath, FALSE));
5247 list->list[i].f.link_to_dir = plist->list[i].f.link_to_dir;
5248 list->list[i].f.stale_link = plist->list[i].f.stale_link;
5249 list->list[i].f.dir_size_computed = plist->list[i].f.dir_size_computed;
5250 list->list[i].f.marked = plist->list[i].f.marked;
5251 list->list[i].st = plist->list[i].st;
5252 list->list[i].name_sort_key = plist->list[i].name_sort_key;
5253 list->list[i].extension_sort_key = plist->list[i].extension_sort_key;
5256 panel->is_panelized = TRUE;
5257 panel_panelize_absolutize_if_needed (panel);
5259 panel_set_current_by_name (panel, NULL);
5262 /* --------------------------------------------------------------------------------------------- */
5265 * Change root directory of panelized content.
5266 * @param panel file panel
5267 * @param new_root new path
5269 void
5270 panel_panelize_change_root (WPanel * panel, const vfs_path_t * new_root)
5272 if (panel->panelized_descr == NULL)
5273 panel->panelized_descr = panelized_descr_new ();
5274 else
5275 vfs_path_free (panel->panelized_descr->root_vpath, TRUE);
5277 panel->panelized_descr->root_vpath = vfs_path_clone (new_root);
5280 /* --------------------------------------------------------------------------------------------- */
5283 * Conditionally switches a panel's directory to "/" (root).
5285 * If a panelized panel's listing contain absolute paths, this function
5286 * sets the panel's directory to "/". Otherwise it does nothing.
5288 * Rationale:
5290 * This makes tokenized strings like "%d/%p" work. This also makes other
5291 * places work where such naive concatenation is done in code (e.g., when
5292 * pressing ctrl+shift+enter, for CK_PutCurrentFullSelected).
5294 * When to call:
5296 * You should always call this function after you populate the listing
5297 * of a panelized panel.
5299 void
5300 panel_panelize_absolutize_if_needed (WPanel * panel)
5302 const dir_list *const list = &panel->dir;
5304 /* Note: We don't support mixing of absolute and relative paths, which is
5305 * why it's ok for us to check only the 1st entry. */
5306 if (list->len > 1 && g_path_is_absolute (list->list[1].fname->str))
5308 vfs_path_t *root;
5310 root = vfs_path_from_str (PATH_SEP_STR);
5311 panel_set_cwd (panel, root);
5312 if (panel == current_panel)
5313 mc_chdir (root);
5314 vfs_path_free (root, TRUE);
5318 /* --------------------------------------------------------------------------------------------- */
5320 void
5321 panel_panelize_save (WPanel * panel)
5323 int i;
5324 dir_list *list = &panel->dir;
5325 dir_list *plist;
5327 panel_panelize_change_root (panel, panel->cwd_vpath);
5329 plist = &panel->panelized_descr->list;
5331 if (plist->len > 0)
5332 dir_list_clean (plist);
5333 if (panel->dir.len == 0)
5334 return;
5336 if (panel->dir.len > plist->size)
5337 dir_list_grow (plist, panel->dir.len - plist->size);
5338 plist->len = panel->dir.len;
5340 for (i = 0; i < panel->dir.len; i++)
5342 plist->list[i].fname = mc_g_string_dup (list->list[i].fname);
5343 plist->list[i].f.link_to_dir = list->list[i].f.link_to_dir;
5344 plist->list[i].f.stale_link = list->list[i].f.stale_link;
5345 plist->list[i].f.dir_size_computed = list->list[i].f.dir_size_computed;
5346 plist->list[i].f.marked = list->list[i].f.marked;
5347 plist->list[i].st = list->list[i].st;
5348 plist->list[i].name_sort_key = list->list[i].name_sort_key;
5349 plist->list[i].extension_sort_key = list->list[i].extension_sort_key;
5353 /* --------------------------------------------------------------------------------------------- */
5355 void
5356 panel_init (void)
5358 panel_sort_up_char = mc_skin_get ("widget-panel", "sort-up-char", "'");
5359 panel_sort_down_char = mc_skin_get ("widget-panel", "sort-down-char", ".");
5360 panel_hiddenfiles_show_char = mc_skin_get ("widget-panel", "hiddenfiles-show-char", ".");
5361 panel_hiddenfiles_hide_char = mc_skin_get ("widget-panel", "hiddenfiles-hide-char", ".");
5362 panel_history_prev_item_char = mc_skin_get ("widget-panel", "history-prev-item-char", "<");
5363 panel_history_next_item_char = mc_skin_get ("widget-panel", "history-next-item-char", ">");
5364 panel_history_show_list_char = mc_skin_get ("widget-panel", "history-show-list-char", "^");
5365 panel_filename_scroll_left_char =
5366 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
5367 panel_filename_scroll_right_char =
5368 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
5370 string_file_name_buffer = g_string_sized_new (MC_MAXFILENAMELEN);
5372 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
5373 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_current_file_to_clip_file",
5374 panel_save_current_file_to_clip_file, NULL, NULL);
5377 /* --------------------------------------------------------------------------------------------- */
5379 void
5380 panel_deinit (void)
5382 g_free (panel_sort_up_char);
5383 g_free (panel_sort_down_char);
5384 g_free (panel_hiddenfiles_show_char);
5385 g_free (panel_hiddenfiles_hide_char);
5386 g_free (panel_history_prev_item_char);
5387 g_free (panel_history_next_item_char);
5388 g_free (panel_history_show_list_char);
5389 g_free (panel_filename_scroll_left_char);
5390 g_free (panel_filename_scroll_right_char);
5391 g_string_free (string_file_name_buffer, TRUE);
5394 /* --------------------------------------------------------------------------------------------- */
5396 gboolean
5397 panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum exact)
5399 gboolean res;
5400 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
5402 if (panel->is_panelized)
5404 size_t new_vpath_len;
5406 new_vpath_len = vfs_path_len (new_dir_vpath);
5407 if (vfs_path_equal_len (new_dir_vpath, panel->panelized_descr->root_vpath, new_vpath_len))
5408 _new_dir_vpath = panel->panelized_descr->root_vpath;
5411 res = panel_do_cd (panel, _new_dir_vpath, exact);
5413 #ifdef HAVE_CHARSET
5414 if (res)
5416 const vfs_path_element_t *path_element;
5418 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
5419 if (path_element->encoding != NULL)
5420 panel->codepage = get_codepage_index (path_element->encoding);
5421 else
5422 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
5424 #endif /* HAVE_CHARSET */
5426 return res;
5429 /* --------------------------------------------------------------------------------------------- */