Fix of DOXYGEN docs: @return instead of @returns
[midnight-commander.git] / src / filemanager / panel.c
blob1d522005b3e07ac7e5c52ebd49995515a04b353f
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_("&Extension"), 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) (p->widget.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 int second_column = 0;
888 int width;
889 int offset = 0;
890 char buffer[BUF_MEDIUM];
891 filename_scroll_flag_t ret_frm;
892 int ypos = 0;
893 gboolean panel_is_split = !isstatus && panel->split;
894 int fln = 0;
896 width = panel->widget.cols - 2;
898 if (panel_is_split)
900 second_column = (file_index - panel->top_file) / llines (panel);
901 width = width / 2 - 1;
903 if (second_column != 0)
905 offset = 1 + width;
906 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
907 width = panel->widget.cols - offset - 2;
911 /* Nothing to paint */
912 if (width <= 0)
913 return;
915 if (mv)
917 int pos = file_index - panel->top_file;
919 if (panel_is_split)
920 ypos = pos % llines (panel);
921 else
922 ypos = pos;
924 ypos += 2;
925 widget_move (&panel->widget, ypos, offset + 1);
928 ret_frm = format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus, &fln);
930 if (panel_is_split)
932 if (second_column)
933 tty_print_char (' ');
934 else
936 tty_setcolor (NORMAL_COLOR);
937 tty_print_one_vline (TRUE);
940 if (ret_frm != FILENAME_NOSCROLL && mv)
942 if (!panel_is_split && fln > 0)
944 if (panel->list_type == list_long)
946 offset = width - fln + 1;
947 width = fln - 1;
949 else
951 width = fln;
954 widget_move (&panel->widget, ypos, offset);
955 tty_setcolor (NORMAL_COLOR);
956 tty_print_string (panel_filename_scroll_left_char);
958 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
960 widget_move (&panel->widget, ypos, offset + 1 + width);
961 tty_setcolor (NORMAL_COLOR);
962 tty_print_string (panel_filename_scroll_right_char);
967 /* --------------------------------------------------------------------------------------------- */
969 static void
970 display_mini_info (WPanel * panel)
972 if (!panels_options.show_mini_info)
973 return;
975 widget_move (&panel->widget, llines (panel) + 3, 1);
977 if (panel->searching)
979 tty_setcolor (INPUT_COLOR);
980 tty_print_char ('/');
981 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
982 return;
985 /* Status resolves links and show them */
986 set_colors (panel);
988 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
990 char link_target[MC_MAXPATHLEN];
991 vfs_path_t *lc_link_vpath;
992 int len;
994 lc_link_vpath =
995 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
996 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
997 vfs_path_free (lc_link_vpath);
998 if (len > 0)
1000 link_target[len] = 0;
1001 tty_print_string ("-> ");
1002 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
1004 else
1005 tty_print_string (str_fit_to_term (_("<readlink failed>"),
1006 panel->widget.cols - 2, J_LEFT));
1008 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
1010 /* FIXME:
1011 * while loading directory (do_load_dir() and do_reload_dir()),
1012 * the actual stat info about ".." directory isn't got;
1013 * so just don't display incorrect info about ".." directory */
1014 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
1016 else
1017 /* Default behavior */
1018 repaint_file (panel, panel->selected, 0, STATUS, 1);
1021 /* --------------------------------------------------------------------------------------------- */
1023 static void
1024 paint_dir (WPanel * panel)
1026 int i;
1027 int color; /* Color value of the line */
1028 int items; /* Number of items */
1030 items = llines (panel) * (panel->split ? 2 : 1);
1031 /* reset max len of filename because we have the new max length for the new file list */
1032 panel->max_shift = -1;
1033 for (i = 0; i < items; i++)
1035 if (i + panel->top_file >= panel->count)
1036 color = 0;
1037 else
1039 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1040 color += (panel->selected == i + panel->top_file && panel->active);
1042 repaint_file (panel, i + panel->top_file, 1, color, 0);
1045 tty_set_normal_attrs ();
1048 /* --------------------------------------------------------------------------------------------- */
1050 static void
1051 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
1053 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
1054 int cols;
1056 if (panel->marked <= 0)
1057 return;
1059 buf = size_only ? b_bytes : buffer;
1060 cols = panel->widget.cols - 2;
1063 * This is a trick to use two ngettext() calls in one sentence.
1064 * First make "N bytes", then insert it into "X in M files".
1066 g_snprintf (b_bytes, sizeof (b_bytes),
1067 ngettext ("%s byte", "%s bytes", panel->total),
1068 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1069 if (!size_only)
1070 g_snprintf (buffer, sizeof (buffer),
1071 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1072 b_bytes, panel->marked);
1074 /* don't forget spaces around buffer content */
1075 buf = (char *) str_trunc (buf, cols - 4);
1077 if (x < 0)
1078 /* center in panel */
1079 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
1082 * y == llines (panel) + 2 for mini_info_separator
1083 * y == panel->widget.lines - 1 for panel bottom frame
1085 widget_move (&panel->widget, y, x);
1086 tty_setcolor (MARKED_COLOR);
1087 tty_printf (" %s ", buf);
1090 /* --------------------------------------------------------------------------------------------- */
1092 static void
1093 mini_info_separator (WPanel * panel)
1095 if (panels_options.show_mini_info)
1097 const int y = llines (panel) + 2;
1099 tty_setcolor (NORMAL_COLOR);
1100 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1101 ACS_HLINE, panel->widget.cols - 2);
1102 /* Status displays total marked size.
1103 * Centered in panel, full format. */
1104 display_total_marked_size (panel, y, -1, FALSE);
1108 /* --------------------------------------------------------------------------------------------- */
1110 static void
1111 show_free_space (WPanel * panel)
1113 /* Used to figure out how many free space we have */
1114 static struct my_statfs myfs_stats;
1115 /* Old current working directory for displaying free space */
1116 static char *old_cwd = NULL;
1117 char *tmp_path;
1119 /* Don't try to stat non-local fs */
1120 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1121 return;
1123 tmp_path = vfs_path_to_str (panel->cwd_vpath);
1124 if (old_cwd == NULL || strcmp (old_cwd, tmp_path) != 0)
1126 char rpath[PATH_MAX];
1128 init_my_statfs ();
1129 g_free (old_cwd);
1130 old_cwd = tmp_path;
1131 tmp_path = NULL;
1133 if (mc_realpath (old_cwd, rpath) == NULL)
1134 return;
1136 my_statfs (&myfs_stats, rpath);
1138 g_free (tmp_path);
1140 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1142 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1143 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1144 panels_options.kilobyte_si);
1145 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1146 panels_options.kilobyte_si);
1147 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1148 myfs_stats.total == 0 ? 0 :
1149 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1150 widget_move (&panel->widget, panel->widget.lines - 1,
1151 panel->widget.cols - 2 - (int) strlen (tmp));
1152 tty_setcolor (NORMAL_COLOR);
1153 tty_print_string (tmp);
1157 /* --------------------------------------------------------------------------------------------- */
1159 * Prepare path string for showing in panel's header.
1160 * Passwords will removed, also home dir will replaced by ~
1162 * @param panel WPanel object
1164 * @return newly allocated string.
1167 static char *
1168 panel_correct_path_to_show (WPanel * panel)
1170 vfs_path_t *last_vpath;
1171 const vfs_path_element_t *path_element;
1172 char *return_path;
1173 int elements_count;
1175 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1177 /* get last path element */
1178 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1181 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1182 strcmp (path_element->class->name, "extfs") == 0 ||
1183 strcmp (path_element->class->name, "tarfs") == 0))
1185 const char *archive_name;
1186 const vfs_path_element_t *prev_path_element;
1188 /* get previous path element for catching archive name */
1189 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1190 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1191 if (archive_name != NULL)
1193 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1195 else
1197 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1198 last_vpath->relative = TRUE;
1201 else
1203 last_vpath = vfs_path_new ();
1204 last_vpath->relative = TRUE;
1207 vfs_path_add_element (last_vpath, path_element);
1208 return_path =
1209 vfs_path_to_str_flags (last_vpath, 0,
1210 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1211 vfs_path_free (last_vpath);
1213 return return_path;
1216 /* --------------------------------------------------------------------------------------------- */
1218 * Get Current path element encoding
1220 * @param panel WPanel object
1222 * @return newly allocated string or NULL if path charset is same as system charset
1225 #ifdef HAVE_CHARSET
1226 static char *
1227 panel_get_encoding_info_str (WPanel * panel)
1229 char *ret_str = NULL;
1230 const vfs_path_element_t *path_element;
1232 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1233 if (path_element->encoding != NULL)
1234 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1236 return ret_str;
1238 #endif
1240 /* --------------------------------------------------------------------------------------------- */
1242 static void
1243 show_dir (WPanel * panel)
1245 gchar *tmp;
1246 set_colors (panel);
1247 draw_box (panel->widget.owner,
1248 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1250 if (panels_options.show_mini_info)
1252 widget_move (&panel->widget, llines (panel) + 2, 0);
1253 tty_print_alt_char (ACS_LTEE, FALSE);
1254 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1255 tty_print_alt_char (ACS_RTEE, FALSE);
1258 widget_move (&panel->widget, 0, 1);
1259 tty_print_string (panel_history_prev_item_sign);
1261 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1262 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1263 panel_history_next_item_sign);
1265 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1266 tty_print_string (tmp);
1268 g_free (tmp);
1270 widget_move (&panel->widget, 0, 3);
1272 if (panel->is_panelized)
1273 tty_printf (" %s ", _("Panelize"));
1274 #ifdef HAVE_CHARSET
1275 else
1277 tmp = panel_get_encoding_info_str (panel);
1278 if (tmp != NULL)
1280 tty_printf ("%s", tmp);
1281 widget_move (&panel->widget, 0, 3 + strlen (tmp));
1282 g_free (tmp);
1285 #endif
1287 if (panel->active)
1288 tty_setcolor (REVERSE_COLOR);
1290 tmp = panel_correct_path_to_show (panel);
1291 tty_printf (" %s ",
1292 str_term_trim (tmp, min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1293 g_free (tmp);
1295 if (!panels_options.show_mini_info)
1297 if (panel->marked == 0)
1299 /* Show size of curret file in the bottom of panel */
1300 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1302 char buffer[BUF_SMALL];
1304 g_snprintf (buffer, sizeof (buffer), " %s ",
1305 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1306 panels_options.kilobyte_si));
1307 tty_setcolor (NORMAL_COLOR);
1308 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1309 tty_print_string (buffer);
1312 else
1314 /* Show total size of marked files
1315 * In the bottom of panel, display size only. */
1316 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1320 show_free_space (panel);
1322 if (panel->active)
1323 tty_set_normal_attrs ();
1326 /* --------------------------------------------------------------------------------------------- */
1328 /* Returns the number of items in the given panel */
1329 static int
1330 ITEMS (WPanel * p)
1332 if (p->split)
1333 return llines (p) * 2;
1334 else
1335 return llines (p);
1338 /* --------------------------------------------------------------------------------------------- */
1340 static void
1341 adjust_top_file (WPanel * panel)
1343 int items = ITEMS (panel);
1345 if (panel->count <= items)
1347 /* If all files fit, show them all. */
1348 panel->top_file = 0;
1350 else
1352 int i;
1354 /* top_file has to be in the range [selected-items+1, selected] so that
1355 the selected file is visible.
1356 top_file should be in the range [0, count-items] so that there's
1357 no empty space wasted.
1358 Within these ranges, adjust it by as little as possible. */
1360 if (panel->top_file < 0)
1361 panel->top_file = 0;
1363 i = panel->selected - items + 1;
1364 if (panel->top_file < i)
1365 panel->top_file = i;
1367 i = panel->count - items;
1368 if (panel->top_file > i)
1369 panel->top_file = i;
1371 if (panel->top_file > panel->selected)
1372 panel->top_file = panel->selected;
1376 /* --------------------------------------------------------------------------------------------- */
1377 /** add "#enc:encodning" to end of path */
1378 /* if path end width a previous #enc:, only encoding is changed no additional
1379 * #enc: is appended
1380 * retun new string
1383 static char *
1384 panel_save_name (WPanel * panel)
1386 /* If the program is shuting down */
1387 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1388 return g_strdup (panel->panel_name);
1389 else
1390 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1393 /* --------------------------------------------------------------------------------------------- */
1395 static void
1396 directory_history_add (struct WPanel *panel, const vfs_path_t * vpath)
1398 char *tmp;
1400 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1401 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1402 panel->dir_history_current = panel->dir_history;
1405 /* --------------------------------------------------------------------------------------------- */
1407 /* "history_load" event handler */
1408 static gboolean
1409 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1410 gpointer init_data, gpointer data)
1412 WPanel *p = (WPanel *) init_data;
1413 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1415 (void) event_group_name;
1416 (void) event_name;
1418 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1420 if (ev->cfg != NULL)
1421 p->dir_history = history_load (ev->cfg, p->hist_name);
1422 else
1423 p->dir_history = history_get (p->hist_name);
1425 directory_history_add (p, p->cwd_vpath);
1428 return TRUE;
1431 /* --------------------------------------------------------------------------------------------- */
1433 /* "history_save" event handler */
1434 static gboolean
1435 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1436 gpointer init_data, gpointer data)
1438 WPanel *p = (WPanel *) init_data;
1440 (void) event_group_name;
1441 (void) event_name;
1443 if (p->dir_history != NULL)
1445 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1447 history_save (ev->cfg, p->hist_name, p->dir_history);
1450 return TRUE;
1453 /* --------------------------------------------------------------------------------------------- */
1455 static void
1456 panel_destroy (WPanel * p)
1458 size_t i;
1460 if (panels_options.auto_save_setup)
1462 char *name;
1464 name = panel_save_name (p);
1465 panel_save_setup (p, name);
1466 g_free (name);
1469 panel_clean_dir (p);
1471 /* clean history */
1472 if (p->dir_history != NULL)
1474 /* directory history is already saved before this moment */
1475 p->dir_history = g_list_first (p->dir_history);
1476 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1477 g_list_free (p->dir_history);
1479 g_free (p->hist_name);
1481 delete_format (p->format);
1482 delete_format (p->status_format);
1484 g_free (p->user_format);
1485 for (i = 0; i < LIST_TYPES; i++)
1486 g_free (p->user_status_format[i]);
1488 g_free (p->dir.list);
1489 g_free (p->panel_name);
1491 vfs_path_free (p->lwd_vpath);
1492 vfs_path_free (p->cwd_vpath);
1495 /* --------------------------------------------------------------------------------------------- */
1497 static void
1498 panel_format_modified (WPanel * panel)
1500 panel->format_modified = 1;
1503 /* --------------------------------------------------------------------------------------------- */
1505 static void
1506 panel_paint_sort_info (WPanel * panel)
1508 if (*panel->sort_info.sort_field->hotkey != '\0')
1510 const char *sort_sign =
1511 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1512 char *str;
1514 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1515 widget_move (&panel->widget, 1, 1);
1516 tty_print_string (str);
1517 g_free (str);
1521 /* --------------------------------------------------------------------------------------------- */
1523 static gchar *
1524 panel_get_title_without_hotkey (const char *title)
1526 char *translated_title;
1527 char *hkey;
1529 if (title == NULL)
1530 return NULL;
1531 if (title[0] == '\0')
1532 return g_strdup ("");
1534 translated_title = g_strdup (_(title));
1536 hkey = strchr (translated_title, '&');
1537 if ((hkey != NULL) && (hkey[1] != '\0'))
1538 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1540 return translated_title;
1543 /* --------------------------------------------------------------------------------------------- */
1545 static void
1546 paint_frame (WPanel * panel)
1548 int side, width;
1549 GString *format_txt;
1551 adjust_top_file (panel);
1553 widget_erase (&panel->widget);
1554 show_dir (panel);
1556 widget_move (&panel->widget, 1, 1);
1558 for (side = 0; side <= panel->split; side++)
1560 format_e *format;
1562 if (side)
1564 tty_setcolor (NORMAL_COLOR);
1565 tty_print_one_vline (TRUE);
1566 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1568 else if (panel->split)
1569 width = panel->widget.cols / 2 - 3;
1570 else
1571 width = panel->widget.cols - 2;
1573 format_txt = g_string_new ("");
1574 for (format = panel->format; format; format = format->next)
1576 if (format->string_fn)
1578 g_string_set_size (format_txt, 0);
1580 if (panel->list_type == list_long
1581 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1582 g_string_append (format_txt,
1583 panel->sort_info.reverse
1584 ? panel_sort_down_sign : panel_sort_up_sign);
1586 g_string_append (format_txt, format->title);
1587 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1589 g_string_append (format_txt, " [");
1590 g_string_append (format_txt, panel->filter);
1591 g_string_append (format_txt, "]");
1594 tty_setcolor (HEADER_COLOR);
1595 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1596 J_CENTER_LEFT));
1597 width -= format->field_len;
1599 else
1601 tty_setcolor (NORMAL_COLOR);
1602 tty_print_one_vline (TRUE);
1603 width--;
1606 g_string_free (format_txt, TRUE);
1608 if (width > 0)
1610 int y, x;
1612 tty_getyx (&y, &x);
1613 tty_draw_hline (y, x, ' ', width);
1617 if (panel->list_type != list_long)
1618 panel_paint_sort_info (panel);
1621 /* --------------------------------------------------------------------------------------------- */
1623 static const char *
1624 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1626 panel_display_t frame = frame_half;
1627 format = skip_separators (format);
1629 if (!strncmp (format, "full", 4))
1631 frame = frame_full;
1632 format += 4;
1634 else if (!strncmp (format, "half", 4))
1636 frame = frame_half;
1637 format += 4;
1640 if (!isstatus)
1642 panel->frame_size = frame;
1643 panel->split = 0;
1646 /* Now, the optional column specifier */
1647 format = skip_separators (format);
1649 if (*format == '1' || *format == '2')
1651 if (!isstatus)
1652 panel->split = *format == '2';
1653 format++;
1656 if (!isstatus)
1657 panel_update_cols (&(panel->widget), panel->frame_size);
1659 return skip_separators (format);
1662 /* Format is:
1664 all := panel_format? format
1665 panel_format := [full|half] [1|2]
1666 format := one_format_e
1667 | format , one_format_e
1669 one_format_e := just format.id [opt_size]
1670 just := [<=>]
1671 opt_size := : size [opt_expand]
1672 size := [0-9]+
1673 opt_expand := +
1677 /* --------------------------------------------------------------------------------------------- */
1679 static format_e *
1680 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1681 int *res_total_cols)
1683 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1684 int total_cols = 0; /* Used columns by the format */
1685 int set_justify; /* flag: set justification mode? */
1686 align_crt_t justify = J_LEFT; /* Which mode. */
1687 size_t i;
1689 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1691 *error = 0;
1693 if (i18n_timelength == 0)
1695 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1697 for (i = 0; panel_fields[i].id != NULL; i++)
1698 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1699 panel_fields[i].min_size = i18n_timelength;
1703 * This makes sure that the panel and mini status full/half mode
1704 * setting is equal
1706 format = parse_panel_size (panel, format, isstatus);
1708 while (*format)
1709 { /* format can be an empty string */
1710 int found = 0;
1712 darr = g_new0 (format_e, 1);
1714 /* I'm so ugly, don't look at me :-) */
1715 if (!home)
1716 home = old = darr;
1718 old->next = darr;
1719 darr->next = 0;
1720 old = darr;
1722 format = skip_separators (format);
1724 if (strchr ("<=>", *format))
1726 set_justify = 1;
1727 switch (*format)
1729 case '<':
1730 justify = J_LEFT;
1731 break;
1732 case '=':
1733 justify = J_CENTER;
1734 break;
1735 case '>':
1736 default:
1737 justify = J_RIGHT;
1738 break;
1740 format = skip_separators (format + 1);
1742 else
1743 set_justify = 0;
1745 for (i = 0; panel_fields[i].id != NULL; i++)
1747 size_t klen = strlen (panel_fields[i].id);
1749 if (strncmp (format, panel_fields[i].id, klen) != 0)
1750 continue;
1752 format += klen;
1754 darr->requested_field_len = panel_fields[i].min_size;
1755 darr->string_fn = panel_fields[i].string_fn;
1756 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1758 darr->id = panel_fields[i].id;
1759 darr->expand = panel_fields[i].expands;
1760 darr->just_mode = panel_fields[i].default_just;
1762 if (set_justify)
1764 if (IS_FIT (darr->just_mode))
1765 darr->just_mode = MAKE_FIT (justify);
1766 else
1767 darr->just_mode = justify;
1769 found = 1;
1771 format = skip_separators (format);
1773 /* If we have a size specifier */
1774 if (*format == ':')
1776 int req_length;
1778 /* If the size was specified, we don't want
1779 * auto-expansion by default
1781 darr->expand = 0;
1782 format++;
1783 req_length = atoi (format);
1784 darr->requested_field_len = req_length;
1786 format = skip_numbers (format);
1788 /* Now, if they insist on expansion */
1789 if (*format == '+')
1791 darr->expand = 1;
1792 format++;
1797 break;
1799 if (!found)
1801 char *tmp_format = g_strdup (format);
1803 int pos = min (8, strlen (format));
1804 delete_format (home);
1805 tmp_format[pos] = 0;
1806 *error =
1807 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1808 g_free (tmp_format);
1809 return 0;
1811 total_cols += darr->requested_field_len;
1814 *res_total_cols = total_cols;
1815 return home;
1818 /* --------------------------------------------------------------------------------------------- */
1820 static format_e *
1821 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1823 #define MAX_EXPAND 4
1824 int expand_top = 0; /* Max used element in expand */
1825 int usable_columns; /* Usable columns in the panel */
1826 int total_cols = 0;
1827 int i;
1828 format_e *darr, *home;
1830 if (!format)
1831 format = DEFAULT_USER_FORMAT;
1833 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1835 if (*error)
1836 return 0;
1838 panel->dirty = 1;
1840 /* Status needn't to be split */
1841 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1843 : (panel->split + 1))) - (!isstatus
1844 && panel->split);
1846 /* Look for the expandable fields and set field_len based on the requested field len */
1847 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1849 darr->field_len = darr->requested_field_len;
1850 if (darr->expand)
1851 expand_top++;
1854 /* If we used more columns than the available columns, adjust that */
1855 if (total_cols > usable_columns)
1857 int pdif, dif = total_cols - usable_columns;
1859 while (dif)
1861 pdif = dif;
1862 for (darr = home; darr; darr = darr->next)
1864 if (dif && darr->field_len - 1)
1866 darr->field_len--;
1867 dif--;
1871 /* avoid endless loop if num fields > 40 */
1872 if (pdif == dif)
1873 break;
1875 total_cols = usable_columns; /* give up, the rest should be truncated */
1878 /* Expand the available space */
1879 if ((usable_columns > total_cols) && expand_top)
1881 int spaces = (usable_columns - total_cols) / expand_top;
1882 int extra = (usable_columns - total_cols) % expand_top;
1884 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1885 if (darr->expand)
1887 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1888 i++;
1891 return home;
1894 /* --------------------------------------------------------------------------------------------- */
1895 /** Given the panel->view_type returns the format string to be parsed */
1897 static const char *
1898 panel_format (WPanel * panel)
1900 switch (panel->list_type)
1902 case list_long:
1903 return "full perm space nlink space owner space group space size space mtime space name";
1905 case list_brief:
1906 return "half 2 type name";
1908 case list_user:
1909 return panel->user_format;
1911 default:
1912 case list_full:
1913 return "half type name | size | mtime";
1917 /* --------------------------------------------------------------------------------------------- */
1919 static const char *
1920 mini_status_format (WPanel * panel)
1922 if (panel->user_mini_status)
1923 return panel->user_status_format[panel->list_type];
1925 switch (panel->list_type)
1927 case list_long:
1928 return "full perm space nlink space owner space group space size space mtime space name";
1930 case list_brief:
1931 return "half type name space bsize space perm space";
1933 case list_full:
1934 return "half type name";
1936 default:
1937 case list_user:
1938 return panel->user_format;
1942 /* */
1943 /* Panel operation commands */
1944 /* */
1946 /* --------------------------------------------------------------------------------------------- */
1947 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1949 static cb_ret_t
1950 maybe_cd (int move_up_dir)
1952 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1954 if (move_up_dir)
1956 vfs_path_t *up_dir;
1958 up_dir = vfs_path_from_str ("..");
1959 do_cd (up_dir, cd_exact);
1960 vfs_path_free (up_dir);
1961 return MSG_HANDLED;
1964 if (S_ISDIR (selection (current_panel)->st.st_mode)
1965 || link_isdir (selection (current_panel)))
1967 vfs_path_t *vpath;
1969 vpath = vfs_path_from_str (selection (current_panel)->fname);
1970 do_cd (vpath, cd_exact);
1971 vfs_path_free (vpath);
1972 return MSG_HANDLED;
1975 return MSG_NOT_HANDLED;
1978 /* --------------------------------------------------------------------------------------------- */
1980 /* if command line is empty then do 'cd ..' */
1981 static cb_ret_t
1982 force_maybe_cd (void)
1984 if (cmdline->buffer[0] == '\0')
1986 vfs_path_t *up_dir = vfs_path_from_str ("..");
1987 do_cd (up_dir, cd_exact);
1988 vfs_path_free (up_dir);
1989 return MSG_HANDLED;
1991 return MSG_NOT_HANDLED;
1994 /* --------------------------------------------------------------------------------------------- */
1996 static void
1997 unselect_item (WPanel * panel)
1999 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
2002 /* --------------------------------------------------------------------------------------------- */
2004 static void
2005 move_down (WPanel * panel)
2007 if (panel->selected + 1 == panel->count)
2008 return;
2010 unselect_item (panel);
2011 panel->selected++;
2012 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
2014 /* Scroll window half screen */
2015 panel->top_file += ITEMS (panel) / 2;
2016 if (panel->top_file > panel->count - ITEMS (panel))
2017 panel->top_file = panel->count - ITEMS (panel);
2018 paint_dir (panel);
2020 select_item (panel);
2023 /* --------------------------------------------------------------------------------------------- */
2025 static void
2026 move_up (WPanel * panel)
2028 if (panel->selected == 0)
2029 return;
2031 unselect_item (panel);
2032 panel->selected--;
2033 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2035 /* Scroll window half screen */
2036 panel->top_file -= ITEMS (panel) / 2;
2037 if (panel->top_file < 0)
2038 panel->top_file = 0;
2039 paint_dir (panel);
2041 select_item (panel);
2044 /* --------------------------------------------------------------------------------------------- */
2045 /** Changes the selection by lines (may be negative) */
2047 static void
2048 move_selection (WPanel * panel, int lines)
2050 int new_pos;
2051 int adjust = 0;
2053 new_pos = panel->selected + lines;
2054 if (new_pos >= panel->count)
2055 new_pos = panel->count - 1;
2057 if (new_pos < 0)
2058 new_pos = 0;
2060 unselect_item (panel);
2061 panel->selected = new_pos;
2063 if (panel->selected - panel->top_file >= ITEMS (panel))
2065 panel->top_file += lines;
2066 adjust = 1;
2069 if (panel->selected - panel->top_file < 0)
2071 panel->top_file += lines;
2072 adjust = 1;
2075 if (adjust)
2077 if (panel->top_file > panel->selected)
2078 panel->top_file = panel->selected;
2079 if (panel->top_file < 0)
2080 panel->top_file = 0;
2081 paint_dir (panel);
2083 select_item (panel);
2086 /* --------------------------------------------------------------------------------------------- */
2088 static cb_ret_t
2089 move_left (WPanel * panel)
2091 if (panel->split)
2093 move_selection (panel, -llines (panel));
2094 return MSG_HANDLED;
2096 else
2097 return maybe_cd (1); /* cd .. */
2100 /* --------------------------------------------------------------------------------------------- */
2102 static cb_ret_t
2103 move_right (WPanel * panel)
2105 if (panel->split)
2107 move_selection (panel, llines (panel));
2108 return MSG_HANDLED;
2110 else
2111 return maybe_cd (0); /* cd (selection) */
2114 /* --------------------------------------------------------------------------------------------- */
2116 static void
2117 prev_page (WPanel * panel)
2119 int items;
2121 if (!panel->selected && !panel->top_file)
2122 return;
2123 unselect_item (panel);
2124 items = ITEMS (panel);
2125 if (panel->top_file < items)
2126 items = panel->top_file;
2127 if (!items)
2128 panel->selected = 0;
2129 else
2130 panel->selected -= items;
2131 panel->top_file -= items;
2133 select_item (panel);
2134 paint_dir (panel);
2137 /* --------------------------------------------------------------------------------------------- */
2139 static void
2140 goto_parent_dir (WPanel * panel)
2142 if (!panel->is_panelized)
2144 vfs_path_t *up_dir;
2146 up_dir = vfs_path_from_str ("..");
2147 do_cd (up_dir, cd_exact);
2148 vfs_path_free (up_dir);
2150 else
2152 char *fname = panel->dir.list[panel->selected].fname;
2153 const char *bname;
2154 vfs_path_t *dname_vpath;
2156 if (g_path_is_absolute (fname))
2157 fname = g_strdup (fname);
2158 else
2160 char *tmp_root;
2162 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2163 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2164 g_free (tmp_root);
2167 bname = x_basename (fname);
2169 if (bname == fname)
2170 dname_vpath = vfs_path_from_str (".");
2171 else
2173 char *dname;
2175 dname = g_strndup (fname, bname - fname);
2176 dname_vpath = vfs_path_from_str (dname);
2177 g_free (dname);
2180 do_cd (dname_vpath, cd_exact);
2181 try_to_select (panel, bname);
2183 vfs_path_free (dname_vpath);
2184 g_free (fname);
2188 /* --------------------------------------------------------------------------------------------- */
2190 static void
2191 next_page (WPanel * panel)
2193 int items;
2195 if (panel->selected == panel->count - 1)
2196 return;
2197 unselect_item (panel);
2198 items = ITEMS (panel);
2199 if (panel->top_file > panel->count - 2 * items)
2200 items = panel->count - items - panel->top_file;
2201 if (panel->top_file + items < 0)
2202 items = -panel->top_file;
2203 if (!items)
2204 panel->selected = panel->count - 1;
2205 else
2206 panel->selected += items;
2207 panel->top_file += items;
2209 select_item (panel);
2210 paint_dir (panel);
2213 /* --------------------------------------------------------------------------------------------- */
2215 static void
2216 goto_child_dir (WPanel * panel)
2218 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2220 vfs_path_t *vpath;
2222 vpath = vfs_path_from_str (selection (panel)->fname);
2223 do_cd (vpath, cd_exact);
2224 vfs_path_free (vpath);
2228 /* --------------------------------------------------------------------------------------------- */
2230 static void
2231 goto_top_file (WPanel * panel)
2233 unselect_item (panel);
2234 panel->selected = panel->top_file;
2235 select_item (panel);
2238 /* --------------------------------------------------------------------------------------------- */
2240 static void
2241 goto_middle_file (WPanel * panel)
2243 unselect_item (panel);
2244 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2245 select_item (panel);
2248 /* --------------------------------------------------------------------------------------------- */
2250 static void
2251 goto_bottom_file (WPanel * panel)
2253 unselect_item (panel);
2254 panel->selected = panel->top_file + ITEMS (panel) - 1;
2255 select_item (panel);
2258 /* --------------------------------------------------------------------------------------------- */
2260 static void
2261 move_home (WPanel * panel)
2263 if (panel->selected == 0)
2264 return;
2266 unselect_item (panel);
2268 if (panels_options.torben_fj_mode)
2270 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2272 if (panel->selected > middle_pos)
2274 goto_middle_file (panel);
2275 return;
2277 if (panel->selected != panel->top_file)
2279 goto_top_file (panel);
2280 return;
2284 panel->top_file = 0;
2285 panel->selected = 0;
2287 paint_dir (panel);
2288 select_item (panel);
2291 /* --------------------------------------------------------------------------------------------- */
2293 static void
2294 move_end (WPanel * panel)
2296 if (panel->selected == panel->count - 1)
2297 return;
2299 unselect_item (panel);
2301 if (panels_options.torben_fj_mode)
2303 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2305 if (panel->selected < middle_pos)
2307 goto_middle_file (panel);
2308 return;
2310 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2312 goto_bottom_file (panel);
2313 return;
2317 panel->selected = panel->count - 1;
2318 paint_dir (panel);
2319 select_item (panel);
2322 /* --------------------------------------------------------------------------------------------- */
2324 static void
2325 do_mark_file (WPanel * panel, mark_act_t do_move)
2327 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2328 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2329 move_down (panel);
2330 else if (do_move == MARK_FORCE_UP)
2331 move_up (panel);
2334 /* --------------------------------------------------------------------------------------------- */
2336 static void
2337 mark_file (WPanel * panel)
2339 do_mark_file (panel, MARK_DOWN);
2342 /* --------------------------------------------------------------------------------------------- */
2344 static void
2345 mark_file_up (WPanel * panel)
2347 do_mark_file (panel, MARK_FORCE_UP);
2350 /* --------------------------------------------------------------------------------------------- */
2352 static void
2353 mark_file_down (WPanel * panel)
2355 do_mark_file (panel, MARK_FORCE_DOWN);
2358 /* --------------------------------------------------------------------------------------------- */
2360 static void
2361 mark_file_right (WPanel * panel)
2363 int lines = llines (panel);
2365 if (state_mark < 0)
2366 state_mark = selection (panel)->f.marked ? 0 : 1;
2368 lines = min (lines, panel->count - panel->selected - 1);
2369 for (; lines != 0; lines--)
2371 do_file_mark (panel, panel->selected, state_mark);
2372 move_down (panel);
2374 do_file_mark (panel, panel->selected, state_mark);
2377 /* --------------------------------------------------------------------------------------------- */
2379 static void
2380 mark_file_left (WPanel * panel)
2382 int lines = llines (panel);
2384 if (state_mark < 0)
2385 state_mark = selection (panel)->f.marked ? 0 : 1;
2387 lines = min (lines, panel->selected + 1);
2388 for (; lines != 0; lines--)
2390 do_file_mark (panel, panel->selected, state_mark);
2391 move_up (panel);
2393 do_file_mark (panel, panel->selected, state_mark);
2396 /* --------------------------------------------------------------------------------------------- */
2397 /** Incremental search of a file name in the panel.
2398 * @param panel instance of WPanel structure
2399 * @param c_code key code
2402 static void
2403 do_search (WPanel * panel, int c_code)
2405 size_t l;
2406 int i, sel;
2407 gboolean wrapped = FALSE;
2408 char *act;
2409 mc_search_t *search;
2410 char *reg_exp, *esc_str;
2411 gboolean is_found = FALSE;
2413 l = strlen (panel->search_buffer);
2414 if (c_code == KEY_BACKSPACE)
2416 if (l != 0)
2418 act = panel->search_buffer + l;
2419 str_prev_noncomb_char (&act, panel->search_buffer);
2420 act[0] = '\0';
2422 panel->search_chpoint = 0;
2424 else
2426 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2428 panel->search_char[panel->search_chpoint] = c_code;
2429 panel->search_chpoint++;
2432 if (panel->search_chpoint > 0)
2434 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2436 case -2:
2437 return;
2438 case -1:
2439 panel->search_chpoint = 0;
2440 return;
2441 default:
2442 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2444 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2445 l += panel->search_chpoint;
2446 *(panel->search_buffer + l) = '\0';
2447 panel->search_chpoint = 0;
2453 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2454 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2455 search = mc_search_new (esc_str, -1);
2456 search->search_type = MC_SEARCH_T_GLOB;
2457 search->is_entire_line = TRUE;
2458 switch (panels_options.qsearch_mode)
2460 case QSEARCH_CASE_SENSITIVE:
2461 search->is_case_sensitive = TRUE;
2462 break;
2463 case QSEARCH_CASE_INSENSITIVE:
2464 search->is_case_sensitive = FALSE;
2465 break;
2466 default:
2467 search->is_case_sensitive = panel->sort_info.case_sensitive;
2468 break;
2470 sel = panel->selected;
2471 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2473 if (i >= panel->count)
2475 i = 0;
2476 if (wrapped)
2477 break;
2478 wrapped = TRUE;
2480 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2482 sel = i;
2483 is_found = TRUE;
2484 break;
2487 if (is_found)
2489 unselect_item (panel);
2490 panel->selected = sel;
2491 select_item (panel);
2492 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2494 else if (c_code != KEY_BACKSPACE)
2496 act = panel->search_buffer + l;
2497 str_prev_noncomb_char (&act, panel->search_buffer);
2498 act[0] = '\0';
2500 mc_search_free (search);
2501 g_free (reg_exp);
2502 g_free (esc_str);
2505 /* --------------------------------------------------------------------------------------------- */
2506 /** Start new search.
2507 * @param panel instance of WPanel structure
2510 static void
2511 start_search (WPanel * panel)
2513 if (panel->searching)
2515 if (panel->selected + 1 == panel->count)
2516 panel->selected = 0;
2517 else
2518 move_down (panel);
2520 /* in case if there was no search string we need to recall
2521 previous string, with which we ended previous searching */
2522 if (panel->search_buffer[0] == '\0')
2523 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2524 sizeof (panel->search_buffer));
2526 do_search (panel, 0);
2528 else
2530 panel->searching = TRUE;
2531 panel->search_buffer[0] = '\0';
2532 panel->search_char[0] = '\0';
2533 panel->search_chpoint = 0;
2534 display_mini_info (panel);
2535 mc_refresh ();
2539 /* --------------------------------------------------------------------------------------------- */
2541 static void
2542 stop_search (WPanel * panel)
2544 panel->searching = FALSE;
2546 /* if user had overrdied search string, we need to store it
2547 to the previous_search_buffer */
2548 if (panel->search_buffer[0] != '\0')
2549 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2550 sizeof (panel->prev_search_buffer));
2552 display_mini_info (panel);
2555 /* --------------------------------------------------------------------------------------------- */
2556 /** Return 1 if the Enter key has been processed, 0 otherwise */
2558 static int
2559 do_enter_on_file_entry (file_entry * fe)
2561 vfs_path_t *full_name_vpath;
2562 gboolean ok;
2565 * Directory or link to directory - change directory.
2566 * Try the same for the entries on which mc_lstat() has failed.
2568 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2570 vfs_path_t *fname_vpath;
2572 fname_vpath = vfs_path_from_str (fe->fname);
2573 if (!do_cd (fname_vpath, cd_exact))
2574 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2575 vfs_path_free (fname_vpath);
2576 return 1;
2579 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2581 /* Try associated command */
2582 if (regex_command (full_name_vpath, "Open") != 0)
2584 vfs_path_free (full_name_vpath);
2585 return 1;
2588 /* Check if the file is executable */
2589 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2590 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2591 vfs_path_free (full_name_vpath);
2592 if (!ok)
2593 return 0;
2595 if (confirm_execute)
2597 if (query_dialog
2598 (_("The Midnight Commander"),
2599 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2600 return 1;
2603 if (!vfs_current_is_local ())
2605 int ret;
2606 vfs_path_t *tmp_vpath;
2608 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2609 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2610 vfs_path_free (tmp_vpath);
2611 /* We took action only if the dialog was shown or the execution
2612 * was successful */
2613 return confirm_execute || (ret == 0);
2617 char *tmp = name_quote (fe->fname, 0);
2618 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2619 g_free (tmp);
2620 shell_execute (cmd, 0);
2621 g_free (cmd);
2624 #ifdef HAVE_CHARSET
2625 mc_global.source_codepage = default_source_codepage;
2626 #endif
2628 return 1;
2631 /* --------------------------------------------------------------------------------------------- */
2633 static int
2634 do_enter (WPanel * panel)
2636 return do_enter_on_file_entry (selection (panel));
2639 /* --------------------------------------------------------------------------------------------- */
2641 static void
2642 chdir_other_panel (WPanel * panel)
2644 const file_entry *entry = &panel->dir.list[panel->selected];
2646 vfs_path_t *new_dir_vpath;
2647 char *sel_entry = NULL;
2649 if (get_other_type () != view_listing)
2651 set_display_type (get_other_index (), view_listing);
2654 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2655 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2656 else
2658 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2659 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2662 change_panel ();
2663 do_cd (new_dir_vpath, cd_exact);
2664 vfs_path_free (new_dir_vpath);
2666 if (sel_entry)
2667 try_to_select (current_panel, sel_entry);
2668 change_panel ();
2670 move_down (panel);
2673 /* --------------------------------------------------------------------------------------------- */
2675 * Make the current directory of the current panel also the current
2676 * directory of the other panel. Put the other panel to the listing
2677 * mode if needed. If the current panel is panelized, the other panel
2678 * doesn't become panelized.
2681 static void
2682 panel_sync_other (const WPanel * panel)
2684 if (get_other_type () != view_listing)
2686 set_display_type (get_other_index (), view_listing);
2689 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2691 /* try to select current filename on the other panel */
2692 if (!panel->is_panelized)
2694 try_to_select (other_panel, selection (panel)->fname);
2698 /* --------------------------------------------------------------------------------------------- */
2700 static void
2701 chdir_to_readlink (WPanel * panel)
2703 vfs_path_t *new_dir_vpath;
2704 char buffer[MC_MAXPATHLEN], *p;
2705 int i;
2706 struct stat st;
2707 vfs_path_t *panel_fname_vpath;
2708 gboolean ok;
2710 if (get_other_type () != view_listing)
2711 return;
2713 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2714 return;
2716 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2717 if (i < 0)
2718 return;
2720 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2721 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2722 vfs_path_free (panel_fname_vpath);
2723 if (!ok)
2724 return;
2726 buffer[i] = 0;
2727 if (!S_ISDIR (st.st_mode))
2729 p = strrchr (buffer, PATH_SEP);
2730 if (p && !p[1])
2732 *p = 0;
2733 p = strrchr (buffer, PATH_SEP);
2735 if (!p)
2736 return;
2737 p[1] = 0;
2739 if (*buffer == PATH_SEP)
2740 new_dir_vpath = vfs_path_from_str (buffer);
2741 else
2742 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2744 change_panel ();
2745 do_cd (new_dir_vpath, cd_exact);
2746 vfs_path_free (new_dir_vpath);
2747 change_panel ();
2749 move_down (panel);
2752 /* --------------------------------------------------------------------------------------------- */
2754 static gsize
2755 panel_get_format_field_count (WPanel * panel)
2757 format_e *format;
2758 gsize lc_index;
2759 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2760 return lc_index;
2763 /* --------------------------------------------------------------------------------------------- */
2765 function return 0 if not found and REAL_INDEX+1 if found
2768 static gsize
2769 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2771 format_e *format;
2772 gsize lc_index;
2774 for (lc_index = 1, format = panel->format;
2775 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2776 if (format == NULL)
2777 lc_index = 0;
2779 return lc_index;
2782 /* --------------------------------------------------------------------------------------------- */
2784 static format_e *
2785 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2787 format_e *format;
2788 for (format = panel->format;
2789 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2790 return format;
2793 /* --------------------------------------------------------------------------------------------- */
2795 static const panel_field_t *
2796 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2798 const panel_field_t *pfield;
2799 format_e *format;
2801 format = panel_get_format_field_by_index (panel, lc_index);
2802 if (format == NULL)
2803 return NULL;
2804 pfield = panel_get_field_by_title (format->title);
2805 if (pfield == NULL)
2806 return NULL;
2807 if (pfield->sort_routine == NULL)
2808 return NULL;
2809 return pfield;
2812 /* --------------------------------------------------------------------------------------------- */
2814 static void
2815 panel_toggle_sort_order_prev (WPanel * panel)
2817 gsize lc_index, i;
2818 gchar *title;
2820 const panel_field_t *pfield = NULL;
2822 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2823 lc_index = panel_get_format_field_index_by_name (panel, title);
2824 g_free (title);
2826 if (lc_index > 1)
2828 /* search for prev sortable column in panel format */
2829 for (i = lc_index - 1;
2830 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2833 if (pfield == NULL)
2835 /* Sortable field not found. Try to search in each array */
2836 for (i = panel_get_format_field_count (panel);
2837 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2840 if (pfield != NULL)
2842 panel->sort_info.sort_field = pfield;
2843 panel_set_sort_order (panel, pfield);
2847 /* --------------------------------------------------------------------------------------------- */
2849 static void
2850 panel_toggle_sort_order_next (WPanel * panel)
2852 gsize lc_index, i;
2853 const panel_field_t *pfield = NULL;
2854 gsize format_field_count;
2855 gchar *title;
2857 format_field_count = panel_get_format_field_count (panel);
2858 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2859 lc_index = panel_get_format_field_index_by_name (panel, title);
2860 g_free (title);
2862 if (lc_index != 0 && lc_index != format_field_count)
2864 /* search for prev sortable column in panel format */
2865 for (i = lc_index;
2866 i != format_field_count
2867 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2870 if (pfield == NULL)
2872 /* Sortable field not found. Try to search in each array */
2873 for (i = 0;
2874 i != format_field_count
2875 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2878 if (pfield != NULL)
2880 panel->sort_info.sort_field = pfield;
2881 panel_set_sort_order (panel, pfield);
2885 /* --------------------------------------------------------------------------------------------- */
2887 static void
2888 panel_select_sort_order (WPanel * panel)
2890 const panel_field_t *sort_order;
2892 sort_order = sort_box (&panel->sort_info);
2893 if (sort_order != NULL)
2895 panel->sort_info.sort_field = sort_order;
2896 panel_set_sort_order (panel, sort_order);
2900 /* --------------------------------------------------------------------------------------------- */
2903 * panel_content_scroll_left:
2904 * @param panel the pointer to the panel on which we operate
2906 * scroll long filename to the left (decrement scroll pointer)
2910 static void
2911 panel_content_scroll_left (WPanel * panel)
2913 if (panel->content_shift > -1)
2915 if (panel->content_shift > panel->max_shift)
2916 panel->content_shift = panel->max_shift;
2918 panel->content_shift--;
2919 show_dir (panel);
2920 paint_dir (panel);
2925 /* --------------------------------------------------------------------------------------------- */
2928 * panel_content_scroll_right:
2929 * @param panel the pointer to the panel on which we operate
2931 * scroll long filename to the right (increment scroll pointer)
2935 static void
2936 panel_content_scroll_right (WPanel * panel)
2938 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
2940 panel->content_shift++;
2941 show_dir (panel);
2942 paint_dir (panel);
2946 /* --------------------------------------------------------------------------------------------- */
2948 static void
2949 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2951 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2953 const panel_field_t *sort_order;
2955 sort_order = panel_get_field_by_id (name);
2956 if (sort_order == NULL)
2957 return;
2958 panel->sort_info.sort_field = sort_order;
2960 else
2961 panel->sort_info.reverse = !panel->sort_info.reverse;
2963 panel_set_sort_order (panel, panel->sort_info.sort_field);
2966 /* --------------------------------------------------------------------------------------------- */
2968 * If we moved to the parent directory move the selection pointer to
2969 * the old directory name; If we leave VFS dir, remove FS specificator.
2971 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2974 static const char *
2975 get_parent_dir_name (const char *cwd, const char *lwd)
2977 size_t llen, clen;
2978 const char *p;
2980 llen = strlen (lwd);
2981 clen = strlen (cwd);
2983 if (llen <= clen)
2984 return NULL;
2986 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2988 if (p == NULL)
2990 p = strrchr (lwd, PATH_SEP);
2992 if ((p != NULL)
2993 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2994 && (clen == (size_t) (p - lwd)
2995 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2996 return (p + 1);
2998 return NULL;
3001 /* skip VFS prefix */
3002 while (--p > lwd && *p != PATH_SEP)
3004 /* get last component */
3005 while (--p > lwd && *p != PATH_SEP)
3008 /* return last component */
3009 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
3012 /* --------------------------------------------------------------------------------------------- */
3013 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3015 static void
3016 subshell_chdir (const vfs_path_t * vpath)
3018 #ifdef ENABLE_SUBSHELL
3019 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3020 do_subshell_chdir (vpath, FALSE, TRUE);
3021 #else /* ENABLE_SUBSHELL */
3022 (void) vpath;
3023 #endif /* ENABLE_SUBSHELL */
3026 /* --------------------------------------------------------------------------------------------- */
3028 * Changes the current directory of the panel.
3029 * Don't record change in the directory history.
3032 static gboolean
3033 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3035 char *olddir;
3037 olddir = vfs_path_to_str (panel->cwd_vpath);
3039 /* Convert *new_path to a suitable pathname, handle ~user */
3040 if (cd_type == cd_parse_command)
3042 const vfs_path_element_t *element;
3044 element = vfs_path_get_by_index (new_dir_vpath, 0);
3045 if (strcmp (element->path, "-") == 0)
3046 new_dir_vpath = panel->lwd_vpath;
3049 if (mc_chdir (new_dir_vpath) == -1)
3051 panel_set_cwd (panel, olddir);
3052 g_free (olddir);
3053 return FALSE;
3056 /* Success: save previous directory, shutdown status of previous dir */
3057 panel_set_lwd (panel, olddir);
3058 input_free_completions (cmdline);
3060 vfs_path_free (panel->cwd_vpath);
3061 vfs_setup_cwd ();
3062 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3064 vfs_release_path (olddir);
3066 subshell_chdir (panel->cwd_vpath);
3068 /* Reload current panel */
3069 panel_clean_dir (panel);
3072 char *tmp_path;
3074 panel->count =
3075 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
3076 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3077 panel->sort_info.exec_first, panel->filter);
3078 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3079 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
3080 g_free (tmp_path);
3083 load_hint (0);
3084 panel->dirty = 1;
3085 update_xterm_title_path ();
3087 g_free (olddir);
3089 return TRUE;
3092 /* --------------------------------------------------------------------------------------------- */
3094 static void
3095 directory_history_next (WPanel * panel)
3097 gboolean ok;
3101 GList *next;
3103 ok = TRUE;
3104 next = g_list_next (panel->dir_history_current);
3105 if (next != NULL)
3107 vfs_path_t *data_vpath;
3109 data_vpath = vfs_path_from_str ((char *) next->data);
3110 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3111 vfs_path_free (data_vpath);
3112 panel->dir_history_current = next;
3114 /* skip directories that present in history but absent in file system */
3116 while (!ok);
3119 /* --------------------------------------------------------------------------------------------- */
3121 static void
3122 directory_history_prev (WPanel * panel)
3124 gboolean ok;
3128 GList *prev;
3130 ok = TRUE;
3131 prev = g_list_previous (panel->dir_history_current);
3132 if (prev != NULL)
3134 vfs_path_t *data_vpath;
3136 data_vpath = vfs_path_from_str ((char *) prev->data);
3137 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3138 vfs_path_free (data_vpath);
3139 panel->dir_history_current = prev;
3141 /* skip directories that present in history but absent in file system */
3143 while (!ok);
3146 /* --------------------------------------------------------------------------------------------- */
3148 static void
3149 directory_history_list (WPanel * panel)
3151 char *s;
3152 gboolean ok = FALSE;
3153 size_t pos;
3155 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3157 s = history_show (&panel->dir_history, &panel->widget, pos);
3158 if (s != NULL)
3160 vfs_path_t *s_vpath;
3162 s_vpath = vfs_path_from_str (s);
3163 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3164 if (ok)
3165 directory_history_add (panel, panel->cwd_vpath);
3166 else
3167 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3168 vfs_path_free (s_vpath);
3169 g_free (s);
3172 if (!ok)
3174 /* Since history is fully modified in history_show(), panel->dir_history actually
3175 * points to the invalid place. Try restore current postition here. */
3177 size_t i;
3179 panel->dir_history_current = panel->dir_history;
3181 for (i = 0; i <= pos; i++)
3183 GList *prev;
3185 prev = g_list_previous (panel->dir_history_current);
3186 if (prev == NULL)
3187 break;
3189 panel->dir_history_current = prev;
3194 /* --------------------------------------------------------------------------------------------- */
3196 static cb_ret_t
3197 panel_execute_cmd (WPanel * panel, unsigned long command)
3199 int res = MSG_HANDLED;
3201 if (command != CK_Search)
3202 stop_search (panel);
3205 switch (command)
3207 case CK_Up:
3208 case CK_Down:
3209 case CK_Left:
3210 case CK_Right:
3211 case CK_Bottom:
3212 case CK_Top:
3213 case CK_PageDown:
3214 case CK_PageUp:
3215 /* reset state of marks flag */
3216 state_mark = -1;
3217 break;
3219 switch (command)
3221 case CK_PanelOtherCd:
3222 chdir_other_panel (panel);
3223 break;
3224 case CK_PanelOtherCdLink:
3225 chdir_to_readlink (panel);
3226 break;
3227 case CK_CopySingle:
3228 copy_cmd_local ();
3229 break;
3230 case CK_DeleteSingle:
3231 delete_cmd_local ();
3232 break;
3233 case CK_Enter:
3234 do_enter (panel);
3235 break;
3236 case CK_ViewRaw:
3237 view_raw_cmd ();
3238 break;
3239 case CK_EditNew:
3240 edit_cmd_new ();
3241 break;
3242 case CK_MoveSingle:
3243 rename_cmd_local ();
3244 break;
3245 case CK_SelectInvert:
3246 select_invert_cmd ();
3247 break;
3248 case CK_Select:
3249 select_cmd ();
3250 break;
3251 case CK_Unselect:
3252 unselect_cmd ();
3253 break;
3254 case CK_PageDown:
3255 next_page (panel);
3256 break;
3257 case CK_PageUp:
3258 prev_page (panel);
3259 break;
3260 case CK_CdChild:
3261 goto_child_dir (panel);
3262 break;
3263 case CK_CdParent:
3264 goto_parent_dir (panel);
3265 break;
3266 case CK_History:
3267 directory_history_list (panel);
3268 break;
3269 case CK_HistoryNext:
3270 directory_history_next (panel);
3271 break;
3272 case CK_HistoryPrev:
3273 directory_history_prev (panel);
3274 break;
3275 case CK_BottomOnScreen:
3276 goto_bottom_file (panel);
3277 break;
3278 case CK_MiddleOnScreen:
3279 goto_middle_file (panel);
3280 break;
3281 case CK_TopOnScreen:
3282 goto_top_file (panel);
3283 break;
3284 case CK_Mark:
3285 mark_file (panel);
3286 break;
3287 case CK_MarkUp:
3288 mark_file_up (panel);
3289 break;
3290 case CK_MarkDown:
3291 mark_file_down (panel);
3292 break;
3293 case CK_MarkLeft:
3294 mark_file_left (panel);
3295 break;
3296 case CK_MarkRight:
3297 mark_file_right (panel);
3298 break;
3299 case CK_CdParentSmart:
3300 res = force_maybe_cd ();
3301 break;
3302 case CK_Up:
3303 move_up (panel);
3304 break;
3305 case CK_Down:
3306 move_down (panel);
3307 break;
3308 case CK_Left:
3309 res = move_left (panel);
3310 break;
3311 case CK_Right:
3312 res = move_right (panel);
3313 break;
3314 case CK_Bottom:
3315 move_end (panel);
3316 break;
3317 case CK_Top:
3318 move_home (panel);
3319 break;
3320 #ifdef HAVE_CHARSET
3321 case CK_SelectCodepage:
3322 panel_change_encoding (panel);
3323 break;
3324 #endif
3325 case CK_ScrollLeft:
3326 panel_content_scroll_left (panel);
3327 break;
3328 case CK_ScrollRight:
3329 panel_content_scroll_right (panel);
3330 break;
3331 case CK_Search:
3332 start_search (panel);
3333 break;
3334 case CK_SearchStop:
3335 break;
3336 case CK_PanelOtherSync:
3337 panel_sync_other (panel);
3338 break;
3339 case CK_Sort:
3340 panel_select_sort_order (panel);
3341 break;
3342 case CK_SortPrev:
3343 panel_toggle_sort_order_prev (panel);
3344 break;
3345 case CK_SortNext:
3346 panel_toggle_sort_order_next (panel);
3347 break;
3348 case CK_SortReverse:
3349 panel->sort_info.reverse = !panel->sort_info.reverse;
3350 panel_set_sort_order (panel, panel->sort_info.sort_field);
3351 break;
3352 case CK_SortByName:
3353 panel_set_sort_type_by_id (panel, "name");
3354 break;
3355 case CK_SortByExt:
3356 panel_set_sort_type_by_id (panel, "extension");
3357 break;
3358 case CK_SortBySize:
3359 panel_set_sort_type_by_id (panel, "size");
3360 break;
3361 case CK_SortByMTime:
3362 panel_set_sort_type_by_id (panel, "mtime");
3363 break;
3365 return res;
3368 /* --------------------------------------------------------------------------------------------- */
3370 static cb_ret_t
3371 panel_key (WPanel * panel, int key)
3373 size_t i;
3375 if (is_abort_char (key))
3377 stop_search (panel);
3378 return MSG_HANDLED;
3381 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3383 do_search (panel, key);
3384 return MSG_HANDLED;
3387 for (i = 0; panel_map[i].key != 0; i++)
3388 if (key == panel_map[i].key)
3389 return panel_execute_cmd (panel, panel_map[i].command);
3391 if (panels_options.torben_fj_mode && key == ALT ('h'))
3393 goto_middle_file (panel);
3394 return MSG_HANDLED;
3397 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3399 start_search (panel);
3400 do_search (panel, key);
3401 return MSG_HANDLED;
3404 return MSG_NOT_HANDLED;
3407 /* --------------------------------------------------------------------------------------------- */
3409 static cb_ret_t
3410 panel_callback (Widget * w, widget_msg_t msg, int parm)
3412 WPanel *panel = (WPanel *) w;
3413 WButtonBar *bb;
3415 switch (msg)
3417 case WIDGET_INIT:
3418 /* subscribe to "history_load" event */
3419 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3420 /* subscribe to "history_save" event */
3421 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3422 return MSG_HANDLED;
3424 case WIDGET_DRAW:
3425 /* Repaint everything, including frame and separator */
3426 paint_frame (panel); /* including show_dir */
3427 paint_dir (panel);
3428 mini_info_separator (panel);
3429 display_mini_info (panel);
3430 panel->dirty = 0;
3431 return MSG_HANDLED;
3433 case WIDGET_FOCUS:
3434 state_mark = -1;
3435 current_panel = panel;
3436 panel->active = 1;
3437 if (mc_chdir (panel->cwd_vpath) != 0)
3439 char *cwd;
3441 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3442 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3443 cwd, unix_error_string (errno));
3444 g_free (cwd);
3446 else
3447 subshell_chdir (panel->cwd_vpath);
3449 update_xterm_title_path ();
3450 select_item (panel);
3451 show_dir (panel);
3452 paint_dir (panel);
3453 panel->dirty = 0;
3455 bb = find_buttonbar (panel->widget.owner);
3456 midnight_set_buttonbar (bb);
3457 buttonbar_redraw (bb);
3458 return MSG_HANDLED;
3460 case WIDGET_UNFOCUS:
3461 /* Janne: look at this for the multiple panel options */
3462 stop_search (panel);
3463 panel->active = 0;
3464 show_dir (panel);
3465 unselect_item (panel);
3466 return MSG_HANDLED;
3468 case WIDGET_KEY:
3469 return panel_key (panel, parm);
3471 case WIDGET_COMMAND:
3472 return panel_execute_cmd (panel, parm);
3474 case WIDGET_DESTROY:
3475 /* unsubscribe from "history_load" event */
3476 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3477 /* unsubscribe from "history_save" event */
3478 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3479 panel_destroy (panel);
3480 free_my_statfs ();
3481 return MSG_HANDLED;
3483 default:
3484 return default_proc (msg, parm);
3488 /* --------------------------------------------------------------------------------------------- */
3489 /* */
3490 /* Panel mouse events support routines */
3491 /* */
3493 static void
3494 mouse_toggle_mark (WPanel * panel)
3496 do_mark_file (panel, MARK_DONT_MOVE);
3497 mouse_marking = selection (panel)->f.marked;
3498 mouse_mark_panel = current_panel;
3501 /* --------------------------------------------------------------------------------------------- */
3503 static void
3504 mouse_set_mark (WPanel * panel)
3507 if (mouse_mark_panel == panel)
3509 if (mouse_marking && !(selection (panel)->f.marked))
3510 do_mark_file (panel, MARK_DONT_MOVE);
3511 else if (!mouse_marking && (selection (panel)->f.marked))
3512 do_mark_file (panel, MARK_DONT_MOVE);
3516 /* --------------------------------------------------------------------------------------------- */
3518 static gboolean
3519 mark_if_marking (WPanel * panel, Gpm_Event * event)
3521 if ((event->buttons & GPM_B_RIGHT) != 0)
3523 if ((event->type & GPM_DOWN) != 0)
3524 mouse_toggle_mark (panel);
3525 else
3526 mouse_set_mark (panel);
3527 return TRUE;
3529 return FALSE;
3532 /* --------------------------------------------------------------------------------------------- */
3533 /** Determine which column was clicked, and sort the panel on
3534 * that column, or reverse sort on that column if already
3535 * sorted on that column.
3538 static void
3539 mouse_sort_col (WPanel * panel, int x)
3541 int i;
3542 const char *lc_sort_name = NULL;
3543 panel_field_t *col_sort_format = NULL;
3544 format_e *format;
3545 gchar *title;
3547 for (i = 0, format = panel->format; format != NULL; format = format->next)
3549 i += format->field_len;
3550 if (x < i + 1)
3552 /* found column */
3553 lc_sort_name = format->title;
3554 break;
3558 if (lc_sort_name == NULL)
3559 return;
3561 for (i = 0; panel_fields[i].id != NULL; i++)
3563 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3564 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3566 col_sort_format = &panel_fields[i];
3567 g_free (title);
3568 break;
3570 g_free (title);
3573 if (col_sort_format == NULL)
3574 return;
3576 if (panel->sort_info.sort_field == col_sort_format)
3578 /* reverse the sort if clicked column is already the sorted column */
3579 panel->sort_info.reverse = !panel->sort_info.reverse;
3581 else
3583 /* new sort is forced to be ascending */
3584 panel->sort_info.reverse = FALSE;
3586 panel_set_sort_order (panel, col_sort_format);
3590 /* --------------------------------------------------------------------------------------------- */
3592 * Mouse callback of the panel minus repainting.
3594 static int
3595 panel_event (Gpm_Event * event, void *data)
3597 WPanel *panel = (WPanel *) data;
3598 Widget *w = (Widget *) data;
3600 const int lines = llines (panel);
3601 const gboolean is_active = dlg_widget_active (panel);
3602 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3603 Gpm_Event local;
3605 if (!mouse_global_in_widget (event, (Widget *) data))
3606 return MOU_UNHANDLED;
3608 local = mouse_get_local (event, w);
3610 /* 1st line */
3611 if (local.y == 1)
3613 /* "<" button */
3614 if (mouse_down && local.x == 2)
3616 directory_history_prev (panel);
3617 goto finish;
3620 /* ">" button */
3621 if (mouse_down && local.x == w->cols - 1)
3623 directory_history_next (panel);
3624 goto finish;
3627 /* "^" button */
3628 if (mouse_down && local.x >= w->cols - 4 && local.x <= w->cols - 2)
3630 directory_history_list (panel);
3631 goto finish;
3634 /* "." button show/hide hidden files */
3635 if (mouse_down && local.x == w->cols - 5)
3637 midnight_dlg->callback (midnight_dlg, NULL, DLG_ACTION, CK_ShowHidden, NULL);
3638 goto finish;
3641 /* no other events on 1st line */
3642 return MOU_UNHANDLED;
3645 /* sort on clicked column; don't handle wheel events */
3646 if (mouse_down && (local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && local.y == 2)
3648 mouse_sort_col (panel, local.x);
3649 goto finish;
3652 /* Mouse wheel events */
3653 if (mouse_down && (local.buttons & GPM_B_UP) != 0)
3655 if (is_active)
3657 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3658 prev_page (panel);
3659 else /* We are in first page */
3660 move_up (panel);
3662 goto finish;
3665 if (mouse_down && (local.buttons & GPM_B_DOWN) != 0)
3667 if (is_active)
3669 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3670 next_page (panel);
3671 else /* We are in last page */
3672 move_down (panel);
3674 goto finish;
3677 local.y -= 2;
3678 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
3680 int my_index;
3682 if (!is_active)
3683 change_panel ();
3685 if (panel->top_file + local.y > panel->count)
3686 my_index = panel->count - 1;
3687 else
3689 my_index = panel->top_file + local.y - 1;
3690 if (panel->split && (local.x > (w->cols - 2) / 2))
3691 my_index += llines (panel);
3693 if (my_index >= panel->count)
3694 my_index = panel->count - 1;
3697 if (my_index != panel->selected)
3699 unselect_item (panel);
3700 panel->selected = my_index;
3701 select_item (panel);
3704 /* This one is new */
3705 mark_if_marking (panel, &local);
3707 else if ((local.type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE) &&
3708 local.y > 0 && local.y <= lines)
3709 do_enter (panel);
3711 finish:
3712 if (panel->dirty)
3713 send_message (w, WIDGET_DRAW, 0);
3714 return MOU_NORMAL;
3717 /* --------------------------------------------------------------------------------------------- */
3719 static void
3720 reload_panelized (WPanel * panel)
3722 int i, j;
3723 dir_list *list = &panel->dir;
3725 if (panel != current_panel)
3726 (void) mc_chdir (panel->cwd_vpath);
3728 for (i = 0, j = 0; i < panel->count; i++)
3730 vfs_path_t *vpath;
3732 if (list->list[i].f.marked)
3734 /* Unmark the file in advance. In case the following mc_lstat
3735 * fails we are done, else we have to mark the file again
3736 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3737 * IMO that's the best way to update the panel's summary status
3738 * -- Norbert
3740 do_file_mark (panel, i, 0);
3742 vpath = vfs_path_from_str (list->list[i].fname);
3743 if (mc_lstat (vpath, &list->list[i].st))
3744 g_free (list->list[i].fname);
3745 else
3747 if (list->list[i].f.marked)
3748 do_file_mark (panel, i, 1);
3749 if (j != i)
3750 list->list[j] = list->list[i];
3751 j++;
3753 vfs_path_free (vpath);
3755 if (j == 0)
3756 panel->count = set_zero_dir (list) ? 1 : 0;
3757 else
3758 panel->count = j;
3760 if (panel != current_panel)
3761 (void) mc_chdir (current_panel->cwd_vpath);
3764 /* --------------------------------------------------------------------------------------------- */
3766 static void
3767 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3769 gboolean free_pointer;
3770 char *my_current_file = NULL;
3772 if ((flags & UP_RELOAD) != 0)
3774 panel->is_panelized = FALSE;
3775 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3776 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3779 /* If current_file == -1 (an invalid pointer) then preserve selection */
3780 free_pointer = current_file == UP_KEEPSEL;
3782 if (free_pointer)
3784 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3785 current_file = my_current_file;
3788 if (panel->is_panelized)
3789 reload_panelized (panel);
3790 else
3791 panel_reload (panel);
3793 try_to_select (panel, current_file);
3794 panel->dirty = 1;
3796 if (free_pointer)
3797 g_free (my_current_file);
3800 /* --------------------------------------------------------------------------------------------- */
3802 static void
3803 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3805 if (get_display_type (which) == view_listing)
3807 WPanel *panel;
3809 panel = (WPanel *) get_panel_widget (which);
3810 if (panel->is_panelized)
3811 flags &= ~UP_RELOAD;
3812 update_one_panel_widget (panel, flags, current_file);
3816 /* --------------------------------------------------------------------------------------------- */
3818 static void
3819 do_select (WPanel * panel, int i)
3821 if (i != panel->selected)
3823 panel->dirty = 1;
3824 panel->selected = i;
3825 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3826 if (panel->top_file < 0)
3827 panel->top_file = 0;
3831 /* --------------------------------------------------------------------------------------------- */
3833 static void
3834 do_try_to_select (WPanel * panel, const char *name)
3836 int i;
3837 char *subdir;
3839 if (!name)
3841 do_select (panel, 0);
3842 return;
3845 /* We only want the last component of the directory,
3846 * and from this only the name without suffix.
3847 * Cut prefix if the panel is not panelized */
3849 if (panel->is_panelized)
3850 subdir = vfs_strip_suffix_from_filename (name);
3851 else
3852 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3854 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3855 for (i = 0; i < panel->count; i++)
3857 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3859 do_select (panel, i);
3860 g_free (subdir);
3861 return;
3865 /* Try to select a file near the file that is missing */
3866 if (panel->selected >= panel->count)
3867 do_select (panel, panel->count - 1);
3868 g_free (subdir);
3871 /* --------------------------------------------------------------------------------------------- */
3873 /* event callback */
3874 static gboolean
3875 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3876 gpointer init_data, gpointer data)
3878 (void) event_group_name;
3879 (void) event_name;
3880 (void) init_data;
3881 (void) data;
3883 update_panels (UP_RELOAD, UP_KEEPSEL);
3885 return TRUE;
3888 /* --------------------------------------------------------------------------------------------- */
3890 /* event callback */
3891 static gboolean
3892 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3893 gpointer init_data, gpointer data)
3895 (void) event_group_name;
3896 (void) event_name;
3897 (void) init_data;
3898 (void) data;
3900 if (current_panel->marked == 0)
3901 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3902 (gpointer) selection (current_panel)->fname);
3903 else
3905 int i;
3906 gboolean first = TRUE;
3907 char *flist = NULL;
3909 for (i = 0; i < current_panel->count; i++)
3910 if (current_panel->dir.list[i].f.marked != 0)
3911 { /* Skip the unmarked ones */
3912 if (first)
3914 flist = g_strdup (current_panel->dir.list[i].fname);
3915 first = FALSE;
3917 else
3919 /* Add empty lines after the file */
3920 char *tmp;
3922 tmp =
3923 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3924 g_free (flist);
3925 flist = tmp;
3929 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3930 g_free (flist);
3932 return TRUE;
3935 /* --------------------------------------------------------------------------------------------- */
3936 /*** public functions ****************************************************************************/
3937 /* --------------------------------------------------------------------------------------------- */
3939 void
3940 try_to_select (WPanel * panel, const char *name)
3942 do_try_to_select (panel, name);
3943 select_item (panel);
3946 /* --------------------------------------------------------------------------------------------- */
3948 void
3949 panel_clean_dir (WPanel * panel)
3951 int count = panel->count;
3953 panel->count = 0;
3954 panel->top_file = 0;
3955 panel->selected = 0;
3956 panel->marked = 0;
3957 panel->dirs_marked = 0;
3958 panel->total = 0;
3959 panel->searching = FALSE;
3960 panel->is_panelized = FALSE;
3961 panel->dirty = 1;
3962 panel->content_shift = -1;
3963 panel->max_shift = -1;
3965 clean_dir (&panel->dir, count);
3968 /* --------------------------------------------------------------------------------------------- */
3970 * Set Up panel's current dir object
3972 * @param panel panel object
3973 * @param path_str string contain path
3976 void
3977 panel_set_cwd (WPanel * panel, const char *path_str)
3979 vfs_path_free (panel->cwd_vpath);
3980 panel->cwd_vpath = vfs_path_from_str (path_str);
3983 /* --------------------------------------------------------------------------------------------- */
3985 * Set Up panel's last working dir object
3987 * @param panel panel object
3988 * @param path_str string contain path
3991 void
3992 panel_set_lwd (WPanel * panel, const char *path_str)
3994 vfs_path_free (panel->lwd_vpath);
3995 panel->lwd_vpath = vfs_path_from_str (path_str);
3998 /* --------------------------------------------------------------------------------------------- */
3999 /** Panel creation.
4000 * @param panel_name the name of the panel for setup retieving
4001 * @return new instance of WPanel
4004 WPanel *
4005 panel_new (const char *panel_name)
4007 return panel_new_with_dir (panel_name, NULL);
4010 /* --------------------------------------------------------------------------------------------- */
4012 * Panel creation for specified directory.
4014 * @param panel_name specifies the name of the panel for setup retieving
4015 * @param wpath the path of working panel directory. If path is NULL then panel will be created
4016 * for current directory
4018 * @return new instance of WPanel
4021 WPanel *
4022 panel_new_with_dir (const char *panel_name, const char *wpath)
4024 WPanel *panel;
4025 char *section;
4026 int i, err;
4027 char *curdir = NULL;
4029 panel = g_new0 (WPanel, 1);
4031 /* No know sizes of the panel at startup */
4032 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
4034 /* We do not want the cursor */
4035 widget_want_cursor (panel->widget, 0);
4037 if (wpath != NULL)
4039 curdir = _vfs_get_cwd ();
4040 panel_set_cwd (panel, wpath);
4042 else
4044 vfs_setup_cwd ();
4045 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4048 panel_set_lwd (panel, ".");
4050 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4051 /* directories history will be get later */
4053 panel->dir.list = g_new (file_entry, MIN_FILES);
4054 panel->dir.size = MIN_FILES;
4055 panel->active = 0;
4056 panel->filter = 0;
4057 panel->split = 0;
4058 panel->top_file = 0;
4059 panel->selected = 0;
4060 panel->marked = 0;
4061 panel->total = 0;
4062 panel->dirty = 1;
4063 panel->searching = FALSE;
4064 panel->dirs_marked = 0;
4065 panel->is_panelized = FALSE;
4066 panel->format = 0;
4067 panel->status_format = 0;
4068 panel->format_modified = 1;
4069 panel->content_shift = -1;
4070 panel->max_shift = -1;
4072 panel->panel_name = g_strdup (panel_name);
4073 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4075 #ifdef HAVE_CHARSET
4076 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4077 #endif
4079 for (i = 0; i < LIST_TYPES; i++)
4080 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4082 panel->search_buffer[0] = '\0';
4083 panel->prev_search_buffer[0] = '\0';
4084 panel->frame_size = frame_half;
4086 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4087 if (!mc_config_has_group (mc_main_config, section))
4089 g_free (section);
4090 section = g_strdup (panel->panel_name);
4092 panel_load_setup (panel, section);
4093 g_free (section);
4095 /* Load format strings */
4096 err = set_panel_formats (panel);
4097 if (err != 0)
4098 set_panel_formats (panel);
4100 #ifdef HAVE_CHARSET
4102 const vfs_path_element_t *path_element;
4104 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4105 if (path_element->encoding != NULL)
4106 panel->codepage = get_codepage_index (path_element->encoding);
4108 #endif
4110 if (mc_chdir (panel->cwd_vpath) != 0)
4112 #ifdef HAVE_CHARSET
4113 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4114 #endif
4115 vfs_setup_cwd ();
4116 vfs_path_free (panel->cwd_vpath);
4117 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4120 /* Load the default format */
4121 panel->count =
4122 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4123 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4124 panel->sort_info.exec_first, panel->filter);
4126 /* Restore old right path */
4127 if (curdir != NULL)
4129 vfs_path_t *vpath;
4131 vpath = vfs_path_from_str (curdir);
4132 err = mc_chdir (vpath);
4133 vfs_path_free (vpath);
4135 g_free (curdir);
4137 return panel;
4140 /* --------------------------------------------------------------------------------------------- */
4142 void
4143 panel_reload (WPanel * panel)
4145 struct stat current_stat;
4146 char *tmp_path;
4147 gboolean ok;
4149 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4150 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
4151 && current_stat.st_ctime == panel->dir_stat.st_ctime
4152 && current_stat.st_mtime == panel->dir_stat.st_mtime);
4153 g_free (tmp_path);
4155 if (ok)
4156 return;
4160 char *last_slash;
4162 if (mc_chdir (panel->cwd_vpath) != -1)
4163 break;
4165 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4166 if (tmp_path[0] == PATH_SEP && tmp_path[1] == '\0')
4168 panel_clean_dir (panel);
4169 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
4170 g_free (tmp_path);
4171 return;
4173 last_slash = strrchr (tmp_path, PATH_SEP);
4174 vfs_path_free (panel->cwd_vpath);
4175 if (last_slash == NULL || last_slash == tmp_path)
4176 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4177 else
4179 *last_slash = '\0';
4180 panel->cwd_vpath = vfs_path_from_str (tmp_path);
4182 g_free (tmp_path);
4183 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4184 show_dir (panel);
4186 while (TRUE);
4188 panel->count =
4189 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4190 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4191 panel->sort_info.exec_first, panel->filter);
4193 panel->dirty = 1;
4194 if (panel->selected >= panel->count)
4195 do_select (panel, panel->count - 1);
4197 recalculate_panel_summary (panel);
4200 /* --------------------------------------------------------------------------------------------- */
4201 /* Switches the panel to the mode specified in the format */
4202 /* Seting up both format and status string. Return: 0 - on success; */
4203 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4206 set_panel_formats (WPanel * p)
4208 format_e *form;
4209 char *err = NULL;
4210 int retcode = 0;
4212 form = use_display_format (p, panel_format (p), &err, 0);
4214 if (err != NULL)
4216 g_free (err);
4217 retcode = 1;
4219 else
4221 delete_format (p->format);
4222 p->format = form;
4225 if (panels_options.show_mini_info)
4227 form = use_display_format (p, mini_status_format (p), &err, 1);
4229 if (err != NULL)
4231 g_free (err);
4232 retcode += 2;
4234 else
4236 delete_format (p->status_format);
4237 p->status_format = form;
4241 panel_format_modified (p);
4242 panel_update_cols (&(p->widget), p->frame_size);
4244 if (retcode)
4245 message (D_ERROR, _("Warning"),
4246 _("User supplied format looks invalid, reverting to default."));
4247 if (retcode & 0x01)
4249 g_free (p->user_format);
4250 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4252 if (retcode & 0x02)
4254 g_free (p->user_status_format[p->list_type]);
4255 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4258 return retcode;
4261 /* --------------------------------------------------------------------------------------------- */
4263 void
4264 panel_update_cols (Widget * widget, panel_display_t frame_size)
4266 int cols, origin;
4268 /* don't touch panel if it is not in dialog yet */
4269 /* if panel is not in dialog it is not in widgets list
4270 and cannot be compared with get_panel_widget() result */
4271 if (widget->owner == NULL)
4272 return;
4274 if (panels_layout.horizontal_split)
4276 widget->cols = COLS;
4277 return;
4280 if (frame_size == frame_full)
4282 cols = COLS;
4283 origin = 0;
4285 else if (widget == get_panel_widget (0))
4287 cols = panels_layout.left_panel_size;
4288 origin = 0;
4290 else
4292 cols = COLS - panels_layout.left_panel_size;
4293 origin = panels_layout.left_panel_size;
4296 widget->cols = cols;
4297 widget->x = origin;
4300 /* --------------------------------------------------------------------------------------------- */
4302 /* Select current item and readjust the panel */
4303 void
4304 select_item (WPanel * panel)
4306 /* Although currently all over the code we set the selection and
4307 top file to decent values before calling select_item, I could
4308 forget it someday, so it's better to do the actual fitting here */
4310 if (panel->selected < 0)
4311 panel->selected = 0;
4313 if (panel->selected > panel->count - 1)
4314 panel->selected = panel->count - 1;
4316 adjust_top_file (panel);
4318 panel->dirty = 1;
4320 execute_hooks (select_file_hook);
4323 /* --------------------------------------------------------------------------------------------- */
4324 /** Clears all files in the panel, used only when one file was marked */
4325 void
4326 unmark_files (WPanel * panel)
4328 int i;
4330 if (!panel->marked)
4331 return;
4332 for (i = 0; i < panel->count; i++)
4333 file_mark (panel, i, 0);
4335 panel->dirs_marked = 0;
4336 panel->marked = 0;
4337 panel->total = 0;
4340 /* --------------------------------------------------------------------------------------------- */
4341 /** Recalculate the panels summary information, used e.g. when marked
4342 files might have been removed by an external command */
4344 void
4345 recalculate_panel_summary (WPanel * panel)
4347 int i;
4349 panel->marked = 0;
4350 panel->dirs_marked = 0;
4351 panel->total = 0;
4353 for (i = 0; i < panel->count; i++)
4354 if (panel->dir.list[i].f.marked)
4356 /* do_file_mark will return immediately if newmark == oldmark.
4357 So we have to first unmark it to get panel's summary information
4358 updated. (Norbert) */
4359 panel->dir.list[i].f.marked = 0;
4360 do_file_mark (panel, i, 1);
4364 /* --------------------------------------------------------------------------------------------- */
4365 /** This routine marks a file or a directory */
4367 void
4368 do_file_mark (WPanel * panel, int idx, int mark)
4370 if (panel->dir.list[idx].f.marked == mark)
4371 return;
4373 /* Only '..' can't be marked, '.' isn't visible */
4374 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4375 return;
4377 file_mark (panel, idx, mark);
4378 if (panel->dir.list[idx].f.marked)
4380 panel->marked++;
4381 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4383 if (panel->dir.list[idx].f.dir_size_computed)
4384 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4385 panel->dirs_marked++;
4387 else
4388 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4389 set_colors (panel);
4391 else
4393 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4395 if (panel->dir.list[idx].f.dir_size_computed)
4396 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4397 panel->dirs_marked--;
4399 else
4400 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4401 panel->marked--;
4405 /* --------------------------------------------------------------------------------------------- */
4407 * Changes the current directory of the panel.
4408 * Record change in the directory history.
4410 gboolean
4411 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4413 gboolean r;
4415 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4416 if (r)
4417 directory_history_add (panel, panel->cwd_vpath);
4418 return r;
4421 /* --------------------------------------------------------------------------------------------- */
4423 void
4424 file_mark (WPanel * panel, int lc_index, int val)
4426 if (panel->dir.list[lc_index].f.marked != val)
4428 panel->dir.list[lc_index].f.marked = val;
4429 panel->dirty = 1;
4433 /* --------------------------------------------------------------------------------------------- */
4435 void
4436 panel_re_sort (WPanel * panel)
4438 char *filename;
4439 int i;
4441 if (panel == NULL)
4442 return;
4444 filename = g_strdup (selection (panel)->fname);
4445 unselect_item (panel);
4446 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4447 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4448 panel->sort_info.exec_first);
4449 panel->selected = -1;
4450 for (i = panel->count; i; i--)
4452 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4454 panel->selected = i - 1;
4455 break;
4458 g_free (filename);
4459 panel->top_file = panel->selected - ITEMS (panel) / 2;
4460 select_item (panel);
4461 panel->dirty = 1;
4464 /* --------------------------------------------------------------------------------------------- */
4466 void
4467 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4469 if (sort_order == NULL)
4470 return;
4472 panel->sort_info.sort_field = sort_order;
4474 /* The directory is already sorted, we have to load the unsorted stuff */
4475 if (sort_order->sort_routine == (sortfn *) unsorted)
4477 char *current_file;
4479 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4480 panel_reload (panel);
4481 try_to_select (panel, current_file);
4482 g_free (current_file);
4484 panel_re_sort (panel);
4487 /* --------------------------------------------------------------------------------------------- */
4489 #ifdef HAVE_CHARSET
4492 * Change panel encoding.
4493 * @param panel WPanel object
4496 void
4497 panel_change_encoding (WPanel * panel)
4499 const char *encoding = NULL;
4500 char *errmsg;
4501 int r;
4503 r = select_charset (-1, -1, panel->codepage, FALSE);
4505 if (r == SELECT_CHARSET_CANCEL)
4506 return; /* Cancel */
4508 panel->codepage = r;
4510 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4512 /* No translation */
4513 vfs_path_t *cd_path_vpath;
4515 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4516 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4517 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4518 show_dir (panel);
4519 vfs_path_free (cd_path_vpath);
4520 return;
4523 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4524 if (errmsg != NULL)
4526 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4527 g_free (errmsg);
4528 return;
4531 encoding = get_codepage_id (panel->codepage);
4532 if (encoding != NULL)
4534 char *cd_path;
4535 vfs_change_encoding (panel->cwd_vpath, encoding);
4537 cd_path = vfs_path_to_str (panel->cwd_vpath);
4538 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4539 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4540 g_free (cd_path);
4544 /* --------------------------------------------------------------------------------------------- */
4547 * Remove encode info from last path element.
4550 vfs_path_t *
4551 remove_encoding_from_path (const vfs_path_t * vpath)
4553 vfs_path_t *ret_vpath;
4554 GString *tmp_conv;
4555 int indx;
4557 ret_vpath = vfs_path_new ();
4559 tmp_conv = g_string_new ("");
4561 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4563 GIConv converter;
4564 vfs_path_element_t *path_element;
4566 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4567 vfs_path_add_element (ret_vpath, path_element);
4569 if (path_element->encoding == NULL)
4570 continue;
4572 converter = str_crt_conv_to (path_element->encoding);
4573 if (converter == INVALID_CONV)
4574 continue;
4576 g_free (path_element->encoding);
4577 path_element->encoding = NULL;
4579 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4581 g_free (path_element->path);
4582 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4584 g_string_set_size (tmp_conv, 0);
4586 str_close_conv (converter);
4587 str_close_conv (path_element->dir.converter);
4588 path_element->dir.converter = INVALID_CONV;
4590 g_string_free (tmp_conv, TRUE);
4591 return ret_vpath;
4593 #endif /* HAVE_CHARSET */
4595 /* --------------------------------------------------------------------------------------------- */
4598 * This routine reloads the directory in both panels. It tries to
4599 * select current_file in current_panel and other_file in other_panel.
4600 * If current_file == -1 then it automatically sets current_file and
4601 * other_file to the currently selected files in the panels.
4603 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4604 * will not reload the other panel.
4606 * @param flags for reload panel
4607 * @param current_file name of the current file
4610 void
4611 update_panels (panel_update_flags_t flags, const char *current_file)
4613 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4614 WPanel *panel;
4616 update_one_panel (get_current_index (), flags, current_file);
4617 if (reload_other)
4618 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4620 if (get_current_type () == view_listing)
4621 panel = (WPanel *) get_panel_widget (get_current_index ());
4622 else
4623 panel = (WPanel *) get_panel_widget (get_other_index ());
4625 if (!panel->is_panelized)
4626 (void) mc_chdir (panel->cwd_vpath);
4629 /* --------------------------------------------------------------------------------------------- */
4631 gsize
4632 panel_get_num_of_sortable_fields (void)
4634 gsize ret = 0, lc_index;
4636 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4637 if (panel_fields[lc_index].is_user_choice)
4638 ret++;
4639 return ret;
4642 /* --------------------------------------------------------------------------------------------- */
4644 const char **
4645 panel_get_sortable_fields (gsize * array_size)
4647 char **ret;
4648 gsize lc_index, i;
4650 lc_index = panel_get_num_of_sortable_fields ();
4652 ret = g_try_new0 (char *, lc_index + 1);
4653 if (ret == NULL)
4654 return NULL;
4656 if (array_size != NULL)
4657 *array_size = lc_index;
4659 lc_index = 0;
4661 for (i = 0; panel_fields[i].id != NULL; i++)
4662 if (panel_fields[i].is_user_choice)
4663 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4664 return (const char **) ret;
4667 /* --------------------------------------------------------------------------------------------- */
4669 const panel_field_t *
4670 panel_get_field_by_id (const char *name)
4672 gsize lc_index;
4673 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4674 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4675 return &panel_fields[lc_index];
4676 return NULL;
4679 /* --------------------------------------------------------------------------------------------- */
4681 const panel_field_t *
4682 panel_get_field_by_title_hotkey (const char *name)
4684 gsize lc_index;
4685 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4686 if (panel_fields[lc_index].title_hotkey != NULL &&
4687 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4688 return &panel_fields[lc_index];
4689 return NULL;
4692 /* --------------------------------------------------------------------------------------------- */
4694 const panel_field_t *
4695 panel_get_field_by_title (const char *name)
4697 gsize lc_index;
4698 gchar *title = NULL;
4700 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4702 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4703 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4705 g_free (title);
4706 return &panel_fields[lc_index];
4709 g_free (title);
4710 return NULL;
4713 /* --------------------------------------------------------------------------------------------- */
4715 gsize
4716 panel_get_num_of_user_possible_fields (void)
4718 gsize ret = 0, lc_index;
4720 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4721 if (panel_fields[lc_index].use_in_user_format)
4722 ret++;
4723 return ret;
4726 /* --------------------------------------------------------------------------------------------- */
4728 const char **
4729 panel_get_user_possible_fields (gsize * array_size)
4731 char **ret;
4732 gsize lc_index, i;
4734 lc_index = panel_get_num_of_user_possible_fields ();
4736 ret = g_try_new0 (char *, lc_index + 1);
4737 if (ret == NULL)
4738 return NULL;
4740 if (array_size != NULL)
4741 *array_size = lc_index;
4743 lc_index = 0;
4745 for (i = 0; panel_fields[i].id != NULL; i++)
4746 if (panel_fields[i].use_in_user_format)
4747 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4748 return (const char **) ret;
4751 /* --------------------------------------------------------------------------------------------- */
4753 void
4754 panel_init (void)
4756 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4757 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4759 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4760 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4761 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4762 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4763 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4764 panel_filename_scroll_left_char =
4765 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4766 panel_filename_scroll_right_char =
4767 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4769 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4770 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4771 panel_save_curent_file_to_clip_file, NULL, NULL);
4775 /* --------------------------------------------------------------------------------------------- */
4777 void
4778 panel_deinit (void)
4780 g_free (panel_sort_up_sign);
4781 g_free (panel_sort_down_sign);
4783 g_free (panel_hiddenfiles_sign_show);
4784 g_free (panel_hiddenfiles_sign_hide);
4785 g_free (panel_history_prev_item_sign);
4786 g_free (panel_history_next_item_sign);
4787 g_free (panel_history_show_list_sign);
4788 g_free (panel_filename_scroll_left_char);
4789 g_free (panel_filename_scroll_right_char);
4793 /* --------------------------------------------------------------------------------------------- */
4795 gboolean
4796 do_cd (const vfs_path_t * new_dir_vpath, enum cd_enum exact)
4798 gboolean res;
4799 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
4801 if (current_panel->is_panelized)
4803 size_t new_vpath_len;
4805 new_vpath_len = vfs_path_len (new_dir_vpath);
4806 if (vfs_path_ncmp (new_dir_vpath, panelized_panel.root_vpath, new_vpath_len) == 0)
4807 _new_dir_vpath = panelized_panel.root_vpath;
4810 res = do_panel_cd (current_panel, _new_dir_vpath, exact);
4812 #ifdef HAVE_CHARSET
4813 if (res)
4815 const vfs_path_element_t *path_element;
4817 path_element = vfs_path_get_by_index (current_panel->cwd_vpath, -1);
4818 if (path_element->encoding != NULL)
4819 current_panel->codepage = get_codepage_index (path_element->encoding);
4820 else
4821 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4823 #endif /* HAVE_CHARSET */
4825 return res;
4828 /* --------------------------------------------------------------------------------------------- */