7e426a5301654fa6e4d1ed74a8e9bd18d6716ed8
[midnight-commander.git] / src / filemanager / panel.c
blob7e426a5301654fa6e4d1ed74a8e9bd18d6716ed8
1 /*
2 Panel managing.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Miguel de Icaza, 1995
10 Timur Bakeyev, 1997, 1999
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file panel.c
29 * \brief Source: panel managin module
32 #include <config.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h" /* For Gpm_Event */
44 #include "lib/tty/key.h" /* XCTRL and ALT macros */
45 #include "lib/skin.h"
46 #include "lib/strescape.h"
47 #include "lib/mcconfig.h"
48 #include "lib/vfs/vfs.h"
49 #include "lib/unixcompat.h"
50 #include "lib/timefmt.h"
51 #include "lib/util.h"
52 #include "lib/widget.h"
53 #ifdef HAVE_CHARSET
54 #include "lib/charsets.h" /* get_codepage_id () */
55 #endif
56 #include "lib/event.h"
58 #include "src/setup.h" /* For loading/saving panel options */
59 #include "src/execute.h"
60 #ifdef HAVE_CHARSET
61 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
62 #endif
63 #include "src/keybind-defaults.h" /* global_keymap_t */
64 #ifdef ENABLE_SUBSHELL
65 #include "src/subshell.h" /* do_subshell_chdir() */
66 #endif
68 #include "dir.h"
69 #include "boxes.h"
70 #include "tree.h"
71 #include "ext.h" /* regexp_command */
72 #include "layout.h" /* Most layout variables are here */
73 #include "cmd.h"
74 #include "command.h" /* cmdline */
75 #include "usermenu.h"
76 #include "midnight.h"
77 #include "mountlist.h" /* my_statfs */
79 #include "panel.h"
81 /*** global variables ****************************************************************************/
83 /* The hook list for the select file function */
84 hook_t *select_file_hook = NULL;
86 /* *INDENT-OFF* */
87 panelized_panel_t panelized_panel = { {NULL, 0}, -1, NULL };
88 /* *INDENT-ON* */
90 static const char *string_file_name (file_entry *, int);
91 static const char *string_file_size (file_entry *, int);
92 static const char *string_file_size_brief (file_entry *, int);
93 static const char *string_file_type (file_entry *, int);
94 static const char *string_file_mtime (file_entry *, int);
95 static const char *string_file_atime (file_entry *, int);
96 static const char *string_file_ctime (file_entry *, int);
97 static const char *string_file_permission (file_entry *, int);
98 static const char *string_file_perm_octal (file_entry *, int);
99 static const char *string_file_nlinks (file_entry *, int);
100 static const char *string_inode (file_entry *, int);
101 static const char *string_file_nuid (file_entry *, int);
102 static const char *string_file_ngid (file_entry *, int);
103 static const char *string_file_owner (file_entry *, int);
104 static const char *string_file_group (file_entry *, int);
105 static const char *string_marked (file_entry *, int);
106 static const char *string_space (file_entry *, int);
107 static const char *string_dot (file_entry *, int);
109 /* *INDENT-OFF* */
110 panel_field_t panel_fields[] = {
112 "unsorted", 12, 1, J_LEFT_FIT,
113 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
114 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
115 N_("sort|u"),
116 N_("&Unsorted"), TRUE, FALSE,
117 string_file_name,
118 (sortfn *) unsorted
122 "name", 12, 1, J_LEFT_FIT,
123 /* TRANSLATORS: one single character to represent 'name' sort mode */
124 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
125 N_("sort|n"),
126 N_("&Name"), TRUE, TRUE,
127 string_file_name,
128 (sortfn *) sort_name
132 "version", 12, 1, J_LEFT_FIT,
133 /* TRANSLATORS: one single character to represent 'version' sort mode */
134 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
135 N_("sort|v"),
136 N_("&Version"), TRUE, FALSE,
137 string_file_name,
138 (sortfn *) sort_vers
142 "extension", 12, 1, J_LEFT_FIT,
143 /* TRANSLATORS: one single character to represent 'extension' sort mode */
144 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
145 N_("sort|e"),
146 N_("E&xtension"), TRUE, FALSE,
147 string_file_name, /* TODO: string_file_ext */
148 (sortfn *) sort_ext
152 "size", 7, 0, J_RIGHT,
153 /* TRANSLATORS: one single character to represent 'size' sort mode */
154 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
155 N_("sort|s"),
156 N_("&Size"), TRUE, TRUE,
157 string_file_size,
158 (sortfn *) sort_size
162 "bsize", 7, 0, J_RIGHT,
164 N_("Block Size"), FALSE, FALSE,
165 string_file_size_brief,
166 (sortfn *) sort_size
170 "type", 1, 0, J_LEFT,
172 "", FALSE, TRUE,
173 string_file_type,
174 NULL
178 "mtime", 12, 0, J_RIGHT,
179 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
180 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
181 N_("sort|m"),
182 N_("&Modify time"), TRUE, TRUE,
183 string_file_mtime,
184 (sortfn *) sort_time
188 "atime", 12, 0, J_RIGHT,
189 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
190 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
191 N_("sort|a"),
192 N_("&Access time"), TRUE, TRUE,
193 string_file_atime,
194 (sortfn *) sort_atime
198 "ctime", 12, 0, J_RIGHT,
199 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
200 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
201 N_("sort|h"),
202 N_("C&hange time"), TRUE, TRUE,
203 string_file_ctime,
204 (sortfn *) sort_ctime
208 "perm", 10, 0, J_LEFT,
210 N_("Permission"), FALSE, TRUE,
211 string_file_permission,
212 NULL
216 "mode", 6, 0, J_RIGHT,
218 N_("Perm"), FALSE, TRUE,
219 string_file_perm_octal,
220 NULL
224 "nlink", 2, 0, J_RIGHT,
226 N_("Nl"), FALSE, TRUE,
227 string_file_nlinks, NULL
231 "inode", 5, 0, J_RIGHT,
232 /* TRANSLATORS: one single character to represent 'inode' sort mode */
233 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
234 N_("sort|i"),
235 N_("&Inode"), TRUE, TRUE,
236 string_inode,
237 (sortfn *) sort_inode
241 "nuid", 5, 0, J_RIGHT,
243 N_("UID"), FALSE, FALSE,
244 string_file_nuid,
245 NULL
249 "ngid", 5, 0, J_RIGHT,
251 N_("GID"), FALSE, FALSE,
252 string_file_ngid,
253 NULL
257 "owner", 8, 0, J_LEFT_FIT,
259 N_("Owner"), FALSE, TRUE,
260 string_file_owner,
261 NULL
265 "group", 8, 0, J_LEFT_FIT,
267 N_("Group"), FALSE, TRUE,
268 string_file_group,
269 NULL
273 "mark", 1, 0, J_RIGHT,
275 " ", FALSE, TRUE,
276 string_marked,
277 NULL
281 "|", 1, 0, J_RIGHT,
283 " ", FALSE, TRUE,
284 NULL,
285 NULL
289 "space", 1, 0, J_RIGHT,
291 " ", FALSE, TRUE,
292 string_space,
293 NULL
297 "dot", 1, 0, J_RIGHT,
299 " ", FALSE, FALSE,
300 string_dot,
301 NULL
305 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
308 /* *INDENT-ON* */
310 mc_fhl_t *mc_filehighlight = NULL;
312 extern int saving_setup;
314 /*** file scope macro definitions ****************************************************************/
316 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
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 *, 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 * 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 * 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 * fe, int len)
501 static char buffer[BUF_TINY];
503 /* Don't ever show size of ".." since we don't calculate it */
504 if (!strcmp (fe->fname, ".."))
506 return _("UP--DIR");
509 #ifdef HAVE_STRUCT_STAT_ST_RDEV
510 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
511 format_device_number (buffer, len + 1, fe->st.st_rdev);
512 else
513 #endif
515 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
517 return buffer;
520 /* --------------------------------------------------------------------------------------------- */
521 /** bsize */
523 static const char *
524 string_file_size_brief (file_entry * fe, int len)
526 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
528 return _("SYMLINK");
531 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
533 return _("SUB-DIR");
536 return string_file_size (fe, len);
539 /* --------------------------------------------------------------------------------------------- */
540 /** This functions return a string representation of a file entry type */
542 static const char *
543 string_file_type (file_entry * fe, int len)
545 static char buffer[2];
547 (void) len;
548 if (S_ISDIR (fe->st.st_mode))
549 buffer[0] = PATH_SEP;
550 else if (S_ISLNK (fe->st.st_mode))
552 if (fe->f.link_to_dir)
553 buffer[0] = '~';
554 else if (fe->f.stale_link)
555 buffer[0] = '!';
556 else
557 buffer[0] = '@';
559 else if (S_ISCHR (fe->st.st_mode))
560 buffer[0] = '-';
561 else if (S_ISSOCK (fe->st.st_mode))
562 buffer[0] = '=';
563 else if (S_ISDOOR (fe->st.st_mode))
564 buffer[0] = '>';
565 else if (S_ISBLK (fe->st.st_mode))
566 buffer[0] = '+';
567 else if (S_ISFIFO (fe->st.st_mode))
568 buffer[0] = '|';
569 else if (S_ISNAM (fe->st.st_mode))
570 buffer[0] = '#';
571 else if (!S_ISREG (fe->st.st_mode))
572 buffer[0] = '?'; /* non-regular of unknown kind */
573 else if (is_exe (fe->st.st_mode))
574 buffer[0] = '*';
575 else
576 buffer[0] = ' ';
577 buffer[1] = '\0';
578 return buffer;
581 /* --------------------------------------------------------------------------------------------- */
582 /** mtime */
584 static const char *
585 string_file_mtime (file_entry * fe, int len)
587 (void) len;
588 return file_date (fe->st.st_mtime);
591 /* --------------------------------------------------------------------------------------------- */
592 /** atime */
594 static const char *
595 string_file_atime (file_entry * fe, int len)
597 (void) len;
598 return file_date (fe->st.st_atime);
601 /* --------------------------------------------------------------------------------------------- */
602 /** ctime */
604 static const char *
605 string_file_ctime (file_entry * fe, int len)
607 (void) len;
608 return file_date (fe->st.st_ctime);
611 /* --------------------------------------------------------------------------------------------- */
612 /** perm */
614 static const char *
615 string_file_permission (file_entry * fe, int len)
617 (void) len;
618 return string_perm (fe->st.st_mode);
621 /* --------------------------------------------------------------------------------------------- */
622 /** mode */
624 static const char *
625 string_file_perm_octal (file_entry * fe, int len)
627 static char buffer[10];
629 (void) len;
630 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
631 return buffer;
634 /* --------------------------------------------------------------------------------------------- */
635 /** nlink */
637 static const char *
638 string_file_nlinks (file_entry * fe, int len)
640 static char buffer[BUF_TINY];
642 (void) len;
643 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
644 return buffer;
647 /* --------------------------------------------------------------------------------------------- */
648 /** inode */
650 static const char *
651 string_inode (file_entry * fe, int len)
653 static char buffer[10];
655 (void) len;
656 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
657 return buffer;
660 /* --------------------------------------------------------------------------------------------- */
661 /** nuid */
663 static const char *
664 string_file_nuid (file_entry * fe, int len)
666 static char buffer[10];
668 (void) len;
669 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
670 return buffer;
673 /* --------------------------------------------------------------------------------------------- */
674 /** ngid */
676 static const char *
677 string_file_ngid (file_entry * fe, int len)
679 static char buffer[10];
681 (void) len;
682 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
683 return buffer;
686 /* --------------------------------------------------------------------------------------------- */
687 /** owner */
689 static const char *
690 string_file_owner (file_entry * fe, int len)
692 (void) len;
693 return get_owner (fe->st.st_uid);
696 /* --------------------------------------------------------------------------------------------- */
697 /** group */
699 static const char *
700 string_file_group (file_entry * fe, int len)
702 (void) len;
703 return get_group (fe->st.st_gid);
706 /* --------------------------------------------------------------------------------------------- */
707 /** mark */
709 static const char *
710 string_marked (file_entry * fe, int len)
712 (void) len;
713 return fe->f.marked ? "*" : " ";
716 /* --------------------------------------------------------------------------------------------- */
717 /** space */
719 static const char *
720 string_space (file_entry * fe, int len)
722 (void) fe;
723 (void) len;
724 return " ";
727 /* --------------------------------------------------------------------------------------------- */
728 /** dot */
730 static const char *
731 string_dot (file_entry * fe, int len)
733 (void) fe;
734 (void) len;
735 return ".";
738 /* --------------------------------------------------------------------------------------------- */
740 static int
741 file_compute_color (int attr, file_entry * fe)
743 switch (attr)
745 case SELECTED:
746 return (SELECTED_COLOR);
747 case MARKED:
748 return (MARKED_COLOR);
749 case MARKED_SELECTED:
750 return (MARKED_SELECTED_COLOR);
751 case STATUS:
752 return (NORMAL_COLOR);
753 case NORMAL:
754 default:
755 if (!panels_options.filetype_mode)
756 return (NORMAL_COLOR);
759 return mc_fhl_get_color (mc_filehighlight, fe);
762 /* --------------------------------------------------------------------------------------------- */
763 /** Formats the file number file_index of panel in the buffer dest */
765 static filename_scroll_flag_t
766 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
767 int isstatus, int *field_lenght)
769 int color, length, empty_line;
770 const char *txt;
771 format_e *format, *home;
772 file_entry *fe;
773 filename_scroll_flag_t res = FILENAME_NOSCROLL;
775 (void) dest;
776 (void) limit;
777 length = 0;
778 empty_line = (file_index >= panel->count);
779 home = (isstatus) ? panel->status_format : panel->format;
780 fe = &panel->dir.list[file_index];
781 *field_lenght = 0;
783 if (!empty_line)
784 color = file_compute_color (attr, fe);
785 else
786 color = NORMAL_COLOR;
787 for (format = home; format; format = format->next)
789 if (length == width)
790 break;
792 if (format->string_fn)
794 int len, perm;
795 const char *prepared_text;
796 int name_offset = 0;
798 if (empty_line)
799 txt = " ";
800 else
801 txt = (*format->string_fn) (fe, format->field_len);
803 len = format->field_len;
804 if (len + length > width)
805 len = width - length;
806 if (len <= 0)
807 break;
809 if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
811 int str_len;
812 int i;
814 *field_lenght = len + 1;
816 str_len = str_length (txt);
817 i = max (0, str_len - len);
818 panel->max_shift = max (panel->max_shift, i);
819 i = min (panel->content_shift, i);
821 if (i > -1)
823 name_offset = str_offset_to_pos (txt, i);
824 if (str_len > len)
826 res = FILENAME_SCROLL_LEFT;
827 if (str_length (txt + name_offset) > len)
828 res |= FILENAME_SCROLL_RIGHT;
833 perm = 0;
834 if (panels_options.permission_mode)
836 if (!strcmp (format->id, "perm"))
837 perm = 1;
838 else if (!strcmp (format->id, "mode"))
839 perm = 2;
842 if (color >= 0)
843 tty_setcolor (color);
844 else
845 tty_lowlevel_setcolor (-color);
847 if (!isstatus && panel->content_shift > -1)
848 prepared_text =
849 str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
850 else
851 prepared_text = str_fit_to_term (txt, len, format->just_mode);
853 if (perm)
854 add_permission_string (prepared_text, format->field_len, fe, attr, color, perm - 1);
855 else
856 tty_print_string (prepared_text);
858 length += len;
860 else
862 if (attr == SELECTED || attr == MARKED_SELECTED)
863 tty_setcolor (SELECTED_COLOR);
864 else
865 tty_setcolor (NORMAL_COLOR);
866 tty_print_one_vline (TRUE);
867 length++;
871 if (length < width)
873 int y, x;
875 tty_getyx (&y, &x);
876 tty_draw_hline (y, x, ' ', width - length);
879 return res;
882 /* --------------------------------------------------------------------------------------------- */
884 static void
885 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
887 Widget *w = WIDGET (panel);
889 int second_column = 0;
890 int width;
891 int offset = 0;
892 char buffer[BUF_MEDIUM];
893 filename_scroll_flag_t ret_frm;
894 int ypos = 0;
895 gboolean panel_is_split = !isstatus && panel->split;
896 int fln = 0;
898 width = w->cols - 2;
900 if (panel_is_split)
902 second_column = (file_index - panel->top_file) / llines (panel);
903 width = width / 2 - 1;
905 if (second_column != 0)
907 offset = 1 + width;
908 width = w->cols - offset - 2;
912 /* Nothing to paint */
913 if (width <= 0)
914 return;
916 if (mv)
918 int pos = file_index - panel->top_file;
920 if (panel_is_split)
921 ypos = pos % llines (panel);
922 else
923 ypos = pos;
925 ypos += 2;
926 widget_move (w, ypos, offset + 1);
929 ret_frm = format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus, &fln);
931 if (panel_is_split)
933 if (second_column)
934 tty_print_char (' ');
935 else
937 tty_setcolor (NORMAL_COLOR);
938 tty_print_one_vline (TRUE);
941 if (ret_frm != FILENAME_NOSCROLL && mv)
943 if (!panel_is_split && fln > 0)
945 if (panel->list_type == list_long)
947 offset = width - fln + 1;
948 width = fln - 1;
950 else
952 width = fln;
955 widget_move (w, ypos, offset);
956 tty_setcolor (NORMAL_COLOR);
957 tty_print_string (panel_filename_scroll_left_char);
959 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
961 widget_move (w, ypos, offset + 1 + width);
962 tty_setcolor (NORMAL_COLOR);
963 tty_print_string (panel_filename_scroll_right_char);
968 /* --------------------------------------------------------------------------------------------- */
970 static void
971 display_mini_info (WPanel * panel)
973 Widget *w = WIDGET (panel);
975 if (!panels_options.show_mini_info)
976 return;
978 widget_move (w, llines (panel) + 3, 1);
980 if (panel->searching)
982 tty_setcolor (INPUT_COLOR);
983 tty_print_char ('/');
984 tty_print_string (str_fit_to_term (panel->search_buffer, w->cols - 3, J_LEFT));
985 return;
988 /* Status resolves links and show them */
989 set_colors (panel);
991 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
993 char link_target[MC_MAXPATHLEN];
994 vfs_path_t *lc_link_vpath;
995 int len;
997 lc_link_vpath =
998 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
999 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
1000 vfs_path_free (lc_link_vpath);
1001 if (len > 0)
1003 link_target[len] = 0;
1004 tty_print_string ("-> ");
1005 tty_print_string (str_fit_to_term (link_target, w->cols - 5, J_LEFT_FIT));
1007 else
1008 tty_print_string (str_fit_to_term (_("<readlink failed>"), w->cols - 2, J_LEFT));
1010 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
1012 /* FIXME:
1013 * while loading directory (do_load_dir() and do_reload_dir()),
1014 * the actual stat info about ".." directory isn't got;
1015 * so just don't display incorrect info about ".." directory */
1016 tty_print_string (str_fit_to_term (_("UP--DIR"), w->cols - 2, J_LEFT));
1018 else
1019 /* Default behavior */
1020 repaint_file (panel, panel->selected, 0, STATUS, 1);
1023 /* --------------------------------------------------------------------------------------------- */
1025 static void
1026 paint_dir (WPanel * panel)
1028 int i;
1029 int color; /* Color value of the line */
1030 int items; /* Number of items */
1032 items = llines (panel) * (panel->split ? 2 : 1);
1033 /* reset max len of filename because we have the new max length for the new file list */
1034 panel->max_shift = -1;
1035 for (i = 0; i < items; i++)
1037 if (i + panel->top_file >= panel->count)
1038 color = 0;
1039 else
1041 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1042 color += (panel->selected == i + panel->top_file && panel->active);
1044 repaint_file (panel, i + panel->top_file, 1, color, 0);
1047 tty_set_normal_attrs ();
1050 /* --------------------------------------------------------------------------------------------- */
1052 static void
1053 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
1055 Widget *w = WIDGET (panel);
1057 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
1058 int cols;
1060 if (panel->marked <= 0)
1061 return;
1063 buf = size_only ? b_bytes : buffer;
1064 cols = w->cols - 2;
1067 * This is a trick to use two ngettext() calls in one sentence.
1068 * First make "N bytes", then insert it into "X in M files".
1070 g_snprintf (b_bytes, sizeof (b_bytes),
1071 ngettext ("%s byte", "%s bytes", panel->total),
1072 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1073 if (!size_only)
1074 g_snprintf (buffer, sizeof (buffer),
1075 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1076 b_bytes, panel->marked);
1078 /* don't forget spaces around buffer content */
1079 buf = (char *) str_trunc (buf, cols - 4);
1081 if (x < 0)
1082 /* center in panel */
1083 x = (w->cols - str_term_width1 (buf)) / 2 - 1;
1086 * y == llines (panel) + 2 for mini_info_separator
1087 * y == w->lines - 1 for panel bottom frame
1089 widget_move (w, y, x);
1090 tty_setcolor (MARKED_COLOR);
1091 tty_printf (" %s ", buf);
1094 /* --------------------------------------------------------------------------------------------- */
1096 static void
1097 mini_info_separator (WPanel * panel)
1099 if (panels_options.show_mini_info)
1101 Widget *w = WIDGET (panel);
1102 const int y = llines (panel) + 2;
1104 tty_setcolor (NORMAL_COLOR);
1105 tty_draw_hline (w->y + y, w->x + 1, ACS_HLINE, w->cols - 2);
1106 /* Status displays total marked size.
1107 * Centered in panel, full format. */
1108 display_total_marked_size (panel, y, -1, FALSE);
1112 /* --------------------------------------------------------------------------------------------- */
1114 static void
1115 show_free_space (WPanel * panel)
1117 /* Used to figure out how many free space we have */
1118 static struct my_statfs myfs_stats;
1119 /* Old current working directory for displaying free space */
1120 static char *old_cwd = NULL;
1121 char *tmp_path;
1123 /* Don't try to stat non-local fs */
1124 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1125 return;
1127 tmp_path = vfs_path_to_str (panel->cwd_vpath);
1128 if (old_cwd == NULL || strcmp (old_cwd, tmp_path) != 0)
1130 char rpath[PATH_MAX];
1132 init_my_statfs ();
1133 g_free (old_cwd);
1134 old_cwd = tmp_path;
1135 tmp_path = NULL;
1137 if (mc_realpath (old_cwd, rpath) == NULL)
1138 return;
1140 my_statfs (&myfs_stats, rpath);
1142 g_free (tmp_path);
1144 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1146 Widget *w = WIDGET (panel);
1147 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1149 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1150 panels_options.kilobyte_si);
1151 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1152 panels_options.kilobyte_si);
1153 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1154 myfs_stats.total == 0 ? 0 :
1155 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1156 widget_move (w, w->lines - 1, w->cols - 2 - (int) strlen (tmp));
1157 tty_setcolor (NORMAL_COLOR);
1158 tty_print_string (tmp);
1162 /* --------------------------------------------------------------------------------------------- */
1164 * Prepare path string for showing in panel's header.
1165 * Passwords will removed, also home dir will replaced by ~
1167 * @param panel WPanel object
1169 * @return newly allocated string.
1172 static char *
1173 panel_correct_path_to_show (WPanel * panel)
1175 vfs_path_t *last_vpath;
1176 const vfs_path_element_t *path_element;
1177 char *return_path;
1178 int elements_count;
1180 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1182 /* get last path element */
1183 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1186 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1187 strcmp (path_element->class->name, "extfs") == 0 ||
1188 strcmp (path_element->class->name, "tarfs") == 0))
1190 const char *archive_name;
1191 const vfs_path_element_t *prev_path_element;
1193 /* get previous path element for catching archive name */
1194 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1195 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1196 if (archive_name != NULL)
1198 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1200 else
1202 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1203 last_vpath->relative = TRUE;
1206 else
1208 last_vpath = vfs_path_new ();
1209 last_vpath->relative = TRUE;
1212 vfs_path_add_element (last_vpath, path_element);
1213 return_path =
1214 vfs_path_to_str_flags (last_vpath, 0,
1215 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1216 vfs_path_free (last_vpath);
1218 return return_path;
1221 /* --------------------------------------------------------------------------------------------- */
1223 * Get Current path element encoding
1225 * @param panel WPanel object
1227 * @return newly allocated string or NULL if path charset is same as system charset
1230 #ifdef HAVE_CHARSET
1231 static char *
1232 panel_get_encoding_info_str (WPanel * panel)
1234 char *ret_str = NULL;
1235 const vfs_path_element_t *path_element;
1237 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1238 if (path_element->encoding != NULL)
1239 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1241 return ret_str;
1243 #endif
1245 /* --------------------------------------------------------------------------------------------- */
1247 static void
1248 show_dir (WPanel * panel)
1250 Widget *w = WIDGET (panel);
1252 gchar *tmp;
1254 set_colors (panel);
1255 draw_box (w->owner, w->y, w->x, w->lines, w->cols, FALSE);
1257 if (panels_options.show_mini_info)
1259 widget_move (w, llines (panel) + 2, 0);
1260 tty_print_alt_char (ACS_LTEE, FALSE);
1261 widget_move (w, llines (panel) + 2, w->cols - 1);
1262 tty_print_alt_char (ACS_RTEE, FALSE);
1265 widget_move (w, 0, 1);
1266 tty_print_string (panel_history_prev_item_sign);
1268 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1269 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1270 panel_history_next_item_sign);
1272 widget_move (w, 0, w->cols - 6);
1273 tty_print_string (tmp);
1275 g_free (tmp);
1277 widget_move (w, 0, 3);
1279 if (panel->is_panelized)
1280 tty_printf (" %s ", _("Panelize"));
1281 #ifdef HAVE_CHARSET
1282 else
1284 tmp = panel_get_encoding_info_str (panel);
1285 if (tmp != NULL)
1287 tty_printf ("%s", tmp);
1288 widget_move (w, 0, 3 + strlen (tmp));
1289 g_free (tmp);
1292 #endif
1294 if (panel->active)
1295 tty_setcolor (REVERSE_COLOR);
1297 tmp = panel_correct_path_to_show (panel);
1298 tty_printf (" %s ", str_term_trim (tmp, min (max (w->cols - 12, 0), w->cols)));
1299 g_free (tmp);
1301 if (!panels_options.show_mini_info)
1303 if (panel->marked == 0)
1305 /* Show size of curret file in the bottom of panel */
1306 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1308 char buffer[BUF_SMALL];
1310 g_snprintf (buffer, sizeof (buffer), " %s ",
1311 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1312 panels_options.kilobyte_si));
1313 tty_setcolor (NORMAL_COLOR);
1314 widget_move (w, w->lines - 1, 4);
1315 tty_print_string (buffer);
1318 else
1320 /* Show total size of marked files
1321 * In the bottom of panel, display size only. */
1322 display_total_marked_size (panel, w->lines - 1, 2, TRUE);
1326 show_free_space (panel);
1328 if (panel->active)
1329 tty_set_normal_attrs ();
1332 /* --------------------------------------------------------------------------------------------- */
1334 /* Returns the number of items in the given panel */
1335 static int
1336 ITEMS (WPanel * p)
1338 if (p->split)
1339 return llines (p) * 2;
1340 else
1341 return llines (p);
1344 /* --------------------------------------------------------------------------------------------- */
1346 static void
1347 adjust_top_file (WPanel * panel)
1349 int items = ITEMS (panel);
1351 if (panel->count <= items)
1353 /* If all files fit, show them all. */
1354 panel->top_file = 0;
1356 else
1358 int i;
1360 /* top_file has to be in the range [selected-items+1, selected] so that
1361 the selected file is visible.
1362 top_file should be in the range [0, count-items] so that there's
1363 no empty space wasted.
1364 Within these ranges, adjust it by as little as possible. */
1366 if (panel->top_file < 0)
1367 panel->top_file = 0;
1369 i = panel->selected - items + 1;
1370 if (panel->top_file < i)
1371 panel->top_file = i;
1373 i = panel->count - items;
1374 if (panel->top_file > i)
1375 panel->top_file = i;
1377 if (panel->top_file > panel->selected)
1378 panel->top_file = panel->selected;
1382 /* --------------------------------------------------------------------------------------------- */
1383 /** add "#enc:encodning" to end of path */
1384 /* if path end width a previous #enc:, only encoding is changed no additional
1385 * #enc: is appended
1386 * retun new string
1389 static char *
1390 panel_save_name (WPanel * panel)
1392 /* If the program is shuting down */
1393 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1394 return g_strdup (panel->panel_name);
1395 else
1396 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1399 /* --------------------------------------------------------------------------------------------- */
1401 static void
1402 directory_history_add (struct WPanel *panel, const vfs_path_t * vpath)
1404 char *tmp;
1406 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1407 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1408 panel->dir_history_current = panel->dir_history;
1411 /* --------------------------------------------------------------------------------------------- */
1413 /* "history_load" event handler */
1414 static gboolean
1415 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1416 gpointer init_data, gpointer data)
1418 WPanel *p = (WPanel *) init_data;
1419 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1421 (void) event_group_name;
1422 (void) event_name;
1424 if (ev->receiver == NULL || ev->receiver == WIDGET (p))
1426 if (ev->cfg != NULL)
1427 p->dir_history = history_load (ev->cfg, p->hist_name);
1428 else
1429 p->dir_history = history_get (p->hist_name);
1431 directory_history_add (p, p->cwd_vpath);
1434 return TRUE;
1437 /* --------------------------------------------------------------------------------------------- */
1439 /* "history_save" event handler */
1440 static gboolean
1441 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1442 gpointer init_data, gpointer data)
1444 WPanel *p = (WPanel *) init_data;
1446 (void) event_group_name;
1447 (void) event_name;
1449 if (p->dir_history != NULL)
1451 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1453 history_save (ev->cfg, p->hist_name, p->dir_history);
1456 return TRUE;
1459 /* --------------------------------------------------------------------------------------------- */
1461 static void
1462 panel_destroy (WPanel * p)
1464 size_t i;
1466 if (panels_options.auto_save_setup)
1468 char *name;
1470 name = panel_save_name (p);
1471 panel_save_setup (p, name);
1472 g_free (name);
1475 panel_clean_dir (p);
1477 /* clean history */
1478 if (p->dir_history != NULL)
1480 /* directory history is already saved before this moment */
1481 p->dir_history = g_list_first (p->dir_history);
1482 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1483 g_list_free (p->dir_history);
1485 g_free (p->hist_name);
1487 delete_format (p->format);
1488 delete_format (p->status_format);
1490 g_free (p->user_format);
1491 for (i = 0; i < LIST_TYPES; i++)
1492 g_free (p->user_status_format[i]);
1494 g_free (p->dir.list);
1495 g_free (p->panel_name);
1497 vfs_path_free (p->lwd_vpath);
1498 vfs_path_free (p->cwd_vpath);
1501 /* --------------------------------------------------------------------------------------------- */
1503 static void
1504 panel_format_modified (WPanel * panel)
1506 panel->format_modified = 1;
1509 /* --------------------------------------------------------------------------------------------- */
1511 static void
1512 panel_paint_sort_info (WPanel * panel)
1514 if (*panel->sort_info.sort_field->hotkey != '\0')
1516 const char *sort_sign =
1517 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1518 char *str;
1520 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1521 widget_move (panel, 1, 1);
1522 tty_print_string (str);
1523 g_free (str);
1527 /* --------------------------------------------------------------------------------------------- */
1529 static gchar *
1530 panel_get_title_without_hotkey (const char *title)
1532 char *translated_title;
1533 char *hkey;
1535 if (title == NULL)
1536 return NULL;
1537 if (title[0] == '\0')
1538 return g_strdup ("");
1540 translated_title = g_strdup (_(title));
1542 hkey = strchr (translated_title, '&');
1543 if ((hkey != NULL) && (hkey[1] != '\0'))
1544 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1546 return translated_title;
1549 /* --------------------------------------------------------------------------------------------- */
1551 static void
1552 paint_frame (WPanel * panel)
1554 Widget *w = WIDGET (panel);
1556 int side, width;
1557 GString *format_txt;
1559 adjust_top_file (panel);
1561 widget_erase (w);
1562 show_dir (panel);
1564 widget_move (w, 1, 1);
1566 for (side = 0; side <= panel->split; side++)
1568 format_e *format;
1570 if (side)
1572 tty_setcolor (NORMAL_COLOR);
1573 tty_print_one_vline (TRUE);
1574 width = w->cols - w->cols / 2 - 1;
1576 else if (panel->split)
1577 width = w->cols / 2 - 3;
1578 else
1579 width = w->cols - 2;
1581 format_txt = g_string_new ("");
1582 for (format = panel->format; format; format = format->next)
1584 if (format->string_fn)
1586 g_string_set_size (format_txt, 0);
1588 if (panel->list_type == list_long
1589 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1590 g_string_append (format_txt,
1591 panel->sort_info.reverse
1592 ? panel_sort_down_sign : panel_sort_up_sign);
1594 g_string_append (format_txt, format->title);
1595 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1597 g_string_append (format_txt, " [");
1598 g_string_append (format_txt, panel->filter);
1599 g_string_append (format_txt, "]");
1602 tty_setcolor (HEADER_COLOR);
1603 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1604 J_CENTER_LEFT));
1605 width -= format->field_len;
1607 else
1609 tty_setcolor (NORMAL_COLOR);
1610 tty_print_one_vline (TRUE);
1611 width--;
1614 g_string_free (format_txt, TRUE);
1616 if (width > 0)
1618 int y, x;
1620 tty_getyx (&y, &x);
1621 tty_draw_hline (y, x, ' ', width);
1625 if (panel->list_type != list_long)
1626 panel_paint_sort_info (panel);
1629 /* --------------------------------------------------------------------------------------------- */
1631 static const char *
1632 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1634 panel_display_t frame = frame_half;
1635 format = skip_separators (format);
1637 if (!strncmp (format, "full", 4))
1639 frame = frame_full;
1640 format += 4;
1642 else if (!strncmp (format, "half", 4))
1644 frame = frame_half;
1645 format += 4;
1648 if (!isstatus)
1650 panel->frame_size = frame;
1651 panel->split = 0;
1654 /* Now, the optional column specifier */
1655 format = skip_separators (format);
1657 if (*format == '1' || *format == '2')
1659 if (!isstatus)
1660 panel->split = *format == '2';
1661 format++;
1664 if (!isstatus)
1665 panel_update_cols (WIDGET (panel), panel->frame_size);
1667 return skip_separators (format);
1670 /* Format is:
1672 all := panel_format? format
1673 panel_format := [full|half] [1|2]
1674 format := one_format_e
1675 | format , one_format_e
1677 one_format_e := just format.id [opt_size]
1678 just := [<=>]
1679 opt_size := : size [opt_expand]
1680 size := [0-9]+
1681 opt_expand := +
1685 /* --------------------------------------------------------------------------------------------- */
1687 static format_e *
1688 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1689 int *res_total_cols)
1691 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1692 int total_cols = 0; /* Used columns by the format */
1693 int set_justify; /* flag: set justification mode? */
1694 align_crt_t justify = J_LEFT; /* Which mode. */
1695 size_t i;
1697 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1699 *error = 0;
1701 if (i18n_timelength == 0)
1703 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1705 for (i = 0; panel_fields[i].id != NULL; i++)
1706 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1707 panel_fields[i].min_size = i18n_timelength;
1711 * This makes sure that the panel and mini status full/half mode
1712 * setting is equal
1714 format = parse_panel_size (panel, format, isstatus);
1716 while (*format)
1717 { /* format can be an empty string */
1718 int found = 0;
1720 darr = g_new0 (format_e, 1);
1722 /* I'm so ugly, don't look at me :-) */
1723 if (!home)
1724 home = old = darr;
1726 old->next = darr;
1727 darr->next = 0;
1728 old = darr;
1730 format = skip_separators (format);
1732 if (strchr ("<=>", *format))
1734 set_justify = 1;
1735 switch (*format)
1737 case '<':
1738 justify = J_LEFT;
1739 break;
1740 case '=':
1741 justify = J_CENTER;
1742 break;
1743 case '>':
1744 default:
1745 justify = J_RIGHT;
1746 break;
1748 format = skip_separators (format + 1);
1750 else
1751 set_justify = 0;
1753 for (i = 0; panel_fields[i].id != NULL; i++)
1755 size_t klen = strlen (panel_fields[i].id);
1757 if (strncmp (format, panel_fields[i].id, klen) != 0)
1758 continue;
1760 format += klen;
1762 darr->requested_field_len = panel_fields[i].min_size;
1763 darr->string_fn = panel_fields[i].string_fn;
1764 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1766 darr->id = panel_fields[i].id;
1767 darr->expand = panel_fields[i].expands;
1768 darr->just_mode = panel_fields[i].default_just;
1770 if (set_justify)
1772 if (IS_FIT (darr->just_mode))
1773 darr->just_mode = MAKE_FIT (justify);
1774 else
1775 darr->just_mode = justify;
1777 found = 1;
1779 format = skip_separators (format);
1781 /* If we have a size specifier */
1782 if (*format == ':')
1784 int req_length;
1786 /* If the size was specified, we don't want
1787 * auto-expansion by default
1789 darr->expand = 0;
1790 format++;
1791 req_length = atoi (format);
1792 darr->requested_field_len = req_length;
1794 format = skip_numbers (format);
1796 /* Now, if they insist on expansion */
1797 if (*format == '+')
1799 darr->expand = 1;
1800 format++;
1805 break;
1807 if (!found)
1809 char *tmp_format = g_strdup (format);
1811 int pos = min (8, strlen (format));
1812 delete_format (home);
1813 tmp_format[pos] = 0;
1814 *error =
1815 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1816 g_free (tmp_format);
1817 return 0;
1819 total_cols += darr->requested_field_len;
1822 *res_total_cols = total_cols;
1823 return home;
1826 /* --------------------------------------------------------------------------------------------- */
1828 static format_e *
1829 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1831 #define MAX_EXPAND 4
1832 int expand_top = 0; /* Max used element in expand */
1833 int usable_columns; /* Usable columns in the panel */
1834 int total_cols = 0;
1835 int i;
1836 format_e *darr, *home;
1838 if (!format)
1839 format = DEFAULT_USER_FORMAT;
1841 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1843 if (*error)
1844 return 0;
1846 panel->dirty = 1;
1848 /* Status needn't to be split */
1849 usable_columns = ((WIDGET (panel)->cols - 2) / ((isstatus)
1851 : (panel->split + 1))) - (!isstatus
1852 && panel->split);
1854 /* Look for the expandable fields and set field_len based on the requested field len */
1855 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1857 darr->field_len = darr->requested_field_len;
1858 if (darr->expand)
1859 expand_top++;
1862 /* If we used more columns than the available columns, adjust that */
1863 if (total_cols > usable_columns)
1865 int pdif, dif = total_cols - usable_columns;
1867 while (dif)
1869 pdif = dif;
1870 for (darr = home; darr; darr = darr->next)
1872 if (dif && darr->field_len - 1)
1874 darr->field_len--;
1875 dif--;
1879 /* avoid endless loop if num fields > 40 */
1880 if (pdif == dif)
1881 break;
1883 total_cols = usable_columns; /* give up, the rest should be truncated */
1886 /* Expand the available space */
1887 if ((usable_columns > total_cols) && expand_top)
1889 int spaces = (usable_columns - total_cols) / expand_top;
1890 int extra = (usable_columns - total_cols) % expand_top;
1892 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1893 if (darr->expand)
1895 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1896 i++;
1899 return home;
1902 /* --------------------------------------------------------------------------------------------- */
1903 /** Given the panel->view_type returns the format string to be parsed */
1905 static const char *
1906 panel_format (WPanel * panel)
1908 switch (panel->list_type)
1910 case list_long:
1911 return "full perm space nlink space owner space group space size space mtime space name";
1913 case list_brief:
1914 return "half 2 type name";
1916 case list_user:
1917 return panel->user_format;
1919 default:
1920 case list_full:
1921 return "half type name | size | mtime";
1925 /* --------------------------------------------------------------------------------------------- */
1927 static const char *
1928 mini_status_format (WPanel * panel)
1930 if (panel->user_mini_status)
1931 return panel->user_status_format[panel->list_type];
1933 switch (panel->list_type)
1935 case list_long:
1936 return "full perm space nlink space owner space group space size space mtime space name";
1938 case list_brief:
1939 return "half type name space bsize space perm space";
1941 case list_full:
1942 return "half type name";
1944 default:
1945 case list_user:
1946 return panel->user_format;
1950 /* */
1951 /* Panel operation commands */
1952 /* */
1954 /* --------------------------------------------------------------------------------------------- */
1955 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1957 static cb_ret_t
1958 maybe_cd (int move_up_dir)
1960 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1962 if (move_up_dir)
1964 vfs_path_t *up_dir;
1966 up_dir = vfs_path_from_str ("..");
1967 do_cd (up_dir, cd_exact);
1968 vfs_path_free (up_dir);
1969 return MSG_HANDLED;
1972 if (S_ISDIR (selection (current_panel)->st.st_mode)
1973 || link_isdir (selection (current_panel)))
1975 vfs_path_t *vpath;
1977 vpath = vfs_path_from_str (selection (current_panel)->fname);
1978 do_cd (vpath, cd_exact);
1979 vfs_path_free (vpath);
1980 return MSG_HANDLED;
1983 return MSG_NOT_HANDLED;
1986 /* --------------------------------------------------------------------------------------------- */
1988 /* if command line is empty then do 'cd ..' */
1989 static cb_ret_t
1990 force_maybe_cd (void)
1992 if (cmdline->buffer[0] == '\0')
1994 vfs_path_t *up_dir = vfs_path_from_str ("..");
1995 do_cd (up_dir, cd_exact);
1996 vfs_path_free (up_dir);
1997 return MSG_HANDLED;
1999 return MSG_NOT_HANDLED;
2002 /* --------------------------------------------------------------------------------------------- */
2004 static void
2005 unselect_item (WPanel * panel)
2007 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
2010 /* --------------------------------------------------------------------------------------------- */
2012 static void
2013 move_down (WPanel * panel)
2015 if (panel->selected + 1 == panel->count)
2016 return;
2018 unselect_item (panel);
2019 panel->selected++;
2020 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
2022 /* Scroll window half screen */
2023 panel->top_file += ITEMS (panel) / 2;
2024 if (panel->top_file > panel->count - ITEMS (panel))
2025 panel->top_file = panel->count - ITEMS (panel);
2026 paint_dir (panel);
2028 select_item (panel);
2031 /* --------------------------------------------------------------------------------------------- */
2033 static void
2034 move_up (WPanel * panel)
2036 if (panel->selected == 0)
2037 return;
2039 unselect_item (panel);
2040 panel->selected--;
2041 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2043 /* Scroll window half screen */
2044 panel->top_file -= ITEMS (panel) / 2;
2045 if (panel->top_file < 0)
2046 panel->top_file = 0;
2047 paint_dir (panel);
2049 select_item (panel);
2052 /* --------------------------------------------------------------------------------------------- */
2053 /** Changes the selection by lines (may be negative) */
2055 static void
2056 move_selection (WPanel * panel, int lines)
2058 int new_pos;
2059 int adjust = 0;
2061 new_pos = panel->selected + lines;
2062 if (new_pos >= panel->count)
2063 new_pos = panel->count - 1;
2065 if (new_pos < 0)
2066 new_pos = 0;
2068 unselect_item (panel);
2069 panel->selected = new_pos;
2071 if (panel->selected - panel->top_file >= ITEMS (panel))
2073 panel->top_file += lines;
2074 adjust = 1;
2077 if (panel->selected - panel->top_file < 0)
2079 panel->top_file += lines;
2080 adjust = 1;
2083 if (adjust)
2085 if (panel->top_file > panel->selected)
2086 panel->top_file = panel->selected;
2087 if (panel->top_file < 0)
2088 panel->top_file = 0;
2089 paint_dir (panel);
2091 select_item (panel);
2094 /* --------------------------------------------------------------------------------------------- */
2096 static cb_ret_t
2097 move_left (WPanel * panel)
2099 if (panel->split)
2101 move_selection (panel, -llines (panel));
2102 return MSG_HANDLED;
2104 else
2105 return maybe_cd (1); /* cd .. */
2108 /* --------------------------------------------------------------------------------------------- */
2110 static cb_ret_t
2111 move_right (WPanel * panel)
2113 if (panel->split)
2115 move_selection (panel, llines (panel));
2116 return MSG_HANDLED;
2118 else
2119 return maybe_cd (0); /* cd (selection) */
2122 /* --------------------------------------------------------------------------------------------- */
2124 static void
2125 prev_page (WPanel * panel)
2127 int items;
2129 if (!panel->selected && !panel->top_file)
2130 return;
2131 unselect_item (panel);
2132 items = ITEMS (panel);
2133 if (panel->top_file < items)
2134 items = panel->top_file;
2135 if (!items)
2136 panel->selected = 0;
2137 else
2138 panel->selected -= items;
2139 panel->top_file -= items;
2141 select_item (panel);
2142 paint_dir (panel);
2145 /* --------------------------------------------------------------------------------------------- */
2147 static void
2148 goto_parent_dir (WPanel * panel)
2150 if (!panel->is_panelized)
2152 vfs_path_t *up_dir;
2154 up_dir = vfs_path_from_str ("..");
2155 do_cd (up_dir, cd_exact);
2156 vfs_path_free (up_dir);
2158 else
2160 char *fname = panel->dir.list[panel->selected].fname;
2161 const char *bname;
2162 vfs_path_t *dname_vpath;
2164 if (g_path_is_absolute (fname))
2165 fname = g_strdup (fname);
2166 else
2168 char *tmp_root;
2170 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2171 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2172 g_free (tmp_root);
2175 bname = x_basename (fname);
2177 if (bname == fname)
2178 dname_vpath = vfs_path_from_str (".");
2179 else
2181 char *dname;
2183 dname = g_strndup (fname, bname - fname);
2184 dname_vpath = vfs_path_from_str (dname);
2185 g_free (dname);
2188 do_cd (dname_vpath, cd_exact);
2189 try_to_select (panel, bname);
2191 vfs_path_free (dname_vpath);
2192 g_free (fname);
2196 /* --------------------------------------------------------------------------------------------- */
2198 static void
2199 next_page (WPanel * panel)
2201 int items;
2203 if (panel->selected == panel->count - 1)
2204 return;
2205 unselect_item (panel);
2206 items = ITEMS (panel);
2207 if (panel->top_file > panel->count - 2 * items)
2208 items = panel->count - items - panel->top_file;
2209 if (panel->top_file + items < 0)
2210 items = -panel->top_file;
2211 if (!items)
2212 panel->selected = panel->count - 1;
2213 else
2214 panel->selected += items;
2215 panel->top_file += items;
2217 select_item (panel);
2218 paint_dir (panel);
2221 /* --------------------------------------------------------------------------------------------- */
2223 static void
2224 goto_child_dir (WPanel * panel)
2226 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2228 vfs_path_t *vpath;
2230 vpath = vfs_path_from_str (selection (panel)->fname);
2231 do_cd (vpath, cd_exact);
2232 vfs_path_free (vpath);
2236 /* --------------------------------------------------------------------------------------------- */
2238 static void
2239 goto_top_file (WPanel * panel)
2241 unselect_item (panel);
2242 panel->selected = panel->top_file;
2243 select_item (panel);
2246 /* --------------------------------------------------------------------------------------------- */
2248 static void
2249 goto_middle_file (WPanel * panel)
2251 unselect_item (panel);
2252 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2253 select_item (panel);
2256 /* --------------------------------------------------------------------------------------------- */
2258 static void
2259 goto_bottom_file (WPanel * panel)
2261 unselect_item (panel);
2262 panel->selected = panel->top_file + ITEMS (panel) - 1;
2263 select_item (panel);
2266 /* --------------------------------------------------------------------------------------------- */
2268 static void
2269 move_home (WPanel * panel)
2271 if (panel->selected == 0)
2272 return;
2274 unselect_item (panel);
2276 if (panels_options.torben_fj_mode)
2278 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2280 if (panel->selected > middle_pos)
2282 goto_middle_file (panel);
2283 return;
2285 if (panel->selected != panel->top_file)
2287 goto_top_file (panel);
2288 return;
2292 panel->top_file = 0;
2293 panel->selected = 0;
2295 paint_dir (panel);
2296 select_item (panel);
2299 /* --------------------------------------------------------------------------------------------- */
2301 static void
2302 move_end (WPanel * panel)
2304 if (panel->selected == panel->count - 1)
2305 return;
2307 unselect_item (panel);
2309 if (panels_options.torben_fj_mode)
2311 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2313 if (panel->selected < middle_pos)
2315 goto_middle_file (panel);
2316 return;
2318 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2320 goto_bottom_file (panel);
2321 return;
2325 panel->selected = panel->count - 1;
2326 paint_dir (panel);
2327 select_item (panel);
2330 /* --------------------------------------------------------------------------------------------- */
2332 static void
2333 do_mark_file (WPanel * panel, mark_act_t do_move)
2335 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2336 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2337 move_down (panel);
2338 else if (do_move == MARK_FORCE_UP)
2339 move_up (panel);
2342 /* --------------------------------------------------------------------------------------------- */
2344 static void
2345 mark_file (WPanel * panel)
2347 do_mark_file (panel, MARK_DOWN);
2350 /* --------------------------------------------------------------------------------------------- */
2352 static void
2353 mark_file_up (WPanel * panel)
2355 do_mark_file (panel, MARK_FORCE_UP);
2358 /* --------------------------------------------------------------------------------------------- */
2360 static void
2361 mark_file_down (WPanel * panel)
2363 do_mark_file (panel, MARK_FORCE_DOWN);
2366 /* --------------------------------------------------------------------------------------------- */
2368 static void
2369 mark_file_right (WPanel * panel)
2371 int lines = llines (panel);
2373 if (state_mark < 0)
2374 state_mark = selection (panel)->f.marked ? 0 : 1;
2376 lines = min (lines, panel->count - panel->selected - 1);
2377 for (; lines != 0; lines--)
2379 do_file_mark (panel, panel->selected, state_mark);
2380 move_down (panel);
2382 do_file_mark (panel, panel->selected, state_mark);
2385 /* --------------------------------------------------------------------------------------------- */
2387 static void
2388 mark_file_left (WPanel * panel)
2390 int lines = llines (panel);
2392 if (state_mark < 0)
2393 state_mark = selection (panel)->f.marked ? 0 : 1;
2395 lines = min (lines, panel->selected + 1);
2396 for (; lines != 0; lines--)
2398 do_file_mark (panel, panel->selected, state_mark);
2399 move_up (panel);
2401 do_file_mark (panel, panel->selected, state_mark);
2404 /* --------------------------------------------------------------------------------------------- */
2405 /** Incremental search of a file name in the panel.
2406 * @param panel instance of WPanel structure
2407 * @param c_code key code
2410 static void
2411 do_search (WPanel * panel, int c_code)
2413 size_t l;
2414 int i, sel;
2415 gboolean wrapped = FALSE;
2416 char *act;
2417 mc_search_t *search;
2418 char *reg_exp, *esc_str;
2419 gboolean is_found = FALSE;
2421 l = strlen (panel->search_buffer);
2422 if (c_code == KEY_BACKSPACE)
2424 if (l != 0)
2426 act = panel->search_buffer + l;
2427 str_prev_noncomb_char (&act, panel->search_buffer);
2428 act[0] = '\0';
2430 panel->search_chpoint = 0;
2432 else
2434 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2436 panel->search_char[panel->search_chpoint] = c_code;
2437 panel->search_chpoint++;
2440 if (panel->search_chpoint > 0)
2442 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2444 case -2:
2445 return;
2446 case -1:
2447 panel->search_chpoint = 0;
2448 return;
2449 default:
2450 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2452 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2453 l += panel->search_chpoint;
2454 *(panel->search_buffer + l) = '\0';
2455 panel->search_chpoint = 0;
2461 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2462 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2463 search = mc_search_new (esc_str, -1);
2464 search->search_type = MC_SEARCH_T_GLOB;
2465 search->is_entire_line = TRUE;
2466 switch (panels_options.qsearch_mode)
2468 case QSEARCH_CASE_SENSITIVE:
2469 search->is_case_sensitive = TRUE;
2470 break;
2471 case QSEARCH_CASE_INSENSITIVE:
2472 search->is_case_sensitive = FALSE;
2473 break;
2474 default:
2475 search->is_case_sensitive = panel->sort_info.case_sensitive;
2476 break;
2478 sel = panel->selected;
2479 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2481 if (i >= panel->count)
2483 i = 0;
2484 if (wrapped)
2485 break;
2486 wrapped = TRUE;
2488 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2490 sel = i;
2491 is_found = TRUE;
2492 break;
2495 if (is_found)
2497 unselect_item (panel);
2498 panel->selected = sel;
2499 select_item (panel);
2500 send_message (panel, NULL, MSG_DRAW, 0, NULL);
2502 else if (c_code != KEY_BACKSPACE)
2504 act = panel->search_buffer + l;
2505 str_prev_noncomb_char (&act, panel->search_buffer);
2506 act[0] = '\0';
2508 mc_search_free (search);
2509 g_free (reg_exp);
2510 g_free (esc_str);
2513 /* --------------------------------------------------------------------------------------------- */
2514 /** Start new search.
2515 * @param panel instance of WPanel structure
2518 static void
2519 start_search (WPanel * panel)
2521 if (panel->searching)
2523 if (panel->selected + 1 == panel->count)
2524 panel->selected = 0;
2525 else
2526 move_down (panel);
2528 /* in case if there was no search string we need to recall
2529 previous string, with which we ended previous searching */
2530 if (panel->search_buffer[0] == '\0')
2531 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2532 sizeof (panel->search_buffer));
2534 do_search (panel, 0);
2536 else
2538 panel->searching = TRUE;
2539 panel->search_buffer[0] = '\0';
2540 panel->search_char[0] = '\0';
2541 panel->search_chpoint = 0;
2542 display_mini_info (panel);
2543 mc_refresh ();
2547 /* --------------------------------------------------------------------------------------------- */
2549 static void
2550 stop_search (WPanel * panel)
2552 panel->searching = FALSE;
2554 /* if user had overrdied search string, we need to store it
2555 to the previous_search_buffer */
2556 if (panel->search_buffer[0] != '\0')
2557 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2558 sizeof (panel->prev_search_buffer));
2560 display_mini_info (panel);
2563 /* --------------------------------------------------------------------------------------------- */
2564 /** Return 1 if the Enter key has been processed, 0 otherwise */
2566 static int
2567 do_enter_on_file_entry (file_entry * fe)
2569 vfs_path_t *full_name_vpath;
2570 gboolean ok;
2573 * Directory or link to directory - change directory.
2574 * Try the same for the entries on which mc_lstat() has failed.
2576 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2578 vfs_path_t *fname_vpath;
2580 fname_vpath = vfs_path_from_str (fe->fname);
2581 if (!do_cd (fname_vpath, cd_exact))
2582 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2583 vfs_path_free (fname_vpath);
2584 return 1;
2587 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2589 /* Try associated command */
2590 if (regex_command (full_name_vpath, "Open") != 0)
2592 vfs_path_free (full_name_vpath);
2593 return 1;
2596 /* Check if the file is executable */
2597 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2598 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2599 vfs_path_free (full_name_vpath);
2600 if (!ok)
2601 return 0;
2603 if (confirm_execute)
2605 if (query_dialog
2606 (_("The Midnight Commander"),
2607 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2608 return 1;
2611 if (!vfs_current_is_local ())
2613 int ret;
2614 vfs_path_t *tmp_vpath;
2616 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2617 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2618 vfs_path_free (tmp_vpath);
2619 /* We took action only if the dialog was shown or the execution
2620 * was successful */
2621 return confirm_execute || (ret == 0);
2625 char *tmp = name_quote (fe->fname, 0);
2626 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2627 g_free (tmp);
2628 shell_execute (cmd, 0);
2629 g_free (cmd);
2632 #ifdef HAVE_CHARSET
2633 mc_global.source_codepage = default_source_codepage;
2634 #endif
2636 return 1;
2639 /* --------------------------------------------------------------------------------------------- */
2641 static int
2642 do_enter (WPanel * panel)
2644 return do_enter_on_file_entry (selection (panel));
2647 /* --------------------------------------------------------------------------------------------- */
2649 static void
2650 chdir_other_panel (WPanel * panel)
2652 const file_entry *entry = &panel->dir.list[panel->selected];
2654 vfs_path_t *new_dir_vpath;
2655 char *sel_entry = NULL;
2657 if (get_other_type () != view_listing)
2659 set_display_type (get_other_index (), view_listing);
2662 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2663 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2664 else
2666 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2667 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2670 change_panel ();
2671 do_cd (new_dir_vpath, cd_exact);
2672 vfs_path_free (new_dir_vpath);
2674 if (sel_entry)
2675 try_to_select (current_panel, sel_entry);
2676 change_panel ();
2678 move_down (panel);
2681 /* --------------------------------------------------------------------------------------------- */
2683 * Make the current directory of the current panel also the current
2684 * directory of the other panel. Put the other panel to the listing
2685 * mode if needed. If the current panel is panelized, the other panel
2686 * doesn't become panelized.
2689 static void
2690 panel_sync_other (const WPanel * panel)
2692 if (get_other_type () != view_listing)
2694 set_display_type (get_other_index (), view_listing);
2697 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2699 /* try to select current filename on the other panel */
2700 if (!panel->is_panelized)
2702 try_to_select (other_panel, selection (panel)->fname);
2706 /* --------------------------------------------------------------------------------------------- */
2708 static void
2709 chdir_to_readlink (WPanel * panel)
2711 vfs_path_t *new_dir_vpath;
2712 char buffer[MC_MAXPATHLEN], *p;
2713 int i;
2714 struct stat st;
2715 vfs_path_t *panel_fname_vpath;
2716 gboolean ok;
2718 if (get_other_type () != view_listing)
2719 return;
2721 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2722 return;
2724 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2725 if (i < 0)
2726 return;
2728 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2729 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2730 vfs_path_free (panel_fname_vpath);
2731 if (!ok)
2732 return;
2734 buffer[i] = 0;
2735 if (!S_ISDIR (st.st_mode))
2737 p = strrchr (buffer, PATH_SEP);
2738 if (p && !p[1])
2740 *p = 0;
2741 p = strrchr (buffer, PATH_SEP);
2743 if (!p)
2744 return;
2745 p[1] = 0;
2747 if (*buffer == PATH_SEP)
2748 new_dir_vpath = vfs_path_from_str (buffer);
2749 else
2750 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2752 change_panel ();
2753 do_cd (new_dir_vpath, cd_exact);
2754 vfs_path_free (new_dir_vpath);
2755 change_panel ();
2757 move_down (panel);
2760 /* --------------------------------------------------------------------------------------------- */
2762 static gsize
2763 panel_get_format_field_count (WPanel * panel)
2765 format_e *format;
2766 gsize lc_index;
2767 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2768 return lc_index;
2771 /* --------------------------------------------------------------------------------------------- */
2773 function return 0 if not found and REAL_INDEX+1 if found
2776 static gsize
2777 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2779 format_e *format;
2780 gsize lc_index;
2782 for (lc_index = 1, format = panel->format;
2783 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2784 if (format == NULL)
2785 lc_index = 0;
2787 return lc_index;
2790 /* --------------------------------------------------------------------------------------------- */
2792 static format_e *
2793 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2795 format_e *format;
2796 for (format = panel->format;
2797 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2798 return format;
2801 /* --------------------------------------------------------------------------------------------- */
2803 static const panel_field_t *
2804 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2806 const panel_field_t *pfield;
2807 format_e *format;
2809 format = panel_get_format_field_by_index (panel, lc_index);
2810 if (format == NULL)
2811 return NULL;
2812 pfield = panel_get_field_by_title (format->title);
2813 if (pfield == NULL)
2814 return NULL;
2815 if (pfield->sort_routine == NULL)
2816 return NULL;
2817 return pfield;
2820 /* --------------------------------------------------------------------------------------------- */
2822 static void
2823 panel_toggle_sort_order_prev (WPanel * panel)
2825 gsize lc_index, i;
2826 gchar *title;
2828 const panel_field_t *pfield = NULL;
2830 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2831 lc_index = panel_get_format_field_index_by_name (panel, title);
2832 g_free (title);
2834 if (lc_index > 1)
2836 /* search for prev sortable column in panel format */
2837 for (i = lc_index - 1;
2838 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2841 if (pfield == NULL)
2843 /* Sortable field not found. Try to search in each array */
2844 for (i = panel_get_format_field_count (panel);
2845 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2848 if (pfield != NULL)
2850 panel->sort_info.sort_field = pfield;
2851 panel_set_sort_order (panel, pfield);
2855 /* --------------------------------------------------------------------------------------------- */
2857 static void
2858 panel_toggle_sort_order_next (WPanel * panel)
2860 gsize lc_index, i;
2861 const panel_field_t *pfield = NULL;
2862 gsize format_field_count;
2863 gchar *title;
2865 format_field_count = panel_get_format_field_count (panel);
2866 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2867 lc_index = panel_get_format_field_index_by_name (panel, title);
2868 g_free (title);
2870 if (lc_index != 0 && lc_index != format_field_count)
2872 /* search for prev sortable column in panel format */
2873 for (i = lc_index;
2874 i != format_field_count
2875 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2878 if (pfield == NULL)
2880 /* Sortable field not found. Try to search in each array */
2881 for (i = 0;
2882 i != format_field_count
2883 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2886 if (pfield != NULL)
2888 panel->sort_info.sort_field = pfield;
2889 panel_set_sort_order (panel, pfield);
2893 /* --------------------------------------------------------------------------------------------- */
2895 static void
2896 panel_select_sort_order (WPanel * panel)
2898 const panel_field_t *sort_order;
2900 sort_order = sort_box (&panel->sort_info);
2901 if (sort_order != NULL)
2903 panel->sort_info.sort_field = sort_order;
2904 panel_set_sort_order (panel, sort_order);
2908 /* --------------------------------------------------------------------------------------------- */
2911 * panel_content_scroll_left:
2912 * @param panel the pointer to the panel on which we operate
2914 * scroll long filename to the left (decrement scroll pointer)
2918 static void
2919 panel_content_scroll_left (WPanel * panel)
2921 if (panel->content_shift > -1)
2923 if (panel->content_shift > panel->max_shift)
2924 panel->content_shift = panel->max_shift;
2926 panel->content_shift--;
2927 show_dir (panel);
2928 paint_dir (panel);
2933 /* --------------------------------------------------------------------------------------------- */
2936 * panel_content_scroll_right:
2937 * @param panel the pointer to the panel on which we operate
2939 * scroll long filename to the right (increment scroll pointer)
2943 static void
2944 panel_content_scroll_right (WPanel * panel)
2946 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
2948 panel->content_shift++;
2949 show_dir (panel);
2950 paint_dir (panel);
2954 /* --------------------------------------------------------------------------------------------- */
2956 static void
2957 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2959 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2961 const panel_field_t *sort_order;
2963 sort_order = panel_get_field_by_id (name);
2964 if (sort_order == NULL)
2965 return;
2966 panel->sort_info.sort_field = sort_order;
2968 else
2969 panel->sort_info.reverse = !panel->sort_info.reverse;
2971 panel_set_sort_order (panel, panel->sort_info.sort_field);
2974 /* --------------------------------------------------------------------------------------------- */
2976 * If we moved to the parent directory move the selection pointer to
2977 * the old directory name; If we leave VFS dir, remove FS specificator.
2979 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2982 static const char *
2983 get_parent_dir_name (const char *cwd, const char *lwd)
2985 size_t llen, clen;
2986 const char *p;
2988 llen = strlen (lwd);
2989 clen = strlen (cwd);
2991 if (llen <= clen)
2992 return NULL;
2994 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2996 if (p == NULL)
2998 p = strrchr (lwd, PATH_SEP);
3000 if ((p != NULL)
3001 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
3002 && (clen == (size_t) (p - lwd)
3003 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
3004 return (p + 1);
3006 return NULL;
3009 /* skip VFS prefix */
3010 while (--p > lwd && *p != PATH_SEP)
3012 /* get last component */
3013 while (--p > lwd && *p != PATH_SEP)
3016 /* return last component */
3017 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
3020 /* --------------------------------------------------------------------------------------------- */
3021 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3023 static void
3024 subshell_chdir (const vfs_path_t * vpath)
3026 #ifdef ENABLE_SUBSHELL
3027 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3028 do_subshell_chdir (vpath, FALSE, TRUE);
3029 #else /* ENABLE_SUBSHELL */
3030 (void) vpath;
3031 #endif /* ENABLE_SUBSHELL */
3034 /* --------------------------------------------------------------------------------------------- */
3036 * Changes the current directory of the panel.
3037 * Don't record change in the directory history.
3040 static gboolean
3041 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3043 char *olddir;
3045 olddir = vfs_path_to_str (panel->cwd_vpath);
3047 /* Convert *new_path to a suitable pathname, handle ~user */
3048 if (cd_type == cd_parse_command)
3050 const vfs_path_element_t *element;
3052 element = vfs_path_get_by_index (new_dir_vpath, 0);
3053 if (strcmp (element->path, "-") == 0)
3054 new_dir_vpath = panel->lwd_vpath;
3057 if (mc_chdir (new_dir_vpath) == -1)
3059 panel_set_cwd (panel, olddir);
3060 g_free (olddir);
3061 return FALSE;
3064 /* Success: save previous directory, shutdown status of previous dir */
3065 panel_set_lwd (panel, olddir);
3066 input_free_completions (cmdline);
3068 vfs_path_free (panel->cwd_vpath);
3069 vfs_setup_cwd ();
3070 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3072 vfs_release_path (olddir);
3074 subshell_chdir (panel->cwd_vpath);
3076 /* Reload current panel */
3077 panel_clean_dir (panel);
3080 char *tmp_path;
3082 panel->count =
3083 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
3084 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3085 panel->sort_info.exec_first, panel->filter);
3086 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3087 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
3088 g_free (tmp_path);
3091 load_hint (0);
3092 panel->dirty = 1;
3093 update_xterm_title_path ();
3095 g_free (olddir);
3097 return TRUE;
3100 /* --------------------------------------------------------------------------------------------- */
3102 static void
3103 directory_history_next (WPanel * panel)
3105 gboolean ok;
3109 GList *next;
3111 ok = TRUE;
3112 next = g_list_next (panel->dir_history_current);
3113 if (next != NULL)
3115 vfs_path_t *data_vpath;
3117 data_vpath = vfs_path_from_str ((char *) next->data);
3118 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3119 vfs_path_free (data_vpath);
3120 panel->dir_history_current = next;
3122 /* skip directories that present in history but absent in file system */
3124 while (!ok);
3127 /* --------------------------------------------------------------------------------------------- */
3129 static void
3130 directory_history_prev (WPanel * panel)
3132 gboolean ok;
3136 GList *prev;
3138 ok = TRUE;
3139 prev = g_list_previous (panel->dir_history_current);
3140 if (prev != NULL)
3142 vfs_path_t *data_vpath;
3144 data_vpath = vfs_path_from_str ((char *) prev->data);
3145 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3146 vfs_path_free (data_vpath);
3147 panel->dir_history_current = prev;
3149 /* skip directories that present in history but absent in file system */
3151 while (!ok);
3154 /* --------------------------------------------------------------------------------------------- */
3156 static void
3157 directory_history_list (WPanel * panel)
3159 char *s;
3160 gboolean ok = FALSE;
3161 size_t pos;
3163 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3165 s = history_show (&panel->dir_history, WIDGET (panel), pos);
3166 if (s != NULL)
3168 vfs_path_t *s_vpath;
3170 s_vpath = vfs_path_from_str (s);
3171 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3172 if (ok)
3173 directory_history_add (panel, panel->cwd_vpath);
3174 else
3175 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3176 vfs_path_free (s_vpath);
3177 g_free (s);
3180 if (!ok)
3182 /* Since history is fully modified in history_show(), panel->dir_history actually
3183 * points to the invalid place. Try restore current postition here. */
3185 size_t i;
3187 panel->dir_history_current = panel->dir_history;
3189 for (i = 0; i <= pos; i++)
3191 GList *prev;
3193 prev = g_list_previous (panel->dir_history_current);
3194 if (prev == NULL)
3195 break;
3197 panel->dir_history_current = prev;
3202 /* --------------------------------------------------------------------------------------------- */
3204 static cb_ret_t
3205 panel_execute_cmd (WPanel * panel, unsigned long command)
3207 int res = MSG_HANDLED;
3209 if (command != CK_Search)
3210 stop_search (panel);
3213 switch (command)
3215 case CK_Up:
3216 case CK_Down:
3217 case CK_Left:
3218 case CK_Right:
3219 case CK_Bottom:
3220 case CK_Top:
3221 case CK_PageDown:
3222 case CK_PageUp:
3223 /* reset state of marks flag */
3224 state_mark = -1;
3225 break;
3227 switch (command)
3229 case CK_PanelOtherCd:
3230 chdir_other_panel (panel);
3231 break;
3232 case CK_PanelOtherCdLink:
3233 chdir_to_readlink (panel);
3234 break;
3235 case CK_CopySingle:
3236 copy_cmd_local ();
3237 break;
3238 case CK_DeleteSingle:
3239 delete_cmd_local ();
3240 break;
3241 case CK_Enter:
3242 do_enter (panel);
3243 break;
3244 case CK_ViewRaw:
3245 view_raw_cmd ();
3246 break;
3247 case CK_EditNew:
3248 edit_cmd_new ();
3249 break;
3250 case CK_MoveSingle:
3251 rename_cmd_local ();
3252 break;
3253 case CK_SelectInvert:
3254 select_invert_cmd ();
3255 break;
3256 case CK_Select:
3257 select_cmd ();
3258 break;
3259 case CK_Unselect:
3260 unselect_cmd ();
3261 break;
3262 case CK_PageDown:
3263 next_page (panel);
3264 break;
3265 case CK_PageUp:
3266 prev_page (panel);
3267 break;
3268 case CK_CdChild:
3269 goto_child_dir (panel);
3270 break;
3271 case CK_CdParent:
3272 goto_parent_dir (panel);
3273 break;
3274 case CK_History:
3275 directory_history_list (panel);
3276 break;
3277 case CK_HistoryNext:
3278 directory_history_next (panel);
3279 break;
3280 case CK_HistoryPrev:
3281 directory_history_prev (panel);
3282 break;
3283 case CK_BottomOnScreen:
3284 goto_bottom_file (panel);
3285 break;
3286 case CK_MiddleOnScreen:
3287 goto_middle_file (panel);
3288 break;
3289 case CK_TopOnScreen:
3290 goto_top_file (panel);
3291 break;
3292 case CK_Mark:
3293 mark_file (panel);
3294 break;
3295 case CK_MarkUp:
3296 mark_file_up (panel);
3297 break;
3298 case CK_MarkDown:
3299 mark_file_down (panel);
3300 break;
3301 case CK_MarkLeft:
3302 mark_file_left (panel);
3303 break;
3304 case CK_MarkRight:
3305 mark_file_right (panel);
3306 break;
3307 case CK_CdParentSmart:
3308 res = force_maybe_cd ();
3309 break;
3310 case CK_Up:
3311 move_up (panel);
3312 break;
3313 case CK_Down:
3314 move_down (panel);
3315 break;
3316 case CK_Left:
3317 res = move_left (panel);
3318 break;
3319 case CK_Right:
3320 res = move_right (panel);
3321 break;
3322 case CK_Bottom:
3323 move_end (panel);
3324 break;
3325 case CK_Top:
3326 move_home (panel);
3327 break;
3328 #ifdef HAVE_CHARSET
3329 case CK_SelectCodepage:
3330 panel_change_encoding (panel);
3331 break;
3332 #endif
3333 case CK_ScrollLeft:
3334 panel_content_scroll_left (panel);
3335 break;
3336 case CK_ScrollRight:
3337 panel_content_scroll_right (panel);
3338 break;
3339 case CK_Search:
3340 start_search (panel);
3341 break;
3342 case CK_SearchStop:
3343 break;
3344 case CK_PanelOtherSync:
3345 panel_sync_other (panel);
3346 break;
3347 case CK_Sort:
3348 panel_select_sort_order (panel);
3349 break;
3350 case CK_SortPrev:
3351 panel_toggle_sort_order_prev (panel);
3352 break;
3353 case CK_SortNext:
3354 panel_toggle_sort_order_next (panel);
3355 break;
3356 case CK_SortReverse:
3357 panel->sort_info.reverse = !panel->sort_info.reverse;
3358 panel_set_sort_order (panel, panel->sort_info.sort_field);
3359 break;
3360 case CK_SortByName:
3361 panel_set_sort_type_by_id (panel, "name");
3362 break;
3363 case CK_SortByExt:
3364 panel_set_sort_type_by_id (panel, "extension");
3365 break;
3366 case CK_SortBySize:
3367 panel_set_sort_type_by_id (panel, "size");
3368 break;
3369 case CK_SortByMTime:
3370 panel_set_sort_type_by_id (panel, "mtime");
3371 break;
3373 return res;
3376 /* --------------------------------------------------------------------------------------------- */
3378 static cb_ret_t
3379 panel_key (WPanel * panel, int key)
3381 size_t i;
3383 if (is_abort_char (key))
3385 stop_search (panel);
3386 return MSG_HANDLED;
3389 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3391 do_search (panel, key);
3392 return MSG_HANDLED;
3395 for (i = 0; panel_map[i].key != 0; i++)
3396 if (key == panel_map[i].key)
3397 return panel_execute_cmd (panel, panel_map[i].command);
3399 if (panels_options.torben_fj_mode && key == ALT ('h'))
3401 goto_middle_file (panel);
3402 return MSG_HANDLED;
3405 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3407 start_search (panel);
3408 do_search (panel, key);
3409 return MSG_HANDLED;
3412 return MSG_NOT_HANDLED;
3415 /* --------------------------------------------------------------------------------------------- */
3417 static cb_ret_t
3418 panel_callback (Widget * w, Widget * sender, widget_msg_t msg, int parm, void *data)
3420 WPanel *panel = (WPanel *) w;
3421 WButtonBar *bb;
3423 switch (msg)
3425 case MSG_INIT:
3426 /* subscribe to "history_load" event */
3427 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3428 /* subscribe to "history_save" event */
3429 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3430 return MSG_HANDLED;
3432 case MSG_DRAW:
3433 /* Repaint everything, including frame and separator */
3434 paint_frame (panel); /* including show_dir */
3435 paint_dir (panel);
3436 mini_info_separator (panel);
3437 display_mini_info (panel);
3438 panel->dirty = 0;
3439 return MSG_HANDLED;
3441 case MSG_FOCUS:
3442 state_mark = -1;
3443 current_panel = panel;
3444 panel->active = 1;
3445 if (mc_chdir (panel->cwd_vpath) != 0)
3447 char *cwd;
3449 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3450 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3451 cwd, unix_error_string (errno));
3452 g_free (cwd);
3454 else
3455 subshell_chdir (panel->cwd_vpath);
3457 update_xterm_title_path ();
3458 select_item (panel);
3459 show_dir (panel);
3460 paint_dir (panel);
3461 panel->dirty = 0;
3463 bb = find_buttonbar (w->owner);
3464 midnight_set_buttonbar (bb);
3465 buttonbar_redraw (bb);
3466 return MSG_HANDLED;
3468 case MSG_UNFOCUS:
3469 /* Janne: look at this for the multiple panel options */
3470 stop_search (panel);
3471 panel->active = 0;
3472 show_dir (panel);
3473 unselect_item (panel);
3474 return MSG_HANDLED;
3476 case MSG_KEY:
3477 return panel_key (panel, parm);
3479 case MSG_ACTION:
3480 return panel_execute_cmd (panel, parm);
3482 case MSG_DESTROY:
3483 /* unsubscribe from "history_load" event */
3484 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3485 /* unsubscribe from "history_save" event */
3486 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3487 panel_destroy (panel);
3488 free_my_statfs ();
3489 return MSG_HANDLED;
3491 default:
3492 return widget_default_callback (w, sender, msg, parm, data);
3496 /* --------------------------------------------------------------------------------------------- */
3497 /* */
3498 /* Panel mouse events support routines */
3499 /* */
3501 static void
3502 mouse_toggle_mark (WPanel * panel)
3504 do_mark_file (panel, MARK_DONT_MOVE);
3505 mouse_marking = selection (panel)->f.marked;
3506 mouse_mark_panel = current_panel;
3509 /* --------------------------------------------------------------------------------------------- */
3511 static void
3512 mouse_set_mark (WPanel * panel)
3515 if (mouse_mark_panel == panel)
3517 if (mouse_marking && !(selection (panel)->f.marked))
3518 do_mark_file (panel, MARK_DONT_MOVE);
3519 else if (!mouse_marking && (selection (panel)->f.marked))
3520 do_mark_file (panel, MARK_DONT_MOVE);
3524 /* --------------------------------------------------------------------------------------------- */
3526 static gboolean
3527 mark_if_marking (WPanel * panel, Gpm_Event * event)
3529 if ((event->buttons & GPM_B_RIGHT) != 0)
3531 if ((event->type & GPM_DOWN) != 0)
3532 mouse_toggle_mark (panel);
3533 else
3534 mouse_set_mark (panel);
3535 return TRUE;
3537 return FALSE;
3540 /* --------------------------------------------------------------------------------------------- */
3541 /** Determine which column was clicked, and sort the panel on
3542 * that column, or reverse sort on that column if already
3543 * sorted on that column.
3546 static void
3547 mouse_sort_col (WPanel * panel, int x)
3549 int i;
3550 const char *lc_sort_name = NULL;
3551 panel_field_t *col_sort_format = NULL;
3552 format_e *format;
3553 gchar *title;
3555 for (i = 0, format = panel->format; format != NULL; format = format->next)
3557 i += format->field_len;
3558 if (x < i + 1)
3560 /* found column */
3561 lc_sort_name = format->title;
3562 break;
3566 if (lc_sort_name == NULL)
3567 return;
3569 for (i = 0; panel_fields[i].id != NULL; i++)
3571 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3572 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3574 col_sort_format = &panel_fields[i];
3575 g_free (title);
3576 break;
3578 g_free (title);
3581 if (col_sort_format == NULL)
3582 return;
3584 if (panel->sort_info.sort_field == col_sort_format)
3586 /* reverse the sort if clicked column is already the sorted column */
3587 panel->sort_info.reverse = !panel->sort_info.reverse;
3589 else
3591 /* new sort is forced to be ascending */
3592 panel->sort_info.reverse = FALSE;
3594 panel_set_sort_order (panel, col_sort_format);
3598 /* --------------------------------------------------------------------------------------------- */
3600 * Mouse callback of the panel minus repainting.
3602 static int
3603 panel_event (Gpm_Event * event, void *data)
3605 WPanel *panel = (WPanel *) data;
3606 Widget *w = WIDGET (data);
3608 const int lines = llines (panel);
3609 const gboolean is_active = dlg_widget_active (panel);
3610 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3611 Gpm_Event local;
3613 if (!mouse_global_in_widget (event, WIDGET (data)))
3614 return MOU_UNHANDLED;
3616 local = mouse_get_local (event, w);
3618 /* 1st line */
3619 if (local.y == 1)
3621 /* "<" button */
3622 if (mouse_down && local.x == 2)
3624 directory_history_prev (panel);
3625 goto finish;
3628 /* ">" button */
3629 if (mouse_down && local.x == w->cols - 1)
3631 directory_history_next (panel);
3632 goto finish;
3635 /* "^" button */
3636 if (mouse_down && local.x >= w->cols - 4 && local.x <= w->cols - 2)
3638 directory_history_list (panel);
3639 goto finish;
3642 /* "." button show/hide hidden files */
3643 if (mouse_down && local.x == w->cols - 5)
3645 send_message (midnight_dlg, NULL, MSG_ACTION, CK_ShowHidden, NULL);
3646 goto finish;
3649 /* no other events on 1st line */
3650 return MOU_UNHANDLED;
3653 /* sort on clicked column; don't handle wheel events */
3654 if (mouse_down && (local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && local.y == 2)
3656 mouse_sort_col (panel, local.x);
3657 goto finish;
3660 /* Mouse wheel events */
3661 if (mouse_down && (local.buttons & GPM_B_UP) != 0)
3663 if (is_active)
3665 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3666 prev_page (panel);
3667 else /* We are in first page */
3668 move_up (panel);
3670 goto finish;
3673 if (mouse_down && (local.buttons & GPM_B_DOWN) != 0)
3675 if (is_active)
3677 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3678 next_page (panel);
3679 else /* We are in last page */
3680 move_down (panel);
3682 goto finish;
3685 local.y -= 2;
3686 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
3688 int my_index;
3690 if (!is_active)
3691 change_panel ();
3693 if (panel->top_file + local.y > panel->count)
3694 my_index = panel->count - 1;
3695 else
3697 my_index = panel->top_file + local.y - 1;
3698 if (panel->split && (local.x > (w->cols - 2) / 2))
3699 my_index += llines (panel);
3701 if (my_index >= panel->count)
3702 my_index = panel->count - 1;
3705 if (my_index != panel->selected)
3707 unselect_item (panel);
3708 panel->selected = my_index;
3709 select_item (panel);
3712 /* This one is new */
3713 mark_if_marking (panel, &local);
3715 else if ((local.type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE) &&
3716 local.y > 0 && local.y <= lines)
3717 do_enter (panel);
3719 finish:
3720 if (panel->dirty)
3721 send_message (w, NULL, MSG_DRAW, 0, NULL);
3723 return MOU_NORMAL;
3726 /* --------------------------------------------------------------------------------------------- */
3728 static void
3729 reload_panelized (WPanel * panel)
3731 int i, j;
3732 dir_list *list = &panel->dir;
3734 if (panel != current_panel)
3735 (void) mc_chdir (panel->cwd_vpath);
3737 for (i = 0, j = 0; i < panel->count; i++)
3739 vfs_path_t *vpath;
3741 if (list->list[i].f.marked)
3743 /* Unmark the file in advance. In case the following mc_lstat
3744 * fails we are done, else we have to mark the file again
3745 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3746 * IMO that's the best way to update the panel's summary status
3747 * -- Norbert
3749 do_file_mark (panel, i, 0);
3751 vpath = vfs_path_from_str (list->list[i].fname);
3752 if (mc_lstat (vpath, &list->list[i].st))
3753 g_free (list->list[i].fname);
3754 else
3756 if (list->list[i].f.marked)
3757 do_file_mark (panel, i, 1);
3758 if (j != i)
3759 list->list[j] = list->list[i];
3760 j++;
3762 vfs_path_free (vpath);
3764 if (j == 0)
3765 panel->count = set_zero_dir (list) ? 1 : 0;
3766 else
3767 panel->count = j;
3769 if (panel != current_panel)
3770 (void) mc_chdir (current_panel->cwd_vpath);
3773 /* --------------------------------------------------------------------------------------------- */
3775 static void
3776 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3778 gboolean free_pointer;
3779 char *my_current_file = NULL;
3781 if ((flags & UP_RELOAD) != 0)
3783 panel->is_panelized = FALSE;
3784 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3785 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3788 /* If current_file == -1 (an invalid pointer) then preserve selection */
3789 free_pointer = current_file == UP_KEEPSEL;
3791 if (free_pointer)
3793 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3794 current_file = my_current_file;
3797 if (panel->is_panelized)
3798 reload_panelized (panel);
3799 else
3800 panel_reload (panel);
3802 try_to_select (panel, current_file);
3803 panel->dirty = 1;
3805 if (free_pointer)
3806 g_free (my_current_file);
3809 /* --------------------------------------------------------------------------------------------- */
3811 static void
3812 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3814 if (get_display_type (which) == view_listing)
3816 WPanel *panel;
3818 panel = (WPanel *) get_panel_widget (which);
3819 if (panel->is_panelized)
3820 flags &= ~UP_RELOAD;
3821 update_one_panel_widget (panel, flags, current_file);
3825 /* --------------------------------------------------------------------------------------------- */
3827 static void
3828 do_select (WPanel * panel, int i)
3830 if (i != panel->selected)
3832 panel->dirty = 1;
3833 panel->selected = i;
3834 panel->top_file = panel->selected - (WIDGET (panel)->lines - 2) / 2;
3835 if (panel->top_file < 0)
3836 panel->top_file = 0;
3840 /* --------------------------------------------------------------------------------------------- */
3842 static void
3843 do_try_to_select (WPanel * panel, const char *name)
3845 int i;
3846 char *subdir;
3848 if (!name)
3850 do_select (panel, 0);
3851 return;
3854 /* We only want the last component of the directory,
3855 * and from this only the name without suffix.
3856 * Cut prefix if the panel is not panelized */
3858 if (panel->is_panelized)
3859 subdir = vfs_strip_suffix_from_filename (name);
3860 else
3861 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3863 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3864 for (i = 0; i < panel->count; i++)
3866 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3868 do_select (panel, i);
3869 g_free (subdir);
3870 return;
3874 /* Try to select a file near the file that is missing */
3875 if (panel->selected >= panel->count)
3876 do_select (panel, panel->count - 1);
3877 g_free (subdir);
3880 /* --------------------------------------------------------------------------------------------- */
3882 /* event callback */
3883 static gboolean
3884 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3885 gpointer init_data, gpointer data)
3887 (void) event_group_name;
3888 (void) event_name;
3889 (void) init_data;
3890 (void) data;
3892 update_panels (UP_RELOAD, UP_KEEPSEL);
3894 return TRUE;
3897 /* --------------------------------------------------------------------------------------------- */
3899 /* event callback */
3900 static gboolean
3901 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3902 gpointer init_data, gpointer data)
3904 (void) event_group_name;
3905 (void) event_name;
3906 (void) init_data;
3907 (void) data;
3909 if (current_panel->marked == 0)
3910 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3911 (gpointer) selection (current_panel)->fname);
3912 else
3914 int i;
3915 gboolean first = TRUE;
3916 char *flist = NULL;
3918 for (i = 0; i < current_panel->count; i++)
3919 if (current_panel->dir.list[i].f.marked != 0)
3920 { /* Skip the unmarked ones */
3921 if (first)
3923 flist = g_strdup (current_panel->dir.list[i].fname);
3924 first = FALSE;
3926 else
3928 /* Add empty lines after the file */
3929 char *tmp;
3931 tmp =
3932 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3933 g_free (flist);
3934 flist = tmp;
3938 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3939 g_free (flist);
3941 return TRUE;
3944 /* --------------------------------------------------------------------------------------------- */
3945 /*** public functions ****************************************************************************/
3946 /* --------------------------------------------------------------------------------------------- */
3948 void
3949 try_to_select (WPanel * panel, const char *name)
3951 do_try_to_select (panel, name);
3952 select_item (panel);
3955 /* --------------------------------------------------------------------------------------------- */
3957 void
3958 panel_clean_dir (WPanel * panel)
3960 int count = panel->count;
3962 panel->count = 0;
3963 panel->top_file = 0;
3964 panel->selected = 0;
3965 panel->marked = 0;
3966 panel->dirs_marked = 0;
3967 panel->total = 0;
3968 panel->searching = FALSE;
3969 panel->is_panelized = FALSE;
3970 panel->dirty = 1;
3971 panel->content_shift = -1;
3972 panel->max_shift = -1;
3974 clean_dir (&panel->dir, count);
3977 /* --------------------------------------------------------------------------------------------- */
3979 * Set Up panel's current dir object
3981 * @param panel panel object
3982 * @param path_str string contain path
3985 void
3986 panel_set_cwd (WPanel * panel, const char *path_str)
3988 vfs_path_free (panel->cwd_vpath);
3989 panel->cwd_vpath = vfs_path_from_str (path_str);
3992 /* --------------------------------------------------------------------------------------------- */
3994 * Set Up panel's last working dir object
3996 * @param panel panel object
3997 * @param path_str string contain path
4000 void
4001 panel_set_lwd (WPanel * panel, const char *path_str)
4003 vfs_path_free (panel->lwd_vpath);
4004 panel->lwd_vpath = vfs_path_from_str (path_str);
4007 /* --------------------------------------------------------------------------------------------- */
4008 /** Panel creation.
4009 * @param panel_name the name of the panel for setup retieving
4010 * @return new instance of WPanel
4013 WPanel *
4014 panel_new (const char *panel_name)
4016 return panel_new_with_dir (panel_name, NULL);
4019 /* --------------------------------------------------------------------------------------------- */
4021 * Panel creation for specified directory.
4023 * @param panel_name specifies the name of the panel for setup retieving
4024 * @param wpath the path of working panel directory. If path is NULL then panel will be created
4025 * for current directory
4027 * @return new instance of WPanel
4030 WPanel *
4031 panel_new_with_dir (const char *panel_name, const char *wpath)
4033 WPanel *panel;
4034 Widget *w;
4035 char *section;
4036 int i, err;
4037 char *curdir = NULL;
4039 panel = g_new0 (WPanel, 1);
4040 w = WIDGET (panel);
4041 /* No know sizes of the panel at startup */
4042 init_widget (w, 0, 0, 0, 0, panel_callback, panel_event);
4043 /* We do not want the cursor */
4044 widget_want_cursor (w, FALSE);
4046 if (wpath != NULL)
4048 curdir = _vfs_get_cwd ();
4049 panel_set_cwd (panel, wpath);
4051 else
4053 vfs_setup_cwd ();
4054 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4057 panel_set_lwd (panel, ".");
4059 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4060 /* directories history will be get later */
4062 panel->dir.list = g_new (file_entry, MIN_FILES);
4063 panel->dir.size = MIN_FILES;
4064 panel->active = 0;
4065 panel->filter = 0;
4066 panel->split = 0;
4067 panel->top_file = 0;
4068 panel->selected = 0;
4069 panel->marked = 0;
4070 panel->total = 0;
4071 panel->dirty = 1;
4072 panel->searching = FALSE;
4073 panel->dirs_marked = 0;
4074 panel->is_panelized = FALSE;
4075 panel->format = 0;
4076 panel->status_format = 0;
4077 panel->format_modified = 1;
4078 panel->content_shift = -1;
4079 panel->max_shift = -1;
4081 panel->panel_name = g_strdup (panel_name);
4082 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4084 #ifdef HAVE_CHARSET
4085 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4086 #endif
4088 for (i = 0; i < LIST_TYPES; i++)
4089 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4091 panel->search_buffer[0] = '\0';
4092 panel->prev_search_buffer[0] = '\0';
4093 panel->frame_size = frame_half;
4095 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4096 if (!mc_config_has_group (mc_main_config, section))
4098 g_free (section);
4099 section = g_strdup (panel->panel_name);
4101 panel_load_setup (panel, section);
4102 g_free (section);
4104 /* Load format strings */
4105 err = set_panel_formats (panel);
4106 if (err != 0)
4107 set_panel_formats (panel);
4109 #ifdef HAVE_CHARSET
4111 const vfs_path_element_t *path_element;
4113 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4114 if (path_element->encoding != NULL)
4115 panel->codepage = get_codepage_index (path_element->encoding);
4117 #endif
4119 if (mc_chdir (panel->cwd_vpath) != 0)
4121 #ifdef HAVE_CHARSET
4122 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4123 #endif
4124 vfs_setup_cwd ();
4125 vfs_path_free (panel->cwd_vpath);
4126 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4129 /* Load the default format */
4130 panel->count =
4131 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4132 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4133 panel->sort_info.exec_first, panel->filter);
4135 /* Restore old right path */
4136 if (curdir != NULL)
4138 vfs_path_t *vpath;
4140 vpath = vfs_path_from_str (curdir);
4141 err = mc_chdir (vpath);
4142 vfs_path_free (vpath);
4144 g_free (curdir);
4146 return panel;
4149 /* --------------------------------------------------------------------------------------------- */
4151 void
4152 panel_reload (WPanel * panel)
4154 struct stat current_stat;
4155 char *tmp_path;
4156 gboolean ok;
4158 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4159 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
4160 && current_stat.st_ctime == panel->dir_stat.st_ctime
4161 && current_stat.st_mtime == panel->dir_stat.st_mtime);
4162 g_free (tmp_path);
4164 if (ok)
4165 return;
4169 char *last_slash;
4171 if (mc_chdir (panel->cwd_vpath) != -1)
4172 break;
4174 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4175 if (tmp_path[0] == PATH_SEP && tmp_path[1] == '\0')
4177 panel_clean_dir (panel);
4178 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
4179 g_free (tmp_path);
4180 return;
4182 last_slash = strrchr (tmp_path, PATH_SEP);
4183 vfs_path_free (panel->cwd_vpath);
4184 if (last_slash == NULL || last_slash == tmp_path)
4185 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4186 else
4188 *last_slash = '\0';
4189 panel->cwd_vpath = vfs_path_from_str (tmp_path);
4191 g_free (tmp_path);
4192 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4193 show_dir (panel);
4195 while (TRUE);
4197 panel->count =
4198 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4199 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4200 panel->sort_info.exec_first, panel->filter);
4202 panel->dirty = 1;
4203 if (panel->selected >= panel->count)
4204 do_select (panel, panel->count - 1);
4206 recalculate_panel_summary (panel);
4209 /* --------------------------------------------------------------------------------------------- */
4210 /* Switches the panel to the mode specified in the format */
4211 /* Seting up both format and status string. Return: 0 - on success; */
4212 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4215 set_panel_formats (WPanel * p)
4217 format_e *form;
4218 char *err = NULL;
4219 int retcode = 0;
4221 form = use_display_format (p, panel_format (p), &err, 0);
4223 if (err != NULL)
4225 g_free (err);
4226 retcode = 1;
4228 else
4230 delete_format (p->format);
4231 p->format = form;
4234 if (panels_options.show_mini_info)
4236 form = use_display_format (p, mini_status_format (p), &err, 1);
4238 if (err != NULL)
4240 g_free (err);
4241 retcode += 2;
4243 else
4245 delete_format (p->status_format);
4246 p->status_format = form;
4250 panel_format_modified (p);
4251 panel_update_cols (WIDGET (p), p->frame_size);
4253 if (retcode)
4254 message (D_ERROR, _("Warning"),
4255 _("User supplied format looks invalid, reverting to default."));
4256 if (retcode & 0x01)
4258 g_free (p->user_format);
4259 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4261 if (retcode & 0x02)
4263 g_free (p->user_status_format[p->list_type]);
4264 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4267 return retcode;
4270 /* --------------------------------------------------------------------------------------------- */
4272 void
4273 panel_update_cols (Widget * widget, panel_display_t frame_size)
4275 int cols, origin;
4277 /* don't touch panel if it is not in dialog yet */
4278 /* if panel is not in dialog it is not in widgets list
4279 and cannot be compared with get_panel_widget() result */
4280 if (widget->owner == NULL)
4281 return;
4283 if (panels_layout.horizontal_split)
4285 widget->cols = COLS;
4286 return;
4289 if (frame_size == frame_full)
4291 cols = COLS;
4292 origin = 0;
4294 else if (widget == get_panel_widget (0))
4296 cols = panels_layout.left_panel_size;
4297 origin = 0;
4299 else
4301 cols = COLS - panels_layout.left_panel_size;
4302 origin = panels_layout.left_panel_size;
4305 widget->cols = cols;
4306 widget->x = origin;
4309 /* --------------------------------------------------------------------------------------------- */
4311 /* Select current item and readjust the panel */
4312 void
4313 select_item (WPanel * panel)
4315 /* Although currently all over the code we set the selection and
4316 top file to decent values before calling select_item, I could
4317 forget it someday, so it's better to do the actual fitting here */
4319 if (panel->selected < 0)
4320 panel->selected = 0;
4322 if (panel->selected > panel->count - 1)
4323 panel->selected = panel->count - 1;
4325 adjust_top_file (panel);
4327 panel->dirty = 1;
4329 execute_hooks (select_file_hook);
4332 /* --------------------------------------------------------------------------------------------- */
4333 /** Clears all files in the panel, used only when one file was marked */
4334 void
4335 unmark_files (WPanel * panel)
4337 int i;
4339 if (!panel->marked)
4340 return;
4341 for (i = 0; i < panel->count; i++)
4342 file_mark (panel, i, 0);
4344 panel->dirs_marked = 0;
4345 panel->marked = 0;
4346 panel->total = 0;
4349 /* --------------------------------------------------------------------------------------------- */
4350 /** Recalculate the panels summary information, used e.g. when marked
4351 files might have been removed by an external command */
4353 void
4354 recalculate_panel_summary (WPanel * panel)
4356 int i;
4358 panel->marked = 0;
4359 panel->dirs_marked = 0;
4360 panel->total = 0;
4362 for (i = 0; i < panel->count; i++)
4363 if (panel->dir.list[i].f.marked)
4365 /* do_file_mark will return immediately if newmark == oldmark.
4366 So we have to first unmark it to get panel's summary information
4367 updated. (Norbert) */
4368 panel->dir.list[i].f.marked = 0;
4369 do_file_mark (panel, i, 1);
4373 /* --------------------------------------------------------------------------------------------- */
4374 /** This routine marks a file or a directory */
4376 void
4377 do_file_mark (WPanel * panel, int idx, int mark)
4379 if (panel->dir.list[idx].f.marked == mark)
4380 return;
4382 /* Only '..' can't be marked, '.' isn't visible */
4383 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4384 return;
4386 file_mark (panel, idx, mark);
4387 if (panel->dir.list[idx].f.marked)
4389 panel->marked++;
4390 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4392 if (panel->dir.list[idx].f.dir_size_computed)
4393 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4394 panel->dirs_marked++;
4396 else
4397 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4398 set_colors (panel);
4400 else
4402 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4404 if (panel->dir.list[idx].f.dir_size_computed)
4405 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4406 panel->dirs_marked--;
4408 else
4409 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4410 panel->marked--;
4414 /* --------------------------------------------------------------------------------------------- */
4416 * Changes the current directory of the panel.
4417 * Record change in the directory history.
4419 gboolean
4420 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4422 gboolean r;
4424 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4425 if (r)
4426 directory_history_add (panel, panel->cwd_vpath);
4427 return r;
4430 /* --------------------------------------------------------------------------------------------- */
4432 void
4433 file_mark (WPanel * panel, int lc_index, int val)
4435 if (panel->dir.list[lc_index].f.marked != val)
4437 panel->dir.list[lc_index].f.marked = val;
4438 panel->dirty = 1;
4442 /* --------------------------------------------------------------------------------------------- */
4444 void
4445 panel_re_sort (WPanel * panel)
4447 char *filename;
4448 int i;
4450 if (panel == NULL)
4451 return;
4453 filename = g_strdup (selection (panel)->fname);
4454 unselect_item (panel);
4455 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4456 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4457 panel->sort_info.exec_first);
4458 panel->selected = -1;
4459 for (i = panel->count; i; i--)
4461 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4463 panel->selected = i - 1;
4464 break;
4467 g_free (filename);
4468 panel->top_file = panel->selected - ITEMS (panel) / 2;
4469 select_item (panel);
4470 panel->dirty = 1;
4473 /* --------------------------------------------------------------------------------------------- */
4475 void
4476 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4478 if (sort_order == NULL)
4479 return;
4481 panel->sort_info.sort_field = sort_order;
4483 /* The directory is already sorted, we have to load the unsorted stuff */
4484 if (sort_order->sort_routine == (sortfn *) unsorted)
4486 char *current_file;
4488 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4489 panel_reload (panel);
4490 try_to_select (panel, current_file);
4491 g_free (current_file);
4493 panel_re_sort (panel);
4496 /* --------------------------------------------------------------------------------------------- */
4498 #ifdef HAVE_CHARSET
4501 * Change panel encoding.
4502 * @param panel WPanel object
4505 void
4506 panel_change_encoding (WPanel * panel)
4508 const char *encoding = NULL;
4509 char *errmsg;
4510 int r;
4512 r = select_charset (-1, -1, panel->codepage, FALSE);
4514 if (r == SELECT_CHARSET_CANCEL)
4515 return; /* Cancel */
4517 panel->codepage = r;
4519 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4521 /* No translation */
4522 vfs_path_t *cd_path_vpath;
4524 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4525 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4526 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4527 show_dir (panel);
4528 vfs_path_free (cd_path_vpath);
4529 return;
4532 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4533 if (errmsg != NULL)
4535 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4536 g_free (errmsg);
4537 return;
4540 encoding = get_codepage_id (panel->codepage);
4541 if (encoding != NULL)
4543 char *cd_path;
4544 vfs_change_encoding (panel->cwd_vpath, encoding);
4546 cd_path = vfs_path_to_str (panel->cwd_vpath);
4547 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4548 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4549 g_free (cd_path);
4553 /* --------------------------------------------------------------------------------------------- */
4556 * Remove encode info from last path element.
4559 vfs_path_t *
4560 remove_encoding_from_path (const vfs_path_t * vpath)
4562 vfs_path_t *ret_vpath;
4563 GString *tmp_conv;
4564 int indx;
4566 ret_vpath = vfs_path_new ();
4568 tmp_conv = g_string_new ("");
4570 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4572 GIConv converter;
4573 vfs_path_element_t *path_element;
4575 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4576 vfs_path_add_element (ret_vpath, path_element);
4578 if (path_element->encoding == NULL)
4579 continue;
4581 converter = str_crt_conv_to (path_element->encoding);
4582 if (converter == INVALID_CONV)
4583 continue;
4585 g_free (path_element->encoding);
4586 path_element->encoding = NULL;
4588 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4590 g_free (path_element->path);
4591 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4593 g_string_set_size (tmp_conv, 0);
4595 str_close_conv (converter);
4596 str_close_conv (path_element->dir.converter);
4597 path_element->dir.converter = INVALID_CONV;
4599 g_string_free (tmp_conv, TRUE);
4600 return ret_vpath;
4602 #endif /* HAVE_CHARSET */
4604 /* --------------------------------------------------------------------------------------------- */
4607 * This routine reloads the directory in both panels. It tries to
4608 * select current_file in current_panel and other_file in other_panel.
4609 * If current_file == -1 then it automatically sets current_file and
4610 * other_file to the currently selected files in the panels.
4612 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4613 * will not reload the other panel.
4615 * @param flags for reload panel
4616 * @param current_file name of the current file
4619 void
4620 update_panels (panel_update_flags_t flags, const char *current_file)
4622 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4623 WPanel *panel;
4625 update_one_panel (get_current_index (), flags, current_file);
4626 if (reload_other)
4627 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4629 if (get_current_type () == view_listing)
4630 panel = (WPanel *) get_panel_widget (get_current_index ());
4631 else
4632 panel = (WPanel *) get_panel_widget (get_other_index ());
4634 if (!panel->is_panelized)
4635 (void) mc_chdir (panel->cwd_vpath);
4638 /* --------------------------------------------------------------------------------------------- */
4640 gsize
4641 panel_get_num_of_sortable_fields (void)
4643 gsize ret = 0, lc_index;
4645 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4646 if (panel_fields[lc_index].is_user_choice)
4647 ret++;
4648 return ret;
4651 /* --------------------------------------------------------------------------------------------- */
4653 const char **
4654 panel_get_sortable_fields (gsize * array_size)
4656 char **ret;
4657 gsize lc_index, i;
4659 lc_index = panel_get_num_of_sortable_fields ();
4661 ret = g_try_new0 (char *, lc_index + 1);
4662 if (ret == NULL)
4663 return NULL;
4665 if (array_size != NULL)
4666 *array_size = lc_index;
4668 lc_index = 0;
4670 for (i = 0; panel_fields[i].id != NULL; i++)
4671 if (panel_fields[i].is_user_choice)
4672 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4673 return (const char **) ret;
4676 /* --------------------------------------------------------------------------------------------- */
4678 const panel_field_t *
4679 panel_get_field_by_id (const char *name)
4681 gsize lc_index;
4682 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4683 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4684 return &panel_fields[lc_index];
4685 return NULL;
4688 /* --------------------------------------------------------------------------------------------- */
4690 const panel_field_t *
4691 panel_get_field_by_title_hotkey (const char *name)
4693 gsize lc_index;
4694 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4695 if (panel_fields[lc_index].title_hotkey != NULL &&
4696 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4697 return &panel_fields[lc_index];
4698 return NULL;
4701 /* --------------------------------------------------------------------------------------------- */
4703 const panel_field_t *
4704 panel_get_field_by_title (const char *name)
4706 gsize lc_index;
4707 gchar *title = NULL;
4709 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4711 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4712 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4714 g_free (title);
4715 return &panel_fields[lc_index];
4718 g_free (title);
4719 return NULL;
4722 /* --------------------------------------------------------------------------------------------- */
4724 gsize
4725 panel_get_num_of_user_possible_fields (void)
4727 gsize ret = 0, lc_index;
4729 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4730 if (panel_fields[lc_index].use_in_user_format)
4731 ret++;
4732 return ret;
4735 /* --------------------------------------------------------------------------------------------- */
4737 const char **
4738 panel_get_user_possible_fields (gsize * array_size)
4740 char **ret;
4741 gsize lc_index, i;
4743 lc_index = panel_get_num_of_user_possible_fields ();
4745 ret = g_try_new0 (char *, lc_index + 1);
4746 if (ret == NULL)
4747 return NULL;
4749 if (array_size != NULL)
4750 *array_size = lc_index;
4752 lc_index = 0;
4754 for (i = 0; panel_fields[i].id != NULL; i++)
4755 if (panel_fields[i].use_in_user_format)
4756 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4757 return (const char **) ret;
4760 /* --------------------------------------------------------------------------------------------- */
4762 void
4763 panel_init (void)
4765 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4766 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4768 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4769 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4770 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4771 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4772 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4773 panel_filename_scroll_left_char =
4774 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4775 panel_filename_scroll_right_char =
4776 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4778 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4779 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4780 panel_save_curent_file_to_clip_file, NULL, NULL);
4784 /* --------------------------------------------------------------------------------------------- */
4786 void
4787 panel_deinit (void)
4789 g_free (panel_sort_up_sign);
4790 g_free (panel_sort_down_sign);
4792 g_free (panel_hiddenfiles_sign_show);
4793 g_free (panel_hiddenfiles_sign_hide);
4794 g_free (panel_history_prev_item_sign);
4795 g_free (panel_history_next_item_sign);
4796 g_free (panel_history_show_list_sign);
4797 g_free (panel_filename_scroll_left_char);
4798 g_free (panel_filename_scroll_right_char);
4802 /* --------------------------------------------------------------------------------------------- */
4804 gboolean
4805 do_cd (const vfs_path_t * new_dir_vpath, enum cd_enum exact)
4807 gboolean res;
4808 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
4810 if (current_panel->is_panelized)
4812 size_t new_vpath_len;
4814 new_vpath_len = vfs_path_len (new_dir_vpath);
4815 if (vfs_path_ncmp (new_dir_vpath, panelized_panel.root_vpath, new_vpath_len) == 0)
4816 _new_dir_vpath = panelized_panel.root_vpath;
4819 res = do_panel_cd (current_panel, _new_dir_vpath, exact);
4821 #ifdef HAVE_CHARSET
4822 if (res)
4824 const vfs_path_element_t *path_element;
4826 path_element = vfs_path_get_by_index (current_panel->cwd_vpath, -1);
4827 if (path_element->encoding != NULL)
4828 current_panel->codepage = get_codepage_index (path_element->encoding);
4829 else
4830 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4832 #endif /* HAVE_CHARSET */
4834 return res;
4837 /* --------------------------------------------------------------------------------------------- */