Use g_strndup() instead of g_strdup()
[midnight-commander.git] / src / filemanager / panel.c
bloba1d0224399db05a1f09aa25d061724215912647e
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/filehighlight.h"
48 #include "lib/mcconfig.h"
49 #include "lib/vfs/vfs.h"
50 #include "lib/unixcompat.h"
51 #include "lib/timefmt.h"
52 #include "lib/util.h"
53 #include "lib/widget.h"
54 #ifdef HAVE_CHARSET
55 #include "lib/charsets.h" /* get_codepage_id () */
56 #endif
57 #include "lib/event.h"
59 #include "src/setup.h" /* For loading/saving panel options */
60 #include "src/execute.h"
61 #ifdef HAVE_CHARSET
62 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
63 #endif
64 #include "src/keybind-defaults.h" /* global_keymap_t */
65 #include "src/subshell.h" /* do_subshell_chdir() */
67 #include "dir.h"
68 #include "boxes.h"
69 #include "tree.h"
70 #include "ext.h" /* regexp_command */
71 #include "layout.h" /* Most layout variables are here */
72 #include "cmd.h"
73 #include "command.h" /* cmdline */
74 #include "usermenu.h"
75 #include "midnight.h"
76 #include "mountlist.h" /* my_statfs */
78 #include "panel.h"
80 /*** global variables ****************************************************************************/
82 /* The hook list for the select file function */
83 hook_t *select_file_hook = NULL;
85 /* *INDENT-OFF* */
86 panelized_panel_t panelized_panel = { {NULL, 0}, -1, NULL };
87 /* *INDENT-ON* */
89 static const char *string_file_name (file_entry *, int);
90 static const char *string_file_size (file_entry *, int);
91 static const char *string_file_size_brief (file_entry *, int);
92 static const char *string_file_type (file_entry *, int);
93 static const char *string_file_mtime (file_entry *, int);
94 static const char *string_file_atime (file_entry *, int);
95 static const char *string_file_ctime (file_entry *, int);
96 static const char *string_file_permission (file_entry *, int);
97 static const char *string_file_perm_octal (file_entry *, int);
98 static const char *string_file_nlinks (file_entry *, int);
99 static const char *string_inode (file_entry *, int);
100 static const char *string_file_nuid (file_entry *, int);
101 static const char *string_file_ngid (file_entry *, int);
102 static const char *string_file_owner (file_entry *, int);
103 static const char *string_file_group (file_entry *, int);
104 static const char *string_marked (file_entry *, int);
105 static const char *string_space (file_entry *, int);
106 static const char *string_dot (file_entry *, int);
108 /* *INDENT-OFF* */
109 panel_field_t panel_fields[] = {
111 "unsorted", 12, 1, J_LEFT_FIT,
112 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
113 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
114 N_("sort|u"),
115 N_("&Unsorted"), TRUE, FALSE,
116 string_file_name,
117 (sortfn *) unsorted
121 "name", 12, 1, J_LEFT_FIT,
122 /* TRANSLATORS: one single character to represent 'name' sort mode */
123 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
124 N_("sort|n"),
125 N_("&Name"), TRUE, TRUE,
126 string_file_name,
127 (sortfn *) sort_name
131 "version", 12, 1, J_LEFT_FIT,
132 /* TRANSLATORS: one single character to represent 'version' sort mode */
133 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
134 N_("sort|v"),
135 N_("&Version"), TRUE, FALSE,
136 string_file_name,
137 (sortfn *) sort_vers
141 "extension", 12, 1, J_LEFT_FIT,
142 /* TRANSLATORS: one single character to represent 'extension' sort mode */
143 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
144 N_("sort|e"),
145 N_("&Extension"), TRUE, FALSE,
146 string_file_name, /* TODO: string_file_ext */
147 (sortfn *) sort_ext
151 "size", 7, 0, J_RIGHT,
152 /* TRANSLATORS: one single character to represent 'size' sort mode */
153 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
154 N_("sort|s"),
155 N_("&Size"), TRUE, TRUE,
156 string_file_size,
157 (sortfn *) sort_size
161 "bsize", 7, 0, J_RIGHT,
163 N_("Block Size"), FALSE, FALSE,
164 string_file_size_brief,
165 (sortfn *) sort_size
169 "type", 1, 0, J_LEFT,
171 "", FALSE, TRUE,
172 string_file_type,
173 NULL
177 "mtime", 12, 0, J_RIGHT,
178 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
179 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
180 N_("sort|m"),
181 N_("&Modify time"), TRUE, TRUE,
182 string_file_mtime,
183 (sortfn *) sort_time
187 "atime", 12, 0, J_RIGHT,
188 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
189 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
190 N_("sort|a"),
191 N_("&Access time"), TRUE, TRUE,
192 string_file_atime,
193 (sortfn *) sort_atime
197 "ctime", 12, 0, J_RIGHT,
198 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
199 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
200 N_("sort|h"),
201 N_("C&hange time"), TRUE, TRUE,
202 string_file_ctime,
203 (sortfn *) sort_ctime
207 "perm", 10, 0, J_LEFT,
209 N_("Permission"), FALSE, TRUE,
210 string_file_permission,
211 NULL
215 "mode", 6, 0, J_RIGHT,
217 N_("Perm"), FALSE, TRUE,
218 string_file_perm_octal,
219 NULL
223 "nlink", 2, 0, J_RIGHT,
225 N_("Nl"), FALSE, TRUE,
226 string_file_nlinks, NULL
230 "inode", 5, 0, J_RIGHT,
231 /* TRANSLATORS: one single character to represent 'inode' sort mode */
232 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
233 N_("sort|i"),
234 N_("&Inode"), TRUE, TRUE,
235 string_inode,
236 (sortfn *) sort_inode
240 "nuid", 5, 0, J_RIGHT,
242 N_("UID"), FALSE, FALSE,
243 string_file_nuid,
244 NULL
248 "ngid", 5, 0, J_RIGHT,
250 N_("GID"), FALSE, FALSE,
251 string_file_ngid,
252 NULL
256 "owner", 8, 0, J_LEFT_FIT,
258 N_("Owner"), FALSE, TRUE,
259 string_file_owner,
260 NULL
264 "group", 8, 0, J_LEFT_FIT,
266 N_("Group"), FALSE, TRUE,
267 string_file_group,
268 NULL
272 "mark", 1, 0, J_RIGHT,
274 " ", FALSE, TRUE,
275 string_marked,
276 NULL
280 "|", 1, 0, J_RIGHT,
282 " ", FALSE, TRUE,
283 NULL,
284 NULL
288 "space", 1, 0, J_RIGHT,
290 " ", FALSE, TRUE,
291 string_space,
292 NULL
296 "dot", 1, 0, J_RIGHT,
298 " ", FALSE, FALSE,
299 string_dot,
300 NULL
304 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
307 /* *INDENT-ON* */
309 extern int saving_setup;
311 /*** file scope macro definitions ****************************************************************/
313 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
315 #define NORMAL 0
316 #define SELECTED 1
317 #define MARKED 2
318 #define MARKED_SELECTED 3
319 #define STATUS 5
321 /* This macro extracts the number of available lines in a panel */
322 #define llines(p) (p->widget.lines - 3 - (panels_options.show_mini_info ? 2 : 0))
324 /*** file scope type declarations ****************************************************************/
326 typedef enum
328 MARK_DONT_MOVE = 0,
329 MARK_DOWN = 1,
330 MARK_FORCE_DOWN = 2,
331 MARK_FORCE_UP = 3
332 } mark_act_t;
335 * This describes a format item. The parse_display_format routine parses
336 * the user specified format and creates a linked list of format_e structures.
338 typedef struct format_e
340 struct format_e *next;
341 int requested_field_len;
342 int field_len;
343 align_crt_t just_mode;
344 int expand;
345 const char *(*string_fn) (file_entry *, int len);
346 char *title;
347 const char *id;
348 } format_e;
350 /* File name scroll states */
351 typedef enum
353 FILENAME_NOSCROLL = 1,
354 FILENAME_SCROLL_LEFT = 2,
355 FILENAME_SCROLL_RIGHT = 4
356 } filename_scroll_flag_t;
358 /*** file scope variables ************************************************************************/
360 static char *panel_sort_up_sign = NULL;
361 static char *panel_sort_down_sign = NULL;
363 static char *panel_hiddenfiles_sign_show = NULL;
364 static char *panel_hiddenfiles_sign_hide = NULL;
365 static char *panel_history_prev_item_sign = NULL;
366 static char *panel_history_next_item_sign = NULL;
367 static char *panel_history_show_list_sign = NULL;
368 static char *panel_filename_scroll_left_char = NULL;
369 static char *panel_filename_scroll_right_char = NULL;
371 /* Panel that selection started */
372 static WPanel *mouse_mark_panel = NULL;
374 static int mouse_marking = 0;
375 static int state_mark = 0;
376 /*** file scope functions ************************************************************************/
377 /* --------------------------------------------------------------------------------------------- */
379 static void
380 set_colors (WPanel * panel)
382 (void) panel;
383 tty_set_normal_attrs ();
384 tty_setcolor (NORMAL_COLOR);
387 /* --------------------------------------------------------------------------------------------- */
388 /** Delete format string, it is a linked list */
390 static void
391 delete_format (format_e * format)
393 while (format != NULL)
395 format_e *next = format->next;
396 g_free (format->title);
397 g_free (format);
398 format = next;
402 /* --------------------------------------------------------------------------------------------- */
403 /** This code relies on the default justification!!! */
405 static void
406 add_permission_string (const char *dest, int width, file_entry * fe, int attr, int color,
407 int is_octal)
409 int i, r, l;
411 l = get_user_permissions (&fe->st);
413 if (is_octal)
415 /* Place of the access bit in octal mode */
416 l = width + l - 3;
417 r = l + 1;
419 else
421 /* The same to the triplet in string mode */
422 l = l * 3 + 1;
423 r = l + 3;
426 for (i = 0; i < width; i++)
428 if (i >= l && i < r)
430 if (attr == SELECTED || attr == MARKED_SELECTED)
431 tty_setcolor (MARKED_SELECTED_COLOR);
432 else
433 tty_setcolor (MARKED_COLOR);
435 else if (color >= 0)
436 tty_setcolor (color);
437 else
438 tty_lowlevel_setcolor (-color);
440 tty_print_char (dest[i]);
444 /* --------------------------------------------------------------------------------------------- */
445 /** String representations of various file attributes name */
447 static const char *
448 string_file_name (file_entry * fe, int len)
450 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
452 (void) len;
453 g_strlcpy (buffer, fe->fname, sizeof (buffer));
454 return buffer;
457 /* --------------------------------------------------------------------------------------------- */
459 static unsigned int
460 ilog10 (dev_t n)
462 unsigned int digits = 0;
465 digits++, n /= 10;
467 while (n != 0);
468 return digits;
471 /* --------------------------------------------------------------------------------------------- */
473 static void
474 format_device_number (char *buf, size_t bufsize, dev_t dev)
476 dev_t major_dev = major (dev);
477 dev_t minor_dev = minor (dev);
478 unsigned int major_digits = ilog10 (major_dev);
479 unsigned int minor_digits = ilog10 (minor_dev);
481 g_assert (bufsize >= 1);
482 if (major_digits + 1 + minor_digits + 1 <= bufsize)
484 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
486 else
488 g_strlcpy (buf, _("[dev]"), bufsize);
492 /* --------------------------------------------------------------------------------------------- */
493 /** size */
495 static const char *
496 string_file_size (file_entry * fe, int len)
498 static char buffer[BUF_TINY];
500 /* Don't ever show size of ".." since we don't calculate it */
501 if (!strcmp (fe->fname, ".."))
503 return _("UP--DIR");
506 #ifdef HAVE_STRUCT_STAT_ST_RDEV
507 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
508 format_device_number (buffer, len + 1, fe->st.st_rdev);
509 else
510 #endif
512 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
514 return buffer;
517 /* --------------------------------------------------------------------------------------------- */
518 /** bsize */
520 static const char *
521 string_file_size_brief (file_entry * fe, int len)
523 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
525 return _("SYMLINK");
528 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
530 return _("SUB-DIR");
533 return string_file_size (fe, len);
536 /* --------------------------------------------------------------------------------------------- */
537 /** This functions return a string representation of a file entry type */
539 static const char *
540 string_file_type (file_entry * fe, int len)
542 static char buffer[2];
544 (void) len;
545 if (S_ISDIR (fe->st.st_mode))
546 buffer[0] = PATH_SEP;
547 else if (S_ISLNK (fe->st.st_mode))
549 if (fe->f.link_to_dir)
550 buffer[0] = '~';
551 else if (fe->f.stale_link)
552 buffer[0] = '!';
553 else
554 buffer[0] = '@';
556 else if (S_ISCHR (fe->st.st_mode))
557 buffer[0] = '-';
558 else if (S_ISSOCK (fe->st.st_mode))
559 buffer[0] = '=';
560 else if (S_ISDOOR (fe->st.st_mode))
561 buffer[0] = '>';
562 else if (S_ISBLK (fe->st.st_mode))
563 buffer[0] = '+';
564 else if (S_ISFIFO (fe->st.st_mode))
565 buffer[0] = '|';
566 else if (S_ISNAM (fe->st.st_mode))
567 buffer[0] = '#';
568 else if (!S_ISREG (fe->st.st_mode))
569 buffer[0] = '?'; /* non-regular of unknown kind */
570 else if (is_exe (fe->st.st_mode))
571 buffer[0] = '*';
572 else
573 buffer[0] = ' ';
574 buffer[1] = '\0';
575 return buffer;
578 /* --------------------------------------------------------------------------------------------- */
579 /** mtime */
581 static const char *
582 string_file_mtime (file_entry * fe, int len)
584 (void) len;
585 return file_date (fe->st.st_mtime);
588 /* --------------------------------------------------------------------------------------------- */
589 /** atime */
591 static const char *
592 string_file_atime (file_entry * fe, int len)
594 (void) len;
595 return file_date (fe->st.st_atime);
598 /* --------------------------------------------------------------------------------------------- */
599 /** ctime */
601 static const char *
602 string_file_ctime (file_entry * fe, int len)
604 (void) len;
605 return file_date (fe->st.st_ctime);
608 /* --------------------------------------------------------------------------------------------- */
609 /** perm */
611 static const char *
612 string_file_permission (file_entry * fe, int len)
614 (void) len;
615 return string_perm (fe->st.st_mode);
618 /* --------------------------------------------------------------------------------------------- */
619 /** mode */
621 static const char *
622 string_file_perm_octal (file_entry * fe, int len)
624 static char buffer[10];
626 (void) len;
627 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
628 return buffer;
631 /* --------------------------------------------------------------------------------------------- */
632 /** nlink */
634 static const char *
635 string_file_nlinks (file_entry * fe, int len)
637 static char buffer[BUF_TINY];
639 (void) len;
640 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
641 return buffer;
644 /* --------------------------------------------------------------------------------------------- */
645 /** inode */
647 static const char *
648 string_inode (file_entry * fe, int len)
650 static char buffer[10];
652 (void) len;
653 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
654 return buffer;
657 /* --------------------------------------------------------------------------------------------- */
658 /** nuid */
660 static const char *
661 string_file_nuid (file_entry * fe, int len)
663 static char buffer[10];
665 (void) len;
666 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
667 return buffer;
670 /* --------------------------------------------------------------------------------------------- */
671 /** ngid */
673 static const char *
674 string_file_ngid (file_entry * fe, int len)
676 static char buffer[10];
678 (void) len;
679 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
680 return buffer;
683 /* --------------------------------------------------------------------------------------------- */
684 /** owner */
686 static const char *
687 string_file_owner (file_entry * fe, int len)
689 (void) len;
690 return get_owner (fe->st.st_uid);
693 /* --------------------------------------------------------------------------------------------- */
694 /** group */
696 static const char *
697 string_file_group (file_entry * fe, int len)
699 (void) len;
700 return get_group (fe->st.st_gid);
703 /* --------------------------------------------------------------------------------------------- */
704 /** mark */
706 static const char *
707 string_marked (file_entry * fe, int len)
709 (void) len;
710 return fe->f.marked ? "*" : " ";
713 /* --------------------------------------------------------------------------------------------- */
714 /** space */
716 static const char *
717 string_space (file_entry * fe, int len)
719 (void) fe;
720 (void) len;
721 return " ";
724 /* --------------------------------------------------------------------------------------------- */
725 /** dot */
727 static const char *
728 string_dot (file_entry * fe, int len)
730 (void) fe;
731 (void) len;
732 return ".";
735 /* --------------------------------------------------------------------------------------------- */
737 static int
738 file_compute_color (int attr, file_entry * fe)
740 switch (attr)
742 case SELECTED:
743 return (SELECTED_COLOR);
744 case MARKED:
745 return (MARKED_COLOR);
746 case MARKED_SELECTED:
747 return (MARKED_SELECTED_COLOR);
748 case STATUS:
749 return (NORMAL_COLOR);
750 case NORMAL:
751 default:
752 if (!panels_options.filetype_mode)
753 return (NORMAL_COLOR);
756 return mc_fhl_get_color (mc_filehighlight, fe);
759 /* --------------------------------------------------------------------------------------------- */
760 /** Formats the file number file_index of panel in the buffer dest */
762 static filename_scroll_flag_t
763 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
764 int isstatus, int *field_lenght)
766 int color, length, empty_line;
767 const char *txt;
768 format_e *format, *home;
769 file_entry *fe;
770 filename_scroll_flag_t res = FILENAME_NOSCROLL;
772 (void) dest;
773 (void) limit;
774 length = 0;
775 empty_line = (file_index >= panel->count);
776 home = (isstatus) ? panel->status_format : panel->format;
777 fe = &panel->dir.list[file_index];
778 *field_lenght = 0;
780 if (!empty_line)
781 color = file_compute_color (attr, fe);
782 else
783 color = NORMAL_COLOR;
784 for (format = home; format; format = format->next)
786 if (length == width)
787 break;
789 if (format->string_fn)
791 int len, perm;
792 const char *prepared_text;
793 int name_offset = 0;
795 if (empty_line)
796 txt = " ";
797 else
798 txt = (*format->string_fn) (fe, format->field_len);
800 len = format->field_len;
801 if (len + length > width)
802 len = width - length;
803 if (len <= 0)
804 break;
806 if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
808 int str_len;
809 int i;
811 *field_lenght = len + 1;
813 str_len = str_length (txt);
814 i = max (0, str_len - len);
815 panel->max_shift = max (panel->max_shift, i);
816 i = min (panel->content_shift, i);
818 if (i > -1)
820 name_offset = str_offset_to_pos (txt, i);
821 if (str_len > len)
823 res = FILENAME_SCROLL_LEFT;
824 if (str_length (txt + name_offset) > len)
825 res |= FILENAME_SCROLL_RIGHT;
830 perm = 0;
831 if (panels_options.permission_mode)
833 if (!strcmp (format->id, "perm"))
834 perm = 1;
835 else if (!strcmp (format->id, "mode"))
836 perm = 2;
839 if (color >= 0)
840 tty_setcolor (color);
841 else
842 tty_lowlevel_setcolor (-color);
844 if (!isstatus && panel->content_shift > -1)
845 prepared_text =
846 str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
847 else
848 prepared_text = str_fit_to_term (txt, len, format->just_mode);
850 if (perm)
851 add_permission_string (prepared_text, format->field_len, fe, attr, color, perm - 1);
852 else
853 tty_print_string (prepared_text);
855 length += len;
857 else
859 if (attr == SELECTED || attr == MARKED_SELECTED)
860 tty_setcolor (SELECTED_COLOR);
861 else
862 tty_setcolor (NORMAL_COLOR);
863 tty_print_one_vline (TRUE);
864 length++;
868 if (length < width)
870 int y, x;
872 tty_getyx (&y, &x);
873 tty_draw_hline (y, x, ' ', width - length);
876 return res;
879 /* --------------------------------------------------------------------------------------------- */
881 static void
882 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
884 int second_column = 0;
885 int width;
886 int offset = 0;
887 char buffer[BUF_MEDIUM];
888 filename_scroll_flag_t ret_frm;
889 int ypos = 0;
890 gboolean panel_is_split = !isstatus && panel->split;
891 int fln = 0;
893 width = panel->widget.cols - 2;
895 if (panel_is_split)
897 second_column = (file_index - panel->top_file) / llines (panel);
898 width = width / 2 - 1;
900 if (second_column != 0)
902 offset = 1 + width;
903 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
904 width = panel->widget.cols - offset - 2;
908 /* Nothing to paint */
909 if (width <= 0)
910 return;
912 if (mv)
914 int pos = file_index - panel->top_file;
916 if (panel_is_split)
917 ypos = pos % llines (panel);
918 else
919 ypos = pos;
921 ypos += 2;
922 widget_move (&panel->widget, ypos, offset + 1);
925 ret_frm = format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus, &fln);
927 if (panel_is_split)
929 if (second_column)
930 tty_print_char (' ');
931 else
933 tty_setcolor (NORMAL_COLOR);
934 tty_print_one_vline (TRUE);
937 if (ret_frm != FILENAME_NOSCROLL && mv)
939 if (!panel_is_split && fln > 0)
941 if (panel->list_type == list_long)
943 offset = width - fln + 1;
944 width = fln - 1;
946 else
948 width = fln;
951 widget_move (&panel->widget, ypos, offset);
952 tty_setcolor (NORMAL_COLOR);
953 tty_print_string (panel_filename_scroll_left_char);
955 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
957 widget_move (&panel->widget, ypos, offset + 1 + width);
958 tty_setcolor (NORMAL_COLOR);
959 tty_print_string (panel_filename_scroll_right_char);
964 /* --------------------------------------------------------------------------------------------- */
966 static void
967 display_mini_info (WPanel * panel)
969 if (!panels_options.show_mini_info)
970 return;
972 widget_move (&panel->widget, llines (panel) + 3, 1);
974 if (panel->searching)
976 tty_setcolor (INPUT_COLOR);
977 tty_print_char ('/');
978 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
979 return;
982 /* Status resolves links and show them */
983 set_colors (panel);
985 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
987 char link_target[MC_MAXPATHLEN];
988 vfs_path_t *lc_link_vpath;
989 int len;
991 lc_link_vpath =
992 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
993 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
994 vfs_path_free (lc_link_vpath);
995 if (len > 0)
997 link_target[len] = 0;
998 tty_print_string ("-> ");
999 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
1001 else
1002 tty_print_string (str_fit_to_term (_("<readlink failed>"),
1003 panel->widget.cols - 2, J_LEFT));
1005 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
1007 /* FIXME:
1008 * while loading directory (do_load_dir() and do_reload_dir()),
1009 * the actual stat info about ".." directory isn't got;
1010 * so just don't display incorrect info about ".." directory */
1011 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
1013 else
1014 /* Default behavior */
1015 repaint_file (panel, panel->selected, 0, STATUS, 1);
1018 /* --------------------------------------------------------------------------------------------- */
1020 static void
1021 paint_dir (WPanel * panel)
1023 int i;
1024 int color; /* Color value of the line */
1025 int items; /* Number of items */
1027 items = llines (panel) * (panel->split ? 2 : 1);
1028 /* reset max len of filename because we have the new max length for the new file list */
1029 panel->max_shift = -1;
1030 for (i = 0; i < items; i++)
1032 if (i + panel->top_file >= panel->count)
1033 color = 0;
1034 else
1036 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1037 color += (panel->selected == i + panel->top_file && panel->active);
1039 repaint_file (panel, i + panel->top_file, 1, color, 0);
1042 tty_set_normal_attrs ();
1045 /* --------------------------------------------------------------------------------------------- */
1047 static void
1048 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
1050 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
1051 int cols;
1053 if (panel->marked <= 0)
1054 return;
1056 buf = size_only ? b_bytes : buffer;
1057 cols = panel->widget.cols - 2;
1060 * This is a trick to use two ngettext() calls in one sentence.
1061 * First make "N bytes", then insert it into "X in M files".
1063 g_snprintf (b_bytes, sizeof (b_bytes),
1064 ngettext ("%s byte", "%s bytes", panel->total),
1065 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1066 if (!size_only)
1067 g_snprintf (buffer, sizeof (buffer),
1068 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1069 b_bytes, panel->marked);
1071 /* don't forget spaces around buffer content */
1072 buf = (char *) str_trunc (buf, cols - 4);
1074 if (x < 0)
1075 /* center in panel */
1076 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
1079 * y == llines (panel) + 2 for mini_info_separator
1080 * y == panel->widget.lines - 1 for panel bottom frame
1082 widget_move (&panel->widget, y, x);
1083 tty_setcolor (MARKED_COLOR);
1084 tty_printf (" %s ", buf);
1087 /* --------------------------------------------------------------------------------------------- */
1089 static void
1090 mini_info_separator (WPanel * panel)
1092 if (panels_options.show_mini_info)
1094 const int y = llines (panel) + 2;
1096 tty_setcolor (NORMAL_COLOR);
1097 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1098 ACS_HLINE, panel->widget.cols - 2);
1099 /* Status displays total marked size.
1100 * Centered in panel, full format. */
1101 display_total_marked_size (panel, y, -1, FALSE);
1105 /* --------------------------------------------------------------------------------------------- */
1107 static void
1108 show_free_space (WPanel * panel)
1110 /* Used to figure out how many free space we have */
1111 static struct my_statfs myfs_stats;
1112 /* Old current working directory for displaying free space */
1113 static char *old_cwd = NULL;
1114 char *tmp_path;
1116 /* Don't try to stat non-local fs */
1117 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1118 return;
1120 tmp_path = vfs_path_to_str (panel->cwd_vpath);
1121 if (old_cwd == NULL || strcmp (old_cwd, tmp_path) != 0)
1123 char rpath[PATH_MAX];
1125 init_my_statfs ();
1126 g_free (old_cwd);
1127 old_cwd = tmp_path;
1128 tmp_path = NULL;
1130 if (mc_realpath (old_cwd, rpath) == NULL)
1131 return;
1133 my_statfs (&myfs_stats, rpath);
1135 g_free (tmp_path);
1137 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1139 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1140 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1141 panels_options.kilobyte_si);
1142 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1143 panels_options.kilobyte_si);
1144 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1145 myfs_stats.total == 0 ? 0 :
1146 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1147 widget_move (&panel->widget, panel->widget.lines - 1,
1148 panel->widget.cols - 2 - (int) strlen (tmp));
1149 tty_setcolor (NORMAL_COLOR);
1150 tty_print_string (tmp);
1154 /* --------------------------------------------------------------------------------------------- */
1156 * Prepare path string for showing in panel's header.
1157 * Passwords will removed, also home dir will replaced by ~
1159 * @param panel WPanel object
1161 * @return newly allocated string.
1164 static char *
1165 panel_correct_path_to_show (WPanel * panel)
1167 vfs_path_t *last_vpath;
1168 const vfs_path_element_t *path_element;
1169 char *return_path;
1170 int elements_count;
1172 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1174 /* get last path element */
1175 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1178 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1179 strcmp (path_element->class->name, "extfs") == 0 ||
1180 strcmp (path_element->class->name, "tarfs") == 0))
1182 const char *archive_name;
1183 const vfs_path_element_t *prev_path_element;
1185 /* get previous path element for catching archive name */
1186 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1187 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1188 if (archive_name != NULL)
1190 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1192 else
1194 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1195 last_vpath->relative = TRUE;
1198 else
1200 last_vpath = vfs_path_new ();
1201 last_vpath->relative = TRUE;
1204 vfs_path_add_element (last_vpath, path_element);
1205 return_path =
1206 vfs_path_to_str_flags (last_vpath, 0,
1207 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1208 vfs_path_free (last_vpath);
1210 return return_path;
1213 /* --------------------------------------------------------------------------------------------- */
1215 * Get Current path element encoding
1217 * @param panel WPanel object
1219 * @return newly allocated string or NULL if path charset is same as system charset
1222 #ifdef HAVE_CHARSET
1223 static char *
1224 panel_get_encoding_info_str (WPanel * panel)
1226 char *ret_str = NULL;
1227 const vfs_path_element_t *path_element;
1229 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1230 if (path_element->encoding != NULL)
1231 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1233 return ret_str;
1235 #endif
1237 /* --------------------------------------------------------------------------------------------- */
1239 static void
1240 show_dir (WPanel * panel)
1242 gchar *tmp;
1243 set_colors (panel);
1244 draw_box (panel->widget.owner,
1245 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1247 if (panels_options.show_mini_info)
1249 widget_move (&panel->widget, llines (panel) + 2, 0);
1250 tty_print_alt_char (ACS_LTEE, FALSE);
1251 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1252 tty_print_alt_char (ACS_RTEE, FALSE);
1255 widget_move (&panel->widget, 0, 1);
1256 tty_print_string (panel_history_prev_item_sign);
1258 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1259 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1260 panel_history_next_item_sign);
1262 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1263 tty_print_string (tmp);
1265 g_free (tmp);
1267 widget_move (&panel->widget, 0, 3);
1269 if (panel->is_panelized)
1270 tty_printf (" %s ", _("Panelize"));
1271 #ifdef HAVE_CHARSET
1272 else
1274 tmp = panel_get_encoding_info_str (panel);
1275 if (tmp != NULL)
1277 tty_printf ("%s", tmp);
1278 widget_move (&panel->widget, 0, 3 + strlen (tmp));
1279 g_free (tmp);
1282 #endif
1284 if (panel->active)
1285 tty_setcolor (REVERSE_COLOR);
1287 tmp = panel_correct_path_to_show (panel);
1288 tty_printf (" %s ",
1289 str_term_trim (tmp, min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1290 g_free (tmp);
1292 if (!panels_options.show_mini_info)
1294 if (panel->marked == 0)
1296 /* Show size of curret file in the bottom of panel */
1297 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1299 char buffer[BUF_SMALL];
1301 g_snprintf (buffer, sizeof (buffer), " %s ",
1302 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1303 panels_options.kilobyte_si));
1304 tty_setcolor (NORMAL_COLOR);
1305 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1306 tty_print_string (buffer);
1309 else
1311 /* Show total size of marked files
1312 * In the bottom of panel, display size only. */
1313 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1317 show_free_space (panel);
1319 if (panel->active)
1320 tty_set_normal_attrs ();
1323 /* --------------------------------------------------------------------------------------------- */
1325 /* Returns the number of items in the given panel */
1326 static int
1327 ITEMS (WPanel * p)
1329 if (p->split)
1330 return llines (p) * 2;
1331 else
1332 return llines (p);
1335 /* --------------------------------------------------------------------------------------------- */
1337 static void
1338 adjust_top_file (WPanel * panel)
1340 int items = ITEMS (panel);
1342 if (panel->count <= items)
1344 /* If all files fit, show them all. */
1345 panel->top_file = 0;
1347 else
1349 int i;
1351 /* top_file has to be in the range [selected-items+1, selected] so that
1352 the selected file is visible.
1353 top_file should be in the range [0, count-items] so that there's
1354 no empty space wasted.
1355 Within these ranges, adjust it by as little as possible. */
1357 if (panel->top_file < 0)
1358 panel->top_file = 0;
1360 i = panel->selected - items + 1;
1361 if (panel->top_file < i)
1362 panel->top_file = i;
1364 i = panel->count - items;
1365 if (panel->top_file > i)
1366 panel->top_file = i;
1368 if (panel->top_file > panel->selected)
1369 panel->top_file = panel->selected;
1373 /* --------------------------------------------------------------------------------------------- */
1374 /** add "#enc:encodning" to end of path */
1375 /* if path end width a previous #enc:, only encoding is changed no additional
1376 * #enc: is appended
1377 * retun new string
1380 static char *
1381 panel_save_name (WPanel * panel)
1383 /* If the program is shuting down */
1384 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1385 return g_strdup (panel->panel_name);
1386 else
1387 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1390 /* --------------------------------------------------------------------------------------------- */
1392 static void
1393 directory_history_add (struct WPanel *panel, const vfs_path_t * vpath)
1395 char *tmp;
1397 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1398 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1399 panel->dir_history_current = panel->dir_history;
1402 /* --------------------------------------------------------------------------------------------- */
1404 /* "history_load" event handler */
1405 static gboolean
1406 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1407 gpointer init_data, gpointer data)
1409 WPanel *p = (WPanel *) init_data;
1410 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1412 (void) event_group_name;
1413 (void) event_name;
1415 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1417 if (ev->cfg != NULL)
1418 p->dir_history = history_load (ev->cfg, p->hist_name);
1419 else
1420 p->dir_history = history_get (p->hist_name);
1422 directory_history_add (p, p->cwd_vpath);
1425 return TRUE;
1428 /* --------------------------------------------------------------------------------------------- */
1430 /* "history_save" event handler */
1431 static gboolean
1432 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1433 gpointer init_data, gpointer data)
1435 WPanel *p = (WPanel *) init_data;
1437 (void) event_group_name;
1438 (void) event_name;
1440 if (p->dir_history != NULL)
1442 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1444 history_save (ev->cfg, p->hist_name, p->dir_history);
1447 return TRUE;
1450 /* --------------------------------------------------------------------------------------------- */
1452 static void
1453 panel_destroy (WPanel * p)
1455 size_t i;
1457 if (panels_options.auto_save_setup)
1459 char *name;
1461 name = panel_save_name (p);
1462 panel_save_setup (p, name);
1463 g_free (name);
1466 panel_clean_dir (p);
1468 /* clean history */
1469 if (p->dir_history != NULL)
1471 /* directory history is already saved before this moment */
1472 p->dir_history = g_list_first (p->dir_history);
1473 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1474 g_list_free (p->dir_history);
1476 g_free (p->hist_name);
1478 delete_format (p->format);
1479 delete_format (p->status_format);
1481 g_free (p->user_format);
1482 for (i = 0; i < LIST_TYPES; i++)
1483 g_free (p->user_status_format[i]);
1485 g_free (p->dir.list);
1486 g_free (p->panel_name);
1488 vfs_path_free (p->lwd_vpath);
1489 vfs_path_free (p->cwd_vpath);
1492 /* --------------------------------------------------------------------------------------------- */
1494 static void
1495 panel_format_modified (WPanel * panel)
1497 panel->format_modified = 1;
1500 /* --------------------------------------------------------------------------------------------- */
1502 static void
1503 panel_paint_sort_info (WPanel * panel)
1505 if (*panel->sort_info.sort_field->hotkey != '\0')
1507 const char *sort_sign =
1508 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1509 char *str;
1511 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1512 widget_move (&panel->widget, 1, 1);
1513 tty_print_string (str);
1514 g_free (str);
1518 /* --------------------------------------------------------------------------------------------- */
1520 static gchar *
1521 panel_get_title_without_hotkey (const char *title)
1523 char *translated_title;
1524 char *hkey;
1526 if (title == NULL)
1527 return NULL;
1528 if (title[0] == '\0')
1529 return g_strdup ("");
1531 translated_title = g_strdup (_(title));
1533 hkey = strchr (translated_title, '&');
1534 if ((hkey != NULL) && (hkey[1] != '\0'))
1535 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1537 return translated_title;
1540 /* --------------------------------------------------------------------------------------------- */
1542 static void
1543 paint_frame (WPanel * panel)
1545 int side, width;
1546 GString *format_txt;
1548 adjust_top_file (panel);
1550 widget_erase (&panel->widget);
1551 show_dir (panel);
1553 widget_move (&panel->widget, 1, 1);
1555 for (side = 0; side <= panel->split; side++)
1557 format_e *format;
1559 if (side)
1561 tty_setcolor (NORMAL_COLOR);
1562 tty_print_one_vline (TRUE);
1563 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1565 else if (panel->split)
1566 width = panel->widget.cols / 2 - 3;
1567 else
1568 width = panel->widget.cols - 2;
1570 format_txt = g_string_new ("");
1571 for (format = panel->format; format; format = format->next)
1573 if (format->string_fn)
1575 g_string_set_size (format_txt, 0);
1577 if (panel->list_type == list_long
1578 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1579 g_string_append (format_txt,
1580 panel->sort_info.reverse
1581 ? panel_sort_down_sign : panel_sort_up_sign);
1583 g_string_append (format_txt, format->title);
1584 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1586 g_string_append (format_txt, " [");
1587 g_string_append (format_txt, panel->filter);
1588 g_string_append (format_txt, "]");
1591 tty_setcolor (HEADER_COLOR);
1592 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1593 J_CENTER_LEFT));
1594 width -= format->field_len;
1596 else
1598 tty_setcolor (NORMAL_COLOR);
1599 tty_print_one_vline (TRUE);
1600 width--;
1603 g_string_free (format_txt, TRUE);
1605 if (width > 0)
1607 int y, x;
1609 tty_getyx (&y, &x);
1610 tty_draw_hline (y, x, ' ', width);
1614 if (panel->list_type != list_long)
1615 panel_paint_sort_info (panel);
1618 /* --------------------------------------------------------------------------------------------- */
1620 static const char *
1621 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1623 panel_display_t frame = frame_half;
1624 format = skip_separators (format);
1626 if (!strncmp (format, "full", 4))
1628 frame = frame_full;
1629 format += 4;
1631 else if (!strncmp (format, "half", 4))
1633 frame = frame_half;
1634 format += 4;
1637 if (!isstatus)
1639 panel->frame_size = frame;
1640 panel->split = 0;
1643 /* Now, the optional column specifier */
1644 format = skip_separators (format);
1646 if (*format == '1' || *format == '2')
1648 if (!isstatus)
1649 panel->split = *format == '2';
1650 format++;
1653 if (!isstatus)
1654 panel_update_cols (&(panel->widget), panel->frame_size);
1656 return skip_separators (format);
1659 /* Format is:
1661 all := panel_format? format
1662 panel_format := [full|half] [1|2]
1663 format := one_format_e
1664 | format , one_format_e
1666 one_format_e := just format.id [opt_size]
1667 just := [<=>]
1668 opt_size := : size [opt_expand]
1669 size := [0-9]+
1670 opt_expand := +
1674 /* --------------------------------------------------------------------------------------------- */
1676 static format_e *
1677 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1678 int *res_total_cols)
1680 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1681 int total_cols = 0; /* Used columns by the format */
1682 int set_justify; /* flag: set justification mode? */
1683 align_crt_t justify = J_LEFT; /* Which mode. */
1684 size_t i;
1686 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1688 *error = 0;
1690 if (i18n_timelength == 0)
1692 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1694 for (i = 0; panel_fields[i].id != NULL; i++)
1695 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1696 panel_fields[i].min_size = i18n_timelength;
1700 * This makes sure that the panel and mini status full/half mode
1701 * setting is equal
1703 format = parse_panel_size (panel, format, isstatus);
1705 while (*format)
1706 { /* format can be an empty string */
1707 int found = 0;
1709 darr = g_new0 (format_e, 1);
1711 /* I'm so ugly, don't look at me :-) */
1712 if (!home)
1713 home = old = darr;
1715 old->next = darr;
1716 darr->next = 0;
1717 old = darr;
1719 format = skip_separators (format);
1721 if (strchr ("<=>", *format))
1723 set_justify = 1;
1724 switch (*format)
1726 case '<':
1727 justify = J_LEFT;
1728 break;
1729 case '=':
1730 justify = J_CENTER;
1731 break;
1732 case '>':
1733 default:
1734 justify = J_RIGHT;
1735 break;
1737 format = skip_separators (format + 1);
1739 else
1740 set_justify = 0;
1742 for (i = 0; panel_fields[i].id != NULL; i++)
1744 size_t klen = strlen (panel_fields[i].id);
1746 if (strncmp (format, panel_fields[i].id, klen) != 0)
1747 continue;
1749 format += klen;
1751 darr->requested_field_len = panel_fields[i].min_size;
1752 darr->string_fn = panel_fields[i].string_fn;
1753 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1755 darr->id = panel_fields[i].id;
1756 darr->expand = panel_fields[i].expands;
1757 darr->just_mode = panel_fields[i].default_just;
1759 if (set_justify)
1761 if (IS_FIT (darr->just_mode))
1762 darr->just_mode = MAKE_FIT (justify);
1763 else
1764 darr->just_mode = justify;
1766 found = 1;
1768 format = skip_separators (format);
1770 /* If we have a size specifier */
1771 if (*format == ':')
1773 int req_length;
1775 /* If the size was specified, we don't want
1776 * auto-expansion by default
1778 darr->expand = 0;
1779 format++;
1780 req_length = atoi (format);
1781 darr->requested_field_len = req_length;
1783 format = skip_numbers (format);
1785 /* Now, if they insist on expansion */
1786 if (*format == '+')
1788 darr->expand = 1;
1789 format++;
1794 break;
1796 if (!found)
1798 char *tmp_format = g_strdup (format);
1800 int pos = min (8, strlen (format));
1801 delete_format (home);
1802 tmp_format[pos] = 0;
1803 *error =
1804 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1805 g_free (tmp_format);
1806 return 0;
1808 total_cols += darr->requested_field_len;
1811 *res_total_cols = total_cols;
1812 return home;
1815 /* --------------------------------------------------------------------------------------------- */
1817 static format_e *
1818 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1820 #define MAX_EXPAND 4
1821 int expand_top = 0; /* Max used element in expand */
1822 int usable_columns; /* Usable columns in the panel */
1823 int total_cols = 0;
1824 int i;
1825 format_e *darr, *home;
1827 if (!format)
1828 format = DEFAULT_USER_FORMAT;
1830 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1832 if (*error)
1833 return 0;
1835 panel->dirty = 1;
1837 /* Status needn't to be split */
1838 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1840 : (panel->split + 1))) - (!isstatus
1841 && panel->split);
1843 /* Look for the expandable fields and set field_len based on the requested field len */
1844 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1846 darr->field_len = darr->requested_field_len;
1847 if (darr->expand)
1848 expand_top++;
1851 /* If we used more columns than the available columns, adjust that */
1852 if (total_cols > usable_columns)
1854 int pdif, dif = total_cols - usable_columns;
1856 while (dif)
1858 pdif = dif;
1859 for (darr = home; darr; darr = darr->next)
1861 if (dif && darr->field_len - 1)
1863 darr->field_len--;
1864 dif--;
1868 /* avoid endless loop if num fields > 40 */
1869 if (pdif == dif)
1870 break;
1872 total_cols = usable_columns; /* give up, the rest should be truncated */
1875 /* Expand the available space */
1876 if ((usable_columns > total_cols) && expand_top)
1878 int spaces = (usable_columns - total_cols) / expand_top;
1879 int extra = (usable_columns - total_cols) % expand_top;
1881 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1882 if (darr->expand)
1884 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1885 i++;
1888 return home;
1891 /* --------------------------------------------------------------------------------------------- */
1892 /** Given the panel->view_type returns the format string to be parsed */
1894 static const char *
1895 panel_format (WPanel * panel)
1897 switch (panel->list_type)
1899 case list_long:
1900 return "full perm space nlink space owner space group space size space mtime space name";
1902 case list_brief:
1903 return "half 2 type name";
1905 case list_user:
1906 return panel->user_format;
1908 default:
1909 case list_full:
1910 return "half type name | size | mtime";
1914 /* --------------------------------------------------------------------------------------------- */
1916 static const char *
1917 mini_status_format (WPanel * panel)
1919 if (panel->user_mini_status)
1920 return panel->user_status_format[panel->list_type];
1922 switch (panel->list_type)
1924 case list_long:
1925 return "full perm space nlink space owner space group space size space mtime space name";
1927 case list_brief:
1928 return "half type name space bsize space perm space";
1930 case list_full:
1931 return "half type name";
1933 default:
1934 case list_user:
1935 return panel->user_format;
1939 /* */
1940 /* Panel operation commands */
1941 /* */
1943 /* --------------------------------------------------------------------------------------------- */
1944 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1946 static cb_ret_t
1947 maybe_cd (int move_up_dir)
1949 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1951 if (move_up_dir)
1953 vfs_path_t *up_dir;
1955 up_dir = vfs_path_from_str ("..");
1956 do_cd (up_dir, cd_exact);
1957 vfs_path_free (up_dir);
1958 return MSG_HANDLED;
1961 if (S_ISDIR (selection (current_panel)->st.st_mode)
1962 || link_isdir (selection (current_panel)))
1964 vfs_path_t *vpath;
1966 vpath = vfs_path_from_str (selection (current_panel)->fname);
1967 do_cd (vpath, cd_exact);
1968 vfs_path_free (vpath);
1969 return MSG_HANDLED;
1972 return MSG_NOT_HANDLED;
1975 /* --------------------------------------------------------------------------------------------- */
1977 /* if command line is empty then do 'cd ..' */
1978 static cb_ret_t
1979 force_maybe_cd (void)
1981 if (cmdline->buffer[0] == '\0')
1983 vfs_path_t *up_dir = vfs_path_from_str ("..");
1984 do_cd (up_dir, cd_exact);
1985 vfs_path_free (up_dir);
1986 return MSG_HANDLED;
1988 return MSG_NOT_HANDLED;
1991 /* --------------------------------------------------------------------------------------------- */
1993 static void
1994 unselect_item (WPanel * panel)
1996 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1999 /* --------------------------------------------------------------------------------------------- */
2001 static void
2002 move_down (WPanel * panel)
2004 if (panel->selected + 1 == panel->count)
2005 return;
2007 unselect_item (panel);
2008 panel->selected++;
2009 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
2011 /* Scroll window half screen */
2012 panel->top_file += ITEMS (panel) / 2;
2013 if (panel->top_file > panel->count - ITEMS (panel))
2014 panel->top_file = panel->count - ITEMS (panel);
2015 paint_dir (panel);
2017 select_item (panel);
2020 /* --------------------------------------------------------------------------------------------- */
2022 static void
2023 move_up (WPanel * panel)
2025 if (panel->selected == 0)
2026 return;
2028 unselect_item (panel);
2029 panel->selected--;
2030 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2032 /* Scroll window half screen */
2033 panel->top_file -= ITEMS (panel) / 2;
2034 if (panel->top_file < 0)
2035 panel->top_file = 0;
2036 paint_dir (panel);
2038 select_item (panel);
2041 /* --------------------------------------------------------------------------------------------- */
2042 /** Changes the selection by lines (may be negative) */
2044 static void
2045 move_selection (WPanel * panel, int lines)
2047 int new_pos;
2048 int adjust = 0;
2050 new_pos = panel->selected + lines;
2051 if (new_pos >= panel->count)
2052 new_pos = panel->count - 1;
2054 if (new_pos < 0)
2055 new_pos = 0;
2057 unselect_item (panel);
2058 panel->selected = new_pos;
2060 if (panel->selected - panel->top_file >= ITEMS (panel))
2062 panel->top_file += lines;
2063 adjust = 1;
2066 if (panel->selected - panel->top_file < 0)
2068 panel->top_file += lines;
2069 adjust = 1;
2072 if (adjust)
2074 if (panel->top_file > panel->selected)
2075 panel->top_file = panel->selected;
2076 if (panel->top_file < 0)
2077 panel->top_file = 0;
2078 paint_dir (panel);
2080 select_item (panel);
2083 /* --------------------------------------------------------------------------------------------- */
2085 static cb_ret_t
2086 move_left (WPanel * panel)
2088 if (panel->split)
2090 move_selection (panel, -llines (panel));
2091 return MSG_HANDLED;
2093 else
2094 return maybe_cd (1); /* cd .. */
2097 /* --------------------------------------------------------------------------------------------- */
2099 static cb_ret_t
2100 move_right (WPanel * panel)
2102 if (panel->split)
2104 move_selection (panel, llines (panel));
2105 return MSG_HANDLED;
2107 else
2108 return maybe_cd (0); /* cd (selection) */
2111 /* --------------------------------------------------------------------------------------------- */
2113 static void
2114 prev_page (WPanel * panel)
2116 int items;
2118 if (!panel->selected && !panel->top_file)
2119 return;
2120 unselect_item (panel);
2121 items = ITEMS (panel);
2122 if (panel->top_file < items)
2123 items = panel->top_file;
2124 if (!items)
2125 panel->selected = 0;
2126 else
2127 panel->selected -= items;
2128 panel->top_file -= items;
2130 select_item (panel);
2131 paint_dir (panel);
2134 /* --------------------------------------------------------------------------------------------- */
2136 static void
2137 goto_parent_dir (WPanel * panel)
2139 if (!panel->is_panelized)
2141 vfs_path_t *up_dir;
2143 up_dir = vfs_path_from_str ("..");
2144 do_cd (up_dir, cd_exact);
2145 vfs_path_free (up_dir);
2147 else
2149 char *fname = panel->dir.list[panel->selected].fname;
2150 const char *bname;
2151 vfs_path_t *dname_vpath;
2153 if (g_path_is_absolute (fname))
2154 fname = g_strdup (fname);
2155 else
2157 char *tmp_root;
2159 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2160 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2161 g_free (tmp_root);
2164 bname = x_basename (fname);
2166 if (bname == fname)
2167 dname_vpath = vfs_path_from_str (".");
2168 else
2170 char *dname;
2172 dname = g_strndup (fname, bname - fname);
2173 dname_vpath = vfs_path_from_str (dname);
2174 g_free (dname);
2177 do_cd (dname_vpath, cd_exact);
2178 try_to_select (panel, bname);
2180 vfs_path_free (dname_vpath);
2181 g_free (fname);
2185 /* --------------------------------------------------------------------------------------------- */
2187 static void
2188 next_page (WPanel * panel)
2190 int items;
2192 if (panel->selected == panel->count - 1)
2193 return;
2194 unselect_item (panel);
2195 items = ITEMS (panel);
2196 if (panel->top_file > panel->count - 2 * items)
2197 items = panel->count - items - panel->top_file;
2198 if (panel->top_file + items < 0)
2199 items = -panel->top_file;
2200 if (!items)
2201 panel->selected = panel->count - 1;
2202 else
2203 panel->selected += items;
2204 panel->top_file += items;
2206 select_item (panel);
2207 paint_dir (panel);
2210 /* --------------------------------------------------------------------------------------------- */
2212 static void
2213 goto_child_dir (WPanel * panel)
2215 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2217 vfs_path_t *vpath;
2219 vpath = vfs_path_from_str (selection (panel)->fname);
2220 do_cd (vpath, cd_exact);
2221 vfs_path_free (vpath);
2225 /* --------------------------------------------------------------------------------------------- */
2227 static void
2228 goto_top_file (WPanel * panel)
2230 unselect_item (panel);
2231 panel->selected = panel->top_file;
2232 select_item (panel);
2235 /* --------------------------------------------------------------------------------------------- */
2237 static void
2238 goto_middle_file (WPanel * panel)
2240 unselect_item (panel);
2241 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2242 select_item (panel);
2245 /* --------------------------------------------------------------------------------------------- */
2247 static void
2248 goto_bottom_file (WPanel * panel)
2250 unselect_item (panel);
2251 panel->selected = panel->top_file + ITEMS (panel) - 1;
2252 select_item (panel);
2255 /* --------------------------------------------------------------------------------------------- */
2257 static void
2258 move_home (WPanel * panel)
2260 if (panel->selected == 0)
2261 return;
2263 unselect_item (panel);
2265 if (panels_options.torben_fj_mode)
2267 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2269 if (panel->selected > middle_pos)
2271 goto_middle_file (panel);
2272 return;
2274 if (panel->selected != panel->top_file)
2276 goto_top_file (panel);
2277 return;
2281 panel->top_file = 0;
2282 panel->selected = 0;
2284 paint_dir (panel);
2285 select_item (panel);
2288 /* --------------------------------------------------------------------------------------------- */
2290 static void
2291 move_end (WPanel * panel)
2293 if (panel->selected == panel->count - 1)
2294 return;
2296 unselect_item (panel);
2298 if (panels_options.torben_fj_mode)
2300 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2302 if (panel->selected < middle_pos)
2304 goto_middle_file (panel);
2305 return;
2307 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2309 goto_bottom_file (panel);
2310 return;
2314 panel->selected = panel->count - 1;
2315 paint_dir (panel);
2316 select_item (panel);
2319 /* --------------------------------------------------------------------------------------------- */
2321 static void
2322 do_mark_file (WPanel * panel, mark_act_t do_move)
2324 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2325 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2326 move_down (panel);
2327 else if (do_move == MARK_FORCE_UP)
2328 move_up (panel);
2331 /* --------------------------------------------------------------------------------------------- */
2333 static void
2334 mark_file (WPanel * panel)
2336 do_mark_file (panel, MARK_DOWN);
2339 /* --------------------------------------------------------------------------------------------- */
2341 static void
2342 mark_file_up (WPanel * panel)
2344 do_mark_file (panel, MARK_FORCE_UP);
2347 /* --------------------------------------------------------------------------------------------- */
2349 static void
2350 mark_file_down (WPanel * panel)
2352 do_mark_file (panel, MARK_FORCE_DOWN);
2355 /* --------------------------------------------------------------------------------------------- */
2357 static void
2358 mark_file_right (WPanel * panel)
2360 int lines = llines (panel);
2362 if (state_mark < 0)
2363 state_mark = selection (panel)->f.marked ? 0 : 1;
2365 lines = min (lines, panel->count - panel->selected - 1);
2366 for (; lines != 0; lines--)
2368 do_file_mark (panel, panel->selected, state_mark);
2369 move_down (panel);
2371 do_file_mark (panel, panel->selected, state_mark);
2374 /* --------------------------------------------------------------------------------------------- */
2376 static void
2377 mark_file_left (WPanel * panel)
2379 int lines = llines (panel);
2381 if (state_mark < 0)
2382 state_mark = selection (panel)->f.marked ? 0 : 1;
2384 lines = min (lines, panel->selected + 1);
2385 for (; lines != 0; lines--)
2387 do_file_mark (panel, panel->selected, state_mark);
2388 move_up (panel);
2390 do_file_mark (panel, panel->selected, state_mark);
2393 /* --------------------------------------------------------------------------------------------- */
2394 /** Incremental search of a file name in the panel.
2395 * @param panel instance of WPanel structure
2396 * @param c_code key code
2399 static void
2400 do_search (WPanel * panel, int c_code)
2402 size_t l;
2403 int i, sel;
2404 gboolean wrapped = FALSE;
2405 char *act;
2406 mc_search_t *search;
2407 char *reg_exp, *esc_str;
2408 gboolean is_found = FALSE;
2410 l = strlen (panel->search_buffer);
2411 if (c_code == KEY_BACKSPACE)
2413 if (l != 0)
2415 act = panel->search_buffer + l;
2416 str_prev_noncomb_char (&act, panel->search_buffer);
2417 act[0] = '\0';
2419 panel->search_chpoint = 0;
2421 else
2423 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2425 panel->search_char[panel->search_chpoint] = c_code;
2426 panel->search_chpoint++;
2429 if (panel->search_chpoint > 0)
2431 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2433 case -2:
2434 return;
2435 case -1:
2436 panel->search_chpoint = 0;
2437 return;
2438 default:
2439 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2441 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2442 l += panel->search_chpoint;
2443 *(panel->search_buffer + l) = '\0';
2444 panel->search_chpoint = 0;
2450 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2451 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2452 search = mc_search_new (esc_str, -1);
2453 search->search_type = MC_SEARCH_T_GLOB;
2454 search->is_entire_line = TRUE;
2455 switch (panels_options.qsearch_mode)
2457 case QSEARCH_CASE_SENSITIVE:
2458 search->is_case_sensitive = TRUE;
2459 break;
2460 case QSEARCH_CASE_INSENSITIVE:
2461 search->is_case_sensitive = FALSE;
2462 break;
2463 default:
2464 search->is_case_sensitive = panel->sort_info.case_sensitive;
2465 break;
2467 sel = panel->selected;
2468 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2470 if (i >= panel->count)
2472 i = 0;
2473 if (wrapped)
2474 break;
2475 wrapped = TRUE;
2477 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2479 sel = i;
2480 is_found = TRUE;
2481 break;
2484 if (is_found)
2486 unselect_item (panel);
2487 panel->selected = sel;
2488 select_item (panel);
2489 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2491 else if (c_code != KEY_BACKSPACE)
2493 act = panel->search_buffer + l;
2494 str_prev_noncomb_char (&act, panel->search_buffer);
2495 act[0] = '\0';
2497 mc_search_free (search);
2498 g_free (reg_exp);
2499 g_free (esc_str);
2502 /* --------------------------------------------------------------------------------------------- */
2503 /** Start new search.
2504 * @param panel instance of WPanel structure
2507 static void
2508 start_search (WPanel * panel)
2510 if (panel->searching)
2512 if (panel->selected + 1 == panel->count)
2513 panel->selected = 0;
2514 else
2515 move_down (panel);
2517 /* in case if there was no search string we need to recall
2518 previous string, with which we ended previous searching */
2519 if (panel->search_buffer[0] == '\0')
2520 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2521 sizeof (panel->search_buffer));
2523 do_search (panel, 0);
2525 else
2527 panel->searching = TRUE;
2528 panel->search_buffer[0] = '\0';
2529 panel->search_char[0] = '\0';
2530 panel->search_chpoint = 0;
2531 display_mini_info (panel);
2532 mc_refresh ();
2536 /* --------------------------------------------------------------------------------------------- */
2538 static void
2539 stop_search (WPanel * panel)
2541 panel->searching = FALSE;
2543 /* if user had overrdied search string, we need to store it
2544 to the previous_search_buffer */
2545 if (panel->search_buffer[0] != '\0')
2546 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2547 sizeof (panel->prev_search_buffer));
2549 display_mini_info (panel);
2552 /* --------------------------------------------------------------------------------------------- */
2553 /** Return 1 if the Enter key has been processed, 0 otherwise */
2555 static int
2556 do_enter_on_file_entry (file_entry * fe)
2558 vfs_path_t *full_name_vpath;
2559 gboolean ok;
2562 * Directory or link to directory - change directory.
2563 * Try the same for the entries on which mc_lstat() has failed.
2565 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2567 vfs_path_t *fname_vpath;
2569 fname_vpath = vfs_path_from_str (fe->fname);
2570 if (!do_cd (fname_vpath, cd_exact))
2571 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2572 vfs_path_free (fname_vpath);
2573 return 1;
2576 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2578 /* Try associated command */
2579 if (regex_command (full_name_vpath, "Open") != 0)
2581 vfs_path_free (full_name_vpath);
2582 return 1;
2585 /* Check if the file is executable */
2586 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2587 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2588 vfs_path_free (full_name_vpath);
2589 if (!ok)
2590 return 0;
2592 if (confirm_execute)
2594 if (query_dialog
2595 (_("The Midnight Commander"),
2596 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2597 return 1;
2600 if (!vfs_current_is_local ())
2602 int ret;
2603 vfs_path_t *tmp_vpath;
2605 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2606 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2607 vfs_path_free (tmp_vpath);
2608 /* We took action only if the dialog was shown or the execution
2609 * was successful */
2610 return confirm_execute || (ret == 0);
2614 char *tmp = name_quote (fe->fname, 0);
2615 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2616 g_free (tmp);
2617 shell_execute (cmd, 0);
2618 g_free (cmd);
2621 #ifdef HAVE_CHARSET
2622 mc_global.source_codepage = default_source_codepage;
2623 #endif
2625 return 1;
2628 /* --------------------------------------------------------------------------------------------- */
2630 static int
2631 do_enter (WPanel * panel)
2633 return do_enter_on_file_entry (selection (panel));
2636 /* --------------------------------------------------------------------------------------------- */
2638 static void
2639 chdir_other_panel (WPanel * panel)
2641 const file_entry *entry = &panel->dir.list[panel->selected];
2643 vfs_path_t *new_dir_vpath;
2644 char *sel_entry = NULL;
2646 if (get_other_type () != view_listing)
2648 set_display_type (get_other_index (), view_listing);
2651 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2652 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2653 else
2655 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2656 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2659 change_panel ();
2660 do_cd (new_dir_vpath, cd_exact);
2661 vfs_path_free (new_dir_vpath);
2663 if (sel_entry)
2664 try_to_select (current_panel, sel_entry);
2665 change_panel ();
2667 move_down (panel);
2670 /* --------------------------------------------------------------------------------------------- */
2672 * Make the current directory of the current panel also the current
2673 * directory of the other panel. Put the other panel to the listing
2674 * mode if needed. If the current panel is panelized, the other panel
2675 * doesn't become panelized.
2678 static void
2679 panel_sync_other (const WPanel * panel)
2681 if (get_other_type () != view_listing)
2683 set_display_type (get_other_index (), view_listing);
2686 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2688 /* try to select current filename on the other panel */
2689 if (!panel->is_panelized)
2691 try_to_select (other_panel, selection (panel)->fname);
2695 /* --------------------------------------------------------------------------------------------- */
2697 static void
2698 chdir_to_readlink (WPanel * panel)
2700 vfs_path_t *new_dir_vpath;
2701 char buffer[MC_MAXPATHLEN], *p;
2702 int i;
2703 struct stat st;
2704 vfs_path_t *panel_fname_vpath;
2705 gboolean ok;
2707 if (get_other_type () != view_listing)
2708 return;
2710 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2711 return;
2713 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2714 if (i < 0)
2715 return;
2717 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2718 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2719 vfs_path_free (panel_fname_vpath);
2720 if (!ok)
2721 return;
2723 buffer[i] = 0;
2724 if (!S_ISDIR (st.st_mode))
2726 p = strrchr (buffer, PATH_SEP);
2727 if (p && !p[1])
2729 *p = 0;
2730 p = strrchr (buffer, PATH_SEP);
2732 if (!p)
2733 return;
2734 p[1] = 0;
2736 if (*buffer == PATH_SEP)
2737 new_dir_vpath = vfs_path_from_str (buffer);
2738 else
2739 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2741 change_panel ();
2742 do_cd (new_dir_vpath, cd_exact);
2743 vfs_path_free (new_dir_vpath);
2744 change_panel ();
2746 move_down (panel);
2749 /* --------------------------------------------------------------------------------------------- */
2751 static gsize
2752 panel_get_format_field_count (WPanel * panel)
2754 format_e *format;
2755 gsize lc_index;
2756 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2757 return lc_index;
2760 /* --------------------------------------------------------------------------------------------- */
2762 function return 0 if not found and REAL_INDEX+1 if found
2765 static gsize
2766 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2768 format_e *format;
2769 gsize lc_index;
2771 for (lc_index = 1, format = panel->format;
2772 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2773 if (format == NULL)
2774 lc_index = 0;
2776 return lc_index;
2779 /* --------------------------------------------------------------------------------------------- */
2781 static format_e *
2782 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2784 format_e *format;
2785 for (format = panel->format;
2786 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2787 return format;
2790 /* --------------------------------------------------------------------------------------------- */
2792 static const panel_field_t *
2793 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2795 const panel_field_t *pfield;
2796 format_e *format;
2798 format = panel_get_format_field_by_index (panel, lc_index);
2799 if (format == NULL)
2800 return NULL;
2801 pfield = panel_get_field_by_title (format->title);
2802 if (pfield == NULL)
2803 return NULL;
2804 if (pfield->sort_routine == NULL)
2805 return NULL;
2806 return pfield;
2809 /* --------------------------------------------------------------------------------------------- */
2811 static void
2812 panel_toggle_sort_order_prev (WPanel * panel)
2814 gsize lc_index, i;
2815 gchar *title;
2817 const panel_field_t *pfield = NULL;
2819 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2820 lc_index = panel_get_format_field_index_by_name (panel, title);
2821 g_free (title);
2823 if (lc_index > 1)
2825 /* search for prev sortable column in panel format */
2826 for (i = lc_index - 1;
2827 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2830 if (pfield == NULL)
2832 /* Sortable field not found. Try to search in each array */
2833 for (i = panel_get_format_field_count (panel);
2834 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2837 if (pfield != NULL)
2839 panel->sort_info.sort_field = pfield;
2840 panel_set_sort_order (panel, pfield);
2844 /* --------------------------------------------------------------------------------------------- */
2846 static void
2847 panel_toggle_sort_order_next (WPanel * panel)
2849 gsize lc_index, i;
2850 const panel_field_t *pfield = NULL;
2851 gsize format_field_count;
2852 gchar *title;
2854 format_field_count = panel_get_format_field_count (panel);
2855 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2856 lc_index = panel_get_format_field_index_by_name (panel, title);
2857 g_free (title);
2859 if (lc_index != 0 && lc_index != format_field_count)
2861 /* search for prev sortable column in panel format */
2862 for (i = lc_index;
2863 i != format_field_count
2864 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2867 if (pfield == NULL)
2869 /* Sortable field not found. Try to search in each array */
2870 for (i = 0;
2871 i != format_field_count
2872 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2875 if (pfield != NULL)
2877 panel->sort_info.sort_field = pfield;
2878 panel_set_sort_order (panel, pfield);
2882 /* --------------------------------------------------------------------------------------------- */
2884 static void
2885 panel_select_sort_order (WPanel * panel)
2887 const panel_field_t *sort_order;
2889 sort_order = sort_box (&panel->sort_info);
2890 if (sort_order != NULL)
2892 panel->sort_info.sort_field = sort_order;
2893 panel_set_sort_order (panel, sort_order);
2897 /* --------------------------------------------------------------------------------------------- */
2900 * panel_content_scroll_left:
2901 * @param panel the pointer to the panel on which we operate
2903 * scroll long filename to the left (decrement scroll pointer)
2907 static void
2908 panel_content_scroll_left (WPanel * panel)
2910 if (panel->content_shift > -1)
2912 if (panel->content_shift > panel->max_shift)
2913 panel->content_shift = panel->max_shift;
2915 panel->content_shift--;
2916 show_dir (panel);
2917 paint_dir (panel);
2922 /* --------------------------------------------------------------------------------------------- */
2925 * panel_content_scroll_right:
2926 * @param panel the pointer to the panel on which we operate
2928 * scroll long filename to the right (increment scroll pointer)
2932 static void
2933 panel_content_scroll_right (WPanel * panel)
2935 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
2937 panel->content_shift++;
2938 show_dir (panel);
2939 paint_dir (panel);
2943 /* --------------------------------------------------------------------------------------------- */
2945 static void
2946 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2948 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2950 const panel_field_t *sort_order;
2952 sort_order = panel_get_field_by_id (name);
2953 if (sort_order == NULL)
2954 return;
2955 panel->sort_info.sort_field = sort_order;
2957 else
2958 panel->sort_info.reverse = !panel->sort_info.reverse;
2960 panel_set_sort_order (panel, panel->sort_info.sort_field);
2963 /* --------------------------------------------------------------------------------------------- */
2965 * If we moved to the parent directory move the selection pointer to
2966 * the old directory name; If we leave VFS dir, remove FS specificator.
2968 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2971 static const char *
2972 get_parent_dir_name (const char *cwd, const char *lwd)
2974 size_t llen, clen;
2975 const char *p;
2977 llen = strlen (lwd);
2978 clen = strlen (cwd);
2980 if (llen <= clen)
2981 return NULL;
2983 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2985 if (p == NULL)
2987 p = strrchr (lwd, PATH_SEP);
2989 if ((p != NULL)
2990 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2991 && (clen == (size_t) (p - lwd)
2992 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2993 return (p + 1);
2995 return NULL;
2998 /* skip VFS prefix */
2999 while (--p > lwd && *p != PATH_SEP)
3001 /* get last component */
3002 while (--p > lwd && *p != PATH_SEP)
3005 /* return last component */
3006 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
3009 /* --------------------------------------------------------------------------------------------- */
3010 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3012 static void
3013 subshell_chdir (const vfs_path_t * vpath)
3015 #ifdef HAVE_SUBSHELL_SUPPORT
3016 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3017 do_subshell_chdir (vpath, FALSE, TRUE);
3018 #else /* HAVE_SUBSHELL_SUPPORT */
3019 (void) vpath;
3020 #endif /* HAVE_SUBSHELL_SUPPORT */
3023 /* --------------------------------------------------------------------------------------------- */
3025 * Changes the current directory of the panel.
3026 * Don't record change in the directory history.
3029 static gboolean
3030 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3032 char *olddir;
3034 olddir = vfs_path_to_str (panel->cwd_vpath);
3036 /* Convert *new_path to a suitable pathname, handle ~user */
3037 if (cd_type == cd_parse_command)
3039 const vfs_path_element_t *element;
3041 element = vfs_path_get_by_index (new_dir_vpath, 0);
3042 if (strcmp (element->path, "-") == 0)
3043 new_dir_vpath = panel->lwd_vpath;
3046 if (mc_chdir (new_dir_vpath) == -1)
3048 panel_set_cwd (panel, olddir);
3049 g_free (olddir);
3050 return FALSE;
3053 /* Success: save previous directory, shutdown status of previous dir */
3054 panel_set_lwd (panel, olddir);
3055 input_free_completions (cmdline);
3057 vfs_path_free (panel->cwd_vpath);
3058 vfs_setup_cwd ();
3059 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3061 vfs_release_path (olddir);
3063 subshell_chdir (panel->cwd_vpath);
3065 /* Reload current panel */
3066 panel_clean_dir (panel);
3069 char *tmp_path;
3071 panel->count =
3072 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
3073 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3074 panel->sort_info.exec_first, panel->filter);
3075 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3076 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
3077 g_free (tmp_path);
3080 load_hint (0);
3081 panel->dirty = 1;
3082 update_xterm_title_path ();
3084 g_free (olddir);
3086 return TRUE;
3089 /* --------------------------------------------------------------------------------------------- */
3091 static void
3092 directory_history_next (WPanel * panel)
3094 gboolean ok;
3098 GList *next;
3100 ok = TRUE;
3101 next = g_list_next (panel->dir_history_current);
3102 if (next != NULL)
3104 vfs_path_t *data_vpath;
3106 data_vpath = vfs_path_from_str ((char *) next->data);
3107 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3108 vfs_path_free (data_vpath);
3109 panel->dir_history_current = next;
3111 /* skip directories that present in history but absent in file system */
3113 while (!ok);
3116 /* --------------------------------------------------------------------------------------------- */
3118 static void
3119 directory_history_prev (WPanel * panel)
3121 gboolean ok;
3125 GList *prev;
3127 ok = TRUE;
3128 prev = g_list_previous (panel->dir_history_current);
3129 if (prev != NULL)
3131 vfs_path_t *data_vpath;
3133 data_vpath = vfs_path_from_str ((char *) prev->data);
3134 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3135 vfs_path_free (data_vpath);
3136 panel->dir_history_current = prev;
3138 /* skip directories that present in history but absent in file system */
3140 while (!ok);
3143 /* --------------------------------------------------------------------------------------------- */
3145 static void
3146 directory_history_list (WPanel * panel)
3148 char *s;
3149 gboolean ok = FALSE;
3150 size_t pos;
3152 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3154 s = history_show (&panel->dir_history, &panel->widget, pos);
3155 if (s != NULL)
3157 vfs_path_t *s_vpath;
3159 s_vpath = vfs_path_from_str (s);
3160 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3161 if (ok)
3162 directory_history_add (panel, panel->cwd_vpath);
3163 else
3164 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3165 vfs_path_free (s_vpath);
3166 g_free (s);
3169 if (!ok)
3171 /* Since history is fully modified in history_show(), panel->dir_history actually
3172 * points to the invalid place. Try restore current postition here. */
3174 size_t i;
3176 panel->dir_history_current = panel->dir_history;
3178 for (i = 0; i <= pos; i++)
3180 GList *prev;
3182 prev = g_list_previous (panel->dir_history_current);
3183 if (prev == NULL)
3184 break;
3186 panel->dir_history_current = prev;
3191 /* --------------------------------------------------------------------------------------------- */
3193 static cb_ret_t
3194 panel_execute_cmd (WPanel * panel, unsigned long command)
3196 int res = MSG_HANDLED;
3198 if (command != CK_Search)
3199 stop_search (panel);
3202 switch (command)
3204 case CK_Up:
3205 case CK_Down:
3206 case CK_Left:
3207 case CK_Right:
3208 case CK_Bottom:
3209 case CK_Top:
3210 case CK_PageDown:
3211 case CK_PageUp:
3212 /* reset state of marks flag */
3213 state_mark = -1;
3214 break;
3216 switch (command)
3218 case CK_PanelOtherCd:
3219 chdir_other_panel (panel);
3220 break;
3221 case CK_PanelOtherCdLink:
3222 chdir_to_readlink (panel);
3223 break;
3224 case CK_CopySingle:
3225 copy_cmd_local ();
3226 break;
3227 case CK_DeleteSingle:
3228 delete_cmd_local ();
3229 break;
3230 case CK_Enter:
3231 do_enter (panel);
3232 break;
3233 case CK_ViewRaw:
3234 view_raw_cmd ();
3235 break;
3236 case CK_EditNew:
3237 edit_cmd_new ();
3238 break;
3239 case CK_MoveSingle:
3240 rename_cmd_local ();
3241 break;
3242 case CK_SelectInvert:
3243 select_invert_cmd ();
3244 break;
3245 case CK_Select:
3246 select_cmd ();
3247 break;
3248 case CK_Unselect:
3249 unselect_cmd ();
3250 break;
3251 case CK_PageDown:
3252 next_page (panel);
3253 break;
3254 case CK_PageUp:
3255 prev_page (panel);
3256 break;
3257 case CK_CdChild:
3258 goto_child_dir (panel);
3259 break;
3260 case CK_CdParent:
3261 goto_parent_dir (panel);
3262 break;
3263 case CK_History:
3264 directory_history_list (panel);
3265 break;
3266 case CK_HistoryNext:
3267 directory_history_next (panel);
3268 break;
3269 case CK_HistoryPrev:
3270 directory_history_prev (panel);
3271 break;
3272 case CK_BottomOnScreen:
3273 goto_bottom_file (panel);
3274 break;
3275 case CK_MiddleOnScreen:
3276 goto_middle_file (panel);
3277 break;
3278 case CK_TopOnScreen:
3279 goto_top_file (panel);
3280 break;
3281 case CK_Mark:
3282 mark_file (panel);
3283 break;
3284 case CK_MarkUp:
3285 mark_file_up (panel);
3286 break;
3287 case CK_MarkDown:
3288 mark_file_down (panel);
3289 break;
3290 case CK_MarkLeft:
3291 mark_file_left (panel);
3292 break;
3293 case CK_MarkRight:
3294 mark_file_right (panel);
3295 break;
3296 case CK_CdParentSmart:
3297 res = force_maybe_cd ();
3298 break;
3299 case CK_Up:
3300 move_up (panel);
3301 break;
3302 case CK_Down:
3303 move_down (panel);
3304 break;
3305 case CK_Left:
3306 res = move_left (panel);
3307 break;
3308 case CK_Right:
3309 res = move_right (panel);
3310 break;
3311 case CK_Bottom:
3312 move_end (panel);
3313 break;
3314 case CK_Top:
3315 move_home (panel);
3316 break;
3317 #ifdef HAVE_CHARSET
3318 case CK_SelectCodepage:
3319 panel_change_encoding (panel);
3320 break;
3321 #endif
3322 case CK_ScrollLeft:
3323 panel_content_scroll_left (panel);
3324 break;
3325 case CK_ScrollRight:
3326 panel_content_scroll_right (panel);
3327 break;
3328 case CK_Search:
3329 start_search (panel);
3330 break;
3331 case CK_SearchStop:
3332 break;
3333 case CK_PanelOtherSync:
3334 panel_sync_other (panel);
3335 break;
3336 case CK_Sort:
3337 panel_select_sort_order (panel);
3338 break;
3339 case CK_SortPrev:
3340 panel_toggle_sort_order_prev (panel);
3341 break;
3342 case CK_SortNext:
3343 panel_toggle_sort_order_next (panel);
3344 break;
3345 case CK_SortReverse:
3346 panel->sort_info.reverse = !panel->sort_info.reverse;
3347 panel_set_sort_order (panel, panel->sort_info.sort_field);
3348 break;
3349 case CK_SortByName:
3350 panel_set_sort_type_by_id (panel, "name");
3351 break;
3352 case CK_SortByExt:
3353 panel_set_sort_type_by_id (panel, "extension");
3354 break;
3355 case CK_SortBySize:
3356 panel_set_sort_type_by_id (panel, "size");
3357 break;
3358 case CK_SortByMTime:
3359 panel_set_sort_type_by_id (panel, "mtime");
3360 break;
3362 return res;
3365 /* --------------------------------------------------------------------------------------------- */
3367 static cb_ret_t
3368 panel_key (WPanel * panel, int key)
3370 size_t i;
3372 if (is_abort_char (key))
3374 stop_search (panel);
3375 return MSG_HANDLED;
3378 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3380 do_search (panel, key);
3381 return MSG_HANDLED;
3384 for (i = 0; panel_map[i].key != 0; i++)
3385 if (key == panel_map[i].key)
3386 return panel_execute_cmd (panel, panel_map[i].command);
3388 if (panels_options.torben_fj_mode && key == ALT ('h'))
3390 goto_middle_file (panel);
3391 return MSG_HANDLED;
3394 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3396 start_search (panel);
3397 do_search (panel, key);
3398 return MSG_HANDLED;
3401 return MSG_NOT_HANDLED;
3404 /* --------------------------------------------------------------------------------------------- */
3406 static cb_ret_t
3407 panel_callback (Widget * w, widget_msg_t msg, int parm)
3409 WPanel *panel = (WPanel *) w;
3410 WButtonBar *bb;
3412 switch (msg)
3414 case WIDGET_INIT:
3415 /* subscribe to "history_load" event */
3416 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3417 /* subscribe to "history_save" event */
3418 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3419 return MSG_HANDLED;
3421 case WIDGET_DRAW:
3422 /* Repaint everything, including frame and separator */
3423 paint_frame (panel); /* including show_dir */
3424 paint_dir (panel);
3425 mini_info_separator (panel);
3426 display_mini_info (panel);
3427 panel->dirty = 0;
3428 return MSG_HANDLED;
3430 case WIDGET_FOCUS:
3431 state_mark = -1;
3432 current_panel = panel;
3433 panel->active = 1;
3434 if (mc_chdir (panel->cwd_vpath) != 0)
3436 char *cwd;
3438 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3439 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3440 cwd, unix_error_string (errno));
3441 g_free (cwd);
3443 else
3444 subshell_chdir (panel->cwd_vpath);
3446 update_xterm_title_path ();
3447 select_item (panel);
3448 show_dir (panel);
3449 paint_dir (panel);
3450 panel->dirty = 0;
3452 bb = find_buttonbar (panel->widget.owner);
3453 midnight_set_buttonbar (bb);
3454 buttonbar_redraw (bb);
3455 return MSG_HANDLED;
3457 case WIDGET_UNFOCUS:
3458 /* Janne: look at this for the multiple panel options */
3459 stop_search (panel);
3460 panel->active = 0;
3461 show_dir (panel);
3462 unselect_item (panel);
3463 return MSG_HANDLED;
3465 case WIDGET_KEY:
3466 return panel_key (panel, parm);
3468 case WIDGET_COMMAND:
3469 return panel_execute_cmd (panel, parm);
3471 case WIDGET_DESTROY:
3472 /* unsubscribe from "history_load" event */
3473 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3474 /* unsubscribe from "history_save" event */
3475 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3476 panel_destroy (panel);
3477 free_my_statfs ();
3478 return MSG_HANDLED;
3480 default:
3481 return default_proc (msg, parm);
3485 /* --------------------------------------------------------------------------------------------- */
3486 /* */
3487 /* Panel mouse events support routines */
3488 /* */
3490 static void
3491 mouse_toggle_mark (WPanel * panel)
3493 do_mark_file (panel, MARK_DONT_MOVE);
3494 mouse_marking = selection (panel)->f.marked;
3495 mouse_mark_panel = current_panel;
3498 /* --------------------------------------------------------------------------------------------- */
3500 static void
3501 mouse_set_mark (WPanel * panel)
3504 if (mouse_mark_panel == panel)
3506 if (mouse_marking && !(selection (panel)->f.marked))
3507 do_mark_file (panel, MARK_DONT_MOVE);
3508 else if (!mouse_marking && (selection (panel)->f.marked))
3509 do_mark_file (panel, MARK_DONT_MOVE);
3513 /* --------------------------------------------------------------------------------------------- */
3515 static gboolean
3516 mark_if_marking (WPanel * panel, Gpm_Event * event)
3518 if ((event->buttons & GPM_B_RIGHT) != 0)
3520 if ((event->type & GPM_DOWN) != 0)
3521 mouse_toggle_mark (panel);
3522 else
3523 mouse_set_mark (panel);
3524 return TRUE;
3526 return FALSE;
3529 /* --------------------------------------------------------------------------------------------- */
3530 /** Determine which column was clicked, and sort the panel on
3531 * that column, or reverse sort on that column if already
3532 * sorted on that column.
3535 static void
3536 mouse_sort_col (WPanel * panel, int x)
3538 int i;
3539 const char *lc_sort_name = NULL;
3540 panel_field_t *col_sort_format = NULL;
3541 format_e *format;
3542 gchar *title;
3544 for (i = 0, format = panel->format; format != NULL; format = format->next)
3546 i += format->field_len;
3547 if (x < i + 1)
3549 /* found column */
3550 lc_sort_name = format->title;
3551 break;
3555 if (lc_sort_name == NULL)
3556 return;
3558 for (i = 0; panel_fields[i].id != NULL; i++)
3560 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3561 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3563 col_sort_format = &panel_fields[i];
3564 g_free (title);
3565 break;
3567 g_free (title);
3570 if (col_sort_format == NULL)
3571 return;
3573 if (panel->sort_info.sort_field == col_sort_format)
3575 /* reverse the sort if clicked column is already the sorted column */
3576 panel->sort_info.reverse = !panel->sort_info.reverse;
3578 else
3580 /* new sort is forced to be ascending */
3581 panel->sort_info.reverse = FALSE;
3583 panel_set_sort_order (panel, col_sort_format);
3587 /* --------------------------------------------------------------------------------------------- */
3589 * Mouse callback of the panel minus repainting.
3591 static int
3592 panel_event (Gpm_Event * event, void *data)
3594 WPanel *panel = (WPanel *) data;
3595 Widget *w = (Widget *) data;
3597 const int lines = llines (panel);
3598 const gboolean is_active = dlg_widget_active (panel);
3599 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3600 Gpm_Event local;
3602 if (!mouse_global_in_widget (event, (Widget *) data))
3603 return MOU_UNHANDLED;
3605 local = mouse_get_local (event, w);
3607 /* 1st line */
3608 if (local.y == 1)
3610 /* "<" button */
3611 if (mouse_down && local.x == 2)
3613 directory_history_prev (panel);
3614 goto finish;
3617 /* ">" button */
3618 if (mouse_down && local.x == w->cols - 1)
3620 directory_history_next (panel);
3621 goto finish;
3624 /* "^" button */
3625 if (mouse_down && local.x >= w->cols - 4 && local.x <= w->cols - 2)
3627 directory_history_list (panel);
3628 goto finish;
3631 /* "." button show/hide hidden files */
3632 if (mouse_down && local.x == w->cols - 5)
3634 midnight_dlg->callback (midnight_dlg, NULL, DLG_ACTION, CK_ShowHidden, NULL);
3635 goto finish;
3638 /* no other events on 1st line */
3639 return MOU_UNHANDLED;
3642 /* sort on clicked column; don't handle wheel events */
3643 if (mouse_down && (local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && local.y == 2)
3645 mouse_sort_col (panel, local.x);
3646 goto finish;
3649 /* Mouse wheel events */
3650 if (mouse_down && (local.buttons & GPM_B_UP) != 0)
3652 if (is_active)
3654 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3655 prev_page (panel);
3656 else /* We are in first page */
3657 move_up (panel);
3659 goto finish;
3662 if (mouse_down && (local.buttons & GPM_B_DOWN) != 0)
3664 if (is_active)
3666 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3667 next_page (panel);
3668 else /* We are in last page */
3669 move_down (panel);
3671 goto finish;
3674 local.y -= 2;
3675 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
3677 int my_index;
3679 if (!is_active)
3680 change_panel ();
3682 if (panel->top_file + local.y > panel->count)
3683 my_index = panel->count - 1;
3684 else
3686 my_index = panel->top_file + local.y - 1;
3687 if (panel->split && (local.x > (w->cols - 2) / 2))
3688 my_index += llines (panel);
3690 if (my_index >= panel->count)
3691 my_index = panel->count - 1;
3694 if (my_index != panel->selected)
3696 unselect_item (panel);
3697 panel->selected = my_index;
3698 select_item (panel);
3701 /* This one is new */
3702 mark_if_marking (panel, &local);
3704 else if ((local.type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE) &&
3705 local.y > 0 && local.y <= lines)
3706 do_enter (panel);
3708 finish:
3709 if (panel->dirty)
3710 send_message (w, WIDGET_DRAW, 0);
3711 return MOU_NORMAL;
3714 /* --------------------------------------------------------------------------------------------- */
3716 static void
3717 reload_panelized (WPanel * panel)
3719 int i, j;
3720 dir_list *list = &panel->dir;
3722 if (panel != current_panel)
3723 (void) mc_chdir (panel->cwd_vpath);
3725 for (i = 0, j = 0; i < panel->count; i++)
3727 vfs_path_t *vpath;
3729 if (list->list[i].f.marked)
3731 /* Unmark the file in advance. In case the following mc_lstat
3732 * fails we are done, else we have to mark the file again
3733 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3734 * IMO that's the best way to update the panel's summary status
3735 * -- Norbert
3737 do_file_mark (panel, i, 0);
3739 vpath = vfs_path_from_str (list->list[i].fname);
3740 if (mc_lstat (vpath, &list->list[i].st))
3741 g_free (list->list[i].fname);
3742 else
3744 if (list->list[i].f.marked)
3745 do_file_mark (panel, i, 1);
3746 if (j != i)
3747 list->list[j] = list->list[i];
3748 j++;
3750 vfs_path_free (vpath);
3752 if (j == 0)
3753 panel->count = set_zero_dir (list) ? 1 : 0;
3754 else
3755 panel->count = j;
3757 if (panel != current_panel)
3758 (void) mc_chdir (current_panel->cwd_vpath);
3761 /* --------------------------------------------------------------------------------------------- */
3763 static void
3764 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3766 gboolean free_pointer;
3767 char *my_current_file = NULL;
3769 if ((flags & UP_RELOAD) != 0)
3771 panel->is_panelized = FALSE;
3772 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3773 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3776 /* If current_file == -1 (an invalid pointer) then preserve selection */
3777 free_pointer = current_file == UP_KEEPSEL;
3779 if (free_pointer)
3781 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3782 current_file = my_current_file;
3785 if (panel->is_panelized)
3786 reload_panelized (panel);
3787 else
3788 panel_reload (panel);
3790 try_to_select (panel, current_file);
3791 panel->dirty = 1;
3793 if (free_pointer)
3794 g_free (my_current_file);
3797 /* --------------------------------------------------------------------------------------------- */
3799 static void
3800 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3802 if (get_display_type (which) == view_listing)
3804 WPanel *panel;
3806 panel = (WPanel *) get_panel_widget (which);
3807 if (panel->is_panelized)
3808 flags &= ~UP_RELOAD;
3809 update_one_panel_widget (panel, flags, current_file);
3813 /* --------------------------------------------------------------------------------------------- */
3815 static void
3816 do_select (WPanel * panel, int i)
3818 if (i != panel->selected)
3820 panel->dirty = 1;
3821 panel->selected = i;
3822 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3823 if (panel->top_file < 0)
3824 panel->top_file = 0;
3828 /* --------------------------------------------------------------------------------------------- */
3830 static void
3831 do_try_to_select (WPanel * panel, const char *name)
3833 int i;
3834 char *subdir;
3836 if (!name)
3838 do_select (panel, 0);
3839 return;
3842 /* We only want the last component of the directory,
3843 * and from this only the name without suffix.
3844 * Cut prefix if the panel is not panelized */
3846 if (panel->is_panelized)
3847 subdir = vfs_strip_suffix_from_filename (name);
3848 else
3849 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3851 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3852 for (i = 0; i < panel->count; i++)
3854 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3856 do_select (panel, i);
3857 g_free (subdir);
3858 return;
3862 /* Try to select a file near the file that is missing */
3863 if (panel->selected >= panel->count)
3864 do_select (panel, panel->count - 1);
3865 g_free (subdir);
3868 /* --------------------------------------------------------------------------------------------- */
3870 /* event callback */
3871 static gboolean
3872 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3873 gpointer init_data, gpointer data)
3875 (void) event_group_name;
3876 (void) event_name;
3877 (void) init_data;
3878 (void) data;
3880 update_panels (UP_RELOAD, UP_KEEPSEL);
3882 return TRUE;
3885 /* --------------------------------------------------------------------------------------------- */
3887 /* event callback */
3888 static gboolean
3889 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3890 gpointer init_data, gpointer data)
3892 (void) event_group_name;
3893 (void) event_name;
3894 (void) init_data;
3895 (void) data;
3897 if (current_panel->marked == 0)
3898 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3899 (gpointer) selection (current_panel)->fname);
3900 else
3902 int i;
3903 gboolean first = TRUE;
3904 char *flist = NULL;
3906 for (i = 0; i < current_panel->count; i++)
3907 if (current_panel->dir.list[i].f.marked != 0)
3908 { /* Skip the unmarked ones */
3909 if (first)
3911 flist = g_strdup (current_panel->dir.list[i].fname);
3912 first = FALSE;
3914 else
3916 /* Add empty lines after the file */
3917 char *tmp;
3919 tmp =
3920 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3921 g_free (flist);
3922 flist = tmp;
3926 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3927 g_free (flist);
3929 return TRUE;
3932 /* --------------------------------------------------------------------------------------------- */
3933 /*** public functions ****************************************************************************/
3934 /* --------------------------------------------------------------------------------------------- */
3936 void
3937 try_to_select (WPanel * panel, const char *name)
3939 do_try_to_select (panel, name);
3940 select_item (panel);
3943 /* --------------------------------------------------------------------------------------------- */
3945 void
3946 panel_clean_dir (WPanel * panel)
3948 int count = panel->count;
3950 panel->count = 0;
3951 panel->top_file = 0;
3952 panel->selected = 0;
3953 panel->marked = 0;
3954 panel->dirs_marked = 0;
3955 panel->total = 0;
3956 panel->searching = FALSE;
3957 panel->is_panelized = FALSE;
3958 panel->dirty = 1;
3959 panel->content_shift = -1;
3960 panel->max_shift = -1;
3962 clean_dir (&panel->dir, count);
3965 /* --------------------------------------------------------------------------------------------- */
3967 * Set Up panel's current dir object
3969 * @param panel panel object
3970 * @param path_str string contain path
3973 void
3974 panel_set_cwd (WPanel * panel, const char *path_str)
3976 vfs_path_free (panel->cwd_vpath);
3977 panel->cwd_vpath = vfs_path_from_str (path_str);
3980 /* --------------------------------------------------------------------------------------------- */
3982 * Set Up panel's last working dir object
3984 * @param panel panel object
3985 * @param path_str string contain path
3988 void
3989 panel_set_lwd (WPanel * panel, const char *path_str)
3991 vfs_path_free (panel->lwd_vpath);
3992 panel->lwd_vpath = vfs_path_from_str (path_str);
3995 /* --------------------------------------------------------------------------------------------- */
3996 /** Panel creation.
3997 * @param panel_name the name of the panel for setup retieving
3998 * @returns new instance of WPanel
4001 WPanel *
4002 panel_new (const char *panel_name)
4004 return panel_new_with_dir (panel_name, NULL);
4007 /* --------------------------------------------------------------------------------------------- */
4008 /** Panel creation for specified directory.
4009 * @param panel_name specifies the name of the panel for setup retieving
4010 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
4011 * @returns new instance of WPanel
4014 WPanel *
4015 panel_new_with_dir (const char *panel_name, const char *wpath)
4017 WPanel *panel;
4018 char *section;
4019 int i, err;
4020 char *curdir = NULL;
4022 panel = g_new0 (WPanel, 1);
4024 /* No know sizes of the panel at startup */
4025 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
4027 /* We do not want the cursor */
4028 widget_want_cursor (panel->widget, 0);
4030 if (wpath != NULL)
4032 curdir = _vfs_get_cwd ();
4033 panel_set_cwd (panel, wpath);
4035 else
4037 vfs_setup_cwd ();
4038 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4041 panel_set_lwd (panel, ".");
4043 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4044 /* directories history will be get later */
4046 panel->dir.list = g_new (file_entry, MIN_FILES);
4047 panel->dir.size = MIN_FILES;
4048 panel->active = 0;
4049 panel->filter = 0;
4050 panel->split = 0;
4051 panel->top_file = 0;
4052 panel->selected = 0;
4053 panel->marked = 0;
4054 panel->total = 0;
4055 panel->dirty = 1;
4056 panel->searching = FALSE;
4057 panel->dirs_marked = 0;
4058 panel->is_panelized = FALSE;
4059 panel->format = 0;
4060 panel->status_format = 0;
4061 panel->format_modified = 1;
4062 panel->content_shift = -1;
4063 panel->max_shift = -1;
4065 panel->panel_name = g_strdup (panel_name);
4066 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4068 #ifdef HAVE_CHARSET
4069 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4070 #endif
4072 for (i = 0; i < LIST_TYPES; i++)
4073 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4075 panel->search_buffer[0] = '\0';
4076 panel->prev_search_buffer[0] = '\0';
4077 panel->frame_size = frame_half;
4079 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4080 if (!mc_config_has_group (mc_main_config, section))
4082 g_free (section);
4083 section = g_strdup (panel->panel_name);
4085 panel_load_setup (panel, section);
4086 g_free (section);
4088 /* Load format strings */
4089 err = set_panel_formats (panel);
4090 if (err != 0)
4091 set_panel_formats (panel);
4093 #ifdef HAVE_CHARSET
4095 const vfs_path_element_t *path_element;
4097 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4098 if (path_element->encoding != NULL)
4099 panel->codepage = get_codepage_index (path_element->encoding);
4101 #endif
4103 if (mc_chdir (panel->cwd_vpath) != 0)
4105 #ifdef HAVE_CHARSET
4106 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4107 #endif
4108 vfs_setup_cwd ();
4109 vfs_path_free (panel->cwd_vpath);
4110 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4113 /* Load the default format */
4114 panel->count =
4115 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4116 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4117 panel->sort_info.exec_first, panel->filter);
4119 /* Restore old right path */
4120 if (curdir != NULL)
4122 vfs_path_t *vpath;
4124 vpath = vfs_path_from_str (curdir);
4125 err = mc_chdir (vpath);
4126 vfs_path_free (vpath);
4128 g_free (curdir);
4130 return panel;
4133 /* --------------------------------------------------------------------------------------------- */
4135 void
4136 panel_reload (WPanel * panel)
4138 struct stat current_stat;
4139 char *tmp_path;
4140 gboolean ok;
4142 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4143 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
4144 && current_stat.st_ctime == panel->dir_stat.st_ctime
4145 && current_stat.st_mtime == panel->dir_stat.st_mtime);
4146 g_free (tmp_path);
4148 if (ok)
4149 return;
4153 char *last_slash;
4155 if (mc_chdir (panel->cwd_vpath) != -1)
4156 break;
4158 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4159 if (tmp_path[0] == PATH_SEP && tmp_path[1] == '\0')
4161 panel_clean_dir (panel);
4162 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
4163 g_free (tmp_path);
4164 return;
4166 last_slash = strrchr (tmp_path, PATH_SEP);
4167 vfs_path_free (panel->cwd_vpath);
4168 if (last_slash == NULL || last_slash == tmp_path)
4169 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4170 else
4172 *last_slash = '\0';
4173 panel->cwd_vpath = vfs_path_from_str (tmp_path);
4175 g_free (tmp_path);
4176 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4177 show_dir (panel);
4179 while (TRUE);
4181 panel->count =
4182 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4183 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4184 panel->sort_info.exec_first, panel->filter);
4186 panel->dirty = 1;
4187 if (panel->selected >= panel->count)
4188 do_select (panel, panel->count - 1);
4190 recalculate_panel_summary (panel);
4193 /* --------------------------------------------------------------------------------------------- */
4194 /* Switches the panel to the mode specified in the format */
4195 /* Seting up both format and status string. Return: 0 - on success; */
4196 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4199 set_panel_formats (WPanel * p)
4201 format_e *form;
4202 char *err = NULL;
4203 int retcode = 0;
4205 form = use_display_format (p, panel_format (p), &err, 0);
4207 if (err != NULL)
4209 g_free (err);
4210 retcode = 1;
4212 else
4214 delete_format (p->format);
4215 p->format = form;
4218 if (panels_options.show_mini_info)
4220 form = use_display_format (p, mini_status_format (p), &err, 1);
4222 if (err != NULL)
4224 g_free (err);
4225 retcode += 2;
4227 else
4229 delete_format (p->status_format);
4230 p->status_format = form;
4234 panel_format_modified (p);
4235 panel_update_cols (&(p->widget), p->frame_size);
4237 if (retcode)
4238 message (D_ERROR, _("Warning"),
4239 _("User supplied format looks invalid, reverting to default."));
4240 if (retcode & 0x01)
4242 g_free (p->user_format);
4243 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4245 if (retcode & 0x02)
4247 g_free (p->user_status_format[p->list_type]);
4248 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4251 return retcode;
4254 /* --------------------------------------------------------------------------------------------- */
4256 void
4257 panel_update_cols (Widget * widget, panel_display_t frame_size)
4259 int cols, origin;
4261 /* don't touch panel if it is not in dialog yet */
4262 /* if panel is not in dialog it is not in widgets list
4263 and cannot be compared with get_panel_widget() result */
4264 if (widget->owner == NULL)
4265 return;
4267 if (panels_layout.horizontal_split)
4269 widget->cols = COLS;
4270 return;
4273 if (frame_size == frame_full)
4275 cols = COLS;
4276 origin = 0;
4278 else if (widget == get_panel_widget (0))
4280 cols = panels_layout.left_panel_size;
4281 origin = 0;
4283 else
4285 cols = COLS - panels_layout.left_panel_size;
4286 origin = panels_layout.left_panel_size;
4289 widget->cols = cols;
4290 widget->x = origin;
4293 /* --------------------------------------------------------------------------------------------- */
4295 /* Select current item and readjust the panel */
4296 void
4297 select_item (WPanel * panel)
4299 /* Although currently all over the code we set the selection and
4300 top file to decent values before calling select_item, I could
4301 forget it someday, so it's better to do the actual fitting here */
4303 if (panel->selected < 0)
4304 panel->selected = 0;
4306 if (panel->selected > panel->count - 1)
4307 panel->selected = panel->count - 1;
4309 adjust_top_file (panel);
4311 panel->dirty = 1;
4313 execute_hooks (select_file_hook);
4316 /* --------------------------------------------------------------------------------------------- */
4317 /** Clears all files in the panel, used only when one file was marked */
4318 void
4319 unmark_files (WPanel * panel)
4321 int i;
4323 if (!panel->marked)
4324 return;
4325 for (i = 0; i < panel->count; i++)
4326 file_mark (panel, i, 0);
4328 panel->dirs_marked = 0;
4329 panel->marked = 0;
4330 panel->total = 0;
4333 /* --------------------------------------------------------------------------------------------- */
4334 /** Recalculate the panels summary information, used e.g. when marked
4335 files might have been removed by an external command */
4337 void
4338 recalculate_panel_summary (WPanel * panel)
4340 int i;
4342 panel->marked = 0;
4343 panel->dirs_marked = 0;
4344 panel->total = 0;
4346 for (i = 0; i < panel->count; i++)
4347 if (panel->dir.list[i].f.marked)
4349 /* do_file_mark will return immediately if newmark == oldmark.
4350 So we have to first unmark it to get panel's summary information
4351 updated. (Norbert) */
4352 panel->dir.list[i].f.marked = 0;
4353 do_file_mark (panel, i, 1);
4357 /* --------------------------------------------------------------------------------------------- */
4358 /** This routine marks a file or a directory */
4360 void
4361 do_file_mark (WPanel * panel, int idx, int mark)
4363 if (panel->dir.list[idx].f.marked == mark)
4364 return;
4366 /* Only '..' can't be marked, '.' isn't visible */
4367 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4368 return;
4370 file_mark (panel, idx, mark);
4371 if (panel->dir.list[idx].f.marked)
4373 panel->marked++;
4374 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4376 if (panel->dir.list[idx].f.dir_size_computed)
4377 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4378 panel->dirs_marked++;
4380 else
4381 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4382 set_colors (panel);
4384 else
4386 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4388 if (panel->dir.list[idx].f.dir_size_computed)
4389 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4390 panel->dirs_marked--;
4392 else
4393 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4394 panel->marked--;
4398 /* --------------------------------------------------------------------------------------------- */
4400 * Changes the current directory of the panel.
4401 * Record change in the directory history.
4403 gboolean
4404 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4406 gboolean r;
4408 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4409 if (r)
4410 directory_history_add (panel, panel->cwd_vpath);
4411 return r;
4414 /* --------------------------------------------------------------------------------------------- */
4416 void
4417 file_mark (WPanel * panel, int lc_index, int val)
4419 if (panel->dir.list[lc_index].f.marked != val)
4421 panel->dir.list[lc_index].f.marked = val;
4422 panel->dirty = 1;
4426 /* --------------------------------------------------------------------------------------------- */
4428 void
4429 panel_re_sort (WPanel * panel)
4431 char *filename;
4432 int i;
4434 if (panel == NULL)
4435 return;
4437 filename = g_strdup (selection (panel)->fname);
4438 unselect_item (panel);
4439 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4440 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4441 panel->sort_info.exec_first);
4442 panel->selected = -1;
4443 for (i = panel->count; i; i--)
4445 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4447 panel->selected = i - 1;
4448 break;
4451 g_free (filename);
4452 panel->top_file = panel->selected - ITEMS (panel) / 2;
4453 select_item (panel);
4454 panel->dirty = 1;
4457 /* --------------------------------------------------------------------------------------------- */
4459 void
4460 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4462 if (sort_order == NULL)
4463 return;
4465 panel->sort_info.sort_field = sort_order;
4467 /* The directory is already sorted, we have to load the unsorted stuff */
4468 if (sort_order->sort_routine == (sortfn *) unsorted)
4470 char *current_file;
4472 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4473 panel_reload (panel);
4474 try_to_select (panel, current_file);
4475 g_free (current_file);
4477 panel_re_sort (panel);
4480 /* --------------------------------------------------------------------------------------------- */
4482 * Change panel encoding.
4483 * @param panel WPanel object
4486 #ifdef HAVE_CHARSET
4487 void
4488 panel_change_encoding (WPanel * panel)
4490 const char *encoding = NULL;
4491 char *errmsg;
4492 int r;
4494 r = select_charset (-1, -1, panel->codepage, FALSE);
4496 if (r == SELECT_CHARSET_CANCEL)
4497 return; /* Cancel */
4499 panel->codepage = r;
4501 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4503 /* No translation */
4504 vfs_path_t *cd_path_vpath;
4506 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4507 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4508 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4509 show_dir (panel);
4510 vfs_path_free (cd_path_vpath);
4511 return;
4514 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4515 if (errmsg != NULL)
4517 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4518 g_free (errmsg);
4519 return;
4522 encoding = get_codepage_id (panel->codepage);
4523 if (encoding != NULL)
4525 char *cd_path;
4526 vfs_change_encoding (panel->cwd_vpath, encoding);
4528 cd_path = vfs_path_to_str (panel->cwd_vpath);
4529 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4530 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4531 g_free (cd_path);
4535 /* --------------------------------------------------------------------------------------------- */
4538 * Remove encode info from last path element.
4541 vfs_path_t *
4542 remove_encoding_from_path (const vfs_path_t * vpath)
4544 vfs_path_t *ret_vpath;
4545 GString *tmp_conv;
4546 int indx;
4548 ret_vpath = vfs_path_new ();
4550 tmp_conv = g_string_new ("");
4552 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4554 GIConv converter;
4555 vfs_path_element_t *path_element;
4557 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4558 vfs_path_add_element (ret_vpath, path_element);
4560 if (path_element->encoding == NULL)
4561 continue;
4563 converter = str_crt_conv_to (path_element->encoding);
4564 if (converter == INVALID_CONV)
4565 continue;
4567 g_free (path_element->encoding);
4568 path_element->encoding = NULL;
4570 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4572 g_free (path_element->path);
4573 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4575 g_string_set_size (tmp_conv, 0);
4577 str_close_conv (converter);
4578 str_close_conv (path_element->dir.converter);
4579 path_element->dir.converter = INVALID_CONV;
4581 g_string_free (tmp_conv, TRUE);
4582 return ret_vpath;
4584 #endif /* HAVE_CHARSET */
4586 /* --------------------------------------------------------------------------------------------- */
4588 * This routine reloads the directory in both panels. It tries to
4589 * select current_file in current_panel and other_file in other_panel.
4590 * If current_file == -1 then it automatically sets current_file and
4591 * other_file to the currently selected files in the panels.
4593 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4594 * will not reload the other panel.
4597 void
4598 update_panels (panel_update_flags_t flags, const char *current_file)
4600 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4601 WPanel *panel;
4603 update_one_panel (get_current_index (), flags, current_file);
4604 if (reload_other)
4605 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4607 if (get_current_type () == view_listing)
4608 panel = (WPanel *) get_panel_widget (get_current_index ());
4609 else
4610 panel = (WPanel *) get_panel_widget (get_other_index ());
4612 if (!panel->is_panelized)
4613 (void) mc_chdir (panel->cwd_vpath);
4616 /* --------------------------------------------------------------------------------------------- */
4618 gsize
4619 panel_get_num_of_sortable_fields (void)
4621 gsize ret = 0, lc_index;
4623 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4624 if (panel_fields[lc_index].is_user_choice)
4625 ret++;
4626 return ret;
4629 /* --------------------------------------------------------------------------------------------- */
4631 const char **
4632 panel_get_sortable_fields (gsize * array_size)
4634 char **ret;
4635 gsize lc_index, i;
4637 lc_index = panel_get_num_of_sortable_fields ();
4639 ret = g_try_new0 (char *, lc_index + 1);
4640 if (ret == NULL)
4641 return NULL;
4643 if (array_size != NULL)
4644 *array_size = lc_index;
4646 lc_index = 0;
4648 for (i = 0; panel_fields[i].id != NULL; i++)
4649 if (panel_fields[i].is_user_choice)
4650 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4651 return (const char **) ret;
4654 /* --------------------------------------------------------------------------------------------- */
4656 const panel_field_t *
4657 panel_get_field_by_id (const char *name)
4659 gsize lc_index;
4660 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4661 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4662 return &panel_fields[lc_index];
4663 return NULL;
4666 /* --------------------------------------------------------------------------------------------- */
4668 const panel_field_t *
4669 panel_get_field_by_title_hotkey (const char *name)
4671 gsize lc_index;
4672 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4673 if (panel_fields[lc_index].title_hotkey != NULL &&
4674 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4675 return &panel_fields[lc_index];
4676 return NULL;
4679 /* --------------------------------------------------------------------------------------------- */
4681 const panel_field_t *
4682 panel_get_field_by_title (const char *name)
4684 gsize lc_index;
4685 gchar *title = NULL;
4687 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4689 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4690 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4692 g_free (title);
4693 return &panel_fields[lc_index];
4696 g_free (title);
4697 return NULL;
4700 /* --------------------------------------------------------------------------------------------- */
4702 gsize
4703 panel_get_num_of_user_possible_fields (void)
4705 gsize ret = 0, lc_index;
4707 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4708 if (panel_fields[lc_index].use_in_user_format)
4709 ret++;
4710 return ret;
4713 /* --------------------------------------------------------------------------------------------- */
4715 const char **
4716 panel_get_user_possible_fields (gsize * array_size)
4718 char **ret;
4719 gsize lc_index, i;
4721 lc_index = panel_get_num_of_user_possible_fields ();
4723 ret = g_try_new0 (char *, lc_index + 1);
4724 if (ret == NULL)
4725 return NULL;
4727 if (array_size != NULL)
4728 *array_size = lc_index;
4730 lc_index = 0;
4732 for (i = 0; panel_fields[i].id != NULL; i++)
4733 if (panel_fields[i].use_in_user_format)
4734 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4735 return (const char **) ret;
4738 /* --------------------------------------------------------------------------------------------- */
4740 void
4741 panel_init (void)
4743 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4744 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4746 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4747 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4748 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4749 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4750 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4751 panel_filename_scroll_left_char =
4752 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4753 panel_filename_scroll_right_char =
4754 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4756 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4757 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4758 panel_save_curent_file_to_clip_file, NULL, NULL);
4762 /* --------------------------------------------------------------------------------------------- */
4764 void
4765 panel_deinit (void)
4767 g_free (panel_sort_up_sign);
4768 g_free (panel_sort_down_sign);
4770 g_free (panel_hiddenfiles_sign_show);
4771 g_free (panel_hiddenfiles_sign_hide);
4772 g_free (panel_history_prev_item_sign);
4773 g_free (panel_history_next_item_sign);
4774 g_free (panel_history_show_list_sign);
4775 g_free (panel_filename_scroll_left_char);
4776 g_free (panel_filename_scroll_right_char);
4780 /* --------------------------------------------------------------------------------------------- */