Fixed mouse handling in WPanel widget.
[midnight-commander.git] / src / filemanager / panel.c
blobec779c3b336e9e9a759a0f6f7fb0737fa4df3eaa
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 /* "history_load" event handler */
1386 static gboolean
1387 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1388 gpointer init_data, gpointer data)
1390 WPanel *p = (WPanel *) init_data;
1391 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1393 (void) event_group_name;
1394 (void) event_name;
1396 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1398 char *tmp_path;
1400 tmp_path = vfs_path_to_str (p->cwd_vpath);
1401 if (ev->cfg != NULL)
1402 p->dir_history = history_load (ev->cfg, p->hist_name);
1403 else
1404 p->dir_history = history_get (p->hist_name);
1406 directory_history_add (p, tmp_path);
1407 g_free (tmp_path);
1410 return TRUE;
1413 /* --------------------------------------------------------------------------------------------- */
1415 /* "history_save" event handler */
1416 static gboolean
1417 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1418 gpointer init_data, gpointer data)
1420 WPanel *p = (WPanel *) init_data;
1422 (void) event_group_name;
1423 (void) event_name;
1425 if (p->dir_history != NULL)
1427 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1429 history_save (ev->cfg, p->hist_name, p->dir_history);
1432 return TRUE;
1435 /* --------------------------------------------------------------------------------------------- */
1437 static void
1438 panel_destroy (WPanel * p)
1440 size_t i;
1442 if (panels_options.auto_save_setup)
1444 char *name;
1446 name = panel_save_name (p);
1447 panel_save_setup (p, name);
1448 g_free (name);
1451 panel_clean_dir (p);
1453 /* clean history */
1454 if (p->dir_history != NULL)
1456 /* directory history is already saved before this moment */
1457 p->dir_history = g_list_first (p->dir_history);
1458 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1459 g_list_free (p->dir_history);
1461 g_free (p->hist_name);
1463 delete_format (p->format);
1464 delete_format (p->status_format);
1466 g_free (p->user_format);
1467 for (i = 0; i < LIST_TYPES; i++)
1468 g_free (p->user_status_format[i]);
1470 g_free (p->dir.list);
1471 g_free (p->panel_name);
1473 vfs_path_free (p->lwd_vpath);
1474 vfs_path_free (p->cwd_vpath);
1477 /* --------------------------------------------------------------------------------------------- */
1479 static void
1480 panel_format_modified (WPanel * panel)
1482 panel->format_modified = 1;
1485 /* --------------------------------------------------------------------------------------------- */
1487 static void
1488 panel_paint_sort_info (WPanel * panel)
1490 if (*panel->sort_info.sort_field->hotkey != '\0')
1492 const char *sort_sign =
1493 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1494 char *str;
1496 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1497 widget_move (&panel->widget, 1, 1);
1498 tty_print_string (str);
1499 g_free (str);
1503 /* --------------------------------------------------------------------------------------------- */
1505 static gchar *
1506 panel_get_title_without_hotkey (const char *title)
1508 char *translated_title;
1509 char *hkey;
1511 if (title == NULL)
1512 return NULL;
1513 if (title[0] == '\0')
1514 return g_strdup ("");
1516 translated_title = g_strdup (_(title));
1518 hkey = strchr (translated_title, '&');
1519 if ((hkey != NULL) && (hkey[1] != '\0'))
1520 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1522 return translated_title;
1525 /* --------------------------------------------------------------------------------------------- */
1527 static void
1528 paint_frame (WPanel * panel)
1530 int side, width;
1531 GString *format_txt;
1533 adjust_top_file (panel);
1535 widget_erase (&panel->widget);
1536 show_dir (panel);
1538 widget_move (&panel->widget, 1, 1);
1540 for (side = 0; side <= panel->split; side++)
1542 format_e *format;
1544 if (side)
1546 tty_setcolor (NORMAL_COLOR);
1547 tty_print_one_vline (TRUE);
1548 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1550 else if (panel->split)
1551 width = panel->widget.cols / 2 - 3;
1552 else
1553 width = panel->widget.cols - 2;
1555 format_txt = g_string_new ("");
1556 for (format = panel->format; format; format = format->next)
1558 if (format->string_fn)
1560 g_string_set_size (format_txt, 0);
1562 if (panel->list_type == list_long
1563 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1564 g_string_append (format_txt,
1565 panel->sort_info.reverse
1566 ? panel_sort_down_sign : panel_sort_up_sign);
1568 g_string_append (format_txt, format->title);
1569 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1571 g_string_append (format_txt, " [");
1572 g_string_append (format_txt, panel->filter);
1573 g_string_append (format_txt, "]");
1576 tty_setcolor (HEADER_COLOR);
1577 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1578 J_CENTER_LEFT));
1579 width -= format->field_len;
1581 else
1583 tty_setcolor (NORMAL_COLOR);
1584 tty_print_one_vline (TRUE);
1585 width--;
1588 g_string_free (format_txt, TRUE);
1590 if (width > 0)
1592 int y, x;
1594 tty_getyx (&y, &x);
1595 tty_draw_hline (y, x, ' ', width);
1599 if (panel->list_type != list_long)
1600 panel_paint_sort_info (panel);
1603 /* --------------------------------------------------------------------------------------------- */
1605 static const char *
1606 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1608 panel_display_t frame = frame_half;
1609 format = skip_separators (format);
1611 if (!strncmp (format, "full", 4))
1613 frame = frame_full;
1614 format += 4;
1616 else if (!strncmp (format, "half", 4))
1618 frame = frame_half;
1619 format += 4;
1622 if (!isstatus)
1624 panel->frame_size = frame;
1625 panel->split = 0;
1628 /* Now, the optional column specifier */
1629 format = skip_separators (format);
1631 if (*format == '1' || *format == '2')
1633 if (!isstatus)
1634 panel->split = *format == '2';
1635 format++;
1638 if (!isstatus)
1639 panel_update_cols (&(panel->widget), panel->frame_size);
1641 return skip_separators (format);
1644 /* Format is:
1646 all := panel_format? format
1647 panel_format := [full|half] [1|2]
1648 format := one_format_e
1649 | format , one_format_e
1651 one_format_e := just format.id [opt_size]
1652 just := [<=>]
1653 opt_size := : size [opt_expand]
1654 size := [0-9]+
1655 opt_expand := +
1659 /* --------------------------------------------------------------------------------------------- */
1661 static format_e *
1662 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1663 int *res_total_cols)
1665 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1666 int total_cols = 0; /* Used columns by the format */
1667 int set_justify; /* flag: set justification mode? */
1668 align_crt_t justify = J_LEFT; /* Which mode. */
1669 size_t i;
1671 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1673 *error = 0;
1675 if (i18n_timelength == 0)
1677 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1679 for (i = 0; panel_fields[i].id != NULL; i++)
1680 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1681 panel_fields[i].min_size = i18n_timelength;
1685 * This makes sure that the panel and mini status full/half mode
1686 * setting is equal
1688 format = parse_panel_size (panel, format, isstatus);
1690 while (*format)
1691 { /* format can be an empty string */
1692 int found = 0;
1694 darr = g_new0 (format_e, 1);
1696 /* I'm so ugly, don't look at me :-) */
1697 if (!home)
1698 home = old = darr;
1700 old->next = darr;
1701 darr->next = 0;
1702 old = darr;
1704 format = skip_separators (format);
1706 if (strchr ("<=>", *format))
1708 set_justify = 1;
1709 switch (*format)
1711 case '<':
1712 justify = J_LEFT;
1713 break;
1714 case '=':
1715 justify = J_CENTER;
1716 break;
1717 case '>':
1718 default:
1719 justify = J_RIGHT;
1720 break;
1722 format = skip_separators (format + 1);
1724 else
1725 set_justify = 0;
1727 for (i = 0; panel_fields[i].id != NULL; i++)
1729 size_t klen = strlen (panel_fields[i].id);
1731 if (strncmp (format, panel_fields[i].id, klen) != 0)
1732 continue;
1734 format += klen;
1736 darr->requested_field_len = panel_fields[i].min_size;
1737 darr->string_fn = panel_fields[i].string_fn;
1738 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1740 darr->id = panel_fields[i].id;
1741 darr->expand = panel_fields[i].expands;
1742 darr->just_mode = panel_fields[i].default_just;
1744 if (set_justify)
1746 if (IS_FIT (darr->just_mode))
1747 darr->just_mode = MAKE_FIT (justify);
1748 else
1749 darr->just_mode = justify;
1751 found = 1;
1753 format = skip_separators (format);
1755 /* If we have a size specifier */
1756 if (*format == ':')
1758 int req_length;
1760 /* If the size was specified, we don't want
1761 * auto-expansion by default
1763 darr->expand = 0;
1764 format++;
1765 req_length = atoi (format);
1766 darr->requested_field_len = req_length;
1768 format = skip_numbers (format);
1770 /* Now, if they insist on expansion */
1771 if (*format == '+')
1773 darr->expand = 1;
1774 format++;
1779 break;
1781 if (!found)
1783 char *tmp_format = g_strdup (format);
1785 int pos = min (8, strlen (format));
1786 delete_format (home);
1787 tmp_format[pos] = 0;
1788 *error =
1789 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1790 g_free (tmp_format);
1791 return 0;
1793 total_cols += darr->requested_field_len;
1796 *res_total_cols = total_cols;
1797 return home;
1800 /* --------------------------------------------------------------------------------------------- */
1802 static format_e *
1803 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1805 #define MAX_EXPAND 4
1806 int expand_top = 0; /* Max used element in expand */
1807 int usable_columns; /* Usable columns in the panel */
1808 int total_cols = 0;
1809 int i;
1810 format_e *darr, *home;
1812 if (!format)
1813 format = DEFAULT_USER_FORMAT;
1815 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1817 if (*error)
1818 return 0;
1820 panel->dirty = 1;
1822 /* Status needn't to be split */
1823 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1825 : (panel->split + 1))) - (!isstatus
1826 && panel->split);
1828 /* Look for the expandable fields and set field_len based on the requested field len */
1829 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1831 darr->field_len = darr->requested_field_len;
1832 if (darr->expand)
1833 expand_top++;
1836 /* If we used more columns than the available columns, adjust that */
1837 if (total_cols > usable_columns)
1839 int pdif, dif = total_cols - usable_columns;
1841 while (dif)
1843 pdif = dif;
1844 for (darr = home; darr; darr = darr->next)
1846 if (dif && darr->field_len - 1)
1848 darr->field_len--;
1849 dif--;
1853 /* avoid endless loop if num fields > 40 */
1854 if (pdif == dif)
1855 break;
1857 total_cols = usable_columns; /* give up, the rest should be truncated */
1860 /* Expand the available space */
1861 if ((usable_columns > total_cols) && expand_top)
1863 int spaces = (usable_columns - total_cols) / expand_top;
1864 int extra = (usable_columns - total_cols) % expand_top;
1866 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1867 if (darr->expand)
1869 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1870 i++;
1873 return home;
1876 /* --------------------------------------------------------------------------------------------- */
1877 /** Given the panel->view_type returns the format string to be parsed */
1879 static const char *
1880 panel_format (WPanel * panel)
1882 switch (panel->list_type)
1884 case list_long:
1885 return "full perm space nlink space owner space group space size space mtime space name";
1887 case list_brief:
1888 return "half 2 type name";
1890 case list_user:
1891 return panel->user_format;
1893 default:
1894 case list_full:
1895 return "half type name | size | mtime";
1899 /* --------------------------------------------------------------------------------------------- */
1901 static const char *
1902 mini_status_format (WPanel * panel)
1904 if (panel->user_mini_status)
1905 return panel->user_status_format[panel->list_type];
1907 switch (panel->list_type)
1909 case list_long:
1910 return "full perm space nlink space owner space group space size space mtime space name";
1912 case list_brief:
1913 return "half type name space bsize space perm space";
1915 case list_full:
1916 return "half type name";
1918 default:
1919 case list_user:
1920 return panel->user_format;
1924 /* */
1925 /* Panel operation commands */
1926 /* */
1928 /* --------------------------------------------------------------------------------------------- */
1929 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1931 static cb_ret_t
1932 maybe_cd (int move_up_dir)
1934 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1936 if (move_up_dir)
1938 vfs_path_t *up_dir;
1940 up_dir = vfs_path_from_str ("..");
1941 do_cd (up_dir, cd_exact);
1942 vfs_path_free (up_dir);
1943 return MSG_HANDLED;
1946 if (S_ISDIR (selection (current_panel)->st.st_mode)
1947 || link_isdir (selection (current_panel)))
1949 vfs_path_t *vpath;
1951 vpath = vfs_path_from_str (selection (current_panel)->fname);
1952 do_cd (vpath, cd_exact);
1953 vfs_path_free (vpath);
1954 return MSG_HANDLED;
1957 return MSG_NOT_HANDLED;
1960 /* --------------------------------------------------------------------------------------------- */
1962 /* if command line is empty then do 'cd ..' */
1963 static cb_ret_t
1964 force_maybe_cd (void)
1966 if (cmdline->buffer[0] == '\0')
1968 vfs_path_t *up_dir = vfs_path_from_str ("..");
1969 do_cd (up_dir, cd_exact);
1970 vfs_path_free (up_dir);
1971 return MSG_HANDLED;
1973 return MSG_NOT_HANDLED;
1976 /* --------------------------------------------------------------------------------------------- */
1978 static void
1979 unselect_item (WPanel * panel)
1981 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1984 /* --------------------------------------------------------------------------------------------- */
1986 static void
1987 move_down (WPanel * panel)
1989 if (panel->selected + 1 == panel->count)
1990 return;
1992 unselect_item (panel);
1993 panel->selected++;
1994 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1996 /* Scroll window half screen */
1997 panel->top_file += ITEMS (panel) / 2;
1998 if (panel->top_file > panel->count - ITEMS (panel))
1999 panel->top_file = panel->count - ITEMS (panel);
2000 paint_dir (panel);
2002 select_item (panel);
2005 /* --------------------------------------------------------------------------------------------- */
2007 static void
2008 move_up (WPanel * panel)
2010 if (panel->selected == 0)
2011 return;
2013 unselect_item (panel);
2014 panel->selected--;
2015 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2017 /* Scroll window half screen */
2018 panel->top_file -= ITEMS (panel) / 2;
2019 if (panel->top_file < 0)
2020 panel->top_file = 0;
2021 paint_dir (panel);
2023 select_item (panel);
2026 /* --------------------------------------------------------------------------------------------- */
2027 /** Changes the selection by lines (may be negative) */
2029 static void
2030 move_selection (WPanel * panel, int lines)
2032 int new_pos;
2033 int adjust = 0;
2035 new_pos = panel->selected + lines;
2036 if (new_pos >= panel->count)
2037 new_pos = panel->count - 1;
2039 if (new_pos < 0)
2040 new_pos = 0;
2042 unselect_item (panel);
2043 panel->selected = new_pos;
2045 if (panel->selected - panel->top_file >= ITEMS (panel))
2047 panel->top_file += lines;
2048 adjust = 1;
2051 if (panel->selected - panel->top_file < 0)
2053 panel->top_file += lines;
2054 adjust = 1;
2057 if (adjust)
2059 if (panel->top_file > panel->selected)
2060 panel->top_file = panel->selected;
2061 if (panel->top_file < 0)
2062 panel->top_file = 0;
2063 paint_dir (panel);
2065 select_item (panel);
2068 /* --------------------------------------------------------------------------------------------- */
2070 static cb_ret_t
2071 move_left (WPanel * panel)
2073 if (panel->split)
2075 move_selection (panel, -llines (panel));
2076 return MSG_HANDLED;
2078 else
2079 return maybe_cd (1); /* cd .. */
2082 /* --------------------------------------------------------------------------------------------- */
2084 static cb_ret_t
2085 move_right (WPanel * panel)
2087 if (panel->split)
2089 move_selection (panel, llines (panel));
2090 return MSG_HANDLED;
2092 else
2093 return maybe_cd (0); /* cd (selection) */
2096 /* --------------------------------------------------------------------------------------------- */
2098 static void
2099 prev_page (WPanel * panel)
2101 int items;
2103 if (!panel->selected && !panel->top_file)
2104 return;
2105 unselect_item (panel);
2106 items = ITEMS (panel);
2107 if (panel->top_file < items)
2108 items = panel->top_file;
2109 if (!items)
2110 panel->selected = 0;
2111 else
2112 panel->selected -= items;
2113 panel->top_file -= items;
2115 select_item (panel);
2116 paint_dir (panel);
2119 /* --------------------------------------------------------------------------------------------- */
2121 static void
2122 goto_parent_dir (WPanel * panel)
2124 if (!panel->is_panelized)
2126 vfs_path_t *up_dir;
2128 up_dir = vfs_path_from_str ("..");
2129 do_cd (up_dir, cd_exact);
2130 vfs_path_free (up_dir);
2132 else
2134 char *fname = panel->dir.list[panel->selected].fname;
2135 const char *bname;
2136 vfs_path_t *dname_vpath;
2138 if (g_path_is_absolute (fname))
2139 fname = g_strdup (fname);
2140 else
2142 char *tmp_root;
2144 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2145 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2146 g_free (tmp_root);
2149 bname = x_basename (fname);
2151 if (bname == fname)
2152 dname_vpath = vfs_path_from_str (".");
2153 else
2155 char *dname;
2157 dname = g_strndup (fname, bname - fname);
2158 dname_vpath = vfs_path_from_str (dname);
2159 g_free (dname);
2162 do_cd (dname_vpath, cd_exact);
2163 try_to_select (panel, bname);
2165 vfs_path_free (dname_vpath);
2166 g_free (fname);
2170 /* --------------------------------------------------------------------------------------------- */
2172 static void
2173 next_page (WPanel * panel)
2175 int items;
2177 if (panel->selected == panel->count - 1)
2178 return;
2179 unselect_item (panel);
2180 items = ITEMS (panel);
2181 if (panel->top_file > panel->count - 2 * items)
2182 items = panel->count - items - panel->top_file;
2183 if (panel->top_file + items < 0)
2184 items = -panel->top_file;
2185 if (!items)
2186 panel->selected = panel->count - 1;
2187 else
2188 panel->selected += items;
2189 panel->top_file += items;
2191 select_item (panel);
2192 paint_dir (panel);
2195 /* --------------------------------------------------------------------------------------------- */
2197 static void
2198 goto_child_dir (WPanel * panel)
2200 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2202 vfs_path_t *vpath;
2204 vpath = vfs_path_from_str (selection (panel)->fname);
2205 do_cd (vpath, cd_exact);
2206 vfs_path_free (vpath);
2210 /* --------------------------------------------------------------------------------------------- */
2212 static void
2213 goto_top_file (WPanel * panel)
2215 unselect_item (panel);
2216 panel->selected = panel->top_file;
2217 select_item (panel);
2220 /* --------------------------------------------------------------------------------------------- */
2222 static void
2223 goto_middle_file (WPanel * panel)
2225 unselect_item (panel);
2226 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2227 select_item (panel);
2230 /* --------------------------------------------------------------------------------------------- */
2232 static void
2233 goto_bottom_file (WPanel * panel)
2235 unselect_item (panel);
2236 panel->selected = panel->top_file + ITEMS (panel) - 1;
2237 select_item (panel);
2240 /* --------------------------------------------------------------------------------------------- */
2242 static void
2243 move_home (WPanel * panel)
2245 if (panel->selected == 0)
2246 return;
2248 unselect_item (panel);
2250 if (panels_options.torben_fj_mode)
2252 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2254 if (panel->selected > middle_pos)
2256 goto_middle_file (panel);
2257 return;
2259 if (panel->selected != panel->top_file)
2261 goto_top_file (panel);
2262 return;
2266 panel->top_file = 0;
2267 panel->selected = 0;
2269 paint_dir (panel);
2270 select_item (panel);
2273 /* --------------------------------------------------------------------------------------------- */
2275 static void
2276 move_end (WPanel * panel)
2278 if (panel->selected == panel->count - 1)
2279 return;
2281 unselect_item (panel);
2283 if (panels_options.torben_fj_mode)
2285 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2287 if (panel->selected < middle_pos)
2289 goto_middle_file (panel);
2290 return;
2292 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2294 goto_bottom_file (panel);
2295 return;
2299 panel->selected = panel->count - 1;
2300 paint_dir (panel);
2301 select_item (panel);
2304 /* --------------------------------------------------------------------------------------------- */
2306 static void
2307 do_mark_file (WPanel * panel, mark_act_t do_move)
2309 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2310 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2311 move_down (panel);
2312 else if (do_move == MARK_FORCE_UP)
2313 move_up (panel);
2316 /* --------------------------------------------------------------------------------------------- */
2318 static void
2319 mark_file (WPanel * panel)
2321 do_mark_file (panel, MARK_DOWN);
2324 /* --------------------------------------------------------------------------------------------- */
2326 static void
2327 mark_file_up (WPanel * panel)
2329 do_mark_file (panel, MARK_FORCE_UP);
2332 /* --------------------------------------------------------------------------------------------- */
2334 static void
2335 mark_file_down (WPanel * panel)
2337 do_mark_file (panel, MARK_FORCE_DOWN);
2340 /* --------------------------------------------------------------------------------------------- */
2342 static void
2343 mark_file_right (WPanel * panel)
2345 int lines = llines (panel);
2347 if (state_mark < 0)
2348 state_mark = selection (panel)->f.marked ? 0 : 1;
2350 lines = min (lines, panel->count - panel->selected - 1);
2351 for (; lines != 0; lines--)
2353 do_file_mark (panel, panel->selected, state_mark);
2354 move_down (panel);
2356 do_file_mark (panel, panel->selected, state_mark);
2359 /* --------------------------------------------------------------------------------------------- */
2361 static void
2362 mark_file_left (WPanel * panel)
2364 int lines = llines (panel);
2366 if (state_mark < 0)
2367 state_mark = selection (panel)->f.marked ? 0 : 1;
2369 lines = min (lines, panel->selected + 1);
2370 for (; lines != 0; lines--)
2372 do_file_mark (panel, panel->selected, state_mark);
2373 move_up (panel);
2375 do_file_mark (panel, panel->selected, state_mark);
2378 /* --------------------------------------------------------------------------------------------- */
2379 /** Incremental search of a file name in the panel.
2380 * @param panel instance of WPanel structure
2381 * @param c_code key code
2384 static void
2385 do_search (WPanel * panel, int c_code)
2387 size_t l;
2388 int i, sel;
2389 gboolean wrapped = FALSE;
2390 char *act;
2391 mc_search_t *search;
2392 char *reg_exp, *esc_str;
2393 gboolean is_found = FALSE;
2395 l = strlen (panel->search_buffer);
2396 if (c_code == KEY_BACKSPACE)
2398 if (l != 0)
2400 act = panel->search_buffer + l;
2401 str_prev_noncomb_char (&act, panel->search_buffer);
2402 act[0] = '\0';
2404 panel->search_chpoint = 0;
2406 else
2408 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2410 panel->search_char[panel->search_chpoint] = c_code;
2411 panel->search_chpoint++;
2414 if (panel->search_chpoint > 0)
2416 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2418 case -2:
2419 return;
2420 case -1:
2421 panel->search_chpoint = 0;
2422 return;
2423 default:
2424 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2426 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2427 l += panel->search_chpoint;
2428 *(panel->search_buffer + l) = '\0';
2429 panel->search_chpoint = 0;
2435 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2436 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2437 search = mc_search_new (esc_str, -1);
2438 search->search_type = MC_SEARCH_T_GLOB;
2439 search->is_entire_line = TRUE;
2440 switch (panels_options.qsearch_mode)
2442 case QSEARCH_CASE_SENSITIVE:
2443 search->is_case_sensitive = TRUE;
2444 break;
2445 case QSEARCH_CASE_INSENSITIVE:
2446 search->is_case_sensitive = FALSE;
2447 break;
2448 default:
2449 search->is_case_sensitive = panel->sort_info.case_sensitive;
2450 break;
2452 sel = panel->selected;
2453 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2455 if (i >= panel->count)
2457 i = 0;
2458 if (wrapped)
2459 break;
2460 wrapped = TRUE;
2462 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2464 sel = i;
2465 is_found = TRUE;
2466 break;
2469 if (is_found)
2471 unselect_item (panel);
2472 panel->selected = sel;
2473 select_item (panel);
2474 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2476 else if (c_code != KEY_BACKSPACE)
2478 act = panel->search_buffer + l;
2479 str_prev_noncomb_char (&act, panel->search_buffer);
2480 act[0] = '\0';
2482 mc_search_free (search);
2483 g_free (reg_exp);
2484 g_free (esc_str);
2487 /* --------------------------------------------------------------------------------------------- */
2488 /** Start new search.
2489 * @param panel instance of WPanel structure
2492 static void
2493 start_search (WPanel * panel)
2495 if (panel->searching)
2497 if (panel->selected + 1 == panel->count)
2498 panel->selected = 0;
2499 else
2500 move_down (panel);
2502 /* in case if there was no search string we need to recall
2503 previous string, with which we ended previous searching */
2504 if (panel->search_buffer[0] == '\0')
2505 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2506 sizeof (panel->search_buffer));
2508 do_search (panel, 0);
2510 else
2512 panel->searching = TRUE;
2513 panel->search_buffer[0] = '\0';
2514 panel->search_char[0] = '\0';
2515 panel->search_chpoint = 0;
2516 display_mini_info (panel);
2517 mc_refresh ();
2521 /* --------------------------------------------------------------------------------------------- */
2523 static void
2524 stop_search (WPanel * panel)
2526 panel->searching = FALSE;
2528 /* if user had overrdied search string, we need to store it
2529 to the previous_search_buffer */
2530 if (panel->search_buffer[0] != '\0')
2531 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2532 sizeof (panel->prev_search_buffer));
2534 display_mini_info (panel);
2537 /* --------------------------------------------------------------------------------------------- */
2538 /** Return 1 if the Enter key has been processed, 0 otherwise */
2540 static int
2541 do_enter_on_file_entry (file_entry * fe)
2543 vfs_path_t *full_name_vpath;
2544 gboolean ok;
2547 * Directory or link to directory - change directory.
2548 * Try the same for the entries on which mc_lstat() has failed.
2550 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2552 vfs_path_t *fname_vpath;
2554 fname_vpath = vfs_path_from_str (fe->fname);
2555 if (!do_cd (fname_vpath, cd_exact))
2556 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2557 vfs_path_free (fname_vpath);
2558 return 1;
2561 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2563 /* Try associated command */
2564 if (regex_command (full_name_vpath, "Open", NULL) != 0)
2566 vfs_path_free (full_name_vpath);
2567 return 1;
2570 /* Check if the file is executable */
2571 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2572 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2573 vfs_path_free (full_name_vpath);
2574 if (!ok)
2575 return 0;
2577 if (confirm_execute)
2579 if (query_dialog
2580 (_("The Midnight Commander"),
2581 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2582 return 1;
2585 if (!vfs_current_is_local ())
2587 int ret;
2588 vfs_path_t *tmp_vpath;
2590 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2591 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2592 vfs_path_free (tmp_vpath);
2593 /* We took action only if the dialog was shown or the execution
2594 * was successful */
2595 return confirm_execute || (ret == 0);
2599 char *tmp = name_quote (fe->fname, 0);
2600 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2601 g_free (tmp);
2602 shell_execute (cmd, 0);
2603 g_free (cmd);
2606 #ifdef HAVE_CHARSET
2607 mc_global.source_codepage = default_source_codepage;
2608 #endif
2610 return 1;
2613 /* --------------------------------------------------------------------------------------------- */
2615 static int
2616 do_enter (WPanel * panel)
2618 return do_enter_on_file_entry (selection (panel));
2621 /* --------------------------------------------------------------------------------------------- */
2623 static void
2624 chdir_other_panel (WPanel * panel)
2626 const file_entry *entry = &panel->dir.list[panel->selected];
2628 vfs_path_t *new_dir_vpath;
2629 char *sel_entry = NULL;
2631 if (get_other_type () != view_listing)
2633 set_display_type (get_other_index (), view_listing);
2636 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2637 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2638 else
2640 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2641 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2644 change_panel ();
2645 do_cd (new_dir_vpath, cd_exact);
2646 vfs_path_free (new_dir_vpath);
2648 if (sel_entry)
2649 try_to_select (current_panel, sel_entry);
2650 change_panel ();
2652 move_down (panel);
2655 /* --------------------------------------------------------------------------------------------- */
2657 * Make the current directory of the current panel also the current
2658 * directory of the other panel. Put the other panel to the listing
2659 * mode if needed. If the current panel is panelized, the other panel
2660 * doesn't become panelized.
2663 static void
2664 panel_sync_other (const WPanel * panel)
2666 if (get_other_type () != view_listing)
2668 set_display_type (get_other_index (), view_listing);
2671 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2673 /* try to select current filename on the other panel */
2674 if (!panel->is_panelized)
2676 try_to_select (other_panel, selection (panel)->fname);
2680 /* --------------------------------------------------------------------------------------------- */
2682 static void
2683 chdir_to_readlink (WPanel * panel)
2685 vfs_path_t *new_dir_vpath;
2686 char buffer[MC_MAXPATHLEN], *p;
2687 int i;
2688 struct stat st;
2689 vfs_path_t *panel_fname_vpath;
2690 gboolean ok;
2692 if (get_other_type () != view_listing)
2693 return;
2695 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2696 return;
2698 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2699 if (i < 0)
2700 return;
2702 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2703 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2704 vfs_path_free (panel_fname_vpath);
2705 if (!ok)
2706 return;
2708 buffer[i] = 0;
2709 if (!S_ISDIR (st.st_mode))
2711 p = strrchr (buffer, PATH_SEP);
2712 if (p && !p[1])
2714 *p = 0;
2715 p = strrchr (buffer, PATH_SEP);
2717 if (!p)
2718 return;
2719 p[1] = 0;
2721 if (*buffer == PATH_SEP)
2722 new_dir_vpath = vfs_path_from_str (buffer);
2723 else
2724 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2726 change_panel ();
2727 do_cd (new_dir_vpath, cd_exact);
2728 vfs_path_free (new_dir_vpath);
2729 change_panel ();
2731 move_down (panel);
2734 /* --------------------------------------------------------------------------------------------- */
2736 static gsize
2737 panel_get_format_field_count (WPanel * panel)
2739 format_e *format;
2740 gsize lc_index;
2741 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2742 return lc_index;
2745 /* --------------------------------------------------------------------------------------------- */
2747 function return 0 if not found and REAL_INDEX+1 if found
2750 static gsize
2751 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2753 format_e *format;
2754 gsize lc_index;
2756 for (lc_index = 1, format = panel->format;
2757 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2758 if (format == NULL)
2759 lc_index = 0;
2761 return lc_index;
2764 /* --------------------------------------------------------------------------------------------- */
2766 static format_e *
2767 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2769 format_e *format;
2770 for (format = panel->format;
2771 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2772 return format;
2775 /* --------------------------------------------------------------------------------------------- */
2777 static const panel_field_t *
2778 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2780 const panel_field_t *pfield;
2781 format_e *format;
2783 format = panel_get_format_field_by_index (panel, lc_index);
2784 if (format == NULL)
2785 return NULL;
2786 pfield = panel_get_field_by_title (format->title);
2787 if (pfield == NULL)
2788 return NULL;
2789 if (pfield->sort_routine == NULL)
2790 return NULL;
2791 return pfield;
2794 /* --------------------------------------------------------------------------------------------- */
2796 static void
2797 panel_toggle_sort_order_prev (WPanel * panel)
2799 gsize lc_index, i;
2800 gchar *title;
2802 const panel_field_t *pfield = NULL;
2804 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2805 lc_index = panel_get_format_field_index_by_name (panel, title);
2806 g_free (title);
2808 if (lc_index > 1)
2810 /* search for prev sortable column in panel format */
2811 for (i = lc_index - 1;
2812 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2815 if (pfield == NULL)
2817 /* Sortable field not found. Try to search in each array */
2818 for (i = panel_get_format_field_count (panel);
2819 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2822 if (pfield != NULL)
2824 panel->sort_info.sort_field = pfield;
2825 panel_set_sort_order (panel, pfield);
2829 /* --------------------------------------------------------------------------------------------- */
2831 static void
2832 panel_toggle_sort_order_next (WPanel * panel)
2834 gsize lc_index, i;
2835 const panel_field_t *pfield = NULL;
2836 gsize format_field_count;
2837 gchar *title;
2839 format_field_count = panel_get_format_field_count (panel);
2840 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2841 lc_index = panel_get_format_field_index_by_name (panel, title);
2842 g_free (title);
2844 if (lc_index != 0 && lc_index != format_field_count)
2846 /* search for prev sortable column in panel format */
2847 for (i = lc_index;
2848 i != format_field_count
2849 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2852 if (pfield == NULL)
2854 /* Sortable field not found. Try to search in each array */
2855 for (i = 0;
2856 i != format_field_count
2857 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2860 if (pfield != NULL)
2862 panel->sort_info.sort_field = pfield;
2863 panel_set_sort_order (panel, pfield);
2867 /* --------------------------------------------------------------------------------------------- */
2869 static void
2870 panel_select_sort_order (WPanel * panel)
2872 const panel_field_t *sort_order;
2874 sort_order = sort_box (&panel->sort_info);
2875 if (sort_order != NULL)
2877 panel->sort_info.sort_field = sort_order;
2878 panel_set_sort_order (panel, sort_order);
2882 /* --------------------------------------------------------------------------------------------- */
2885 * panel_content_scroll_left:
2886 * @param panel the pointer to the panel on which we operate
2888 * scroll long filename to the left (decrement scroll pointer)
2892 static void
2893 panel_content_scroll_left (WPanel * panel)
2895 if (panel->content_shift > -1)
2897 if (panel->content_shift > panel->max_shift)
2898 panel->content_shift = panel->max_shift;
2900 panel->content_shift--;
2901 show_dir (panel);
2902 paint_dir (panel);
2907 /* --------------------------------------------------------------------------------------------- */
2910 * panel_content_scroll_right:
2911 * @param panel the pointer to the panel on which we operate
2913 * scroll long filename to the right (increment scroll pointer)
2917 static void
2918 panel_content_scroll_right (WPanel * panel)
2920 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
2922 panel->content_shift++;
2923 show_dir (panel);
2924 paint_dir (panel);
2928 /* --------------------------------------------------------------------------------------------- */
2930 static void
2931 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2933 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2935 const panel_field_t *sort_order;
2937 sort_order = panel_get_field_by_id (name);
2938 if (sort_order == NULL)
2939 return;
2940 panel->sort_info.sort_field = sort_order;
2942 else
2943 panel->sort_info.reverse = !panel->sort_info.reverse;
2945 panel_set_sort_order (panel, panel->sort_info.sort_field);
2948 /* --------------------------------------------------------------------------------------------- */
2950 * If we moved to the parent directory move the selection pointer to
2951 * the old directory name; If we leave VFS dir, remove FS specificator.
2953 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2956 static const char *
2957 get_parent_dir_name (const char *cwd, const char *lwd)
2959 size_t llen, clen;
2960 const char *p;
2962 llen = strlen (lwd);
2963 clen = strlen (cwd);
2965 if (llen <= clen)
2966 return NULL;
2968 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2970 if (p == NULL)
2972 p = strrchr (lwd, PATH_SEP);
2974 if ((p != NULL)
2975 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2976 && (clen == (size_t) (p - lwd)
2977 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2978 return (p + 1);
2980 return NULL;
2983 /* skip VFS prefix */
2984 while (--p > lwd && *p != PATH_SEP)
2986 /* get last component */
2987 while (--p > lwd && *p != PATH_SEP)
2990 /* return last component */
2991 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
2994 /* --------------------------------------------------------------------------------------------- */
2995 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2997 static void
2998 subshell_chdir (const vfs_path_t * vpath)
3000 #ifdef HAVE_SUBSHELL_SUPPORT
3001 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3002 do_subshell_chdir (vpath, FALSE, TRUE);
3003 #endif /* HAVE_SUBSHELL_SUPPORT */
3006 /* --------------------------------------------------------------------------------------------- */
3008 * Changes the current directory of the panel.
3009 * Don't record change in the directory history.
3012 static gboolean
3013 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3015 char *olddir;
3017 olddir = vfs_path_to_str (panel->cwd_vpath);
3019 /* Convert *new_path to a suitable pathname, handle ~user */
3020 if (cd_type == cd_parse_command)
3022 const vfs_path_element_t *element;
3024 element = vfs_path_get_by_index (new_dir_vpath, 0);
3025 if (strcmp (element->path, "-") == 0)
3026 new_dir_vpath = panel->lwd_vpath;
3029 if (mc_chdir (new_dir_vpath) == -1)
3031 panel_set_cwd (panel, olddir);
3032 g_free (olddir);
3033 return FALSE;
3036 /* Success: save previous directory, shutdown status of previous dir */
3037 panel_set_lwd (panel, olddir);
3038 input_free_completions (cmdline);
3040 vfs_path_free (panel->cwd_vpath);
3041 vfs_setup_cwd ();
3042 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3044 vfs_release_path (olddir);
3046 subshell_chdir (panel->cwd_vpath);
3048 /* Reload current panel */
3049 panel_clean_dir (panel);
3051 char *tmp_path;
3053 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3054 panel->count =
3055 do_load_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
3056 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3057 panel->sort_info.exec_first, panel->filter);
3058 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
3059 g_free (tmp_path);
3061 load_hint (0);
3062 panel->dirty = 1;
3063 update_xterm_title_path ();
3065 g_free (olddir);
3067 return TRUE;
3070 /* --------------------------------------------------------------------------------------------- */
3072 static void
3073 directory_history_next (WPanel * panel)
3075 GList *nextdir;
3077 nextdir = g_list_next (panel->dir_history);
3078 if (nextdir != NULL)
3080 vfs_path_t *data_vpath;
3082 data_vpath = vfs_path_from_str ((char *) nextdir->data);
3083 if (_do_panel_cd (panel, data_vpath, cd_exact))
3084 panel->dir_history = nextdir;
3085 vfs_path_free (data_vpath);
3089 /* --------------------------------------------------------------------------------------------- */
3091 static void
3092 directory_history_prev (WPanel * panel)
3094 GList *prevdir;
3096 prevdir = g_list_previous (panel->dir_history);
3098 if (prevdir != NULL)
3100 vfs_path_t *data_vpath;
3102 data_vpath = vfs_path_from_str ((char *) prevdir->data);
3103 if (_do_panel_cd (panel, data_vpath, cd_exact))
3104 panel->dir_history = prevdir;
3105 vfs_path_free (data_vpath);
3109 /* --------------------------------------------------------------------------------------------- */
3111 static void
3112 directory_history_list (WPanel * panel)
3114 char *s;
3116 s = history_show (&panel->dir_history, &panel->widget);
3118 if (s != NULL)
3120 vfs_path_t *s_vpath;
3122 s_vpath = vfs_path_from_str (s);
3123 if (_do_panel_cd (panel, s_vpath, cd_exact))
3125 char *tmp_path;
3127 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3128 directory_history_add (panel, tmp_path);
3129 g_free (tmp_path);
3131 else
3132 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3133 vfs_path_free (s_vpath);
3134 g_free (s);
3138 /* --------------------------------------------------------------------------------------------- */
3140 static cb_ret_t
3141 panel_execute_cmd (WPanel * panel, unsigned long command)
3143 int res = MSG_HANDLED;
3145 if (command != CK_Search)
3146 stop_search (panel);
3149 switch (command)
3151 case CK_Up:
3152 case CK_Down:
3153 case CK_Left:
3154 case CK_Right:
3155 case CK_Bottom:
3156 case CK_Top:
3157 case CK_PageDown:
3158 case CK_PageUp:
3159 /* reset state of marks flag */
3160 state_mark = -1;
3161 break;
3163 switch (command)
3165 case CK_PanelOtherCd:
3166 chdir_other_panel (panel);
3167 break;
3168 case CK_PanelOtherCdLink:
3169 chdir_to_readlink (panel);
3170 break;
3171 case CK_CopySingle:
3172 copy_cmd_local ();
3173 break;
3174 case CK_DeleteSingle:
3175 delete_cmd_local ();
3176 break;
3177 case CK_Enter:
3178 do_enter (panel);
3179 break;
3180 case CK_ViewRaw:
3181 view_raw_cmd ();
3182 break;
3183 case CK_EditNew:
3184 edit_cmd_new ();
3185 break;
3186 case CK_MoveSingle:
3187 rename_cmd_local ();
3188 break;
3189 case CK_SelectInvert:
3190 select_invert_cmd ();
3191 break;
3192 case CK_Select:
3193 select_cmd ();
3194 break;
3195 case CK_Unselect:
3196 unselect_cmd ();
3197 break;
3198 case CK_PageDown:
3199 next_page (panel);
3200 break;
3201 case CK_PageUp:
3202 prev_page (panel);
3203 break;
3204 case CK_CdChild:
3205 goto_child_dir (panel);
3206 break;
3207 case CK_CdParent:
3208 goto_parent_dir (panel);
3209 break;
3210 case CK_History:
3211 directory_history_list (panel);
3212 break;
3213 case CK_HistoryNext:
3214 directory_history_next (panel);
3215 break;
3216 case CK_HistoryPrev:
3217 directory_history_prev (panel);
3218 break;
3219 case CK_BottomOnScreen:
3220 goto_bottom_file (panel);
3221 break;
3222 case CK_MiddleOnScreen:
3223 goto_middle_file (panel);
3224 break;
3225 case CK_TopOnScreen:
3226 goto_top_file (panel);
3227 break;
3228 case CK_Mark:
3229 mark_file (panel);
3230 break;
3231 case CK_MarkUp:
3232 mark_file_up (panel);
3233 break;
3234 case CK_MarkDown:
3235 mark_file_down (panel);
3236 break;
3237 case CK_MarkLeft:
3238 mark_file_left (panel);
3239 break;
3240 case CK_MarkRight:
3241 mark_file_right (panel);
3242 break;
3243 case CK_CdParentSmart:
3244 res = force_maybe_cd ();
3245 break;
3246 case CK_Up:
3247 move_up (panel);
3248 break;
3249 case CK_Down:
3250 move_down (panel);
3251 break;
3252 case CK_Left:
3253 res = move_left (panel);
3254 break;
3255 case CK_Right:
3256 res = move_right (panel);
3257 break;
3258 case CK_Bottom:
3259 move_end (panel);
3260 break;
3261 case CK_Top:
3262 move_home (panel);
3263 break;
3264 #ifdef HAVE_CHARSET
3265 case CK_SelectCodepage:
3266 panel_change_encoding (panel);
3267 break;
3268 #endif
3269 case CK_ScrollLeft:
3270 panel_content_scroll_left (panel);
3271 break;
3272 case CK_ScrollRight:
3273 panel_content_scroll_right (panel);
3274 break;
3275 case CK_Search:
3276 start_search (panel);
3277 break;
3278 case CK_SearchStop:
3279 break;
3280 case CK_PanelOtherSync:
3281 panel_sync_other (panel);
3282 break;
3283 case CK_Sort:
3284 panel_select_sort_order (panel);
3285 break;
3286 case CK_SortPrev:
3287 panel_toggle_sort_order_prev (panel);
3288 break;
3289 case CK_SortNext:
3290 panel_toggle_sort_order_next (panel);
3291 break;
3292 case CK_SortReverse:
3293 panel->sort_info.reverse = !panel->sort_info.reverse;
3294 panel_set_sort_order (panel, panel->sort_info.sort_field);
3295 break;
3296 case CK_SortByName:
3297 panel_set_sort_type_by_id (panel, "name");
3298 break;
3299 case CK_SortByExt:
3300 panel_set_sort_type_by_id (panel, "extension");
3301 break;
3302 case CK_SortBySize:
3303 panel_set_sort_type_by_id (panel, "size");
3304 break;
3305 case CK_SortByMTime:
3306 panel_set_sort_type_by_id (panel, "mtime");
3307 break;
3309 return res;
3312 /* --------------------------------------------------------------------------------------------- */
3314 static cb_ret_t
3315 panel_key (WPanel * panel, int key)
3317 size_t i;
3319 if (is_abort_char (key))
3321 stop_search (panel);
3322 return MSG_HANDLED;
3325 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3327 do_search (panel, key);
3328 return MSG_HANDLED;
3331 for (i = 0; panel_map[i].key != 0; i++)
3332 if (key == panel_map[i].key)
3333 return panel_execute_cmd (panel, panel_map[i].command);
3335 if (panels_options.torben_fj_mode && key == ALT ('h'))
3337 goto_middle_file (panel);
3338 return MSG_HANDLED;
3341 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3343 start_search (panel);
3344 do_search (panel, key);
3345 return MSG_HANDLED;
3348 return MSG_NOT_HANDLED;
3351 /* --------------------------------------------------------------------------------------------- */
3353 static cb_ret_t
3354 panel_callback (Widget * w, widget_msg_t msg, int parm)
3356 WPanel *panel = (WPanel *) w;
3357 WButtonBar *bb;
3359 switch (msg)
3361 case WIDGET_INIT:
3362 /* subscribe to "history_load" event */
3363 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3364 /* subscribe to "history_save" event */
3365 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3366 return MSG_HANDLED;
3368 case WIDGET_DRAW:
3369 /* Repaint everything, including frame and separator */
3370 paint_frame (panel); /* including show_dir */
3371 paint_dir (panel);
3372 mini_info_separator (panel);
3373 display_mini_info (panel);
3374 panel->dirty = 0;
3375 return MSG_HANDLED;
3377 case WIDGET_FOCUS:
3378 state_mark = -1;
3379 current_panel = panel;
3380 panel->active = 1;
3381 if (mc_chdir (panel->cwd_vpath) != 0)
3383 char *cwd;
3385 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3386 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3387 cwd, unix_error_string (errno));
3388 g_free (cwd);
3390 else
3391 subshell_chdir (panel->cwd_vpath);
3393 update_xterm_title_path ();
3394 select_item (panel);
3395 show_dir (panel);
3396 paint_dir (panel);
3397 panel->dirty = 0;
3399 bb = find_buttonbar (panel->widget.owner);
3400 midnight_set_buttonbar (bb);
3401 buttonbar_redraw (bb);
3402 return MSG_HANDLED;
3404 case WIDGET_UNFOCUS:
3405 /* Janne: look at this for the multiple panel options */
3406 stop_search (panel);
3407 panel->active = 0;
3408 show_dir (panel);
3409 unselect_item (panel);
3410 return MSG_HANDLED;
3412 case WIDGET_KEY:
3413 return panel_key (panel, parm);
3415 case WIDGET_COMMAND:
3416 return panel_execute_cmd (panel, parm);
3418 case WIDGET_DESTROY:
3419 /* unsubscribe from "history_load" event */
3420 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3421 /* unsubscribe from "history_save" event */
3422 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3423 panel_destroy (panel);
3424 free_my_statfs ();
3425 return MSG_HANDLED;
3427 default:
3428 return default_proc (msg, parm);
3432 /* --------------------------------------------------------------------------------------------- */
3433 /* */
3434 /* Panel mouse events support routines */
3435 /* */
3437 static void
3438 mouse_toggle_mark (WPanel * panel)
3440 do_mark_file (panel, MARK_DONT_MOVE);
3441 mouse_marking = selection (panel)->f.marked;
3442 mouse_mark_panel = current_panel;
3445 /* --------------------------------------------------------------------------------------------- */
3447 static void
3448 mouse_set_mark (WPanel * panel)
3451 if (mouse_mark_panel == panel)
3453 if (mouse_marking && !(selection (panel)->f.marked))
3454 do_mark_file (panel, MARK_DONT_MOVE);
3455 else if (!mouse_marking && (selection (panel)->f.marked))
3456 do_mark_file (panel, MARK_DONT_MOVE);
3460 /* --------------------------------------------------------------------------------------------- */
3462 static gboolean
3463 mark_if_marking (WPanel * panel, Gpm_Event * event)
3465 if ((event->buttons & GPM_B_RIGHT) != 0)
3467 if ((event->type & GPM_DOWN) != 0)
3468 mouse_toggle_mark (panel);
3469 else
3470 mouse_set_mark (panel);
3471 return TRUE;
3473 return FALSE;
3476 /* --------------------------------------------------------------------------------------------- */
3477 /** Determine which column was clicked, and sort the panel on
3478 * that column, or reverse sort on that column if already
3479 * sorted on that column.
3482 static void
3483 mouse_sort_col (WPanel * panel, int x)
3485 int i;
3486 const char *lc_sort_name = NULL;
3487 panel_field_t *col_sort_format = NULL;
3488 format_e *format;
3489 gchar *title;
3491 for (i = 0, format = panel->format; format != NULL; format = format->next)
3493 i += format->field_len;
3494 if (x < i + 1)
3496 /* found column */
3497 lc_sort_name = format->title;
3498 break;
3502 if (lc_sort_name == NULL)
3503 return;
3505 for (i = 0; panel_fields[i].id != NULL; i++)
3507 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3508 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3510 col_sort_format = &panel_fields[i];
3511 g_free (title);
3512 break;
3514 g_free (title);
3517 if (col_sort_format == NULL)
3518 return;
3520 if (panel->sort_info.sort_field == col_sort_format)
3522 /* reverse the sort if clicked column is already the sorted column */
3523 panel->sort_info.reverse = !panel->sort_info.reverse;
3525 else
3527 /* new sort is forced to be ascending */
3528 panel->sort_info.reverse = FALSE;
3530 panel_set_sort_order (panel, col_sort_format);
3534 /* --------------------------------------------------------------------------------------------- */
3536 * Mouse callback of the panel minus repainting.
3537 * If the event is redirected to the menu, *redir is set to TRUE.
3539 static inline int
3540 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3542 const int lines = llines (panel);
3543 const gboolean is_active = dlg_widget_active (panel);
3544 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3545 Widget *w = (Widget *) panel;
3546 Gpm_Event local;
3548 *redir = FALSE;
3550 local = mouse_get_local (event, w);
3552 /* 1st line */
3553 if (mouse_down && local.y == 1)
3555 /* "<" button */
3556 if (local.x == 2)
3558 directory_history_prev (panel);
3559 return MOU_NORMAL;
3562 /* "." button show/hide hidden files */
3563 if (local.x == panel->widget.cols - 5)
3565 midnight_dlg->callback (midnight_dlg, NULL, DLG_ACTION, CK_ShowHidden, NULL);
3566 repaint_screen ();
3567 return MOU_NORMAL;
3570 /* ">" button */
3571 if (local.x == panel->widget.cols - 1)
3573 directory_history_next (panel);
3574 return MOU_NORMAL;
3577 /* "^" button */
3578 if (local.x >= panel->widget.cols - 4 && local.x <= panel->widget.cols - 2)
3580 directory_history_list (panel);
3581 return MOU_NORMAL;
3584 /* rest of the upper frame, the menu is invisible - call menu */
3585 if (!menubar_visible)
3587 *redir = TRUE;
3588 return the_menubar->widget.mouse (event, the_menubar);
3591 /* no other events on 1st line */
3592 return MOU_NORMAL;
3595 /* sort on clicked column; don't handle wheel events */
3596 if (mouse_down && (local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && local.y == 2)
3598 mouse_sort_col (panel, local.x);
3599 return MOU_NORMAL;
3602 /* Mouse wheel events */
3603 if (mouse_down && (local.buttons & GPM_B_UP) != 0)
3605 if (is_active)
3607 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3608 prev_page (panel);
3609 else /* We are in first page */
3610 move_up (panel);
3612 return MOU_NORMAL;
3615 if (mouse_down && (local.buttons & GPM_B_DOWN) != 0)
3617 if (is_active)
3619 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3620 next_page (panel);
3621 else /* We are in last page */
3622 move_down (panel);
3624 return MOU_NORMAL;
3627 local.y -= 2;
3628 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
3630 int my_index;
3632 if (!is_active)
3633 change_panel ();
3635 if (panel->top_file + local.y > panel->count)
3636 my_index = panel->count - 1;
3637 else
3639 my_index = panel->top_file + local.y - 1;
3640 if (panel->split && (local.x > ((panel->widget.cols - 2) / 2)))
3641 my_index += llines (panel);
3643 if (my_index >= panel->count)
3644 my_index = panel->count - 1;
3647 if (my_index != panel->selected)
3649 unselect_item (panel);
3650 panel->selected = my_index;
3651 select_item (panel);
3654 /* This one is new */
3655 mark_if_marking (panel, &local);
3657 else if ((local.type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3659 if (local.y > 0 && local.y <= lines)
3660 do_enter (panel);
3663 return MOU_NORMAL;
3666 /* --------------------------------------------------------------------------------------------- */
3667 /** Mouse callback of the panel */
3669 static int
3670 panel_event (Gpm_Event * event, void *data)
3672 WPanel *panel = (WPanel *) data;
3673 int ret;
3674 gboolean redir;
3676 if (!mouse_global_in_widget (event, (Widget *) data))
3677 return MOU_UNHANDLED;
3679 ret = do_panel_event (event, panel, &redir);
3680 if (!redir)
3681 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3683 return ret;
3686 /* --------------------------------------------------------------------------------------------- */
3688 static void
3689 reload_panelized (WPanel * panel)
3691 int i, j;
3692 dir_list *list = &panel->dir;
3694 if (panel != current_panel)
3695 (void) mc_chdir (panel->cwd_vpath);
3697 for (i = 0, j = 0; i < panel->count; i++)
3699 vfs_path_t *vpath;
3701 if (list->list[i].f.marked)
3703 /* Unmark the file in advance. In case the following mc_lstat
3704 * fails we are done, else we have to mark the file again
3705 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3706 * IMO that's the best way to update the panel's summary status
3707 * -- Norbert
3709 do_file_mark (panel, i, 0);
3711 vpath = vfs_path_from_str (list->list[i].fname);
3712 if (mc_lstat (vpath, &list->list[i].st))
3713 g_free (list->list[i].fname);
3714 else
3716 if (list->list[i].f.marked)
3717 do_file_mark (panel, i, 1);
3718 if (j != i)
3719 list->list[j] = list->list[i];
3720 j++;
3722 vfs_path_free (vpath);
3724 if (j == 0)
3725 panel->count = set_zero_dir (list) ? 1 : 0;
3726 else
3727 panel->count = j;
3729 if (panel != current_panel)
3730 (void) mc_chdir (current_panel->cwd_vpath);
3733 /* --------------------------------------------------------------------------------------------- */
3735 static void
3736 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3738 gboolean free_pointer;
3739 char *my_current_file = NULL;
3741 if ((flags & UP_RELOAD) != 0)
3743 panel->is_panelized = FALSE;
3744 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3745 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3748 /* If current_file == -1 (an invalid pointer) then preserve selection */
3749 free_pointer = current_file == UP_KEEPSEL;
3751 if (free_pointer)
3753 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3754 current_file = my_current_file;
3757 if (panel->is_panelized)
3758 reload_panelized (panel);
3759 else
3760 panel_reload (panel);
3762 try_to_select (panel, current_file);
3763 panel->dirty = 1;
3765 if (free_pointer)
3766 g_free (my_current_file);
3769 /* --------------------------------------------------------------------------------------------- */
3771 static void
3772 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3774 if (get_display_type (which) == view_listing)
3776 WPanel *panel;
3778 panel = (WPanel *) get_panel_widget (which);
3779 if (panel->is_panelized)
3780 flags &= ~UP_RELOAD;
3781 update_one_panel_widget (panel, flags, current_file);
3785 /* --------------------------------------------------------------------------------------------- */
3786 /*** public functions ****************************************************************************/
3787 /* --------------------------------------------------------------------------------------------- */
3789 * Remove encode info from last path element.
3793 vfs_path_t *
3794 remove_encoding_from_path (const vfs_path_t * vpath)
3796 vfs_path_t *ret_vpath;
3797 GString *tmp_conv;
3798 int indx;
3800 ret_vpath = vfs_path_new ();
3802 tmp_conv = g_string_new ("");
3804 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
3806 GIConv converter;
3807 vfs_path_element_t *path_element;
3809 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
3810 vfs_path_add_element (ret_vpath, path_element);
3812 if (path_element->encoding == NULL)
3814 continue;
3817 converter = str_crt_conv_to (path_element->encoding);
3818 if (converter == INVALID_CONV)
3819 continue;
3821 g_free (path_element->encoding);
3822 path_element->encoding = NULL;
3824 str_vfs_convert_from (converter, path_element->path, tmp_conv);
3826 g_free (path_element->path);
3827 path_element->path = g_strdup (tmp_conv->str);
3829 g_string_set_size (tmp_conv, 0);
3831 str_close_conv (converter);
3832 str_close_conv (path_element->dir.converter);
3833 path_element->dir.converter = INVALID_CONV;
3835 g_string_free (tmp_conv, TRUE);
3836 return ret_vpath;
3839 /* --------------------------------------------------------------------------------------------- */
3841 static void
3842 do_select (WPanel * panel, int i)
3844 if (i != panel->selected)
3846 panel->dirty = 1;
3847 panel->selected = i;
3848 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3849 if (panel->top_file < 0)
3850 panel->top_file = 0;
3854 /* --------------------------------------------------------------------------------------------- */
3856 static void
3857 do_try_to_select (WPanel * panel, const char *name)
3859 int i;
3860 char *subdir;
3862 if (!name)
3864 do_select (panel, 0);
3865 return;
3868 /* We only want the last component of the directory,
3869 * and from this only the name without suffix.
3870 * Cut prefix if the panel is not panelized */
3872 if (panel->is_panelized)
3873 subdir = vfs_strip_suffix_from_filename (name);
3874 else
3875 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3877 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3878 for (i = 0; i < panel->count; i++)
3880 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3882 do_select (panel, i);
3883 g_free (subdir);
3884 return;
3888 /* Try to select a file near the file that is missing */
3889 if (panel->selected >= panel->count)
3890 do_select (panel, panel->count - 1);
3891 g_free (subdir);
3894 /* --------------------------------------------------------------------------------------------- */
3896 /* event callback */
3897 static gboolean
3898 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3899 gpointer init_data, gpointer data)
3901 (void) event_group_name;
3902 (void) event_name;
3903 (void) init_data;
3904 (void) data;
3906 update_panels (UP_RELOAD, UP_KEEPSEL);
3908 return TRUE;
3911 /* --------------------------------------------------------------------------------------------- */
3913 /* event callback */
3914 static gboolean
3915 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3916 gpointer init_data, gpointer data)
3918 (void) event_group_name;
3919 (void) event_name;
3920 (void) init_data;
3921 (void) data;
3923 if (current_panel->marked == 0)
3924 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3925 (gpointer) selection (current_panel)->fname);
3926 else
3928 int i;
3929 gboolean first = TRUE;
3930 char *flist = NULL;
3932 for (i = 0; i < current_panel->count; i++)
3933 if (current_panel->dir.list[i].f.marked != 0)
3934 { /* Skip the unmarked ones */
3935 if (first)
3937 flist = g_strdup (current_panel->dir.list[i].fname);
3938 first = FALSE;
3940 else
3942 /* Add empty lines after the file */
3943 char *tmp;
3945 tmp =
3946 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3947 g_free (flist);
3948 flist = tmp;
3952 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3953 g_free (flist);
3955 return TRUE;
3958 /* --------------------------------------------------------------------------------------------- */
3959 /*** public functions ****************************************************************************/
3960 /* --------------------------------------------------------------------------------------------- */
3962 void
3963 try_to_select (WPanel * panel, const char *name)
3965 do_try_to_select (panel, name);
3966 select_item (panel);
3969 /* --------------------------------------------------------------------------------------------- */
3971 void
3972 panel_clean_dir (WPanel * panel)
3974 int count = panel->count;
3976 panel->count = 0;
3977 panel->top_file = 0;
3978 panel->selected = 0;
3979 panel->marked = 0;
3980 panel->dirs_marked = 0;
3981 panel->total = 0;
3982 panel->searching = FALSE;
3983 panel->is_panelized = FALSE;
3984 panel->dirty = 1;
3985 panel->content_shift = -1;
3986 panel->max_shift = -1;
3988 clean_dir (&panel->dir, count);
3991 /* --------------------------------------------------------------------------------------------- */
3993 * Set Up panel's current dir object
3995 * @param panel panel object
3996 * @param path_str string contain path
3999 void
4000 panel_set_cwd (WPanel * panel, const char *path_str)
4002 vfs_path_free (panel->cwd_vpath);
4003 panel->cwd_vpath = vfs_path_from_str (path_str);
4006 /* --------------------------------------------------------------------------------------------- */
4008 * Set Up panel's last working dir object
4010 * @param panel panel object
4011 * @param path_str string contain path
4014 void
4015 panel_set_lwd (WPanel * panel, const char *path_str)
4017 vfs_path_free (panel->lwd_vpath);
4018 panel->lwd_vpath = vfs_path_from_str (path_str);
4021 /* --------------------------------------------------------------------------------------------- */
4022 /** Panel creation.
4023 * @param panel_name the name of the panel for setup retieving
4024 * @returns new instance of WPanel
4027 WPanel *
4028 panel_new (const char *panel_name)
4030 return panel_new_with_dir (panel_name, NULL);
4033 /* --------------------------------------------------------------------------------------------- */
4034 /** Panel creation for specified directory.
4035 * @param panel_name specifies the name of the panel for setup retieving
4036 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
4037 * @returns new instance of WPanel
4040 WPanel *
4041 panel_new_with_dir (const char *panel_name, const char *wpath)
4043 WPanel *panel;
4044 char *section;
4045 int i, err;
4046 char *curdir = NULL;
4048 panel = g_new0 (WPanel, 1);
4050 /* No know sizes of the panel at startup */
4051 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
4053 /* We do not want the cursor */
4054 widget_want_cursor (panel->widget, 0);
4056 if (wpath != NULL)
4058 curdir = _vfs_get_cwd ();
4059 panel_set_cwd (panel, wpath);
4061 else
4063 vfs_setup_cwd ();
4064 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4067 panel_set_lwd (panel, ".");
4069 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4070 /* directories history will be get later */
4072 panel->dir.list = g_new (file_entry, MIN_FILES);
4073 panel->dir.size = MIN_FILES;
4074 panel->active = 0;
4075 panel->filter = 0;
4076 panel->split = 0;
4077 panel->top_file = 0;
4078 panel->selected = 0;
4079 panel->marked = 0;
4080 panel->total = 0;
4081 panel->dirty = 1;
4082 panel->searching = FALSE;
4083 panel->dirs_marked = 0;
4084 panel->is_panelized = FALSE;
4085 panel->format = 0;
4086 panel->status_format = 0;
4087 panel->format_modified = 1;
4088 panel->content_shift = -1;
4089 panel->max_shift = -1;
4091 panel->panel_name = g_strdup (panel_name);
4092 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4094 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4096 for (i = 0; i < LIST_TYPES; i++)
4097 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4099 panel->search_buffer[0] = '\0';
4100 panel->prev_search_buffer[0] = '\0';
4101 panel->frame_size = frame_half;
4103 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4104 if (!mc_config_has_group (mc_main_config, section))
4106 g_free (section);
4107 section = g_strdup (panel->panel_name);
4109 panel_load_setup (panel, section);
4110 g_free (section);
4112 /* Load format strings */
4113 err = set_panel_formats (panel);
4114 if (err != 0)
4115 set_panel_formats (panel);
4117 #ifdef HAVE_CHARSET
4119 const vfs_path_element_t *path_element;
4121 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4122 if (path_element->encoding != NULL)
4123 panel->codepage = get_codepage_index (path_element->encoding);
4125 #endif
4127 if (mc_chdir (panel->cwd_vpath) != 0)
4129 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4130 vfs_setup_cwd ();
4131 vfs_path_free (panel->cwd_vpath);
4132 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4135 /* Load the default format */
4137 char *tmp_path;
4139 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4140 panel->count =
4141 do_load_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
4142 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4143 panel->sort_info.exec_first, panel->filter);
4144 g_free (tmp_path);
4147 /* Restore old right path */
4148 if (curdir != NULL)
4150 vfs_path_t *vpath;
4152 vpath = vfs_path_from_str (curdir);
4153 err = mc_chdir (vpath);
4154 vfs_path_free (vpath);
4156 g_free (curdir);
4158 return panel;
4161 /* --------------------------------------------------------------------------------------------- */
4163 void
4164 panel_reload (WPanel * panel)
4166 struct stat current_stat;
4167 char *tmp_path;
4168 gboolean ok;
4170 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4171 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
4172 && current_stat.st_ctime == panel->dir_stat.st_ctime
4173 && current_stat.st_mtime == panel->dir_stat.st_mtime);
4174 g_free (tmp_path);
4176 if (ok)
4177 return;
4181 char *last_slash;
4183 if (mc_chdir (panel->cwd_vpath) != -1)
4184 break;
4186 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4187 if (tmp_path[0] == PATH_SEP && tmp_path[1] == '\0')
4189 panel_clean_dir (panel);
4190 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
4191 g_free (tmp_path);
4192 return;
4194 last_slash = strrchr (tmp_path, PATH_SEP);
4195 vfs_path_free (panel->cwd_vpath);
4196 if (last_slash == NULL || last_slash == tmp_path)
4197 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4198 else
4200 *last_slash = '\0';
4201 panel->cwd_vpath = vfs_path_from_str (tmp_path);
4203 g_free (tmp_path);
4204 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4205 show_dir (panel);
4207 while (TRUE);
4209 panel->count =
4210 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4211 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4212 panel->sort_info.exec_first, panel->filter);
4214 panel->dirty = 1;
4215 if (panel->selected >= panel->count)
4216 do_select (panel, panel->count - 1);
4218 recalculate_panel_summary (panel);
4221 /* --------------------------------------------------------------------------------------------- */
4222 /* Switches the panel to the mode specified in the format */
4223 /* Seting up both format and status string. Return: 0 - on success; */
4224 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4227 set_panel_formats (WPanel * p)
4229 format_e *form;
4230 char *err = NULL;
4231 int retcode = 0;
4233 form = use_display_format (p, panel_format (p), &err, 0);
4235 if (err != NULL)
4237 g_free (err);
4238 retcode = 1;
4240 else
4242 delete_format (p->format);
4243 p->format = form;
4246 if (panels_options.show_mini_info)
4248 form = use_display_format (p, mini_status_format (p), &err, 1);
4250 if (err != NULL)
4252 g_free (err);
4253 retcode += 2;
4255 else
4257 delete_format (p->status_format);
4258 p->status_format = form;
4262 panel_format_modified (p);
4263 panel_update_cols (&(p->widget), p->frame_size);
4265 if (retcode)
4266 message (D_ERROR, _("Warning"),
4267 _("User supplied format looks invalid, reverting to default."));
4268 if (retcode & 0x01)
4270 g_free (p->user_format);
4271 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4273 if (retcode & 0x02)
4275 g_free (p->user_status_format[p->list_type]);
4276 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4279 return retcode;
4282 /* --------------------------------------------------------------------------------------------- */
4284 void
4285 panel_update_cols (Widget * widget, panel_display_t frame_size)
4287 int cols, origin;
4289 /* don't touch panel if it is not in dialog yet */
4290 /* if panel is not in dialog it is not in widgets list
4291 and cannot be compared with get_panel_widget() result */
4292 if (widget->owner == NULL)
4293 return;
4295 if (panels_layout.horizontal_split)
4297 widget->cols = COLS;
4298 return;
4301 if (frame_size == frame_full)
4303 cols = COLS;
4304 origin = 0;
4306 else if (widget == get_panel_widget (0))
4308 cols = panels_layout.left_panel_size;
4309 origin = 0;
4311 else
4313 cols = COLS - panels_layout.left_panel_size;
4314 origin = panels_layout.left_panel_size;
4317 widget->cols = cols;
4318 widget->x = origin;
4321 /* --------------------------------------------------------------------------------------------- */
4323 /* Select current item and readjust the panel */
4324 void
4325 select_item (WPanel * panel)
4327 /* Although currently all over the code we set the selection and
4328 top file to decent values before calling select_item, I could
4329 forget it someday, so it's better to do the actual fitting here */
4331 if (panel->selected < 0)
4332 panel->selected = 0;
4334 if (panel->selected > panel->count - 1)
4335 panel->selected = panel->count - 1;
4337 adjust_top_file (panel);
4339 panel->dirty = 1;
4341 execute_hooks (select_file_hook);
4344 /* --------------------------------------------------------------------------------------------- */
4345 /** Clears all files in the panel, used only when one file was marked */
4346 void
4347 unmark_files (WPanel * panel)
4349 int i;
4351 if (!panel->marked)
4352 return;
4353 for (i = 0; i < panel->count; i++)
4354 file_mark (panel, i, 0);
4356 panel->dirs_marked = 0;
4357 panel->marked = 0;
4358 panel->total = 0;
4361 /* --------------------------------------------------------------------------------------------- */
4362 /** Recalculate the panels summary information, used e.g. when marked
4363 files might have been removed by an external command */
4365 void
4366 recalculate_panel_summary (WPanel * panel)
4368 int i;
4370 panel->marked = 0;
4371 panel->dirs_marked = 0;
4372 panel->total = 0;
4374 for (i = 0; i < panel->count; i++)
4375 if (panel->dir.list[i].f.marked)
4377 /* do_file_mark will return immediately if newmark == oldmark.
4378 So we have to first unmark it to get panel's summary information
4379 updated. (Norbert) */
4380 panel->dir.list[i].f.marked = 0;
4381 do_file_mark (panel, i, 1);
4385 /* --------------------------------------------------------------------------------------------- */
4386 /** This routine marks a file or a directory */
4388 void
4389 do_file_mark (WPanel * panel, int idx, int mark)
4391 if (panel->dir.list[idx].f.marked == mark)
4392 return;
4394 /* Only '..' can't be marked, '.' isn't visible */
4395 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4396 return;
4398 file_mark (panel, idx, mark);
4399 if (panel->dir.list[idx].f.marked)
4401 panel->marked++;
4402 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4404 if (panel->dir.list[idx].f.dir_size_computed)
4405 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4406 panel->dirs_marked++;
4408 else
4409 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4410 set_colors (panel);
4412 else
4414 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4416 if (panel->dir.list[idx].f.dir_size_computed)
4417 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4418 panel->dirs_marked--;
4420 else
4421 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4422 panel->marked--;
4426 /* --------------------------------------------------------------------------------------------- */
4428 * Changes the current directory of the panel.
4429 * Record change in the directory history.
4431 gboolean
4432 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4434 gboolean r;
4436 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4437 if (r)
4439 char *tmp_path;
4441 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4442 directory_history_add (panel, tmp_path);
4443 g_free (tmp_path);
4445 return r;
4448 /* --------------------------------------------------------------------------------------------- */
4450 void
4451 file_mark (WPanel * panel, int lc_index, int val)
4453 if (panel->dir.list[lc_index].f.marked != val)
4455 panel->dir.list[lc_index].f.marked = val;
4456 panel->dirty = 1;
4460 /* --------------------------------------------------------------------------------------------- */
4462 void
4463 panel_re_sort (WPanel * panel)
4465 char *filename;
4466 int i;
4468 if (panel == NULL)
4469 return;
4471 filename = g_strdup (selection (panel)->fname);
4472 unselect_item (panel);
4473 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4474 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4475 panel->sort_info.exec_first);
4476 panel->selected = -1;
4477 for (i = panel->count; i; i--)
4479 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4481 panel->selected = i - 1;
4482 break;
4485 g_free (filename);
4486 panel->top_file = panel->selected - ITEMS (panel) / 2;
4487 select_item (panel);
4488 panel->dirty = 1;
4491 /* --------------------------------------------------------------------------------------------- */
4493 void
4494 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4496 if (sort_order == NULL)
4497 return;
4499 panel->sort_info.sort_field = sort_order;
4501 /* The directory is already sorted, we have to load the unsorted stuff */
4502 if (sort_order->sort_routine == (sortfn *) unsorted)
4504 char *current_file;
4506 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4507 panel_reload (panel);
4508 try_to_select (panel, current_file);
4509 g_free (current_file);
4511 panel_re_sort (panel);
4514 /* --------------------------------------------------------------------------------------------- */
4516 * Change panel encoding.
4517 * @param panel WPanel object
4520 void
4521 panel_change_encoding (WPanel * panel)
4523 const char *encoding = NULL;
4524 #ifdef HAVE_CHARSET
4525 char *errmsg;
4526 int r;
4528 r = select_charset (-1, -1, panel->codepage, FALSE);
4530 if (r == SELECT_CHARSET_CANCEL)
4531 return; /* Cancel */
4533 panel->codepage = r;
4535 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4537 /* No translation */
4538 vfs_path_t *cd_path_vpath;
4540 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4541 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4542 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4543 show_dir (panel);
4544 vfs_path_free (cd_path_vpath);
4545 return;
4548 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4549 if (errmsg != NULL)
4551 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4552 g_free (errmsg);
4553 return;
4556 encoding = get_codepage_id (panel->codepage);
4557 #endif
4558 if (encoding != NULL)
4560 char *cd_path;
4561 vfs_change_encoding (panel->cwd_vpath, encoding);
4563 cd_path = vfs_path_to_str (panel->cwd_vpath);
4564 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4565 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4566 g_free (cd_path);
4570 /* --------------------------------------------------------------------------------------------- */
4572 * This routine reloads the directory in both panels. It tries to
4573 * select current_file in current_panel and other_file in other_panel.
4574 * If current_file == -1 then it automatically sets current_file and
4575 * other_file to the currently selected files in the panels.
4577 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4578 * will not reload the other panel.
4581 void
4582 update_panels (panel_update_flags_t flags, const char *current_file)
4584 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4585 WPanel *panel;
4587 update_one_panel (get_current_index (), flags, current_file);
4588 if (reload_other)
4589 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4591 if (get_current_type () == view_listing)
4592 panel = (WPanel *) get_panel_widget (get_current_index ());
4593 else
4594 panel = (WPanel *) get_panel_widget (get_other_index ());
4596 if (!panel->is_panelized)
4597 (void) mc_chdir (panel->cwd_vpath);
4600 /* --------------------------------------------------------------------------------------------- */
4602 void
4603 directory_history_add (struct WPanel *panel, const char *dir)
4605 vfs_path_t *vpath;
4606 char *tmp;
4608 vpath = vfs_path_from_str (dir);
4609 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
4610 vfs_path_free (vpath);
4611 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4614 /* --------------------------------------------------------------------------------------------- */
4616 gsize
4617 panel_get_num_of_sortable_fields (void)
4619 gsize ret = 0, lc_index;
4621 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4622 if (panel_fields[lc_index].is_user_choice)
4623 ret++;
4624 return ret;
4627 /* --------------------------------------------------------------------------------------------- */
4629 const char **
4630 panel_get_sortable_fields (gsize * array_size)
4632 char **ret;
4633 gsize lc_index, i;
4635 lc_index = panel_get_num_of_sortable_fields ();
4637 ret = g_try_new0 (char *, lc_index + 1);
4638 if (ret == NULL)
4639 return NULL;
4641 if (array_size != NULL)
4642 *array_size = lc_index;
4644 lc_index = 0;
4646 for (i = 0; panel_fields[i].id != NULL; i++)
4647 if (panel_fields[i].is_user_choice)
4648 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4649 return (const char **) ret;
4652 /* --------------------------------------------------------------------------------------------- */
4654 const panel_field_t *
4655 panel_get_field_by_id (const char *name)
4657 gsize lc_index;
4658 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4659 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4660 return &panel_fields[lc_index];
4661 return NULL;
4664 /* --------------------------------------------------------------------------------------------- */
4666 const panel_field_t *
4667 panel_get_field_by_title_hotkey (const char *name)
4669 gsize lc_index;
4670 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4671 if (panel_fields[lc_index].title_hotkey != NULL &&
4672 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4673 return &panel_fields[lc_index];
4674 return NULL;
4677 /* --------------------------------------------------------------------------------------------- */
4679 const panel_field_t *
4680 panel_get_field_by_title (const char *name)
4682 gsize lc_index;
4683 gchar *title = NULL;
4685 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4687 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4688 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4690 g_free (title);
4691 return &panel_fields[lc_index];
4694 g_free (title);
4695 return NULL;
4698 /* --------------------------------------------------------------------------------------------- */
4700 gsize
4701 panel_get_num_of_user_possible_fields (void)
4703 gsize ret = 0, lc_index;
4705 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4706 if (panel_fields[lc_index].use_in_user_format)
4707 ret++;
4708 return ret;
4711 /* --------------------------------------------------------------------------------------------- */
4713 const char **
4714 panel_get_user_possible_fields (gsize * array_size)
4716 char **ret;
4717 gsize lc_index, i;
4719 lc_index = panel_get_num_of_user_possible_fields ();
4721 ret = g_try_new0 (char *, lc_index + 1);
4722 if (ret == NULL)
4723 return NULL;
4725 if (array_size != NULL)
4726 *array_size = lc_index;
4728 lc_index = 0;
4730 for (i = 0; panel_fields[i].id != NULL; i++)
4731 if (panel_fields[i].use_in_user_format)
4732 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4733 return (const char **) ret;
4736 /* --------------------------------------------------------------------------------------------- */
4738 void
4739 panel_init (void)
4741 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4742 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4744 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4745 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4746 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4747 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4748 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4749 panel_filename_scroll_left_char =
4750 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4751 panel_filename_scroll_right_char =
4752 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4754 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4755 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4756 panel_save_curent_file_to_clip_file, NULL, NULL);
4760 /* --------------------------------------------------------------------------------------------- */
4762 void
4763 panel_deinit (void)
4765 g_free (panel_sort_up_sign);
4766 g_free (panel_sort_down_sign);
4768 g_free (panel_hiddenfiles_sign_show);
4769 g_free (panel_hiddenfiles_sign_hide);
4770 g_free (panel_history_prev_item_sign);
4771 g_free (panel_history_next_item_sign);
4772 g_free (panel_history_show_list_sign);
4773 g_free (panel_filename_scroll_left_char);
4774 g_free (panel_filename_scroll_right_char);
4778 /* --------------------------------------------------------------------------------------------- */