Ticket 3948: can't create network link from panel...
[midnight-commander.git] / src / filemanager / panel.c
blob88d4c60599a9e51edeff22d72e26af72f5601957
1 /*
2 Panel managing.
4 Copyright (C) 1994-2018
5 Free Software Foundation, Inc.
7 Written by:
8 Miguel de Icaza, 1995
9 Timur Bakeyev, 1997, 1999
10 Slava Zanko <slavazanko@gmail.com>, 2013
11 Andrew Borodin <aborodin@vmail.ru>, 2013-2016
13 This file is part of the Midnight Commander.
15 The Midnight Commander is free software: you can redistribute it
16 and/or modify it under the terms of the GNU General Public License as
17 published by the Free Software Foundation, either version 3 of the License,
18 or (at your option) any later version.
20 The Midnight Commander is distributed in the hope that it will be useful,
21 but WITHOUT ANY WARRANTY; without even the implied warranty of
22 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 GNU General Public License for more details.
25 You should have received a copy of the GNU General Public License
26 along with this program. If not, see <http://www.gnu.org/licenses/>.
29 /** \file panel.c
30 * \brief Source: panel managin module
33 #include <config.h>
35 #include <errno.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/key.h" /* XCTRL and ALT macros */
44 #include "lib/skin.h"
45 #include "lib/strescape.h"
46 #include "lib/mcconfig.h"
47 #include "lib/vfs/vfs.h"
48 #include "lib/unixcompat.h"
49 #include "lib/search.h"
50 #include "lib/timefmt.h" /* file_date() */
51 #include "lib/util.h"
52 #include "lib/widget.h"
53 #ifdef HAVE_CHARSET
54 #include "lib/charsets.h" /* get_codepage_id () */
55 #endif
56 #include "lib/event.h"
58 #include "src/setup.h" /* For loading/saving panel options */
59 #include "src/execute.h"
60 #ifdef HAVE_CHARSET
61 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
62 #endif
63 #include "src/keybind-defaults.h" /* global_keymap_t */
64 #ifdef ENABLE_SUBSHELL
65 #include "src/subshell/subshell.h" /* do_subshell_chdir() */
66 #endif
68 #include "src/usermenu.h"
70 #include "dir.h"
71 #include "boxes.h"
72 #include "tree.h"
73 #include "ext.h" /* regexp_command */
74 #include "layout.h" /* Most layout variables are here */
75 #include "cmd.h"
76 #include "command.h" /* cmdline */
77 #include "midnight.h"
78 #include "mountlist.h" /* my_statfs */
80 #include "panel.h"
82 /*** global variables ****************************************************************************/
84 /* The hook list for the select file function */
85 hook_t *select_file_hook = NULL;
87 /* *INDENT-OFF* */
88 panelized_panel_t panelized_panel = { {NULL, 0, -1}, NULL };
89 /* *INDENT-ON* */
91 static const char *string_file_name (file_entry_t *, int);
92 static const char *string_file_size (file_entry_t *, int);
93 static const char *string_file_size_brief (file_entry_t *, int);
94 static const char *string_file_type (file_entry_t *, int);
95 static const char *string_file_mtime (file_entry_t *, int);
96 static const char *string_file_atime (file_entry_t *, int);
97 static const char *string_file_ctime (file_entry_t *, int);
98 static const char *string_file_permission (file_entry_t *, int);
99 static const char *string_file_perm_octal (file_entry_t *, int);
100 static const char *string_file_nlinks (file_entry_t *, int);
101 static const char *string_inode (file_entry_t *, int);
102 static const char *string_file_nuid (file_entry_t *, int);
103 static const char *string_file_ngid (file_entry_t *, int);
104 static const char *string_file_owner (file_entry_t *, int);
105 static const char *string_file_group (file_entry_t *, int);
106 static const char *string_marked (file_entry_t *, int);
107 static const char *string_space (file_entry_t *, int);
108 static const char *string_dot (file_entry_t *, int);
110 mc_fhl_t *mc_filehighlight = NULL;
112 /*** file scope macro definitions ****************************************************************/
114 #define NORMAL 0
115 #define SELECTED 1
116 #define MARKED 2
117 #define MARKED_SELECTED 3
118 #define STATUS 5
120 /*** file scope type declarations ****************************************************************/
122 typedef enum
124 MARK_DONT_MOVE = 0,
125 MARK_DOWN = 1,
126 MARK_FORCE_DOWN = 2,
127 MARK_FORCE_UP = 3
128 } mark_act_t;
131 * This describes a format item. The parse_display_format routine parses
132 * the user specified format and creates a linked list of format_e structures.
134 typedef struct format_e
136 struct format_e *next;
137 int requested_field_len;
138 int field_len;
139 align_crt_t just_mode;
140 gboolean expand;
141 const char *(*string_fn) (file_entry_t *, int len);
142 char *title;
143 const char *id;
144 } format_e;
146 /* File name scroll states */
147 typedef enum
149 FILENAME_NOSCROLL = 1,
150 FILENAME_SCROLL_LEFT = 2,
151 FILENAME_SCROLL_RIGHT = 4
152 } filename_scroll_flag_t;
154 /*** file scope variables ************************************************************************/
156 /* *INDENT-OFF* */
157 static panel_field_t panel_fields[] = {
159 "unsorted", 12, TRUE, J_LEFT_FIT,
160 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
161 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
162 N_("sort|u"),
163 N_("&Unsorted"), TRUE, FALSE,
164 string_file_name,
165 (GCompareFunc) unsorted
169 "name", 12, TRUE, J_LEFT_FIT,
170 /* TRANSLATORS: one single character to represent 'name' sort mode */
171 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
172 N_("sort|n"),
173 N_("&Name"), TRUE, TRUE,
174 string_file_name,
175 (GCompareFunc) sort_name
179 "version", 12, TRUE, J_LEFT_FIT,
180 /* TRANSLATORS: one single character to represent 'version' sort mode */
181 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
182 N_("sort|v"),
183 N_("&Version"), TRUE, FALSE,
184 string_file_name,
185 (GCompareFunc) sort_vers
189 "extension", 12, TRUE, J_LEFT_FIT,
190 /* TRANSLATORS: one single character to represent 'extension' sort mode */
191 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
192 N_("sort|e"),
193 N_("E&xtension"), TRUE, FALSE,
194 string_file_name, /* TODO: string_file_ext */
195 (GCompareFunc) sort_ext
199 "size", 7, FALSE, J_RIGHT,
200 /* TRANSLATORS: one single character to represent 'size' sort mode */
201 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
202 N_("sort|s"),
203 N_("&Size"), TRUE, TRUE,
204 string_file_size,
205 (GCompareFunc) sort_size
209 "bsize", 7, FALSE, J_RIGHT,
211 N_("Block Size"), FALSE, FALSE,
212 string_file_size_brief,
213 (GCompareFunc) sort_size
217 "type", 1, FALSE, J_LEFT,
219 "", FALSE, TRUE,
220 string_file_type,
221 NULL
225 "mtime", 12, FALSE, J_RIGHT,
226 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
227 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
228 N_("sort|m"),
229 N_("&Modify time"), TRUE, TRUE,
230 string_file_mtime,
231 (GCompareFunc) sort_time
235 "atime", 12, FALSE, J_RIGHT,
236 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
237 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
238 N_("sort|a"),
239 N_("&Access time"), TRUE, TRUE,
240 string_file_atime,
241 (GCompareFunc) sort_atime
245 "ctime", 12, FALSE, J_RIGHT,
246 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
247 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
248 N_("sort|h"),
249 N_("C&hange time"), TRUE, TRUE,
250 string_file_ctime,
251 (GCompareFunc) sort_ctime
255 "perm", 10, FALSE, J_LEFT,
257 N_("Permission"), FALSE, TRUE,
258 string_file_permission,
259 NULL
263 "mode", 6, FALSE, J_RIGHT,
265 N_("Perm"), FALSE, TRUE,
266 string_file_perm_octal,
267 NULL
271 "nlink", 2, FALSE, J_RIGHT,
273 N_("Nl"), FALSE, TRUE,
274 string_file_nlinks, NULL
278 "inode", 5, FALSE, J_RIGHT,
279 /* TRANSLATORS: one single character to represent 'inode' sort mode */
280 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
281 N_("sort|i"),
282 N_("&Inode"), TRUE, TRUE,
283 string_inode,
284 (GCompareFunc) sort_inode
288 "nuid", 5, FALSE, J_RIGHT,
290 N_("UID"), FALSE, FALSE,
291 string_file_nuid,
292 NULL
296 "ngid", 5, FALSE, J_RIGHT,
298 N_("GID"), FALSE, FALSE,
299 string_file_ngid,
300 NULL
304 "owner", 8, FALSE, J_LEFT_FIT,
306 N_("Owner"), FALSE, TRUE,
307 string_file_owner,
308 NULL
312 "group", 8, FALSE, J_LEFT_FIT,
314 N_("Group"), FALSE, TRUE,
315 string_file_group,
316 NULL
320 "mark", 1, FALSE, J_RIGHT,
322 " ", FALSE, TRUE,
323 string_marked,
324 NULL
328 "|", 1, FALSE, J_RIGHT,
330 " ", FALSE, TRUE,
331 NULL,
332 NULL
336 "space", 1, FALSE, J_RIGHT,
338 " ", FALSE, TRUE,
339 string_space,
340 NULL
344 "dot", 1, FALSE, J_RIGHT,
346 " ", FALSE, FALSE,
347 string_dot,
348 NULL
352 NULL, 0, FALSE, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
355 /* *INDENT-ON* */
357 static char *panel_sort_up_sign = NULL;
358 static char *panel_sort_down_sign = NULL;
360 static char *panel_hiddenfiles_sign_show = NULL;
361 static char *panel_hiddenfiles_sign_hide = NULL;
362 static char *panel_history_prev_item_sign = NULL;
363 static char *panel_history_next_item_sign = NULL;
364 static char *panel_history_show_list_sign = NULL;
365 static char *panel_filename_scroll_left_char = NULL;
366 static char *panel_filename_scroll_right_char = NULL;
368 /* Panel that selection started */
369 static WPanel *mouse_mark_panel = NULL;
371 static int mouse_marking = 0;
372 static int state_mark = 0;
374 /* --------------------------------------------------------------------------------------------- */
375 /*** file scope functions ************************************************************************/
376 /* --------------------------------------------------------------------------------------------- */
378 static void
379 set_colors (const WPanel * panel)
381 (void) panel;
383 tty_set_normal_attrs ();
384 tty_setcolor (NORMAL_COLOR);
387 /* --------------------------------------------------------------------------------------------- */
388 /** Delete format string, it is a linked list */
390 static void
391 delete_format (format_e * format)
393 while (format != NULL)
395 format_e *next = format->next;
396 g_free (format->title);
397 g_free (format);
398 format = next;
402 /* --------------------------------------------------------------------------------------------- */
403 /** Extract the number of available lines in a panel */
405 static int
406 panel_lines (const WPanel * p)
408 /* 3 lines are: top frame, column header, botton frame */
409 return (CONST_WIDGET (p)->lines - 3 - (panels_options.show_mini_info ? 2 : 0));
412 /* --------------------------------------------------------------------------------------------- */
413 /** This code relies on the default justification!!! */
415 static void
416 add_permission_string (const char *dest, int width, file_entry_t * fe, int attr, int color,
417 gboolean is_octal)
419 int i, r, l;
421 l = get_user_permissions (&fe->st);
423 if (is_octal)
425 /* Place of the access bit in octal mode */
426 l = width + l - 3;
427 r = l + 1;
429 else
431 /* The same to the triplet in string mode */
432 l = l * 3 + 1;
433 r = l + 3;
436 for (i = 0; i < width; i++)
438 if (i >= l && i < r)
440 if (attr == SELECTED || attr == MARKED_SELECTED)
441 tty_setcolor (MARKED_SELECTED_COLOR);
442 else
443 tty_setcolor (MARKED_COLOR);
445 else if (color >= 0)
446 tty_setcolor (color);
447 else
448 tty_lowlevel_setcolor (-color);
450 tty_print_char (dest[i]);
454 /* --------------------------------------------------------------------------------------------- */
455 /** String representations of various file attributes name */
457 static const char *
458 string_file_name (file_entry_t * fe, int len)
460 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
462 (void) len;
464 g_strlcpy (buffer, fe->fname, sizeof (buffer));
465 return buffer;
468 /* --------------------------------------------------------------------------------------------- */
470 static unsigned int
471 ilog10 (dev_t n)
473 unsigned int digits = 0;
477 digits++;
478 n /= 10;
480 while (n != 0);
482 return digits;
485 /* --------------------------------------------------------------------------------------------- */
487 static void
488 format_device_number (char *buf, size_t bufsize, dev_t dev)
490 dev_t major_dev = major (dev);
491 dev_t minor_dev = minor (dev);
492 unsigned int major_digits = ilog10 (major_dev);
493 unsigned int minor_digits = ilog10 (minor_dev);
495 g_assert (bufsize >= 1);
497 if (major_digits + 1 + minor_digits + 1 <= bufsize)
498 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
499 else
500 g_strlcpy (buf, _("[dev]"), bufsize);
503 /* --------------------------------------------------------------------------------------------- */
504 /** size */
506 static const char *
507 string_file_size (file_entry_t * fe, int len)
509 static char buffer[BUF_TINY];
511 /* Don't ever show size of ".." since we don't calculate it */
512 if (DIR_IS_DOTDOT (fe->fname))
513 return _("UP--DIR");
515 #ifdef HAVE_STRUCT_STAT_ST_RDEV
516 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
517 format_device_number (buffer, len + 1, fe->st.st_rdev);
518 else
519 #endif
520 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
522 return buffer;
525 /* --------------------------------------------------------------------------------------------- */
526 /** bsize */
528 static const char *
529 string_file_size_brief (file_entry_t * fe, int len)
531 if (S_ISLNK (fe->st.st_mode) && !link_isdir (fe))
532 return _("SYMLINK");
534 if ((S_ISDIR (fe->st.st_mode) || link_isdir (fe)) && !DIR_IS_DOTDOT (fe->fname))
535 return _("SUB-DIR");
537 return string_file_size (fe, len);
540 /* --------------------------------------------------------------------------------------------- */
541 /** This functions return a string representation of a file entry type */
543 static const char *
544 string_file_type (file_entry_t * fe, int len)
546 static char buffer[2];
548 (void) len;
550 if (S_ISDIR (fe->st.st_mode))
551 buffer[0] = PATH_SEP;
552 else if (S_ISLNK (fe->st.st_mode))
554 if (link_isdir (fe))
555 buffer[0] = '~';
556 else if (fe->f.stale_link)
557 buffer[0] = '!';
558 else
559 buffer[0] = '@';
561 else if (S_ISCHR (fe->st.st_mode))
562 buffer[0] = '-';
563 else if (S_ISSOCK (fe->st.st_mode))
564 buffer[0] = '=';
565 else if (S_ISDOOR (fe->st.st_mode))
566 buffer[0] = '>';
567 else if (S_ISBLK (fe->st.st_mode))
568 buffer[0] = '+';
569 else if (S_ISFIFO (fe->st.st_mode))
570 buffer[0] = '|';
571 else if (S_ISNAM (fe->st.st_mode))
572 buffer[0] = '#';
573 else if (!S_ISREG (fe->st.st_mode))
574 buffer[0] = '?'; /* non-regular of unknown kind */
575 else if (is_exe (fe->st.st_mode))
576 buffer[0] = '*';
577 else
578 buffer[0] = ' ';
579 buffer[1] = '\0';
580 return buffer;
583 /* --------------------------------------------------------------------------------------------- */
584 /** mtime */
586 static const char *
587 string_file_mtime (file_entry_t * fe, int len)
589 (void) len;
591 return file_date (fe->st.st_mtime);
594 /* --------------------------------------------------------------------------------------------- */
595 /** atime */
597 static const char *
598 string_file_atime (file_entry_t * fe, int len)
600 (void) len;
602 return file_date (fe->st.st_atime);
605 /* --------------------------------------------------------------------------------------------- */
606 /** ctime */
608 static const char *
609 string_file_ctime (file_entry_t * fe, int len)
611 (void) len;
613 return file_date (fe->st.st_ctime);
616 /* --------------------------------------------------------------------------------------------- */
617 /** perm */
619 static const char *
620 string_file_permission (file_entry_t * fe, int len)
622 (void) len;
624 return string_perm (fe->st.st_mode);
627 /* --------------------------------------------------------------------------------------------- */
628 /** mode */
630 static const char *
631 string_file_perm_octal (file_entry_t * fe, int len)
633 static char buffer[10];
635 (void) len;
637 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
638 return buffer;
641 /* --------------------------------------------------------------------------------------------- */
642 /** nlink */
644 static const char *
645 string_file_nlinks (file_entry_t * fe, int len)
647 static char buffer[BUF_TINY];
649 (void) len;
651 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
652 return buffer;
655 /* --------------------------------------------------------------------------------------------- */
656 /** inode */
658 static const char *
659 string_inode (file_entry_t * fe, int len)
661 static char buffer[10];
663 (void) len;
665 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
666 return buffer;
669 /* --------------------------------------------------------------------------------------------- */
670 /** nuid */
672 static const char *
673 string_file_nuid (file_entry_t * fe, int len)
675 static char buffer[10];
677 (void) len;
679 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
680 return buffer;
683 /* --------------------------------------------------------------------------------------------- */
684 /** ngid */
686 static const char *
687 string_file_ngid (file_entry_t * fe, int len)
689 static char buffer[10];
691 (void) len;
693 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
694 return buffer;
697 /* --------------------------------------------------------------------------------------------- */
698 /** owner */
700 static const char *
701 string_file_owner (file_entry_t * fe, int len)
703 (void) len;
705 return get_owner (fe->st.st_uid);
708 /* --------------------------------------------------------------------------------------------- */
709 /** group */
711 static const char *
712 string_file_group (file_entry_t * fe, int len)
714 (void) len;
716 return get_group (fe->st.st_gid);
719 /* --------------------------------------------------------------------------------------------- */
720 /** mark */
722 static const char *
723 string_marked (file_entry_t * fe, int len)
725 (void) len;
727 return fe->f.marked ? "*" : " ";
730 /* --------------------------------------------------------------------------------------------- */
731 /** space */
733 static const char *
734 string_space (file_entry_t * fe, int len)
736 (void) fe;
737 (void) len;
739 return " ";
742 /* --------------------------------------------------------------------------------------------- */
743 /** dot */
745 static const char *
746 string_dot (file_entry_t * fe, int len)
748 (void) fe;
749 (void) len;
751 return ".";
754 /* --------------------------------------------------------------------------------------------- */
756 static int
757 file_compute_color (int attr, file_entry_t * fe)
759 switch (attr)
761 case SELECTED:
762 return (SELECTED_COLOR);
763 case MARKED:
764 return (MARKED_COLOR);
765 case MARKED_SELECTED:
766 return (MARKED_SELECTED_COLOR);
767 case STATUS:
768 return (NORMAL_COLOR);
769 case NORMAL:
770 default:
771 if (!panels_options.filetype_mode)
772 return (NORMAL_COLOR);
775 return mc_fhl_get_color (mc_filehighlight, fe);
778 /* --------------------------------------------------------------------------------------------- */
779 /** Returns the number of items in the given panel */
781 static int
782 panel_items (const WPanel * p)
784 return panel_lines (p) * p->list_cols;
787 /* --------------------------------------------------------------------------------------------- */
788 /** Formats the file number file_index of panel in the buffer dest */
790 static filename_scroll_flag_t
791 format_file (WPanel * panel, int file_index, int width, int attr, gboolean isstatus,
792 int *field_length)
794 int color = NORMAL_COLOR;
795 int length = 0;
796 format_e *format, *home;
797 file_entry_t *fe = NULL;
798 filename_scroll_flag_t res = FILENAME_NOSCROLL;
800 *field_length = 0;
802 if (file_index < panel->dir.len)
804 fe = &panel->dir.list[file_index];
805 color = file_compute_color (attr, fe);
808 home = isstatus ? panel->status_format : panel->format;
810 for (format = home; format != NULL && length != width; format = format->next)
812 if (format->string_fn != NULL)
814 const char *txt = " ";
815 int len, perm = 0;
816 const char *prepared_text;
817 int name_offset = 0;
819 if (fe != NULL)
820 txt = format->string_fn (fe, format->field_len);
822 len = format->field_len;
823 if (len + length > width)
824 len = width - length;
825 if (len <= 0)
826 break;
828 if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
830 int str_len;
831 int i;
833 *field_length = len + 1;
835 str_len = str_length (txt);
836 i = MAX (0, str_len - len);
837 panel->max_shift = MAX (panel->max_shift, i);
838 i = MIN (panel->content_shift, i);
840 if (i > -1)
842 name_offset = str_offset_to_pos (txt, i);
843 if (str_len > len)
845 res = FILENAME_SCROLL_LEFT;
846 if (str_length (txt + name_offset) > len)
847 res |= FILENAME_SCROLL_RIGHT;
852 if (panels_options.permission_mode)
854 if (strcmp (format->id, "perm") == 0)
855 perm = 1;
856 else if (strcmp (format->id, "mode") == 0)
857 perm = 2;
860 if (color >= 0)
861 tty_setcolor (color);
862 else
863 tty_lowlevel_setcolor (-color);
865 if (!isstatus && panel->content_shift > -1)
866 prepared_text =
867 str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
868 else
869 prepared_text = str_fit_to_term (txt, len, format->just_mode);
871 if (perm != 0 && fe != NULL)
872 add_permission_string (prepared_text, format->field_len, fe, attr, color,
873 perm != 1);
874 else
875 tty_print_string (prepared_text);
877 length += len;
879 else
881 if (attr == SELECTED || attr == MARKED_SELECTED)
882 tty_setcolor (SELECTED_COLOR);
883 else
884 tty_setcolor (NORMAL_COLOR);
885 tty_print_one_vline (TRUE);
886 length++;
890 if (length < width)
892 int y, x;
894 tty_getyx (&y, &x);
895 tty_draw_hline (y, x, ' ', width - length);
898 return res;
901 /* --------------------------------------------------------------------------------------------- */
903 static void
904 repaint_file (WPanel * panel, int file_index, gboolean mv, int attr, gboolean isstatus)
906 Widget *w = WIDGET (panel);
908 int nth_column = 0;
909 int width;
910 int offset = 0;
911 filename_scroll_flag_t ret_frm;
912 int ypos = 0;
913 gboolean panel_is_split;
914 int fln = 0;
916 panel_is_split = !isstatus && panel->list_cols > 1;
917 width = w->cols - 2;
919 if (panel_is_split)
921 nth_column = (file_index - panel->top_file) / panel_lines (panel);
922 width /= panel->list_cols;
924 offset = width * nth_column;
926 if (nth_column + 1 >= panel->list_cols)
927 width = w->cols - offset - 2;
930 /* Nothing to paint */
931 if (width <= 0)
932 return;
934 if (mv)
936 ypos = file_index - panel->top_file;
938 if (panel_is_split)
939 ypos %= panel_lines (panel);
941 ypos += 2; /* top frame and header */
942 widget_move (w, ypos, offset + 1);
945 ret_frm = format_file (panel, file_index, width, attr, isstatus, &fln);
947 if (panel_is_split && nth_column + 1 < panel->list_cols)
949 tty_setcolor (NORMAL_COLOR);
950 tty_print_one_vline (TRUE);
953 if (ret_frm != FILENAME_NOSCROLL && mv)
955 if (!panel_is_split && fln > 0)
957 if (panel->list_format != list_long)
958 width = fln;
959 else
961 offset = width - fln + 1;
962 width = fln - 1;
966 widget_move (w, ypos, offset);
967 tty_setcolor (NORMAL_COLOR);
968 tty_print_string (panel_filename_scroll_left_char);
970 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
972 offset += width;
973 if (nth_column + 1 >= panel->list_cols)
974 offset++;
976 widget_move (w, ypos, offset);
977 tty_setcolor (NORMAL_COLOR);
978 tty_print_string (panel_filename_scroll_right_char);
983 /* --------------------------------------------------------------------------------------------- */
985 static void
986 display_mini_info (WPanel * panel)
988 Widget *w = WIDGET (panel);
990 if (!panels_options.show_mini_info)
991 return;
993 widget_move (w, panel_lines (panel) + 3, 1);
995 if (panel->searching)
997 tty_setcolor (INPUT_COLOR);
998 tty_print_char ('/');
999 tty_print_string (str_fit_to_term (panel->search_buffer, w->cols - 3, J_LEFT));
1000 return;
1003 /* Status resolves links and show them */
1004 set_colors (panel);
1006 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
1008 char link_target[MC_MAXPATHLEN];
1009 vfs_path_t *lc_link_vpath;
1010 int len;
1012 lc_link_vpath =
1013 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname,
1014 (char *) NULL);
1015 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
1016 vfs_path_free (lc_link_vpath);
1017 if (len > 0)
1019 link_target[len] = 0;
1020 tty_print_string ("-> ");
1021 tty_print_string (str_fit_to_term (link_target, w->cols - 5, J_LEFT_FIT));
1023 else
1024 tty_print_string (str_fit_to_term (_("<readlink failed>"), w->cols - 2, J_LEFT));
1026 else if (DIR_IS_DOTDOT (panel->dir.list[panel->selected].fname))
1028 /* FIXME:
1029 * while loading directory (dir_list_load() and dir_list_reload()),
1030 * the actual stat info about ".." directory isn't got;
1031 * so just don't display incorrect info about ".." directory */
1032 tty_print_string (str_fit_to_term (_("UP--DIR"), w->cols - 2, J_LEFT));
1034 else
1035 /* Default behavior */
1036 repaint_file (panel, panel->selected, FALSE, STATUS, TRUE);
1039 /* --------------------------------------------------------------------------------------------- */
1041 static void
1042 paint_dir (WPanel * panel)
1044 int i;
1045 int items; /* Number of items */
1047 items = panel_items (panel);
1048 /* reset max len of filename because we have the new max length for the new file list */
1049 panel->max_shift = -1;
1051 for (i = 0; i < items; i++)
1053 int color = 0; /* Color value of the line */
1055 if (i + panel->top_file < panel->dir.len)
1057 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1058 color += (panel->selected == i + panel->top_file && panel->active);
1061 repaint_file (panel, i + panel->top_file, TRUE, color, FALSE);
1064 tty_set_normal_attrs ();
1067 /* --------------------------------------------------------------------------------------------- */
1069 static void
1070 display_total_marked_size (const WPanel * panel, int y, int x, gboolean size_only)
1072 const Widget *w = CONST_WIDGET (panel);
1074 char buffer[BUF_SMALL], b_bytes[BUF_SMALL];
1075 const char *buf;
1076 int cols;
1078 if (panel->marked <= 0)
1079 return;
1081 buf = size_only ? b_bytes : buffer;
1082 cols = w->cols - 2;
1084 g_strlcpy (b_bytes, size_trunc_sep (panel->total, panels_options.kilobyte_si),
1085 sizeof (b_bytes));
1087 if (!size_only)
1088 g_snprintf (buffer, sizeof (buffer),
1089 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1090 b_bytes, panel->marked);
1092 /* don't forget spaces around buffer content */
1093 buf = str_trunc (buf, cols - 4);
1095 if (x < 0)
1096 /* center in panel */
1097 x = (w->cols - str_term_width1 (buf)) / 2 - 1;
1100 * y == panel_lines (panel) + 2 for mini_info_separator
1101 * y == w->lines - 1 for panel bottom frame
1103 widget_move (w, y, x);
1104 tty_setcolor (MARKED_COLOR);
1105 tty_printf (" %s ", buf);
1108 /* --------------------------------------------------------------------------------------------- */
1110 static void
1111 mini_info_separator (const WPanel * panel)
1113 if (panels_options.show_mini_info)
1115 const Widget *w = CONST_WIDGET (panel);
1116 int y;
1118 y = panel_lines (panel) + 2;
1120 tty_setcolor (NORMAL_COLOR);
1121 tty_draw_hline (w->y + y, w->x + 1, ACS_HLINE, w->cols - 2);
1122 /* Status displays total marked size.
1123 * Centered in panel, full format. */
1124 display_total_marked_size (panel, y, -1, FALSE);
1128 /* --------------------------------------------------------------------------------------------- */
1130 static void
1131 show_free_space (const WPanel * panel)
1133 /* Used to figure out how many free space we have */
1134 static struct my_statfs myfs_stats;
1135 /* Old current working directory for displaying free space */
1136 static char *old_cwd = NULL;
1138 /* Don't try to stat non-local fs */
1139 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1140 return;
1142 if (old_cwd == NULL || strcmp (old_cwd, vfs_path_as_str (panel->cwd_vpath)) != 0)
1144 char rpath[PATH_MAX];
1146 init_my_statfs ();
1147 g_free (old_cwd);
1148 old_cwd = g_strdup (vfs_path_as_str (panel->cwd_vpath));
1150 if (mc_realpath (old_cwd, rpath) == NULL)
1151 return;
1153 my_statfs (&myfs_stats, rpath);
1156 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1158 const Widget *w = CONST_WIDGET (panel);
1159 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1161 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1162 panels_options.kilobyte_si);
1163 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1164 panels_options.kilobyte_si);
1165 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1166 myfs_stats.total == 0 ? 0 :
1167 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1168 widget_move (w, w->lines - 1, w->cols - 2 - (int) strlen (tmp));
1169 tty_setcolor (NORMAL_COLOR);
1170 tty_print_string (tmp);
1174 /* --------------------------------------------------------------------------------------------- */
1176 * Prepare path string for showing in panel's header.
1177 * Passwords will removed, also home dir will replaced by ~
1179 * @param panel WPanel object
1181 * @return newly allocated string.
1184 static char *
1185 panel_correct_path_to_show (const WPanel * panel)
1187 vfs_path_t *last_vpath;
1188 const vfs_path_element_t *path_element;
1189 char *return_path;
1190 int elements_count;
1192 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1194 /* get last path element */
1195 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1197 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1198 strcmp (path_element->class->name, "extfs") == 0 ||
1199 strcmp (path_element->class->name, "tarfs") == 0))
1201 const char *archive_name;
1202 const vfs_path_element_t *prev_path_element;
1204 /* get previous path element for catching archive name */
1205 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1206 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1207 if (archive_name != NULL)
1208 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1209 else
1211 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1212 last_vpath->relative = TRUE;
1215 else
1217 last_vpath = vfs_path_new ();
1218 last_vpath->relative = TRUE;
1221 vfs_path_add_element (last_vpath, path_element);
1222 return_path =
1223 vfs_path_to_str_flags (last_vpath, 0,
1224 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1225 vfs_path_free (last_vpath);
1227 return return_path;
1230 /* --------------------------------------------------------------------------------------------- */
1232 * Get Current path element encoding
1234 * @param panel WPanel object
1236 * @return newly allocated string or NULL if path charset is same as system charset
1239 #ifdef HAVE_CHARSET
1240 static char *
1241 panel_get_encoding_info_str (const WPanel * panel)
1243 char *ret_str = NULL;
1244 const vfs_path_element_t *path_element;
1246 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1247 if (path_element->encoding != NULL)
1248 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1250 return ret_str;
1252 #endif
1254 /* --------------------------------------------------------------------------------------------- */
1256 static void
1257 show_dir (const WPanel * panel)
1259 const Widget *w = CONST_WIDGET (panel);
1261 gchar *tmp;
1263 set_colors (panel);
1264 tty_draw_box (w->y, w->x, w->lines, w->cols, FALSE);
1266 if (panels_options.show_mini_info)
1268 int y;
1270 y = panel_lines (panel) + 2;
1272 widget_move (w, y, 0);
1273 tty_print_alt_char (ACS_LTEE, FALSE);
1274 widget_move (w, y, w->cols - 1);
1275 tty_print_alt_char (ACS_RTEE, FALSE);
1278 widget_move (w, 0, 1);
1279 tty_print_string (panel_history_prev_item_sign);
1281 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1282 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1283 panel_history_next_item_sign);
1285 widget_move (w, 0, w->cols - 6);
1286 tty_print_string (tmp);
1288 g_free (tmp);
1290 widget_move (w, 0, 3);
1292 if (panel->is_panelized)
1293 tty_printf (" %s ", _("Panelize"));
1294 #ifdef HAVE_CHARSET
1295 else
1297 tmp = panel_get_encoding_info_str (panel);
1298 if (tmp != NULL)
1300 tty_printf ("%s", tmp);
1301 widget_move (w, 0, 3 + strlen (tmp));
1302 g_free (tmp);
1305 #endif
1307 if (panel->active)
1308 tty_setcolor (REVERSE_COLOR);
1310 tmp = panel_correct_path_to_show (panel);
1311 tty_printf (" %s ", str_term_trim (tmp, MIN (MAX (w->cols - 12, 0), w->cols)));
1312 g_free (tmp);
1314 if (!panels_options.show_mini_info)
1316 if (panel->marked == 0)
1318 /* Show size of curret file in the bottom of panel */
1319 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1321 char buffer[BUF_SMALL];
1323 g_snprintf (buffer, sizeof (buffer), " %s ",
1324 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1325 panels_options.kilobyte_si));
1326 tty_setcolor (NORMAL_COLOR);
1327 widget_move (w, w->lines - 1, 4);
1328 tty_print_string (buffer);
1331 else
1333 /* Show total size of marked files
1334 * In the bottom of panel, display size only. */
1335 display_total_marked_size (panel, w->lines - 1, 2, TRUE);
1339 show_free_space (panel);
1341 if (panel->active)
1342 tty_set_normal_attrs ();
1345 /* --------------------------------------------------------------------------------------------- */
1347 static void
1348 adjust_top_file (WPanel * panel)
1350 int items;
1352 /* Update panel->selected to avoid out of range in panel->dir.list[panel->selected]
1353 * when panel is redrawing when directory is reloading, for example in path:
1354 * dir_list_reload() -> mc_refresh() -> dialog_change_screen_size() ->
1355 * midnight_callback (MSG_RESIZE) -> setup_panels() -> panel_callback(MSG_DRAW) ->
1356 * display_mini_info()
1358 panel->selected = CLAMP (panel->selected, 0, panel->dir.len - 1);
1360 items = panel_items (panel);
1362 if (panel->dir.len <= items)
1364 /* If all files fit, show them all. */
1365 panel->top_file = 0;
1367 else
1369 int i;
1371 /* top_file has to be in the range [selected-items+1, selected] so that
1372 the selected file is visible.
1373 top_file should be in the range [0, count-items] so that there's
1374 no empty space wasted.
1375 Within these ranges, adjust it by as little as possible. */
1377 if (panel->top_file < 0)
1378 panel->top_file = 0;
1380 i = panel->selected - items + 1;
1381 if (panel->top_file < i)
1382 panel->top_file = i;
1384 i = panel->dir.len - items;
1385 if (panel->top_file > i)
1386 panel->top_file = i;
1388 if (panel->top_file > panel->selected)
1389 panel->top_file = panel->selected;
1393 /* --------------------------------------------------------------------------------------------- */
1394 /** add "#enc:encodning" to end of path */
1395 /* if path end width a previous #enc:, only encoding is changed no additional
1396 * #enc: is appended
1397 * retun new string
1400 static char *
1401 panel_save_name (WPanel * panel)
1403 /* If the program is shuting down */
1404 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1405 return g_strdup (panel->panel_name);
1407 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1410 /* --------------------------------------------------------------------------------------------- */
1412 static void
1413 directory_history_add (WPanel * panel, const vfs_path_t * vpath)
1415 char *tmp;
1417 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1418 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1419 panel->dir_history_current = panel->dir_history;
1422 /* --------------------------------------------------------------------------------------------- */
1424 /* "history_load" event handler */
1425 static gboolean
1426 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1427 gpointer init_data, gpointer data)
1429 WPanel *p = PANEL (init_data);
1430 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1432 (void) event_group_name;
1433 (void) event_name;
1435 if (ev->receiver == NULL || ev->receiver == WIDGET (p))
1437 if (ev->cfg != NULL)
1438 p->dir_history = history_load (ev->cfg, p->hist_name);
1439 else
1440 p->dir_history = history_get (p->hist_name);
1442 directory_history_add (p, p->cwd_vpath);
1445 return TRUE;
1448 /* --------------------------------------------------------------------------------------------- */
1450 /* "history_save" event handler */
1451 static gboolean
1452 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1453 gpointer init_data, gpointer data)
1455 WPanel *p = PANEL (init_data);
1457 (void) event_group_name;
1458 (void) event_name;
1460 if (p->dir_history != NULL)
1462 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1464 history_save (ev->cfg, p->hist_name, p->dir_history);
1467 return TRUE;
1470 /* --------------------------------------------------------------------------------------------- */
1472 static void
1473 panel_destroy (WPanel * p)
1475 size_t i;
1477 if (panels_options.auto_save_setup)
1479 char *name;
1481 name = panel_save_name (p);
1482 panel_save_setup (p, name);
1483 g_free (name);
1486 panel_clean_dir (p);
1488 /* clean history */
1489 if (p->dir_history != NULL)
1491 /* directory history is already saved before this moment */
1492 p->dir_history = g_list_first (p->dir_history);
1493 g_list_free_full (p->dir_history, g_free);
1495 g_free (p->hist_name);
1497 delete_format (p->format);
1498 delete_format (p->status_format);
1500 g_free (p->user_format);
1501 for (i = 0; i < LIST_FORMATS; i++)
1502 g_free (p->user_status_format[i]);
1504 g_free (p->dir.list);
1505 g_free (p->panel_name);
1507 vfs_path_free (p->lwd_vpath);
1508 vfs_path_free (p->cwd_vpath);
1511 /* --------------------------------------------------------------------------------------------- */
1513 static inline void
1514 panel_format_modified (WPanel * panel)
1516 panel->format_modified = 1;
1519 /* --------------------------------------------------------------------------------------------- */
1521 static void
1522 panel_paint_sort_info (const WPanel * panel)
1524 if (*panel->sort_field->hotkey != '\0')
1526 const char *sort_sign =
1527 panel->sort_info.reverse ? panel_sort_up_sign : panel_sort_down_sign;
1528 char *str;
1530 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_field->hotkey));
1531 widget_move (panel, 1, 1);
1532 tty_print_string (str);
1533 g_free (str);
1537 /* --------------------------------------------------------------------------------------------- */
1539 static const char *
1540 panel_get_title_without_hotkey (const char *title)
1542 static char translated_title[BUF_TINY];
1544 if (title == NULL || title[0] == '\0')
1545 translated_title[0] = '\0';
1546 else
1548 char *hkey;
1550 g_snprintf (translated_title, sizeof (translated_title), "%s", _(title));
1552 hkey = strchr (translated_title, '&');
1553 if (hkey != NULL && hkey[1] != '\0')
1554 memmove (hkey, hkey + 1, strlen (hkey));
1557 return translated_title;
1560 /* --------------------------------------------------------------------------------------------- */
1562 static void
1563 panel_print_header (const WPanel * panel)
1565 const Widget *w = CONST_WIDGET (panel);
1567 int y, x;
1568 int i;
1569 GString *format_txt;
1571 widget_move (w, 1, 1);
1572 tty_getyx (&y, &x);
1573 tty_setcolor (NORMAL_COLOR);
1574 tty_draw_hline (y, x, ' ', w->cols - 2);
1576 format_txt = g_string_new ("");
1578 for (i = 0; i < panel->list_cols; i++)
1580 format_e *format;
1582 for (format = panel->format; format != NULL; format = format->next)
1584 if (format->string_fn != NULL)
1586 g_string_set_size (format_txt, 0);
1588 if (panel->list_format == list_long
1589 && strcmp (format->id, panel->sort_field->id) == 0)
1590 g_string_append (format_txt,
1591 panel->sort_info.reverse
1592 ? panel_sort_up_sign : panel_sort_down_sign);
1594 g_string_append (format_txt, format->title);
1596 if (panel->filter != NULL && strcmp (format->id, "name") == 0)
1598 g_string_append (format_txt, " [");
1599 g_string_append (format_txt, panel->filter);
1600 g_string_append (format_txt, "]");
1603 tty_setcolor (HEADER_COLOR);
1604 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1605 J_CENTER_LEFT));
1607 else
1609 tty_setcolor (NORMAL_COLOR);
1610 tty_print_one_vline (TRUE);
1614 if (i < panel->list_cols - 1)
1616 tty_setcolor (NORMAL_COLOR);
1617 tty_print_one_vline (TRUE);
1621 g_string_free (format_txt, TRUE);
1623 if (panel->list_format != list_long)
1624 panel_paint_sort_info (panel);
1627 /* --------------------------------------------------------------------------------------------- */
1629 static const char *
1630 parse_panel_size (WPanel * panel, const char *format, gboolean isstatus)
1632 panel_display_t frame = frame_half;
1633 format = skip_separators (format);
1635 if (strncmp (format, "full", 4) == 0)
1637 frame = frame_full;
1638 format += 4;
1640 else if (strncmp (format, "half", 4) == 0)
1642 frame = frame_half;
1643 format += 4;
1646 if (!isstatus)
1648 panel->frame_size = frame;
1649 panel->list_cols = 1;
1652 /* Now, the optional column specifier */
1653 format = skip_separators (format);
1655 if (g_ascii_isdigit (*format))
1657 if (!isstatus)
1659 panel->list_cols = g_ascii_digit_value (*format);
1660 if (panel->list_cols < 1)
1661 panel->list_cols = 1;
1664 format++;
1667 if (!isstatus)
1668 panel_update_cols (WIDGET (panel), panel->frame_size);
1670 return skip_separators (format);
1673 /* Format is:
1675 all := panel_format? format
1676 panel_format := [full|half] [1-9]
1677 format := one_format_e
1678 | format , one_format_e
1680 one_format_e := just format.id [opt_size]
1681 just := [<=>]
1682 opt_size := : size [opt_expand]
1683 size := [0-9]+
1684 opt_expand := +
1688 /* --------------------------------------------------------------------------------------------- */
1690 static format_e *
1691 parse_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus,
1692 int *res_total_cols)
1694 format_e *darr, *old = NULL, *home = NULL; /* The formats we return */
1695 int total_cols = 0; /* Used columns by the format */
1696 size_t i;
1698 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1700 *error = NULL;
1702 if (i18n_timelength == 0)
1704 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1706 for (i = 0; panel_fields[i].id != NULL; i++)
1707 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1708 panel_fields[i].min_size = i18n_timelength;
1712 * This makes sure that the panel and mini status full/half mode
1713 * setting is equal
1715 format = parse_panel_size (panel, format, isstatus);
1717 while (*format != '\0')
1718 { /* format can be an empty string */
1719 align_crt_t justify; /* Which mode. */
1720 gboolean set_justify = TRUE; /* flag: set justification mode? */
1721 gboolean found = FALSE;
1723 darr = g_new0 (format_e, 1);
1725 /* I'm so ugly, don't look at me :-) */
1726 if (home == NULL)
1727 home = old = darr;
1729 old->next = darr;
1730 darr->next = NULL;
1731 old = darr;
1733 format = skip_separators (format);
1735 switch (*format)
1737 case '<':
1738 justify = J_LEFT;
1739 format = skip_separators (format + 1);
1740 break;
1741 case '=':
1742 justify = J_CENTER;
1743 format = skip_separators (format + 1);
1744 break;
1745 case '>':
1746 justify = J_RIGHT;
1747 format = skip_separators (format + 1);
1748 break;
1749 default:
1750 justify = J_LEFT;
1751 set_justify = FALSE;
1752 break;
1755 for (i = 0; panel_fields[i].id != NULL; i++)
1757 size_t klen;
1759 klen = strlen (panel_fields[i].id);
1761 if (strncmp (format, panel_fields[i].id, klen) != 0)
1762 continue;
1764 format += klen;
1766 darr->requested_field_len = panel_fields[i].min_size;
1767 darr->string_fn = panel_fields[i].string_fn;
1768 darr->title = g_strdup (panel_get_title_without_hotkey (panel_fields[i].title_hotkey));
1770 darr->id = panel_fields[i].id;
1771 darr->expand = panel_fields[i].expands;
1772 darr->just_mode = panel_fields[i].default_just;
1774 if (set_justify)
1776 if (IS_FIT (darr->just_mode))
1777 darr->just_mode = MAKE_FIT (justify);
1778 else
1779 darr->just_mode = justify;
1781 found = TRUE;
1783 format = skip_separators (format);
1785 /* If we have a size specifier */
1786 if (*format == ':')
1788 int req_length;
1790 /* If the size was specified, we don't want
1791 * auto-expansion by default
1793 darr->expand = FALSE;
1794 format++;
1795 req_length = atoi (format);
1796 darr->requested_field_len = req_length;
1798 format = skip_numbers (format);
1800 /* Now, if they insist on expansion */
1801 if (*format == '+')
1803 darr->expand = TRUE;
1804 format++;
1809 break;
1812 if (!found)
1814 size_t pos;
1815 char *tmp_format;
1817 pos = strlen (format);
1818 if (pos > 8)
1819 pos = 8;
1821 tmp_format = g_strndup (format, pos);
1822 delete_format (home);
1823 *error =
1824 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1825 g_free (tmp_format);
1827 return NULL;
1830 total_cols += darr->requested_field_len;
1833 *res_total_cols = total_cols;
1834 return home;
1837 /* --------------------------------------------------------------------------------------------- */
1839 static format_e *
1840 use_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus)
1842 #define MAX_EXPAND 4
1843 int expand_top = 0; /* Max used element in expand */
1844 int usable_columns; /* Usable columns in the panel */
1845 int total_cols = 0;
1846 format_e *darr, *home;
1848 if (format == NULL)
1849 format = DEFAULT_USER_FORMAT;
1851 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1853 if (*error != NULL)
1854 return NULL;
1856 panel->dirty = 1;
1858 usable_columns = WIDGET (panel)->cols - 2;
1859 /* Status needn't to be split */
1860 if (!isstatus)
1862 usable_columns /= panel->list_cols;
1863 if (panel->list_cols > 1)
1864 usable_columns--;
1867 /* Look for the expandable fields and set field_len based on the requested field len */
1868 for (darr = home; darr != NULL && expand_top < MAX_EXPAND; darr = darr->next)
1870 darr->field_len = darr->requested_field_len;
1871 if (darr->expand)
1872 expand_top++;
1875 /* If we used more columns than the available columns, adjust that */
1876 if (total_cols > usable_columns)
1878 int dif;
1879 int pdif = 0;
1881 dif = total_cols - usable_columns;
1883 while (dif != 0 && pdif != dif)
1885 pdif = dif;
1887 for (darr = home; darr; darr = darr->next)
1888 if (dif != 0 && darr->field_len != 1)
1890 darr->field_len--;
1891 dif--;
1895 total_cols = usable_columns; /* give up, the rest should be truncated */
1898 /* Expand the available space */
1899 if (usable_columns > total_cols && expand_top != 0)
1901 int i;
1902 int spaces = (usable_columns - total_cols) / expand_top;
1904 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1905 if (darr->expand)
1907 darr->field_len += spaces;
1908 if (i == 0)
1909 darr->field_len += (usable_columns - total_cols) % expand_top;
1910 i++;
1913 return home;
1916 /* --------------------------------------------------------------------------------------------- */
1917 /** Given the panel->view_type returns the format string to be parsed */
1919 static const char *
1920 panel_format (WPanel * panel)
1923 switch (panel->list_format)
1925 case list_long:
1926 return "full perm space nlink space owner space group space size space mtime space name";
1928 case list_brief:
1930 static char format[BUF_TINY];
1931 int brief_cols = panel->brief_cols;
1933 if (brief_cols < 1)
1934 brief_cols = 2;
1936 if (brief_cols > 9)
1937 brief_cols = 9;
1939 g_snprintf (format, sizeof (format), "half %d type name", brief_cols);
1940 return format;
1943 case list_user:
1944 return panel->user_format;
1946 default:
1947 case list_full:
1948 return "half type name | size | mtime";
1952 /* --------------------------------------------------------------------------------------------- */
1954 static const char *
1955 mini_status_format (WPanel * panel)
1957 if (panel->user_mini_status)
1958 return panel->user_status_format[panel->list_format];
1960 switch (panel->list_format)
1962 case list_long:
1963 return "full perm space nlink space owner space group space size space mtime space name";
1965 case list_brief:
1966 return "half type name space bsize space perm space";
1968 case list_full:
1969 return "half type name";
1971 default:
1972 case list_user:
1973 return panel->user_format;
1977 /* */
1978 /* Panel operation commands */
1979 /* */
1981 /* --------------------------------------------------------------------------------------------- */
1982 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1984 static cb_ret_t
1985 maybe_cd (gboolean move_up_dir)
1987 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1989 if (move_up_dir)
1991 vfs_path_t *up_dir;
1993 up_dir = vfs_path_from_str ("..");
1994 do_cd (up_dir, cd_exact);
1995 vfs_path_free (up_dir);
1996 return MSG_HANDLED;
1999 if (S_ISDIR (selection (current_panel)->st.st_mode)
2000 || link_isdir (selection (current_panel)))
2002 vfs_path_t *vpath;
2004 vpath = vfs_path_from_str (selection (current_panel)->fname);
2005 do_cd (vpath, cd_exact);
2006 vfs_path_free (vpath);
2007 return MSG_HANDLED;
2011 return MSG_NOT_HANDLED;
2014 /* --------------------------------------------------------------------------------------------- */
2016 /* if command line is empty then do 'cd ..' */
2017 static cb_ret_t
2018 force_maybe_cd (void)
2020 if (cmdline->buffer[0] == '\0')
2022 vfs_path_t *up_dir = vfs_path_from_str ("..");
2023 do_cd (up_dir, cd_exact);
2024 vfs_path_free (up_dir);
2025 return MSG_HANDLED;
2028 return MSG_NOT_HANDLED;
2031 /* --------------------------------------------------------------------------------------------- */
2033 static inline void
2034 unselect_item (WPanel * panel)
2036 repaint_file (panel, panel->selected, TRUE, 2 * selection (panel)->f.marked, FALSE);
2039 /* --------------------------------------------------------------------------------------------- */
2040 /** Select/unselect all the files like a current file by extension */
2042 static void
2043 panel_select_ext_cmd (void)
2045 gboolean do_select = !selection (current_panel)->f.marked;
2046 char *filename = selection (current_panel)->fname;
2047 char *reg_exp, *cur_file_ext;
2048 mc_search_t *search;
2049 int i;
2051 if (filename == NULL)
2052 return;
2054 cur_file_ext = strutils_regex_escape (extension (filename));
2056 if (cur_file_ext[0] != '\0')
2057 reg_exp = g_strconcat ("^.*\\.", cur_file_ext, "$", (char *) NULL);
2058 else
2059 reg_exp = g_strdup ("^[^\\.]+$");
2061 g_free (cur_file_ext);
2063 search = mc_search_new (reg_exp, NULL);
2064 search->search_type = MC_SEARCH_T_REGEX;
2065 search->is_case_sensitive = FALSE;
2067 for (i = 0; i < current_panel->dir.len; i++)
2069 file_entry_t *file_entry = &current_panel->dir.list[i];
2071 if (DIR_IS_DOTDOT (file_entry->fname) || S_ISDIR (file_entry->st.st_mode))
2072 continue;
2074 if (!mc_search_run (search, file_entry->fname, 0, file_entry->fnamelen, NULL))
2075 continue;
2077 do_file_mark (current_panel, i, do_select);
2080 mc_search_free (search);
2081 g_free (reg_exp);
2085 /* --------------------------------------------------------------------------------------------- */
2087 static int
2088 panel_selected_at_half (const WPanel * panel)
2090 int lines, top;
2092 lines = panel_lines (panel);
2094 /* define top file of column */
2095 top = panel->top_file;
2096 if (panel->list_cols > 1)
2097 top += lines * ((panel->selected - top) / lines);
2099 return (panel->selected - top - lines / 2);
2102 /* --------------------------------------------------------------------------------------------- */
2104 static void
2105 move_down (WPanel * panel)
2107 int items;
2109 if (panel->selected + 1 == panel->dir.len)
2110 return;
2112 unselect_item (panel);
2113 panel->selected++;
2115 items = panel_items (panel);
2117 if (panels_options.scroll_pages && panel->selected - panel->top_file == items)
2119 /* Scroll window half screen */
2120 panel->top_file += items / 2;
2121 if (panel->top_file > panel->dir.len - items)
2122 panel->top_file = panel->dir.len - items;
2123 paint_dir (panel);
2125 else if (panels_options.scroll_center && panel_selected_at_half (panel) > 0)
2127 /* Scroll window when cursor is halfway down */
2128 panel->top_file++;
2129 if (panel->top_file > panel->dir.len - items)
2130 panel->top_file = panel->dir.len - items;
2132 select_item (panel);
2135 /* --------------------------------------------------------------------------------------------- */
2137 static void
2138 move_up (WPanel * panel)
2140 if (panel->selected == 0)
2141 return;
2143 unselect_item (panel);
2144 panel->selected--;
2146 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2148 /* Scroll window half screen */
2149 panel->top_file -= panel_items (panel) / 2;
2150 if (panel->top_file < 0)
2151 panel->top_file = 0;
2152 paint_dir (panel);
2154 else if (panels_options.scroll_center && panel_selected_at_half (panel) < 0)
2156 /* Scroll window when cursor is halfway up */
2157 panel->top_file--;
2158 if (panel->top_file < 0)
2159 panel->top_file = 0;
2161 select_item (panel);
2164 /* --------------------------------------------------------------------------------------------- */
2165 /** Changes the selection by lines (may be negative) */
2167 static void
2168 move_selection (WPanel * panel, int lines)
2170 int new_pos;
2171 gboolean adjust = FALSE;
2173 new_pos = panel->selected + lines;
2174 if (new_pos >= panel->dir.len)
2175 new_pos = panel->dir.len - 1;
2177 if (new_pos < 0)
2178 new_pos = 0;
2180 unselect_item (panel);
2181 panel->selected = new_pos;
2183 if (panel->selected - panel->top_file >= panel_items (panel))
2185 panel->top_file += lines;
2186 adjust = TRUE;
2189 if (panel->selected - panel->top_file < 0)
2191 panel->top_file += lines;
2192 adjust = TRUE;
2195 if (adjust)
2197 if (panel->top_file > panel->selected)
2198 panel->top_file = panel->selected;
2199 if (panel->top_file < 0)
2200 panel->top_file = 0;
2201 paint_dir (panel);
2203 select_item (panel);
2206 /* --------------------------------------------------------------------------------------------- */
2208 static cb_ret_t
2209 move_left (WPanel * panel)
2211 if (panel->list_cols > 1)
2213 move_selection (panel, -panel_lines (panel));
2214 return MSG_HANDLED;
2217 return maybe_cd (TRUE); /* cd .. */
2220 /* --------------------------------------------------------------------------------------------- */
2222 static cb_ret_t
2223 move_right (WPanel * panel)
2225 if (panel->list_cols > 1)
2227 move_selection (panel, panel_lines (panel));
2228 return MSG_HANDLED;
2231 return maybe_cd (FALSE); /* cd (selection) */
2234 /* --------------------------------------------------------------------------------------------- */
2236 static void
2237 prev_page (WPanel * panel)
2239 int items;
2241 if (!panel->selected && !panel->top_file)
2242 return;
2244 unselect_item (panel);
2245 items = panel_items (panel);
2246 if (panel->top_file < items)
2247 items = panel->top_file;
2248 if (items == 0)
2249 panel->selected = 0;
2250 else
2251 panel->selected -= items;
2252 panel->top_file -= items;
2254 select_item (panel);
2255 paint_dir (panel);
2258 /* --------------------------------------------------------------------------------------------- */
2260 static void
2261 goto_parent_dir (WPanel * panel)
2263 if (!panel->is_panelized)
2265 vfs_path_t *up_dir;
2267 up_dir = vfs_path_from_str ("..");
2268 do_cd (up_dir, cd_exact);
2269 vfs_path_free (up_dir);
2271 else
2273 char *fname = panel->dir.list[panel->selected].fname;
2274 const char *bname;
2275 vfs_path_t *dname_vpath;
2277 if (g_path_is_absolute (fname))
2278 fname = g_strdup (fname);
2279 else
2280 fname =
2281 mc_build_filename (vfs_path_as_str (panelized_panel.root_vpath), fname,
2282 (char *) NULL);
2284 bname = x_basename (fname);
2286 if (bname == fname)
2287 dname_vpath = vfs_path_from_str (".");
2288 else
2290 char *dname;
2292 dname = g_strndup (fname, bname - fname);
2293 dname_vpath = vfs_path_from_str (dname);
2294 g_free (dname);
2297 do_cd (dname_vpath, cd_exact);
2298 try_to_select (panel, bname);
2300 vfs_path_free (dname_vpath);
2301 g_free (fname);
2305 /* --------------------------------------------------------------------------------------------- */
2307 static void
2308 next_page (WPanel * panel)
2310 int items;
2312 if (panel->selected == panel->dir.len - 1)
2313 return;
2315 unselect_item (panel);
2316 items = panel_items (panel);
2317 if (panel->top_file > panel->dir.len - 2 * items)
2318 items = panel->dir.len - items - panel->top_file;
2319 if (panel->top_file + items < 0)
2320 items = -panel->top_file;
2321 if (items == 0)
2322 panel->selected = panel->dir.len - 1;
2323 else
2324 panel->selected += items;
2325 panel->top_file += items;
2327 select_item (panel);
2328 paint_dir (panel);
2331 /* --------------------------------------------------------------------------------------------- */
2333 static void
2334 goto_child_dir (WPanel * panel)
2336 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2338 vfs_path_t *vpath;
2340 vpath = vfs_path_from_str (selection (panel)->fname);
2341 do_cd (vpath, cd_exact);
2342 vfs_path_free (vpath);
2346 /* --------------------------------------------------------------------------------------------- */
2348 static void
2349 goto_top_file (WPanel * panel)
2351 unselect_item (panel);
2352 panel->selected = panel->top_file;
2353 select_item (panel);
2356 /* --------------------------------------------------------------------------------------------- */
2358 static void
2359 goto_middle_file (WPanel * panel)
2361 unselect_item (panel);
2362 panel->selected = panel->top_file + panel_items (panel) / 2;
2363 select_item (panel);
2366 /* --------------------------------------------------------------------------------------------- */
2368 static void
2369 goto_bottom_file (WPanel * panel)
2371 unselect_item (panel);
2372 panel->selected = panel->top_file + panel_items (panel) - 1;
2373 select_item (panel);
2376 /* --------------------------------------------------------------------------------------------- */
2378 static void
2379 move_home (WPanel * panel)
2381 if (panel->selected == 0)
2382 return;
2384 unselect_item (panel);
2386 if (panels_options.torben_fj_mode)
2388 int middle_pos = panel->top_file + panel_items (panel) / 2;
2390 if (panel->selected > middle_pos)
2392 goto_middle_file (panel);
2393 return;
2395 if (panel->selected != panel->top_file)
2397 goto_top_file (panel);
2398 return;
2402 panel->top_file = 0;
2403 panel->selected = 0;
2405 paint_dir (panel);
2406 select_item (panel);
2409 /* --------------------------------------------------------------------------------------------- */
2411 static void
2412 move_end (WPanel * panel)
2414 if (panel->selected == panel->dir.len - 1)
2415 return;
2417 unselect_item (panel);
2419 if (panels_options.torben_fj_mode)
2421 int items, middle_pos;
2423 items = panel_items (panel);
2424 middle_pos = panel->top_file + items / 2;
2426 if (panel->selected < middle_pos)
2428 goto_middle_file (panel);
2429 return;
2431 if (panel->selected != panel->top_file + items - 1)
2433 goto_bottom_file (panel);
2434 return;
2438 panel->selected = panel->dir.len - 1;
2439 paint_dir (panel);
2440 select_item (panel);
2443 /* --------------------------------------------------------------------------------------------- */
2445 static void
2446 do_mark_file (WPanel * panel, mark_act_t do_move)
2448 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2449 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2450 move_down (panel);
2451 else if (do_move == MARK_FORCE_UP)
2452 move_up (panel);
2455 /* --------------------------------------------------------------------------------------------- */
2457 static inline void
2458 mark_file (WPanel * panel)
2460 do_mark_file (panel, MARK_DOWN);
2463 /* --------------------------------------------------------------------------------------------- */
2465 static inline void
2466 mark_file_up (WPanel * panel)
2468 do_mark_file (panel, MARK_FORCE_UP);
2471 /* --------------------------------------------------------------------------------------------- */
2473 static inline void
2474 mark_file_down (WPanel * panel)
2476 do_mark_file (panel, MARK_FORCE_DOWN);
2479 /* --------------------------------------------------------------------------------------------- */
2481 static void
2482 mark_file_right (WPanel * panel)
2484 int lines;
2486 if (state_mark < 0)
2487 state_mark = selection (panel)->f.marked ? 0 : 1;
2489 lines = panel_lines (panel);
2490 lines = MIN (lines, panel->dir.len - panel->selected - 1);
2491 for (; lines != 0; lines--)
2493 do_file_mark (panel, panel->selected, state_mark);
2494 move_down (panel);
2496 do_file_mark (panel, panel->selected, state_mark);
2499 /* --------------------------------------------------------------------------------------------- */
2501 static void
2502 mark_file_left (WPanel * panel)
2504 int lines;
2506 if (state_mark < 0)
2507 state_mark = selection (panel)->f.marked ? 0 : 1;
2509 lines = panel_lines (panel);
2510 lines = MIN (lines, panel->selected + 1);
2511 for (; lines != 0; lines--)
2513 do_file_mark (panel, panel->selected, state_mark);
2514 move_up (panel);
2516 do_file_mark (panel, panel->selected, state_mark);
2519 /* --------------------------------------------------------------------------------------------- */
2521 static void
2522 panel_select_unselect_files (WPanel * panel, const char *title, const char *history_name,
2523 gboolean do_select)
2525 gboolean files_only = (panels_options.select_flags & SELECT_FILES_ONLY) != 0;
2526 gboolean case_sens = (panels_options.select_flags & SELECT_MATCH_CASE) != 0;
2527 gboolean shell_patterns = (panels_options.select_flags & SELECT_SHELL_PATTERNS) != 0;
2529 char *reg_exp;
2530 mc_search_t *search;
2531 int i;
2533 quick_widget_t quick_widgets[] = {
2534 /* *INDENT-OFF* */
2535 QUICK_INPUT (INPUT_LAST_TEXT, history_name, &reg_exp, NULL,
2536 FALSE, FALSE, INPUT_COMPLETE_FILENAMES),
2537 QUICK_START_COLUMNS,
2538 QUICK_CHECKBOX (N_("&Files only"), &files_only, NULL),
2539 QUICK_CHECKBOX (N_("&Using shell patterns"), &shell_patterns, NULL),
2540 QUICK_NEXT_COLUMN,
2541 QUICK_CHECKBOX (N_("&Case sensitive"), &case_sens, NULL),
2542 QUICK_STOP_COLUMNS,
2543 QUICK_END
2544 /* *INDENT-ON* */
2547 quick_dialog_t qdlg = {
2548 -1, -1, 50,
2549 title, "[Select/Unselect Files]",
2550 quick_widgets, NULL, NULL
2553 if (quick_dialog (&qdlg) == B_CANCEL)
2554 return;
2556 if (reg_exp == NULL || *reg_exp == '\0')
2558 g_free (reg_exp);
2559 return;
2562 search = mc_search_new (reg_exp, NULL);
2563 search->search_type = shell_patterns ? MC_SEARCH_T_GLOB : MC_SEARCH_T_REGEX;
2564 search->is_entire_line = TRUE;
2565 search->is_case_sensitive = case_sens;
2567 for (i = 0; i < panel->dir.len; i++)
2569 if (DIR_IS_DOTDOT (panel->dir.list[i].fname))
2570 continue;
2571 if (S_ISDIR (panel->dir.list[i].st.st_mode) && files_only)
2572 continue;
2574 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2575 do_file_mark (panel, i, do_select);
2578 mc_search_free (search);
2579 g_free (reg_exp);
2581 /* result flags */
2582 panels_options.select_flags = 0;
2583 if (case_sens)
2584 panels_options.select_flags |= SELECT_MATCH_CASE;
2585 if (files_only)
2586 panels_options.select_flags |= SELECT_FILES_ONLY;
2587 if (shell_patterns)
2588 panels_options.select_flags |= SELECT_SHELL_PATTERNS;
2591 /* --------------------------------------------------------------------------------------------- */
2593 static void
2594 panel_select_files (WPanel * panel)
2596 panel_select_unselect_files (panel, _("Select"), ":select_cmd: Select ", TRUE);
2599 /* --------------------------------------------------------------------------------------------- */
2601 static void
2602 panel_unselect_files (WPanel * panel)
2604 panel_select_unselect_files (panel, _("Unselect"), ":unselect_cmd: Unselect ", FALSE);
2607 /* --------------------------------------------------------------------------------------------- */
2609 static void
2610 panel_select_invert_files (WPanel * panel)
2612 int i;
2614 for (i = 0; i < panel->dir.len; i++)
2616 file_entry_t *file = &panel->dir.list[i];
2618 if (!panels_options.reverse_files_only || !S_ISDIR (file->st.st_mode))
2619 do_file_mark (panel, i, !file->f.marked);
2623 /* --------------------------------------------------------------------------------------------- */
2624 /** Incremental search of a file name in the panel.
2625 * @param panel instance of WPanel structure
2626 * @param c_code key code
2629 static void
2630 do_search (WPanel * panel, int c_code)
2632 size_t l;
2633 int i, sel;
2634 gboolean wrapped = FALSE;
2635 char *act;
2636 mc_search_t *search;
2637 char *reg_exp, *esc_str;
2638 gboolean is_found = FALSE;
2640 l = strlen (panel->search_buffer);
2641 if (c_code == KEY_BACKSPACE)
2643 if (l != 0)
2645 act = panel->search_buffer + l;
2646 str_prev_noncomb_char (&act, panel->search_buffer);
2647 act[0] = '\0';
2649 panel->search_chpoint = 0;
2651 else
2653 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2655 panel->search_char[panel->search_chpoint] = c_code;
2656 panel->search_chpoint++;
2659 if (panel->search_chpoint > 0)
2661 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2663 case -2:
2664 return;
2665 case -1:
2666 panel->search_chpoint = 0;
2667 return;
2668 default:
2669 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2671 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2672 l += panel->search_chpoint;
2673 *(panel->search_buffer + l) = '\0';
2674 panel->search_chpoint = 0;
2680 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2681 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2682 search = mc_search_new (esc_str, NULL);
2683 search->search_type = MC_SEARCH_T_GLOB;
2684 search->is_entire_line = TRUE;
2686 switch (panels_options.qsearch_mode)
2688 case QSEARCH_CASE_SENSITIVE:
2689 search->is_case_sensitive = TRUE;
2690 break;
2691 case QSEARCH_CASE_INSENSITIVE:
2692 search->is_case_sensitive = FALSE;
2693 break;
2694 default:
2695 search->is_case_sensitive = panel->sort_info.case_sensitive;
2696 break;
2699 sel = panel->selected;
2701 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2703 if (i >= panel->dir.len)
2705 i = 0;
2706 if (wrapped)
2707 break;
2708 wrapped = TRUE;
2710 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2712 sel = i;
2713 is_found = TRUE;
2714 break;
2717 if (is_found)
2719 unselect_item (panel);
2720 panel->selected = sel;
2721 select_item (panel);
2722 widget_redraw (WIDGET (panel));
2724 else if (c_code != KEY_BACKSPACE)
2726 act = panel->search_buffer + l;
2727 str_prev_noncomb_char (&act, panel->search_buffer);
2728 act[0] = '\0';
2730 mc_search_free (search);
2731 g_free (reg_exp);
2732 g_free (esc_str);
2735 /* --------------------------------------------------------------------------------------------- */
2736 /** Start new search.
2737 * @param panel instance of WPanel structure
2740 static void
2741 start_search (WPanel * panel)
2743 if (panel->searching)
2745 if (panel->selected + 1 == panel->dir.len)
2746 panel->selected = 0;
2747 else
2748 move_down (panel);
2750 /* in case if there was no search string we need to recall
2751 previous string, with which we ended previous searching */
2752 if (panel->search_buffer[0] == '\0')
2753 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2754 sizeof (panel->search_buffer));
2756 do_search (panel, 0);
2758 else
2760 panel->searching = TRUE;
2761 panel->search_buffer[0] = '\0';
2762 panel->search_char[0] = '\0';
2763 panel->search_chpoint = 0;
2764 display_mini_info (panel);
2765 mc_refresh ();
2769 /* --------------------------------------------------------------------------------------------- */
2771 static void
2772 stop_search (WPanel * panel)
2774 panel->searching = FALSE;
2776 /* if user had overrdied search string, we need to store it
2777 to the previous_search_buffer */
2778 if (panel->search_buffer[0] != '\0')
2779 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2780 sizeof (panel->prev_search_buffer));
2782 display_mini_info (panel);
2785 /* --------------------------------------------------------------------------------------------- */
2786 /** Return TRUE if the Enter key has been processed, FALSE otherwise */
2788 static gboolean
2789 do_enter_on_file_entry (file_entry_t * fe)
2791 vfs_path_t *full_name_vpath;
2792 gboolean ok;
2795 * Directory or link to directory - change directory.
2796 * Try the same for the entries on which mc_lstat() has failed.
2798 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2800 vfs_path_t *fname_vpath;
2802 fname_vpath = vfs_path_from_str (fe->fname);
2803 if (!do_cd (fname_vpath, cd_exact))
2804 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2805 vfs_path_free (fname_vpath);
2806 return TRUE;
2809 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, (char *) NULL);
2811 /* Try associated command */
2812 ok = regex_command (full_name_vpath, "Open") != 0;
2813 vfs_path_free (full_name_vpath);
2814 if (ok)
2815 return TRUE;
2817 /* Check if the file is executable */
2818 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, (char *) NULL);
2819 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2820 vfs_path_free (full_name_vpath);
2821 if (!ok)
2822 return FALSE;
2824 if (confirm_execute)
2826 if (query_dialog
2827 (_("The Midnight Commander"),
2828 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2829 return TRUE;
2832 if (!vfs_current_is_local ())
2834 int ret;
2835 vfs_path_t *tmp_vpath;
2837 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, (char *) NULL);
2838 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2839 vfs_path_free (tmp_vpath);
2840 /* We took action only if the dialog was shown or the execution
2841 * was successful */
2842 return confirm_execute || (ret == 0);
2846 char *tmp, *cmd;
2848 tmp = name_quote (fe->fname, FALSE);
2849 cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2850 g_free (tmp);
2851 shell_execute (cmd, 0);
2852 g_free (cmd);
2855 #ifdef HAVE_CHARSET
2856 mc_global.source_codepage = default_source_codepage;
2857 #endif
2859 return TRUE;
2862 /* --------------------------------------------------------------------------------------------- */
2864 static inline gboolean
2865 do_enter (WPanel * panel)
2867 return do_enter_on_file_entry (selection (panel));
2871 /* --------------------------------------------------------------------------------------------- */
2873 static void
2874 panel_cycle_listing_format (WPanel * panel)
2876 panel->list_format = (panel->list_format + 1) % LIST_FORMATS;
2878 if (set_panel_formats (panel) == 0)
2879 do_refresh ();
2882 /* --------------------------------------------------------------------------------------------- */
2884 static void
2885 chdir_other_panel (WPanel * panel)
2887 const file_entry_t *entry = &panel->dir.list[panel->selected];
2889 vfs_path_t *new_dir_vpath;
2890 char *sel_entry = NULL;
2892 if (get_other_type () != view_listing)
2893 set_display_type (get_other_index (), view_listing);
2895 if (S_ISDIR (entry->st.st_mode) || link_isdir (entry))
2896 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, (char *) NULL);
2897 else
2899 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2900 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2903 change_panel ();
2904 do_cd (new_dir_vpath, cd_exact);
2905 vfs_path_free (new_dir_vpath);
2907 if (sel_entry)
2908 try_to_select (current_panel, sel_entry);
2909 change_panel ();
2911 move_down (panel);
2914 /* --------------------------------------------------------------------------------------------- */
2916 * Make the current directory of the current panel also the current
2917 * directory of the other panel. Put the other panel to the listing
2918 * mode if needed. If the current panel is panelized, the other panel
2919 * doesn't become panelized.
2922 static void
2923 panel_sync_other (const WPanel * panel)
2925 if (get_other_type () != view_listing)
2926 set_display_type (get_other_index (), view_listing);
2928 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2930 /* try to select current filename on the other panel */
2931 if (!panel->is_panelized)
2932 try_to_select (other_panel, selection (panel)->fname);
2935 /* --------------------------------------------------------------------------------------------- */
2937 static void
2938 chdir_to_readlink (WPanel * panel)
2940 vfs_path_t *new_dir_vpath;
2941 char buffer[MC_MAXPATHLEN];
2942 int i;
2943 struct stat st;
2944 vfs_path_t *panel_fname_vpath;
2945 gboolean ok;
2947 if (get_other_type () != view_listing)
2948 return;
2950 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2951 return;
2953 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2954 if (i < 0)
2955 return;
2957 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2958 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2959 vfs_path_free (panel_fname_vpath);
2960 if (!ok)
2961 return;
2963 buffer[i] = 0;
2964 if (!S_ISDIR (st.st_mode))
2966 char *p;
2968 p = strrchr (buffer, PATH_SEP);
2969 if (p && !p[1])
2971 *p = 0;
2972 p = strrchr (buffer, PATH_SEP);
2974 if (!p)
2975 return;
2976 p[1] = 0;
2978 if (IS_PATH_SEP (*buffer))
2979 new_dir_vpath = vfs_path_from_str (buffer);
2980 else
2981 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, (char *) NULL);
2983 change_panel ();
2984 do_cd (new_dir_vpath, cd_exact);
2985 vfs_path_free (new_dir_vpath);
2986 change_panel ();
2988 move_down (panel);
2991 /* --------------------------------------------------------------------------------------------- */
2993 static gsize
2994 panel_get_format_field_count (WPanel * panel)
2996 format_e *format;
2997 gsize lc_index;
2999 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++)
3002 return lc_index;
3005 /* --------------------------------------------------------------------------------------------- */
3007 function return 0 if not found and REAL_INDEX+1 if found
3010 static gsize
3011 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
3013 format_e *format;
3014 gsize lc_index;
3016 for (lc_index = 1, format = panel->format;
3017 format != NULL && strcmp (format->title, name) != 0; format = format->next, lc_index++)
3020 if (format == NULL)
3021 lc_index = 0;
3023 return lc_index;
3026 /* --------------------------------------------------------------------------------------------- */
3028 static format_e *
3029 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
3031 format_e *format;
3033 for (format = panel->format; format != NULL && lc_index != 0; format = format->next, lc_index--)
3036 return format;
3039 /* --------------------------------------------------------------------------------------------- */
3041 static const panel_field_t *
3042 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
3044 const panel_field_t *pfield;
3045 format_e *format;
3047 format = panel_get_format_field_by_index (panel, lc_index);
3048 if (format == NULL)
3049 return NULL;
3051 pfield = panel_get_field_by_title (format->title);
3052 if (pfield == NULL)
3053 return NULL;
3054 if (pfield->sort_routine == NULL)
3055 return NULL;
3056 return pfield;
3059 /* --------------------------------------------------------------------------------------------- */
3061 static void
3062 panel_toggle_sort_order_prev (WPanel * panel)
3064 gsize lc_index, i;
3065 const char *title;
3066 const panel_field_t *pfield = NULL;
3068 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3069 lc_index = panel_get_format_field_index_by_name (panel, title);
3071 if (lc_index > 1)
3073 /* search for prev sortable column in panel format */
3074 for (i = lc_index - 1;
3075 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3079 if (pfield == NULL)
3081 /* Sortable field not found. Try to search in each array */
3082 for (i = panel_get_format_field_count (panel);
3083 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
3087 if (pfield != NULL)
3089 panel->sort_field = pfield;
3090 panel_set_sort_order (panel, pfield);
3094 /* --------------------------------------------------------------------------------------------- */
3096 static void
3097 panel_toggle_sort_order_next (WPanel * panel)
3099 gsize lc_index, i;
3100 const panel_field_t *pfield = NULL;
3101 gsize format_field_count;
3102 const char *title;
3104 format_field_count = panel_get_format_field_count (panel);
3105 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
3106 lc_index = panel_get_format_field_index_by_name (panel, title);
3108 if (lc_index != 0 && lc_index != format_field_count)
3110 /* search for prev sortable column in panel format */
3111 for (i = lc_index;
3112 i != format_field_count
3113 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
3117 if (pfield == NULL)
3119 /* Sortable field not found. Try to search in each array */
3120 for (i = 0;
3121 i != format_field_count
3122 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
3126 if (pfield != NULL)
3128 panel->sort_field = pfield;
3129 panel_set_sort_order (panel, pfield);
3133 /* --------------------------------------------------------------------------------------------- */
3135 static void
3136 panel_select_sort_order (WPanel * panel)
3138 const panel_field_t *sort_order;
3140 sort_order = sort_box (&panel->sort_info, panel->sort_field);
3141 if (sort_order != NULL)
3143 panel->sort_field = sort_order;
3144 panel_set_sort_order (panel, sort_order);
3148 /* --------------------------------------------------------------------------------------------- */
3151 * panel_content_scroll_left:
3152 * @param panel the pointer to the panel on which we operate
3154 * scroll long filename to the left (decrement scroll pointer)
3158 static void
3159 panel_content_scroll_left (WPanel * panel)
3161 if (panel->content_shift > -1)
3163 if (panel->content_shift > panel->max_shift)
3164 panel->content_shift = panel->max_shift;
3166 panel->content_shift--;
3167 show_dir (panel);
3168 paint_dir (panel);
3172 /* --------------------------------------------------------------------------------------------- */
3175 * panel_content_scroll_right:
3176 * @param panel the pointer to the panel on which we operate
3178 * scroll long filename to the right (increment scroll pointer)
3182 static void
3183 panel_content_scroll_right (WPanel * panel)
3185 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
3187 panel->content_shift++;
3188 show_dir (panel);
3189 paint_dir (panel);
3193 /* --------------------------------------------------------------------------------------------- */
3195 static void
3196 panel_set_sort_type_by_id (WPanel * panel, const char *name)
3198 if (strcmp (panel->sort_field->id, name) != 0)
3200 const panel_field_t *sort_order;
3202 sort_order = panel_get_field_by_id (name);
3203 if (sort_order == NULL)
3204 return;
3206 panel->sort_field = sort_order;
3208 else
3209 panel->sort_info.reverse = !panel->sort_info.reverse;
3211 panel_set_sort_order (panel, panel->sort_field);
3214 /* --------------------------------------------------------------------------------------------- */
3216 * If we moved to the parent directory move the selection pointer to
3217 * the old directory name; If we leave VFS dir, remove FS specificator.
3219 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
3222 static const char *
3223 get_parent_dir_name (const vfs_path_t * cwd_vpath, const vfs_path_t * lwd_vpath)
3225 size_t llen, clen;
3226 const char *p, *cwd, *lwd;
3228 llen = vfs_path_len (lwd_vpath);
3229 clen = vfs_path_len (cwd_vpath);
3231 if (llen <= clen)
3232 return NULL;
3234 cwd = vfs_path_as_str (cwd_vpath);
3235 lwd = vfs_path_as_str (lwd_vpath);
3237 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
3239 if (p == NULL)
3241 p = strrchr (lwd, PATH_SEP);
3243 if ((p != NULL)
3244 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
3245 && (clen == (size_t) (p - lwd) || (p == lwd && IS_PATH_SEP (cwd[0]) && cwd[1] == '\0')))
3246 return (p + 1);
3248 return NULL;
3251 /* skip VFS prefix */
3252 while (--p > lwd && !IS_PATH_SEP (*p))
3254 /* get last component */
3255 while (--p > lwd && !IS_PATH_SEP (*p))
3258 /* return last component */
3259 return (p != lwd || IS_PATH_SEP (*p)) ? p + 1 : p;
3262 /* --------------------------------------------------------------------------------------------- */
3263 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3265 static void
3266 subshell_chdir (const vfs_path_t * vpath)
3268 #ifdef ENABLE_SUBSHELL
3269 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3270 do_subshell_chdir (vpath, FALSE);
3271 #else /* ENABLE_SUBSHELL */
3272 (void) vpath;
3273 #endif /* ENABLE_SUBSHELL */
3276 /* --------------------------------------------------------------------------------------------- */
3278 * Changes the current directory of the panel.
3279 * Don't record change in the directory history.
3282 static gboolean
3283 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3285 vfs_path_t *olddir_vpath;
3287 /* Convert *new_path to a suitable pathname, handle ~user */
3288 if (cd_type == cd_parse_command)
3290 const vfs_path_element_t *element;
3292 element = vfs_path_get_by_index (new_dir_vpath, 0);
3293 if (strcmp (element->path, "-") == 0)
3294 new_dir_vpath = panel->lwd_vpath;
3297 if (mc_chdir (new_dir_vpath) == -1)
3298 return FALSE;
3300 /* Success: save previous directory, shutdown status of previous dir */
3301 olddir_vpath = vfs_path_clone (panel->cwd_vpath);
3302 panel_set_lwd (panel, panel->cwd_vpath);
3303 input_free_completions (cmdline);
3305 vfs_path_free (panel->cwd_vpath);
3306 vfs_setup_cwd ();
3307 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3309 vfs_release_path (olddir_vpath);
3311 subshell_chdir (panel->cwd_vpath);
3313 /* Reload current panel */
3314 panel_clean_dir (panel);
3316 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
3317 &panel->sort_info, panel->filter);
3318 try_to_select (panel, get_parent_dir_name (panel->cwd_vpath, olddir_vpath));
3320 load_hint (FALSE);
3321 panel->dirty = 1;
3322 update_xterm_title_path ();
3324 vfs_path_free (olddir_vpath);
3326 return TRUE;
3329 /* --------------------------------------------------------------------------------------------- */
3331 static void
3332 directory_history_next (WPanel * panel)
3334 gboolean ok;
3338 GList *next;
3340 ok = TRUE;
3341 next = g_list_next (panel->dir_history_current);
3342 if (next != NULL)
3344 vfs_path_t *data_vpath;
3346 data_vpath = vfs_path_from_str ((char *) next->data);
3347 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3348 vfs_path_free (data_vpath);
3349 panel->dir_history_current = next;
3351 /* skip directories that present in history but absent in file system */
3353 while (!ok);
3356 /* --------------------------------------------------------------------------------------------- */
3358 static void
3359 directory_history_prev (WPanel * panel)
3361 gboolean ok;
3365 GList *prev;
3367 ok = TRUE;
3368 prev = g_list_previous (panel->dir_history_current);
3369 if (prev != NULL)
3371 vfs_path_t *data_vpath;
3373 data_vpath = vfs_path_from_str ((char *) prev->data);
3374 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3375 vfs_path_free (data_vpath);
3376 panel->dir_history_current = prev;
3378 /* skip directories that present in history but absent in file system */
3380 while (!ok);
3383 /* --------------------------------------------------------------------------------------------- */
3385 static void
3386 directory_history_list (WPanel * panel)
3388 char *s;
3389 gboolean ok = FALSE;
3390 size_t pos;
3392 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3394 s = history_show (&panel->dir_history, WIDGET (panel), pos);
3395 if (s != NULL)
3397 vfs_path_t *s_vpath;
3399 s_vpath = vfs_path_from_str (s);
3400 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3401 if (ok)
3402 directory_history_add (panel, panel->cwd_vpath);
3403 else
3404 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3405 vfs_path_free (s_vpath);
3406 g_free (s);
3409 if (!ok)
3411 /* Since history is fully modified in history_show(), panel->dir_history actually
3412 * points to the invalid place. Try restore current postition here. */
3414 size_t i;
3416 panel->dir_history_current = panel->dir_history;
3418 for (i = 0; i <= pos; i++)
3420 GList *prev;
3422 prev = g_list_previous (panel->dir_history_current);
3423 if (prev == NULL)
3424 break;
3426 panel->dir_history_current = prev;
3431 /* --------------------------------------------------------------------------------------------- */
3433 static cb_ret_t
3434 panel_execute_cmd (WPanel * panel, long command)
3436 int res = MSG_HANDLED;
3438 if (command != CK_Search)
3439 stop_search (panel);
3441 switch (command)
3443 case CK_Up:
3444 case CK_Down:
3445 case CK_Left:
3446 case CK_Right:
3447 case CK_Bottom:
3448 case CK_Top:
3449 case CK_PageDown:
3450 case CK_PageUp:
3451 /* reset state of marks flag */
3452 state_mark = -1;
3453 break;
3454 default:
3455 break;
3458 switch (command)
3460 case CK_CycleListingFormat:
3461 panel_cycle_listing_format (panel);
3462 break;
3463 case CK_PanelOtherCd:
3464 chdir_other_panel (panel);
3465 break;
3466 case CK_PanelOtherCdLink:
3467 chdir_to_readlink (panel);
3468 break;
3469 case CK_CopySingle:
3470 copy_cmd_local ();
3471 break;
3472 case CK_DeleteSingle:
3473 delete_cmd_local ();
3474 break;
3475 case CK_Enter:
3476 do_enter (panel);
3477 break;
3478 case CK_ViewRaw:
3479 view_raw_cmd ();
3480 break;
3481 case CK_EditNew:
3482 edit_cmd_new ();
3483 break;
3484 case CK_MoveSingle:
3485 rename_cmd_local ();
3486 break;
3487 case CK_SelectInvert:
3488 panel_select_invert_files (panel);
3489 break;
3490 case CK_Select:
3491 panel_select_files (panel);
3492 break;
3493 case CK_SelectExt:
3494 panel_select_ext_cmd ();
3495 break;
3496 case CK_Unselect:
3497 panel_unselect_files (panel);
3498 break;
3499 case CK_PageDown:
3500 next_page (panel);
3501 break;
3502 case CK_PageUp:
3503 prev_page (panel);
3504 break;
3505 case CK_CdChild:
3506 goto_child_dir (panel);
3507 break;
3508 case CK_CdParent:
3509 goto_parent_dir (panel);
3510 break;
3511 case CK_History:
3512 directory_history_list (panel);
3513 break;
3514 case CK_HistoryNext:
3515 directory_history_next (panel);
3516 break;
3517 case CK_HistoryPrev:
3518 directory_history_prev (panel);
3519 break;
3520 case CK_BottomOnScreen:
3521 goto_bottom_file (panel);
3522 break;
3523 case CK_MiddleOnScreen:
3524 goto_middle_file (panel);
3525 break;
3526 case CK_TopOnScreen:
3527 goto_top_file (panel);
3528 break;
3529 case CK_Mark:
3530 mark_file (panel);
3531 break;
3532 case CK_MarkUp:
3533 mark_file_up (panel);
3534 break;
3535 case CK_MarkDown:
3536 mark_file_down (panel);
3537 break;
3538 case CK_MarkLeft:
3539 mark_file_left (panel);
3540 break;
3541 case CK_MarkRight:
3542 mark_file_right (panel);
3543 break;
3544 case CK_CdParentSmart:
3545 res = force_maybe_cd ();
3546 break;
3547 case CK_Up:
3548 move_up (panel);
3549 break;
3550 case CK_Down:
3551 move_down (panel);
3552 break;
3553 case CK_Left:
3554 res = move_left (panel);
3555 break;
3556 case CK_Right:
3557 res = move_right (panel);
3558 break;
3559 case CK_Bottom:
3560 move_end (panel);
3561 break;
3562 case CK_Top:
3563 move_home (panel);
3564 break;
3565 #ifdef HAVE_CHARSET
3566 case CK_SelectCodepage:
3567 panel_change_encoding (panel);
3568 break;
3569 #endif
3570 case CK_ScrollLeft:
3571 panel_content_scroll_left (panel);
3572 break;
3573 case CK_ScrollRight:
3574 panel_content_scroll_right (panel);
3575 break;
3576 case CK_Search:
3577 start_search (panel);
3578 break;
3579 case CK_SearchStop:
3580 break;
3581 case CK_PanelOtherSync:
3582 panel_sync_other (panel);
3583 break;
3584 case CK_Sort:
3585 panel_select_sort_order (panel);
3586 break;
3587 case CK_SortPrev:
3588 panel_toggle_sort_order_prev (panel);
3589 break;
3590 case CK_SortNext:
3591 panel_toggle_sort_order_next (panel);
3592 break;
3593 case CK_SortReverse:
3594 panel->sort_info.reverse = !panel->sort_info.reverse;
3595 panel_set_sort_order (panel, panel->sort_field);
3596 break;
3597 case CK_SortByName:
3598 panel_set_sort_type_by_id (panel, "name");
3599 break;
3600 case CK_SortByExt:
3601 panel_set_sort_type_by_id (panel, "extension");
3602 break;
3603 case CK_SortBySize:
3604 panel_set_sort_type_by_id (panel, "size");
3605 break;
3606 case CK_SortByMTime:
3607 panel_set_sort_type_by_id (panel, "mtime");
3608 break;
3609 default:
3610 res = MSG_NOT_HANDLED;
3611 break;
3614 return res;
3617 /* --------------------------------------------------------------------------------------------- */
3619 static cb_ret_t
3620 panel_key (WPanel * panel, int key)
3622 size_t i;
3624 if (is_abort_char (key))
3626 stop_search (panel);
3627 return MSG_HANDLED;
3630 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3632 do_search (panel, key);
3633 return MSG_HANDLED;
3636 for (i = 0; panel_map[i].key != 0; i++)
3637 if (key == panel_map[i].key)
3638 return panel_execute_cmd (panel, panel_map[i].command);
3640 if (panels_options.torben_fj_mode && key == ALT ('h'))
3642 goto_middle_file (panel);
3643 return MSG_HANDLED;
3646 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3648 start_search (panel);
3649 do_search (panel, key);
3650 return MSG_HANDLED;
3653 return MSG_NOT_HANDLED;
3656 /* --------------------------------------------------------------------------------------------- */
3658 static cb_ret_t
3659 panel_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3661 WPanel *panel = PANEL (w);
3662 WButtonBar *bb;
3664 switch (msg)
3666 case MSG_INIT:
3667 /* subscribe to "history_load" event */
3668 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3669 /* subscribe to "history_save" event */
3670 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3671 return MSG_HANDLED;
3673 case MSG_DRAW:
3674 /* Repaint everything, including frame and separator */
3675 widget_erase (w);
3676 show_dir (panel);
3677 panel_print_header (panel);
3678 adjust_top_file (panel);
3679 paint_dir (panel);
3680 mini_info_separator (panel);
3681 display_mini_info (panel);
3682 panel->dirty = 0;
3683 return MSG_HANDLED;
3685 case MSG_FOCUS:
3686 state_mark = -1;
3687 current_panel = panel;
3688 panel->active = 1;
3690 if (mc_chdir (panel->cwd_vpath) != 0)
3692 char *cwd;
3694 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3695 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3696 cwd, unix_error_string (errno));
3697 g_free (cwd);
3699 else
3700 subshell_chdir (panel->cwd_vpath);
3702 update_xterm_title_path ();
3703 select_item (panel);
3705 bb = find_buttonbar (w->owner);
3706 midnight_set_buttonbar (bb);
3707 widget_redraw (WIDGET (bb));
3708 return MSG_HANDLED;
3710 case MSG_UNFOCUS:
3711 /* Janne: look at this for the multiple panel options */
3712 stop_search (panel);
3713 panel->active = 0;
3714 unselect_item (panel);
3715 return MSG_HANDLED;
3717 case MSG_KEY:
3718 return panel_key (panel, parm);
3720 case MSG_ACTION:
3721 return panel_execute_cmd (panel, parm);
3723 case MSG_DESTROY:
3724 /* unsubscribe from "history_load" event */
3725 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3726 /* unsubscribe from "history_save" event */
3727 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3728 panel_destroy (panel);
3729 free_my_statfs ();
3730 return MSG_HANDLED;
3732 default:
3733 return widget_default_callback (w, sender, msg, parm, data);
3737 /* --------------------------------------------------------------------------------------------- */
3738 /* */
3739 /* Panel mouse events support routines */
3740 /* */
3742 static void
3743 mouse_toggle_mark (WPanel * panel)
3745 do_mark_file (panel, MARK_DONT_MOVE);
3746 mouse_marking = selection (panel)->f.marked;
3747 mouse_mark_panel = current_panel;
3750 /* --------------------------------------------------------------------------------------------- */
3752 static void
3753 mouse_set_mark (WPanel * panel)
3755 if (mouse_mark_panel == panel)
3757 if (mouse_marking && !(selection (panel)->f.marked))
3758 do_mark_file (panel, MARK_DONT_MOVE);
3759 else if (!mouse_marking && (selection (panel)->f.marked))
3760 do_mark_file (panel, MARK_DONT_MOVE);
3764 /* --------------------------------------------------------------------------------------------- */
3766 static gboolean
3767 mark_if_marking (WPanel * panel, const mouse_event_t * event)
3769 if ((event->buttons & GPM_B_RIGHT) != 0)
3771 if (event->msg == MSG_MOUSE_DOWN)
3772 mouse_toggle_mark (panel);
3773 else
3774 mouse_set_mark (panel);
3775 return TRUE;
3778 return FALSE;
3781 /* --------------------------------------------------------------------------------------------- */
3782 /** Determine which column was clicked, and sort the panel on
3783 * that column, or reverse sort on that column if already
3784 * sorted on that column.
3787 static void
3788 mouse_sort_col (WPanel * panel, int x)
3790 int i;
3791 const char *lc_sort_name = NULL;
3792 panel_field_t *col_sort_format = NULL;
3793 format_e *format;
3795 for (i = 0, format = panel->format; format != NULL; format = format->next)
3797 i += format->field_len;
3798 if (x < i + 1)
3800 /* found column */
3801 lc_sort_name = format->title;
3802 break;
3806 if (lc_sort_name == NULL)
3807 return;
3809 for (i = 0; panel_fields[i].id != NULL; i++)
3811 const char *title;
3813 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3814 if (strcmp (title, lc_sort_name) == 0 && panel_fields[i].sort_routine != NULL)
3816 col_sort_format = &panel_fields[i];
3817 break;
3821 if (col_sort_format == NULL)
3822 return;
3824 if (panel->sort_field == col_sort_format)
3826 /* reverse the sort if clicked column is already the sorted column */
3827 panel->sort_info.reverse = !panel->sort_info.reverse;
3829 else
3831 /* new sort is forced to be ascending */
3832 panel->sort_info.reverse = FALSE;
3834 panel_set_sort_order (panel, col_sort_format);
3837 /* --------------------------------------------------------------------------------------------- */
3839 static void
3840 panel_mouse_callback (Widget * w, mouse_msg_t msg, mouse_event_t * event)
3842 WPanel *panel = PANEL (w);
3843 gboolean is_active;
3845 is_active = widget_is_active (w);
3847 switch (msg)
3849 case MSG_MOUSE_DOWN:
3850 if (event->y == 0)
3852 /* top frame */
3853 if (event->x == 1)
3854 /* "<" button */
3855 directory_history_prev (panel);
3856 else if (event->x == w->cols - 2)
3857 /* ">" button */
3858 directory_history_next (panel);
3859 else if (event->x >= w->cols - 5 && event->x <= w->cols - 3)
3860 /* "^" button */
3861 directory_history_list (panel);
3862 else if (event->x == w->cols - 6)
3863 /* "." button show/hide hidden files */
3864 send_message (midnight_dlg, NULL, MSG_ACTION, CK_ShowHidden, NULL);
3865 else
3867 /* no other events on 1st line, return MOU_UNHANDLED */
3868 event->result.abort = TRUE;
3869 /* avoid extra panel redraw */
3870 panel->dirty = 0;
3872 break;
3875 if (event->y == 1)
3877 /* sort on clicked column */
3878 mouse_sort_col (panel, event->x + 1);
3879 break;
3882 if (!is_active)
3883 change_panel ();
3884 MC_FALLTHROUGH;
3886 case MSG_MOUSE_DRAG:
3888 int y, last, my_index;
3890 last = panel->dir.len - 1;
3891 y = event->y - 2;
3893 if (panel->top_file + y > last)
3894 my_index = last;
3895 else
3897 my_index = panel->top_file + y;
3899 if (panel->list_cols > 1)
3901 int width, lines;
3903 width = (w->cols - 2) / panel->list_cols;
3904 lines = panel_lines (panel);
3905 my_index += lines * (event->x / width);
3908 if (my_index > last)
3909 my_index = last;
3912 if (my_index != panel->selected)
3914 unselect_item (panel);
3915 panel->selected = my_index;
3916 select_item (panel);
3919 /* This one is new */
3920 mark_if_marking (panel, event);
3922 break;
3924 case MSG_MOUSE_UP:
3925 break;
3927 case MSG_MOUSE_CLICK:
3928 if ((event->count & GPM_DOUBLE) != 0)
3930 int y, lines;
3932 y = event->y - 2;
3933 lines = panel_lines (panel);
3935 if (y >= 0 && y < lines)
3936 do_enter (panel);
3938 break;
3940 case MSG_MOUSE_MOVE:
3941 break;
3943 case MSG_MOUSE_SCROLL_UP:
3944 if (is_active)
3946 if (panels_options.mouse_move_pages && panel->top_file > 0)
3947 prev_page (panel);
3948 else /* We are in first page */
3949 move_up (panel);
3951 break;
3953 case MSG_MOUSE_SCROLL_DOWN:
3954 if (is_active)
3956 if (panels_options.mouse_move_pages
3957 && panel->top_file + panel_items (panel) < panel->dir.len)
3958 next_page (panel);
3959 else /* We are in last page */
3960 move_down (panel);
3962 break;
3964 default:
3965 break;
3968 if (panel->dirty)
3969 widget_redraw (w);
3972 /* --------------------------------------------------------------------------------------------- */
3974 static void
3975 reload_panelized (WPanel * panel)
3977 int i, j;
3978 dir_list *list = &panel->dir;
3980 /* refresh current VFS directory required for vfs_path_from_str() */
3981 (void) mc_chdir (panel->cwd_vpath);
3983 for (i = 0, j = 0; i < list->len; i++)
3985 vfs_path_t *vpath;
3987 vpath = vfs_path_from_str (list->list[i].fname);
3988 if (mc_lstat (vpath, &list->list[i].st) != 0)
3989 g_free (list->list[i].fname);
3990 else
3992 if (j != i)
3993 list->list[j] = list->list[i];
3994 j++;
3996 vfs_path_free (vpath);
3998 if (j == 0)
3999 dir_list_init (list);
4000 else
4001 list->len = j;
4003 recalculate_panel_summary (panel);
4005 if (panel != current_panel)
4006 (void) mc_chdir (current_panel->cwd_vpath);
4009 /* --------------------------------------------------------------------------------------------- */
4011 static void
4012 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
4014 gboolean free_pointer;
4015 char *my_current_file = NULL;
4017 if ((flags & UP_RELOAD) != 0)
4019 panel->is_panelized = FALSE;
4020 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
4021 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4024 /* If current_file == -1 (an invalid pointer) then preserve selection */
4025 free_pointer = current_file == UP_KEEPSEL;
4027 if (free_pointer)
4029 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
4030 current_file = my_current_file;
4033 if (panel->is_panelized)
4034 reload_panelized (panel);
4035 else
4036 panel_reload (panel);
4038 try_to_select (panel, current_file);
4039 panel->dirty = 1;
4041 if (free_pointer)
4042 g_free (my_current_file);
4045 /* --------------------------------------------------------------------------------------------- */
4047 static void
4048 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
4050 if (get_display_type (which) == view_listing)
4052 WPanel *panel;
4054 panel = PANEL (get_panel_widget (which));
4055 if (panel->is_panelized)
4056 flags &= ~UP_RELOAD;
4057 update_one_panel_widget (panel, flags, current_file);
4061 /* --------------------------------------------------------------------------------------------- */
4063 static void
4064 do_select (WPanel * panel, int i)
4066 if (i != panel->selected)
4068 panel->dirty = 1;
4069 panel->selected = i;
4070 panel->top_file = panel->selected - (WIDGET (panel)->lines - 2) / 2;
4071 if (panel->top_file < 0)
4072 panel->top_file = 0;
4076 /* --------------------------------------------------------------------------------------------- */
4078 static void
4079 do_try_to_select (WPanel * panel, const char *name)
4081 int i;
4082 char *subdir;
4084 if (!name)
4086 do_select (panel, 0);
4087 return;
4090 /* We only want the last component of the directory,
4091 * and from this only the name without suffix.
4092 * Cut prefix if the panel is not panelized */
4094 if (panel->is_panelized)
4095 subdir = vfs_strip_suffix_from_filename (name);
4096 else
4097 subdir = vfs_strip_suffix_from_filename (x_basename (name));
4099 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
4100 for (i = 0; i < panel->dir.len; i++)
4102 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
4104 do_select (panel, i);
4105 g_free (subdir);
4106 return;
4110 /* Try to select a file near the file that is missing */
4111 if (panel->selected >= panel->dir.len)
4112 do_select (panel, panel->dir.len - 1);
4113 g_free (subdir);
4116 /* --------------------------------------------------------------------------------------------- */
4118 /* event callback */
4119 static gboolean
4120 event_update_panels (const gchar * event_group_name, const gchar * event_name,
4121 gpointer init_data, gpointer data)
4123 (void) event_group_name;
4124 (void) event_name;
4125 (void) init_data;
4126 (void) data;
4128 update_panels (UP_RELOAD, UP_KEEPSEL);
4130 return TRUE;
4133 /* --------------------------------------------------------------------------------------------- */
4135 /* event callback */
4136 static gboolean
4137 panel_save_current_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
4138 gpointer init_data, gpointer data)
4140 (void) event_group_name;
4141 (void) event_name;
4142 (void) init_data;
4143 (void) data;
4145 if (current_panel->marked == 0)
4146 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
4147 (gpointer) selection (current_panel)->fname);
4148 else
4150 int i;
4151 gboolean first = TRUE;
4152 char *flist = NULL;
4154 for (i = 0; i < current_panel->dir.len; i++)
4155 if (current_panel->dir.list[i].f.marked != 0)
4156 { /* Skip the unmarked ones */
4157 if (first)
4159 flist = g_strdup (current_panel->dir.list[i].fname);
4160 first = FALSE;
4162 else
4164 /* Add empty lines after the file */
4165 char *tmp;
4167 tmp =
4168 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
4169 g_free (flist);
4170 flist = tmp;
4174 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
4175 g_free (flist);
4177 return TRUE;
4180 /* --------------------------------------------------------------------------------------------- */
4182 static vfs_path_t *
4183 panel_recursive_cd_to_parent (const vfs_path_t * vpath)
4185 vfs_path_t *cwd_vpath;
4187 cwd_vpath = vfs_path_clone (vpath);
4189 while (mc_chdir (cwd_vpath) < 0)
4191 const char *panel_cwd_path;
4192 vfs_path_t *tmp_vpath;
4194 /* check if path contains only '/' */
4195 panel_cwd_path = vfs_path_as_str (cwd_vpath);
4196 if (panel_cwd_path != NULL && IS_PATH_SEP (panel_cwd_path[0]) && panel_cwd_path[1] == '\0')
4197 return NULL;
4199 tmp_vpath = vfs_path_vtokens_get (cwd_vpath, 0, -1);
4200 vfs_path_free (cwd_vpath);
4201 cwd_vpath =
4202 vfs_path_build_filename (PATH_SEP_STR, vfs_path_as_str (tmp_vpath), (char *) NULL);
4203 vfs_path_free (tmp_vpath);
4206 return cwd_vpath;
4209 /* --------------------------------------------------------------------------------------------- */
4210 /*** public functions ****************************************************************************/
4211 /* --------------------------------------------------------------------------------------------- */
4213 void
4214 try_to_select (WPanel * panel, const char *name)
4216 do_try_to_select (panel, name);
4217 select_item (panel);
4220 /* --------------------------------------------------------------------------------------------- */
4222 void
4223 panel_clean_dir (WPanel * panel)
4225 panel->top_file = 0;
4226 panel->selected = 0;
4227 panel->marked = 0;
4228 panel->dirs_marked = 0;
4229 panel->total = 0;
4230 panel->searching = FALSE;
4231 panel->is_panelized = FALSE;
4232 panel->dirty = 1;
4233 panel->content_shift = -1;
4234 panel->max_shift = -1;
4236 dir_list_free_list (&panel->dir);
4239 /* --------------------------------------------------------------------------------------------- */
4241 * Set Up panel's current dir object
4243 * @param panel panel object
4244 * @param path_str string contain path
4247 void
4248 panel_set_cwd (WPanel * panel, const vfs_path_t * vpath)
4250 if (vpath != panel->cwd_vpath) /* check if new vpath is not the panel->cwd_vpath object */
4252 vfs_path_free (panel->cwd_vpath);
4253 panel->cwd_vpath = vfs_path_clone (vpath);
4257 /* --------------------------------------------------------------------------------------------- */
4259 * Set Up panel's last working dir object
4261 * @param panel panel object
4262 * @param path_str string contain path
4265 void
4266 panel_set_lwd (WPanel * panel, const vfs_path_t * vpath)
4268 if (vpath != panel->lwd_vpath) /* check if new vpath is not the panel->lwd_vpath object */
4270 vfs_path_free (panel->lwd_vpath);
4271 panel->lwd_vpath = vfs_path_clone (vpath);
4275 /* --------------------------------------------------------------------------------------------- */
4277 * Creatie an empty panel with specified size.
4279 * @param panel_name name of panel for setup retieving
4281 * @return new instance of WPanel
4284 WPanel *
4285 panel_sized_empty_new (const char *panel_name, int y, int x, int lines, int cols)
4287 WPanel *panel;
4288 Widget *w;
4289 char *section;
4290 int i, err;
4292 panel = g_new0 (WPanel, 1);
4293 w = WIDGET (panel);
4294 widget_init (w, y, x, lines, cols, panel_callback, panel_mouse_callback);
4295 w->options |= WOP_SELECTABLE | WOP_TOP_SELECT;
4297 panel->dir.size = DIR_LIST_MIN_SIZE;
4298 panel->dir.list = g_new (file_entry_t, panel->dir.size);
4299 panel->dir.len = 0;
4301 panel->list_cols = 1;
4302 panel->brief_cols = 2;
4303 panel->dirty = 1;
4304 panel->format_modified = 1;
4305 panel->content_shift = -1;
4306 panel->max_shift = -1;
4308 panel->list_format = list_full;
4309 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4311 for (i = 0; i < LIST_FORMATS; i++)
4312 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4314 #ifdef HAVE_CHARSET
4315 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4316 #endif
4318 panel->frame_size = frame_half;
4320 panel->panel_name = g_strdup (panel_name);
4321 panel->hist_name = g_strconcat ("Dir Hist ", panel->panel_name, (char *) NULL);
4322 /* directories history will be get later */
4324 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4325 if (!mc_config_has_group (mc_global.main_config, section))
4327 g_free (section);
4328 section = g_strdup (panel->panel_name);
4330 panel_load_setup (panel, section);
4331 g_free (section);
4333 /* Load format strings */
4334 err = set_panel_formats (panel);
4335 if (err != 0)
4336 set_panel_formats (panel);
4338 return panel;
4341 /* --------------------------------------------------------------------------------------------- */
4343 * Panel creation for specified size and directory.
4345 * @param panel_name name of panel for setup retieving
4346 * @param y y coordinate of top-left corner
4347 * @param x x coordinate of top-left corner
4348 * @param lines vertical size
4349 * @param cols horizontal size
4350 * @param vpath working panel directory. If NULL then current directory is used
4352 * @return new instance of WPanel
4355 WPanel *
4356 panel_sized_with_dir_new (const char *panel_name, int y, int x, int lines, int cols,
4357 const vfs_path_t * vpath)
4359 WPanel *panel;
4360 char *curdir = NULL;
4361 #ifdef HAVE_CHARSET
4362 const vfs_path_element_t *path_element;
4363 #endif
4365 panel = panel_sized_empty_new (panel_name, y, x, lines, cols);
4367 if (vpath != NULL)
4369 curdir = _vfs_get_cwd ();
4370 panel_set_cwd (panel, vpath);
4372 else
4374 vfs_setup_cwd ();
4375 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4378 panel_set_lwd (panel, vfs_get_raw_current_dir ());
4380 #ifdef HAVE_CHARSET
4381 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4382 if (path_element->encoding != NULL)
4383 panel->codepage = get_codepage_index (path_element->encoding);
4384 #endif
4386 if (mc_chdir (panel->cwd_vpath) != 0)
4388 #ifdef HAVE_CHARSET
4389 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4390 #endif
4391 vfs_setup_cwd ();
4392 vfs_path_free (panel->cwd_vpath);
4393 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4396 /* Load the default format */
4397 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4398 &panel->sort_info, panel->filter);
4400 /* Restore old right path */
4401 if (curdir != NULL)
4403 vfs_path_t *tmp_vpath;
4404 int err;
4406 tmp_vpath = vfs_path_from_str (curdir);
4407 err = mc_chdir (tmp_vpath);
4408 vfs_path_free (tmp_vpath);
4409 (void) err;
4411 g_free (curdir);
4413 return panel;
4416 /* --------------------------------------------------------------------------------------------- */
4418 void
4419 panel_reload (WPanel * panel)
4421 struct stat current_stat;
4422 vfs_path_t *cwd_vpath;
4424 if (panels_options.fast_reload && stat (vfs_path_as_str (panel->cwd_vpath), &current_stat) == 0
4425 && current_stat.st_ctime == panel->dir_stat.st_ctime
4426 && current_stat.st_mtime == panel->dir_stat.st_mtime)
4427 return;
4429 cwd_vpath = panel_recursive_cd_to_parent (panel->cwd_vpath);
4430 vfs_path_free (panel->cwd_vpath);
4432 if (cwd_vpath == NULL)
4434 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4435 panel_clean_dir (panel);
4436 dir_list_init (&panel->dir);
4437 return;
4440 panel->cwd_vpath = cwd_vpath;
4441 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4442 show_dir (panel);
4444 dir_list_reload (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4445 &panel->sort_info, panel->filter);
4447 panel->dirty = 1;
4448 if (panel->selected >= panel->dir.len)
4449 do_select (panel, panel->dir.len - 1);
4451 recalculate_panel_summary (panel);
4454 /* --------------------------------------------------------------------------------------------- */
4455 /* Switches the panel to the mode specified in the format */
4456 /* Seting up both format and status string. Return: 0 - on success; */
4457 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4460 set_panel_formats (WPanel * p)
4462 format_e *form;
4463 char *err = NULL;
4464 int retcode = 0;
4466 form = use_display_format (p, panel_format (p), &err, FALSE);
4468 if (err != NULL)
4470 g_free (err);
4471 retcode = 1;
4473 else
4475 delete_format (p->format);
4476 p->format = form;
4479 if (panels_options.show_mini_info)
4481 form = use_display_format (p, mini_status_format (p), &err, TRUE);
4483 if (err != NULL)
4485 g_free (err);
4486 retcode += 2;
4488 else
4490 delete_format (p->status_format);
4491 p->status_format = form;
4495 panel_format_modified (p);
4496 panel_update_cols (WIDGET (p), p->frame_size);
4498 if (retcode)
4499 message (D_ERROR, _("Warning"),
4500 _("User supplied format looks invalid, reverting to default."));
4501 if (retcode & 0x01)
4503 g_free (p->user_format);
4504 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4506 if (retcode & 0x02)
4508 g_free (p->user_status_format[p->list_format]);
4509 p->user_status_format[p->list_format] = g_strdup (DEFAULT_USER_FORMAT);
4512 return retcode;
4515 /* --------------------------------------------------------------------------------------------- */
4517 /* Select current item and readjust the panel */
4518 void
4519 select_item (WPanel * panel)
4521 adjust_top_file (panel);
4523 panel->dirty = 1;
4525 execute_hooks (select_file_hook);
4528 /* --------------------------------------------------------------------------------------------- */
4529 /** Clears all files in the panel, used only when one file was marked */
4530 void
4531 unmark_files (WPanel * panel)
4533 int i;
4535 if (!panel->marked)
4536 return;
4538 for (i = 0; i < panel->dir.len; i++)
4539 file_mark (panel, i, 0);
4541 panel->dirs_marked = 0;
4542 panel->marked = 0;
4543 panel->total = 0;
4546 /* --------------------------------------------------------------------------------------------- */
4547 /** Recalculate the panels summary information, used e.g. when marked
4548 files might have been removed by an external command */
4550 void
4551 recalculate_panel_summary (WPanel * panel)
4553 int i;
4555 panel->marked = 0;
4556 panel->dirs_marked = 0;
4557 panel->total = 0;
4559 for (i = 0; i < panel->dir.len; i++)
4560 if (panel->dir.list[i].f.marked)
4562 /* do_file_mark will return immediately if newmark == oldmark.
4563 So we have to first unmark it to get panel's summary information
4564 updated. (Norbert) */
4565 panel->dir.list[i].f.marked = 0;
4566 do_file_mark (panel, i, 1);
4570 /* --------------------------------------------------------------------------------------------- */
4571 /** This routine marks a file or a directory */
4573 void
4574 do_file_mark (WPanel * panel, int idx, int mark)
4576 if (panel->dir.list[idx].f.marked == mark)
4577 return;
4579 /* Only '..' can't be marked, '.' isn't visible */
4580 if (DIR_IS_DOTDOT (panel->dir.list[idx].fname))
4581 return;
4583 file_mark (panel, idx, mark);
4584 if (panel->dir.list[idx].f.marked)
4586 panel->marked++;
4588 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4590 if (panel->dir.list[idx].f.dir_size_computed)
4591 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4592 panel->dirs_marked++;
4594 else
4595 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4597 set_colors (panel);
4599 else
4601 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4603 if (panel->dir.list[idx].f.dir_size_computed)
4604 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4605 panel->dirs_marked--;
4607 else
4608 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4610 panel->marked--;
4614 /* --------------------------------------------------------------------------------------------- */
4616 * Changes the current directory of the panel.
4617 * Record change in the directory history.
4619 gboolean
4620 do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4622 gboolean r;
4624 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4625 if (r)
4626 directory_history_add (panel, panel->cwd_vpath);
4627 return r;
4630 /* --------------------------------------------------------------------------------------------- */
4632 void
4633 file_mark (WPanel * panel, int lc_index, int val)
4635 if (panel->dir.list[lc_index].f.marked != val)
4637 panel->dir.list[lc_index].f.marked = val;
4638 panel->dirty = 1;
4642 /* --------------------------------------------------------------------------------------------- */
4644 void
4645 panel_re_sort (WPanel * panel)
4647 char *filename;
4648 int i;
4650 if (panel == NULL)
4651 return;
4653 filename = g_strdup (selection (panel)->fname);
4654 unselect_item (panel);
4655 dir_list_sort (&panel->dir, panel->sort_field->sort_routine, &panel->sort_info);
4656 panel->selected = -1;
4658 for (i = panel->dir.len; i != 0; i--)
4659 if (strcmp (panel->dir.list[i - 1].fname, filename) == 0)
4661 panel->selected = i - 1;
4662 break;
4665 g_free (filename);
4666 panel->top_file = panel->selected - panel_items (panel) / 2;
4667 select_item (panel);
4668 panel->dirty = 1;
4671 /* --------------------------------------------------------------------------------------------- */
4673 void
4674 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4676 if (sort_order == NULL)
4677 return;
4679 panel->sort_field = sort_order;
4681 /* The directory is already sorted, we have to load the unsorted stuff */
4682 if (sort_order->sort_routine == (GCompareFunc) unsorted)
4684 char *current_file;
4686 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4687 panel_reload (panel);
4688 try_to_select (panel, current_file);
4689 g_free (current_file);
4691 panel_re_sort (panel);
4694 /* --------------------------------------------------------------------------------------------- */
4696 #ifdef HAVE_CHARSET
4699 * Change panel encoding.
4700 * @param panel WPanel object
4703 void
4704 panel_change_encoding (WPanel * panel)
4706 const char *encoding = NULL;
4707 char *errmsg;
4708 int r;
4710 r = select_charset (-1, -1, panel->codepage, FALSE);
4712 if (r == SELECT_CHARSET_CANCEL)
4713 return; /* Cancel */
4715 panel->codepage = r;
4717 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4719 /* No translation */
4720 vfs_path_t *cd_path_vpath;
4722 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4723 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4724 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4725 show_dir (panel);
4726 vfs_path_free (cd_path_vpath);
4727 return;
4730 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4731 if (errmsg != NULL)
4733 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4734 g_free (errmsg);
4735 return;
4738 encoding = get_codepage_id (panel->codepage);
4739 if (encoding != NULL)
4741 vfs_path_change_encoding (panel->cwd_vpath, encoding);
4743 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4744 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""),
4745 vfs_path_as_str (panel->cwd_vpath));
4749 /* --------------------------------------------------------------------------------------------- */
4752 * Remove encode info from last path element.
4755 vfs_path_t *
4756 remove_encoding_from_path (const vfs_path_t * vpath)
4758 vfs_path_t *ret_vpath;
4759 GString *tmp_conv;
4760 int indx;
4762 ret_vpath = vfs_path_new ();
4764 tmp_conv = g_string_new ("");
4766 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4768 GIConv converter;
4769 vfs_path_element_t *path_element;
4771 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4773 if (path_element->encoding == NULL)
4775 vfs_path_add_element (ret_vpath, path_element);
4776 continue;
4779 converter = str_crt_conv_to (path_element->encoding);
4780 if (converter == INVALID_CONV)
4782 vfs_path_add_element (ret_vpath, path_element);
4783 continue;
4786 MC_PTR_FREE (path_element->encoding);
4788 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4790 g_free (path_element->path);
4791 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4793 g_string_set_size (tmp_conv, 0);
4795 str_close_conv (converter);
4796 str_close_conv (path_element->dir.converter);
4797 path_element->dir.converter = INVALID_CONV;
4798 vfs_path_add_element (ret_vpath, path_element);
4800 g_string_free (tmp_conv, TRUE);
4801 return ret_vpath;
4803 #endif /* HAVE_CHARSET */
4805 /* --------------------------------------------------------------------------------------------- */
4808 * This routine reloads the directory in both panels. It tries to
4809 * select current_file in current_panel and other_file in other_panel.
4810 * If current_file == -1 then it automatically sets current_file and
4811 * other_file to the currently selected files in the panels.
4813 * If flags has the UP_ONLY_CURRENT bit toggled on, then it
4814 * will not reload the other panel.
4816 * @param flags for reload panel
4817 * @param current_file name of the current file
4820 void
4821 update_panels (panel_update_flags_t flags, const char *current_file)
4823 WPanel *panel;
4825 /* first, update other panel... */
4826 if ((flags & UP_ONLY_CURRENT) == 0)
4827 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4828 /* ...then current one */
4829 update_one_panel (get_current_index (), flags, current_file);
4831 if (get_current_type () == view_listing)
4832 panel = PANEL (get_panel_widget (get_current_index ()));
4833 else
4834 panel = PANEL (get_panel_widget (get_other_index ()));
4836 if (!panel->is_panelized)
4837 (void) mc_chdir (panel->cwd_vpath);
4840 /* --------------------------------------------------------------------------------------------- */
4842 gsize
4843 panel_get_num_of_sortable_fields (void)
4845 gsize ret = 0, lc_index;
4847 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4848 if (panel_fields[lc_index].is_user_choice)
4849 ret++;
4850 return ret;
4853 /* --------------------------------------------------------------------------------------------- */
4855 char **
4856 panel_get_sortable_fields (gsize * array_size)
4858 char **ret;
4859 gsize lc_index, i;
4861 lc_index = panel_get_num_of_sortable_fields ();
4863 ret = g_try_new0 (char *, lc_index + 1);
4864 if (ret == NULL)
4865 return NULL;
4867 if (array_size != NULL)
4868 *array_size = lc_index;
4870 lc_index = 0;
4872 for (i = 0; panel_fields[i].id != NULL; i++)
4873 if (panel_fields[i].is_user_choice)
4874 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4876 return ret;
4879 /* --------------------------------------------------------------------------------------------- */
4881 const panel_field_t *
4882 panel_get_field_by_id (const char *name)
4884 gsize lc_index;
4886 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4887 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4888 return &panel_fields[lc_index];
4890 return NULL;
4893 /* --------------------------------------------------------------------------------------------- */
4895 const panel_field_t *
4896 panel_get_field_by_title_hotkey (const char *name)
4898 gsize lc_index;
4900 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4901 if (panel_fields[lc_index].title_hotkey != NULL &&
4902 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4903 return &panel_fields[lc_index];
4905 return NULL;
4908 /* --------------------------------------------------------------------------------------------- */
4910 const panel_field_t *
4911 panel_get_field_by_title (const char *name)
4913 gsize lc_index;
4915 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4917 const char *title;
4919 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4920 if (strcmp (title, name) == 0)
4921 return &panel_fields[lc_index];
4924 return NULL;
4927 /* --------------------------------------------------------------------------------------------- */
4929 gsize
4930 panel_get_num_of_user_possible_fields (void)
4932 gsize ret = 0, lc_index;
4934 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4935 if (panel_fields[lc_index].use_in_user_format)
4936 ret++;
4938 return ret;
4941 /* --------------------------------------------------------------------------------------------- */
4943 char **
4944 panel_get_user_possible_fields (gsize * array_size)
4946 char **ret;
4947 gsize lc_index, i;
4949 lc_index = panel_get_num_of_user_possible_fields ();
4951 ret = g_try_new0 (char *, lc_index + 1);
4952 if (ret == NULL)
4953 return NULL;
4955 if (array_size != NULL)
4956 *array_size = lc_index;
4958 lc_index = 0;
4960 for (i = 0; panel_fields[i].id != NULL; i++)
4961 if (panel_fields[i].use_in_user_format)
4962 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4964 return ret;
4967 /* --------------------------------------------------------------------------------------------- */
4969 void
4970 panel_init (void)
4972 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4973 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ".");
4975 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4976 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4977 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4978 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4979 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4980 panel_filename_scroll_left_char =
4981 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4982 panel_filename_scroll_right_char =
4983 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4985 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4986 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_current_file_to_clip_file",
4987 panel_save_current_file_to_clip_file, NULL, NULL);
4990 /* --------------------------------------------------------------------------------------------- */
4992 void
4993 panel_deinit (void)
4995 g_free (panel_sort_up_sign);
4996 g_free (panel_sort_down_sign);
4998 g_free (panel_hiddenfiles_sign_show);
4999 g_free (panel_hiddenfiles_sign_hide);
5000 g_free (panel_history_prev_item_sign);
5001 g_free (panel_history_next_item_sign);
5002 g_free (panel_history_show_list_sign);
5003 g_free (panel_filename_scroll_left_char);
5004 g_free (panel_filename_scroll_right_char);
5007 /* --------------------------------------------------------------------------------------------- */
5009 gboolean
5010 do_cd (const vfs_path_t * new_dir_vpath, enum cd_enum exact)
5012 gboolean res;
5013 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
5015 if (current_panel->is_panelized)
5017 size_t new_vpath_len;
5019 new_vpath_len = vfs_path_len (new_dir_vpath);
5020 if (vfs_path_equal_len (new_dir_vpath, panelized_panel.root_vpath, new_vpath_len))
5021 _new_dir_vpath = panelized_panel.root_vpath;
5024 res = do_panel_cd (current_panel, _new_dir_vpath, exact);
5026 #ifdef HAVE_CHARSET
5027 if (res)
5029 const vfs_path_element_t *path_element;
5031 path_element = vfs_path_get_by_index (current_panel->cwd_vpath, -1);
5032 if (path_element->encoding != NULL)
5033 current_panel->codepage = get_codepage_index (path_element->encoding);
5034 else
5035 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
5037 #endif /* HAVE_CHARSET */
5039 return res;
5042 /* --------------------------------------------------------------------------------------------- */