src/filemanager/panel.c: clarify condition in 'for' statements.
[midnight-commander.git] / src / filemanager / panel.c
blobd329ae29fbd766c567d78be843dec7d364e64285
1 /*
2 Panel managing.
4 Copyright (C) 1994-2014
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
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/mouse.h" /* For Gpm_Event */
44 #include "lib/tty/key.h" /* XCTRL and ALT macros */
45 #include "lib/skin.h"
46 #include "lib/strescape.h"
47 #include "lib/mcconfig.h"
48 #include "lib/vfs/vfs.h"
49 #include "lib/unixcompat.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.h" /* do_subshell_chdir() */
66 #endif
68 #include "dir.h"
69 #include "boxes.h"
70 #include "tree.h"
71 #include "ext.h" /* regexp_command */
72 #include "layout.h" /* Most layout variables are here */
73 #include "cmd.h"
74 #include "command.h" /* cmdline */
75 #include "usermenu.h"
76 #include "midnight.h"
77 #include "mountlist.h" /* my_statfs */
79 #include "panel.h"
81 /*** global variables ****************************************************************************/
83 /* The hook list for the select file function */
84 hook_t *select_file_hook = NULL;
86 /* *INDENT-OFF* */
87 panelized_panel_t panelized_panel = { {NULL, 0, -1}, NULL };
88 /* *INDENT-ON* */
90 static const char *string_file_name (file_entry_t *, int);
91 static const char *string_file_size (file_entry_t *, int);
92 static const char *string_file_size_brief (file_entry_t *, int);
93 static const char *string_file_type (file_entry_t *, int);
94 static const char *string_file_mtime (file_entry_t *, int);
95 static const char *string_file_atime (file_entry_t *, int);
96 static const char *string_file_ctime (file_entry_t *, int);
97 static const char *string_file_permission (file_entry_t *, int);
98 static const char *string_file_perm_octal (file_entry_t *, int);
99 static const char *string_file_nlinks (file_entry_t *, int);
100 static const char *string_inode (file_entry_t *, int);
101 static const char *string_file_nuid (file_entry_t *, int);
102 static const char *string_file_ngid (file_entry_t *, int);
103 static const char *string_file_owner (file_entry_t *, int);
104 static const char *string_file_group (file_entry_t *, int);
105 static const char *string_marked (file_entry_t *, int);
106 static const char *string_space (file_entry_t *, int);
107 static const char *string_dot (file_entry_t *, int);
109 /* *INDENT-OFF* */
110 panel_field_t panel_fields[] = {
112 "unsorted", 12, TRUE, J_LEFT_FIT,
113 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
114 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
115 N_("sort|u"),
116 N_("&Unsorted"), TRUE, FALSE,
117 string_file_name,
118 (GCompareFunc) unsorted
122 "name", 12, TRUE, J_LEFT_FIT,
123 /* TRANSLATORS: one single character to represent 'name' sort mode */
124 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
125 N_("sort|n"),
126 N_("&Name"), TRUE, TRUE,
127 string_file_name,
128 (GCompareFunc) sort_name
132 "version", 12, TRUE, J_LEFT_FIT,
133 /* TRANSLATORS: one single character to represent 'version' sort mode */
134 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
135 N_("sort|v"),
136 N_("&Version"), TRUE, FALSE,
137 string_file_name,
138 (GCompareFunc) sort_vers
142 "extension", 12, TRUE, J_LEFT_FIT,
143 /* TRANSLATORS: one single character to represent 'extension' sort mode */
144 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
145 N_("sort|e"),
146 N_("E&xtension"), TRUE, FALSE,
147 string_file_name, /* TODO: string_file_ext */
148 (GCompareFunc) sort_ext
152 "size", 7, FALSE, J_RIGHT,
153 /* TRANSLATORS: one single character to represent 'size' sort mode */
154 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
155 N_("sort|s"),
156 N_("&Size"), TRUE, TRUE,
157 string_file_size,
158 (GCompareFunc) sort_size
162 "bsize", 7, FALSE, J_RIGHT,
164 N_("Block Size"), FALSE, FALSE,
165 string_file_size_brief,
166 (GCompareFunc) sort_size
170 "type", 1, FALSE, J_LEFT,
172 "", FALSE, TRUE,
173 string_file_type,
174 NULL
178 "mtime", 12, FALSE, J_RIGHT,
179 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
180 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
181 N_("sort|m"),
182 N_("&Modify time"), TRUE, TRUE,
183 string_file_mtime,
184 (GCompareFunc) sort_time
188 "atime", 12, FALSE, J_RIGHT,
189 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
190 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
191 N_("sort|a"),
192 N_("&Access time"), TRUE, TRUE,
193 string_file_atime,
194 (GCompareFunc) sort_atime
198 "ctime", 12, FALSE, J_RIGHT,
199 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
200 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
201 N_("sort|h"),
202 N_("C&hange time"), TRUE, TRUE,
203 string_file_ctime,
204 (GCompareFunc) sort_ctime
208 "perm", 10, FALSE, J_LEFT,
210 N_("Permission"), FALSE, TRUE,
211 string_file_permission,
212 NULL
216 "mode", 6, FALSE, J_RIGHT,
218 N_("Perm"), FALSE, TRUE,
219 string_file_perm_octal,
220 NULL
224 "nlink", 2, FALSE, J_RIGHT,
226 N_("Nl"), FALSE, TRUE,
227 string_file_nlinks, NULL
231 "inode", 5, FALSE, J_RIGHT,
232 /* TRANSLATORS: one single character to represent 'inode' sort mode */
233 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
234 N_("sort|i"),
235 N_("&Inode"), TRUE, TRUE,
236 string_inode,
237 (GCompareFunc) sort_inode
241 "nuid", 5, FALSE, J_RIGHT,
243 N_("UID"), FALSE, FALSE,
244 string_file_nuid,
245 NULL
249 "ngid", 5, FALSE, J_RIGHT,
251 N_("GID"), FALSE, FALSE,
252 string_file_ngid,
253 NULL
257 "owner", 8, FALSE, J_LEFT_FIT,
259 N_("Owner"), FALSE, TRUE,
260 string_file_owner,
261 NULL
265 "group", 8, FALSE, J_LEFT_FIT,
267 N_("Group"), FALSE, TRUE,
268 string_file_group,
269 NULL
273 "mark", 1, FALSE, J_RIGHT,
275 " ", FALSE, TRUE,
276 string_marked,
277 NULL
281 "|", 1, FALSE, J_RIGHT,
283 " ", FALSE, TRUE,
284 NULL,
285 NULL
289 "space", 1, FALSE, J_RIGHT,
291 " ", FALSE, TRUE,
292 string_space,
293 NULL
297 "dot", 1, FALSE, J_RIGHT,
299 " ", FALSE, FALSE,
300 string_dot,
301 NULL
305 NULL, 0, FALSE, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
308 /* *INDENT-ON* */
310 mc_fhl_t *mc_filehighlight = NULL;
312 extern int saving_setup;
314 /*** file scope macro definitions ****************************************************************/
316 #define NORMAL 0
317 #define SELECTED 1
318 #define MARKED 2
319 #define MARKED_SELECTED 3
320 #define STATUS 5
322 /* This macro extracts the number of available lines in a panel */
323 #define llines(p) (WIDGET (p)->lines - 3 - (panels_options.show_mini_info ? 2 : 0))
325 /*** file scope type declarations ****************************************************************/
327 typedef enum
329 MARK_DONT_MOVE = 0,
330 MARK_DOWN = 1,
331 MARK_FORCE_DOWN = 2,
332 MARK_FORCE_UP = 3
333 } mark_act_t;
336 * This describes a format item. The parse_display_format routine parses
337 * the user specified format and creates a linked list of format_e structures.
339 typedef struct format_e
341 struct format_e *next;
342 int requested_field_len;
343 int field_len;
344 align_crt_t just_mode;
345 gboolean expand;
346 const char *(*string_fn) (file_entry_t *, int len);
347 char *title;
348 const char *id;
349 } format_e;
351 /* File name scroll states */
352 typedef enum
354 FILENAME_NOSCROLL = 1,
355 FILENAME_SCROLL_LEFT = 2,
356 FILENAME_SCROLL_RIGHT = 4
357 } filename_scroll_flag_t;
359 /*** file scope variables ************************************************************************/
361 static char *panel_sort_up_sign = NULL;
362 static char *panel_sort_down_sign = NULL;
364 static char *panel_hiddenfiles_sign_show = NULL;
365 static char *panel_hiddenfiles_sign_hide = NULL;
366 static char *panel_history_prev_item_sign = NULL;
367 static char *panel_history_next_item_sign = NULL;
368 static char *panel_history_show_list_sign = NULL;
369 static char *panel_filename_scroll_left_char = NULL;
370 static char *panel_filename_scroll_right_char = NULL;
372 /* Panel that selection started */
373 static WPanel *mouse_mark_panel = NULL;
375 static int mouse_marking = 0;
376 static int state_mark = 0;
377 /*** file scope functions ************************************************************************/
378 /* --------------------------------------------------------------------------------------------- */
380 static void
381 set_colors (WPanel * panel)
383 (void) panel;
385 tty_set_normal_attrs ();
386 tty_setcolor (NORMAL_COLOR);
389 /* --------------------------------------------------------------------------------------------- */
390 /** Delete format string, it is a linked list */
392 static void
393 delete_format (format_e * format)
395 while (format != NULL)
397 format_e *next = format->next;
398 g_free (format->title);
399 g_free (format);
400 format = next;
404 /* --------------------------------------------------------------------------------------------- */
405 /** This code relies on the default justification!!! */
407 static void
408 add_permission_string (const char *dest, int width, file_entry_t * fe, int attr, int color,
409 int is_octal)
411 int i, r, l;
413 l = get_user_permissions (&fe->st);
415 if (is_octal)
417 /* Place of the access bit in octal mode */
418 l = width + l - 3;
419 r = l + 1;
421 else
423 /* The same to the triplet in string mode */
424 l = l * 3 + 1;
425 r = l + 3;
428 for (i = 0; i < width; i++)
430 if (i >= l && i < r)
432 if (attr == SELECTED || attr == MARKED_SELECTED)
433 tty_setcolor (MARKED_SELECTED_COLOR);
434 else
435 tty_setcolor (MARKED_COLOR);
437 else if (color >= 0)
438 tty_setcolor (color);
439 else
440 tty_lowlevel_setcolor (-color);
442 tty_print_char (dest[i]);
446 /* --------------------------------------------------------------------------------------------- */
447 /** String representations of various file attributes name */
449 static const char *
450 string_file_name (file_entry_t * fe, int len)
452 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
454 (void) len;
456 g_strlcpy (buffer, fe->fname, sizeof (buffer));
457 return buffer;
460 /* --------------------------------------------------------------------------------------------- */
462 static unsigned int
463 ilog10 (dev_t n)
465 unsigned int digits = 0;
469 digits++, n /= 10;
471 while (n != 0);
473 return digits;
476 /* --------------------------------------------------------------------------------------------- */
478 static void
479 format_device_number (char *buf, size_t bufsize, dev_t dev)
481 dev_t major_dev = major (dev);
482 dev_t minor_dev = minor (dev);
483 unsigned int major_digits = ilog10 (major_dev);
484 unsigned int minor_digits = ilog10 (minor_dev);
486 g_assert (bufsize >= 1);
488 if (major_digits + 1 + minor_digits + 1 <= bufsize)
489 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
490 else
491 g_strlcpy (buf, _("[dev]"), bufsize);
494 /* --------------------------------------------------------------------------------------------- */
495 /** size */
497 static const char *
498 string_file_size (file_entry_t * fe, int len)
500 static char buffer[BUF_TINY];
502 /* Don't ever show size of ".." since we don't calculate it */
503 if (DIR_IS_DOTDOT (fe->fname))
504 return _("UP--DIR");
506 #ifdef HAVE_STRUCT_STAT_ST_RDEV
507 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
508 format_device_number (buffer, len + 1, fe->st.st_rdev);
509 else
510 #endif
511 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
513 return buffer;
516 /* --------------------------------------------------------------------------------------------- */
517 /** bsize */
519 static const char *
520 string_file_size_brief (file_entry_t * fe, int len)
522 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
523 return _("SYMLINK");
525 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && !DIR_IS_DOTDOT (fe->fname))
526 return _("SUB-DIR");
528 return string_file_size (fe, len);
531 /* --------------------------------------------------------------------------------------------- */
532 /** This functions return a string representation of a file entry type */
534 static const char *
535 string_file_type (file_entry_t * fe, int len)
537 static char buffer[2];
539 (void) len;
541 if (S_ISDIR (fe->st.st_mode))
542 buffer[0] = PATH_SEP;
543 else if (S_ISLNK (fe->st.st_mode))
545 if (fe->f.link_to_dir)
546 buffer[0] = '~';
547 else if (fe->f.stale_link)
548 buffer[0] = '!';
549 else
550 buffer[0] = '@';
552 else if (S_ISCHR (fe->st.st_mode))
553 buffer[0] = '-';
554 else if (S_ISSOCK (fe->st.st_mode))
555 buffer[0] = '=';
556 else if (S_ISDOOR (fe->st.st_mode))
557 buffer[0] = '>';
558 else if (S_ISBLK (fe->st.st_mode))
559 buffer[0] = '+';
560 else if (S_ISFIFO (fe->st.st_mode))
561 buffer[0] = '|';
562 else if (S_ISNAM (fe->st.st_mode))
563 buffer[0] = '#';
564 else if (!S_ISREG (fe->st.st_mode))
565 buffer[0] = '?'; /* non-regular of unknown kind */
566 else if (is_exe (fe->st.st_mode))
567 buffer[0] = '*';
568 else
569 buffer[0] = ' ';
570 buffer[1] = '\0';
571 return buffer;
574 /* --------------------------------------------------------------------------------------------- */
575 /** mtime */
577 static const char *
578 string_file_mtime (file_entry_t * fe, int len)
580 (void) len;
582 return file_date (fe->st.st_mtime);
585 /* --------------------------------------------------------------------------------------------- */
586 /** atime */
588 static const char *
589 string_file_atime (file_entry_t * fe, int len)
591 (void) len;
593 return file_date (fe->st.st_atime);
596 /* --------------------------------------------------------------------------------------------- */
597 /** ctime */
599 static const char *
600 string_file_ctime (file_entry_t * fe, int len)
602 (void) len;
604 return file_date (fe->st.st_ctime);
607 /* --------------------------------------------------------------------------------------------- */
608 /** perm */
610 static const char *
611 string_file_permission (file_entry_t * fe, int len)
613 (void) len;
615 return string_perm (fe->st.st_mode);
618 /* --------------------------------------------------------------------------------------------- */
619 /** mode */
621 static const char *
622 string_file_perm_octal (file_entry_t * fe, int len)
624 static char buffer[10];
626 (void) len;
628 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
629 return buffer;
632 /* --------------------------------------------------------------------------------------------- */
633 /** nlink */
635 static const char *
636 string_file_nlinks (file_entry_t * fe, int len)
638 static char buffer[BUF_TINY];
640 (void) len;
642 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
643 return buffer;
646 /* --------------------------------------------------------------------------------------------- */
647 /** inode */
649 static const char *
650 string_inode (file_entry_t * fe, int len)
652 static char buffer[10];
654 (void) len;
656 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
657 return buffer;
660 /* --------------------------------------------------------------------------------------------- */
661 /** nuid */
663 static const char *
664 string_file_nuid (file_entry_t * fe, int len)
666 static char buffer[10];
668 (void) len;
670 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
671 return buffer;
674 /* --------------------------------------------------------------------------------------------- */
675 /** ngid */
677 static const char *
678 string_file_ngid (file_entry_t * fe, int len)
680 static char buffer[10];
682 (void) len;
684 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
685 return buffer;
688 /* --------------------------------------------------------------------------------------------- */
689 /** owner */
691 static const char *
692 string_file_owner (file_entry_t * fe, int len)
694 (void) len;
696 return get_owner (fe->st.st_uid);
699 /* --------------------------------------------------------------------------------------------- */
700 /** group */
702 static const char *
703 string_file_group (file_entry_t * fe, int len)
705 (void) len;
707 return get_group (fe->st.st_gid);
710 /* --------------------------------------------------------------------------------------------- */
711 /** mark */
713 static const char *
714 string_marked (file_entry_t * fe, int len)
716 (void) len;
718 return fe->f.marked ? "*" : " ";
721 /* --------------------------------------------------------------------------------------------- */
722 /** space */
724 static const char *
725 string_space (file_entry_t * fe, int len)
727 (void) fe;
728 (void) len;
730 return " ";
733 /* --------------------------------------------------------------------------------------------- */
734 /** dot */
736 static const char *
737 string_dot (file_entry_t * fe, int len)
739 (void) fe;
740 (void) len;
742 return ".";
745 /* --------------------------------------------------------------------------------------------- */
747 static int
748 file_compute_color (int attr, file_entry_t * fe)
750 switch (attr)
752 case SELECTED:
753 return (SELECTED_COLOR);
754 case MARKED:
755 return (MARKED_COLOR);
756 case MARKED_SELECTED:
757 return (MARKED_SELECTED_COLOR);
758 case STATUS:
759 return (NORMAL_COLOR);
760 case NORMAL:
761 default:
762 if (!panels_options.filetype_mode)
763 return (NORMAL_COLOR);
766 return mc_fhl_get_color (mc_filehighlight, fe);
769 /* --------------------------------------------------------------------------------------------- */
770 /** Formats the file number file_index of panel in the buffer dest */
772 static filename_scroll_flag_t
773 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
774 gboolean isstatus, int *field_lenght)
776 int color, length = 0, empty_line;
777 format_e *format, *home;
778 file_entry_t *fe;
779 filename_scroll_flag_t res = FILENAME_NOSCROLL;
781 (void) dest;
782 (void) limit;
784 empty_line = (file_index >= panel->dir.len);
785 home = isstatus ? panel->status_format : panel->format;
786 fe = &panel->dir.list[file_index];
787 *field_lenght = 0;
789 if (!empty_line)
790 color = file_compute_color (attr, fe);
791 else
792 color = NORMAL_COLOR;
794 for (format = home; format != NULL && length != width; format = format->next)
796 if (format->string_fn)
798 const char *txt = " ";
799 int len, perm;
800 const char *prepared_text;
801 int name_offset = 0;
803 if (!empty_line)
804 txt = (*format->string_fn) (fe, format->field_len);
806 len = format->field_len;
807 if (len + length > width)
808 len = width - length;
809 if (len <= 0)
810 break;
812 if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
814 int str_len;
815 int i;
817 *field_lenght = len + 1;
819 str_len = str_length (txt);
820 i = max (0, str_len - len);
821 panel->max_shift = max (panel->max_shift, i);
822 i = min (panel->content_shift, i);
824 if (i > -1)
826 name_offset = str_offset_to_pos (txt, i);
827 if (str_len > len)
829 res = FILENAME_SCROLL_LEFT;
830 if (str_length (txt + name_offset) > len)
831 res |= FILENAME_SCROLL_RIGHT;
836 perm = 0;
837 if (panels_options.permission_mode)
839 if (!strcmp (format->id, "perm"))
840 perm = 1;
841 else if (!strcmp (format->id, "mode"))
842 perm = 2;
845 if (color >= 0)
846 tty_setcolor (color);
847 else
848 tty_lowlevel_setcolor (-color);
850 if (!isstatus && panel->content_shift > -1)
851 prepared_text =
852 str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
853 else
854 prepared_text = str_fit_to_term (txt, len, format->just_mode);
856 if (perm)
857 add_permission_string (prepared_text, format->field_len, fe, attr, color, perm - 1);
858 else
859 tty_print_string (prepared_text);
861 length += len;
863 else
865 if (attr == SELECTED || attr == MARKED_SELECTED)
866 tty_setcolor (SELECTED_COLOR);
867 else
868 tty_setcolor (NORMAL_COLOR);
869 tty_print_one_vline (TRUE);
870 length++;
874 if (length < width)
876 int y, x;
878 tty_getyx (&y, &x);
879 tty_draw_hline (y, x, ' ', width - length);
882 return res;
885 /* --------------------------------------------------------------------------------------------- */
887 static void
888 repaint_file (WPanel * panel, int file_index, gboolean mv, int attr, gboolean isstatus)
890 Widget *w = WIDGET (panel);
892 int second_column = 0;
893 int width;
894 int offset = 0;
895 char buffer[BUF_MEDIUM];
896 filename_scroll_flag_t ret_frm;
897 int ypos = 0;
898 gboolean panel_is_split = !isstatus && panel->split;
899 int fln = 0;
901 width = w->cols - 2;
903 if (panel_is_split)
905 second_column = (file_index - panel->top_file) / llines (panel);
906 width = width / 2 - 1;
908 if (second_column != 0)
910 offset = 1 + width;
911 width = w->cols - offset - 2;
915 /* Nothing to paint */
916 if (width <= 0)
917 return;
919 if (mv)
921 int pos = file_index - panel->top_file;
923 if (panel_is_split)
924 ypos = pos % llines (panel);
925 else
926 ypos = pos;
928 ypos += 2;
929 widget_move (w, ypos, offset + 1);
932 ret_frm = format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus, &fln);
934 if (panel_is_split)
936 if (second_column)
937 tty_print_char (' ');
938 else
940 tty_setcolor (NORMAL_COLOR);
941 tty_print_one_vline (TRUE);
945 if (ret_frm != FILENAME_NOSCROLL && mv)
947 if (!panel_is_split && fln > 0)
949 if (panel->list_type == list_long)
951 offset = width - fln + 1;
952 width = fln - 1;
954 else
955 width = fln;
958 widget_move (w, ypos, offset);
959 tty_setcolor (NORMAL_COLOR);
960 tty_print_string (panel_filename_scroll_left_char);
962 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
964 widget_move (w, ypos, offset + 1 + width);
965 tty_setcolor (NORMAL_COLOR);
966 tty_print_string (panel_filename_scroll_right_char);
971 /* --------------------------------------------------------------------------------------------- */
973 static void
974 display_mini_info (WPanel * panel)
976 Widget *w = WIDGET (panel);
978 if (!panels_options.show_mini_info)
979 return;
981 widget_move (w, llines (panel) + 3, 1);
983 if (panel->searching)
985 tty_setcolor (INPUT_COLOR);
986 tty_print_char ('/');
987 tty_print_string (str_fit_to_term (panel->search_buffer, w->cols - 3, J_LEFT));
988 return;
991 /* Status resolves links and show them */
992 set_colors (panel);
994 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
996 char link_target[MC_MAXPATHLEN];
997 vfs_path_t *lc_link_vpath;
998 int len;
1000 lc_link_vpath =
1001 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
1002 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
1003 vfs_path_free (lc_link_vpath);
1004 if (len > 0)
1006 link_target[len] = 0;
1007 tty_print_string ("-> ");
1008 tty_print_string (str_fit_to_term (link_target, w->cols - 5, J_LEFT_FIT));
1010 else
1011 tty_print_string (str_fit_to_term (_("<readlink failed>"), w->cols - 2, J_LEFT));
1013 else if (DIR_IS_DOTDOT (panel->dir.list[panel->selected].fname))
1015 /* FIXME:
1016 * while loading directory (dir_list_load() and dir_list_reload()),
1017 * the actual stat info about ".." directory isn't got;
1018 * so just don't display incorrect info about ".." directory */
1019 tty_print_string (str_fit_to_term (_("UP--DIR"), w->cols - 2, J_LEFT));
1021 else
1022 /* Default behavior */
1023 repaint_file (panel, panel->selected, FALSE, STATUS, TRUE);
1026 /* --------------------------------------------------------------------------------------------- */
1028 static void
1029 paint_dir (WPanel * panel)
1031 int i;
1032 int items; /* Number of items */
1034 items = llines (panel) * (panel->split ? 2 : 1);
1035 /* reset max len of filename because we have the new max length for the new file list */
1036 panel->max_shift = -1;
1038 for (i = 0; i < items; i++)
1040 int color = 0; /* Color value of the line */
1042 if (i + panel->top_file < panel->dir.len)
1044 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1045 color += (panel->selected == i + panel->top_file && panel->active);
1048 repaint_file (panel, i + panel->top_file, TRUE, color, FALSE);
1051 tty_set_normal_attrs ();
1054 /* --------------------------------------------------------------------------------------------- */
1056 static void
1057 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
1059 Widget *w = WIDGET (panel);
1061 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
1062 int cols;
1064 if (panel->marked <= 0)
1065 return;
1067 buf = size_only ? b_bytes : buffer;
1068 cols = w->cols - 2;
1071 * This is a trick to use two ngettext() calls in one sentence.
1072 * First make "N bytes", then insert it into "X in M files".
1074 g_snprintf (b_bytes, sizeof (b_bytes),
1075 ngettext ("%s byte", "%s bytes", panel->total),
1076 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1077 if (!size_only)
1078 g_snprintf (buffer, sizeof (buffer),
1079 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1080 b_bytes, panel->marked);
1082 /* don't forget spaces around buffer content */
1083 buf = (char *) str_trunc (buf, cols - 4);
1085 if (x < 0)
1086 /* center in panel */
1087 x = (w->cols - str_term_width1 (buf)) / 2 - 1;
1090 * y == llines (panel) + 2 for mini_info_separator
1091 * y == w->lines - 1 for panel bottom frame
1093 widget_move (w, y, x);
1094 tty_setcolor (MARKED_COLOR);
1095 tty_printf (" %s ", buf);
1098 /* --------------------------------------------------------------------------------------------- */
1100 static void
1101 mini_info_separator (WPanel * panel)
1103 if (panels_options.show_mini_info)
1105 Widget *w = WIDGET (panel);
1106 const int y = llines (panel) + 2;
1108 tty_setcolor (NORMAL_COLOR);
1109 tty_draw_hline (w->y + y, w->x + 1, ACS_HLINE, w->cols - 2);
1110 /* Status displays total marked size.
1111 * Centered in panel, full format. */
1112 display_total_marked_size (panel, y, -1, FALSE);
1116 /* --------------------------------------------------------------------------------------------- */
1118 static void
1119 show_free_space (WPanel * panel)
1121 /* Used to figure out how many free space we have */
1122 static struct my_statfs myfs_stats;
1123 /* Old current working directory for displaying free space */
1124 static char *old_cwd = NULL;
1126 /* Don't try to stat non-local fs */
1127 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1128 return;
1130 if (old_cwd == NULL || strcmp (old_cwd, vfs_path_as_str (panel->cwd_vpath)) != 0)
1132 char rpath[PATH_MAX];
1134 init_my_statfs ();
1135 g_free (old_cwd);
1136 old_cwd = g_strdup (vfs_path_as_str (panel->cwd_vpath));
1138 if (mc_realpath (old_cwd, rpath) == NULL)
1139 return;
1141 my_statfs (&myfs_stats, rpath);
1144 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1146 Widget *w = WIDGET (panel);
1147 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1149 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1150 panels_options.kilobyte_si);
1151 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1152 panels_options.kilobyte_si);
1153 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1154 myfs_stats.total == 0 ? 0 :
1155 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1156 widget_move (w, w->lines - 1, w->cols - 2 - (int) strlen (tmp));
1157 tty_setcolor (NORMAL_COLOR);
1158 tty_print_string (tmp);
1162 /* --------------------------------------------------------------------------------------------- */
1164 * Prepare path string for showing in panel's header.
1165 * Passwords will removed, also home dir will replaced by ~
1167 * @param panel WPanel object
1169 * @return newly allocated string.
1172 static char *
1173 panel_correct_path_to_show (WPanel * panel)
1175 vfs_path_t *last_vpath;
1176 const vfs_path_element_t *path_element;
1177 char *return_path;
1178 int elements_count;
1180 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1182 /* get last path element */
1183 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1186 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1187 strcmp (path_element->class->name, "extfs") == 0 ||
1188 strcmp (path_element->class->name, "tarfs") == 0))
1190 const char *archive_name;
1191 const vfs_path_element_t *prev_path_element;
1193 /* get previous path element for catching archive name */
1194 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1195 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1196 if (archive_name != NULL)
1197 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1198 else
1200 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1201 last_vpath->relative = TRUE;
1204 else
1206 last_vpath = vfs_path_new ();
1207 last_vpath->relative = TRUE;
1210 vfs_path_add_element (last_vpath, path_element);
1211 return_path =
1212 vfs_path_to_str_flags (last_vpath, 0,
1213 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1214 vfs_path_free (last_vpath);
1216 return return_path;
1219 /* --------------------------------------------------------------------------------------------- */
1221 * Get Current path element encoding
1223 * @param panel WPanel object
1225 * @return newly allocated string or NULL if path charset is same as system charset
1228 #ifdef HAVE_CHARSET
1229 static char *
1230 panel_get_encoding_info_str (WPanel * panel)
1232 char *ret_str = NULL;
1233 const vfs_path_element_t *path_element;
1235 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1236 if (path_element->encoding != NULL)
1237 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1239 return ret_str;
1241 #endif
1243 /* --------------------------------------------------------------------------------------------- */
1245 static void
1246 show_dir (WPanel * panel)
1248 Widget *w = WIDGET (panel);
1250 gchar *tmp;
1252 set_colors (panel);
1253 tty_draw_box (w->y, w->x, w->lines, w->cols, FALSE);
1255 if (panels_options.show_mini_info)
1257 widget_move (w, llines (panel) + 2, 0);
1258 tty_print_alt_char (ACS_LTEE, FALSE);
1259 widget_move (w, llines (panel) + 2, w->cols - 1);
1260 tty_print_alt_char (ACS_RTEE, FALSE);
1263 widget_move (w, 0, 1);
1264 tty_print_string (panel_history_prev_item_sign);
1266 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1267 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1268 panel_history_next_item_sign);
1270 widget_move (w, 0, w->cols - 6);
1271 tty_print_string (tmp);
1273 g_free (tmp);
1275 widget_move (w, 0, 3);
1277 if (panel->is_panelized)
1278 tty_printf (" %s ", _("Panelize"));
1279 #ifdef HAVE_CHARSET
1280 else
1282 tmp = panel_get_encoding_info_str (panel);
1283 if (tmp != NULL)
1285 tty_printf ("%s", tmp);
1286 widget_move (w, 0, 3 + strlen (tmp));
1287 g_free (tmp);
1290 #endif
1292 if (panel->active)
1293 tty_setcolor (REVERSE_COLOR);
1295 tmp = panel_correct_path_to_show (panel);
1296 tty_printf (" %s ", str_term_trim (tmp, min (max (w->cols - 12, 0), w->cols)));
1297 g_free (tmp);
1299 if (!panels_options.show_mini_info)
1301 if (panel->marked == 0)
1303 /* Show size of curret file in the bottom of panel */
1304 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1306 char buffer[BUF_SMALL];
1308 g_snprintf (buffer, sizeof (buffer), " %s ",
1309 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1310 panels_options.kilobyte_si));
1311 tty_setcolor (NORMAL_COLOR);
1312 widget_move (w, w->lines - 1, 4);
1313 tty_print_string (buffer);
1316 else
1318 /* Show total size of marked files
1319 * In the bottom of panel, display size only. */
1320 display_total_marked_size (panel, w->lines - 1, 2, TRUE);
1324 show_free_space (panel);
1326 if (panel->active)
1327 tty_set_normal_attrs ();
1330 /* --------------------------------------------------------------------------------------------- */
1332 /* Returns the number of items in the given panel */
1333 static int
1334 ITEMS (WPanel * p)
1336 if (p->split)
1337 return llines (p) * 2;
1339 return llines (p);
1342 /* --------------------------------------------------------------------------------------------- */
1344 static void
1345 adjust_top_file (WPanel * panel)
1347 int items = ITEMS (panel);
1349 if (panel->dir.len <= items)
1351 /* If all files fit, show them all. */
1352 panel->top_file = 0;
1354 else
1356 int i;
1358 /* top_file has to be in the range [selected-items+1, selected] so that
1359 the selected file is visible.
1360 top_file should be in the range [0, count-items] so that there's
1361 no empty space wasted.
1362 Within these ranges, adjust it by as little as possible. */
1364 if (panel->top_file < 0)
1365 panel->top_file = 0;
1367 i = panel->selected - items + 1;
1368 if (panel->top_file < i)
1369 panel->top_file = i;
1371 i = panel->dir.len - items;
1372 if (panel->top_file > i)
1373 panel->top_file = i;
1375 if (panel->top_file > panel->selected)
1376 panel->top_file = panel->selected;
1380 /* --------------------------------------------------------------------------------------------- */
1381 /** add "#enc:encodning" to end of path */
1382 /* if path end width a previous #enc:, only encoding is changed no additional
1383 * #enc: is appended
1384 * retun new string
1387 static char *
1388 panel_save_name (WPanel * panel)
1390 /* If the program is shuting down */
1391 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1392 return g_strdup (panel->panel_name);
1394 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1397 /* --------------------------------------------------------------------------------------------- */
1399 static void
1400 directory_history_add (WPanel * panel, const vfs_path_t * vpath)
1402 char *tmp;
1404 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1405 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1406 panel->dir_history_current = panel->dir_history;
1409 /* --------------------------------------------------------------------------------------------- */
1411 /* "history_load" event handler */
1412 static gboolean
1413 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1414 gpointer init_data, gpointer data)
1416 WPanel *p = PANEL (init_data);
1417 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1419 (void) event_group_name;
1420 (void) event_name;
1422 if (ev->receiver == NULL || ev->receiver == WIDGET (p))
1424 if (ev->cfg != NULL)
1425 p->dir_history = history_load (ev->cfg, p->hist_name);
1426 else
1427 p->dir_history = history_get (p->hist_name);
1429 directory_history_add (p, p->cwd_vpath);
1432 return TRUE;
1435 /* --------------------------------------------------------------------------------------------- */
1437 /* "history_save" event handler */
1438 static gboolean
1439 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1440 gpointer init_data, gpointer data)
1442 WPanel *p = PANEL (init_data);
1444 (void) event_group_name;
1445 (void) event_name;
1447 if (p->dir_history != NULL)
1449 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1451 history_save (ev->cfg, p->hist_name, p->dir_history);
1454 return TRUE;
1457 /* --------------------------------------------------------------------------------------------- */
1459 static void
1460 panel_destroy (WPanel * p)
1462 size_t i;
1464 if (panels_options.auto_save_setup)
1466 char *name;
1468 name = panel_save_name (p);
1469 panel_save_setup (p, name);
1470 g_free (name);
1473 panel_clean_dir (p);
1475 /* clean history */
1476 if (p->dir_history != NULL)
1478 /* directory history is already saved before this moment */
1479 p->dir_history = g_list_first (p->dir_history);
1480 g_list_free_full (p->dir_history, g_free);
1482 g_free (p->hist_name);
1484 delete_format (p->format);
1485 delete_format (p->status_format);
1487 g_free (p->user_format);
1488 for (i = 0; i < LIST_TYPES; i++)
1489 g_free (p->user_status_format[i]);
1491 g_free (p->dir.list);
1492 g_free (p->panel_name);
1494 vfs_path_free (p->lwd_vpath);
1495 vfs_path_free (p->cwd_vpath);
1498 /* --------------------------------------------------------------------------------------------- */
1500 static inline void
1501 panel_format_modified (WPanel * panel)
1503 panel->format_modified = 1;
1506 /* --------------------------------------------------------------------------------------------- */
1508 static void
1509 panel_paint_sort_info (WPanel * panel)
1511 if (*panel->sort_field->hotkey != '\0')
1513 const char *sort_sign =
1514 panel->sort_info.reverse ? panel_sort_up_sign : panel_sort_down_sign;
1515 char *str;
1517 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_field->hotkey));
1518 widget_move (panel, 1, 1);
1519 tty_print_string (str);
1520 g_free (str);
1524 /* --------------------------------------------------------------------------------------------- */
1526 static gchar *
1527 panel_get_title_without_hotkey (const char *title)
1529 char *translated_title;
1530 char *hkey;
1532 if (title == NULL)
1533 return NULL;
1534 if (title[0] == '\0')
1535 return g_strdup ("");
1537 translated_title = g_strdup (_(title));
1539 hkey = strchr (translated_title, '&');
1540 if ((hkey != NULL) && (hkey[1] != '\0'))
1541 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1543 return translated_title;
1546 /* --------------------------------------------------------------------------------------------- */
1548 static void
1549 paint_frame (WPanel * panel)
1551 Widget *w = WIDGET (panel);
1553 int side, width;
1555 adjust_top_file (panel);
1557 widget_erase (w);
1558 show_dir (panel);
1560 widget_move (w, 1, 1);
1562 for (side = 0; side <= panel->split; side++)
1564 GString *format_txt;
1565 format_e *format;
1567 if (side)
1569 tty_setcolor (NORMAL_COLOR);
1570 tty_print_one_vline (TRUE);
1571 width = w->cols - w->cols / 2 - 1;
1573 else if (panel->split)
1574 width = w->cols / 2 - 3;
1575 else
1576 width = w->cols - 2;
1578 format_txt = g_string_new ("");
1580 for (format = panel->format; format; format = format->next)
1582 if (format->string_fn)
1584 g_string_set_size (format_txt, 0);
1586 if (panel->list_type == list_long
1587 && strcmp (format->id, panel->sort_field->id) == 0)
1588 g_string_append (format_txt,
1589 panel->sort_info.reverse
1590 ? panel_sort_up_sign : panel_sort_down_sign);
1592 g_string_append (format_txt, format->title);
1594 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1596 g_string_append (format_txt, " [");
1597 g_string_append (format_txt, panel->filter);
1598 g_string_append (format_txt, "]");
1601 tty_setcolor (HEADER_COLOR);
1602 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1603 J_CENTER_LEFT));
1604 width -= format->field_len;
1606 else
1608 tty_setcolor (NORMAL_COLOR);
1609 tty_print_one_vline (TRUE);
1610 width--;
1613 g_string_free (format_txt, TRUE);
1615 if (width > 0)
1617 int y, x;
1619 tty_getyx (&y, &x);
1620 tty_draw_hline (y, x, ' ', width);
1624 if (panel->list_type != list_long)
1625 panel_paint_sort_info (panel);
1628 /* --------------------------------------------------------------------------------------------- */
1630 static const char *
1631 parse_panel_size (WPanel * panel, const char *format, gboolean isstatus)
1633 panel_display_t frame = frame_half;
1634 format = skip_separators (format);
1636 if (!strncmp (format, "full", 4))
1638 frame = frame_full;
1639 format += 4;
1641 else if (!strncmp (format, "half", 4))
1643 frame = frame_half;
1644 format += 4;
1647 if (!isstatus)
1649 panel->frame_size = frame;
1650 panel->split = 0;
1653 /* Now, the optional column specifier */
1654 format = skip_separators (format);
1656 if (*format == '1' || *format == '2')
1658 if (!isstatus)
1659 panel->split = *format == '2';
1660 format++;
1663 if (!isstatus)
1664 panel_update_cols (WIDGET (panel), panel->frame_size);
1666 return skip_separators (format);
1669 /* Format is:
1671 all := panel_format? format
1672 panel_format := [full|half] [1|2]
1673 format := one_format_e
1674 | format , one_format_e
1676 one_format_e := just format.id [opt_size]
1677 just := [<=>]
1678 opt_size := : size [opt_expand]
1679 size := [0-9]+
1680 opt_expand := +
1684 /* --------------------------------------------------------------------------------------------- */
1686 static format_e *
1687 parse_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus,
1688 int *res_total_cols)
1690 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1691 int total_cols = 0; /* Used columns by the format */
1692 size_t i;
1694 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1696 *error = 0;
1698 if (i18n_timelength == 0)
1700 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1702 for (i = 0; panel_fields[i].id != NULL; i++)
1703 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1704 panel_fields[i].min_size = i18n_timelength;
1708 * This makes sure that the panel and mini status full/half mode
1709 * setting is equal
1711 format = parse_panel_size (panel, format, isstatus);
1713 while (*format)
1714 { /* format can be an empty string */
1715 align_crt_t justify; /* Which mode. */
1716 gboolean set_justify = TRUE; /* flag: set justification mode? */
1717 gboolean found = FALSE;
1719 darr = g_new0 (format_e, 1);
1721 /* I'm so ugly, don't look at me :-) */
1722 if (!home)
1723 home = old = darr;
1725 old->next = darr;
1726 darr->next = 0;
1727 old = darr;
1729 format = skip_separators (format);
1731 switch (*format)
1733 case '<':
1734 justify = J_LEFT;
1735 format = skip_separators (format + 1);
1736 break;
1737 case '=':
1738 justify = J_CENTER;
1739 format = skip_separators (format + 1);
1740 break;
1741 case '>':
1742 justify = J_RIGHT;
1743 format = skip_separators (format + 1);
1744 break;
1745 default:
1746 justify = J_LEFT;
1747 set_justify = FALSE;
1748 break;
1751 for (i = 0; panel_fields[i].id != NULL; i++)
1753 size_t klen = strlen (panel_fields[i].id);
1755 if (strncmp (format, panel_fields[i].id, klen) != 0)
1756 continue;
1758 format += klen;
1760 darr->requested_field_len = panel_fields[i].min_size;
1761 darr->string_fn = panel_fields[i].string_fn;
1762 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1764 darr->id = panel_fields[i].id;
1765 darr->expand = panel_fields[i].expands;
1766 darr->just_mode = panel_fields[i].default_just;
1768 if (set_justify)
1770 if (IS_FIT (darr->just_mode))
1771 darr->just_mode = MAKE_FIT (justify);
1772 else
1773 darr->just_mode = justify;
1775 found = TRUE;
1777 format = skip_separators (format);
1779 /* If we have a size specifier */
1780 if (*format == ':')
1782 int req_length;
1784 /* If the size was specified, we don't want
1785 * auto-expansion by default
1787 darr->expand = FALSE;
1788 format++;
1789 req_length = atoi (format);
1790 darr->requested_field_len = req_length;
1792 format = skip_numbers (format);
1794 /* Now, if they insist on expansion */
1795 if (*format == '+')
1797 darr->expand = TRUE;
1798 format++;
1803 break;
1806 if (!found)
1808 char *tmp_format = g_strdup (format);
1810 int pos = min (8, strlen (format));
1811 delete_format (home);
1812 tmp_format[pos] = 0;
1813 *error =
1814 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1815 g_free (tmp_format);
1817 return 0;
1820 total_cols += darr->requested_field_len;
1823 *res_total_cols = total_cols;
1824 return home;
1827 /* --------------------------------------------------------------------------------------------- */
1829 static format_e *
1830 use_display_format (WPanel * panel, const char *format, char **error, gboolean isstatus)
1832 #define MAX_EXPAND 4
1833 int expand_top = 0; /* Max used element in expand */
1834 int usable_columns; /* Usable columns in the panel */
1835 int total_cols = 0;
1836 format_e *darr, *home;
1838 if (!format)
1839 format = DEFAULT_USER_FORMAT;
1841 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1843 if (*error)
1844 return 0;
1846 panel->dirty = 1;
1848 usable_columns = WIDGET (panel)->cols - 2;
1850 /* Status needn't to be split */
1851 if (!isstatus)
1853 usable_columns /= panel->split + 1;
1854 if (panel->split != 0)
1855 usable_columns--;
1858 /* Look for the expandable fields and set field_len based on the requested field len */
1859 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1861 darr->field_len = darr->requested_field_len;
1862 if (darr->expand)
1863 expand_top++;
1866 /* If we used more columns than the available columns, adjust that */
1867 if (total_cols > usable_columns)
1869 int dif;
1870 int pdif = 0;
1872 dif = total_cols - usable_columns;
1874 while (dif != 0 && pdif != dif)
1876 pdif = dif;
1878 for (darr = home; darr; darr = darr->next)
1879 if (dif != 0 && darr->field_len != 1)
1881 darr->field_len--;
1882 dif--;
1886 total_cols = usable_columns; /* give up, the rest should be truncated */
1889 /* Expand the available space */
1890 if (usable_columns > total_cols && expand_top != 0)
1892 int i;
1893 int spaces = (usable_columns - total_cols) / expand_top;
1895 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1896 if (darr->expand)
1898 darr->field_len += spaces;
1899 if (i == 0)
1900 darr->field_len += (usable_columns - total_cols) % expand_top;
1901 i++;
1904 return home;
1907 /* --------------------------------------------------------------------------------------------- */
1908 /** Given the panel->view_type returns the format string to be parsed */
1910 static const char *
1911 panel_format (WPanel * panel)
1913 switch (panel->list_type)
1915 case list_long:
1916 return "full perm space nlink space owner space group space size space mtime space name";
1918 case list_brief:
1919 return "half 2 type name";
1921 case list_user:
1922 return panel->user_format;
1924 default:
1925 case list_full:
1926 return "half type name | size | mtime";
1930 /* --------------------------------------------------------------------------------------------- */
1932 static const char *
1933 mini_status_format (WPanel * panel)
1935 if (panel->user_mini_status)
1936 return panel->user_status_format[panel->list_type];
1938 switch (panel->list_type)
1940 case list_long:
1941 return "full perm space nlink space owner space group space size space mtime space name";
1943 case list_brief:
1944 return "half type name space bsize space perm space";
1946 case list_full:
1947 return "half type name";
1949 default:
1950 case list_user:
1951 return panel->user_format;
1955 /* */
1956 /* Panel operation commands */
1957 /* */
1959 /* --------------------------------------------------------------------------------------------- */
1960 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1962 static cb_ret_t
1963 maybe_cd (gboolean move_up_dir)
1965 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1967 if (move_up_dir)
1969 vfs_path_t *up_dir;
1971 up_dir = vfs_path_from_str ("..");
1972 do_cd (up_dir, cd_exact);
1973 vfs_path_free (up_dir);
1974 return MSG_HANDLED;
1977 if (S_ISDIR (selection (current_panel)->st.st_mode)
1978 || link_isdir (selection (current_panel)))
1980 vfs_path_t *vpath;
1982 vpath = vfs_path_from_str (selection (current_panel)->fname);
1983 do_cd (vpath, cd_exact);
1984 vfs_path_free (vpath);
1985 return MSG_HANDLED;
1989 return MSG_NOT_HANDLED;
1992 /* --------------------------------------------------------------------------------------------- */
1994 /* if command line is empty then do 'cd ..' */
1995 static cb_ret_t
1996 force_maybe_cd (void)
1998 if (cmdline->buffer[0] == '\0')
2000 vfs_path_t *up_dir = vfs_path_from_str ("..");
2001 do_cd (up_dir, cd_exact);
2002 vfs_path_free (up_dir);
2003 return MSG_HANDLED;
2006 return MSG_NOT_HANDLED;
2009 /* --------------------------------------------------------------------------------------------- */
2011 static inline void
2012 unselect_item (WPanel * panel)
2014 repaint_file (panel, panel->selected, TRUE, 2 * selection (panel)->f.marked, FALSE);
2017 /* --------------------------------------------------------------------------------------------- */
2019 static void
2020 move_down (WPanel * panel)
2022 if (panel->selected + 1 == panel->dir.len)
2023 return;
2025 unselect_item (panel);
2026 panel->selected++;
2028 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
2030 /* Scroll window half screen */
2031 panel->top_file += ITEMS (panel) / 2;
2032 if (panel->top_file > panel->dir.len - ITEMS (panel))
2033 panel->top_file = panel->dir.len - ITEMS (panel);
2034 paint_dir (panel);
2036 select_item (panel);
2039 /* --------------------------------------------------------------------------------------------- */
2041 static void
2042 move_up (WPanel * panel)
2044 if (panel->selected == 0)
2045 return;
2047 unselect_item (panel);
2048 panel->selected--;
2050 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2052 /* Scroll window half screen */
2053 panel->top_file -= ITEMS (panel) / 2;
2054 if (panel->top_file < 0)
2055 panel->top_file = 0;
2056 paint_dir (panel);
2058 select_item (panel);
2061 /* --------------------------------------------------------------------------------------------- */
2062 /** Changes the selection by lines (may be negative) */
2064 static void
2065 move_selection (WPanel * panel, int lines)
2067 int new_pos;
2068 gboolean adjust = FALSE;
2070 new_pos = panel->selected + lines;
2071 if (new_pos >= panel->dir.len)
2072 new_pos = panel->dir.len - 1;
2074 if (new_pos < 0)
2075 new_pos = 0;
2077 unselect_item (panel);
2078 panel->selected = new_pos;
2080 if (panel->selected - panel->top_file >= ITEMS (panel))
2082 panel->top_file += lines;
2083 adjust = TRUE;
2086 if (panel->selected - panel->top_file < 0)
2088 panel->top_file += lines;
2089 adjust = TRUE;
2092 if (adjust)
2094 if (panel->top_file > panel->selected)
2095 panel->top_file = panel->selected;
2096 if (panel->top_file < 0)
2097 panel->top_file = 0;
2098 paint_dir (panel);
2100 select_item (panel);
2103 /* --------------------------------------------------------------------------------------------- */
2105 static cb_ret_t
2106 move_left (WPanel * panel)
2108 if (panel->split)
2110 move_selection (panel, -llines (panel));
2111 return MSG_HANDLED;
2114 return maybe_cd (TRUE); /* cd .. */
2117 /* --------------------------------------------------------------------------------------------- */
2119 static cb_ret_t
2120 move_right (WPanel * panel)
2122 if (panel->split)
2124 move_selection (panel, llines (panel));
2125 return MSG_HANDLED;
2128 return maybe_cd (FALSE); /* cd (selection) */
2131 /* --------------------------------------------------------------------------------------------- */
2133 static void
2134 prev_page (WPanel * panel)
2136 int items;
2138 if (!panel->selected && !panel->top_file)
2139 return;
2141 unselect_item (panel);
2142 items = ITEMS (panel);
2143 if (panel->top_file < items)
2144 items = panel->top_file;
2145 if (!items)
2146 panel->selected = 0;
2147 else
2148 panel->selected -= items;
2149 panel->top_file -= items;
2151 select_item (panel);
2152 paint_dir (panel);
2155 /* --------------------------------------------------------------------------------------------- */
2157 static void
2158 goto_parent_dir (WPanel * panel)
2160 if (!panel->is_panelized)
2162 vfs_path_t *up_dir;
2164 up_dir = vfs_path_from_str ("..");
2165 do_cd (up_dir, cd_exact);
2166 vfs_path_free (up_dir);
2168 else
2170 char *fname = panel->dir.list[panel->selected].fname;
2171 const char *bname;
2172 vfs_path_t *dname_vpath;
2174 if (g_path_is_absolute (fname))
2175 fname = g_strdup (fname);
2176 else
2177 fname =
2178 mc_build_filename (vfs_path_as_str (panelized_panel.root_vpath), fname,
2179 (char *) NULL);
2181 bname = x_basename (fname);
2183 if (bname == fname)
2184 dname_vpath = vfs_path_from_str (".");
2185 else
2187 char *dname;
2189 dname = g_strndup (fname, bname - fname);
2190 dname_vpath = vfs_path_from_str (dname);
2191 g_free (dname);
2194 do_cd (dname_vpath, cd_exact);
2195 try_to_select (panel, bname);
2197 vfs_path_free (dname_vpath);
2198 g_free (fname);
2202 /* --------------------------------------------------------------------------------------------- */
2204 static void
2205 next_page (WPanel * panel)
2207 int items;
2209 if (panel->selected == panel->dir.len - 1)
2210 return;
2212 unselect_item (panel);
2213 items = ITEMS (panel);
2214 if (panel->top_file > panel->dir.len - 2 * items)
2215 items = panel->dir.len - items - panel->top_file;
2216 if (panel->top_file + items < 0)
2217 items = -panel->top_file;
2218 if (!items)
2219 panel->selected = panel->dir.len - 1;
2220 else
2221 panel->selected += items;
2222 panel->top_file += items;
2224 select_item (panel);
2225 paint_dir (panel);
2228 /* --------------------------------------------------------------------------------------------- */
2230 static void
2231 goto_child_dir (WPanel * panel)
2233 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2235 vfs_path_t *vpath;
2237 vpath = vfs_path_from_str (selection (panel)->fname);
2238 do_cd (vpath, cd_exact);
2239 vfs_path_free (vpath);
2243 /* --------------------------------------------------------------------------------------------- */
2245 static void
2246 goto_top_file (WPanel * panel)
2248 unselect_item (panel);
2249 panel->selected = panel->top_file;
2250 select_item (panel);
2253 /* --------------------------------------------------------------------------------------------- */
2255 static void
2256 goto_middle_file (WPanel * panel)
2258 unselect_item (panel);
2259 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2260 select_item (panel);
2263 /* --------------------------------------------------------------------------------------------- */
2265 static void
2266 goto_bottom_file (WPanel * panel)
2268 unselect_item (panel);
2269 panel->selected = panel->top_file + ITEMS (panel) - 1;
2270 select_item (panel);
2273 /* --------------------------------------------------------------------------------------------- */
2275 static void
2276 move_home (WPanel * panel)
2278 if (panel->selected == 0)
2279 return;
2281 unselect_item (panel);
2283 if (panels_options.torben_fj_mode)
2285 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2287 if (panel->selected > middle_pos)
2289 goto_middle_file (panel);
2290 return;
2292 if (panel->selected != panel->top_file)
2294 goto_top_file (panel);
2295 return;
2299 panel->top_file = 0;
2300 panel->selected = 0;
2302 paint_dir (panel);
2303 select_item (panel);
2306 /* --------------------------------------------------------------------------------------------- */
2308 static void
2309 move_end (WPanel * panel)
2311 if (panel->selected == panel->dir.len - 1)
2312 return;
2314 unselect_item (panel);
2316 if (panels_options.torben_fj_mode)
2318 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2320 if (panel->selected < middle_pos)
2322 goto_middle_file (panel);
2323 return;
2325 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2327 goto_bottom_file (panel);
2328 return;
2332 panel->selected = panel->dir.len - 1;
2333 paint_dir (panel);
2334 select_item (panel);
2337 /* --------------------------------------------------------------------------------------------- */
2339 static void
2340 do_mark_file (WPanel * panel, mark_act_t do_move)
2342 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2343 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2344 move_down (panel);
2345 else if (do_move == MARK_FORCE_UP)
2346 move_up (panel);
2349 /* --------------------------------------------------------------------------------------------- */
2351 static inline void
2352 mark_file (WPanel * panel)
2354 do_mark_file (panel, MARK_DOWN);
2357 /* --------------------------------------------------------------------------------------------- */
2359 static inline void
2360 mark_file_up (WPanel * panel)
2362 do_mark_file (panel, MARK_FORCE_UP);
2365 /* --------------------------------------------------------------------------------------------- */
2367 static inline void
2368 mark_file_down (WPanel * panel)
2370 do_mark_file (panel, MARK_FORCE_DOWN);
2373 /* --------------------------------------------------------------------------------------------- */
2375 static void
2376 mark_file_right (WPanel * panel)
2378 int lines = llines (panel);
2380 if (state_mark < 0)
2381 state_mark = selection (panel)->f.marked ? 0 : 1;
2383 lines = min (lines, panel->dir.len - panel->selected - 1);
2384 for (; lines != 0; lines--)
2386 do_file_mark (panel, panel->selected, state_mark);
2387 move_down (panel);
2389 do_file_mark (panel, panel->selected, state_mark);
2392 /* --------------------------------------------------------------------------------------------- */
2394 static void
2395 mark_file_left (WPanel * panel)
2397 int lines = llines (panel);
2399 if (state_mark < 0)
2400 state_mark = selection (panel)->f.marked ? 0 : 1;
2402 lines = min (lines, panel->selected + 1);
2403 for (; lines != 0; lines--)
2405 do_file_mark (panel, panel->selected, state_mark);
2406 move_up (panel);
2408 do_file_mark (panel, panel->selected, state_mark);
2411 /* --------------------------------------------------------------------------------------------- */
2412 /** Incremental search of a file name in the panel.
2413 * @param panel instance of WPanel structure
2414 * @param c_code key code
2417 static void
2418 do_search (WPanel * panel, int c_code)
2420 size_t l;
2421 int i, sel;
2422 gboolean wrapped = FALSE;
2423 char *act;
2424 mc_search_t *search;
2425 char *reg_exp, *esc_str;
2426 gboolean is_found = FALSE;
2428 l = strlen (panel->search_buffer);
2429 if (c_code == KEY_BACKSPACE)
2431 if (l != 0)
2433 act = panel->search_buffer + l;
2434 str_prev_noncomb_char (&act, panel->search_buffer);
2435 act[0] = '\0';
2437 panel->search_chpoint = 0;
2439 else
2441 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2443 panel->search_char[panel->search_chpoint] = c_code;
2444 panel->search_chpoint++;
2447 if (panel->search_chpoint > 0)
2449 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2451 case -2:
2452 return;
2453 case -1:
2454 panel->search_chpoint = 0;
2455 return;
2456 default:
2457 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2459 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2460 l += panel->search_chpoint;
2461 *(panel->search_buffer + l) = '\0';
2462 panel->search_chpoint = 0;
2468 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2469 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2470 search = mc_search_new (esc_str, -1, NULL);
2471 search->search_type = MC_SEARCH_T_GLOB;
2472 search->is_entire_line = TRUE;
2474 switch (panels_options.qsearch_mode)
2476 case QSEARCH_CASE_SENSITIVE:
2477 search->is_case_sensitive = TRUE;
2478 break;
2479 case QSEARCH_CASE_INSENSITIVE:
2480 search->is_case_sensitive = FALSE;
2481 break;
2482 default:
2483 search->is_case_sensitive = panel->sort_info.case_sensitive;
2484 break;
2487 sel = panel->selected;
2489 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2491 if (i >= panel->dir.len)
2493 i = 0;
2494 if (wrapped)
2495 break;
2496 wrapped = TRUE;
2498 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2500 sel = i;
2501 is_found = TRUE;
2502 break;
2505 if (is_found)
2507 unselect_item (panel);
2508 panel->selected = sel;
2509 select_item (panel);
2510 widget_redraw (WIDGET (panel));
2512 else if (c_code != KEY_BACKSPACE)
2514 act = panel->search_buffer + l;
2515 str_prev_noncomb_char (&act, panel->search_buffer);
2516 act[0] = '\0';
2518 mc_search_free (search);
2519 g_free (reg_exp);
2520 g_free (esc_str);
2523 /* --------------------------------------------------------------------------------------------- */
2524 /** Start new search.
2525 * @param panel instance of WPanel structure
2528 static void
2529 start_search (WPanel * panel)
2531 if (panel->searching)
2533 if (panel->selected + 1 == panel->dir.len)
2534 panel->selected = 0;
2535 else
2536 move_down (panel);
2538 /* in case if there was no search string we need to recall
2539 previous string, with which we ended previous searching */
2540 if (panel->search_buffer[0] == '\0')
2541 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2542 sizeof (panel->search_buffer));
2544 do_search (panel, 0);
2546 else
2548 panel->searching = TRUE;
2549 panel->search_buffer[0] = '\0';
2550 panel->search_char[0] = '\0';
2551 panel->search_chpoint = 0;
2552 display_mini_info (panel);
2553 mc_refresh ();
2557 /* --------------------------------------------------------------------------------------------- */
2559 static void
2560 stop_search (WPanel * panel)
2562 panel->searching = FALSE;
2564 /* if user had overrdied search string, we need to store it
2565 to the previous_search_buffer */
2566 if (panel->search_buffer[0] != '\0')
2567 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2568 sizeof (panel->prev_search_buffer));
2570 display_mini_info (panel);
2573 /* --------------------------------------------------------------------------------------------- */
2574 /** Return 1 if the Enter key has been processed, 0 otherwise */
2576 static int
2577 do_enter_on_file_entry_t (file_entry_t * fe)
2579 vfs_path_t *full_name_vpath;
2580 gboolean ok;
2583 * Directory or link to directory - change directory.
2584 * Try the same for the entries on which mc_lstat() has failed.
2586 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2588 vfs_path_t *fname_vpath;
2590 fname_vpath = vfs_path_from_str (fe->fname);
2591 if (!do_cd (fname_vpath, cd_exact))
2592 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2593 vfs_path_free (fname_vpath);
2594 return 1;
2597 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2599 /* Try associated command */
2600 if (regex_command (full_name_vpath, "Open") != 0)
2602 vfs_path_free (full_name_vpath);
2603 return 1;
2606 /* Check if the file is executable */
2607 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2608 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2609 vfs_path_free (full_name_vpath);
2610 if (!ok)
2611 return 0;
2613 if (confirm_execute)
2615 if (query_dialog
2616 (_("The Midnight Commander"),
2617 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2618 return 1;
2621 if (!vfs_current_is_local ())
2623 int ret;
2624 vfs_path_t *tmp_vpath;
2626 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2627 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2628 vfs_path_free (tmp_vpath);
2629 /* We took action only if the dialog was shown or the execution
2630 * was successful */
2631 return confirm_execute || (ret == 0);
2635 char *tmp = name_quote (fe->fname, 0);
2636 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2637 g_free (tmp);
2638 shell_execute (cmd, 0);
2639 g_free (cmd);
2642 #ifdef HAVE_CHARSET
2643 mc_global.source_codepage = default_source_codepage;
2644 #endif
2646 return 1;
2649 /* --------------------------------------------------------------------------------------------- */
2651 static int
2652 do_enter (WPanel * panel)
2654 return do_enter_on_file_entry_t (selection (panel));
2657 /* --------------------------------------------------------------------------------------------- */
2659 static void
2660 chdir_other_panel (WPanel * panel)
2662 const file_entry_t *entry = &panel->dir.list[panel->selected];
2664 vfs_path_t *new_dir_vpath;
2665 char *sel_entry = NULL;
2667 if (get_other_type () != view_listing)
2668 set_display_type (get_other_index (), view_listing);
2670 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2671 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2672 else
2674 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2675 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2678 change_panel ();
2679 do_cd (new_dir_vpath, cd_exact);
2680 vfs_path_free (new_dir_vpath);
2682 if (sel_entry)
2683 try_to_select (current_panel, sel_entry);
2684 change_panel ();
2686 move_down (panel);
2689 /* --------------------------------------------------------------------------------------------- */
2691 * Make the current directory of the current panel also the current
2692 * directory of the other panel. Put the other panel to the listing
2693 * mode if needed. If the current panel is panelized, the other panel
2694 * doesn't become panelized.
2697 static void
2698 panel_sync_other (const WPanel * panel)
2700 if (get_other_type () != view_listing)
2701 set_display_type (get_other_index (), view_listing);
2703 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2705 /* try to select current filename on the other panel */
2706 if (!panel->is_panelized)
2707 try_to_select (other_panel, selection (panel)->fname);
2710 /* --------------------------------------------------------------------------------------------- */
2712 static void
2713 chdir_to_readlink (WPanel * panel)
2715 vfs_path_t *new_dir_vpath;
2716 char buffer[MC_MAXPATHLEN];
2717 int i;
2718 struct stat st;
2719 vfs_path_t *panel_fname_vpath;
2720 gboolean ok;
2722 if (get_other_type () != view_listing)
2723 return;
2725 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2726 return;
2728 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2729 if (i < 0)
2730 return;
2732 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2733 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2734 vfs_path_free (panel_fname_vpath);
2735 if (!ok)
2736 return;
2738 buffer[i] = 0;
2739 if (!S_ISDIR (st.st_mode))
2741 char *p;
2743 p = strrchr (buffer, PATH_SEP);
2744 if (p && !p[1])
2746 *p = 0;
2747 p = strrchr (buffer, PATH_SEP);
2749 if (!p)
2750 return;
2751 p[1] = 0;
2753 if (*buffer == PATH_SEP)
2754 new_dir_vpath = vfs_path_from_str (buffer);
2755 else
2756 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2758 change_panel ();
2759 do_cd (new_dir_vpath, cd_exact);
2760 vfs_path_free (new_dir_vpath);
2761 change_panel ();
2763 move_down (panel);
2766 /* --------------------------------------------------------------------------------------------- */
2768 static gsize
2769 panel_get_format_field_count (WPanel * panel)
2771 format_e *format;
2772 gsize lc_index;
2774 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++)
2777 return lc_index;
2780 /* --------------------------------------------------------------------------------------------- */
2782 function return 0 if not found and REAL_INDEX+1 if found
2785 static gsize
2786 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2788 format_e *format;
2789 gsize lc_index;
2791 for (lc_index = 1, format = panel->format;
2792 format != NULL && strcmp (format->title, name) != 0; format = format->next, lc_index++)
2795 if (format == NULL)
2796 lc_index = 0;
2798 return lc_index;
2801 /* --------------------------------------------------------------------------------------------- */
2803 static format_e *
2804 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2806 format_e *format;
2808 for (format = panel->format; format != NULL && lc_index != 0; format = format->next, lc_index--)
2811 return format;
2814 /* --------------------------------------------------------------------------------------------- */
2816 static const panel_field_t *
2817 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2819 const panel_field_t *pfield;
2820 format_e *format;
2822 format = panel_get_format_field_by_index (panel, lc_index);
2823 if (format == NULL)
2824 return NULL;
2826 pfield = panel_get_field_by_title (format->title);
2827 if (pfield == NULL)
2828 return NULL;
2829 if (pfield->sort_routine == NULL)
2830 return NULL;
2831 return pfield;
2834 /* --------------------------------------------------------------------------------------------- */
2836 static void
2837 panel_toggle_sort_order_prev (WPanel * panel)
2839 gsize lc_index, i;
2840 gchar *title;
2841 const panel_field_t *pfield = NULL;
2843 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
2844 lc_index = panel_get_format_field_index_by_name (panel, title);
2845 g_free (title);
2847 if (lc_index > 1)
2849 /* search for prev sortable column in panel format */
2850 for (i = lc_index - 1;
2851 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
2855 if (pfield == NULL)
2857 /* Sortable field not found. Try to search in each array */
2858 for (i = panel_get_format_field_count (panel);
2859 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--)
2863 if (pfield != NULL)
2865 panel->sort_field = pfield;
2866 panel_set_sort_order (panel, pfield);
2870 /* --------------------------------------------------------------------------------------------- */
2872 static void
2873 panel_toggle_sort_order_next (WPanel * panel)
2875 gsize lc_index, i;
2876 const panel_field_t *pfield = NULL;
2877 gsize format_field_count;
2878 gchar *title;
2880 format_field_count = panel_get_format_field_count (panel);
2881 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
2882 lc_index = panel_get_format_field_index_by_name (panel, title);
2883 g_free (title);
2885 if (lc_index != 0 && lc_index != format_field_count)
2887 /* search for prev sortable column in panel format */
2888 for (i = lc_index;
2889 i != format_field_count
2890 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
2894 if (pfield == NULL)
2896 /* Sortable field not found. Try to search in each array */
2897 for (i = 0;
2898 i != format_field_count
2899 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++)
2903 if (pfield != NULL)
2905 panel->sort_field = pfield;
2906 panel_set_sort_order (panel, pfield);
2910 /* --------------------------------------------------------------------------------------------- */
2912 static void
2913 panel_select_sort_order (WPanel * panel)
2915 const panel_field_t *sort_order;
2917 sort_order = sort_box (&panel->sort_info, panel->sort_field);
2918 if (sort_order != NULL)
2920 panel->sort_field = sort_order;
2921 panel_set_sort_order (panel, sort_order);
2925 /* --------------------------------------------------------------------------------------------- */
2928 * panel_content_scroll_left:
2929 * @param panel the pointer to the panel on which we operate
2931 * scroll long filename to the left (decrement scroll pointer)
2935 static void
2936 panel_content_scroll_left (WPanel * panel)
2938 if (panel->content_shift > -1)
2940 if (panel->content_shift > panel->max_shift)
2941 panel->content_shift = panel->max_shift;
2943 panel->content_shift--;
2944 show_dir (panel);
2945 paint_dir (panel);
2949 /* --------------------------------------------------------------------------------------------- */
2952 * panel_content_scroll_right:
2953 * @param panel the pointer to the panel on which we operate
2955 * scroll long filename to the right (increment scroll pointer)
2959 static void
2960 panel_content_scroll_right (WPanel * panel)
2962 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
2964 panel->content_shift++;
2965 show_dir (panel);
2966 paint_dir (panel);
2970 /* --------------------------------------------------------------------------------------------- */
2972 static void
2973 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2975 if (strcmp (panel->sort_field->id, name) != 0)
2977 const panel_field_t *sort_order;
2979 sort_order = panel_get_field_by_id (name);
2980 if (sort_order == NULL)
2981 return;
2983 panel->sort_field = sort_order;
2985 else
2986 panel->sort_info.reverse = !panel->sort_info.reverse;
2988 panel_set_sort_order (panel, panel->sort_field);
2991 /* --------------------------------------------------------------------------------------------- */
2993 * If we moved to the parent directory move the selection pointer to
2994 * the old directory name; If we leave VFS dir, remove FS specificator.
2996 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2999 static const char *
3000 get_parent_dir_name (const vfs_path_t * cwd_vpath, const vfs_path_t * lwd_vpath)
3002 size_t llen, clen;
3003 const char *p, *cwd, *lwd;
3005 llen = vfs_path_len (lwd_vpath);
3006 clen = vfs_path_len (cwd_vpath);
3008 if (llen <= clen)
3009 return NULL;
3011 cwd = vfs_path_as_str (cwd_vpath);
3012 lwd = vfs_path_as_str (lwd_vpath);
3014 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
3016 if (p == NULL)
3018 p = strrchr (lwd, PATH_SEP);
3020 if ((p != NULL)
3021 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
3022 && (clen == (size_t) (p - lwd)
3023 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
3024 return (p + 1);
3026 return NULL;
3029 /* skip VFS prefix */
3030 while (--p > lwd && *p != PATH_SEP)
3032 /* get last component */
3033 while (--p > lwd && *p != PATH_SEP)
3036 /* return last component */
3037 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
3040 /* --------------------------------------------------------------------------------------------- */
3041 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3043 static void
3044 subshell_chdir (const vfs_path_t * vpath)
3046 #ifdef ENABLE_SUBSHELL
3047 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3048 do_subshell_chdir (vpath, FALSE);
3049 #else /* ENABLE_SUBSHELL */
3050 (void) vpath;
3051 #endif /* ENABLE_SUBSHELL */
3054 /* --------------------------------------------------------------------------------------------- */
3056 * Changes the current directory of the panel.
3057 * Don't record change in the directory history.
3060 static gboolean
3061 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3063 vfs_path_t *olddir_vpath;
3065 /* Convert *new_path to a suitable pathname, handle ~user */
3066 if (cd_type == cd_parse_command)
3068 const vfs_path_element_t *element;
3070 element = vfs_path_get_by_index (new_dir_vpath, 0);
3071 if (strcmp (element->path, "-") == 0)
3072 new_dir_vpath = panel->lwd_vpath;
3075 if (mc_chdir (new_dir_vpath) == -1)
3076 return FALSE;
3078 /* Success: save previous directory, shutdown status of previous dir */
3079 olddir_vpath = vfs_path_clone (panel->cwd_vpath);
3080 panel_set_lwd (panel, panel->cwd_vpath);
3081 input_free_completions (cmdline);
3083 vfs_path_free (panel->cwd_vpath);
3084 vfs_setup_cwd ();
3085 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3087 vfs_release_path (olddir_vpath);
3089 subshell_chdir (panel->cwd_vpath);
3091 /* Reload current panel */
3092 panel_clean_dir (panel);
3094 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
3095 &panel->sort_info, panel->filter);
3096 try_to_select (panel, get_parent_dir_name (panel->cwd_vpath, olddir_vpath));
3098 load_hint (0);
3099 panel->dirty = 1;
3100 update_xterm_title_path ();
3102 vfs_path_free (olddir_vpath);
3104 return TRUE;
3107 /* --------------------------------------------------------------------------------------------- */
3109 static void
3110 directory_history_next (WPanel * panel)
3112 gboolean ok;
3116 GList *next;
3118 ok = TRUE;
3119 next = g_list_next (panel->dir_history_current);
3120 if (next != NULL)
3122 vfs_path_t *data_vpath;
3124 data_vpath = vfs_path_from_str ((char *) next->data);
3125 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3126 vfs_path_free (data_vpath);
3127 panel->dir_history_current = next;
3129 /* skip directories that present in history but absent in file system */
3131 while (!ok);
3134 /* --------------------------------------------------------------------------------------------- */
3136 static void
3137 directory_history_prev (WPanel * panel)
3139 gboolean ok;
3143 GList *prev;
3145 ok = TRUE;
3146 prev = g_list_previous (panel->dir_history_current);
3147 if (prev != NULL)
3149 vfs_path_t *data_vpath;
3151 data_vpath = vfs_path_from_str ((char *) prev->data);
3152 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3153 vfs_path_free (data_vpath);
3154 panel->dir_history_current = prev;
3156 /* skip directories that present in history but absent in file system */
3158 while (!ok);
3161 /* --------------------------------------------------------------------------------------------- */
3163 static void
3164 directory_history_list (WPanel * panel)
3166 char *s;
3167 gboolean ok = FALSE;
3168 size_t pos;
3170 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3172 s = history_show (&panel->dir_history, WIDGET (panel), pos);
3173 if (s != NULL)
3175 vfs_path_t *s_vpath;
3177 s_vpath = vfs_path_from_str (s);
3178 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3179 if (ok)
3180 directory_history_add (panel, panel->cwd_vpath);
3181 else
3182 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3183 vfs_path_free (s_vpath);
3184 g_free (s);
3187 if (!ok)
3189 /* Since history is fully modified in history_show(), panel->dir_history actually
3190 * points to the invalid place. Try restore current postition here. */
3192 size_t i;
3194 panel->dir_history_current = panel->dir_history;
3196 for (i = 0; i <= pos; i++)
3198 GList *prev;
3200 prev = g_list_previous (panel->dir_history_current);
3201 if (prev == NULL)
3202 break;
3204 panel->dir_history_current = prev;
3209 /* --------------------------------------------------------------------------------------------- */
3211 static cb_ret_t
3212 panel_execute_cmd (WPanel * panel, unsigned long command)
3214 int res = MSG_HANDLED;
3216 if (command != CK_Search)
3217 stop_search (panel);
3219 switch (command)
3221 case CK_Up:
3222 case CK_Down:
3223 case CK_Left:
3224 case CK_Right:
3225 case CK_Bottom:
3226 case CK_Top:
3227 case CK_PageDown:
3228 case CK_PageUp:
3229 /* reset state of marks flag */
3230 state_mark = -1;
3231 break;
3233 switch (command)
3235 case CK_PanelOtherCd:
3236 chdir_other_panel (panel);
3237 break;
3238 case CK_PanelOtherCdLink:
3239 chdir_to_readlink (panel);
3240 break;
3241 case CK_CopySingle:
3242 copy_cmd_local ();
3243 break;
3244 case CK_DeleteSingle:
3245 delete_cmd_local ();
3246 break;
3247 case CK_Enter:
3248 do_enter (panel);
3249 break;
3250 case CK_ViewRaw:
3251 view_raw_cmd ();
3252 break;
3253 case CK_EditNew:
3254 edit_cmd_new ();
3255 break;
3256 case CK_MoveSingle:
3257 rename_cmd_local ();
3258 break;
3259 case CK_SelectInvert:
3260 select_invert_cmd ();
3261 break;
3262 case CK_Select:
3263 select_cmd ();
3264 break;
3265 case CK_Unselect:
3266 unselect_cmd ();
3267 break;
3268 case CK_PageDown:
3269 next_page (panel);
3270 break;
3271 case CK_PageUp:
3272 prev_page (panel);
3273 break;
3274 case CK_CdChild:
3275 goto_child_dir (panel);
3276 break;
3277 case CK_CdParent:
3278 goto_parent_dir (panel);
3279 break;
3280 case CK_History:
3281 directory_history_list (panel);
3282 break;
3283 case CK_HistoryNext:
3284 directory_history_next (panel);
3285 break;
3286 case CK_HistoryPrev:
3287 directory_history_prev (panel);
3288 break;
3289 case CK_BottomOnScreen:
3290 goto_bottom_file (panel);
3291 break;
3292 case CK_MiddleOnScreen:
3293 goto_middle_file (panel);
3294 break;
3295 case CK_TopOnScreen:
3296 goto_top_file (panel);
3297 break;
3298 case CK_Mark:
3299 mark_file (panel);
3300 break;
3301 case CK_MarkUp:
3302 mark_file_up (panel);
3303 break;
3304 case CK_MarkDown:
3305 mark_file_down (panel);
3306 break;
3307 case CK_MarkLeft:
3308 mark_file_left (panel);
3309 break;
3310 case CK_MarkRight:
3311 mark_file_right (panel);
3312 break;
3313 case CK_CdParentSmart:
3314 res = force_maybe_cd ();
3315 break;
3316 case CK_Up:
3317 move_up (panel);
3318 break;
3319 case CK_Down:
3320 move_down (panel);
3321 break;
3322 case CK_Left:
3323 res = move_left (panel);
3324 break;
3325 case CK_Right:
3326 res = move_right (panel);
3327 break;
3328 case CK_Bottom:
3329 move_end (panel);
3330 break;
3331 case CK_Top:
3332 move_home (panel);
3333 break;
3334 #ifdef HAVE_CHARSET
3335 case CK_SelectCodepage:
3336 panel_change_encoding (panel);
3337 break;
3338 #endif
3339 case CK_ScrollLeft:
3340 panel_content_scroll_left (panel);
3341 break;
3342 case CK_ScrollRight:
3343 panel_content_scroll_right (panel);
3344 break;
3345 case CK_Search:
3346 start_search (panel);
3347 break;
3348 case CK_SearchStop:
3349 break;
3350 case CK_PanelOtherSync:
3351 panel_sync_other (panel);
3352 break;
3353 case CK_Sort:
3354 panel_select_sort_order (panel);
3355 break;
3356 case CK_SortPrev:
3357 panel_toggle_sort_order_prev (panel);
3358 break;
3359 case CK_SortNext:
3360 panel_toggle_sort_order_next (panel);
3361 break;
3362 case CK_SortReverse:
3363 panel->sort_info.reverse = !panel->sort_info.reverse;
3364 panel_set_sort_order (panel, panel->sort_field);
3365 break;
3366 case CK_SortByName:
3367 panel_set_sort_type_by_id (panel, "name");
3368 break;
3369 case CK_SortByExt:
3370 panel_set_sort_type_by_id (panel, "extension");
3371 break;
3372 case CK_SortBySize:
3373 panel_set_sort_type_by_id (panel, "size");
3374 break;
3375 case CK_SortByMTime:
3376 panel_set_sort_type_by_id (panel, "mtime");
3377 break;
3379 return res;
3382 /* --------------------------------------------------------------------------------------------- */
3384 static cb_ret_t
3385 panel_key (WPanel * panel, int key)
3387 size_t i;
3389 if (is_abort_char (key))
3391 stop_search (panel);
3392 return MSG_HANDLED;
3395 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3397 do_search (panel, key);
3398 return MSG_HANDLED;
3401 for (i = 0; panel_map[i].key != 0; i++)
3402 if (key == panel_map[i].key)
3403 return panel_execute_cmd (panel, panel_map[i].command);
3405 if (panels_options.torben_fj_mode && key == ALT ('h'))
3407 goto_middle_file (panel);
3408 return MSG_HANDLED;
3411 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3413 start_search (panel);
3414 do_search (panel, key);
3415 return MSG_HANDLED;
3418 return MSG_NOT_HANDLED;
3421 /* --------------------------------------------------------------------------------------------- */
3423 static cb_ret_t
3424 panel_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3426 WPanel *panel = PANEL (w);
3427 WButtonBar *bb;
3429 switch (msg)
3431 case MSG_INIT:
3432 /* subscribe to "history_load" event */
3433 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3434 /* subscribe to "history_save" event */
3435 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3436 return MSG_HANDLED;
3438 case MSG_DRAW:
3439 /* Repaint everything, including frame and separator */
3440 paint_frame (panel); /* including show_dir */
3441 paint_dir (panel);
3442 mini_info_separator (panel);
3443 display_mini_info (panel);
3444 panel->dirty = 0;
3445 return MSG_HANDLED;
3447 case MSG_FOCUS:
3448 state_mark = -1;
3449 current_panel = panel;
3450 panel->active = 1;
3452 if (mc_chdir (panel->cwd_vpath) != 0)
3454 char *cwd;
3456 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3457 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3458 cwd, unix_error_string (errno));
3459 g_free (cwd);
3461 else
3462 subshell_chdir (panel->cwd_vpath);
3464 update_xterm_title_path ();
3465 select_item (panel);
3466 show_dir (panel);
3467 paint_dir (panel);
3468 panel->dirty = 0;
3470 bb = find_buttonbar (w->owner);
3471 midnight_set_buttonbar (bb);
3472 widget_redraw (WIDGET (bb));
3473 return MSG_HANDLED;
3475 case MSG_UNFOCUS:
3476 /* Janne: look at this for the multiple panel options */
3477 stop_search (panel);
3478 panel->active = 0;
3479 show_dir (panel);
3480 unselect_item (panel);
3481 return MSG_HANDLED;
3483 case MSG_KEY:
3484 return panel_key (panel, parm);
3486 case MSG_ACTION:
3487 return panel_execute_cmd (panel, parm);
3489 case MSG_DESTROY:
3490 /* unsubscribe from "history_load" event */
3491 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3492 /* unsubscribe from "history_save" event */
3493 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3494 panel_destroy (panel);
3495 free_my_statfs ();
3496 return MSG_HANDLED;
3498 default:
3499 return widget_default_callback (w, sender, msg, parm, data);
3503 /* --------------------------------------------------------------------------------------------- */
3504 /* */
3505 /* Panel mouse events support routines */
3506 /* */
3508 static void
3509 mouse_toggle_mark (WPanel * panel)
3511 do_mark_file (panel, MARK_DONT_MOVE);
3512 mouse_marking = selection (panel)->f.marked;
3513 mouse_mark_panel = current_panel;
3516 /* --------------------------------------------------------------------------------------------- */
3518 static void
3519 mouse_set_mark (WPanel * panel)
3521 if (mouse_mark_panel == panel)
3523 if (mouse_marking && !(selection (panel)->f.marked))
3524 do_mark_file (panel, MARK_DONT_MOVE);
3525 else if (!mouse_marking && (selection (panel)->f.marked))
3526 do_mark_file (panel, MARK_DONT_MOVE);
3530 /* --------------------------------------------------------------------------------------------- */
3532 static gboolean
3533 mark_if_marking (WPanel * panel, Gpm_Event * event)
3535 if ((event->buttons & GPM_B_RIGHT) != 0)
3537 if ((event->type & GPM_DOWN) != 0)
3538 mouse_toggle_mark (panel);
3539 else
3540 mouse_set_mark (panel);
3541 return TRUE;
3544 return FALSE;
3547 /* --------------------------------------------------------------------------------------------- */
3548 /** Determine which column was clicked, and sort the panel on
3549 * that column, or reverse sort on that column if already
3550 * sorted on that column.
3553 static void
3554 mouse_sort_col (WPanel * panel, int x)
3556 int i;
3557 const char *lc_sort_name = NULL;
3558 panel_field_t *col_sort_format = NULL;
3559 format_e *format;
3561 for (i = 0, format = panel->format; format != NULL; format = format->next)
3563 i += format->field_len;
3564 if (x < i + 1)
3566 /* found column */
3567 lc_sort_name = format->title;
3568 break;
3572 if (lc_sort_name == NULL)
3573 return;
3575 for (i = 0; panel_fields[i].id != NULL; i++)
3577 char *title;
3579 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3580 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3582 col_sort_format = &panel_fields[i];
3583 g_free (title);
3584 break;
3586 g_free (title);
3589 if (col_sort_format == NULL)
3590 return;
3592 if (panel->sort_field == col_sort_format)
3594 /* reverse the sort if clicked column is already the sorted column */
3595 panel->sort_info.reverse = !panel->sort_info.reverse;
3597 else
3599 /* new sort is forced to be ascending */
3600 panel->sort_info.reverse = FALSE;
3602 panel_set_sort_order (panel, col_sort_format);
3606 /* --------------------------------------------------------------------------------------------- */
3608 * Mouse callback of the panel minus repainting.
3610 static int
3611 panel_event (Gpm_Event * event, void *data)
3613 WPanel *panel = PANEL (data);
3614 Widget *w = WIDGET (data);
3616 const int lines = llines (panel);
3617 const gboolean is_active = widget_is_active (panel);
3618 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3619 Gpm_Event local;
3621 if (!mouse_global_in_widget (event, WIDGET (data)))
3622 return MOU_UNHANDLED;
3624 local = mouse_get_local (event, w);
3626 /* 1st line */
3627 if (local.y == 1)
3629 /* "<" button */
3630 if (mouse_down && local.x == 2)
3632 directory_history_prev (panel);
3633 goto finish;
3636 /* ">" button */
3637 if (mouse_down && local.x == w->cols - 1)
3639 directory_history_next (panel);
3640 goto finish;
3643 /* "^" button */
3644 if (mouse_down && local.x >= w->cols - 4 && local.x <= w->cols - 2)
3646 directory_history_list (panel);
3647 goto finish;
3650 /* "." button show/hide hidden files */
3651 if (mouse_down && local.x == w->cols - 5)
3653 send_message (midnight_dlg, NULL, MSG_ACTION, CK_ShowHidden, NULL);
3654 goto finish;
3657 /* no other events on 1st line */
3658 return MOU_UNHANDLED;
3661 /* sort on clicked column; don't handle wheel events */
3662 if (mouse_down && (local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && local.y == 2)
3664 mouse_sort_col (panel, local.x);
3665 goto finish;
3668 /* Mouse wheel events */
3669 if (mouse_down && (local.buttons & GPM_B_UP) != 0)
3671 if (is_active)
3673 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3674 prev_page (panel);
3675 else /* We are in first page */
3676 move_up (panel);
3678 goto finish;
3681 if (mouse_down && (local.buttons & GPM_B_DOWN) != 0)
3683 if (is_active)
3685 if (panels_options.mouse_move_pages
3686 && (panel->top_file + ITEMS (panel) < panel->dir.len))
3687 next_page (panel);
3688 else /* We are in last page */
3689 move_down (panel);
3691 goto finish;
3694 local.y -= 2;
3695 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
3697 int my_index;
3699 if (!is_active)
3700 change_panel ();
3702 if (panel->top_file + local.y > panel->dir.len)
3703 my_index = panel->dir.len - 1;
3704 else
3706 my_index = panel->top_file + local.y - 1;
3707 if (panel->split && (local.x > (w->cols - 2) / 2))
3708 my_index += llines (panel);
3710 if (my_index >= panel->dir.len)
3711 my_index = panel->dir.len - 1;
3714 if (my_index != panel->selected)
3716 unselect_item (panel);
3717 panel->selected = my_index;
3718 select_item (panel);
3721 /* This one is new */
3722 mark_if_marking (panel, &local);
3724 else if ((local.type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE) &&
3725 local.y > 0 && local.y <= lines)
3726 do_enter (panel);
3728 finish:
3729 if (panel->dirty)
3730 widget_redraw (w);
3732 return MOU_NORMAL;
3735 /* --------------------------------------------------------------------------------------------- */
3737 static void
3738 reload_panelized (WPanel * panel)
3740 int i, j;
3741 dir_list *list = &panel->dir;
3743 if (panel != current_panel)
3744 (void) mc_chdir (panel->cwd_vpath);
3746 for (i = 0, j = 0; i < panel->dir.len; i++)
3748 vfs_path_t *vpath;
3750 if (list->list[i].f.marked)
3752 /* Unmark the file in advance. In case the following mc_lstat
3753 * fails we are done, else we have to mark the file again
3754 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3755 * IMO that's the best way to update the panel's summary status
3756 * -- Norbert
3758 do_file_mark (panel, i, 0);
3760 vpath = vfs_path_from_str (list->list[i].fname);
3761 if (mc_lstat (vpath, &list->list[i].st) != 0)
3762 g_free (list->list[i].fname);
3763 else
3765 if (list->list[i].f.marked)
3766 do_file_mark (panel, i, 1);
3767 if (j != i)
3768 list->list[j] = list->list[i];
3769 j++;
3771 vfs_path_free (vpath);
3773 if (j == 0)
3774 dir_list_init (list);
3775 else
3776 list->len = j;
3778 if (panel != current_panel)
3779 (void) mc_chdir (current_panel->cwd_vpath);
3782 /* --------------------------------------------------------------------------------------------- */
3784 static void
3785 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3787 gboolean free_pointer;
3788 char *my_current_file = NULL;
3790 if ((flags & UP_RELOAD) != 0)
3792 panel->is_panelized = FALSE;
3793 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3794 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3797 /* If current_file == -1 (an invalid pointer) then preserve selection */
3798 free_pointer = current_file == UP_KEEPSEL;
3800 if (free_pointer)
3802 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3803 current_file = my_current_file;
3806 if (panel->is_panelized)
3807 reload_panelized (panel);
3808 else
3809 panel_reload (panel);
3811 try_to_select (panel, current_file);
3812 panel->dirty = 1;
3814 if (free_pointer)
3815 g_free (my_current_file);
3818 /* --------------------------------------------------------------------------------------------- */
3820 static void
3821 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3823 if (get_display_type (which) == view_listing)
3825 WPanel *panel;
3827 panel = PANEL (get_panel_widget (which));
3828 if (panel->is_panelized)
3829 flags &= ~UP_RELOAD;
3830 update_one_panel_widget (panel, flags, current_file);
3834 /* --------------------------------------------------------------------------------------------- */
3836 static void
3837 do_select (WPanel * panel, int i)
3839 if (i != panel->selected)
3841 panel->dirty = 1;
3842 panel->selected = i;
3843 panel->top_file = panel->selected - (WIDGET (panel)->lines - 2) / 2;
3844 if (panel->top_file < 0)
3845 panel->top_file = 0;
3849 /* --------------------------------------------------------------------------------------------- */
3851 static void
3852 do_try_to_select (WPanel * panel, const char *name)
3854 int i;
3855 char *subdir;
3857 if (!name)
3859 do_select (panel, 0);
3860 return;
3863 /* We only want the last component of the directory,
3864 * and from this only the name without suffix.
3865 * Cut prefix if the panel is not panelized */
3867 if (panel->is_panelized)
3868 subdir = vfs_strip_suffix_from_filename (name);
3869 else
3870 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3872 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3873 for (i = 0; i < panel->dir.len; i++)
3875 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3877 do_select (panel, i);
3878 g_free (subdir);
3879 return;
3883 /* Try to select a file near the file that is missing */
3884 if (panel->selected >= panel->dir.len)
3885 do_select (panel, panel->dir.len - 1);
3886 g_free (subdir);
3889 /* --------------------------------------------------------------------------------------------- */
3891 /* event callback */
3892 static gboolean
3893 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3894 gpointer init_data, gpointer data)
3896 (void) event_group_name;
3897 (void) event_name;
3898 (void) init_data;
3899 (void) data;
3901 update_panels (UP_RELOAD, UP_KEEPSEL);
3903 return TRUE;
3906 /* --------------------------------------------------------------------------------------------- */
3908 /* event callback */
3909 static gboolean
3910 panel_save_current_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3911 gpointer init_data, gpointer data)
3913 (void) event_group_name;
3914 (void) event_name;
3915 (void) init_data;
3916 (void) data;
3918 if (current_panel->marked == 0)
3919 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3920 (gpointer) selection (current_panel)->fname);
3921 else
3923 int i;
3924 gboolean first = TRUE;
3925 char *flist = NULL;
3927 for (i = 0; i < current_panel->dir.len; i++)
3928 if (current_panel->dir.list[i].f.marked != 0)
3929 { /* Skip the unmarked ones */
3930 if (first)
3932 flist = g_strdup (current_panel->dir.list[i].fname);
3933 first = FALSE;
3935 else
3937 /* Add empty lines after the file */
3938 char *tmp;
3940 tmp =
3941 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3942 g_free (flist);
3943 flist = tmp;
3947 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3948 g_free (flist);
3950 return TRUE;
3953 /* --------------------------------------------------------------------------------------------- */
3955 static vfs_path_t *
3956 panel_recursive_cd_to_parent (const vfs_path_t * vpath)
3958 vfs_path_t *cwd_vpath;
3960 cwd_vpath = vfs_path_clone (vpath);
3962 while (mc_chdir (cwd_vpath) < 0)
3964 const char *panel_cwd_path;
3965 vfs_path_t *tmp_vpath;
3967 /* check if path contains only '/' */
3968 panel_cwd_path = vfs_path_as_str (cwd_vpath);
3969 if (panel_cwd_path[0] == PATH_SEP && panel_cwd_path[1] == '\0')
3970 return NULL;
3972 tmp_vpath = vfs_path_vtokens_get (cwd_vpath, 0, -1);
3973 vfs_path_free (cwd_vpath);
3974 cwd_vpath = vfs_path_build_filename (PATH_SEP_STR, vfs_path_as_str (tmp_vpath), NULL);
3975 vfs_path_free (tmp_vpath);
3978 return cwd_vpath;
3981 /* --------------------------------------------------------------------------------------------- */
3982 /*** public functions ****************************************************************************/
3983 /* --------------------------------------------------------------------------------------------- */
3985 void
3986 try_to_select (WPanel * panel, const char *name)
3988 do_try_to_select (panel, name);
3989 select_item (panel);
3992 /* --------------------------------------------------------------------------------------------- */
3994 void
3995 panel_clean_dir (WPanel * panel)
3997 panel->top_file = 0;
3998 panel->selected = 0;
3999 panel->marked = 0;
4000 panel->dirs_marked = 0;
4001 panel->total = 0;
4002 panel->searching = FALSE;
4003 panel->is_panelized = FALSE;
4004 panel->dirty = 1;
4005 panel->content_shift = -1;
4006 panel->max_shift = -1;
4008 dir_list_clean (&panel->dir);
4011 /* --------------------------------------------------------------------------------------------- */
4013 * Set Up panel's current dir object
4015 * @param panel panel object
4016 * @param path_str string contain path
4019 void
4020 panel_set_cwd (WPanel * panel, const vfs_path_t * vpath)
4022 if (vpath != panel->cwd_vpath) /* check if new vpath is not the panel->cwd_vpath object */
4024 vfs_path_free (panel->cwd_vpath);
4025 panel->cwd_vpath = vfs_path_clone (vpath);
4029 /* --------------------------------------------------------------------------------------------- */
4031 * Set Up panel's last working dir object
4033 * @param panel panel object
4034 * @param path_str string contain path
4037 void
4038 panel_set_lwd (WPanel * panel, const vfs_path_t * vpath)
4040 if (vpath != panel->lwd_vpath) /* check if new vpath is not the panel->lwd_vpath object */
4042 vfs_path_free (panel->lwd_vpath);
4043 panel->lwd_vpath = vfs_path_clone (vpath);
4047 /* --------------------------------------------------------------------------------------------- */
4049 * Panel creation for specified directory.
4051 * @param panel_name specifies the name of the panel for setup retieving
4052 * @param wpath the path of working panel directory. If path is NULL then panel will be created
4053 * for current directory
4055 * @return new instance of WPanel
4058 WPanel *
4059 panel_new_with_dir (const char *panel_name, const vfs_path_t * vpath)
4061 WPanel *panel;
4062 Widget *w;
4063 char *section;
4064 int i, err;
4065 char *curdir = NULL;
4067 panel = g_new0 (WPanel, 1);
4068 w = WIDGET (panel);
4069 /* No know sizes of the panel at startup */
4070 widget_init (w, 0, 0, 0, 0, panel_callback, panel_event);
4071 /* We do not want the cursor */
4072 widget_want_cursor (w, FALSE);
4074 if (vpath != NULL)
4076 curdir = _vfs_get_cwd ();
4077 panel_set_cwd (panel, vpath);
4079 else
4081 vfs_setup_cwd ();
4082 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4085 panel_set_lwd (panel, vfs_get_raw_current_dir ());
4087 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4088 /* directories history will be get later */
4090 panel->dir.size = DIR_LIST_MIN_SIZE;
4091 panel->dir.list = g_new (file_entry_t, panel->dir.size);
4092 panel->dir.len = 0;
4093 panel->active = 0;
4094 panel->filter = 0;
4095 panel->split = 0;
4096 panel->top_file = 0;
4097 panel->selected = 0;
4098 panel->marked = 0;
4099 panel->total = 0;
4100 panel->dirty = 1;
4101 panel->searching = FALSE;
4102 panel->dirs_marked = 0;
4103 panel->is_panelized = FALSE;
4104 panel->format = 0;
4105 panel->status_format = 0;
4106 panel->format_modified = 1;
4107 panel->content_shift = -1;
4108 panel->max_shift = -1;
4110 panel->panel_name = g_strdup (panel_name);
4111 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4113 #ifdef HAVE_CHARSET
4114 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4115 #endif
4117 for (i = 0; i < LIST_TYPES; i++)
4118 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4120 panel->search_buffer[0] = '\0';
4121 panel->prev_search_buffer[0] = '\0';
4122 panel->frame_size = frame_half;
4124 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4125 if (!mc_config_has_group (mc_main_config, section))
4127 g_free (section);
4128 section = g_strdup (panel->panel_name);
4130 panel_load_setup (panel, section);
4131 g_free (section);
4133 /* Load format strings */
4134 err = set_panel_formats (panel);
4135 if (err != 0)
4136 set_panel_formats (panel);
4138 #ifdef HAVE_CHARSET
4140 const vfs_path_element_t *path_element;
4142 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4143 if (path_element->encoding != NULL)
4144 panel->codepage = get_codepage_index (path_element->encoding);
4146 #endif
4148 if (mc_chdir (panel->cwd_vpath) != 0)
4150 #ifdef HAVE_CHARSET
4151 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4152 #endif
4153 vfs_setup_cwd ();
4154 vfs_path_free (panel->cwd_vpath);
4155 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4158 /* Load the default format */
4159 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4160 &panel->sort_info, panel->filter);
4162 /* Restore old right path */
4163 if (curdir != NULL)
4165 vfs_path_t *tmp_vpath;
4167 tmp_vpath = vfs_path_from_str (curdir);
4168 err = mc_chdir (tmp_vpath);
4169 vfs_path_free (tmp_vpath);
4171 g_free (curdir);
4173 return panel;
4176 /* --------------------------------------------------------------------------------------------- */
4178 void
4179 panel_reload (WPanel * panel)
4181 struct stat current_stat;
4182 vfs_path_t *cwd_vpath;
4184 if (panels_options.fast_reload && stat (vfs_path_as_str (panel->cwd_vpath), &current_stat) == 0
4185 && current_stat.st_ctime == panel->dir_stat.st_ctime
4186 && current_stat.st_mtime == panel->dir_stat.st_mtime)
4187 return;
4189 cwd_vpath = panel_recursive_cd_to_parent (panel->cwd_vpath);
4190 vfs_path_free (panel->cwd_vpath);
4192 if (cwd_vpath == NULL)
4194 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4195 panel_clean_dir (panel);
4196 dir_list_init (&panel->dir);
4197 return;
4200 panel->cwd_vpath = cwd_vpath;
4201 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4202 show_dir (panel);
4204 dir_list_reload (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4205 &panel->sort_info, panel->filter);
4207 panel->dirty = 1;
4208 if (panel->selected >= panel->dir.len)
4209 do_select (panel, panel->dir.len - 1);
4211 recalculate_panel_summary (panel);
4214 /* --------------------------------------------------------------------------------------------- */
4215 /* Switches the panel to the mode specified in the format */
4216 /* Seting up both format and status string. Return: 0 - on success; */
4217 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4220 set_panel_formats (WPanel * p)
4222 format_e *form;
4223 char *err = NULL;
4224 int retcode = 0;
4226 form = use_display_format (p, panel_format (p), &err, FALSE);
4228 if (err != NULL)
4230 g_free (err);
4231 retcode = 1;
4233 else
4235 delete_format (p->format);
4236 p->format = form;
4239 if (panels_options.show_mini_info)
4241 form = use_display_format (p, mini_status_format (p), &err, TRUE);
4243 if (err != NULL)
4245 g_free (err);
4246 retcode += 2;
4248 else
4250 delete_format (p->status_format);
4251 p->status_format = form;
4255 panel_format_modified (p);
4256 panel_update_cols (WIDGET (p), p->frame_size);
4258 if (retcode)
4259 message (D_ERROR, _("Warning"),
4260 _("User supplied format looks invalid, reverting to default."));
4261 if (retcode & 0x01)
4263 g_free (p->user_format);
4264 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4266 if (retcode & 0x02)
4268 g_free (p->user_status_format[p->list_type]);
4269 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4272 return retcode;
4275 /* --------------------------------------------------------------------------------------------- */
4277 void
4278 panel_update_cols (Widget * widget, panel_display_t frame_size)
4280 int cols, origin;
4282 /* don't touch panel if it is not in dialog yet */
4283 /* if panel is not in dialog it is not in widgets list
4284 and cannot be compared with get_panel_widget() result */
4285 if (widget->owner == NULL)
4286 return;
4288 if (panels_layout.horizontal_split)
4290 widget->cols = COLS;
4291 return;
4294 if (frame_size == frame_full)
4296 cols = COLS;
4297 origin = 0;
4299 else if (widget == get_panel_widget (0))
4301 cols = panels_layout.left_panel_size;
4302 origin = 0;
4304 else
4306 cols = COLS - panels_layout.left_panel_size;
4307 origin = panels_layout.left_panel_size;
4310 widget->cols = cols;
4311 widget->x = origin;
4314 /* --------------------------------------------------------------------------------------------- */
4316 /* Select current item and readjust the panel */
4317 void
4318 select_item (WPanel * panel)
4320 /* Although currently all over the code we set the selection and
4321 top file to decent values before calling select_item, I could
4322 forget it someday, so it's better to do the actual fitting here */
4324 if (panel->selected < 0)
4325 panel->selected = 0;
4327 if (panel->selected > panel->dir.len - 1)
4328 panel->selected = panel->dir.len - 1;
4330 adjust_top_file (panel);
4332 panel->dirty = 1;
4334 execute_hooks (select_file_hook);
4337 /* --------------------------------------------------------------------------------------------- */
4338 /** Clears all files in the panel, used only when one file was marked */
4339 void
4340 unmark_files (WPanel * panel)
4342 int i;
4344 if (!panel->marked)
4345 return;
4347 for (i = 0; i < panel->dir.len; i++)
4348 file_mark (panel, i, 0);
4350 panel->dirs_marked = 0;
4351 panel->marked = 0;
4352 panel->total = 0;
4355 /* --------------------------------------------------------------------------------------------- */
4356 /** Recalculate the panels summary information, used e.g. when marked
4357 files might have been removed by an external command */
4359 void
4360 recalculate_panel_summary (WPanel * panel)
4362 int i;
4364 panel->marked = 0;
4365 panel->dirs_marked = 0;
4366 panel->total = 0;
4368 for (i = 0; i < panel->dir.len; i++)
4369 if (panel->dir.list[i].f.marked)
4371 /* do_file_mark will return immediately if newmark == oldmark.
4372 So we have to first unmark it to get panel's summary information
4373 updated. (Norbert) */
4374 panel->dir.list[i].f.marked = 0;
4375 do_file_mark (panel, i, 1);
4379 /* --------------------------------------------------------------------------------------------- */
4380 /** This routine marks a file or a directory */
4382 void
4383 do_file_mark (WPanel * panel, int idx, int mark)
4385 if (panel->dir.list[idx].f.marked == mark)
4386 return;
4388 /* Only '..' can't be marked, '.' isn't visible */
4389 if (DIR_IS_DOTDOT (panel->dir.list[idx].fname))
4390 return;
4392 file_mark (panel, idx, mark);
4393 if (panel->dir.list[idx].f.marked)
4395 panel->marked++;
4397 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4399 if (panel->dir.list[idx].f.dir_size_computed)
4400 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4401 panel->dirs_marked++;
4403 else
4404 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4406 set_colors (panel);
4408 else
4410 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4412 if (panel->dir.list[idx].f.dir_size_computed)
4413 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4414 panel->dirs_marked--;
4416 else
4417 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4419 panel->marked--;
4423 /* --------------------------------------------------------------------------------------------- */
4425 * Changes the current directory of the panel.
4426 * Record change in the directory history.
4428 gboolean
4429 do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4431 gboolean r;
4433 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4434 if (r)
4435 directory_history_add (panel, panel->cwd_vpath);
4436 return r;
4439 /* --------------------------------------------------------------------------------------------- */
4441 void
4442 file_mark (WPanel * panel, int lc_index, int val)
4444 if (panel->dir.list[lc_index].f.marked != val)
4446 panel->dir.list[lc_index].f.marked = val;
4447 panel->dirty = 1;
4451 /* --------------------------------------------------------------------------------------------- */
4453 void
4454 panel_re_sort (WPanel * panel)
4456 char *filename;
4457 int i;
4459 if (panel == NULL)
4460 return;
4462 filename = g_strdup (selection (panel)->fname);
4463 unselect_item (panel);
4464 dir_list_sort (&panel->dir, panel->sort_field->sort_routine, &panel->sort_info);
4465 panel->selected = -1;
4467 for (i = panel->dir.len; i != 0; i--)
4468 if (strcmp (panel->dir.list[i - 1].fname, filename) == 0)
4470 panel->selected = i - 1;
4471 break;
4474 g_free (filename);
4475 panel->top_file = panel->selected - ITEMS (panel) / 2;
4476 select_item (panel);
4477 panel->dirty = 1;
4480 /* --------------------------------------------------------------------------------------------- */
4482 void
4483 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4485 if (sort_order == NULL)
4486 return;
4488 panel->sort_field = sort_order;
4490 /* The directory is already sorted, we have to load the unsorted stuff */
4491 if (sort_order->sort_routine == (GCompareFunc) unsorted)
4493 char *current_file;
4495 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4496 panel_reload (panel);
4497 try_to_select (panel, current_file);
4498 g_free (current_file);
4500 panel_re_sort (panel);
4503 /* --------------------------------------------------------------------------------------------- */
4505 #ifdef HAVE_CHARSET
4508 * Change panel encoding.
4509 * @param panel WPanel object
4512 void
4513 panel_change_encoding (WPanel * panel)
4515 const char *encoding = NULL;
4516 char *errmsg;
4517 int r;
4519 r = select_charset (-1, -1, panel->codepage, FALSE);
4521 if (r == SELECT_CHARSET_CANCEL)
4522 return; /* Cancel */
4524 panel->codepage = r;
4526 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4528 /* No translation */
4529 vfs_path_t *cd_path_vpath;
4531 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4532 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4533 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4534 show_dir (panel);
4535 vfs_path_free (cd_path_vpath);
4536 return;
4539 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4540 if (errmsg != NULL)
4542 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4543 g_free (errmsg);
4544 return;
4547 encoding = get_codepage_id (panel->codepage);
4548 if (encoding != NULL)
4550 vfs_path_change_encoding (panel->cwd_vpath, encoding);
4552 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4553 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""),
4554 vfs_path_as_str (panel->cwd_vpath));
4558 /* --------------------------------------------------------------------------------------------- */
4561 * Remove encode info from last path element.
4564 vfs_path_t *
4565 remove_encoding_from_path (const vfs_path_t * vpath)
4567 vfs_path_t *ret_vpath;
4568 GString *tmp_conv;
4569 int indx;
4571 ret_vpath = vfs_path_new ();
4573 tmp_conv = g_string_new ("");
4575 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4577 GIConv converter;
4578 vfs_path_element_t *path_element;
4580 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4582 if (path_element->encoding == NULL)
4584 vfs_path_add_element (ret_vpath, path_element);
4585 continue;
4588 converter = str_crt_conv_to (path_element->encoding);
4589 if (converter == INVALID_CONV)
4591 vfs_path_add_element (ret_vpath, path_element);
4592 continue;
4595 g_free (path_element->encoding);
4596 path_element->encoding = NULL;
4598 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4600 g_free (path_element->path);
4601 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4603 g_string_set_size (tmp_conv, 0);
4605 str_close_conv (converter);
4606 str_close_conv (path_element->dir.converter);
4607 path_element->dir.converter = INVALID_CONV;
4608 vfs_path_add_element (ret_vpath, path_element);
4610 g_string_free (tmp_conv, TRUE);
4611 return ret_vpath;
4613 #endif /* HAVE_CHARSET */
4615 /* --------------------------------------------------------------------------------------------- */
4618 * This routine reloads the directory in both panels. It tries to
4619 * select current_file in current_panel and other_file in other_panel.
4620 * If current_file == -1 then it automatically sets current_file and
4621 * other_file to the currently selected files in the panels.
4623 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4624 * will not reload the other panel.
4626 * @param flags for reload panel
4627 * @param current_file name of the current file
4630 void
4631 update_panels (panel_update_flags_t flags, const char *current_file)
4633 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4634 WPanel *panel;
4636 update_one_panel (get_current_index (), flags, current_file);
4637 if (reload_other)
4638 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4640 if (get_current_type () == view_listing)
4641 panel = PANEL (get_panel_widget (get_current_index ()));
4642 else
4643 panel = PANEL (get_panel_widget (get_other_index ()));
4645 if (!panel->is_panelized)
4646 (void) mc_chdir (panel->cwd_vpath);
4649 /* --------------------------------------------------------------------------------------------- */
4651 gsize
4652 panel_get_num_of_sortable_fields (void)
4654 gsize ret = 0, lc_index;
4656 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4657 if (panel_fields[lc_index].is_user_choice)
4658 ret++;
4659 return ret;
4662 /* --------------------------------------------------------------------------------------------- */
4664 const char **
4665 panel_get_sortable_fields (gsize * array_size)
4667 char **ret;
4668 gsize lc_index, i;
4670 lc_index = panel_get_num_of_sortable_fields ();
4672 ret = g_try_new0 (char *, lc_index + 1);
4673 if (ret == NULL)
4674 return NULL;
4676 if (array_size != NULL)
4677 *array_size = lc_index;
4679 lc_index = 0;
4681 for (i = 0; panel_fields[i].id != NULL; i++)
4682 if (panel_fields[i].is_user_choice)
4683 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4685 return (const char **) ret;
4688 /* --------------------------------------------------------------------------------------------- */
4690 const panel_field_t *
4691 panel_get_field_by_id (const char *name)
4693 gsize lc_index;
4695 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4696 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4697 return &panel_fields[lc_index];
4699 return NULL;
4702 /* --------------------------------------------------------------------------------------------- */
4704 const panel_field_t *
4705 panel_get_field_by_title_hotkey (const char *name)
4707 gsize lc_index;
4709 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4710 if (panel_fields[lc_index].title_hotkey != NULL &&
4711 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4712 return &panel_fields[lc_index];
4714 return NULL;
4717 /* --------------------------------------------------------------------------------------------- */
4719 const panel_field_t *
4720 panel_get_field_by_title (const char *name)
4722 gsize lc_index;
4724 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4726 gchar *title;
4727 gboolean ok;
4729 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4730 ok = title != NULL && strcmp (name, title) == 0;
4731 g_free (title);
4733 if (ok)
4734 return &panel_fields[lc_index];
4737 return NULL;
4740 /* --------------------------------------------------------------------------------------------- */
4742 gsize
4743 panel_get_num_of_user_possible_fields (void)
4745 gsize ret = 0, lc_index;
4747 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4748 if (panel_fields[lc_index].use_in_user_format)
4749 ret++;
4751 return ret;
4754 /* --------------------------------------------------------------------------------------------- */
4756 const char **
4757 panel_get_user_possible_fields (gsize * array_size)
4759 char **ret;
4760 gsize lc_index, i;
4762 lc_index = panel_get_num_of_user_possible_fields ();
4764 ret = g_try_new0 (char *, lc_index + 1);
4765 if (ret == NULL)
4766 return NULL;
4768 if (array_size != NULL)
4769 *array_size = lc_index;
4771 lc_index = 0;
4773 for (i = 0; panel_fields[i].id != NULL; i++)
4774 if (panel_fields[i].use_in_user_format)
4775 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4777 return (const char **) ret;
4780 /* --------------------------------------------------------------------------------------------- */
4782 void
4783 panel_init (void)
4785 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4786 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ".");
4788 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4789 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4790 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4791 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4792 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4793 panel_filename_scroll_left_char =
4794 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4795 panel_filename_scroll_right_char =
4796 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4798 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4799 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_current_file_to_clip_file",
4800 panel_save_current_file_to_clip_file, NULL, NULL);
4803 /* --------------------------------------------------------------------------------------------- */
4805 void
4806 panel_deinit (void)
4808 g_free (panel_sort_up_sign);
4809 g_free (panel_sort_down_sign);
4811 g_free (panel_hiddenfiles_sign_show);
4812 g_free (panel_hiddenfiles_sign_hide);
4813 g_free (panel_history_prev_item_sign);
4814 g_free (panel_history_next_item_sign);
4815 g_free (panel_history_show_list_sign);
4816 g_free (panel_filename_scroll_left_char);
4817 g_free (panel_filename_scroll_right_char);
4820 /* --------------------------------------------------------------------------------------------- */
4822 gboolean
4823 do_cd (const vfs_path_t * new_dir_vpath, enum cd_enum exact)
4825 gboolean res;
4826 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
4828 if (current_panel->is_panelized)
4830 size_t new_vpath_len;
4832 new_vpath_len = vfs_path_len (new_dir_vpath);
4833 if (vfs_path_equal_len (new_dir_vpath, panelized_panel.root_vpath, new_vpath_len))
4834 _new_dir_vpath = panelized_panel.root_vpath;
4837 res = do_panel_cd (current_panel, _new_dir_vpath, exact);
4839 #ifdef HAVE_CHARSET
4840 if (res)
4842 const vfs_path_element_t *path_element;
4844 path_element = vfs_path_get_by_index (current_panel->cwd_vpath, -1);
4845 if (path_element->encoding != NULL)
4846 current_panel->codepage = get_codepage_index (path_element->encoding);
4847 else
4848 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4850 #endif /* HAVE_CHARSET */
4852 return res;
4855 /* --------------------------------------------------------------------------------------------- */