Set WOP_TOP_SELECT options for panel widgets: WPanel, WView and WTree.
[midnight-commander.git] / src / filemanager / panel.c
blob908ae37773939a3f3b58f75f17056a3260af0df6
1 /*
2 Panel managing.
4 Copyright (C) 1994-2016
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 "dir.h"
69 #include "boxes.h"
70 #include "tree.h"
71 #include "ext.h" /* regexp_command */
72 #include "layout.h" /* Most layout variables are here */
73 #include "cmd.h"
74 #include "command.h" /* cmdline */
75 #include "usermenu.h"
76 #include "midnight.h"
77 #include "mountlist.h" /* my_statfs */
79 #include "panel.h"
81 /*** global variables ****************************************************************************/
83 /* The hook list for the select file function */
84 hook_t *select_file_hook = NULL;
86 /* *INDENT-OFF* */
87 panelized_panel_t panelized_panel = { {NULL, 0, -1}, NULL };
88 /* *INDENT-ON* */
90 static const char *string_file_name (file_entry_t *, int);
91 static const char *string_file_size (file_entry_t *, int);
92 static const char *string_file_size_brief (file_entry_t *, int);
93 static const char *string_file_type (file_entry_t *, int);
94 static const char *string_file_mtime (file_entry_t *, int);
95 static const char *string_file_atime (file_entry_t *, int);
96 static const char *string_file_ctime (file_entry_t *, int);
97 static const char *string_file_permission (file_entry_t *, int);
98 static const char *string_file_perm_octal (file_entry_t *, int);
99 static const char *string_file_nlinks (file_entry_t *, int);
100 static const char *string_inode (file_entry_t *, int);
101 static const char *string_file_nuid (file_entry_t *, int);
102 static const char *string_file_ngid (file_entry_t *, int);
103 static const char *string_file_owner (file_entry_t *, int);
104 static const char *string_file_group (file_entry_t *, int);
105 static const char *string_marked (file_entry_t *, int);
106 static const char *string_space (file_entry_t *, int);
107 static const char *string_dot (file_entry_t *, int);
109 mc_fhl_t *mc_filehighlight = NULL;
111 /*** file scope macro definitions ****************************************************************/
113 #define NORMAL 0
114 #define SELECTED 1
115 #define MARKED 2
116 #define MARKED_SELECTED 3
117 #define STATUS 5
119 /*** file scope type declarations ****************************************************************/
121 typedef enum
123 MARK_DONT_MOVE = 0,
124 MARK_DOWN = 1,
125 MARK_FORCE_DOWN = 2,
126 MARK_FORCE_UP = 3
127 } mark_act_t;
130 * This describes a format item. The parse_display_format routine parses
131 * the user specified format and creates a linked list of format_e structures.
133 typedef struct format_e
135 struct format_e *next;
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_e;
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 string, it is a linked list */
389 static void
390 delete_format (format_e * format)
392 while (format != NULL)
394 format_e *next = format->next;
395 g_free (format->title);
396 g_free (format);
397 format = next;
401 /* --------------------------------------------------------------------------------------------- */
402 /** Extract the number of available lines in a panel */
404 static int
405 panel_lines (const WPanel * p)
407 /* 3 lines are: top frame, column header, botton frame */
408 return (CONST_WIDGET (p)->lines - 3 - (panels_options.show_mini_info ? 2 : 0));
411 /* --------------------------------------------------------------------------------------------- */
412 /** This code relies on the default justification!!! */
414 static void
415 add_permission_string (const char *dest, int width, file_entry_t * fe, int attr, int color,
416 gboolean is_octal)
418 int i, r, l;
420 l = get_user_permissions (&fe->st);
422 if (is_octal)
424 /* Place of the access bit in octal mode */
425 l = width + l - 3;
426 r = l + 1;
428 else
430 /* The same to the triplet in string mode */
431 l = l * 3 + 1;
432 r = l + 3;
435 for (i = 0; i < width; i++)
437 if (i >= l && i < r)
439 if (attr == SELECTED || attr == MARKED_SELECTED)
440 tty_setcolor (MARKED_SELECTED_COLOR);
441 else
442 tty_setcolor (MARKED_COLOR);
444 else if (color >= 0)
445 tty_setcolor (color);
446 else
447 tty_lowlevel_setcolor (-color);
449 tty_print_char (dest[i]);
453 /* --------------------------------------------------------------------------------------------- */
454 /** String representations of various file attributes name */
456 static const char *
457 string_file_name (file_entry_t * fe, int len)
459 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
461 (void) len;
463 g_strlcpy (buffer, fe->fname, sizeof (buffer));
464 return buffer;
467 /* --------------------------------------------------------------------------------------------- */
469 static unsigned int
470 ilog10 (dev_t n)
472 unsigned int digits = 0;
476 digits++, n /= 10;
478 while (n != 0);
480 return digits;
483 /* --------------------------------------------------------------------------------------------- */
485 static void
486 format_device_number (char *buf, size_t bufsize, dev_t dev)
488 dev_t major_dev = major (dev);
489 dev_t minor_dev = minor (dev);
490 unsigned int major_digits = ilog10 (major_dev);
491 unsigned int 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) && !fe->f.link_to_dir)
530 return _("SYMLINK");
532 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && !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 (fe->f.link_to_dir)
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 format_e *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 = format->next)
810 if (format->string_fn != NULL)
812 const char *txt = " ";
813 int len, perm = 0;
814 const char *prepared_text;
815 int name_offset = 0;
817 if (fe != NULL)
818 txt = format->string_fn (fe, format->field_len);
820 len = format->field_len;
821 if (len + length > width)
822 len = width - length;
823 if (len <= 0)
824 break;
826 if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
828 int str_len;
829 int i;
831 *field_length = len + 1;
833 str_len = str_length (txt);
834 i = MAX (0, str_len - len);
835 panel->max_shift = MAX (panel->max_shift, i);
836 i = MIN (panel->content_shift, i);
838 if (i > -1)
840 name_offset = str_offset_to_pos (txt, i);
841 if (str_len > len)
843 res = FILENAME_SCROLL_LEFT;
844 if (str_length (txt + name_offset) > len)
845 res |= FILENAME_SCROLL_RIGHT;
850 if (panels_options.permission_mode)
852 if (strcmp (format->id, "perm") == 0)
853 perm = 1;
854 else if (strcmp (format->id, "mode") == 0)
855 perm = 2;
858 if (color >= 0)
859 tty_setcolor (color);
860 else
861 tty_lowlevel_setcolor (-color);
863 if (!isstatus && panel->content_shift > -1)
864 prepared_text =
865 str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
866 else
867 prepared_text = str_fit_to_term (txt, len, format->just_mode);
869 if (perm != 0 && fe != NULL)
870 add_permission_string (prepared_text, format->field_len, fe, attr, color,
871 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_type != 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;
1083 * This is a trick to use two ngettext() calls in one sentence.
1084 * First make "N bytes", then insert it into "X in M files".
1086 g_snprintf (b_bytes, sizeof (b_bytes),
1087 ngettext ("%s byte", "%s bytes", panel->total),
1088 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1089 if (!size_only)
1090 g_snprintf (buffer, sizeof (buffer),
1091 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1092 b_bytes, panel->marked);
1094 /* don't forget spaces around buffer content */
1095 buf = str_trunc (buf, cols - 4);
1097 if (x < 0)
1098 /* center in panel */
1099 x = (w->cols - str_term_width1 (buf)) / 2 - 1;
1102 * y == panel_lines (panel) + 2 for mini_info_separator
1103 * y == w->lines - 1 for panel bottom frame
1105 widget_move (w, y, x);
1106 tty_setcolor (MARKED_COLOR);
1107 tty_printf (" %s ", buf);
1110 /* --------------------------------------------------------------------------------------------- */
1112 static void
1113 mini_info_separator (const WPanel * panel)
1115 if (panels_options.show_mini_info)
1117 const Widget *w = CONST_WIDGET (panel);
1118 int y;
1120 y = panel_lines (panel) + 2;
1122 tty_setcolor (NORMAL_COLOR);
1123 tty_draw_hline (w->y + y, w->x + 1, ACS_HLINE, w->cols - 2);
1124 /* Status displays total marked size.
1125 * Centered in panel, full format. */
1126 display_total_marked_size (panel, y, -1, FALSE);
1130 /* --------------------------------------------------------------------------------------------- */
1132 static void
1133 show_free_space (const WPanel * panel)
1135 /* Used to figure out how many free space we have */
1136 static struct my_statfs myfs_stats;
1137 /* Old current working directory for displaying free space */
1138 static char *old_cwd = NULL;
1140 /* Don't try to stat non-local fs */
1141 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1142 return;
1144 if (old_cwd == NULL || strcmp (old_cwd, vfs_path_as_str (panel->cwd_vpath)) != 0)
1146 char rpath[PATH_MAX];
1148 init_my_statfs ();
1149 g_free (old_cwd);
1150 old_cwd = g_strdup (vfs_path_as_str (panel->cwd_vpath));
1152 if (mc_realpath (old_cwd, rpath) == NULL)
1153 return;
1155 my_statfs (&myfs_stats, rpath);
1158 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1160 const Widget *w = CONST_WIDGET (panel);
1161 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1163 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1164 panels_options.kilobyte_si);
1165 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1166 panels_options.kilobyte_si);
1167 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1168 myfs_stats.total == 0 ? 0 :
1169 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1170 widget_move (w, w->lines - 1, w->cols - 2 - (int) strlen (tmp));
1171 tty_setcolor (NORMAL_COLOR);
1172 tty_print_string (tmp);
1176 /* --------------------------------------------------------------------------------------------- */
1178 * Prepare path string for showing in panel's header.
1179 * Passwords will removed, also home dir will replaced by ~
1181 * @param panel WPanel object
1183 * @return newly allocated string.
1186 static char *
1187 panel_correct_path_to_show (const WPanel * panel)
1189 vfs_path_t *last_vpath;
1190 const vfs_path_element_t *path_element;
1191 char *return_path;
1192 int elements_count;
1194 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1196 /* get last path element */
1197 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1199 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1200 strcmp (path_element->class->name, "extfs") == 0 ||
1201 strcmp (path_element->class->name, "tarfs") == 0))
1203 const char *archive_name;
1204 const vfs_path_element_t *prev_path_element;
1206 /* get previous path element for catching archive name */
1207 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1208 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1209 if (archive_name != NULL)
1210 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1211 else
1213 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1214 last_vpath->relative = TRUE;
1217 else
1219 last_vpath = vfs_path_new ();
1220 last_vpath->relative = TRUE;
1223 vfs_path_add_element (last_vpath, path_element);
1224 return_path =
1225 vfs_path_to_str_flags (last_vpath, 0,
1226 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1227 vfs_path_free (last_vpath);
1229 return return_path;
1232 /* --------------------------------------------------------------------------------------------- */
1234 * Get Current path element encoding
1236 * @param panel WPanel object
1238 * @return newly allocated string or NULL if path charset is same as system charset
1241 #ifdef HAVE_CHARSET
1242 static char *
1243 panel_get_encoding_info_str (const WPanel * panel)
1245 char *ret_str = NULL;
1246 const vfs_path_element_t *path_element;
1248 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1249 if (path_element->encoding != NULL)
1250 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1252 return ret_str;
1254 #endif
1256 /* --------------------------------------------------------------------------------------------- */
1258 static void
1259 show_dir (const WPanel * panel)
1261 const Widget *w = CONST_WIDGET (panel);
1263 gchar *tmp;
1265 set_colors (panel);
1266 tty_draw_box (w->y, w->x, w->lines, w->cols, FALSE);
1268 if (panels_options.show_mini_info)
1270 int y;
1272 y = panel_lines (panel) + 2;
1274 widget_move (w, y, 0);
1275 tty_print_alt_char (ACS_LTEE, FALSE);
1276 widget_move (w, y, w->cols - 1);
1277 tty_print_alt_char (ACS_RTEE, FALSE);
1280 widget_move (w, 0, 1);
1281 tty_print_string (panel_history_prev_item_sign);
1283 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1284 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1285 panel_history_next_item_sign);
1287 widget_move (w, 0, w->cols - 6);
1288 tty_print_string (tmp);
1290 g_free (tmp);
1292 widget_move (w, 0, 3);
1294 if (panel->is_panelized)
1295 tty_printf (" %s ", _("Panelize"));
1296 #ifdef HAVE_CHARSET
1297 else
1299 tmp = panel_get_encoding_info_str (panel);
1300 if (tmp != NULL)
1302 tty_printf ("%s", tmp);
1303 widget_move (w, 0, 3 + strlen (tmp));
1304 g_free (tmp);
1307 #endif
1309 if (panel->active)
1310 tty_setcolor (REVERSE_COLOR);
1312 tmp = panel_correct_path_to_show (panel);
1313 tty_printf (" %s ", str_term_trim (tmp, MIN (MAX (w->cols - 12, 0), w->cols)));
1314 g_free (tmp);
1316 if (!panels_options.show_mini_info)
1318 if (panel->marked == 0)
1320 /* Show size of curret file in the bottom of panel */
1321 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1323 char buffer[BUF_SMALL];
1325 g_snprintf (buffer, sizeof (buffer), " %s ",
1326 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1327 panels_options.kilobyte_si));
1328 tty_setcolor (NORMAL_COLOR);
1329 widget_move (w, w->lines - 1, 4);
1330 tty_print_string (buffer);
1333 else
1335 /* Show total size of marked files
1336 * In the bottom of panel, display size only. */
1337 display_total_marked_size (panel, w->lines - 1, 2, TRUE);
1341 show_free_space (panel);
1343 if (panel->active)
1344 tty_set_normal_attrs ();
1347 /* --------------------------------------------------------------------------------------------- */
1349 static void
1350 adjust_top_file (WPanel * panel)
1352 int items;
1354 items = panel_items (panel);
1356 if (panel->dir.len <= items)
1358 /* If all files fit, show them all. */
1359 panel->top_file = 0;
1361 else
1363 int i;
1365 /* top_file has to be in the range [selected-items+1, selected] so that
1366 the selected file is visible.
1367 top_file should be in the range [0, count-items] so that there's
1368 no empty space wasted.
1369 Within these ranges, adjust it by as little as possible. */
1371 if (panel->top_file < 0)
1372 panel->top_file = 0;
1374 i = panel->selected - items + 1;
1375 if (panel->top_file < i)
1376 panel->top_file = i;
1378 i = panel->dir.len - items;
1379 if (panel->top_file > i)
1380 panel->top_file = i;
1382 if (panel->top_file > panel->selected)
1383 panel->top_file = panel->selected;
1387 /* --------------------------------------------------------------------------------------------- */
1388 /** add "#enc:encodning" to end of path */
1389 /* if path end width a previous #enc:, only encoding is changed no additional
1390 * #enc: is appended
1391 * retun new string
1394 static char *
1395 panel_save_name (WPanel * panel)
1397 /* If the program is shuting down */
1398 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1399 return g_strdup (panel->panel_name);
1401 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1404 /* --------------------------------------------------------------------------------------------- */
1406 static void
1407 directory_history_add (WPanel * panel, const vfs_path_t * vpath)
1409 char *tmp;
1411 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1412 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1413 panel->dir_history_current = panel->dir_history;
1416 /* --------------------------------------------------------------------------------------------- */
1418 /* "history_load" event handler */
1419 static gboolean
1420 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1421 gpointer init_data, gpointer data)
1423 WPanel *p = PANEL (init_data);
1424 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1426 (void) event_group_name;
1427 (void) event_name;
1429 if (ev->receiver == NULL || ev->receiver == WIDGET (p))
1431 if (ev->cfg != NULL)
1432 p->dir_history = history_load (ev->cfg, p->hist_name);
1433 else
1434 p->dir_history = history_get (p->hist_name);
1436 directory_history_add (p, p->cwd_vpath);
1439 return TRUE;
1442 /* --------------------------------------------------------------------------------------------- */
1444 /* "history_save" event handler */
1445 static gboolean
1446 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1447 gpointer init_data, gpointer data)
1449 WPanel *p = PANEL (init_data);
1451 (void) event_group_name;
1452 (void) event_name;
1454 if (p->dir_history != NULL)
1456 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1458 history_save (ev->cfg, p->hist_name, p->dir_history);
1461 return TRUE;
1464 /* --------------------------------------------------------------------------------------------- */
1466 static void
1467 panel_destroy (WPanel * p)
1469 size_t i;
1471 if (panels_options.auto_save_setup)
1473 char *name;
1475 name = panel_save_name (p);
1476 panel_save_setup (p, name);
1477 g_free (name);
1480 panel_clean_dir (p);
1482 /* clean history */
1483 if (p->dir_history != NULL)
1485 /* directory history is already saved before this moment */
1486 p->dir_history = g_list_first (p->dir_history);
1487 g_list_free_full (p->dir_history, g_free);
1489 g_free (p->hist_name);
1491 delete_format (p->format);
1492 delete_format (p->status_format);
1494 g_free (p->user_format);
1495 for (i = 0; i < LIST_TYPES; i++)
1496 g_free (p->user_status_format[i]);
1498 g_free (p->dir.list);
1499 g_free (p->panel_name);
1501 vfs_path_free (p->lwd_vpath);
1502 vfs_path_free (p->cwd_vpath);
1505 /* --------------------------------------------------------------------------------------------- */
1507 static inline void
1508 panel_format_modified (WPanel * panel)
1510 panel->format_modified = 1;
1513 /* --------------------------------------------------------------------------------------------- */
1515 static void
1516 panel_paint_sort_info (const WPanel * panel)
1518 if (*panel->sort_field->hotkey != '\0')
1520 const char *sort_sign =
1521 panel->sort_info.reverse ? panel_sort_up_sign : panel_sort_down_sign;
1522 char *str;
1524 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_field->hotkey));
1525 widget_move (panel, 1, 1);
1526 tty_print_string (str);
1527 g_free (str);
1531 /* --------------------------------------------------------------------------------------------- */
1533 static const char *
1534 panel_get_title_without_hotkey (const char *title)
1536 static char translated_title[BUF_TINY];
1538 if (title == NULL || title[0] == '\0')
1539 translated_title[0] = '\0';
1540 else
1542 char *hkey;
1544 g_snprintf (translated_title, sizeof (translated_title), "%s", _(title));
1546 hkey = strchr (translated_title, '&');
1547 if (hkey != NULL && hkey[1] != '\0')
1548 memmove (hkey, hkey + 1, strlen (hkey));
1551 return translated_title;
1554 /* --------------------------------------------------------------------------------------------- */
1556 static void
1557 panel_print_header (const WPanel * panel)
1559 const Widget *w = CONST_WIDGET (panel);
1561 int y, x;
1562 int i;
1563 GString *format_txt;
1565 widget_move (w, 1, 1);
1566 tty_getyx (&y, &x);
1567 tty_setcolor (NORMAL_COLOR);
1568 tty_draw_hline (y, x, ' ', w->cols - 2);
1570 format_txt = g_string_new ("");
1572 for (i = 0; i < panel->list_cols; i++)
1574 format_e *format;
1576 for (format = panel->format; format != NULL; format = format->next)
1578 if (format->string_fn != NULL)
1580 g_string_set_size (format_txt, 0);
1582 if (panel->list_type == list_long
1583 && strcmp (format->id, panel->sort_field->id) == 0)
1584 g_string_append (format_txt,
1585 panel->sort_info.reverse
1586 ? panel_sort_up_sign : panel_sort_down_sign);
1588 g_string_append (format_txt, format->title);
1590 if (panel->filter != NULL && *panel->filter != '\0'
1591 && strcmp (format->id, "name") == 0)
1593 g_string_append (format_txt, " [");
1594 g_string_append (format_txt, panel->filter);
1595 g_string_append (format_txt, "]");
1598 tty_setcolor (HEADER_COLOR);
1599 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1600 J_CENTER_LEFT));
1602 else
1604 tty_setcolor (NORMAL_COLOR);
1605 tty_print_one_vline (TRUE);
1609 if (i < panel->list_cols - 1)
1611 tty_setcolor (NORMAL_COLOR);
1612 tty_print_one_vline (TRUE);
1616 g_string_free (format_txt, TRUE);
1618 if (panel->list_type != list_long)
1619 panel_paint_sort_info (panel);
1622 /* --------------------------------------------------------------------------------------------- */
1624 static const char *
1625 parse_panel_size (WPanel * panel, const char *format, gboolean isstatus)
1627 panel_display_t frame = frame_half;
1628 format = skip_separators (format);
1630 if (strncmp (format, "full", 4) == 0)
1632 frame = frame_full;
1633 format += 4;
1635 else if (strncmp (format, "half", 4) == 0)
1637 frame = frame_half;
1638 format += 4;
1641 if (!isstatus)
1643 panel->frame_size = frame;
1644 panel->list_cols = 1;
1647 /* Now, the optional column specifier */
1648 format = skip_separators (format);
1650 if (g_ascii_isdigit (*format))
1652 if (!isstatus && panel->list_type == list_brief)
1654 panel->brief_cols = g_ascii_digit_value (*format);
1655 if (panel->brief_cols < 1)
1656 panel->brief_cols = 1;
1658 panel->list_cols = panel->brief_cols;
1661 format++;
1664 if (!isstatus)
1665 panel_update_cols (WIDGET (panel), panel->frame_size);
1667 return skip_separators (format);
1670 /* Format is:
1672 all := panel_format? format
1673 panel_format := [full|half] [1-9]
1674 format := one_format_e
1675 | format , one_format_e
1677 one_format_e := just format.id [opt_size]
1678 just := [<=>]
1679 opt_size := : size [opt_expand]
1680 size := [0-9]+
1681 opt_expand := +
1685 /* --------------------------------------------------------------------------------------------- */
1687 static format_e *
1688 parse_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus,
1689 int *res_total_cols)
1691 format_e *darr, *old = NULL, *home = NULL; /* The formats we return */
1692 int total_cols = 0; /* Used columns by the format */
1693 size_t i;
1695 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1697 *error = NULL;
1699 if (i18n_timelength == 0)
1701 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1703 for (i = 0; panel_fields[i].id != NULL; i++)
1704 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1705 panel_fields[i].min_size = i18n_timelength;
1709 * This makes sure that the panel and mini status full/half mode
1710 * setting is equal
1712 format = parse_panel_size (panel, format, isstatus);
1714 while (*format != '\0')
1715 { /* format can be an empty string */
1716 align_crt_t justify; /* Which mode. */
1717 gboolean set_justify = TRUE; /* flag: set justification mode? */
1718 gboolean found = FALSE;
1720 darr = g_new0 (format_e, 1);
1722 /* I'm so ugly, don't look at me :-) */
1723 if (home == NULL)
1724 home = old = darr;
1726 old->next = darr;
1727 darr->next = NULL;
1728 old = darr;
1730 format = skip_separators (format);
1732 switch (*format)
1734 case '<':
1735 justify = J_LEFT;
1736 format = skip_separators (format + 1);
1737 break;
1738 case '=':
1739 justify = J_CENTER;
1740 format = skip_separators (format + 1);
1741 break;
1742 case '>':
1743 justify = J_RIGHT;
1744 format = skip_separators (format + 1);
1745 break;
1746 default:
1747 justify = J_LEFT;
1748 set_justify = FALSE;
1749 break;
1752 for (i = 0; panel_fields[i].id != NULL; i++)
1754 size_t klen;
1756 klen = strlen (panel_fields[i].id);
1758 if (strncmp (format, panel_fields[i].id, klen) != 0)
1759 continue;
1761 format += klen;
1763 darr->requested_field_len = panel_fields[i].min_size;
1764 darr->string_fn = panel_fields[i].string_fn;
1765 darr->title = g_strdup (panel_get_title_without_hotkey (panel_fields[i].title_hotkey));
1767 darr->id = panel_fields[i].id;
1768 darr->expand = panel_fields[i].expands;
1769 darr->just_mode = panel_fields[i].default_just;
1771 if (set_justify)
1773 if (IS_FIT (darr->just_mode))
1774 darr->just_mode = MAKE_FIT (justify);
1775 else
1776 darr->just_mode = justify;
1778 found = TRUE;
1780 format = skip_separators (format);
1782 /* If we have a size specifier */
1783 if (*format == ':')
1785 int req_length;
1787 /* If the size was specified, we don't want
1788 * auto-expansion by default
1790 darr->expand = FALSE;
1791 format++;
1792 req_length = atoi (format);
1793 darr->requested_field_len = req_length;
1795 format = skip_numbers (format);
1797 /* Now, if they insist on expansion */
1798 if (*format == '+')
1800 darr->expand = TRUE;
1801 format++;
1806 break;
1809 if (!found)
1811 size_t pos;
1812 char *tmp_format;
1814 pos = strlen (format);
1815 if (pos > 8)
1816 pos = 8;
1818 tmp_format = g_strndup (format, pos);
1819 delete_format (home);
1820 *error =
1821 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1822 g_free (tmp_format);
1824 return NULL;
1827 total_cols += darr->requested_field_len;
1830 *res_total_cols = total_cols;
1831 return home;
1834 /* --------------------------------------------------------------------------------------------- */
1836 static format_e *
1837 use_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus)
1839 #define MAX_EXPAND 4
1840 int expand_top = 0; /* Max used element in expand */
1841 int usable_columns; /* Usable columns in the panel */
1842 int total_cols = 0;
1843 format_e *darr, *home;
1845 if (format == NULL)
1846 format = DEFAULT_USER_FORMAT;
1848 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1850 if (*error != NULL)
1851 return NULL;
1853 panel->dirty = 1;
1855 usable_columns = WIDGET (panel)->cols - 2;
1856 /* Status needn't to be split */
1857 if (!isstatus)
1859 usable_columns /= panel->list_cols;
1860 if (panel->list_cols > 1)
1861 usable_columns--;
1864 /* Look for the expandable fields and set field_len based on the requested field len */
1865 for (darr = home; darr != NULL && expand_top < MAX_EXPAND; darr = darr->next)
1867 darr->field_len = darr->requested_field_len;
1868 if (darr->expand)
1869 expand_top++;
1872 /* If we used more columns than the available columns, adjust that */
1873 if (total_cols > usable_columns)
1875 int dif;
1876 int pdif = 0;
1878 dif = total_cols - usable_columns;
1880 while (dif != 0 && pdif != dif)
1882 pdif = dif;
1884 for (darr = home; darr; darr = darr->next)
1885 if (dif != 0 && darr->field_len != 1)
1887 darr->field_len--;
1888 dif--;
1892 total_cols = usable_columns; /* give up, the rest should be truncated */
1895 /* Expand the available space */
1896 if (usable_columns > total_cols && expand_top != 0)
1898 int i;
1899 int spaces = (usable_columns - total_cols) / expand_top;
1901 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1902 if (darr->expand)
1904 darr->field_len += spaces;
1905 if (i == 0)
1906 darr->field_len += (usable_columns - total_cols) % expand_top;
1907 i++;
1910 return home;
1913 /* --------------------------------------------------------------------------------------------- */
1914 /** Given the panel->view_type returns the format string to be parsed */
1916 static const char *
1917 panel_format (WPanel * panel)
1920 switch (panel->list_type)
1922 case list_long:
1923 return "full perm space nlink space owner space group space size space mtime space name";
1925 case list_brief:
1927 static char format[BUF_TINY];
1928 int brief_cols = panel->brief_cols;
1930 if (brief_cols < 1)
1931 brief_cols = 2;
1933 if (brief_cols > 9)
1934 brief_cols = 9;
1936 g_snprintf (format, sizeof (format), "half %d type name", brief_cols);
1937 return format;
1940 case list_user:
1941 return panel->user_format;
1943 default:
1944 case list_full:
1945 return "half type name | size | mtime";
1949 /* --------------------------------------------------------------------------------------------- */
1951 static const char *
1952 mini_status_format (WPanel * panel)
1954 if (panel->user_mini_status)
1955 return panel->user_status_format[panel->list_type];
1957 switch (panel->list_type)
1959 case list_long:
1960 return "full perm space nlink space owner space group space size space mtime space name";
1962 case list_brief:
1963 return "half type name space bsize space perm space";
1965 case list_full:
1966 return "half type name";
1968 default:
1969 case list_user:
1970 return panel->user_format;
1974 /* */
1975 /* Panel operation commands */
1976 /* */
1978 /* --------------------------------------------------------------------------------------------- */
1979 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1981 static cb_ret_t
1982 maybe_cd (gboolean move_up_dir)
1984 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1986 if (move_up_dir)
1988 vfs_path_t *up_dir;
1990 up_dir = vfs_path_from_str ("..");
1991 do_cd (up_dir, cd_exact);
1992 vfs_path_free (up_dir);
1993 return MSG_HANDLED;
1996 if (S_ISDIR (selection (current_panel)->st.st_mode)
1997 || link_isdir (selection (current_panel)))
1999 vfs_path_t *vpath;
2001 vpath = vfs_path_from_str (selection (current_panel)->fname);
2002 do_cd (vpath, cd_exact);
2003 vfs_path_free (vpath);
2004 return MSG_HANDLED;
2008 return MSG_NOT_HANDLED;
2011 /* --------------------------------------------------------------------------------------------- */
2013 /* if command line is empty then do 'cd ..' */
2014 static cb_ret_t
2015 force_maybe_cd (void)
2017 if (cmdline->buffer[0] == '\0')
2019 vfs_path_t *up_dir = vfs_path_from_str ("..");
2020 do_cd (up_dir, cd_exact);
2021 vfs_path_free (up_dir);
2022 return MSG_HANDLED;
2025 return MSG_NOT_HANDLED;
2028 /* --------------------------------------------------------------------------------------------- */
2030 static inline void
2031 unselect_item (WPanel * panel)
2033 repaint_file (panel, panel->selected, TRUE, 2 * selection (panel)->f.marked, FALSE);
2036 /* --------------------------------------------------------------------------------------------- */
2037 /** Select/unselect all the files like a current file by extension */
2039 static void
2040 panel_select_ext_cmd (void)
2042 gboolean do_select = !selection (current_panel)->f.marked;
2043 char *filename = selection (current_panel)->fname;
2044 char *reg_exp, *cur_file_ext;
2045 mc_search_t *search;
2046 int i;
2048 if (filename == NULL)
2049 return;
2051 cur_file_ext = strutils_regex_escape (extension (filename));
2053 if (cur_file_ext[0] != '\0')
2054 reg_exp = g_strconcat ("^.*\\.", cur_file_ext, "$", (char *) NULL);
2055 else
2056 reg_exp = g_strdup ("^[^\\.]+$");
2058 g_free (cur_file_ext);
2060 search = mc_search_new (reg_exp, NULL);
2061 search->search_type = MC_SEARCH_T_REGEX;
2062 search->is_case_sensitive = FALSE;
2064 for (i = 0; i < current_panel->dir.len; i++)
2066 file_entry_t *file_entry = &current_panel->dir.list[i];
2068 if (DIR_IS_DOTDOT (file_entry->fname) || S_ISDIR (file_entry->st.st_mode))
2069 continue;
2071 if (!mc_search_run (search, file_entry->fname, 0, file_entry->fnamelen, NULL))
2072 continue;
2074 do_file_mark (current_panel, i, do_select);
2077 mc_search_free (search);
2078 g_free (reg_exp);
2082 /* --------------------------------------------------------------------------------------------- */
2084 static int
2085 panel_selected_at_half (const WPanel * panel)
2087 int lines, top;
2089 lines = panel_lines (panel);
2091 /* define top file of column */
2092 top = panel->top_file;
2093 if (panel->list_cols > 1)
2094 top += lines * ((panel->selected - top) / lines);
2096 return (panel->selected - top - lines / 2);
2099 /* --------------------------------------------------------------------------------------------- */
2101 static void
2102 move_down (WPanel * panel)
2104 int items;
2106 if (panel->selected + 1 == panel->dir.len)
2107 return;
2109 unselect_item (panel);
2110 panel->selected++;
2112 items = panel_items (panel);
2114 if (panels_options.scroll_pages && panel->selected - panel->top_file == items)
2116 /* Scroll window half screen */
2117 panel->top_file += items / 2;
2118 if (panel->top_file > panel->dir.len - items)
2119 panel->top_file = panel->dir.len - items;
2120 paint_dir (panel);
2122 else if (panels_options.scroll_center && panel_selected_at_half (panel) > 0)
2124 /* Scroll window when cursor is halfway down */
2125 panel->top_file++;
2126 if (panel->top_file > panel->dir.len - items)
2127 panel->top_file = panel->dir.len - items;
2129 select_item (panel);
2132 /* --------------------------------------------------------------------------------------------- */
2134 static void
2135 move_up (WPanel * panel)
2137 if (panel->selected == 0)
2138 return;
2140 unselect_item (panel);
2141 panel->selected--;
2143 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2145 /* Scroll window half screen */
2146 panel->top_file -= panel_items (panel) / 2;
2147 if (panel->top_file < 0)
2148 panel->top_file = 0;
2149 paint_dir (panel);
2151 else if (panels_options.scroll_center && panel_selected_at_half (panel) < 0)
2153 /* Scroll window when cursor is halfway up */
2154 panel->top_file--;
2155 if (panel->top_file < 0)
2156 panel->top_file = 0;
2158 select_item (panel);
2161 /* --------------------------------------------------------------------------------------------- */
2162 /** Changes the selection by lines (may be negative) */
2164 static void
2165 move_selection (WPanel * panel, int lines)
2167 int new_pos;
2168 gboolean adjust = FALSE;
2170 new_pos = panel->selected + lines;
2171 if (new_pos >= panel->dir.len)
2172 new_pos = panel->dir.len - 1;
2174 if (new_pos < 0)
2175 new_pos = 0;
2177 unselect_item (panel);
2178 panel->selected = new_pos;
2180 if (panel->selected - panel->top_file >= panel_items (panel))
2182 panel->top_file += lines;
2183 adjust = TRUE;
2186 if (panel->selected - panel->top_file < 0)
2188 panel->top_file += lines;
2189 adjust = TRUE;
2192 if (adjust)
2194 if (panel->top_file > panel->selected)
2195 panel->top_file = panel->selected;
2196 if (panel->top_file < 0)
2197 panel->top_file = 0;
2198 paint_dir (panel);
2200 select_item (panel);
2203 /* --------------------------------------------------------------------------------------------- */
2205 static cb_ret_t
2206 move_left (WPanel * panel)
2208 if (panel->list_cols > 1)
2210 move_selection (panel, -panel_lines (panel));
2211 return MSG_HANDLED;
2214 return maybe_cd (TRUE); /* cd .. */
2217 /* --------------------------------------------------------------------------------------------- */
2219 static cb_ret_t
2220 move_right (WPanel * panel)
2222 if (panel->list_cols > 1)
2224 move_selection (panel, panel_lines (panel));
2225 return MSG_HANDLED;
2228 return maybe_cd (FALSE); /* cd (selection) */
2231 /* --------------------------------------------------------------------------------------------- */
2233 static void
2234 prev_page (WPanel * panel)
2236 int items;
2238 if (!panel->selected && !panel->top_file)
2239 return;
2241 unselect_item (panel);
2242 items = panel_items (panel);
2243 if (panel->top_file < items)
2244 items = panel->top_file;
2245 if (items == 0)
2246 panel->selected = 0;
2247 else
2248 panel->selected -= items;
2249 panel->top_file -= items;
2251 select_item (panel);
2252 paint_dir (panel);
2255 /* --------------------------------------------------------------------------------------------- */
2257 static void
2258 goto_parent_dir (WPanel * panel)
2260 if (!panel->is_panelized)
2262 vfs_path_t *up_dir;
2264 up_dir = vfs_path_from_str ("..");
2265 do_cd (up_dir, cd_exact);
2266 vfs_path_free (up_dir);
2268 else
2270 char *fname = panel->dir.list[panel->selected].fname;
2271 const char *bname;
2272 vfs_path_t *dname_vpath;
2274 if (g_path_is_absolute (fname))
2275 fname = g_strdup (fname);
2276 else
2277 fname =
2278 mc_build_filename (vfs_path_as_str (panelized_panel.root_vpath), fname,
2279 (char *) NULL);
2281 bname = x_basename (fname);
2283 if (bname == fname)
2284 dname_vpath = vfs_path_from_str (".");
2285 else
2287 char *dname;
2289 dname = g_strndup (fname, bname - fname);
2290 dname_vpath = vfs_path_from_str (dname);
2291 g_free (dname);
2294 do_cd (dname_vpath, cd_exact);
2295 try_to_select (panel, bname);
2297 vfs_path_free (dname_vpath);
2298 g_free (fname);
2302 /* --------------------------------------------------------------------------------------------- */
2304 static void
2305 next_page (WPanel * panel)
2307 int items;
2309 if (panel->selected == panel->dir.len - 1)
2310 return;
2312 unselect_item (panel);
2313 items = panel_items (panel);
2314 if (panel->top_file > panel->dir.len - 2 * items)
2315 items = panel->dir.len - items - panel->top_file;
2316 if (panel->top_file + items < 0)
2317 items = -panel->top_file;
2318 if (items == 0)
2319 panel->selected = panel->dir.len - 1;
2320 else
2321 panel->selected += items;
2322 panel->top_file += items;
2324 select_item (panel);
2325 paint_dir (panel);
2328 /* --------------------------------------------------------------------------------------------- */
2330 static void
2331 goto_child_dir (WPanel * panel)
2333 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2335 vfs_path_t *vpath;
2337 vpath = vfs_path_from_str (selection (panel)->fname);
2338 do_cd (vpath, cd_exact);
2339 vfs_path_free (vpath);
2343 /* --------------------------------------------------------------------------------------------- */
2345 static void
2346 goto_top_file (WPanel * panel)
2348 unselect_item (panel);
2349 panel->selected = panel->top_file;
2350 select_item (panel);
2353 /* --------------------------------------------------------------------------------------------- */
2355 static void
2356 goto_middle_file (WPanel * panel)
2358 unselect_item (panel);
2359 panel->selected = panel->top_file + panel_items (panel) / 2;
2360 select_item (panel);
2363 /* --------------------------------------------------------------------------------------------- */
2365 static void
2366 goto_bottom_file (WPanel * panel)
2368 unselect_item (panel);
2369 panel->selected = panel->top_file + panel_items (panel) - 1;
2370 select_item (panel);
2373 /* --------------------------------------------------------------------------------------------- */
2375 static void
2376 move_home (WPanel * panel)
2378 if (panel->selected == 0)
2379 return;
2381 unselect_item (panel);
2383 if (panels_options.torben_fj_mode)
2385 int middle_pos = panel->top_file + panel_items (panel) / 2;
2387 if (panel->selected > middle_pos)
2389 goto_middle_file (panel);
2390 return;
2392 if (panel->selected != panel->top_file)
2394 goto_top_file (panel);
2395 return;
2399 panel->top_file = 0;
2400 panel->selected = 0;
2402 paint_dir (panel);
2403 select_item (panel);
2406 /* --------------------------------------------------------------------------------------------- */
2408 static void
2409 move_end (WPanel * panel)
2411 if (panel->selected == panel->dir.len - 1)
2412 return;
2414 unselect_item (panel);
2416 if (panels_options.torben_fj_mode)
2418 int items, middle_pos;
2420 items = panel_items (panel);
2421 middle_pos = panel->top_file + items / 2;
2423 if (panel->selected < middle_pos)
2425 goto_middle_file (panel);
2426 return;
2428 if (panel->selected != panel->top_file + items - 1)
2430 goto_bottom_file (panel);
2431 return;
2435 panel->selected = panel->dir.len - 1;
2436 paint_dir (panel);
2437 select_item (panel);
2440 /* --------------------------------------------------------------------------------------------- */
2442 static void
2443 do_mark_file (WPanel * panel, mark_act_t do_move)
2445 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2446 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2447 move_down (panel);
2448 else if (do_move == MARK_FORCE_UP)
2449 move_up (panel);
2452 /* --------------------------------------------------------------------------------------------- */
2454 static inline void
2455 mark_file (WPanel * panel)
2457 do_mark_file (panel, MARK_DOWN);
2460 /* --------------------------------------------------------------------------------------------- */
2462 static inline void
2463 mark_file_up (WPanel * panel)
2465 do_mark_file (panel, MARK_FORCE_UP);
2468 /* --------------------------------------------------------------------------------------------- */
2470 static inline void
2471 mark_file_down (WPanel * panel)
2473 do_mark_file (panel, MARK_FORCE_DOWN);
2476 /* --------------------------------------------------------------------------------------------- */
2478 static void
2479 mark_file_right (WPanel * panel)
2481 int lines;
2483 if (state_mark < 0)
2484 state_mark = selection (panel)->f.marked ? 0 : 1;
2486 lines = panel_lines (panel);
2487 lines = MIN (lines, panel->dir.len - panel->selected - 1);
2488 for (; lines != 0; lines--)
2490 do_file_mark (panel, panel->selected, state_mark);
2491 move_down (panel);
2493 do_file_mark (panel, panel->selected, state_mark);
2496 /* --------------------------------------------------------------------------------------------- */
2498 static void
2499 mark_file_left (WPanel * panel)
2501 int lines;
2503 if (state_mark < 0)
2504 state_mark = selection (panel)->f.marked ? 0 : 1;
2506 lines = panel_lines (panel);
2507 lines = MIN (lines, panel->selected + 1);
2508 for (; lines != 0; lines--)
2510 do_file_mark (panel, panel->selected, state_mark);
2511 move_up (panel);
2513 do_file_mark (panel, panel->selected, state_mark);
2516 /* --------------------------------------------------------------------------------------------- */
2518 static void
2519 panel_select_unselect_files (WPanel * panel, const char *title, const char *history_name,
2520 gboolean do_select)
2522 int files_only = (panels_options.select_flags & SELECT_FILES_ONLY) != 0;
2523 int case_sens = (panels_options.select_flags & SELECT_MATCH_CASE) != 0;
2524 int shell_patterns = (panels_options.select_flags & SELECT_SHELL_PATTERNS) != 0;
2526 char *reg_exp;
2527 mc_search_t *search;
2528 int i;
2530 quick_widget_t quick_widgets[] = {
2531 /* *INDENT-OFF* */
2532 QUICK_INPUT (INPUT_LAST_TEXT, history_name, &reg_exp, NULL,
2533 FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
2534 QUICK_START_COLUMNS,
2535 QUICK_CHECKBOX (N_("&Files only"), &files_only, NULL),
2536 QUICK_CHECKBOX (N_("&Using shell patterns"), &shell_patterns, NULL),
2537 QUICK_NEXT_COLUMN,
2538 QUICK_CHECKBOX (N_("&Case sensitive"), &case_sens, NULL),
2539 QUICK_STOP_COLUMNS,
2540 QUICK_END
2541 /* *INDENT-ON* */
2544 quick_dialog_t qdlg = {
2545 -1, -1, 50,
2546 title, "[Select/Unselect Files]",
2547 quick_widgets, NULL, NULL
2550 if (quick_dialog (&qdlg) == B_CANCEL)
2551 return;
2553 if (reg_exp == NULL || *reg_exp == '\0')
2555 g_free (reg_exp);
2556 return;
2559 search = mc_search_new (reg_exp, NULL);
2560 search->search_type = (shell_patterns != 0) ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
2561 search->is_entire_line = TRUE;
2562 search->is_case_sensitive = case_sens != 0;
2564 for (i = 0; i < panel->dir.len; i++)
2566 if (DIR_IS_DOTDOT (panel->dir.list[i].fname))
2567 continue;
2568 if (S_ISDIR (panel->dir.list[i].st.st_mode) && files_only != 0)
2569 continue;
2571 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2572 do_file_mark (panel, i, do_select);
2575 mc_search_free (search);
2576 g_free (reg_exp);
2578 /* result flags */
2579 panels_options.select_flags = 0;
2580 if (case_sens != 0)
2581 panels_options.select_flags |= SELECT_MATCH_CASE;
2582 if (files_only != 0)
2583 panels_options.select_flags |= SELECT_FILES_ONLY;
2584 if (shell_patterns != 0)
2585 panels_options.select_flags |= SELECT_SHELL_PATTERNS;
2588 /* --------------------------------------------------------------------------------------------- */
2590 static void
2591 panel_select_files (WPanel * panel)
2593 panel_select_unselect_files (panel, _("Select"), ":select_cmd: Select ", TRUE);
2596 /* --------------------------------------------------------------------------------------------- */
2598 static void
2599 panel_unselect_files (WPanel * panel)
2601 panel_select_unselect_files (panel, _("Unselect"), ":unselect_cmd: Unselect ", FALSE);
2604 /* --------------------------------------------------------------------------------------------- */
2606 static void
2607 panel_select_invert_files (WPanel * panel)
2609 int i;
2611 for (i = 0; i < panel->dir.len; i++)
2613 file_entry_t *file = &panel->dir.list[i];
2615 if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
2616 do_file_mark (panel, i, !file->f.marked);
2620 /* --------------------------------------------------------------------------------------------- */
2621 /** Incremental search of a file name in the panel.
2622 * @param panel instance of WPanel structure
2623 * @param c_code key code
2626 static void
2627 do_search (WPanel * panel, int c_code)
2629 size_t l;
2630 int i, sel;
2631 gboolean wrapped = FALSE;
2632 char *act;
2633 mc_search_t *search;
2634 char *reg_exp, *esc_str;
2635 gboolean is_found = FALSE;
2637 l = strlen (panel->search_buffer);
2638 if (c_code == KEY_BACKSPACE)
2640 if (l != 0)
2642 act = panel->search_buffer + l;
2643 str_prev_noncomb_char (&act, panel->search_buffer);
2644 act[0] = '\0';
2646 panel->search_chpoint = 0;
2648 else
2650 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2652 panel->search_char[panel->search_chpoint] = c_code;
2653 panel->search_chpoint++;
2656 if (panel->search_chpoint > 0)
2658 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2660 case -2:
2661 return;
2662 case -1:
2663 panel->search_chpoint = 0;
2664 return;
2665 default:
2666 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2668 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2669 l += panel->search_chpoint;
2670 *(panel->search_buffer + l) = '\0';
2671 panel->search_chpoint = 0;
2677 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2678 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2679 search = mc_search_new (esc_str, NULL);
2680 search->search_type = MC_SEARCH_T_GLOB;
2681 search->is_entire_line = TRUE;
2683 switch (panels_options.qsearch_mode)
2685 case QSEARCH_CASE_SENSITIVE:
2686 search->is_case_sensitive = TRUE;
2687 break;
2688 case QSEARCH_CASE_INSENSITIVE:
2689 search->is_case_sensitive = FALSE;
2690 break;
2691 default:
2692 search->is_case_sensitive = panel->sort_info.case_sensitive;
2693 break;
2696 sel = panel->selected;
2698 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2700 if (i >= panel->dir.len)
2702 i = 0;
2703 if (wrapped)
2704 break;
2705 wrapped = TRUE;
2707 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2709 sel = i;
2710 is_found = TRUE;
2711 break;
2714 if (is_found)
2716 unselect_item (panel);
2717 panel->selected = sel;
2718 select_item (panel);
2719 widget_redraw (WIDGET (panel));
2721 else if (c_code != KEY_BACKSPACE)
2723 act = panel->search_buffer + l;
2724 str_prev_noncomb_char (&act, panel->search_buffer);
2725 act[0] = '\0';
2727 mc_search_free (search);
2728 g_free (reg_exp);
2729 g_free (esc_str);
2732 /* --------------------------------------------------------------------------------------------- */
2733 /** Start new search.
2734 * @param panel instance of WPanel structure
2737 static void
2738 start_search (WPanel * panel)
2740 if (panel->searching)
2742 if (panel->selected + 1 == panel->dir.len)
2743 panel->selected = 0;
2744 else
2745 move_down (panel);
2747 /* in case if there was no search string we need to recall
2748 previous string, with which we ended previous searching */
2749 if (panel->search_buffer[0] == '\0')
2750 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2751 sizeof (panel->search_buffer));
2753 do_search (panel, 0);
2755 else
2757 panel->searching = TRUE;
2758 panel->search_buffer[0] = '\0';
2759 panel->search_char[0] = '\0';
2760 panel->search_chpoint = 0;
2761 display_mini_info (panel);
2762 mc_refresh ();
2766 /* --------------------------------------------------------------------------------------------- */
2768 static void
2769 stop_search (WPanel * panel)
2771 panel->searching = FALSE;
2773 /* if user had overrdied search string, we need to store it
2774 to the previous_search_buffer */
2775 if (panel->search_buffer[0] != '\0')
2776 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2777 sizeof (panel->prev_search_buffer));
2779 display_mini_info (panel);
2782 /* --------------------------------------------------------------------------------------------- */
2783 /** Return TRUE if the Enter key has been processed, FALSE otherwise */
2785 static gboolean
2786 do_enter_on_file_entry (file_entry_t * fe)
2788 vfs_path_t *full_name_vpath;
2789 gboolean ok;
2792 * Directory or link to directory - change directory.
2793 * Try the same for the entries on which mc_lstat() has failed.
2795 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2797 vfs_path_t *fname_vpath;
2799 fname_vpath = vfs_path_from_str (fe->fname);
2800 if (!do_cd (fname_vpath, cd_exact))
2801 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2802 vfs_path_free (fname_vpath);
2803 return TRUE;
2806 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, (char *) NULL);
2808 /* Try associated command */
2809 ok = regex_command (full_name_vpath, "Open") != 0;
2810 vfs_path_free (full_name_vpath);
2811 if (ok)
2812 return TRUE;
2814 /* Check if the file is executable */
2815 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, (char *) NULL);
2816 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2817 vfs_path_free (full_name_vpath);
2818 if (!ok)
2819 return FALSE;
2821 if (confirm_execute)
2823 if (query_dialog
2824 (_("The Midnight Commander"),
2825 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2826 return TRUE;
2829 if (!vfs_current_is_local ())
2831 int ret;
2832 vfs_path_t *tmp_vpath;
2834 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, (char *) NULL);
2835 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2836 vfs_path_free (tmp_vpath);
2837 /* We took action only if the dialog was shown or the execution
2838 * was successful */
2839 return confirm_execute || (ret == 0);
2843 char *tmp, *cmd;
2845 tmp = name_quote (fe->fname, FALSE);
2846 cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2847 g_free (tmp);
2848 shell_execute (cmd, 0);
2849 g_free (cmd);
2852 #ifdef HAVE_CHARSET
2853 mc_global.source_codepage = default_source_codepage;
2854 #endif
2856 return TRUE;
2859 /* --------------------------------------------------------------------------------------------- */
2861 static inline gboolean
2862 do_enter (WPanel * panel)
2864 return do_enter_on_file_entry (selection (panel));
2867 /* --------------------------------------------------------------------------------------------- */
2869 static void
2870 chdir_other_panel (WPanel * panel)
2872 const file_entry_t *entry = &panel->dir.list[panel->selected];
2874 vfs_path_t *new_dir_vpath;
2875 char *sel_entry = NULL;
2877 if (get_other_type () != view_listing)
2878 set_display_type (get_other_index (), view_listing);
2880 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2881 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, (char *) NULL);
2882 else
2884 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2885 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2888 change_panel ();
2889 do_cd (new_dir_vpath, cd_exact);
2890 vfs_path_free (new_dir_vpath);
2892 if (sel_entry)
2893 try_to_select (current_panel, sel_entry);
2894 change_panel ();
2896 move_down (panel);
2899 /* --------------------------------------------------------------------------------------------- */
2901 * Make the current directory of the current panel also the current
2902 * directory of the other panel. Put the other panel to the listing
2903 * mode if needed. If the current panel is panelized, the other panel
2904 * doesn't become panelized.
2907 static void
2908 panel_sync_other (const WPanel * panel)
2910 if (get_other_type () != view_listing)
2911 set_display_type (get_other_index (), view_listing);
2913 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2915 /* try to select current filename on the other panel */
2916 if (!panel->is_panelized)
2917 try_to_select (other_panel, selection (panel)->fname);
2920 /* --------------------------------------------------------------------------------------------- */
2922 static void
2923 chdir_to_readlink (WPanel * panel)
2925 vfs_path_t *new_dir_vpath;
2926 char buffer[MC_MAXPATHLEN];
2927 int i;
2928 struct stat st;
2929 vfs_path_t *panel_fname_vpath;
2930 gboolean ok;
2932 if (get_other_type () != view_listing)
2933 return;
2935 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2936 return;
2938 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2939 if (i < 0)
2940 return;
2942 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2943 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2944 vfs_path_free (panel_fname_vpath);
2945 if (!ok)
2946 return;
2948 buffer[i] = 0;
2949 if (!S_ISDIR (st.st_mode))
2951 char *p;
2953 p = strrchr (buffer, PATH_SEP);
2954 if (p && !p[1])
2956 *p = 0;
2957 p = strrchr (buffer, PATH_SEP);
2959 if (!p)
2960 return;
2961 p[1] = 0;
2963 if (IS_PATH_SEP (*buffer))
2964 new_dir_vpath = vfs_path_from_str (buffer);
2965 else
2966 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, (char *) NULL);
2968 change_panel ();
2969 do_cd (new_dir_vpath, cd_exact);
2970 vfs_path_free (new_dir_vpath);
2971 change_panel ();
2973 move_down (panel);
2976 /* --------------------------------------------------------------------------------------------- */
2978 static gsize
2979 panel_get_format_field_count (WPanel * panel)
2981 format_e *format;
2982 gsize lc_index;
2984 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++)
2987 return lc_index;
2990 /* --------------------------------------------------------------------------------------------- */
2992 function return 0 if not found and REAL_INDEX+1 if found
2995 static gsize
2996 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2998 format_e *format;
2999 gsize lc_index;
3001 for (lc_index = 1, format = panel->format;
3002 format != NULL && strcmp (format->title, name) != 0; format = format->next, lc_index++)
3005 if (format == NULL)
3006 lc_index = 0;
3008 return lc_index;
3011 /* --------------------------------------------------------------------------------------------- */
3013 static format_e *
3014 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
3016 format_e *format;
3018 for (format = panel->format; format != NULL && lc_index != 0; format = format->next, lc_index--)
3021 return format;
3024 /* --------------------------------------------------------------------------------------------- */
3026 static const panel_field_t *
3027 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
3029 const panel_field_t *pfield;
3030 format_e *format;
3032 format = panel_get_format_field_by_index (panel, lc_index);
3033 if (format == NULL)
3034 return NULL;
3036 pfield = panel_get_field_by_title (format->title);
3037 if (pfield == NULL)
3038 return NULL;
3039 if (pfield->sort_routine == NULL)
3040 return NULL;
3041 return pfield;
3044 /* --------------------------------------------------------------------------------------------- */
3046 static void
3047 panel_toggle_sort_order_prev (WPanel * panel)
3049 gsize lc_index, i;
3050 const char *title;
3051 const panel_field_t *pfield = NULL;
3053 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3054 lc_index = panel_get_format_field_index_by_name (panel, title);
3056 if (lc_index > 1)
3058 /* search for prev sortable column in panel format */
3059 for (i = lc_index - 1;
3060 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3064 if (pfield == NULL)
3066 /* Sortable field not found. Try to search in each array */
3067 for (i = panel_get_format_field_count (panel);
3068 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3072 if (pfield != NULL)
3074 panel->sort_field = pfield;
3075 panel_set_sort_order (panel, pfield);
3079 /* --------------------------------------------------------------------------------------------- */
3081 static void
3082 panel_toggle_sort_order_next (WPanel * panel)
3084 gsize lc_index, i;
3085 const panel_field_t *pfield = NULL;
3086 gsize format_field_count;
3087 const char *title;
3089 format_field_count = panel_get_format_field_count (panel);
3090 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3091 lc_index = panel_get_format_field_index_by_name (panel, title);
3093 if (lc_index != 0 && lc_index != format_field_count)
3095 /* search for prev sortable column in panel format */
3096 for (i = lc_index;
3097 i != format_field_count
3098 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
3102 if (pfield == NULL)
3104 /* Sortable field not found. Try to search in each array */
3105 for (i = 0;
3106 i != format_field_count
3107 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
3111 if (pfield != NULL)
3113 panel->sort_field = pfield;
3114 panel_set_sort_order (panel, pfield);
3118 /* --------------------------------------------------------------------------------------------- */
3120 static void
3121 panel_select_sort_order (WPanel * panel)
3123 const panel_field_t *sort_order;
3125 sort_order = sort_box (&panel->sort_info, panel->sort_field);
3126 if (sort_order != NULL)
3128 panel->sort_field = sort_order;
3129 panel_set_sort_order (panel, sort_order);
3133 /* --------------------------------------------------------------------------------------------- */
3136 * panel_content_scroll_left:
3137 * @param panel the pointer to the panel on which we operate
3139 * scroll long filename to the left (decrement scroll pointer)
3143 static void
3144 panel_content_scroll_left (WPanel * panel)
3146 if (panel->content_shift > -1)
3148 if (panel->content_shift > panel->max_shift)
3149 panel->content_shift = panel->max_shift;
3151 panel->content_shift--;
3152 show_dir (panel);
3153 paint_dir (panel);
3157 /* --------------------------------------------------------------------------------------------- */
3160 * panel_content_scroll_right:
3161 * @param panel the pointer to the panel on which we operate
3163 * scroll long filename to the right (increment scroll pointer)
3167 static void
3168 panel_content_scroll_right (WPanel * panel)
3170 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
3172 panel->content_shift++;
3173 show_dir (panel);
3174 paint_dir (panel);
3178 /* --------------------------------------------------------------------------------------------- */
3180 static void
3181 panel_set_sort_type_by_id (WPanel * panel, const char *name)
3183 if (strcmp (panel->sort_field->id, name) != 0)
3185 const panel_field_t *sort_order;
3187 sort_order = panel_get_field_by_id (name);
3188 if (sort_order == NULL)
3189 return;
3191 panel->sort_field = sort_order;
3193 else
3194 panel->sort_info.reverse = !panel->sort_info.reverse;
3196 panel_set_sort_order (panel, panel->sort_field);
3199 /* --------------------------------------------------------------------------------------------- */
3201 * If we moved to the parent directory move the selection pointer to
3202 * the old directory name; If we leave VFS dir, remove FS specificator.
3204 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
3207 static const char *
3208 get_parent_dir_name (const vfs_path_t * cwd_vpath, const vfs_path_t * lwd_vpath)
3210 size_t llen, clen;
3211 const char *p, *cwd, *lwd;
3213 llen = vfs_path_len (lwd_vpath);
3214 clen = vfs_path_len (cwd_vpath);
3216 if (llen <= clen)
3217 return NULL;
3219 cwd = vfs_path_as_str (cwd_vpath);
3220 lwd = vfs_path_as_str (lwd_vpath);
3222 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
3224 if (p == NULL)
3226 p = strrchr (lwd, PATH_SEP);
3228 if ((p != NULL)
3229 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
3230 && (clen == (size_t) (p - lwd) || (p == lwd && IS_PATH_SEP (cwd[0]) && cwd[1] == '\0')))
3231 return (p + 1);
3233 return NULL;
3236 /* skip VFS prefix */
3237 while (--p > lwd && !IS_PATH_SEP (*p))
3239 /* get last component */
3240 while (--p > lwd && !IS_PATH_SEP (*p))
3243 /* return last component */
3244 return (p != lwd || IS_PATH_SEP (*p)) ? p + 1 : p;
3247 /* --------------------------------------------------------------------------------------------- */
3248 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3250 static void
3251 subshell_chdir (const vfs_path_t * vpath)
3253 #ifdef ENABLE_SUBSHELL
3254 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3255 do_subshell_chdir (vpath, FALSE);
3256 #else /* ENABLE_SUBSHELL */
3257 (void) vpath;
3258 #endif /* ENABLE_SUBSHELL */
3261 /* --------------------------------------------------------------------------------------------- */
3263 * Changes the current directory of the panel.
3264 * Don't record change in the directory history.
3267 static gboolean
3268 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3270 vfs_path_t *olddir_vpath;
3272 /* Convert *new_path to a suitable pathname, handle ~user */
3273 if (cd_type == cd_parse_command)
3275 const vfs_path_element_t *element;
3277 element = vfs_path_get_by_index (new_dir_vpath, 0);
3278 if (strcmp (element->path, "-") == 0)
3279 new_dir_vpath = panel->lwd_vpath;
3282 if (mc_chdir (new_dir_vpath) == -1)
3283 return FALSE;
3285 /* Success: save previous directory, shutdown status of previous dir */
3286 olddir_vpath = vfs_path_clone (panel->cwd_vpath);
3287 panel_set_lwd (panel, panel->cwd_vpath);
3288 input_free_completions (cmdline);
3290 vfs_path_free (panel->cwd_vpath);
3291 vfs_setup_cwd ();
3292 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3294 vfs_release_path (olddir_vpath);
3296 subshell_chdir (panel->cwd_vpath);
3298 /* Reload current panel */
3299 panel_clean_dir (panel);
3301 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
3302 &panel->sort_info, panel->filter);
3303 try_to_select (panel, get_parent_dir_name (panel->cwd_vpath, olddir_vpath));
3305 load_hint (FALSE);
3306 panel->dirty = 1;
3307 update_xterm_title_path ();
3309 vfs_path_free (olddir_vpath);
3311 return TRUE;
3314 /* --------------------------------------------------------------------------------------------- */
3316 static void
3317 directory_history_next (WPanel * panel)
3319 gboolean ok;
3323 GList *next;
3325 ok = TRUE;
3326 next = g_list_next (panel->dir_history_current);
3327 if (next != NULL)
3329 vfs_path_t *data_vpath;
3331 data_vpath = vfs_path_from_str ((char *) next->data);
3332 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3333 vfs_path_free (data_vpath);
3334 panel->dir_history_current = next;
3336 /* skip directories that present in history but absent in file system */
3338 while (!ok);
3341 /* --------------------------------------------------------------------------------------------- */
3343 static void
3344 directory_history_prev (WPanel * panel)
3346 gboolean ok;
3350 GList *prev;
3352 ok = TRUE;
3353 prev = g_list_previous (panel->dir_history_current);
3354 if (prev != NULL)
3356 vfs_path_t *data_vpath;
3358 data_vpath = vfs_path_from_str ((char *) prev->data);
3359 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3360 vfs_path_free (data_vpath);
3361 panel->dir_history_current = prev;
3363 /* skip directories that present in history but absent in file system */
3365 while (!ok);
3368 /* --------------------------------------------------------------------------------------------- */
3370 static void
3371 directory_history_list (WPanel * panel)
3373 char *s;
3374 gboolean ok = FALSE;
3375 size_t pos;
3377 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3379 s = history_show (&panel->dir_history, WIDGET (panel), pos);
3380 if (s != NULL)
3382 vfs_path_t *s_vpath;
3384 s_vpath = vfs_path_from_str (s);
3385 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3386 if (ok)
3387 directory_history_add (panel, panel->cwd_vpath);
3388 else
3389 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3390 vfs_path_free (s_vpath);
3391 g_free (s);
3394 if (!ok)
3396 /* Since history is fully modified in history_show(), panel->dir_history actually
3397 * points to the invalid place. Try restore current postition here. */
3399 size_t i;
3401 panel->dir_history_current = panel->dir_history;
3403 for (i = 0; i <= pos; i++)
3405 GList *prev;
3407 prev = g_list_previous (panel->dir_history_current);
3408 if (prev == NULL)
3409 break;
3411 panel->dir_history_current = prev;
3416 /* --------------------------------------------------------------------------------------------- */
3418 static cb_ret_t
3419 panel_execute_cmd (WPanel * panel, long command)
3421 int res = MSG_HANDLED;
3423 if (command != CK_Search)
3424 stop_search (panel);
3426 switch (command)
3428 case CK_Up:
3429 case CK_Down:
3430 case CK_Left:
3431 case CK_Right:
3432 case CK_Bottom:
3433 case CK_Top:
3434 case CK_PageDown:
3435 case CK_PageUp:
3436 /* reset state of marks flag */
3437 state_mark = -1;
3438 break;
3439 default:
3440 break;
3443 switch (command)
3445 case CK_PanelOtherCd:
3446 chdir_other_panel (panel);
3447 break;
3448 case CK_PanelOtherCdLink:
3449 chdir_to_readlink (panel);
3450 break;
3451 case CK_CopySingle:
3452 copy_cmd_local ();
3453 break;
3454 case CK_DeleteSingle:
3455 delete_cmd_local ();
3456 break;
3457 case CK_Enter:
3458 do_enter (panel);
3459 break;
3460 case CK_ViewRaw:
3461 view_raw_cmd ();
3462 break;
3463 case CK_EditNew:
3464 edit_cmd_new ();
3465 break;
3466 case CK_MoveSingle:
3467 rename_cmd_local ();
3468 break;
3469 case CK_SelectInvert:
3470 panel_select_invert_files (panel);
3471 break;
3472 case CK_Select:
3473 panel_select_files (panel);
3474 break;
3475 case CK_SelectExt:
3476 panel_select_ext_cmd ();
3477 break;
3478 case CK_Unselect:
3479 panel_unselect_files (panel);
3480 break;
3481 case CK_PageDown:
3482 next_page (panel);
3483 break;
3484 case CK_PageUp:
3485 prev_page (panel);
3486 break;
3487 case CK_CdChild:
3488 goto_child_dir (panel);
3489 break;
3490 case CK_CdParent:
3491 goto_parent_dir (panel);
3492 break;
3493 case CK_History:
3494 directory_history_list (panel);
3495 break;
3496 case CK_HistoryNext:
3497 directory_history_next (panel);
3498 break;
3499 case CK_HistoryPrev:
3500 directory_history_prev (panel);
3501 break;
3502 case CK_BottomOnScreen:
3503 goto_bottom_file (panel);
3504 break;
3505 case CK_MiddleOnScreen:
3506 goto_middle_file (panel);
3507 break;
3508 case CK_TopOnScreen:
3509 goto_top_file (panel);
3510 break;
3511 case CK_Mark:
3512 mark_file (panel);
3513 break;
3514 case CK_MarkUp:
3515 mark_file_up (panel);
3516 break;
3517 case CK_MarkDown:
3518 mark_file_down (panel);
3519 break;
3520 case CK_MarkLeft:
3521 mark_file_left (panel);
3522 break;
3523 case CK_MarkRight:
3524 mark_file_right (panel);
3525 break;
3526 case CK_CdParentSmart:
3527 res = force_maybe_cd ();
3528 break;
3529 case CK_Up:
3530 move_up (panel);
3531 break;
3532 case CK_Down:
3533 move_down (panel);
3534 break;
3535 case CK_Left:
3536 res = move_left (panel);
3537 break;
3538 case CK_Right:
3539 res = move_right (panel);
3540 break;
3541 case CK_Bottom:
3542 move_end (panel);
3543 break;
3544 case CK_Top:
3545 move_home (panel);
3546 break;
3547 #ifdef HAVE_CHARSET
3548 case CK_SelectCodepage:
3549 panel_change_encoding (panel);
3550 break;
3551 #endif
3552 case CK_ScrollLeft:
3553 panel_content_scroll_left (panel);
3554 break;
3555 case CK_ScrollRight:
3556 panel_content_scroll_right (panel);
3557 break;
3558 case CK_Search:
3559 start_search (panel);
3560 break;
3561 case CK_SearchStop:
3562 break;
3563 case CK_PanelOtherSync:
3564 panel_sync_other (panel);
3565 break;
3566 case CK_Sort:
3567 panel_select_sort_order (panel);
3568 break;
3569 case CK_SortPrev:
3570 panel_toggle_sort_order_prev (panel);
3571 break;
3572 case CK_SortNext:
3573 panel_toggle_sort_order_next (panel);
3574 break;
3575 case CK_SortReverse:
3576 panel->sort_info.reverse = !panel->sort_info.reverse;
3577 panel_set_sort_order (panel, panel->sort_field);
3578 break;
3579 case CK_SortByName:
3580 panel_set_sort_type_by_id (panel, "name");
3581 break;
3582 case CK_SortByExt:
3583 panel_set_sort_type_by_id (panel, "extension");
3584 break;
3585 case CK_SortBySize:
3586 panel_set_sort_type_by_id (panel, "size");
3587 break;
3588 case CK_SortByMTime:
3589 panel_set_sort_type_by_id (panel, "mtime");
3590 break;
3591 default:
3592 res = MSG_NOT_HANDLED;
3593 break;
3596 return res;
3599 /* --------------------------------------------------------------------------------------------- */
3601 static cb_ret_t
3602 panel_key (WPanel * panel, int key)
3604 size_t i;
3606 if (is_abort_char (key))
3608 stop_search (panel);
3609 return MSG_HANDLED;
3612 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3614 do_search (panel, key);
3615 return MSG_HANDLED;
3618 for (i = 0; panel_map[i].key != 0; i++)
3619 if (key == panel_map[i].key)
3620 return panel_execute_cmd (panel, panel_map[i].command);
3622 if (panels_options.torben_fj_mode && key == ALT ('h'))
3624 goto_middle_file (panel);
3625 return MSG_HANDLED;
3628 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3630 start_search (panel);
3631 do_search (panel, key);
3632 return MSG_HANDLED;
3635 return MSG_NOT_HANDLED;
3638 /* --------------------------------------------------------------------------------------------- */
3640 static cb_ret_t
3641 panel_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3643 WPanel *panel = PANEL (w);
3644 WButtonBar *bb;
3646 switch (msg)
3648 case MSG_INIT:
3649 /* subscribe to "history_load" event */
3650 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3651 /* subscribe to "history_save" event */
3652 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3653 return MSG_HANDLED;
3655 case MSG_DRAW:
3656 /* Repaint everything, including frame and separator */
3657 widget_erase (w);
3658 show_dir (panel);
3659 panel_print_header (panel);
3660 adjust_top_file (panel);
3661 paint_dir (panel);
3662 mini_info_separator (panel);
3663 display_mini_info (panel);
3664 panel->dirty = 0;
3665 return MSG_HANDLED;
3667 case MSG_FOCUS:
3668 state_mark = -1;
3669 current_panel = panel;
3670 panel->active = 1;
3672 if (mc_chdir (panel->cwd_vpath) != 0)
3674 char *cwd;
3676 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3677 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3678 cwd, unix_error_string (errno));
3679 g_free (cwd);
3681 else
3682 subshell_chdir (panel->cwd_vpath);
3684 update_xterm_title_path ();
3685 select_item (panel);
3686 show_dir (panel);
3687 paint_dir (panel);
3688 panel->dirty = 0;
3690 bb = find_buttonbar (w->owner);
3691 midnight_set_buttonbar (bb);
3692 widget_redraw (WIDGET (bb));
3693 return MSG_HANDLED;
3695 case MSG_UNFOCUS:
3696 /* Janne: look at this for the multiple panel options */
3697 stop_search (panel);
3698 panel->active = 0;
3699 show_dir (panel);
3700 unselect_item (panel);
3701 return MSG_HANDLED;
3703 case MSG_KEY:
3704 return panel_key (panel, parm);
3706 case MSG_ACTION:
3707 return panel_execute_cmd (panel, parm);
3709 case MSG_DESTROY:
3710 /* unsubscribe from "history_load" event */
3711 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3712 /* unsubscribe from "history_save" event */
3713 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3714 panel_destroy (panel);
3715 free_my_statfs ();
3716 return MSG_HANDLED;
3718 default:
3719 return widget_default_callback (w, sender, msg, parm, data);
3723 /* --------------------------------------------------------------------------------------------- */
3724 /* */
3725 /* Panel mouse events support routines */
3726 /* */
3728 static void
3729 mouse_toggle_mark (WPanel * panel)
3731 do_mark_file (panel, MARK_DONT_MOVE);
3732 mouse_marking = selection (panel)->f.marked;
3733 mouse_mark_panel = current_panel;
3736 /* --------------------------------------------------------------------------------------------- */
3738 static void
3739 mouse_set_mark (WPanel * panel)
3741 if (mouse_mark_panel == panel)
3743 if (mouse_marking && !(selection (panel)->f.marked))
3744 do_mark_file (panel, MARK_DONT_MOVE);
3745 else if (!mouse_marking && (selection (panel)->f.marked))
3746 do_mark_file (panel, MARK_DONT_MOVE);
3750 /* --------------------------------------------------------------------------------------------- */
3752 static gboolean
3753 mark_if_marking (WPanel * panel, const mouse_event_t * event)
3755 if ((event->buttons & GPM_B_RIGHT) != 0)
3757 if (event->msg == MSG_MOUSE_DOWN)
3758 mouse_toggle_mark (panel);
3759 else
3760 mouse_set_mark (panel);
3761 return TRUE;
3764 return FALSE;
3767 /* --------------------------------------------------------------------------------------------- */
3768 /** Determine which column was clicked, and sort the panel on
3769 * that column, or reverse sort on that column if already
3770 * sorted on that column.
3773 static void
3774 mouse_sort_col (WPanel * panel, int x)
3776 int i;
3777 const char *lc_sort_name = NULL;
3778 panel_field_t *col_sort_format = NULL;
3779 format_e *format;
3781 for (i = 0, format = panel->format; format != NULL; format = format->next)
3783 i += format->field_len;
3784 if (x < i + 1)
3786 /* found column */
3787 lc_sort_name = format->title;
3788 break;
3792 if (lc_sort_name == NULL)
3793 return;
3795 for (i = 0; panel_fields[i].id != NULL; i++)
3797 const char *title;
3799 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3800 if (strcmp (title, lc_sort_name) == 0 && panel_fields[i].sort_routine != NULL)
3802 col_sort_format = &panel_fields[i];
3803 break;
3807 if (col_sort_format == NULL)
3808 return;
3810 if (panel->sort_field == col_sort_format)
3812 /* reverse the sort if clicked column is already the sorted column */
3813 panel->sort_info.reverse = !panel->sort_info.reverse;
3815 else
3817 /* new sort is forced to be ascending */
3818 panel->sort_info.reverse = FALSE;
3820 panel_set_sort_order (panel, col_sort_format);
3823 /* --------------------------------------------------------------------------------------------- */
3825 static void
3826 panel_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
3828 WPanel *panel = PANEL (w);
3829 gboolean is_active;
3831 is_active = widget_is_active (w);
3833 switch (msg)
3835 case MSG_MOUSE_DOWN:
3836 if (event->y == 0)
3838 /* top frame */
3839 if (event->x == 1)
3840 /* "<" button */
3841 directory_history_prev (panel);
3842 else if (event->x == w->cols - 2)
3843 /* ">" button */
3844 directory_history_next (panel);
3845 else if (event->x >= w->cols - 5 && event->x <= w->cols - 3)
3846 /* "^" button */
3847 directory_history_list (panel);
3848 else if (event->x == w->cols - 6)
3849 /* "." button show/hide hidden files */
3850 send_message (midnight_dlg, NULL, MSG_ACTION, CK_ShowHidden, NULL);
3851 else
3853 /* no other events on 1st line, return MOU_UNHANDLED */
3854 event->result.abort = TRUE;
3855 /* avoid extra panel redraw */
3856 panel->dirty = 0;
3858 break;
3861 if (event->y == 1)
3863 /* sort on clicked column */
3864 mouse_sort_col (panel, event->x + 1);
3865 break;
3868 if (!is_active)
3869 change_panel ();
3870 /* fall through */
3872 case MSG_MOUSE_DRAG:
3874 int y, last, my_index;
3876 last = panel->dir.len - 1;
3877 y = event->y - 2;
3879 if (panel->top_file + y > last)
3880 my_index = last;
3881 else
3883 my_index = panel->top_file + y;
3885 if (panel->list_type == list_brief && panel->list_cols > 1)
3887 int width, lines;
3889 width = (w->cols - 2) / panel->list_cols;
3890 lines = panel_lines (panel);
3891 my_index += lines * (event->x / width);
3894 if (my_index > last)
3895 my_index = last;
3898 if (my_index != panel->selected)
3900 unselect_item (panel);
3901 panel->selected = my_index;
3902 select_item (panel);
3905 /* This one is new */
3906 mark_if_marking (panel, event);
3908 break;
3910 case MSG_MOUSE_UP:
3911 break;
3913 case MSG_MOUSE_CLICK:
3914 if ((event->count & GPM_DOUBLE) != 0)
3916 int y, lines;
3918 y = event->y - 1;
3919 lines = panel_lines (panel);
3921 if (y <= lines)
3922 do_enter (panel);
3924 break;
3926 case MSG_MOUSE_MOVE:
3927 break;
3929 case MSG_MOUSE_SCROLL_UP:
3930 if (is_active)
3932 if (panels_options.mouse_move_pages && panel->top_file > 0)
3933 prev_page (panel);
3934 else /* We are in first page */
3935 move_up (panel);
3937 break;
3939 case MSG_MOUSE_SCROLL_DOWN:
3940 if (is_active)
3942 if (panels_options.mouse_move_pages
3943 && panel->top_file + panel_items (panel) < panel->dir.len)
3944 next_page (panel);
3945 else /* We are in last page */
3946 move_down (panel);
3948 break;
3950 default:
3951 break;
3954 if (panel->dirty)
3955 widget_redraw (w);
3958 /* --------------------------------------------------------------------------------------------- */
3960 static void
3961 reload_panelized (WPanel * panel)
3963 int i, j;
3964 dir_list *list = &panel->dir;
3966 /* refresh current VFS directory required for vfs_path_from_str() */
3967 (void) mc_chdir (panel->cwd_vpath);
3969 for (i = 0, j = 0; i < list->len; i++)
3971 vfs_path_t *vpath;
3973 if (list->list[i].f.marked)
3975 /* Unmark the file in advance. In case the following mc_lstat
3976 * fails we are done, else we have to mark the file again
3977 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3978 * IMO that's the best way to update the panel's summary status
3979 * -- Norbert
3981 do_file_mark (panel, i, 0);
3983 vpath = vfs_path_from_str (list->list[i].fname);
3984 if (mc_lstat (vpath, &list->list[i].st) != 0)
3985 g_free (list->list[i].fname);
3986 else
3988 if (list->list[i].f.marked)
3989 do_file_mark (panel, i, 1);
3990 if (j != i)
3991 list->list[j] = list->list[i];
3992 j++;
3994 vfs_path_free (vpath);
3996 if (j == 0)
3997 dir_list_init (list);
3998 else
3999 list->len = j;
4001 if (panel != current_panel)
4002 (void) mc_chdir (current_panel->cwd_vpath);
4005 /* --------------------------------------------------------------------------------------------- */
4007 static void
4008 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
4010 gboolean free_pointer;
4011 char *my_current_file = NULL;
4013 if ((flags & UP_RELOAD) != 0)
4015 panel->is_panelized = FALSE;
4016 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
4017 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4020 /* If current_file == -1 (an invalid pointer) then preserve selection */
4021 free_pointer = current_file == UP_KEEPSEL;
4023 if (free_pointer)
4025 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
4026 current_file = my_current_file;
4029 if (panel->is_panelized)
4030 reload_panelized (panel);
4031 else
4032 panel_reload (panel);
4034 try_to_select (panel, current_file);
4035 panel->dirty = 1;
4037 if (free_pointer)
4038 g_free (my_current_file);
4041 /* --------------------------------------------------------------------------------------------- */
4043 static void
4044 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
4046 if (get_display_type (which) == view_listing)
4048 WPanel *panel;
4050 panel = PANEL (get_panel_widget (which));
4051 if (panel->is_panelized)
4052 flags &= ~UP_RELOAD;
4053 update_one_panel_widget (panel, flags, current_file);
4057 /* --------------------------------------------------------------------------------------------- */
4059 static void
4060 do_select (WPanel * panel, int i)
4062 if (i != panel->selected)
4064 panel->dirty = 1;
4065 panel->selected = i;
4066 panel->top_file = panel->selected - (WIDGET (panel)->lines - 2) / 2;
4067 if (panel->top_file < 0)
4068 panel->top_file = 0;
4072 /* --------------------------------------------------------------------------------------------- */
4074 static void
4075 do_try_to_select (WPanel * panel, const char *name)
4077 int i;
4078 char *subdir;
4080 if (!name)
4082 do_select (panel, 0);
4083 return;
4086 /* We only want the last component of the directory,
4087 * and from this only the name without suffix.
4088 * Cut prefix if the panel is not panelized */
4090 if (panel->is_panelized)
4091 subdir = vfs_strip_suffix_from_filename (name);
4092 else
4093 subdir = vfs_strip_suffix_from_filename (x_basename (name));
4095 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
4096 for (i = 0; i < panel->dir.len; i++)
4098 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
4100 do_select (panel, i);
4101 g_free (subdir);
4102 return;
4106 /* Try to select a file near the file that is missing */
4107 if (panel->selected >= panel->dir.len)
4108 do_select (panel, panel->dir.len - 1);
4109 g_free (subdir);
4112 /* --------------------------------------------------------------------------------------------- */
4114 /* event callback */
4115 static gboolean
4116 event_update_panels (const gchar * event_group_name, const gchar * event_name,
4117 gpointer init_data, gpointer data)
4119 (void) event_group_name;
4120 (void) event_name;
4121 (void) init_data;
4122 (void) data;
4124 update_panels (UP_RELOAD, UP_KEEPSEL);
4126 return TRUE;
4129 /* --------------------------------------------------------------------------------------------- */
4131 /* event callback */
4132 static gboolean
4133 panel_save_current_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
4134 gpointer init_data, gpointer data)
4136 (void) event_group_name;
4137 (void) event_name;
4138 (void) init_data;
4139 (void) data;
4141 if (current_panel->marked == 0)
4142 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
4143 (gpointer) selection (current_panel)->fname);
4144 else
4146 int i;
4147 gboolean first = TRUE;
4148 char *flist = NULL;
4150 for (i = 0; i < current_panel->dir.len; i++)
4151 if (current_panel->dir.list[i].f.marked != 0)
4152 { /* Skip the unmarked ones */
4153 if (first)
4155 flist = g_strdup (current_panel->dir.list[i].fname);
4156 first = FALSE;
4158 else
4160 /* Add empty lines after the file */
4161 char *tmp;
4163 tmp =
4164 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
4165 g_free (flist);
4166 flist = tmp;
4170 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
4171 g_free (flist);
4173 return TRUE;
4176 /* --------------------------------------------------------------------------------------------- */
4178 static vfs_path_t *
4179 panel_recursive_cd_to_parent (const vfs_path_t * vpath)
4181 vfs_path_t *cwd_vpath;
4183 cwd_vpath = vfs_path_clone (vpath);
4185 while (mc_chdir (cwd_vpath) < 0)
4187 const char *panel_cwd_path;
4188 vfs_path_t *tmp_vpath;
4190 /* check if path contains only '/' */
4191 panel_cwd_path = vfs_path_as_str (cwd_vpath);
4192 if (IS_PATH_SEP (panel_cwd_path[0]) && panel_cwd_path[1] == '\0')
4193 return NULL;
4195 tmp_vpath = vfs_path_vtokens_get (cwd_vpath, 0, -1);
4196 vfs_path_free (cwd_vpath);
4197 cwd_vpath =
4198 vfs_path_build_filename (PATH_SEP_STR, vfs_path_as_str (tmp_vpath), (char *) NULL);
4199 vfs_path_free (tmp_vpath);
4202 return cwd_vpath;
4205 /* --------------------------------------------------------------------------------------------- */
4206 /*** public functions ****************************************************************************/
4207 /* --------------------------------------------------------------------------------------------- */
4209 void
4210 try_to_select (WPanel * panel, const char *name)
4212 do_try_to_select (panel, name);
4213 select_item (panel);
4216 /* --------------------------------------------------------------------------------------------- */
4218 void
4219 panel_clean_dir (WPanel * panel)
4221 panel->top_file = 0;
4222 panel->selected = 0;
4223 panel->marked = 0;
4224 panel->dirs_marked = 0;
4225 panel->total = 0;
4226 panel->searching = FALSE;
4227 panel->is_panelized = FALSE;
4228 panel->dirty = 1;
4229 panel->content_shift = -1;
4230 panel->max_shift = -1;
4232 dir_list_clean (&panel->dir);
4235 /* --------------------------------------------------------------------------------------------- */
4237 * Set Up panel's current dir object
4239 * @param panel panel object
4240 * @param path_str string contain path
4243 void
4244 panel_set_cwd (WPanel * panel, const vfs_path_t * vpath)
4246 if (vpath != panel->cwd_vpath) /* check if new vpath is not the panel->cwd_vpath object */
4248 vfs_path_free (panel->cwd_vpath);
4249 panel->cwd_vpath = vfs_path_clone (vpath);
4253 /* --------------------------------------------------------------------------------------------- */
4255 * Set Up panel's last working dir object
4257 * @param panel panel object
4258 * @param path_str string contain path
4261 void
4262 panel_set_lwd (WPanel * panel, const vfs_path_t * vpath)
4264 if (vpath != panel->lwd_vpath) /* check if new vpath is not the panel->lwd_vpath object */
4266 vfs_path_free (panel->lwd_vpath);
4267 panel->lwd_vpath = vfs_path_clone (vpath);
4271 /* --------------------------------------------------------------------------------------------- */
4273 * Panel creation for specified directory.
4275 * @param panel_name specifies the name of the panel for setup retieving
4276 * @param wpath the path of working panel directory. If path is NULL then panel will be created
4277 * for current directory
4279 * @return new instance of WPanel
4282 WPanel *
4283 panel_new_with_dir (const char *panel_name, const vfs_path_t * vpath)
4285 WPanel *panel;
4286 Widget *w;
4287 char *section;
4288 int i, err;
4289 char *curdir = NULL;
4291 panel = g_new0 (WPanel, 1);
4292 w = WIDGET (panel);
4293 /* No know sizes of the panel at startup */
4294 widget_init (w, 0, 0, 0, 0, panel_callback, panel_mouse_callback);
4295 widget_set_options (w, WOP_TOP_SELECT, TRUE);
4297 if (vpath != NULL)
4299 curdir = _vfs_get_cwd ();
4300 panel_set_cwd (panel, vpath);
4302 else
4304 vfs_setup_cwd ();
4305 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4308 panel_set_lwd (panel, vfs_get_raw_current_dir ());
4310 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4311 /* directories history will be get later */
4313 panel->dir.size = DIR_LIST_MIN_SIZE;
4314 panel->dir.list = g_new (file_entry_t, panel->dir.size);
4315 panel->dir.len = 0;
4316 panel->active = 0;
4317 panel->filter = NULL;
4318 panel->list_cols = 1;
4319 panel->brief_cols = 2;
4320 panel->top_file = 0;
4321 panel->selected = 0;
4322 panel->marked = 0;
4323 panel->total = 0;
4324 panel->dirty = 1;
4325 panel->searching = FALSE;
4326 panel->dirs_marked = 0;
4327 panel->is_panelized = FALSE;
4328 panel->format = NULL;
4329 panel->status_format = NULL;
4330 panel->format_modified = 1;
4331 panel->content_shift = -1;
4332 panel->max_shift = -1;
4334 panel->panel_name = g_strdup (panel_name);
4335 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4337 #ifdef HAVE_CHARSET
4338 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4339 #endif
4341 for (i = 0; i < LIST_TYPES; i++)
4342 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4344 panel->search_buffer[0] = '\0';
4345 panel->prev_search_buffer[0] = '\0';
4346 panel->frame_size = frame_half;
4348 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4349 if (!mc_config_has_group (mc_global.main_config, section))
4351 g_free (section);
4352 section = g_strdup (panel->panel_name);
4354 panel_load_setup (panel, section);
4355 g_free (section);
4357 /* Load format strings */
4358 err = set_panel_formats (panel);
4359 if (err != 0)
4360 set_panel_formats (panel);
4362 #ifdef HAVE_CHARSET
4364 const vfs_path_element_t *path_element;
4366 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4367 if (path_element->encoding != NULL)
4368 panel->codepage = get_codepage_index (path_element->encoding);
4370 #endif
4372 if (mc_chdir (panel->cwd_vpath) != 0)
4374 #ifdef HAVE_CHARSET
4375 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4376 #endif
4377 vfs_setup_cwd ();
4378 vfs_path_free (panel->cwd_vpath);
4379 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4382 /* Load the default format */
4383 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4384 &panel->sort_info, panel->filter);
4386 /* Restore old right path */
4387 if (curdir != NULL)
4389 vfs_path_t *tmp_vpath;
4391 tmp_vpath = vfs_path_from_str (curdir);
4392 err = mc_chdir (tmp_vpath);
4393 vfs_path_free (tmp_vpath);
4395 g_free (curdir);
4397 return panel;
4400 /* --------------------------------------------------------------------------------------------- */
4402 void
4403 panel_reload (WPanel * panel)
4405 struct stat current_stat;
4406 vfs_path_t *cwd_vpath;
4408 if (panels_options.fast_reload && stat (vfs_path_as_str (panel->cwd_vpath), &current_stat) == 0
4409 && current_stat.st_ctime == panel->dir_stat.st_ctime
4410 && current_stat.st_mtime == panel->dir_stat.st_mtime)
4411 return;
4413 cwd_vpath = panel_recursive_cd_to_parent (panel->cwd_vpath);
4414 vfs_path_free (panel->cwd_vpath);
4416 if (cwd_vpath == NULL)
4418 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4419 panel_clean_dir (panel);
4420 dir_list_init (&panel->dir);
4421 return;
4424 panel->cwd_vpath = cwd_vpath;
4425 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4426 show_dir (panel);
4428 dir_list_reload (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4429 &panel->sort_info, panel->filter);
4431 panel->dirty = 1;
4432 if (panel->selected >= panel->dir.len)
4433 do_select (panel, panel->dir.len - 1);
4435 recalculate_panel_summary (panel);
4438 /* --------------------------------------------------------------------------------------------- */
4439 /* Switches the panel to the mode specified in the format */
4440 /* Seting up both format and status string. Return: 0 - on success; */
4441 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4444 set_panel_formats (WPanel * p)
4446 format_e *form;
4447 char *err = NULL;
4448 int retcode = 0;
4450 form = use_display_format (p, panel_format (p), &err, FALSE);
4452 if (err != NULL)
4454 g_free (err);
4455 retcode = 1;
4457 else
4459 delete_format (p->format);
4460 p->format = form;
4463 if (panels_options.show_mini_info)
4465 form = use_display_format (p, mini_status_format (p), &err, TRUE);
4467 if (err != NULL)
4469 g_free (err);
4470 retcode += 2;
4472 else
4474 delete_format (p->status_format);
4475 p->status_format = form;
4479 panel_format_modified (p);
4480 panel_update_cols (WIDGET (p), p->frame_size);
4482 if (retcode)
4483 message (D_ERROR, _("Warning"),
4484 _("User supplied format looks invalid, reverting to default."));
4485 if (retcode & 0x01)
4487 g_free (p->user_format);
4488 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4490 if (retcode & 0x02)
4492 g_free (p->user_status_format[p->list_type]);
4493 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4496 return retcode;
4499 /* --------------------------------------------------------------------------------------------- */
4501 void
4502 panel_update_cols (Widget * widget, panel_display_t frame_size)
4504 int cols, origin;
4506 /* don't touch panel if it is not in dialog yet */
4507 /* if panel is not in dialog it is not in widgets list
4508 and cannot be compared with get_panel_widget() result */
4509 if (widget->owner == NULL)
4510 return;
4512 if (panels_layout.horizontal_split)
4514 widget->cols = COLS;
4515 return;
4518 if (frame_size == frame_full)
4520 cols = COLS;
4521 origin = 0;
4523 else if (widget == get_panel_widget (0))
4525 cols = panels_layout.left_panel_size;
4526 origin = 0;
4528 else
4530 cols = COLS - panels_layout.left_panel_size;
4531 origin = panels_layout.left_panel_size;
4534 widget->cols = cols;
4535 widget->x = origin;
4538 /* --------------------------------------------------------------------------------------------- */
4540 /* Select current item and readjust the panel */
4541 void
4542 select_item (WPanel * panel)
4544 /* Although currently all over the code we set the selection and
4545 top file to decent values before calling select_item, I could
4546 forget it someday, so it's better to do the actual fitting here */
4548 if (panel->selected < 0)
4549 panel->selected = 0;
4551 if (panel->selected > panel->dir.len - 1)
4552 panel->selected = panel->dir.len - 1;
4554 adjust_top_file (panel);
4556 panel->dirty = 1;
4558 execute_hooks (select_file_hook);
4561 /* --------------------------------------------------------------------------------------------- */
4562 /** Clears all files in the panel, used only when one file was marked */
4563 void
4564 unmark_files (WPanel * panel)
4566 int i;
4568 if (!panel->marked)
4569 return;
4571 for (i = 0; i < panel->dir.len; i++)
4572 file_mark (panel, i, 0);
4574 panel->dirs_marked = 0;
4575 panel->marked = 0;
4576 panel->total = 0;
4579 /* --------------------------------------------------------------------------------------------- */
4580 /** Recalculate the panels summary information, used e.g. when marked
4581 files might have been removed by an external command */
4583 void
4584 recalculate_panel_summary (WPanel * panel)
4586 int i;
4588 panel->marked = 0;
4589 panel->dirs_marked = 0;
4590 panel->total = 0;
4592 for (i = 0; i < panel->dir.len; i++)
4593 if (panel->dir.list[i].f.marked)
4595 /* do_file_mark will return immediately if newmark == oldmark.
4596 So we have to first unmark it to get panel's summary information
4597 updated. (Norbert) */
4598 panel->dir.list[i].f.marked = 0;
4599 do_file_mark (panel, i, 1);
4603 /* --------------------------------------------------------------------------------------------- */
4604 /** This routine marks a file or a directory */
4606 void
4607 do_file_mark (WPanel * panel, int idx, int mark)
4609 if (panel->dir.list[idx].f.marked == mark)
4610 return;
4612 /* Only '..' can't be marked, '.' isn't visible */
4613 if (DIR_IS_DOTDOT (panel->dir.list[idx].fname))
4614 return;
4616 file_mark (panel, idx, mark);
4617 if (panel->dir.list[idx].f.marked)
4619 panel->marked++;
4621 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4623 if (panel->dir.list[idx].f.dir_size_computed)
4624 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4625 panel->dirs_marked++;
4627 else
4628 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4630 set_colors (panel);
4632 else
4634 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4636 if (panel->dir.list[idx].f.dir_size_computed)
4637 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4638 panel->dirs_marked--;
4640 else
4641 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4643 panel->marked--;
4647 /* --------------------------------------------------------------------------------------------- */
4649 * Changes the current directory of the panel.
4650 * Record change in the directory history.
4652 gboolean
4653 do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4655 gboolean r;
4657 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4658 if (r)
4659 directory_history_add (panel, panel->cwd_vpath);
4660 return r;
4663 /* --------------------------------------------------------------------------------------------- */
4665 void
4666 file_mark (WPanel * panel, int lc_index, int val)
4668 if (panel->dir.list[lc_index].f.marked != val)
4670 panel->dir.list[lc_index].f.marked = val;
4671 panel->dirty = 1;
4675 /* --------------------------------------------------------------------------------------------- */
4677 void
4678 panel_re_sort (WPanel * panel)
4680 char *filename;
4681 int i;
4683 if (panel == NULL)
4684 return;
4686 filename = g_strdup (selection (panel)->fname);
4687 unselect_item (panel);
4688 dir_list_sort (&panel->dir, panel->sort_field->sort_routine, &panel->sort_info);
4689 panel->selected = -1;
4691 for (i = panel->dir.len; i != 0; i--)
4692 if (strcmp (panel->dir.list[i - 1].fname, filename) == 0)
4694 panel->selected = i - 1;
4695 break;
4698 g_free (filename);
4699 panel->top_file = panel->selected - panel_items (panel) / 2;
4700 select_item (panel);
4701 panel->dirty = 1;
4704 /* --------------------------------------------------------------------------------------------- */
4706 void
4707 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4709 if (sort_order == NULL)
4710 return;
4712 panel->sort_field = sort_order;
4714 /* The directory is already sorted, we have to load the unsorted stuff */
4715 if (sort_order->sort_routine == (GCompareFunc) unsorted)
4717 char *current_file;
4719 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4720 panel_reload (panel);
4721 try_to_select (panel, current_file);
4722 g_free (current_file);
4724 panel_re_sort (panel);
4727 /* --------------------------------------------------------------------------------------------- */
4729 #ifdef HAVE_CHARSET
4732 * Change panel encoding.
4733 * @param panel WPanel object
4736 void
4737 panel_change_encoding (WPanel * panel)
4739 const char *encoding = NULL;
4740 char *errmsg;
4741 int r;
4743 r = select_charset (-1, -1, panel->codepage, FALSE);
4745 if (r == SELECT_CHARSET_CANCEL)
4746 return; /* Cancel */
4748 panel->codepage = r;
4750 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4752 /* No translation */
4753 vfs_path_t *cd_path_vpath;
4755 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4756 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4757 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4758 show_dir (panel);
4759 vfs_path_free (cd_path_vpath);
4760 return;
4763 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4764 if (errmsg != NULL)
4766 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4767 g_free (errmsg);
4768 return;
4771 encoding = get_codepage_id (panel->codepage);
4772 if (encoding != NULL)
4774 vfs_path_change_encoding (panel->cwd_vpath, encoding);
4776 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4777 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""),
4778 vfs_path_as_str (panel->cwd_vpath));
4782 /* --------------------------------------------------------------------------------------------- */
4785 * Remove encode info from last path element.
4788 vfs_path_t *
4789 remove_encoding_from_path (const vfs_path_t * vpath)
4791 vfs_path_t *ret_vpath;
4792 GString *tmp_conv;
4793 int indx;
4795 ret_vpath = vfs_path_new ();
4797 tmp_conv = g_string_new ("");
4799 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4801 GIConv converter;
4802 vfs_path_element_t *path_element;
4804 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4806 if (path_element->encoding == NULL)
4808 vfs_path_add_element (ret_vpath, path_element);
4809 continue;
4812 converter = str_crt_conv_to (path_element->encoding);
4813 if (converter == INVALID_CONV)
4815 vfs_path_add_element (ret_vpath, path_element);
4816 continue;
4819 MC_PTR_FREE (path_element->encoding);
4821 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4823 g_free (path_element->path);
4824 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4826 g_string_set_size (tmp_conv, 0);
4828 str_close_conv (converter);
4829 str_close_conv (path_element->dir.converter);
4830 path_element->dir.converter = INVALID_CONV;
4831 vfs_path_add_element (ret_vpath, path_element);
4833 g_string_free (tmp_conv, TRUE);
4834 return ret_vpath;
4836 #endif /* HAVE_CHARSET */
4838 /* --------------------------------------------------------------------------------------------- */
4841 * This routine reloads the directory in both panels. It tries to
4842 * select current_file in current_panel and other_file in other_panel.
4843 * If current_file == -1 then it automatically sets current_file and
4844 * other_file to the currently selected files in the panels.
4846 * If flags has the UP_ONLY_CURRENT bit toggled on, then it
4847 * will not reload the other panel.
4849 * @param flags for reload panel
4850 * @param current_file name of the current file
4853 void
4854 update_panels (panel_update_flags_t flags, const char *current_file)
4856 WPanel *panel;
4858 /* first, update other panel... */
4859 if ((flags & UP_ONLY_CURRENT) == 0)
4860 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4861 /* ...then current one */
4862 update_one_panel (get_current_index (), flags, current_file);
4864 if (get_current_type () == view_listing)
4865 panel = PANEL (get_panel_widget (get_current_index ()));
4866 else
4867 panel = PANEL (get_panel_widget (get_other_index ()));
4869 if (!panel->is_panelized)
4870 (void) mc_chdir (panel->cwd_vpath);
4873 /* --------------------------------------------------------------------------------------------- */
4875 gsize
4876 panel_get_num_of_sortable_fields (void)
4878 gsize ret = 0, lc_index;
4880 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4881 if (panel_fields[lc_index].is_user_choice)
4882 ret++;
4883 return ret;
4886 /* --------------------------------------------------------------------------------------------- */
4888 char **
4889 panel_get_sortable_fields (gsize * array_size)
4891 char **ret;
4892 gsize lc_index, i;
4894 lc_index = panel_get_num_of_sortable_fields ();
4896 ret = g_try_new0 (char *, lc_index + 1);
4897 if (ret == NULL)
4898 return NULL;
4900 if (array_size != NULL)
4901 *array_size = lc_index;
4903 lc_index = 0;
4905 for (i = 0; panel_fields[i].id != NULL; i++)
4906 if (panel_fields[i].is_user_choice)
4907 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4909 return ret;
4912 /* --------------------------------------------------------------------------------------------- */
4914 const panel_field_t *
4915 panel_get_field_by_id (const char *name)
4917 gsize lc_index;
4919 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4920 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4921 return &panel_fields[lc_index];
4923 return NULL;
4926 /* --------------------------------------------------------------------------------------------- */
4928 const panel_field_t *
4929 panel_get_field_by_title_hotkey (const char *name)
4931 gsize lc_index;
4933 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4934 if (panel_fields[lc_index].title_hotkey != NULL &&
4935 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4936 return &panel_fields[lc_index];
4938 return NULL;
4941 /* --------------------------------------------------------------------------------------------- */
4943 const panel_field_t *
4944 panel_get_field_by_title (const char *name)
4946 gsize lc_index;
4948 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4950 const char *title;
4952 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4953 if (strcmp (title, name) == 0)
4954 return &panel_fields[lc_index];
4957 return NULL;
4960 /* --------------------------------------------------------------------------------------------- */
4962 gsize
4963 panel_get_num_of_user_possible_fields (void)
4965 gsize ret = 0, lc_index;
4967 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4968 if (panel_fields[lc_index].use_in_user_format)
4969 ret++;
4971 return ret;
4974 /* --------------------------------------------------------------------------------------------- */
4976 char **
4977 panel_get_user_possible_fields (gsize * array_size)
4979 char **ret;
4980 gsize lc_index, i;
4982 lc_index = panel_get_num_of_user_possible_fields ();
4984 ret = g_try_new0 (char *, lc_index + 1);
4985 if (ret == NULL)
4986 return NULL;
4988 if (array_size != NULL)
4989 *array_size = lc_index;
4991 lc_index = 0;
4993 for (i = 0; panel_fields[i].id != NULL; i++)
4994 if (panel_fields[i].use_in_user_format)
4995 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4997 return ret;
5000 /* --------------------------------------------------------------------------------------------- */
5002 void
5003 panel_init (void)
5005 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
5006 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ".");
5008 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
5009 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
5010 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
5011 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
5012 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
5013 panel_filename_scroll_left_char =
5014 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
5015 panel_filename_scroll_right_char =
5016 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
5018 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
5019 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_current_file_to_clip_file",
5020 panel_save_current_file_to_clip_file, NULL, NULL);
5023 /* --------------------------------------------------------------------------------------------- */
5025 void
5026 panel_deinit (void)
5028 g_free (panel_sort_up_sign);
5029 g_free (panel_sort_down_sign);
5031 g_free (panel_hiddenfiles_sign_show);
5032 g_free (panel_hiddenfiles_sign_hide);
5033 g_free (panel_history_prev_item_sign);
5034 g_free (panel_history_next_item_sign);
5035 g_free (panel_history_show_list_sign);
5036 g_free (panel_filename_scroll_left_char);
5037 g_free (panel_filename_scroll_right_char);
5040 /* --------------------------------------------------------------------------------------------- */
5042 gboolean
5043 do_cd (const vfs_path_t * new_dir_vpath, enum cd_enum exact)
5045 gboolean res;
5046 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
5048 if (current_panel->is_panelized)
5050 size_t new_vpath_len;
5052 new_vpath_len = vfs_path_len (new_dir_vpath);
5053 if (vfs_path_equal_len (new_dir_vpath, panelized_panel.root_vpath, new_vpath_len))
5054 _new_dir_vpath = panelized_panel.root_vpath;
5057 res = do_panel_cd (current_panel, _new_dir_vpath, exact);
5059 #ifdef HAVE_CHARSET
5060 if (res)
5062 const vfs_path_element_t *path_element;
5064 path_element = vfs_path_get_by_index (current_panel->cwd_vpath, -1);
5065 if (path_element->encoding != NULL)
5066 current_panel->codepage = get_codepage_index (path_element->encoding);
5067 else
5068 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
5070 #endif /* HAVE_CHARSET */
5072 return res;
5075 /* --------------------------------------------------------------------------------------------- */