Changed interface of following functions to handle vfs_path_t object as parameter:
[midnight-commander.git] / src / filemanager / panel.c
blobb44b77192dc3182b136f715b7d5fd20580387e1a
1 /*
2 Panel managing.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Miguel de Icaza, 1995
10 Timur Bakeyev, 1997, 1999
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file panel.c
29 * \brief Source: panel managin module
32 #include <config.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h" /* For Gpm_Event */
44 #include "lib/tty/key.h" /* XCTRL and ALT macros */
45 #include "lib/skin.h"
46 #include "lib/strescape.h"
47 #include "lib/filehighlight.h"
48 #include "lib/mcconfig.h"
49 #include "lib/vfs/vfs.h"
50 #include "lib/unixcompat.h"
51 #include "lib/timefmt.h"
52 #include "lib/util.h"
53 #include "lib/widget.h"
54 #ifdef HAVE_CHARSET
55 #include "lib/charsets.h" /* get_codepage_id () */
56 #endif
57 #include "lib/event.h"
59 #include "src/setup.h" /* For loading/saving panel options */
60 #include "src/execute.h"
61 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
62 #include "src/keybind-defaults.h" /* global_keymap_t */
63 #include "src/subshell.h" /* do_subshell_chdir() */
65 #include "dir.h"
66 #include "boxes.h"
67 #include "tree.h"
68 #include "ext.h" /* regexp_command */
69 #include "layout.h" /* Most layout variables are here */
70 #include "cmd.h"
71 #include "command.h" /* cmdline */
72 #include "usermenu.h"
73 #include "midnight.h"
74 #include "mountlist.h" /* my_statfs */
76 #include "panel.h"
78 /*** global variables ****************************************************************************/
80 /* The hook list for the select file function */
81 hook_t *select_file_hook = NULL;
83 panelized_panel_t panelized_panel = { { NULL, 0 }, -1, { '\0' } };
85 static const char *string_file_name (file_entry *, int);
86 static const char *string_file_size (file_entry *, int);
87 static const char *string_file_size_brief (file_entry *, int);
88 static const char *string_file_type (file_entry *, int);
89 static const char *string_file_mtime (file_entry *, int);
90 static const char *string_file_atime (file_entry *, int);
91 static const char *string_file_ctime (file_entry *, int);
92 static const char *string_file_permission (file_entry *, int);
93 static const char *string_file_perm_octal (file_entry *, int);
94 static const char *string_file_nlinks (file_entry *, int);
95 static const char *string_inode (file_entry *, int);
96 static const char *string_file_nuid (file_entry *, int);
97 static const char *string_file_ngid (file_entry *, int);
98 static const char *string_file_owner (file_entry *, int);
99 static const char *string_file_group (file_entry *, int);
100 static const char *string_marked (file_entry *, int);
101 static const char *string_space (file_entry *, int);
102 static const char *string_dot (file_entry *, int);
104 /* *INDENT-OFF* */
105 panel_field_t panel_fields[] = {
107 "unsorted", 12, 1, J_LEFT_FIT,
108 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
109 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
110 N_("sort|u"),
111 N_("&Unsorted"), TRUE, FALSE,
112 string_file_name,
113 (sortfn *) unsorted
117 "name", 12, 1, J_LEFT_FIT,
118 /* TRANSLATORS: one single character to represent 'name' sort mode */
119 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
120 N_("sort|n"),
121 N_("&Name"), TRUE, TRUE,
122 string_file_name,
123 (sortfn *) sort_name
127 "version", 12, 1, J_LEFT_FIT,
128 /* TRANSLATORS: one single character to represent 'version' sort mode */
129 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
130 N_("sort|v"),
131 N_("&Version"), TRUE, FALSE,
132 string_file_name,
133 (sortfn *) sort_vers
137 "extension", 12, 1, J_LEFT_FIT,
138 /* TRANSLATORS: one single character to represent 'extension' sort mode */
139 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
140 N_("sort|e"),
141 N_("&Extension"), TRUE, FALSE,
142 string_file_name, /* TODO: string_file_ext */
143 (sortfn *) sort_ext
147 "size", 7, 0, J_RIGHT,
148 /* TRANSLATORS: one single character to represent 'size' sort mode */
149 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
150 N_("sort|s"),
151 N_("&Size"), TRUE, TRUE,
152 string_file_size,
153 (sortfn *) sort_size
157 "bsize", 7, 0, J_RIGHT,
159 N_("Block Size"), FALSE, FALSE,
160 string_file_size_brief,
161 (sortfn *) sort_size
165 "type", 1, 0, J_LEFT,
167 "", FALSE, TRUE,
168 string_file_type,
169 NULL
173 "mtime", 12, 0, J_RIGHT,
174 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
175 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
176 N_("sort|m"),
177 N_("&Modify time"), TRUE, TRUE,
178 string_file_mtime,
179 (sortfn *) sort_time
183 "atime", 12, 0, J_RIGHT,
184 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
185 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
186 N_("sort|a"),
187 N_("&Access time"), TRUE, TRUE,
188 string_file_atime,
189 (sortfn *) sort_atime
193 "ctime", 12, 0, J_RIGHT,
194 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
195 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
196 N_("sort|h"),
197 N_("C&hange time"), TRUE, TRUE,
198 string_file_ctime,
199 (sortfn *) sort_ctime
203 "perm", 10, 0, J_LEFT,
205 N_("Permission"), FALSE, TRUE,
206 string_file_permission,
207 NULL
211 "mode", 6, 0, J_RIGHT,
213 N_("Perm"), FALSE, TRUE,
214 string_file_perm_octal,
215 NULL
219 "nlink", 2, 0, J_RIGHT,
221 N_("Nl"), FALSE, TRUE,
222 string_file_nlinks, NULL
226 "inode", 5, 0, J_RIGHT,
227 /* TRANSLATORS: one single character to represent 'inode' sort mode */
228 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
229 N_("sort|i"),
230 N_("&Inode"), TRUE, TRUE,
231 string_inode,
232 (sortfn *) sort_inode
236 "nuid", 5, 0, J_RIGHT,
238 N_("UID"), FALSE, FALSE,
239 string_file_nuid,
240 NULL
244 "ngid", 5, 0, J_RIGHT,
246 N_("GID"), FALSE, FALSE,
247 string_file_ngid,
248 NULL
252 "owner", 8, 0, J_LEFT_FIT,
254 N_("Owner"), FALSE, TRUE,
255 string_file_owner,
256 NULL
260 "group", 8, 0, J_LEFT_FIT,
262 N_("Group"), FALSE, TRUE,
263 string_file_group,
264 NULL
268 "mark", 1, 0, J_RIGHT,
270 " ", FALSE, TRUE,
271 string_marked,
272 NULL
276 "|", 1, 0, J_RIGHT,
278 " ", FALSE, TRUE,
279 NULL,
280 NULL
284 "space", 1, 0, J_RIGHT,
286 " ", FALSE, TRUE,
287 string_space,
288 NULL
292 "dot", 1, 0, J_RIGHT,
294 " ", FALSE, FALSE,
295 string_dot,
296 NULL
300 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
303 /* *INDENT-ON* */
305 extern int saving_setup;
307 /*** file scope macro definitions ****************************************************************/
309 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
311 #define NORMAL 0
312 #define SELECTED 1
313 #define MARKED 2
314 #define MARKED_SELECTED 3
315 #define STATUS 5
317 /* This macro extracts the number of available lines in a panel */
318 #define llines(p) (p->widget.lines - 3 - (panels_options.show_mini_info ? 2 : 0))
320 /*** file scope type declarations ****************************************************************/
322 typedef enum
324 MARK_DONT_MOVE = 0,
325 MARK_DOWN = 1,
326 MARK_FORCE_DOWN = 2,
327 MARK_FORCE_UP = 3
328 } mark_act_t;
331 * This describes a format item. The parse_display_format routine parses
332 * the user specified format and creates a linked list of format_e structures.
334 typedef struct format_e
336 struct format_e *next;
337 int requested_field_len;
338 int field_len;
339 align_crt_t just_mode;
340 int expand;
341 const char *(*string_fn) (file_entry *, int len);
342 char *title;
343 const char *id;
344 } format_e;
346 /*** file scope variables ************************************************************************/
348 static char *panel_sort_up_sign = NULL;
349 static char *panel_sort_down_sign = NULL;
351 static char *panel_hiddenfiles_sign_show = NULL;
352 static char *panel_hiddenfiles_sign_hide = NULL;
353 static char *panel_history_prev_item_sign = NULL;
354 static char *panel_history_next_item_sign = NULL;
355 static char *panel_history_show_list_sign = NULL;
357 /* Panel that selection started */
358 static WPanel *mouse_mark_panel = NULL;
360 static int mouse_marking = 0;
361 static int state_mark = 0;
362 /*** file scope functions ************************************************************************/
363 /* --------------------------------------------------------------------------------------------- */
365 static void
366 set_colors (WPanel * panel)
368 (void) panel;
369 tty_set_normal_attrs ();
370 tty_setcolor (NORMAL_COLOR);
373 /* --------------------------------------------------------------------------------------------- */
374 /** Delete format string, it is a linked list */
376 static void
377 delete_format (format_e * format)
379 while (format != NULL)
381 format_e *next = format->next;
382 g_free (format->title);
383 g_free (format);
384 format = next;
388 /* --------------------------------------------------------------------------------------------- */
389 /** This code relies on the default justification!!! */
391 static void
392 add_permission_string (char *dest, int width, file_entry * fe, int attr, int color, int is_octal)
394 int i, r, l;
396 l = get_user_permissions (&fe->st);
398 if (is_octal)
400 /* Place of the access bit in octal mode */
401 l = width + l - 3;
402 r = l + 1;
404 else
406 /* The same to the triplet in string mode */
407 l = l * 3 + 1;
408 r = l + 3;
411 for (i = 0; i < width; i++)
413 if (i >= l && i < r)
415 if (attr == SELECTED || attr == MARKED_SELECTED)
416 tty_setcolor (MARKED_SELECTED_COLOR);
417 else
418 tty_setcolor (MARKED_COLOR);
420 else if (color >= 0)
421 tty_setcolor (color);
422 else
423 tty_lowlevel_setcolor (-color);
425 tty_print_char (dest[i]);
429 /* --------------------------------------------------------------------------------------------- */
430 /** String representations of various file attributes name */
432 static const char *
433 string_file_name (file_entry * fe, int len)
435 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
437 (void) len;
438 g_strlcpy (buffer, fe->fname, sizeof (buffer));
439 return buffer;
442 /* --------------------------------------------------------------------------------------------- */
444 static unsigned int
445 ilog10 (dev_t n)
447 unsigned int digits = 0;
450 digits++, n /= 10;
452 while (n != 0);
453 return digits;
456 /* --------------------------------------------------------------------------------------------- */
458 static void
459 format_device_number (char *buf, size_t bufsize, dev_t dev)
461 dev_t major_dev = major (dev);
462 dev_t minor_dev = minor (dev);
463 unsigned int major_digits = ilog10 (major_dev);
464 unsigned int minor_digits = ilog10 (minor_dev);
466 g_assert (bufsize >= 1);
467 if (major_digits + 1 + minor_digits + 1 <= bufsize)
469 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
471 else
473 g_strlcpy (buf, _("[dev]"), bufsize);
477 /* --------------------------------------------------------------------------------------------- */
478 /** size */
480 static const char *
481 string_file_size (file_entry * fe, int len)
483 static char buffer[BUF_TINY];
485 /* Don't ever show size of ".." since we don't calculate it */
486 if (!strcmp (fe->fname, ".."))
488 return _("UP--DIR");
491 #ifdef HAVE_STRUCT_STAT_ST_RDEV
492 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
493 format_device_number (buffer, len + 1, fe->st.st_rdev);
494 else
495 #endif
497 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
499 return buffer;
502 /* --------------------------------------------------------------------------------------------- */
503 /** bsize */
505 static const char *
506 string_file_size_brief (file_entry * fe, int len)
508 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
510 return _("SYMLINK");
513 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
515 return _("SUB-DIR");
518 return string_file_size (fe, len);
521 /* --------------------------------------------------------------------------------------------- */
522 /** This functions return a string representation of a file entry type */
524 static const char *
525 string_file_type (file_entry * fe, int len)
527 static char buffer[2];
529 (void) len;
530 if (S_ISDIR (fe->st.st_mode))
531 buffer[0] = PATH_SEP;
532 else if (S_ISLNK (fe->st.st_mode))
534 if (fe->f.link_to_dir)
535 buffer[0] = '~';
536 else if (fe->f.stale_link)
537 buffer[0] = '!';
538 else
539 buffer[0] = '@';
541 else if (S_ISCHR (fe->st.st_mode))
542 buffer[0] = '-';
543 else if (S_ISSOCK (fe->st.st_mode))
544 buffer[0] = '=';
545 else if (S_ISDOOR (fe->st.st_mode))
546 buffer[0] = '>';
547 else if (S_ISBLK (fe->st.st_mode))
548 buffer[0] = '+';
549 else if (S_ISFIFO (fe->st.st_mode))
550 buffer[0] = '|';
551 else if (S_ISNAM (fe->st.st_mode))
552 buffer[0] = '#';
553 else if (!S_ISREG (fe->st.st_mode))
554 buffer[0] = '?'; /* non-regular of unknown kind */
555 else if (is_exe (fe->st.st_mode))
556 buffer[0] = '*';
557 else
558 buffer[0] = ' ';
559 buffer[1] = '\0';
560 return buffer;
563 /* --------------------------------------------------------------------------------------------- */
564 /** mtime */
566 static const char *
567 string_file_mtime (file_entry * fe, int len)
569 (void) len;
570 return file_date (fe->st.st_mtime);
573 /* --------------------------------------------------------------------------------------------- */
574 /** atime */
576 static const char *
577 string_file_atime (file_entry * fe, int len)
579 (void) len;
580 return file_date (fe->st.st_atime);
583 /* --------------------------------------------------------------------------------------------- */
584 /** ctime */
586 static const char *
587 string_file_ctime (file_entry * fe, int len)
589 (void) len;
590 return file_date (fe->st.st_ctime);
593 /* --------------------------------------------------------------------------------------------- */
594 /** perm */
596 static const char *
597 string_file_permission (file_entry * fe, int len)
599 (void) len;
600 return string_perm (fe->st.st_mode);
603 /* --------------------------------------------------------------------------------------------- */
604 /** mode */
606 static const char *
607 string_file_perm_octal (file_entry * fe, int len)
609 static char buffer[10];
611 (void) len;
612 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
613 return buffer;
616 /* --------------------------------------------------------------------------------------------- */
617 /** nlink */
619 static const char *
620 string_file_nlinks (file_entry * fe, int len)
622 static char buffer[BUF_TINY];
624 (void) len;
625 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
626 return buffer;
629 /* --------------------------------------------------------------------------------------------- */
630 /** inode */
632 static const char *
633 string_inode (file_entry * fe, int len)
635 static char buffer[10];
637 (void) len;
638 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
639 return buffer;
642 /* --------------------------------------------------------------------------------------------- */
643 /** nuid */
645 static const char *
646 string_file_nuid (file_entry * fe, int len)
648 static char buffer[10];
650 (void) len;
651 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
652 return buffer;
655 /* --------------------------------------------------------------------------------------------- */
656 /** ngid */
658 static const char *
659 string_file_ngid (file_entry * fe, int len)
661 static char buffer[10];
663 (void) len;
664 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
665 return buffer;
668 /* --------------------------------------------------------------------------------------------- */
669 /** owner */
671 static const char *
672 string_file_owner (file_entry * fe, int len)
674 (void) len;
675 return get_owner (fe->st.st_uid);
678 /* --------------------------------------------------------------------------------------------- */
679 /** group */
681 static const char *
682 string_file_group (file_entry * fe, int len)
684 (void) len;
685 return get_group (fe->st.st_gid);
688 /* --------------------------------------------------------------------------------------------- */
689 /** mark */
691 static const char *
692 string_marked (file_entry * fe, int len)
694 (void) len;
695 return fe->f.marked ? "*" : " ";
698 /* --------------------------------------------------------------------------------------------- */
699 /** space */
701 static const char *
702 string_space (file_entry * fe, int len)
704 (void) fe;
705 (void) len;
706 return " ";
709 /* --------------------------------------------------------------------------------------------- */
710 /** dot */
712 static const char *
713 string_dot (file_entry * fe, int len)
715 (void) fe;
716 (void) len;
717 return ".";
720 /* --------------------------------------------------------------------------------------------- */
722 static int
723 file_compute_color (int attr, file_entry * fe)
725 switch (attr)
727 case SELECTED:
728 return (SELECTED_COLOR);
729 case MARKED:
730 return (MARKED_COLOR);
731 case MARKED_SELECTED:
732 return (MARKED_SELECTED_COLOR);
733 case STATUS:
734 return (NORMAL_COLOR);
735 case NORMAL:
736 default:
737 if (!panels_options.filetype_mode)
738 return (NORMAL_COLOR);
741 return mc_fhl_get_color (mc_filehighlight, fe);
744 /* --------------------------------------------------------------------------------------------- */
745 /** Formats the file number file_index of panel in the buffer dest */
747 static void
748 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
749 int isstatus)
751 int color, length, empty_line;
752 const char *txt;
753 format_e *format, *home;
754 file_entry *fe;
756 (void) dest;
757 (void) limit;
758 length = 0;
759 empty_line = (file_index >= panel->count);
760 home = (isstatus) ? panel->status_format : panel->format;
761 fe = &panel->dir.list[file_index];
763 if (!empty_line)
764 color = file_compute_color (attr, fe);
765 else
766 color = NORMAL_COLOR;
768 for (format = home; format; format = format->next)
770 if (length == width)
771 break;
773 if (format->string_fn)
775 int len, perm;
776 char *preperad_text;
778 if (empty_line)
779 txt = " ";
780 else
781 txt = (*format->string_fn) (fe, format->field_len);
783 len = format->field_len;
784 if (len + length > width)
785 len = width - length;
786 if (len <= 0)
787 break;
789 perm = 0;
790 if (panels_options.permission_mode)
792 if (!strcmp (format->id, "perm"))
793 perm = 1;
794 else if (!strcmp (format->id, "mode"))
795 perm = 2;
798 if (color >= 0)
799 tty_setcolor (color);
800 else
801 tty_lowlevel_setcolor (-color);
803 preperad_text = (char *) str_fit_to_term (txt, len, format->just_mode);
805 if (perm)
806 add_permission_string (preperad_text, format->field_len, fe, attr, color, perm - 1);
807 else
808 tty_print_string (preperad_text);
810 length += len;
812 else
814 if (attr == SELECTED || attr == MARKED_SELECTED)
815 tty_setcolor (SELECTED_COLOR);
816 else
817 tty_setcolor (NORMAL_COLOR);
818 tty_print_one_vline (TRUE);
819 length++;
823 if (length < width)
824 tty_draw_hline (-1, -1, ' ', width - length);
827 /* --------------------------------------------------------------------------------------------- */
829 static void
830 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
832 int second_column = 0;
833 int width;
834 int offset = 0;
835 char buffer[BUF_MEDIUM];
837 gboolean panel_is_split = !isstatus && panel->split;
839 width = panel->widget.cols - 2;
841 if (panel_is_split)
843 second_column = (file_index - panel->top_file) / llines (panel);
844 width = width / 2 - 1;
846 if (second_column != 0)
848 offset = 1 + width;
849 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
850 width = panel->widget.cols - offset - 2;
854 /* Nothing to paint */
855 if (width <= 0)
856 return;
858 if (mv)
860 if (panel_is_split)
861 widget_move (&panel->widget,
862 (file_index - panel->top_file) % llines (panel) + 2, offset + 1);
863 else
864 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
867 format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus);
869 if (panel_is_split)
871 if (second_column)
872 tty_print_char (' ');
873 else
875 tty_setcolor (NORMAL_COLOR);
876 tty_print_one_vline (TRUE);
881 /* --------------------------------------------------------------------------------------------- */
883 static void
884 display_mini_info (WPanel * panel)
886 if (!panels_options.show_mini_info)
887 return;
889 widget_move (&panel->widget, llines (panel) + 3, 1);
891 if (panel->searching)
893 tty_setcolor (INPUT_COLOR);
894 tty_print_char ('/');
895 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
896 return;
899 /* Status resolves links and show them */
900 set_colors (panel);
902 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
904 char link_target[MC_MAXPATHLEN];
905 vfs_path_t *lc_link_vpath;
906 int len;
908 lc_link_vpath =
909 vfs_path_build_filename (panel->cwd, panel->dir.list[panel->selected].fname, NULL);
910 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
911 vfs_path_free (lc_link_vpath);
912 if (len > 0)
914 link_target[len] = 0;
915 tty_print_string ("-> ");
916 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
918 else
919 tty_print_string (str_fit_to_term (_("<readlink failed>"),
920 panel->widget.cols - 2, J_LEFT));
922 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
924 /* FIXME:
925 * while loading directory (do_load_dir() and do_reload_dir()),
926 * the actual stat info about ".." directory isn't got;
927 * so just don't display incorrect info about ".." directory */
928 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
930 else
931 /* Default behavior */
932 repaint_file (panel, panel->selected, 0, STATUS, 1);
935 /* --------------------------------------------------------------------------------------------- */
937 static void
938 paint_dir (WPanel * panel)
940 int i;
941 int color; /* Color value of the line */
942 int items; /* Number of items */
944 items = llines (panel) * (panel->split ? 2 : 1);
946 for (i = 0; i < items; i++)
948 if (i + panel->top_file >= panel->count)
949 color = 0;
950 else
952 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
953 color += (panel->selected == i + panel->top_file && panel->active);
955 repaint_file (panel, i + panel->top_file, 1, color, 0);
957 tty_set_normal_attrs ();
960 /* --------------------------------------------------------------------------------------------- */
962 static void
963 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
965 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
966 int cols;
968 if (panel->marked <= 0)
969 return;
971 buf = size_only ? b_bytes : buffer;
972 cols = panel->widget.cols - 2;
975 * This is a trick to use two ngettext() calls in one sentence.
976 * First make "N bytes", then insert it into "X in M files".
978 g_snprintf (b_bytes, sizeof (b_bytes),
979 ngettext ("%s byte", "%s bytes", panel->total),
980 size_trunc_sep (panel->total, panels_options.kilobyte_si));
981 if (!size_only)
982 g_snprintf (buffer, sizeof (buffer),
983 ngettext ("%s in %d file", "%s in %d files", panel->marked),
984 b_bytes, panel->marked);
986 /* don't forget spaces around buffer content */
987 buf = (char *) str_trunc (buf, cols - 4);
989 if (x < 0)
990 /* center in panel */
991 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
994 * y == llines (panel) + 2 for mini_info_separator
995 * y == panel->widget.lines - 1 for panel bottom frame
997 widget_move (&panel->widget, y, x);
998 tty_setcolor (MARKED_COLOR);
999 tty_printf (" %s ", buf);
1002 /* --------------------------------------------------------------------------------------------- */
1004 static void
1005 mini_info_separator (WPanel * panel)
1007 if (panels_options.show_mini_info)
1009 const int y = llines (panel) + 2;
1011 tty_setcolor (NORMAL_COLOR);
1012 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1013 ACS_HLINE, panel->widget.cols - 2);
1014 /* Status displays total marked size.
1015 * Centered in panel, full format. */
1016 display_total_marked_size (panel, y, -1, FALSE);
1020 /* --------------------------------------------------------------------------------------------- */
1022 static void
1023 show_free_space (WPanel * panel)
1025 /* Used to figure out how many free space we have */
1026 static struct my_statfs myfs_stats;
1027 /* Old current working directory for displaying free space */
1028 static char *old_cwd = NULL;
1029 vfs_path_t *vpath = vfs_path_from_str (panel->cwd);
1031 /* Don't try to stat non-local fs */
1032 if (!vfs_file_is_local (vpath) || !free_space)
1034 vfs_path_free (vpath);
1035 return;
1037 vfs_path_free (vpath);
1039 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0)
1041 char rpath[PATH_MAX];
1043 init_my_statfs ();
1044 g_free (old_cwd);
1045 old_cwd = g_strdup (panel->cwd);
1047 if (mc_realpath (panel->cwd, rpath) == NULL)
1048 return;
1050 my_statfs (&myfs_stats, rpath);
1053 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1055 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1056 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1057 panels_options.kilobyte_si);
1058 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1059 panels_options.kilobyte_si);
1060 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1061 myfs_stats.total == 0 ? 0 :
1062 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1063 widget_move (&panel->widget, panel->widget.lines - 1,
1064 panel->widget.cols - 2 - (int) strlen (tmp));
1065 tty_setcolor (NORMAL_COLOR);
1066 tty_print_string (tmp);
1070 /* --------------------------------------------------------------------------------------------- */
1072 static void
1073 show_dir (WPanel * panel)
1075 gchar *tmp;
1076 set_colors (panel);
1077 draw_box (panel->widget.owner,
1078 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1080 if (panels_options.show_mini_info)
1082 widget_move (&panel->widget, llines (panel) + 2, 0);
1083 tty_print_alt_char (ACS_LTEE, FALSE);
1084 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1085 tty_print_alt_char (ACS_RTEE, FALSE);
1088 widget_move (&panel->widget, 0, 1);
1089 tty_print_string (panel_history_prev_item_sign);
1091 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1092 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1093 panel_history_next_item_sign);
1095 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1096 tty_print_string (tmp);
1098 g_free (tmp);
1100 if (panel->active)
1101 tty_setcolor (REVERSE_COLOR);
1103 widget_move (&panel->widget, 0, 3);
1105 if (panel->is_panelized)
1106 tty_printf (" %s ", _("Panelize"));
1107 else
1108 tty_printf (" %s ",
1109 str_term_trim (strip_home_and_password (panel->cwd),
1110 min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1112 if (!panels_options.show_mini_info)
1114 if (panel->marked == 0)
1116 /* Show size of curret file in the bottom of panel */
1117 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1119 char buffer[BUF_SMALL];
1121 g_snprintf (buffer, sizeof (buffer), " %s ",
1122 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1123 panels_options.kilobyte_si));
1124 tty_setcolor (NORMAL_COLOR);
1125 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1126 tty_print_string (buffer);
1129 else
1131 /* Show total size of marked files
1132 * In the bottom of panel, display size only. */
1133 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1137 show_free_space (panel);
1139 if (panel->active)
1140 tty_set_normal_attrs ();
1143 /* --------------------------------------------------------------------------------------------- */
1145 /* Returns the number of items in the given panel */
1146 static int
1147 ITEMS (WPanel * p)
1149 if (p->split)
1150 return llines (p) * 2;
1151 else
1152 return llines (p);
1155 /* --------------------------------------------------------------------------------------------- */
1157 static void
1158 adjust_top_file (WPanel * panel)
1160 int items = ITEMS (panel);
1162 if (panel->count <= items)
1164 /* If all files fit, show them all. */
1165 panel->top_file = 0;
1167 else
1169 int i;
1171 /* top_file has to be in the range [selected-items+1, selected] so that
1172 the selected file is visible.
1173 top_file should be in the range [0, count-items] so that there's
1174 no empty space wasted.
1175 Within these ranges, adjust it by as little as possible. */
1177 if (panel->top_file < 0)
1178 panel->top_file = 0;
1180 i = panel->selected - items + 1;
1181 if (panel->top_file < i)
1182 panel->top_file = i;
1184 i = panel->count - items;
1185 if (panel->top_file > i)
1186 panel->top_file = i;
1188 if (panel->top_file > panel->selected)
1189 panel->top_file = panel->selected;
1193 /* --------------------------------------------------------------------------------------------- */
1194 /** add "#enc:encodning" to end of path */
1195 /* if path end width a previous #enc:, only encoding is changed no additional
1196 * #enc: is appended
1197 * retun new string
1200 static char *
1201 panel_save_name (WPanel * panel)
1203 /* If the program is shuting down */
1204 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1205 return g_strdup (panel->panel_name);
1206 else
1207 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1210 /* --------------------------------------------------------------------------------------------- */
1212 /* "history_load" event handler */
1213 static gboolean
1214 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1215 gpointer init_data, gpointer data)
1217 WPanel *p = (WPanel *) init_data;
1218 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1220 (void) event_group_name;
1221 (void) event_name;
1223 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1225 if (ev->cfg != NULL)
1226 p->dir_history = history_load (ev->cfg, p->hist_name);
1227 else
1228 p->dir_history = history_get (p->hist_name);
1230 directory_history_add (p, p->cwd);
1233 return TRUE;
1236 /* --------------------------------------------------------------------------------------------- */
1238 /* "history_save" event handler */
1239 static gboolean
1240 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1241 gpointer init_data, gpointer data)
1243 WPanel *p = (WPanel *) init_data;
1245 (void) event_group_name;
1246 (void) event_name;
1248 if (p->dir_history != NULL)
1250 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1252 history_save (ev->cfg, p->hist_name, p->dir_history);
1255 return TRUE;
1258 /* --------------------------------------------------------------------------------------------- */
1260 static void
1261 panel_destroy (WPanel * p)
1263 size_t i;
1265 if (panels_options.auto_save_setup)
1267 char *name;
1269 name = panel_save_name (p);
1270 panel_save_setup (p, name);
1271 g_free (name);
1274 panel_clean_dir (p);
1276 /* clean history */
1277 if (p->dir_history != NULL)
1279 /* directory history is already saved before this moment */
1280 p->dir_history = g_list_first (p->dir_history);
1281 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1282 g_list_free (p->dir_history);
1284 g_free (p->hist_name);
1286 delete_format (p->format);
1287 delete_format (p->status_format);
1289 g_free (p->user_format);
1290 for (i = 0; i < LIST_TYPES; i++)
1291 g_free (p->user_status_format[i]);
1292 g_free (p->dir.list);
1293 g_free (p->panel_name);
1296 /* --------------------------------------------------------------------------------------------- */
1298 static void
1299 panel_format_modified (WPanel * panel)
1301 panel->format_modified = 1;
1304 /* --------------------------------------------------------------------------------------------- */
1306 static void
1307 panel_paint_sort_info (WPanel * panel)
1309 if (*panel->sort_info.sort_field->hotkey != '\0')
1311 const char *sort_sign =
1312 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1313 char *str;
1315 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1316 widget_move (&panel->widget, 1, 1);
1317 tty_print_string (str);
1318 g_free (str);
1322 /* --------------------------------------------------------------------------------------------- */
1324 static gchar *
1325 panel_get_title_without_hotkey (const char *title)
1327 char *translated_title;
1328 char *hkey;
1330 if (title == NULL)
1331 return NULL;
1332 if (title[0] == '\0')
1333 return g_strdup ("");
1335 translated_title = g_strdup (_(title));
1337 hkey = strchr (translated_title, '&');
1338 if ((hkey != NULL) && (hkey[1] != '\0'))
1339 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1341 return translated_title;
1344 /* --------------------------------------------------------------------------------------------- */
1346 static void
1347 paint_frame (WPanel * panel)
1349 int side, width;
1350 GString *format_txt;
1352 adjust_top_file (panel);
1354 widget_erase (&panel->widget);
1355 show_dir (panel);
1357 widget_move (&panel->widget, 1, 1);
1359 for (side = 0; side <= panel->split; side++)
1361 format_e *format;
1363 if (side)
1365 tty_setcolor (NORMAL_COLOR);
1366 tty_print_one_vline (TRUE);
1367 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1369 else if (panel->split)
1370 width = panel->widget.cols / 2 - 3;
1371 else
1372 width = panel->widget.cols - 2;
1374 format_txt = g_string_new ("");
1375 for (format = panel->format; format; format = format->next)
1377 if (format->string_fn)
1379 g_string_set_size (format_txt, 0);
1381 if (panel->list_type == list_long
1382 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1383 g_string_append (format_txt,
1384 panel->sort_info.reverse
1385 ? panel_sort_down_sign : panel_sort_up_sign);
1387 g_string_append (format_txt, format->title);
1388 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1390 g_string_append (format_txt, " [");
1391 g_string_append (format_txt, panel->filter);
1392 g_string_append (format_txt, "]");
1395 tty_setcolor (HEADER_COLOR);
1396 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1397 J_CENTER_LEFT));
1398 width -= format->field_len;
1400 else
1402 tty_setcolor (NORMAL_COLOR);
1403 tty_print_one_vline (TRUE);
1404 width--;
1407 g_string_free (format_txt, TRUE);
1409 if (width > 0)
1410 tty_draw_hline (-1, -1, ' ', width);
1413 if (panel->list_type != list_long)
1414 panel_paint_sort_info (panel);
1417 /* --------------------------------------------------------------------------------------------- */
1419 static const char *
1420 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1422 panel_display_t frame = frame_half;
1423 format = skip_separators (format);
1425 if (!strncmp (format, "full", 4))
1427 frame = frame_full;
1428 format += 4;
1430 else if (!strncmp (format, "half", 4))
1432 frame = frame_half;
1433 format += 4;
1436 if (!isstatus)
1438 panel->frame_size = frame;
1439 panel->split = 0;
1442 /* Now, the optional column specifier */
1443 format = skip_separators (format);
1445 if (*format == '1' || *format == '2')
1447 if (!isstatus)
1448 panel->split = *format == '2';
1449 format++;
1452 if (!isstatus)
1453 panel_update_cols (&(panel->widget), panel->frame_size);
1455 return skip_separators (format);
1458 /* Format is:
1460 all := panel_format? format
1461 panel_format := [full|half] [1|2]
1462 format := one_format_e
1463 | format , one_format_e
1465 one_format_e := just format.id [opt_size]
1466 just := [<=>]
1467 opt_size := : size [opt_expand]
1468 size := [0-9]+
1469 opt_expand := +
1473 /* --------------------------------------------------------------------------------------------- */
1475 static format_e *
1476 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1477 int *res_total_cols)
1479 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1480 int total_cols = 0; /* Used columns by the format */
1481 int set_justify; /* flag: set justification mode? */
1482 align_crt_t justify = J_LEFT; /* Which mode. */
1483 size_t i;
1485 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1487 *error = 0;
1489 if (i18n_timelength == 0)
1491 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1493 for (i = 0; panel_fields[i].id != NULL; i++)
1494 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1495 panel_fields[i].min_size = i18n_timelength;
1499 * This makes sure that the panel and mini status full/half mode
1500 * setting is equal
1502 format = parse_panel_size (panel, format, isstatus);
1504 while (*format)
1505 { /* format can be an empty string */
1506 int found = 0;
1508 darr = g_new0 (format_e, 1);
1510 /* I'm so ugly, don't look at me :-) */
1511 if (!home)
1512 home = old = darr;
1514 old->next = darr;
1515 darr->next = 0;
1516 old = darr;
1518 format = skip_separators (format);
1520 if (strchr ("<=>", *format))
1522 set_justify = 1;
1523 switch (*format)
1525 case '<':
1526 justify = J_LEFT;
1527 break;
1528 case '=':
1529 justify = J_CENTER;
1530 break;
1531 case '>':
1532 default:
1533 justify = J_RIGHT;
1534 break;
1536 format = skip_separators (format + 1);
1538 else
1539 set_justify = 0;
1541 for (i = 0; panel_fields[i].id != NULL; i++)
1543 size_t klen = strlen (panel_fields[i].id);
1545 if (strncmp (format, panel_fields[i].id, klen) != 0)
1546 continue;
1548 format += klen;
1550 darr->requested_field_len = panel_fields[i].min_size;
1551 darr->string_fn = panel_fields[i].string_fn;
1552 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1554 darr->id = panel_fields[i].id;
1555 darr->expand = panel_fields[i].expands;
1556 darr->just_mode = panel_fields[i].default_just;
1558 if (set_justify)
1560 if (IS_FIT (darr->just_mode))
1561 darr->just_mode = MAKE_FIT (justify);
1562 else
1563 darr->just_mode = justify;
1565 found = 1;
1567 format = skip_separators (format);
1569 /* If we have a size specifier */
1570 if (*format == ':')
1572 int req_length;
1574 /* If the size was specified, we don't want
1575 * auto-expansion by default
1577 darr->expand = 0;
1578 format++;
1579 req_length = atoi (format);
1580 darr->requested_field_len = req_length;
1582 format = skip_numbers (format);
1584 /* Now, if they insist on expansion */
1585 if (*format == '+')
1587 darr->expand = 1;
1588 format++;
1593 break;
1595 if (!found)
1597 char *tmp_format = g_strdup (format);
1599 int pos = min (8, strlen (format));
1600 delete_format (home);
1601 tmp_format[pos] = 0;
1602 *error =
1603 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1604 g_free (tmp_format);
1605 return 0;
1607 total_cols += darr->requested_field_len;
1610 *res_total_cols = total_cols;
1611 return home;
1614 /* --------------------------------------------------------------------------------------------- */
1616 static format_e *
1617 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1619 #define MAX_EXPAND 4
1620 int expand_top = 0; /* Max used element in expand */
1621 int usable_columns; /* Usable columns in the panel */
1622 int total_cols = 0;
1623 int i;
1624 format_e *darr, *home;
1626 if (!format)
1627 format = DEFAULT_USER_FORMAT;
1629 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1631 if (*error)
1632 return 0;
1634 panel->dirty = 1;
1636 /* Status needn't to be split */
1637 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1639 : (panel->split + 1))) - (!isstatus
1640 && panel->split);
1642 /* Look for the expandable fields and set field_len based on the requested field len */
1643 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1645 darr->field_len = darr->requested_field_len;
1646 if (darr->expand)
1647 expand_top++;
1650 /* If we used more columns than the available columns, adjust that */
1651 if (total_cols > usable_columns)
1653 int pdif, dif = total_cols - usable_columns;
1655 while (dif)
1657 pdif = dif;
1658 for (darr = home; darr; darr = darr->next)
1660 if (dif && darr->field_len - 1)
1662 darr->field_len--;
1663 dif--;
1667 /* avoid endless loop if num fields > 40 */
1668 if (pdif == dif)
1669 break;
1671 total_cols = usable_columns; /* give up, the rest should be truncated */
1674 /* Expand the available space */
1675 if ((usable_columns > total_cols) && expand_top)
1677 int spaces = (usable_columns - total_cols) / expand_top;
1678 int extra = (usable_columns - total_cols) % expand_top;
1680 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1681 if (darr->expand)
1683 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1684 i++;
1687 return home;
1690 /* --------------------------------------------------------------------------------------------- */
1691 /** Given the panel->view_type returns the format string to be parsed */
1693 static const char *
1694 panel_format (WPanel * panel)
1696 switch (panel->list_type)
1698 case list_long:
1699 return "full perm space nlink space owner space group space size space mtime space name";
1701 case list_brief:
1702 return "half 2 type name";
1704 case list_user:
1705 return panel->user_format;
1707 default:
1708 case list_full:
1709 return "half type name | size | mtime";
1713 /* --------------------------------------------------------------------------------------------- */
1715 static const char *
1716 mini_status_format (WPanel * panel)
1718 if (panel->user_mini_status)
1719 return panel->user_status_format[panel->list_type];
1721 switch (panel->list_type)
1724 case list_long:
1725 return "full perm space nlink space owner space group space size space mtime space name";
1727 case list_brief:
1728 return "half type name space bsize space perm space";
1730 case list_full:
1731 return "half type name";
1733 default:
1734 case list_user:
1735 return panel->user_format;
1739 /* */
1740 /* Panel operation commands */
1741 /* */
1743 /* --------------------------------------------------------------------------------------------- */
1744 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1746 static cb_ret_t
1747 maybe_cd (int move_up_dir)
1749 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1751 if (move_up_dir)
1753 do_cd ("..", cd_exact);
1754 return MSG_HANDLED;
1757 if (S_ISDIR (selection (current_panel)->st.st_mode)
1758 || link_isdir (selection (current_panel)))
1760 do_cd (selection (current_panel)->fname, cd_exact);
1761 return MSG_HANDLED;
1764 return MSG_NOT_HANDLED;
1767 /* --------------------------------------------------------------------------------------------- */
1769 /* if command line is empty then do 'cd ..' */
1770 static cb_ret_t
1771 force_maybe_cd (void)
1773 if (cmdline->buffer[0] == '\0')
1775 do_cd ("..", cd_exact);
1776 return MSG_HANDLED;
1778 return MSG_NOT_HANDLED;
1781 /* --------------------------------------------------------------------------------------------- */
1783 static void
1784 unselect_item (WPanel * panel)
1786 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1789 /* --------------------------------------------------------------------------------------------- */
1791 static void
1792 move_down (WPanel * panel)
1794 if (panel->selected + 1 == panel->count)
1795 return;
1797 unselect_item (panel);
1798 panel->selected++;
1799 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1801 /* Scroll window half screen */
1802 panel->top_file += ITEMS (panel) / 2;
1803 if (panel->top_file > panel->count - ITEMS (panel))
1804 panel->top_file = panel->count - ITEMS (panel);
1805 paint_dir (panel);
1807 select_item (panel);
1810 /* --------------------------------------------------------------------------------------------- */
1812 static void
1813 move_up (WPanel * panel)
1815 if (panel->selected == 0)
1816 return;
1818 unselect_item (panel);
1819 panel->selected--;
1820 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1822 /* Scroll window half screen */
1823 panel->top_file -= ITEMS (panel) / 2;
1824 if (panel->top_file < 0)
1825 panel->top_file = 0;
1826 paint_dir (panel);
1828 select_item (panel);
1831 /* --------------------------------------------------------------------------------------------- */
1832 /** Changes the selection by lines (may be negative) */
1834 static void
1835 move_selection (WPanel * panel, int lines)
1837 int new_pos;
1838 int adjust = 0;
1840 new_pos = panel->selected + lines;
1841 if (new_pos >= panel->count)
1842 new_pos = panel->count - 1;
1844 if (new_pos < 0)
1845 new_pos = 0;
1847 unselect_item (panel);
1848 panel->selected = new_pos;
1850 if (panel->selected - panel->top_file >= ITEMS (panel))
1852 panel->top_file += lines;
1853 adjust = 1;
1856 if (panel->selected - panel->top_file < 0)
1858 panel->top_file += lines;
1859 adjust = 1;
1862 if (adjust)
1864 if (panel->top_file > panel->selected)
1865 panel->top_file = panel->selected;
1866 if (panel->top_file < 0)
1867 panel->top_file = 0;
1868 paint_dir (panel);
1870 select_item (panel);
1873 /* --------------------------------------------------------------------------------------------- */
1875 static cb_ret_t
1876 move_left (WPanel * panel)
1878 if (panel->split)
1880 move_selection (panel, -llines (panel));
1881 return MSG_HANDLED;
1883 else
1884 return maybe_cd (1); /* cd .. */
1887 /* --------------------------------------------------------------------------------------------- */
1889 static cb_ret_t
1890 move_right (WPanel * panel)
1892 if (panel->split)
1894 move_selection (panel, llines (panel));
1895 return MSG_HANDLED;
1897 else
1898 return maybe_cd (0); /* cd (selection) */
1901 /* --------------------------------------------------------------------------------------------- */
1903 static void
1904 prev_page (WPanel * panel)
1906 int items;
1908 if (!panel->selected && !panel->top_file)
1909 return;
1910 unselect_item (panel);
1911 items = ITEMS (panel);
1912 if (panel->top_file < items)
1913 items = panel->top_file;
1914 if (!items)
1915 panel->selected = 0;
1916 else
1917 panel->selected -= items;
1918 panel->top_file -= items;
1920 select_item (panel);
1921 paint_dir (panel);
1924 /* --------------------------------------------------------------------------------------------- */
1926 static void
1927 goto_parent_dir (WPanel * panel)
1929 if (!panel->is_panelized)
1930 do_cd ("..", cd_exact);
1931 else
1933 char *fname = panel->dir.list[panel->selected].fname;
1934 const char *bname;
1935 char *dname;
1937 if (g_path_is_absolute (fname))
1938 fname = g_strdup (fname);
1939 else
1940 fname = mc_build_filename (panelized_panel.root, fname, (char *) NULL);
1942 bname = x_basename (fname);
1944 if (bname == fname)
1945 dname = g_strdup (".");
1946 else
1947 dname = g_strndup (fname, bname - fname);
1949 do_cd (dname, cd_exact);
1950 try_to_select (panel, bname);
1952 g_free (dname);
1953 g_free (fname);
1957 /* --------------------------------------------------------------------------------------------- */
1959 static void
1960 next_page (WPanel * panel)
1962 int items;
1964 if (panel->selected == panel->count - 1)
1965 return;
1966 unselect_item (panel);
1967 items = ITEMS (panel);
1968 if (panel->top_file > panel->count - 2 * items)
1969 items = panel->count - items - panel->top_file;
1970 if (panel->top_file + items < 0)
1971 items = -panel->top_file;
1972 if (!items)
1973 panel->selected = panel->count - 1;
1974 else
1975 panel->selected += items;
1976 panel->top_file += items;
1978 select_item (panel);
1979 paint_dir (panel);
1982 /* --------------------------------------------------------------------------------------------- */
1984 static void
1985 goto_child_dir (WPanel * panel)
1987 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
1989 do_cd (selection (panel)->fname, cd_exact);
1993 /* --------------------------------------------------------------------------------------------- */
1995 static void
1996 goto_top_file (WPanel * panel)
1998 unselect_item (panel);
1999 panel->selected = panel->top_file;
2000 select_item (panel);
2003 /* --------------------------------------------------------------------------------------------- */
2005 static void
2006 goto_middle_file (WPanel * panel)
2008 unselect_item (panel);
2009 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2010 select_item (panel);
2013 /* --------------------------------------------------------------------------------------------- */
2015 static void
2016 goto_bottom_file (WPanel * panel)
2018 unselect_item (panel);
2019 panel->selected = panel->top_file + ITEMS (panel) - 1;
2020 select_item (panel);
2023 /* --------------------------------------------------------------------------------------------- */
2025 static void
2026 move_home (WPanel * panel)
2028 if (panel->selected == 0)
2029 return;
2031 unselect_item (panel);
2033 if (panels_options.torben_fj_mode)
2035 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2037 if (panel->selected > middle_pos)
2039 goto_middle_file (panel);
2040 return;
2042 if (panel->selected != panel->top_file)
2044 goto_top_file (panel);
2045 return;
2049 panel->top_file = 0;
2050 panel->selected = 0;
2052 paint_dir (panel);
2053 select_item (panel);
2056 /* --------------------------------------------------------------------------------------------- */
2058 static void
2059 move_end (WPanel * panel)
2061 if (panel->selected == panel->count - 1)
2062 return;
2064 unselect_item (panel);
2066 if (panels_options.torben_fj_mode)
2068 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2070 if (panel->selected < middle_pos)
2072 goto_middle_file (panel);
2073 return;
2075 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2077 goto_bottom_file (panel);
2078 return;
2082 panel->selected = panel->count - 1;
2083 paint_dir (panel);
2084 select_item (panel);
2087 /* --------------------------------------------------------------------------------------------- */
2089 static void
2090 do_mark_file (WPanel * panel, mark_act_t do_move)
2092 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2093 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2094 move_down (panel);
2095 else if (do_move == MARK_FORCE_UP)
2096 move_up (panel);
2099 /* --------------------------------------------------------------------------------------------- */
2101 static void
2102 mark_file (WPanel * panel)
2104 do_mark_file (panel, MARK_DOWN);
2107 /* --------------------------------------------------------------------------------------------- */
2109 static void
2110 mark_file_up (WPanel * panel)
2112 do_mark_file (panel, MARK_FORCE_UP);
2115 /* --------------------------------------------------------------------------------------------- */
2117 static void
2118 mark_file_down (WPanel * panel)
2120 do_mark_file (panel, MARK_FORCE_DOWN);
2123 /* --------------------------------------------------------------------------------------------- */
2125 static void
2126 mark_file_right (WPanel * panel)
2128 int lines = llines (panel);
2130 if (state_mark < 0)
2131 state_mark = selection (panel)->f.marked ? 0 : 1;
2133 lines = min (lines, panel->count - panel->selected - 1);
2134 for (; lines != 0; lines--)
2136 do_file_mark (panel, panel->selected, state_mark);
2137 move_down (panel);
2139 do_file_mark (panel, panel->selected, state_mark);
2142 /* --------------------------------------------------------------------------------------------- */
2144 static void
2145 mark_file_left (WPanel * panel)
2147 int lines = llines (panel);
2149 if (state_mark < 0)
2150 state_mark = selection (panel)->f.marked ? 0 : 1;
2152 lines = min (lines, panel->selected + 1);
2153 for (; lines != 0; lines--)
2155 do_file_mark (panel, panel->selected, state_mark);
2156 move_up (panel);
2158 do_file_mark (panel, panel->selected, state_mark);
2161 /* --------------------------------------------------------------------------------------------- */
2162 /** Incremental search of a file name in the panel.
2163 * @param panel instance of WPanel structure
2164 * @param c_code key code
2167 static void
2168 do_search (WPanel * panel, int c_code)
2170 size_t l;
2171 int i, sel;
2172 gboolean wrapped = FALSE;
2173 char *act;
2174 mc_search_t *search;
2175 char *reg_exp, *esc_str;
2176 gboolean is_found = FALSE;
2178 l = strlen (panel->search_buffer);
2179 if (c_code == KEY_BACKSPACE)
2181 if (l != 0)
2183 act = panel->search_buffer + l;
2184 str_prev_noncomb_char (&act, panel->search_buffer);
2185 act[0] = '\0';
2187 panel->search_chpoint = 0;
2189 else
2191 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2193 panel->search_char[panel->search_chpoint] = c_code;
2194 panel->search_chpoint++;
2197 if (panel->search_chpoint > 0)
2199 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2201 case -2:
2202 return;
2203 case -1:
2204 panel->search_chpoint = 0;
2205 return;
2206 default:
2207 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2209 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2210 l += panel->search_chpoint;
2211 *(panel->search_buffer + l) = '\0';
2212 panel->search_chpoint = 0;
2218 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2219 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2220 search = mc_search_new (esc_str, -1);
2221 search->search_type = MC_SEARCH_T_GLOB;
2222 search->is_entire_line = TRUE;
2223 switch (panels_options.qsearch_mode)
2225 case QSEARCH_CASE_SENSITIVE:
2226 search->is_case_sensitive = TRUE;
2227 break;
2228 case QSEARCH_CASE_INSENSITIVE:
2229 search->is_case_sensitive = FALSE;
2230 break;
2231 default:
2232 search->is_case_sensitive = panel->sort_info.case_sensitive;
2233 break;
2235 sel = panel->selected;
2236 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2238 if (i >= panel->count)
2240 i = 0;
2241 if (wrapped)
2242 break;
2243 wrapped = TRUE;
2245 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2247 sel = i;
2248 is_found = TRUE;
2249 break;
2252 if (is_found)
2254 unselect_item (panel);
2255 panel->selected = sel;
2256 select_item (panel);
2257 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2259 else if (c_code != KEY_BACKSPACE)
2261 act = panel->search_buffer + l;
2262 str_prev_noncomb_char (&act, panel->search_buffer);
2263 act[0] = '\0';
2265 mc_search_free (search);
2266 g_free (reg_exp);
2267 g_free (esc_str);
2270 /* --------------------------------------------------------------------------------------------- */
2271 /** Start new search.
2272 * @param panel instance of WPanel structure
2275 static void
2276 start_search (WPanel * panel)
2278 if (panel->searching)
2280 if (panel->selected + 1 == panel->count)
2281 panel->selected = 0;
2282 else
2283 move_down (panel);
2285 /* in case if there was no search string we need to recall
2286 previous string, with which we ended previous searching */
2287 if (panel->search_buffer[0] == '\0')
2288 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2289 sizeof (panel->search_buffer));
2291 do_search (panel, 0);
2293 else
2295 panel->searching = TRUE;
2296 panel->search_buffer[0] = '\0';
2297 panel->search_char[0] = '\0';
2298 panel->search_chpoint = 0;
2299 display_mini_info (panel);
2300 mc_refresh ();
2304 /* --------------------------------------------------------------------------------------------- */
2306 static void
2307 stop_search (WPanel * panel)
2309 panel->searching = FALSE;
2311 /* if user had overrdied search string, we need to store it
2312 to the previous_search_buffer */
2313 if (panel->search_buffer[0] != '\0')
2314 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2315 sizeof (panel->prev_search_buffer));
2317 display_mini_info (panel);
2320 /* --------------------------------------------------------------------------------------------- */
2321 /** Return 1 if the Enter key has been processed, 0 otherwise */
2323 static int
2324 do_enter_on_file_entry (file_entry * fe)
2326 char *full_name;
2329 * Directory or link to directory - change directory.
2330 * Try the same for the entries on which mc_lstat() has failed.
2332 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2334 if (!do_cd (fe->fname, cd_exact))
2335 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2336 return 1;
2339 /* Try associated command */
2340 if (regex_command (fe->fname, "Open", NULL) != 0)
2341 return 1;
2343 /* Check if the file is executable */
2344 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2345 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe))
2347 g_free (full_name);
2348 return 0;
2350 g_free (full_name);
2352 if (confirm_execute)
2354 if (query_dialog
2355 (_("The Midnight Commander"),
2356 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2357 return 1;
2360 if (!vfs_current_is_local ())
2362 int ret;
2363 vfs_path_t *tmp_vpath;
2365 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2366 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2367 vfs_path_free (tmp_vpath);
2368 /* We took action only if the dialog was shown or the execution
2369 * was successful */
2370 return confirm_execute || (ret == 0);
2374 char *tmp = name_quote (fe->fname, 0);
2375 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2376 g_free (tmp);
2377 shell_execute (cmd, 0);
2378 g_free (cmd);
2381 #ifdef HAVE_CHARSET
2382 mc_global.source_codepage = default_source_codepage;
2383 #endif
2385 return 1;
2388 /* --------------------------------------------------------------------------------------------- */
2390 static int
2391 do_enter (WPanel * panel)
2393 return do_enter_on_file_entry (selection (panel));
2396 /* --------------------------------------------------------------------------------------------- */
2398 static void
2399 chdir_other_panel (WPanel * panel)
2401 const file_entry *entry = &panel->dir.list[panel->selected];
2403 char *new_dir;
2404 char *sel_entry = NULL;
2406 if (get_other_type () != view_listing)
2408 set_display_type (get_other_index (), view_listing);
2411 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2412 new_dir = mc_build_filename (panel->cwd, entry->fname, (char *) NULL);
2413 else
2415 new_dir = mc_build_filename (panel->cwd, "..", (char *) NULL);
2416 sel_entry = strrchr (panel->cwd, PATH_SEP);
2419 change_panel ();
2420 do_cd (new_dir, cd_exact);
2421 if (sel_entry)
2422 try_to_select (current_panel, sel_entry);
2423 change_panel ();
2425 move_down (panel);
2427 g_free (new_dir);
2430 /* --------------------------------------------------------------------------------------------- */
2432 * Make the current directory of the current panel also the current
2433 * directory of the other panel. Put the other panel to the listing
2434 * mode if needed. If the current panel is panelized, the other panel
2435 * doesn't become panelized.
2438 static void
2439 panel_sync_other (const WPanel * panel)
2441 if (get_other_type () != view_listing)
2443 set_display_type (get_other_index (), view_listing);
2446 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2448 /* try to select current filename on the other panel */
2449 if (!panel->is_panelized)
2451 try_to_select (other_panel, selection (panel)->fname);
2455 /* --------------------------------------------------------------------------------------------- */
2457 static void
2458 chdir_to_readlink (WPanel * panel)
2460 char *new_dir;
2462 if (get_other_type () != view_listing)
2463 return;
2465 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2467 char buffer[MC_MAXPATHLEN], *p;
2468 int i;
2469 struct stat st;
2470 vfs_path_t *panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2472 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2473 if (i < 0)
2475 vfs_path_free (panel_fname_vpath);
2476 return;
2478 if (mc_stat (panel_fname_vpath, &st) < 0)
2480 vfs_path_free (panel_fname_vpath);
2481 return;
2483 vfs_path_free (panel_fname_vpath);
2484 buffer[i] = 0;
2485 if (!S_ISDIR (st.st_mode))
2487 p = strrchr (buffer, PATH_SEP);
2488 if (p && !p[1])
2490 *p = 0;
2491 p = strrchr (buffer, PATH_SEP);
2493 if (!p)
2494 return;
2495 p[1] = 0;
2497 if (*buffer == PATH_SEP)
2498 new_dir = g_strdup (buffer);
2499 else
2500 new_dir = concat_dir_and_file (panel->cwd, buffer);
2502 change_panel ();
2503 do_cd (new_dir, cd_exact);
2504 change_panel ();
2506 move_down (panel);
2508 g_free (new_dir);
2512 /* --------------------------------------------------------------------------------------------- */
2514 static gsize
2515 panel_get_format_field_count (WPanel * panel)
2517 format_e *format;
2518 gsize lc_index;
2519 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2520 return lc_index;
2523 /* --------------------------------------------------------------------------------------------- */
2525 function return 0 if not found and REAL_INDEX+1 if found
2528 static gsize
2529 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2531 format_e *format;
2532 gsize lc_index;
2534 for (lc_index = 1, format = panel->format;
2535 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2536 if (format == NULL)
2537 lc_index = 0;
2539 return lc_index;
2542 /* --------------------------------------------------------------------------------------------- */
2544 static format_e *
2545 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2547 format_e *format;
2548 for (format = panel->format;
2549 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2550 return format;
2553 /* --------------------------------------------------------------------------------------------- */
2555 static const panel_field_t *
2556 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2558 const panel_field_t *pfield;
2559 format_e *format;
2561 format = panel_get_format_field_by_index (panel, lc_index);
2562 if (format == NULL)
2563 return NULL;
2564 pfield = panel_get_field_by_title (format->title);
2565 if (pfield == NULL)
2566 return NULL;
2567 if (pfield->sort_routine == NULL)
2568 return NULL;
2569 return pfield;
2572 /* --------------------------------------------------------------------------------------------- */
2574 static void
2575 panel_toggle_sort_order_prev (WPanel * panel)
2577 gsize lc_index, i;
2578 gchar *title;
2580 const panel_field_t *pfield = NULL;
2582 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2583 lc_index = panel_get_format_field_index_by_name (panel, title);
2584 g_free (title);
2586 if (lc_index > 1)
2588 /* search for prev sortable column in panel format */
2589 for (i = lc_index - 1;
2590 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2593 if (pfield == NULL)
2595 /* Sortable field not found. Try to search in each array */
2596 for (i = panel_get_format_field_count (panel);
2597 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2600 if (pfield != NULL)
2602 panel->sort_info.sort_field = pfield;
2603 panel_set_sort_order (panel, pfield);
2607 /* --------------------------------------------------------------------------------------------- */
2609 static void
2610 panel_toggle_sort_order_next (WPanel * panel)
2612 gsize lc_index, i;
2613 const panel_field_t *pfield = NULL;
2614 gsize format_field_count;
2615 gchar *title;
2617 format_field_count = panel_get_format_field_count (panel);
2618 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2619 lc_index = panel_get_format_field_index_by_name (panel, title);
2620 g_free (title);
2622 if (lc_index != 0 && lc_index != format_field_count)
2624 /* search for prev sortable column in panel format */
2625 for (i = lc_index;
2626 i != format_field_count
2627 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2630 if (pfield == NULL)
2632 /* Sortable field not found. Try to search in each array */
2633 for (i = 0;
2634 i != format_field_count
2635 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2638 if (pfield != NULL)
2640 panel->sort_info.sort_field = pfield;
2641 panel_set_sort_order (panel, pfield);
2645 /* --------------------------------------------------------------------------------------------- */
2647 static void
2648 panel_select_sort_order (WPanel * panel)
2650 const panel_field_t *sort_order;
2652 sort_order = sort_box (&panel->sort_info);
2653 if (sort_order != NULL)
2655 panel->sort_info.sort_field = sort_order;
2656 panel_set_sort_order (panel, sort_order);
2660 /* --------------------------------------------------------------------------------------------- */
2662 static void
2663 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2665 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2667 const panel_field_t *sort_order;
2669 sort_order = panel_get_field_by_id (name);
2670 if (sort_order == NULL)
2671 return;
2672 panel->sort_info.sort_field = sort_order;
2674 else
2675 panel->sort_info.reverse = !panel->sort_info.reverse;
2677 panel_set_sort_order (panel, panel->sort_info.sort_field);
2680 /* --------------------------------------------------------------------------------------------- */
2682 * If we moved to the parent directory move the selection pointer to
2683 * the old directory name; If we leave VFS dir, remove FS specificator.
2685 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2688 static const char *
2689 get_parent_dir_name (const char *cwd, const char *lwd)
2691 size_t llen, clen;
2692 const char *p;
2694 llen = strlen (lwd);
2695 clen = strlen (cwd);
2697 if (llen <= clen)
2698 return NULL;
2700 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2702 if (p == NULL)
2704 p = strrchr (lwd, PATH_SEP);
2706 if ((p != NULL)
2707 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2708 && (clen == (size_t) (p - lwd)
2709 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2710 return (p + 1);
2712 return NULL;
2715 /* skip VFS prefix */
2716 while (--p > lwd && *p != PATH_SEP)
2718 /* get last component */
2719 while (--p > lwd && *p != PATH_SEP)
2722 /* return last component */
2723 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
2726 /* --------------------------------------------------------------------------------------------- */
2727 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2729 static void
2730 subshell_chdir (const char *directory)
2732 #ifdef HAVE_SUBSHELL_SUPPORT
2733 if (mc_global.tty.use_subshell && vfs_current_is_local ())
2734 do_subshell_chdir (directory, FALSE, TRUE);
2735 #endif /* HAVE_SUBSHELL_SUPPORT */
2738 /* --------------------------------------------------------------------------------------------- */
2740 * Changes the current directory of the panel.
2741 * Don't record change in the directory history.
2744 static gboolean
2745 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
2747 vfs_path_t *vpath;
2748 char *olddir;
2749 char temp[MC_MAXPATHLEN];
2751 if (cd_type == cd_parse_command)
2753 while (*new_dir == ' ')
2754 new_dir++;
2757 olddir = g_strdup (panel->cwd);
2759 /* Convert *new_path to a suitable pathname, handle ~user */
2761 if (cd_type == cd_parse_command)
2763 if (!strcmp (new_dir, "-"))
2765 strcpy (temp, panel->lwd);
2766 new_dir = temp;
2769 vpath = vfs_path_from_str (*new_dir ? new_dir : mc_config_get_home_dir ());
2771 if (mc_chdir (vpath) == -1)
2773 strcpy (panel->cwd, olddir);
2774 g_free (olddir);
2775 vfs_path_free (vpath);
2776 return FALSE;
2778 vfs_path_free (vpath);
2780 /* Success: save previous directory, shutdown status of previous dir */
2781 strcpy (panel->lwd, olddir);
2782 input_free_completions (cmdline);
2784 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
2786 vfs_release_path (olddir);
2788 subshell_chdir (panel->cwd);
2790 /* Reload current panel */
2791 panel_clean_dir (panel);
2792 panel->count =
2793 do_load_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
2794 panel->sort_info.reverse, panel->sort_info.case_sensitive,
2795 panel->sort_info.exec_first, panel->filter);
2796 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
2797 load_hint (0);
2798 panel->dirty = 1;
2799 update_xterm_title_path ();
2801 g_free (olddir);
2803 return TRUE;
2806 /* --------------------------------------------------------------------------------------------- */
2808 static void
2809 directory_history_next (WPanel * panel)
2811 GList *nextdir;
2813 nextdir = g_list_next (panel->dir_history);
2815 if ((nextdir != NULL) && (_do_panel_cd (panel, (char *) nextdir->data, cd_exact)))
2816 panel->dir_history = nextdir;
2819 /* --------------------------------------------------------------------------------------------- */
2821 static void
2822 directory_history_prev (WPanel * panel)
2824 GList *prevdir;
2826 prevdir = g_list_previous (panel->dir_history);
2828 if ((prevdir != NULL) && (_do_panel_cd (panel, (char *) prevdir->data, cd_exact)))
2829 panel->dir_history = prevdir;
2832 /* --------------------------------------------------------------------------------------------- */
2834 static void
2835 directory_history_list (WPanel * panel)
2837 char *s;
2839 s = history_show (&panel->dir_history, &panel->widget);
2841 if (s != NULL)
2843 if (_do_panel_cd (panel, s, cd_exact))
2844 directory_history_add (panel, panel->cwd);
2845 else
2846 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2847 g_free (s);
2851 /* --------------------------------------------------------------------------------------------- */
2853 static cb_ret_t
2854 panel_execute_cmd (WPanel * panel, unsigned long command)
2856 int res = MSG_HANDLED;
2858 if (command != CK_Search)
2859 stop_search (panel);
2862 switch (command)
2864 case CK_Up:
2865 case CK_Down:
2866 case CK_Left:
2867 case CK_Right:
2868 case CK_Bottom:
2869 case CK_Top:
2870 case CK_PageDown:
2871 case CK_PageUp:
2872 /* reset state of marks flag */
2873 state_mark = -1;
2874 break;
2876 switch (command)
2878 case CK_PanelOtherCd:
2879 chdir_other_panel (panel);
2880 break;
2881 case CK_PanelOtherCdLink:
2882 chdir_to_readlink (panel);
2883 break;
2884 case CK_CopySingle:
2885 copy_cmd_local ();
2886 break;
2887 case CK_DeleteSingle:
2888 delete_cmd_local ();
2889 break;
2890 case CK_Enter:
2891 do_enter (panel);
2892 break;
2893 case CK_ViewRaw:
2894 view_raw_cmd ();
2895 break;
2896 case CK_EditNew:
2897 edit_cmd_new ();
2898 break;
2899 case CK_MoveSingle:
2900 rename_cmd_local ();
2901 break;
2902 case CK_SelectInvert:
2903 select_invert_cmd ();
2904 break;
2905 case CK_Select:
2906 select_cmd ();
2907 break;
2908 case CK_Unselect:
2909 unselect_cmd ();
2910 break;
2911 case CK_PageDown:
2912 next_page (panel);
2913 break;
2914 case CK_PageUp:
2915 prev_page (panel);
2916 break;
2917 case CK_CdChild:
2918 goto_child_dir (panel);
2919 break;
2920 case CK_CdParent:
2921 goto_parent_dir (panel);
2922 break;
2923 case CK_History:
2924 directory_history_list (panel);
2925 break;
2926 case CK_HistoryNext:
2927 directory_history_next (panel);
2928 break;
2929 case CK_HistoryPrev:
2930 directory_history_prev (panel);
2931 break;
2932 case CK_BottomOnScreen:
2933 goto_bottom_file (panel);
2934 break;
2935 case CK_MiddleOnScreen:
2936 goto_middle_file (panel);
2937 break;
2938 case CK_TopOnScreen:
2939 goto_top_file (panel);
2940 break;
2941 case CK_Mark:
2942 mark_file (panel);
2943 break;
2944 case CK_MarkUp:
2945 mark_file_up (panel);
2946 break;
2947 case CK_MarkDown:
2948 mark_file_down (panel);
2949 break;
2950 case CK_MarkLeft:
2951 mark_file_left (panel);
2952 break;
2953 case CK_MarkRight:
2954 mark_file_right (panel);
2955 break;
2956 case CK_CdParentSmart:
2957 res = force_maybe_cd ();
2958 break;
2959 case CK_Up:
2960 move_up (panel);
2961 break;
2962 case CK_Down:
2963 move_down (panel);
2964 break;
2965 case CK_Left:
2966 res = move_left (panel);
2967 break;
2968 case CK_Right:
2969 res = move_right (panel);
2970 break;
2971 case CK_Bottom:
2972 move_end (panel);
2973 break;
2974 case CK_Top:
2975 move_home (panel);
2976 break;
2977 #ifdef HAVE_CHARSET
2978 case CK_SelectCodepage:
2979 panel_change_encoding (panel);
2980 break;
2981 #endif
2982 case CK_Search:
2983 start_search (panel);
2984 break;
2985 case CK_SearchStop:
2986 break;
2987 case CK_PanelOtherSync:
2988 panel_sync_other (panel);
2989 break;
2990 case CK_Sort:
2991 panel_select_sort_order (panel);
2992 break;
2993 case CK_SortPrev:
2994 panel_toggle_sort_order_prev (panel);
2995 break;
2996 case CK_SortNext:
2997 panel_toggle_sort_order_next (panel);
2998 break;
2999 case CK_SortReverse:
3000 panel->sort_info.reverse = !panel->sort_info.reverse;
3001 panel_set_sort_order (panel, panel->sort_info.sort_field);
3002 break;
3003 case CK_SortByName:
3004 panel_set_sort_type_by_id (panel, "name");
3005 break;
3006 case CK_SortByExt:
3007 panel_set_sort_type_by_id (panel, "extension");
3008 break;
3009 case CK_SortBySize:
3010 panel_set_sort_type_by_id (panel, "size");
3011 break;
3012 case CK_SortByMTime:
3013 panel_set_sort_type_by_id (panel, "mtime");
3014 break;
3016 return res;
3019 /* --------------------------------------------------------------------------------------------- */
3021 static cb_ret_t
3022 panel_key (WPanel * panel, int key)
3024 size_t i;
3026 if (is_abort_char (key))
3028 stop_search (panel);
3029 return MSG_HANDLED;
3032 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3034 do_search (panel, key);
3035 return MSG_HANDLED;
3038 for (i = 0; panel_map[i].key != 0; i++)
3039 if (key == panel_map[i].key)
3040 return panel_execute_cmd (panel, panel_map[i].command);
3042 if (panels_options.torben_fj_mode && key == ALT ('h'))
3044 goto_middle_file (panel);
3045 return MSG_HANDLED;
3048 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3050 start_search (panel);
3051 do_search (panel, key);
3052 return MSG_HANDLED;
3055 return MSG_NOT_HANDLED;
3058 /* --------------------------------------------------------------------------------------------- */
3060 static cb_ret_t
3061 panel_callback (Widget * w, widget_msg_t msg, int parm)
3063 vfs_path_t *vpath;
3064 WPanel *panel = (WPanel *) w;
3065 WButtonBar *bb;
3067 switch (msg)
3069 case WIDGET_INIT:
3070 /* subscribe to "history_load" event */
3071 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3072 /* subscribe to "history_save" event */
3073 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3074 return MSG_HANDLED;
3076 case WIDGET_DRAW:
3077 /* Repaint everything, including frame and separator */
3078 paint_frame (panel); /* including show_dir */
3079 paint_dir (panel);
3080 mini_info_separator (panel);
3081 display_mini_info (panel);
3082 panel->dirty = 0;
3083 return MSG_HANDLED;
3085 case WIDGET_FOCUS:
3086 state_mark = -1;
3087 current_panel = panel;
3088 panel->active = 1;
3089 vpath = vfs_path_from_str (panel->cwd);
3090 if (mc_chdir (vpath) != 0)
3092 char *cwd = strip_password (g_strdup (panel->cwd), 1);
3093 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3094 cwd, unix_error_string (errno));
3095 g_free (cwd);
3097 else
3098 subshell_chdir (panel->cwd);
3100 vfs_path_free (vpath);
3101 update_xterm_title_path ();
3102 select_item (panel);
3103 show_dir (panel);
3104 paint_dir (panel);
3105 panel->dirty = 0;
3107 bb = find_buttonbar (panel->widget.owner);
3108 midnight_set_buttonbar (bb);
3109 buttonbar_redraw (bb);
3110 return MSG_HANDLED;
3112 case WIDGET_UNFOCUS:
3113 /* Janne: look at this for the multiple panel options */
3114 stop_search (panel);
3115 panel->active = 0;
3116 show_dir (panel);
3117 unselect_item (panel);
3118 return MSG_HANDLED;
3120 case WIDGET_KEY:
3121 return panel_key (panel, parm);
3123 case WIDGET_COMMAND:
3124 return panel_execute_cmd (panel, parm);
3126 case WIDGET_DESTROY:
3127 /* unsubscribe from "history_load" event */
3128 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3129 /* unsubscribe from "history_save" event */
3130 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3131 panel_destroy (panel);
3132 free_my_statfs ();
3133 return MSG_HANDLED;
3135 default:
3136 return default_proc (msg, parm);
3140 /* --------------------------------------------------------------------------------------------- */
3141 /* */
3142 /* Panel mouse events support routines */
3143 /* */
3145 static void
3146 mouse_toggle_mark (WPanel * panel)
3148 do_mark_file (panel, MARK_DONT_MOVE);
3149 mouse_marking = selection (panel)->f.marked;
3150 mouse_mark_panel = current_panel;
3153 /* --------------------------------------------------------------------------------------------- */
3155 static void
3156 mouse_set_mark (WPanel * panel)
3159 if (mouse_mark_panel == panel)
3161 if (mouse_marking && !(selection (panel)->f.marked))
3162 do_mark_file (panel, MARK_DONT_MOVE);
3163 else if (!mouse_marking && (selection (panel)->f.marked))
3164 do_mark_file (panel, MARK_DONT_MOVE);
3168 /* --------------------------------------------------------------------------------------------- */
3170 static int
3171 mark_if_marking (WPanel * panel, Gpm_Event * event)
3173 if (event->buttons & GPM_B_RIGHT)
3175 if (event->type & GPM_DOWN)
3176 mouse_toggle_mark (panel);
3177 else
3178 mouse_set_mark (panel);
3179 return 1;
3181 return 0;
3184 /* --------------------------------------------------------------------------------------------- */
3185 /** Determine which column was clicked, and sort the panel on
3186 * that column, or reverse sort on that column if already
3187 * sorted on that column.
3190 static void
3191 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3193 int i;
3194 const char *lc_sort_name = NULL;
3195 panel_field_t *col_sort_format = NULL;
3196 format_e *format;
3197 gchar *title;
3199 for (i = 0, format = panel->format; format != NULL; format = format->next)
3201 i += format->field_len;
3202 if (event->x < i + 1)
3204 /* found column */
3205 lc_sort_name = format->title;
3206 break;
3210 if (lc_sort_name == NULL)
3211 return;
3213 for (i = 0; panel_fields[i].id != NULL; i++)
3215 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3216 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3218 col_sort_format = &panel_fields[i];
3219 g_free (title);
3220 break;
3222 g_free (title);
3225 if (col_sort_format == NULL)
3226 return;
3228 if (panel->sort_info.sort_field == col_sort_format)
3230 /* reverse the sort if clicked column is already the sorted column */
3231 panel->sort_info.reverse = !panel->sort_info.reverse;
3233 else
3235 /* new sort is forced to be ascending */
3236 panel->sort_info.reverse = FALSE;
3238 panel_set_sort_order (panel, col_sort_format);
3242 /* --------------------------------------------------------------------------------------------- */
3244 * Mouse callback of the panel minus repainting.
3245 * If the event is redirected to the menu, *redir is set to TRUE.
3247 static int
3248 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3250 const int lines = llines (panel);
3251 const gboolean is_active = dlg_widget_active (panel);
3252 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3254 *redir = FALSE;
3256 /* 1st line */
3257 if (mouse_down && event->y == 1)
3259 /* "<" button */
3260 if (event->x == 2)
3262 directory_history_prev (panel);
3263 return MOU_NORMAL;
3266 /* "." button show/hide hidden files */
3267 if (event->x == panel->widget.cols - 5)
3269 panel->widget.owner->callback (panel->widget.owner, NULL,
3270 DLG_ACTION, CK_ShowHidden, NULL);
3271 repaint_screen ();
3272 return MOU_NORMAL;
3275 /* ">" button */
3276 if (event->x == panel->widget.cols - 1)
3278 directory_history_next (panel);
3279 return MOU_NORMAL;
3282 /* "^" button */
3283 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3285 directory_history_list (panel);
3286 return MOU_NORMAL;
3289 /* rest of the upper frame, the menu is invisible - call menu */
3290 if (!menubar_visible)
3292 *redir = TRUE;
3293 event->x += panel->widget.x;
3294 return the_menubar->widget.mouse (event, the_menubar);
3297 /* no other events on 1st line */
3298 return MOU_NORMAL;
3301 /* sort on clicked column; don't handle wheel events */
3302 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3304 mouse_sort_col (event, panel);
3305 return MOU_NORMAL;
3308 /* Mouse wheel events */
3309 if (mouse_down && (event->buttons & GPM_B_UP))
3311 if (is_active)
3313 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3314 prev_page (panel);
3315 else /* We are in first page */
3316 move_up (panel);
3318 return MOU_NORMAL;
3321 if (mouse_down && (event->buttons & GPM_B_DOWN))
3323 if (is_active)
3325 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3326 next_page (panel);
3327 else /* We are in last page */
3328 move_down (panel);
3330 return MOU_NORMAL;
3333 event->y -= 2;
3334 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3336 int my_index;
3338 if (!is_active)
3339 change_panel ();
3341 if (panel->top_file + event->y > panel->count)
3342 my_index = panel->count - 1;
3343 else
3345 my_index = panel->top_file + event->y - 1;
3346 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3347 my_index += llines (panel);
3349 if (my_index >= panel->count)
3350 my_index = panel->count - 1;
3353 if (my_index != panel->selected)
3355 unselect_item (panel);
3356 panel->selected = my_index;
3357 select_item (panel);
3360 /* This one is new */
3361 mark_if_marking (panel, event);
3363 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3365 if (event->y > 0 && event->y <= lines)
3366 do_enter (panel);
3368 return MOU_NORMAL;
3371 /* --------------------------------------------------------------------------------------------- */
3372 /** Mouse callback of the panel */
3374 static int
3375 panel_event (Gpm_Event * event, void *data)
3377 WPanel *panel = data;
3378 int ret;
3379 gboolean redir;
3381 ret = do_panel_event (event, panel, &redir);
3382 if (!redir)
3383 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3385 return ret;
3388 /* --------------------------------------------------------------------------------------------- */
3390 static void
3391 reload_panelized (WPanel * panel)
3393 int i, j;
3394 dir_list *list = &panel->dir;
3395 vfs_path_t *vpath;
3397 if (panel != current_panel)
3399 int ret;
3401 vpath = vfs_path_from_str (panel->cwd);
3402 ret = mc_chdir (vpath);
3403 vfs_path_free (vpath);
3406 for (i = 0, j = 0; i < panel->count; i++)
3408 if (list->list[i].f.marked)
3410 /* Unmark the file in advance. In case the following mc_lstat
3411 * fails we are done, else we have to mark the file again
3412 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3413 * IMO that's the best way to update the panel's summary status
3414 * -- Norbert
3416 do_file_mark (panel, i, 0);
3418 vpath = vfs_path_from_str (list->list[i].fname);
3419 if (mc_lstat (vpath, &list->list[i].st))
3421 g_free (list->list[i].fname);
3422 vfs_path_free (vpath);
3423 continue;
3425 if (list->list[i].f.marked)
3426 do_file_mark (panel, i, 1);
3427 if (j != i)
3428 list->list[j] = list->list[i];
3429 j++;
3430 vfs_path_free (vpath);
3432 if (j == 0)
3433 panel->count = set_zero_dir (list) ? 1 : 0;
3434 else
3435 panel->count = j;
3437 if (panel != current_panel)
3439 int ret;
3441 vpath = vfs_path_from_str (current_panel->cwd);
3442 ret = mc_chdir (vpath);
3443 vfs_path_free (vpath);
3447 /* --------------------------------------------------------------------------------------------- */
3449 static void
3450 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3452 gboolean free_pointer;
3453 char *my_current_file = NULL;
3455 if ((flags & UP_RELOAD) != 0)
3457 vfs_path_t *tmp_vpath;
3459 tmp_vpath = vfs_path_from_str (panel->cwd);
3460 panel->is_panelized = FALSE;
3461 mc_setctl (tmp_vpath, VFS_SETCTL_FLUSH, 0);
3462 vfs_path_free (tmp_vpath);
3463 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3466 /* If current_file == -1 (an invalid pointer) then preserve selection */
3467 free_pointer = current_file == UP_KEEPSEL;
3469 if (free_pointer)
3471 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3472 current_file = my_current_file;
3475 if (panel->is_panelized)
3476 reload_panelized (panel);
3477 else
3478 panel_reload (panel);
3480 try_to_select (panel, current_file);
3481 panel->dirty = 1;
3483 if (free_pointer)
3484 g_free (my_current_file);
3487 /* --------------------------------------------------------------------------------------------- */
3489 static void
3490 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3492 if (get_display_type (which) == view_listing)
3494 WPanel *panel;
3496 panel = (WPanel *) get_panel_widget (which);
3497 if (panel->is_panelized)
3498 flags &= ~UP_RELOAD;
3499 update_one_panel_widget (panel, flags, current_file);
3503 /* --------------------------------------------------------------------------------------------- */
3504 /*** public functions ****************************************************************************/
3505 /* --------------------------------------------------------------------------------------------- */
3507 char *
3508 remove_encoding_from_path (const char *path)
3510 GString *ret;
3511 GString *tmp_path, *tmp_conv;
3512 char *tmp;
3514 ret = g_string_new ("");
3515 tmp_conv = g_string_new ("");
3516 tmp_path = g_string_new (path);
3518 while ((tmp = g_strrstr (tmp_path->str, PATH_SEP_STR VFS_ENCODING_PREFIX)) != NULL)
3520 GIConv converter;
3521 char *tmp2;
3523 vfs_path_t *vpath = vfs_path_from_str (tmp);
3524 vfs_path_element_t *path_element = vfs_path_get_by_index (vpath, -1);
3526 converter =
3527 path_element->encoding !=
3528 NULL ? str_crt_conv_to (path_element->encoding) : str_cnv_to_term;
3529 vfs_path_free (vpath);
3531 if (converter == INVALID_CONV)
3532 converter = str_cnv_to_term;
3534 tmp2 = tmp + 1;
3535 while (*tmp2 != '\0' && *tmp2 != PATH_SEP)
3536 tmp2++;
3538 if (*tmp2 != '\0')
3540 str_vfs_convert_from (converter, tmp2, tmp_conv);
3541 g_string_prepend (ret, tmp_conv->str);
3542 g_string_set_size (tmp_conv, 0);
3545 g_string_set_size (tmp_path, tmp - tmp_path->str);
3546 str_close_conv (converter);
3549 g_string_prepend (ret, tmp_path->str);
3550 g_string_free (tmp_path, TRUE);
3551 g_string_free (tmp_conv, TRUE);
3553 return g_string_free (ret, FALSE);
3556 /* --------------------------------------------------------------------------------------------- */
3558 static void
3559 do_select (WPanel * panel, int i)
3561 if (i != panel->selected)
3563 panel->dirty = 1;
3564 panel->selected = i;
3565 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3566 if (panel->top_file < 0)
3567 panel->top_file = 0;
3571 /* --------------------------------------------------------------------------------------------- */
3573 static void
3574 do_try_to_select (WPanel * panel, const char *name)
3576 int i;
3577 char *subdir;
3579 if (!name)
3581 do_select (panel, 0);
3582 return;
3585 /* We only want the last component of the directory,
3586 * and from this only the name without suffix. */
3587 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3589 /* Search that subdirectory, if found select it */
3590 for (i = 0; i < panel->count; i++)
3592 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3594 do_select (panel, i);
3595 g_free (subdir);
3596 return;
3600 /* Try to select a file near the file that is missing */
3601 if (panel->selected >= panel->count)
3602 do_select (panel, panel->count - 1);
3603 g_free (subdir);
3606 /* --------------------------------------------------------------------------------------------- */
3608 /* event callback */
3609 static gboolean
3610 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3611 gpointer init_data, gpointer data)
3613 (void) event_group_name;
3614 (void) event_name;
3615 (void) init_data;
3616 (void) data;
3618 update_panels (UP_RELOAD, UP_KEEPSEL);
3620 return TRUE;
3623 /* --------------------------------------------------------------------------------------------- */
3625 /* event callback */
3626 static gboolean
3627 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3628 gpointer init_data, gpointer data)
3630 (void) event_group_name;
3631 (void) event_name;
3632 (void) init_data;
3633 (void) data;
3635 if (current_panel->marked == 0)
3636 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3637 (gpointer) selection (current_panel)->fname);
3638 else
3640 int i;
3641 gboolean first = TRUE;
3642 char *flist = NULL;
3644 for (i = 0; i < current_panel->count; i++)
3645 if (current_panel->dir.list[i].f.marked != 0)
3646 { /* Skip the unmarked ones */
3647 if (first)
3649 flist = g_strdup (current_panel->dir.list[i].fname);
3650 first = FALSE;
3652 else
3654 /* Add empty lines after the file */
3655 char *tmp;
3657 tmp =
3658 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3659 g_free (flist);
3660 flist = tmp;
3664 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3665 g_free (flist);
3667 return TRUE;
3670 /* --------------------------------------------------------------------------------------------- */
3671 /*** public functions ****************************************************************************/
3672 /* --------------------------------------------------------------------------------------------- */
3674 void
3675 try_to_select (WPanel * panel, const char *name)
3677 do_try_to_select (panel, name);
3678 select_item (panel);
3681 /* --------------------------------------------------------------------------------------------- */
3683 void
3684 panel_clean_dir (WPanel * panel)
3686 int count = panel->count;
3688 panel->count = 0;
3689 panel->top_file = 0;
3690 panel->selected = 0;
3691 panel->marked = 0;
3692 panel->dirs_marked = 0;
3693 panel->total = 0;
3694 panel->searching = FALSE;
3695 panel->is_panelized = FALSE;
3696 panel->dirty = 1;
3698 clean_dir (&panel->dir, count);
3701 /* --------------------------------------------------------------------------------------------- */
3702 /** Panel creation.
3703 * @param panel_name the name of the panel for setup retieving
3704 * @returns new instance of WPanel
3707 WPanel *
3708 panel_new (const char *panel_name)
3710 return panel_new_with_dir (panel_name, NULL);
3713 /* --------------------------------------------------------------------------------------------- */
3714 /** Panel creation for specified directory.
3715 * @param panel_name specifies the name of the panel for setup retieving
3716 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3717 * @returns new instance of WPanel
3720 WPanel *
3721 panel_new_with_dir (const char *panel_name, const char *wpath)
3723 WPanel *panel;
3724 char *section;
3725 int i, err;
3726 char curdir[MC_MAXPATHLEN] = "\0";
3728 panel = g_new0 (WPanel, 1);
3730 /* No know sizes of the panel at startup */
3731 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3733 /* We do not want the cursor */
3734 widget_want_cursor (panel->widget, 0);
3736 if (wpath != NULL)
3738 g_strlcpy (panel->cwd, wpath, sizeof (panel->cwd));
3739 mc_get_current_wd (curdir, sizeof (curdir) - 2);
3741 else
3742 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3744 strcpy (panel->lwd, ".");
3746 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3747 /* directories history will be get later */
3749 panel->dir.list = g_new (file_entry, MIN_FILES);
3750 panel->dir.size = MIN_FILES;
3751 panel->active = 0;
3752 panel->filter = 0;
3753 panel->split = 0;
3754 panel->top_file = 0;
3755 panel->selected = 0;
3756 panel->marked = 0;
3757 panel->total = 0;
3758 panel->dirty = 1;
3759 panel->searching = FALSE;
3760 panel->dirs_marked = 0;
3761 panel->is_panelized = FALSE;
3762 panel->format = 0;
3763 panel->status_format = 0;
3764 panel->format_modified = 1;
3766 panel->panel_name = g_strdup (panel_name);
3767 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3769 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3771 for (i = 0; i < LIST_TYPES; i++)
3772 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3774 panel->search_buffer[0] = '\0';
3775 panel->prev_search_buffer[0] = '\0';
3776 panel->frame_size = frame_half;
3778 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3779 if (!mc_config_has_group (mc_main_config, section))
3781 g_free (section);
3782 section = g_strdup (panel->panel_name);
3784 panel_load_setup (panel, section);
3785 g_free (section);
3787 /* Load format strings */
3788 err = set_panel_formats (panel);
3789 if (err != 0)
3790 set_panel_formats (panel);
3792 #ifdef HAVE_CHARSET
3794 vfs_path_t *vpath = vfs_path_from_str (panel->cwd);
3795 vfs_path_element_t *path_element = vfs_path_get_by_index (vpath, -1);
3797 if (path_element->encoding != NULL)
3798 panel->codepage = get_codepage_index (path_element->encoding);
3800 vfs_path_free (vpath);
3802 #endif
3805 vfs_path_t *vpath;
3807 vpath = vfs_path_from_str (panel->cwd);
3808 if (mc_chdir (vpath) != 0)
3810 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3811 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3813 vfs_path_free (vpath);
3816 /* Load the default format */
3817 panel->count =
3818 do_load_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
3819 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3820 panel->sort_info.exec_first, panel->filter);
3822 /* Restore old right path */
3823 if (curdir[0] != '\0')
3825 vfs_path_t *vpath;
3827 vpath = vfs_path_from_str (curdir);
3828 err = mc_chdir (vpath);
3829 vfs_path_free (vpath);
3832 return panel;
3835 /* --------------------------------------------------------------------------------------------- */
3837 void
3838 panel_reload (WPanel * panel)
3840 struct stat current_stat;
3842 if (panels_options.fast_reload && !stat (panel->cwd, &current_stat)
3843 && current_stat.st_ctime == panel->dir_stat.st_ctime
3844 && current_stat.st_mtime == panel->dir_stat.st_mtime)
3845 return;
3849 vfs_path_t *vpath;
3850 gboolean ok;
3851 char *last_slash;
3853 vpath = vfs_path_from_str (panel->cwd);
3854 ok = (mc_chdir (vpath) != -1);
3855 vfs_path_free (vpath);
3856 if (ok)
3857 break;
3859 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0)
3861 panel_clean_dir (panel);
3862 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
3863 return;
3865 last_slash = strrchr (panel->cwd, PATH_SEP);
3866 if (!last_slash || last_slash == panel->cwd)
3867 strcpy (panel->cwd, PATH_SEP_STR);
3868 else
3869 *last_slash = 0;
3870 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3871 show_dir (panel);
3873 while (TRUE);
3875 panel->count =
3876 do_reload_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
3877 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
3878 panel->sort_info.exec_first, panel->filter);
3880 panel->dirty = 1;
3881 if (panel->selected >= panel->count)
3882 do_select (panel, panel->count - 1);
3884 recalculate_panel_summary (panel);
3887 /* --------------------------------------------------------------------------------------------- */
3888 /* Switches the panel to the mode specified in the format */
3889 /* Seting up both format and status string. Return: 0 - on success; */
3890 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
3893 set_panel_formats (WPanel * p)
3895 format_e *form;
3896 char *err = NULL;
3897 int retcode = 0;
3899 form = use_display_format (p, panel_format (p), &err, 0);
3901 if (err != NULL)
3903 g_free (err);
3904 retcode = 1;
3906 else
3908 delete_format (p->format);
3909 p->format = form;
3912 if (panels_options.show_mini_info)
3914 form = use_display_format (p, mini_status_format (p), &err, 1);
3916 if (err != NULL)
3918 g_free (err);
3919 retcode += 2;
3921 else
3923 delete_format (p->status_format);
3924 p->status_format = form;
3928 panel_format_modified (p);
3929 panel_update_cols (&(p->widget), p->frame_size);
3931 if (retcode)
3932 message (D_ERROR, _("Warning"),
3933 _("User supplied format looks invalid, reverting to default."));
3934 if (retcode & 0x01)
3936 g_free (p->user_format);
3937 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
3939 if (retcode & 0x02)
3941 g_free (p->user_status_format[p->list_type]);
3942 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
3945 return retcode;
3948 /* --------------------------------------------------------------------------------------------- */
3950 void
3951 panel_update_cols (Widget * widget, panel_display_t frame_size)
3953 int cols, origin;
3955 /* don't touch panel if it is not in dialog yet */
3956 /* if panel is not in dialog it is not in widgets list
3957 and cannot be compared with get_panel_widget() result */
3958 if (widget->owner == NULL)
3959 return;
3961 if (panels_layout.horizontal_split)
3963 widget->cols = COLS;
3964 return;
3967 if (frame_size == frame_full)
3969 cols = COLS;
3970 origin = 0;
3972 else if (widget == get_panel_widget (0))
3974 cols = panels_layout.left_panel_size;
3975 origin = 0;
3977 else
3979 cols = COLS - panels_layout.left_panel_size;
3980 origin = panels_layout.left_panel_size;
3983 widget->cols = cols;
3984 widget->x = origin;
3987 /* --------------------------------------------------------------------------------------------- */
3989 /* Select current item and readjust the panel */
3990 void
3991 select_item (WPanel * panel)
3993 /* Although currently all over the code we set the selection and
3994 top file to decent values before calling select_item, I could
3995 forget it someday, so it's better to do the actual fitting here */
3997 if (panel->selected < 0)
3998 panel->selected = 0;
4000 if (panel->selected > panel->count - 1)
4001 panel->selected = panel->count - 1;
4003 adjust_top_file (panel);
4005 panel->dirty = 1;
4007 execute_hooks (select_file_hook);
4010 /* --------------------------------------------------------------------------------------------- */
4011 /** Clears all files in the panel, used only when one file was marked */
4012 void
4013 unmark_files (WPanel * panel)
4015 int i;
4017 if (!panel->marked)
4018 return;
4019 for (i = 0; i < panel->count; i++)
4020 file_mark (panel, i, 0);
4022 panel->dirs_marked = 0;
4023 panel->marked = 0;
4024 panel->total = 0;
4027 /* --------------------------------------------------------------------------------------------- */
4028 /** Recalculate the panels summary information, used e.g. when marked
4029 files might have been removed by an external command */
4031 void
4032 recalculate_panel_summary (WPanel * panel)
4034 int i;
4036 panel->marked = 0;
4037 panel->dirs_marked = 0;
4038 panel->total = 0;
4040 for (i = 0; i < panel->count; i++)
4041 if (panel->dir.list[i].f.marked)
4043 /* do_file_mark will return immediately if newmark == oldmark.
4044 So we have to first unmark it to get panel's summary information
4045 updated. (Norbert) */
4046 panel->dir.list[i].f.marked = 0;
4047 do_file_mark (panel, i, 1);
4051 /* --------------------------------------------------------------------------------------------- */
4052 /** This routine marks a file or a directory */
4054 void
4055 do_file_mark (WPanel * panel, int idx, int mark)
4057 if (panel->dir.list[idx].f.marked == mark)
4058 return;
4060 /* Only '..' can't be marked, '.' isn't visible */
4061 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4062 return;
4064 file_mark (panel, idx, mark);
4065 if (panel->dir.list[idx].f.marked)
4067 panel->marked++;
4068 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4070 if (panel->dir.list[idx].f.dir_size_computed)
4071 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4072 panel->dirs_marked++;
4074 else
4075 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4076 set_colors (panel);
4078 else
4080 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4082 if (panel->dir.list[idx].f.dir_size_computed)
4083 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4084 panel->dirs_marked--;
4086 else
4087 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4088 panel->marked--;
4092 /* --------------------------------------------------------------------------------------------- */
4094 * Changes the current directory of the panel.
4095 * Record change in the directory history.
4097 gboolean
4098 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
4100 gboolean r;
4102 r = _do_panel_cd (panel, new_dir, cd_type);
4103 if (r)
4104 directory_history_add (panel, panel->cwd);
4105 return r;
4108 /* --------------------------------------------------------------------------------------------- */
4110 void
4111 file_mark (WPanel * panel, int lc_index, int val)
4113 if (panel->dir.list[lc_index].f.marked != val)
4115 panel->dir.list[lc_index].f.marked = val;
4116 panel->dirty = 1;
4120 /* --------------------------------------------------------------------------------------------- */
4122 void
4123 panel_re_sort (WPanel * panel)
4125 char *filename;
4126 int i;
4128 if (panel == NULL)
4129 return;
4131 filename = g_strdup (selection (panel)->fname);
4132 unselect_item (panel);
4133 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4134 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4135 panel->sort_info.exec_first);
4136 panel->selected = -1;
4137 for (i = panel->count; i; i--)
4139 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4141 panel->selected = i - 1;
4142 break;
4145 g_free (filename);
4146 panel->top_file = panel->selected - ITEMS (panel) / 2;
4147 select_item (panel);
4148 panel->dirty = 1;
4151 /* --------------------------------------------------------------------------------------------- */
4153 void
4154 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4156 if (sort_order == NULL)
4157 return;
4159 panel->sort_info.sort_field = sort_order;
4161 /* The directory is already sorted, we have to load the unsorted stuff */
4162 if (sort_order->sort_routine == (sortfn *) unsorted)
4164 char *current_file;
4166 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4167 panel_reload (panel);
4168 try_to_select (panel, current_file);
4169 g_free (current_file);
4171 panel_re_sort (panel);
4174 /* --------------------------------------------------------------------------------------------- */
4176 * Change panel encoding.
4177 * @param panel WPanel object
4180 void
4181 panel_change_encoding (WPanel * panel)
4183 const char *encoding = NULL;
4184 char *cd_path;
4185 #ifdef HAVE_CHARSET
4186 char *errmsg;
4187 int r;
4189 r = select_charset (-1, -1, panel->codepage, FALSE);
4191 if (r == SELECT_CHARSET_CANCEL)
4192 return; /* Cancel */
4194 panel->codepage = r;
4196 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4198 /* No translation */
4199 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4200 cd_path = remove_encoding_from_path (panel->cwd);
4201 do_panel_cd (panel, cd_path, cd_parse_command);
4202 g_free (cd_path);
4203 return;
4206 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4207 if (errmsg != NULL)
4209 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4210 g_free (errmsg);
4211 return;
4214 encoding = get_codepage_id (panel->codepage);
4215 #endif
4216 if (encoding != NULL)
4218 vfs_path_t *vpath = vfs_path_from_str (panel->cwd);
4220 vfs_change_encoding (vpath, encoding);
4222 cd_path = vfs_path_to_str (vpath);
4223 if (!do_panel_cd (panel, cd_path, cd_parse_command))
4224 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4225 g_free (cd_path);
4227 vfs_path_free (vpath);
4231 /* --------------------------------------------------------------------------------------------- */
4233 * This routine reloads the directory in both panels. It tries to
4234 * select current_file in current_panel and other_file in other_panel.
4235 * If current_file == -1 then it automatically sets current_file and
4236 * other_file to the currently selected files in the panels.
4238 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4239 * will not reload the other panel.
4242 void
4243 update_panels (panel_update_flags_t flags, const char *current_file)
4245 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4246 WPanel *panel;
4247 vfs_path_t *vpath;
4248 int ret;
4250 update_one_panel (get_current_index (), flags, current_file);
4251 if (reload_other)
4252 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4254 if (get_current_type () == view_listing)
4255 panel = (WPanel *) get_panel_widget (get_current_index ());
4256 else
4257 panel = (WPanel *) get_panel_widget (get_other_index ());
4259 if (!panel->is_panelized)
4261 vpath = vfs_path_from_str (panel->cwd);
4262 ret = mc_chdir (vpath);
4263 vfs_path_free (vpath);
4267 /* --------------------------------------------------------------------------------------------- */
4269 void
4270 directory_history_add (struct WPanel *panel, const char *dir)
4272 char *tmp;
4274 tmp = g_strdup (dir);
4275 strip_password (tmp, 1);
4277 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4280 /* --------------------------------------------------------------------------------------------- */
4282 gsize
4283 panel_get_num_of_sortable_fields (void)
4285 gsize ret = 0, lc_index;
4287 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4288 if (panel_fields[lc_index].is_user_choice)
4289 ret++;
4290 return ret;
4293 /* --------------------------------------------------------------------------------------------- */
4295 const char **
4296 panel_get_sortable_fields (gsize * array_size)
4298 char **ret;
4299 gsize lc_index, i;
4301 lc_index = panel_get_num_of_sortable_fields ();
4303 ret = g_try_new0 (char *, lc_index + 1);
4304 if (ret == NULL)
4305 return NULL;
4307 if (array_size != NULL)
4308 *array_size = lc_index;
4310 lc_index = 0;
4312 for (i = 0; panel_fields[i].id != NULL; i++)
4313 if (panel_fields[i].is_user_choice)
4314 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4315 return (const char **) ret;
4318 /* --------------------------------------------------------------------------------------------- */
4320 const panel_field_t *
4321 panel_get_field_by_id (const char *name)
4323 gsize lc_index;
4324 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4325 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4326 return &panel_fields[lc_index];
4327 return NULL;
4330 /* --------------------------------------------------------------------------------------------- */
4332 const panel_field_t *
4333 panel_get_field_by_title_hotkey (const char *name)
4335 gsize lc_index;
4336 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4337 if (panel_fields[lc_index].title_hotkey != NULL &&
4338 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4339 return &panel_fields[lc_index];
4340 return NULL;
4343 /* --------------------------------------------------------------------------------------------- */
4345 const panel_field_t *
4346 panel_get_field_by_title (const char *name)
4348 gsize lc_index;
4349 gchar *title = NULL;
4351 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4353 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4354 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4356 g_free (title);
4357 return &panel_fields[lc_index];
4360 g_free (title);
4361 return NULL;
4364 /* --------------------------------------------------------------------------------------------- */
4366 gsize
4367 panel_get_num_of_user_possible_fields (void)
4369 gsize ret = 0, lc_index;
4371 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4372 if (panel_fields[lc_index].use_in_user_format)
4373 ret++;
4374 return ret;
4377 /* --------------------------------------------------------------------------------------------- */
4379 const char **
4380 panel_get_user_possible_fields (gsize * array_size)
4382 char **ret;
4383 gsize lc_index, i;
4385 lc_index = panel_get_num_of_user_possible_fields ();
4387 ret = g_try_new0 (char *, lc_index + 1);
4388 if (ret == NULL)
4389 return NULL;
4391 if (array_size != NULL)
4392 *array_size = lc_index;
4394 lc_index = 0;
4396 for (i = 0; panel_fields[i].id != NULL; i++)
4397 if (panel_fields[i].use_in_user_format)
4398 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4399 return (const char **) ret;
4402 /* --------------------------------------------------------------------------------------------- */
4404 void
4405 panel_init (void)
4407 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4408 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4410 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4411 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4412 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4413 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4414 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4416 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4417 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4418 panel_save_curent_file_to_clip_file, NULL, NULL);
4422 /* --------------------------------------------------------------------------------------------- */
4424 void
4425 panel_deinit (void)
4427 g_free (panel_sort_up_sign);
4428 g_free (panel_sort_down_sign);
4430 g_free (panel_hiddenfiles_sign_show);
4431 g_free (panel_hiddenfiles_sign_hide);
4432 g_free (panel_history_prev_item_sign);
4433 g_free (panel_history_next_item_sign);
4434 g_free (panel_history_show_list_sign);
4438 /* --------------------------------------------------------------------------------------------- */