Handle history of input line.
[midnight-commander.git] / src / filemanager / panel.c
bloba056e964f6b0f477f0c42178a00f0b9d5ea67326
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 =
844 str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
845 else
846 prepared_text = str_fit_to_term (txt, len, format->just_mode);
848 if (perm)
849 add_permission_string (prepared_text, format->field_len, fe, attr, color, perm - 1);
850 else
851 tty_print_string (prepared_text);
853 length += len;
855 else
857 if (attr == SELECTED || attr == MARKED_SELECTED)
858 tty_setcolor (SELECTED_COLOR);
859 else
860 tty_setcolor (NORMAL_COLOR);
861 tty_print_one_vline (TRUE);
862 length++;
866 if (length < width)
868 int y, x;
870 tty_getyx (&y, &x);
871 tty_draw_hline (y, x, ' ', width - length);
874 return res;
877 /* --------------------------------------------------------------------------------------------- */
879 static void
880 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
882 int second_column = 0;
883 int width;
884 int offset = 0;
885 char buffer[BUF_MEDIUM];
886 filename_scroll_flag_t ret_frm;
887 int ypos = 0;
888 gboolean panel_is_split = !isstatus && panel->split;
889 int fln = 0;
891 width = panel->widget.cols - 2;
893 if (panel_is_split)
895 second_column = (file_index - panel->top_file) / llines (panel);
896 width = width / 2 - 1;
898 if (second_column != 0)
900 offset = 1 + width;
901 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
902 width = panel->widget.cols - offset - 2;
906 /* Nothing to paint */
907 if (width <= 0)
908 return;
910 if (mv)
912 int pos = file_index - panel->top_file;
914 if (panel_is_split)
915 ypos = pos % llines (panel);
916 else
917 ypos = pos;
919 ypos += 2;
920 widget_move (&panel->widget, ypos, offset + 1);
923 ret_frm = format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus, &fln);
925 if (panel_is_split)
927 if (second_column)
928 tty_print_char (' ');
929 else
931 tty_setcolor (NORMAL_COLOR);
932 tty_print_one_vline (TRUE);
935 if (ret_frm != FILENAME_NOSCROLL && mv)
937 if (!panel_is_split && fln > 0)
939 if (panel->list_type == list_long)
941 offset = width - fln + 1;
942 width = fln - 1;
944 else
946 width = fln;
949 widget_move (&panel->widget, ypos, offset);
950 tty_setcolor (NORMAL_COLOR);
951 tty_print_string (panel_filename_scroll_left_char);
953 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
955 widget_move (&panel->widget, ypos, offset + 1 + width);
956 tty_setcolor (NORMAL_COLOR);
957 tty_print_string (panel_filename_scroll_right_char);
962 /* --------------------------------------------------------------------------------------------- */
964 static void
965 display_mini_info (WPanel * panel)
967 if (!panels_options.show_mini_info)
968 return;
970 widget_move (&panel->widget, llines (panel) + 3, 1);
972 if (panel->searching)
974 tty_setcolor (INPUT_COLOR);
975 tty_print_char ('/');
976 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
977 return;
980 /* Status resolves links and show them */
981 set_colors (panel);
983 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
985 char link_target[MC_MAXPATHLEN];
986 vfs_path_t *lc_link_vpath;
987 int len;
989 lc_link_vpath =
990 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
991 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
992 vfs_path_free (lc_link_vpath);
993 if (len > 0)
995 link_target[len] = 0;
996 tty_print_string ("-> ");
997 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
999 else
1000 tty_print_string (str_fit_to_term (_("<readlink failed>"),
1001 panel->widget.cols - 2, J_LEFT));
1003 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
1005 /* FIXME:
1006 * while loading directory (do_load_dir() and do_reload_dir()),
1007 * the actual stat info about ".." directory isn't got;
1008 * so just don't display incorrect info about ".." directory */
1009 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
1011 else
1012 /* Default behavior */
1013 repaint_file (panel, panel->selected, 0, STATUS, 1);
1016 /* --------------------------------------------------------------------------------------------- */
1018 static void
1019 paint_dir (WPanel * panel)
1021 int i;
1022 int color; /* Color value of the line */
1023 int items; /* Number of items */
1025 items = llines (panel) * (panel->split ? 2 : 1);
1026 /* reset max len of filename because we have the new max length for the new file list */
1027 panel->max_shift = -1;
1028 for (i = 0; i < items; i++)
1030 if (i + panel->top_file >= panel->count)
1031 color = 0;
1032 else
1034 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1035 color += (panel->selected == i + panel->top_file && panel->active);
1037 repaint_file (panel, i + panel->top_file, 1, color, 0);
1040 tty_set_normal_attrs ();
1043 /* --------------------------------------------------------------------------------------------- */
1045 static void
1046 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
1048 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
1049 int cols;
1051 if (panel->marked <= 0)
1052 return;
1054 buf = size_only ? b_bytes : buffer;
1055 cols = panel->widget.cols - 2;
1058 * This is a trick to use two ngettext() calls in one sentence.
1059 * First make "N bytes", then insert it into "X in M files".
1061 g_snprintf (b_bytes, sizeof (b_bytes),
1062 ngettext ("%s byte", "%s bytes", panel->total),
1063 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1064 if (!size_only)
1065 g_snprintf (buffer, sizeof (buffer),
1066 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1067 b_bytes, panel->marked);
1069 /* don't forget spaces around buffer content */
1070 buf = (char *) str_trunc (buf, cols - 4);
1072 if (x < 0)
1073 /* center in panel */
1074 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
1077 * y == llines (panel) + 2 for mini_info_separator
1078 * y == panel->widget.lines - 1 for panel bottom frame
1080 widget_move (&panel->widget, y, x);
1081 tty_setcolor (MARKED_COLOR);
1082 tty_printf (" %s ", buf);
1085 /* --------------------------------------------------------------------------------------------- */
1087 static void
1088 mini_info_separator (WPanel * panel)
1090 if (panels_options.show_mini_info)
1092 const int y = llines (panel) + 2;
1094 tty_setcolor (NORMAL_COLOR);
1095 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1096 ACS_HLINE, panel->widget.cols - 2);
1097 /* Status displays total marked size.
1098 * Centered in panel, full format. */
1099 display_total_marked_size (panel, y, -1, FALSE);
1103 /* --------------------------------------------------------------------------------------------- */
1105 static void
1106 show_free_space (WPanel * panel)
1108 /* Used to figure out how many free space we have */
1109 static struct my_statfs myfs_stats;
1110 /* Old current working directory for displaying free space */
1111 static char *old_cwd = NULL;
1112 char *tmp_path;
1114 /* Don't try to stat non-local fs */
1115 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1116 return;
1118 tmp_path = vfs_path_to_str (panel->cwd_vpath);
1119 if (old_cwd == NULL || strcmp (old_cwd, tmp_path) != 0)
1121 char rpath[PATH_MAX];
1123 init_my_statfs ();
1124 g_free (old_cwd);
1125 old_cwd = tmp_path;
1126 tmp_path = NULL;
1128 if (mc_realpath (old_cwd, rpath) == NULL)
1129 return;
1131 my_statfs (&myfs_stats, rpath);
1133 g_free (tmp_path);
1135 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1137 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1138 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1139 panels_options.kilobyte_si);
1140 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1141 panels_options.kilobyte_si);
1142 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1143 myfs_stats.total == 0 ? 0 :
1144 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1145 widget_move (&panel->widget, panel->widget.lines - 1,
1146 panel->widget.cols - 2 - (int) strlen (tmp));
1147 tty_setcolor (NORMAL_COLOR);
1148 tty_print_string (tmp);
1152 /* --------------------------------------------------------------------------------------------- */
1154 * Prepare path string for showing in panel's header.
1155 * Passwords will removed, also home dir will replaced by ~
1157 * @param panel WPanel object
1159 * @return newly allocated string.
1162 static char *
1163 panel_correct_path_to_show (WPanel * panel)
1165 vfs_path_t *last_vpath;
1166 const vfs_path_element_t *path_element;
1167 char *return_path;
1168 int elements_count;
1170 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1172 /* get last path element */
1173 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1176 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1177 strcmp (path_element->class->name, "extfs") == 0 ||
1178 strcmp (path_element->class->name, "tarfs") == 0))
1180 const char *archive_name;
1181 const vfs_path_element_t *prev_path_element;
1183 /* get previous path element for catching archive name */
1184 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1185 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1186 if (archive_name != NULL)
1188 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1190 else
1192 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1193 last_vpath->relative = TRUE;
1196 else
1198 last_vpath = vfs_path_new ();
1199 last_vpath->relative = TRUE;
1202 vfs_path_add_element (last_vpath, path_element);
1203 return_path =
1204 vfs_path_to_str_flags (last_vpath, 0,
1205 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1206 vfs_path_free (last_vpath);
1208 return return_path;
1211 /* --------------------------------------------------------------------------------------------- */
1213 * Get Current path element encoding
1215 * @param panel WPanel object
1217 * @return newly allocated string or NULL if path charset is same as system charset
1220 static char *
1221 panel_get_encoding_info_str (WPanel * panel)
1223 char *ret_str = NULL;
1224 const vfs_path_element_t *path_element;
1226 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1227 if (path_element->encoding != NULL)
1228 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1230 return ret_str;
1233 /* --------------------------------------------------------------------------------------------- */
1235 static void
1236 show_dir (WPanel * panel)
1238 gchar *tmp;
1239 set_colors (panel);
1240 draw_box (panel->widget.owner,
1241 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1243 if (panels_options.show_mini_info)
1245 widget_move (&panel->widget, llines (panel) + 2, 0);
1246 tty_print_alt_char (ACS_LTEE, FALSE);
1247 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1248 tty_print_alt_char (ACS_RTEE, FALSE);
1251 widget_move (&panel->widget, 0, 1);
1252 tty_print_string (panel_history_prev_item_sign);
1254 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1255 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1256 panel_history_next_item_sign);
1258 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1259 tty_print_string (tmp);
1261 g_free (tmp);
1263 widget_move (&panel->widget, 0, 3);
1265 if (panel->is_panelized)
1266 tty_printf (" %s ", _("Panelize"));
1267 else
1269 tmp = panel_get_encoding_info_str (panel);
1270 if (tmp != NULL)
1272 tty_printf ("%s", tmp);
1273 widget_move (&panel->widget, 0, 3 + strlen (tmp));
1274 g_free (tmp);
1277 if (panel->active)
1278 tty_setcolor (REVERSE_COLOR);
1280 tmp = panel_correct_path_to_show (panel);
1281 tty_printf (" %s ",
1282 str_term_trim (tmp, min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1283 g_free (tmp);
1285 if (!panels_options.show_mini_info)
1287 if (panel->marked == 0)
1289 /* Show size of curret file in the bottom of panel */
1290 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1292 char buffer[BUF_SMALL];
1294 g_snprintf (buffer, sizeof (buffer), " %s ",
1295 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1296 panels_options.kilobyte_si));
1297 tty_setcolor (NORMAL_COLOR);
1298 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1299 tty_print_string (buffer);
1302 else
1304 /* Show total size of marked files
1305 * In the bottom of panel, display size only. */
1306 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1310 show_free_space (panel);
1312 if (panel->active)
1313 tty_set_normal_attrs ();
1316 /* --------------------------------------------------------------------------------------------- */
1318 /* Returns the number of items in the given panel */
1319 static int
1320 ITEMS (WPanel * p)
1322 if (p->split)
1323 return llines (p) * 2;
1324 else
1325 return llines (p);
1328 /* --------------------------------------------------------------------------------------------- */
1330 static void
1331 adjust_top_file (WPanel * panel)
1333 int items = ITEMS (panel);
1335 if (panel->count <= items)
1337 /* If all files fit, show them all. */
1338 panel->top_file = 0;
1340 else
1342 int i;
1344 /* top_file has to be in the range [selected-items+1, selected] so that
1345 the selected file is visible.
1346 top_file should be in the range [0, count-items] so that there's
1347 no empty space wasted.
1348 Within these ranges, adjust it by as little as possible. */
1350 if (panel->top_file < 0)
1351 panel->top_file = 0;
1353 i = panel->selected - items + 1;
1354 if (panel->top_file < i)
1355 panel->top_file = i;
1357 i = panel->count - items;
1358 if (panel->top_file > i)
1359 panel->top_file = i;
1361 if (panel->top_file > panel->selected)
1362 panel->top_file = panel->selected;
1366 /* --------------------------------------------------------------------------------------------- */
1367 /** add "#enc:encodning" to end of path */
1368 /* if path end width a previous #enc:, only encoding is changed no additional
1369 * #enc: is appended
1370 * retun new string
1373 static char *
1374 panel_save_name (WPanel * panel)
1376 /* If the program is shuting down */
1377 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1378 return g_strdup (panel->panel_name);
1379 else
1380 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1383 /* --------------------------------------------------------------------------------------------- */
1385 static void
1386 directory_history_add (struct WPanel *panel, const vfs_path_t * vpath)
1388 char *tmp;
1390 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1391 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1394 /* --------------------------------------------------------------------------------------------- */
1396 /* "history_load" event handler */
1397 static gboolean
1398 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1399 gpointer init_data, gpointer data)
1401 WPanel *p = (WPanel *) init_data;
1402 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1404 (void) event_group_name;
1405 (void) event_name;
1407 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1409 if (ev->cfg != NULL)
1410 p->dir_history = history_load (ev->cfg, p->hist_name);
1411 else
1412 p->dir_history = history_get (p->hist_name);
1414 directory_history_add (p, p->cwd_vpath);
1417 return TRUE;
1420 /* --------------------------------------------------------------------------------------------- */
1422 /* "history_save" event handler */
1423 static gboolean
1424 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1425 gpointer init_data, gpointer data)
1427 WPanel *p = (WPanel *) init_data;
1429 (void) event_group_name;
1430 (void) event_name;
1432 if (p->dir_history != NULL)
1434 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1436 history_save (ev->cfg, p->hist_name, p->dir_history);
1439 return TRUE;
1442 /* --------------------------------------------------------------------------------------------- */
1444 static void
1445 panel_destroy (WPanel * p)
1447 size_t i;
1449 if (panels_options.auto_save_setup)
1451 char *name;
1453 name = panel_save_name (p);
1454 panel_save_setup (p, name);
1455 g_free (name);
1458 panel_clean_dir (p);
1460 /* clean history */
1461 if (p->dir_history != NULL)
1463 /* directory history is already saved before this moment */
1464 p->dir_history = g_list_first (p->dir_history);
1465 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1466 g_list_free (p->dir_history);
1468 g_free (p->hist_name);
1470 delete_format (p->format);
1471 delete_format (p->status_format);
1473 g_free (p->user_format);
1474 for (i = 0; i < LIST_TYPES; i++)
1475 g_free (p->user_status_format[i]);
1477 g_free (p->dir.list);
1478 g_free (p->panel_name);
1480 vfs_path_free (p->lwd_vpath);
1481 vfs_path_free (p->cwd_vpath);
1484 /* --------------------------------------------------------------------------------------------- */
1486 static void
1487 panel_format_modified (WPanel * panel)
1489 panel->format_modified = 1;
1492 /* --------------------------------------------------------------------------------------------- */
1494 static void
1495 panel_paint_sort_info (WPanel * panel)
1497 if (*panel->sort_info.sort_field->hotkey != '\0')
1499 const char *sort_sign =
1500 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1501 char *str;
1503 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1504 widget_move (&panel->widget, 1, 1);
1505 tty_print_string (str);
1506 g_free (str);
1510 /* --------------------------------------------------------------------------------------------- */
1512 static gchar *
1513 panel_get_title_without_hotkey (const char *title)
1515 char *translated_title;
1516 char *hkey;
1518 if (title == NULL)
1519 return NULL;
1520 if (title[0] == '\0')
1521 return g_strdup ("");
1523 translated_title = g_strdup (_(title));
1525 hkey = strchr (translated_title, '&');
1526 if ((hkey != NULL) && (hkey[1] != '\0'))
1527 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1529 return translated_title;
1532 /* --------------------------------------------------------------------------------------------- */
1534 static void
1535 paint_frame (WPanel * panel)
1537 int side, width;
1538 GString *format_txt;
1540 adjust_top_file (panel);
1542 widget_erase (&panel->widget);
1543 show_dir (panel);
1545 widget_move (&panel->widget, 1, 1);
1547 for (side = 0; side <= panel->split; side++)
1549 format_e *format;
1551 if (side)
1553 tty_setcolor (NORMAL_COLOR);
1554 tty_print_one_vline (TRUE);
1555 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1557 else if (panel->split)
1558 width = panel->widget.cols / 2 - 3;
1559 else
1560 width = panel->widget.cols - 2;
1562 format_txt = g_string_new ("");
1563 for (format = panel->format; format; format = format->next)
1565 if (format->string_fn)
1567 g_string_set_size (format_txt, 0);
1569 if (panel->list_type == list_long
1570 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1571 g_string_append (format_txt,
1572 panel->sort_info.reverse
1573 ? panel_sort_down_sign : panel_sort_up_sign);
1575 g_string_append (format_txt, format->title);
1576 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1578 g_string_append (format_txt, " [");
1579 g_string_append (format_txt, panel->filter);
1580 g_string_append (format_txt, "]");
1583 tty_setcolor (HEADER_COLOR);
1584 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1585 J_CENTER_LEFT));
1586 width -= format->field_len;
1588 else
1590 tty_setcolor (NORMAL_COLOR);
1591 tty_print_one_vline (TRUE);
1592 width--;
1595 g_string_free (format_txt, TRUE);
1597 if (width > 0)
1599 int y, x;
1601 tty_getyx (&y, &x);
1602 tty_draw_hline (y, x, ' ', width);
1606 if (panel->list_type != list_long)
1607 panel_paint_sort_info (panel);
1610 /* --------------------------------------------------------------------------------------------- */
1612 static const char *
1613 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1615 panel_display_t frame = frame_half;
1616 format = skip_separators (format);
1618 if (!strncmp (format, "full", 4))
1620 frame = frame_full;
1621 format += 4;
1623 else if (!strncmp (format, "half", 4))
1625 frame = frame_half;
1626 format += 4;
1629 if (!isstatus)
1631 panel->frame_size = frame;
1632 panel->split = 0;
1635 /* Now, the optional column specifier */
1636 format = skip_separators (format);
1638 if (*format == '1' || *format == '2')
1640 if (!isstatus)
1641 panel->split = *format == '2';
1642 format++;
1645 if (!isstatus)
1646 panel_update_cols (&(panel->widget), panel->frame_size);
1648 return skip_separators (format);
1651 /* Format is:
1653 all := panel_format? format
1654 panel_format := [full|half] [1|2]
1655 format := one_format_e
1656 | format , one_format_e
1658 one_format_e := just format.id [opt_size]
1659 just := [<=>]
1660 opt_size := : size [opt_expand]
1661 size := [0-9]+
1662 opt_expand := +
1666 /* --------------------------------------------------------------------------------------------- */
1668 static format_e *
1669 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1670 int *res_total_cols)
1672 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1673 int total_cols = 0; /* Used columns by the format */
1674 int set_justify; /* flag: set justification mode? */
1675 align_crt_t justify = J_LEFT; /* Which mode. */
1676 size_t i;
1678 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1680 *error = 0;
1682 if (i18n_timelength == 0)
1684 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1686 for (i = 0; panel_fields[i].id != NULL; i++)
1687 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1688 panel_fields[i].min_size = i18n_timelength;
1692 * This makes sure that the panel and mini status full/half mode
1693 * setting is equal
1695 format = parse_panel_size (panel, format, isstatus);
1697 while (*format)
1698 { /* format can be an empty string */
1699 int found = 0;
1701 darr = g_new0 (format_e, 1);
1703 /* I'm so ugly, don't look at me :-) */
1704 if (!home)
1705 home = old = darr;
1707 old->next = darr;
1708 darr->next = 0;
1709 old = darr;
1711 format = skip_separators (format);
1713 if (strchr ("<=>", *format))
1715 set_justify = 1;
1716 switch (*format)
1718 case '<':
1719 justify = J_LEFT;
1720 break;
1721 case '=':
1722 justify = J_CENTER;
1723 break;
1724 case '>':
1725 default:
1726 justify = J_RIGHT;
1727 break;
1729 format = skip_separators (format + 1);
1731 else
1732 set_justify = 0;
1734 for (i = 0; panel_fields[i].id != NULL; i++)
1736 size_t klen = strlen (panel_fields[i].id);
1738 if (strncmp (format, panel_fields[i].id, klen) != 0)
1739 continue;
1741 format += klen;
1743 darr->requested_field_len = panel_fields[i].min_size;
1744 darr->string_fn = panel_fields[i].string_fn;
1745 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1747 darr->id = panel_fields[i].id;
1748 darr->expand = panel_fields[i].expands;
1749 darr->just_mode = panel_fields[i].default_just;
1751 if (set_justify)
1753 if (IS_FIT (darr->just_mode))
1754 darr->just_mode = MAKE_FIT (justify);
1755 else
1756 darr->just_mode = justify;
1758 found = 1;
1760 format = skip_separators (format);
1762 /* If we have a size specifier */
1763 if (*format == ':')
1765 int req_length;
1767 /* If the size was specified, we don't want
1768 * auto-expansion by default
1770 darr->expand = 0;
1771 format++;
1772 req_length = atoi (format);
1773 darr->requested_field_len = req_length;
1775 format = skip_numbers (format);
1777 /* Now, if they insist on expansion */
1778 if (*format == '+')
1780 darr->expand = 1;
1781 format++;
1786 break;
1788 if (!found)
1790 char *tmp_format = g_strdup (format);
1792 int pos = min (8, strlen (format));
1793 delete_format (home);
1794 tmp_format[pos] = 0;
1795 *error =
1796 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1797 g_free (tmp_format);
1798 return 0;
1800 total_cols += darr->requested_field_len;
1803 *res_total_cols = total_cols;
1804 return home;
1807 /* --------------------------------------------------------------------------------------------- */
1809 static format_e *
1810 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1812 #define MAX_EXPAND 4
1813 int expand_top = 0; /* Max used element in expand */
1814 int usable_columns; /* Usable columns in the panel */
1815 int total_cols = 0;
1816 int i;
1817 format_e *darr, *home;
1819 if (!format)
1820 format = DEFAULT_USER_FORMAT;
1822 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1824 if (*error)
1825 return 0;
1827 panel->dirty = 1;
1829 /* Status needn't to be split */
1830 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1832 : (panel->split + 1))) - (!isstatus
1833 && panel->split);
1835 /* Look for the expandable fields and set field_len based on the requested field len */
1836 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1838 darr->field_len = darr->requested_field_len;
1839 if (darr->expand)
1840 expand_top++;
1843 /* If we used more columns than the available columns, adjust that */
1844 if (total_cols > usable_columns)
1846 int pdif, dif = total_cols - usable_columns;
1848 while (dif)
1850 pdif = dif;
1851 for (darr = home; darr; darr = darr->next)
1853 if (dif && darr->field_len - 1)
1855 darr->field_len--;
1856 dif--;
1860 /* avoid endless loop if num fields > 40 */
1861 if (pdif == dif)
1862 break;
1864 total_cols = usable_columns; /* give up, the rest should be truncated */
1867 /* Expand the available space */
1868 if ((usable_columns > total_cols) && expand_top)
1870 int spaces = (usable_columns - total_cols) / expand_top;
1871 int extra = (usable_columns - total_cols) % expand_top;
1873 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1874 if (darr->expand)
1876 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1877 i++;
1880 return home;
1883 /* --------------------------------------------------------------------------------------------- */
1884 /** Given the panel->view_type returns the format string to be parsed */
1886 static const char *
1887 panel_format (WPanel * panel)
1889 switch (panel->list_type)
1891 case list_long:
1892 return "full perm space nlink space owner space group space size space mtime space name";
1894 case list_brief:
1895 return "half 2 type name";
1897 case list_user:
1898 return panel->user_format;
1900 default:
1901 case list_full:
1902 return "half type name | size | mtime";
1906 /* --------------------------------------------------------------------------------------------- */
1908 static const char *
1909 mini_status_format (WPanel * panel)
1911 if (panel->user_mini_status)
1912 return panel->user_status_format[panel->list_type];
1914 switch (panel->list_type)
1916 case list_long:
1917 return "full perm space nlink space owner space group space size space mtime space name";
1919 case list_brief:
1920 return "half type name space bsize space perm space";
1922 case list_full:
1923 return "half type name";
1925 default:
1926 case list_user:
1927 return panel->user_format;
1931 /* */
1932 /* Panel operation commands */
1933 /* */
1935 /* --------------------------------------------------------------------------------------------- */
1936 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1938 static cb_ret_t
1939 maybe_cd (int move_up_dir)
1941 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1943 if (move_up_dir)
1945 vfs_path_t *up_dir;
1947 up_dir = vfs_path_from_str ("..");
1948 do_cd (up_dir, cd_exact);
1949 vfs_path_free (up_dir);
1950 return MSG_HANDLED;
1953 if (S_ISDIR (selection (current_panel)->st.st_mode)
1954 || link_isdir (selection (current_panel)))
1956 vfs_path_t *vpath;
1958 vpath = vfs_path_from_str (selection (current_panel)->fname);
1959 do_cd (vpath, cd_exact);
1960 vfs_path_free (vpath);
1961 return MSG_HANDLED;
1964 return MSG_NOT_HANDLED;
1967 /* --------------------------------------------------------------------------------------------- */
1969 /* if command line is empty then do 'cd ..' */
1970 static cb_ret_t
1971 force_maybe_cd (void)
1973 if (cmdline->buffer[0] == '\0')
1975 vfs_path_t *up_dir = vfs_path_from_str ("..");
1976 do_cd (up_dir, cd_exact);
1977 vfs_path_free (up_dir);
1978 return MSG_HANDLED;
1980 return MSG_NOT_HANDLED;
1983 /* --------------------------------------------------------------------------------------------- */
1985 static void
1986 unselect_item (WPanel * panel)
1988 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1991 /* --------------------------------------------------------------------------------------------- */
1993 static void
1994 move_down (WPanel * panel)
1996 if (panel->selected + 1 == panel->count)
1997 return;
1999 unselect_item (panel);
2000 panel->selected++;
2001 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
2003 /* Scroll window half screen */
2004 panel->top_file += ITEMS (panel) / 2;
2005 if (panel->top_file > panel->count - ITEMS (panel))
2006 panel->top_file = panel->count - ITEMS (panel);
2007 paint_dir (panel);
2009 select_item (panel);
2012 /* --------------------------------------------------------------------------------------------- */
2014 static void
2015 move_up (WPanel * panel)
2017 if (panel->selected == 0)
2018 return;
2020 unselect_item (panel);
2021 panel->selected--;
2022 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2024 /* Scroll window half screen */
2025 panel->top_file -= ITEMS (panel) / 2;
2026 if (panel->top_file < 0)
2027 panel->top_file = 0;
2028 paint_dir (panel);
2030 select_item (panel);
2033 /* --------------------------------------------------------------------------------------------- */
2034 /** Changes the selection by lines (may be negative) */
2036 static void
2037 move_selection (WPanel * panel, int lines)
2039 int new_pos;
2040 int adjust = 0;
2042 new_pos = panel->selected + lines;
2043 if (new_pos >= panel->count)
2044 new_pos = panel->count - 1;
2046 if (new_pos < 0)
2047 new_pos = 0;
2049 unselect_item (panel);
2050 panel->selected = new_pos;
2052 if (panel->selected - panel->top_file >= ITEMS (panel))
2054 panel->top_file += lines;
2055 adjust = 1;
2058 if (panel->selected - panel->top_file < 0)
2060 panel->top_file += lines;
2061 adjust = 1;
2064 if (adjust)
2066 if (panel->top_file > panel->selected)
2067 panel->top_file = panel->selected;
2068 if (panel->top_file < 0)
2069 panel->top_file = 0;
2070 paint_dir (panel);
2072 select_item (panel);
2075 /* --------------------------------------------------------------------------------------------- */
2077 static cb_ret_t
2078 move_left (WPanel * panel)
2080 if (panel->split)
2082 move_selection (panel, -llines (panel));
2083 return MSG_HANDLED;
2085 else
2086 return maybe_cd (1); /* cd .. */
2089 /* --------------------------------------------------------------------------------------------- */
2091 static cb_ret_t
2092 move_right (WPanel * panel)
2094 if (panel->split)
2096 move_selection (panel, llines (panel));
2097 return MSG_HANDLED;
2099 else
2100 return maybe_cd (0); /* cd (selection) */
2103 /* --------------------------------------------------------------------------------------------- */
2105 static void
2106 prev_page (WPanel * panel)
2108 int items;
2110 if (!panel->selected && !panel->top_file)
2111 return;
2112 unselect_item (panel);
2113 items = ITEMS (panel);
2114 if (panel->top_file < items)
2115 items = panel->top_file;
2116 if (!items)
2117 panel->selected = 0;
2118 else
2119 panel->selected -= items;
2120 panel->top_file -= items;
2122 select_item (panel);
2123 paint_dir (panel);
2126 /* --------------------------------------------------------------------------------------------- */
2128 static void
2129 goto_parent_dir (WPanel * panel)
2131 if (!panel->is_panelized)
2133 vfs_path_t *up_dir;
2135 up_dir = vfs_path_from_str ("..");
2136 do_cd (up_dir, cd_exact);
2137 vfs_path_free (up_dir);
2139 else
2141 char *fname = panel->dir.list[panel->selected].fname;
2142 const char *bname;
2143 vfs_path_t *dname_vpath;
2145 if (g_path_is_absolute (fname))
2146 fname = g_strdup (fname);
2147 else
2149 char *tmp_root;
2151 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2152 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2153 g_free (tmp_root);
2156 bname = x_basename (fname);
2158 if (bname == fname)
2159 dname_vpath = vfs_path_from_str (".");
2160 else
2162 char *dname;
2164 dname = g_strndup (fname, bname - fname);
2165 dname_vpath = vfs_path_from_str (dname);
2166 g_free (dname);
2169 do_cd (dname_vpath, cd_exact);
2170 try_to_select (panel, bname);
2172 vfs_path_free (dname_vpath);
2173 g_free (fname);
2177 /* --------------------------------------------------------------------------------------------- */
2179 static void
2180 next_page (WPanel * panel)
2182 int items;
2184 if (panel->selected == panel->count - 1)
2185 return;
2186 unselect_item (panel);
2187 items = ITEMS (panel);
2188 if (panel->top_file > panel->count - 2 * items)
2189 items = panel->count - items - panel->top_file;
2190 if (panel->top_file + items < 0)
2191 items = -panel->top_file;
2192 if (!items)
2193 panel->selected = panel->count - 1;
2194 else
2195 panel->selected += items;
2196 panel->top_file += items;
2198 select_item (panel);
2199 paint_dir (panel);
2202 /* --------------------------------------------------------------------------------------------- */
2204 static void
2205 goto_child_dir (WPanel * panel)
2207 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2209 vfs_path_t *vpath;
2211 vpath = vfs_path_from_str (selection (panel)->fname);
2212 do_cd (vpath, cd_exact);
2213 vfs_path_free (vpath);
2217 /* --------------------------------------------------------------------------------------------- */
2219 static void
2220 goto_top_file (WPanel * panel)
2222 unselect_item (panel);
2223 panel->selected = panel->top_file;
2224 select_item (panel);
2227 /* --------------------------------------------------------------------------------------------- */
2229 static void
2230 goto_middle_file (WPanel * panel)
2232 unselect_item (panel);
2233 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2234 select_item (panel);
2237 /* --------------------------------------------------------------------------------------------- */
2239 static void
2240 goto_bottom_file (WPanel * panel)
2242 unselect_item (panel);
2243 panel->selected = panel->top_file + ITEMS (panel) - 1;
2244 select_item (panel);
2247 /* --------------------------------------------------------------------------------------------- */
2249 static void
2250 move_home (WPanel * panel)
2252 if (panel->selected == 0)
2253 return;
2255 unselect_item (panel);
2257 if (panels_options.torben_fj_mode)
2259 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2261 if (panel->selected > middle_pos)
2263 goto_middle_file (panel);
2264 return;
2266 if (panel->selected != panel->top_file)
2268 goto_top_file (panel);
2269 return;
2273 panel->top_file = 0;
2274 panel->selected = 0;
2276 paint_dir (panel);
2277 select_item (panel);
2280 /* --------------------------------------------------------------------------------------------- */
2282 static void
2283 move_end (WPanel * panel)
2285 if (panel->selected == panel->count - 1)
2286 return;
2288 unselect_item (panel);
2290 if (panels_options.torben_fj_mode)
2292 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2294 if (panel->selected < middle_pos)
2296 goto_middle_file (panel);
2297 return;
2299 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2301 goto_bottom_file (panel);
2302 return;
2306 panel->selected = panel->count - 1;
2307 paint_dir (panel);
2308 select_item (panel);
2311 /* --------------------------------------------------------------------------------------------- */
2313 static void
2314 do_mark_file (WPanel * panel, mark_act_t do_move)
2316 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2317 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2318 move_down (panel);
2319 else if (do_move == MARK_FORCE_UP)
2320 move_up (panel);
2323 /* --------------------------------------------------------------------------------------------- */
2325 static void
2326 mark_file (WPanel * panel)
2328 do_mark_file (panel, MARK_DOWN);
2331 /* --------------------------------------------------------------------------------------------- */
2333 static void
2334 mark_file_up (WPanel * panel)
2336 do_mark_file (panel, MARK_FORCE_UP);
2339 /* --------------------------------------------------------------------------------------------- */
2341 static void
2342 mark_file_down (WPanel * panel)
2344 do_mark_file (panel, MARK_FORCE_DOWN);
2347 /* --------------------------------------------------------------------------------------------- */
2349 static void
2350 mark_file_right (WPanel * panel)
2352 int lines = llines (panel);
2354 if (state_mark < 0)
2355 state_mark = selection (panel)->f.marked ? 0 : 1;
2357 lines = min (lines, panel->count - panel->selected - 1);
2358 for (; lines != 0; lines--)
2360 do_file_mark (panel, panel->selected, state_mark);
2361 move_down (panel);
2363 do_file_mark (panel, panel->selected, state_mark);
2366 /* --------------------------------------------------------------------------------------------- */
2368 static void
2369 mark_file_left (WPanel * panel)
2371 int lines = llines (panel);
2373 if (state_mark < 0)
2374 state_mark = selection (panel)->f.marked ? 0 : 1;
2376 lines = min (lines, panel->selected + 1);
2377 for (; lines != 0; lines--)
2379 do_file_mark (panel, panel->selected, state_mark);
2380 move_up (panel);
2382 do_file_mark (panel, panel->selected, state_mark);
2385 /* --------------------------------------------------------------------------------------------- */
2386 /** Incremental search of a file name in the panel.
2387 * @param panel instance of WPanel structure
2388 * @param c_code key code
2391 static void
2392 do_search (WPanel * panel, int c_code)
2394 size_t l;
2395 int i, sel;
2396 gboolean wrapped = FALSE;
2397 char *act;
2398 mc_search_t *search;
2399 char *reg_exp, *esc_str;
2400 gboolean is_found = FALSE;
2402 l = strlen (panel->search_buffer);
2403 if (c_code == KEY_BACKSPACE)
2405 if (l != 0)
2407 act = panel->search_buffer + l;
2408 str_prev_noncomb_char (&act, panel->search_buffer);
2409 act[0] = '\0';
2411 panel->search_chpoint = 0;
2413 else
2415 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2417 panel->search_char[panel->search_chpoint] = c_code;
2418 panel->search_chpoint++;
2421 if (panel->search_chpoint > 0)
2423 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2425 case -2:
2426 return;
2427 case -1:
2428 panel->search_chpoint = 0;
2429 return;
2430 default:
2431 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2433 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2434 l += panel->search_chpoint;
2435 *(panel->search_buffer + l) = '\0';
2436 panel->search_chpoint = 0;
2442 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2443 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2444 search = mc_search_new (esc_str, -1);
2445 search->search_type = MC_SEARCH_T_GLOB;
2446 search->is_entire_line = TRUE;
2447 switch (panels_options.qsearch_mode)
2449 case QSEARCH_CASE_SENSITIVE:
2450 search->is_case_sensitive = TRUE;
2451 break;
2452 case QSEARCH_CASE_INSENSITIVE:
2453 search->is_case_sensitive = FALSE;
2454 break;
2455 default:
2456 search->is_case_sensitive = panel->sort_info.case_sensitive;
2457 break;
2459 sel = panel->selected;
2460 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2462 if (i >= panel->count)
2464 i = 0;
2465 if (wrapped)
2466 break;
2467 wrapped = TRUE;
2469 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2471 sel = i;
2472 is_found = TRUE;
2473 break;
2476 if (is_found)
2478 unselect_item (panel);
2479 panel->selected = sel;
2480 select_item (panel);
2481 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2483 else if (c_code != KEY_BACKSPACE)
2485 act = panel->search_buffer + l;
2486 str_prev_noncomb_char (&act, panel->search_buffer);
2487 act[0] = '\0';
2489 mc_search_free (search);
2490 g_free (reg_exp);
2491 g_free (esc_str);
2494 /* --------------------------------------------------------------------------------------------- */
2495 /** Start new search.
2496 * @param panel instance of WPanel structure
2499 static void
2500 start_search (WPanel * panel)
2502 if (panel->searching)
2504 if (panel->selected + 1 == panel->count)
2505 panel->selected = 0;
2506 else
2507 move_down (panel);
2509 /* in case if there was no search string we need to recall
2510 previous string, with which we ended previous searching */
2511 if (panel->search_buffer[0] == '\0')
2512 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2513 sizeof (panel->search_buffer));
2515 do_search (panel, 0);
2517 else
2519 panel->searching = TRUE;
2520 panel->search_buffer[0] = '\0';
2521 panel->search_char[0] = '\0';
2522 panel->search_chpoint = 0;
2523 display_mini_info (panel);
2524 mc_refresh ();
2528 /* --------------------------------------------------------------------------------------------- */
2530 static void
2531 stop_search (WPanel * panel)
2533 panel->searching = FALSE;
2535 /* if user had overrdied search string, we need to store it
2536 to the previous_search_buffer */
2537 if (panel->search_buffer[0] != '\0')
2538 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2539 sizeof (panel->prev_search_buffer));
2541 display_mini_info (panel);
2544 /* --------------------------------------------------------------------------------------------- */
2545 /** Return 1 if the Enter key has been processed, 0 otherwise */
2547 static int
2548 do_enter_on_file_entry (file_entry * fe)
2550 vfs_path_t *full_name_vpath;
2551 gboolean ok;
2554 * Directory or link to directory - change directory.
2555 * Try the same for the entries on which mc_lstat() has failed.
2557 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2559 vfs_path_t *fname_vpath;
2561 fname_vpath = vfs_path_from_str (fe->fname);
2562 if (!do_cd (fname_vpath, cd_exact))
2563 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2564 vfs_path_free (fname_vpath);
2565 return 1;
2568 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2570 /* Try associated command */
2571 if (regex_command (full_name_vpath, "Open") != 0)
2573 vfs_path_free (full_name_vpath);
2574 return 1;
2577 /* Check if the file is executable */
2578 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2579 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2580 vfs_path_free (full_name_vpath);
2581 if (!ok)
2582 return 0;
2584 if (confirm_execute)
2586 if (query_dialog
2587 (_("The Midnight Commander"),
2588 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2589 return 1;
2592 if (!vfs_current_is_local ())
2594 int ret;
2595 vfs_path_t *tmp_vpath;
2597 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2598 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2599 vfs_path_free (tmp_vpath);
2600 /* We took action only if the dialog was shown or the execution
2601 * was successful */
2602 return confirm_execute || (ret == 0);
2606 char *tmp = name_quote (fe->fname, 0);
2607 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2608 g_free (tmp);
2609 shell_execute (cmd, 0);
2610 g_free (cmd);
2613 #ifdef HAVE_CHARSET
2614 mc_global.source_codepage = default_source_codepage;
2615 #endif
2617 return 1;
2620 /* --------------------------------------------------------------------------------------------- */
2622 static int
2623 do_enter (WPanel * panel)
2625 return do_enter_on_file_entry (selection (panel));
2628 /* --------------------------------------------------------------------------------------------- */
2630 static void
2631 chdir_other_panel (WPanel * panel)
2633 const file_entry *entry = &panel->dir.list[panel->selected];
2635 vfs_path_t *new_dir_vpath;
2636 char *sel_entry = NULL;
2638 if (get_other_type () != view_listing)
2640 set_display_type (get_other_index (), view_listing);
2643 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2644 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2645 else
2647 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2648 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2651 change_panel ();
2652 do_cd (new_dir_vpath, cd_exact);
2653 vfs_path_free (new_dir_vpath);
2655 if (sel_entry)
2656 try_to_select (current_panel, sel_entry);
2657 change_panel ();
2659 move_down (panel);
2662 /* --------------------------------------------------------------------------------------------- */
2664 * Make the current directory of the current panel also the current
2665 * directory of the other panel. Put the other panel to the listing
2666 * mode if needed. If the current panel is panelized, the other panel
2667 * doesn't become panelized.
2670 static void
2671 panel_sync_other (const WPanel * panel)
2673 if (get_other_type () != view_listing)
2675 set_display_type (get_other_index (), view_listing);
2678 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2680 /* try to select current filename on the other panel */
2681 if (!panel->is_panelized)
2683 try_to_select (other_panel, selection (panel)->fname);
2687 /* --------------------------------------------------------------------------------------------- */
2689 static void
2690 chdir_to_readlink (WPanel * panel)
2692 vfs_path_t *new_dir_vpath;
2693 char buffer[MC_MAXPATHLEN], *p;
2694 int i;
2695 struct stat st;
2696 vfs_path_t *panel_fname_vpath;
2697 gboolean ok;
2699 if (get_other_type () != view_listing)
2700 return;
2702 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2703 return;
2705 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2706 if (i < 0)
2707 return;
2709 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2710 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2711 vfs_path_free (panel_fname_vpath);
2712 if (!ok)
2713 return;
2715 buffer[i] = 0;
2716 if (!S_ISDIR (st.st_mode))
2718 p = strrchr (buffer, PATH_SEP);
2719 if (p && !p[1])
2721 *p = 0;
2722 p = strrchr (buffer, PATH_SEP);
2724 if (!p)
2725 return;
2726 p[1] = 0;
2728 if (*buffer == PATH_SEP)
2729 new_dir_vpath = vfs_path_from_str (buffer);
2730 else
2731 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2733 change_panel ();
2734 do_cd (new_dir_vpath, cd_exact);
2735 vfs_path_free (new_dir_vpath);
2736 change_panel ();
2738 move_down (panel);
2741 /* --------------------------------------------------------------------------------------------- */
2743 static gsize
2744 panel_get_format_field_count (WPanel * panel)
2746 format_e *format;
2747 gsize lc_index;
2748 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2749 return lc_index;
2752 /* --------------------------------------------------------------------------------------------- */
2754 function return 0 if not found and REAL_INDEX+1 if found
2757 static gsize
2758 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2760 format_e *format;
2761 gsize lc_index;
2763 for (lc_index = 1, format = panel->format;
2764 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2765 if (format == NULL)
2766 lc_index = 0;
2768 return lc_index;
2771 /* --------------------------------------------------------------------------------------------- */
2773 static format_e *
2774 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2776 format_e *format;
2777 for (format = panel->format;
2778 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2779 return format;
2782 /* --------------------------------------------------------------------------------------------- */
2784 static const panel_field_t *
2785 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2787 const panel_field_t *pfield;
2788 format_e *format;
2790 format = panel_get_format_field_by_index (panel, lc_index);
2791 if (format == NULL)
2792 return NULL;
2793 pfield = panel_get_field_by_title (format->title);
2794 if (pfield == NULL)
2795 return NULL;
2796 if (pfield->sort_routine == NULL)
2797 return NULL;
2798 return pfield;
2801 /* --------------------------------------------------------------------------------------------- */
2803 static void
2804 panel_toggle_sort_order_prev (WPanel * panel)
2806 gsize lc_index, i;
2807 gchar *title;
2809 const panel_field_t *pfield = NULL;
2811 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2812 lc_index = panel_get_format_field_index_by_name (panel, title);
2813 g_free (title);
2815 if (lc_index > 1)
2817 /* search for prev sortable column in panel format */
2818 for (i = lc_index - 1;
2819 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2822 if (pfield == NULL)
2824 /* Sortable field not found. Try to search in each array */
2825 for (i = panel_get_format_field_count (panel);
2826 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2829 if (pfield != NULL)
2831 panel->sort_info.sort_field = pfield;
2832 panel_set_sort_order (panel, pfield);
2836 /* --------------------------------------------------------------------------------------------- */
2838 static void
2839 panel_toggle_sort_order_next (WPanel * panel)
2841 gsize lc_index, i;
2842 const panel_field_t *pfield = NULL;
2843 gsize format_field_count;
2844 gchar *title;
2846 format_field_count = panel_get_format_field_count (panel);
2847 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2848 lc_index = panel_get_format_field_index_by_name (panel, title);
2849 g_free (title);
2851 if (lc_index != 0 && lc_index != format_field_count)
2853 /* search for prev sortable column in panel format */
2854 for (i = lc_index;
2855 i != format_field_count
2856 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2859 if (pfield == NULL)
2861 /* Sortable field not found. Try to search in each array */
2862 for (i = 0;
2863 i != format_field_count
2864 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2867 if (pfield != NULL)
2869 panel->sort_info.sort_field = pfield;
2870 panel_set_sort_order (panel, pfield);
2874 /* --------------------------------------------------------------------------------------------- */
2876 static void
2877 panel_select_sort_order (WPanel * panel)
2879 const panel_field_t *sort_order;
2881 sort_order = sort_box (&panel->sort_info);
2882 if (sort_order != NULL)
2884 panel->sort_info.sort_field = sort_order;
2885 panel_set_sort_order (panel, sort_order);
2889 /* --------------------------------------------------------------------------------------------- */
2892 * panel_content_scroll_left:
2893 * @param panel the pointer to the panel on which we operate
2895 * scroll long filename to the left (decrement scroll pointer)
2899 static void
2900 panel_content_scroll_left (WPanel * panel)
2902 if (panel->content_shift > -1)
2904 if (panel->content_shift > panel->max_shift)
2905 panel->content_shift = panel->max_shift;
2907 panel->content_shift--;
2908 show_dir (panel);
2909 paint_dir (panel);
2914 /* --------------------------------------------------------------------------------------------- */
2917 * panel_content_scroll_right:
2918 * @param panel the pointer to the panel on which we operate
2920 * scroll long filename to the right (increment scroll pointer)
2924 static void
2925 panel_content_scroll_right (WPanel * panel)
2927 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
2929 panel->content_shift++;
2930 show_dir (panel);
2931 paint_dir (panel);
2935 /* --------------------------------------------------------------------------------------------- */
2937 static void
2938 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2940 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2942 const panel_field_t *sort_order;
2944 sort_order = panel_get_field_by_id (name);
2945 if (sort_order == NULL)
2946 return;
2947 panel->sort_info.sort_field = sort_order;
2949 else
2950 panel->sort_info.reverse = !panel->sort_info.reverse;
2952 panel_set_sort_order (panel, panel->sort_info.sort_field);
2955 /* --------------------------------------------------------------------------------------------- */
2957 * If we moved to the parent directory move the selection pointer to
2958 * the old directory name; If we leave VFS dir, remove FS specificator.
2960 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2963 static const char *
2964 get_parent_dir_name (const char *cwd, const char *lwd)
2966 size_t llen, clen;
2967 const char *p;
2969 llen = strlen (lwd);
2970 clen = strlen (cwd);
2972 if (llen <= clen)
2973 return NULL;
2975 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2977 if (p == NULL)
2979 p = strrchr (lwd, PATH_SEP);
2981 if ((p != NULL)
2982 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2983 && (clen == (size_t) (p - lwd)
2984 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2985 return (p + 1);
2987 return NULL;
2990 /* skip VFS prefix */
2991 while (--p > lwd && *p != PATH_SEP)
2993 /* get last component */
2994 while (--p > lwd && *p != PATH_SEP)
2997 /* return last component */
2998 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
3001 /* --------------------------------------------------------------------------------------------- */
3002 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3004 static void
3005 subshell_chdir (const vfs_path_t * vpath)
3007 #ifdef HAVE_SUBSHELL_SUPPORT
3008 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3009 do_subshell_chdir (vpath, FALSE, TRUE);
3010 #endif /* HAVE_SUBSHELL_SUPPORT */
3013 /* --------------------------------------------------------------------------------------------- */
3015 * Changes the current directory of the panel.
3016 * Don't record change in the directory history.
3019 static gboolean
3020 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3022 char *olddir;
3024 olddir = vfs_path_to_str (panel->cwd_vpath);
3026 /* Convert *new_path to a suitable pathname, handle ~user */
3027 if (cd_type == cd_parse_command)
3029 const vfs_path_element_t *element;
3031 element = vfs_path_get_by_index (new_dir_vpath, 0);
3032 if (strcmp (element->path, "-") == 0)
3033 new_dir_vpath = panel->lwd_vpath;
3036 if (mc_chdir (new_dir_vpath) == -1)
3038 panel_set_cwd (panel, olddir);
3039 g_free (olddir);
3040 return FALSE;
3043 /* Success: save previous directory, shutdown status of previous dir */
3044 panel_set_lwd (panel, olddir);
3045 input_free_completions (cmdline);
3047 vfs_path_free (panel->cwd_vpath);
3048 vfs_setup_cwd ();
3049 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3051 vfs_release_path (olddir);
3053 subshell_chdir (panel->cwd_vpath);
3055 /* Reload current panel */
3056 panel_clean_dir (panel);
3059 char *tmp_path;
3061 panel->count =
3062 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
3063 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3064 panel->sort_info.exec_first, panel->filter);
3065 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3066 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
3067 g_free (tmp_path);
3070 load_hint (0);
3071 panel->dirty = 1;
3072 update_xterm_title_path ();
3074 g_free (olddir);
3076 return TRUE;
3079 /* --------------------------------------------------------------------------------------------- */
3081 static void
3082 directory_history_next (WPanel * panel)
3084 GList *nextdir;
3086 nextdir = g_list_next (panel->dir_history);
3087 if (nextdir != NULL)
3089 vfs_path_t *data_vpath;
3091 data_vpath = vfs_path_from_str ((char *) nextdir->data);
3092 if (_do_panel_cd (panel, data_vpath, cd_exact))
3093 panel->dir_history = nextdir;
3094 vfs_path_free (data_vpath);
3098 /* --------------------------------------------------------------------------------------------- */
3100 static void
3101 directory_history_prev (WPanel * panel)
3103 GList *prevdir;
3105 prevdir = g_list_previous (panel->dir_history);
3107 if (prevdir != NULL)
3109 vfs_path_t *data_vpath;
3111 data_vpath = vfs_path_from_str ((char *) prevdir->data);
3112 if (_do_panel_cd (panel, data_vpath, cd_exact))
3113 panel->dir_history = prevdir;
3114 vfs_path_free (data_vpath);
3118 /* --------------------------------------------------------------------------------------------- */
3120 static void
3121 directory_history_list (WPanel * panel)
3123 char *s;
3125 s = history_show (&panel->dir_history, &panel->widget, -1);
3127 if (s != NULL)
3129 vfs_path_t *s_vpath;
3131 s_vpath = vfs_path_from_str (s);
3132 if (_do_panel_cd (panel, s_vpath, cd_exact))
3133 directory_history_add (panel, panel->cwd_vpath);
3134 else
3135 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3136 vfs_path_free (s_vpath);
3137 g_free (s);
3141 /* --------------------------------------------------------------------------------------------- */
3143 static cb_ret_t
3144 panel_execute_cmd (WPanel * panel, unsigned long command)
3146 int res = MSG_HANDLED;
3148 if (command != CK_Search)
3149 stop_search (panel);
3152 switch (command)
3154 case CK_Up:
3155 case CK_Down:
3156 case CK_Left:
3157 case CK_Right:
3158 case CK_Bottom:
3159 case CK_Top:
3160 case CK_PageDown:
3161 case CK_PageUp:
3162 /* reset state of marks flag */
3163 state_mark = -1;
3164 break;
3166 switch (command)
3168 case CK_PanelOtherCd:
3169 chdir_other_panel (panel);
3170 break;
3171 case CK_PanelOtherCdLink:
3172 chdir_to_readlink (panel);
3173 break;
3174 case CK_CopySingle:
3175 copy_cmd_local ();
3176 break;
3177 case CK_DeleteSingle:
3178 delete_cmd_local ();
3179 break;
3180 case CK_Enter:
3181 do_enter (panel);
3182 break;
3183 case CK_ViewRaw:
3184 view_raw_cmd ();
3185 break;
3186 case CK_EditNew:
3187 edit_cmd_new ();
3188 break;
3189 case CK_MoveSingle:
3190 rename_cmd_local ();
3191 break;
3192 case CK_SelectInvert:
3193 select_invert_cmd ();
3194 break;
3195 case CK_Select:
3196 select_cmd ();
3197 break;
3198 case CK_Unselect:
3199 unselect_cmd ();
3200 break;
3201 case CK_PageDown:
3202 next_page (panel);
3203 break;
3204 case CK_PageUp:
3205 prev_page (panel);
3206 break;
3207 case CK_CdChild:
3208 goto_child_dir (panel);
3209 break;
3210 case CK_CdParent:
3211 goto_parent_dir (panel);
3212 break;
3213 case CK_History:
3214 directory_history_list (panel);
3215 break;
3216 case CK_HistoryNext:
3217 directory_history_next (panel);
3218 break;
3219 case CK_HistoryPrev:
3220 directory_history_prev (panel);
3221 break;
3222 case CK_BottomOnScreen:
3223 goto_bottom_file (panel);
3224 break;
3225 case CK_MiddleOnScreen:
3226 goto_middle_file (panel);
3227 break;
3228 case CK_TopOnScreen:
3229 goto_top_file (panel);
3230 break;
3231 case CK_Mark:
3232 mark_file (panel);
3233 break;
3234 case CK_MarkUp:
3235 mark_file_up (panel);
3236 break;
3237 case CK_MarkDown:
3238 mark_file_down (panel);
3239 break;
3240 case CK_MarkLeft:
3241 mark_file_left (panel);
3242 break;
3243 case CK_MarkRight:
3244 mark_file_right (panel);
3245 break;
3246 case CK_CdParentSmart:
3247 res = force_maybe_cd ();
3248 break;
3249 case CK_Up:
3250 move_up (panel);
3251 break;
3252 case CK_Down:
3253 move_down (panel);
3254 break;
3255 case CK_Left:
3256 res = move_left (panel);
3257 break;
3258 case CK_Right:
3259 res = move_right (panel);
3260 break;
3261 case CK_Bottom:
3262 move_end (panel);
3263 break;
3264 case CK_Top:
3265 move_home (panel);
3266 break;
3267 #ifdef HAVE_CHARSET
3268 case CK_SelectCodepage:
3269 panel_change_encoding (panel);
3270 break;
3271 #endif
3272 case CK_ScrollLeft:
3273 panel_content_scroll_left (panel);
3274 break;
3275 case CK_ScrollRight:
3276 panel_content_scroll_right (panel);
3277 break;
3278 case CK_Search:
3279 start_search (panel);
3280 break;
3281 case CK_SearchStop:
3282 break;
3283 case CK_PanelOtherSync:
3284 panel_sync_other (panel);
3285 break;
3286 case CK_Sort:
3287 panel_select_sort_order (panel);
3288 break;
3289 case CK_SortPrev:
3290 panel_toggle_sort_order_prev (panel);
3291 break;
3292 case CK_SortNext:
3293 panel_toggle_sort_order_next (panel);
3294 break;
3295 case CK_SortReverse:
3296 panel->sort_info.reverse = !panel->sort_info.reverse;
3297 panel_set_sort_order (panel, panel->sort_info.sort_field);
3298 break;
3299 case CK_SortByName:
3300 panel_set_sort_type_by_id (panel, "name");
3301 break;
3302 case CK_SortByExt:
3303 panel_set_sort_type_by_id (panel, "extension");
3304 break;
3305 case CK_SortBySize:
3306 panel_set_sort_type_by_id (panel, "size");
3307 break;
3308 case CK_SortByMTime:
3309 panel_set_sort_type_by_id (panel, "mtime");
3310 break;
3312 return res;
3315 /* --------------------------------------------------------------------------------------------- */
3317 static cb_ret_t
3318 panel_key (WPanel * panel, int key)
3320 size_t i;
3322 if (is_abort_char (key))
3324 stop_search (panel);
3325 return MSG_HANDLED;
3328 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3330 do_search (panel, key);
3331 return MSG_HANDLED;
3334 for (i = 0; panel_map[i].key != 0; i++)
3335 if (key == panel_map[i].key)
3336 return panel_execute_cmd (panel, panel_map[i].command);
3338 if (panels_options.torben_fj_mode && key == ALT ('h'))
3340 goto_middle_file (panel);
3341 return MSG_HANDLED;
3344 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3346 start_search (panel);
3347 do_search (panel, key);
3348 return MSG_HANDLED;
3351 return MSG_NOT_HANDLED;
3354 /* --------------------------------------------------------------------------------------------- */
3356 static cb_ret_t
3357 panel_callback (Widget * w, widget_msg_t msg, int parm)
3359 WPanel *panel = (WPanel *) w;
3360 WButtonBar *bb;
3362 switch (msg)
3364 case WIDGET_INIT:
3365 /* subscribe to "history_load" event */
3366 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3367 /* subscribe to "history_save" event */
3368 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3369 return MSG_HANDLED;
3371 case WIDGET_DRAW:
3372 /* Repaint everything, including frame and separator */
3373 paint_frame (panel); /* including show_dir */
3374 paint_dir (panel);
3375 mini_info_separator (panel);
3376 display_mini_info (panel);
3377 panel->dirty = 0;
3378 return MSG_HANDLED;
3380 case WIDGET_FOCUS:
3381 state_mark = -1;
3382 current_panel = panel;
3383 panel->active = 1;
3384 if (mc_chdir (panel->cwd_vpath) != 0)
3386 char *cwd;
3388 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3389 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3390 cwd, unix_error_string (errno));
3391 g_free (cwd);
3393 else
3394 subshell_chdir (panel->cwd_vpath);
3396 update_xterm_title_path ();
3397 select_item (panel);
3398 show_dir (panel);
3399 paint_dir (panel);
3400 panel->dirty = 0;
3402 bb = find_buttonbar (panel->widget.owner);
3403 midnight_set_buttonbar (bb);
3404 buttonbar_redraw (bb);
3405 return MSG_HANDLED;
3407 case WIDGET_UNFOCUS:
3408 /* Janne: look at this for the multiple panel options */
3409 stop_search (panel);
3410 panel->active = 0;
3411 show_dir (panel);
3412 unselect_item (panel);
3413 return MSG_HANDLED;
3415 case WIDGET_KEY:
3416 return panel_key (panel, parm);
3418 case WIDGET_COMMAND:
3419 return panel_execute_cmd (panel, parm);
3421 case WIDGET_DESTROY:
3422 /* unsubscribe from "history_load" event */
3423 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3424 /* unsubscribe from "history_save" event */
3425 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3426 panel_destroy (panel);
3427 free_my_statfs ();
3428 return MSG_HANDLED;
3430 default:
3431 return default_proc (msg, parm);
3435 /* --------------------------------------------------------------------------------------------- */
3436 /* */
3437 /* Panel mouse events support routines */
3438 /* */
3440 static void
3441 mouse_toggle_mark (WPanel * panel)
3443 do_mark_file (panel, MARK_DONT_MOVE);
3444 mouse_marking = selection (panel)->f.marked;
3445 mouse_mark_panel = current_panel;
3448 /* --------------------------------------------------------------------------------------------- */
3450 static void
3451 mouse_set_mark (WPanel * panel)
3454 if (mouse_mark_panel == panel)
3456 if (mouse_marking && !(selection (panel)->f.marked))
3457 do_mark_file (panel, MARK_DONT_MOVE);
3458 else if (!mouse_marking && (selection (panel)->f.marked))
3459 do_mark_file (panel, MARK_DONT_MOVE);
3463 /* --------------------------------------------------------------------------------------------- */
3465 static gboolean
3466 mark_if_marking (WPanel * panel, Gpm_Event * event)
3468 if ((event->buttons & GPM_B_RIGHT) != 0)
3470 if ((event->type & GPM_DOWN) != 0)
3471 mouse_toggle_mark (panel);
3472 else
3473 mouse_set_mark (panel);
3474 return TRUE;
3476 return FALSE;
3479 /* --------------------------------------------------------------------------------------------- */
3480 /** Determine which column was clicked, and sort the panel on
3481 * that column, or reverse sort on that column if already
3482 * sorted on that column.
3485 static void
3486 mouse_sort_col (WPanel * panel, int x)
3488 int i;
3489 const char *lc_sort_name = NULL;
3490 panel_field_t *col_sort_format = NULL;
3491 format_e *format;
3492 gchar *title;
3494 for (i = 0, format = panel->format; format != NULL; format = format->next)
3496 i += format->field_len;
3497 if (x < i + 1)
3499 /* found column */
3500 lc_sort_name = format->title;
3501 break;
3505 if (lc_sort_name == NULL)
3506 return;
3508 for (i = 0; panel_fields[i].id != NULL; i++)
3510 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3511 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3513 col_sort_format = &panel_fields[i];
3514 g_free (title);
3515 break;
3517 g_free (title);
3520 if (col_sort_format == NULL)
3521 return;
3523 if (panel->sort_info.sort_field == col_sort_format)
3525 /* reverse the sort if clicked column is already the sorted column */
3526 panel->sort_info.reverse = !panel->sort_info.reverse;
3528 else
3530 /* new sort is forced to be ascending */
3531 panel->sort_info.reverse = FALSE;
3533 panel_set_sort_order (panel, col_sort_format);
3537 /* --------------------------------------------------------------------------------------------- */
3539 * Mouse callback of the panel minus repainting.
3541 static int
3542 panel_event (Gpm_Event * event, void *data)
3544 WPanel *panel = (WPanel *) data;
3545 Widget *w = (Widget *) data;
3547 const int lines = llines (panel);
3548 const gboolean is_active = dlg_widget_active (panel);
3549 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3550 Gpm_Event local;
3552 if (!mouse_global_in_widget (event, (Widget *) data))
3553 return MOU_UNHANDLED;
3555 local = mouse_get_local (event, w);
3557 /* 1st line */
3558 if (local.y == 1)
3560 /* "<" button */
3561 if (mouse_down && local.x == 2)
3563 directory_history_prev (panel);
3564 goto finish;
3567 /* ">" button */
3568 if (mouse_down && local.x == w->cols - 1)
3570 directory_history_next (panel);
3571 goto finish;
3574 /* "^" button */
3575 if (mouse_down && local.x >= w->cols - 4 && local.x <= w->cols - 2)
3577 directory_history_list (panel);
3578 /* both panels have been redrawn */
3579 return MOU_NORMAL;
3582 /* "." button show/hide hidden files */
3583 if (mouse_down && local.x == w->cols - 5)
3585 midnight_dlg->callback (midnight_dlg, NULL, DLG_ACTION, CK_ShowHidden, NULL);
3586 /* both panels have been updated */
3587 return MOU_NORMAL;
3590 /* no other events on 1st line */
3591 return MOU_UNHANDLED;
3594 /* sort on clicked column; don't handle wheel events */
3595 if (mouse_down && (local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && local.y == 2)
3597 mouse_sort_col (panel, local.x);
3598 goto finish;
3601 /* Mouse wheel events */
3602 if (mouse_down && (local.buttons & GPM_B_UP) != 0)
3604 if (is_active)
3606 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3607 prev_page (panel);
3608 else /* We are in first page */
3609 move_up (panel);
3611 goto finish;
3614 if (mouse_down && (local.buttons & GPM_B_DOWN) != 0)
3616 if (is_active)
3618 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3619 next_page (panel);
3620 else /* We are in last page */
3621 move_down (panel);
3623 goto finish;
3626 local.y -= 2;
3627 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
3629 int my_index;
3631 if (!is_active)
3632 change_panel ();
3634 if (panel->top_file + local.y > panel->count)
3635 my_index = panel->count - 1;
3636 else
3638 my_index = panel->top_file + local.y - 1;
3639 if (panel->split && (local.x > (w->cols - 2) / 2))
3640 my_index += llines (panel);
3642 if (my_index >= panel->count)
3643 my_index = panel->count - 1;
3646 if (my_index != panel->selected)
3648 unselect_item (panel);
3649 panel->selected = my_index;
3650 select_item (panel);
3653 /* This one is new */
3654 mark_if_marking (panel, &local);
3656 else if ((local.type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE) &&
3657 local.y > 0 && local.y <= lines)
3658 do_enter (panel);
3660 finish:
3661 send_message (w, WIDGET_DRAW, 0);
3662 return MOU_NORMAL;
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 * Remove encode info from last path element.
3772 vfs_path_t *
3773 remove_encoding_from_path (const vfs_path_t * vpath)
3775 vfs_path_t *ret_vpath;
3776 GString *tmp_conv;
3777 int indx;
3779 ret_vpath = vfs_path_new ();
3781 tmp_conv = g_string_new ("");
3783 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
3785 GIConv converter;
3786 vfs_path_element_t *path_element;
3788 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
3789 vfs_path_add_element (ret_vpath, path_element);
3791 if (path_element->encoding == NULL)
3793 continue;
3796 converter = str_crt_conv_to (path_element->encoding);
3797 if (converter == INVALID_CONV)
3798 continue;
3800 g_free (path_element->encoding);
3801 path_element->encoding = NULL;
3803 str_vfs_convert_from (converter, path_element->path, tmp_conv);
3805 g_free (path_element->path);
3806 path_element->path = g_strdup (tmp_conv->str);
3808 g_string_set_size (tmp_conv, 0);
3810 str_close_conv (converter);
3811 str_close_conv (path_element->dir.converter);
3812 path_element->dir.converter = INVALID_CONV;
3814 g_string_free (tmp_conv, TRUE);
3815 return ret_vpath;
3818 /* --------------------------------------------------------------------------------------------- */
3820 static void
3821 do_select (WPanel * panel, int i)
3823 if (i != panel->selected)
3825 panel->dirty = 1;
3826 panel->selected = i;
3827 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3828 if (panel->top_file < 0)
3829 panel->top_file = 0;
3833 /* --------------------------------------------------------------------------------------------- */
3835 static void
3836 do_try_to_select (WPanel * panel, const char *name)
3838 int i;
3839 char *subdir;
3841 if (!name)
3843 do_select (panel, 0);
3844 return;
3847 /* We only want the last component of the directory,
3848 * and from this only the name without suffix.
3849 * Cut prefix if the panel is not panelized */
3851 if (panel->is_panelized)
3852 subdir = vfs_strip_suffix_from_filename (name);
3853 else
3854 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3856 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3857 for (i = 0; i < panel->count; i++)
3859 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3861 do_select (panel, i);
3862 g_free (subdir);
3863 return;
3867 /* Try to select a file near the file that is missing */
3868 if (panel->selected >= panel->count)
3869 do_select (panel, panel->count - 1);
3870 g_free (subdir);
3873 /* --------------------------------------------------------------------------------------------- */
3875 /* event callback */
3876 static gboolean
3877 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3878 gpointer init_data, gpointer data)
3880 (void) event_group_name;
3881 (void) event_name;
3882 (void) init_data;
3883 (void) data;
3885 update_panels (UP_RELOAD, UP_KEEPSEL);
3887 return TRUE;
3890 /* --------------------------------------------------------------------------------------------- */
3892 /* event callback */
3893 static gboolean
3894 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3895 gpointer init_data, gpointer data)
3897 (void) event_group_name;
3898 (void) event_name;
3899 (void) init_data;
3900 (void) data;
3902 if (current_panel->marked == 0)
3903 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3904 (gpointer) selection (current_panel)->fname);
3905 else
3907 int i;
3908 gboolean first = TRUE;
3909 char *flist = NULL;
3911 for (i = 0; i < current_panel->count; i++)
3912 if (current_panel->dir.list[i].f.marked != 0)
3913 { /* Skip the unmarked ones */
3914 if (first)
3916 flist = g_strdup (current_panel->dir.list[i].fname);
3917 first = FALSE;
3919 else
3921 /* Add empty lines after the file */
3922 char *tmp;
3924 tmp =
3925 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3926 g_free (flist);
3927 flist = tmp;
3931 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3932 g_free (flist);
3934 return TRUE;
3937 /* --------------------------------------------------------------------------------------------- */
3938 /*** public functions ****************************************************************************/
3939 /* --------------------------------------------------------------------------------------------- */
3941 void
3942 try_to_select (WPanel * panel, const char *name)
3944 do_try_to_select (panel, name);
3945 select_item (panel);
3948 /* --------------------------------------------------------------------------------------------- */
3950 void
3951 panel_clean_dir (WPanel * panel)
3953 int count = panel->count;
3955 panel->count = 0;
3956 panel->top_file = 0;
3957 panel->selected = 0;
3958 panel->marked = 0;
3959 panel->dirs_marked = 0;
3960 panel->total = 0;
3961 panel->searching = FALSE;
3962 panel->is_panelized = FALSE;
3963 panel->dirty = 1;
3964 panel->content_shift = -1;
3965 panel->max_shift = -1;
3967 clean_dir (&panel->dir, count);
3970 /* --------------------------------------------------------------------------------------------- */
3972 * Set Up panel's current dir object
3974 * @param panel panel object
3975 * @param path_str string contain path
3978 void
3979 panel_set_cwd (WPanel * panel, const char *path_str)
3981 vfs_path_free (panel->cwd_vpath);
3982 panel->cwd_vpath = vfs_path_from_str (path_str);
3985 /* --------------------------------------------------------------------------------------------- */
3987 * Set Up panel's last working dir object
3989 * @param panel panel object
3990 * @param path_str string contain path
3993 void
3994 panel_set_lwd (WPanel * panel, const char *path_str)
3996 vfs_path_free (panel->lwd_vpath);
3997 panel->lwd_vpath = vfs_path_from_str (path_str);
4000 /* --------------------------------------------------------------------------------------------- */
4001 /** Panel creation.
4002 * @param panel_name the name of the panel for setup retieving
4003 * @returns new instance of WPanel
4006 WPanel *
4007 panel_new (const char *panel_name)
4009 return panel_new_with_dir (panel_name, NULL);
4012 /* --------------------------------------------------------------------------------------------- */
4013 /** Panel creation for specified directory.
4014 * @param panel_name specifies the name of the panel for setup retieving
4015 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
4016 * @returns new instance of WPanel
4019 WPanel *
4020 panel_new_with_dir (const char *panel_name, const char *wpath)
4022 WPanel *panel;
4023 char *section;
4024 int i, err;
4025 char *curdir = NULL;
4027 panel = g_new0 (WPanel, 1);
4029 /* No know sizes of the panel at startup */
4030 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
4032 /* We do not want the cursor */
4033 widget_want_cursor (panel->widget, 0);
4035 if (wpath != NULL)
4037 curdir = _vfs_get_cwd ();
4038 panel_set_cwd (panel, wpath);
4040 else
4042 vfs_setup_cwd ();
4043 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4046 panel_set_lwd (panel, ".");
4048 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4049 /* directories history will be get later */
4051 panel->dir.list = g_new (file_entry, MIN_FILES);
4052 panel->dir.size = MIN_FILES;
4053 panel->active = 0;
4054 panel->filter = 0;
4055 panel->split = 0;
4056 panel->top_file = 0;
4057 panel->selected = 0;
4058 panel->marked = 0;
4059 panel->total = 0;
4060 panel->dirty = 1;
4061 panel->searching = FALSE;
4062 panel->dirs_marked = 0;
4063 panel->is_panelized = FALSE;
4064 panel->format = 0;
4065 panel->status_format = 0;
4066 panel->format_modified = 1;
4067 panel->content_shift = -1;
4068 panel->max_shift = -1;
4070 panel->panel_name = g_strdup (panel_name);
4071 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4073 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4075 for (i = 0; i < LIST_TYPES; i++)
4076 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4078 panel->search_buffer[0] = '\0';
4079 panel->prev_search_buffer[0] = '\0';
4080 panel->frame_size = frame_half;
4082 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4083 if (!mc_config_has_group (mc_main_config, section))
4085 g_free (section);
4086 section = g_strdup (panel->panel_name);
4088 panel_load_setup (panel, section);
4089 g_free (section);
4091 /* Load format strings */
4092 err = set_panel_formats (panel);
4093 if (err != 0)
4094 set_panel_formats (panel);
4096 #ifdef HAVE_CHARSET
4098 const vfs_path_element_t *path_element;
4100 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4101 if (path_element->encoding != NULL)
4102 panel->codepage = get_codepage_index (path_element->encoding);
4104 #endif
4106 if (mc_chdir (panel->cwd_vpath) != 0)
4108 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4109 vfs_setup_cwd ();
4110 vfs_path_free (panel->cwd_vpath);
4111 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4114 /* Load the default format */
4115 panel->count =
4116 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4117 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4118 panel->sort_info.exec_first, panel->filter);
4120 /* Restore old right path */
4121 if (curdir != NULL)
4123 vfs_path_t *vpath;
4125 vpath = vfs_path_from_str (curdir);
4126 err = mc_chdir (vpath);
4127 vfs_path_free (vpath);
4129 g_free (curdir);
4131 return panel;
4134 /* --------------------------------------------------------------------------------------------- */
4136 void
4137 panel_reload (WPanel * panel)
4139 struct stat current_stat;
4140 char *tmp_path;
4141 gboolean ok;
4143 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4144 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
4145 && current_stat.st_ctime == panel->dir_stat.st_ctime
4146 && current_stat.st_mtime == panel->dir_stat.st_mtime);
4147 g_free (tmp_path);
4149 if (ok)
4150 return;
4154 char *last_slash;
4156 if (mc_chdir (panel->cwd_vpath) != -1)
4157 break;
4159 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4160 if (tmp_path[0] == PATH_SEP && tmp_path[1] == '\0')
4162 panel_clean_dir (panel);
4163 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
4164 g_free (tmp_path);
4165 return;
4167 last_slash = strrchr (tmp_path, PATH_SEP);
4168 vfs_path_free (panel->cwd_vpath);
4169 if (last_slash == NULL || last_slash == tmp_path)
4170 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4171 else
4173 *last_slash = '\0';
4174 panel->cwd_vpath = vfs_path_from_str (tmp_path);
4176 g_free (tmp_path);
4177 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4178 show_dir (panel);
4180 while (TRUE);
4182 panel->count =
4183 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4184 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4185 panel->sort_info.exec_first, panel->filter);
4187 panel->dirty = 1;
4188 if (panel->selected >= panel->count)
4189 do_select (panel, panel->count - 1);
4191 recalculate_panel_summary (panel);
4194 /* --------------------------------------------------------------------------------------------- */
4195 /* Switches the panel to the mode specified in the format */
4196 /* Seting up both format and status string. Return: 0 - on success; */
4197 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4200 set_panel_formats (WPanel * p)
4202 format_e *form;
4203 char *err = NULL;
4204 int retcode = 0;
4206 form = use_display_format (p, panel_format (p), &err, 0);
4208 if (err != NULL)
4210 g_free (err);
4211 retcode = 1;
4213 else
4215 delete_format (p->format);
4216 p->format = form;
4219 if (panels_options.show_mini_info)
4221 form = use_display_format (p, mini_status_format (p), &err, 1);
4223 if (err != NULL)
4225 g_free (err);
4226 retcode += 2;
4228 else
4230 delete_format (p->status_format);
4231 p->status_format = form;
4235 panel_format_modified (p);
4236 panel_update_cols (&(p->widget), p->frame_size);
4238 if (retcode)
4239 message (D_ERROR, _("Warning"),
4240 _("User supplied format looks invalid, reverting to default."));
4241 if (retcode & 0x01)
4243 g_free (p->user_format);
4244 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4246 if (retcode & 0x02)
4248 g_free (p->user_status_format[p->list_type]);
4249 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4252 return retcode;
4255 /* --------------------------------------------------------------------------------------------- */
4257 void
4258 panel_update_cols (Widget * widget, panel_display_t frame_size)
4260 int cols, origin;
4262 /* don't touch panel if it is not in dialog yet */
4263 /* if panel is not in dialog it is not in widgets list
4264 and cannot be compared with get_panel_widget() result */
4265 if (widget->owner == NULL)
4266 return;
4268 if (panels_layout.horizontal_split)
4270 widget->cols = COLS;
4271 return;
4274 if (frame_size == frame_full)
4276 cols = COLS;
4277 origin = 0;
4279 else if (widget == get_panel_widget (0))
4281 cols = panels_layout.left_panel_size;
4282 origin = 0;
4284 else
4286 cols = COLS - panels_layout.left_panel_size;
4287 origin = panels_layout.left_panel_size;
4290 widget->cols = cols;
4291 widget->x = origin;
4294 /* --------------------------------------------------------------------------------------------- */
4296 /* Select current item and readjust the panel */
4297 void
4298 select_item (WPanel * panel)
4300 /* Although currently all over the code we set the selection and
4301 top file to decent values before calling select_item, I could
4302 forget it someday, so it's better to do the actual fitting here */
4304 if (panel->selected < 0)
4305 panel->selected = 0;
4307 if (panel->selected > panel->count - 1)
4308 panel->selected = panel->count - 1;
4310 adjust_top_file (panel);
4312 panel->dirty = 1;
4314 execute_hooks (select_file_hook);
4317 /* --------------------------------------------------------------------------------------------- */
4318 /** Clears all files in the panel, used only when one file was marked */
4319 void
4320 unmark_files (WPanel * panel)
4322 int i;
4324 if (!panel->marked)
4325 return;
4326 for (i = 0; i < panel->count; i++)
4327 file_mark (panel, i, 0);
4329 panel->dirs_marked = 0;
4330 panel->marked = 0;
4331 panel->total = 0;
4334 /* --------------------------------------------------------------------------------------------- */
4335 /** Recalculate the panels summary information, used e.g. when marked
4336 files might have been removed by an external command */
4338 void
4339 recalculate_panel_summary (WPanel * panel)
4341 int i;
4343 panel->marked = 0;
4344 panel->dirs_marked = 0;
4345 panel->total = 0;
4347 for (i = 0; i < panel->count; i++)
4348 if (panel->dir.list[i].f.marked)
4350 /* do_file_mark will return immediately if newmark == oldmark.
4351 So we have to first unmark it to get panel's summary information
4352 updated. (Norbert) */
4353 panel->dir.list[i].f.marked = 0;
4354 do_file_mark (panel, i, 1);
4358 /* --------------------------------------------------------------------------------------------- */
4359 /** This routine marks a file or a directory */
4361 void
4362 do_file_mark (WPanel * panel, int idx, int mark)
4364 if (panel->dir.list[idx].f.marked == mark)
4365 return;
4367 /* Only '..' can't be marked, '.' isn't visible */
4368 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4369 return;
4371 file_mark (panel, idx, mark);
4372 if (panel->dir.list[idx].f.marked)
4374 panel->marked++;
4375 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4377 if (panel->dir.list[idx].f.dir_size_computed)
4378 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4379 panel->dirs_marked++;
4381 else
4382 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4383 set_colors (panel);
4385 else
4387 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4389 if (panel->dir.list[idx].f.dir_size_computed)
4390 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4391 panel->dirs_marked--;
4393 else
4394 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4395 panel->marked--;
4399 /* --------------------------------------------------------------------------------------------- */
4401 * Changes the current directory of the panel.
4402 * Record change in the directory history.
4404 gboolean
4405 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4407 gboolean r;
4409 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4410 if (r)
4411 directory_history_add (panel, panel->cwd_vpath);
4412 return r;
4415 /* --------------------------------------------------------------------------------------------- */
4417 void
4418 file_mark (WPanel * panel, int lc_index, int val)
4420 if (panel->dir.list[lc_index].f.marked != val)
4422 panel->dir.list[lc_index].f.marked = val;
4423 panel->dirty = 1;
4427 /* --------------------------------------------------------------------------------------------- */
4429 void
4430 panel_re_sort (WPanel * panel)
4432 char *filename;
4433 int i;
4435 if (panel == NULL)
4436 return;
4438 filename = g_strdup (selection (panel)->fname);
4439 unselect_item (panel);
4440 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4441 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4442 panel->sort_info.exec_first);
4443 panel->selected = -1;
4444 for (i = panel->count; i; i--)
4446 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4448 panel->selected = i - 1;
4449 break;
4452 g_free (filename);
4453 panel->top_file = panel->selected - ITEMS (panel) / 2;
4454 select_item (panel);
4455 panel->dirty = 1;
4458 /* --------------------------------------------------------------------------------------------- */
4460 void
4461 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4463 if (sort_order == NULL)
4464 return;
4466 panel->sort_info.sort_field = sort_order;
4468 /* The directory is already sorted, we have to load the unsorted stuff */
4469 if (sort_order->sort_routine == (sortfn *) unsorted)
4471 char *current_file;
4473 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4474 panel_reload (panel);
4475 try_to_select (panel, current_file);
4476 g_free (current_file);
4478 panel_re_sort (panel);
4481 /* --------------------------------------------------------------------------------------------- */
4483 * Change panel encoding.
4484 * @param panel WPanel object
4487 void
4488 panel_change_encoding (WPanel * panel)
4490 const char *encoding = NULL;
4491 #ifdef HAVE_CHARSET
4492 char *errmsg;
4493 int r;
4495 r = select_charset (-1, -1, panel->codepage, FALSE);
4497 if (r == SELECT_CHARSET_CANCEL)
4498 return; /* Cancel */
4500 panel->codepage = r;
4502 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4504 /* No translation */
4505 vfs_path_t *cd_path_vpath;
4507 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4508 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4509 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4510 show_dir (panel);
4511 vfs_path_free (cd_path_vpath);
4512 return;
4515 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4516 if (errmsg != NULL)
4518 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4519 g_free (errmsg);
4520 return;
4523 encoding = get_codepage_id (panel->codepage);
4524 #endif
4525 if (encoding != NULL)
4527 char *cd_path;
4528 vfs_change_encoding (panel->cwd_vpath, encoding);
4530 cd_path = vfs_path_to_str (panel->cwd_vpath);
4531 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4532 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4533 g_free (cd_path);
4537 /* --------------------------------------------------------------------------------------------- */
4539 * This routine reloads the directory in both panels. It tries to
4540 * select current_file in current_panel and other_file in other_panel.
4541 * If current_file == -1 then it automatically sets current_file and
4542 * other_file to the currently selected files in the panels.
4544 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4545 * will not reload the other panel.
4548 void
4549 update_panels (panel_update_flags_t flags, const char *current_file)
4551 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4552 WPanel *panel;
4554 update_one_panel (get_current_index (), flags, current_file);
4555 if (reload_other)
4556 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4558 if (get_current_type () == view_listing)
4559 panel = (WPanel *) get_panel_widget (get_current_index ());
4560 else
4561 panel = (WPanel *) get_panel_widget (get_other_index ());
4563 if (!panel->is_panelized)
4564 (void) mc_chdir (panel->cwd_vpath);
4567 /* --------------------------------------------------------------------------------------------- */
4569 gsize
4570 panel_get_num_of_sortable_fields (void)
4572 gsize ret = 0, lc_index;
4574 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4575 if (panel_fields[lc_index].is_user_choice)
4576 ret++;
4577 return ret;
4580 /* --------------------------------------------------------------------------------------------- */
4582 const char **
4583 panel_get_sortable_fields (gsize * array_size)
4585 char **ret;
4586 gsize lc_index, i;
4588 lc_index = panel_get_num_of_sortable_fields ();
4590 ret = g_try_new0 (char *, lc_index + 1);
4591 if (ret == NULL)
4592 return NULL;
4594 if (array_size != NULL)
4595 *array_size = lc_index;
4597 lc_index = 0;
4599 for (i = 0; panel_fields[i].id != NULL; i++)
4600 if (panel_fields[i].is_user_choice)
4601 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4602 return (const char **) ret;
4605 /* --------------------------------------------------------------------------------------------- */
4607 const panel_field_t *
4608 panel_get_field_by_id (const char *name)
4610 gsize lc_index;
4611 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4612 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4613 return &panel_fields[lc_index];
4614 return NULL;
4617 /* --------------------------------------------------------------------------------------------- */
4619 const panel_field_t *
4620 panel_get_field_by_title_hotkey (const char *name)
4622 gsize lc_index;
4623 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4624 if (panel_fields[lc_index].title_hotkey != NULL &&
4625 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4626 return &panel_fields[lc_index];
4627 return NULL;
4630 /* --------------------------------------------------------------------------------------------- */
4632 const panel_field_t *
4633 panel_get_field_by_title (const char *name)
4635 gsize lc_index;
4636 gchar *title = NULL;
4638 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4640 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4641 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4643 g_free (title);
4644 return &panel_fields[lc_index];
4647 g_free (title);
4648 return NULL;
4651 /* --------------------------------------------------------------------------------------------- */
4653 gsize
4654 panel_get_num_of_user_possible_fields (void)
4656 gsize ret = 0, lc_index;
4658 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4659 if (panel_fields[lc_index].use_in_user_format)
4660 ret++;
4661 return ret;
4664 /* --------------------------------------------------------------------------------------------- */
4666 const char **
4667 panel_get_user_possible_fields (gsize * array_size)
4669 char **ret;
4670 gsize lc_index, i;
4672 lc_index = panel_get_num_of_user_possible_fields ();
4674 ret = g_try_new0 (char *, lc_index + 1);
4675 if (ret == NULL)
4676 return NULL;
4678 if (array_size != NULL)
4679 *array_size = lc_index;
4681 lc_index = 0;
4683 for (i = 0; panel_fields[i].id != NULL; i++)
4684 if (panel_fields[i].use_in_user_format)
4685 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4686 return (const char **) ret;
4689 /* --------------------------------------------------------------------------------------------- */
4691 void
4692 panel_init (void)
4694 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4695 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4697 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4698 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4699 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4700 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4701 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4702 panel_filename_scroll_left_char =
4703 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4704 panel_filename_scroll_right_char =
4705 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4707 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4708 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4709 panel_save_curent_file_to_clip_file, NULL, NULL);
4713 /* --------------------------------------------------------------------------------------------- */
4715 void
4716 panel_deinit (void)
4718 g_free (panel_sort_up_sign);
4719 g_free (panel_sort_down_sign);
4721 g_free (panel_hiddenfiles_sign_show);
4722 g_free (panel_hiddenfiles_sign_hide);
4723 g_free (panel_history_prev_item_sign);
4724 g_free (panel_history_next_item_sign);
4725 g_free (panel_history_show_list_sign);
4726 g_free (panel_filename_scroll_left_char);
4727 g_free (panel_filename_scroll_right_char);
4731 /* --------------------------------------------------------------------------------------------- */