Panel update: type accuracy.
[midnight-commander.git] / src / filemanager / panel.c
blob06a4c5c0d40af067f4fba210458f782ef44900ef
1 /* Panel managing.
2 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
3 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
5 This program is free software; you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation; either version 2 of the License, or
8 (at your option) any later version.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 Written by: 1995 Miguel de Icaza
16 1997, 1999 Timur Bakeyev
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 /** \file panel.c
23 * \brief Source: panel managin module
26 #include <config.h>
28 #include <errno.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <unistd.h>
34 #include "lib/global.h"
36 #include "lib/tty/tty.h"
37 #include "lib/tty/mouse.h" /* For Gpm_Event */
38 #include "lib/tty/key.h" /* XCTRL and ALT macros */
39 #include "lib/skin.h"
40 #include "lib/strescape.h"
41 #include "lib/filehighlight.h"
42 #include "lib/mcconfig.h"
43 #include "lib/vfs/mc-vfs/vfs.h"
44 #include "lib/unixcompat.h"
45 #include "lib/timefmt.h"
46 #include "lib/util.h"
47 #include "lib/widget.h"
48 #ifdef HAVE_CHARSET
49 #include "lib/charsets.h" /* get_codepage_id () */
50 #endif
52 #include "src/setup.h" /* For loading/saving panel options */
53 #include "src/execute.h"
54 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
55 #include "src/keybind-defaults.h" /* global_keymap_t */
56 #include "src/subshell.h" /* use_subshell */
58 #include "dir.h"
59 #include "boxes.h"
60 #include "tree.h"
61 #include "ext.h" /* regexp_command */
62 #include "layout.h" /* Most layout variables are here */
63 #include "cmd.h"
64 #include "command.h" /* cmdline */
65 #include "usermenu.h"
66 #include "midnight.h"
67 #include "mountlist.h" /* my_statfs */
69 #include "panel.h"
71 /*** global variables ****************************************************************************/
73 /* If true, show the mini-info on the panel */
74 int show_mini_info = 1;
76 /* If true, use some usability hacks by Torben */
77 int torben_fj_mode = 0;
79 /* The hook list for the select file function */
80 hook_t *select_file_hook = NULL;
84 static const char *string_file_name (file_entry *, int);
85 static const char *string_file_size (file_entry *, int);
86 static const char *string_file_size_brief (file_entry *, int);
87 static const char *string_file_type (file_entry *, int);
88 static const char *string_file_mtime (file_entry *, int);
89 static const char *string_file_atime (file_entry *, int);
90 static const char *string_file_ctime (file_entry *, int);
91 static const char *string_file_permission (file_entry *, int);
92 static const char *string_file_perm_octal (file_entry *, int);
93 static const char *string_file_nlinks (file_entry *, int);
94 static const char *string_inode (file_entry *, int);
95 static const char *string_file_nuid (file_entry *, int);
96 static const char *string_file_ngid (file_entry *, int);
97 static const char *string_file_owner (file_entry *, int);
98 static const char *string_file_group (file_entry *, int);
99 static const char *string_marked (file_entry *, int);
100 static const char *string_space (file_entry *, int);
101 static const char *string_dot (file_entry *, int);
103 /* *INDENT-OFF* */
104 panel_field_t panel_fields[] = {
106 "unsorted", 12, 1, J_LEFT_FIT,
107 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
108 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
109 N_("sort|u"),
110 N_("&Unsorted"), TRUE, FALSE,
111 string_file_name,
112 (sortfn *) unsorted
116 "name", 12, 1, J_LEFT_FIT,
117 /* TRANSLATORS: one single character to represent 'name' sort mode */
118 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
119 N_("sort|n"),
120 N_("&Name"), TRUE, TRUE,
121 string_file_name,
122 (sortfn *) sort_name
126 "version", 12, 1, J_LEFT_FIT,
127 /* TRANSLATORS: one single character to represent 'version' sort mode */
128 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
129 N_("sort|v"),
130 N_("&Version"), TRUE, FALSE,
131 string_file_name,
132 (sortfn *) sort_vers
136 "extension", 12, 1, J_LEFT_FIT,
137 /* TRANSLATORS: one single character to represent 'extension' sort mode */
138 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
139 N_("sort|e"),
140 N_("&Extension"), TRUE, FALSE,
141 string_file_name, /* TODO: string_file_ext */
142 (sortfn *) sort_ext
146 "size", 7, 0, J_RIGHT,
147 /* TRANSLATORS: one single character to represent 'size' sort mode */
148 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
149 N_("sort|s"),
150 N_("&Size"), TRUE, TRUE,
151 string_file_size,
152 (sortfn *) sort_size
156 "bsize", 7, 0, J_RIGHT,
158 N_("Block Size"), FALSE, FALSE,
159 string_file_size_brief,
160 (sortfn *) sort_size
164 "type", 1, 0, J_LEFT,
166 "", FALSE, TRUE,
167 string_file_type,
168 NULL
172 "mtime", 12, 0, J_RIGHT,
173 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
174 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
175 N_("sort|m"),
176 N_("&Modify time"), TRUE, TRUE,
177 string_file_mtime,
178 (sortfn *) sort_time
182 "atime", 12, 0, J_RIGHT,
183 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
184 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
185 N_("sort|a"),
186 N_("&Access time"), TRUE, TRUE,
187 string_file_atime,
188 (sortfn *) sort_atime
192 "ctime", 12, 0, J_RIGHT,
193 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
194 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
195 N_("sort|h"),
196 N_("C&hange time"), TRUE, TRUE,
197 string_file_ctime,
198 (sortfn *) sort_ctime
202 "perm", 10, 0, J_LEFT,
204 N_("Permission"), FALSE, TRUE,
205 string_file_permission,
206 NULL
210 "mode", 6, 0, J_RIGHT,
212 N_("Perm"), FALSE, TRUE,
213 string_file_perm_octal,
214 NULL
218 "nlink", 2, 0, J_RIGHT,
220 N_("Nl"), FALSE, TRUE,
221 string_file_nlinks, NULL
225 "inode", 5, 0, J_RIGHT,
226 /* TRANSLATORS: one single character to represent 'inode' sort mode */
227 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
228 N_("sort|i"),
229 N_("&Inode"), TRUE, TRUE,
230 string_inode,
231 (sortfn *) sort_inode
235 "nuid", 5, 0, J_RIGHT,
237 N_("UID"), FALSE, FALSE,
238 string_file_nuid,
239 NULL
243 "ngid", 5, 0, J_RIGHT,
245 N_("GID"), FALSE, FALSE,
246 string_file_ngid,
247 NULL
251 "owner", 8, 0, J_LEFT_FIT,
253 N_("Owner"), FALSE, TRUE,
254 string_file_owner,
255 NULL
259 "group", 8, 0, J_LEFT_FIT,
261 N_("Group"), FALSE, TRUE,
262 string_file_group,
263 NULL
267 "mark", 1, 0, J_RIGHT,
269 " ", FALSE, TRUE,
270 string_marked,
271 NULL
275 "|", 1, 0, J_RIGHT,
277 " ", FALSE, TRUE,
278 NULL,
279 NULL
283 "space", 1, 0, J_RIGHT,
285 " ", FALSE, TRUE,
286 string_space,
287 NULL
291 "dot", 1, 0, J_RIGHT,
293 " ", FALSE, FALSE,
294 string_dot,
295 NULL
299 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
302 /* *INDENT-ON* */
304 extern int saving_setup;
306 /*** file scope macro definitions ****************************************************************/
308 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
310 #define NORMAL 0
311 #define SELECTED 1
312 #define MARKED 2
313 #define MARKED_SELECTED 3
314 #define STATUS 5
316 /* This macro extracts the number of available lines in a panel */
317 #define llines(p) (p->widget.lines-3 - (show_mini_info ? 2 : 0))
319 /*** file scope type declarations ****************************************************************/
321 typedef enum
323 MARK_DONT_MOVE = 0,
324 MARK_DOWN = 1,
325 MARK_FORCE_DOWN = 2,
326 MARK_FORCE_UP = 3
327 } mark_act_t;
330 * This describes a format item. The parse_display_format routine parses
331 * the user specified format and creates a linked list of format_e structures.
333 typedef struct format_e
335 struct format_e *next;
336 int requested_field_len;
337 int field_len;
338 align_crt_t just_mode;
339 int expand;
340 const char *(*string_fn) (file_entry *, int len);
341 char *title;
342 const char *id;
343 } format_e;
345 /*** file scope variables ************************************************************************/
347 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
348 static int panel_event (Gpm_Event * event, void *);
349 static void paint_frame (WPanel * panel);
350 static const char *panel_format (WPanel * panel);
351 static const char *mini_status_format (WPanel * panel);
353 static char *panel_sort_up_sign = NULL;
354 static char *panel_sort_down_sign = NULL;
356 static char *panel_hiddenfiles_sign_show = NULL;
357 static char *panel_hiddenfiles_sign_hide = NULL;
358 static char *panel_history_prev_item_sign = NULL;
359 static char *panel_history_next_item_sign = NULL;
360 static char *panel_history_show_list_sign = NULL;
362 static int mouse_marking = 0;
364 /*** file scope functions ************************************************************************/
365 /* --------------------------------------------------------------------------------------------- */
367 static void
368 set_colors (WPanel * panel)
370 (void) panel;
371 tty_set_normal_attrs ();
372 tty_setcolor (NORMAL_COLOR);
375 /* --------------------------------------------------------------------------------------------- */
376 /** Delete format string, it is a linked list */
378 static void
379 delete_format (format_e * format)
381 while (format != NULL)
383 format_e *next = format->next;
384 g_free (format->title);
385 g_free (format);
386 format = next;
390 /* --------------------------------------------------------------------------------------------- */
391 /** This code relies on the default justification!!! */
393 static void
394 add_permission_string (char *dest, int width, file_entry * fe, int attr, int color, int is_octal)
396 int i, r, l;
398 l = get_user_permissions (&fe->st);
400 if (is_octal)
402 /* Place of the access bit in octal mode */
403 l = width + l - 3;
404 r = l + 1;
406 else
408 /* The same to the triplet in string mode */
409 l = l * 3 + 1;
410 r = l + 3;
413 for (i = 0; i < width; i++)
415 if (i >= l && i < r)
417 if (attr == SELECTED || attr == MARKED_SELECTED)
418 tty_setcolor (MARKED_SELECTED_COLOR);
419 else
420 tty_setcolor (MARKED_COLOR);
422 else if (color >= 0)
423 tty_setcolor (color);
424 else
425 tty_lowlevel_setcolor (-color);
427 tty_print_char (dest[i]);
431 /* --------------------------------------------------------------------------------------------- */
432 /** String representations of various file attributes name */
434 static const char *
435 string_file_name (file_entry * fe, int len)
437 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
439 (void) len;
440 g_strlcpy (buffer, fe->fname, sizeof (buffer));
441 return buffer;
444 /* --------------------------------------------------------------------------------------------- */
446 static unsigned int
447 ilog10 (dev_t n)
449 unsigned int digits = 0;
452 digits++, n /= 10;
454 while (n != 0);
455 return digits;
458 /* --------------------------------------------------------------------------------------------- */
460 static void
461 format_device_number (char *buf, size_t bufsize, dev_t dev)
463 dev_t major_dev = major (dev);
464 dev_t minor_dev = minor (dev);
465 unsigned int major_digits = ilog10 (major_dev);
466 unsigned int minor_digits = ilog10 (minor_dev);
468 g_assert (bufsize >= 1);
469 if (major_digits + 1 + minor_digits + 1 <= bufsize)
471 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
473 else
475 g_strlcpy (buf, _("[dev]"), bufsize);
479 /* --------------------------------------------------------------------------------------------- */
480 /** size */
482 static const char *
483 string_file_size (file_entry * fe, int len)
485 static char buffer[BUF_TINY];
487 /* Don't ever show size of ".." since we don't calculate it */
488 if (!strcmp (fe->fname, ".."))
490 return _("UP--DIR");
493 #ifdef HAVE_STRUCT_STAT_ST_RDEV
494 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
495 format_device_number (buffer, len + 1, fe->st.st_rdev);
496 else
497 #endif
499 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
501 return buffer;
504 /* --------------------------------------------------------------------------------------------- */
505 /** bsize */
507 static const char *
508 string_file_size_brief (file_entry * fe, int len)
510 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
512 return _("SYMLINK");
515 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
517 return _("SUB-DIR");
520 return string_file_size (fe, len);
523 /* --------------------------------------------------------------------------------------------- */
524 /** This functions return a string representation of a file entry type */
526 static const char *
527 string_file_type (file_entry * fe, int len)
529 static char buffer[2];
531 (void) len;
532 if (S_ISDIR (fe->st.st_mode))
533 buffer[0] = PATH_SEP;
534 else if (S_ISLNK (fe->st.st_mode))
536 if (fe->f.link_to_dir)
537 buffer[0] = '~';
538 else if (fe->f.stale_link)
539 buffer[0] = '!';
540 else
541 buffer[0] = '@';
543 else if (S_ISCHR (fe->st.st_mode))
544 buffer[0] = '-';
545 else if (S_ISSOCK (fe->st.st_mode))
546 buffer[0] = '=';
547 else if (S_ISDOOR (fe->st.st_mode))
548 buffer[0] = '>';
549 else if (S_ISBLK (fe->st.st_mode))
550 buffer[0] = '+';
551 else if (S_ISFIFO (fe->st.st_mode))
552 buffer[0] = '|';
553 else if (S_ISNAM (fe->st.st_mode))
554 buffer[0] = '#';
555 else if (!S_ISREG (fe->st.st_mode))
556 buffer[0] = '?'; /* non-regular of unknown kind */
557 else if (is_exe (fe->st.st_mode))
558 buffer[0] = '*';
559 else
560 buffer[0] = ' ';
561 buffer[1] = '\0';
562 return buffer;
565 /* --------------------------------------------------------------------------------------------- */
566 /** mtime */
568 static const char *
569 string_file_mtime (file_entry * fe, int len)
571 (void) len;
572 return file_date (fe->st.st_mtime);
575 /* --------------------------------------------------------------------------------------------- */
576 /** atime */
578 static const char *
579 string_file_atime (file_entry * fe, int len)
581 (void) len;
582 return file_date (fe->st.st_atime);
585 /* --------------------------------------------------------------------------------------------- */
586 /** ctime */
588 static const char *
589 string_file_ctime (file_entry * fe, int len)
591 (void) len;
592 return file_date (fe->st.st_ctime);
595 /* --------------------------------------------------------------------------------------------- */
596 /** perm */
598 static const char *
599 string_file_permission (file_entry * fe, int len)
601 (void) len;
602 return string_perm (fe->st.st_mode);
605 /* --------------------------------------------------------------------------------------------- */
606 /** mode */
608 static const char *
609 string_file_perm_octal (file_entry * fe, int len)
611 static char buffer[10];
613 (void) len;
614 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
615 return buffer;
618 /* --------------------------------------------------------------------------------------------- */
619 /** nlink */
621 static const char *
622 string_file_nlinks (file_entry * fe, int len)
624 static char buffer[BUF_TINY];
626 (void) len;
627 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
628 return buffer;
631 /* --------------------------------------------------------------------------------------------- */
632 /** inode */
634 static const char *
635 string_inode (file_entry * fe, int len)
637 static char buffer[10];
639 (void) len;
640 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
641 return buffer;
644 /* --------------------------------------------------------------------------------------------- */
645 /** nuid */
647 static const char *
648 string_file_nuid (file_entry * fe, int len)
650 static char buffer[10];
652 (void) len;
653 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
654 return buffer;
657 /* --------------------------------------------------------------------------------------------- */
658 /** ngid */
660 static const char *
661 string_file_ngid (file_entry * fe, int len)
663 static char buffer[10];
665 (void) len;
666 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
667 return buffer;
670 /* --------------------------------------------------------------------------------------------- */
671 /** owner */
673 static const char *
674 string_file_owner (file_entry * fe, int len)
676 (void) len;
677 return get_owner (fe->st.st_uid);
680 /* --------------------------------------------------------------------------------------------- */
681 /** group */
683 static const char *
684 string_file_group (file_entry * fe, int len)
686 (void) len;
687 return get_group (fe->st.st_gid);
690 /* --------------------------------------------------------------------------------------------- */
691 /** mark */
693 static const char *
694 string_marked (file_entry * fe, int len)
696 (void) len;
697 return fe->f.marked ? "*" : " ";
700 /* --------------------------------------------------------------------------------------------- */
701 /** space */
703 static const char *
704 string_space (file_entry * fe, int len)
706 (void) fe;
707 (void) len;
708 return " ";
711 /* --------------------------------------------------------------------------------------------- */
712 /** dot */
714 static const char *
715 string_dot (file_entry * fe, int len)
717 (void) fe;
718 (void) len;
719 return ".";
722 /* --------------------------------------------------------------------------------------------- */
724 static int
725 file_compute_color (int attr, file_entry * fe)
727 switch (attr)
729 case SELECTED:
730 return (SELECTED_COLOR);
731 case MARKED:
732 return (MARKED_COLOR);
733 case MARKED_SELECTED:
734 return (MARKED_SELECTED_COLOR);
735 case STATUS:
736 return (NORMAL_COLOR);
737 case NORMAL:
738 default:
739 if (!panels_options.filetype_mode)
740 return (NORMAL_COLOR);
743 return mc_fhl_get_color (mc_filehighlight, fe);
746 /* --------------------------------------------------------------------------------------------- */
747 /** Formats the file number file_index of panel in the buffer dest */
749 static void
750 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
751 int isstatus)
753 int color, length, empty_line;
754 const char *txt;
755 format_e *format, *home;
756 file_entry *fe;
758 (void) dest;
759 (void) limit;
760 length = 0;
761 empty_line = (file_index >= panel->count);
762 home = (isstatus) ? panel->status_format : panel->format;
763 fe = &panel->dir.list[file_index];
765 if (!empty_line)
766 color = file_compute_color (attr, fe);
767 else
768 color = NORMAL_COLOR;
770 for (format = home; format; format = format->next)
772 if (length == width)
773 break;
775 if (format->string_fn)
777 int len, perm;
778 char *preperad_text;
780 if (empty_line)
781 txt = " ";
782 else
783 txt = (*format->string_fn) (fe, format->field_len);
785 len = format->field_len;
786 if (len + length > width)
787 len = width - length;
788 if (len <= 0)
789 break;
791 perm = 0;
792 if (panels_options.permission_mode)
794 if (!strcmp (format->id, "perm"))
795 perm = 1;
796 else if (!strcmp (format->id, "mode"))
797 perm = 2;
800 if (color >= 0)
801 tty_setcolor (color);
802 else
803 tty_lowlevel_setcolor (-color);
805 preperad_text = (char *) str_fit_to_term (txt, len, format->just_mode);
806 if (perm)
807 add_permission_string (preperad_text, format->field_len, fe, attr, color, perm - 1);
808 else
809 tty_print_string (preperad_text);
811 length += len;
813 else
815 if (attr == SELECTED || attr == MARKED_SELECTED)
816 tty_setcolor (SELECTED_COLOR);
817 else
818 tty_setcolor (NORMAL_COLOR);
819 tty_print_one_vline (TRUE);
820 length++;
824 if (length < width)
825 tty_draw_hline (-1, -1, ' ', width - length);
828 /* --------------------------------------------------------------------------------------------- */
830 static void
831 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
833 int second_column = 0;
834 int width;
835 int offset = 0;
836 char buffer[BUF_MEDIUM];
838 gboolean panel_is_split = !isstatus && panel->split;
840 width = panel->widget.cols - 2;
842 if (panel_is_split)
844 second_column = (file_index - panel->top_file) / llines (panel);
845 width = width / 2 - 1;
847 if (second_column != 0)
849 offset = 1 + width;
850 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
851 width = panel->widget.cols - offset - 2;
855 /* Nothing to paint */
856 if (width <= 0)
857 return;
859 if (mv)
861 if (panel_is_split)
862 widget_move (&panel->widget,
863 (file_index - panel->top_file) % llines (panel) + 2, offset + 1);
864 else
865 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
868 format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus);
870 if (panel_is_split)
872 if (second_column)
873 tty_print_char (' ');
874 else
876 tty_setcolor (NORMAL_COLOR);
877 tty_print_one_vline (TRUE);
882 /* --------------------------------------------------------------------------------------------- */
884 static void
885 display_mini_info (WPanel * panel)
887 if (!show_mini_info)
888 return;
890 widget_move (&panel->widget, llines (panel) + 3, 1);
892 if (panel->searching)
894 tty_setcolor (INPUT_COLOR);
895 tty_print_char ('/');
896 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
897 return;
900 /* Status resolves links and show them */
901 set_colors (panel);
903 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
905 char *lc_link, link_target[MC_MAXPATHLEN];
906 int len;
908 lc_link = concat_dir_and_file (panel->cwd, panel->dir.list[panel->selected].fname);
909 len = mc_readlink (lc_link, link_target, MC_MAXPATHLEN - 1);
910 g_free (lc_link);
911 if (len > 0)
913 link_target[len] = 0;
914 tty_print_string ("-> ");
915 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
917 else
918 tty_print_string (str_fit_to_term (_("<readlink failed>"),
919 panel->widget.cols - 2, J_LEFT));
921 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
923 /* FIXME:
924 * while loading directory (do_load_dir() and do_reload_dir()),
925 * the actual stat info about ".." directory isn't got;
926 * so just don't display incorrect info about ".." directory */
927 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
929 else
930 /* Default behavior */
931 repaint_file (panel, panel->selected, 0, STATUS, 1);
934 /* --------------------------------------------------------------------------------------------- */
936 static void
937 paint_dir (WPanel * panel)
939 int i;
940 int color; /* Color value of the line */
941 int items; /* Number of items */
943 items = llines (panel) * (panel->split ? 2 : 1);
945 for (i = 0; i < items; i++)
947 if (i + panel->top_file >= panel->count)
948 color = 0;
949 else
951 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
952 color += (panel->selected == i + panel->top_file && panel->active);
954 repaint_file (panel, i + panel->top_file, 1, color, 0);
956 tty_set_normal_attrs ();
959 /* --------------------------------------------------------------------------------------------- */
961 static void
962 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
964 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
965 int cols;
967 if (panel->marked <= 0)
968 return;
970 buf = size_only ? b_bytes : buffer;
971 cols = panel->widget.cols - 2;
974 * This is a trick to use two ngettext() calls in one sentence.
975 * First make "N bytes", then insert it into "X in M files".
977 g_snprintf (b_bytes, sizeof (b_bytes),
978 ngettext ("%s byte", "%s bytes", panel->total),
979 size_trunc_sep (panel->total, panels_options.kilobyte_si));
980 if (!size_only)
981 g_snprintf (buffer, sizeof (buffer),
982 ngettext ("%s in %d file", "%s in %d files", panel->marked),
983 b_bytes, panel->marked);
985 /* don't forget spaces around buffer content */
986 buf = (char *) str_trunc (buf, cols - 4);
988 if (x < 0)
989 /* center in panel */
990 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
993 * y == llines (panel) + 2 for mini_info_separator
994 * y == panel->widget.lines - 1 for panel bottom frame
996 widget_move (&panel->widget, y, x);
997 tty_setcolor (MARKED_COLOR);
998 tty_printf (" %s ", buf);
1001 /* --------------------------------------------------------------------------------------------- */
1003 static void
1004 mini_info_separator (WPanel * panel)
1006 if (show_mini_info)
1008 const int y = llines (panel) + 2;
1010 tty_setcolor (NORMAL_COLOR);
1011 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1012 ACS_HLINE, panel->widget.cols - 2);
1013 /* Status displays total marked size.
1014 * Centered in panel, full format. */
1015 display_total_marked_size (panel, y, -1, FALSE);
1019 /* --------------------------------------------------------------------------------------------- */
1021 static void
1022 show_free_space (WPanel * panel)
1024 /* Used to figure out how many free space we have */
1025 static struct my_statfs myfs_stats;
1026 /* Old current working directory for displaying free space */
1027 static char *old_cwd = NULL;
1029 /* Don't try to stat non-local fs */
1030 if (!vfs_file_is_local (panel->cwd) || !free_space)
1031 return;
1033 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0)
1035 char rpath[PATH_MAX];
1037 init_my_statfs ();
1038 g_free (old_cwd);
1039 old_cwd = g_strdup (panel->cwd);
1041 if (mc_realpath (panel->cwd, rpath) == NULL)
1042 return;
1044 my_statfs (&myfs_stats, rpath);
1047 if (myfs_stats.avail > 0 || myfs_stats.total > 0)
1049 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1050 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1051 panels_options.kilobyte_si);
1052 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1053 panels_options.kilobyte_si);
1054 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1055 myfs_stats.total >
1056 0 ? (int) (100 * (double) myfs_stats.avail / myfs_stats.total) : 0);
1057 widget_move (&panel->widget, panel->widget.lines - 1,
1058 panel->widget.cols - 2 - (int) strlen (tmp));
1059 tty_setcolor (NORMAL_COLOR);
1060 tty_print_string (tmp);
1064 /* --------------------------------------------------------------------------------------------- */
1066 static void
1067 show_dir (WPanel * panel)
1069 gchar *tmp;
1070 set_colors (panel);
1071 draw_box (panel->widget.owner,
1072 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1074 if (show_mini_info)
1076 widget_move (&panel->widget, llines (panel) + 2, 0);
1077 tty_print_alt_char (ACS_LTEE, FALSE);
1078 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1079 tty_print_alt_char (ACS_RTEE, FALSE);
1082 widget_move (&panel->widget, 0, 1);
1083 tty_print_string (panel_history_prev_item_sign);
1085 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1086 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1087 panel_history_next_item_sign);
1089 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1090 tty_print_string (tmp);
1092 g_free (tmp);
1094 if (panel->active)
1095 tty_setcolor (REVERSE_COLOR);
1097 widget_move (&panel->widget, 0, 3);
1099 tty_printf (" %s ",
1100 str_term_trim (strip_home_and_password (panel->cwd),
1101 min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1103 if (!show_mini_info)
1105 if (panel->marked == 0)
1107 /* Show size of curret file in the bottom of panel */
1108 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1110 char buffer[BUF_SMALL];
1112 g_snprintf (buffer, sizeof (buffer), " %s ",
1113 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1114 panels_options.kilobyte_si));
1115 tty_setcolor (NORMAL_COLOR);
1116 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1117 tty_print_string (buffer);
1120 else
1122 /* Show total size of marked files
1123 * In the bottom of panel, display size only. */
1124 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1128 show_free_space (panel);
1130 if (panel->active)
1131 tty_set_normal_attrs ();
1134 /* --------------------------------------------------------------------------------------------- */
1135 /** To be used only by long_frame and full_frame to adjust top_file */
1137 static void
1138 adjust_top_file (WPanel * panel)
1140 int old_top = panel->top_file;
1142 if (panel->selected - old_top > llines (panel))
1143 panel->top_file = panel->selected;
1144 if (old_top - panel->count > llines (panel))
1145 panel->top_file = panel->count - llines (panel);
1148 /* --------------------------------------------------------------------------------------------- */
1149 /** Repaint everything, including frame and separator */
1151 static void
1152 paint_panel (WPanel * panel)
1154 paint_frame (panel); /* including show_dir */
1155 paint_dir (panel);
1156 mini_info_separator (panel);
1157 display_mini_info (panel);
1158 panel->dirty = 0;
1161 /* --------------------------------------------------------------------------------------------- */
1162 /** add "#enc:encodning" to end of path */
1163 /* if path end width a previous #enc:, only encoding is changed no additional
1164 * #enc: is appended
1165 * retun new string
1168 static char *
1169 add_encoding_to_path (const char *path, const char *encoding)
1171 char *result;
1172 char *semi;
1173 char *slash;
1175 semi = g_strrstr (path, VFS_ENCODING_PREFIX);
1177 if (semi != NULL)
1179 slash = strchr (semi, PATH_SEP);
1180 if (slash != NULL)
1182 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1184 else
1186 *semi = '\0';
1187 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1188 *semi = '#';
1191 else
1193 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1196 return result;
1199 /* --------------------------------------------------------------------------------------------- */
1201 static char *
1202 panel_save_name (WPanel * panel)
1204 /* If the program is shuting down */
1205 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1206 return g_strdup (panel->panel_name);
1207 else
1208 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1211 /* --------------------------------------------------------------------------------------------- */
1213 static void
1214 panel_destroy (WPanel * p)
1216 size_t i;
1218 if (panels_options.auto_save_setup)
1220 char *name;
1222 name = panel_save_name (p);
1223 panel_save_setup (p, name);
1224 g_free (name);
1227 panel_clean_dir (p);
1229 /* save and clean history */
1230 if (p->dir_history != NULL)
1232 history_put (p->hist_name, p->dir_history);
1234 p->dir_history = g_list_first (p->dir_history);
1235 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1236 g_list_free (p->dir_history);
1238 g_free (p->hist_name);
1240 delete_format (p->format);
1241 delete_format (p->status_format);
1243 g_free (p->user_format);
1244 for (i = 0; i < LIST_TYPES; i++)
1245 g_free (p->user_status_format[i]);
1246 g_free (p->dir.list);
1247 g_free (p->panel_name);
1250 /* --------------------------------------------------------------------------------------------- */
1252 static void
1253 panel_format_modified (WPanel * panel)
1255 panel->format_modified = 1;
1258 /* --------------------------------------------------------------------------------------------- */
1260 static void
1261 panel_paint_sort_info (WPanel * panel)
1263 const char *sort_sign = (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign;
1264 char *str;
1266 if (*panel->current_sort_field->hotkey == '\0')
1267 return;
1269 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->current_sort_field->hotkey));
1271 widget_move (&panel->widget, 1, 1);
1272 tty_print_string (str);
1273 g_free (str);
1276 /* --------------------------------------------------------------------------------------------- */
1278 static gchar *
1279 panel_get_title_without_hotkey (const char *title)
1281 char *translated_title;
1282 char *hkey;
1284 if (title == NULL)
1285 return NULL;
1286 if (title[0] == '\0')
1287 return g_strdup ("");
1289 translated_title = g_strdup (_(title));
1291 hkey = strchr (translated_title, '&');
1292 if ((hkey != NULL) && (hkey[1] != '\0'))
1293 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1295 return translated_title;
1298 /* --------------------------------------------------------------------------------------------- */
1300 static void
1301 paint_frame (WPanel * panel)
1303 int side, width;
1304 GString *format_txt;
1306 if (!panel->split)
1307 adjust_top_file (panel);
1309 widget_erase (&panel->widget);
1310 show_dir (panel);
1312 widget_move (&panel->widget, 1, 1);
1314 for (side = 0; side <= panel->split; side++)
1316 format_e *format;
1318 if (side)
1320 tty_setcolor (NORMAL_COLOR);
1321 tty_print_one_vline (TRUE);
1322 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1324 else if (panel->split)
1325 width = panel->widget.cols / 2 - 3;
1326 else
1327 width = panel->widget.cols - 2;
1329 format_txt = g_string_new ("");
1330 for (format = panel->format; format; format = format->next)
1332 if (format->string_fn)
1334 g_string_set_size (format_txt, 0);
1336 if (panel->list_type == list_long
1337 && strcmp (format->id, panel->current_sort_field->id) == 0)
1338 g_string_append (format_txt,
1339 (panel->reverse) ? panel_sort_down_sign : panel_sort_up_sign);
1341 g_string_append (format_txt, format->title);
1342 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1344 g_string_append (format_txt, " [");
1345 g_string_append (format_txt, panel->filter);
1346 g_string_append (format_txt, "]");
1349 tty_setcolor (HEADER_COLOR);
1350 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1351 J_CENTER_LEFT));
1352 width -= format->field_len;
1354 else
1356 tty_setcolor (NORMAL_COLOR);
1357 tty_print_one_vline (TRUE);
1358 width--;
1361 g_string_free (format_txt, TRUE);
1363 if (width > 0)
1364 tty_draw_hline (-1, -1, ' ', width);
1367 if (panel->list_type != list_long)
1368 panel_paint_sort_info (panel);
1371 /* --------------------------------------------------------------------------------------------- */
1373 static const char *
1374 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1376 int frame = frame_half;
1377 format = skip_separators (format);
1379 if (!strncmp (format, "full", 4))
1381 frame = frame_full;
1382 format += 4;
1384 else if (!strncmp (format, "half", 4))
1386 frame = frame_half;
1387 format += 4;
1390 if (!isstatus)
1392 panel->frame_size = frame;
1393 panel->split = 0;
1396 /* Now, the optional column specifier */
1397 format = skip_separators (format);
1399 if (*format == '1' || *format == '2')
1401 if (!isstatus)
1402 panel->split = *format == '2';
1403 format++;
1406 if (!isstatus)
1407 panel_update_cols (&(panel->widget), panel->frame_size);
1409 return skip_separators (format);
1412 /* Format is:
1414 all := panel_format? format
1415 panel_format := [full|half] [1|2]
1416 format := one_format_e
1417 | format , one_format_e
1419 one_format_e := just format.id [opt_size]
1420 just := [<=>]
1421 opt_size := : size [opt_expand]
1422 size := [0-9]+
1423 opt_expand := +
1427 /* --------------------------------------------------------------------------------------------- */
1429 static format_e *
1430 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1431 int *res_total_cols)
1433 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1434 int total_cols = 0; /* Used columns by the format */
1435 int set_justify; /* flag: set justification mode? */
1436 align_crt_t justify = J_LEFT; /* Which mode. */
1437 size_t i;
1439 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1441 *error = 0;
1443 if (i18n_timelength == 0)
1445 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1447 for (i = 0; panel_fields[i].id != NULL; i++)
1448 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1449 panel_fields[i].min_size = i18n_timelength;
1453 * This makes sure that the panel and mini status full/half mode
1454 * setting is equal
1456 format = parse_panel_size (panel, format, isstatus);
1458 while (*format)
1459 { /* format can be an empty string */
1460 int found = 0;
1462 darr = g_new0 (format_e, 1);
1464 /* I'm so ugly, don't look at me :-) */
1465 if (!home)
1466 home = old = darr;
1468 old->next = darr;
1469 darr->next = 0;
1470 old = darr;
1472 format = skip_separators (format);
1474 if (strchr ("<=>", *format))
1476 set_justify = 1;
1477 switch (*format)
1479 case '<':
1480 justify = J_LEFT;
1481 break;
1482 case '=':
1483 justify = J_CENTER;
1484 break;
1485 case '>':
1486 default:
1487 justify = J_RIGHT;
1488 break;
1490 format = skip_separators (format + 1);
1492 else
1493 set_justify = 0;
1495 for (i = 0; panel_fields[i].id != NULL; i++)
1497 size_t klen = strlen (panel_fields[i].id);
1499 if (strncmp (format, panel_fields[i].id, klen) != 0)
1500 continue;
1502 format += klen;
1504 darr->requested_field_len = panel_fields[i].min_size;
1505 darr->string_fn = panel_fields[i].string_fn;
1506 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1508 darr->id = panel_fields[i].id;
1509 darr->expand = panel_fields[i].expands;
1510 darr->just_mode = panel_fields[i].default_just;
1512 if (set_justify)
1514 if (IS_FIT (darr->just_mode))
1515 darr->just_mode = MAKE_FIT (justify);
1516 else
1517 darr->just_mode = justify;
1519 found = 1;
1521 format = skip_separators (format);
1523 /* If we have a size specifier */
1524 if (*format == ':')
1526 int req_length;
1528 /* If the size was specified, we don't want
1529 * auto-expansion by default
1531 darr->expand = 0;
1532 format++;
1533 req_length = atoi (format);
1534 darr->requested_field_len = req_length;
1536 format = skip_numbers (format);
1538 /* Now, if they insist on expansion */
1539 if (*format == '+')
1541 darr->expand = 1;
1542 format++;
1547 break;
1549 if (!found)
1551 char *tmp_format = g_strdup (format);
1553 int pos = min (8, strlen (format));
1554 delete_format (home);
1555 tmp_format[pos] = 0;
1556 *error =
1557 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1558 g_free (tmp_format);
1559 return 0;
1561 total_cols += darr->requested_field_len;
1564 *res_total_cols = total_cols;
1565 return home;
1568 /* --------------------------------------------------------------------------------------------- */
1570 static format_e *
1571 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1573 #define MAX_EXPAND 4
1574 int expand_top = 0; /* Max used element in expand */
1575 int usable_columns; /* Usable columns in the panel */
1576 int total_cols = 0;
1577 int i;
1578 format_e *darr, *home;
1580 if (!format)
1581 format = DEFAULT_USER_FORMAT;
1583 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1585 if (*error)
1586 return 0;
1588 panel->dirty = 1;
1590 /* Status needn't to be split */
1591 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1593 : (panel->split + 1))) - (!isstatus
1594 && panel->split);
1596 /* Look for the expandable fields and set field_len based on the requested field len */
1597 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1599 darr->field_len = darr->requested_field_len;
1600 if (darr->expand)
1601 expand_top++;
1604 /* If we used more columns than the available columns, adjust that */
1605 if (total_cols > usable_columns)
1607 int pdif, dif = total_cols - usable_columns;
1609 while (dif)
1611 pdif = dif;
1612 for (darr = home; darr; darr = darr->next)
1614 if (dif && darr->field_len - 1)
1616 darr->field_len--;
1617 dif--;
1621 /* avoid endless loop if num fields > 40 */
1622 if (pdif == dif)
1623 break;
1625 total_cols = usable_columns; /* give up, the rest should be truncated */
1628 /* Expand the available space */
1629 if ((usable_columns > total_cols) && expand_top)
1631 int spaces = (usable_columns - total_cols) / expand_top;
1632 int extra = (usable_columns - total_cols) % expand_top;
1634 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1635 if (darr->expand)
1637 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1638 i++;
1641 return home;
1644 /* --------------------------------------------------------------------------------------------- */
1645 /** Given the panel->view_type returns the format string to be parsed */
1647 static const char *
1648 panel_format (WPanel * panel)
1650 switch (panel->list_type)
1653 case list_long:
1654 return "full perm space nlink space owner space group space size space mtime space name";
1656 case list_brief:
1657 return "half 2 type name";
1659 case list_user:
1660 return panel->user_format;
1662 default:
1663 case list_full:
1664 return "half type name | size | mtime";
1668 /* --------------------------------------------------------------------------------------------- */
1670 static const char *
1671 mini_status_format (WPanel * panel)
1673 if (panel->user_mini_status)
1674 return panel->user_status_format[panel->list_type];
1676 switch (panel->list_type)
1679 case list_long:
1680 return "full perm space nlink space owner space group space size space mtime space name";
1682 case list_brief:
1683 return "half type name space bsize space perm space";
1685 case list_full:
1686 return "half type name";
1688 default:
1689 case list_user:
1690 return panel->user_format;
1694 /* */
1695 /* Panel operation commands */
1696 /* */
1698 /* --------------------------------------------------------------------------------------------- */
1699 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1701 static cb_ret_t
1702 maybe_cd (int move_up_dir)
1704 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1706 if (move_up_dir)
1708 do_cd ("..", cd_exact);
1709 return MSG_HANDLED;
1712 if (S_ISDIR (selection (current_panel)->st.st_mode)
1713 || link_isdir (selection (current_panel)))
1715 do_cd (selection (current_panel)->fname, cd_exact);
1716 return MSG_HANDLED;
1719 return MSG_NOT_HANDLED;
1722 /* --------------------------------------------------------------------------------------------- */
1724 /* if command line is empty then do 'cd ..' */
1725 static cb_ret_t
1726 force_maybe_cd (void)
1728 if (cmdline->buffer[0] == '\0')
1730 do_cd ("..", cd_exact);
1731 return MSG_HANDLED;
1733 return MSG_NOT_HANDLED;
1736 /* --------------------------------------------------------------------------------------------- */
1738 /* Returns the number of items in the given panel */
1739 static int
1740 ITEMS (WPanel * p)
1742 if (p->split)
1743 return llines (p) * 2;
1744 else
1745 return llines (p);
1748 /* --------------------------------------------------------------------------------------------- */
1750 static void
1751 unselect_item (WPanel * panel)
1753 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1756 /* --------------------------------------------------------------------------------------------- */
1758 static void
1759 move_down (WPanel * panel)
1761 if (panel->selected + 1 == panel->count)
1762 return;
1764 unselect_item (panel);
1765 panel->selected++;
1766 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1768 /* Scroll window half screen */
1769 panel->top_file += ITEMS (panel) / 2;
1770 if (panel->top_file > panel->count - ITEMS (panel))
1771 panel->top_file = panel->count - ITEMS (panel);
1772 paint_dir (panel);
1774 select_item (panel);
1777 /* --------------------------------------------------------------------------------------------- */
1779 static void
1780 move_up (WPanel * panel)
1782 if (panel->selected == 0)
1783 return;
1785 unselect_item (panel);
1786 panel->selected--;
1787 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1789 /* Scroll window half screen */
1790 panel->top_file -= ITEMS (panel) / 2;
1791 if (panel->top_file < 0)
1792 panel->top_file = 0;
1793 paint_dir (panel);
1795 select_item (panel);
1798 /* --------------------------------------------------------------------------------------------- */
1799 /** Changes the selection by lines (may be negative) */
1801 static void
1802 move_selection (WPanel * panel, int lines)
1804 int new_pos;
1805 int adjust = 0;
1807 new_pos = panel->selected + lines;
1808 if (new_pos >= panel->count)
1809 new_pos = panel->count - 1;
1811 if (new_pos < 0)
1812 new_pos = 0;
1814 unselect_item (panel);
1815 panel->selected = new_pos;
1817 if (panel->selected - panel->top_file >= ITEMS (panel))
1819 panel->top_file += lines;
1820 adjust = 1;
1823 if (panel->selected - panel->top_file < 0)
1825 panel->top_file += lines;
1826 adjust = 1;
1829 if (adjust)
1831 if (panel->top_file > panel->selected)
1832 panel->top_file = panel->selected;
1833 if (panel->top_file < 0)
1834 panel->top_file = 0;
1835 paint_dir (panel);
1837 select_item (panel);
1840 /* --------------------------------------------------------------------------------------------- */
1842 static cb_ret_t
1843 move_left (WPanel * panel)
1845 if (panel->split)
1847 move_selection (panel, -llines (panel));
1848 return MSG_HANDLED;
1850 else
1851 return maybe_cd (1); /* cd .. */
1854 /* --------------------------------------------------------------------------------------------- */
1856 static int
1857 move_right (WPanel * panel)
1859 if (panel->split)
1861 move_selection (panel, llines (panel));
1862 return MSG_HANDLED;
1864 else
1865 return maybe_cd (0); /* cd (selection) */
1868 /* --------------------------------------------------------------------------------------------- */
1870 static void
1871 prev_page (WPanel * panel)
1873 int items;
1875 if (!panel->selected && !panel->top_file)
1876 return;
1877 unselect_item (panel);
1878 items = ITEMS (panel);
1879 if (panel->top_file < items)
1880 items = panel->top_file;
1881 if (!items)
1882 panel->selected = 0;
1883 else
1884 panel->selected -= items;
1885 panel->top_file -= items;
1887 select_item (panel);
1888 paint_dir (panel);
1891 /* --------------------------------------------------------------------------------------------- */
1893 static void
1894 goto_parent_dir (WPanel * panel)
1896 (void) panel;
1897 do_cd ("..", cd_exact);
1900 /* --------------------------------------------------------------------------------------------- */
1902 static void
1903 next_page (WPanel * panel)
1905 int items;
1907 if (panel->selected == panel->count - 1)
1908 return;
1909 unselect_item (panel);
1910 items = ITEMS (panel);
1911 if (panel->top_file > panel->count - 2 * items)
1912 items = panel->count - items - panel->top_file;
1913 if (panel->top_file + items < 0)
1914 items = -panel->top_file;
1915 if (!items)
1916 panel->selected = panel->count - 1;
1917 else
1918 panel->selected += items;
1919 panel->top_file += items;
1921 select_item (panel);
1922 paint_dir (panel);
1925 /* --------------------------------------------------------------------------------------------- */
1927 static void
1928 goto_child_dir (WPanel * panel)
1930 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
1932 do_cd (selection (panel)->fname, cd_exact);
1936 /* --------------------------------------------------------------------------------------------- */
1938 static void
1939 goto_top_file (WPanel * panel)
1941 unselect_item (panel);
1942 panel->selected = panel->top_file;
1943 select_item (panel);
1946 /* --------------------------------------------------------------------------------------------- */
1948 static void
1949 goto_middle_file (WPanel * panel)
1951 unselect_item (panel);
1952 panel->selected = panel->top_file + (ITEMS (panel) / 2);
1953 select_item (panel);
1956 /* --------------------------------------------------------------------------------------------- */
1958 static void
1959 goto_bottom_file (WPanel * panel)
1961 unselect_item (panel);
1962 panel->selected = panel->top_file + ITEMS (panel) - 1;
1963 select_item (panel);
1966 /* --------------------------------------------------------------------------------------------- */
1968 static void
1969 move_home (WPanel * panel)
1971 if (panel->selected == 0)
1972 return;
1973 unselect_item (panel);
1975 if (torben_fj_mode)
1977 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
1979 if (panel->selected > middle_pos)
1981 goto_middle_file (panel);
1982 return;
1984 if (panel->selected != panel->top_file)
1986 goto_top_file (panel);
1987 return;
1991 panel->top_file = 0;
1992 panel->selected = 0;
1994 paint_dir (panel);
1995 select_item (panel);
1998 /* --------------------------------------------------------------------------------------------- */
2000 static void
2001 move_end (WPanel * panel)
2003 if (panel->selected == panel->count - 1)
2004 return;
2005 unselect_item (panel);
2006 if (torben_fj_mode)
2008 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2010 if (panel->selected < middle_pos)
2012 goto_middle_file (panel);
2013 return;
2015 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2017 goto_bottom_file (panel);
2018 return;
2022 panel->selected = panel->count - 1;
2023 paint_dir (panel);
2024 select_item (panel);
2027 /* --------------------------------------------------------------------------------------------- */
2029 static void
2030 do_mark_file (WPanel * panel, mark_act_t do_move)
2032 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2033 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2034 move_down (panel);
2035 else if (do_move == MARK_FORCE_UP)
2036 move_up (panel);
2039 /* --------------------------------------------------------------------------------------------- */
2041 static void
2042 mark_file (WPanel * panel)
2044 do_mark_file (panel, MARK_DOWN);
2047 /* --------------------------------------------------------------------------------------------- */
2049 static void
2050 mark_file_up (WPanel * panel)
2052 do_mark_file (panel, MARK_FORCE_UP);
2055 /* --------------------------------------------------------------------------------------------- */
2057 static void
2058 mark_file_down (WPanel * panel)
2060 do_mark_file (panel, MARK_FORCE_DOWN);
2063 /* --------------------------------------------------------------------------------------------- */
2064 /** Incremental search of a file name in the panel.
2065 * @param panel instance of WPanel structure
2066 * @param c_code key code
2069 static void
2070 do_search (WPanel * panel, int c_code)
2072 size_t l;
2073 int i, sel;
2074 gboolean wrapped = FALSE;
2075 char *act;
2076 mc_search_t *search;
2077 char *reg_exp, *esc_str;
2078 gboolean is_found = FALSE;
2080 l = strlen (panel->search_buffer);
2081 if (c_code == KEY_BACKSPACE)
2083 if (l != 0)
2085 act = panel->search_buffer + l;
2086 str_prev_noncomb_char (&act, panel->search_buffer);
2087 act[0] = '\0';
2089 panel->search_chpoint = 0;
2091 else
2093 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2095 panel->search_char[panel->search_chpoint] = c_code;
2096 panel->search_chpoint++;
2099 if (panel->search_chpoint > 0)
2101 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2103 case -2:
2104 return;
2105 case -1:
2106 panel->search_chpoint = 0;
2107 return;
2108 default:
2109 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2111 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2112 l += panel->search_chpoint;
2113 *(panel->search_buffer + l) = '\0';
2114 panel->search_chpoint = 0;
2120 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2121 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2122 search = mc_search_new (esc_str, -1);
2123 search->search_type = MC_SEARCH_T_GLOB;
2124 search->is_entire_line = TRUE;
2125 switch (panels_options.qsearch_mode)
2127 case QSEARCH_CASE_SENSITIVE:
2128 search->is_case_sensitive = TRUE;
2129 break;
2130 case QSEARCH_CASE_INSENSITIVE:
2131 search->is_case_sensitive = FALSE;
2132 break;
2133 default:
2134 search->is_case_sensitive = panel->case_sensitive;
2135 break;
2137 sel = panel->selected;
2138 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2140 if (i >= panel->count)
2142 i = 0;
2143 if (wrapped)
2144 break;
2145 wrapped = TRUE;
2147 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2149 sel = i;
2150 is_found = TRUE;
2151 break;
2154 if (is_found)
2156 unselect_item (panel);
2157 panel->selected = sel;
2158 select_item (panel);
2159 paint_panel (panel);
2161 else if (c_code != KEY_BACKSPACE)
2163 act = panel->search_buffer + l;
2164 str_prev_noncomb_char (&act, panel->search_buffer);
2165 act[0] = '\0';
2167 mc_search_free (search);
2168 g_free (reg_exp);
2169 g_free (esc_str);
2172 /* --------------------------------------------------------------------------------------------- */
2173 /** Start new search.
2174 * @param panel instance of WPanel structure
2177 static void
2178 start_search (WPanel * panel)
2180 if (panel->searching)
2182 if (panel->selected + 1 == panel->count)
2183 panel->selected = 0;
2184 else
2185 move_down (panel);
2187 /* in case if there was no search string we need to recall
2188 previous string, with which we ended previous searching */
2189 if (panel->search_buffer[0] == '\0')
2190 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2191 sizeof (panel->search_buffer));
2193 do_search (panel, 0);
2195 else
2197 panel->searching = TRUE;
2198 panel->search_buffer[0] = '\0';
2199 panel->search_char[0] = '\0';
2200 panel->search_chpoint = 0;
2201 display_mini_info (panel);
2202 mc_refresh ();
2206 /* --------------------------------------------------------------------------------------------- */
2208 static void
2209 stop_search (WPanel * panel)
2211 panel->searching = FALSE;
2213 /* if user had overrdied search string, we need to store it
2214 to the previous_search_buffer */
2215 if (panel->search_buffer[0] != '\0')
2216 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2217 sizeof (panel->prev_search_buffer));
2219 display_mini_info (panel);
2222 /* --------------------------------------------------------------------------------------------- */
2223 /** Return 1 if the Enter key has been processed, 0 otherwise */
2225 static int
2226 do_enter_on_file_entry (file_entry * fe)
2228 char *full_name;
2231 * Directory or link to directory - change directory.
2232 * Try the same for the entries on which mc_lstat() has failed.
2234 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2236 if (!do_cd (fe->fname, cd_exact))
2237 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2238 return 1;
2241 /* Try associated command */
2242 if (regex_command (fe->fname, "Open", NULL) != 0)
2243 return 1;
2245 /* Check if the file is executable */
2246 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2247 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe))
2249 g_free (full_name);
2250 return 0;
2252 g_free (full_name);
2254 if (confirm_execute)
2256 if (query_dialog
2257 (_("The Midnight Commander"),
2258 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2259 return 1;
2262 if (!vfs_current_is_local ())
2264 char *tmp;
2265 int ret;
2267 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2268 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2269 g_free (tmp);
2270 /* We took action only if the dialog was shown or the execution
2271 * was successful */
2272 return confirm_execute || (ret == 0);
2276 char *tmp = name_quote (fe->fname, 0);
2277 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2278 g_free (tmp);
2279 shell_execute (cmd, 0);
2280 g_free (cmd);
2283 #if HAVE_CHARSET
2284 source_codepage = default_source_codepage;
2285 #endif
2287 return 1;
2290 /* --------------------------------------------------------------------------------------------- */
2292 static int
2293 do_enter (WPanel * panel)
2295 return do_enter_on_file_entry (selection (panel));
2298 /* --------------------------------------------------------------------------------------------- */
2300 static void
2301 chdir_other_panel (WPanel * panel)
2303 char *new_dir;
2304 char *sel_entry = NULL;
2306 if (get_other_type () != view_listing)
2308 set_display_type (get_other_index (), view_listing);
2311 if (!S_ISDIR (panel->dir.list[panel->selected].st.st_mode))
2313 new_dir = concat_dir_and_file (panel->cwd, "..");
2314 sel_entry = strrchr (panel->cwd, PATH_SEP);
2316 else
2317 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list[panel->selected].fname);
2319 change_panel ();
2320 do_cd (new_dir, cd_exact);
2321 if (sel_entry)
2322 try_to_select (current_panel, sel_entry);
2323 change_panel ();
2325 move_down (panel);
2327 g_free (new_dir);
2330 /* --------------------------------------------------------------------------------------------- */
2332 * Make the current directory of the current panel also the current
2333 * directory of the other panel. Put the other panel to the listing
2334 * mode if needed. If the current panel is panelized, the other panel
2335 * doesn't become panelized.
2338 static void
2339 sync_other_panel (WPanel * panel)
2341 if (get_other_type () != view_listing)
2343 set_display_type (get_other_index (), view_listing);
2346 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2348 /* try to select current filename on the other panel */
2349 if (!panel->is_panelized)
2351 try_to_select (other_panel, selection (panel)->fname);
2355 /* --------------------------------------------------------------------------------------------- */
2357 static void
2358 chdir_to_readlink (WPanel * panel)
2360 char *new_dir;
2362 if (get_other_type () != view_listing)
2363 return;
2365 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2367 char buffer[MC_MAXPATHLEN], *p;
2368 int i;
2369 struct stat st;
2371 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2372 if (i < 0)
2373 return;
2374 if (mc_stat (selection (panel)->fname, &st) < 0)
2375 return;
2376 buffer[i] = 0;
2377 if (!S_ISDIR (st.st_mode))
2379 p = strrchr (buffer, PATH_SEP);
2380 if (p && !p[1])
2382 *p = 0;
2383 p = strrchr (buffer, PATH_SEP);
2385 if (!p)
2386 return;
2387 p[1] = 0;
2389 if (*buffer == PATH_SEP)
2390 new_dir = g_strdup (buffer);
2391 else
2392 new_dir = concat_dir_and_file (panel->cwd, buffer);
2394 change_panel ();
2395 do_cd (new_dir, cd_exact);
2396 change_panel ();
2398 move_down (panel);
2400 g_free (new_dir);
2404 /* --------------------------------------------------------------------------------------------- */
2406 static gsize
2407 panel_get_format_field_count (WPanel * panel)
2409 format_e *format;
2410 gsize lc_index;
2411 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2412 return lc_index;
2415 /* --------------------------------------------------------------------------------------------- */
2417 function return 0 if not found and REAL_INDEX+1 if found
2420 static gsize
2421 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2423 format_e *format;
2424 gsize lc_index;
2426 for (lc_index = 1, format = panel->format;
2427 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2428 if (format == NULL)
2429 lc_index = 0;
2431 return lc_index;
2434 /* --------------------------------------------------------------------------------------------- */
2436 static format_e *
2437 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2439 format_e *format;
2440 for (format = panel->format;
2441 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2442 return format;
2445 /* --------------------------------------------------------------------------------------------- */
2447 static const panel_field_t *
2448 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2450 const panel_field_t *pfield;
2451 format_e *format;
2453 format = panel_get_format_field_by_index (panel, lc_index);
2454 if (format == NULL)
2455 return NULL;
2456 pfield = panel_get_field_by_title (format->title);
2457 if (pfield == NULL)
2458 return NULL;
2459 if (pfield->sort_routine == NULL)
2460 return NULL;
2461 return pfield;
2464 /* --------------------------------------------------------------------------------------------- */
2466 static void
2467 panel_toggle_sort_order_prev (WPanel * panel)
2469 gsize lc_index, i;
2470 gchar *title;
2472 const panel_field_t *pfield = NULL;
2474 title = panel_get_title_without_hotkey (panel->current_sort_field->title_hotkey);
2475 lc_index = panel_get_format_field_index_by_name (panel, title);
2476 g_free (title);
2478 if (lc_index > 1)
2480 /* search for prev sortable column in panel format */
2481 for (i = lc_index - 1;
2482 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2485 if (pfield == NULL)
2487 /* Sortable field not found. Try to search in each array */
2488 for (i = panel_get_format_field_count (panel);
2489 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2491 if (pfield == NULL)
2492 return;
2493 panel->current_sort_field = pfield;
2494 panel_set_sort_order (panel, panel->current_sort_field);
2497 /* --------------------------------------------------------------------------------------------- */
2499 static void
2500 panel_toggle_sort_order_next (WPanel * panel)
2502 gsize lc_index, i;
2503 const panel_field_t *pfield = NULL;
2504 gsize format_field_count = panel_get_format_field_count (panel);
2505 gchar *title;
2507 title = panel_get_title_without_hotkey (panel->current_sort_field->title_hotkey);
2508 lc_index = panel_get_format_field_index_by_name (panel, title);
2509 g_free (title);
2511 if (lc_index != 0 && lc_index != format_field_count)
2513 /* search for prev sortable column in panel format */
2514 for (i = lc_index;
2515 i != format_field_count
2516 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2519 if (pfield == NULL)
2521 /* Sortable field not found. Try to search in each array */
2522 for (i = 0;
2523 i != format_field_count
2524 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2526 if (pfield == NULL)
2527 return;
2528 panel->current_sort_field = pfield;
2529 panel_set_sort_order (panel, panel->current_sort_field);
2532 /* --------------------------------------------------------------------------------------------- */
2534 static void
2535 panel_select_sort_order (WPanel * panel)
2537 const panel_field_t *sort_order;
2538 sort_order = sort_box (panel->current_sort_field, &panel->reverse,
2539 &panel->case_sensitive, &panel->exec_first);
2540 if (sort_order == NULL)
2541 return;
2542 panel->current_sort_field = sort_order;
2543 panel_set_sort_order (panel, panel->current_sort_field);
2547 /* --------------------------------------------------------------------------------------------- */
2549 static void
2550 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2552 const panel_field_t *sort_order;
2554 if (strcmp (panel->current_sort_field->id, name) != 0)
2556 sort_order = panel_get_field_by_id (name);
2557 if (sort_order == NULL)
2558 return;
2559 panel->current_sort_field = sort_order;
2561 else
2563 panel->reverse = !panel->reverse;
2565 panel_set_sort_order (panel, panel->current_sort_field);
2568 /* --------------------------------------------------------------------------------------------- */
2570 * If we moved to the parent directory move the selection pointer to
2571 * the old directory name; If we leave VFS dir, remove FS specificator.
2573 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2576 static const char *
2577 get_parent_dir_name (const char *cwd, const char *lwd)
2579 size_t llen, clen;
2581 llen = strlen (lwd);
2582 clen = strlen (cwd);
2584 if (llen > clen)
2586 const char *p;
2588 p = strrchr (lwd, PATH_SEP);
2590 if ((p != NULL)
2591 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2592 && (clen == (size_t) (p - lwd)
2593 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2594 return (p + 1);
2597 return NULL;
2600 /* --------------------------------------------------------------------------------------------- */
2601 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2603 static void
2604 subshell_chdir (const char *directory)
2606 #ifdef HAVE_SUBSHELL_SUPPORT
2607 if (use_subshell && vfs_current_is_local ())
2608 do_subshell_chdir (directory, FALSE, TRUE);
2609 #endif /* HAVE_SUBSHELL_SUPPORT */
2612 /* --------------------------------------------------------------------------------------------- */
2614 * Changes the current directory of the panel.
2615 * Don't record change in the directory history.
2618 static gboolean
2619 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
2621 const char *directory;
2622 char *olddir;
2623 char temp[MC_MAXPATHLEN];
2624 char *translated_url;
2626 if (cd_type == cd_parse_command)
2628 while (*new_dir == ' ')
2629 new_dir++;
2632 olddir = g_strdup (panel->cwd);
2633 new_dir = translated_url = vfs_translate_url (new_dir);
2635 /* Convert *new_path to a suitable pathname, handle ~user */
2637 if (cd_type == cd_parse_command)
2639 if (!strcmp (new_dir, "-"))
2641 strcpy (temp, panel->lwd);
2642 new_dir = temp;
2645 directory = *new_dir ? new_dir : home_dir;
2647 if (mc_chdir (directory) == -1)
2649 strcpy (panel->cwd, olddir);
2650 g_free (olddir);
2651 g_free (translated_url);
2652 return FALSE;
2654 g_free (translated_url);
2656 /* Success: save previous directory, shutdown status of previous dir */
2657 strcpy (panel->lwd, olddir);
2658 input_free_completions (cmdline);
2660 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
2662 vfs_release_path (olddir);
2664 subshell_chdir (panel->cwd);
2666 /* Reload current panel */
2667 panel_clean_dir (panel);
2668 panel->count =
2669 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
2670 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
2671 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
2672 load_hint (0);
2673 panel->dirty = 1;
2674 update_xterm_title_path ();
2676 g_free (olddir);
2678 return TRUE;
2681 /* --------------------------------------------------------------------------------------------- */
2683 static void
2684 directory_history_next (WPanel * panel)
2686 GList *nextdir;
2688 nextdir = g_list_next (panel->dir_history);
2690 if ((nextdir != NULL) && (_do_panel_cd (panel, (char *) nextdir->data, cd_exact)))
2691 panel->dir_history = nextdir;
2694 /* --------------------------------------------------------------------------------------------- */
2696 static void
2697 directory_history_prev (WPanel * panel)
2699 GList *prevdir;
2701 prevdir = g_list_previous (panel->dir_history);
2703 if ((prevdir != NULL) && (_do_panel_cd (panel, (char *) prevdir->data, cd_exact)))
2704 panel->dir_history = prevdir;
2707 /* --------------------------------------------------------------------------------------------- */
2709 static void
2710 directory_history_list (WPanel * panel)
2712 char *s;
2714 s = history_show (&panel->dir_history, &panel->widget);
2716 if (s != NULL)
2718 if (_do_panel_cd (panel, s, cd_exact))
2719 directory_history_add (panel, panel->cwd);
2720 else
2721 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2722 g_free (s);
2726 /* --------------------------------------------------------------------------------------------- */
2728 static cb_ret_t
2729 panel_execute_cmd (WPanel * panel, unsigned long command)
2731 int res = MSG_HANDLED;
2733 if (command != CK_PanelStartSearch)
2734 stop_search (panel);
2736 switch (command)
2738 case CK_PanelChdirOtherPanel:
2739 chdir_other_panel (panel);
2740 break;
2741 case CK_PanelChdirToReadlink:
2742 chdir_to_readlink (panel);
2743 break;
2744 case CK_PanelCmdCopyLocal:
2745 copy_cmd_local ();
2746 break;
2747 case CK_PanelCmdDeleteLocal:
2748 delete_cmd_local ();
2749 break;
2750 case CK_PanelCmdDoEnter:
2751 do_enter (panel);
2752 break;
2753 case CK_PanelCmdViewSimple:
2754 view_simple_cmd ();
2755 break;
2756 case CK_PanelCmdEditNew:
2757 edit_cmd_new ();
2758 break;
2759 case CK_PanelCmdRenameLocal:
2760 rename_cmd_local ();
2761 break;
2762 case CK_PanelCmdReverseSelection:
2763 reverse_selection_cmd ();
2764 break;
2765 case CK_PanelCmdSelect:
2766 select_cmd ();
2767 break;
2768 case CK_PanelCmdUnselect:
2769 unselect_cmd ();
2770 break;
2771 case CK_PanelNextPage:
2772 next_page (panel);
2773 break;
2774 case CK_PanelPrevPage:
2775 prev_page (panel);
2776 break;
2777 case CK_PanelGotoChildDir:
2778 goto_child_dir (panel);
2779 break;
2780 case CK_PanelGotoParentDir:
2781 goto_parent_dir (panel);
2782 break;
2783 case CK_PanelDirectoryHistoryList:
2784 directory_history_list (panel);
2785 break;
2786 case CK_PanelDirectoryHistoryNext:
2787 directory_history_next (panel);
2788 break;
2789 case CK_PanelDirectoryHistoryPrev:
2790 directory_history_prev (panel);
2791 break;
2792 case CK_PanelGotoBottomFile:
2793 goto_bottom_file (panel);
2794 break;
2795 case CK_PanelGotoMiddleFile:
2796 goto_middle_file (panel);
2797 break;
2798 case CK_PanelGotoTopFile:
2799 goto_top_file (panel);
2800 break;
2801 case CK_PanelMarkFile:
2802 mark_file (panel);
2803 break;
2804 case CK_PanelMarkFileUp:
2805 mark_file_up (panel);
2806 break;
2807 case CK_PanelMarkFileDown:
2808 mark_file_down (panel);
2809 break;
2810 case CK_PanelSmartGotoParentDir:
2811 res = force_maybe_cd ();
2812 break;
2813 case CK_PanelMoveUp:
2814 move_up (panel);
2815 break;
2816 case CK_PanelMoveDown:
2817 move_down (panel);
2818 break;
2819 case CK_PanelMoveLeft:
2820 res = move_left (panel);
2821 break;
2822 case CK_PanelMoveRight:
2823 res = move_right (panel);
2824 break;
2825 case CK_PanelMoveEnd:
2826 move_end (panel);
2827 break;
2828 case CK_PanelMoveHome:
2829 move_home (panel);
2830 break;
2831 case CK_PanelSetPanelEncoding:
2832 panel_change_encoding (panel);
2833 break;
2834 case CK_PanelStartSearch:
2835 start_search (panel);
2836 break;
2837 case CK_PanelStopSearch:
2838 break;
2839 case CK_PanelSyncOtherPanel:
2840 sync_other_panel (panel);
2841 break;
2842 case CK_PanelSelectSortOrder:
2843 panel_select_sort_order (panel);
2844 break;
2845 case CK_PanelToggleSortOrderPrev:
2846 panel_toggle_sort_order_prev (panel);
2847 break;
2848 case CK_PanelToggleSortOrderNext:
2849 panel_toggle_sort_order_next (panel);
2850 break;
2851 case CK_PanelReverseSort:
2852 panel->reverse = !panel->reverse;
2853 panel_set_sort_order (panel, panel->current_sort_field);
2854 break;
2855 case CK_PanelSortOrderByName:
2856 panel_set_sort_type_by_id (panel, "name");
2857 break;
2858 case CK_PanelSortOrderByExt:
2859 panel_set_sort_type_by_id (panel, "extension");
2860 break;
2861 case CK_PanelSortOrderBySize:
2862 panel_set_sort_type_by_id (panel, "size");
2863 break;
2864 case CK_PanelSortOrderByMTime:
2865 panel_set_sort_type_by_id (panel, "mtime");
2866 break;
2868 return res;
2871 /* --------------------------------------------------------------------------------------------- */
2873 static cb_ret_t
2874 panel_key (WPanel * panel, int key)
2876 size_t i;
2878 for (i = 0; panel_map[i].key != 0; i++)
2879 if (key == panel_map[i].key)
2880 return panel_execute_cmd (panel, panel_map[i].command);
2882 if (torben_fj_mode && key == ALT ('h'))
2884 goto_middle_file (panel);
2885 return MSG_HANDLED;
2888 if (is_abort_char (key))
2890 stop_search (panel);
2891 return MSG_HANDLED;
2894 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2895 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE)
2897 if (panel->searching)
2899 do_search (panel, key);
2900 return MSG_HANDLED;
2903 if (!command_prompt)
2905 start_search (panel);
2906 do_search (panel, key);
2907 return MSG_HANDLED;
2911 return MSG_NOT_HANDLED;
2914 /* --------------------------------------------------------------------------------------------- */
2916 static cb_ret_t
2917 panel_callback (Widget * w, widget_msg_t msg, int parm)
2919 WPanel *panel = (WPanel *) w;
2920 WButtonBar *bb;
2922 switch (msg)
2924 case WIDGET_DRAW:
2925 paint_panel (panel);
2926 return MSG_HANDLED;
2928 case WIDGET_FOCUS:
2929 current_panel = panel;
2930 panel->active = 1;
2931 if (mc_chdir (panel->cwd) != 0)
2933 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2934 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
2935 cwd, unix_error_string (errno));
2936 g_free (cwd);
2938 else
2939 subshell_chdir (panel->cwd);
2941 update_xterm_title_path ();
2942 select_item (panel);
2943 show_dir (panel);
2944 paint_dir (panel);
2945 panel->dirty = 0;
2947 bb = find_buttonbar (panel->widget.owner);
2948 midnight_set_buttonbar (bb);
2949 buttonbar_redraw (bb);
2950 return MSG_HANDLED;
2952 case WIDGET_UNFOCUS:
2953 /* Janne: look at this for the multiple panel options */
2954 stop_search (panel);
2955 panel->active = 0;
2956 show_dir (panel);
2957 unselect_item (panel);
2958 return MSG_HANDLED;
2960 case WIDGET_KEY:
2961 return panel_key (panel, parm);
2963 case WIDGET_COMMAND:
2964 return panel_execute_cmd (panel, parm);
2966 case WIDGET_DESTROY:
2967 panel_destroy (panel);
2968 free_my_statfs ();
2969 return MSG_HANDLED;
2971 default:
2972 return default_proc (msg, parm);
2976 /* --------------------------------------------------------------------------------------------- */
2977 /* */
2978 /* Panel mouse events support routines */
2979 /* */
2981 static void
2982 mouse_toggle_mark (WPanel * panel)
2984 do_mark_file (panel, MARK_DONT_MOVE);
2985 mouse_marking = selection (panel)->f.marked;
2988 /* --------------------------------------------------------------------------------------------- */
2990 static void
2991 mouse_set_mark (WPanel * panel)
2993 if (mouse_marking && !(selection (panel)->f.marked))
2994 do_mark_file (panel, MARK_DONT_MOVE);
2995 else if (!mouse_marking && (selection (panel)->f.marked))
2996 do_mark_file (panel, MARK_DONT_MOVE);
2999 /* --------------------------------------------------------------------------------------------- */
3001 static int
3002 mark_if_marking (WPanel * panel, Gpm_Event * event)
3004 if (event->buttons & GPM_B_RIGHT)
3006 if (event->type & GPM_DOWN)
3007 mouse_toggle_mark (panel);
3008 else
3009 mouse_set_mark (panel);
3010 return 1;
3012 return 0;
3015 /* --------------------------------------------------------------------------------------------- */
3016 /** Determine which column was clicked, and sort the panel on
3017 * that column, or reverse sort on that column if already
3018 * sorted on that column.
3021 static void
3022 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3024 int i;
3025 const char *lc_sort_name = NULL;
3026 panel_field_t *col_sort_format = NULL;
3027 format_e *format;
3028 gchar *title;
3030 for (i = 0, format = panel->format; format != NULL; format = format->next)
3032 i += format->field_len;
3033 if (event->x < i + 1)
3035 /* found column */
3036 lc_sort_name = format->title;
3037 break;
3041 if (lc_sort_name == NULL)
3042 return;
3044 for (i = 0; panel_fields[i].id != NULL; i++)
3046 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3047 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3049 col_sort_format = &panel_fields[i];
3050 g_free (title);
3051 break;
3053 g_free (title);
3056 if (!col_sort_format)
3057 return;
3059 if (panel->current_sort_field == col_sort_format)
3061 /* reverse the sort if clicked column is already the sorted column */
3062 panel->reverse = !panel->reverse;
3064 else
3066 /* new sort is forced to be ascending */
3067 panel->reverse = 0;
3069 panel_set_sort_order (panel, col_sort_format);
3073 /* --------------------------------------------------------------------------------------------- */
3075 * Mouse callback of the panel minus repainting.
3076 * If the event is redirected to the menu, *redir is set to TRUE.
3078 static int
3079 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3081 const int lines = llines (panel);
3082 const gboolean is_active = dlg_widget_active (panel);
3083 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3085 *redir = FALSE;
3087 /* 1st line */
3088 if (mouse_down && event->y == 1)
3090 /* "<" button */
3091 if (event->x == 2)
3093 directory_history_prev (panel);
3094 return MOU_NORMAL;
3097 /* "." button show/hide hidden files */
3098 if (event->x == panel->widget.cols - 5)
3100 panel->widget.owner->callback (panel->widget.owner, NULL,
3101 DLG_ACTION, CK_ToggleShowHidden, NULL);
3102 repaint_screen ();
3103 return MOU_NORMAL;
3106 /* ">" button */
3107 if (event->x == panel->widget.cols - 1)
3109 directory_history_next (panel);
3110 return MOU_NORMAL;
3113 /* "^" button */
3114 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3116 directory_history_list (panel);
3117 return MOU_NORMAL;
3120 /* rest of the upper frame, the menu is invisible - call menu */
3121 if (!menubar_visible)
3123 *redir = TRUE;
3124 event->x += panel->widget.x;
3125 return the_menubar->widget.mouse (event, the_menubar);
3128 /* no other events on 1st line */
3129 return MOU_NORMAL;
3132 /* sort on clicked column; don't handle wheel events */
3133 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3135 mouse_sort_col (event, panel);
3136 return MOU_NORMAL;
3139 /* Mouse wheel events */
3140 if (mouse_down && (event->buttons & GPM_B_UP))
3142 if (is_active)
3144 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3145 prev_page (panel);
3146 else /* We are in first page */
3147 move_up (panel);
3149 return MOU_NORMAL;
3152 if (mouse_down && (event->buttons & GPM_B_DOWN))
3154 if (is_active)
3156 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3157 next_page (panel);
3158 else /* We are in last page */
3159 move_down (panel);
3161 return MOU_NORMAL;
3164 event->y -= 2;
3165 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3167 int my_index;
3169 if (!is_active)
3170 change_panel ();
3172 if (panel->top_file + event->y > panel->count)
3173 my_index = panel->count - 1;
3174 else
3176 my_index = panel->top_file + event->y - 1;
3177 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3178 my_index += llines (panel);
3180 if (my_index >= panel->count)
3181 my_index = panel->count - 1;
3184 if (my_index != panel->selected)
3186 unselect_item (panel);
3187 panel->selected = my_index;
3188 select_item (panel);
3191 /* This one is new */
3192 mark_if_marking (panel, event);
3194 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3196 if (event->y > 0 && event->y <= lines)
3197 do_enter (panel);
3199 return MOU_NORMAL;
3202 /* --------------------------------------------------------------------------------------------- */
3203 /** Mouse callback of the panel */
3205 static int
3206 panel_event (Gpm_Event * event, void *data)
3208 WPanel *panel = data;
3209 int ret;
3210 gboolean redir;
3212 ret = do_panel_event (event, panel, &redir);
3213 if (!redir)
3214 paint_panel (panel);
3216 return ret;
3219 /* --------------------------------------------------------------------------------------------- */
3221 static void
3222 reload_panelized (WPanel * panel)
3224 int i, j;
3225 dir_list *list = &panel->dir;
3227 if (panel != current_panel)
3229 int ret;
3230 ret = mc_chdir (panel->cwd);
3233 for (i = 0, j = 0; i < panel->count; i++)
3235 if (list->list[i].f.marked)
3237 /* Unmark the file in advance. In case the following mc_lstat
3238 * fails we are done, else we have to mark the file again
3239 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3240 * IMO that's the best way to update the panel's summary status
3241 * -- Norbert
3243 do_file_mark (panel, i, 0);
3245 if (mc_lstat (list->list[i].fname, &list->list[i].st))
3247 g_free (list->list[i].fname);
3248 continue;
3250 if (list->list[i].f.marked)
3251 do_file_mark (panel, i, 1);
3252 if (j != i)
3253 list->list[j] = list->list[i];
3254 j++;
3256 if (j == 0)
3257 panel->count = set_zero_dir (list) ? 1 : 0;
3258 else
3259 panel->count = j;
3261 if (panel != current_panel)
3263 int ret;
3264 ret = mc_chdir (current_panel->cwd);
3268 /* --------------------------------------------------------------------------------------------- */
3270 static void
3271 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3273 gboolean free_pointer;
3274 char *my_current_file = NULL;
3276 if ((flags & UP_RELOAD) != 0)
3278 panel->is_panelized = 0;
3279 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
3280 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3283 /* If current_file == -1 (an invalid pointer) then preserve selection */
3284 free_pointer = current_file == UP_KEEPSEL;
3286 if (free_pointer)
3288 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3289 current_file = my_current_file;
3292 if (panel->is_panelized)
3293 reload_panelized (panel);
3294 else
3295 panel_reload (panel);
3297 try_to_select (panel, current_file);
3298 panel->dirty = 1;
3300 if (free_pointer)
3301 g_free (my_current_file);
3304 /* --------------------------------------------------------------------------------------------- */
3306 static void
3307 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3309 if (get_display_type (which) == view_listing)
3311 WPanel *panel;
3312 panel = (WPanel *) get_panel_widget (which);
3313 update_one_panel_widget (panel, flags, current_file);
3317 /* --------------------------------------------------------------------------------------------- */
3318 /*** public functions ****************************************************************************/
3319 /* --------------------------------------------------------------------------------------------- */
3321 char *
3322 remove_encoding_from_path (const char *path)
3324 GString *ret;
3325 GString *tmp_path, *tmp_conv;
3326 char *tmp;
3328 ret = g_string_new ("");
3329 tmp_conv = g_string_new ("");
3330 tmp_path = g_string_new (path);
3332 while ((tmp = g_strrstr (tmp_path->str, PATH_SEP_STR VFS_ENCODING_PREFIX)) != NULL)
3334 const char *enc;
3335 GIConv converter;
3336 char *tmp2;
3338 enc = vfs_get_encoding ((const char *) tmp);
3339 converter = enc != NULL ? str_crt_conv_to (enc) : str_cnv_to_term;
3340 if (converter == INVALID_CONV)
3341 converter = str_cnv_to_term;
3343 tmp2 = tmp + 1;
3344 while (*tmp2 != '\0' && *tmp2 != PATH_SEP)
3345 tmp2++;
3347 if (*tmp2 != '\0')
3349 str_vfs_convert_from (converter, tmp2, tmp_conv);
3350 g_string_prepend (ret, tmp_conv->str);
3351 g_string_set_size (tmp_conv, 0);
3354 g_string_set_size (tmp_path, tmp - tmp_path->str);
3355 str_close_conv (converter);
3358 g_string_prepend (ret, tmp_path->str);
3359 g_string_free (tmp_path, TRUE);
3360 g_string_free (tmp_conv, TRUE);
3362 return g_string_free (ret, FALSE);
3365 /* --------------------------------------------------------------------------------------------- */
3367 * Repaint the contents of the panels without frames. To schedule panel
3368 * for repainting, set panel->dirty to 1. There are many reasons why
3369 * the panels need to be repainted, and this is a costly operation, so
3370 * it's done once per event.
3373 void
3374 update_dirty_panels (void)
3376 if (current_panel->dirty)
3377 paint_panel (current_panel);
3379 if ((get_other_type () == view_listing) && other_panel->dirty)
3380 paint_panel (other_panel);
3383 /* --------------------------------------------------------------------------------------------- */
3385 static void
3386 do_select (WPanel * panel, int i)
3388 if (i != panel->selected)
3390 panel->dirty = 1;
3391 panel->selected = i;
3392 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3393 if (panel->top_file < 0)
3394 panel->top_file = 0;
3398 /* --------------------------------------------------------------------------------------------- */
3400 static void
3401 do_try_to_select (WPanel * panel, const char *name)
3403 int i;
3404 char *subdir;
3406 if (!name)
3408 do_select (panel, 0);
3409 return;
3412 /* We only want the last component of the directory,
3413 * and from this only the name without suffix. */
3414 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3416 /* Search that subdirectory, if found select it */
3417 for (i = 0; i < panel->count; i++)
3419 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3421 do_select (panel, i);
3422 g_free (subdir);
3423 return;
3427 /* Try to select a file near the file that is missing */
3428 if (panel->selected >= panel->count)
3429 do_select (panel, panel->count - 1);
3430 g_free (subdir);
3433 /* --------------------------------------------------------------------------------------------- */
3435 void
3436 try_to_select (WPanel * panel, const char *name)
3438 do_try_to_select (panel, name);
3439 select_item (panel);
3442 /* --------------------------------------------------------------------------------------------- */
3444 void
3445 panel_update_cols (Widget * widget, int frame_size)
3447 int cols, origin;
3449 if (horizontal_split)
3451 widget->cols = COLS;
3452 return;
3455 if (frame_size == frame_full)
3457 cols = COLS;
3458 origin = 0;
3460 else
3462 if (widget == get_panel_widget (0))
3464 cols = first_panel_size;
3465 origin = 0;
3467 else
3469 cols = COLS - first_panel_size;
3470 origin = first_panel_size;
3474 widget->cols = cols;
3475 widget->x = origin;
3478 /* --------------------------------------------------------------------------------------------- */
3480 void
3481 panel_clean_dir (WPanel * panel)
3483 int count = panel->count;
3485 panel->count = 0;
3486 panel->top_file = 0;
3487 panel->selected = 0;
3488 panel->marked = 0;
3489 panel->dirs_marked = 0;
3490 panel->total = 0;
3491 panel->searching = FALSE;
3492 panel->is_panelized = 0;
3493 panel->dirty = 1;
3495 clean_dir (&panel->dir, count);
3498 /* --------------------------------------------------------------------------------------------- */
3499 /** Panel creation.
3500 * @param panel_name the name of the panel for setup retieving
3501 * @returns new instance of WPanel
3504 WPanel *
3505 panel_new (const char *panel_name)
3507 return panel_new_with_dir (panel_name, NULL);
3510 /* --------------------------------------------------------------------------------------------- */
3511 /** Panel creation for specified directory.
3512 * @param panel_name specifies the name of the panel for setup retieving
3513 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3514 * @returns new instance of WPanel
3517 WPanel *
3518 panel_new_with_dir (const char *panel_name, const char *wpath)
3520 WPanel *panel;
3521 char *section;
3522 int i, err;
3523 char curdir[MC_MAXPATHLEN] = "\0";
3525 panel = g_new0 (WPanel, 1);
3527 /* No know sizes of the panel at startup */
3528 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3530 /* We do not want the cursor */
3531 widget_want_cursor (panel->widget, 0);
3533 if (wpath != NULL)
3535 g_strlcpy (panel->cwd, wpath, sizeof (panel->cwd));
3536 mc_get_current_wd (curdir, sizeof (curdir) - 2);
3538 else
3539 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3541 strcpy (panel->lwd, ".");
3543 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3544 panel->dir_history = history_get (panel->hist_name);
3545 directory_history_add (panel, panel->cwd);
3547 panel->dir.list = g_new (file_entry, MIN_FILES);
3548 panel->dir.size = MIN_FILES;
3549 panel->active = 0;
3550 panel->filter = 0;
3551 panel->split = 0;
3552 panel->top_file = 0;
3553 panel->selected = 0;
3554 panel->marked = 0;
3555 panel->total = 0;
3556 panel->reverse = 0;
3557 panel->dirty = 1;
3558 panel->searching = FALSE;
3559 panel->dirs_marked = 0;
3560 panel->is_panelized = 0;
3561 panel->format = 0;
3562 panel->status_format = 0;
3563 panel->format_modified = 1;
3565 panel->panel_name = g_strdup (panel_name);
3566 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3568 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3570 for (i = 0; i < LIST_TYPES; i++)
3571 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3573 panel->search_buffer[0] = '\0';
3574 panel->prev_search_buffer[0] = '\0';
3575 panel->frame_size = frame_half;
3577 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3578 if (!mc_config_has_group (mc_main_config, section))
3580 g_free (section);
3581 section = g_strdup (panel->panel_name);
3583 panel_load_setup (panel, section);
3584 g_free (section);
3586 /* Load format strings */
3587 err = set_panel_formats (panel);
3588 if (err != 0)
3589 set_panel_formats (panel);
3591 #ifdef HAVE_CHARSET
3593 const char *enc = vfs_get_encoding (panel->cwd);
3594 if (enc != NULL)
3595 panel->codepage = get_codepage_index (enc);
3597 #endif
3599 if (mc_chdir (panel->cwd) != 0)
3601 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3602 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3605 /* Load the default format */
3606 panel->count =
3607 do_load_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
3608 panel->reverse, panel->case_sensitive, panel->exec_first, panel->filter);
3610 /* Restore old right path */
3611 if (curdir[0] != '\0')
3612 err = mc_chdir (curdir);
3614 return panel;
3617 /* --------------------------------------------------------------------------------------------- */
3619 void
3620 panel_reload (WPanel * panel)
3622 struct stat current_stat;
3624 if (panels_options.fast_reload && !stat (panel->cwd, &current_stat)
3625 && current_stat.st_ctime == panel->dir_stat.st_ctime
3626 && current_stat.st_mtime == panel->dir_stat.st_mtime)
3627 return;
3629 while (mc_chdir (panel->cwd) == -1)
3631 char *last_slash;
3633 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0)
3635 panel_clean_dir (panel);
3636 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
3637 return;
3639 last_slash = strrchr (panel->cwd, PATH_SEP);
3640 if (!last_slash || last_slash == panel->cwd)
3641 strcpy (panel->cwd, PATH_SEP_STR);
3642 else
3643 *last_slash = 0;
3644 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3645 show_dir (panel);
3648 panel->count =
3649 do_reload_dir (panel->cwd, &panel->dir, panel->current_sort_field->sort_routine,
3650 panel->count, panel->reverse, panel->case_sensitive,
3651 panel->exec_first, panel->filter);
3653 panel->dirty = 1;
3654 if (panel->selected >= panel->count)
3655 do_select (panel, panel->count - 1);
3657 recalculate_panel_summary (panel);
3660 /* --------------------------------------------------------------------------------------------- */
3661 /* Switches the panel to the mode specified in the format */
3662 /* Seting up both format and status string. Return: 0 - on success; */
3663 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
3666 set_panel_formats (WPanel * p)
3668 format_e *form;
3669 char *err = NULL;
3670 int retcode = 0;
3672 form = use_display_format (p, panel_format (p), &err, 0);
3674 if (err != NULL)
3676 g_free (err);
3677 retcode = 1;
3679 else
3681 delete_format (p->format);
3682 p->format = form;
3685 if (show_mini_info)
3687 form = use_display_format (p, mini_status_format (p), &err, 1);
3689 if (err != NULL)
3691 g_free (err);
3692 retcode += 2;
3694 else
3696 delete_format (p->status_format);
3697 p->status_format = form;
3701 panel_format_modified (p);
3702 panel_update_cols (&(p->widget), p->frame_size);
3704 if (retcode)
3705 message (D_ERROR, _("Warning"),
3706 _("User supplied format looks invalid, reverting to default."));
3707 if (retcode & 0x01)
3709 g_free (p->user_format);
3710 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
3712 if (retcode & 0x02)
3714 g_free (p->user_status_format[p->list_type]);
3715 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
3718 return retcode;
3721 /* --------------------------------------------------------------------------------------------- */
3723 /* Select current item and readjust the panel */
3724 void
3725 select_item (WPanel * panel)
3727 int items = ITEMS (panel);
3729 /* Although currently all over the code we set the selection and
3730 top file to decent values before calling select_item, I could
3731 forget it someday, so it's better to do the actual fitting here */
3733 if (panel->top_file < 0)
3734 panel->top_file = 0;
3736 if (panel->selected < 0)
3737 panel->selected = 0;
3739 if (panel->selected > panel->count - 1)
3740 panel->selected = panel->count - 1;
3742 if (panel->top_file > panel->count - 1)
3743 panel->top_file = panel->count - 1;
3745 if ((panel->count - panel->top_file) < items)
3747 panel->top_file = panel->count - items;
3748 if (panel->top_file < 0)
3749 panel->top_file = 0;
3752 if (panel->selected < panel->top_file)
3753 panel->top_file = panel->selected;
3755 if ((panel->selected - panel->top_file) >= items)
3756 panel->top_file = panel->selected - items + 1;
3758 panel->dirty = 1;
3760 execute_hooks (select_file_hook);
3763 /* --------------------------------------------------------------------------------------------- */
3764 /** Clears all files in the panel, used only when one file was marked */
3765 void
3766 unmark_files (WPanel * panel)
3768 int i;
3770 if (!panel->marked)
3771 return;
3772 for (i = 0; i < panel->count; i++)
3773 file_mark (panel, i, 0);
3775 panel->dirs_marked = 0;
3776 panel->marked = 0;
3777 panel->total = 0;
3780 /* --------------------------------------------------------------------------------------------- */
3781 /** Recalculate the panels summary information, used e.g. when marked
3782 files might have been removed by an external command */
3784 void
3785 recalculate_panel_summary (WPanel * panel)
3787 int i;
3789 panel->marked = 0;
3790 panel->dirs_marked = 0;
3791 panel->total = 0;
3793 for (i = 0; i < panel->count; i++)
3794 if (panel->dir.list[i].f.marked)
3796 /* do_file_mark will return immediately if newmark == oldmark.
3797 So we have to first unmark it to get panel's summary information
3798 updated. (Norbert) */
3799 panel->dir.list[i].f.marked = 0;
3800 do_file_mark (panel, i, 1);
3804 /* --------------------------------------------------------------------------------------------- */
3805 /** This routine marks a file or a directory */
3807 void
3808 do_file_mark (WPanel * panel, int idx, int mark)
3810 if (panel->dir.list[idx].f.marked == mark)
3811 return;
3813 /* Only '..' can't be marked, '.' isn't visible */
3814 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
3815 return;
3817 file_mark (panel, idx, mark);
3818 if (panel->dir.list[idx].f.marked)
3820 panel->marked++;
3821 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
3823 if (panel->dir.list[idx].f.dir_size_computed)
3824 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
3825 panel->dirs_marked++;
3827 else
3828 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
3829 set_colors (panel);
3831 else
3833 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
3835 if (panel->dir.list[idx].f.dir_size_computed)
3836 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
3837 panel->dirs_marked--;
3839 else
3840 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
3841 panel->marked--;
3845 /* --------------------------------------------------------------------------------------------- */
3847 * Changes the current directory of the panel.
3848 * Record change in the directory history.
3850 gboolean
3851 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
3853 gboolean r;
3855 r = _do_panel_cd (panel, new_dir, cd_type);
3856 if (r)
3857 directory_history_add (panel, panel->cwd);
3858 return r;
3861 /* --------------------------------------------------------------------------------------------- */
3863 void
3864 file_mark (WPanel * panel, int lc_index, int val)
3866 if (panel->dir.list[lc_index].f.marked != val)
3868 panel->dir.list[lc_index].f.marked = val;
3869 panel->dirty = 1;
3873 /* --------------------------------------------------------------------------------------------- */
3875 void
3876 panel_re_sort (WPanel * panel)
3878 char *filename;
3879 int i;
3881 if (panel == NULL)
3882 return;
3884 filename = g_strdup (selection (panel)->fname);
3885 unselect_item (panel);
3886 do_sort (&panel->dir, panel->current_sort_field->sort_routine, panel->count - 1, panel->reverse,
3887 panel->case_sensitive, panel->exec_first);
3888 panel->selected = -1;
3889 for (i = panel->count; i; i--)
3891 if (!strcmp (panel->dir.list[i - 1].fname, filename))
3893 panel->selected = i - 1;
3894 break;
3897 g_free (filename);
3898 panel->top_file = panel->selected - ITEMS (panel) / 2;
3899 select_item (panel);
3900 panel->dirty = 1;
3903 /* --------------------------------------------------------------------------------------------- */
3905 void
3906 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
3908 if (sort_order == 0)
3909 return;
3911 panel->current_sort_field = sort_order;
3913 /* The directory is already sorted, we have to load the unsorted stuff */
3914 if (sort_order->sort_routine == (sortfn *) unsorted)
3916 char *current_file;
3918 current_file = g_strdup (panel->dir.list[panel->selected].fname);
3919 panel_reload (panel);
3920 try_to_select (panel, current_file);
3921 g_free (current_file);
3923 panel_re_sort (panel);
3926 /* --------------------------------------------------------------------------------------------- */
3928 * Change panel encoding.
3929 * @param panel WPanel object
3932 void
3933 panel_change_encoding (WPanel * panel)
3935 const char *encoding = NULL;
3936 char *cd_path;
3937 #ifdef HAVE_CHARSET
3938 char *errmsg;
3939 int r;
3941 r = select_charset (-1, -1, panel->codepage, FALSE);
3943 if (r == SELECT_CHARSET_CANCEL)
3944 return; /* Cancel */
3946 panel->codepage = r;
3948 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
3950 /* No translation */
3951 g_free (init_translation_table (display_codepage, display_codepage));
3952 cd_path = remove_encoding_from_path (panel->cwd);
3953 do_panel_cd (panel, cd_path, cd_parse_command);
3954 g_free (cd_path);
3955 return;
3958 errmsg = init_translation_table (panel->codepage, display_codepage);
3959 if (errmsg != NULL)
3961 message (D_ERROR, MSG_ERROR, "%s", errmsg);
3962 g_free (errmsg);
3963 return;
3966 encoding = get_codepage_id (panel->codepage);
3967 #endif
3968 if (encoding != NULL)
3970 const char *enc;
3972 enc = vfs_get_encoding (panel->cwd);
3974 /* don't add current encoding */
3975 if ((enc == NULL) || (strcmp (encoding, enc) != 0))
3977 cd_path = add_encoding_to_path (panel->cwd, encoding);
3978 if (!do_panel_cd (panel, cd_path, cd_parse_command))
3979 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
3980 g_free (cd_path);
3985 /* --------------------------------------------------------------------------------------------- */
3987 * This routine reloads the directory in both panels. It tries to
3988 * select current_file in current_panel and other_file in other_panel.
3989 * If current_file == -1 then it automatically sets current_file and
3990 * other_file to the currently selected files in the panels.
3992 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
3993 * will not reload the other panel.
3996 void
3997 update_panels (panel_update_flags_t flags, const char *current_file)
3999 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4000 WPanel *panel;
4001 int ret;
4003 update_one_panel (get_current_index (), flags, current_file);
4004 if (reload_other)
4005 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4007 if (get_current_type () == view_listing)
4008 panel = (WPanel *) get_panel_widget (get_current_index ());
4009 else
4010 panel = (WPanel *) get_panel_widget (get_other_index ());
4012 ret = mc_chdir (panel->cwd);
4015 /* --------------------------------------------------------------------------------------------- */
4017 void
4018 directory_history_add (struct WPanel *panel, const char *dir)
4020 char *tmp;
4022 tmp = g_strdup (dir);
4023 strip_password (tmp, 1);
4025 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4028 /* --------------------------------------------------------------------------------------------- */
4030 gsize
4031 panel_get_num_of_sortable_fields (void)
4033 gsize ret = 0, lc_index;
4035 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4036 if (panel_fields[lc_index].is_user_choice)
4037 ret++;
4038 return ret;
4041 /* --------------------------------------------------------------------------------------------- */
4043 const char **
4044 panel_get_sortable_fields (gsize * array_size)
4046 char **ret;
4047 gsize lc_index, i;
4049 lc_index = panel_get_num_of_sortable_fields ();
4051 ret = g_try_new0 (char *, lc_index + 1);
4052 if (ret == NULL)
4053 return NULL;
4055 if (array_size != NULL)
4056 *array_size = lc_index;
4058 lc_index = 0;
4060 for (i = 0; panel_fields[i].id != NULL; i++)
4061 if (panel_fields[i].is_user_choice)
4062 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4063 return (const char **) ret;
4066 /* --------------------------------------------------------------------------------------------- */
4068 const panel_field_t *
4069 panel_get_field_by_id (const char *name)
4071 gsize lc_index;
4072 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4073 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4074 return &panel_fields[lc_index];
4075 return NULL;
4078 /* --------------------------------------------------------------------------------------------- */
4080 const panel_field_t *
4081 panel_get_field_by_title_hotkey (const char *name)
4083 gsize lc_index;
4084 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4085 if (panel_fields[lc_index].title_hotkey != NULL &&
4086 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4087 return &panel_fields[lc_index];
4088 return NULL;
4091 /* --------------------------------------------------------------------------------------------- */
4093 const panel_field_t *
4094 panel_get_field_by_title (const char *name)
4096 gsize lc_index;
4097 gchar *title = NULL;
4099 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4101 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4102 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4104 g_free (title);
4105 return &panel_fields[lc_index];
4108 g_free (title);
4109 return NULL;
4112 /* --------------------------------------------------------------------------------------------- */
4114 gsize
4115 panel_get_num_of_user_possible_fields (void)
4117 gsize ret = 0, lc_index;
4119 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4120 if (panel_fields[lc_index].use_in_user_format)
4121 ret++;
4122 return ret;
4125 /* --------------------------------------------------------------------------------------------- */
4127 const char **
4128 panel_get_user_possible_fields (gsize * array_size)
4130 char **ret;
4131 gsize lc_index, i;
4133 lc_index = panel_get_num_of_user_possible_fields ();
4135 ret = g_try_new0 (char *, lc_index + 1);
4136 if (ret == NULL)
4137 return NULL;
4139 if (array_size != NULL)
4140 *array_size = lc_index;
4142 lc_index = 0;
4144 for (i = 0; panel_fields[i].id != NULL; i++)
4145 if (panel_fields[i].use_in_user_format)
4146 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4147 return (const char **) ret;
4150 /* --------------------------------------------------------------------------------------------- */
4152 void
4153 panel_init (void)
4155 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4156 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4158 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4159 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4160 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4161 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4162 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4166 /* --------------------------------------------------------------------------------------------- */
4168 void
4169 panel_deinit (void)
4171 g_free (panel_sort_up_sign);
4172 g_free (panel_sort_down_sign);
4174 g_free (panel_hiddenfiles_sign_show);
4175 g_free (panel_hiddenfiles_sign_hide);
4176 g_free (panel_history_prev_item_sign);
4177 g_free (panel_history_next_item_sign);
4178 g_free (panel_history_show_list_sign);
4182 /* --------------------------------------------------------------------------------------------- */