Refactoring of subshell support.
[midnight-commander.git] / src / filemanager / panel.c
blob39b9bb9111835c40bb0be9d1590bec2feb1a124c
1 /*
2 Panel managing.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Miguel de Icaza, 1995
10 Timur Bakeyev, 1997, 1999
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file panel.c
29 * \brief Source: panel managin module
32 #include <config.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h" /* For Gpm_Event */
44 #include "lib/tty/key.h" /* XCTRL and ALT macros */
45 #include "lib/skin.h"
46 #include "lib/strescape.h"
47 #include "lib/filehighlight.h"
48 #include "lib/mcconfig.h"
49 #include "lib/vfs/vfs.h"
50 #include "lib/unixcompat.h"
51 #include "lib/timefmt.h"
52 #include "lib/util.h"
53 #include "lib/widget.h"
54 #ifdef HAVE_CHARSET
55 #include "lib/charsets.h" /* get_codepage_id () */
56 #endif
57 #include "lib/event.h"
59 #include "src/setup.h" /* For loading/saving panel options */
60 #include "src/execute.h"
61 #ifdef HAVE_CHARSET
62 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
63 #endif
64 #include "src/keybind-defaults.h" /* global_keymap_t */
65 #ifdef ENABLE_SUBSHELL
66 #include "src/subshell.h" /* do_subshell_chdir() */
67 #endif
69 #include "dir.h"
70 #include "boxes.h"
71 #include "tree.h"
72 #include "ext.h" /* regexp_command */
73 #include "layout.h" /* Most layout variables are here */
74 #include "cmd.h"
75 #include "command.h" /* cmdline */
76 #include "usermenu.h"
77 #include "midnight.h"
78 #include "mountlist.h" /* my_statfs */
80 #include "panel.h"
82 /*** global variables ****************************************************************************/
84 /* The hook list for the select file function */
85 hook_t *select_file_hook = NULL;
87 /* *INDENT-OFF* */
88 panelized_panel_t panelized_panel = { {NULL, 0}, -1, NULL };
89 /* *INDENT-ON* */
91 static const char *string_file_name (file_entry *, int);
92 static const char *string_file_size (file_entry *, int);
93 static const char *string_file_size_brief (file_entry *, int);
94 static const char *string_file_type (file_entry *, int);
95 static const char *string_file_mtime (file_entry *, int);
96 static const char *string_file_atime (file_entry *, int);
97 static const char *string_file_ctime (file_entry *, int);
98 static const char *string_file_permission (file_entry *, int);
99 static const char *string_file_perm_octal (file_entry *, int);
100 static const char *string_file_nlinks (file_entry *, int);
101 static const char *string_inode (file_entry *, int);
102 static const char *string_file_nuid (file_entry *, int);
103 static const char *string_file_ngid (file_entry *, int);
104 static const char *string_file_owner (file_entry *, int);
105 static const char *string_file_group (file_entry *, int);
106 static const char *string_marked (file_entry *, int);
107 static const char *string_space (file_entry *, int);
108 static const char *string_dot (file_entry *, int);
110 /* *INDENT-OFF* */
111 panel_field_t panel_fields[] = {
113 "unsorted", 12, 1, J_LEFT_FIT,
114 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
115 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
116 N_("sort|u"),
117 N_("&Unsorted"), TRUE, FALSE,
118 string_file_name,
119 (sortfn *) unsorted
123 "name", 12, 1, J_LEFT_FIT,
124 /* TRANSLATORS: one single character to represent 'name' sort mode */
125 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
126 N_("sort|n"),
127 N_("&Name"), TRUE, TRUE,
128 string_file_name,
129 (sortfn *) sort_name
133 "version", 12, 1, J_LEFT_FIT,
134 /* TRANSLATORS: one single character to represent 'version' sort mode */
135 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
136 N_("sort|v"),
137 N_("&Version"), TRUE, FALSE,
138 string_file_name,
139 (sortfn *) sort_vers
143 "extension", 12, 1, J_LEFT_FIT,
144 /* TRANSLATORS: one single character to represent 'extension' sort mode */
145 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
146 N_("sort|e"),
147 N_("&Extension"), TRUE, FALSE,
148 string_file_name, /* TODO: string_file_ext */
149 (sortfn *) sort_ext
153 "size", 7, 0, J_RIGHT,
154 /* TRANSLATORS: one single character to represent 'size' sort mode */
155 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
156 N_("sort|s"),
157 N_("&Size"), TRUE, TRUE,
158 string_file_size,
159 (sortfn *) sort_size
163 "bsize", 7, 0, J_RIGHT,
165 N_("Block Size"), FALSE, FALSE,
166 string_file_size_brief,
167 (sortfn *) sort_size
171 "type", 1, 0, J_LEFT,
173 "", FALSE, TRUE,
174 string_file_type,
175 NULL
179 "mtime", 12, 0, J_RIGHT,
180 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
181 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
182 N_("sort|m"),
183 N_("&Modify time"), TRUE, TRUE,
184 string_file_mtime,
185 (sortfn *) sort_time
189 "atime", 12, 0, J_RIGHT,
190 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
191 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
192 N_("sort|a"),
193 N_("&Access time"), TRUE, TRUE,
194 string_file_atime,
195 (sortfn *) sort_atime
199 "ctime", 12, 0, J_RIGHT,
200 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
201 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
202 N_("sort|h"),
203 N_("C&hange time"), TRUE, TRUE,
204 string_file_ctime,
205 (sortfn *) sort_ctime
209 "perm", 10, 0, J_LEFT,
211 N_("Permission"), FALSE, TRUE,
212 string_file_permission,
213 NULL
217 "mode", 6, 0, J_RIGHT,
219 N_("Perm"), FALSE, TRUE,
220 string_file_perm_octal,
221 NULL
225 "nlink", 2, 0, J_RIGHT,
227 N_("Nl"), FALSE, TRUE,
228 string_file_nlinks, NULL
232 "inode", 5, 0, J_RIGHT,
233 /* TRANSLATORS: one single character to represent 'inode' sort mode */
234 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
235 N_("sort|i"),
236 N_("&Inode"), TRUE, TRUE,
237 string_inode,
238 (sortfn *) sort_inode
242 "nuid", 5, 0, J_RIGHT,
244 N_("UID"), FALSE, FALSE,
245 string_file_nuid,
246 NULL
250 "ngid", 5, 0, J_RIGHT,
252 N_("GID"), FALSE, FALSE,
253 string_file_ngid,
254 NULL
258 "owner", 8, 0, J_LEFT_FIT,
260 N_("Owner"), FALSE, TRUE,
261 string_file_owner,
262 NULL
266 "group", 8, 0, J_LEFT_FIT,
268 N_("Group"), FALSE, TRUE,
269 string_file_group,
270 NULL
274 "mark", 1, 0, J_RIGHT,
276 " ", FALSE, TRUE,
277 string_marked,
278 NULL
282 "|", 1, 0, J_RIGHT,
284 " ", FALSE, TRUE,
285 NULL,
286 NULL
290 "space", 1, 0, J_RIGHT,
292 " ", FALSE, TRUE,
293 string_space,
294 NULL
298 "dot", 1, 0, J_RIGHT,
300 " ", FALSE, FALSE,
301 string_dot,
302 NULL
306 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
309 /* *INDENT-ON* */
311 extern int saving_setup;
313 /*** file scope macro definitions ****************************************************************/
315 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
317 #define NORMAL 0
318 #define SELECTED 1
319 #define MARKED 2
320 #define MARKED_SELECTED 3
321 #define STATUS 5
323 /* This macro extracts the number of available lines in a panel */
324 #define llines(p) (p->widget.lines - 3 - (panels_options.show_mini_info ? 2 : 0))
326 /*** file scope type declarations ****************************************************************/
328 typedef enum
330 MARK_DONT_MOVE = 0,
331 MARK_DOWN = 1,
332 MARK_FORCE_DOWN = 2,
333 MARK_FORCE_UP = 3
334 } mark_act_t;
337 * This describes a format item. The parse_display_format routine parses
338 * the user specified format and creates a linked list of format_e structures.
340 typedef struct format_e
342 struct format_e *next;
343 int requested_field_len;
344 int field_len;
345 align_crt_t just_mode;
346 int expand;
347 const char *(*string_fn) (file_entry *, int len);
348 char *title;
349 const char *id;
350 } format_e;
352 /* File name scroll states */
353 typedef enum
355 FILENAME_NOSCROLL = 1,
356 FILENAME_SCROLL_LEFT = 2,
357 FILENAME_SCROLL_RIGHT = 4
358 } filename_scroll_flag_t;
360 /*** file scope variables ************************************************************************/
362 static char *panel_sort_up_sign = NULL;
363 static char *panel_sort_down_sign = NULL;
365 static char *panel_hiddenfiles_sign_show = NULL;
366 static char *panel_hiddenfiles_sign_hide = NULL;
367 static char *panel_history_prev_item_sign = NULL;
368 static char *panel_history_next_item_sign = NULL;
369 static char *panel_history_show_list_sign = NULL;
370 static char *panel_filename_scroll_left_char = NULL;
371 static char *panel_filename_scroll_right_char = NULL;
373 /* Panel that selection started */
374 static WPanel *mouse_mark_panel = NULL;
376 static int mouse_marking = 0;
377 static int state_mark = 0;
378 /*** file scope functions ************************************************************************/
379 /* --------------------------------------------------------------------------------------------- */
381 static void
382 set_colors (WPanel * panel)
384 (void) panel;
385 tty_set_normal_attrs ();
386 tty_setcolor (NORMAL_COLOR);
389 /* --------------------------------------------------------------------------------------------- */
390 /** Delete format string, it is a linked list */
392 static void
393 delete_format (format_e * format)
395 while (format != NULL)
397 format_e *next = format->next;
398 g_free (format->title);
399 g_free (format);
400 format = next;
404 /* --------------------------------------------------------------------------------------------- */
405 /** This code relies on the default justification!!! */
407 static void
408 add_permission_string (const char *dest, int width, file_entry * fe, int attr, int color,
409 int is_octal)
411 int i, r, l;
413 l = get_user_permissions (&fe->st);
415 if (is_octal)
417 /* Place of the access bit in octal mode */
418 l = width + l - 3;
419 r = l + 1;
421 else
423 /* The same to the triplet in string mode */
424 l = l * 3 + 1;
425 r = l + 3;
428 for (i = 0; i < width; i++)
430 if (i >= l && i < r)
432 if (attr == SELECTED || attr == MARKED_SELECTED)
433 tty_setcolor (MARKED_SELECTED_COLOR);
434 else
435 tty_setcolor (MARKED_COLOR);
437 else if (color >= 0)
438 tty_setcolor (color);
439 else
440 tty_lowlevel_setcolor (-color);
442 tty_print_char (dest[i]);
446 /* --------------------------------------------------------------------------------------------- */
447 /** String representations of various file attributes name */
449 static const char *
450 string_file_name (file_entry * fe, int len)
452 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
454 (void) len;
455 g_strlcpy (buffer, fe->fname, sizeof (buffer));
456 return buffer;
459 /* --------------------------------------------------------------------------------------------- */
461 static unsigned int
462 ilog10 (dev_t n)
464 unsigned int digits = 0;
467 digits++, n /= 10;
469 while (n != 0);
470 return digits;
473 /* --------------------------------------------------------------------------------------------- */
475 static void
476 format_device_number (char *buf, size_t bufsize, dev_t dev)
478 dev_t major_dev = major (dev);
479 dev_t minor_dev = minor (dev);
480 unsigned int major_digits = ilog10 (major_dev);
481 unsigned int minor_digits = ilog10 (minor_dev);
483 g_assert (bufsize >= 1);
484 if (major_digits + 1 + minor_digits + 1 <= bufsize)
486 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
488 else
490 g_strlcpy (buf, _("[dev]"), bufsize);
494 /* --------------------------------------------------------------------------------------------- */
495 /** size */
497 static const char *
498 string_file_size (file_entry * fe, int len)
500 static char buffer[BUF_TINY];
502 /* Don't ever show size of ".." since we don't calculate it */
503 if (!strcmp (fe->fname, ".."))
505 return _("UP--DIR");
508 #ifdef HAVE_STRUCT_STAT_ST_RDEV
509 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
510 format_device_number (buffer, len + 1, fe->st.st_rdev);
511 else
512 #endif
514 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
516 return buffer;
519 /* --------------------------------------------------------------------------------------------- */
520 /** bsize */
522 static const char *
523 string_file_size_brief (file_entry * fe, int len)
525 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
527 return _("SYMLINK");
530 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
532 return _("SUB-DIR");
535 return string_file_size (fe, len);
538 /* --------------------------------------------------------------------------------------------- */
539 /** This functions return a string representation of a file entry type */
541 static const char *
542 string_file_type (file_entry * fe, int len)
544 static char buffer[2];
546 (void) len;
547 if (S_ISDIR (fe->st.st_mode))
548 buffer[0] = PATH_SEP;
549 else if (S_ISLNK (fe->st.st_mode))
551 if (fe->f.link_to_dir)
552 buffer[0] = '~';
553 else if (fe->f.stale_link)
554 buffer[0] = '!';
555 else
556 buffer[0] = '@';
558 else if (S_ISCHR (fe->st.st_mode))
559 buffer[0] = '-';
560 else if (S_ISSOCK (fe->st.st_mode))
561 buffer[0] = '=';
562 else if (S_ISDOOR (fe->st.st_mode))
563 buffer[0] = '>';
564 else if (S_ISBLK (fe->st.st_mode))
565 buffer[0] = '+';
566 else if (S_ISFIFO (fe->st.st_mode))
567 buffer[0] = '|';
568 else if (S_ISNAM (fe->st.st_mode))
569 buffer[0] = '#';
570 else if (!S_ISREG (fe->st.st_mode))
571 buffer[0] = '?'; /* non-regular of unknown kind */
572 else if (is_exe (fe->st.st_mode))
573 buffer[0] = '*';
574 else
575 buffer[0] = ' ';
576 buffer[1] = '\0';
577 return buffer;
580 /* --------------------------------------------------------------------------------------------- */
581 /** mtime */
583 static const char *
584 string_file_mtime (file_entry * fe, int len)
586 (void) len;
587 return file_date (fe->st.st_mtime);
590 /* --------------------------------------------------------------------------------------------- */
591 /** atime */
593 static const char *
594 string_file_atime (file_entry * fe, int len)
596 (void) len;
597 return file_date (fe->st.st_atime);
600 /* --------------------------------------------------------------------------------------------- */
601 /** ctime */
603 static const char *
604 string_file_ctime (file_entry * fe, int len)
606 (void) len;
607 return file_date (fe->st.st_ctime);
610 /* --------------------------------------------------------------------------------------------- */
611 /** perm */
613 static const char *
614 string_file_permission (file_entry * fe, int len)
616 (void) len;
617 return string_perm (fe->st.st_mode);
620 /* --------------------------------------------------------------------------------------------- */
621 /** mode */
623 static const char *
624 string_file_perm_octal (file_entry * fe, int len)
626 static char buffer[10];
628 (void) len;
629 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
630 return buffer;
633 /* --------------------------------------------------------------------------------------------- */
634 /** nlink */
636 static const char *
637 string_file_nlinks (file_entry * fe, int len)
639 static char buffer[BUF_TINY];
641 (void) len;
642 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
643 return buffer;
646 /* --------------------------------------------------------------------------------------------- */
647 /** inode */
649 static const char *
650 string_inode (file_entry * fe, int len)
652 static char buffer[10];
654 (void) len;
655 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
656 return buffer;
659 /* --------------------------------------------------------------------------------------------- */
660 /** nuid */
662 static const char *
663 string_file_nuid (file_entry * fe, int len)
665 static char buffer[10];
667 (void) len;
668 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
669 return buffer;
672 /* --------------------------------------------------------------------------------------------- */
673 /** ngid */
675 static const char *
676 string_file_ngid (file_entry * fe, int len)
678 static char buffer[10];
680 (void) len;
681 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
682 return buffer;
685 /* --------------------------------------------------------------------------------------------- */
686 /** owner */
688 static const char *
689 string_file_owner (file_entry * fe, int len)
691 (void) len;
692 return get_owner (fe->st.st_uid);
695 /* --------------------------------------------------------------------------------------------- */
696 /** group */
698 static const char *
699 string_file_group (file_entry * fe, int len)
701 (void) len;
702 return get_group (fe->st.st_gid);
705 /* --------------------------------------------------------------------------------------------- */
706 /** mark */
708 static const char *
709 string_marked (file_entry * fe, int len)
711 (void) len;
712 return fe->f.marked ? "*" : " ";
715 /* --------------------------------------------------------------------------------------------- */
716 /** space */
718 static const char *
719 string_space (file_entry * fe, int len)
721 (void) fe;
722 (void) len;
723 return " ";
726 /* --------------------------------------------------------------------------------------------- */
727 /** dot */
729 static const char *
730 string_dot (file_entry * fe, int len)
732 (void) fe;
733 (void) len;
734 return ".";
737 /* --------------------------------------------------------------------------------------------- */
739 static int
740 file_compute_color (int attr, file_entry * fe)
742 switch (attr)
744 case SELECTED:
745 return (SELECTED_COLOR);
746 case MARKED:
747 return (MARKED_COLOR);
748 case MARKED_SELECTED:
749 return (MARKED_SELECTED_COLOR);
750 case STATUS:
751 return (NORMAL_COLOR);
752 case NORMAL:
753 default:
754 if (!panels_options.filetype_mode)
755 return (NORMAL_COLOR);
758 return mc_fhl_get_color (mc_filehighlight, fe);
761 /* --------------------------------------------------------------------------------------------- */
762 /** Formats the file number file_index of panel in the buffer dest */
764 static filename_scroll_flag_t
765 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
766 int isstatus, int *field_lenght)
768 int color, length, empty_line;
769 const char *txt;
770 format_e *format, *home;
771 file_entry *fe;
772 filename_scroll_flag_t res = FILENAME_NOSCROLL;
774 (void) dest;
775 (void) limit;
776 length = 0;
777 empty_line = (file_index >= panel->count);
778 home = (isstatus) ? panel->status_format : panel->format;
779 fe = &panel->dir.list[file_index];
780 *field_lenght = 0;
782 if (!empty_line)
783 color = file_compute_color (attr, fe);
784 else
785 color = NORMAL_COLOR;
786 for (format = home; format; format = format->next)
788 if (length == width)
789 break;
791 if (format->string_fn)
793 int len, perm;
794 const char *prepared_text;
795 int name_offset = 0;
797 if (empty_line)
798 txt = " ";
799 else
800 txt = (*format->string_fn) (fe, format->field_len);
802 len = format->field_len;
803 if (len + length > width)
804 len = width - length;
805 if (len <= 0)
806 break;
808 if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
810 int str_len;
811 int i;
813 *field_lenght = len + 1;
815 str_len = str_length (txt);
816 i = max (0, str_len - len);
817 panel->max_shift = max (panel->max_shift, i);
818 i = min (panel->content_shift, i);
820 if (i > -1)
822 name_offset = str_offset_to_pos (txt, i);
823 if (str_len > len)
825 res = FILENAME_SCROLL_LEFT;
826 if (str_length (txt + name_offset) > len)
827 res |= FILENAME_SCROLL_RIGHT;
832 perm = 0;
833 if (panels_options.permission_mode)
835 if (!strcmp (format->id, "perm"))
836 perm = 1;
837 else if (!strcmp (format->id, "mode"))
838 perm = 2;
841 if (color >= 0)
842 tty_setcolor (color);
843 else
844 tty_lowlevel_setcolor (-color);
846 if (!isstatus && panel->content_shift > -1)
847 prepared_text =
848 str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
849 else
850 prepared_text = str_fit_to_term (txt, len, format->just_mode);
852 if (perm)
853 add_permission_string (prepared_text, format->field_len, fe, attr, color, perm - 1);
854 else
855 tty_print_string (prepared_text);
857 length += len;
859 else
861 if (attr == SELECTED || attr == MARKED_SELECTED)
862 tty_setcolor (SELECTED_COLOR);
863 else
864 tty_setcolor (NORMAL_COLOR);
865 tty_print_one_vline (TRUE);
866 length++;
870 if (length < width)
872 int y, x;
874 tty_getyx (&y, &x);
875 tty_draw_hline (y, x, ' ', width - length);
878 return res;
881 /* --------------------------------------------------------------------------------------------- */
883 static void
884 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
886 int second_column = 0;
887 int width;
888 int offset = 0;
889 char buffer[BUF_MEDIUM];
890 filename_scroll_flag_t ret_frm;
891 int ypos = 0;
892 gboolean panel_is_split = !isstatus && panel->split;
893 int fln = 0;
895 width = panel->widget.cols - 2;
897 if (panel_is_split)
899 second_column = (file_index - panel->top_file) / llines (panel);
900 width = width / 2 - 1;
902 if (second_column != 0)
904 offset = 1 + width;
905 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
906 width = panel->widget.cols - offset - 2;
910 /* Nothing to paint */
911 if (width <= 0)
912 return;
914 if (mv)
916 int pos = file_index - panel->top_file;
918 if (panel_is_split)
919 ypos = pos % llines (panel);
920 else
921 ypos = pos;
923 ypos += 2;
924 widget_move (&panel->widget, ypos, offset + 1);
927 ret_frm = format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus, &fln);
929 if (panel_is_split)
931 if (second_column)
932 tty_print_char (' ');
933 else
935 tty_setcolor (NORMAL_COLOR);
936 tty_print_one_vline (TRUE);
939 if (ret_frm != FILENAME_NOSCROLL && mv)
941 if (!panel_is_split && fln > 0)
943 if (panel->list_type == list_long)
945 offset = width - fln + 1;
946 width = fln - 1;
948 else
950 width = fln;
953 widget_move (&panel->widget, ypos, offset);
954 tty_setcolor (NORMAL_COLOR);
955 tty_print_string (panel_filename_scroll_left_char);
957 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
959 widget_move (&panel->widget, ypos, offset + 1 + width);
960 tty_setcolor (NORMAL_COLOR);
961 tty_print_string (panel_filename_scroll_right_char);
966 /* --------------------------------------------------------------------------------------------- */
968 static void
969 display_mini_info (WPanel * panel)
971 if (!panels_options.show_mini_info)
972 return;
974 widget_move (&panel->widget, llines (panel) + 3, 1);
976 if (panel->searching)
978 tty_setcolor (INPUT_COLOR);
979 tty_print_char ('/');
980 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
981 return;
984 /* Status resolves links and show them */
985 set_colors (panel);
987 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
989 char link_target[MC_MAXPATHLEN];
990 vfs_path_t *lc_link_vpath;
991 int len;
993 lc_link_vpath =
994 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
995 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
996 vfs_path_free (lc_link_vpath);
997 if (len > 0)
999 link_target[len] = 0;
1000 tty_print_string ("-> ");
1001 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
1003 else
1004 tty_print_string (str_fit_to_term (_("<readlink failed>"),
1005 panel->widget.cols - 2, J_LEFT));
1007 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
1009 /* FIXME:
1010 * while loading directory (do_load_dir() and do_reload_dir()),
1011 * the actual stat info about ".." directory isn't got;
1012 * so just don't display incorrect info about ".." directory */
1013 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
1015 else
1016 /* Default behavior */
1017 repaint_file (panel, panel->selected, 0, STATUS, 1);
1020 /* --------------------------------------------------------------------------------------------- */
1022 static void
1023 paint_dir (WPanel * panel)
1025 int i;
1026 int color; /* Color value of the line */
1027 int items; /* Number of items */
1029 items = llines (panel) * (panel->split ? 2 : 1);
1030 /* reset max len of filename because we have the new max length for the new file list */
1031 panel->max_shift = -1;
1032 for (i = 0; i < items; i++)
1034 if (i + panel->top_file >= panel->count)
1035 color = 0;
1036 else
1038 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1039 color += (panel->selected == i + panel->top_file && panel->active);
1041 repaint_file (panel, i + panel->top_file, 1, color, 0);
1044 tty_set_normal_attrs ();
1047 /* --------------------------------------------------------------------------------------------- */
1049 static void
1050 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
1052 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
1053 int cols;
1055 if (panel->marked <= 0)
1056 return;
1058 buf = size_only ? b_bytes : buffer;
1059 cols = panel->widget.cols - 2;
1062 * This is a trick to use two ngettext() calls in one sentence.
1063 * First make "N bytes", then insert it into "X in M files".
1065 g_snprintf (b_bytes, sizeof (b_bytes),
1066 ngettext ("%s byte", "%s bytes", panel->total),
1067 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1068 if (!size_only)
1069 g_snprintf (buffer, sizeof (buffer),
1070 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1071 b_bytes, panel->marked);
1073 /* don't forget spaces around buffer content */
1074 buf = (char *) str_trunc (buf, cols - 4);
1076 if (x < 0)
1077 /* center in panel */
1078 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
1081 * y == llines (panel) + 2 for mini_info_separator
1082 * y == panel->widget.lines - 1 for panel bottom frame
1084 widget_move (&panel->widget, y, x);
1085 tty_setcolor (MARKED_COLOR);
1086 tty_printf (" %s ", buf);
1089 /* --------------------------------------------------------------------------------------------- */
1091 static void
1092 mini_info_separator (WPanel * panel)
1094 if (panels_options.show_mini_info)
1096 const int y = llines (panel) + 2;
1098 tty_setcolor (NORMAL_COLOR);
1099 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1100 ACS_HLINE, panel->widget.cols - 2);
1101 /* Status displays total marked size.
1102 * Centered in panel, full format. */
1103 display_total_marked_size (panel, y, -1, FALSE);
1107 /* --------------------------------------------------------------------------------------------- */
1109 static void
1110 show_free_space (WPanel * panel)
1112 /* Used to figure out how many free space we have */
1113 static struct my_statfs myfs_stats;
1114 /* Old current working directory for displaying free space */
1115 static char *old_cwd = NULL;
1116 char *tmp_path;
1118 /* Don't try to stat non-local fs */
1119 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1120 return;
1122 tmp_path = vfs_path_to_str (panel->cwd_vpath);
1123 if (old_cwd == NULL || strcmp (old_cwd, tmp_path) != 0)
1125 char rpath[PATH_MAX];
1127 init_my_statfs ();
1128 g_free (old_cwd);
1129 old_cwd = tmp_path;
1130 tmp_path = NULL;
1132 if (mc_realpath (old_cwd, rpath) == NULL)
1133 return;
1135 my_statfs (&myfs_stats, rpath);
1137 g_free (tmp_path);
1139 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1141 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1142 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1143 panels_options.kilobyte_si);
1144 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1145 panels_options.kilobyte_si);
1146 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1147 myfs_stats.total == 0 ? 0 :
1148 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1149 widget_move (&panel->widget, panel->widget.lines - 1,
1150 panel->widget.cols - 2 - (int) strlen (tmp));
1151 tty_setcolor (NORMAL_COLOR);
1152 tty_print_string (tmp);
1156 /* --------------------------------------------------------------------------------------------- */
1158 * Prepare path string for showing in panel's header.
1159 * Passwords will removed, also home dir will replaced by ~
1161 * @param panel WPanel object
1163 * @return newly allocated string.
1166 static char *
1167 panel_correct_path_to_show (WPanel * panel)
1169 vfs_path_t *last_vpath;
1170 const vfs_path_element_t *path_element;
1171 char *return_path;
1172 int elements_count;
1174 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1176 /* get last path element */
1177 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1180 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1181 strcmp (path_element->class->name, "extfs") == 0 ||
1182 strcmp (path_element->class->name, "tarfs") == 0))
1184 const char *archive_name;
1185 const vfs_path_element_t *prev_path_element;
1187 /* get previous path element for catching archive name */
1188 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1189 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1190 if (archive_name != NULL)
1192 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1194 else
1196 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1197 last_vpath->relative = TRUE;
1200 else
1202 last_vpath = vfs_path_new ();
1203 last_vpath->relative = TRUE;
1206 vfs_path_add_element (last_vpath, path_element);
1207 return_path =
1208 vfs_path_to_str_flags (last_vpath, 0,
1209 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1210 vfs_path_free (last_vpath);
1212 return return_path;
1215 /* --------------------------------------------------------------------------------------------- */
1217 * Get Current path element encoding
1219 * @param panel WPanel object
1221 * @return newly allocated string or NULL if path charset is same as system charset
1224 #ifdef HAVE_CHARSET
1225 static char *
1226 panel_get_encoding_info_str (WPanel * panel)
1228 char *ret_str = NULL;
1229 const vfs_path_element_t *path_element;
1231 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1232 if (path_element->encoding != NULL)
1233 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1235 return ret_str;
1237 #endif
1239 /* --------------------------------------------------------------------------------------------- */
1241 static void
1242 show_dir (WPanel * panel)
1244 gchar *tmp;
1245 set_colors (panel);
1246 draw_box (panel->widget.owner,
1247 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1249 if (panels_options.show_mini_info)
1251 widget_move (&panel->widget, llines (panel) + 2, 0);
1252 tty_print_alt_char (ACS_LTEE, FALSE);
1253 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1254 tty_print_alt_char (ACS_RTEE, FALSE);
1257 widget_move (&panel->widget, 0, 1);
1258 tty_print_string (panel_history_prev_item_sign);
1260 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1261 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1262 panel_history_next_item_sign);
1264 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1265 tty_print_string (tmp);
1267 g_free (tmp);
1269 widget_move (&panel->widget, 0, 3);
1271 if (panel->is_panelized)
1272 tty_printf (" %s ", _("Panelize"));
1273 #ifdef HAVE_CHARSET
1274 else
1276 tmp = panel_get_encoding_info_str (panel);
1277 if (tmp != NULL)
1279 tty_printf ("%s", tmp);
1280 widget_move (&panel->widget, 0, 3 + strlen (tmp));
1281 g_free (tmp);
1284 #endif
1286 if (panel->active)
1287 tty_setcolor (REVERSE_COLOR);
1289 tmp = panel_correct_path_to_show (panel);
1290 tty_printf (" %s ",
1291 str_term_trim (tmp, min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1292 g_free (tmp);
1294 if (!panels_options.show_mini_info)
1296 if (panel->marked == 0)
1298 /* Show size of curret file in the bottom of panel */
1299 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1301 char buffer[BUF_SMALL];
1303 g_snprintf (buffer, sizeof (buffer), " %s ",
1304 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1305 panels_options.kilobyte_si));
1306 tty_setcolor (NORMAL_COLOR);
1307 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1308 tty_print_string (buffer);
1311 else
1313 /* Show total size of marked files
1314 * In the bottom of panel, display size only. */
1315 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1319 show_free_space (panel);
1321 if (panel->active)
1322 tty_set_normal_attrs ();
1325 /* --------------------------------------------------------------------------------------------- */
1327 /* Returns the number of items in the given panel */
1328 static int
1329 ITEMS (WPanel * p)
1331 if (p->split)
1332 return llines (p) * 2;
1333 else
1334 return llines (p);
1337 /* --------------------------------------------------------------------------------------------- */
1339 static void
1340 adjust_top_file (WPanel * panel)
1342 int items = ITEMS (panel);
1344 if (panel->count <= items)
1346 /* If all files fit, show them all. */
1347 panel->top_file = 0;
1349 else
1351 int i;
1353 /* top_file has to be in the range [selected-items+1, selected] so that
1354 the selected file is visible.
1355 top_file should be in the range [0, count-items] so that there's
1356 no empty space wasted.
1357 Within these ranges, adjust it by as little as possible. */
1359 if (panel->top_file < 0)
1360 panel->top_file = 0;
1362 i = panel->selected - items + 1;
1363 if (panel->top_file < i)
1364 panel->top_file = i;
1366 i = panel->count - items;
1367 if (panel->top_file > i)
1368 panel->top_file = i;
1370 if (panel->top_file > panel->selected)
1371 panel->top_file = panel->selected;
1375 /* --------------------------------------------------------------------------------------------- */
1376 /** add "#enc:encodning" to end of path */
1377 /* if path end width a previous #enc:, only encoding is changed no additional
1378 * #enc: is appended
1379 * retun new string
1382 static char *
1383 panel_save_name (WPanel * panel)
1385 /* If the program is shuting down */
1386 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1387 return g_strdup (panel->panel_name);
1388 else
1389 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1392 /* --------------------------------------------------------------------------------------------- */
1394 static void
1395 directory_history_add (struct WPanel *panel, const vfs_path_t * vpath)
1397 char *tmp;
1399 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1400 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1401 panel->dir_history_current = panel->dir_history;
1404 /* --------------------------------------------------------------------------------------------- */
1406 /* "history_load" event handler */
1407 static gboolean
1408 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1409 gpointer init_data, gpointer data)
1411 WPanel *p = (WPanel *) init_data;
1412 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1414 (void) event_group_name;
1415 (void) event_name;
1417 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1419 if (ev->cfg != NULL)
1420 p->dir_history = history_load (ev->cfg, p->hist_name);
1421 else
1422 p->dir_history = history_get (p->hist_name);
1424 directory_history_add (p, p->cwd_vpath);
1427 return TRUE;
1430 /* --------------------------------------------------------------------------------------------- */
1432 /* "history_save" event handler */
1433 static gboolean
1434 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1435 gpointer init_data, gpointer data)
1437 WPanel *p = (WPanel *) init_data;
1439 (void) event_group_name;
1440 (void) event_name;
1442 if (p->dir_history != NULL)
1444 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1446 history_save (ev->cfg, p->hist_name, p->dir_history);
1449 return TRUE;
1452 /* --------------------------------------------------------------------------------------------- */
1454 static void
1455 panel_destroy (WPanel * p)
1457 size_t i;
1459 if (panels_options.auto_save_setup)
1461 char *name;
1463 name = panel_save_name (p);
1464 panel_save_setup (p, name);
1465 g_free (name);
1468 panel_clean_dir (p);
1470 /* clean history */
1471 if (p->dir_history != NULL)
1473 /* directory history is already saved before this moment */
1474 p->dir_history = g_list_first (p->dir_history);
1475 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1476 g_list_free (p->dir_history);
1478 g_free (p->hist_name);
1480 delete_format (p->format);
1481 delete_format (p->status_format);
1483 g_free (p->user_format);
1484 for (i = 0; i < LIST_TYPES; i++)
1485 g_free (p->user_status_format[i]);
1487 g_free (p->dir.list);
1488 g_free (p->panel_name);
1490 vfs_path_free (p->lwd_vpath);
1491 vfs_path_free (p->cwd_vpath);
1494 /* --------------------------------------------------------------------------------------------- */
1496 static void
1497 panel_format_modified (WPanel * panel)
1499 panel->format_modified = 1;
1502 /* --------------------------------------------------------------------------------------------- */
1504 static void
1505 panel_paint_sort_info (WPanel * panel)
1507 if (*panel->sort_info.sort_field->hotkey != '\0')
1509 const char *sort_sign =
1510 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1511 char *str;
1513 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1514 widget_move (&panel->widget, 1, 1);
1515 tty_print_string (str);
1516 g_free (str);
1520 /* --------------------------------------------------------------------------------------------- */
1522 static gchar *
1523 panel_get_title_without_hotkey (const char *title)
1525 char *translated_title;
1526 char *hkey;
1528 if (title == NULL)
1529 return NULL;
1530 if (title[0] == '\0')
1531 return g_strdup ("");
1533 translated_title = g_strdup (_(title));
1535 hkey = strchr (translated_title, '&');
1536 if ((hkey != NULL) && (hkey[1] != '\0'))
1537 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1539 return translated_title;
1542 /* --------------------------------------------------------------------------------------------- */
1544 static void
1545 paint_frame (WPanel * panel)
1547 int side, width;
1548 GString *format_txt;
1550 adjust_top_file (panel);
1552 widget_erase (&panel->widget);
1553 show_dir (panel);
1555 widget_move (&panel->widget, 1, 1);
1557 for (side = 0; side <= panel->split; side++)
1559 format_e *format;
1561 if (side)
1563 tty_setcolor (NORMAL_COLOR);
1564 tty_print_one_vline (TRUE);
1565 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1567 else if (panel->split)
1568 width = panel->widget.cols / 2 - 3;
1569 else
1570 width = panel->widget.cols - 2;
1572 format_txt = g_string_new ("");
1573 for (format = panel->format; format; format = format->next)
1575 if (format->string_fn)
1577 g_string_set_size (format_txt, 0);
1579 if (panel->list_type == list_long
1580 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1581 g_string_append (format_txt,
1582 panel->sort_info.reverse
1583 ? panel_sort_down_sign : panel_sort_up_sign);
1585 g_string_append (format_txt, format->title);
1586 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1588 g_string_append (format_txt, " [");
1589 g_string_append (format_txt, panel->filter);
1590 g_string_append (format_txt, "]");
1593 tty_setcolor (HEADER_COLOR);
1594 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1595 J_CENTER_LEFT));
1596 width -= format->field_len;
1598 else
1600 tty_setcolor (NORMAL_COLOR);
1601 tty_print_one_vline (TRUE);
1602 width--;
1605 g_string_free (format_txt, TRUE);
1607 if (width > 0)
1609 int y, x;
1611 tty_getyx (&y, &x);
1612 tty_draw_hline (y, x, ' ', width);
1616 if (panel->list_type != list_long)
1617 panel_paint_sort_info (panel);
1620 /* --------------------------------------------------------------------------------------------- */
1622 static const char *
1623 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1625 panel_display_t frame = frame_half;
1626 format = skip_separators (format);
1628 if (!strncmp (format, "full", 4))
1630 frame = frame_full;
1631 format += 4;
1633 else if (!strncmp (format, "half", 4))
1635 frame = frame_half;
1636 format += 4;
1639 if (!isstatus)
1641 panel->frame_size = frame;
1642 panel->split = 0;
1645 /* Now, the optional column specifier */
1646 format = skip_separators (format);
1648 if (*format == '1' || *format == '2')
1650 if (!isstatus)
1651 panel->split = *format == '2';
1652 format++;
1655 if (!isstatus)
1656 panel_update_cols (&(panel->widget), panel->frame_size);
1658 return skip_separators (format);
1661 /* Format is:
1663 all := panel_format? format
1664 panel_format := [full|half] [1|2]
1665 format := one_format_e
1666 | format , one_format_e
1668 one_format_e := just format.id [opt_size]
1669 just := [<=>]
1670 opt_size := : size [opt_expand]
1671 size := [0-9]+
1672 opt_expand := +
1676 /* --------------------------------------------------------------------------------------------- */
1678 static format_e *
1679 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1680 int *res_total_cols)
1682 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1683 int total_cols = 0; /* Used columns by the format */
1684 int set_justify; /* flag: set justification mode? */
1685 align_crt_t justify = J_LEFT; /* Which mode. */
1686 size_t i;
1688 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1690 *error = 0;
1692 if (i18n_timelength == 0)
1694 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1696 for (i = 0; panel_fields[i].id != NULL; i++)
1697 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1698 panel_fields[i].min_size = i18n_timelength;
1702 * This makes sure that the panel and mini status full/half mode
1703 * setting is equal
1705 format = parse_panel_size (panel, format, isstatus);
1707 while (*format)
1708 { /* format can be an empty string */
1709 int found = 0;
1711 darr = g_new0 (format_e, 1);
1713 /* I'm so ugly, don't look at me :-) */
1714 if (!home)
1715 home = old = darr;
1717 old->next = darr;
1718 darr->next = 0;
1719 old = darr;
1721 format = skip_separators (format);
1723 if (strchr ("<=>", *format))
1725 set_justify = 1;
1726 switch (*format)
1728 case '<':
1729 justify = J_LEFT;
1730 break;
1731 case '=':
1732 justify = J_CENTER;
1733 break;
1734 case '>':
1735 default:
1736 justify = J_RIGHT;
1737 break;
1739 format = skip_separators (format + 1);
1741 else
1742 set_justify = 0;
1744 for (i = 0; panel_fields[i].id != NULL; i++)
1746 size_t klen = strlen (panel_fields[i].id);
1748 if (strncmp (format, panel_fields[i].id, klen) != 0)
1749 continue;
1751 format += klen;
1753 darr->requested_field_len = panel_fields[i].min_size;
1754 darr->string_fn = panel_fields[i].string_fn;
1755 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1757 darr->id = panel_fields[i].id;
1758 darr->expand = panel_fields[i].expands;
1759 darr->just_mode = panel_fields[i].default_just;
1761 if (set_justify)
1763 if (IS_FIT (darr->just_mode))
1764 darr->just_mode = MAKE_FIT (justify);
1765 else
1766 darr->just_mode = justify;
1768 found = 1;
1770 format = skip_separators (format);
1772 /* If we have a size specifier */
1773 if (*format == ':')
1775 int req_length;
1777 /* If the size was specified, we don't want
1778 * auto-expansion by default
1780 darr->expand = 0;
1781 format++;
1782 req_length = atoi (format);
1783 darr->requested_field_len = req_length;
1785 format = skip_numbers (format);
1787 /* Now, if they insist on expansion */
1788 if (*format == '+')
1790 darr->expand = 1;
1791 format++;
1796 break;
1798 if (!found)
1800 char *tmp_format = g_strdup (format);
1802 int pos = min (8, strlen (format));
1803 delete_format (home);
1804 tmp_format[pos] = 0;
1805 *error =
1806 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1807 g_free (tmp_format);
1808 return 0;
1810 total_cols += darr->requested_field_len;
1813 *res_total_cols = total_cols;
1814 return home;
1817 /* --------------------------------------------------------------------------------------------- */
1819 static format_e *
1820 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1822 #define MAX_EXPAND 4
1823 int expand_top = 0; /* Max used element in expand */
1824 int usable_columns; /* Usable columns in the panel */
1825 int total_cols = 0;
1826 int i;
1827 format_e *darr, *home;
1829 if (!format)
1830 format = DEFAULT_USER_FORMAT;
1832 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1834 if (*error)
1835 return 0;
1837 panel->dirty = 1;
1839 /* Status needn't to be split */
1840 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1842 : (panel->split + 1))) - (!isstatus
1843 && panel->split);
1845 /* Look for the expandable fields and set field_len based on the requested field len */
1846 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1848 darr->field_len = darr->requested_field_len;
1849 if (darr->expand)
1850 expand_top++;
1853 /* If we used more columns than the available columns, adjust that */
1854 if (total_cols > usable_columns)
1856 int pdif, dif = total_cols - usable_columns;
1858 while (dif)
1860 pdif = dif;
1861 for (darr = home; darr; darr = darr->next)
1863 if (dif && darr->field_len - 1)
1865 darr->field_len--;
1866 dif--;
1870 /* avoid endless loop if num fields > 40 */
1871 if (pdif == dif)
1872 break;
1874 total_cols = usable_columns; /* give up, the rest should be truncated */
1877 /* Expand the available space */
1878 if ((usable_columns > total_cols) && expand_top)
1880 int spaces = (usable_columns - total_cols) / expand_top;
1881 int extra = (usable_columns - total_cols) % expand_top;
1883 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1884 if (darr->expand)
1886 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1887 i++;
1890 return home;
1893 /* --------------------------------------------------------------------------------------------- */
1894 /** Given the panel->view_type returns the format string to be parsed */
1896 static const char *
1897 panel_format (WPanel * panel)
1899 switch (panel->list_type)
1901 case list_long:
1902 return "full perm space nlink space owner space group space size space mtime space name";
1904 case list_brief:
1905 return "half 2 type name";
1907 case list_user:
1908 return panel->user_format;
1910 default:
1911 case list_full:
1912 return "half type name | size | mtime";
1916 /* --------------------------------------------------------------------------------------------- */
1918 static const char *
1919 mini_status_format (WPanel * panel)
1921 if (panel->user_mini_status)
1922 return panel->user_status_format[panel->list_type];
1924 switch (panel->list_type)
1926 case list_long:
1927 return "full perm space nlink space owner space group space size space mtime space name";
1929 case list_brief:
1930 return "half type name space bsize space perm space";
1932 case list_full:
1933 return "half type name";
1935 default:
1936 case list_user:
1937 return panel->user_format;
1941 /* */
1942 /* Panel operation commands */
1943 /* */
1945 /* --------------------------------------------------------------------------------------------- */
1946 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1948 static cb_ret_t
1949 maybe_cd (int move_up_dir)
1951 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1953 if (move_up_dir)
1955 vfs_path_t *up_dir;
1957 up_dir = vfs_path_from_str ("..");
1958 do_cd (up_dir, cd_exact);
1959 vfs_path_free (up_dir);
1960 return MSG_HANDLED;
1963 if (S_ISDIR (selection (current_panel)->st.st_mode)
1964 || link_isdir (selection (current_panel)))
1966 vfs_path_t *vpath;
1968 vpath = vfs_path_from_str (selection (current_panel)->fname);
1969 do_cd (vpath, cd_exact);
1970 vfs_path_free (vpath);
1971 return MSG_HANDLED;
1974 return MSG_NOT_HANDLED;
1977 /* --------------------------------------------------------------------------------------------- */
1979 /* if command line is empty then do 'cd ..' */
1980 static cb_ret_t
1981 force_maybe_cd (void)
1983 if (cmdline->buffer[0] == '\0')
1985 vfs_path_t *up_dir = vfs_path_from_str ("..");
1986 do_cd (up_dir, cd_exact);
1987 vfs_path_free (up_dir);
1988 return MSG_HANDLED;
1990 return MSG_NOT_HANDLED;
1993 /* --------------------------------------------------------------------------------------------- */
1995 static void
1996 unselect_item (WPanel * panel)
1998 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
2001 /* --------------------------------------------------------------------------------------------- */
2003 static void
2004 move_down (WPanel * panel)
2006 if (panel->selected + 1 == panel->count)
2007 return;
2009 unselect_item (panel);
2010 panel->selected++;
2011 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
2013 /* Scroll window half screen */
2014 panel->top_file += ITEMS (panel) / 2;
2015 if (panel->top_file > panel->count - ITEMS (panel))
2016 panel->top_file = panel->count - ITEMS (panel);
2017 paint_dir (panel);
2019 select_item (panel);
2022 /* --------------------------------------------------------------------------------------------- */
2024 static void
2025 move_up (WPanel * panel)
2027 if (panel->selected == 0)
2028 return;
2030 unselect_item (panel);
2031 panel->selected--;
2032 if (panels_options.scroll_pages && panel->selected < panel->top_file)
2034 /* Scroll window half screen */
2035 panel->top_file -= ITEMS (panel) / 2;
2036 if (panel->top_file < 0)
2037 panel->top_file = 0;
2038 paint_dir (panel);
2040 select_item (panel);
2043 /* --------------------------------------------------------------------------------------------- */
2044 /** Changes the selection by lines (may be negative) */
2046 static void
2047 move_selection (WPanel * panel, int lines)
2049 int new_pos;
2050 int adjust = 0;
2052 new_pos = panel->selected + lines;
2053 if (new_pos >= panel->count)
2054 new_pos = panel->count - 1;
2056 if (new_pos < 0)
2057 new_pos = 0;
2059 unselect_item (panel);
2060 panel->selected = new_pos;
2062 if (panel->selected - panel->top_file >= ITEMS (panel))
2064 panel->top_file += lines;
2065 adjust = 1;
2068 if (panel->selected - panel->top_file < 0)
2070 panel->top_file += lines;
2071 adjust = 1;
2074 if (adjust)
2076 if (panel->top_file > panel->selected)
2077 panel->top_file = panel->selected;
2078 if (panel->top_file < 0)
2079 panel->top_file = 0;
2080 paint_dir (panel);
2082 select_item (panel);
2085 /* --------------------------------------------------------------------------------------------- */
2087 static cb_ret_t
2088 move_left (WPanel * panel)
2090 if (panel->split)
2092 move_selection (panel, -llines (panel));
2093 return MSG_HANDLED;
2095 else
2096 return maybe_cd (1); /* cd .. */
2099 /* --------------------------------------------------------------------------------------------- */
2101 static cb_ret_t
2102 move_right (WPanel * panel)
2104 if (panel->split)
2106 move_selection (panel, llines (panel));
2107 return MSG_HANDLED;
2109 else
2110 return maybe_cd (0); /* cd (selection) */
2113 /* --------------------------------------------------------------------------------------------- */
2115 static void
2116 prev_page (WPanel * panel)
2118 int items;
2120 if (!panel->selected && !panel->top_file)
2121 return;
2122 unselect_item (panel);
2123 items = ITEMS (panel);
2124 if (panel->top_file < items)
2125 items = panel->top_file;
2126 if (!items)
2127 panel->selected = 0;
2128 else
2129 panel->selected -= items;
2130 panel->top_file -= items;
2132 select_item (panel);
2133 paint_dir (panel);
2136 /* --------------------------------------------------------------------------------------------- */
2138 static void
2139 goto_parent_dir (WPanel * panel)
2141 if (!panel->is_panelized)
2143 vfs_path_t *up_dir;
2145 up_dir = vfs_path_from_str ("..");
2146 do_cd (up_dir, cd_exact);
2147 vfs_path_free (up_dir);
2149 else
2151 char *fname = panel->dir.list[panel->selected].fname;
2152 const char *bname;
2153 vfs_path_t *dname_vpath;
2155 if (g_path_is_absolute (fname))
2156 fname = g_strdup (fname);
2157 else
2159 char *tmp_root;
2161 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2162 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2163 g_free (tmp_root);
2166 bname = x_basename (fname);
2168 if (bname == fname)
2169 dname_vpath = vfs_path_from_str (".");
2170 else
2172 char *dname;
2174 dname = g_strndup (fname, bname - fname);
2175 dname_vpath = vfs_path_from_str (dname);
2176 g_free (dname);
2179 do_cd (dname_vpath, cd_exact);
2180 try_to_select (panel, bname);
2182 vfs_path_free (dname_vpath);
2183 g_free (fname);
2187 /* --------------------------------------------------------------------------------------------- */
2189 static void
2190 next_page (WPanel * panel)
2192 int items;
2194 if (panel->selected == panel->count - 1)
2195 return;
2196 unselect_item (panel);
2197 items = ITEMS (panel);
2198 if (panel->top_file > panel->count - 2 * items)
2199 items = panel->count - items - panel->top_file;
2200 if (panel->top_file + items < 0)
2201 items = -panel->top_file;
2202 if (!items)
2203 panel->selected = panel->count - 1;
2204 else
2205 panel->selected += items;
2206 panel->top_file += items;
2208 select_item (panel);
2209 paint_dir (panel);
2212 /* --------------------------------------------------------------------------------------------- */
2214 static void
2215 goto_child_dir (WPanel * panel)
2217 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2219 vfs_path_t *vpath;
2221 vpath = vfs_path_from_str (selection (panel)->fname);
2222 do_cd (vpath, cd_exact);
2223 vfs_path_free (vpath);
2227 /* --------------------------------------------------------------------------------------------- */
2229 static void
2230 goto_top_file (WPanel * panel)
2232 unselect_item (panel);
2233 panel->selected = panel->top_file;
2234 select_item (panel);
2237 /* --------------------------------------------------------------------------------------------- */
2239 static void
2240 goto_middle_file (WPanel * panel)
2242 unselect_item (panel);
2243 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2244 select_item (panel);
2247 /* --------------------------------------------------------------------------------------------- */
2249 static void
2250 goto_bottom_file (WPanel * panel)
2252 unselect_item (panel);
2253 panel->selected = panel->top_file + ITEMS (panel) - 1;
2254 select_item (panel);
2257 /* --------------------------------------------------------------------------------------------- */
2259 static void
2260 move_home (WPanel * panel)
2262 if (panel->selected == 0)
2263 return;
2265 unselect_item (panel);
2267 if (panels_options.torben_fj_mode)
2269 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2271 if (panel->selected > middle_pos)
2273 goto_middle_file (panel);
2274 return;
2276 if (panel->selected != panel->top_file)
2278 goto_top_file (panel);
2279 return;
2283 panel->top_file = 0;
2284 panel->selected = 0;
2286 paint_dir (panel);
2287 select_item (panel);
2290 /* --------------------------------------------------------------------------------------------- */
2292 static void
2293 move_end (WPanel * panel)
2295 if (panel->selected == panel->count - 1)
2296 return;
2298 unselect_item (panel);
2300 if (panels_options.torben_fj_mode)
2302 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2304 if (panel->selected < middle_pos)
2306 goto_middle_file (panel);
2307 return;
2309 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2311 goto_bottom_file (panel);
2312 return;
2316 panel->selected = panel->count - 1;
2317 paint_dir (panel);
2318 select_item (panel);
2321 /* --------------------------------------------------------------------------------------------- */
2323 static void
2324 do_mark_file (WPanel * panel, mark_act_t do_move)
2326 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2327 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2328 move_down (panel);
2329 else if (do_move == MARK_FORCE_UP)
2330 move_up (panel);
2333 /* --------------------------------------------------------------------------------------------- */
2335 static void
2336 mark_file (WPanel * panel)
2338 do_mark_file (panel, MARK_DOWN);
2341 /* --------------------------------------------------------------------------------------------- */
2343 static void
2344 mark_file_up (WPanel * panel)
2346 do_mark_file (panel, MARK_FORCE_UP);
2349 /* --------------------------------------------------------------------------------------------- */
2351 static void
2352 mark_file_down (WPanel * panel)
2354 do_mark_file (panel, MARK_FORCE_DOWN);
2357 /* --------------------------------------------------------------------------------------------- */
2359 static void
2360 mark_file_right (WPanel * panel)
2362 int lines = llines (panel);
2364 if (state_mark < 0)
2365 state_mark = selection (panel)->f.marked ? 0 : 1;
2367 lines = min (lines, panel->count - panel->selected - 1);
2368 for (; lines != 0; lines--)
2370 do_file_mark (panel, panel->selected, state_mark);
2371 move_down (panel);
2373 do_file_mark (panel, panel->selected, state_mark);
2376 /* --------------------------------------------------------------------------------------------- */
2378 static void
2379 mark_file_left (WPanel * panel)
2381 int lines = llines (panel);
2383 if (state_mark < 0)
2384 state_mark = selection (panel)->f.marked ? 0 : 1;
2386 lines = min (lines, panel->selected + 1);
2387 for (; lines != 0; lines--)
2389 do_file_mark (panel, panel->selected, state_mark);
2390 move_up (panel);
2392 do_file_mark (panel, panel->selected, state_mark);
2395 /* --------------------------------------------------------------------------------------------- */
2396 /** Incremental search of a file name in the panel.
2397 * @param panel instance of WPanel structure
2398 * @param c_code key code
2401 static void
2402 do_search (WPanel * panel, int c_code)
2404 size_t l;
2405 int i, sel;
2406 gboolean wrapped = FALSE;
2407 char *act;
2408 mc_search_t *search;
2409 char *reg_exp, *esc_str;
2410 gboolean is_found = FALSE;
2412 l = strlen (panel->search_buffer);
2413 if (c_code == KEY_BACKSPACE)
2415 if (l != 0)
2417 act = panel->search_buffer + l;
2418 str_prev_noncomb_char (&act, panel->search_buffer);
2419 act[0] = '\0';
2421 panel->search_chpoint = 0;
2423 else
2425 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2427 panel->search_char[panel->search_chpoint] = c_code;
2428 panel->search_chpoint++;
2431 if (panel->search_chpoint > 0)
2433 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2435 case -2:
2436 return;
2437 case -1:
2438 panel->search_chpoint = 0;
2439 return;
2440 default:
2441 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2443 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2444 l += panel->search_chpoint;
2445 *(panel->search_buffer + l) = '\0';
2446 panel->search_chpoint = 0;
2452 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2453 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2454 search = mc_search_new (esc_str, -1);
2455 search->search_type = MC_SEARCH_T_GLOB;
2456 search->is_entire_line = TRUE;
2457 switch (panels_options.qsearch_mode)
2459 case QSEARCH_CASE_SENSITIVE:
2460 search->is_case_sensitive = TRUE;
2461 break;
2462 case QSEARCH_CASE_INSENSITIVE:
2463 search->is_case_sensitive = FALSE;
2464 break;
2465 default:
2466 search->is_case_sensitive = panel->sort_info.case_sensitive;
2467 break;
2469 sel = panel->selected;
2470 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2472 if (i >= panel->count)
2474 i = 0;
2475 if (wrapped)
2476 break;
2477 wrapped = TRUE;
2479 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2481 sel = i;
2482 is_found = TRUE;
2483 break;
2486 if (is_found)
2488 unselect_item (panel);
2489 panel->selected = sel;
2490 select_item (panel);
2491 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2493 else if (c_code != KEY_BACKSPACE)
2495 act = panel->search_buffer + l;
2496 str_prev_noncomb_char (&act, panel->search_buffer);
2497 act[0] = '\0';
2499 mc_search_free (search);
2500 g_free (reg_exp);
2501 g_free (esc_str);
2504 /* --------------------------------------------------------------------------------------------- */
2505 /** Start new search.
2506 * @param panel instance of WPanel structure
2509 static void
2510 start_search (WPanel * panel)
2512 if (panel->searching)
2514 if (panel->selected + 1 == panel->count)
2515 panel->selected = 0;
2516 else
2517 move_down (panel);
2519 /* in case if there was no search string we need to recall
2520 previous string, with which we ended previous searching */
2521 if (panel->search_buffer[0] == '\0')
2522 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2523 sizeof (panel->search_buffer));
2525 do_search (panel, 0);
2527 else
2529 panel->searching = TRUE;
2530 panel->search_buffer[0] = '\0';
2531 panel->search_char[0] = '\0';
2532 panel->search_chpoint = 0;
2533 display_mini_info (panel);
2534 mc_refresh ();
2538 /* --------------------------------------------------------------------------------------------- */
2540 static void
2541 stop_search (WPanel * panel)
2543 panel->searching = FALSE;
2545 /* if user had overrdied search string, we need to store it
2546 to the previous_search_buffer */
2547 if (panel->search_buffer[0] != '\0')
2548 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2549 sizeof (panel->prev_search_buffer));
2551 display_mini_info (panel);
2554 /* --------------------------------------------------------------------------------------------- */
2555 /** Return 1 if the Enter key has been processed, 0 otherwise */
2557 static int
2558 do_enter_on_file_entry (file_entry * fe)
2560 vfs_path_t *full_name_vpath;
2561 gboolean ok;
2564 * Directory or link to directory - change directory.
2565 * Try the same for the entries on which mc_lstat() has failed.
2567 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2569 vfs_path_t *fname_vpath;
2571 fname_vpath = vfs_path_from_str (fe->fname);
2572 if (!do_cd (fname_vpath, cd_exact))
2573 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2574 vfs_path_free (fname_vpath);
2575 return 1;
2578 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2580 /* Try associated command */
2581 if (regex_command (full_name_vpath, "Open") != 0)
2583 vfs_path_free (full_name_vpath);
2584 return 1;
2587 /* Check if the file is executable */
2588 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2589 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2590 vfs_path_free (full_name_vpath);
2591 if (!ok)
2592 return 0;
2594 if (confirm_execute)
2596 if (query_dialog
2597 (_("The Midnight Commander"),
2598 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2599 return 1;
2602 if (!vfs_current_is_local ())
2604 int ret;
2605 vfs_path_t *tmp_vpath;
2607 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2608 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2609 vfs_path_free (tmp_vpath);
2610 /* We took action only if the dialog was shown or the execution
2611 * was successful */
2612 return confirm_execute || (ret == 0);
2616 char *tmp = name_quote (fe->fname, 0);
2617 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2618 g_free (tmp);
2619 shell_execute (cmd, 0);
2620 g_free (cmd);
2623 #ifdef HAVE_CHARSET
2624 mc_global.source_codepage = default_source_codepage;
2625 #endif
2627 return 1;
2630 /* --------------------------------------------------------------------------------------------- */
2632 static int
2633 do_enter (WPanel * panel)
2635 return do_enter_on_file_entry (selection (panel));
2638 /* --------------------------------------------------------------------------------------------- */
2640 static void
2641 chdir_other_panel (WPanel * panel)
2643 const file_entry *entry = &panel->dir.list[panel->selected];
2645 vfs_path_t *new_dir_vpath;
2646 char *sel_entry = NULL;
2648 if (get_other_type () != view_listing)
2650 set_display_type (get_other_index (), view_listing);
2653 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2654 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2655 else
2657 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2658 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2661 change_panel ();
2662 do_cd (new_dir_vpath, cd_exact);
2663 vfs_path_free (new_dir_vpath);
2665 if (sel_entry)
2666 try_to_select (current_panel, sel_entry);
2667 change_panel ();
2669 move_down (panel);
2672 /* --------------------------------------------------------------------------------------------- */
2674 * Make the current directory of the current panel also the current
2675 * directory of the other panel. Put the other panel to the listing
2676 * mode if needed. If the current panel is panelized, the other panel
2677 * doesn't become panelized.
2680 static void
2681 panel_sync_other (const WPanel * panel)
2683 if (get_other_type () != view_listing)
2685 set_display_type (get_other_index (), view_listing);
2688 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2690 /* try to select current filename on the other panel */
2691 if (!panel->is_panelized)
2693 try_to_select (other_panel, selection (panel)->fname);
2697 /* --------------------------------------------------------------------------------------------- */
2699 static void
2700 chdir_to_readlink (WPanel * panel)
2702 vfs_path_t *new_dir_vpath;
2703 char buffer[MC_MAXPATHLEN], *p;
2704 int i;
2705 struct stat st;
2706 vfs_path_t *panel_fname_vpath;
2707 gboolean ok;
2709 if (get_other_type () != view_listing)
2710 return;
2712 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2713 return;
2715 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2716 if (i < 0)
2717 return;
2719 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2720 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2721 vfs_path_free (panel_fname_vpath);
2722 if (!ok)
2723 return;
2725 buffer[i] = 0;
2726 if (!S_ISDIR (st.st_mode))
2728 p = strrchr (buffer, PATH_SEP);
2729 if (p && !p[1])
2731 *p = 0;
2732 p = strrchr (buffer, PATH_SEP);
2734 if (!p)
2735 return;
2736 p[1] = 0;
2738 if (*buffer == PATH_SEP)
2739 new_dir_vpath = vfs_path_from_str (buffer);
2740 else
2741 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2743 change_panel ();
2744 do_cd (new_dir_vpath, cd_exact);
2745 vfs_path_free (new_dir_vpath);
2746 change_panel ();
2748 move_down (panel);
2751 /* --------------------------------------------------------------------------------------------- */
2753 static gsize
2754 panel_get_format_field_count (WPanel * panel)
2756 format_e *format;
2757 gsize lc_index;
2758 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2759 return lc_index;
2762 /* --------------------------------------------------------------------------------------------- */
2764 function return 0 if not found and REAL_INDEX+1 if found
2767 static gsize
2768 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2770 format_e *format;
2771 gsize lc_index;
2773 for (lc_index = 1, format = panel->format;
2774 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2775 if (format == NULL)
2776 lc_index = 0;
2778 return lc_index;
2781 /* --------------------------------------------------------------------------------------------- */
2783 static format_e *
2784 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2786 format_e *format;
2787 for (format = panel->format;
2788 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2789 return format;
2792 /* --------------------------------------------------------------------------------------------- */
2794 static const panel_field_t *
2795 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2797 const panel_field_t *pfield;
2798 format_e *format;
2800 format = panel_get_format_field_by_index (panel, lc_index);
2801 if (format == NULL)
2802 return NULL;
2803 pfield = panel_get_field_by_title (format->title);
2804 if (pfield == NULL)
2805 return NULL;
2806 if (pfield->sort_routine == NULL)
2807 return NULL;
2808 return pfield;
2811 /* --------------------------------------------------------------------------------------------- */
2813 static void
2814 panel_toggle_sort_order_prev (WPanel * panel)
2816 gsize lc_index, i;
2817 gchar *title;
2819 const panel_field_t *pfield = NULL;
2821 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2822 lc_index = panel_get_format_field_index_by_name (panel, title);
2823 g_free (title);
2825 if (lc_index > 1)
2827 /* search for prev sortable column in panel format */
2828 for (i = lc_index - 1;
2829 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2832 if (pfield == NULL)
2834 /* Sortable field not found. Try to search in each array */
2835 for (i = panel_get_format_field_count (panel);
2836 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2839 if (pfield != NULL)
2841 panel->sort_info.sort_field = pfield;
2842 panel_set_sort_order (panel, pfield);
2846 /* --------------------------------------------------------------------------------------------- */
2848 static void
2849 panel_toggle_sort_order_next (WPanel * panel)
2851 gsize lc_index, i;
2852 const panel_field_t *pfield = NULL;
2853 gsize format_field_count;
2854 gchar *title;
2856 format_field_count = panel_get_format_field_count (panel);
2857 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2858 lc_index = panel_get_format_field_index_by_name (panel, title);
2859 g_free (title);
2861 if (lc_index != 0 && lc_index != format_field_count)
2863 /* search for prev sortable column in panel format */
2864 for (i = lc_index;
2865 i != format_field_count
2866 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2869 if (pfield == NULL)
2871 /* Sortable field not found. Try to search in each array */
2872 for (i = 0;
2873 i != format_field_count
2874 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2877 if (pfield != NULL)
2879 panel->sort_info.sort_field = pfield;
2880 panel_set_sort_order (panel, pfield);
2884 /* --------------------------------------------------------------------------------------------- */
2886 static void
2887 panel_select_sort_order (WPanel * panel)
2889 const panel_field_t *sort_order;
2891 sort_order = sort_box (&panel->sort_info);
2892 if (sort_order != NULL)
2894 panel->sort_info.sort_field = sort_order;
2895 panel_set_sort_order (panel, sort_order);
2899 /* --------------------------------------------------------------------------------------------- */
2902 * panel_content_scroll_left:
2903 * @param panel the pointer to the panel on which we operate
2905 * scroll long filename to the left (decrement scroll pointer)
2909 static void
2910 panel_content_scroll_left (WPanel * panel)
2912 if (panel->content_shift > -1)
2914 if (panel->content_shift > panel->max_shift)
2915 panel->content_shift = panel->max_shift;
2917 panel->content_shift--;
2918 show_dir (panel);
2919 paint_dir (panel);
2924 /* --------------------------------------------------------------------------------------------- */
2927 * panel_content_scroll_right:
2928 * @param panel the pointer to the panel on which we operate
2930 * scroll long filename to the right (increment scroll pointer)
2934 static void
2935 panel_content_scroll_right (WPanel * panel)
2937 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
2939 panel->content_shift++;
2940 show_dir (panel);
2941 paint_dir (panel);
2945 /* --------------------------------------------------------------------------------------------- */
2947 static void
2948 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2950 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2952 const panel_field_t *sort_order;
2954 sort_order = panel_get_field_by_id (name);
2955 if (sort_order == NULL)
2956 return;
2957 panel->sort_info.sort_field = sort_order;
2959 else
2960 panel->sort_info.reverse = !panel->sort_info.reverse;
2962 panel_set_sort_order (panel, panel->sort_info.sort_field);
2965 /* --------------------------------------------------------------------------------------------- */
2967 * If we moved to the parent directory move the selection pointer to
2968 * the old directory name; If we leave VFS dir, remove FS specificator.
2970 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2973 static const char *
2974 get_parent_dir_name (const char *cwd, const char *lwd)
2976 size_t llen, clen;
2977 const char *p;
2979 llen = strlen (lwd);
2980 clen = strlen (cwd);
2982 if (llen <= clen)
2983 return NULL;
2985 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2987 if (p == NULL)
2989 p = strrchr (lwd, PATH_SEP);
2991 if ((p != NULL)
2992 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2993 && (clen == (size_t) (p - lwd)
2994 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2995 return (p + 1);
2997 return NULL;
3000 /* skip VFS prefix */
3001 while (--p > lwd && *p != PATH_SEP)
3003 /* get last component */
3004 while (--p > lwd && *p != PATH_SEP)
3007 /* return last component */
3008 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
3011 /* --------------------------------------------------------------------------------------------- */
3012 /** Wrapper for do_subshell_chdir, check for availability of subshell */
3014 static void
3015 subshell_chdir (const vfs_path_t * vpath)
3017 #ifdef ENABLE_SUBSHELL
3018 if (mc_global.tty.use_subshell && vfs_current_is_local ())
3019 do_subshell_chdir (vpath, FALSE, TRUE);
3020 #else /* ENABLE_SUBSHELL */
3021 (void) vpath;
3022 #endif /* ENABLE_SUBSHELL */
3025 /* --------------------------------------------------------------------------------------------- */
3027 * Changes the current directory of the panel.
3028 * Don't record change in the directory history.
3031 static gboolean
3032 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
3034 char *olddir;
3036 olddir = vfs_path_to_str (panel->cwd_vpath);
3038 /* Convert *new_path to a suitable pathname, handle ~user */
3039 if (cd_type == cd_parse_command)
3041 const vfs_path_element_t *element;
3043 element = vfs_path_get_by_index (new_dir_vpath, 0);
3044 if (strcmp (element->path, "-") == 0)
3045 new_dir_vpath = panel->lwd_vpath;
3048 if (mc_chdir (new_dir_vpath) == -1)
3050 panel_set_cwd (panel, olddir);
3051 g_free (olddir);
3052 return FALSE;
3055 /* Success: save previous directory, shutdown status of previous dir */
3056 panel_set_lwd (panel, olddir);
3057 input_free_completions (cmdline);
3059 vfs_path_free (panel->cwd_vpath);
3060 vfs_setup_cwd ();
3061 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3063 vfs_release_path (olddir);
3065 subshell_chdir (panel->cwd_vpath);
3067 /* Reload current panel */
3068 panel_clean_dir (panel);
3071 char *tmp_path;
3073 panel->count =
3074 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
3075 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3076 panel->sort_info.exec_first, panel->filter);
3077 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3078 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
3079 g_free (tmp_path);
3082 load_hint (0);
3083 panel->dirty = 1;
3084 update_xterm_title_path ();
3086 g_free (olddir);
3088 return TRUE;
3091 /* --------------------------------------------------------------------------------------------- */
3093 static void
3094 directory_history_next (WPanel * panel)
3096 gboolean ok;
3100 GList *next;
3102 ok = TRUE;
3103 next = g_list_next (panel->dir_history_current);
3104 if (next != NULL)
3106 vfs_path_t *data_vpath;
3108 data_vpath = vfs_path_from_str ((char *) next->data);
3109 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3110 vfs_path_free (data_vpath);
3111 panel->dir_history_current = next;
3113 /* skip directories that present in history but absent in file system */
3115 while (!ok);
3118 /* --------------------------------------------------------------------------------------------- */
3120 static void
3121 directory_history_prev (WPanel * panel)
3123 gboolean ok;
3127 GList *prev;
3129 ok = TRUE;
3130 prev = g_list_previous (panel->dir_history_current);
3131 if (prev != NULL)
3133 vfs_path_t *data_vpath;
3135 data_vpath = vfs_path_from_str ((char *) prev->data);
3136 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3137 vfs_path_free (data_vpath);
3138 panel->dir_history_current = prev;
3140 /* skip directories that present in history but absent in file system */
3142 while (!ok);
3145 /* --------------------------------------------------------------------------------------------- */
3147 static void
3148 directory_history_list (WPanel * panel)
3150 char *s;
3151 gboolean ok = FALSE;
3152 size_t pos;
3154 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3156 s = history_show (&panel->dir_history, &panel->widget, pos);
3157 if (s != NULL)
3159 vfs_path_t *s_vpath;
3161 s_vpath = vfs_path_from_str (s);
3162 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3163 if (ok)
3164 directory_history_add (panel, panel->cwd_vpath);
3165 else
3166 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3167 vfs_path_free (s_vpath);
3168 g_free (s);
3171 if (!ok)
3173 /* Since history is fully modified in history_show(), panel->dir_history actually
3174 * points to the invalid place. Try restore current postition here. */
3176 size_t i;
3178 panel->dir_history_current = panel->dir_history;
3180 for (i = 0; i <= pos; i++)
3182 GList *prev;
3184 prev = g_list_previous (panel->dir_history_current);
3185 if (prev == NULL)
3186 break;
3188 panel->dir_history_current = prev;
3193 /* --------------------------------------------------------------------------------------------- */
3195 static cb_ret_t
3196 panel_execute_cmd (WPanel * panel, unsigned long command)
3198 int res = MSG_HANDLED;
3200 if (command != CK_Search)
3201 stop_search (panel);
3204 switch (command)
3206 case CK_Up:
3207 case CK_Down:
3208 case CK_Left:
3209 case CK_Right:
3210 case CK_Bottom:
3211 case CK_Top:
3212 case CK_PageDown:
3213 case CK_PageUp:
3214 /* reset state of marks flag */
3215 state_mark = -1;
3216 break;
3218 switch (command)
3220 case CK_PanelOtherCd:
3221 chdir_other_panel (panel);
3222 break;
3223 case CK_PanelOtherCdLink:
3224 chdir_to_readlink (panel);
3225 break;
3226 case CK_CopySingle:
3227 copy_cmd_local ();
3228 break;
3229 case CK_DeleteSingle:
3230 delete_cmd_local ();
3231 break;
3232 case CK_Enter:
3233 do_enter (panel);
3234 break;
3235 case CK_ViewRaw:
3236 view_raw_cmd ();
3237 break;
3238 case CK_EditNew:
3239 edit_cmd_new ();
3240 break;
3241 case CK_MoveSingle:
3242 rename_cmd_local ();
3243 break;
3244 case CK_SelectInvert:
3245 select_invert_cmd ();
3246 break;
3247 case CK_Select:
3248 select_cmd ();
3249 break;
3250 case CK_Unselect:
3251 unselect_cmd ();
3252 break;
3253 case CK_PageDown:
3254 next_page (panel);
3255 break;
3256 case CK_PageUp:
3257 prev_page (panel);
3258 break;
3259 case CK_CdChild:
3260 goto_child_dir (panel);
3261 break;
3262 case CK_CdParent:
3263 goto_parent_dir (panel);
3264 break;
3265 case CK_History:
3266 directory_history_list (panel);
3267 break;
3268 case CK_HistoryNext:
3269 directory_history_next (panel);
3270 break;
3271 case CK_HistoryPrev:
3272 directory_history_prev (panel);
3273 break;
3274 case CK_BottomOnScreen:
3275 goto_bottom_file (panel);
3276 break;
3277 case CK_MiddleOnScreen:
3278 goto_middle_file (panel);
3279 break;
3280 case CK_TopOnScreen:
3281 goto_top_file (panel);
3282 break;
3283 case CK_Mark:
3284 mark_file (panel);
3285 break;
3286 case CK_MarkUp:
3287 mark_file_up (panel);
3288 break;
3289 case CK_MarkDown:
3290 mark_file_down (panel);
3291 break;
3292 case CK_MarkLeft:
3293 mark_file_left (panel);
3294 break;
3295 case CK_MarkRight:
3296 mark_file_right (panel);
3297 break;
3298 case CK_CdParentSmart:
3299 res = force_maybe_cd ();
3300 break;
3301 case CK_Up:
3302 move_up (panel);
3303 break;
3304 case CK_Down:
3305 move_down (panel);
3306 break;
3307 case CK_Left:
3308 res = move_left (panel);
3309 break;
3310 case CK_Right:
3311 res = move_right (panel);
3312 break;
3313 case CK_Bottom:
3314 move_end (panel);
3315 break;
3316 case CK_Top:
3317 move_home (panel);
3318 break;
3319 #ifdef HAVE_CHARSET
3320 case CK_SelectCodepage:
3321 panel_change_encoding (panel);
3322 break;
3323 #endif
3324 case CK_ScrollLeft:
3325 panel_content_scroll_left (panel);
3326 break;
3327 case CK_ScrollRight:
3328 panel_content_scroll_right (panel);
3329 break;
3330 case CK_Search:
3331 start_search (panel);
3332 break;
3333 case CK_SearchStop:
3334 break;
3335 case CK_PanelOtherSync:
3336 panel_sync_other (panel);
3337 break;
3338 case CK_Sort:
3339 panel_select_sort_order (panel);
3340 break;
3341 case CK_SortPrev:
3342 panel_toggle_sort_order_prev (panel);
3343 break;
3344 case CK_SortNext:
3345 panel_toggle_sort_order_next (panel);
3346 break;
3347 case CK_SortReverse:
3348 panel->sort_info.reverse = !panel->sort_info.reverse;
3349 panel_set_sort_order (panel, panel->sort_info.sort_field);
3350 break;
3351 case CK_SortByName:
3352 panel_set_sort_type_by_id (panel, "name");
3353 break;
3354 case CK_SortByExt:
3355 panel_set_sort_type_by_id (panel, "extension");
3356 break;
3357 case CK_SortBySize:
3358 panel_set_sort_type_by_id (panel, "size");
3359 break;
3360 case CK_SortByMTime:
3361 panel_set_sort_type_by_id (panel, "mtime");
3362 break;
3364 return res;
3367 /* --------------------------------------------------------------------------------------------- */
3369 static cb_ret_t
3370 panel_key (WPanel * panel, int key)
3372 size_t i;
3374 if (is_abort_char (key))
3376 stop_search (panel);
3377 return MSG_HANDLED;
3380 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3382 do_search (panel, key);
3383 return MSG_HANDLED;
3386 for (i = 0; panel_map[i].key != 0; i++)
3387 if (key == panel_map[i].key)
3388 return panel_execute_cmd (panel, panel_map[i].command);
3390 if (panels_options.torben_fj_mode && key == ALT ('h'))
3392 goto_middle_file (panel);
3393 return MSG_HANDLED;
3396 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3398 start_search (panel);
3399 do_search (panel, key);
3400 return MSG_HANDLED;
3403 return MSG_NOT_HANDLED;
3406 /* --------------------------------------------------------------------------------------------- */
3408 static cb_ret_t
3409 panel_callback (Widget * w, widget_msg_t msg, int parm)
3411 WPanel *panel = (WPanel *) w;
3412 WButtonBar *bb;
3414 switch (msg)
3416 case WIDGET_INIT:
3417 /* subscribe to "history_load" event */
3418 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3419 /* subscribe to "history_save" event */
3420 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3421 return MSG_HANDLED;
3423 case WIDGET_DRAW:
3424 /* Repaint everything, including frame and separator */
3425 paint_frame (panel); /* including show_dir */
3426 paint_dir (panel);
3427 mini_info_separator (panel);
3428 display_mini_info (panel);
3429 panel->dirty = 0;
3430 return MSG_HANDLED;
3432 case WIDGET_FOCUS:
3433 state_mark = -1;
3434 current_panel = panel;
3435 panel->active = 1;
3436 if (mc_chdir (panel->cwd_vpath) != 0)
3438 char *cwd;
3440 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3441 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3442 cwd, unix_error_string (errno));
3443 g_free (cwd);
3445 else
3446 subshell_chdir (panel->cwd_vpath);
3448 update_xterm_title_path ();
3449 select_item (panel);
3450 show_dir (panel);
3451 paint_dir (panel);
3452 panel->dirty = 0;
3454 bb = find_buttonbar (panel->widget.owner);
3455 midnight_set_buttonbar (bb);
3456 buttonbar_redraw (bb);
3457 return MSG_HANDLED;
3459 case WIDGET_UNFOCUS:
3460 /* Janne: look at this for the multiple panel options */
3461 stop_search (panel);
3462 panel->active = 0;
3463 show_dir (panel);
3464 unselect_item (panel);
3465 return MSG_HANDLED;
3467 case WIDGET_KEY:
3468 return panel_key (panel, parm);
3470 case WIDGET_COMMAND:
3471 return panel_execute_cmd (panel, parm);
3473 case WIDGET_DESTROY:
3474 /* unsubscribe from "history_load" event */
3475 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3476 /* unsubscribe from "history_save" event */
3477 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3478 panel_destroy (panel);
3479 free_my_statfs ();
3480 return MSG_HANDLED;
3482 default:
3483 return default_proc (msg, parm);
3487 /* --------------------------------------------------------------------------------------------- */
3488 /* */
3489 /* Panel mouse events support routines */
3490 /* */
3492 static void
3493 mouse_toggle_mark (WPanel * panel)
3495 do_mark_file (panel, MARK_DONT_MOVE);
3496 mouse_marking = selection (panel)->f.marked;
3497 mouse_mark_panel = current_panel;
3500 /* --------------------------------------------------------------------------------------------- */
3502 static void
3503 mouse_set_mark (WPanel * panel)
3506 if (mouse_mark_panel == panel)
3508 if (mouse_marking && !(selection (panel)->f.marked))
3509 do_mark_file (panel, MARK_DONT_MOVE);
3510 else if (!mouse_marking && (selection (panel)->f.marked))
3511 do_mark_file (panel, MARK_DONT_MOVE);
3515 /* --------------------------------------------------------------------------------------------- */
3517 static gboolean
3518 mark_if_marking (WPanel * panel, Gpm_Event * event)
3520 if ((event->buttons & GPM_B_RIGHT) != 0)
3522 if ((event->type & GPM_DOWN) != 0)
3523 mouse_toggle_mark (panel);
3524 else
3525 mouse_set_mark (panel);
3526 return TRUE;
3528 return FALSE;
3531 /* --------------------------------------------------------------------------------------------- */
3532 /** Determine which column was clicked, and sort the panel on
3533 * that column, or reverse sort on that column if already
3534 * sorted on that column.
3537 static void
3538 mouse_sort_col (WPanel * panel, int x)
3540 int i;
3541 const char *lc_sort_name = NULL;
3542 panel_field_t *col_sort_format = NULL;
3543 format_e *format;
3544 gchar *title;
3546 for (i = 0, format = panel->format; format != NULL; format = format->next)
3548 i += format->field_len;
3549 if (x < i + 1)
3551 /* found column */
3552 lc_sort_name = format->title;
3553 break;
3557 if (lc_sort_name == NULL)
3558 return;
3560 for (i = 0; panel_fields[i].id != NULL; i++)
3562 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3563 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3565 col_sort_format = &panel_fields[i];
3566 g_free (title);
3567 break;
3569 g_free (title);
3572 if (col_sort_format == NULL)
3573 return;
3575 if (panel->sort_info.sort_field == col_sort_format)
3577 /* reverse the sort if clicked column is already the sorted column */
3578 panel->sort_info.reverse = !panel->sort_info.reverse;
3580 else
3582 /* new sort is forced to be ascending */
3583 panel->sort_info.reverse = FALSE;
3585 panel_set_sort_order (panel, col_sort_format);
3589 /* --------------------------------------------------------------------------------------------- */
3591 * Mouse callback of the panel minus repainting.
3593 static int
3594 panel_event (Gpm_Event * event, void *data)
3596 WPanel *panel = (WPanel *) data;
3597 Widget *w = (Widget *) data;
3599 const int lines = llines (panel);
3600 const gboolean is_active = dlg_widget_active (panel);
3601 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3602 Gpm_Event local;
3604 if (!mouse_global_in_widget (event, (Widget *) data))
3605 return MOU_UNHANDLED;
3607 local = mouse_get_local (event, w);
3609 /* 1st line */
3610 if (local.y == 1)
3612 /* "<" button */
3613 if (mouse_down && local.x == 2)
3615 directory_history_prev (panel);
3616 goto finish;
3619 /* ">" button */
3620 if (mouse_down && local.x == w->cols - 1)
3622 directory_history_next (panel);
3623 goto finish;
3626 /* "^" button */
3627 if (mouse_down && local.x >= w->cols - 4 && local.x <= w->cols - 2)
3629 directory_history_list (panel);
3630 goto finish;
3633 /* "." button show/hide hidden files */
3634 if (mouse_down && local.x == w->cols - 5)
3636 midnight_dlg->callback (midnight_dlg, NULL, DLG_ACTION, CK_ShowHidden, NULL);
3637 goto finish;
3640 /* no other events on 1st line */
3641 return MOU_UNHANDLED;
3644 /* sort on clicked column; don't handle wheel events */
3645 if (mouse_down && (local.buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && local.y == 2)
3647 mouse_sort_col (panel, local.x);
3648 goto finish;
3651 /* Mouse wheel events */
3652 if (mouse_down && (local.buttons & GPM_B_UP) != 0)
3654 if (is_active)
3656 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3657 prev_page (panel);
3658 else /* We are in first page */
3659 move_up (panel);
3661 goto finish;
3664 if (mouse_down && (local.buttons & GPM_B_DOWN) != 0)
3666 if (is_active)
3668 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3669 next_page (panel);
3670 else /* We are in last page */
3671 move_down (panel);
3673 goto finish;
3676 local.y -= 2;
3677 if ((local.type & (GPM_DOWN | GPM_DRAG)) != 0)
3679 int my_index;
3681 if (!is_active)
3682 change_panel ();
3684 if (panel->top_file + local.y > panel->count)
3685 my_index = panel->count - 1;
3686 else
3688 my_index = panel->top_file + local.y - 1;
3689 if (panel->split && (local.x > (w->cols - 2) / 2))
3690 my_index += llines (panel);
3692 if (my_index >= panel->count)
3693 my_index = panel->count - 1;
3696 if (my_index != panel->selected)
3698 unselect_item (panel);
3699 panel->selected = my_index;
3700 select_item (panel);
3703 /* This one is new */
3704 mark_if_marking (panel, &local);
3706 else if ((local.type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE) &&
3707 local.y > 0 && local.y <= lines)
3708 do_enter (panel);
3710 finish:
3711 if (panel->dirty)
3712 send_message (w, WIDGET_DRAW, 0);
3713 return MOU_NORMAL;
3716 /* --------------------------------------------------------------------------------------------- */
3718 static void
3719 reload_panelized (WPanel * panel)
3721 int i, j;
3722 dir_list *list = &panel->dir;
3724 if (panel != current_panel)
3725 (void) mc_chdir (panel->cwd_vpath);
3727 for (i = 0, j = 0; i < panel->count; i++)
3729 vfs_path_t *vpath;
3731 if (list->list[i].f.marked)
3733 /* Unmark the file in advance. In case the following mc_lstat
3734 * fails we are done, else we have to mark the file again
3735 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3736 * IMO that's the best way to update the panel's summary status
3737 * -- Norbert
3739 do_file_mark (panel, i, 0);
3741 vpath = vfs_path_from_str (list->list[i].fname);
3742 if (mc_lstat (vpath, &list->list[i].st))
3743 g_free (list->list[i].fname);
3744 else
3746 if (list->list[i].f.marked)
3747 do_file_mark (panel, i, 1);
3748 if (j != i)
3749 list->list[j] = list->list[i];
3750 j++;
3752 vfs_path_free (vpath);
3754 if (j == 0)
3755 panel->count = set_zero_dir (list) ? 1 : 0;
3756 else
3757 panel->count = j;
3759 if (panel != current_panel)
3760 (void) mc_chdir (current_panel->cwd_vpath);
3763 /* --------------------------------------------------------------------------------------------- */
3765 static void
3766 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3768 gboolean free_pointer;
3769 char *my_current_file = NULL;
3771 if ((flags & UP_RELOAD) != 0)
3773 panel->is_panelized = FALSE;
3774 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3775 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3778 /* If current_file == -1 (an invalid pointer) then preserve selection */
3779 free_pointer = current_file == UP_KEEPSEL;
3781 if (free_pointer)
3783 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3784 current_file = my_current_file;
3787 if (panel->is_panelized)
3788 reload_panelized (panel);
3789 else
3790 panel_reload (panel);
3792 try_to_select (panel, current_file);
3793 panel->dirty = 1;
3795 if (free_pointer)
3796 g_free (my_current_file);
3799 /* --------------------------------------------------------------------------------------------- */
3801 static void
3802 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3804 if (get_display_type (which) == view_listing)
3806 WPanel *panel;
3808 panel = (WPanel *) get_panel_widget (which);
3809 if (panel->is_panelized)
3810 flags &= ~UP_RELOAD;
3811 update_one_panel_widget (panel, flags, current_file);
3815 /* --------------------------------------------------------------------------------------------- */
3817 static void
3818 do_select (WPanel * panel, int i)
3820 if (i != panel->selected)
3822 panel->dirty = 1;
3823 panel->selected = i;
3824 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3825 if (panel->top_file < 0)
3826 panel->top_file = 0;
3830 /* --------------------------------------------------------------------------------------------- */
3832 static void
3833 do_try_to_select (WPanel * panel, const char *name)
3835 int i;
3836 char *subdir;
3838 if (!name)
3840 do_select (panel, 0);
3841 return;
3844 /* We only want the last component of the directory,
3845 * and from this only the name without suffix.
3846 * Cut prefix if the panel is not panelized */
3848 if (panel->is_panelized)
3849 subdir = vfs_strip_suffix_from_filename (name);
3850 else
3851 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3853 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3854 for (i = 0; i < panel->count; i++)
3856 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3858 do_select (panel, i);
3859 g_free (subdir);
3860 return;
3864 /* Try to select a file near the file that is missing */
3865 if (panel->selected >= panel->count)
3866 do_select (panel, panel->count - 1);
3867 g_free (subdir);
3870 /* --------------------------------------------------------------------------------------------- */
3872 /* event callback */
3873 static gboolean
3874 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3875 gpointer init_data, gpointer data)
3877 (void) event_group_name;
3878 (void) event_name;
3879 (void) init_data;
3880 (void) data;
3882 update_panels (UP_RELOAD, UP_KEEPSEL);
3884 return TRUE;
3887 /* --------------------------------------------------------------------------------------------- */
3889 /* event callback */
3890 static gboolean
3891 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3892 gpointer init_data, gpointer data)
3894 (void) event_group_name;
3895 (void) event_name;
3896 (void) init_data;
3897 (void) data;
3899 if (current_panel->marked == 0)
3900 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3901 (gpointer) selection (current_panel)->fname);
3902 else
3904 int i;
3905 gboolean first = TRUE;
3906 char *flist = NULL;
3908 for (i = 0; i < current_panel->count; i++)
3909 if (current_panel->dir.list[i].f.marked != 0)
3910 { /* Skip the unmarked ones */
3911 if (first)
3913 flist = g_strdup (current_panel->dir.list[i].fname);
3914 first = FALSE;
3916 else
3918 /* Add empty lines after the file */
3919 char *tmp;
3921 tmp =
3922 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3923 g_free (flist);
3924 flist = tmp;
3928 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3929 g_free (flist);
3931 return TRUE;
3934 /* --------------------------------------------------------------------------------------------- */
3935 /*** public functions ****************************************************************************/
3936 /* --------------------------------------------------------------------------------------------- */
3938 void
3939 try_to_select (WPanel * panel, const char *name)
3941 do_try_to_select (panel, name);
3942 select_item (panel);
3945 /* --------------------------------------------------------------------------------------------- */
3947 void
3948 panel_clean_dir (WPanel * panel)
3950 int count = panel->count;
3952 panel->count = 0;
3953 panel->top_file = 0;
3954 panel->selected = 0;
3955 panel->marked = 0;
3956 panel->dirs_marked = 0;
3957 panel->total = 0;
3958 panel->searching = FALSE;
3959 panel->is_panelized = FALSE;
3960 panel->dirty = 1;
3961 panel->content_shift = -1;
3962 panel->max_shift = -1;
3964 clean_dir (&panel->dir, count);
3967 /* --------------------------------------------------------------------------------------------- */
3969 * Set Up panel's current dir object
3971 * @param panel panel object
3972 * @param path_str string contain path
3975 void
3976 panel_set_cwd (WPanel * panel, const char *path_str)
3978 vfs_path_free (panel->cwd_vpath);
3979 panel->cwd_vpath = vfs_path_from_str (path_str);
3982 /* --------------------------------------------------------------------------------------------- */
3984 * Set Up panel's last working dir object
3986 * @param panel panel object
3987 * @param path_str string contain path
3990 void
3991 panel_set_lwd (WPanel * panel, const char *path_str)
3993 vfs_path_free (panel->lwd_vpath);
3994 panel->lwd_vpath = vfs_path_from_str (path_str);
3997 /* --------------------------------------------------------------------------------------------- */
3998 /** Panel creation.
3999 * @param panel_name the name of the panel for setup retieving
4000 * @returns new instance of WPanel
4003 WPanel *
4004 panel_new (const char *panel_name)
4006 return panel_new_with_dir (panel_name, NULL);
4009 /* --------------------------------------------------------------------------------------------- */
4011 * Panel creation for specified directory.
4013 * @param panel_name specifies the name of the panel for setup retieving
4014 * @param wpath the path of working panel directory. If path is NULL then panel will be created
4015 * for current directory
4017 * @returns new instance of WPanel
4020 WPanel *
4021 panel_new_with_dir (const char *panel_name, const char *wpath)
4023 WPanel *panel;
4024 char *section;
4025 int i, err;
4026 char *curdir = NULL;
4028 panel = g_new0 (WPanel, 1);
4030 /* No know sizes of the panel at startup */
4031 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
4033 /* We do not want the cursor */
4034 widget_want_cursor (panel->widget, 0);
4036 if (wpath != NULL)
4038 curdir = _vfs_get_cwd ();
4039 panel_set_cwd (panel, wpath);
4041 else
4043 vfs_setup_cwd ();
4044 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4047 panel_set_lwd (panel, ".");
4049 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
4050 /* directories history will be get later */
4052 panel->dir.list = g_new (file_entry, MIN_FILES);
4053 panel->dir.size = MIN_FILES;
4054 panel->active = 0;
4055 panel->filter = 0;
4056 panel->split = 0;
4057 panel->top_file = 0;
4058 panel->selected = 0;
4059 panel->marked = 0;
4060 panel->total = 0;
4061 panel->dirty = 1;
4062 panel->searching = FALSE;
4063 panel->dirs_marked = 0;
4064 panel->is_panelized = FALSE;
4065 panel->format = 0;
4066 panel->status_format = 0;
4067 panel->format_modified = 1;
4068 panel->content_shift = -1;
4069 panel->max_shift = -1;
4071 panel->panel_name = g_strdup (panel_name);
4072 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4074 #ifdef HAVE_CHARSET
4075 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4076 #endif
4078 for (i = 0; i < LIST_TYPES; i++)
4079 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4081 panel->search_buffer[0] = '\0';
4082 panel->prev_search_buffer[0] = '\0';
4083 panel->frame_size = frame_half;
4085 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4086 if (!mc_config_has_group (mc_main_config, section))
4088 g_free (section);
4089 section = g_strdup (panel->panel_name);
4091 panel_load_setup (panel, section);
4092 g_free (section);
4094 /* Load format strings */
4095 err = set_panel_formats (panel);
4096 if (err != 0)
4097 set_panel_formats (panel);
4099 #ifdef HAVE_CHARSET
4101 const vfs_path_element_t *path_element;
4103 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4104 if (path_element->encoding != NULL)
4105 panel->codepage = get_codepage_index (path_element->encoding);
4107 #endif
4109 if (mc_chdir (panel->cwd_vpath) != 0)
4111 #ifdef HAVE_CHARSET
4112 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4113 #endif
4114 vfs_setup_cwd ();
4115 vfs_path_free (panel->cwd_vpath);
4116 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4119 /* Load the default format */
4120 panel->count =
4121 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4122 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4123 panel->sort_info.exec_first, panel->filter);
4125 /* Restore old right path */
4126 if (curdir != NULL)
4128 vfs_path_t *vpath;
4130 vpath = vfs_path_from_str (curdir);
4131 err = mc_chdir (vpath);
4132 vfs_path_free (vpath);
4134 g_free (curdir);
4136 return panel;
4139 /* --------------------------------------------------------------------------------------------- */
4141 void
4142 panel_reload (WPanel * panel)
4144 struct stat current_stat;
4145 char *tmp_path;
4146 gboolean ok;
4148 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4149 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
4150 && current_stat.st_ctime == panel->dir_stat.st_ctime
4151 && current_stat.st_mtime == panel->dir_stat.st_mtime);
4152 g_free (tmp_path);
4154 if (ok)
4155 return;
4159 char *last_slash;
4161 if (mc_chdir (panel->cwd_vpath) != -1)
4162 break;
4164 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4165 if (tmp_path[0] == PATH_SEP && tmp_path[1] == '\0')
4167 panel_clean_dir (panel);
4168 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
4169 g_free (tmp_path);
4170 return;
4172 last_slash = strrchr (tmp_path, PATH_SEP);
4173 vfs_path_free (panel->cwd_vpath);
4174 if (last_slash == NULL || last_slash == tmp_path)
4175 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4176 else
4178 *last_slash = '\0';
4179 panel->cwd_vpath = vfs_path_from_str (tmp_path);
4181 g_free (tmp_path);
4182 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4183 show_dir (panel);
4185 while (TRUE);
4187 panel->count =
4188 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4189 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4190 panel->sort_info.exec_first, panel->filter);
4192 panel->dirty = 1;
4193 if (panel->selected >= panel->count)
4194 do_select (panel, panel->count - 1);
4196 recalculate_panel_summary (panel);
4199 /* --------------------------------------------------------------------------------------------- */
4200 /* Switches the panel to the mode specified in the format */
4201 /* Seting up both format and status string. Return: 0 - on success; */
4202 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4205 set_panel_formats (WPanel * p)
4207 format_e *form;
4208 char *err = NULL;
4209 int retcode = 0;
4211 form = use_display_format (p, panel_format (p), &err, 0);
4213 if (err != NULL)
4215 g_free (err);
4216 retcode = 1;
4218 else
4220 delete_format (p->format);
4221 p->format = form;
4224 if (panels_options.show_mini_info)
4226 form = use_display_format (p, mini_status_format (p), &err, 1);
4228 if (err != NULL)
4230 g_free (err);
4231 retcode += 2;
4233 else
4235 delete_format (p->status_format);
4236 p->status_format = form;
4240 panel_format_modified (p);
4241 panel_update_cols (&(p->widget), p->frame_size);
4243 if (retcode)
4244 message (D_ERROR, _("Warning"),
4245 _("User supplied format looks invalid, reverting to default."));
4246 if (retcode & 0x01)
4248 g_free (p->user_format);
4249 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4251 if (retcode & 0x02)
4253 g_free (p->user_status_format[p->list_type]);
4254 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4257 return retcode;
4260 /* --------------------------------------------------------------------------------------------- */
4262 void
4263 panel_update_cols (Widget * widget, panel_display_t frame_size)
4265 int cols, origin;
4267 /* don't touch panel if it is not in dialog yet */
4268 /* if panel is not in dialog it is not in widgets list
4269 and cannot be compared with get_panel_widget() result */
4270 if (widget->owner == NULL)
4271 return;
4273 if (panels_layout.horizontal_split)
4275 widget->cols = COLS;
4276 return;
4279 if (frame_size == frame_full)
4281 cols = COLS;
4282 origin = 0;
4284 else if (widget == get_panel_widget (0))
4286 cols = panels_layout.left_panel_size;
4287 origin = 0;
4289 else
4291 cols = COLS - panels_layout.left_panel_size;
4292 origin = panels_layout.left_panel_size;
4295 widget->cols = cols;
4296 widget->x = origin;
4299 /* --------------------------------------------------------------------------------------------- */
4301 /* Select current item and readjust the panel */
4302 void
4303 select_item (WPanel * panel)
4305 /* Although currently all over the code we set the selection and
4306 top file to decent values before calling select_item, I could
4307 forget it someday, so it's better to do the actual fitting here */
4309 if (panel->selected < 0)
4310 panel->selected = 0;
4312 if (panel->selected > panel->count - 1)
4313 panel->selected = panel->count - 1;
4315 adjust_top_file (panel);
4317 panel->dirty = 1;
4319 execute_hooks (select_file_hook);
4322 /* --------------------------------------------------------------------------------------------- */
4323 /** Clears all files in the panel, used only when one file was marked */
4324 void
4325 unmark_files (WPanel * panel)
4327 int i;
4329 if (!panel->marked)
4330 return;
4331 for (i = 0; i < panel->count; i++)
4332 file_mark (panel, i, 0);
4334 panel->dirs_marked = 0;
4335 panel->marked = 0;
4336 panel->total = 0;
4339 /* --------------------------------------------------------------------------------------------- */
4340 /** Recalculate the panels summary information, used e.g. when marked
4341 files might have been removed by an external command */
4343 void
4344 recalculate_panel_summary (WPanel * panel)
4346 int i;
4348 panel->marked = 0;
4349 panel->dirs_marked = 0;
4350 panel->total = 0;
4352 for (i = 0; i < panel->count; i++)
4353 if (panel->dir.list[i].f.marked)
4355 /* do_file_mark will return immediately if newmark == oldmark.
4356 So we have to first unmark it to get panel's summary information
4357 updated. (Norbert) */
4358 panel->dir.list[i].f.marked = 0;
4359 do_file_mark (panel, i, 1);
4363 /* --------------------------------------------------------------------------------------------- */
4364 /** This routine marks a file or a directory */
4366 void
4367 do_file_mark (WPanel * panel, int idx, int mark)
4369 if (panel->dir.list[idx].f.marked == mark)
4370 return;
4372 /* Only '..' can't be marked, '.' isn't visible */
4373 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4374 return;
4376 file_mark (panel, idx, mark);
4377 if (panel->dir.list[idx].f.marked)
4379 panel->marked++;
4380 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4382 if (panel->dir.list[idx].f.dir_size_computed)
4383 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4384 panel->dirs_marked++;
4386 else
4387 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4388 set_colors (panel);
4390 else
4392 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4394 if (panel->dir.list[idx].f.dir_size_computed)
4395 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4396 panel->dirs_marked--;
4398 else
4399 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4400 panel->marked--;
4404 /* --------------------------------------------------------------------------------------------- */
4406 * Changes the current directory of the panel.
4407 * Record change in the directory history.
4409 gboolean
4410 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4412 gboolean r;
4414 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4415 if (r)
4416 directory_history_add (panel, panel->cwd_vpath);
4417 return r;
4420 /* --------------------------------------------------------------------------------------------- */
4422 void
4423 file_mark (WPanel * panel, int lc_index, int val)
4425 if (panel->dir.list[lc_index].f.marked != val)
4427 panel->dir.list[lc_index].f.marked = val;
4428 panel->dirty = 1;
4432 /* --------------------------------------------------------------------------------------------- */
4434 void
4435 panel_re_sort (WPanel * panel)
4437 char *filename;
4438 int i;
4440 if (panel == NULL)
4441 return;
4443 filename = g_strdup (selection (panel)->fname);
4444 unselect_item (panel);
4445 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4446 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4447 panel->sort_info.exec_first);
4448 panel->selected = -1;
4449 for (i = panel->count; i; i--)
4451 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4453 panel->selected = i - 1;
4454 break;
4457 g_free (filename);
4458 panel->top_file = panel->selected - ITEMS (panel) / 2;
4459 select_item (panel);
4460 panel->dirty = 1;
4463 /* --------------------------------------------------------------------------------------------- */
4465 void
4466 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4468 if (sort_order == NULL)
4469 return;
4471 panel->sort_info.sort_field = sort_order;
4473 /* The directory is already sorted, we have to load the unsorted stuff */
4474 if (sort_order->sort_routine == (sortfn *) unsorted)
4476 char *current_file;
4478 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4479 panel_reload (panel);
4480 try_to_select (panel, current_file);
4481 g_free (current_file);
4483 panel_re_sort (panel);
4486 /* --------------------------------------------------------------------------------------------- */
4488 #ifdef HAVE_CHARSET
4491 * Change panel encoding.
4492 * @param panel WPanel object
4495 void
4496 panel_change_encoding (WPanel * panel)
4498 const char *encoding = NULL;
4499 char *errmsg;
4500 int r;
4502 r = select_charset (-1, -1, panel->codepage, FALSE);
4504 if (r == SELECT_CHARSET_CANCEL)
4505 return; /* Cancel */
4507 panel->codepage = r;
4509 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4511 /* No translation */
4512 vfs_path_t *cd_path_vpath;
4514 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4515 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4516 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4517 show_dir (panel);
4518 vfs_path_free (cd_path_vpath);
4519 return;
4522 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4523 if (errmsg != NULL)
4525 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4526 g_free (errmsg);
4527 return;
4530 encoding = get_codepage_id (panel->codepage);
4531 if (encoding != NULL)
4533 char *cd_path;
4534 vfs_change_encoding (panel->cwd_vpath, encoding);
4536 cd_path = vfs_path_to_str (panel->cwd_vpath);
4537 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4538 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4539 g_free (cd_path);
4543 /* --------------------------------------------------------------------------------------------- */
4546 * Remove encode info from last path element.
4549 vfs_path_t *
4550 remove_encoding_from_path (const vfs_path_t * vpath)
4552 vfs_path_t *ret_vpath;
4553 GString *tmp_conv;
4554 int indx;
4556 ret_vpath = vfs_path_new ();
4558 tmp_conv = g_string_new ("");
4560 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4562 GIConv converter;
4563 vfs_path_element_t *path_element;
4565 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4566 vfs_path_add_element (ret_vpath, path_element);
4568 if (path_element->encoding == NULL)
4569 continue;
4571 converter = str_crt_conv_to (path_element->encoding);
4572 if (converter == INVALID_CONV)
4573 continue;
4575 g_free (path_element->encoding);
4576 path_element->encoding = NULL;
4578 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4580 g_free (path_element->path);
4581 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4583 g_string_set_size (tmp_conv, 0);
4585 str_close_conv (converter);
4586 str_close_conv (path_element->dir.converter);
4587 path_element->dir.converter = INVALID_CONV;
4589 g_string_free (tmp_conv, TRUE);
4590 return ret_vpath;
4592 #endif /* HAVE_CHARSET */
4594 /* --------------------------------------------------------------------------------------------- */
4597 * This routine reloads the directory in both panels. It tries to
4598 * select current_file in current_panel and other_file in other_panel.
4599 * If current_file == -1 then it automatically sets current_file and
4600 * other_file to the currently selected files in the panels.
4602 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4603 * will not reload the other panel.
4605 * @param flags for reload panel
4606 * @param current_file name of the current file
4609 void
4610 update_panels (panel_update_flags_t flags, const char *current_file)
4612 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4613 WPanel *panel;
4615 update_one_panel (get_current_index (), flags, current_file);
4616 if (reload_other)
4617 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4619 if (get_current_type () == view_listing)
4620 panel = (WPanel *) get_panel_widget (get_current_index ());
4621 else
4622 panel = (WPanel *) get_panel_widget (get_other_index ());
4624 if (!panel->is_panelized)
4625 (void) mc_chdir (panel->cwd_vpath);
4628 /* --------------------------------------------------------------------------------------------- */
4630 gsize
4631 panel_get_num_of_sortable_fields (void)
4633 gsize ret = 0, lc_index;
4635 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4636 if (panel_fields[lc_index].is_user_choice)
4637 ret++;
4638 return ret;
4641 /* --------------------------------------------------------------------------------------------- */
4643 const char **
4644 panel_get_sortable_fields (gsize * array_size)
4646 char **ret;
4647 gsize lc_index, i;
4649 lc_index = panel_get_num_of_sortable_fields ();
4651 ret = g_try_new0 (char *, lc_index + 1);
4652 if (ret == NULL)
4653 return NULL;
4655 if (array_size != NULL)
4656 *array_size = lc_index;
4658 lc_index = 0;
4660 for (i = 0; panel_fields[i].id != NULL; i++)
4661 if (panel_fields[i].is_user_choice)
4662 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4663 return (const char **) ret;
4666 /* --------------------------------------------------------------------------------------------- */
4668 const panel_field_t *
4669 panel_get_field_by_id (const char *name)
4671 gsize lc_index;
4672 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4673 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4674 return &panel_fields[lc_index];
4675 return NULL;
4678 /* --------------------------------------------------------------------------------------------- */
4680 const panel_field_t *
4681 panel_get_field_by_title_hotkey (const char *name)
4683 gsize lc_index;
4684 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4685 if (panel_fields[lc_index].title_hotkey != NULL &&
4686 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4687 return &panel_fields[lc_index];
4688 return NULL;
4691 /* --------------------------------------------------------------------------------------------- */
4693 const panel_field_t *
4694 panel_get_field_by_title (const char *name)
4696 gsize lc_index;
4697 gchar *title = NULL;
4699 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4701 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4702 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4704 g_free (title);
4705 return &panel_fields[lc_index];
4708 g_free (title);
4709 return NULL;
4712 /* --------------------------------------------------------------------------------------------- */
4714 gsize
4715 panel_get_num_of_user_possible_fields (void)
4717 gsize ret = 0, lc_index;
4719 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4720 if (panel_fields[lc_index].use_in_user_format)
4721 ret++;
4722 return ret;
4725 /* --------------------------------------------------------------------------------------------- */
4727 const char **
4728 panel_get_user_possible_fields (gsize * array_size)
4730 char **ret;
4731 gsize lc_index, i;
4733 lc_index = panel_get_num_of_user_possible_fields ();
4735 ret = g_try_new0 (char *, lc_index + 1);
4736 if (ret == NULL)
4737 return NULL;
4739 if (array_size != NULL)
4740 *array_size = lc_index;
4742 lc_index = 0;
4744 for (i = 0; panel_fields[i].id != NULL; i++)
4745 if (panel_fields[i].use_in_user_format)
4746 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4747 return (const char **) ret;
4750 /* --------------------------------------------------------------------------------------------- */
4752 void
4753 panel_init (void)
4755 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4756 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4758 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4759 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4760 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4761 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4762 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4763 panel_filename_scroll_left_char =
4764 mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4765 panel_filename_scroll_right_char =
4766 mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4768 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4769 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4770 panel_save_curent_file_to_clip_file, NULL, NULL);
4774 /* --------------------------------------------------------------------------------------------- */
4776 void
4777 panel_deinit (void)
4779 g_free (panel_sort_up_sign);
4780 g_free (panel_sort_down_sign);
4782 g_free (panel_hiddenfiles_sign_show);
4783 g_free (panel_hiddenfiles_sign_hide);
4784 g_free (panel_history_prev_item_sign);
4785 g_free (panel_history_next_item_sign);
4786 g_free (panel_history_show_list_sign);
4787 g_free (panel_filename_scroll_left_char);
4788 g_free (panel_filename_scroll_right_char);
4792 /* --------------------------------------------------------------------------------------------- */
4794 gboolean
4795 do_cd (const vfs_path_t * new_dir_vpath, enum cd_enum exact)
4797 gboolean res;
4798 const vfs_path_t *_new_dir_vpath = new_dir_vpath;
4800 if (current_panel->is_panelized)
4802 size_t new_vpath_len;
4804 new_vpath_len = vfs_path_len (new_dir_vpath);
4805 if (vfs_path_ncmp (new_dir_vpath, panelized_panel.root_vpath, new_vpath_len) == 0)
4806 _new_dir_vpath = panelized_panel.root_vpath;
4809 res = do_panel_cd (current_panel, _new_dir_vpath, exact);
4811 #ifdef HAVE_CHARSET
4812 if (res)
4814 const vfs_path_element_t *path_element;
4816 path_element = vfs_path_get_by_index (current_panel->cwd_vpath, -1);
4817 if (path_element->encoding != NULL)
4818 current_panel->codepage = get_codepage_index (path_element->encoding);
4819 else
4820 current_panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4822 #endif /* HAVE_CHARSET */
4824 return res;
4827 /* --------------------------------------------------------------------------------------------- */