panels: Show archive name when entering to archive
[midnight-commander.git] / src / filemanager / panel.c
blobeac0e6ba81b49414a1f11b136095abdbc61a05dd
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 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
62 #include "src/keybind-defaults.h" /* global_keymap_t */
63 #include "src/subshell.h" /* do_subshell_chdir() */
65 #include "dir.h"
66 #include "boxes.h"
67 #include "tree.h"
68 #include "ext.h" /* regexp_command */
69 #include "layout.h" /* Most layout variables are here */
70 #include "cmd.h"
71 #include "command.h" /* cmdline */
72 #include "usermenu.h"
73 #include "midnight.h"
74 #include "mountlist.h" /* my_statfs */
76 #include "panel.h"
78 /*** global variables ****************************************************************************/
80 /* The hook list for the select file function */
81 hook_t *select_file_hook = NULL;
83 /* *INDENT-OFF* */
84 panelized_panel_t panelized_panel = { {NULL, 0}, -1, NULL };
85 /* *INDENT-ON* */
87 static const char *string_file_name (file_entry *, int);
88 static const char *string_file_size (file_entry *, int);
89 static const char *string_file_size_brief (file_entry *, int);
90 static const char *string_file_type (file_entry *, int);
91 static const char *string_file_mtime (file_entry *, int);
92 static const char *string_file_atime (file_entry *, int);
93 static const char *string_file_ctime (file_entry *, int);
94 static const char *string_file_permission (file_entry *, int);
95 static const char *string_file_perm_octal (file_entry *, int);
96 static const char *string_file_nlinks (file_entry *, int);
97 static const char *string_inode (file_entry *, int);
98 static const char *string_file_nuid (file_entry *, int);
99 static const char *string_file_ngid (file_entry *, int);
100 static const char *string_file_owner (file_entry *, int);
101 static const char *string_file_group (file_entry *, int);
102 static const char *string_marked (file_entry *, int);
103 static const char *string_space (file_entry *, int);
104 static const char *string_dot (file_entry *, int);
106 /* *INDENT-OFF* */
107 panel_field_t panel_fields[] = {
109 "unsorted", 12, 1, J_LEFT_FIT,
110 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
111 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
112 N_("sort|u"),
113 N_("&Unsorted"), TRUE, FALSE,
114 string_file_name,
115 (sortfn *) unsorted
119 "name", 12, 1, J_LEFT_FIT,
120 /* TRANSLATORS: one single character to represent 'name' sort mode */
121 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
122 N_("sort|n"),
123 N_("&Name"), TRUE, TRUE,
124 string_file_name,
125 (sortfn *) sort_name
129 "version", 12, 1, J_LEFT_FIT,
130 /* TRANSLATORS: one single character to represent 'version' sort mode */
131 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
132 N_("sort|v"),
133 N_("&Version"), TRUE, FALSE,
134 string_file_name,
135 (sortfn *) sort_vers
139 "extension", 12, 1, J_LEFT_FIT,
140 /* TRANSLATORS: one single character to represent 'extension' sort mode */
141 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
142 N_("sort|e"),
143 N_("&Extension"), TRUE, FALSE,
144 string_file_name, /* TODO: string_file_ext */
145 (sortfn *) sort_ext
149 "size", 7, 0, J_RIGHT,
150 /* TRANSLATORS: one single character to represent 'size' sort mode */
151 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
152 N_("sort|s"),
153 N_("&Size"), TRUE, TRUE,
154 string_file_size,
155 (sortfn *) sort_size
159 "bsize", 7, 0, J_RIGHT,
161 N_("Block Size"), FALSE, FALSE,
162 string_file_size_brief,
163 (sortfn *) sort_size
167 "type", 1, 0, J_LEFT,
169 "", FALSE, TRUE,
170 string_file_type,
171 NULL
175 "mtime", 12, 0, J_RIGHT,
176 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
177 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
178 N_("sort|m"),
179 N_("&Modify time"), TRUE, TRUE,
180 string_file_mtime,
181 (sortfn *) sort_time
185 "atime", 12, 0, J_RIGHT,
186 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
187 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
188 N_("sort|a"),
189 N_("&Access time"), TRUE, TRUE,
190 string_file_atime,
191 (sortfn *) sort_atime
195 "ctime", 12, 0, J_RIGHT,
196 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
197 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
198 N_("sort|h"),
199 N_("C&hange time"), TRUE, TRUE,
200 string_file_ctime,
201 (sortfn *) sort_ctime
205 "perm", 10, 0, J_LEFT,
207 N_("Permission"), FALSE, TRUE,
208 string_file_permission,
209 NULL
213 "mode", 6, 0, J_RIGHT,
215 N_("Perm"), FALSE, TRUE,
216 string_file_perm_octal,
217 NULL
221 "nlink", 2, 0, J_RIGHT,
223 N_("Nl"), FALSE, TRUE,
224 string_file_nlinks, NULL
228 "inode", 5, 0, J_RIGHT,
229 /* TRANSLATORS: one single character to represent 'inode' sort mode */
230 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
231 N_("sort|i"),
232 N_("&Inode"), TRUE, TRUE,
233 string_inode,
234 (sortfn *) sort_inode
238 "nuid", 5, 0, J_RIGHT,
240 N_("UID"), FALSE, FALSE,
241 string_file_nuid,
242 NULL
246 "ngid", 5, 0, J_RIGHT,
248 N_("GID"), FALSE, FALSE,
249 string_file_ngid,
250 NULL
254 "owner", 8, 0, J_LEFT_FIT,
256 N_("Owner"), FALSE, TRUE,
257 string_file_owner,
258 NULL
262 "group", 8, 0, J_LEFT_FIT,
264 N_("Group"), FALSE, TRUE,
265 string_file_group,
266 NULL
270 "mark", 1, 0, J_RIGHT,
272 " ", FALSE, TRUE,
273 string_marked,
274 NULL
278 "|", 1, 0, J_RIGHT,
280 " ", FALSE, TRUE,
281 NULL,
282 NULL
286 "space", 1, 0, J_RIGHT,
288 " ", FALSE, TRUE,
289 string_space,
290 NULL
294 "dot", 1, 0, J_RIGHT,
296 " ", FALSE, FALSE,
297 string_dot,
298 NULL
302 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
305 /* *INDENT-ON* */
307 extern int saving_setup;
309 /*** file scope macro definitions ****************************************************************/
311 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
313 #define NORMAL 0
314 #define SELECTED 1
315 #define MARKED 2
316 #define MARKED_SELECTED 3
317 #define STATUS 5
319 /* This macro extracts the number of available lines in a panel */
320 #define llines(p) (p->widget.lines - 3 - (panels_options.show_mini_info ? 2 : 0))
322 /*** file scope type declarations ****************************************************************/
324 typedef enum
326 MARK_DONT_MOVE = 0,
327 MARK_DOWN = 1,
328 MARK_FORCE_DOWN = 2,
329 MARK_FORCE_UP = 3
330 } mark_act_t;
333 * This describes a format item. The parse_display_format routine parses
334 * the user specified format and creates a linked list of format_e structures.
336 typedef struct format_e
338 struct format_e *next;
339 int requested_field_len;
340 int field_len;
341 align_crt_t just_mode;
342 int expand;
343 const char *(*string_fn) (file_entry *, int len);
344 char *title;
345 const char *id;
346 } format_e;
348 /* File name scroll states */
349 typedef enum
351 FILENAME_NOSCROLL = 1,
352 FILENAME_SCROLL_LEFT = 2,
353 FILENAME_SCROLL_RIGHT = 4
354 } filename_scroll_flag_t;
356 /*** file scope variables ************************************************************************/
358 static char *panel_sort_up_sign = NULL;
359 static char *panel_sort_down_sign = NULL;
361 static char *panel_hiddenfiles_sign_show = NULL;
362 static char *panel_hiddenfiles_sign_hide = NULL;
363 static char *panel_history_prev_item_sign = NULL;
364 static char *panel_history_next_item_sign = NULL;
365 static char *panel_history_show_list_sign = NULL;
366 static char *panel_filename_scroll_left_char = NULL;
367 static char *panel_filename_scroll_right_char = NULL;
369 /* Panel that selection started */
370 static WPanel *mouse_mark_panel = NULL;
372 static int mouse_marking = 0;
373 static int state_mark = 0;
374 /*** file scope functions ************************************************************************/
375 /* --------------------------------------------------------------------------------------------- */
377 static void
378 set_colors (WPanel * panel)
380 (void) panel;
381 tty_set_normal_attrs ();
382 tty_setcolor (NORMAL_COLOR);
385 /* --------------------------------------------------------------------------------------------- */
386 /** Delete format string, it is a linked list */
388 static void
389 delete_format (format_e * format)
391 while (format != NULL)
393 format_e *next = format->next;
394 g_free (format->title);
395 g_free (format);
396 format = next;
400 /* --------------------------------------------------------------------------------------------- */
401 /** This code relies on the default justification!!! */
403 static void
404 add_permission_string (const char *dest, int width, file_entry * fe, int attr, int color,
405 int is_octal)
407 int i, r, l;
409 l = get_user_permissions (&fe->st);
411 if (is_octal)
413 /* Place of the access bit in octal mode */
414 l = width + l - 3;
415 r = l + 1;
417 else
419 /* The same to the triplet in string mode */
420 l = l * 3 + 1;
421 r = l + 3;
424 for (i = 0; i < width; i++)
426 if (i >= l && i < r)
428 if (attr == SELECTED || attr == MARKED_SELECTED)
429 tty_setcolor (MARKED_SELECTED_COLOR);
430 else
431 tty_setcolor (MARKED_COLOR);
433 else if (color >= 0)
434 tty_setcolor (color);
435 else
436 tty_lowlevel_setcolor (-color);
438 tty_print_char (dest[i]);
442 /* --------------------------------------------------------------------------------------------- */
443 /** String representations of various file attributes name */
445 static const char *
446 string_file_name (file_entry * fe, int len)
448 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
450 (void) len;
451 g_strlcpy (buffer, fe->fname, sizeof (buffer));
452 return buffer;
455 /* --------------------------------------------------------------------------------------------- */
457 static unsigned int
458 ilog10 (dev_t n)
460 unsigned int digits = 0;
463 digits++, n /= 10;
465 while (n != 0);
466 return digits;
469 /* --------------------------------------------------------------------------------------------- */
471 static void
472 format_device_number (char *buf, size_t bufsize, dev_t dev)
474 dev_t major_dev = major (dev);
475 dev_t minor_dev = minor (dev);
476 unsigned int major_digits = ilog10 (major_dev);
477 unsigned int minor_digits = ilog10 (minor_dev);
479 g_assert (bufsize >= 1);
480 if (major_digits + 1 + minor_digits + 1 <= bufsize)
482 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
484 else
486 g_strlcpy (buf, _("[dev]"), bufsize);
490 /* --------------------------------------------------------------------------------------------- */
491 /** size */
493 static const char *
494 string_file_size (file_entry * fe, int len)
496 static char buffer[BUF_TINY];
498 /* Don't ever show size of ".." since we don't calculate it */
499 if (!strcmp (fe->fname, ".."))
501 return _("UP--DIR");
504 #ifdef HAVE_STRUCT_STAT_ST_RDEV
505 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
506 format_device_number (buffer, len + 1, fe->st.st_rdev);
507 else
508 #endif
510 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
512 return buffer;
515 /* --------------------------------------------------------------------------------------------- */
516 /** bsize */
518 static const char *
519 string_file_size_brief (file_entry * fe, int len)
521 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
523 return _("SYMLINK");
526 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
528 return _("SUB-DIR");
531 return string_file_size (fe, len);
534 /* --------------------------------------------------------------------------------------------- */
535 /** This functions return a string representation of a file entry type */
537 static const char *
538 string_file_type (file_entry * fe, int len)
540 static char buffer[2];
542 (void) len;
543 if (S_ISDIR (fe->st.st_mode))
544 buffer[0] = PATH_SEP;
545 else if (S_ISLNK (fe->st.st_mode))
547 if (fe->f.link_to_dir)
548 buffer[0] = '~';
549 else if (fe->f.stale_link)
550 buffer[0] = '!';
551 else
552 buffer[0] = '@';
554 else if (S_ISCHR (fe->st.st_mode))
555 buffer[0] = '-';
556 else if (S_ISSOCK (fe->st.st_mode))
557 buffer[0] = '=';
558 else if (S_ISDOOR (fe->st.st_mode))
559 buffer[0] = '>';
560 else if (S_ISBLK (fe->st.st_mode))
561 buffer[0] = '+';
562 else if (S_ISFIFO (fe->st.st_mode))
563 buffer[0] = '|';
564 else if (S_ISNAM (fe->st.st_mode))
565 buffer[0] = '#';
566 else if (!S_ISREG (fe->st.st_mode))
567 buffer[0] = '?'; /* non-regular of unknown kind */
568 else if (is_exe (fe->st.st_mode))
569 buffer[0] = '*';
570 else
571 buffer[0] = ' ';
572 buffer[1] = '\0';
573 return buffer;
576 /* --------------------------------------------------------------------------------------------- */
577 /** mtime */
579 static const char *
580 string_file_mtime (file_entry * fe, int len)
582 (void) len;
583 return file_date (fe->st.st_mtime);
586 /* --------------------------------------------------------------------------------------------- */
587 /** atime */
589 static const char *
590 string_file_atime (file_entry * fe, int len)
592 (void) len;
593 return file_date (fe->st.st_atime);
596 /* --------------------------------------------------------------------------------------------- */
597 /** ctime */
599 static const char *
600 string_file_ctime (file_entry * fe, int len)
602 (void) len;
603 return file_date (fe->st.st_ctime);
606 /* --------------------------------------------------------------------------------------------- */
607 /** perm */
609 static const char *
610 string_file_permission (file_entry * fe, int len)
612 (void) len;
613 return string_perm (fe->st.st_mode);
616 /* --------------------------------------------------------------------------------------------- */
617 /** mode */
619 static const char *
620 string_file_perm_octal (file_entry * fe, int len)
622 static char buffer[10];
624 (void) len;
625 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
626 return buffer;
629 /* --------------------------------------------------------------------------------------------- */
630 /** nlink */
632 static const char *
633 string_file_nlinks (file_entry * fe, int len)
635 static char buffer[BUF_TINY];
637 (void) len;
638 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
639 return buffer;
642 /* --------------------------------------------------------------------------------------------- */
643 /** inode */
645 static const char *
646 string_inode (file_entry * fe, int len)
648 static char buffer[10];
650 (void) len;
651 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
652 return buffer;
655 /* --------------------------------------------------------------------------------------------- */
656 /** nuid */
658 static const char *
659 string_file_nuid (file_entry * fe, int len)
661 static char buffer[10];
663 (void) len;
664 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
665 return buffer;
668 /* --------------------------------------------------------------------------------------------- */
669 /** ngid */
671 static const char *
672 string_file_ngid (file_entry * fe, int len)
674 static char buffer[10];
676 (void) len;
677 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
678 return buffer;
681 /* --------------------------------------------------------------------------------------------- */
682 /** owner */
684 static const char *
685 string_file_owner (file_entry * fe, int len)
687 (void) len;
688 return get_owner (fe->st.st_uid);
691 /* --------------------------------------------------------------------------------------------- */
692 /** group */
694 static const char *
695 string_file_group (file_entry * fe, int len)
697 (void) len;
698 return get_group (fe->st.st_gid);
701 /* --------------------------------------------------------------------------------------------- */
702 /** mark */
704 static const char *
705 string_marked (file_entry * fe, int len)
707 (void) len;
708 return fe->f.marked ? "*" : " ";
711 /* --------------------------------------------------------------------------------------------- */
712 /** space */
714 static const char *
715 string_space (file_entry * fe, int len)
717 (void) fe;
718 (void) len;
719 return " ";
722 /* --------------------------------------------------------------------------------------------- */
723 /** dot */
725 static const char *
726 string_dot (file_entry * fe, int len)
728 (void) fe;
729 (void) len;
730 return ".";
733 /* --------------------------------------------------------------------------------------------- */
735 static int
736 file_compute_color (int attr, file_entry * fe)
738 switch (attr)
740 case SELECTED:
741 return (SELECTED_COLOR);
742 case MARKED:
743 return (MARKED_COLOR);
744 case MARKED_SELECTED:
745 return (MARKED_SELECTED_COLOR);
746 case STATUS:
747 return (NORMAL_COLOR);
748 case NORMAL:
749 default:
750 if (!panels_options.filetype_mode)
751 return (NORMAL_COLOR);
754 return mc_fhl_get_color (mc_filehighlight, fe);
757 /* --------------------------------------------------------------------------------------------- */
758 /** Formats the file number file_index of panel in the buffer dest */
760 static filename_scroll_flag_t
761 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
762 int isstatus, int * field_lenght)
764 int color, length, empty_line;
765 const char *txt;
766 format_e *format, *home;
767 file_entry *fe;
768 filename_scroll_flag_t res = FILENAME_NOSCROLL;
770 (void) dest;
771 (void) limit;
772 length = 0;
773 empty_line = (file_index >= panel->count);
774 home = (isstatus) ? panel->status_format : panel->format;
775 fe = &panel->dir.list[file_index];
776 *field_lenght = 0;
778 if (!empty_line)
779 color = file_compute_color (attr, fe);
780 else
781 color = NORMAL_COLOR;
782 for (format = home; format; format = format->next)
784 if (length == width)
785 break;
787 if (format->string_fn)
789 int len, perm;
790 const char *prepared_text;
791 int name_offset = 0;
793 if (empty_line)
794 txt = " ";
795 else
796 txt = (*format->string_fn) (fe, format->field_len);
798 len = format->field_len;
799 if (len + length > width)
800 len = width - length;
801 if (len <= 0)
802 break;
804 if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
806 int str_len;
807 int i;
809 *field_lenght = len + 1;
811 str_len = str_length (txt);
812 i = max (0, str_len - len);
813 panel->max_shift = max (panel->max_shift, i);
814 i = min (panel->content_shift, i);
816 if (i > -1)
818 name_offset = str_offset_to_pos (txt, i);
819 if (str_len > len)
821 res = FILENAME_SCROLL_LEFT;
822 if (str_length (txt + name_offset) > len)
823 res |= FILENAME_SCROLL_RIGHT;
828 perm = 0;
829 if (panels_options.permission_mode)
831 if (!strcmp (format->id, "perm"))
832 perm = 1;
833 else if (!strcmp (format->id, "mode"))
834 perm = 2;
837 if (color >= 0)
838 tty_setcolor (color);
839 else
840 tty_lowlevel_setcolor (-color);
842 if (!isstatus && panel->content_shift > -1)
843 prepared_text = str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
844 else
845 prepared_text = str_fit_to_term (txt, len, format->just_mode);
847 if (perm)
848 add_permission_string (prepared_text, format->field_len, fe, attr, color, perm - 1);
849 else
850 tty_print_string (prepared_text);
852 length += len;
854 else
856 if (attr == SELECTED || attr == MARKED_SELECTED)
857 tty_setcolor (SELECTED_COLOR);
858 else
859 tty_setcolor (NORMAL_COLOR);
860 tty_print_one_vline (TRUE);
861 length++;
865 if (length < width)
866 tty_draw_hline (-1, -1, ' ', width - length);
868 return res;
871 /* --------------------------------------------------------------------------------------------- */
873 static void
874 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
876 int second_column = 0;
877 int width;
878 int offset = 0;
879 char buffer[BUF_MEDIUM];
880 filename_scroll_flag_t ret_frm;
881 int ypos = 0;
882 gboolean panel_is_split = !isstatus && panel->split;
883 int fln = 0;
885 width = panel->widget.cols - 2;
887 if (panel_is_split)
889 second_column = (file_index - panel->top_file) / llines (panel);
890 width = width / 2 - 1;
892 if (second_column != 0)
894 offset = 1 + width;
895 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
896 width = panel->widget.cols - offset - 2;
900 /* Nothing to paint */
901 if (width <= 0)
902 return;
904 if (mv)
906 int pos = file_index - panel->top_file;
908 if (panel_is_split)
909 ypos = pos % llines (panel);
910 else
911 ypos = pos;
913 ypos += 2;
914 widget_move (&panel->widget, ypos, offset + 1);
917 ret_frm = format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus, &fln);
919 if (panel_is_split)
921 if (second_column)
922 tty_print_char (' ');
923 else
925 tty_setcolor (NORMAL_COLOR);
926 tty_print_one_vline (TRUE);
929 if (ret_frm != FILENAME_NOSCROLL && mv)
931 if (!panel_is_split && fln > 0)
933 if (panel->list_type == list_long)
935 offset = width - fln + 1;
936 width = fln - 1;
938 else
940 width = fln;
943 widget_move (&panel->widget, ypos, offset);
944 tty_setcolor (NORMAL_COLOR);
945 tty_print_string (panel_filename_scroll_left_char);
947 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
949 widget_move (&panel->widget, ypos, offset + 1 + width);
950 tty_setcolor (NORMAL_COLOR);
951 tty_print_string (panel_filename_scroll_right_char);
956 /* --------------------------------------------------------------------------------------------- */
958 static void
959 display_mini_info (WPanel * panel)
961 if (!panels_options.show_mini_info)
962 return;
964 widget_move (&panel->widget, llines (panel) + 3, 1);
966 if (panel->searching)
968 tty_setcolor (INPUT_COLOR);
969 tty_print_char ('/');
970 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
971 return;
974 /* Status resolves links and show them */
975 set_colors (panel);
977 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
979 char link_target[MC_MAXPATHLEN];
980 vfs_path_t *lc_link_vpath;
981 int len;
983 lc_link_vpath =
984 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
985 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
986 vfs_path_free (lc_link_vpath);
987 if (len > 0)
989 link_target[len] = 0;
990 tty_print_string ("-> ");
991 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
993 else
994 tty_print_string (str_fit_to_term (_("<readlink failed>"),
995 panel->widget.cols - 2, J_LEFT));
997 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
999 /* FIXME:
1000 * while loading directory (do_load_dir() and do_reload_dir()),
1001 * the actual stat info about ".." directory isn't got;
1002 * so just don't display incorrect info about ".." directory */
1003 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
1005 else
1006 /* Default behavior */
1007 repaint_file (panel, panel->selected, 0, STATUS, 1);
1010 /* --------------------------------------------------------------------------------------------- */
1012 static void
1013 paint_dir (WPanel * panel)
1015 int i;
1016 int color; /* Color value of the line */
1017 int items; /* Number of items */
1019 items = llines (panel) * (panel->split ? 2 : 1);
1020 /* reset max len of filename because we have the new max length for the new file list */
1021 panel->max_shift = -1;
1022 for (i = 0; i < items; i++)
1024 if (i + panel->top_file >= panel->count)
1025 color = 0;
1026 else
1028 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1029 color += (panel->selected == i + panel->top_file && panel->active);
1031 repaint_file (panel, i + panel->top_file, 1, color, 0);
1034 tty_set_normal_attrs ();
1037 /* --------------------------------------------------------------------------------------------- */
1039 static void
1040 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
1042 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
1043 int cols;
1045 if (panel->marked <= 0)
1046 return;
1048 buf = size_only ? b_bytes : buffer;
1049 cols = panel->widget.cols - 2;
1052 * This is a trick to use two ngettext() calls in one sentence.
1053 * First make "N bytes", then insert it into "X in M files".
1055 g_snprintf (b_bytes, sizeof (b_bytes),
1056 ngettext ("%s byte", "%s bytes", panel->total),
1057 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1058 if (!size_only)
1059 g_snprintf (buffer, sizeof (buffer),
1060 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1061 b_bytes, panel->marked);
1063 /* don't forget spaces around buffer content */
1064 buf = (char *) str_trunc (buf, cols - 4);
1066 if (x < 0)
1067 /* center in panel */
1068 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
1071 * y == llines (panel) + 2 for mini_info_separator
1072 * y == panel->widget.lines - 1 for panel bottom frame
1074 widget_move (&panel->widget, y, x);
1075 tty_setcolor (MARKED_COLOR);
1076 tty_printf (" %s ", buf);
1079 /* --------------------------------------------------------------------------------------------- */
1081 static void
1082 mini_info_separator (WPanel * panel)
1084 if (panels_options.show_mini_info)
1086 const int y = llines (panel) + 2;
1088 tty_setcolor (NORMAL_COLOR);
1089 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1090 ACS_HLINE, panel->widget.cols - 2);
1091 /* Status displays total marked size.
1092 * Centered in panel, full format. */
1093 display_total_marked_size (panel, y, -1, FALSE);
1097 /* --------------------------------------------------------------------------------------------- */
1099 static void
1100 show_free_space (WPanel * panel)
1102 /* Used to figure out how many free space we have */
1103 static struct my_statfs myfs_stats;
1104 /* Old current working directory for displaying free space */
1105 static char *old_cwd = NULL;
1106 char *tmp_path;
1108 /* Don't try to stat non-local fs */
1109 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1110 return;
1112 tmp_path = vfs_path_to_str (panel->cwd_vpath);
1113 if (old_cwd == NULL || strcmp (old_cwd, tmp_path) != 0)
1115 char rpath[PATH_MAX];
1117 init_my_statfs ();
1118 g_free (old_cwd);
1119 old_cwd = tmp_path;
1120 tmp_path = NULL;
1122 if (mc_realpath (old_cwd, rpath) == NULL)
1123 return;
1125 my_statfs (&myfs_stats, rpath);
1127 g_free (tmp_path);
1129 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1131 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1132 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1133 panels_options.kilobyte_si);
1134 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1135 panels_options.kilobyte_si);
1136 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1137 myfs_stats.total == 0 ? 0 :
1138 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1139 widget_move (&panel->widget, panel->widget.lines - 1,
1140 panel->widget.cols - 2 - (int) strlen (tmp));
1141 tty_setcolor (NORMAL_COLOR);
1142 tty_print_string (tmp);
1146 /* --------------------------------------------------------------------------------------------- */
1148 * Prepare path string for showing in panel's header.
1149 * Passwords will removed, also home dir will replaced by ~
1151 * @param panel WPanel object
1153 * @return newly allocated string.
1156 static char *
1157 panel_correct_path_to_show (WPanel * panel)
1159 vfs_path_t *last_vpath;
1160 const vfs_path_element_t *path_element;
1161 char *return_path;
1162 int elements_count;
1164 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1166 /* get last path element */
1167 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1170 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1171 strcmp (path_element->class->name, "extfs") == 0 ||
1172 strcmp (path_element->class->name, "tarfs") == 0))
1174 const char *archive_name;
1175 const vfs_path_element_t *prev_path_element;
1177 /* get previous path element for catching archive name */
1178 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1179 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1180 if (archive_name != NULL)
1182 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1184 else
1186 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1187 last_vpath->relative = TRUE;
1190 else
1192 last_vpath = vfs_path_new ();
1193 last_vpath->relative = TRUE;
1196 vfs_path_add_element (last_vpath, path_element);
1197 return_path =
1198 vfs_path_to_str_flags (last_vpath, 0,
1199 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1200 vfs_path_free (last_vpath);
1202 return return_path;
1205 /* --------------------------------------------------------------------------------------------- */
1207 * Get Current path element encoding
1209 * @param panel WPanel object
1211 * @return newly allocated string or NULL if path charset is same as system charset
1214 static char *
1215 panel_get_encoding_info_str (WPanel * panel)
1217 char *ret_str = NULL;
1218 const vfs_path_element_t *path_element;
1220 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1221 if (path_element->encoding != NULL)
1222 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1224 return ret_str;
1227 /* --------------------------------------------------------------------------------------------- */
1229 static void
1230 show_dir (WPanel * panel)
1232 gchar *tmp;
1233 set_colors (panel);
1234 draw_box (panel->widget.owner,
1235 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1237 if (panels_options.show_mini_info)
1239 widget_move (&panel->widget, llines (panel) + 2, 0);
1240 tty_print_alt_char (ACS_LTEE, FALSE);
1241 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1242 tty_print_alt_char (ACS_RTEE, FALSE);
1245 widget_move (&panel->widget, 0, 1);
1246 tty_print_string (panel_history_prev_item_sign);
1248 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1249 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1250 panel_history_next_item_sign);
1252 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1253 tty_print_string (tmp);
1255 g_free (tmp);
1257 widget_move (&panel->widget, 0, 3);
1259 if (panel->is_panelized)
1260 tty_printf (" %s ", _("Panelize"));
1261 else
1263 tmp = panel_get_encoding_info_str (panel);
1264 if (tmp != NULL)
1266 tty_printf ("%s", tmp);
1267 widget_move (&panel->widget, 0, 3 + strlen (tmp));
1268 g_free (tmp);
1271 if (panel->active)
1272 tty_setcolor (REVERSE_COLOR);
1274 tmp = panel_correct_path_to_show (panel);
1275 tty_printf (" %s ",
1276 str_term_trim (tmp, min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1277 g_free (tmp);
1279 if (!panels_options.show_mini_info)
1281 if (panel->marked == 0)
1283 /* Show size of curret file in the bottom of panel */
1284 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1286 char buffer[BUF_SMALL];
1288 g_snprintf (buffer, sizeof (buffer), " %s ",
1289 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1290 panels_options.kilobyte_si));
1291 tty_setcolor (NORMAL_COLOR);
1292 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1293 tty_print_string (buffer);
1296 else
1298 /* Show total size of marked files
1299 * In the bottom of panel, display size only. */
1300 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1304 show_free_space (panel);
1306 if (panel->active)
1307 tty_set_normal_attrs ();
1310 /* --------------------------------------------------------------------------------------------- */
1312 /* Returns the number of items in the given panel */
1313 static int
1314 ITEMS (WPanel * p)
1316 if (p->split)
1317 return llines (p) * 2;
1318 else
1319 return llines (p);
1322 /* --------------------------------------------------------------------------------------------- */
1324 static void
1325 adjust_top_file (WPanel * panel)
1327 int items = ITEMS (panel);
1329 if (panel->count <= items)
1331 /* If all files fit, show them all. */
1332 panel->top_file = 0;
1334 else
1336 int i;
1338 /* top_file has to be in the range [selected-items+1, selected] so that
1339 the selected file is visible.
1340 top_file should be in the range [0, count-items] so that there's
1341 no empty space wasted.
1342 Within these ranges, adjust it by as little as possible. */
1344 if (panel->top_file < 0)
1345 panel->top_file = 0;
1347 i = panel->selected - items + 1;
1348 if (panel->top_file < i)
1349 panel->top_file = i;
1351 i = panel->count - items;
1352 if (panel->top_file > i)
1353 panel->top_file = i;
1355 if (panel->top_file > panel->selected)
1356 panel->top_file = panel->selected;
1360 /* --------------------------------------------------------------------------------------------- */
1361 /** add "#enc:encodning" to end of path */
1362 /* if path end width a previous #enc:, only encoding is changed no additional
1363 * #enc: is appended
1364 * retun new string
1367 static char *
1368 panel_save_name (WPanel * panel)
1370 /* If the program is shuting down */
1371 if ((mc_global.widget.midnight_shutdown && auto_save_setup) || saving_setup)
1372 return g_strdup (panel->panel_name);
1373 else
1374 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1377 /* --------------------------------------------------------------------------------------------- */
1379 /* "history_load" event handler */
1380 static gboolean
1381 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1382 gpointer init_data, gpointer data)
1384 WPanel *p = (WPanel *) init_data;
1385 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1387 (void) event_group_name;
1388 (void) event_name;
1390 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1392 char *tmp_path;
1394 tmp_path = vfs_path_to_str (p->cwd_vpath);
1395 if (ev->cfg != NULL)
1396 p->dir_history = history_load (ev->cfg, p->hist_name);
1397 else
1398 p->dir_history = history_get (p->hist_name);
1400 directory_history_add (p, tmp_path);
1401 g_free (tmp_path);
1404 return TRUE;
1407 /* --------------------------------------------------------------------------------------------- */
1409 /* "history_save" event handler */
1410 static gboolean
1411 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1412 gpointer init_data, gpointer data)
1414 WPanel *p = (WPanel *) init_data;
1416 (void) event_group_name;
1417 (void) event_name;
1419 if (p->dir_history != NULL)
1421 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1423 history_save (ev->cfg, p->hist_name, p->dir_history);
1426 return TRUE;
1429 /* --------------------------------------------------------------------------------------------- */
1431 static void
1432 panel_destroy (WPanel * p)
1434 size_t i;
1436 if (panels_options.auto_save_setup)
1438 char *name;
1440 name = panel_save_name (p);
1441 panel_save_setup (p, name);
1442 g_free (name);
1445 panel_clean_dir (p);
1447 /* clean history */
1448 if (p->dir_history != NULL)
1450 /* directory history is already saved before this moment */
1451 p->dir_history = g_list_first (p->dir_history);
1452 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1453 g_list_free (p->dir_history);
1455 g_free (p->hist_name);
1457 delete_format (p->format);
1458 delete_format (p->status_format);
1460 g_free (p->user_format);
1461 for (i = 0; i < LIST_TYPES; i++)
1462 g_free (p->user_status_format[i]);
1464 g_free (p->dir.list);
1465 g_free (p->panel_name);
1467 vfs_path_free (p->lwd_vpath);
1468 vfs_path_free (p->cwd_vpath);
1471 /* --------------------------------------------------------------------------------------------- */
1473 static void
1474 panel_format_modified (WPanel * panel)
1476 panel->format_modified = 1;
1479 /* --------------------------------------------------------------------------------------------- */
1481 static void
1482 panel_paint_sort_info (WPanel * panel)
1484 if (*panel->sort_info.sort_field->hotkey != '\0')
1486 const char *sort_sign =
1487 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1488 char *str;
1490 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1491 widget_move (&panel->widget, 1, 1);
1492 tty_print_string (str);
1493 g_free (str);
1497 /* --------------------------------------------------------------------------------------------- */
1499 static gchar *
1500 panel_get_title_without_hotkey (const char *title)
1502 char *translated_title;
1503 char *hkey;
1505 if (title == NULL)
1506 return NULL;
1507 if (title[0] == '\0')
1508 return g_strdup ("");
1510 translated_title = g_strdup (_(title));
1512 hkey = strchr (translated_title, '&');
1513 if ((hkey != NULL) && (hkey[1] != '\0'))
1514 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1516 return translated_title;
1519 /* --------------------------------------------------------------------------------------------- */
1521 static void
1522 paint_frame (WPanel * panel)
1524 int side, width;
1525 GString *format_txt;
1527 adjust_top_file (panel);
1529 widget_erase (&panel->widget);
1530 show_dir (panel);
1532 widget_move (&panel->widget, 1, 1);
1534 for (side = 0; side <= panel->split; side++)
1536 format_e *format;
1538 if (side)
1540 tty_setcolor (NORMAL_COLOR);
1541 tty_print_one_vline (TRUE);
1542 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1544 else if (panel->split)
1545 width = panel->widget.cols / 2 - 3;
1546 else
1547 width = panel->widget.cols - 2;
1549 format_txt = g_string_new ("");
1550 for (format = panel->format; format; format = format->next)
1552 if (format->string_fn)
1554 g_string_set_size (format_txt, 0);
1556 if (panel->list_type == list_long
1557 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1558 g_string_append (format_txt,
1559 panel->sort_info.reverse
1560 ? panel_sort_down_sign : panel_sort_up_sign);
1562 g_string_append (format_txt, format->title);
1563 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1565 g_string_append (format_txt, " [");
1566 g_string_append (format_txt, panel->filter);
1567 g_string_append (format_txt, "]");
1570 tty_setcolor (HEADER_COLOR);
1571 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1572 J_CENTER_LEFT));
1573 width -= format->field_len;
1575 else
1577 tty_setcolor (NORMAL_COLOR);
1578 tty_print_one_vline (TRUE);
1579 width--;
1582 g_string_free (format_txt, TRUE);
1584 if (width > 0)
1585 tty_draw_hline (-1, -1, ' ', width);
1588 if (panel->list_type != list_long)
1589 panel_paint_sort_info (panel);
1592 /* --------------------------------------------------------------------------------------------- */
1594 static const char *
1595 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1597 panel_display_t frame = frame_half;
1598 format = skip_separators (format);
1600 if (!strncmp (format, "full", 4))
1602 frame = frame_full;
1603 format += 4;
1605 else if (!strncmp (format, "half", 4))
1607 frame = frame_half;
1608 format += 4;
1611 if (!isstatus)
1613 panel->frame_size = frame;
1614 panel->split = 0;
1617 /* Now, the optional column specifier */
1618 format = skip_separators (format);
1620 if (*format == '1' || *format == '2')
1622 if (!isstatus)
1623 panel->split = *format == '2';
1624 format++;
1627 if (!isstatus)
1628 panel_update_cols (&(panel->widget), panel->frame_size);
1630 return skip_separators (format);
1633 /* Format is:
1635 all := panel_format? format
1636 panel_format := [full|half] [1|2]
1637 format := one_format_e
1638 | format , one_format_e
1640 one_format_e := just format.id [opt_size]
1641 just := [<=>]
1642 opt_size := : size [opt_expand]
1643 size := [0-9]+
1644 opt_expand := +
1648 /* --------------------------------------------------------------------------------------------- */
1650 static format_e *
1651 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1652 int *res_total_cols)
1654 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1655 int total_cols = 0; /* Used columns by the format */
1656 int set_justify; /* flag: set justification mode? */
1657 align_crt_t justify = J_LEFT; /* Which mode. */
1658 size_t i;
1660 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1662 *error = 0;
1664 if (i18n_timelength == 0)
1666 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1668 for (i = 0; panel_fields[i].id != NULL; i++)
1669 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1670 panel_fields[i].min_size = i18n_timelength;
1674 * This makes sure that the panel and mini status full/half mode
1675 * setting is equal
1677 format = parse_panel_size (panel, format, isstatus);
1679 while (*format)
1680 { /* format can be an empty string */
1681 int found = 0;
1683 darr = g_new0 (format_e, 1);
1685 /* I'm so ugly, don't look at me :-) */
1686 if (!home)
1687 home = old = darr;
1689 old->next = darr;
1690 darr->next = 0;
1691 old = darr;
1693 format = skip_separators (format);
1695 if (strchr ("<=>", *format))
1697 set_justify = 1;
1698 switch (*format)
1700 case '<':
1701 justify = J_LEFT;
1702 break;
1703 case '=':
1704 justify = J_CENTER;
1705 break;
1706 case '>':
1707 default:
1708 justify = J_RIGHT;
1709 break;
1711 format = skip_separators (format + 1);
1713 else
1714 set_justify = 0;
1716 for (i = 0; panel_fields[i].id != NULL; i++)
1718 size_t klen = strlen (panel_fields[i].id);
1720 if (strncmp (format, panel_fields[i].id, klen) != 0)
1721 continue;
1723 format += klen;
1725 darr->requested_field_len = panel_fields[i].min_size;
1726 darr->string_fn = panel_fields[i].string_fn;
1727 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1729 darr->id = panel_fields[i].id;
1730 darr->expand = panel_fields[i].expands;
1731 darr->just_mode = panel_fields[i].default_just;
1733 if (set_justify)
1735 if (IS_FIT (darr->just_mode))
1736 darr->just_mode = MAKE_FIT (justify);
1737 else
1738 darr->just_mode = justify;
1740 found = 1;
1742 format = skip_separators (format);
1744 /* If we have a size specifier */
1745 if (*format == ':')
1747 int req_length;
1749 /* If the size was specified, we don't want
1750 * auto-expansion by default
1752 darr->expand = 0;
1753 format++;
1754 req_length = atoi (format);
1755 darr->requested_field_len = req_length;
1757 format = skip_numbers (format);
1759 /* Now, if they insist on expansion */
1760 if (*format == '+')
1762 darr->expand = 1;
1763 format++;
1768 break;
1770 if (!found)
1772 char *tmp_format = g_strdup (format);
1774 int pos = min (8, strlen (format));
1775 delete_format (home);
1776 tmp_format[pos] = 0;
1777 *error =
1778 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1779 g_free (tmp_format);
1780 return 0;
1782 total_cols += darr->requested_field_len;
1785 *res_total_cols = total_cols;
1786 return home;
1789 /* --------------------------------------------------------------------------------------------- */
1791 static format_e *
1792 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1794 #define MAX_EXPAND 4
1795 int expand_top = 0; /* Max used element in expand */
1796 int usable_columns; /* Usable columns in the panel */
1797 int total_cols = 0;
1798 int i;
1799 format_e *darr, *home;
1801 if (!format)
1802 format = DEFAULT_USER_FORMAT;
1804 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1806 if (*error)
1807 return 0;
1809 panel->dirty = 1;
1811 /* Status needn't to be split */
1812 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1814 : (panel->split + 1))) - (!isstatus
1815 && panel->split);
1817 /* Look for the expandable fields and set field_len based on the requested field len */
1818 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1820 darr->field_len = darr->requested_field_len;
1821 if (darr->expand)
1822 expand_top++;
1825 /* If we used more columns than the available columns, adjust that */
1826 if (total_cols > usable_columns)
1828 int pdif, dif = total_cols - usable_columns;
1830 while (dif)
1832 pdif = dif;
1833 for (darr = home; darr; darr = darr->next)
1835 if (dif && darr->field_len - 1)
1837 darr->field_len--;
1838 dif--;
1842 /* avoid endless loop if num fields > 40 */
1843 if (pdif == dif)
1844 break;
1846 total_cols = usable_columns; /* give up, the rest should be truncated */
1849 /* Expand the available space */
1850 if ((usable_columns > total_cols) && expand_top)
1852 int spaces = (usable_columns - total_cols) / expand_top;
1853 int extra = (usable_columns - total_cols) % expand_top;
1855 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1856 if (darr->expand)
1858 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1859 i++;
1862 return home;
1865 /* --------------------------------------------------------------------------------------------- */
1866 /** Given the panel->view_type returns the format string to be parsed */
1868 static const char *
1869 panel_format (WPanel * panel)
1871 switch (panel->list_type)
1873 case list_long:
1874 return "full perm space nlink space owner space group space size space mtime space name";
1876 case list_brief:
1877 return "half 2 type name";
1879 case list_user:
1880 return panel->user_format;
1882 default:
1883 case list_full:
1884 return "half type name | size | mtime";
1888 /* --------------------------------------------------------------------------------------------- */
1890 static const char *
1891 mini_status_format (WPanel * panel)
1893 if (panel->user_mini_status)
1894 return panel->user_status_format[panel->list_type];
1896 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 type name space bsize space perm space";
1905 case list_full:
1906 return "half type name";
1908 default:
1909 case list_user:
1910 return panel->user_format;
1914 /* */
1915 /* Panel operation commands */
1916 /* */
1918 /* --------------------------------------------------------------------------------------------- */
1919 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1921 static cb_ret_t
1922 maybe_cd (int move_up_dir)
1924 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1926 if (move_up_dir)
1928 vfs_path_t *up_dir;
1930 up_dir = vfs_path_from_str ("..");
1931 do_cd (up_dir, cd_exact);
1932 vfs_path_free (up_dir);
1933 return MSG_HANDLED;
1936 if (S_ISDIR (selection (current_panel)->st.st_mode)
1937 || link_isdir (selection (current_panel)))
1939 vfs_path_t *vpath;
1941 vpath = vfs_path_from_str (selection (current_panel)->fname);
1942 do_cd (vpath, cd_exact);
1943 vfs_path_free (vpath);
1944 return MSG_HANDLED;
1947 return MSG_NOT_HANDLED;
1950 /* --------------------------------------------------------------------------------------------- */
1952 /* if command line is empty then do 'cd ..' */
1953 static cb_ret_t
1954 force_maybe_cd (void)
1956 if (cmdline->buffer[0] == '\0')
1958 vfs_path_t *up_dir = vfs_path_from_str ("..");
1959 do_cd (up_dir, cd_exact);
1960 vfs_path_free (up_dir);
1961 return MSG_HANDLED;
1963 return MSG_NOT_HANDLED;
1966 /* --------------------------------------------------------------------------------------------- */
1968 static void
1969 unselect_item (WPanel * panel)
1971 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1974 /* --------------------------------------------------------------------------------------------- */
1976 static void
1977 move_down (WPanel * panel)
1979 if (panel->selected + 1 == panel->count)
1980 return;
1982 unselect_item (panel);
1983 panel->selected++;
1984 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1986 /* Scroll window half screen */
1987 panel->top_file += ITEMS (panel) / 2;
1988 if (panel->top_file > panel->count - ITEMS (panel))
1989 panel->top_file = panel->count - ITEMS (panel);
1990 paint_dir (panel);
1992 select_item (panel);
1995 /* --------------------------------------------------------------------------------------------- */
1997 static void
1998 move_up (WPanel * panel)
2000 if (panel->selected == 0)
2001 return;
2003 unselect_item (panel);
2004 panel->selected--;
2005 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2007 /* Scroll window half screen */
2008 panel->top_file -= ITEMS (panel) / 2;
2009 if (panel->top_file < 0)
2010 panel->top_file = 0;
2011 paint_dir (panel);
2013 select_item (panel);
2016 /* --------------------------------------------------------------------------------------------- */
2017 /** Changes the selection by lines (may be negative) */
2019 static void
2020 move_selection (WPanel * panel, int lines)
2022 int new_pos;
2023 int adjust = 0;
2025 new_pos = panel->selected + lines;
2026 if (new_pos >= panel->count)
2027 new_pos = panel->count - 1;
2029 if (new_pos < 0)
2030 new_pos = 0;
2032 unselect_item (panel);
2033 panel->selected = new_pos;
2035 if (panel->selected - panel->top_file >= ITEMS (panel))
2037 panel->top_file += lines;
2038 adjust = 1;
2041 if (panel->selected - panel->top_file < 0)
2043 panel->top_file += lines;
2044 adjust = 1;
2047 if (adjust)
2049 if (panel->top_file > panel->selected)
2050 panel->top_file = panel->selected;
2051 if (panel->top_file < 0)
2052 panel->top_file = 0;
2053 paint_dir (panel);
2055 select_item (panel);
2058 /* --------------------------------------------------------------------------------------------- */
2060 static cb_ret_t
2061 move_left (WPanel * panel)
2063 if (panel->split)
2065 move_selection (panel, -llines (panel));
2066 return MSG_HANDLED;
2068 else
2069 return maybe_cd (1); /* cd .. */
2072 /* --------------------------------------------------------------------------------------------- */
2074 static cb_ret_t
2075 move_right (WPanel * panel)
2077 if (panel->split)
2079 move_selection (panel, llines (panel));
2080 return MSG_HANDLED;
2082 else
2083 return maybe_cd (0); /* cd (selection) */
2086 /* --------------------------------------------------------------------------------------------- */
2088 static void
2089 prev_page (WPanel * panel)
2091 int items;
2093 if (!panel->selected && !panel->top_file)
2094 return;
2095 unselect_item (panel);
2096 items = ITEMS (panel);
2097 if (panel->top_file < items)
2098 items = panel->top_file;
2099 if (!items)
2100 panel->selected = 0;
2101 else
2102 panel->selected -= items;
2103 panel->top_file -= items;
2105 select_item (panel);
2106 paint_dir (panel);
2109 /* --------------------------------------------------------------------------------------------- */
2111 static void
2112 goto_parent_dir (WPanel * panel)
2114 if (!panel->is_panelized)
2116 vfs_path_t *up_dir;
2118 up_dir = vfs_path_from_str ("..");
2119 do_cd (up_dir, cd_exact);
2120 vfs_path_free (up_dir);
2122 else
2124 char *fname = panel->dir.list[panel->selected].fname;
2125 const char *bname;
2126 vfs_path_t *dname_vpath;
2128 if (g_path_is_absolute (fname))
2129 fname = g_strdup (fname);
2130 else
2132 char *tmp_root;
2134 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2135 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2136 g_free (tmp_root);
2139 bname = x_basename (fname);
2141 if (bname == fname)
2142 dname_vpath = vfs_path_from_str (".");
2143 else
2145 char *dname;
2147 dname = g_strndup (fname, bname - fname);
2148 dname_vpath = vfs_path_from_str (dname);
2149 g_free (dname);
2152 do_cd (dname_vpath, cd_exact);
2153 try_to_select (panel, bname);
2155 vfs_path_free (dname_vpath);
2156 g_free (fname);
2160 /* --------------------------------------------------------------------------------------------- */
2162 static void
2163 next_page (WPanel * panel)
2165 int items;
2167 if (panel->selected == panel->count - 1)
2168 return;
2169 unselect_item (panel);
2170 items = ITEMS (panel);
2171 if (panel->top_file > panel->count - 2 * items)
2172 items = panel->count - items - panel->top_file;
2173 if (panel->top_file + items < 0)
2174 items = -panel->top_file;
2175 if (!items)
2176 panel->selected = panel->count - 1;
2177 else
2178 panel->selected += items;
2179 panel->top_file += items;
2181 select_item (panel);
2182 paint_dir (panel);
2185 /* --------------------------------------------------------------------------------------------- */
2187 static void
2188 goto_child_dir (WPanel * panel)
2190 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2192 vfs_path_t *vpath;
2194 vpath = vfs_path_from_str (selection (panel)->fname);
2195 do_cd (vpath, cd_exact);
2196 vfs_path_free (vpath);
2200 /* --------------------------------------------------------------------------------------------- */
2202 static void
2203 goto_top_file (WPanel * panel)
2205 unselect_item (panel);
2206 panel->selected = panel->top_file;
2207 select_item (panel);
2210 /* --------------------------------------------------------------------------------------------- */
2212 static void
2213 goto_middle_file (WPanel * panel)
2215 unselect_item (panel);
2216 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2217 select_item (panel);
2220 /* --------------------------------------------------------------------------------------------- */
2222 static void
2223 goto_bottom_file (WPanel * panel)
2225 unselect_item (panel);
2226 panel->selected = panel->top_file + ITEMS (panel) - 1;
2227 select_item (panel);
2230 /* --------------------------------------------------------------------------------------------- */
2232 static void
2233 move_home (WPanel * panel)
2235 if (panel->selected == 0)
2236 return;
2238 unselect_item (panel);
2240 if (panels_options.torben_fj_mode)
2242 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2244 if (panel->selected > middle_pos)
2246 goto_middle_file (panel);
2247 return;
2249 if (panel->selected != panel->top_file)
2251 goto_top_file (panel);
2252 return;
2256 panel->top_file = 0;
2257 panel->selected = 0;
2259 paint_dir (panel);
2260 select_item (panel);
2263 /* --------------------------------------------------------------------------------------------- */
2265 static void
2266 move_end (WPanel * panel)
2268 if (panel->selected == panel->count - 1)
2269 return;
2271 unselect_item (panel);
2273 if (panels_options.torben_fj_mode)
2275 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2277 if (panel->selected < middle_pos)
2279 goto_middle_file (panel);
2280 return;
2282 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2284 goto_bottom_file (panel);
2285 return;
2289 panel->selected = panel->count - 1;
2290 paint_dir (panel);
2291 select_item (panel);
2294 /* --------------------------------------------------------------------------------------------- */
2296 static void
2297 do_mark_file (WPanel * panel, mark_act_t do_move)
2299 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2300 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2301 move_down (panel);
2302 else if (do_move == MARK_FORCE_UP)
2303 move_up (panel);
2306 /* --------------------------------------------------------------------------------------------- */
2308 static void
2309 mark_file (WPanel * panel)
2311 do_mark_file (panel, MARK_DOWN);
2314 /* --------------------------------------------------------------------------------------------- */
2316 static void
2317 mark_file_up (WPanel * panel)
2319 do_mark_file (panel, MARK_FORCE_UP);
2322 /* --------------------------------------------------------------------------------------------- */
2324 static void
2325 mark_file_down (WPanel * panel)
2327 do_mark_file (panel, MARK_FORCE_DOWN);
2330 /* --------------------------------------------------------------------------------------------- */
2332 static void
2333 mark_file_right (WPanel * panel)
2335 int lines = llines (panel);
2337 if (state_mark < 0)
2338 state_mark = selection (panel)->f.marked ? 0 : 1;
2340 lines = min (lines, panel->count - panel->selected - 1);
2341 for (; lines != 0; lines--)
2343 do_file_mark (panel, panel->selected, state_mark);
2344 move_down (panel);
2346 do_file_mark (panel, panel->selected, state_mark);
2349 /* --------------------------------------------------------------------------------------------- */
2351 static void
2352 mark_file_left (WPanel * panel)
2354 int lines = llines (panel);
2356 if (state_mark < 0)
2357 state_mark = selection (panel)->f.marked ? 0 : 1;
2359 lines = min (lines, panel->selected + 1);
2360 for (; lines != 0; lines--)
2362 do_file_mark (panel, panel->selected, state_mark);
2363 move_up (panel);
2365 do_file_mark (panel, panel->selected, state_mark);
2368 /* --------------------------------------------------------------------------------------------- */
2369 /** Incremental search of a file name in the panel.
2370 * @param panel instance of WPanel structure
2371 * @param c_code key code
2374 static void
2375 do_search (WPanel * panel, int c_code)
2377 size_t l;
2378 int i, sel;
2379 gboolean wrapped = FALSE;
2380 char *act;
2381 mc_search_t *search;
2382 char *reg_exp, *esc_str;
2383 gboolean is_found = FALSE;
2385 l = strlen (panel->search_buffer);
2386 if (c_code == KEY_BACKSPACE)
2388 if (l != 0)
2390 act = panel->search_buffer + l;
2391 str_prev_noncomb_char (&act, panel->search_buffer);
2392 act[0] = '\0';
2394 panel->search_chpoint = 0;
2396 else
2398 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2400 panel->search_char[panel->search_chpoint] = c_code;
2401 panel->search_chpoint++;
2404 if (panel->search_chpoint > 0)
2406 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2408 case -2:
2409 return;
2410 case -1:
2411 panel->search_chpoint = 0;
2412 return;
2413 default:
2414 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2416 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2417 l += panel->search_chpoint;
2418 *(panel->search_buffer + l) = '\0';
2419 panel->search_chpoint = 0;
2425 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2426 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2427 search = mc_search_new (esc_str, -1);
2428 search->search_type = MC_SEARCH_T_GLOB;
2429 search->is_entire_line = TRUE;
2430 switch (panels_options.qsearch_mode)
2432 case QSEARCH_CASE_SENSITIVE:
2433 search->is_case_sensitive = TRUE;
2434 break;
2435 case QSEARCH_CASE_INSENSITIVE:
2436 search->is_case_sensitive = FALSE;
2437 break;
2438 default:
2439 search->is_case_sensitive = panel->sort_info.case_sensitive;
2440 break;
2442 sel = panel->selected;
2443 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2445 if (i >= panel->count)
2447 i = 0;
2448 if (wrapped)
2449 break;
2450 wrapped = TRUE;
2452 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2454 sel = i;
2455 is_found = TRUE;
2456 break;
2459 if (is_found)
2461 unselect_item (panel);
2462 panel->selected = sel;
2463 select_item (panel);
2464 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2466 else if (c_code != KEY_BACKSPACE)
2468 act = panel->search_buffer + l;
2469 str_prev_noncomb_char (&act, panel->search_buffer);
2470 act[0] = '\0';
2472 mc_search_free (search);
2473 g_free (reg_exp);
2474 g_free (esc_str);
2477 /* --------------------------------------------------------------------------------------------- */
2478 /** Start new search.
2479 * @param panel instance of WPanel structure
2482 static void
2483 start_search (WPanel * panel)
2485 if (panel->searching)
2487 if (panel->selected + 1 == panel->count)
2488 panel->selected = 0;
2489 else
2490 move_down (panel);
2492 /* in case if there was no search string we need to recall
2493 previous string, with which we ended previous searching */
2494 if (panel->search_buffer[0] == '\0')
2495 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2496 sizeof (panel->search_buffer));
2498 do_search (panel, 0);
2500 else
2502 panel->searching = TRUE;
2503 panel->search_buffer[0] = '\0';
2504 panel->search_char[0] = '\0';
2505 panel->search_chpoint = 0;
2506 display_mini_info (panel);
2507 mc_refresh ();
2511 /* --------------------------------------------------------------------------------------------- */
2513 static void
2514 stop_search (WPanel * panel)
2516 panel->searching = FALSE;
2518 /* if user had overrdied search string, we need to store it
2519 to the previous_search_buffer */
2520 if (panel->search_buffer[0] != '\0')
2521 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2522 sizeof (panel->prev_search_buffer));
2524 display_mini_info (panel);
2527 /* --------------------------------------------------------------------------------------------- */
2528 /** Return 1 if the Enter key has been processed, 0 otherwise */
2530 static int
2531 do_enter_on_file_entry (file_entry * fe)
2533 vfs_path_t *full_name_vpath;
2534 gboolean ok;
2537 * Directory or link to directory - change directory.
2538 * Try the same for the entries on which mc_lstat() has failed.
2540 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2542 vfs_path_t *fname_vpath;
2544 fname_vpath = vfs_path_from_str (fe->fname);
2545 if (!do_cd (fname_vpath, cd_exact))
2546 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2547 vfs_path_free (fname_vpath);
2548 return 1;
2551 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2553 /* Try associated command */
2554 if (regex_command (full_name_vpath, "Open", NULL) != 0)
2556 vfs_path_free (full_name_vpath);
2557 return 1;
2560 /* Check if the file is executable */
2561 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2562 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2563 vfs_path_free (full_name_vpath);
2564 if (!ok)
2565 return 0;
2567 if (confirm_execute)
2569 if (query_dialog
2570 (_("The Midnight Commander"),
2571 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2572 return 1;
2575 if (!vfs_current_is_local ())
2577 int ret;
2578 vfs_path_t *tmp_vpath;
2580 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2581 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2582 vfs_path_free (tmp_vpath);
2583 /* We took action only if the dialog was shown or the execution
2584 * was successful */
2585 return confirm_execute || (ret == 0);
2589 char *tmp = name_quote (fe->fname, 0);
2590 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2591 g_free (tmp);
2592 shell_execute (cmd, 0);
2593 g_free (cmd);
2596 #if HAVE_CHARSET
2597 mc_global.source_codepage = default_source_codepage;
2598 #endif
2600 return 1;
2603 /* --------------------------------------------------------------------------------------------- */
2605 static int
2606 do_enter (WPanel * panel)
2608 return do_enter_on_file_entry (selection (panel));
2611 /* --------------------------------------------------------------------------------------------- */
2613 static void
2614 chdir_other_panel (WPanel * panel)
2616 const file_entry *entry = &panel->dir.list[panel->selected];
2618 vfs_path_t *new_dir_vpath;
2619 char *sel_entry = NULL;
2621 if (get_other_type () != view_listing)
2623 set_display_type (get_other_index (), view_listing);
2626 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2627 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2628 else
2630 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2631 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2634 change_panel ();
2635 do_cd (new_dir_vpath, cd_exact);
2636 vfs_path_free (new_dir_vpath);
2638 if (sel_entry)
2639 try_to_select (current_panel, sel_entry);
2640 change_panel ();
2642 move_down (panel);
2645 /* --------------------------------------------------------------------------------------------- */
2647 * Make the current directory of the current panel also the current
2648 * directory of the other panel. Put the other panel to the listing
2649 * mode if needed. If the current panel is panelized, the other panel
2650 * doesn't become panelized.
2653 static void
2654 panel_sync_other (const WPanel * panel)
2656 if (get_other_type () != view_listing)
2658 set_display_type (get_other_index (), view_listing);
2661 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2663 /* try to select current filename on the other panel */
2664 if (!panel->is_panelized)
2666 try_to_select (other_panel, selection (panel)->fname);
2670 /* --------------------------------------------------------------------------------------------- */
2672 static void
2673 chdir_to_readlink (WPanel * panel)
2675 vfs_path_t *new_dir_vpath;
2676 char buffer[MC_MAXPATHLEN], *p;
2677 int i;
2678 struct stat st;
2679 vfs_path_t *panel_fname_vpath;
2680 gboolean ok;
2682 if (get_other_type () != view_listing)
2683 return;
2685 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2686 return;
2688 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2689 if (i < 0)
2690 return;
2692 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2693 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2694 vfs_path_free (panel_fname_vpath);
2695 if (!ok)
2696 return;
2698 buffer[i] = 0;
2699 if (!S_ISDIR (st.st_mode))
2701 p = strrchr (buffer, PATH_SEP);
2702 if (p && !p[1])
2704 *p = 0;
2705 p = strrchr (buffer, PATH_SEP);
2707 if (!p)
2708 return;
2709 p[1] = 0;
2711 if (*buffer == PATH_SEP)
2712 new_dir_vpath = vfs_path_from_str (buffer);
2713 else
2714 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2716 change_panel ();
2717 do_cd (new_dir_vpath, cd_exact);
2718 vfs_path_free (new_dir_vpath);
2719 change_panel ();
2721 move_down (panel);
2724 /* --------------------------------------------------------------------------------------------- */
2726 static gsize
2727 panel_get_format_field_count (WPanel * panel)
2729 format_e *format;
2730 gsize lc_index;
2731 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2732 return lc_index;
2735 /* --------------------------------------------------------------------------------------------- */
2737 function return 0 if not found and REAL_INDEX+1 if found
2740 static gsize
2741 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2743 format_e *format;
2744 gsize lc_index;
2746 for (lc_index = 1, format = panel->format;
2747 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2748 if (format == NULL)
2749 lc_index = 0;
2751 return lc_index;
2754 /* --------------------------------------------------------------------------------------------- */
2756 static format_e *
2757 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2759 format_e *format;
2760 for (format = panel->format;
2761 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2762 return format;
2765 /* --------------------------------------------------------------------------------------------- */
2767 static const panel_field_t *
2768 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2770 const panel_field_t *pfield;
2771 format_e *format;
2773 format = panel_get_format_field_by_index (panel, lc_index);
2774 if (format == NULL)
2775 return NULL;
2776 pfield = panel_get_field_by_title (format->title);
2777 if (pfield == NULL)
2778 return NULL;
2779 if (pfield->sort_routine == NULL)
2780 return NULL;
2781 return pfield;
2784 /* --------------------------------------------------------------------------------------------- */
2786 static void
2787 panel_toggle_sort_order_prev (WPanel * panel)
2789 gsize lc_index, i;
2790 gchar *title;
2792 const panel_field_t *pfield = NULL;
2794 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2795 lc_index = panel_get_format_field_index_by_name (panel, title);
2796 g_free (title);
2798 if (lc_index > 1)
2800 /* search for prev sortable column in panel format */
2801 for (i = lc_index - 1;
2802 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2805 if (pfield == NULL)
2807 /* Sortable field not found. Try to search in each array */
2808 for (i = panel_get_format_field_count (panel);
2809 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2812 if (pfield != NULL)
2814 panel->sort_info.sort_field = pfield;
2815 panel_set_sort_order (panel, pfield);
2819 /* --------------------------------------------------------------------------------------------- */
2821 static void
2822 panel_toggle_sort_order_next (WPanel * panel)
2824 gsize lc_index, i;
2825 const panel_field_t *pfield = NULL;
2826 gsize format_field_count;
2827 gchar *title;
2829 format_field_count = panel_get_format_field_count (panel);
2830 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2831 lc_index = panel_get_format_field_index_by_name (panel, title);
2832 g_free (title);
2834 if (lc_index != 0 && lc_index != format_field_count)
2836 /* search for prev sortable column in panel format */
2837 for (i = lc_index;
2838 i != format_field_count
2839 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2842 if (pfield == NULL)
2844 /* Sortable field not found. Try to search in each array */
2845 for (i = 0;
2846 i != format_field_count
2847 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2850 if (pfield != NULL)
2852 panel->sort_info.sort_field = pfield;
2853 panel_set_sort_order (panel, pfield);
2857 /* --------------------------------------------------------------------------------------------- */
2859 static void
2860 panel_select_sort_order (WPanel * panel)
2862 const panel_field_t *sort_order;
2864 sort_order = sort_box (&panel->sort_info);
2865 if (sort_order != NULL)
2867 panel->sort_info.sort_field = sort_order;
2868 panel_set_sort_order (panel, sort_order);
2872 /* --------------------------------------------------------------------------------------------- */
2875 * panel_content_scroll_left:
2876 * @param panel the pointer to the panel on which we operate
2878 * scroll long filename to the left (decrement scroll pointer)
2882 static void
2883 panel_content_scroll_left (WPanel * panel)
2885 if (panel->content_shift > -1)
2887 if (panel->content_shift > panel->max_shift)
2888 panel->content_shift = panel->max_shift;
2890 panel->content_shift--;
2891 show_dir (panel);
2892 paint_dir (panel);
2897 /* --------------------------------------------------------------------------------------------- */
2900 * panel_content_scroll_right:
2901 * @param panel the pointer to the panel on which we operate
2903 * scroll long filename to the right (increment scroll pointer)
2907 static void
2908 panel_content_scroll_right (WPanel * panel)
2910 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
2912 panel->content_shift++;
2913 show_dir (panel);
2914 paint_dir (panel);
2918 /* --------------------------------------------------------------------------------------------- */
2920 static void
2921 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2923 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2925 const panel_field_t *sort_order;
2927 sort_order = panel_get_field_by_id (name);
2928 if (sort_order == NULL)
2929 return;
2930 panel->sort_info.sort_field = sort_order;
2932 else
2933 panel->sort_info.reverse = !panel->sort_info.reverse;
2935 panel_set_sort_order (panel, panel->sort_info.sort_field);
2938 /* --------------------------------------------------------------------------------------------- */
2940 * If we moved to the parent directory move the selection pointer to
2941 * the old directory name; If we leave VFS dir, remove FS specificator.
2943 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2946 static const char *
2947 get_parent_dir_name (const char *cwd, const char *lwd)
2949 size_t llen, clen;
2950 const char *p;
2952 llen = strlen (lwd);
2953 clen = strlen (cwd);
2955 if (llen <= clen)
2956 return NULL;
2958 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2960 if (p == NULL)
2962 p = strrchr (lwd, PATH_SEP);
2964 if ((p != NULL)
2965 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2966 && (clen == (size_t) (p - lwd)
2967 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2968 return (p + 1);
2970 return NULL;
2973 while (--p > lwd && *p != PATH_SEP);
2974 while (--p > lwd && *p != PATH_SEP);
2976 return (p != lwd) ? p + 1 : NULL;
2979 /* --------------------------------------------------------------------------------------------- */
2980 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2982 static void
2983 subshell_chdir (const vfs_path_t * vpath)
2985 #ifdef HAVE_SUBSHELL_SUPPORT
2986 if (mc_global.tty.use_subshell && vfs_current_is_local ())
2987 do_subshell_chdir (vpath, FALSE, TRUE);
2988 #endif /* HAVE_SUBSHELL_SUPPORT */
2991 /* --------------------------------------------------------------------------------------------- */
2993 * Changes the current directory of the panel.
2994 * Don't record change in the directory history.
2997 static gboolean
2998 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3000 char *olddir;
3002 olddir = vfs_path_to_str (panel->cwd_vpath);
3004 /* Convert *new_path to a suitable pathname, handle ~user */
3005 if (cd_type == cd_parse_command)
3007 const vfs_path_element_t *element;
3009 element = vfs_path_get_by_index (new_dir_vpath, 0);
3010 if (strcmp (element->path, "-") == 0)
3011 new_dir_vpath = panel->lwd_vpath;
3014 if (mc_chdir (new_dir_vpath) == -1)
3016 panel_set_cwd (panel, olddir);
3017 g_free (olddir);
3018 return FALSE;
3021 /* Success: save previous directory, shutdown status of previous dir */
3022 panel_set_lwd (panel, olddir);
3023 input_free_completions (cmdline);
3025 vfs_path_free (panel->cwd_vpath);
3026 vfs_setup_cwd ();
3027 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3029 vfs_release_path (olddir);
3031 subshell_chdir (panel->cwd_vpath);
3033 /* Reload current panel */
3034 panel_clean_dir (panel);
3036 char *tmp_path;
3038 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3039 panel->count =
3040 do_load_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
3041 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3042 panel->sort_info.exec_first, panel->filter);
3043 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
3044 g_free (tmp_path);
3046 load_hint (0);
3047 panel->dirty = 1;
3048 update_xterm_title_path ();
3050 g_free (olddir);
3052 return TRUE;
3055 /* --------------------------------------------------------------------------------------------- */
3057 static void
3058 directory_history_next (WPanel * panel)
3060 GList *nextdir;
3062 nextdir = g_list_next (panel->dir_history);
3063 if (nextdir != NULL)
3065 vfs_path_t *data_vpath;
3067 data_vpath = vfs_path_from_str ((char *) nextdir->data);
3068 if (_do_panel_cd (panel, data_vpath, cd_exact))
3069 panel->dir_history = nextdir;
3070 vfs_path_free (data_vpath);
3074 /* --------------------------------------------------------------------------------------------- */
3076 static void
3077 directory_history_prev (WPanel * panel)
3079 GList *prevdir;
3081 prevdir = g_list_previous (panel->dir_history);
3083 if (prevdir != NULL)
3085 vfs_path_t *data_vpath;
3087 data_vpath = vfs_path_from_str ((char *) prevdir->data);
3088 if (_do_panel_cd (panel, data_vpath, cd_exact))
3089 panel->dir_history = prevdir;
3090 vfs_path_free (data_vpath);
3094 /* --------------------------------------------------------------------------------------------- */
3096 static void
3097 directory_history_list (WPanel * panel)
3099 char *s;
3101 s = history_show (&panel->dir_history, &panel->widget);
3103 if (s != NULL)
3105 vfs_path_t *s_vpath;
3107 s_vpath = vfs_path_from_str (s);
3108 if (_do_panel_cd (panel, s_vpath, cd_exact))
3110 char *tmp_path;
3112 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3113 directory_history_add (panel, tmp_path);
3114 g_free (tmp_path);
3116 else
3117 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3118 vfs_path_free (s_vpath);
3119 g_free (s);
3123 /* --------------------------------------------------------------------------------------------- */
3125 static cb_ret_t
3126 panel_execute_cmd (WPanel * panel, unsigned long command)
3128 int res = MSG_HANDLED;
3130 if (command != CK_Search)
3131 stop_search (panel);
3134 switch (command)
3136 case CK_Up:
3137 case CK_Down:
3138 case CK_Left:
3139 case CK_Right:
3140 case CK_Bottom:
3141 case CK_Top:
3142 case CK_PageDown:
3143 case CK_PageUp:
3144 /* reset state of marks flag */
3145 state_mark = -1;
3146 break;
3148 switch (command)
3150 case CK_PanelOtherCd:
3151 chdir_other_panel (panel);
3152 break;
3153 case CK_PanelOtherCdLink:
3154 chdir_to_readlink (panel);
3155 break;
3156 case CK_CopySingle:
3157 copy_cmd_local ();
3158 break;
3159 case CK_DeleteSingle:
3160 delete_cmd_local ();
3161 break;
3162 case CK_Enter:
3163 do_enter (panel);
3164 break;
3165 case CK_ViewRaw:
3166 view_raw_cmd ();
3167 break;
3168 case CK_EditNew:
3169 edit_cmd_new ();
3170 break;
3171 case CK_MoveSingle:
3172 rename_cmd_local ();
3173 break;
3174 case CK_SelectInvert:
3175 select_invert_cmd ();
3176 break;
3177 case CK_Select:
3178 select_cmd ();
3179 break;
3180 case CK_Unselect:
3181 unselect_cmd ();
3182 break;
3183 case CK_PageDown:
3184 next_page (panel);
3185 break;
3186 case CK_PageUp:
3187 prev_page (panel);
3188 break;
3189 case CK_CdChild:
3190 goto_child_dir (panel);
3191 break;
3192 case CK_CdParent:
3193 goto_parent_dir (panel);
3194 break;
3195 case CK_History:
3196 directory_history_list (panel);
3197 break;
3198 case CK_HistoryNext:
3199 directory_history_next (panel);
3200 break;
3201 case CK_HistoryPrev:
3202 directory_history_prev (panel);
3203 break;
3204 case CK_BottomOnScreen:
3205 goto_bottom_file (panel);
3206 break;
3207 case CK_MiddleOnScreen:
3208 goto_middle_file (panel);
3209 break;
3210 case CK_TopOnScreen:
3211 goto_top_file (panel);
3212 break;
3213 case CK_Mark:
3214 mark_file (panel);
3215 break;
3216 case CK_MarkUp:
3217 mark_file_up (panel);
3218 break;
3219 case CK_MarkDown:
3220 mark_file_down (panel);
3221 break;
3222 case CK_MarkLeft:
3223 mark_file_left (panel);
3224 break;
3225 case CK_MarkRight:
3226 mark_file_right (panel);
3227 break;
3228 case CK_CdParentSmart:
3229 res = force_maybe_cd ();
3230 break;
3231 case CK_Up:
3232 move_up (panel);
3233 break;
3234 case CK_Down:
3235 move_down (panel);
3236 break;
3237 case CK_Left:
3238 res = move_left (panel);
3239 break;
3240 case CK_Right:
3241 res = move_right (panel);
3242 break;
3243 case CK_Bottom:
3244 move_end (panel);
3245 break;
3246 case CK_Top:
3247 move_home (panel);
3248 break;
3249 #ifdef HAVE_CHARSET
3250 case CK_SelectCodepage:
3251 panel_change_encoding (panel);
3252 break;
3253 #endif
3254 case CK_ScrollLeft:
3255 panel_content_scroll_left (panel);
3256 break;
3257 case CK_ScrollRight:
3258 panel_content_scroll_right (panel);
3259 break;
3260 case CK_Search:
3261 start_search (panel);
3262 break;
3263 case CK_SearchStop:
3264 break;
3265 case CK_PanelOtherSync:
3266 panel_sync_other (panel);
3267 break;
3268 case CK_Sort:
3269 panel_select_sort_order (panel);
3270 break;
3271 case CK_SortPrev:
3272 panel_toggle_sort_order_prev (panel);
3273 break;
3274 case CK_SortNext:
3275 panel_toggle_sort_order_next (panel);
3276 break;
3277 case CK_SortReverse:
3278 panel->sort_info.reverse = !panel->sort_info.reverse;
3279 panel_set_sort_order (panel, panel->sort_info.sort_field);
3280 break;
3281 case CK_SortByName:
3282 panel_set_sort_type_by_id (panel, "name");
3283 break;
3284 case CK_SortByExt:
3285 panel_set_sort_type_by_id (panel, "extension");
3286 break;
3287 case CK_SortBySize:
3288 panel_set_sort_type_by_id (panel, "size");
3289 break;
3290 case CK_SortByMTime:
3291 panel_set_sort_type_by_id (panel, "mtime");
3292 break;
3294 return res;
3297 /* --------------------------------------------------------------------------------------------- */
3299 static cb_ret_t
3300 panel_key (WPanel * panel, int key)
3302 size_t i;
3304 if (is_abort_char (key))
3306 stop_search (panel);
3307 return MSG_HANDLED;
3310 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3312 do_search (panel, key);
3313 return MSG_HANDLED;
3316 for (i = 0; panel_map[i].key != 0; i++)
3317 if (key == panel_map[i].key)
3318 return panel_execute_cmd (panel, panel_map[i].command);
3320 if (panels_options.torben_fj_mode && key == ALT ('h'))
3322 goto_middle_file (panel);
3323 return MSG_HANDLED;
3326 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3328 start_search (panel);
3329 do_search (panel, key);
3330 return MSG_HANDLED;
3333 return MSG_NOT_HANDLED;
3336 /* --------------------------------------------------------------------------------------------- */
3338 static cb_ret_t
3339 panel_callback (Widget * w, widget_msg_t msg, int parm)
3341 WPanel *panel = (WPanel *) w;
3342 WButtonBar *bb;
3344 switch (msg)
3346 case WIDGET_INIT:
3347 /* subscribe to "history_load" event */
3348 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3349 /* subscribe to "history_save" event */
3350 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3351 return MSG_HANDLED;
3353 case WIDGET_DRAW:
3354 /* Repaint everything, including frame and separator */
3355 paint_frame (panel); /* including show_dir */
3356 paint_dir (panel);
3357 mini_info_separator (panel);
3358 display_mini_info (panel);
3359 panel->dirty = 0;
3360 return MSG_HANDLED;
3362 case WIDGET_FOCUS:
3363 state_mark = -1;
3364 current_panel = panel;
3365 panel->active = 1;
3366 if (mc_chdir (panel->cwd_vpath) != 0)
3368 char *cwd;
3370 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3371 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3372 cwd, unix_error_string (errno));
3373 g_free (cwd);
3375 else
3376 subshell_chdir (panel->cwd_vpath);
3378 update_xterm_title_path ();
3379 select_item (panel);
3380 show_dir (panel);
3381 paint_dir (panel);
3382 panel->dirty = 0;
3384 bb = find_buttonbar (panel->widget.owner);
3385 midnight_set_buttonbar (bb);
3386 buttonbar_redraw (bb);
3387 return MSG_HANDLED;
3389 case WIDGET_UNFOCUS:
3390 /* Janne: look at this for the multiple panel options */
3391 stop_search (panel);
3392 panel->active = 0;
3393 show_dir (panel);
3394 unselect_item (panel);
3395 return MSG_HANDLED;
3397 case WIDGET_KEY:
3398 return panel_key (panel, parm);
3400 case WIDGET_COMMAND:
3401 return panel_execute_cmd (panel, parm);
3403 case WIDGET_DESTROY:
3404 /* unsubscribe from "history_load" event */
3405 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3406 /* unsubscribe from "history_save" event */
3407 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3408 panel_destroy (panel);
3409 free_my_statfs ();
3410 return MSG_HANDLED;
3412 default:
3413 return default_proc (msg, parm);
3417 /* --------------------------------------------------------------------------------------------- */
3418 /* */
3419 /* Panel mouse events support routines */
3420 /* */
3422 static void
3423 mouse_toggle_mark (WPanel * panel)
3425 do_mark_file (panel, MARK_DONT_MOVE);
3426 mouse_marking = selection (panel)->f.marked;
3427 mouse_mark_panel = current_panel;
3430 /* --------------------------------------------------------------------------------------------- */
3432 static void
3433 mouse_set_mark (WPanel * panel)
3436 if (mouse_mark_panel == panel)
3438 if (mouse_marking && !(selection (panel)->f.marked))
3439 do_mark_file (panel, MARK_DONT_MOVE);
3440 else if (!mouse_marking && (selection (panel)->f.marked))
3441 do_mark_file (panel, MARK_DONT_MOVE);
3445 /* --------------------------------------------------------------------------------------------- */
3447 static int
3448 mark_if_marking (WPanel * panel, Gpm_Event * event)
3450 if (event->buttons & GPM_B_RIGHT)
3452 if (event->type & GPM_DOWN)
3453 mouse_toggle_mark (panel);
3454 else
3455 mouse_set_mark (panel);
3456 return 1;
3458 return 0;
3461 /* --------------------------------------------------------------------------------------------- */
3462 /** Determine which column was clicked, and sort the panel on
3463 * that column, or reverse sort on that column if already
3464 * sorted on that column.
3467 static void
3468 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3470 int i;
3471 const char *lc_sort_name = NULL;
3472 panel_field_t *col_sort_format = NULL;
3473 format_e *format;
3474 gchar *title;
3476 for (i = 0, format = panel->format; format != NULL; format = format->next)
3478 i += format->field_len;
3479 if (event->x < i + 1)
3481 /* found column */
3482 lc_sort_name = format->title;
3483 break;
3487 if (lc_sort_name == NULL)
3488 return;
3490 for (i = 0; panel_fields[i].id != NULL; i++)
3492 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3493 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3495 col_sort_format = &panel_fields[i];
3496 g_free (title);
3497 break;
3499 g_free (title);
3502 if (col_sort_format == NULL)
3503 return;
3505 if (panel->sort_info.sort_field == col_sort_format)
3507 /* reverse the sort if clicked column is already the sorted column */
3508 panel->sort_info.reverse = !panel->sort_info.reverse;
3510 else
3512 /* new sort is forced to be ascending */
3513 panel->sort_info.reverse = FALSE;
3515 panel_set_sort_order (panel, col_sort_format);
3519 /* --------------------------------------------------------------------------------------------- */
3521 * Mouse callback of the panel minus repainting.
3522 * If the event is redirected to the menu, *redir is set to TRUE.
3524 static int
3525 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3527 const int lines = llines (panel);
3528 const gboolean is_active = dlg_widget_active (panel);
3529 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3531 *redir = FALSE;
3533 /* 1st line */
3534 if (mouse_down && event->y == 1)
3536 /* "<" button */
3537 if (event->x == 2)
3539 directory_history_prev (panel);
3540 return MOU_NORMAL;
3543 /* "." button show/hide hidden files */
3544 if (event->x == panel->widget.cols - 5)
3546 panel->widget.owner->callback (panel->widget.owner, NULL,
3547 DLG_ACTION, CK_ShowHidden, NULL);
3548 repaint_screen ();
3549 return MOU_NORMAL;
3552 /* ">" button */
3553 if (event->x == panel->widget.cols - 1)
3555 directory_history_next (panel);
3556 return MOU_NORMAL;
3559 /* "^" button */
3560 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3562 directory_history_list (panel);
3563 return MOU_NORMAL;
3566 /* rest of the upper frame, the menu is invisible - call menu */
3567 if (!menubar_visible)
3569 *redir = TRUE;
3570 event->x += panel->widget.x;
3571 return the_menubar->widget.mouse (event, the_menubar);
3574 /* no other events on 1st line */
3575 return MOU_NORMAL;
3578 /* sort on clicked column; don't handle wheel events */
3579 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3581 mouse_sort_col (event, panel);
3582 return MOU_NORMAL;
3585 /* Mouse wheel events */
3586 if (mouse_down && (event->buttons & GPM_B_UP))
3588 if (is_active)
3590 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3591 prev_page (panel);
3592 else /* We are in first page */
3593 move_up (panel);
3595 return MOU_NORMAL;
3598 if (mouse_down && (event->buttons & GPM_B_DOWN))
3600 if (is_active)
3602 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3603 next_page (panel);
3604 else /* We are in last page */
3605 move_down (panel);
3607 return MOU_NORMAL;
3610 event->y -= 2;
3611 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3613 int my_index;
3615 if (!is_active)
3616 change_panel ();
3618 if (panel->top_file + event->y > panel->count)
3619 my_index = panel->count - 1;
3620 else
3622 my_index = panel->top_file + event->y - 1;
3623 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3624 my_index += llines (panel);
3626 if (my_index >= panel->count)
3627 my_index = panel->count - 1;
3630 if (my_index != panel->selected)
3632 unselect_item (panel);
3633 panel->selected = my_index;
3634 select_item (panel);
3637 /* This one is new */
3638 mark_if_marking (panel, event);
3640 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3642 if (event->y > 0 && event->y <= lines)
3643 do_enter (panel);
3645 return MOU_NORMAL;
3648 /* --------------------------------------------------------------------------------------------- */
3649 /** Mouse callback of the panel */
3651 static int
3652 panel_event (Gpm_Event * event, void *data)
3654 WPanel *panel = data;
3655 int ret;
3656 gboolean redir;
3658 ret = do_panel_event (event, panel, &redir);
3659 if (!redir)
3660 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3662 return ret;
3665 /* --------------------------------------------------------------------------------------------- */
3667 static void
3668 reload_panelized (WPanel * panel)
3670 int i, j;
3671 dir_list *list = &panel->dir;
3673 if (panel != current_panel)
3674 (void) mc_chdir (panel->cwd_vpath);
3676 for (i = 0, j = 0; i < panel->count; i++)
3678 vfs_path_t *vpath;
3680 if (list->list[i].f.marked)
3682 /* Unmark the file in advance. In case the following mc_lstat
3683 * fails we are done, else we have to mark the file again
3684 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3685 * IMO that's the best way to update the panel's summary status
3686 * -- Norbert
3688 do_file_mark (panel, i, 0);
3690 vpath = vfs_path_from_str (list->list[i].fname);
3691 if (mc_lstat (vpath, &list->list[i].st))
3692 g_free (list->list[i].fname);
3693 else
3695 if (list->list[i].f.marked)
3696 do_file_mark (panel, i, 1);
3697 if (j != i)
3698 list->list[j] = list->list[i];
3699 j++;
3701 vfs_path_free (vpath);
3703 if (j == 0)
3704 panel->count = set_zero_dir (list) ? 1 : 0;
3705 else
3706 panel->count = j;
3708 if (panel != current_panel)
3709 (void) mc_chdir (current_panel->cwd_vpath);
3712 /* --------------------------------------------------------------------------------------------- */
3714 static void
3715 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3717 gboolean free_pointer;
3718 char *my_current_file = NULL;
3720 if ((flags & UP_RELOAD) != 0)
3722 panel->is_panelized = FALSE;
3723 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3724 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3727 /* If current_file == -1 (an invalid pointer) then preserve selection */
3728 free_pointer = current_file == UP_KEEPSEL;
3730 if (free_pointer)
3732 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3733 current_file = my_current_file;
3736 if (panel->is_panelized)
3737 reload_panelized (panel);
3738 else
3739 panel_reload (panel);
3741 try_to_select (panel, current_file);
3742 panel->dirty = 1;
3744 if (free_pointer)
3745 g_free (my_current_file);
3748 /* --------------------------------------------------------------------------------------------- */
3750 static void
3751 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3753 if (get_display_type (which) == view_listing)
3755 WPanel *panel;
3757 panel = (WPanel *) get_panel_widget (which);
3758 if (panel->is_panelized)
3759 flags &= ~UP_RELOAD;
3760 update_one_panel_widget (panel, flags, current_file);
3764 /* --------------------------------------------------------------------------------------------- */
3765 /*** public functions ****************************************************************************/
3766 /* --------------------------------------------------------------------------------------------- */
3768 char *
3769 remove_encoding_from_path (const vfs_path_t * vpath)
3771 GString *ret;
3772 GString *tmp_conv;
3773 int indx;
3775 ret = g_string_new ("");
3776 tmp_conv = g_string_new ("");
3778 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
3780 const vfs_path_element_t *path_element;
3781 GIConv converter;
3783 path_element = vfs_path_get_by_index (vpath, indx);
3784 converter =
3785 path_element->encoding != NULL ?
3786 str_crt_conv_to (path_element->encoding) : str_cnv_to_term;
3787 if (converter == INVALID_CONV)
3788 converter = str_cnv_to_term;
3790 str_vfs_convert_from (converter, path_element->path, tmp_conv);
3791 g_string_append (ret, tmp_conv->str);
3792 g_string_set_size (tmp_conv, 0);
3793 str_close_conv (converter);
3795 g_string_free (tmp_conv, TRUE);
3796 return g_string_free (ret, FALSE);
3799 /* --------------------------------------------------------------------------------------------- */
3801 static void
3802 do_select (WPanel * panel, int i)
3804 if (i != panel->selected)
3806 panel->dirty = 1;
3807 panel->selected = i;
3808 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3809 if (panel->top_file < 0)
3810 panel->top_file = 0;
3814 /* --------------------------------------------------------------------------------------------- */
3816 static void
3817 do_try_to_select (WPanel * panel, const char *name)
3819 int i;
3820 char *subdir;
3822 if (!name)
3824 do_select (panel, 0);
3825 return;
3828 /* We only want the last component of the directory,
3829 * and from this only the name without suffix. */
3830 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3832 /* Search that subdirectory, if found select it */
3833 for (i = 0; i < panel->count; i++)
3835 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3837 do_select (panel, i);
3838 g_free (subdir);
3839 return;
3843 /* Try to select a file near the file that is missing */
3844 if (panel->selected >= panel->count)
3845 do_select (panel, panel->count - 1);
3846 g_free (subdir);
3849 /* --------------------------------------------------------------------------------------------- */
3851 /* event callback */
3852 static gboolean
3853 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3854 gpointer init_data, gpointer data)
3856 (void) event_group_name;
3857 (void) event_name;
3858 (void) init_data;
3859 (void) data;
3861 update_panels (UP_RELOAD, UP_KEEPSEL);
3863 return TRUE;
3866 /* --------------------------------------------------------------------------------------------- */
3868 /* event callback */
3869 static gboolean
3870 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3871 gpointer init_data, gpointer data)
3873 (void) event_group_name;
3874 (void) event_name;
3875 (void) init_data;
3876 (void) data;
3878 if (current_panel->marked == 0)
3879 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3880 (gpointer) selection (current_panel)->fname);
3881 else
3883 int i;
3884 gboolean first = TRUE;
3885 char *flist = NULL;
3887 for (i = 0; i < current_panel->count; i++)
3888 if (current_panel->dir.list[i].f.marked != 0)
3889 { /* Skip the unmarked ones */
3890 if (first)
3892 flist = g_strdup (current_panel->dir.list[i].fname);
3893 first = FALSE;
3895 else
3897 /* Add empty lines after the file */
3898 char *tmp;
3900 tmp =
3901 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3902 g_free (flist);
3903 flist = tmp;
3907 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3908 g_free (flist);
3910 return TRUE;
3913 /* --------------------------------------------------------------------------------------------- */
3914 /*** public functions ****************************************************************************/
3915 /* --------------------------------------------------------------------------------------------- */
3917 void
3918 try_to_select (WPanel * panel, const char *name)
3920 do_try_to_select (panel, name);
3921 select_item (panel);
3924 /* --------------------------------------------------------------------------------------------- */
3926 void
3927 panel_clean_dir (WPanel * panel)
3929 int count = panel->count;
3931 panel->count = 0;
3932 panel->top_file = 0;
3933 panel->selected = 0;
3934 panel->marked = 0;
3935 panel->dirs_marked = 0;
3936 panel->total = 0;
3937 panel->searching = FALSE;
3938 panel->is_panelized = FALSE;
3939 panel->dirty = 1;
3940 panel->content_shift = -1;
3941 panel->max_shift = -1;
3943 clean_dir (&panel->dir, count);
3946 /* --------------------------------------------------------------------------------------------- */
3948 * Set Up panel's current dir object
3950 * @param panel panel object
3951 * @param path_str string contain path
3954 void
3955 panel_set_cwd (WPanel * panel, const char *path_str)
3957 vfs_path_free (panel->cwd_vpath);
3958 panel->cwd_vpath = vfs_path_from_str (path_str);
3961 /* --------------------------------------------------------------------------------------------- */
3963 * Set Up panel's last working dir object
3965 * @param panel panel object
3966 * @param path_str string contain path
3969 void
3970 panel_set_lwd (WPanel * panel, const char *path_str)
3972 vfs_path_free (panel->lwd_vpath);
3973 panel->lwd_vpath = vfs_path_from_str (path_str);
3976 /* --------------------------------------------------------------------------------------------- */
3977 /** Panel creation.
3978 * @param panel_name the name of the panel for setup retieving
3979 * @returns new instance of WPanel
3982 WPanel *
3983 panel_new (const char *panel_name)
3985 return panel_new_with_dir (panel_name, NULL);
3988 /* --------------------------------------------------------------------------------------------- */
3989 /** Panel creation for specified directory.
3990 * @param panel_name specifies the name of the panel for setup retieving
3991 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3992 * @returns new instance of WPanel
3995 WPanel *
3996 panel_new_with_dir (const char *panel_name, const char *wpath)
3998 WPanel *panel;
3999 char *section;
4000 int i, err;
4001 char *curdir = NULL;
4003 panel = g_new0 (WPanel, 1);
4005 /* No know sizes of the panel at startup */
4006 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
4008 /* We do not want the cursor */
4009 widget_want_cursor (panel->widget, 0);
4011 if (wpath != NULL)
4013 curdir = _vfs_get_cwd ();
4014 panel_set_cwd (panel, wpath);
4016 else
4018 vfs_setup_cwd ();
4019 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4022 panel_set_lwd (panel, ".");
4024 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4025 /* directories history will be get later */
4027 panel->dir.list = g_new (file_entry, MIN_FILES);
4028 panel->dir.size = MIN_FILES;
4029 panel->active = 0;
4030 panel->filter = 0;
4031 panel->split = 0;
4032 panel->top_file = 0;
4033 panel->selected = 0;
4034 panel->marked = 0;
4035 panel->total = 0;
4036 panel->dirty = 1;
4037 panel->searching = FALSE;
4038 panel->dirs_marked = 0;
4039 panel->is_panelized = FALSE;
4040 panel->format = 0;
4041 panel->status_format = 0;
4042 panel->format_modified = 1;
4043 panel->content_shift = -1;
4044 panel->max_shift = -1;
4046 panel->panel_name = g_strdup (panel_name);
4047 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4049 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4051 for (i = 0; i < LIST_TYPES; i++)
4052 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4054 panel->search_buffer[0] = '\0';
4055 panel->prev_search_buffer[0] = '\0';
4056 panel->frame_size = frame_half;
4058 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4059 if (!mc_config_has_group (mc_main_config, section))
4061 g_free (section);
4062 section = g_strdup (panel->panel_name);
4064 panel_load_setup (panel, section);
4065 g_free (section);
4067 /* Load format strings */
4068 err = set_panel_formats (panel);
4069 if (err != 0)
4070 set_panel_formats (panel);
4072 #ifdef HAVE_CHARSET
4074 const vfs_path_element_t *path_element;
4076 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4077 if (path_element->encoding != NULL)
4078 panel->codepage = get_codepage_index (path_element->encoding);
4080 #endif
4082 if (mc_chdir (panel->cwd_vpath) != 0)
4084 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4085 vfs_setup_cwd ();
4086 vfs_path_free (panel->cwd_vpath);
4087 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4090 /* Load the default format */
4092 char *tmp_path;
4094 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4095 panel->count =
4096 do_load_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
4097 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4098 panel->sort_info.exec_first, panel->filter);
4099 g_free (tmp_path);
4102 /* Restore old right path */
4103 if (curdir != NULL)
4105 vfs_path_t *vpath;
4107 vpath = vfs_path_from_str (curdir);
4108 err = mc_chdir (vpath);
4109 vfs_path_free (vpath);
4111 g_free (curdir);
4113 return panel;
4116 /* --------------------------------------------------------------------------------------------- */
4118 void
4119 panel_reload (WPanel * panel)
4121 struct stat current_stat;
4122 char *tmp_path;
4123 gboolean ok;
4125 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4126 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
4127 && current_stat.st_ctime == panel->dir_stat.st_ctime
4128 && current_stat.st_mtime == panel->dir_stat.st_mtime);
4129 g_free (tmp_path);
4131 if (ok)
4132 return;
4136 char *last_slash;
4138 if (mc_chdir (panel->cwd_vpath) != -1)
4139 break;
4141 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4142 if (tmp_path[0] == PATH_SEP && tmp_path[1] == 0)
4144 panel_clean_dir (panel);
4145 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
4146 g_free (tmp_path);
4147 return;
4149 last_slash = strrchr (tmp_path, PATH_SEP);
4150 if (!last_slash || last_slash == tmp_path)
4152 vfs_path_free (panel->cwd_vpath);
4153 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4155 else
4156 *last_slash = 0;
4157 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4158 g_free (tmp_path);
4159 show_dir (panel);
4161 while (TRUE);
4163 panel->count =
4164 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4165 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4166 panel->sort_info.exec_first, panel->filter);
4168 panel->dirty = 1;
4169 if (panel->selected >= panel->count)
4170 do_select (panel, panel->count - 1);
4172 recalculate_panel_summary (panel);
4175 /* --------------------------------------------------------------------------------------------- */
4176 /* Switches the panel to the mode specified in the format */
4177 /* Seting up both format and status string. Return: 0 - on success; */
4178 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4181 set_panel_formats (WPanel * p)
4183 format_e *form;
4184 char *err = NULL;
4185 int retcode = 0;
4187 form = use_display_format (p, panel_format (p), &err, 0);
4189 if (err != NULL)
4191 g_free (err);
4192 retcode = 1;
4194 else
4196 delete_format (p->format);
4197 p->format = form;
4200 if (panels_options.show_mini_info)
4202 form = use_display_format (p, mini_status_format (p), &err, 1);
4204 if (err != NULL)
4206 g_free (err);
4207 retcode += 2;
4209 else
4211 delete_format (p->status_format);
4212 p->status_format = form;
4216 panel_format_modified (p);
4217 panel_update_cols (&(p->widget), p->frame_size);
4219 if (retcode)
4220 message (D_ERROR, _("Warning"),
4221 _("User supplied format looks invalid, reverting to default."));
4222 if (retcode & 0x01)
4224 g_free (p->user_format);
4225 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4227 if (retcode & 0x02)
4229 g_free (p->user_status_format[p->list_type]);
4230 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4233 return retcode;
4236 /* --------------------------------------------------------------------------------------------- */
4238 void
4239 panel_update_cols (Widget * widget, panel_display_t frame_size)
4241 int cols, origin;
4243 /* don't touch panel if it is not in dialog yet */
4244 /* if panel is not in dialog it is not in widgets list
4245 and cannot be compared with get_panel_widget() result */
4246 if (widget->owner == NULL)
4247 return;
4249 if (panels_layout.horizontal_split)
4251 widget->cols = COLS;
4252 return;
4255 if (frame_size == frame_full)
4257 cols = COLS;
4258 origin = 0;
4260 else if (widget == get_panel_widget (0))
4262 cols = panels_layout.left_panel_size;
4263 origin = 0;
4265 else
4267 cols = COLS - panels_layout.left_panel_size;
4268 origin = panels_layout.left_panel_size;
4271 widget->cols = cols;
4272 widget->x = origin;
4275 /* --------------------------------------------------------------------------------------------- */
4277 /* Select current item and readjust the panel */
4278 void
4279 select_item (WPanel * panel)
4281 /* Although currently all over the code we set the selection and
4282 top file to decent values before calling select_item, I could
4283 forget it someday, so it's better to do the actual fitting here */
4285 if (panel->selected < 0)
4286 panel->selected = 0;
4288 if (panel->selected > panel->count - 1)
4289 panel->selected = panel->count - 1;
4291 adjust_top_file (panel);
4293 panel->dirty = 1;
4295 execute_hooks (select_file_hook);
4298 /* --------------------------------------------------------------------------------------------- */
4299 /** Clears all files in the panel, used only when one file was marked */
4300 void
4301 unmark_files (WPanel * panel)
4303 int i;
4305 if (!panel->marked)
4306 return;
4307 for (i = 0; i < panel->count; i++)
4308 file_mark (panel, i, 0);
4310 panel->dirs_marked = 0;
4311 panel->marked = 0;
4312 panel->total = 0;
4315 /* --------------------------------------------------------------------------------------------- */
4316 /** Recalculate the panels summary information, used e.g. when marked
4317 files might have been removed by an external command */
4319 void
4320 recalculate_panel_summary (WPanel * panel)
4322 int i;
4324 panel->marked = 0;
4325 panel->dirs_marked = 0;
4326 panel->total = 0;
4328 for (i = 0; i < panel->count; i++)
4329 if (panel->dir.list[i].f.marked)
4331 /* do_file_mark will return immediately if newmark == oldmark.
4332 So we have to first unmark it to get panel's summary information
4333 updated. (Norbert) */
4334 panel->dir.list[i].f.marked = 0;
4335 do_file_mark (panel, i, 1);
4339 /* --------------------------------------------------------------------------------------------- */
4340 /** This routine marks a file or a directory */
4342 void
4343 do_file_mark (WPanel * panel, int idx, int mark)
4345 if (panel->dir.list[idx].f.marked == mark)
4346 return;
4348 /* Only '..' can't be marked, '.' isn't visible */
4349 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4350 return;
4352 file_mark (panel, idx, mark);
4353 if (panel->dir.list[idx].f.marked)
4355 panel->marked++;
4356 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4358 if (panel->dir.list[idx].f.dir_size_computed)
4359 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4360 panel->dirs_marked++;
4362 else
4363 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4364 set_colors (panel);
4366 else
4368 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4370 if (panel->dir.list[idx].f.dir_size_computed)
4371 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4372 panel->dirs_marked--;
4374 else
4375 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4376 panel->marked--;
4380 /* --------------------------------------------------------------------------------------------- */
4382 * Changes the current directory of the panel.
4383 * Record change in the directory history.
4385 gboolean
4386 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4388 gboolean r;
4390 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4391 if (r)
4393 char *tmp_path;
4395 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4396 directory_history_add (panel, tmp_path);
4397 g_free (tmp_path);
4399 return r;
4402 /* --------------------------------------------------------------------------------------------- */
4404 void
4405 file_mark (WPanel * panel, int lc_index, int val)
4407 if (panel->dir.list[lc_index].f.marked != val)
4409 panel->dir.list[lc_index].f.marked = val;
4410 panel->dirty = 1;
4414 /* --------------------------------------------------------------------------------------------- */
4416 void
4417 panel_re_sort (WPanel * panel)
4419 char *filename;
4420 int i;
4422 if (panel == NULL)
4423 return;
4425 filename = g_strdup (selection (panel)->fname);
4426 unselect_item (panel);
4427 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4428 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4429 panel->sort_info.exec_first);
4430 panel->selected = -1;
4431 for (i = panel->count; i; i--)
4433 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4435 panel->selected = i - 1;
4436 break;
4439 g_free (filename);
4440 panel->top_file = panel->selected - ITEMS (panel) / 2;
4441 select_item (panel);
4442 panel->dirty = 1;
4445 /* --------------------------------------------------------------------------------------------- */
4447 void
4448 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4450 if (sort_order == NULL)
4451 return;
4453 panel->sort_info.sort_field = sort_order;
4455 /* The directory is already sorted, we have to load the unsorted stuff */
4456 if (sort_order->sort_routine == (sortfn *) unsorted)
4458 char *current_file;
4460 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4461 panel_reload (panel);
4462 try_to_select (panel, current_file);
4463 g_free (current_file);
4465 panel_re_sort (panel);
4468 /* --------------------------------------------------------------------------------------------- */
4470 * Change panel encoding.
4471 * @param panel WPanel object
4474 void
4475 panel_change_encoding (WPanel * panel)
4477 const char *encoding = NULL;
4478 char *cd_path;
4479 #ifdef HAVE_CHARSET
4480 char *errmsg;
4481 int r;
4483 r = select_charset (-1, -1, panel->codepage, FALSE);
4485 if (r == SELECT_CHARSET_CANCEL)
4486 return; /* Cancel */
4488 panel->codepage = r;
4490 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4492 /* No translation */
4493 vfs_path_t *cd_path_vpath;
4495 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4496 cd_path = remove_encoding_from_path (panel->cwd_vpath);
4497 cd_path_vpath = vfs_path_from_str (cd_path);
4498 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4499 show_dir (panel);
4500 vfs_path_free (cd_path_vpath);
4501 g_free (cd_path);
4502 return;
4505 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4506 if (errmsg != NULL)
4508 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4509 g_free (errmsg);
4510 return;
4513 encoding = get_codepage_id (panel->codepage);
4514 #endif
4515 if (encoding != NULL)
4517 vfs_change_encoding (panel->cwd_vpath, encoding);
4519 cd_path = vfs_path_to_str (panel->cwd_vpath);
4520 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4521 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4522 g_free (cd_path);
4526 /* --------------------------------------------------------------------------------------------- */
4528 * This routine reloads the directory in both panels. It tries to
4529 * select current_file in current_panel and other_file in other_panel.
4530 * If current_file == -1 then it automatically sets current_file and
4531 * other_file to the currently selected files in the panels.
4533 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4534 * will not reload the other panel.
4537 void
4538 update_panels (panel_update_flags_t flags, const char *current_file)
4540 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4541 WPanel *panel;
4543 update_one_panel (get_current_index (), flags, current_file);
4544 if (reload_other)
4545 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4547 if (get_current_type () == view_listing)
4548 panel = (WPanel *) get_panel_widget (get_current_index ());
4549 else
4550 panel = (WPanel *) get_panel_widget (get_other_index ());
4552 if (!panel->is_panelized)
4553 (void) mc_chdir (panel->cwd_vpath);
4556 /* --------------------------------------------------------------------------------------------- */
4558 void
4559 directory_history_add (struct WPanel *panel, const char *dir)
4561 vfs_path_t *vpath;
4562 char *tmp;
4564 vpath = vfs_path_from_str (dir);
4565 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
4566 vfs_path_free (vpath);
4567 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4570 /* --------------------------------------------------------------------------------------------- */
4572 gsize
4573 panel_get_num_of_sortable_fields (void)
4575 gsize ret = 0, lc_index;
4577 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4578 if (panel_fields[lc_index].is_user_choice)
4579 ret++;
4580 return ret;
4583 /* --------------------------------------------------------------------------------------------- */
4585 const char **
4586 panel_get_sortable_fields (gsize * array_size)
4588 char **ret;
4589 gsize lc_index, i;
4591 lc_index = panel_get_num_of_sortable_fields ();
4593 ret = g_try_new0 (char *, lc_index + 1);
4594 if (ret == NULL)
4595 return NULL;
4597 if (array_size != NULL)
4598 *array_size = lc_index;
4600 lc_index = 0;
4602 for (i = 0; panel_fields[i].id != NULL; i++)
4603 if (panel_fields[i].is_user_choice)
4604 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4605 return (const char **) ret;
4608 /* --------------------------------------------------------------------------------------------- */
4610 const panel_field_t *
4611 panel_get_field_by_id (const char *name)
4613 gsize lc_index;
4614 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4615 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4616 return &panel_fields[lc_index];
4617 return NULL;
4620 /* --------------------------------------------------------------------------------------------- */
4622 const panel_field_t *
4623 panel_get_field_by_title_hotkey (const char *name)
4625 gsize lc_index;
4626 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4627 if (panel_fields[lc_index].title_hotkey != NULL &&
4628 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4629 return &panel_fields[lc_index];
4630 return NULL;
4633 /* --------------------------------------------------------------------------------------------- */
4635 const panel_field_t *
4636 panel_get_field_by_title (const char *name)
4638 gsize lc_index;
4639 gchar *title = NULL;
4641 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4643 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4644 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4646 g_free (title);
4647 return &panel_fields[lc_index];
4650 g_free (title);
4651 return NULL;
4654 /* --------------------------------------------------------------------------------------------- */
4656 gsize
4657 panel_get_num_of_user_possible_fields (void)
4659 gsize ret = 0, lc_index;
4661 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4662 if (panel_fields[lc_index].use_in_user_format)
4663 ret++;
4664 return ret;
4667 /* --------------------------------------------------------------------------------------------- */
4669 const char **
4670 panel_get_user_possible_fields (gsize * array_size)
4672 char **ret;
4673 gsize lc_index, i;
4675 lc_index = panel_get_num_of_user_possible_fields ();
4677 ret = g_try_new0 (char *, lc_index + 1);
4678 if (ret == NULL)
4679 return NULL;
4681 if (array_size != NULL)
4682 *array_size = lc_index;
4684 lc_index = 0;
4686 for (i = 0; panel_fields[i].id != NULL; i++)
4687 if (panel_fields[i].use_in_user_format)
4688 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4689 return (const char **) ret;
4692 /* --------------------------------------------------------------------------------------------- */
4694 void
4695 panel_init (void)
4697 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4698 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4700 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4701 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4702 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4703 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4704 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4705 panel_filename_scroll_left_char = mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4706 panel_filename_scroll_right_char = mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4708 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4709 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4710 panel_save_curent_file_to_clip_file, NULL, NULL);
4714 /* --------------------------------------------------------------------------------------------- */
4716 void
4717 panel_deinit (void)
4719 g_free (panel_sort_up_sign);
4720 g_free (panel_sort_down_sign);
4722 g_free (panel_hiddenfiles_sign_show);
4723 g_free (panel_hiddenfiles_sign_hide);
4724 g_free (panel_history_prev_item_sign);
4725 g_free (panel_history_next_item_sign);
4726 g_free (panel_history_show_list_sign);
4727 g_free (panel_filename_scroll_left_char);
4728 g_free (panel_filename_scroll_right_char);
4732 /* --------------------------------------------------------------------------------------------- */