Update translations from Transifex
[midnight-commander.git] / src / filemanager / panel.c
blob476d21219e8acae1f47895fb704fa577758f4232
1 /*
2 Panel managing.
4 Copyright (C) 1994-2019
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-2016
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 <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/key.h" /* XCTRL and ALT macros */
44 #include "lib/skin.h"
45 #include "lib/strescape.h"
46 #include "lib/mcconfig.h"
47 #include "lib/vfs/vfs.h"
48 #include "lib/unixcompat.h"
49 #include "lib/search.h"
50 #include "lib/timefmt.h" /* file_date() */
51 #include "lib/util.h"
52 #include "lib/widget.h"
53 #ifdef HAVE_CHARSET
54 #include "lib/charsets.h" /* get_codepage_id () */
55 #endif
56 #include "lib/event.h"
58 #include "src/setup.h" /* For loading/saving panel options */
59 #include "src/execute.h"
60 #ifdef HAVE_CHARSET
61 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
62 #endif
63 #include "src/keybind-defaults.h" /* global_keymap_t */
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 "midnight.h"
78 #include "mountlist.h" /* my_statfs */
80 #include "panel.h"
82 /*** global variables ****************************************************************************/
84 /* The hook list for the select file function */
85 hook_t *select_file_hook = NULL;
87 /* *INDENT-OFF* */
88 panelized_panel_t panelized_panel = { {NULL, 0, -1}, NULL };
89 /* *INDENT-ON* */
91 static const char *string_file_name (file_entry_t *, int);
92 static const char *string_file_size (file_entry_t *, int);
93 static const char *string_file_size_brief (file_entry_t *, int);
94 static const char *string_file_type (file_entry_t *, int);
95 static const char *string_file_mtime (file_entry_t *, int);
96 static const char *string_file_atime (file_entry_t *, int);
97 static const char *string_file_ctime (file_entry_t *, int);
98 static const char *string_file_permission (file_entry_t *, int);
99 static const char *string_file_perm_octal (file_entry_t *, int);
100 static const char *string_file_nlinks (file_entry_t *, int);
101 static const char *string_inode (file_entry_t *, int);
102 static const char *string_file_nuid (file_entry_t *, int);
103 static const char *string_file_ngid (file_entry_t *, int);
104 static const char *string_file_owner (file_entry_t *, int);
105 static const char *string_file_group (file_entry_t *, int);
106 static const char *string_marked (file_entry_t *, int);
107 static const char *string_space (file_entry_t *, int);
108 static const char *string_dot (file_entry_t *, int);
110 mc_fhl_t *mc_filehighlight = NULL;
112 /*** file scope macro definitions ****************************************************************/
114 #define NORMAL 0
115 #define SELECTED 1
116 #define MARKED 2
117 #define MARKED_SELECTED 3
118 #define STATUS 5
120 /*** file scope type declarations ****************************************************************/
122 typedef enum
124 MARK_DONT_MOVE = 0,
125 MARK_DOWN = 1,
126 MARK_FORCE_DOWN = 2,
127 MARK_FORCE_UP = 3
128 } mark_act_t;
131 * This describes a format item. The parse_display_format routine parses
132 * the user specified format and creates a linked list of format_item_t structures.
134 typedef struct format_item_t
136 int requested_field_len;
137 int field_len;
138 align_crt_t just_mode;
139 gboolean expand;
140 const char *(*string_fn) (file_entry_t *, int len);
141 char *title;
142 const char *id;
143 } format_item_t;
145 /* File name scroll states */
146 typedef enum
148 FILENAME_NOSCROLL = 1,
149 FILENAME_SCROLL_LEFT = 2,
150 FILENAME_SCROLL_RIGHT = 4
151 } filename_scroll_flag_t;
153 /*** file scope variables ************************************************************************/
155 /* *INDENT-OFF* */
156 static panel_field_t panel_fields[] = {
158 "unsorted", 12, TRUE, J_LEFT_FIT,
159 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
160 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
161 N_("sort|u"),
162 N_("&Unsorted"), TRUE, FALSE,
163 string_file_name,
164 (GCompareFunc) unsorted
168 "name", 12, TRUE, J_LEFT_FIT,
169 /* TRANSLATORS: one single character to represent 'name' sort mode */
170 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
171 N_("sort|n"),
172 N_("&Name"), TRUE, TRUE,
173 string_file_name,
174 (GCompareFunc) sort_name
178 "version", 12, TRUE, J_LEFT_FIT,
179 /* TRANSLATORS: one single character to represent 'version' sort mode */
180 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
181 N_("sort|v"),
182 N_("&Version"), TRUE, FALSE,
183 string_file_name,
184 (GCompareFunc) sort_vers
188 "extension", 12, TRUE, J_LEFT_FIT,
189 /* TRANSLATORS: one single character to represent 'extension' sort mode */
190 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
191 N_("sort|e"),
192 N_("E&xtension"), TRUE, FALSE,
193 string_file_name, /* TODO: string_file_ext */
194 (GCompareFunc) sort_ext
198 "size", 7, FALSE, J_RIGHT,
199 /* TRANSLATORS: one single character to represent 'size' sort mode */
200 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
201 N_("sort|s"),
202 N_("&Size"), TRUE, TRUE,
203 string_file_size,
204 (GCompareFunc) sort_size
208 "bsize", 7, FALSE, J_RIGHT,
210 N_("Block Size"), FALSE, FALSE,
211 string_file_size_brief,
212 (GCompareFunc) sort_size
216 "type", 1, FALSE, J_LEFT,
218 "", FALSE, TRUE,
219 string_file_type,
220 NULL
224 "mtime", 12, FALSE, J_RIGHT,
225 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
226 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
227 N_("sort|m"),
228 N_("&Modify time"), TRUE, TRUE,
229 string_file_mtime,
230 (GCompareFunc) sort_time
234 "atime", 12, FALSE, J_RIGHT,
235 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
236 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
237 N_("sort|a"),
238 N_("&Access time"), TRUE, TRUE,
239 string_file_atime,
240 (GCompareFunc) sort_atime
244 "ctime", 12, FALSE, J_RIGHT,
245 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
246 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
247 N_("sort|h"),
248 N_("C&hange time"), TRUE, TRUE,
249 string_file_ctime,
250 (GCompareFunc) sort_ctime
254 "perm", 10, FALSE, J_LEFT,
256 N_("Permission"), FALSE, TRUE,
257 string_file_permission,
258 NULL
262 "mode", 6, FALSE, J_RIGHT,
264 N_("Perm"), FALSE, TRUE,
265 string_file_perm_octal,
266 NULL
270 "nlink", 2, FALSE, J_RIGHT,
272 N_("Nl"), FALSE, TRUE,
273 string_file_nlinks, NULL
277 "inode", 5, FALSE, J_RIGHT,
278 /* TRANSLATORS: one single character to represent 'inode' sort mode */
279 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
280 N_("sort|i"),
281 N_("&Inode"), TRUE, TRUE,
282 string_inode,
283 (GCompareFunc) sort_inode
287 "nuid", 5, FALSE, J_RIGHT,
289 N_("UID"), FALSE, FALSE,
290 string_file_nuid,
291 NULL
295 "ngid", 5, FALSE, J_RIGHT,
297 N_("GID"), FALSE, FALSE,
298 string_file_ngid,
299 NULL
303 "owner", 8, FALSE, J_LEFT_FIT,
305 N_("Owner"), FALSE, TRUE,
306 string_file_owner,
307 NULL
311 "group", 8, FALSE, J_LEFT_FIT,
313 N_("Group"), FALSE, TRUE,
314 string_file_group,
315 NULL
319 "mark", 1, FALSE, J_RIGHT,
321 " ", FALSE, TRUE,
322 string_marked,
323 NULL
327 "|", 1, FALSE, J_RIGHT,
329 " ", FALSE, TRUE,
330 NULL,
331 NULL
335 "space", 1, FALSE, J_RIGHT,
337 " ", FALSE, TRUE,
338 string_space,
339 NULL
343 "dot", 1, FALSE, J_RIGHT,
345 " ", FALSE, FALSE,
346 string_dot,
347 NULL
351 NULL, 0, FALSE, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
354 /* *INDENT-ON* */
356 static char *panel_sort_up_sign = NULL;
357 static char *panel_sort_down_sign = NULL;
359 static char *panel_hiddenfiles_sign_show = NULL;
360 static char *panel_hiddenfiles_sign_hide = NULL;
361 static char *panel_history_prev_item_sign = NULL;
362 static char *panel_history_next_item_sign = NULL;
363 static char *panel_history_show_list_sign = NULL;
364 static char *panel_filename_scroll_left_char = NULL;
365 static char *panel_filename_scroll_right_char = NULL;
367 /* Panel that selection started */
368 static WPanel *mouse_mark_panel = NULL;
370 static int mouse_marking = 0;
371 static int state_mark = 0;
373 /* --------------------------------------------------------------------------------------------- */
374 /*** file scope functions ************************************************************************/
375 /* --------------------------------------------------------------------------------------------- */
377 static void
378 set_colors (const WPanel * panel)
380 (void) panel;
382 tty_set_normal_attrs ();
383 tty_setcolor (NORMAL_COLOR);
386 /* --------------------------------------------------------------------------------------------- */
387 /** Delete format_item_t object */
389 static void
390 format_item_free (format_item_t * format)
392 g_free (format->title);
393 g_free (format);
396 /* --------------------------------------------------------------------------------------------- */
397 /** Extract the number of available lines in a panel */
399 static int
400 panel_lines (const WPanel * p)
402 /* 3 lines are: top frame, column header, botton frame */
403 return (CONST_WIDGET (p)->lines - 3 - (panels_options.show_mini_info ? 2 : 0));
406 /* --------------------------------------------------------------------------------------------- */
407 /** This code relies on the default justification!!! */
409 static void
410 add_permission_string (const char *dest, int width, file_entry_t * fe, int attr, int color,
411 gboolean is_octal)
413 int i, r, l;
415 l = get_user_permissions (&fe->st);
417 if (is_octal)
419 /* Place of the access bit in octal mode */
420 l = width + l - 3;
421 r = l + 1;
423 else
425 /* The same to the triplet in string mode */
426 l = l * 3 + 1;
427 r = l + 3;
430 for (i = 0; i < width; i++)
432 if (i >= l && i < r)
434 if (attr == SELECTED || attr == MARKED_SELECTED)
435 tty_setcolor (MARKED_SELECTED_COLOR);
436 else
437 tty_setcolor (MARKED_COLOR);
439 else if (color >= 0)
440 tty_setcolor (color);
441 else
442 tty_lowlevel_setcolor (-color);
444 tty_print_char (dest[i]);
448 /* --------------------------------------------------------------------------------------------- */
449 /** String representations of various file attributes name */
451 static const char *
452 string_file_name (file_entry_t * fe, int len)
454 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
456 (void) len;
458 g_strlcpy (buffer, fe->fname, sizeof (buffer));
459 return buffer;
462 /* --------------------------------------------------------------------------------------------- */
464 static unsigned int
465 ilog10 (dev_t n)
467 unsigned int digits = 0;
471 digits++;
472 n /= 10;
474 while (n != 0);
476 return digits;
479 /* --------------------------------------------------------------------------------------------- */
481 static void
482 format_device_number (char *buf, size_t bufsize, dev_t dev)
484 dev_t major_dev, minor_dev;
485 unsigned int major_digits, minor_digits;
487 major_dev = major (dev);
488 major_digits = ilog10 (major_dev);
490 minor_dev = minor (dev);
491 minor_digits = ilog10 (minor_dev);
493 g_assert (bufsize >= 1);
495 if (major_digits + 1 + minor_digits + 1 <= bufsize)
496 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
497 else
498 g_strlcpy (buf, _("[dev]"), bufsize);
501 /* --------------------------------------------------------------------------------------------- */
502 /** size */
504 static const char *
505 string_file_size (file_entry_t * fe, int len)
507 static char buffer[BUF_TINY];
509 /* Don't ever show size of ".." since we don't calculate it */
510 if (DIR_IS_DOTDOT (fe->fname))
511 return _("UP--DIR");
513 #ifdef HAVE_STRUCT_STAT_ST_RDEV
514 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
515 format_device_number (buffer, len + 1, fe->st.st_rdev);
516 else
517 #endif
518 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
520 return buffer;
523 /* --------------------------------------------------------------------------------------------- */
524 /** bsize */
526 static const char *
527 string_file_size_brief (file_entry_t * fe, int len)
529 if (S_ISLNK (fe->st.st_mode) && !link_isdir (fe))
530 return _("SYMLINK");
532 if ((S_ISDIR (fe->st.st_mode) || link_isdir (fe)) && !DIR_IS_DOTDOT (fe->fname))
533 return _("SUB-DIR");
535 return string_file_size (fe, len);
538 /* --------------------------------------------------------------------------------------------- */
539 /** This functions return a string representation of a file entry type */
541 static const char *
542 string_file_type (file_entry_t * fe, int len)
544 static char buffer[2];
546 (void) len;
548 if (S_ISDIR (fe->st.st_mode))
549 buffer[0] = PATH_SEP;
550 else if (S_ISLNK (fe->st.st_mode))
552 if (link_isdir (fe))
553 buffer[0] = '~';
554 else if (fe->f.stale_link)
555 buffer[0] = '!';
556 else
557 buffer[0] = '@';
559 else if (S_ISCHR (fe->st.st_mode))
560 buffer[0] = '-';
561 else if (S_ISSOCK (fe->st.st_mode))
562 buffer[0] = '=';
563 else if (S_ISDOOR (fe->st.st_mode))
564 buffer[0] = '>';
565 else if (S_ISBLK (fe->st.st_mode))
566 buffer[0] = '+';
567 else if (S_ISFIFO (fe->st.st_mode))
568 buffer[0] = '|';
569 else if (S_ISNAM (fe->st.st_mode))
570 buffer[0] = '#';
571 else if (!S_ISREG (fe->st.st_mode))
572 buffer[0] = '?'; /* non-regular of unknown kind */
573 else if (is_exe (fe->st.st_mode))
574 buffer[0] = '*';
575 else
576 buffer[0] = ' ';
577 buffer[1] = '\0';
578 return buffer;
581 /* --------------------------------------------------------------------------------------------- */
582 /** mtime */
584 static const char *
585 string_file_mtime (file_entry_t * fe, int len)
587 (void) len;
589 return file_date (fe->st.st_mtime);
592 /* --------------------------------------------------------------------------------------------- */
593 /** atime */
595 static const char *
596 string_file_atime (file_entry_t * fe, int len)
598 (void) len;
600 return file_date (fe->st.st_atime);
603 /* --------------------------------------------------------------------------------------------- */
604 /** ctime */
606 static const char *
607 string_file_ctime (file_entry_t * fe, int len)
609 (void) len;
611 return file_date (fe->st.st_ctime);
614 /* --------------------------------------------------------------------------------------------- */
615 /** perm */
617 static const char *
618 string_file_permission (file_entry_t * fe, int len)
620 (void) len;
622 return string_perm (fe->st.st_mode);
625 /* --------------------------------------------------------------------------------------------- */
626 /** mode */
628 static const char *
629 string_file_perm_octal (file_entry_t * fe, int len)
631 static char buffer[10];
633 (void) len;
635 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
636 return buffer;
639 /* --------------------------------------------------------------------------------------------- */
640 /** nlink */
642 static const char *
643 string_file_nlinks (file_entry_t * fe, int len)
645 static char buffer[BUF_TINY];
647 (void) len;
649 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
650 return buffer;
653 /* --------------------------------------------------------------------------------------------- */
654 /** inode */
656 static const char *
657 string_inode (file_entry_t * fe, int len)
659 static char buffer[10];
661 (void) len;
663 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
664 return buffer;
667 /* --------------------------------------------------------------------------------------------- */
668 /** nuid */
670 static const char *
671 string_file_nuid (file_entry_t * fe, int len)
673 static char buffer[10];
675 (void) len;
677 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
678 return buffer;
681 /* --------------------------------------------------------------------------------------------- */
682 /** ngid */
684 static const char *
685 string_file_ngid (file_entry_t * fe, int len)
687 static char buffer[10];
689 (void) len;
691 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
692 return buffer;
695 /* --------------------------------------------------------------------------------------------- */
696 /** owner */
698 static const char *
699 string_file_owner (file_entry_t * fe, int len)
701 (void) len;
703 return get_owner (fe->st.st_uid);
706 /* --------------------------------------------------------------------------------------------- */
707 /** group */
709 static const char *
710 string_file_group (file_entry_t * fe, int len)
712 (void) len;
714 return get_group (fe->st.st_gid);
717 /* --------------------------------------------------------------------------------------------- */
718 /** mark */
720 static const char *
721 string_marked (file_entry_t * fe, int len)
723 (void) len;
725 return fe->f.marked ? "*" : " ";
728 /* --------------------------------------------------------------------------------------------- */
729 /** space */
731 static const char *
732 string_space (file_entry_t * fe, int len)
734 (void) fe;
735 (void) len;
737 return " ";
740 /* --------------------------------------------------------------------------------------------- */
741 /** dot */
743 static const char *
744 string_dot (file_entry_t * fe, int len)
746 (void) fe;
747 (void) len;
749 return ".";
752 /* --------------------------------------------------------------------------------------------- */
754 static int
755 file_compute_color (int attr, file_entry_t * fe)
757 switch (attr)
759 case SELECTED:
760 return (SELECTED_COLOR);
761 case MARKED:
762 return (MARKED_COLOR);
763 case MARKED_SELECTED:
764 return (MARKED_SELECTED_COLOR);
765 case STATUS:
766 return (NORMAL_COLOR);
767 case NORMAL:
768 default:
769 if (!panels_options.filetype_mode)
770 return (NORMAL_COLOR);
773 return mc_fhl_get_color (mc_filehighlight, fe);
776 /* --------------------------------------------------------------------------------------------- */
777 /** Returns the number of items in the given panel */
779 static int
780 panel_items (const WPanel * p)
782 return panel_lines (p) * p->list_cols;
785 /* --------------------------------------------------------------------------------------------- */
786 /** Formats the file number file_index of panel in the buffer dest */
788 static filename_scroll_flag_t
789 format_file (WPanel * panel, int file_index, int width, int attr, gboolean isstatus,
790 int *field_length)
792 int color = NORMAL_COLOR;
793 int length = 0;
794 GSList *format, *home;
795 file_entry_t *fe = NULL;
796 filename_scroll_flag_t res = FILENAME_NOSCROLL;
798 *field_length = 0;
800 if (file_index < panel->dir.len)
802 fe = &panel->dir.list[file_index];
803 color = file_compute_color (attr, fe);
806 home = isstatus ? panel->status_format : panel->format;
808 for (format = home; format != NULL && length != width; format = g_slist_next (format))
810 format_item_t *fi = (format_item_t *) format->data;
812 if (fi->string_fn != NULL)
814 const char *txt = " ";
815 int len, perm = 0;
816 const char *prepared_text;
817 int name_offset = 0;
819 if (fe != NULL)
820 txt = fi->string_fn (fe, fi->field_len);
822 len = fi->field_len;
823 if (len + length > width)
824 len = width - length;
825 if (len <= 0)
826 break;
828 if (!isstatus && panel->content_shift > -1 && strcmp (fi->id, "name") == 0)
830 int str_len;
831 int i;
833 *field_length = len + 1;
835 str_len = str_length (txt);
836 i = MAX (0, str_len - len);
837 panel->max_shift = MAX (panel->max_shift, i);
838 i = MIN (panel->content_shift, i);
840 if (i > -1)
842 name_offset = str_offset_to_pos (txt, i);
843 if (str_len > len)
845 res = FILENAME_SCROLL_LEFT;
846 if (str_length (txt + name_offset) > len)
847 res |= FILENAME_SCROLL_RIGHT;
852 if (panels_options.permission_mode)
854 if (strcmp (fi->id, "perm") == 0)
855 perm = 1;
856 else if (strcmp (fi->id, "mode") == 0)
857 perm = 2;
860 if (color >= 0)
861 tty_setcolor (color);
862 else
863 tty_lowlevel_setcolor (-color);
865 if (!isstatus && panel->content_shift > -1)
866 prepared_text = str_fit_to_term (txt + name_offset, len, HIDE_FIT (fi->just_mode));
867 else
868 prepared_text = str_fit_to_term (txt, len, fi->just_mode);
870 if (perm != 0 && fe != NULL)
871 add_permission_string (prepared_text, fi->field_len, fe, attr, color, perm != 1);
872 else
873 tty_print_string (prepared_text);
875 length += len;
877 else
879 if (attr == SELECTED || attr == MARKED_SELECTED)
880 tty_setcolor (SELECTED_COLOR);
881 else
882 tty_setcolor (NORMAL_COLOR);
883 tty_print_one_vline (TRUE);
884 length++;
888 if (length < width)
890 int y, x;
892 tty_getyx (&y, &x);
893 tty_draw_hline (y, x, ' ', width - length);
896 return res;
899 /* --------------------------------------------------------------------------------------------- */
901 static void
902 repaint_file (WPanel * panel, int file_index, gboolean mv, int attr, gboolean isstatus)
904 Widget *w = WIDGET (panel);
906 int nth_column = 0;
907 int width;
908 int offset = 0;
909 filename_scroll_flag_t ret_frm;
910 int ypos = 0;
911 gboolean panel_is_split;
912 int fln = 0;
914 panel_is_split = !isstatus && panel->list_cols > 1;
915 width = w->cols - 2;
917 if (panel_is_split)
919 nth_column = (file_index - panel->top_file) / panel_lines (panel);
920 width /= panel->list_cols;
922 offset = width * nth_column;
924 if (nth_column + 1 >= panel->list_cols)
925 width = w->cols - offset - 2;
928 /* Nothing to paint */
929 if (width <= 0)
930 return;
932 if (mv)
934 ypos = file_index - panel->top_file;
936 if (panel_is_split)
937 ypos %= panel_lines (panel);
939 ypos += 2; /* top frame and header */
940 widget_move (w, ypos, offset + 1);
943 ret_frm = format_file (panel, file_index, width, attr, isstatus, &fln);
945 if (panel_is_split && nth_column + 1 < panel->list_cols)
947 tty_setcolor (NORMAL_COLOR);
948 tty_print_one_vline (TRUE);
951 if (ret_frm != FILENAME_NOSCROLL && mv)
953 if (!panel_is_split && fln > 0)
955 if (panel->list_format != list_long)
956 width = fln;
957 else
959 offset = width - fln + 1;
960 width = fln - 1;
964 widget_move (w, ypos, offset);
965 tty_setcolor (NORMAL_COLOR);
966 tty_print_string (panel_filename_scroll_left_char);
968 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
970 offset += width;
971 if (nth_column + 1 >= panel->list_cols)
972 offset++;
974 widget_move (w, ypos, offset);
975 tty_setcolor (NORMAL_COLOR);
976 tty_print_string (panel_filename_scroll_right_char);
981 /* --------------------------------------------------------------------------------------------- */
983 static void
984 display_mini_info (WPanel * panel)
986 Widget *w = WIDGET (panel);
988 if (!panels_options.show_mini_info)
989 return;
991 widget_move (w, panel_lines (panel) + 3, 1);
993 if (panel->searching)
995 tty_setcolor (INPUT_COLOR);
996 tty_print_char ('/');
997 tty_print_string (str_fit_to_term (panel->search_buffer, w->cols - 3, J_LEFT));
998 return;
1001 /* Status resolves links and show them */
1002 set_colors (panel);
1004 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
1006 char link_target[MC_MAXPATHLEN];
1007 vfs_path_t *lc_link_vpath;
1008 int len;
1010 lc_link_vpath =
1011 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname,
1012 (char *) NULL);
1013 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
1014 vfs_path_free (lc_link_vpath);
1015 if (len > 0)
1017 link_target[len] = 0;
1018 tty_print_string ("-> ");
1019 tty_print_string (str_fit_to_term (link_target, w->cols - 5, J_LEFT_FIT));
1021 else
1022 tty_print_string (str_fit_to_term (_("<readlink failed>"), w->cols - 2, J_LEFT));
1024 else if (DIR_IS_DOTDOT (panel->dir.list[panel->selected].fname))
1026 /* FIXME:
1027 * while loading directory (dir_list_load() and dir_list_reload()),
1028 * the actual stat info about ".." directory isn't got;
1029 * so just don't display incorrect info about ".." directory */
1030 tty_print_string (str_fit_to_term (_("UP--DIR"), w->cols - 2, J_LEFT));
1032 else
1033 /* Default behavior */
1034 repaint_file (panel, panel->selected, FALSE, STATUS, TRUE);
1037 /* --------------------------------------------------------------------------------------------- */
1039 static void
1040 paint_dir (WPanel * panel)
1042 int i;
1043 int items; /* Number of items */
1045 items = panel_items (panel);
1046 /* reset max len of filename because we have the new max length for the new file list */
1047 panel->max_shift = -1;
1049 for (i = 0; i < items; i++)
1051 int color = 0; /* Color value of the line */
1053 if (i + panel->top_file < panel->dir.len)
1055 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1056 color += (panel->selected == i + panel->top_file && panel->active);
1059 repaint_file (panel, i + panel->top_file, TRUE, color, FALSE);
1062 tty_set_normal_attrs ();
1065 /* --------------------------------------------------------------------------------------------- */
1067 static void
1068 display_total_marked_size (const WPanel * panel, int y, int x, gboolean size_only)
1070 const Widget *w = CONST_WIDGET (panel);
1072 char buffer[BUF_SMALL], b_bytes[BUF_SMALL];
1073 const char *buf;
1074 int cols;
1076 if (panel->marked <= 0)
1077 return;
1079 buf = size_only ? b_bytes : buffer;
1080 cols = w->cols - 2;
1082 g_strlcpy (b_bytes, size_trunc_sep (panel->total, panels_options.kilobyte_si),
1083 sizeof (b_bytes));
1085 if (!size_only)
1086 g_snprintf (buffer, sizeof (buffer),
1087 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1088 b_bytes, panel->marked);
1090 /* don't forget spaces around buffer content */
1091 buf = str_trunc (buf, cols - 4);
1093 if (x < 0)
1094 /* center in panel */
1095 x = (w->cols - str_term_width1 (buf)) / 2 - 1;
1098 * y == panel_lines (panel) + 2 for mini_info_separator
1099 * y == w->lines - 1 for panel bottom frame
1101 widget_move (w, y, x);
1102 tty_setcolor (MARKED_COLOR);
1103 tty_printf (" %s ", buf);
1106 /* --------------------------------------------------------------------------------------------- */
1108 static void
1109 mini_info_separator (const WPanel * panel)
1111 if (panels_options.show_mini_info)
1113 const Widget *w = CONST_WIDGET (panel);
1114 int y;
1116 y = panel_lines (panel) + 2;
1118 tty_setcolor (NORMAL_COLOR);
1119 tty_draw_hline (w->y + y, w->x + 1, ACS_HLINE, w->cols - 2);
1120 /* Status displays total marked size.
1121 * Centered in panel, full format. */
1122 display_total_marked_size (panel, y, -1, FALSE);
1126 /* --------------------------------------------------------------------------------------------- */
1128 static void
1129 show_free_space (const WPanel * panel)
1131 /* Used to figure out how many free space we have */
1132 static struct my_statfs myfs_stats;
1133 /* Old current working directory for displaying free space */
1134 static char *old_cwd = NULL;
1136 /* Don't try to stat non-local fs */
1137 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1138 return;
1140 if (old_cwd == NULL || strcmp (old_cwd, vfs_path_as_str (panel->cwd_vpath)) != 0)
1142 char rpath[PATH_MAX];
1144 init_my_statfs ();
1145 g_free (old_cwd);
1146 old_cwd = g_strdup (vfs_path_as_str (panel->cwd_vpath));
1148 if (mc_realpath (old_cwd, rpath) == NULL)
1149 return;
1151 my_statfs (&myfs_stats, rpath);
1154 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1156 const Widget *w = CONST_WIDGET (panel);
1157 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1159 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1160 panels_options.kilobyte_si);
1161 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1162 panels_options.kilobyte_si);
1163 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1164 myfs_stats.total == 0 ? 0 :
1165 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1166 widget_move (w, w->lines - 1, w->cols - 2 - (int) strlen (tmp));
1167 tty_setcolor (NORMAL_COLOR);
1168 tty_print_string (tmp);
1172 /* --------------------------------------------------------------------------------------------- */
1174 * Prepare path string for showing in panel's header.
1175 * Passwords will removed, also home dir will replaced by ~
1177 * @param panel WPanel object
1179 * @return newly allocated string.
1182 static char *
1183 panel_correct_path_to_show (const WPanel * panel)
1185 vfs_path_t *last_vpath;
1186 const vfs_path_element_t *path_element;
1187 char *return_path;
1188 int elements_count;
1190 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1192 /* get last path element */
1193 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1195 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1196 strcmp (path_element->class->name, "extfs") == 0 ||
1197 strcmp (path_element->class->name, "tarfs") == 0))
1199 const char *archive_name;
1200 const vfs_path_element_t *prev_path_element;
1202 /* get previous path element for catching archive name */
1203 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1204 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1205 if (archive_name != NULL)
1206 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1207 else
1209 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1210 last_vpath->relative = TRUE;
1213 else
1215 last_vpath = vfs_path_new ();
1216 last_vpath->relative = TRUE;
1219 vfs_path_add_element (last_vpath, path_element);
1220 return_path =
1221 vfs_path_to_str_flags (last_vpath, 0,
1222 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1223 vfs_path_free (last_vpath);
1225 return return_path;
1228 /* --------------------------------------------------------------------------------------------- */
1230 * Get Current path element encoding
1232 * @param panel WPanel object
1234 * @return newly allocated string or NULL if path charset is same as system charset
1237 #ifdef HAVE_CHARSET
1238 static char *
1239 panel_get_encoding_info_str (const WPanel * panel)
1241 char *ret_str = NULL;
1242 const vfs_path_element_t *path_element;
1244 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1245 if (path_element->encoding != NULL)
1246 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1248 return ret_str;
1250 #endif
1252 /* --------------------------------------------------------------------------------------------- */
1254 static void
1255 show_dir (const WPanel * panel)
1257 const Widget *w = CONST_WIDGET (panel);
1259 gchar *tmp;
1261 set_colors (panel);
1262 tty_draw_box (w->y, w->x, w->lines, w->cols, FALSE);
1264 if (panels_options.show_mini_info)
1266 int y;
1268 y = panel_lines (panel) + 2;
1270 widget_move (w, y, 0);
1271 tty_print_alt_char (ACS_LTEE, FALSE);
1272 widget_move (w, y, w->cols - 1);
1273 tty_print_alt_char (ACS_RTEE, FALSE);
1276 widget_move (w, 0, 1);
1277 tty_print_string (panel_history_prev_item_sign);
1279 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1280 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1281 panel_history_next_item_sign);
1283 widget_move (w, 0, w->cols - 6);
1284 tty_print_string (tmp);
1286 g_free (tmp);
1288 widget_move (w, 0, 3);
1290 if (panel->is_panelized)
1291 tty_printf (" %s ", _("Panelize"));
1292 #ifdef HAVE_CHARSET
1293 else
1295 tmp = panel_get_encoding_info_str (panel);
1296 if (tmp != NULL)
1298 tty_printf ("%s", tmp);
1299 widget_move (w, 0, 3 + strlen (tmp));
1300 g_free (tmp);
1303 #endif
1305 if (panel->active)
1306 tty_setcolor (REVERSE_COLOR);
1308 tmp = panel_correct_path_to_show (panel);
1309 tty_printf (" %s ", str_term_trim (tmp, MIN (MAX (w->cols - 12, 0), w->cols)));
1310 g_free (tmp);
1312 if (!panels_options.show_mini_info)
1314 if (panel->marked == 0)
1316 /* Show size of curret file in the bottom of panel */
1317 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1319 char buffer[BUF_SMALL];
1321 g_snprintf (buffer, sizeof (buffer), " %s ",
1322 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1323 panels_options.kilobyte_si));
1324 tty_setcolor (NORMAL_COLOR);
1325 widget_move (w, w->lines - 1, 4);
1326 tty_print_string (buffer);
1329 else
1331 /* Show total size of marked files
1332 * In the bottom of panel, display size only. */
1333 display_total_marked_size (panel, w->lines - 1, 2, TRUE);
1337 show_free_space (panel);
1339 if (panel->active)
1340 tty_set_normal_attrs ();
1343 /* --------------------------------------------------------------------------------------------- */
1345 static void
1346 adjust_top_file (WPanel * panel)
1348 int items;
1350 /* Update panel->selected to avoid out of range in panel->dir.list[panel->selected]
1351 * when panel is redrawing when directory is reloading, for example in path:
1352 * dir_list_reload() -> mc_refresh() -> dialog_change_screen_size() ->
1353 * midnight_callback (MSG_RESIZE) -> setup_panels() -> panel_callback(MSG_DRAW) ->
1354 * display_mini_info()
1356 panel->selected = CLAMP (panel->selected, 0, panel->dir.len - 1);
1358 items = panel_items (panel);
1360 if (panel->dir.len <= items)
1362 /* If all files fit, show them all. */
1363 panel->top_file = 0;
1365 else
1367 int i;
1369 /* top_file has to be in the range [selected-items+1, selected] so that
1370 the selected file is visible.
1371 top_file should be in the range [0, count-items] so that there's
1372 no empty space wasted.
1373 Within these ranges, adjust it by as little as possible. */
1375 if (panel->top_file < 0)
1376 panel->top_file = 0;
1378 i = panel->selected - items + 1;
1379 if (panel->top_file < i)
1380 panel->top_file = i;
1382 i = panel->dir.len - items;
1383 if (panel->top_file > i)
1384 panel->top_file = i;
1386 if (panel->top_file > panel->selected)
1387 panel->top_file = panel->selected;
1391 /* --------------------------------------------------------------------------------------------- */
1392 /** add "#enc:encodning" to end of path */
1393 /* if path end width a previous #enc:, only encoding is changed no additional
1394 * #enc: is appended
1395 * retun new string
1398 static char *
1399 panel_save_name (WPanel * panel)
1401 /* If the program is shuting down */
1402 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1403 return g_strdup (panel->panel_name);
1405 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1408 /* --------------------------------------------------------------------------------------------- */
1410 static void
1411 directory_history_add (WPanel * panel, const vfs_path_t * vpath)
1413 char *tmp;
1415 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1416 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1417 panel->dir_history_current = panel->dir_history;
1420 /* --------------------------------------------------------------------------------------------- */
1422 /* "history_load" event handler */
1423 static gboolean
1424 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1425 gpointer init_data, gpointer data)
1427 WPanel *p = PANEL (init_data);
1428 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1430 (void) event_group_name;
1431 (void) event_name;
1433 if (ev->receiver == NULL || ev->receiver == WIDGET (p))
1435 if (ev->cfg != NULL)
1436 p->dir_history = history_load (ev->cfg, p->hist_name);
1437 else
1438 p->dir_history = history_get (p->hist_name);
1440 directory_history_add (p, p->cwd_vpath);
1443 return TRUE;
1446 /* --------------------------------------------------------------------------------------------- */
1448 /* "history_save" event handler */
1449 static gboolean
1450 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1451 gpointer init_data, gpointer data)
1453 WPanel *p = PANEL (init_data);
1455 (void) event_group_name;
1456 (void) event_name;
1458 if (p->dir_history != NULL)
1460 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1462 history_save (ev->cfg, p->hist_name, p->dir_history);
1465 return TRUE;
1468 /* --------------------------------------------------------------------------------------------- */
1470 static void
1471 panel_destroy (WPanel * p)
1473 size_t i;
1475 if (panels_options.auto_save_setup)
1477 char *name;
1479 name = panel_save_name (p);
1480 panel_save_setup (p, name);
1481 g_free (name);
1484 panel_clean_dir (p);
1486 /* clean history */
1487 if (p->dir_history != NULL)
1489 /* directory history is already saved before this moment */
1490 p->dir_history = g_list_first (p->dir_history);
1491 g_list_free_full (p->dir_history, g_free);
1493 g_free (p->hist_name);
1495 g_slist_free_full (p->format, (GDestroyNotify) format_item_free);
1496 g_slist_free_full (p->status_format, (GDestroyNotify) format_item_free);
1498 g_free (p->user_format);
1499 for (i = 0; i < LIST_FORMATS; i++)
1500 g_free (p->user_status_format[i]);
1502 g_free (p->dir.list);
1503 g_free (p->panel_name);
1505 vfs_path_free (p->lwd_vpath);
1506 vfs_path_free (p->cwd_vpath);
1509 /* --------------------------------------------------------------------------------------------- */
1511 static void
1512 panel_paint_sort_info (const WPanel * panel)
1514 if (*panel->sort_field->hotkey != '\0')
1516 const char *sort_sign =
1517 panel->sort_info.reverse ? panel_sort_up_sign : panel_sort_down_sign;
1518 char *str;
1520 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_field->hotkey));
1521 widget_move (panel, 1, 1);
1522 tty_print_string (str);
1523 g_free (str);
1527 /* --------------------------------------------------------------------------------------------- */
1529 static const char *
1530 panel_get_title_without_hotkey (const char *title)
1532 static char translated_title[BUF_TINY];
1534 if (title == NULL || title[0] == '\0')
1535 translated_title[0] = '\0';
1536 else
1538 char *hkey;
1540 g_snprintf (translated_title, sizeof (translated_title), "%s", _(title));
1542 hkey = strchr (translated_title, '&');
1543 if (hkey != NULL && hkey[1] != '\0')
1544 memmove (hkey, hkey + 1, strlen (hkey));
1547 return translated_title;
1550 /* --------------------------------------------------------------------------------------------- */
1552 static void
1553 panel_print_header (const WPanel * panel)
1555 const Widget *w = CONST_WIDGET (panel);
1557 int y, x;
1558 int i;
1559 GString *format_txt;
1561 widget_move (w, 1, 1);
1562 tty_getyx (&y, &x);
1563 tty_setcolor (NORMAL_COLOR);
1564 tty_draw_hline (y, x, ' ', w->cols - 2);
1566 format_txt = g_string_new ("");
1568 for (i = 0; i < panel->list_cols; i++)
1570 GSList *format;
1572 for (format = panel->format; format != NULL; format = g_slist_next (format))
1574 format_item_t *fi = (format_item_t *) format->data;
1576 if (fi->string_fn != NULL)
1578 g_string_set_size (format_txt, 0);
1580 if (panel->list_format == list_long && strcmp (fi->id, panel->sort_field->id) == 0)
1581 g_string_append (format_txt,
1582 panel->sort_info.reverse
1583 ? panel_sort_up_sign : panel_sort_down_sign);
1585 g_string_append (format_txt, fi->title);
1587 if (panel->filter != NULL && strcmp (fi->id, "name") == 0)
1589 g_string_append (format_txt, " [");
1590 g_string_append (format_txt, panel->filter);
1591 g_string_append (format_txt, "]");
1594 tty_setcolor (HEADER_COLOR);
1595 tty_print_string (str_fit_to_term (format_txt->str, fi->field_len, J_CENTER_LEFT));
1597 else
1599 tty_setcolor (NORMAL_COLOR);
1600 tty_print_one_vline (TRUE);
1604 if (i < panel->list_cols - 1)
1606 tty_setcolor (NORMAL_COLOR);
1607 tty_print_one_vline (TRUE);
1611 g_string_free (format_txt, TRUE);
1613 if (panel->list_format != list_long)
1614 panel_paint_sort_info (panel);
1617 /* --------------------------------------------------------------------------------------------- */
1619 static const char *
1620 parse_panel_size (WPanel * panel, const char *format, gboolean isstatus)
1622 panel_display_t frame = frame_half;
1623 format = skip_separators (format);
1625 if (strncmp (format, "full", 4) == 0)
1627 frame = frame_full;
1628 format += 4;
1630 else if (strncmp (format, "half", 4) == 0)
1632 frame = frame_half;
1633 format += 4;
1636 if (!isstatus)
1638 panel->frame_size = frame;
1639 panel->list_cols = 1;
1642 /* Now, the optional column specifier */
1643 format = skip_separators (format);
1645 if (g_ascii_isdigit (*format))
1647 if (!isstatus)
1649 panel->list_cols = g_ascii_digit_value (*format);
1650 if (panel->list_cols < 1)
1651 panel->list_cols = 1;
1654 format++;
1657 if (!isstatus)
1658 panel_update_cols (WIDGET (panel), panel->frame_size);
1660 return skip_separators (format);
1663 /* --------------------------------------------------------------------------------------------- */
1665 /* *INDENT-OFF* */
1666 /* Format is:
1668 all := panel_format? format
1669 panel_format := [full|half] [1-9]
1670 format := one_format_item_t
1671 | format , one_format_item_t
1673 one_format_item_t := just format.id [opt_size]
1674 just := [<=>]
1675 opt_size := : size [opt_expand]
1676 size := [0-9]+
1677 opt_expand := +
1680 /* *INDENT-ON* */
1682 static GSList *
1683 parse_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus,
1684 int *res_total_cols)
1686 GSList *home = NULL; /* The formats we return */
1687 int total_cols = 0; /* Used columns by the format */
1688 size_t i;
1690 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1692 *error = NULL;
1694 if (i18n_timelength == 0)
1696 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1698 for (i = 0; panel_fields[i].id != NULL; i++)
1699 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1700 panel_fields[i].min_size = i18n_timelength;
1704 * This makes sure that the panel and mini status full/half mode
1705 * setting is equal
1707 format = parse_panel_size (panel, format, isstatus);
1709 while (*format != '\0')
1710 { /* format can be an empty string */
1711 format_item_t *darr;
1712 align_crt_t justify; /* Which mode. */
1713 gboolean set_justify = TRUE; /* flag: set justification mode? */
1714 gboolean found = FALSE;
1715 size_t klen = 0;
1717 darr = g_new0 (format_item_t, 1);
1718 home = g_slist_append (home, darr);
1720 format = skip_separators (format);
1722 switch (*format)
1724 case '<':
1725 justify = J_LEFT;
1726 format = skip_separators (format + 1);
1727 break;
1728 case '=':
1729 justify = J_CENTER;
1730 format = skip_separators (format + 1);
1731 break;
1732 case '>':
1733 justify = J_RIGHT;
1734 format = skip_separators (format + 1);
1735 break;
1736 default:
1737 justify = J_LEFT;
1738 set_justify = FALSE;
1739 break;
1742 for (i = 0; !found && panel_fields[i].id != NULL; i++)
1744 klen = strlen (panel_fields[i].id);
1745 found = strncmp (format, panel_fields[i].id, klen) == 0;
1748 if (found)
1750 i--;
1751 format += klen;
1753 darr->requested_field_len = panel_fields[i].min_size;
1754 darr->string_fn = panel_fields[i].string_fn;
1755 darr->title = g_strdup (panel_get_title_without_hotkey (panel_fields[i].title_hotkey));
1756 darr->id = panel_fields[i].id;
1757 darr->expand = panel_fields[i].expands;
1758 darr->just_mode = panel_fields[i].default_just;
1760 if (set_justify)
1762 if (IS_FIT (darr->just_mode))
1763 darr->just_mode = MAKE_FIT (justify);
1764 else
1765 darr->just_mode = justify;
1768 format = skip_separators (format);
1770 /* If we have a size specifier */
1771 if (*format == ':')
1773 int req_length;
1775 /* If the size was specified, we don't want
1776 * auto-expansion by default
1778 darr->expand = FALSE;
1779 format++;
1780 req_length = atoi (format);
1781 darr->requested_field_len = req_length;
1783 format = skip_numbers (format);
1785 /* Now, if they insist on expansion */
1786 if (*format == '+')
1788 darr->expand = TRUE;
1789 format++;
1793 else
1795 size_t pos;
1796 char *tmp_format;
1798 pos = strlen (format);
1799 if (pos > 8)
1800 pos = 8;
1802 tmp_format = g_strndup (format, pos);
1803 g_slist_free_full (home, (GDestroyNotify) format_item_free);
1804 *error =
1805 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1806 g_free (tmp_format);
1808 return NULL;
1811 total_cols += darr->requested_field_len;
1814 *res_total_cols = total_cols;
1815 return home;
1818 /* --------------------------------------------------------------------------------------------- */
1820 static GSList *
1821 use_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus)
1823 #define MAX_EXPAND 4
1824 int expand_top = 0; /* Max used element in expand */
1825 int usable_columns; /* Usable columns in the panel */
1826 int total_cols = 0;
1827 GSList *darr, *home;
1829 if (format == NULL)
1830 format = DEFAULT_USER_FORMAT;
1832 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1834 if (*error != NULL)
1835 return NULL;
1837 panel->dirty = 1;
1839 usable_columns = WIDGET (panel)->cols - 2;
1840 /* Status needn't to be split */
1841 if (!isstatus)
1843 usable_columns /= panel->list_cols;
1844 if (panel->list_cols > 1)
1845 usable_columns--;
1848 /* Look for the expandable fields and set field_len based on the requested field len */
1849 for (darr = home; darr != NULL && expand_top < MAX_EXPAND; darr = g_slist_next (darr))
1851 format_item_t *fi = (format_item_t *) darr->data;
1853 fi->field_len = fi->requested_field_len;
1854 if (fi->expand)
1855 expand_top++;
1858 /* If we used more columns than the available columns, adjust that */
1859 if (total_cols > usable_columns)
1861 int dif;
1862 int pdif = 0;
1864 dif = total_cols - usable_columns;
1866 while (dif != 0 && pdif != dif)
1868 pdif = dif;
1870 for (darr = home; darr != NULL; darr = g_slist_next (darr))
1872 format_item_t *fi = (format_item_t *) darr->data;
1874 if (dif != 0 && fi->field_len != 1)
1876 fi->field_len--;
1877 dif--;
1882 total_cols = usable_columns; /* give up, the rest should be truncated */
1885 /* Expand the available space */
1886 if (usable_columns > total_cols && expand_top != 0)
1888 int i;
1889 int spaces;
1891 spaces = (usable_columns - total_cols) / expand_top;
1893 for (i = 0, darr = home; darr != NULL && i < expand_top; darr = g_slist_next (darr))
1895 format_item_t *fi = (format_item_t *) darr->data;
1897 if (fi->expand)
1899 fi->field_len += spaces;
1900 if (i == 0)
1901 fi->field_len += (usable_columns - total_cols) % expand_top;
1902 i++;
1907 return home;
1910 /* --------------------------------------------------------------------------------------------- */
1911 /** Given the panel->view_type returns the format string to be parsed */
1913 static const char *
1914 panel_format (WPanel * panel)
1916 switch (panel->list_format)
1918 case list_long:
1919 return "full perm space nlink space owner space group space size space mtime space name";
1921 case list_brief:
1923 static char format[BUF_TINY];
1924 int brief_cols = panel->brief_cols;
1926 if (brief_cols < 1)
1927 brief_cols = 2;
1929 if (brief_cols > 9)
1930 brief_cols = 9;
1932 g_snprintf (format, sizeof (format), "half %d type name", brief_cols);
1933 return format;
1936 case list_user:
1937 return panel->user_format;
1939 default:
1940 case list_full:
1941 return "half type name | size | mtime";
1945 /* --------------------------------------------------------------------------------------------- */
1947 static const char *
1948 mini_status_format (WPanel * panel)
1950 if (panel->user_mini_status)
1951 return panel->user_status_format[panel->list_format];
1953 switch (panel->list_format)
1955 case list_long:
1956 return "full perm space nlink space owner space group space size space mtime space name";
1958 case list_brief:
1959 return "half type name space bsize space perm space";
1961 case list_full:
1962 return "half type name";
1964 default:
1965 case list_user:
1966 return panel->user_format;
1970 /* */
1971 /* Panel operation commands */
1972 /* */
1974 /* --------------------------------------------------------------------------------------------- */
1976 static void
1977 cd_up_dir (void)
1979 vfs_path_t *up_dir;
1981 up_dir = vfs_path_from_str ("..");
1982 do_cd (up_dir, cd_exact);
1983 vfs_path_free (up_dir);
1986 /* --------------------------------------------------------------------------------------------- */
1987 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1989 static cb_ret_t
1990 maybe_cd (gboolean move_up_dir)
1992 if (panels_options.navigate_with_arrows && input_is_empty (cmdline))
1994 if (move_up_dir)
1996 cd_up_dir ();
1997 return MSG_HANDLED;
2000 if (S_ISDIR (selection (current_panel)->st.st_mode)
2001 || link_isdir (selection (current_panel)))
2003 vfs_path_t *vpath;
2005 vpath = vfs_path_from_str (selection (current_panel)->fname);
2006 do_cd (vpath, cd_exact);
2007 vfs_path_free (vpath);
2008 return MSG_HANDLED;
2012 return MSG_NOT_HANDLED;
2015 /* --------------------------------------------------------------------------------------------- */
2017 /* if command line is empty then do 'cd ..' */
2018 static cb_ret_t
2019 force_maybe_cd (void)
2021 if (input_is_empty (cmdline))
2023 cd_up_dir ();
2024 return MSG_HANDLED;
2027 return MSG_NOT_HANDLED;
2030 /* --------------------------------------------------------------------------------------------- */
2032 static inline void
2033 unselect_item (WPanel * panel)
2035 repaint_file (panel, panel->selected, TRUE, 2 * selection (panel)->f.marked, FALSE);
2038 /* --------------------------------------------------------------------------------------------- */
2039 /** Select/unselect all the files like a current file by extension */
2041 static void
2042 panel_select_ext_cmd (void)
2044 gboolean do_select = !selection (current_panel)->f.marked;
2045 char *filename = selection (current_panel)->fname;
2046 char *reg_exp, *cur_file_ext;
2047 mc_search_t *search;
2048 int i;
2050 if (filename == NULL)
2051 return;
2053 cur_file_ext = strutils_regex_escape (extension (filename));
2055 if (cur_file_ext[0] != '\0')
2056 reg_exp = g_strconcat ("^.*\\.", cur_file_ext, "$", (char *) NULL);
2057 else
2058 reg_exp = g_strdup ("^[^\\.]+$");
2060 g_free (cur_file_ext);
2062 search = mc_search_new (reg_exp, NULL);
2063 search->search_type = MC_SEARCH_T_REGEX;
2064 search->is_case_sensitive = FALSE;
2066 for (i = 0; i < current_panel->dir.len; i++)
2068 file_entry_t *file_entry = &current_panel->dir.list[i];
2070 if (DIR_IS_DOTDOT (file_entry->fname) || S_ISDIR (file_entry->st.st_mode))
2071 continue;
2073 if (!mc_search_run (search, file_entry->fname, 0, file_entry->fnamelen, NULL))
2074 continue;
2076 do_file_mark (current_panel, i, do_select);
2079 mc_search_free (search);
2080 g_free (reg_exp);
2083 /* --------------------------------------------------------------------------------------------- */
2085 static int
2086 panel_selected_at_half (const WPanel * panel)
2088 int lines, top;
2090 lines = panel_lines (panel);
2092 /* define top file of column */
2093 top = panel->top_file;
2094 if (panel->list_cols > 1)
2095 top += lines * ((panel->selected - top) / lines);
2097 return (panel->selected - top - lines / 2);
2100 /* --------------------------------------------------------------------------------------------- */
2102 static void
2103 move_down (WPanel * panel)
2105 int items;
2107 if (panel->selected + 1 == panel->dir.len)
2108 return;
2110 unselect_item (panel);
2111 panel->selected++;
2113 items = panel_items (panel);
2115 if (panels_options.scroll_pages && panel->selected - panel->top_file == items)
2117 /* Scroll window half screen */
2118 panel->top_file += items / 2;
2119 if (panel->top_file > panel->dir.len - items)
2120 panel->top_file = panel->dir.len - items;
2121 paint_dir (panel);
2123 else if (panels_options.scroll_center && panel_selected_at_half (panel) > 0)
2125 /* Scroll window when cursor is halfway down */
2126 panel->top_file++;
2127 if (panel->top_file > panel->dir.len - items)
2128 panel->top_file = panel->dir.len - items;
2130 select_item (panel);
2133 /* --------------------------------------------------------------------------------------------- */
2135 static void
2136 move_up (WPanel * panel)
2138 if (panel->selected == 0)
2139 return;
2141 unselect_item (panel);
2142 panel->selected--;
2144 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2146 /* Scroll window half screen */
2147 panel->top_file -= panel_items (panel) / 2;
2148 if (panel->top_file < 0)
2149 panel->top_file = 0;
2150 paint_dir (panel);
2152 else if (panels_options.scroll_center && panel_selected_at_half (panel) < 0)
2154 /* Scroll window when cursor is halfway up */
2155 panel->top_file--;
2156 if (panel->top_file < 0)
2157 panel->top_file = 0;
2159 select_item (panel);
2162 /* --------------------------------------------------------------------------------------------- */
2163 /** Changes the selection by lines (may be negative) */
2165 static void
2166 move_selection (WPanel * panel, int lines)
2168 int new_pos;
2169 gboolean adjust = FALSE;
2171 new_pos = panel->selected + lines;
2172 if (new_pos >= panel->dir.len)
2173 new_pos = panel->dir.len - 1;
2175 if (new_pos < 0)
2176 new_pos = 0;
2178 unselect_item (panel);
2179 panel->selected = new_pos;
2181 if (panel->selected - panel->top_file >= panel_items (panel))
2183 panel->top_file += lines;
2184 adjust = TRUE;
2187 if (panel->selected - panel->top_file < 0)
2189 panel->top_file += lines;
2190 adjust = TRUE;
2193 if (adjust)
2195 if (panel->top_file > panel->selected)
2196 panel->top_file = panel->selected;
2197 if (panel->top_file < 0)
2198 panel->top_file = 0;
2199 paint_dir (panel);
2201 select_item (panel);
2204 /* --------------------------------------------------------------------------------------------- */
2206 static cb_ret_t
2207 move_left (WPanel * panel)
2209 if (panel->list_cols > 1)
2211 move_selection (panel, -panel_lines (panel));
2212 return MSG_HANDLED;
2215 return maybe_cd (TRUE); /* cd .. */
2218 /* --------------------------------------------------------------------------------------------- */
2220 static cb_ret_t
2221 move_right (WPanel * panel)
2223 if (panel->list_cols > 1)
2225 move_selection (panel, panel_lines (panel));
2226 return MSG_HANDLED;
2229 return maybe_cd (FALSE); /* cd (selection) */
2232 /* --------------------------------------------------------------------------------------------- */
2234 static void
2235 prev_page (WPanel * panel)
2237 int items;
2239 if (!panel->selected && !panel->top_file)
2240 return;
2242 unselect_item (panel);
2243 items = panel_items (panel);
2244 if (panel->top_file < items)
2245 items = panel->top_file;
2246 if (items == 0)
2247 panel->selected = 0;
2248 else
2249 panel->selected -= items;
2250 panel->top_file -= items;
2252 select_item (panel);
2253 paint_dir (panel);
2256 /* --------------------------------------------------------------------------------------------- */
2258 static void
2259 goto_parent_dir (WPanel * panel)
2261 if (!panel->is_panelized)
2262 cd_up_dir ();
2263 else
2265 char *fname = panel->dir.list[panel->selected].fname;
2266 const char *bname;
2267 vfs_path_t *dname_vpath;
2269 if (g_path_is_absolute (fname))
2270 fname = g_strdup (fname);
2271 else
2272 fname =
2273 mc_build_filename (vfs_path_as_str (panelized_panel.root_vpath), fname,
2274 (char *) NULL);
2276 bname = x_basename (fname);
2278 if (bname == fname)
2279 dname_vpath = vfs_path_from_str (".");
2280 else
2282 char *dname;
2284 dname = g_strndup (fname, bname - fname);
2285 dname_vpath = vfs_path_from_str (dname);
2286 g_free (dname);
2289 do_cd (dname_vpath, cd_exact);
2290 try_to_select (panel, bname);
2292 vfs_path_free (dname_vpath);
2293 g_free (fname);
2297 /* --------------------------------------------------------------------------------------------- */
2299 static void
2300 next_page (WPanel * panel)
2302 int items;
2304 if (panel->selected == panel->dir.len - 1)
2305 return;
2307 unselect_item (panel);
2308 items = panel_items (panel);
2309 if (panel->top_file > panel->dir.len - 2 * items)
2310 items = panel->dir.len - items - panel->top_file;
2311 if (panel->top_file + items < 0)
2312 items = -panel->top_file;
2313 if (items == 0)
2314 panel->selected = panel->dir.len - 1;
2315 else
2316 panel->selected += items;
2317 panel->top_file += items;
2319 select_item (panel);
2320 paint_dir (panel);
2323 /* --------------------------------------------------------------------------------------------- */
2325 static void
2326 goto_child_dir (WPanel * panel)
2328 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2330 vfs_path_t *vpath;
2332 vpath = vfs_path_from_str (selection (panel)->fname);
2333 do_cd (vpath, cd_exact);
2334 vfs_path_free (vpath);
2338 /* --------------------------------------------------------------------------------------------- */
2340 static void
2341 goto_top_file (WPanel * panel)
2343 unselect_item (panel);
2344 panel->selected = panel->top_file;
2345 select_item (panel);
2348 /* --------------------------------------------------------------------------------------------- */
2350 static void
2351 goto_middle_file (WPanel * panel)
2353 unselect_item (panel);
2354 panel->selected = panel->top_file + panel_items (panel) / 2;
2355 select_item (panel);
2358 /* --------------------------------------------------------------------------------------------- */
2360 static void
2361 goto_bottom_file (WPanel * panel)
2363 unselect_item (panel);
2364 panel->selected = panel->top_file + panel_items (panel) - 1;
2365 select_item (panel);
2368 /* --------------------------------------------------------------------------------------------- */
2370 static void
2371 move_home (WPanel * panel)
2373 if (panel->selected == 0)
2374 return;
2376 unselect_item (panel);
2378 if (panels_options.torben_fj_mode)
2380 int middle_pos = panel->top_file + panel_items (panel) / 2;
2382 if (panel->selected > middle_pos)
2384 goto_middle_file (panel);
2385 return;
2387 if (panel->selected != panel->top_file)
2389 goto_top_file (panel);
2390 return;
2394 panel->top_file = 0;
2395 panel->selected = 0;
2397 paint_dir (panel);
2398 select_item (panel);
2401 /* --------------------------------------------------------------------------------------------- */
2403 static void
2404 move_end (WPanel * panel)
2406 if (panel->selected == panel->dir.len - 1)
2407 return;
2409 unselect_item (panel);
2411 if (panels_options.torben_fj_mode)
2413 int items, middle_pos;
2415 items = panel_items (panel);
2416 middle_pos = panel->top_file + items / 2;
2418 if (panel->selected < middle_pos)
2420 goto_middle_file (panel);
2421 return;
2423 if (panel->selected != panel->top_file + items - 1)
2425 goto_bottom_file (panel);
2426 return;
2430 panel->selected = panel->dir.len - 1;
2431 paint_dir (panel);
2432 select_item (panel);
2435 /* --------------------------------------------------------------------------------------------- */
2437 static void
2438 do_mark_file (WPanel * panel, mark_act_t do_move)
2440 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2441 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2442 move_down (panel);
2443 else if (do_move == MARK_FORCE_UP)
2444 move_up (panel);
2447 /* --------------------------------------------------------------------------------------------- */
2449 static inline void
2450 mark_file (WPanel * panel)
2452 do_mark_file (panel, MARK_DOWN);
2455 /* --------------------------------------------------------------------------------------------- */
2457 static inline void
2458 mark_file_up (WPanel * panel)
2460 do_mark_file (panel, MARK_FORCE_UP);
2463 /* --------------------------------------------------------------------------------------------- */
2465 static inline void
2466 mark_file_down (WPanel * panel)
2468 do_mark_file (panel, MARK_FORCE_DOWN);
2471 /* --------------------------------------------------------------------------------------------- */
2473 static void
2474 mark_file_right (WPanel * panel)
2476 int lines;
2478 if (state_mark < 0)
2479 state_mark = selection (panel)->f.marked ? 0 : 1;
2481 lines = panel_lines (panel);
2482 lines = MIN (lines, panel->dir.len - panel->selected - 1);
2483 for (; lines != 0; lines--)
2485 do_file_mark (panel, panel->selected, state_mark);
2486 move_down (panel);
2488 do_file_mark (panel, panel->selected, state_mark);
2491 /* --------------------------------------------------------------------------------------------- */
2493 static void
2494 mark_file_left (WPanel * panel)
2496 int lines;
2498 if (state_mark < 0)
2499 state_mark = selection (panel)->f.marked ? 0 : 1;
2501 lines = panel_lines (panel);
2502 lines = MIN (lines, panel->selected + 1);
2503 for (; lines != 0; lines--)
2505 do_file_mark (panel, panel->selected, state_mark);
2506 move_up (panel);
2508 do_file_mark (panel, panel->selected, state_mark);
2511 /* --------------------------------------------------------------------------------------------- */
2513 static void
2514 panel_select_unselect_files (WPanel * panel, const char *title, const char *history_name,
2515 gboolean do_select)
2517 gboolean files_only = (panels_options.select_flags & SELECT_FILES_ONLY) != 0;
2518 gboolean case_sens = (panels_options.select_flags & SELECT_MATCH_CASE) != 0;
2519 gboolean shell_patterns = (panels_options.select_flags & SELECT_SHELL_PATTERNS) != 0;
2521 char *reg_exp;
2522 mc_search_t *search;
2523 int i;
2525 quick_widget_t quick_widgets[] = {
2526 /* *INDENT-OFF* */
2527 QUICK_INPUT (INPUT_LAST_TEXT, history_name, &reg_exp, NULL,
2528 FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
2529 QUICK_START_COLUMNS,
2530 QUICK_CHECKBOX (N_("&Files only"), &files_only, NULL),
2531 QUICK_CHECKBOX (N_("&Using shell patterns"), &shell_patterns, NULL),
2532 QUICK_NEXT_COLUMN,
2533 QUICK_CHECKBOX (N_("&Case sensitive"), &case_sens, NULL),
2534 QUICK_STOP_COLUMNS,
2535 QUICK_END
2536 /* *INDENT-ON* */
2539 quick_dialog_t qdlg = {
2540 -1, -1, 50,
2541 title, "[Select/Unselect Files]",
2542 quick_widgets, NULL, NULL
2545 if (quick_dialog (&qdlg) == B_CANCEL)
2546 return;
2548 if (reg_exp == NULL || *reg_exp == '\0')
2550 g_free (reg_exp);
2551 return;
2554 search = mc_search_new (reg_exp, NULL);
2555 search->search_type = shell_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
2556 search->is_entire_line = TRUE;
2557 search->is_case_sensitive = case_sens;
2559 for (i = 0; i < panel->dir.len; i++)
2561 if (DIR_IS_DOTDOT (panel->dir.list[i].fname))
2562 continue;
2563 if (S_ISDIR (panel->dir.list[i].st.st_mode) && files_only)
2564 continue;
2566 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2567 do_file_mark (panel, i, do_select);
2570 mc_search_free (search);
2571 g_free (reg_exp);
2573 /* result flags */
2574 panels_options.select_flags = 0;
2575 if (case_sens)
2576 panels_options.select_flags |= SELECT_MATCH_CASE;
2577 if (files_only)
2578 panels_options.select_flags |= SELECT_FILES_ONLY;
2579 if (shell_patterns)
2580 panels_options.select_flags |= SELECT_SHELL_PATTERNS;
2583 /* --------------------------------------------------------------------------------------------- */
2585 static void
2586 panel_select_files (WPanel * panel)
2588 panel_select_unselect_files (panel, _("Select"), ":select_cmd: Select ", TRUE);
2591 /* --------------------------------------------------------------------------------------------- */
2593 static void
2594 panel_unselect_files (WPanel * panel)
2596 panel_select_unselect_files (panel, _("Unselect"), ":unselect_cmd: Unselect ", FALSE);
2599 /* --------------------------------------------------------------------------------------------- */
2601 static void
2602 panel_select_invert_files (WPanel * panel)
2604 int i;
2606 for (i = 0; i < panel->dir.len; i++)
2608 file_entry_t *file = &panel->dir.list[i];
2610 if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
2611 do_file_mark (panel, i, !file->f.marked);
2615 /* --------------------------------------------------------------------------------------------- */
2616 /** Incremental search of a file name in the panel.
2617 * @param panel instance of WPanel structure
2618 * @param c_code key code
2621 static void
2622 do_search (WPanel * panel, int c_code)
2624 size_t l;
2625 int i, sel;
2626 gboolean wrapped = FALSE;
2627 char *act;
2628 mc_search_t *search;
2629 char *reg_exp, *esc_str;
2630 gboolean is_found = FALSE;
2632 l = strlen (panel->search_buffer);
2633 if (c_code == KEY_BACKSPACE)
2635 if (l != 0)
2637 act = panel->search_buffer + l;
2638 str_prev_noncomb_char (&act, panel->search_buffer);
2639 act[0] = '\0';
2641 panel->search_chpoint = 0;
2643 else
2645 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2647 panel->search_char[panel->search_chpoint] = c_code;
2648 panel->search_chpoint++;
2651 if (panel->search_chpoint > 0)
2653 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2655 case -2:
2656 return;
2657 case -1:
2658 panel->search_chpoint = 0;
2659 return;
2660 default:
2661 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2663 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2664 l += panel->search_chpoint;
2665 *(panel->search_buffer + l) = '\0';
2666 panel->search_chpoint = 0;
2672 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2673 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2674 search = mc_search_new (esc_str, NULL);
2675 search->search_type = MC_SEARCH_T_GLOB;
2676 search->is_entire_line = TRUE;
2678 switch (panels_options.qsearch_mode)
2680 case QSEARCH_CASE_SENSITIVE:
2681 search->is_case_sensitive = TRUE;
2682 break;
2683 case QSEARCH_CASE_INSENSITIVE:
2684 search->is_case_sensitive = FALSE;
2685 break;
2686 default:
2687 search->is_case_sensitive = panel->sort_info.case_sensitive;
2688 break;
2691 sel = panel->selected;
2693 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2695 if (i >= panel->dir.len)
2697 i = 0;
2698 if (wrapped)
2699 break;
2700 wrapped = TRUE;
2702 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2704 sel = i;
2705 is_found = TRUE;
2706 break;
2709 if (is_found)
2711 unselect_item (panel);
2712 panel->selected = sel;
2713 select_item (panel);
2714 widget_redraw (WIDGET (panel));
2716 else if (c_code != KEY_BACKSPACE)
2718 act = panel->search_buffer + l;
2719 str_prev_noncomb_char (&act, panel->search_buffer);
2720 act[0] = '\0';
2722 mc_search_free (search);
2723 g_free (reg_exp);
2724 g_free (esc_str);
2727 /* --------------------------------------------------------------------------------------------- */
2728 /** Start new search.
2729 * @param panel instance of WPanel structure
2732 static void
2733 start_search (WPanel * panel)
2735 if (panel->searching)
2737 if (panel->selected + 1 == panel->dir.len)
2738 panel->selected = 0;
2739 else
2740 move_down (panel);
2742 /* in case if there was no search string we need to recall
2743 previous string, with which we ended previous searching */
2744 if (panel->search_buffer[0] == '\0')
2745 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2746 sizeof (panel->search_buffer));
2748 do_search (panel, 0);
2750 else
2752 panel->searching = TRUE;
2753 panel->search_buffer[0] = '\0';
2754 panel->search_char[0] = '\0';
2755 panel->search_chpoint = 0;
2756 display_mini_info (panel);
2757 mc_refresh ();
2761 /* --------------------------------------------------------------------------------------------- */
2763 static void
2764 stop_search (WPanel * panel)
2766 panel->searching = FALSE;
2768 /* if user had overrdied search string, we need to store it
2769 to the previous_search_buffer */
2770 if (panel->search_buffer[0] != '\0')
2771 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2772 sizeof (panel->prev_search_buffer));
2774 display_mini_info (panel);
2777 /* --------------------------------------------------------------------------------------------- */
2778 /** Return TRUE if the Enter key has been processed, FALSE otherwise */
2780 static gboolean
2781 do_enter_on_file_entry (file_entry_t * fe)
2783 vfs_path_t *full_name_vpath;
2784 gboolean ok;
2787 * Directory or link to directory - change directory.
2788 * Try the same for the entries on which mc_lstat() has failed.
2790 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2792 vfs_path_t *fname_vpath;
2794 fname_vpath = vfs_path_from_str (fe->fname);
2795 if (!do_cd (fname_vpath, cd_exact))
2796 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2797 vfs_path_free (fname_vpath);
2798 return TRUE;
2801 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, (char *) NULL);
2803 /* Try associated command */
2804 ok = regex_command (full_name_vpath, "Open") != 0;
2805 vfs_path_free (full_name_vpath);
2806 if (ok)
2807 return TRUE;
2809 /* Check if the file is executable */
2810 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, (char *) NULL);
2811 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2812 vfs_path_free (full_name_vpath);
2813 if (!ok)
2814 return FALSE;
2816 if (confirm_execute
2817 && query_dialog (_("The Midnight Commander"), _("Do you really want to execute?"), D_NORMAL,
2818 2, _("&Yes"), _("&No")) != 0)
2819 return TRUE;
2821 if (!vfs_current_is_local ())
2823 int ret;
2824 vfs_path_t *tmp_vpath;
2826 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, (char *) NULL);
2827 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2828 vfs_path_free (tmp_vpath);
2829 /* We took action only if the dialog was shown or the execution was successful */
2830 return confirm_execute || (ret == 0);
2834 char *tmp, *cmd;
2836 tmp = name_quote (fe->fname, FALSE);
2837 cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2838 g_free (tmp);
2839 shell_execute (cmd, 0);
2840 g_free (cmd);
2843 #ifdef HAVE_CHARSET
2844 mc_global.source_codepage = default_source_codepage;
2845 #endif
2847 return TRUE;
2850 /* --------------------------------------------------------------------------------------------- */
2852 static inline gboolean
2853 do_enter (WPanel * panel)
2855 return do_enter_on_file_entry (selection (panel));
2859 /* --------------------------------------------------------------------------------------------- */
2861 static void
2862 panel_cycle_listing_format (WPanel * panel)
2864 panel->list_format = (panel->list_format + 1) % LIST_FORMATS;
2866 if (set_panel_formats (panel) == 0)
2867 do_refresh ();
2870 /* --------------------------------------------------------------------------------------------- */
2872 static void
2873 chdir_other_panel (WPanel * panel)
2875 const file_entry_t *entry = &panel->dir.list[panel->selected];
2876 vfs_path_t *new_dir_vpath;
2877 char *sel_entry = NULL;
2879 if (get_other_type () != view_listing)
2880 create_panel (get_other_index (), view_listing);
2882 if (S_ISDIR (entry->st.st_mode) || link_isdir (entry))
2883 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, (char *) NULL);
2884 else
2886 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2887 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2890 change_panel ();
2891 do_cd (new_dir_vpath, cd_exact);
2892 vfs_path_free (new_dir_vpath);
2894 if (sel_entry)
2895 try_to_select (current_panel, sel_entry);
2896 change_panel ();
2898 move_down (panel);
2901 /* --------------------------------------------------------------------------------------------- */
2903 * Make the current directory of the current panel also the current
2904 * directory of the other panel. Put the other panel to the listing
2905 * mode if needed. If the current panel is panelized, the other panel
2906 * doesn't become panelized.
2909 static void
2910 panel_sync_other (const WPanel * panel)
2912 if (get_other_type () != view_listing)
2913 create_panel (get_other_index (), view_listing);
2915 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2917 /* try to select current filename on the other panel */
2918 if (!panel->is_panelized)
2919 try_to_select (other_panel, selection (panel)->fname);
2922 /* --------------------------------------------------------------------------------------------- */
2924 static void
2925 chdir_to_readlink (WPanel * panel)
2927 vfs_path_t *new_dir_vpath;
2928 char buffer[MC_MAXPATHLEN];
2929 int i;
2930 struct stat st;
2931 vfs_path_t *panel_fname_vpath;
2932 gboolean ok;
2934 if (get_other_type () != view_listing)
2935 return;
2937 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2938 return;
2940 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2941 if (i < 0)
2942 return;
2944 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2945 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2946 vfs_path_free (panel_fname_vpath);
2947 if (!ok)
2948 return;
2950 buffer[i] = '\0';
2951 if (!S_ISDIR (st.st_mode))
2953 char *p;
2955 p = strrchr (buffer, PATH_SEP);
2956 if (p != NULL && p[1] == '\0')
2958 *p = '\0';
2959 p = strrchr (buffer, PATH_SEP);
2961 if (p == NULL)
2962 return;
2964 p[1] = '\0';
2966 if (IS_PATH_SEP (*buffer))
2967 new_dir_vpath = vfs_path_from_str (buffer);
2968 else
2969 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, (char *) NULL);
2971 change_panel ();
2972 do_cd (new_dir_vpath, cd_exact);
2973 vfs_path_free (new_dir_vpath);
2974 change_panel ();
2976 move_down (panel);
2979 /* --------------------------------------------------------------------------------------------- */
2981 function return 0 if not found and REAL_INDEX+1 if found
2984 static gsize
2985 panel_get_format_field_index_by_name (const WPanel * panel, const char *name)
2987 GSList *format;
2988 gsize lc_index;
2990 for (lc_index = 1, format = panel->format;
2991 format != NULL && strcmp (((format_item_t *) format->data)->title, name) != 0;
2992 format = g_slist_next (format), lc_index++)
2995 if (format == NULL)
2996 lc_index = 0;
2998 return lc_index;
3001 /* --------------------------------------------------------------------------------------------- */
3003 static const panel_field_t *
3004 panel_get_sortable_field_by_format (const WPanel * panel, gsize lc_index)
3006 const panel_field_t *pfield;
3007 const format_item_t *format;
3009 format = (const format_item_t *) g_slist_nth_data (panel->format, lc_index);
3010 if (format == NULL)
3011 return NULL;
3013 pfield = panel_get_field_by_title (format->title);
3014 if (pfield == NULL)
3015 return NULL;
3016 if (pfield->sort_routine == NULL)
3017 return NULL;
3018 return pfield;
3021 /* --------------------------------------------------------------------------------------------- */
3023 static void
3024 panel_toggle_sort_order_prev (WPanel * panel)
3026 gsize lc_index, i;
3027 const char *title;
3028 const panel_field_t *pfield = NULL;
3030 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3031 lc_index = panel_get_format_field_index_by_name (panel, title);
3033 if (lc_index > 1)
3035 /* search for prev sortable column in panel format */
3036 for (i = lc_index - 1;
3037 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3041 if (pfield == NULL)
3043 /* Sortable field not found. Try to search in each array */
3044 for (i = g_slist_length (panel->format);
3045 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3049 if (pfield != NULL)
3051 panel->sort_field = pfield;
3052 panel_set_sort_order (panel, pfield);
3056 /* --------------------------------------------------------------------------------------------- */
3058 static void
3059 panel_toggle_sort_order_next (WPanel * panel)
3061 gsize lc_index, i;
3062 const panel_field_t *pfield = NULL;
3063 gsize format_field_count;
3064 const char *title;
3066 format_field_count = g_slist_length (panel->format);
3067 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3068 lc_index = panel_get_format_field_index_by_name (panel, title);
3070 if (lc_index != 0 && lc_index != format_field_count)
3072 /* search for prev sortable column in panel format */
3073 for (i = lc_index;
3074 i != format_field_count
3075 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
3079 if (pfield == NULL)
3081 /* Sortable field not found. Try to search in each array */
3082 for (i = 0;
3083 i != format_field_count
3084 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
3088 if (pfield != NULL)
3090 panel->sort_field = pfield;
3091 panel_set_sort_order (panel, pfield);
3095 /* --------------------------------------------------------------------------------------------- */
3097 static void
3098 panel_select_sort_order (WPanel * panel)
3100 const panel_field_t *sort_order;
3102 sort_order = sort_box (&panel->sort_info, panel->sort_field);
3103 if (sort_order != NULL)
3105 panel->sort_field = sort_order;
3106 panel_set_sort_order (panel, sort_order);
3110 /* --------------------------------------------------------------------------------------------- */
3113 * panel_content_scroll_left:
3114 * @param panel the pointer to the panel on which we operate
3116 * scroll long filename to the left (decrement scroll pointer)
3120 static void
3121 panel_content_scroll_left (WPanel * panel)
3123 if (panel->content_shift > -1)
3125 if (panel->content_shift > panel->max_shift)
3126 panel->content_shift = panel->max_shift;
3128 panel->content_shift--;
3129 show_dir (panel);
3130 paint_dir (panel);
3134 /* --------------------------------------------------------------------------------------------- */
3136 * panel_content_scroll_right:
3137 * @param panel the pointer to the panel on which we operate
3139 * scroll long filename to the right (increment scroll pointer)
3143 static void
3144 panel_content_scroll_right (WPanel * panel)
3146 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
3148 panel->content_shift++;
3149 show_dir (panel);
3150 paint_dir (panel);
3154 /* --------------------------------------------------------------------------------------------- */
3156 static void
3157 panel_set_sort_type_by_id (WPanel * panel, const char *name)
3159 if (strcmp (panel->sort_field->id, name) != 0)
3161 const panel_field_t *sort_order;
3163 sort_order = panel_get_field_by_id (name);
3164 if (sort_order == NULL)
3165 return;
3167 panel->sort_field = sort_order;
3169 else
3170 panel->sort_info.reverse = !panel->sort_info.reverse;
3172 panel_set_sort_order (panel, panel->sort_field);
3175 /* --------------------------------------------------------------------------------------------- */
3177 * If we moved to the parent directory move the selection pointer to
3178 * the old directory name; If we leave VFS dir, remove FS specificator.
3180 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
3183 static const char *
3184 get_parent_dir_name (const vfs_path_t * cwd_vpath, const vfs_path_t * lwd_vpath)
3186 size_t llen, clen;
3187 const char *p, *lwd;
3189 llen = vfs_path_len (lwd_vpath);
3190 clen = vfs_path_len (cwd_vpath);
3192 if (llen <= clen)
3193 return NULL;
3195 lwd = vfs_path_as_str (lwd_vpath);
3197 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
3199 if (p == NULL)
3201 const char *cwd;
3203 cwd = vfs_path_as_str (cwd_vpath);
3205 p = strrchr (lwd, PATH_SEP);
3207 if ((p != NULL)
3208 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
3209 && (clen == (size_t) (p - lwd) || (p == lwd && IS_PATH_SEP (cwd[0]) && cwd[1] == '\0')))
3210 return (p + 1);
3212 return NULL;
3215 /* skip VFS prefix */
3216 while (--p > lwd && !IS_PATH_SEP (*p))
3218 /* get last component */
3219 while (--p > lwd && !IS_PATH_SEP (*p))
3222 /* return last component */
3223 return (p != lwd || IS_PATH_SEP (*p)) ? p + 1 : p;
3226 /* --------------------------------------------------------------------------------------------- */
3227 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3229 static void
3230 subshell_chdir (const vfs_path_t * vpath)
3232 #ifdef ENABLE_SUBSHELL
3233 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3234 do_subshell_chdir (vpath, FALSE);
3235 #else /* ENABLE_SUBSHELL */
3236 (void) vpath;
3237 #endif /* ENABLE_SUBSHELL */
3240 /* --------------------------------------------------------------------------------------------- */
3242 * Changes the current directory of the panel.
3243 * Don't record change in the directory history.
3246 static gboolean
3247 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3249 vfs_path_t *olddir_vpath;
3251 /* Convert *new_path to a suitable pathname, handle ~user */
3252 if (cd_type == cd_parse_command)
3254 const vfs_path_element_t *element;
3256 element = vfs_path_get_by_index (new_dir_vpath, 0);
3257 if (strcmp (element->path, "-") == 0)
3258 new_dir_vpath = panel->lwd_vpath;
3261 if (mc_chdir (new_dir_vpath) == -1)
3262 return FALSE;
3264 /* Success: save previous directory, shutdown status of previous dir */
3265 olddir_vpath = vfs_path_clone (panel->cwd_vpath);
3266 panel_set_lwd (panel, panel->cwd_vpath);
3267 input_free_completions (cmdline);
3269 vfs_path_free (panel->cwd_vpath);
3270 vfs_setup_cwd ();
3271 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3273 vfs_release_path (olddir_vpath);
3275 subshell_chdir (panel->cwd_vpath);
3277 /* Reload current panel */
3278 panel_clean_dir (panel);
3280 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
3281 &panel->sort_info, panel->filter);
3282 try_to_select (panel, get_parent_dir_name (panel->cwd_vpath, olddir_vpath));
3284 load_hint (FALSE);
3285 panel->dirty = 1;
3286 update_xterm_title_path ();
3288 vfs_path_free (olddir_vpath);
3290 return TRUE;
3293 /* --------------------------------------------------------------------------------------------- */
3295 static void
3296 directory_history_next (WPanel * panel)
3298 gboolean ok;
3302 GList *next;
3304 ok = TRUE;
3305 next = g_list_next (panel->dir_history_current);
3306 if (next != NULL)
3308 vfs_path_t *data_vpath;
3310 data_vpath = vfs_path_from_str ((char *) next->data);
3311 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3312 vfs_path_free (data_vpath);
3313 panel->dir_history_current = next;
3315 /* skip directories that present in history but absent in file system */
3317 while (!ok);
3320 /* --------------------------------------------------------------------------------------------- */
3322 static void
3323 directory_history_prev (WPanel * panel)
3325 gboolean ok;
3329 GList *prev;
3331 ok = TRUE;
3332 prev = g_list_previous (panel->dir_history_current);
3333 if (prev != NULL)
3335 vfs_path_t *data_vpath;
3337 data_vpath = vfs_path_from_str ((char *) prev->data);
3338 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3339 vfs_path_free (data_vpath);
3340 panel->dir_history_current = prev;
3342 /* skip directories that present in history but absent in file system */
3344 while (!ok);
3347 /* --------------------------------------------------------------------------------------------- */
3349 static void
3350 directory_history_list (WPanel * panel)
3352 char *s;
3353 gboolean ok = FALSE;
3354 size_t pos;
3356 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3358 s = history_show (&panel->dir_history, WIDGET (panel), pos);
3359 if (s != NULL)
3361 vfs_path_t *s_vpath;
3363 s_vpath = vfs_path_from_str (s);
3364 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3365 if (ok)
3366 directory_history_add (panel, panel->cwd_vpath);
3367 else
3368 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3369 vfs_path_free (s_vpath);
3370 g_free (s);
3373 if (!ok)
3375 /* Since history is fully modified in history_show(), panel->dir_history actually
3376 * points to the invalid place. Try restore current postition here. */
3378 size_t i;
3380 panel->dir_history_current = panel->dir_history;
3382 for (i = 0; i <= pos; i++)
3384 GList *prev;
3386 prev = g_list_previous (panel->dir_history_current);
3387 if (prev == NULL)
3388 break;
3390 panel->dir_history_current = prev;
3395 /* --------------------------------------------------------------------------------------------- */
3397 static cb_ret_t
3398 panel_execute_cmd (WPanel * panel, long command)
3400 int res = MSG_HANDLED;
3402 if (command != CK_Search)
3403 stop_search (panel);
3405 switch (command)
3407 case CK_Up:
3408 case CK_Down:
3409 case CK_Left:
3410 case CK_Right:
3411 case CK_Bottom:
3412 case CK_Top:
3413 case CK_PageDown:
3414 case CK_PageUp:
3415 /* reset state of marks flag */
3416 state_mark = -1;
3417 break;
3418 default:
3419 break;
3422 switch (command)
3424 case CK_CycleListingFormat:
3425 panel_cycle_listing_format (panel);
3426 break;
3427 case CK_PanelOtherCd:
3428 chdir_other_panel (panel);
3429 break;
3430 case CK_PanelOtherCdLink:
3431 chdir_to_readlink (panel);
3432 break;
3433 case CK_CopySingle:
3434 copy_cmd_local ();
3435 break;
3436 case CK_DeleteSingle:
3437 delete_cmd_local ();
3438 break;
3439 case CK_Enter:
3440 do_enter (panel);
3441 break;
3442 case CK_ViewRaw:
3443 view_raw_cmd ();
3444 break;
3445 case CK_EditNew:
3446 edit_cmd_new ();
3447 break;
3448 case CK_MoveSingle:
3449 rename_cmd_local ();
3450 break;
3451 case CK_SelectInvert:
3452 panel_select_invert_files (panel);
3453 break;
3454 case CK_Select:
3455 panel_select_files (panel);
3456 break;
3457 case CK_SelectExt:
3458 panel_select_ext_cmd ();
3459 break;
3460 case CK_Unselect:
3461 panel_unselect_files (panel);
3462 break;
3463 case CK_PageDown:
3464 next_page (panel);
3465 break;
3466 case CK_PageUp:
3467 prev_page (panel);
3468 break;
3469 case CK_CdChild:
3470 goto_child_dir (panel);
3471 break;
3472 case CK_CdParent:
3473 goto_parent_dir (panel);
3474 break;
3475 case CK_History:
3476 directory_history_list (panel);
3477 break;
3478 case CK_HistoryNext:
3479 directory_history_next (panel);
3480 break;
3481 case CK_HistoryPrev:
3482 directory_history_prev (panel);
3483 break;
3484 case CK_BottomOnScreen:
3485 goto_bottom_file (panel);
3486 break;
3487 case CK_MiddleOnScreen:
3488 goto_middle_file (panel);
3489 break;
3490 case CK_TopOnScreen:
3491 goto_top_file (panel);
3492 break;
3493 case CK_Mark:
3494 mark_file (panel);
3495 break;
3496 case CK_MarkUp:
3497 mark_file_up (panel);
3498 break;
3499 case CK_MarkDown:
3500 mark_file_down (panel);
3501 break;
3502 case CK_MarkLeft:
3503 mark_file_left (panel);
3504 break;
3505 case CK_MarkRight:
3506 mark_file_right (panel);
3507 break;
3508 case CK_CdParentSmart:
3509 res = force_maybe_cd ();
3510 break;
3511 case CK_Up:
3512 move_up (panel);
3513 break;
3514 case CK_Down:
3515 move_down (panel);
3516 break;
3517 case CK_Left:
3518 res = move_left (panel);
3519 break;
3520 case CK_Right:
3521 res = move_right (panel);
3522 break;
3523 case CK_Bottom:
3524 move_end (panel);
3525 break;
3526 case CK_Top:
3527 move_home (panel);
3528 break;
3529 #ifdef HAVE_CHARSET
3530 case CK_SelectCodepage:
3531 panel_change_encoding (panel);
3532 break;
3533 #endif
3534 case CK_ScrollLeft:
3535 panel_content_scroll_left (panel);
3536 break;
3537 case CK_ScrollRight:
3538 panel_content_scroll_right (panel);
3539 break;
3540 case CK_Search:
3541 start_search (panel);
3542 break;
3543 case CK_SearchStop:
3544 break;
3545 case CK_PanelOtherSync:
3546 panel_sync_other (panel);
3547 break;
3548 case CK_Sort:
3549 panel_select_sort_order (panel);
3550 break;
3551 case CK_SortPrev:
3552 panel_toggle_sort_order_prev (panel);
3553 break;
3554 case CK_SortNext:
3555 panel_toggle_sort_order_next (panel);
3556 break;
3557 case CK_SortReverse:
3558 panel->sort_info.reverse = !panel->sort_info.reverse;
3559 panel_set_sort_order (panel, panel->sort_field);
3560 break;
3561 case CK_SortByName:
3562 panel_set_sort_type_by_id (panel, "name");
3563 break;
3564 case CK_SortByExt:
3565 panel_set_sort_type_by_id (panel, "extension");
3566 break;
3567 case CK_SortBySize:
3568 panel_set_sort_type_by_id (panel, "size");
3569 break;
3570 case CK_SortByMTime:
3571 panel_set_sort_type_by_id (panel, "mtime");
3572 break;
3573 default:
3574 res = MSG_NOT_HANDLED;
3575 break;
3578 return res;
3581 /* --------------------------------------------------------------------------------------------- */
3583 static cb_ret_t
3584 panel_key (WPanel * panel, int key)
3586 size_t i;
3588 if (is_abort_char (key))
3590 stop_search (panel);
3591 return MSG_HANDLED;
3594 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3596 do_search (panel, key);
3597 return MSG_HANDLED;
3600 for (i = 0; panel_map[i].key != 0; i++)
3601 if (key == panel_map[i].key)
3602 return panel_execute_cmd (panel, panel_map[i].command);
3604 if (panels_options.torben_fj_mode && key == ALT ('h'))
3606 goto_middle_file (panel);
3607 return MSG_HANDLED;
3610 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3612 start_search (panel);
3613 do_search (panel, key);
3614 return MSG_HANDLED;
3617 return MSG_NOT_HANDLED;
3620 /* --------------------------------------------------------------------------------------------- */
3622 static cb_ret_t
3623 panel_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3625 WPanel *panel = PANEL (w);
3626 WButtonBar *bb;
3628 switch (msg)
3630 case MSG_INIT:
3631 /* subscribe to "history_load" event */
3632 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3633 /* subscribe to "history_save" event */
3634 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3635 return MSG_HANDLED;
3637 case MSG_DRAW:
3638 /* Repaint everything, including frame and separator */
3639 widget_erase (w);
3640 show_dir (panel);
3641 panel_print_header (panel);
3642 adjust_top_file (panel);
3643 paint_dir (panel);
3644 mini_info_separator (panel);
3645 display_mini_info (panel);
3646 panel->dirty = 0;
3647 return MSG_HANDLED;
3649 case MSG_FOCUS:
3650 state_mark = -1;
3651 current_panel = panel;
3652 panel->active = 1;
3654 if (mc_chdir (panel->cwd_vpath) != 0)
3656 char *cwd;
3658 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3659 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3660 cwd, unix_error_string (errno));
3661 g_free (cwd);
3663 else
3664 subshell_chdir (panel->cwd_vpath);
3666 update_xterm_title_path ();
3667 select_item (panel);
3669 bb = find_buttonbar (w->owner);
3670 midnight_set_buttonbar (bb);
3671 widget_redraw (WIDGET (bb));
3672 return MSG_HANDLED;
3674 case MSG_UNFOCUS:
3675 /* Janne: look at this for the multiple panel options */
3676 stop_search (panel);
3677 panel->active = 0;
3678 unselect_item (panel);
3679 return MSG_HANDLED;
3681 case MSG_KEY:
3682 return panel_key (panel, parm);
3684 case MSG_ACTION:
3685 return panel_execute_cmd (panel, parm);
3687 case MSG_DESTROY:
3688 vfs_stamp_path (panel->cwd_vpath);
3689 /* unsubscribe from "history_load" event */
3690 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3691 /* unsubscribe from "history_save" event */
3692 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3693 panel_destroy (panel);
3694 free_my_statfs ();
3695 return MSG_HANDLED;
3697 default:
3698 return widget_default_callback (w, sender, msg, parm, data);
3702 /* --------------------------------------------------------------------------------------------- */
3703 /* */
3704 /* Panel mouse events support routines */
3705 /* */
3707 static void
3708 mouse_toggle_mark (WPanel * panel)
3710 do_mark_file (panel, MARK_DONT_MOVE);
3711 mouse_marking = selection (panel)->f.marked;
3712 mouse_mark_panel = current_panel;
3715 /* --------------------------------------------------------------------------------------------- */
3717 static void
3718 mouse_set_mark (WPanel * panel)
3720 if (mouse_mark_panel == panel)
3722 if (mouse_marking && !(selection (panel)->f.marked))
3723 do_mark_file (panel, MARK_DONT_MOVE);
3724 else if (!mouse_marking && (selection (panel)->f.marked))
3725 do_mark_file (panel, MARK_DONT_MOVE);
3729 /* --------------------------------------------------------------------------------------------- */
3731 static gboolean
3732 mark_if_marking (WPanel * panel, const mouse_event_t * event)
3734 if ((event->buttons & GPM_B_RIGHT) != 0)
3736 if (event->msg == MSG_MOUSE_DOWN)
3737 mouse_toggle_mark (panel);
3738 else
3739 mouse_set_mark (panel);
3740 return TRUE;
3743 return FALSE;
3746 /* --------------------------------------------------------------------------------------------- */
3747 /** Determine which column was clicked, and sort the panel on
3748 * that column, or reverse sort on that column if already
3749 * sorted on that column.
3752 static void
3753 mouse_sort_col (WPanel * panel, int x)
3755 int i = 0;
3756 GSList *format = panel->format;
3757 const char *lc_sort_name = NULL;
3758 panel_field_t *col_sort_format = NULL;
3760 for (; format != NULL; format = g_slist_next (format))
3762 format_item_t *fi = (format_item_t *) format->data;
3764 i += fi->field_len;
3765 if (x < i + 1)
3767 /* found column */
3768 lc_sort_name = fi->title;
3769 break;
3773 if (lc_sort_name == NULL)
3774 return;
3776 for (i = 0; panel_fields[i].id != NULL; i++)
3778 const char *title;
3780 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3781 if (strcmp (title, lc_sort_name) == 0 && panel_fields[i].sort_routine != NULL)
3783 col_sort_format = &panel_fields[i];
3784 break;
3788 if (col_sort_format == NULL)
3789 return;
3791 if (panel->sort_field == col_sort_format)
3793 /* reverse the sort if clicked column is already the sorted column */
3794 panel->sort_info.reverse = !panel->sort_info.reverse;
3796 else
3798 /* new sort is forced to be ascending */
3799 panel->sort_info.reverse = FALSE;
3801 panel_set_sort_order (panel, col_sort_format);
3804 /* --------------------------------------------------------------------------------------------- */
3806 static void
3807 panel_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
3809 WPanel *panel = PANEL (w);
3810 gboolean is_active;
3812 is_active = widget_is_active (w);
3814 switch (msg)
3816 case MSG_MOUSE_DOWN:
3817 if (event->y == 0)
3819 /* top frame */
3820 if (event->x == 1)
3821 /* "<" button */
3822 directory_history_prev (panel);
3823 else if (event->x == w->cols - 2)
3824 /* ">" button */
3825 directory_history_next (panel);
3826 else if (event->x >= w->cols - 5 && event->x <= w->cols - 3)
3827 /* "^" button */
3828 directory_history_list (panel);
3829 else if (event->x == w->cols - 6)
3830 /* "." button show/hide hidden files */
3831 send_message (midnight_dlg, NULL, MSG_ACTION, CK_ShowHidden, NULL);
3832 else
3834 /* no other events on 1st line, return MOU_UNHANDLED */
3835 event->result.abort = TRUE;
3836 /* avoid extra panel redraw */
3837 panel->dirty = 0;
3839 break;
3842 if (event->y == 1)
3844 /* sort on clicked column */
3845 mouse_sort_col (panel, event->x + 1);
3846 break;
3849 if (!is_active)
3850 change_panel ();
3851 MC_FALLTHROUGH;
3853 case MSG_MOUSE_DRAG:
3855 int y, last, my_index;
3857 last = panel->dir.len - 1;
3858 y = event->y - 2;
3860 if (panel->top_file + y > last)
3861 my_index = last;
3862 else
3864 my_index = panel->top_file + y;
3866 if (panel->list_cols > 1)
3868 int width, lines;
3870 width = (w->cols - 2) / panel->list_cols;
3871 lines = panel_lines (panel);
3872 my_index += lines * (event->x / width);
3875 if (my_index > last)
3876 my_index = last;
3879 if (my_index != panel->selected)
3881 unselect_item (panel);
3882 panel->selected = my_index;
3883 select_item (panel);
3886 /* This one is new */
3887 mark_if_marking (panel, event);
3889 break;
3891 case MSG_MOUSE_UP:
3892 break;
3894 case MSG_MOUSE_CLICK:
3895 if ((event->count & GPM_DOUBLE) != 0)
3897 int y, lines;
3899 y = event->y - 2;
3900 lines = panel_lines (panel);
3902 if (y >= 0 && y < lines)
3903 do_enter (panel);
3905 break;
3907 case MSG_MOUSE_MOVE:
3908 break;
3910 case MSG_MOUSE_SCROLL_UP:
3911 if (is_active)
3913 if (panels_options.mouse_move_pages && panel->top_file > 0)
3914 prev_page (panel);
3915 else /* We are in first page */
3916 move_up (panel);
3918 break;
3920 case MSG_MOUSE_SCROLL_DOWN:
3921 if (is_active)
3923 if (panels_options.mouse_move_pages
3924 && panel->top_file + panel_items (panel) < panel->dir.len)
3925 next_page (panel);
3926 else /* We are in last page */
3927 move_down (panel);
3929 break;
3931 default:
3932 break;
3935 if (panel->dirty)
3936 widget_redraw (w);
3939 /* --------------------------------------------------------------------------------------------- */
3941 static void
3942 reload_panelized (WPanel * panel)
3944 int i, j;
3945 dir_list *list = &panel->dir;
3947 /* refresh current VFS directory required for vfs_path_from_str() */
3948 (void) mc_chdir (panel->cwd_vpath);
3950 for (i = 0, j = 0; i < list->len; i++)
3952 vfs_path_t *vpath;
3954 vpath = vfs_path_from_str (list->list[i].fname);
3955 if (mc_lstat (vpath, &list->list[i].st) != 0)
3956 g_free (list->list[i].fname);
3957 else
3959 if (j != i)
3960 list->list[j] = list->list[i];
3961 j++;
3963 vfs_path_free (vpath);
3965 if (j == 0)
3966 dir_list_init (list);
3967 else
3968 list->len = j;
3970 recalculate_panel_summary (panel);
3972 if (panel != current_panel)
3973 (void) mc_chdir (current_panel->cwd_vpath);
3976 /* --------------------------------------------------------------------------------------------- */
3978 static void
3979 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3981 gboolean free_pointer;
3982 char *my_current_file = NULL;
3984 if ((flags & UP_RELOAD) != 0)
3986 panel->is_panelized = FALSE;
3987 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, NULL);
3988 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3991 /* If current_file == -1 (an invalid pointer) then preserve selection */
3992 free_pointer = current_file == UP_KEEPSEL;
3994 if (free_pointer)
3996 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3997 current_file = my_current_file;
4000 if (panel->is_panelized)
4001 reload_panelized (panel);
4002 else
4003 panel_reload (panel);
4005 try_to_select (panel, current_file);
4006 panel->dirty = 1;
4008 if (free_pointer)
4009 g_free (my_current_file);
4012 /* --------------------------------------------------------------------------------------------- */
4014 static void
4015 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
4017 if (get_panel_type (which) == view_listing)
4019 WPanel *panel;
4021 panel = PANEL (get_panel_widget (which));
4022 if (panel->is_panelized)
4023 flags &= ~UP_RELOAD;
4024 update_one_panel_widget (panel, flags, current_file);
4028 /* --------------------------------------------------------------------------------------------- */
4030 static void
4031 do_select (WPanel * panel, int i)
4033 if (i != panel->selected)
4035 panel->dirty = 1;
4036 panel->selected = i;
4037 panel->top_file = panel->selected - (WIDGET (panel)->lines - 2) / 2;
4038 if (panel->top_file < 0)
4039 panel->top_file = 0;
4043 /* --------------------------------------------------------------------------------------------- */
4045 static void
4046 do_try_to_select (WPanel * panel, const char *name)
4048 int i;
4049 char *subdir;
4051 if (name == NULL)
4053 do_select (panel, 0);
4054 return;
4057 /* We only want the last component of the directory,
4058 * and from this only the name without suffix.
4059 * Cut prefix if the panel is not panelized */
4061 if (panel->is_panelized)
4062 subdir = vfs_strip_suffix_from_filename (name);
4063 else
4064 subdir = vfs_strip_suffix_from_filename (x_basename (name));
4066 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
4067 for (i = 0; i < panel->dir.len; i++)
4069 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
4071 do_select (panel, i);
4072 g_free (subdir);
4073 return;
4077 /* Try to select a file near the file that is missing */
4078 if (panel->selected >= panel->dir.len)
4079 do_select (panel, panel->dir.len - 1);
4080 g_free (subdir);
4083 /* --------------------------------------------------------------------------------------------- */
4085 /* event callback */
4086 static gboolean
4087 event_update_panels (const gchar * event_group_name, const gchar * event_name,
4088 gpointer init_data, gpointer data)
4090 (void) event_group_name;
4091 (void) event_name;
4092 (void) init_data;
4093 (void) data;
4095 update_panels (UP_RELOAD, UP_KEEPSEL);
4097 return TRUE;
4100 /* --------------------------------------------------------------------------------------------- */
4102 /* event callback */
4103 static gboolean
4104 panel_save_current_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
4105 gpointer init_data, gpointer data)
4107 (void) event_group_name;
4108 (void) event_name;
4109 (void) init_data;
4110 (void) data;
4112 if (current_panel->marked == 0)
4113 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
4114 (gpointer) selection (current_panel)->fname);
4115 else
4117 int i;
4118 gboolean first = TRUE;
4119 char *flist = NULL;
4121 for (i = 0; i < current_panel->dir.len; i++)
4122 if (current_panel->dir.list[i].f.marked != 0)
4123 { /* Skip the unmarked ones */
4124 if (first)
4126 flist = g_strdup (current_panel->dir.list[i].fname);
4127 first = FALSE;
4129 else
4131 /* Add empty lines after the file */
4132 char *tmp;
4134 tmp =
4135 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
4136 g_free (flist);
4137 flist = tmp;
4141 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
4142 g_free (flist);
4144 return TRUE;
4147 /* --------------------------------------------------------------------------------------------- */
4149 static vfs_path_t *
4150 panel_recursive_cd_to_parent (const vfs_path_t * vpath)
4152 vfs_path_t *cwd_vpath;
4154 cwd_vpath = vfs_path_clone (vpath);
4156 while (mc_chdir (cwd_vpath) < 0)
4158 const char *panel_cwd_path;
4159 vfs_path_t *tmp_vpath;
4161 /* check if path contains only '/' */
4162 panel_cwd_path = vfs_path_as_str (cwd_vpath);
4163 if (panel_cwd_path != NULL && IS_PATH_SEP (panel_cwd_path[0]) && panel_cwd_path[1] == '\0')
4164 return NULL;
4166 tmp_vpath = vfs_path_vtokens_get (cwd_vpath, 0, -1);
4167 vfs_path_free (cwd_vpath);
4168 cwd_vpath =
4169 vfs_path_build_filename (PATH_SEP_STR, vfs_path_as_str (tmp_vpath), (char *) NULL);
4170 vfs_path_free (tmp_vpath);
4173 return cwd_vpath;
4176 /* --------------------------------------------------------------------------------------------- */
4177 /*** public functions ****************************************************************************/
4178 /* --------------------------------------------------------------------------------------------- */
4180 void
4181 try_to_select (WPanel * panel, const char *name)
4183 do_try_to_select (panel, name);
4184 select_item (panel);
4187 /* --------------------------------------------------------------------------------------------- */
4189 void
4190 panel_clean_dir (WPanel * panel)
4192 panel->top_file = 0;
4193 panel->selected = 0;
4194 panel->marked = 0;
4195 panel->dirs_marked = 0;
4196 panel->total = 0;
4197 panel->searching = FALSE;
4198 panel->is_panelized = FALSE;
4199 panel->dirty = 1;
4200 panel->content_shift = -1;
4201 panel->max_shift = -1;
4203 dir_list_free_list (&panel->dir);
4206 /* --------------------------------------------------------------------------------------------- */
4208 * Set Up panel's current dir object
4210 * @param panel panel object
4211 * @param path_str string contain path
4214 void
4215 panel_set_cwd (WPanel * panel, const vfs_path_t * vpath)
4217 if (vpath != panel->cwd_vpath) /* check if new vpath is not the panel->cwd_vpath object */
4219 vfs_path_free (panel->cwd_vpath);
4220 panel->cwd_vpath = vfs_path_clone (vpath);
4224 /* --------------------------------------------------------------------------------------------- */
4226 * Set Up panel's last working dir object
4228 * @param panel panel object
4229 * @param path_str string contain path
4232 void
4233 panel_set_lwd (WPanel * panel, const vfs_path_t * vpath)
4235 if (vpath != panel->lwd_vpath) /* check if new vpath is not the panel->lwd_vpath object */
4237 vfs_path_free (panel->lwd_vpath);
4238 panel->lwd_vpath = vfs_path_clone (vpath);
4242 /* --------------------------------------------------------------------------------------------- */
4244 * Creatie an empty panel with specified size.
4246 * @param panel_name name of panel for setup retieving
4248 * @return new instance of WPanel
4251 WPanel *
4252 panel_sized_empty_new (const char *panel_name, int y, int x, int lines, int cols)
4254 WPanel *panel;
4255 Widget *w;
4256 char *section;
4257 int i, err;
4259 panel = g_new0 (WPanel, 1);
4260 w = WIDGET (panel);
4261 widget_init (w, y, x, lines, cols, panel_callback, panel_mouse_callback);
4262 w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
4264 panel->dir.size = DIR_LIST_MIN_SIZE;
4265 panel->dir.list = g_new (file_entry_t, panel->dir.size);
4266 panel->dir.len = 0;
4268 panel->list_cols = 1;
4269 panel->brief_cols = 2;
4270 panel->dirty = 1;
4271 panel->content_shift = -1;
4272 panel->max_shift = -1;
4274 panel->list_format = list_full;
4275 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4277 for (i = 0; i < LIST_FORMATS; i++)
4278 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4280 #ifdef HAVE_CHARSET
4281 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4282 #endif
4284 panel->frame_size = frame_half;
4286 panel->panel_name = g_strdup (panel_name);
4287 panel->hist_name = g_strconcat ("Dir Hist ", panel->panel_name, (char *) NULL);
4288 /* directories history will be get later */
4290 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4291 if (!mc_config_has_group (mc_global.main_config, section))
4293 g_free (section);
4294 section = g_strdup (panel->panel_name);
4296 panel_load_setup (panel, section);
4297 g_free (section);
4299 /* Load format strings */
4300 err = set_panel_formats (panel);
4301 if (err != 0)
4302 set_panel_formats (panel);
4304 return panel;
4307 /* --------------------------------------------------------------------------------------------- */
4309 * Panel creation for specified size and directory.
4311 * @param panel_name name of panel for setup retieving
4312 * @param y y coordinate of top-left corner
4313 * @param x x coordinate of top-left corner
4314 * @param lines vertical size
4315 * @param cols horizontal size
4316 * @param vpath working panel directory. If NULL then current directory is used
4318 * @return new instance of WPanel
4321 WPanel *
4322 panel_sized_with_dir_new (const char *panel_name, int y, int x, int lines, int cols,
4323 const vfs_path_t * vpath)
4325 WPanel *panel;
4326 char *curdir = NULL;
4327 #ifdef HAVE_CHARSET
4328 const vfs_path_element_t *path_element;
4329 #endif
4331 panel = panel_sized_empty_new (panel_name, y, x, lines, cols);
4333 if (vpath != NULL)
4335 curdir = _vfs_get_cwd ();
4336 panel_set_cwd (panel, vpath);
4338 else
4340 vfs_setup_cwd ();
4341 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4344 panel_set_lwd (panel, vfs_get_raw_current_dir ());
4346 #ifdef HAVE_CHARSET
4347 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4348 if (path_element->encoding != NULL)
4349 panel->codepage = get_codepage_index (path_element->encoding);
4350 #endif
4352 if (mc_chdir (panel->cwd_vpath) != 0)
4354 #ifdef HAVE_CHARSET
4355 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4356 #endif
4357 vfs_setup_cwd ();
4358 vfs_path_free (panel->cwd_vpath);
4359 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4362 /* Load the default format */
4363 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4364 &panel->sort_info, panel->filter);
4366 /* Restore old right path */
4367 if (curdir != NULL)
4369 vfs_path_t *tmp_vpath;
4370 int err;
4372 tmp_vpath = vfs_path_from_str (curdir);
4373 mc_chdir (tmp_vpath);
4374 vfs_path_free (tmp_vpath);
4375 (void) err;
4377 g_free (curdir);
4379 return panel;
4382 /* --------------------------------------------------------------------------------------------- */
4384 void
4385 panel_reload (WPanel * panel)
4387 struct stat current_stat;
4388 vfs_path_t *cwd_vpath;
4390 if (panels_options.fast_reload && stat (vfs_path_as_str (panel->cwd_vpath), &current_stat) == 0
4391 && current_stat.st_ctime == panel->dir_stat.st_ctime
4392 && current_stat.st_mtime == panel->dir_stat.st_mtime)
4393 return;
4395 cwd_vpath = panel_recursive_cd_to_parent (panel->cwd_vpath);
4396 vfs_path_free (panel->cwd_vpath);
4398 if (cwd_vpath == NULL)
4400 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4401 panel_clean_dir (panel);
4402 dir_list_init (&panel->dir);
4403 return;
4406 panel->cwd_vpath = cwd_vpath;
4407 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4408 show_dir (panel);
4410 dir_list_reload (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4411 &panel->sort_info, panel->filter);
4413 panel->dirty = 1;
4414 if (panel->selected >= panel->dir.len)
4415 do_select (panel, panel->dir.len - 1);
4417 recalculate_panel_summary (panel);
4420 /* --------------------------------------------------------------------------------------------- */
4421 /* Switches the panel to the mode specified in the format */
4422 /* Seting up both format and status string. Return: 0 - on success; */
4423 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4426 set_panel_formats (WPanel * p)
4428 GSList *form;
4429 char *err = NULL;
4430 int retcode = 0;
4432 form = use_display_format (p, panel_format (p), &err, FALSE);
4434 if (err != NULL)
4436 g_free (err);
4437 retcode = 1;
4439 else
4441 g_slist_free_full (p->format, (GDestroyNotify) format_item_free);
4442 p->format = form;
4445 if (panels_options.show_mini_info)
4447 form = use_display_format (p, mini_status_format (p), &err, TRUE);
4449 if (err != NULL)
4451 g_free (err);
4452 retcode += 2;
4454 else
4456 g_slist_free_full (p->status_format, (GDestroyNotify) format_item_free);
4457 p->status_format = form;
4461 panel_update_cols (WIDGET (p), p->frame_size);
4463 if (retcode)
4464 message (D_ERROR, _("Warning"),
4465 _("User supplied format looks invalid, reverting to default."));
4466 if (retcode & 0x01)
4468 g_free (p->user_format);
4469 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4471 if (retcode & 0x02)
4473 g_free (p->user_status_format[p->list_format]);
4474 p->user_status_format[p->list_format] = g_strdup (DEFAULT_USER_FORMAT);
4477 return retcode;
4480 /* --------------------------------------------------------------------------------------------- */
4482 /* Select current item and readjust the panel */
4483 void
4484 select_item (WPanel * panel)
4486 adjust_top_file (panel);
4488 panel->dirty = 1;
4490 execute_hooks (select_file_hook);
4493 /* --------------------------------------------------------------------------------------------- */
4494 /** Clears all files in the panel, used only when one file was marked */
4495 void
4496 unmark_files (WPanel * panel)
4498 int i;
4500 if (panel->marked == 0)
4501 return;
4503 for (i = 0; i < panel->dir.len; i++)
4504 file_mark (panel, i, 0);
4506 panel->dirs_marked = 0;
4507 panel->marked = 0;
4508 panel->total = 0;
4511 /* --------------------------------------------------------------------------------------------- */
4512 /** Recalculate the panels summary information, used e.g. when marked
4513 files might have been removed by an external command */
4515 void
4516 recalculate_panel_summary (WPanel * panel)
4518 int i;
4520 panel->marked = 0;
4521 panel->dirs_marked = 0;
4522 panel->total = 0;
4524 for (i = 0; i < panel->dir.len; i++)
4525 if (panel->dir.list[i].f.marked)
4527 /* do_file_mark will return immediately if newmark == oldmark.
4528 So we have to first unmark it to get panel's summary information
4529 updated. (Norbert) */
4530 panel->dir.list[i].f.marked = 0;
4531 do_file_mark (panel, i, 1);
4535 /* --------------------------------------------------------------------------------------------- */
4536 /** This routine marks a file or a directory */
4538 void
4539 do_file_mark (WPanel * panel, int idx, int mark)
4541 if (panel->dir.list[idx].f.marked == mark)
4542 return;
4544 /* Only '..' can't be marked, '.' isn't visible */
4545 if (DIR_IS_DOTDOT (panel->dir.list[idx].fname))
4546 return;
4548 file_mark (panel, idx, mark);
4549 if (panel->dir.list[idx].f.marked)
4551 panel->marked++;
4553 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4555 if (panel->dir.list[idx].f.dir_size_computed)
4556 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4557 panel->dirs_marked++;
4559 else
4560 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4562 set_colors (panel);
4564 else
4566 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4568 if (panel->dir.list[idx].f.dir_size_computed)
4569 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4570 panel->dirs_marked--;
4572 else
4573 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4575 panel->marked--;
4579 /* --------------------------------------------------------------------------------------------- */
4581 * Changes the current directory of the panel.
4582 * Record change in the directory history.
4584 gboolean
4585 do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4587 gboolean r;
4589 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4590 if (r)
4591 directory_history_add (panel, panel->cwd_vpath);
4592 return r;
4595 /* --------------------------------------------------------------------------------------------- */
4597 void
4598 file_mark (WPanel * panel, int lc_index, int val)
4600 if (panel->dir.list[lc_index].f.marked != val)
4602 panel->dir.list[lc_index].f.marked = val;
4603 panel->dirty = 1;
4607 /* --------------------------------------------------------------------------------------------- */
4609 void
4610 panel_re_sort (WPanel * panel)
4612 char *filename;
4613 int i;
4615 if (panel == NULL)
4616 return;
4618 filename = g_strdup (selection (panel)->fname);
4619 unselect_item (panel);
4620 dir_list_sort (&panel->dir, panel->sort_field->sort_routine, &panel->sort_info);
4621 panel->selected = -1;
4623 for (i = panel->dir.len; i != 0; i--)
4624 if (strcmp (panel->dir.list[i - 1].fname, filename) == 0)
4626 panel->selected = i - 1;
4627 break;
4630 g_free (filename);
4631 panel->top_file = panel->selected - panel_items (panel) / 2;
4632 select_item (panel);
4633 panel->dirty = 1;
4636 /* --------------------------------------------------------------------------------------------- */
4638 void
4639 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4641 if (sort_order == NULL)
4642 return;
4644 panel->sort_field = sort_order;
4646 /* The directory is already sorted, we have to load the unsorted stuff */
4647 if (sort_order->sort_routine == (GCompareFunc) unsorted)
4649 char *current_file;
4651 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4652 panel_reload (panel);
4653 try_to_select (panel, current_file);
4654 g_free (current_file);
4656 panel_re_sort (panel);
4659 /* --------------------------------------------------------------------------------------------- */
4661 #ifdef HAVE_CHARSET
4664 * Change panel encoding.
4665 * @param panel WPanel object
4668 void
4669 panel_change_encoding (WPanel * panel)
4671 const char *encoding = NULL;
4672 char *errmsg;
4673 int r;
4675 r = select_charset (-1, -1, panel->codepage, FALSE);
4677 if (r == SELECT_CHARSET_CANCEL)
4678 return; /* Cancel */
4680 panel->codepage = r;
4682 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4684 /* No translation */
4685 vfs_path_t *cd_path_vpath;
4687 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4688 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4689 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4690 show_dir (panel);
4691 vfs_path_free (cd_path_vpath);
4692 return;
4695 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4696 if (errmsg != NULL)
4698 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4699 g_free (errmsg);
4700 return;
4703 encoding = get_codepage_id (panel->codepage);
4704 if (encoding != NULL)
4706 vfs_path_change_encoding (panel->cwd_vpath, encoding);
4708 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4709 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""),
4710 vfs_path_as_str (panel->cwd_vpath));
4714 /* --------------------------------------------------------------------------------------------- */
4717 * Remove encode info from last path element.
4720 vfs_path_t *
4721 remove_encoding_from_path (const vfs_path_t * vpath)
4723 vfs_path_t *ret_vpath;
4724 GString *tmp_conv;
4725 int indx;
4727 ret_vpath = vfs_path_new ();
4729 tmp_conv = g_string_new ("");
4731 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4733 GIConv converter;
4734 vfs_path_element_t *path_element;
4736 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4738 if (path_element->encoding == NULL)
4740 vfs_path_add_element (ret_vpath, path_element);
4741 continue;
4744 converter = str_crt_conv_to (path_element->encoding);
4745 if (converter == INVALID_CONV)
4747 vfs_path_add_element (ret_vpath, path_element);
4748 continue;
4751 MC_PTR_FREE (path_element->encoding);
4753 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4755 g_free (path_element->path);
4756 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4758 g_string_set_size (tmp_conv, 0);
4760 str_close_conv (converter);
4761 str_close_conv (path_element->dir.converter);
4762 path_element->dir.converter = INVALID_CONV;
4763 vfs_path_add_element (ret_vpath, path_element);
4765 g_string_free (tmp_conv, TRUE);
4766 return ret_vpath;
4768 #endif /* HAVE_CHARSET */
4770 /* --------------------------------------------------------------------------------------------- */
4773 * This routine reloads the directory in both panels. It tries to
4774 * select current_file in current_panel and other_file in other_panel.
4775 * If current_file == -1 then it automatically sets current_file and
4776 * other_file to the currently selected files in the panels.
4778 * If flags has the UP_ONLY_CURRENT bit toggled on, then it
4779 * will not reload the other panel.
4781 * @param flags for reload panel
4782 * @param current_file name of the current file
4785 void
4786 update_panels (panel_update_flags_t flags, const char *current_file)
4788 WPanel *panel;
4790 /* first, update other panel... */
4791 if ((flags & UP_ONLY_CURRENT) == 0)
4792 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4793 /* ...then current one */
4794 update_one_panel (get_current_index (), flags, current_file);
4796 if (get_current_type () == view_listing)
4797 panel = PANEL (get_panel_widget (get_current_index ()));
4798 else
4799 panel = PANEL (get_panel_widget (get_other_index ()));
4801 if (!panel->is_panelized)
4802 (void) mc_chdir (panel->cwd_vpath);
4805 /* --------------------------------------------------------------------------------------------- */
4807 gsize
4808 panel_get_num_of_sortable_fields (void)
4810 gsize ret = 0, lc_index;
4812 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4813 if (panel_fields[lc_index].is_user_choice)
4814 ret++;
4815 return ret;
4818 /* --------------------------------------------------------------------------------------------- */
4820 char **
4821 panel_get_sortable_fields (gsize * array_size)
4823 char **ret;
4824 gsize lc_index, i;
4826 lc_index = panel_get_num_of_sortable_fields ();
4828 ret = g_try_new0 (char *, lc_index + 1);
4829 if (ret == NULL)
4830 return NULL;
4832 if (array_size != NULL)
4833 *array_size = lc_index;
4835 lc_index = 0;
4837 for (i = 0; panel_fields[i].id != NULL; i++)
4838 if (panel_fields[i].is_user_choice)
4839 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4841 return ret;
4844 /* --------------------------------------------------------------------------------------------- */
4846 const panel_field_t *
4847 panel_get_field_by_id (const char *name)
4849 gsize lc_index;
4851 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4852 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4853 return &panel_fields[lc_index];
4855 return NULL;
4858 /* --------------------------------------------------------------------------------------------- */
4860 const panel_field_t *
4861 panel_get_field_by_title_hotkey (const char *name)
4863 gsize lc_index;
4865 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4866 if (panel_fields[lc_index].title_hotkey != NULL &&
4867 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4868 return &panel_fields[lc_index];
4870 return NULL;
4873 /* --------------------------------------------------------------------------------------------- */
4875 const panel_field_t *
4876 panel_get_field_by_title (const char *name)
4878 gsize lc_index;
4880 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4882 const char *title;
4884 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4885 if (strcmp (title, name) == 0)
4886 return &panel_fields[lc_index];
4889 return NULL;
4892 /* --------------------------------------------------------------------------------------------- */
4894 gsize
4895 panel_get_num_of_user_possible_fields (void)
4897 gsize ret = 0, lc_index;
4899 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4900 if (panel_fields[lc_index].use_in_user_format)
4901 ret++;
4903 return ret;
4906 /* --------------------------------------------------------------------------------------------- */
4908 char **
4909 panel_get_user_possible_fields (gsize * array_size)
4911 char **ret;
4912 gsize lc_index, i;
4914 lc_index = panel_get_num_of_user_possible_fields ();
4916 ret = g_try_new0 (char *, lc_index + 1);
4917 if (ret == NULL)
4918 return NULL;
4920 if (array_size != NULL)
4921 *array_size = lc_index;
4923 lc_index = 0;
4925 for (i = 0; panel_fields[i].id != NULL; i++)
4926 if (panel_fields[i].use_in_user_format)
4927 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4929 return ret;
4932 /* --------------------------------------------------------------------------------------------- */
4934 void
4935 panel_init (void)
4937 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4938 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ".");
4940 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4941 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4942 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4943 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4944 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4945 panel_filename_scroll_left_char =
4946 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4947 panel_filename_scroll_right_char =
4948 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4950 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4951 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_current_file_to_clip_file",
4952 panel_save_current_file_to_clip_file, NULL, NULL);
4955 /* --------------------------------------------------------------------------------------------- */
4957 void
4958 panel_deinit (void)
4960 g_free (panel_sort_up_sign);
4961 g_free (panel_sort_down_sign);
4963 g_free (panel_hiddenfiles_sign_show);
4964 g_free (panel_hiddenfiles_sign_hide);
4965 g_free (panel_history_prev_item_sign);
4966 g_free (panel_history_next_item_sign);
4967 g_free (panel_history_show_list_sign);
4968 g_free (panel_filename_scroll_left_char);
4969 g_free (panel_filename_scroll_right_char);
4972 /* --------------------------------------------------------------------------------------------- */
4974 gboolean
4975 do_cd (const vfs_path_t * new_dir_vpath, enum cd_enum exact)
4977 gboolean res;
4978 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
4980 if (current_panel->is_panelized)
4982 size_t new_vpath_len;
4984 new_vpath_len = vfs_path_len (new_dir_vpath);
4985 if (vfs_path_equal_len (new_dir_vpath, panelized_panel.root_vpath, new_vpath_len))
4986 _new_dir_vpath = panelized_panel.root_vpath;
4989 res = do_panel_cd (current_panel, _new_dir_vpath, exact);
4991 #ifdef HAVE_CHARSET
4992 if (res)
4994 const vfs_path_element_t *path_element;
4996 path_element = vfs_path_get_by_index (current_panel->cwd_vpath, -1);
4997 if (path_element->encoding != NULL)
4998 current_panel->codepage = get_codepage_index (path_element->encoding);
4999 else
5000 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
5002 #endif /* HAVE_CHARSET */
5004 return res;
5007 /* --------------------------------------------------------------------------------------------- */