cppcheck: reduce variable scope.
[midnight-commander.git] / src / filemanager / panel.c
blobbac486ec0c9b8089577d3e6aac519b34d29c2218
1 /*
2 Panel managing.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2009, 2011, 2012, 2013
6 The Free Software Foundation, Inc.
8 Written by:
9 Miguel de Icaza, 1995
10 Timur Bakeyev, 1997, 1999
11 Slava Zanko <slavazanko@gmail.com>, 2013
12 Andrew Borodin <aborodin@vmail.ru>, 2013
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 /** \file panel.c
31 * \brief Source: panel managin module
34 #include <config.h>
36 #include <errno.h>
37 #include <stdio.h>
38 #include <stdlib.h>
39 #include <string.h>
40 #include <unistd.h>
42 #include "lib/global.h"
44 #include "lib/tty/tty.h"
45 #include "lib/tty/mouse.h" /* For Gpm_Event */
46 #include "lib/tty/key.h" /* XCTRL and ALT macros */
47 #include "lib/skin.h"
48 #include "lib/strescape.h"
49 #include "lib/mcconfig.h"
50 #include "lib/vfs/vfs.h"
51 #include "lib/unixcompat.h"
52 #include "lib/timefmt.h" /* file_date() */
53 #include "lib/util.h"
54 #include "lib/widget.h"
55 #ifdef HAVE_CHARSET
56 #include "lib/charsets.h" /* get_codepage_id () */
57 #endif
58 #include "lib/event.h"
60 #include "src/setup.h" /* For loading/saving panel options */
61 #include "src/execute.h"
62 #ifdef HAVE_CHARSET
63 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
64 #endif
65 #include "src/keybind-defaults.h" /* global_keymap_t */
66 #ifdef ENABLE_SUBSHELL
67 #include "src/subshell.h" /* do_subshell_chdir() */
68 #endif
70 #include "dir.h"
71 #include "boxes.h"
72 #include "tree.h"
73 #include "ext.h" /* regexp_command */
74 #include "layout.h" /* Most layout variables are here */
75 #include "cmd.h"
76 #include "command.h" /* cmdline */
77 #include "usermenu.h"
78 #include "midnight.h"
79 #include "mountlist.h" /* my_statfs */
81 #include "panel.h"
83 /*** global variables ****************************************************************************/
85 /* The hook list for the select file function */
86 hook_t *select_file_hook = NULL;
88 /* *INDENT-OFF* */
89 panelized_panel_t panelized_panel = { {NULL, 0, -1}, NULL };
90 /* *INDENT-ON* */
92 static const char *string_file_name (file_entry_t *, int);
93 static const char *string_file_size (file_entry_t *, int);
94 static const char *string_file_size_brief (file_entry_t *, int);
95 static const char *string_file_type (file_entry_t *, int);
96 static const char *string_file_mtime (file_entry_t *, int);
97 static const char *string_file_atime (file_entry_t *, int);
98 static const char *string_file_ctime (file_entry_t *, int);
99 static const char *string_file_permission (file_entry_t *, int);
100 static const char *string_file_perm_octal (file_entry_t *, int);
101 static const char *string_file_nlinks (file_entry_t *, int);
102 static const char *string_inode (file_entry_t *, int);
103 static const char *string_file_nuid (file_entry_t *, int);
104 static const char *string_file_ngid (file_entry_t *, int);
105 static const char *string_file_owner (file_entry_t *, int);
106 static const char *string_file_group (file_entry_t *, int);
107 static const char *string_marked (file_entry_t *, int);
108 static const char *string_space (file_entry_t *, int);
109 static const char *string_dot (file_entry_t *, int);
111 /* *INDENT-OFF* */
112 panel_field_t panel_fields[] = {
114 "unsorted", 12, 1, J_LEFT_FIT,
115 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
116 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
117 N_("sort|u"),
118 N_("&Unsorted"), TRUE, FALSE,
119 string_file_name,
120 (GCompareFunc) unsorted
124 "name", 12, 1, J_LEFT_FIT,
125 /* TRANSLATORS: one single character to represent 'name' sort mode */
126 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
127 N_("sort|n"),
128 N_("&Name"), TRUE, TRUE,
129 string_file_name,
130 (GCompareFunc) sort_name
134 "version", 12, 1, J_LEFT_FIT,
135 /* TRANSLATORS: one single character to represent 'version' sort mode */
136 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
137 N_("sort|v"),
138 N_("&Version"), TRUE, FALSE,
139 string_file_name,
140 (GCompareFunc) sort_vers
144 "extension", 12, 1, J_LEFT_FIT,
145 /* TRANSLATORS: one single character to represent 'extension' sort mode */
146 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
147 N_("sort|e"),
148 N_("E&xtension"), TRUE, FALSE,
149 string_file_name, /* TODO: string_file_ext */
150 (GCompareFunc) sort_ext
154 "size", 7, 0, J_RIGHT,
155 /* TRANSLATORS: one single character to represent 'size' sort mode */
156 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
157 N_("sort|s"),
158 N_("&Size"), TRUE, TRUE,
159 string_file_size,
160 (GCompareFunc) sort_size
164 "bsize", 7, 0, J_RIGHT,
166 N_("Block Size"), FALSE, FALSE,
167 string_file_size_brief,
168 (GCompareFunc) sort_size
172 "type", 1, 0, J_LEFT,
174 "", FALSE, TRUE,
175 string_file_type,
176 NULL
180 "mtime", 12, 0, J_RIGHT,
181 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
182 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
183 N_("sort|m"),
184 N_("&Modify time"), TRUE, TRUE,
185 string_file_mtime,
186 (GCompareFunc) sort_time
190 "atime", 12, 0, J_RIGHT,
191 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
192 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
193 N_("sort|a"),
194 N_("&Access time"), TRUE, TRUE,
195 string_file_atime,
196 (GCompareFunc) sort_atime
200 "ctime", 12, 0, J_RIGHT,
201 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
202 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
203 N_("sort|h"),
204 N_("C&hange time"), TRUE, TRUE,
205 string_file_ctime,
206 (GCompareFunc) sort_ctime
210 "perm", 10, 0, J_LEFT,
212 N_("Permission"), FALSE, TRUE,
213 string_file_permission,
214 NULL
218 "mode", 6, 0, J_RIGHT,
220 N_("Perm"), FALSE, TRUE,
221 string_file_perm_octal,
222 NULL
226 "nlink", 2, 0, J_RIGHT,
228 N_("Nl"), FALSE, TRUE,
229 string_file_nlinks, NULL
233 "inode", 5, 0, J_RIGHT,
234 /* TRANSLATORS: one single character to represent 'inode' sort mode */
235 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
236 N_("sort|i"),
237 N_("&Inode"), TRUE, TRUE,
238 string_inode,
239 (GCompareFunc) sort_inode
243 "nuid", 5, 0, J_RIGHT,
245 N_("UID"), FALSE, FALSE,
246 string_file_nuid,
247 NULL
251 "ngid", 5, 0, J_RIGHT,
253 N_("GID"), FALSE, FALSE,
254 string_file_ngid,
255 NULL
259 "owner", 8, 0, J_LEFT_FIT,
261 N_("Owner"), FALSE, TRUE,
262 string_file_owner,
263 NULL
267 "group", 8, 0, J_LEFT_FIT,
269 N_("Group"), FALSE, TRUE,
270 string_file_group,
271 NULL
275 "mark", 1, 0, J_RIGHT,
277 " ", FALSE, TRUE,
278 string_marked,
279 NULL
283 "|", 1, 0, J_RIGHT,
285 " ", FALSE, TRUE,
286 NULL,
287 NULL
291 "space", 1, 0, J_RIGHT,
293 " ", FALSE, TRUE,
294 string_space,
295 NULL
299 "dot", 1, 0, J_RIGHT,
301 " ", FALSE, FALSE,
302 string_dot,
303 NULL
307 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
310 /* *INDENT-ON* */
312 mc_fhl_t *mc_filehighlight = NULL;
314 extern int saving_setup;
316 /*** file scope macro definitions ****************************************************************/
318 #define NORMAL 0
319 #define SELECTED 1
320 #define MARKED 2
321 #define MARKED_SELECTED 3
322 #define STATUS 5
324 /* This macro extracts the number of available lines in a panel */
325 #define llines(p) (WIDGET (p)->lines - 3 - (panels_options.show_mini_info ? 2 : 0))
327 /*** file scope type declarations ****************************************************************/
329 typedef enum
331 MARK_DONT_MOVE = 0,
332 MARK_DOWN = 1,
333 MARK_FORCE_DOWN = 2,
334 MARK_FORCE_UP = 3
335 } mark_act_t;
338 * This describes a format item. The parse_display_format routine parses
339 * the user specified format and creates a linked list of format_e structures.
341 typedef struct format_e
343 struct format_e *next;
344 int requested_field_len;
345 int field_len;
346 align_crt_t just_mode;
347 int expand;
348 const char *(*string_fn) (file_entry_t *, int len);
349 char *title;
350 const char *id;
351 } format_e;
353 /* File name scroll states */
354 typedef enum
356 FILENAME_NOSCROLL = 1,
357 FILENAME_SCROLL_LEFT = 2,
358 FILENAME_SCROLL_RIGHT = 4
359 } filename_scroll_flag_t;
361 /*** file scope variables ************************************************************************/
363 static char *panel_sort_up_sign = NULL;
364 static char *panel_sort_down_sign = NULL;
366 static char *panel_hiddenfiles_sign_show = NULL;
367 static char *panel_hiddenfiles_sign_hide = NULL;
368 static char *panel_history_prev_item_sign = NULL;
369 static char *panel_history_next_item_sign = NULL;
370 static char *panel_history_show_list_sign = NULL;
371 static char *panel_filename_scroll_left_char = NULL;
372 static char *panel_filename_scroll_right_char = NULL;
374 /* Panel that selection started */
375 static WPanel *mouse_mark_panel = NULL;
377 static int mouse_marking = 0;
378 static int state_mark = 0;
379 /*** file scope functions ************************************************************************/
380 /* --------------------------------------------------------------------------------------------- */
382 static void
383 set_colors (WPanel * panel)
385 (void) panel;
386 tty_set_normal_attrs ();
387 tty_setcolor (NORMAL_COLOR);
390 /* --------------------------------------------------------------------------------------------- */
391 /** Delete format string, it is a linked list */
393 static void
394 delete_format (format_e * format)
396 while (format != NULL)
398 format_e *next = format->next;
399 g_free (format->title);
400 g_free (format);
401 format = next;
405 /* --------------------------------------------------------------------------------------------- */
406 /** This code relies on the default justification!!! */
408 static void
409 add_permission_string (const char *dest, int width, file_entry_t * fe, int attr, int color,
410 int is_octal)
412 int i, r, l;
414 l = get_user_permissions (&fe->st);
416 if (is_octal)
418 /* Place of the access bit in octal mode */
419 l = width + l - 3;
420 r = l + 1;
422 else
424 /* The same to the triplet in string mode */
425 l = l * 3 + 1;
426 r = l + 3;
429 for (i = 0; i < width; i++)
431 if (i >= l && i < r)
433 if (attr == SELECTED || attr == MARKED_SELECTED)
434 tty_setcolor (MARKED_SELECTED_COLOR);
435 else
436 tty_setcolor (MARKED_COLOR);
438 else if (color >= 0)
439 tty_setcolor (color);
440 else
441 tty_lowlevel_setcolor (-color);
443 tty_print_char (dest[i]);
447 /* --------------------------------------------------------------------------------------------- */
448 /** String representations of various file attributes name */
450 static const char *
451 string_file_name (file_entry_t * fe, int len)
453 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
455 (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;
468 digits++, n /= 10;
470 while (n != 0);
471 return digits;
474 /* --------------------------------------------------------------------------------------------- */
476 static void
477 format_device_number (char *buf, size_t bufsize, dev_t dev)
479 dev_t major_dev = major (dev);
480 dev_t minor_dev = minor (dev);
481 unsigned int major_digits = ilog10 (major_dev);
482 unsigned int minor_digits = ilog10 (minor_dev);
484 g_assert (bufsize >= 1);
485 if (major_digits + 1 + minor_digits + 1 <= bufsize)
487 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
489 else
491 g_strlcpy (buf, _("[dev]"), bufsize);
495 /* --------------------------------------------------------------------------------------------- */
496 /** size */
498 static const char *
499 string_file_size (file_entry_t * fe, int len)
501 static char buffer[BUF_TINY];
503 /* Don't ever show size of ".." since we don't calculate it */
504 if (DIR_IS_DOTDOT (fe->fname))
505 return _("UP--DIR");
507 #ifdef HAVE_STRUCT_STAT_ST_RDEV
508 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
509 format_device_number (buffer, len + 1, fe->st.st_rdev);
510 else
511 #endif
513 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
515 return buffer;
518 /* --------------------------------------------------------------------------------------------- */
519 /** bsize */
521 static const char *
522 string_file_size_brief (file_entry_t * fe, int len)
524 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
526 return _("SYMLINK");
529 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && !DIR_IS_DOTDOT (fe->fname))
531 return _("SUB-DIR");
534 return string_file_size (fe, len);
537 /* --------------------------------------------------------------------------------------------- */
538 /** This functions return a string representation of a file entry type */
540 static const char *
541 string_file_type (file_entry_t * fe, int len)
543 static char buffer[2];
545 (void) len;
546 if (S_ISDIR (fe->st.st_mode))
547 buffer[0] = PATH_SEP;
548 else if (S_ISLNK (fe->st.st_mode))
550 if (fe->f.link_to_dir)
551 buffer[0] = '~';
552 else if (fe->f.stale_link)
553 buffer[0] = '!';
554 else
555 buffer[0] = '@';
557 else if (S_ISCHR (fe->st.st_mode))
558 buffer[0] = '-';
559 else if (S_ISSOCK (fe->st.st_mode))
560 buffer[0] = '=';
561 else if (S_ISDOOR (fe->st.st_mode))
562 buffer[0] = '>';
563 else if (S_ISBLK (fe->st.st_mode))
564 buffer[0] = '+';
565 else if (S_ISFIFO (fe->st.st_mode))
566 buffer[0] = '|';
567 else if (S_ISNAM (fe->st.st_mode))
568 buffer[0] = '#';
569 else if (!S_ISREG (fe->st.st_mode))
570 buffer[0] = '?'; /* non-regular of unknown kind */
571 else if (is_exe (fe->st.st_mode))
572 buffer[0] = '*';
573 else
574 buffer[0] = ' ';
575 buffer[1] = '\0';
576 return buffer;
579 /* --------------------------------------------------------------------------------------------- */
580 /** mtime */
582 static const char *
583 string_file_mtime (file_entry_t * fe, int len)
585 (void) len;
586 return file_date (fe->st.st_mtime);
589 /* --------------------------------------------------------------------------------------------- */
590 /** atime */
592 static const char *
593 string_file_atime (file_entry_t * fe, int len)
595 (void) len;
596 return file_date (fe->st.st_atime);
599 /* --------------------------------------------------------------------------------------------- */
600 /** ctime */
602 static const char *
603 string_file_ctime (file_entry_t * fe, int len)
605 (void) len;
606 return file_date (fe->st.st_ctime);
609 /* --------------------------------------------------------------------------------------------- */
610 /** perm */
612 static const char *
613 string_file_permission (file_entry_t * fe, int len)
615 (void) len;
616 return string_perm (fe->st.st_mode);
619 /* --------------------------------------------------------------------------------------------- */
620 /** mode */
622 static const char *
623 string_file_perm_octal (file_entry_t * fe, int len)
625 static char buffer[10];
627 (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;
641 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
642 return buffer;
645 /* --------------------------------------------------------------------------------------------- */
646 /** inode */
648 static const char *
649 string_inode (file_entry_t * fe, int len)
651 static char buffer[10];
653 (void) len;
654 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
655 return buffer;
658 /* --------------------------------------------------------------------------------------------- */
659 /** nuid */
661 static const char *
662 string_file_nuid (file_entry_t * fe, int len)
664 static char buffer[10];
666 (void) len;
667 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
668 return buffer;
671 /* --------------------------------------------------------------------------------------------- */
672 /** ngid */
674 static const char *
675 string_file_ngid (file_entry_t * fe, int len)
677 static char buffer[10];
679 (void) len;
680 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
681 return buffer;
684 /* --------------------------------------------------------------------------------------------- */
685 /** owner */
687 static const char *
688 string_file_owner (file_entry_t * fe, int len)
690 (void) len;
691 return get_owner (fe->st.st_uid);
694 /* --------------------------------------------------------------------------------------------- */
695 /** group */
697 static const char *
698 string_file_group (file_entry_t * fe, int len)
700 (void) len;
701 return get_group (fe->st.st_gid);
704 /* --------------------------------------------------------------------------------------------- */
705 /** mark */
707 static const char *
708 string_marked (file_entry_t * fe, int len)
710 (void) len;
711 return fe->f.marked ? "*" : " ";
714 /* --------------------------------------------------------------------------------------------- */
715 /** space */
717 static const char *
718 string_space (file_entry_t * fe, int len)
720 (void) fe;
721 (void) len;
722 return " ";
725 /* --------------------------------------------------------------------------------------------- */
726 /** dot */
728 static const char *
729 string_dot (file_entry_t * fe, int len)
731 (void) fe;
732 (void) len;
733 return ".";
736 /* --------------------------------------------------------------------------------------------- */
738 static int
739 file_compute_color (int attr, file_entry_t * fe)
741 switch (attr)
743 case SELECTED:
744 return (SELECTED_COLOR);
745 case MARKED:
746 return (MARKED_COLOR);
747 case MARKED_SELECTED:
748 return (MARKED_SELECTED_COLOR);
749 case STATUS:
750 return (NORMAL_COLOR);
751 case NORMAL:
752 default:
753 if (!panels_options.filetype_mode)
754 return (NORMAL_COLOR);
757 return mc_fhl_get_color (mc_filehighlight, fe);
760 /* --------------------------------------------------------------------------------------------- */
761 /** Formats the file number file_index of panel in the buffer dest */
763 static filename_scroll_flag_t
764 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
765 int isstatus, int *field_lenght)
767 int color, length, empty_line;
768 const char *txt;
769 format_e *format, *home;
770 file_entry_t *fe;
771 filename_scroll_flag_t res = FILENAME_NOSCROLL;
773 (void) dest;
774 (void) limit;
775 length = 0;
776 empty_line = (file_index >= panel->dir.len);
777 home = (isstatus) ? panel->status_format : panel->format;
778 fe = &panel->dir.list[file_index];
779 *field_lenght = 0;
781 if (!empty_line)
782 color = file_compute_color (attr, fe);
783 else
784 color = NORMAL_COLOR;
785 for (format = home; format; format = format->next)
787 if (length == width)
788 break;
790 if (format->string_fn)
792 int len, perm;
793 const char *prepared_text;
794 int name_offset = 0;
796 if (empty_line)
797 txt = " ";
798 else
799 txt = (*format->string_fn) (fe, format->field_len);
801 len = format->field_len;
802 if (len + length > width)
803 len = width - length;
804 if (len <= 0)
805 break;
807 if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
809 int str_len;
810 int i;
812 *field_lenght = len + 1;
814 str_len = str_length (txt);
815 i = max (0, str_len - len);
816 panel->max_shift = max (panel->max_shift, i);
817 i = min (panel->content_shift, i);
819 if (i > -1)
821 name_offset = str_offset_to_pos (txt, i);
822 if (str_len > len)
824 res = FILENAME_SCROLL_LEFT;
825 if (str_length (txt + name_offset) > len)
826 res |= FILENAME_SCROLL_RIGHT;
831 perm = 0;
832 if (panels_options.permission_mode)
834 if (!strcmp (format->id, "perm"))
835 perm = 1;
836 else if (!strcmp (format->id, "mode"))
837 perm = 2;
840 if (color >= 0)
841 tty_setcolor (color);
842 else
843 tty_lowlevel_setcolor (-color);
845 if (!isstatus && panel->content_shift > -1)
846 prepared_text =
847 str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
848 else
849 prepared_text = str_fit_to_term (txt, len, format->just_mode);
851 if (perm)
852 add_permission_string (prepared_text, format->field_len, fe, attr, color, perm - 1);
853 else
854 tty_print_string (prepared_text);
856 length += len;
858 else
860 if (attr == SELECTED || attr == MARKED_SELECTED)
861 tty_setcolor (SELECTED_COLOR);
862 else
863 tty_setcolor (NORMAL_COLOR);
864 tty_print_one_vline (TRUE);
865 length++;
869 if (length < width)
871 int y, x;
873 tty_getyx (&y, &x);
874 tty_draw_hline (y, x, ' ', width - length);
877 return res;
880 /* --------------------------------------------------------------------------------------------- */
882 static void
883 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
885 Widget *w = WIDGET (panel);
887 int second_column = 0;
888 int width;
889 int offset = 0;
890 char buffer[BUF_MEDIUM];
891 filename_scroll_flag_t ret_frm;
892 int ypos = 0;
893 gboolean panel_is_split = !isstatus && panel->split;
894 int fln = 0;
896 width = w->cols - 2;
898 if (panel_is_split)
900 second_column = (file_index - panel->top_file) / llines (panel);
901 width = width / 2 - 1;
903 if (second_column != 0)
905 offset = 1 + width;
906 width = w->cols - offset - 2;
910 /* Nothing to paint */
911 if (width <= 0)
912 return;
914 if (mv)
916 int pos = file_index - panel->top_file;
918 if (panel_is_split)
919 ypos = pos % llines (panel);
920 else
921 ypos = pos;
923 ypos += 2;
924 widget_move (w, ypos, offset + 1);
927 ret_frm = format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus, &fln);
929 if (panel_is_split)
931 if (second_column)
932 tty_print_char (' ');
933 else
935 tty_setcolor (NORMAL_COLOR);
936 tty_print_one_vline (TRUE);
939 if (ret_frm != FILENAME_NOSCROLL && mv)
941 if (!panel_is_split && fln > 0)
943 if (panel->list_type == list_long)
945 offset = width - fln + 1;
946 width = fln - 1;
948 else
950 width = fln;
953 widget_move (w, ypos, offset);
954 tty_setcolor (NORMAL_COLOR);
955 tty_print_string (panel_filename_scroll_left_char);
957 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
959 widget_move (w, ypos, offset + 1 + width);
960 tty_setcolor (NORMAL_COLOR);
961 tty_print_string (panel_filename_scroll_right_char);
966 /* --------------------------------------------------------------------------------------------- */
968 static void
969 display_mini_info (WPanel * panel)
971 Widget *w = WIDGET (panel);
973 if (!panels_options.show_mini_info)
974 return;
976 widget_move (w, llines (panel) + 3, 1);
978 if (panel->searching)
980 tty_setcolor (INPUT_COLOR);
981 tty_print_char ('/');
982 tty_print_string (str_fit_to_term (panel->search_buffer, w->cols - 3, J_LEFT));
983 return;
986 /* Status resolves links and show them */
987 set_colors (panel);
989 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
991 char link_target[MC_MAXPATHLEN];
992 vfs_path_t *lc_link_vpath;
993 int len;
995 lc_link_vpath =
996 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
997 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
998 vfs_path_free (lc_link_vpath);
999 if (len > 0)
1001 link_target[len] = 0;
1002 tty_print_string ("-> ");
1003 tty_print_string (str_fit_to_term (link_target, w->cols - 5, J_LEFT_FIT));
1005 else
1006 tty_print_string (str_fit_to_term (_("<readlink failed>"), w->cols - 2, J_LEFT));
1008 else if (DIR_IS_DOTDOT (panel->dir.list[panel->selected].fname))
1010 /* FIXME:
1011 * while loading directory (dir_list_load() and dir_list_reload()),
1012 * the actual stat info about ".." directory isn't got;
1013 * so just don't display incorrect info about ".." directory */
1014 tty_print_string (str_fit_to_term (_("UP--DIR"), w->cols - 2, J_LEFT));
1016 else
1017 /* Default behavior */
1018 repaint_file (panel, panel->selected, 0, STATUS, 1);
1021 /* --------------------------------------------------------------------------------------------- */
1023 static void
1024 paint_dir (WPanel * panel)
1026 int i;
1027 int color; /* Color value of the line */
1028 int items; /* Number of items */
1030 items = llines (panel) * (panel->split ? 2 : 1);
1031 /* reset max len of filename because we have the new max length for the new file list */
1032 panel->max_shift = -1;
1033 for (i = 0; i < items; i++)
1035 if (i + panel->top_file >= panel->dir.len)
1036 color = 0;
1037 else
1039 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1040 color += (panel->selected == i + panel->top_file && panel->active);
1042 repaint_file (panel, i + panel->top_file, 1, color, 0);
1045 tty_set_normal_attrs ();
1048 /* --------------------------------------------------------------------------------------------- */
1050 static void
1051 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
1053 Widget *w = WIDGET (panel);
1055 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
1056 int cols;
1058 if (panel->marked <= 0)
1059 return;
1061 buf = size_only ? b_bytes : buffer;
1062 cols = w->cols - 2;
1065 * This is a trick to use two ngettext() calls in one sentence.
1066 * First make "N bytes", then insert it into "X in M files".
1068 g_snprintf (b_bytes, sizeof (b_bytes),
1069 ngettext ("%s byte", "%s bytes", panel->total),
1070 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1071 if (!size_only)
1072 g_snprintf (buffer, sizeof (buffer),
1073 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1074 b_bytes, panel->marked);
1076 /* don't forget spaces around buffer content */
1077 buf = (char *) str_trunc (buf, cols - 4);
1079 if (x < 0)
1080 /* center in panel */
1081 x = (w->cols - str_term_width1 (buf)) / 2 - 1;
1084 * y == llines (panel) + 2 for mini_info_separator
1085 * y == w->lines - 1 for panel bottom frame
1087 widget_move (w, y, x);
1088 tty_setcolor (MARKED_COLOR);
1089 tty_printf (" %s ", buf);
1092 /* --------------------------------------------------------------------------------------------- */
1094 static void
1095 mini_info_separator (WPanel * panel)
1097 if (panels_options.show_mini_info)
1099 Widget *w = WIDGET (panel);
1100 const int y = llines (panel) + 2;
1102 tty_setcolor (NORMAL_COLOR);
1103 tty_draw_hline (w->y + y, w->x + 1, ACS_HLINE, w->cols - 2);
1104 /* Status displays total marked size.
1105 * Centered in panel, full format. */
1106 display_total_marked_size (panel, y, -1, FALSE);
1110 /* --------------------------------------------------------------------------------------------- */
1112 static void
1113 show_free_space (WPanel * panel)
1115 /* Used to figure out how many free space we have */
1116 static struct my_statfs myfs_stats;
1117 /* Old current working directory for displaying free space */
1118 static char *old_cwd = NULL;
1120 /* Don't try to stat non-local fs */
1121 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1122 return;
1124 if (old_cwd == NULL || strcmp (old_cwd, vfs_path_as_str (panel->cwd_vpath)) != 0)
1126 char rpath[PATH_MAX];
1128 init_my_statfs ();
1129 g_free (old_cwd);
1130 old_cwd = g_strdup (vfs_path_as_str (panel->cwd_vpath));
1132 if (mc_realpath (old_cwd, rpath) == NULL)
1133 return;
1135 my_statfs (&myfs_stats, rpath);
1138 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1140 Widget *w = WIDGET (panel);
1141 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1143 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1144 panels_options.kilobyte_si);
1145 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1146 panels_options.kilobyte_si);
1147 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1148 myfs_stats.total == 0 ? 0 :
1149 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1150 widget_move (w, w->lines - 1, w->cols - 2 - (int) strlen (tmp));
1151 tty_setcolor (NORMAL_COLOR);
1152 tty_print_string (tmp);
1156 /* --------------------------------------------------------------------------------------------- */
1158 * Prepare path string for showing in panel's header.
1159 * Passwords will removed, also home dir will replaced by ~
1161 * @param panel WPanel object
1163 * @return newly allocated string.
1166 static char *
1167 panel_correct_path_to_show (WPanel * panel)
1169 vfs_path_t *last_vpath;
1170 const vfs_path_element_t *path_element;
1171 char *return_path;
1172 int elements_count;
1174 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1176 /* get last path element */
1177 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1180 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1181 strcmp (path_element->class->name, "extfs") == 0 ||
1182 strcmp (path_element->class->name, "tarfs") == 0))
1184 const char *archive_name;
1185 const vfs_path_element_t *prev_path_element;
1187 /* get previous path element for catching archive name */
1188 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1189 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1190 if (archive_name != NULL)
1192 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1194 else
1196 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1197 last_vpath->relative = TRUE;
1200 else
1202 last_vpath = vfs_path_new ();
1203 last_vpath->relative = TRUE;
1206 vfs_path_add_element (last_vpath, path_element);
1207 return_path =
1208 vfs_path_to_str_flags (last_vpath, 0,
1209 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1210 vfs_path_free (last_vpath);
1212 return return_path;
1215 /* --------------------------------------------------------------------------------------------- */
1217 * Get Current path element encoding
1219 * @param panel WPanel object
1221 * @return newly allocated string or NULL if path charset is same as system charset
1224 #ifdef HAVE_CHARSET
1225 static char *
1226 panel_get_encoding_info_str (WPanel * panel)
1228 char *ret_str = NULL;
1229 const vfs_path_element_t *path_element;
1231 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1232 if (path_element->encoding != NULL)
1233 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1235 return ret_str;
1237 #endif
1239 /* --------------------------------------------------------------------------------------------- */
1241 static void
1242 show_dir (WPanel * panel)
1244 Widget *w = WIDGET (panel);
1246 gchar *tmp;
1248 set_colors (panel);
1249 tty_draw_box (w->y, w->x, w->lines, w->cols, FALSE);
1251 if (panels_options.show_mini_info)
1253 widget_move (w, llines (panel) + 2, 0);
1254 tty_print_alt_char (ACS_LTEE, FALSE);
1255 widget_move (w, llines (panel) + 2, w->cols - 1);
1256 tty_print_alt_char (ACS_RTEE, FALSE);
1259 widget_move (w, 0, 1);
1260 tty_print_string (panel_history_prev_item_sign);
1262 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1263 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1264 panel_history_next_item_sign);
1266 widget_move (w, 0, w->cols - 6);
1267 tty_print_string (tmp);
1269 g_free (tmp);
1271 widget_move (w, 0, 3);
1273 if (panel->is_panelized)
1274 tty_printf (" %s ", _("Panelize"));
1275 #ifdef HAVE_CHARSET
1276 else
1278 tmp = panel_get_encoding_info_str (panel);
1279 if (tmp != NULL)
1281 tty_printf ("%s", tmp);
1282 widget_move (w, 0, 3 + strlen (tmp));
1283 g_free (tmp);
1286 #endif
1288 if (panel->active)
1289 tty_setcolor (REVERSE_COLOR);
1291 tmp = panel_correct_path_to_show (panel);
1292 tty_printf (" %s ", str_term_trim (tmp, min (max (w->cols - 12, 0), w->cols)));
1293 g_free (tmp);
1295 if (!panels_options.show_mini_info)
1297 if (panel->marked == 0)
1299 /* Show size of curret file in the bottom of panel */
1300 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1302 char buffer[BUF_SMALL];
1304 g_snprintf (buffer, sizeof (buffer), " %s ",
1305 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1306 panels_options.kilobyte_si));
1307 tty_setcolor (NORMAL_COLOR);
1308 widget_move (w, w->lines - 1, 4);
1309 tty_print_string (buffer);
1312 else
1314 /* Show total size of marked files
1315 * In the bottom of panel, display size only. */
1316 display_total_marked_size (panel, w->lines - 1, 2, TRUE);
1320 show_free_space (panel);
1322 if (panel->active)
1323 tty_set_normal_attrs ();
1326 /* --------------------------------------------------------------------------------------------- */
1328 /* Returns the number of items in the given panel */
1329 static int
1330 ITEMS (WPanel * p)
1332 if (p->split)
1333 return llines (p) * 2;
1334 else
1335 return llines (p);
1338 /* --------------------------------------------------------------------------------------------- */
1340 static void
1341 adjust_top_file (WPanel * panel)
1343 int items = ITEMS (panel);
1345 if (panel->dir.len <= items)
1347 /* If all files fit, show them all. */
1348 panel->top_file = 0;
1350 else
1352 int i;
1354 /* top_file has to be in the range [selected-items+1, selected] so that
1355 the selected file is visible.
1356 top_file should be in the range [0, count-items] so that there's
1357 no empty space wasted.
1358 Within these ranges, adjust it by as little as possible. */
1360 if (panel->top_file < 0)
1361 panel->top_file = 0;
1363 i = panel->selected - items + 1;
1364 if (panel->top_file < i)
1365 panel->top_file = i;
1367 i = panel->dir.len - items;
1368 if (panel->top_file > i)
1369 panel->top_file = i;
1371 if (panel->top_file > panel->selected)
1372 panel->top_file = panel->selected;
1376 /* --------------------------------------------------------------------------------------------- */
1377 /** add "#enc:encodning" to end of path */
1378 /* if path end width a previous #enc:, only encoding is changed no additional
1379 * #enc: is appended
1380 * retun new string
1383 static char *
1384 panel_save_name (WPanel * panel)
1386 /* If the program is shuting down */
1387 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1388 return g_strdup (panel->panel_name);
1389 else
1390 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1393 /* --------------------------------------------------------------------------------------------- */
1395 static void
1396 directory_history_add (struct WPanel *panel, const vfs_path_t * vpath)
1398 char *tmp;
1400 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1401 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1402 panel->dir_history_current = panel->dir_history;
1405 /* --------------------------------------------------------------------------------------------- */
1407 /* "history_load" event handler */
1408 static gboolean
1409 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1410 gpointer init_data, gpointer data)
1412 WPanel *p = (WPanel *) init_data;
1413 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1415 (void) event_group_name;
1416 (void) event_name;
1418 if (ev->receiver == NULL || ev->receiver == WIDGET (p))
1420 if (ev->cfg != NULL)
1421 p->dir_history = history_load (ev->cfg, p->hist_name);
1422 else
1423 p->dir_history = history_get (p->hist_name);
1425 directory_history_add (p, p->cwd_vpath);
1428 return TRUE;
1431 /* --------------------------------------------------------------------------------------------- */
1433 /* "history_save" event handler */
1434 static gboolean
1435 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1436 gpointer init_data, gpointer data)
1438 WPanel *p = (WPanel *) init_data;
1440 (void) event_group_name;
1441 (void) event_name;
1443 if (p->dir_history != NULL)
1445 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1447 history_save (ev->cfg, p->hist_name, p->dir_history);
1450 return TRUE;
1453 /* --------------------------------------------------------------------------------------------- */
1455 static void
1456 panel_destroy (WPanel * p)
1458 size_t i;
1460 if (panels_options.auto_save_setup)
1462 char *name;
1464 name = panel_save_name (p);
1465 panel_save_setup (p, name);
1466 g_free (name);
1469 panel_clean_dir (p);
1471 /* clean history */
1472 if (p->dir_history != NULL)
1474 /* directory history is already saved before this moment */
1475 p->dir_history = g_list_first (p->dir_history);
1476 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1477 g_list_free (p->dir_history);
1479 g_free (p->hist_name);
1481 delete_format (p->format);
1482 delete_format (p->status_format);
1484 g_free (p->user_format);
1485 for (i = 0; i < LIST_TYPES; i++)
1486 g_free (p->user_status_format[i]);
1488 g_free (p->dir.list);
1489 g_free (p->panel_name);
1491 vfs_path_free (p->lwd_vpath);
1492 vfs_path_free (p->cwd_vpath);
1495 /* --------------------------------------------------------------------------------------------- */
1497 static void
1498 panel_format_modified (WPanel * panel)
1500 panel->format_modified = 1;
1503 /* --------------------------------------------------------------------------------------------- */
1505 static void
1506 panel_paint_sort_info (WPanel * panel)
1508 if (*panel->sort_field->hotkey != '\0')
1510 const char *sort_sign =
1511 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1512 char *str;
1514 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_field->hotkey));
1515 widget_move (panel, 1, 1);
1516 tty_print_string (str);
1517 g_free (str);
1521 /* --------------------------------------------------------------------------------------------- */
1523 static gchar *
1524 panel_get_title_without_hotkey (const char *title)
1526 char *translated_title;
1527 char *hkey;
1529 if (title == NULL)
1530 return NULL;
1531 if (title[0] == '\0')
1532 return g_strdup ("");
1534 translated_title = g_strdup (_(title));
1536 hkey = strchr (translated_title, '&');
1537 if ((hkey != NULL) && (hkey[1] != '\0'))
1538 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1540 return translated_title;
1543 /* --------------------------------------------------------------------------------------------- */
1545 static void
1546 paint_frame (WPanel * panel)
1548 Widget *w = WIDGET (panel);
1550 int side, width;
1552 adjust_top_file (panel);
1554 widget_erase (w);
1555 show_dir (panel);
1557 widget_move (w, 1, 1);
1559 for (side = 0; side <= panel->split; side++)
1561 GString *format_txt;
1562 format_e *format;
1564 if (side)
1566 tty_setcolor (NORMAL_COLOR);
1567 tty_print_one_vline (TRUE);
1568 width = w->cols - w->cols / 2 - 1;
1570 else if (panel->split)
1571 width = w->cols / 2 - 3;
1572 else
1573 width = w->cols - 2;
1575 format_txt = g_string_new ("");
1576 for (format = panel->format; format; format = format->next)
1578 if (format->string_fn)
1580 g_string_set_size (format_txt, 0);
1582 if (panel->list_type == list_long
1583 && strcmp (format->id, panel->sort_field->id) == 0)
1584 g_string_append (format_txt,
1585 panel->sort_info.reverse
1586 ? panel_sort_down_sign : panel_sort_up_sign);
1588 g_string_append (format_txt, format->title);
1589 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1591 g_string_append (format_txt, " [");
1592 g_string_append (format_txt, panel->filter);
1593 g_string_append (format_txt, "]");
1596 tty_setcolor (HEADER_COLOR);
1597 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1598 J_CENTER_LEFT));
1599 width -= format->field_len;
1601 else
1603 tty_setcolor (NORMAL_COLOR);
1604 tty_print_one_vline (TRUE);
1605 width--;
1608 g_string_free (format_txt, TRUE);
1610 if (width > 0)
1612 int y, x;
1614 tty_getyx (&y, &x);
1615 tty_draw_hline (y, x, ' ', width);
1619 if (panel->list_type != list_long)
1620 panel_paint_sort_info (panel);
1623 /* --------------------------------------------------------------------------------------------- */
1625 static const char *
1626 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1628 panel_display_t frame = frame_half;
1629 format = skip_separators (format);
1631 if (!strncmp (format, "full", 4))
1633 frame = frame_full;
1634 format += 4;
1636 else if (!strncmp (format, "half", 4))
1638 frame = frame_half;
1639 format += 4;
1642 if (!isstatus)
1644 panel->frame_size = frame;
1645 panel->split = 0;
1648 /* Now, the optional column specifier */
1649 format = skip_separators (format);
1651 if (*format == '1' || *format == '2')
1653 if (!isstatus)
1654 panel->split = *format == '2';
1655 format++;
1658 if (!isstatus)
1659 panel_update_cols (WIDGET (panel), panel->frame_size);
1661 return skip_separators (format);
1664 /* Format is:
1666 all := panel_format? format
1667 panel_format := [full|half] [1|2]
1668 format := one_format_e
1669 | format , one_format_e
1671 one_format_e := just format.id [opt_size]
1672 just := [<=>]
1673 opt_size := : size [opt_expand]
1674 size := [0-9]+
1675 opt_expand := +
1679 /* --------------------------------------------------------------------------------------------- */
1681 static format_e *
1682 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1683 int *res_total_cols)
1685 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1686 int total_cols = 0; /* Used columns by the format */
1687 int set_justify; /* flag: set justification mode? */
1688 align_crt_t justify = J_LEFT; /* Which mode. */
1689 size_t i;
1691 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1693 *error = 0;
1695 if (i18n_timelength == 0)
1697 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1699 for (i = 0; panel_fields[i].id != NULL; i++)
1700 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1701 panel_fields[i].min_size = i18n_timelength;
1705 * This makes sure that the panel and mini status full/half mode
1706 * setting is equal
1708 format = parse_panel_size (panel, format, isstatus);
1710 while (*format)
1711 { /* format can be an empty string */
1712 int found = 0;
1714 darr = g_new0 (format_e, 1);
1716 /* I'm so ugly, don't look at me :-) */
1717 if (!home)
1718 home = old = darr;
1720 old->next = darr;
1721 darr->next = 0;
1722 old = darr;
1724 format = skip_separators (format);
1726 if (strchr ("<=>", *format))
1728 set_justify = 1;
1729 switch (*format)
1731 case '<':
1732 justify = J_LEFT;
1733 break;
1734 case '=':
1735 justify = J_CENTER;
1736 break;
1737 case '>':
1738 default:
1739 justify = J_RIGHT;
1740 break;
1742 format = skip_separators (format + 1);
1744 else
1745 set_justify = 0;
1747 for (i = 0; panel_fields[i].id != NULL; i++)
1749 size_t klen = strlen (panel_fields[i].id);
1751 if (strncmp (format, panel_fields[i].id, klen) != 0)
1752 continue;
1754 format += klen;
1756 darr->requested_field_len = panel_fields[i].min_size;
1757 darr->string_fn = panel_fields[i].string_fn;
1758 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1760 darr->id = panel_fields[i].id;
1761 darr->expand = panel_fields[i].expands;
1762 darr->just_mode = panel_fields[i].default_just;
1764 if (set_justify)
1766 if (IS_FIT (darr->just_mode))
1767 darr->just_mode = MAKE_FIT (justify);
1768 else
1769 darr->just_mode = justify;
1771 found = 1;
1773 format = skip_separators (format);
1775 /* If we have a size specifier */
1776 if (*format == ':')
1778 int req_length;
1780 /* If the size was specified, we don't want
1781 * auto-expansion by default
1783 darr->expand = 0;
1784 format++;
1785 req_length = atoi (format);
1786 darr->requested_field_len = req_length;
1788 format = skip_numbers (format);
1790 /* Now, if they insist on expansion */
1791 if (*format == '+')
1793 darr->expand = 1;
1794 format++;
1799 break;
1801 if (!found)
1803 char *tmp_format = g_strdup (format);
1805 int pos = min (8, strlen (format));
1806 delete_format (home);
1807 tmp_format[pos] = 0;
1808 *error =
1809 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1810 g_free (tmp_format);
1811 return 0;
1813 total_cols += darr->requested_field_len;
1816 *res_total_cols = total_cols;
1817 return home;
1820 /* --------------------------------------------------------------------------------------------- */
1822 static format_e *
1823 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1825 #define MAX_EXPAND 4
1826 int expand_top = 0; /* Max used element in expand */
1827 int usable_columns; /* Usable columns in the panel */
1828 int total_cols = 0;
1829 format_e *darr, *home;
1831 if (!format)
1832 format = DEFAULT_USER_FORMAT;
1834 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1836 if (*error)
1837 return 0;
1839 panel->dirty = 1;
1841 /* Status needn't to be split */
1842 usable_columns = ((WIDGET (panel)->cols - 2) / ((isstatus)
1844 : (panel->split + 1))) - (!isstatus
1845 && panel->split);
1847 /* Look for the expandable fields and set field_len based on the requested field len */
1848 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1850 darr->field_len = darr->requested_field_len;
1851 if (darr->expand)
1852 expand_top++;
1855 /* If we used more columns than the available columns, adjust that */
1856 if (total_cols > usable_columns)
1858 int dif = total_cols - usable_columns;
1860 while (dif)
1862 int pdif = dif;
1864 for (darr = home; darr; darr = darr->next)
1866 if (dif && darr->field_len - 1)
1868 darr->field_len--;
1869 dif--;
1873 /* avoid endless loop if num fields > 40 */
1874 if (pdif == dif)
1875 break;
1877 total_cols = usable_columns; /* give up, the rest should be truncated */
1880 /* Expand the available space */
1881 if ((usable_columns > total_cols) && expand_top)
1883 int i;
1884 int spaces = (usable_columns - total_cols) / expand_top;
1885 int extra = (usable_columns - total_cols) % expand_top;
1887 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1888 if (darr->expand)
1890 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1891 i++;
1894 return home;
1897 /* --------------------------------------------------------------------------------------------- */
1898 /** Given the panel->view_type returns the format string to be parsed */
1900 static const char *
1901 panel_format (WPanel * panel)
1903 switch (panel->list_type)
1905 case list_long:
1906 return "full perm space nlink space owner space group space size space mtime space name";
1908 case list_brief:
1909 return "half 2 type name";
1911 case list_user:
1912 return panel->user_format;
1914 default:
1915 case list_full:
1916 return "half type name | size | mtime";
1920 /* --------------------------------------------------------------------------------------------- */
1922 static const char *
1923 mini_status_format (WPanel * panel)
1925 if (panel->user_mini_status)
1926 return panel->user_status_format[panel->list_type];
1928 switch (panel->list_type)
1930 case list_long:
1931 return "full perm space nlink space owner space group space size space mtime space name";
1933 case list_brief:
1934 return "half type name space bsize space perm space";
1936 case list_full:
1937 return "half type name";
1939 default:
1940 case list_user:
1941 return panel->user_format;
1945 /* */
1946 /* Panel operation commands */
1947 /* */
1949 /* --------------------------------------------------------------------------------------------- */
1950 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1952 static cb_ret_t
1953 maybe_cd (int move_up_dir)
1955 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1957 if (move_up_dir)
1959 vfs_path_t *up_dir;
1961 up_dir = vfs_path_from_str ("..");
1962 do_cd (up_dir, cd_exact);
1963 vfs_path_free (up_dir);
1964 return MSG_HANDLED;
1967 if (S_ISDIR (selection (current_panel)->st.st_mode)
1968 || link_isdir (selection (current_panel)))
1970 vfs_path_t *vpath;
1972 vpath = vfs_path_from_str (selection (current_panel)->fname);
1973 do_cd (vpath, cd_exact);
1974 vfs_path_free (vpath);
1975 return MSG_HANDLED;
1978 return MSG_NOT_HANDLED;
1981 /* --------------------------------------------------------------------------------------------- */
1983 /* if command line is empty then do 'cd ..' */
1984 static cb_ret_t
1985 force_maybe_cd (void)
1987 if (cmdline->buffer[0] == '\0')
1989 vfs_path_t *up_dir = vfs_path_from_str ("..");
1990 do_cd (up_dir, cd_exact);
1991 vfs_path_free (up_dir);
1992 return MSG_HANDLED;
1994 return MSG_NOT_HANDLED;
1997 /* --------------------------------------------------------------------------------------------- */
1999 static void
2000 unselect_item (WPanel * panel)
2002 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
2005 /* --------------------------------------------------------------------------------------------- */
2007 static void
2008 move_down (WPanel * panel)
2010 if (panel->selected + 1 == panel->dir.len)
2011 return;
2013 unselect_item (panel);
2014 panel->selected++;
2015 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
2017 /* Scroll window half screen */
2018 panel->top_file += ITEMS (panel) / 2;
2019 if (panel->top_file > panel->dir.len - ITEMS (panel))
2020 panel->top_file = panel->dir.len - ITEMS (panel);
2021 paint_dir (panel);
2023 select_item (panel);
2026 /* --------------------------------------------------------------------------------------------- */
2028 static void
2029 move_up (WPanel * panel)
2031 if (panel->selected == 0)
2032 return;
2034 unselect_item (panel);
2035 panel->selected--;
2036 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2038 /* Scroll window half screen */
2039 panel->top_file -= ITEMS (panel) / 2;
2040 if (panel->top_file < 0)
2041 panel->top_file = 0;
2042 paint_dir (panel);
2044 select_item (panel);
2047 /* --------------------------------------------------------------------------------------------- */
2048 /** Changes the selection by lines (may be negative) */
2050 static void
2051 move_selection (WPanel * panel, int lines)
2053 int new_pos;
2054 int adjust = 0;
2056 new_pos = panel->selected + lines;
2057 if (new_pos >= panel->dir.len)
2058 new_pos = panel->dir.len - 1;
2060 if (new_pos < 0)
2061 new_pos = 0;
2063 unselect_item (panel);
2064 panel->selected = new_pos;
2066 if (panel->selected - panel->top_file >= ITEMS (panel))
2068 panel->top_file += lines;
2069 adjust = 1;
2072 if (panel->selected - panel->top_file < 0)
2074 panel->top_file += lines;
2075 adjust = 1;
2078 if (adjust)
2080 if (panel->top_file > panel->selected)
2081 panel->top_file = panel->selected;
2082 if (panel->top_file < 0)
2083 panel->top_file = 0;
2084 paint_dir (panel);
2086 select_item (panel);
2089 /* --------------------------------------------------------------------------------------------- */
2091 static cb_ret_t
2092 move_left (WPanel * panel)
2094 if (panel->split)
2096 move_selection (panel, -llines (panel));
2097 return MSG_HANDLED;
2099 else
2100 return maybe_cd (1); /* cd .. */
2103 /* --------------------------------------------------------------------------------------------- */
2105 static cb_ret_t
2106 move_right (WPanel * panel)
2108 if (panel->split)
2110 move_selection (panel, llines (panel));
2111 return MSG_HANDLED;
2113 else
2114 return maybe_cd (0); /* cd (selection) */
2117 /* --------------------------------------------------------------------------------------------- */
2119 static void
2120 prev_page (WPanel * panel)
2122 int items;
2124 if (!panel->selected && !panel->top_file)
2125 return;
2126 unselect_item (panel);
2127 items = ITEMS (panel);
2128 if (panel->top_file < items)
2129 items = panel->top_file;
2130 if (!items)
2131 panel->selected = 0;
2132 else
2133 panel->selected -= items;
2134 panel->top_file -= items;
2136 select_item (panel);
2137 paint_dir (panel);
2140 /* --------------------------------------------------------------------------------------------- */
2142 static void
2143 goto_parent_dir (WPanel * panel)
2145 if (!panel->is_panelized)
2147 vfs_path_t *up_dir;
2149 up_dir = vfs_path_from_str ("..");
2150 do_cd (up_dir, cd_exact);
2151 vfs_path_free (up_dir);
2153 else
2155 char *fname = panel->dir.list[panel->selected].fname;
2156 const char *bname;
2157 vfs_path_t *dname_vpath;
2159 if (g_path_is_absolute (fname))
2160 fname = g_strdup (fname);
2161 else
2162 fname =
2163 mc_build_filename (vfs_path_as_str (panelized_panel.root_vpath), fname,
2164 (char *) NULL);
2166 bname = x_basename (fname);
2168 if (bname == fname)
2169 dname_vpath = vfs_path_from_str (".");
2170 else
2172 char *dname;
2174 dname = g_strndup (fname, bname - fname);
2175 dname_vpath = vfs_path_from_str (dname);
2176 g_free (dname);
2179 do_cd (dname_vpath, cd_exact);
2180 try_to_select (panel, bname);
2182 vfs_path_free (dname_vpath);
2183 g_free (fname);
2187 /* --------------------------------------------------------------------------------------------- */
2189 static void
2190 next_page (WPanel * panel)
2192 int items;
2194 if (panel->selected == panel->dir.len - 1)
2195 return;
2196 unselect_item (panel);
2197 items = ITEMS (panel);
2198 if (panel->top_file > panel->dir.len - 2 * items)
2199 items = panel->dir.len - items - panel->top_file;
2200 if (panel->top_file + items < 0)
2201 items = -panel->top_file;
2202 if (!items)
2203 panel->selected = panel->dir.len - 1;
2204 else
2205 panel->selected += items;
2206 panel->top_file += items;
2208 select_item (panel);
2209 paint_dir (panel);
2212 /* --------------------------------------------------------------------------------------------- */
2214 static void
2215 goto_child_dir (WPanel * panel)
2217 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2219 vfs_path_t *vpath;
2221 vpath = vfs_path_from_str (selection (panel)->fname);
2222 do_cd (vpath, cd_exact);
2223 vfs_path_free (vpath);
2227 /* --------------------------------------------------------------------------------------------- */
2229 static void
2230 goto_top_file (WPanel * panel)
2232 unselect_item (panel);
2233 panel->selected = panel->top_file;
2234 select_item (panel);
2237 /* --------------------------------------------------------------------------------------------- */
2239 static void
2240 goto_middle_file (WPanel * panel)
2242 unselect_item (panel);
2243 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2244 select_item (panel);
2247 /* --------------------------------------------------------------------------------------------- */
2249 static void
2250 goto_bottom_file (WPanel * panel)
2252 unselect_item (panel);
2253 panel->selected = panel->top_file + ITEMS (panel) - 1;
2254 select_item (panel);
2257 /* --------------------------------------------------------------------------------------------- */
2259 static void
2260 move_home (WPanel * panel)
2262 if (panel->selected == 0)
2263 return;
2265 unselect_item (panel);
2267 if (panels_options.torben_fj_mode)
2269 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2271 if (panel->selected > middle_pos)
2273 goto_middle_file (panel);
2274 return;
2276 if (panel->selected != panel->top_file)
2278 goto_top_file (panel);
2279 return;
2283 panel->top_file = 0;
2284 panel->selected = 0;
2286 paint_dir (panel);
2287 select_item (panel);
2290 /* --------------------------------------------------------------------------------------------- */
2292 static void
2293 move_end (WPanel * panel)
2295 if (panel->selected == panel->dir.len - 1)
2296 return;
2298 unselect_item (panel);
2300 if (panels_options.torben_fj_mode)
2302 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2304 if (panel->selected < middle_pos)
2306 goto_middle_file (panel);
2307 return;
2309 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2311 goto_bottom_file (panel);
2312 return;
2316 panel->selected = panel->dir.len - 1;
2317 paint_dir (panel);
2318 select_item (panel);
2321 /* --------------------------------------------------------------------------------------------- */
2323 static void
2324 do_mark_file (WPanel * panel, mark_act_t do_move)
2326 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2327 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2328 move_down (panel);
2329 else if (do_move == MARK_FORCE_UP)
2330 move_up (panel);
2333 /* --------------------------------------------------------------------------------------------- */
2335 static void
2336 mark_file (WPanel * panel)
2338 do_mark_file (panel, MARK_DOWN);
2341 /* --------------------------------------------------------------------------------------------- */
2343 static void
2344 mark_file_up (WPanel * panel)
2346 do_mark_file (panel, MARK_FORCE_UP);
2349 /* --------------------------------------------------------------------------------------------- */
2351 static void
2352 mark_file_down (WPanel * panel)
2354 do_mark_file (panel, MARK_FORCE_DOWN);
2357 /* --------------------------------------------------------------------------------------------- */
2359 static void
2360 mark_file_right (WPanel * panel)
2362 int lines = llines (panel);
2364 if (state_mark < 0)
2365 state_mark = selection (panel)->f.marked ? 0 : 1;
2367 lines = min (lines, panel->dir.len - panel->selected - 1);
2368 for (; lines != 0; lines--)
2370 do_file_mark (panel, panel->selected, state_mark);
2371 move_down (panel);
2373 do_file_mark (panel, panel->selected, state_mark);
2376 /* --------------------------------------------------------------------------------------------- */
2378 static void
2379 mark_file_left (WPanel * panel)
2381 int lines = llines (panel);
2383 if (state_mark < 0)
2384 state_mark = selection (panel)->f.marked ? 0 : 1;
2386 lines = min (lines, panel->selected + 1);
2387 for (; lines != 0; lines--)
2389 do_file_mark (panel, panel->selected, state_mark);
2390 move_up (panel);
2392 do_file_mark (panel, panel->selected, state_mark);
2395 /* --------------------------------------------------------------------------------------------- */
2396 /** Incremental search of a file name in the panel.
2397 * @param panel instance of WPanel structure
2398 * @param c_code key code
2401 static void
2402 do_search (WPanel * panel, int c_code)
2404 size_t l;
2405 int i, sel;
2406 gboolean wrapped = FALSE;
2407 char *act;
2408 mc_search_t *search;
2409 char *reg_exp, *esc_str;
2410 gboolean is_found = FALSE;
2412 l = strlen (panel->search_buffer);
2413 if (c_code == KEY_BACKSPACE)
2415 if (l != 0)
2417 act = panel->search_buffer + l;
2418 str_prev_noncomb_char (&act, panel->search_buffer);
2419 act[0] = '\0';
2421 panel->search_chpoint = 0;
2423 else
2425 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2427 panel->search_char[panel->search_chpoint] = c_code;
2428 panel->search_chpoint++;
2431 if (panel->search_chpoint > 0)
2433 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2435 case -2:
2436 return;
2437 case -1:
2438 panel->search_chpoint = 0;
2439 return;
2440 default:
2441 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2443 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2444 l += panel->search_chpoint;
2445 *(panel->search_buffer + l) = '\0';
2446 panel->search_chpoint = 0;
2452 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2453 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2454 search = mc_search_new (esc_str, -1, NULL);
2455 search->search_type = MC_SEARCH_T_GLOB;
2456 search->is_entire_line = TRUE;
2457 switch (panels_options.qsearch_mode)
2459 case QSEARCH_CASE_SENSITIVE:
2460 search->is_case_sensitive = TRUE;
2461 break;
2462 case QSEARCH_CASE_INSENSITIVE:
2463 search->is_case_sensitive = FALSE;
2464 break;
2465 default:
2466 search->is_case_sensitive = panel->sort_info.case_sensitive;
2467 break;
2469 sel = panel->selected;
2470 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2472 if (i >= panel->dir.len)
2474 i = 0;
2475 if (wrapped)
2476 break;
2477 wrapped = TRUE;
2479 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2481 sel = i;
2482 is_found = TRUE;
2483 break;
2486 if (is_found)
2488 unselect_item (panel);
2489 panel->selected = sel;
2490 select_item (panel);
2491 widget_redraw (WIDGET (panel));
2493 else if (c_code != KEY_BACKSPACE)
2495 act = panel->search_buffer + l;
2496 str_prev_noncomb_char (&act, panel->search_buffer);
2497 act[0] = '\0';
2499 mc_search_free (search);
2500 g_free (reg_exp);
2501 g_free (esc_str);
2504 /* --------------------------------------------------------------------------------------------- */
2505 /** Start new search.
2506 * @param panel instance of WPanel structure
2509 static void
2510 start_search (WPanel * panel)
2512 if (panel->searching)
2514 if (panel->selected + 1 == panel->dir.len)
2515 panel->selected = 0;
2516 else
2517 move_down (panel);
2519 /* in case if there was no search string we need to recall
2520 previous string, with which we ended previous searching */
2521 if (panel->search_buffer[0] == '\0')
2522 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2523 sizeof (panel->search_buffer));
2525 do_search (panel, 0);
2527 else
2529 panel->searching = TRUE;
2530 panel->search_buffer[0] = '\0';
2531 panel->search_char[0] = '\0';
2532 panel->search_chpoint = 0;
2533 display_mini_info (panel);
2534 mc_refresh ();
2538 /* --------------------------------------------------------------------------------------------- */
2540 static void
2541 stop_search (WPanel * panel)
2543 panel->searching = FALSE;
2545 /* if user had overrdied search string, we need to store it
2546 to the previous_search_buffer */
2547 if (panel->search_buffer[0] != '\0')
2548 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2549 sizeof (panel->prev_search_buffer));
2551 display_mini_info (panel);
2554 /* --------------------------------------------------------------------------------------------- */
2555 /** Return 1 if the Enter key has been processed, 0 otherwise */
2557 static int
2558 do_enter_on_file_entry_t (file_entry_t * fe)
2560 vfs_path_t *full_name_vpath;
2561 gboolean ok;
2564 * Directory or link to directory - change directory.
2565 * Try the same for the entries on which mc_lstat() has failed.
2567 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2569 vfs_path_t *fname_vpath;
2571 fname_vpath = vfs_path_from_str (fe->fname);
2572 if (!do_cd (fname_vpath, cd_exact))
2573 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2574 vfs_path_free (fname_vpath);
2575 return 1;
2578 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2580 /* Try associated command */
2581 if (regex_command (full_name_vpath, "Open") != 0)
2583 vfs_path_free (full_name_vpath);
2584 return 1;
2587 /* Check if the file is executable */
2588 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2589 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2590 vfs_path_free (full_name_vpath);
2591 if (!ok)
2592 return 0;
2594 if (confirm_execute)
2596 if (query_dialog
2597 (_("The Midnight Commander"),
2598 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2599 return 1;
2602 if (!vfs_current_is_local ())
2604 int ret;
2605 vfs_path_t *tmp_vpath;
2607 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2608 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2609 vfs_path_free (tmp_vpath);
2610 /* We took action only if the dialog was shown or the execution
2611 * was successful */
2612 return confirm_execute || (ret == 0);
2616 char *tmp = name_quote (fe->fname, 0);
2617 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2618 g_free (tmp);
2619 shell_execute (cmd, 0);
2620 g_free (cmd);
2623 #ifdef HAVE_CHARSET
2624 mc_global.source_codepage = default_source_codepage;
2625 #endif
2627 return 1;
2630 /* --------------------------------------------------------------------------------------------- */
2632 static int
2633 do_enter (WPanel * panel)
2635 return do_enter_on_file_entry_t (selection (panel));
2638 /* --------------------------------------------------------------------------------------------- */
2640 static void
2641 chdir_other_panel (WPanel * panel)
2643 const file_entry_t *entry = &panel->dir.list[panel->selected];
2645 vfs_path_t *new_dir_vpath;
2646 char *sel_entry = NULL;
2648 if (get_other_type () != view_listing)
2650 set_display_type (get_other_index (), view_listing);
2653 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2654 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2655 else
2657 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2658 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2661 change_panel ();
2662 do_cd (new_dir_vpath, cd_exact);
2663 vfs_path_free (new_dir_vpath);
2665 if (sel_entry)
2666 try_to_select (current_panel, sel_entry);
2667 change_panel ();
2669 move_down (panel);
2672 /* --------------------------------------------------------------------------------------------- */
2674 * Make the current directory of the current panel also the current
2675 * directory of the other panel. Put the other panel to the listing
2676 * mode if needed. If the current panel is panelized, the other panel
2677 * doesn't become panelized.
2680 static void
2681 panel_sync_other (const WPanel * panel)
2683 if (get_other_type () != view_listing)
2685 set_display_type (get_other_index (), view_listing);
2688 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2690 /* try to select current filename on the other panel */
2691 if (!panel->is_panelized)
2693 try_to_select (other_panel, selection (panel)->fname);
2697 /* --------------------------------------------------------------------------------------------- */
2699 static void
2700 chdir_to_readlink (WPanel * panel)
2702 vfs_path_t *new_dir_vpath;
2703 char buffer[MC_MAXPATHLEN];
2704 int i;
2705 struct stat st;
2706 vfs_path_t *panel_fname_vpath;
2707 gboolean ok;
2709 if (get_other_type () != view_listing)
2710 return;
2712 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2713 return;
2715 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2716 if (i < 0)
2717 return;
2719 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2720 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2721 vfs_path_free (panel_fname_vpath);
2722 if (!ok)
2723 return;
2725 buffer[i] = 0;
2726 if (!S_ISDIR (st.st_mode))
2728 char *p;
2730 p = strrchr (buffer, PATH_SEP);
2731 if (p && !p[1])
2733 *p = 0;
2734 p = strrchr (buffer, PATH_SEP);
2736 if (!p)
2737 return;
2738 p[1] = 0;
2740 if (*buffer == PATH_SEP)
2741 new_dir_vpath = vfs_path_from_str (buffer);
2742 else
2743 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2745 change_panel ();
2746 do_cd (new_dir_vpath, cd_exact);
2747 vfs_path_free (new_dir_vpath);
2748 change_panel ();
2750 move_down (panel);
2753 /* --------------------------------------------------------------------------------------------- */
2755 static gsize
2756 panel_get_format_field_count (WPanel * panel)
2758 format_e *format;
2759 gsize lc_index;
2760 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2761 return lc_index;
2764 /* --------------------------------------------------------------------------------------------- */
2766 function return 0 if not found and REAL_INDEX+1 if found
2769 static gsize
2770 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2772 format_e *format;
2773 gsize lc_index;
2775 for (lc_index = 1, format = panel->format;
2776 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2777 if (format == NULL)
2778 lc_index = 0;
2780 return lc_index;
2783 /* --------------------------------------------------------------------------------------------- */
2785 static format_e *
2786 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2788 format_e *format;
2789 for (format = panel->format;
2790 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2791 return format;
2794 /* --------------------------------------------------------------------------------------------- */
2796 static const panel_field_t *
2797 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2799 const panel_field_t *pfield;
2800 format_e *format;
2802 format = panel_get_format_field_by_index (panel, lc_index);
2803 if (format == NULL)
2804 return NULL;
2805 pfield = panel_get_field_by_title (format->title);
2806 if (pfield == NULL)
2807 return NULL;
2808 if (pfield->sort_routine == NULL)
2809 return NULL;
2810 return pfield;
2813 /* --------------------------------------------------------------------------------------------- */
2815 static void
2816 panel_toggle_sort_order_prev (WPanel * panel)
2818 gsize lc_index, i;
2819 gchar *title;
2821 const panel_field_t *pfield = NULL;
2823 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
2824 lc_index = panel_get_format_field_index_by_name (panel, title);
2825 g_free (title);
2827 if (lc_index > 1)
2829 /* search for prev sortable column in panel format */
2830 for (i = lc_index - 1;
2831 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2834 if (pfield == NULL)
2836 /* Sortable field not found. Try to search in each array */
2837 for (i = panel_get_format_field_count (panel);
2838 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2841 if (pfield != NULL)
2843 panel->sort_field = pfield;
2844 panel_set_sort_order (panel, pfield);
2848 /* --------------------------------------------------------------------------------------------- */
2850 static void
2851 panel_toggle_sort_order_next (WPanel * panel)
2853 gsize lc_index, i;
2854 const panel_field_t *pfield = NULL;
2855 gsize format_field_count;
2856 gchar *title;
2858 format_field_count = panel_get_format_field_count (panel);
2859 title = panel_get_title_without_hotkey (panel->sort_field->title_hotkey);
2860 lc_index = panel_get_format_field_index_by_name (panel, title);
2861 g_free (title);
2863 if (lc_index != 0 && lc_index != format_field_count)
2865 /* search for prev sortable column in panel format */
2866 for (i = lc_index;
2867 i != format_field_count
2868 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2871 if (pfield == NULL)
2873 /* Sortable field not found. Try to search in each array */
2874 for (i = 0;
2875 i != format_field_count
2876 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2879 if (pfield != NULL)
2881 panel->sort_field = pfield;
2882 panel_set_sort_order (panel, pfield);
2886 /* --------------------------------------------------------------------------------------------- */
2888 static void
2889 panel_select_sort_order (WPanel * panel)
2891 const panel_field_t *sort_order;
2893 sort_order = sort_box (&panel->sort_info, panel->sort_field);
2894 if (sort_order != NULL)
2896 panel->sort_field = sort_order;
2897 panel_set_sort_order (panel, sort_order);
2901 /* --------------------------------------------------------------------------------------------- */
2904 * panel_content_scroll_left:
2905 * @param panel the pointer to the panel on which we operate
2907 * scroll long filename to the left (decrement scroll pointer)
2911 static void
2912 panel_content_scroll_left (WPanel * panel)
2914 if (panel->content_shift > -1)
2916 if (panel->content_shift > panel->max_shift)
2917 panel->content_shift = panel->max_shift;
2919 panel->content_shift--;
2920 show_dir (panel);
2921 paint_dir (panel);
2926 /* --------------------------------------------------------------------------------------------- */
2929 * panel_content_scroll_right:
2930 * @param panel the pointer to the panel on which we operate
2932 * scroll long filename to the right (increment scroll pointer)
2936 static void
2937 panel_content_scroll_right (WPanel * panel)
2939 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
2941 panel->content_shift++;
2942 show_dir (panel);
2943 paint_dir (panel);
2947 /* --------------------------------------------------------------------------------------------- */
2949 static void
2950 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2952 if (strcmp (panel->sort_field->id, name) != 0)
2954 const panel_field_t *sort_order;
2956 sort_order = panel_get_field_by_id (name);
2957 if (sort_order == NULL)
2958 return;
2959 panel->sort_field = sort_order;
2961 else
2962 panel->sort_info.reverse = !panel->sort_info.reverse;
2964 panel_set_sort_order (panel, panel->sort_field);
2967 /* --------------------------------------------------------------------------------------------- */
2969 * If we moved to the parent directory move the selection pointer to
2970 * the old directory name; If we leave VFS dir, remove FS specificator.
2972 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2975 static const char *
2976 get_parent_dir_name (const vfs_path_t * cwd_vpath, const vfs_path_t * lwd_vpath)
2978 size_t llen, clen;
2979 const char *p, *cwd, *lwd;
2981 llen = vfs_path_len (lwd_vpath);
2982 clen = vfs_path_len (cwd_vpath);
2984 if (llen <= clen)
2985 return NULL;
2987 cwd = vfs_path_as_str (cwd_vpath);
2988 lwd = vfs_path_as_str (lwd_vpath);
2990 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2992 if (p == NULL)
2994 p = strrchr (lwd, PATH_SEP);
2996 if ((p != NULL)
2997 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2998 && (clen == (size_t) (p - lwd)
2999 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
3000 return (p + 1);
3002 return NULL;
3005 /* skip VFS prefix */
3006 while (--p > lwd && *p != PATH_SEP)
3008 /* get last component */
3009 while (--p > lwd && *p != PATH_SEP)
3012 /* return last component */
3013 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
3016 /* --------------------------------------------------------------------------------------------- */
3017 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3019 static void
3020 subshell_chdir (const vfs_path_t * vpath)
3022 #ifdef ENABLE_SUBSHELL
3023 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3024 do_subshell_chdir (vpath, FALSE);
3025 #else /* ENABLE_SUBSHELL */
3026 (void) vpath;
3027 #endif /* ENABLE_SUBSHELL */
3030 /* --------------------------------------------------------------------------------------------- */
3032 * Changes the current directory of the panel.
3033 * Don't record change in the directory history.
3036 static gboolean
3037 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3039 vfs_path_t *olddir_vpath;
3040 /* Convert *new_path to a suitable pathname, handle ~user */
3041 if (cd_type == cd_parse_command)
3043 const vfs_path_element_t *element;
3045 element = vfs_path_get_by_index (new_dir_vpath, 0);
3046 if (strcmp (element->path, "-") == 0)
3047 new_dir_vpath = panel->lwd_vpath;
3050 if (mc_chdir (new_dir_vpath) == -1)
3051 return FALSE;
3053 /* Success: save previous directory, shutdown status of previous dir */
3054 olddir_vpath = vfs_path_clone (panel->cwd_vpath);
3055 panel_set_lwd (panel, panel->cwd_vpath);
3056 input_free_completions (cmdline);
3058 vfs_path_free (panel->cwd_vpath);
3059 vfs_setup_cwd ();
3060 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3062 vfs_release_path (olddir_vpath);
3064 subshell_chdir (panel->cwd_vpath);
3066 /* Reload current panel */
3067 panel_clean_dir (panel);
3069 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
3070 &panel->sort_info, panel->filter);
3071 try_to_select (panel, get_parent_dir_name (panel->cwd_vpath, olddir_vpath));
3073 load_hint (0);
3074 panel->dirty = 1;
3075 update_xterm_title_path ();
3077 vfs_path_free (olddir_vpath);
3079 return TRUE;
3082 /* --------------------------------------------------------------------------------------------- */
3084 static void
3085 directory_history_next (WPanel * panel)
3087 gboolean ok;
3091 GList *next;
3093 ok = TRUE;
3094 next = g_list_next (panel->dir_history_current);
3095 if (next != NULL)
3097 vfs_path_t *data_vpath;
3099 data_vpath = vfs_path_from_str ((char *) next->data);
3100 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3101 vfs_path_free (data_vpath);
3102 panel->dir_history_current = next;
3104 /* skip directories that present in history but absent in file system */
3106 while (!ok);
3109 /* --------------------------------------------------------------------------------------------- */
3111 static void
3112 directory_history_prev (WPanel * panel)
3114 gboolean ok;
3118 GList *prev;
3120 ok = TRUE;
3121 prev = g_list_previous (panel->dir_history_current);
3122 if (prev != NULL)
3124 vfs_path_t *data_vpath;
3126 data_vpath = vfs_path_from_str ((char *) prev->data);
3127 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3128 vfs_path_free (data_vpath);
3129 panel->dir_history_current = prev;
3131 /* skip directories that present in history but absent in file system */
3133 while (!ok);
3136 /* --------------------------------------------------------------------------------------------- */
3138 static void
3139 directory_history_list (WPanel * panel)
3141 char *s;
3142 gboolean ok = FALSE;
3143 size_t pos;
3145 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3147 s = history_show (&panel->dir_history, WIDGET (panel), pos);
3148 if (s != NULL)
3150 vfs_path_t *s_vpath;
3152 s_vpath = vfs_path_from_str (s);
3153 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3154 if (ok)
3155 directory_history_add (panel, panel->cwd_vpath);
3156 else
3157 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3158 vfs_path_free (s_vpath);
3159 g_free (s);
3162 if (!ok)
3164 /* Since history is fully modified in history_show(), panel->dir_history actually
3165 * points to the invalid place. Try restore current postition here. */
3167 size_t i;
3169 panel->dir_history_current = panel->dir_history;
3171 for (i = 0; i <= pos; i++)
3173 GList *prev;
3175 prev = g_list_previous (panel->dir_history_current);
3176 if (prev == NULL)
3177 break;
3179 panel->dir_history_current = prev;
3184 /* --------------------------------------------------------------------------------------------- */
3186 static cb_ret_t
3187 panel_execute_cmd (WPanel * panel, unsigned long command)
3189 int res = MSG_HANDLED;
3191 if (command != CK_Search)
3192 stop_search (panel);
3195 switch (command)
3197 case CK_Up:
3198 case CK_Down:
3199 case CK_Left:
3200 case CK_Right:
3201 case CK_Bottom:
3202 case CK_Top:
3203 case CK_PageDown:
3204 case CK_PageUp:
3205 /* reset state of marks flag */
3206 state_mark = -1;
3207 break;
3209 switch (command)
3211 case CK_PanelOtherCd:
3212 chdir_other_panel (panel);
3213 break;
3214 case CK_PanelOtherCdLink:
3215 chdir_to_readlink (panel);
3216 break;
3217 case CK_CopySingle:
3218 copy_cmd_local ();
3219 break;
3220 case CK_DeleteSingle:
3221 delete_cmd_local ();
3222 break;
3223 case CK_Enter:
3224 do_enter (panel);
3225 break;
3226 case CK_ViewRaw:
3227 view_raw_cmd ();
3228 break;
3229 case CK_EditNew:
3230 edit_cmd_new ();
3231 break;
3232 case CK_MoveSingle:
3233 rename_cmd_local ();
3234 break;
3235 case CK_SelectInvert:
3236 select_invert_cmd ();
3237 break;
3238 case CK_Select:
3239 select_cmd ();
3240 break;
3241 case CK_Unselect:
3242 unselect_cmd ();
3243 break;
3244 case CK_PageDown:
3245 next_page (panel);
3246 break;
3247 case CK_PageUp:
3248 prev_page (panel);
3249 break;
3250 case CK_CdChild:
3251 goto_child_dir (panel);
3252 break;
3253 case CK_CdParent:
3254 goto_parent_dir (panel);
3255 break;
3256 case CK_History:
3257 directory_history_list (panel);
3258 break;
3259 case CK_HistoryNext:
3260 directory_history_next (panel);
3261 break;
3262 case CK_HistoryPrev:
3263 directory_history_prev (panel);
3264 break;
3265 case CK_BottomOnScreen:
3266 goto_bottom_file (panel);
3267 break;
3268 case CK_MiddleOnScreen:
3269 goto_middle_file (panel);
3270 break;
3271 case CK_TopOnScreen:
3272 goto_top_file (panel);
3273 break;
3274 case CK_Mark:
3275 mark_file (panel);
3276 break;
3277 case CK_MarkUp:
3278 mark_file_up (panel);
3279 break;
3280 case CK_MarkDown:
3281 mark_file_down (panel);
3282 break;
3283 case CK_MarkLeft:
3284 mark_file_left (panel);
3285 break;
3286 case CK_MarkRight:
3287 mark_file_right (panel);
3288 break;
3289 case CK_CdParentSmart:
3290 res = force_maybe_cd ();
3291 break;
3292 case CK_Up:
3293 move_up (panel);
3294 break;
3295 case CK_Down:
3296 move_down (panel);
3297 break;
3298 case CK_Left:
3299 res = move_left (panel);
3300 break;
3301 case CK_Right:
3302 res = move_right (panel);
3303 break;
3304 case CK_Bottom:
3305 move_end (panel);
3306 break;
3307 case CK_Top:
3308 move_home (panel);
3309 break;
3310 #ifdef HAVE_CHARSET
3311 case CK_SelectCodepage:
3312 panel_change_encoding (panel);
3313 break;
3314 #endif
3315 case CK_ScrollLeft:
3316 panel_content_scroll_left (panel);
3317 break;
3318 case CK_ScrollRight:
3319 panel_content_scroll_right (panel);
3320 break;
3321 case CK_Search:
3322 start_search (panel);
3323 break;
3324 case CK_SearchStop:
3325 break;
3326 case CK_PanelOtherSync:
3327 panel_sync_other (panel);
3328 break;
3329 case CK_Sort:
3330 panel_select_sort_order (panel);
3331 break;
3332 case CK_SortPrev:
3333 panel_toggle_sort_order_prev (panel);
3334 break;
3335 case CK_SortNext:
3336 panel_toggle_sort_order_next (panel);
3337 break;
3338 case CK_SortReverse:
3339 panel->sort_info.reverse = !panel->sort_info.reverse;
3340 panel_set_sort_order (panel, panel->sort_field);
3341 break;
3342 case CK_SortByName:
3343 panel_set_sort_type_by_id (panel, "name");
3344 break;
3345 case CK_SortByExt:
3346 panel_set_sort_type_by_id (panel, "extension");
3347 break;
3348 case CK_SortBySize:
3349 panel_set_sort_type_by_id (panel, "size");
3350 break;
3351 case CK_SortByMTime:
3352 panel_set_sort_type_by_id (panel, "mtime");
3353 break;
3355 return res;
3358 /* --------------------------------------------------------------------------------------------- */
3360 static cb_ret_t
3361 panel_key (WPanel * panel, int key)
3363 size_t i;
3365 if (is_abort_char (key))
3367 stop_search (panel);
3368 return MSG_HANDLED;
3371 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3373 do_search (panel, key);
3374 return MSG_HANDLED;
3377 for (i = 0; panel_map[i].key != 0; i++)
3378 if (key == panel_map[i].key)
3379 return panel_execute_cmd (panel, panel_map[i].command);
3381 if (panels_options.torben_fj_mode && key == ALT ('h'))
3383 goto_middle_file (panel);
3384 return MSG_HANDLED;
3387 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3389 start_search (panel);
3390 do_search (panel, key);
3391 return MSG_HANDLED;
3394 return MSG_NOT_HANDLED;
3397 /* --------------------------------------------------------------------------------------------- */
3399 static cb_ret_t
3400 panel_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3402 WPanel *panel = (WPanel *) w;
3403 WButtonBar *bb;
3405 switch (msg)
3407 case MSG_INIT:
3408 /* subscribe to "history_load" event */
3409 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3410 /* subscribe to "history_save" event */
3411 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3412 return MSG_HANDLED;
3414 case MSG_DRAW:
3415 /* Repaint everything, including frame and separator */
3416 paint_frame (panel); /* including show_dir */
3417 paint_dir (panel);
3418 mini_info_separator (panel);
3419 display_mini_info (panel);
3420 panel->dirty = 0;
3421 return MSG_HANDLED;
3423 case MSG_FOCUS:
3424 state_mark = -1;
3425 current_panel = panel;
3426 panel->active = 1;
3427 if (mc_chdir (panel->cwd_vpath) != 0)
3429 char *cwd;
3431 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3432 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3433 cwd, unix_error_string (errno));
3434 g_free (cwd);
3436 else
3437 subshell_chdir (panel->cwd_vpath);
3439 update_xterm_title_path ();
3440 select_item (panel);
3441 show_dir (panel);
3442 paint_dir (panel);
3443 panel->dirty = 0;
3445 bb = find_buttonbar (w->owner);
3446 midnight_set_buttonbar (bb);
3447 widget_redraw (WIDGET (bb));
3448 return MSG_HANDLED;
3450 case MSG_UNFOCUS:
3451 /* Janne: look at this for the multiple panel options */
3452 stop_search (panel);
3453 panel->active = 0;
3454 show_dir (panel);
3455 unselect_item (panel);
3456 return MSG_HANDLED;
3458 case MSG_KEY:
3459 return panel_key (panel, parm);
3461 case MSG_ACTION:
3462 return panel_execute_cmd (panel, parm);
3464 case MSG_DESTROY:
3465 /* unsubscribe from "history_load" event */
3466 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3467 /* unsubscribe from "history_save" event */
3468 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3469 panel_destroy (panel);
3470 free_my_statfs ();
3471 return MSG_HANDLED;
3473 default:
3474 return widget_default_callback (w, sender, msg, parm, data);
3478 /* --------------------------------------------------------------------------------------------- */
3479 /* */
3480 /* Panel mouse events support routines */
3481 /* */
3483 static void
3484 mouse_toggle_mark (WPanel * panel)
3486 do_mark_file (panel, MARK_DONT_MOVE);
3487 mouse_marking = selection (panel)->f.marked;
3488 mouse_mark_panel = current_panel;
3491 /* --------------------------------------------------------------------------------------------- */
3493 static void
3494 mouse_set_mark (WPanel * panel)
3497 if (mouse_mark_panel == panel)
3499 if (mouse_marking && !(selection (panel)->f.marked))
3500 do_mark_file (panel, MARK_DONT_MOVE);
3501 else if (!mouse_marking && (selection (panel)->f.marked))
3502 do_mark_file (panel, MARK_DONT_MOVE);
3506 /* --------------------------------------------------------------------------------------------- */
3508 static gboolean
3509 mark_if_marking (WPanel * panel, Gpm_Event * event)
3511 if ((event->buttons & GPM_B_RIGHT) != 0)
3513 if ((event->type & GPM_DOWN) != 0)
3514 mouse_toggle_mark (panel);
3515 else
3516 mouse_set_mark (panel);
3517 return TRUE;
3519 return FALSE;
3522 /* --------------------------------------------------------------------------------------------- */
3523 /** Determine which column was clicked, and sort the panel on
3524 * that column, or reverse sort on that column if already
3525 * sorted on that column.
3528 static void
3529 mouse_sort_col (WPanel * panel, int x)
3531 int i;
3532 const char *lc_sort_name = NULL;
3533 panel_field_t *col_sort_format = NULL;
3534 format_e *format;
3536 for (i = 0, format = panel->format; format != NULL; format = format->next)
3538 i += format->field_len;
3539 if (x < i + 1)
3541 /* found column */
3542 lc_sort_name = format->title;
3543 break;
3547 if (lc_sort_name == NULL)
3548 return;
3550 for (i = 0; panel_fields[i].id != NULL; i++)
3552 char *title;
3554 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3555 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3557 col_sort_format = &panel_fields[i];
3558 g_free (title);
3559 break;
3561 g_free (title);
3564 if (col_sort_format == NULL)
3565 return;
3567 if (panel->sort_field == col_sort_format)
3569 /* reverse the sort if clicked column is already the sorted column */
3570 panel->sort_info.reverse = !panel->sort_info.reverse;
3572 else
3574 /* new sort is forced to be ascending */
3575 panel->sort_info.reverse = FALSE;
3577 panel_set_sort_order (panel, col_sort_format);
3581 /* --------------------------------------------------------------------------------------------- */
3583 * Mouse callback of the panel minus repainting.
3585 static int
3586 panel_event (Gpm_Event * event, void *data)
3588 WPanel *panel = (WPanel *) data;
3589 Widget *w = WIDGET (data);
3591 const int lines = llines (panel);
3592 const gboolean is_active = widget_is_active (panel);
3593 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3594 Gpm_Event local;
3596 if (!mouse_global_in_widget (event, WIDGET (data)))
3597 return MOU_UNHANDLED;
3599 local = mouse_get_local (event, w);
3601 /* 1st line */
3602 if (local.y == 1)
3604 /* "<" button */
3605 if (mouse_down && local.x == 2)
3607 directory_history_prev (panel);
3608 goto finish;
3611 /* ">" button */
3612 if (mouse_down && local.x == w->cols - 1)
3614 directory_history_next (panel);
3615 goto finish;
3618 /* "^" button */
3619 if (mouse_down && local.x >= w->cols - 4 && local.x <= w->cols - 2)
3621 directory_history_list (panel);
3622 goto finish;
3625 /* "." button show/hide hidden files */
3626 if (mouse_down && local.x == w->cols - 5)
3628 send_message (midnight_dlg, NULL, MSG_ACTION, CK_ShowHidden, NULL);
3629 goto finish;
3632 /* no other events on 1st line */
3633 return MOU_UNHANDLED;
3636 /* sort on clicked column; don't handle wheel events */
3637 if (mouse_down && (local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && local.y == 2)
3639 mouse_sort_col (panel, local.x);
3640 goto finish;
3643 /* Mouse wheel events */
3644 if (mouse_down && (local.buttons & GPM_B_UP) != 0)
3646 if (is_active)
3648 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3649 prev_page (panel);
3650 else /* We are in first page */
3651 move_up (panel);
3653 goto finish;
3656 if (mouse_down && (local.buttons & GPM_B_DOWN) != 0)
3658 if (is_active)
3660 if (panels_options.mouse_move_pages
3661 && (panel->top_file + ITEMS (panel) < panel->dir.len))
3662 next_page (panel);
3663 else /* We are in last page */
3664 move_down (panel);
3666 goto finish;
3669 local.y -= 2;
3670 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
3672 int my_index;
3674 if (!is_active)
3675 change_panel ();
3677 if (panel->top_file + local.y > panel->dir.len)
3678 my_index = panel->dir.len - 1;
3679 else
3681 my_index = panel->top_file + local.y - 1;
3682 if (panel->split && (local.x > (w->cols - 2) / 2))
3683 my_index += llines (panel);
3685 if (my_index >= panel->dir.len)
3686 my_index = panel->dir.len - 1;
3689 if (my_index != panel->selected)
3691 unselect_item (panel);
3692 panel->selected = my_index;
3693 select_item (panel);
3696 /* This one is new */
3697 mark_if_marking (panel, &local);
3699 else if ((local.type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE) &&
3700 local.y > 0 && local.y <= lines)
3701 do_enter (panel);
3703 finish:
3704 if (panel->dirty)
3705 widget_redraw (w);
3707 return MOU_NORMAL;
3710 /* --------------------------------------------------------------------------------------------- */
3712 static void
3713 reload_panelized (WPanel * panel)
3715 int i, j;
3716 dir_list *list = &panel->dir;
3718 if (panel != current_panel)
3719 (void) mc_chdir (panel->cwd_vpath);
3721 for (i = 0, j = 0; i < panel->dir.len; i++)
3723 vfs_path_t *vpath;
3725 if (list->list[i].f.marked)
3727 /* Unmark the file in advance. In case the following mc_lstat
3728 * fails we are done, else we have to mark the file again
3729 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3730 * IMO that's the best way to update the panel's summary status
3731 * -- Norbert
3733 do_file_mark (panel, i, 0);
3735 vpath = vfs_path_from_str (list->list[i].fname);
3736 if (mc_lstat (vpath, &list->list[i].st) != 0)
3737 g_free (list->list[i].fname);
3738 else
3740 if (list->list[i].f.marked)
3741 do_file_mark (panel, i, 1);
3742 if (j != i)
3743 list->list[j] = list->list[i];
3744 j++;
3746 vfs_path_free (vpath);
3748 if (j == 0)
3749 dir_list_init (list);
3750 else
3751 list->len = j;
3753 if (panel != current_panel)
3754 (void) mc_chdir (current_panel->cwd_vpath);
3757 /* --------------------------------------------------------------------------------------------- */
3759 static void
3760 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3762 gboolean free_pointer;
3763 char *my_current_file = NULL;
3765 if ((flags & UP_RELOAD) != 0)
3767 panel->is_panelized = FALSE;
3768 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3769 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3772 /* If current_file == -1 (an invalid pointer) then preserve selection */
3773 free_pointer = current_file == UP_KEEPSEL;
3775 if (free_pointer)
3777 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3778 current_file = my_current_file;
3781 if (panel->is_panelized)
3782 reload_panelized (panel);
3783 else
3784 panel_reload (panel);
3786 try_to_select (panel, current_file);
3787 panel->dirty = 1;
3789 if (free_pointer)
3790 g_free (my_current_file);
3793 /* --------------------------------------------------------------------------------------------- */
3795 static void
3796 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3798 if (get_display_type (which) == view_listing)
3800 WPanel *panel;
3802 panel = (WPanel *) get_panel_widget (which);
3803 if (panel->is_panelized)
3804 flags &= ~UP_RELOAD;
3805 update_one_panel_widget (panel, flags, current_file);
3809 /* --------------------------------------------------------------------------------------------- */
3811 static void
3812 do_select (WPanel * panel, int i)
3814 if (i != panel->selected)
3816 panel->dirty = 1;
3817 panel->selected = i;
3818 panel->top_file = panel->selected - (WIDGET (panel)->lines - 2) / 2;
3819 if (panel->top_file < 0)
3820 panel->top_file = 0;
3824 /* --------------------------------------------------------------------------------------------- */
3826 static void
3827 do_try_to_select (WPanel * panel, const char *name)
3829 int i;
3830 char *subdir;
3832 if (!name)
3834 do_select (panel, 0);
3835 return;
3838 /* We only want the last component of the directory,
3839 * and from this only the name without suffix.
3840 * Cut prefix if the panel is not panelized */
3842 if (panel->is_panelized)
3843 subdir = vfs_strip_suffix_from_filename (name);
3844 else
3845 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3847 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3848 for (i = 0; i < panel->dir.len; i++)
3850 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3852 do_select (panel, i);
3853 g_free (subdir);
3854 return;
3858 /* Try to select a file near the file that is missing */
3859 if (panel->selected >= panel->dir.len)
3860 do_select (panel, panel->dir.len - 1);
3861 g_free (subdir);
3864 /* --------------------------------------------------------------------------------------------- */
3866 /* event callback */
3867 static gboolean
3868 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3869 gpointer init_data, gpointer data)
3871 (void) event_group_name;
3872 (void) event_name;
3873 (void) init_data;
3874 (void) data;
3876 update_panels (UP_RELOAD, UP_KEEPSEL);
3878 return TRUE;
3881 /* --------------------------------------------------------------------------------------------- */
3883 /* event callback */
3884 static gboolean
3885 panel_save_current_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3886 gpointer init_data, gpointer data)
3888 (void) event_group_name;
3889 (void) event_name;
3890 (void) init_data;
3891 (void) data;
3893 if (current_panel->marked == 0)
3894 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3895 (gpointer) selection (current_panel)->fname);
3896 else
3898 int i;
3899 gboolean first = TRUE;
3900 char *flist = NULL;
3902 for (i = 0; i < current_panel->dir.len; i++)
3903 if (current_panel->dir.list[i].f.marked != 0)
3904 { /* Skip the unmarked ones */
3905 if (first)
3907 flist = g_strdup (current_panel->dir.list[i].fname);
3908 first = FALSE;
3910 else
3912 /* Add empty lines after the file */
3913 char *tmp;
3915 tmp =
3916 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3917 g_free (flist);
3918 flist = tmp;
3922 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3923 g_free (flist);
3925 return TRUE;
3928 /* --------------------------------------------------------------------------------------------- */
3930 static vfs_path_t *
3931 panel_recursive_cd_to_parent (const vfs_path_t * vpath)
3933 vfs_path_t *cwd_vpath;
3935 cwd_vpath = vfs_path_clone (vpath);
3937 while (mc_chdir (cwd_vpath) < 0)
3939 const char *panel_cwd_path;
3940 vfs_path_t *tmp_vpath;
3942 /* check if path contains only '/' */
3943 panel_cwd_path = vfs_path_as_str (cwd_vpath);
3944 if (panel_cwd_path[0] == PATH_SEP && panel_cwd_path[1] == '\0')
3945 return NULL;
3947 tmp_vpath = vfs_path_vtokens_get (cwd_vpath, 0, -1);
3948 vfs_path_free (cwd_vpath);
3949 cwd_vpath = vfs_path_build_filename (PATH_SEP_STR, vfs_path_as_str (tmp_vpath), NULL);
3950 vfs_path_free (tmp_vpath);
3953 return cwd_vpath;
3956 /* --------------------------------------------------------------------------------------------- */
3957 /*** public functions ****************************************************************************/
3958 /* --------------------------------------------------------------------------------------------- */
3960 void
3961 try_to_select (WPanel * panel, const char *name)
3963 do_try_to_select (panel, name);
3964 select_item (panel);
3967 /* --------------------------------------------------------------------------------------------- */
3969 void
3970 panel_clean_dir (WPanel * panel)
3972 panel->top_file = 0;
3973 panel->selected = 0;
3974 panel->marked = 0;
3975 panel->dirs_marked = 0;
3976 panel->total = 0;
3977 panel->searching = FALSE;
3978 panel->is_panelized = FALSE;
3979 panel->dirty = 1;
3980 panel->content_shift = -1;
3981 panel->max_shift = -1;
3983 dir_list_clean (&panel->dir);
3986 /* --------------------------------------------------------------------------------------------- */
3988 * Set Up panel's current dir object
3990 * @param panel panel object
3991 * @param path_str string contain path
3994 void
3995 panel_set_cwd (WPanel * panel, const vfs_path_t * vpath)
3997 if (vpath != panel->cwd_vpath) /* check if new vpath is not the panel->cwd_vpath object */
3999 vfs_path_free (panel->cwd_vpath);
4000 panel->cwd_vpath = vfs_path_clone (vpath);
4004 /* --------------------------------------------------------------------------------------------- */
4006 * Set Up panel's last working dir object
4008 * @param panel panel object
4009 * @param path_str string contain path
4012 void
4013 panel_set_lwd (WPanel * panel, const vfs_path_t * vpath)
4015 if (vpath != panel->lwd_vpath) /* check if new vpath is not the panel->lwd_vpath object */
4017 vfs_path_free (panel->lwd_vpath);
4018 panel->lwd_vpath = vfs_path_clone (vpath);
4022 /* --------------------------------------------------------------------------------------------- */
4023 /** Panel creation.
4024 * @param panel_name the name of the panel for setup retieving
4025 * @return new instance of WPanel
4028 WPanel *
4029 panel_new (const char *panel_name)
4031 return panel_new_with_dir (panel_name, NULL);
4034 /* --------------------------------------------------------------------------------------------- */
4036 * Panel creation for specified directory.
4038 * @param panel_name specifies the name of the panel for setup retieving
4039 * @param wpath the path of working panel directory. If path is NULL then panel will be created
4040 * for current directory
4042 * @return new instance of WPanel
4045 WPanel *
4046 panel_new_with_dir (const char *panel_name, const vfs_path_t * vpath)
4048 WPanel *panel;
4049 Widget *w;
4050 char *section;
4051 int i, err;
4052 char *curdir = NULL;
4054 panel = g_new0 (WPanel, 1);
4055 w = WIDGET (panel);
4056 /* No know sizes of the panel at startup */
4057 widget_init (w, 0, 0, 0, 0, panel_callback, panel_event);
4058 /* We do not want the cursor */
4059 widget_want_cursor (w, FALSE);
4061 if (vpath != NULL)
4063 curdir = _vfs_get_cwd ();
4064 panel_set_cwd (panel, vpath);
4066 else
4068 vfs_setup_cwd ();
4069 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4072 panel_set_lwd (panel, vfs_get_raw_current_dir ());
4074 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4075 /* directories history will be get later */
4077 panel->dir.size = DIR_LIST_MIN_SIZE;
4078 panel->dir.list = g_new (file_entry_t, panel->dir.size);
4079 panel->dir.len = 0;
4080 panel->active = 0;
4081 panel->filter = 0;
4082 panel->split = 0;
4083 panel->top_file = 0;
4084 panel->selected = 0;
4085 panel->marked = 0;
4086 panel->total = 0;
4087 panel->dirty = 1;
4088 panel->searching = FALSE;
4089 panel->dirs_marked = 0;
4090 panel->is_panelized = FALSE;
4091 panel->format = 0;
4092 panel->status_format = 0;
4093 panel->format_modified = 1;
4094 panel->content_shift = -1;
4095 panel->max_shift = -1;
4097 panel->panel_name = g_strdup (panel_name);
4098 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4100 #ifdef HAVE_CHARSET
4101 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4102 #endif
4104 for (i = 0; i < LIST_TYPES; i++)
4105 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4107 panel->search_buffer[0] = '\0';
4108 panel->prev_search_buffer[0] = '\0';
4109 panel->frame_size = frame_half;
4111 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4112 if (!mc_config_has_group (mc_main_config, section))
4114 g_free (section);
4115 section = g_strdup (panel->panel_name);
4117 panel_load_setup (panel, section);
4118 g_free (section);
4120 /* Load format strings */
4121 err = set_panel_formats (panel);
4122 if (err != 0)
4123 set_panel_formats (panel);
4125 #ifdef HAVE_CHARSET
4127 const vfs_path_element_t *path_element;
4129 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4130 if (path_element->encoding != NULL)
4131 panel->codepage = get_codepage_index (path_element->encoding);
4133 #endif
4135 if (mc_chdir (panel->cwd_vpath) != 0)
4137 #ifdef HAVE_CHARSET
4138 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4139 #endif
4140 vfs_setup_cwd ();
4141 vfs_path_free (panel->cwd_vpath);
4142 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4145 /* Load the default format */
4146 dir_list_load (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4147 &panel->sort_info, panel->filter);
4149 /* Restore old right path */
4150 if (curdir != NULL)
4152 vfs_path_t *tmp_vpath;
4154 tmp_vpath = vfs_path_from_str (curdir);
4155 err = mc_chdir (tmp_vpath);
4156 vfs_path_free (tmp_vpath);
4158 g_free (curdir);
4160 return panel;
4163 /* --------------------------------------------------------------------------------------------- */
4165 void
4166 panel_reload (WPanel * panel)
4168 struct stat current_stat;
4169 vfs_path_t *cwd_vpath;
4171 if (panels_options.fast_reload && stat (vfs_path_as_str (panel->cwd_vpath), &current_stat) == 0
4172 && current_stat.st_ctime == panel->dir_stat.st_ctime
4173 && current_stat.st_mtime == panel->dir_stat.st_mtime)
4174 return;
4176 cwd_vpath = panel_recursive_cd_to_parent (panel->cwd_vpath);
4177 vfs_path_free (panel->cwd_vpath);
4179 if (cwd_vpath == NULL)
4181 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4182 panel_clean_dir (panel);
4183 dir_list_init (&panel->dir);
4184 return;
4187 panel->cwd_vpath = cwd_vpath;
4188 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4189 show_dir (panel);
4191 dir_list_reload (&panel->dir, panel->cwd_vpath, panel->sort_field->sort_routine,
4192 &panel->sort_info, panel->filter);
4194 panel->dirty = 1;
4195 if (panel->selected >= panel->dir.len)
4196 do_select (panel, panel->dir.len - 1);
4198 recalculate_panel_summary (panel);
4201 /* --------------------------------------------------------------------------------------------- */
4202 /* Switches the panel to the mode specified in the format */
4203 /* Seting up both format and status string. Return: 0 - on success; */
4204 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4207 set_panel_formats (WPanel * p)
4209 format_e *form;
4210 char *err = NULL;
4211 int retcode = 0;
4213 form = use_display_format (p, panel_format (p), &err, 0);
4215 if (err != NULL)
4217 g_free (err);
4218 retcode = 1;
4220 else
4222 delete_format (p->format);
4223 p->format = form;
4226 if (panels_options.show_mini_info)
4228 form = use_display_format (p, mini_status_format (p), &err, 1);
4230 if (err != NULL)
4232 g_free (err);
4233 retcode += 2;
4235 else
4237 delete_format (p->status_format);
4238 p->status_format = form;
4242 panel_format_modified (p);
4243 panel_update_cols (WIDGET (p), p->frame_size);
4245 if (retcode)
4246 message (D_ERROR, _("Warning"),
4247 _("User supplied format looks invalid, reverting to default."));
4248 if (retcode & 0x01)
4250 g_free (p->user_format);
4251 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4253 if (retcode & 0x02)
4255 g_free (p->user_status_format[p->list_type]);
4256 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4259 return retcode;
4262 /* --------------------------------------------------------------------------------------------- */
4264 void
4265 panel_update_cols (Widget * widget, panel_display_t frame_size)
4267 int cols, origin;
4269 /* don't touch panel if it is not in dialog yet */
4270 /* if panel is not in dialog it is not in widgets list
4271 and cannot be compared with get_panel_widget() result */
4272 if (widget->owner == NULL)
4273 return;
4275 if (panels_layout.horizontal_split)
4277 widget->cols = COLS;
4278 return;
4281 if (frame_size == frame_full)
4283 cols = COLS;
4284 origin = 0;
4286 else if (widget == get_panel_widget (0))
4288 cols = panels_layout.left_panel_size;
4289 origin = 0;
4291 else
4293 cols = COLS - panels_layout.left_panel_size;
4294 origin = panels_layout.left_panel_size;
4297 widget->cols = cols;
4298 widget->x = origin;
4301 /* --------------------------------------------------------------------------------------------- */
4303 /* Select current item and readjust the panel */
4304 void
4305 select_item (WPanel * panel)
4307 /* Although currently all over the code we set the selection and
4308 top file to decent values before calling select_item, I could
4309 forget it someday, so it's better to do the actual fitting here */
4311 if (panel->selected < 0)
4312 panel->selected = 0;
4314 if (panel->selected > panel->dir.len - 1)
4315 panel->selected = panel->dir.len - 1;
4317 adjust_top_file (panel);
4319 panel->dirty = 1;
4321 execute_hooks (select_file_hook);
4324 /* --------------------------------------------------------------------------------------------- */
4325 /** Clears all files in the panel, used only when one file was marked */
4326 void
4327 unmark_files (WPanel * panel)
4329 int i;
4331 if (!panel->marked)
4332 return;
4333 for (i = 0; i < panel->dir.len; i++)
4334 file_mark (panel, i, 0);
4336 panel->dirs_marked = 0;
4337 panel->marked = 0;
4338 panel->total = 0;
4341 /* --------------------------------------------------------------------------------------------- */
4342 /** Recalculate the panels summary information, used e.g. when marked
4343 files might have been removed by an external command */
4345 void
4346 recalculate_panel_summary (WPanel * panel)
4348 int i;
4350 panel->marked = 0;
4351 panel->dirs_marked = 0;
4352 panel->total = 0;
4354 for (i = 0; i < panel->dir.len; i++)
4355 if (panel->dir.list[i].f.marked)
4357 /* do_file_mark will return immediately if newmark == oldmark.
4358 So we have to first unmark it to get panel's summary information
4359 updated. (Norbert) */
4360 panel->dir.list[i].f.marked = 0;
4361 do_file_mark (panel, i, 1);
4365 /* --------------------------------------------------------------------------------------------- */
4366 /** This routine marks a file or a directory */
4368 void
4369 do_file_mark (WPanel * panel, int idx, int mark)
4371 if (panel->dir.list[idx].f.marked == mark)
4372 return;
4374 /* Only '..' can't be marked, '.' isn't visible */
4375 if (DIR_IS_DOTDOT (panel->dir.list[idx].fname))
4376 return;
4378 file_mark (panel, idx, mark);
4379 if (panel->dir.list[idx].f.marked)
4381 panel->marked++;
4382 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4384 if (panel->dir.list[idx].f.dir_size_computed)
4385 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4386 panel->dirs_marked++;
4388 else
4389 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4390 set_colors (panel);
4392 else
4394 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4396 if (panel->dir.list[idx].f.dir_size_computed)
4397 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4398 panel->dirs_marked--;
4400 else
4401 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4402 panel->marked--;
4406 /* --------------------------------------------------------------------------------------------- */
4408 * Changes the current directory of the panel.
4409 * Record change in the directory history.
4411 gboolean
4412 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4414 gboolean r;
4416 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4417 if (r)
4418 directory_history_add (panel, panel->cwd_vpath);
4419 return r;
4422 /* --------------------------------------------------------------------------------------------- */
4424 void
4425 file_mark (WPanel * panel, int lc_index, int val)
4427 if (panel->dir.list[lc_index].f.marked != val)
4429 panel->dir.list[lc_index].f.marked = val;
4430 panel->dirty = 1;
4434 /* --------------------------------------------------------------------------------------------- */
4436 void
4437 panel_re_sort (WPanel * panel)
4439 char *filename;
4440 int i;
4442 if (panel == NULL)
4443 return;
4445 filename = g_strdup (selection (panel)->fname);
4446 unselect_item (panel);
4447 dir_list_sort (&panel->dir, panel->sort_field->sort_routine, &panel->sort_info);
4448 panel->selected = -1;
4449 for (i = panel->dir.len; i != 0; i--)
4451 if (strcmp (panel->dir.list[i - 1].fname, filename) == 0)
4453 panel->selected = i - 1;
4454 break;
4457 g_free (filename);
4458 panel->top_file = panel->selected - ITEMS (panel) / 2;
4459 select_item (panel);
4460 panel->dirty = 1;
4463 /* --------------------------------------------------------------------------------------------- */
4465 void
4466 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4468 if (sort_order == NULL)
4469 return;
4471 panel->sort_field = sort_order;
4473 /* The directory is already sorted, we have to load the unsorted stuff */
4474 if (sort_order->sort_routine == (GCompareFunc) unsorted)
4476 char *current_file;
4478 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4479 panel_reload (panel);
4480 try_to_select (panel, current_file);
4481 g_free (current_file);
4483 panel_re_sort (panel);
4486 /* --------------------------------------------------------------------------------------------- */
4488 #ifdef HAVE_CHARSET
4491 * Change panel encoding.
4492 * @param panel WPanel object
4495 void
4496 panel_change_encoding (WPanel * panel)
4498 const char *encoding = NULL;
4499 char *errmsg;
4500 int r;
4502 r = select_charset (-1, -1, panel->codepage, FALSE);
4504 if (r == SELECT_CHARSET_CANCEL)
4505 return; /* Cancel */
4507 panel->codepage = r;
4509 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4511 /* No translation */
4512 vfs_path_t *cd_path_vpath;
4514 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4515 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4516 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4517 show_dir (panel);
4518 vfs_path_free (cd_path_vpath);
4519 return;
4522 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4523 if (errmsg != NULL)
4525 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4526 g_free (errmsg);
4527 return;
4530 encoding = get_codepage_id (panel->codepage);
4531 if (encoding != NULL)
4533 vfs_path_change_encoding (panel->cwd_vpath, encoding);
4535 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4536 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""),
4537 vfs_path_as_str (panel->cwd_vpath));
4541 /* --------------------------------------------------------------------------------------------- */
4544 * Remove encode info from last path element.
4547 vfs_path_t *
4548 remove_encoding_from_path (const vfs_path_t * vpath)
4550 vfs_path_t *ret_vpath;
4551 GString *tmp_conv;
4552 int indx;
4554 ret_vpath = vfs_path_new ();
4556 tmp_conv = g_string_new ("");
4558 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4560 GIConv converter;
4561 vfs_path_element_t *path_element;
4563 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4565 if (path_element->encoding == NULL)
4567 vfs_path_add_element (ret_vpath, path_element);
4568 continue;
4571 converter = str_crt_conv_to (path_element->encoding);
4572 if (converter == INVALID_CONV)
4574 vfs_path_add_element (ret_vpath, path_element);
4575 continue;
4578 g_free (path_element->encoding);
4579 path_element->encoding = NULL;
4581 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4583 g_free (path_element->path);
4584 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4586 g_string_set_size (tmp_conv, 0);
4588 str_close_conv (converter);
4589 str_close_conv (path_element->dir.converter);
4590 path_element->dir.converter = INVALID_CONV;
4591 vfs_path_add_element (ret_vpath, path_element);
4593 g_string_free (tmp_conv, TRUE);
4594 return ret_vpath;
4596 #endif /* HAVE_CHARSET */
4598 /* --------------------------------------------------------------------------------------------- */
4601 * This routine reloads the directory in both panels. It tries to
4602 * select current_file in current_panel and other_file in other_panel.
4603 * If current_file == -1 then it automatically sets current_file and
4604 * other_file to the currently selected files in the panels.
4606 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4607 * will not reload the other panel.
4609 * @param flags for reload panel
4610 * @param current_file name of the current file
4613 void
4614 update_panels (panel_update_flags_t flags, const char *current_file)
4616 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4617 WPanel *panel;
4619 update_one_panel (get_current_index (), flags, current_file);
4620 if (reload_other)
4621 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4623 if (get_current_type () == view_listing)
4624 panel = (WPanel *) get_panel_widget (get_current_index ());
4625 else
4626 panel = (WPanel *) get_panel_widget (get_other_index ());
4628 if (!panel->is_panelized)
4629 (void) mc_chdir (panel->cwd_vpath);
4632 /* --------------------------------------------------------------------------------------------- */
4634 gsize
4635 panel_get_num_of_sortable_fields (void)
4637 gsize ret = 0, lc_index;
4639 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4640 if (panel_fields[lc_index].is_user_choice)
4641 ret++;
4642 return ret;
4645 /* --------------------------------------------------------------------------------------------- */
4647 const char **
4648 panel_get_sortable_fields (gsize * array_size)
4650 char **ret;
4651 gsize lc_index, i;
4653 lc_index = panel_get_num_of_sortable_fields ();
4655 ret = g_try_new0 (char *, lc_index + 1);
4656 if (ret == NULL)
4657 return NULL;
4659 if (array_size != NULL)
4660 *array_size = lc_index;
4662 lc_index = 0;
4664 for (i = 0; panel_fields[i].id != NULL; i++)
4665 if (panel_fields[i].is_user_choice)
4666 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4667 return (const char **) ret;
4670 /* --------------------------------------------------------------------------------------------- */
4672 const panel_field_t *
4673 panel_get_field_by_id (const char *name)
4675 gsize lc_index;
4676 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4677 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4678 return &panel_fields[lc_index];
4679 return NULL;
4682 /* --------------------------------------------------------------------------------------------- */
4684 const panel_field_t *
4685 panel_get_field_by_title_hotkey (const char *name)
4687 gsize lc_index;
4688 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4689 if (panel_fields[lc_index].title_hotkey != NULL &&
4690 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4691 return &panel_fields[lc_index];
4692 return NULL;
4695 /* --------------------------------------------------------------------------------------------- */
4697 const panel_field_t *
4698 panel_get_field_by_title (const char *name)
4700 gsize lc_index;
4702 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4704 gchar *title;
4705 gboolean ok;
4707 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4708 ok = title != NULL && strcmp (name, title) == 0;
4709 g_free (title);
4711 if (ok)
4712 return &panel_fields[lc_index];
4715 return NULL;
4718 /* --------------------------------------------------------------------------------------------- */
4720 gsize
4721 panel_get_num_of_user_possible_fields (void)
4723 gsize ret = 0, lc_index;
4725 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4726 if (panel_fields[lc_index].use_in_user_format)
4727 ret++;
4728 return ret;
4731 /* --------------------------------------------------------------------------------------------- */
4733 const char **
4734 panel_get_user_possible_fields (gsize * array_size)
4736 char **ret;
4737 gsize lc_index, i;
4739 lc_index = panel_get_num_of_user_possible_fields ();
4741 ret = g_try_new0 (char *, lc_index + 1);
4742 if (ret == NULL)
4743 return NULL;
4745 if (array_size != NULL)
4746 *array_size = lc_index;
4748 lc_index = 0;
4750 for (i = 0; panel_fields[i].id != NULL; i++)
4751 if (panel_fields[i].use_in_user_format)
4752 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4753 return (const char **) ret;
4756 /* --------------------------------------------------------------------------------------------- */
4758 void
4759 panel_init (void)
4761 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4762 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4764 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4765 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4766 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4767 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4768 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4769 panel_filename_scroll_left_char =
4770 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4771 panel_filename_scroll_right_char =
4772 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4774 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4775 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_current_file_to_clip_file",
4776 panel_save_current_file_to_clip_file, NULL, NULL);
4780 /* --------------------------------------------------------------------------------------------- */
4782 void
4783 panel_deinit (void)
4785 g_free (panel_sort_up_sign);
4786 g_free (panel_sort_down_sign);
4788 g_free (panel_hiddenfiles_sign_show);
4789 g_free (panel_hiddenfiles_sign_hide);
4790 g_free (panel_history_prev_item_sign);
4791 g_free (panel_history_next_item_sign);
4792 g_free (panel_history_show_list_sign);
4793 g_free (panel_filename_scroll_left_char);
4794 g_free (panel_filename_scroll_right_char);
4798 /* --------------------------------------------------------------------------------------------- */
4800 gboolean
4801 do_cd (const vfs_path_t * new_dir_vpath, enum cd_enum exact)
4803 gboolean res;
4804 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
4806 if (current_panel->is_panelized)
4808 size_t new_vpath_len;
4810 new_vpath_len = vfs_path_len (new_dir_vpath);
4811 if (vfs_path_equal_len (new_dir_vpath, panelized_panel.root_vpath, new_vpath_len))
4812 _new_dir_vpath = panelized_panel.root_vpath;
4815 res = do_panel_cd (current_panel, _new_dir_vpath, exact);
4817 #ifdef HAVE_CHARSET
4818 if (res)
4820 const vfs_path_element_t *path_element;
4822 path_element = vfs_path_get_by_index (current_panel->cwd_vpath, -1);
4823 if (path_element->encoding != NULL)
4824 current_panel->codepage = get_codepage_index (path_element->encoding);
4825 else
4826 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4828 #endif /* HAVE_CHARSET */
4830 return res;
4833 /* --------------------------------------------------------------------------------------------- */