Ticket #2390: file list format of panel is intialized incorrectly
[midnight-commander.git] / src / filemanager / panel.c
blobf55bc9cb3f7b53b95631bf22a2cdd9fac813f3f6
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 if (*panel->sort_info.sort_field->hotkey != '\0')
1265 const char *sort_sign = panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1266 char *str;
1268 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1269 widget_move (&panel->widget, 1, 1);
1270 tty_print_string (str);
1271 g_free (str);
1275 /* --------------------------------------------------------------------------------------------- */
1277 static gchar *
1278 panel_get_title_without_hotkey (const char *title)
1280 char *translated_title;
1281 char *hkey;
1283 if (title == NULL)
1284 return NULL;
1285 if (title[0] == '\0')
1286 return g_strdup ("");
1288 translated_title = g_strdup (_(title));
1290 hkey = strchr (translated_title, '&');
1291 if ((hkey != NULL) && (hkey[1] != '\0'))
1292 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1294 return translated_title;
1297 /* --------------------------------------------------------------------------------------------- */
1299 static void
1300 paint_frame (WPanel * panel)
1302 int side, width;
1303 GString *format_txt;
1305 if (!panel->split)
1306 adjust_top_file (panel);
1308 widget_erase (&panel->widget);
1309 show_dir (panel);
1311 widget_move (&panel->widget, 1, 1);
1313 for (side = 0; side <= panel->split; side++)
1315 format_e *format;
1317 if (side)
1319 tty_setcolor (NORMAL_COLOR);
1320 tty_print_one_vline (TRUE);
1321 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1323 else if (panel->split)
1324 width = panel->widget.cols / 2 - 3;
1325 else
1326 width = panel->widget.cols - 2;
1328 format_txt = g_string_new ("");
1329 for (format = panel->format; format; format = format->next)
1331 if (format->string_fn)
1333 g_string_set_size (format_txt, 0);
1335 if (panel->list_type == list_long
1336 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1337 g_string_append (format_txt,
1338 panel->sort_info.reverse
1339 ? 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->sort_info.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->sort_info.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--);
2492 if (pfield != NULL)
2494 panel->sort_info.sort_field = pfield;
2495 panel_set_sort_order (panel, pfield);
2499 /* --------------------------------------------------------------------------------------------- */
2501 static void
2502 panel_toggle_sort_order_next (WPanel * panel)
2504 gsize lc_index, i;
2505 const panel_field_t *pfield = NULL;
2506 gsize format_field_count;
2507 gchar *title;
2509 format_field_count = panel_get_format_field_count (panel);
2510 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2511 lc_index = panel_get_format_field_index_by_name (panel, title);
2512 g_free (title);
2514 if (lc_index != 0 && lc_index != format_field_count)
2516 /* search for prev sortable column in panel format */
2517 for (i = lc_index;
2518 i != format_field_count
2519 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2522 if (pfield == NULL)
2524 /* Sortable field not found. Try to search in each array */
2525 for (i = 0;
2526 i != format_field_count
2527 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2530 if (pfield != NULL)
2532 panel->sort_info.sort_field = pfield;
2533 panel_set_sort_order (panel, pfield);
2537 /* --------------------------------------------------------------------------------------------- */
2539 static void
2540 panel_select_sort_order (WPanel * panel)
2542 const panel_field_t *sort_order;
2544 sort_order = sort_box (&panel->sort_info);
2545 if (sort_order != NULL)
2547 panel->sort_info.sort_field = sort_order;
2548 panel_set_sort_order (panel, sort_order);
2552 /* --------------------------------------------------------------------------------------------- */
2554 static void
2555 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2557 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2559 const panel_field_t *sort_order;
2561 sort_order = panel_get_field_by_id (name);
2562 if (sort_order == NULL)
2563 return;
2564 panel->sort_info.sort_field = sort_order;
2566 else
2567 panel->sort_info.reverse = !panel->sort_info.reverse;
2569 panel_set_sort_order (panel, panel->sort_info.sort_field);
2572 /* --------------------------------------------------------------------------------------------- */
2574 * If we moved to the parent directory move the selection pointer to
2575 * the old directory name; If we leave VFS dir, remove FS specificator.
2577 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2580 static const char *
2581 get_parent_dir_name (const char *cwd, const char *lwd)
2583 size_t llen, clen;
2585 llen = strlen (lwd);
2586 clen = strlen (cwd);
2588 if (llen > clen)
2590 const char *p;
2592 p = strrchr (lwd, PATH_SEP);
2594 if ((p != NULL)
2595 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2596 && (clen == (size_t) (p - lwd)
2597 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2598 return (p + 1);
2601 return NULL;
2604 /* --------------------------------------------------------------------------------------------- */
2605 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2607 static void
2608 subshell_chdir (const char *directory)
2610 #ifdef HAVE_SUBSHELL_SUPPORT
2611 if (use_subshell && vfs_current_is_local ())
2612 do_subshell_chdir (directory, FALSE, TRUE);
2613 #endif /* HAVE_SUBSHELL_SUPPORT */
2616 /* --------------------------------------------------------------------------------------------- */
2618 * Changes the current directory of the panel.
2619 * Don't record change in the directory history.
2622 static gboolean
2623 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
2625 const char *directory;
2626 char *olddir;
2627 char temp[MC_MAXPATHLEN];
2628 char *translated_url;
2630 if (cd_type == cd_parse_command)
2632 while (*new_dir == ' ')
2633 new_dir++;
2636 olddir = g_strdup (panel->cwd);
2637 new_dir = translated_url = vfs_translate_url (new_dir);
2639 /* Convert *new_path to a suitable pathname, handle ~user */
2641 if (cd_type == cd_parse_command)
2643 if (!strcmp (new_dir, "-"))
2645 strcpy (temp, panel->lwd);
2646 new_dir = temp;
2649 directory = *new_dir ? new_dir : home_dir;
2651 if (mc_chdir (directory) == -1)
2653 strcpy (panel->cwd, olddir);
2654 g_free (olddir);
2655 g_free (translated_url);
2656 return FALSE;
2658 g_free (translated_url);
2660 /* Success: save previous directory, shutdown status of previous dir */
2661 strcpy (panel->lwd, olddir);
2662 input_free_completions (cmdline);
2664 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
2666 vfs_release_path (olddir);
2668 subshell_chdir (panel->cwd);
2670 /* Reload current panel */
2671 panel_clean_dir (panel);
2672 panel->count =
2673 do_load_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
2674 panel->sort_info.reverse, panel->sort_info.case_sensitive,
2675 panel->sort_info.exec_first, panel->filter);
2676 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
2677 load_hint (0);
2678 panel->dirty = 1;
2679 update_xterm_title_path ();
2681 g_free (olddir);
2683 return TRUE;
2686 /* --------------------------------------------------------------------------------------------- */
2688 static void
2689 directory_history_next (WPanel * panel)
2691 GList *nextdir;
2693 nextdir = g_list_next (panel->dir_history);
2695 if ((nextdir != NULL) && (_do_panel_cd (panel, (char *) nextdir->data, cd_exact)))
2696 panel->dir_history = nextdir;
2699 /* --------------------------------------------------------------------------------------------- */
2701 static void
2702 directory_history_prev (WPanel * panel)
2704 GList *prevdir;
2706 prevdir = g_list_previous (panel->dir_history);
2708 if ((prevdir != NULL) && (_do_panel_cd (panel, (char *) prevdir->data, cd_exact)))
2709 panel->dir_history = prevdir;
2712 /* --------------------------------------------------------------------------------------------- */
2714 static void
2715 directory_history_list (WPanel * panel)
2717 char *s;
2719 s = history_show (&panel->dir_history, &panel->widget);
2721 if (s != NULL)
2723 if (_do_panel_cd (panel, s, cd_exact))
2724 directory_history_add (panel, panel->cwd);
2725 else
2726 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2727 g_free (s);
2731 /* --------------------------------------------------------------------------------------------- */
2733 static cb_ret_t
2734 panel_execute_cmd (WPanel * panel, unsigned long command)
2736 int res = MSG_HANDLED;
2738 if (command != CK_PanelStartSearch)
2739 stop_search (panel);
2741 switch (command)
2743 case CK_PanelChdirOtherPanel:
2744 chdir_other_panel (panel);
2745 break;
2746 case CK_PanelChdirToReadlink:
2747 chdir_to_readlink (panel);
2748 break;
2749 case CK_PanelCmdCopyLocal:
2750 copy_cmd_local ();
2751 break;
2752 case CK_PanelCmdDeleteLocal:
2753 delete_cmd_local ();
2754 break;
2755 case CK_PanelCmdDoEnter:
2756 do_enter (panel);
2757 break;
2758 case CK_PanelCmdViewSimple:
2759 view_simple_cmd ();
2760 break;
2761 case CK_PanelCmdEditNew:
2762 edit_cmd_new ();
2763 break;
2764 case CK_PanelCmdRenameLocal:
2765 rename_cmd_local ();
2766 break;
2767 case CK_PanelCmdReverseSelection:
2768 reverse_selection_cmd ();
2769 break;
2770 case CK_PanelCmdSelect:
2771 select_cmd ();
2772 break;
2773 case CK_PanelCmdUnselect:
2774 unselect_cmd ();
2775 break;
2776 case CK_PanelNextPage:
2777 next_page (panel);
2778 break;
2779 case CK_PanelPrevPage:
2780 prev_page (panel);
2781 break;
2782 case CK_PanelGotoChildDir:
2783 goto_child_dir (panel);
2784 break;
2785 case CK_PanelGotoParentDir:
2786 goto_parent_dir (panel);
2787 break;
2788 case CK_PanelDirectoryHistoryList:
2789 directory_history_list (panel);
2790 break;
2791 case CK_PanelDirectoryHistoryNext:
2792 directory_history_next (panel);
2793 break;
2794 case CK_PanelDirectoryHistoryPrev:
2795 directory_history_prev (panel);
2796 break;
2797 case CK_PanelGotoBottomFile:
2798 goto_bottom_file (panel);
2799 break;
2800 case CK_PanelGotoMiddleFile:
2801 goto_middle_file (panel);
2802 break;
2803 case CK_PanelGotoTopFile:
2804 goto_top_file (panel);
2805 break;
2806 case CK_PanelMarkFile:
2807 mark_file (panel);
2808 break;
2809 case CK_PanelMarkFileUp:
2810 mark_file_up (panel);
2811 break;
2812 case CK_PanelMarkFileDown:
2813 mark_file_down (panel);
2814 break;
2815 case CK_PanelSmartGotoParentDir:
2816 res = force_maybe_cd ();
2817 break;
2818 case CK_PanelMoveUp:
2819 move_up (panel);
2820 break;
2821 case CK_PanelMoveDown:
2822 move_down (panel);
2823 break;
2824 case CK_PanelMoveLeft:
2825 res = move_left (panel);
2826 break;
2827 case CK_PanelMoveRight:
2828 res = move_right (panel);
2829 break;
2830 case CK_PanelMoveEnd:
2831 move_end (panel);
2832 break;
2833 case CK_PanelMoveHome:
2834 move_home (panel);
2835 break;
2836 case CK_PanelSetPanelEncoding:
2837 panel_change_encoding (panel);
2838 break;
2839 case CK_PanelStartSearch:
2840 start_search (panel);
2841 break;
2842 case CK_PanelStopSearch:
2843 break;
2844 case CK_PanelSyncOtherPanel:
2845 sync_other_panel (panel);
2846 break;
2847 case CK_PanelSelectSortOrder:
2848 panel_select_sort_order (panel);
2849 break;
2850 case CK_PanelToggleSortOrderPrev:
2851 panel_toggle_sort_order_prev (panel);
2852 break;
2853 case CK_PanelToggleSortOrderNext:
2854 panel_toggle_sort_order_next (panel);
2855 break;
2856 case CK_PanelReverseSort:
2857 panel->sort_info.reverse = !panel->sort_info.reverse;
2858 panel_set_sort_order (panel, panel->sort_info.sort_field);
2859 break;
2860 case CK_PanelSortOrderByName:
2861 panel_set_sort_type_by_id (panel, "name");
2862 break;
2863 case CK_PanelSortOrderByExt:
2864 panel_set_sort_type_by_id (panel, "extension");
2865 break;
2866 case CK_PanelSortOrderBySize:
2867 panel_set_sort_type_by_id (panel, "size");
2868 break;
2869 case CK_PanelSortOrderByMTime:
2870 panel_set_sort_type_by_id (panel, "mtime");
2871 break;
2873 return res;
2876 /* --------------------------------------------------------------------------------------------- */
2878 static cb_ret_t
2879 panel_key (WPanel * panel, int key)
2881 size_t i;
2883 for (i = 0; panel_map[i].key != 0; i++)
2884 if (key == panel_map[i].key)
2885 return panel_execute_cmd (panel, panel_map[i].command);
2887 if (torben_fj_mode && key == ALT ('h'))
2889 goto_middle_file (panel);
2890 return MSG_HANDLED;
2893 if (is_abort_char (key))
2895 stop_search (panel);
2896 return MSG_HANDLED;
2899 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2900 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE)
2902 if (panel->searching)
2904 do_search (panel, key);
2905 return MSG_HANDLED;
2908 if (!command_prompt)
2910 start_search (panel);
2911 do_search (panel, key);
2912 return MSG_HANDLED;
2916 return MSG_NOT_HANDLED;
2919 /* --------------------------------------------------------------------------------------------- */
2921 static cb_ret_t
2922 panel_callback (Widget * w, widget_msg_t msg, int parm)
2924 WPanel *panel = (WPanel *) w;
2925 WButtonBar *bb;
2927 switch (msg)
2929 case WIDGET_DRAW:
2930 paint_panel (panel);
2931 return MSG_HANDLED;
2933 case WIDGET_FOCUS:
2934 current_panel = panel;
2935 panel->active = 1;
2936 if (mc_chdir (panel->cwd) != 0)
2938 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2939 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
2940 cwd, unix_error_string (errno));
2941 g_free (cwd);
2943 else
2944 subshell_chdir (panel->cwd);
2946 update_xterm_title_path ();
2947 select_item (panel);
2948 show_dir (panel);
2949 paint_dir (panel);
2950 panel->dirty = 0;
2952 bb = find_buttonbar (panel->widget.owner);
2953 midnight_set_buttonbar (bb);
2954 buttonbar_redraw (bb);
2955 return MSG_HANDLED;
2957 case WIDGET_UNFOCUS:
2958 /* Janne: look at this for the multiple panel options */
2959 stop_search (panel);
2960 panel->active = 0;
2961 show_dir (panel);
2962 unselect_item (panel);
2963 return MSG_HANDLED;
2965 case WIDGET_KEY:
2966 return panel_key (panel, parm);
2968 case WIDGET_COMMAND:
2969 return panel_execute_cmd (panel, parm);
2971 case WIDGET_DESTROY:
2972 panel_destroy (panel);
2973 free_my_statfs ();
2974 return MSG_HANDLED;
2976 default:
2977 return default_proc (msg, parm);
2981 /* --------------------------------------------------------------------------------------------- */
2982 /* */
2983 /* Panel mouse events support routines */
2984 /* */
2986 static void
2987 mouse_toggle_mark (WPanel * panel)
2989 do_mark_file (panel, MARK_DONT_MOVE);
2990 mouse_marking = selection (panel)->f.marked;
2993 /* --------------------------------------------------------------------------------------------- */
2995 static void
2996 mouse_set_mark (WPanel * panel)
2998 if (mouse_marking && !(selection (panel)->f.marked))
2999 do_mark_file (panel, MARK_DONT_MOVE);
3000 else if (!mouse_marking && (selection (panel)->f.marked))
3001 do_mark_file (panel, MARK_DONT_MOVE);
3004 /* --------------------------------------------------------------------------------------------- */
3006 static int
3007 mark_if_marking (WPanel * panel, Gpm_Event * event)
3009 if (event->buttons & GPM_B_RIGHT)
3011 if (event->type & GPM_DOWN)
3012 mouse_toggle_mark (panel);
3013 else
3014 mouse_set_mark (panel);
3015 return 1;
3017 return 0;
3020 /* --------------------------------------------------------------------------------------------- */
3021 /** Determine which column was clicked, and sort the panel on
3022 * that column, or reverse sort on that column if already
3023 * sorted on that column.
3026 static void
3027 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3029 int i;
3030 const char *lc_sort_name = NULL;
3031 panel_field_t *col_sort_format = NULL;
3032 format_e *format;
3033 gchar *title;
3035 for (i = 0, format = panel->format; format != NULL; format = format->next)
3037 i += format->field_len;
3038 if (event->x < i + 1)
3040 /* found column */
3041 lc_sort_name = format->title;
3042 break;
3046 if (lc_sort_name == NULL)
3047 return;
3049 for (i = 0; panel_fields[i].id != NULL; i++)
3051 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3052 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3054 col_sort_format = &panel_fields[i];
3055 g_free (title);
3056 break;
3058 g_free (title);
3061 if (col_sort_format == NULL)
3062 return;
3064 if (panel->sort_info.sort_field == col_sort_format)
3066 /* reverse the sort if clicked column is already the sorted column */
3067 panel->sort_info.reverse = !panel->sort_info.reverse;
3069 else
3071 /* new sort is forced to be ascending */
3072 panel->sort_info.reverse = FALSE;
3074 panel_set_sort_order (panel, col_sort_format);
3078 /* --------------------------------------------------------------------------------------------- */
3080 * Mouse callback of the panel minus repainting.
3081 * If the event is redirected to the menu, *redir is set to TRUE.
3083 static int
3084 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3086 const int lines = llines (panel);
3087 const gboolean is_active = dlg_widget_active (panel);
3088 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3090 *redir = FALSE;
3092 /* 1st line */
3093 if (mouse_down && event->y == 1)
3095 /* "<" button */
3096 if (event->x == 2)
3098 directory_history_prev (panel);
3099 return MOU_NORMAL;
3102 /* "." button show/hide hidden files */
3103 if (event->x == panel->widget.cols - 5)
3105 panel->widget.owner->callback (panel->widget.owner, NULL,
3106 DLG_ACTION, CK_ToggleShowHidden, NULL);
3107 repaint_screen ();
3108 return MOU_NORMAL;
3111 /* ">" button */
3112 if (event->x == panel->widget.cols - 1)
3114 directory_history_next (panel);
3115 return MOU_NORMAL;
3118 /* "^" button */
3119 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3121 directory_history_list (panel);
3122 return MOU_NORMAL;
3125 /* rest of the upper frame, the menu is invisible - call menu */
3126 if (!menubar_visible)
3128 *redir = TRUE;
3129 event->x += panel->widget.x;
3130 return the_menubar->widget.mouse (event, the_menubar);
3133 /* no other events on 1st line */
3134 return MOU_NORMAL;
3137 /* sort on clicked column; don't handle wheel events */
3138 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3140 mouse_sort_col (event, panel);
3141 return MOU_NORMAL;
3144 /* Mouse wheel events */
3145 if (mouse_down && (event->buttons & GPM_B_UP))
3147 if (is_active)
3149 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3150 prev_page (panel);
3151 else /* We are in first page */
3152 move_up (panel);
3154 return MOU_NORMAL;
3157 if (mouse_down && (event->buttons & GPM_B_DOWN))
3159 if (is_active)
3161 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3162 next_page (panel);
3163 else /* We are in last page */
3164 move_down (panel);
3166 return MOU_NORMAL;
3169 event->y -= 2;
3170 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3172 int my_index;
3174 if (!is_active)
3175 change_panel ();
3177 if (panel->top_file + event->y > panel->count)
3178 my_index = panel->count - 1;
3179 else
3181 my_index = panel->top_file + event->y - 1;
3182 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3183 my_index += llines (panel);
3185 if (my_index >= panel->count)
3186 my_index = panel->count - 1;
3189 if (my_index != panel->selected)
3191 unselect_item (panel);
3192 panel->selected = my_index;
3193 select_item (panel);
3196 /* This one is new */
3197 mark_if_marking (panel, event);
3199 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3201 if (event->y > 0 && event->y <= lines)
3202 do_enter (panel);
3204 return MOU_NORMAL;
3207 /* --------------------------------------------------------------------------------------------- */
3208 /** Mouse callback of the panel */
3210 static int
3211 panel_event (Gpm_Event * event, void *data)
3213 WPanel *panel = data;
3214 int ret;
3215 gboolean redir;
3217 ret = do_panel_event (event, panel, &redir);
3218 if (!redir)
3219 paint_panel (panel);
3221 return ret;
3224 /* --------------------------------------------------------------------------------------------- */
3226 static void
3227 reload_panelized (WPanel * panel)
3229 int i, j;
3230 dir_list *list = &panel->dir;
3232 if (panel != current_panel)
3234 int ret;
3235 ret = mc_chdir (panel->cwd);
3238 for (i = 0, j = 0; i < panel->count; i++)
3240 if (list->list[i].f.marked)
3242 /* Unmark the file in advance. In case the following mc_lstat
3243 * fails we are done, else we have to mark the file again
3244 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3245 * IMO that's the best way to update the panel's summary status
3246 * -- Norbert
3248 do_file_mark (panel, i, 0);
3250 if (mc_lstat (list->list[i].fname, &list->list[i].st))
3252 g_free (list->list[i].fname);
3253 continue;
3255 if (list->list[i].f.marked)
3256 do_file_mark (panel, i, 1);
3257 if (j != i)
3258 list->list[j] = list->list[i];
3259 j++;
3261 if (j == 0)
3262 panel->count = set_zero_dir (list) ? 1 : 0;
3263 else
3264 panel->count = j;
3266 if (panel != current_panel)
3268 int ret;
3269 ret = mc_chdir (current_panel->cwd);
3273 /* --------------------------------------------------------------------------------------------- */
3275 static void
3276 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3278 gboolean free_pointer;
3279 char *my_current_file = NULL;
3281 if ((flags & UP_RELOAD) != 0)
3283 panel->is_panelized = 0;
3284 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
3285 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3288 /* If current_file == -1 (an invalid pointer) then preserve selection */
3289 free_pointer = current_file == UP_KEEPSEL;
3291 if (free_pointer)
3293 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3294 current_file = my_current_file;
3297 if (panel->is_panelized)
3298 reload_panelized (panel);
3299 else
3300 panel_reload (panel);
3302 try_to_select (panel, current_file);
3303 panel->dirty = 1;
3305 if (free_pointer)
3306 g_free (my_current_file);
3309 /* --------------------------------------------------------------------------------------------- */
3311 static void
3312 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3314 if (get_display_type (which) == view_listing)
3316 WPanel *panel;
3317 panel = (WPanel *) get_panel_widget (which);
3318 update_one_panel_widget (panel, flags, current_file);
3322 /* --------------------------------------------------------------------------------------------- */
3323 /*** public functions ****************************************************************************/
3324 /* --------------------------------------------------------------------------------------------- */
3326 char *
3327 remove_encoding_from_path (const char *path)
3329 GString *ret;
3330 GString *tmp_path, *tmp_conv;
3331 char *tmp;
3333 ret = g_string_new ("");
3334 tmp_conv = g_string_new ("");
3335 tmp_path = g_string_new (path);
3337 while ((tmp = g_strrstr (tmp_path->str, PATH_SEP_STR VFS_ENCODING_PREFIX)) != NULL)
3339 const char *enc;
3340 GIConv converter;
3341 char *tmp2;
3343 enc = vfs_get_encoding ((const char *) tmp);
3344 converter = enc != NULL ? str_crt_conv_to (enc) : str_cnv_to_term;
3345 if (converter == INVALID_CONV)
3346 converter = str_cnv_to_term;
3348 tmp2 = tmp + 1;
3349 while (*tmp2 != '\0' && *tmp2 != PATH_SEP)
3350 tmp2++;
3352 if (*tmp2 != '\0')
3354 str_vfs_convert_from (converter, tmp2, tmp_conv);
3355 g_string_prepend (ret, tmp_conv->str);
3356 g_string_set_size (tmp_conv, 0);
3359 g_string_set_size (tmp_path, tmp - tmp_path->str);
3360 str_close_conv (converter);
3363 g_string_prepend (ret, tmp_path->str);
3364 g_string_free (tmp_path, TRUE);
3365 g_string_free (tmp_conv, TRUE);
3367 return g_string_free (ret, FALSE);
3370 /* --------------------------------------------------------------------------------------------- */
3372 * Repaint the contents of the panels without frames. To schedule panel
3373 * for repainting, set panel->dirty to 1. There are many reasons why
3374 * the panels need to be repainted, and this is a costly operation, so
3375 * it's done once per event.
3378 void
3379 update_dirty_panels (void)
3381 if (current_panel->dirty)
3382 paint_panel (current_panel);
3384 if ((get_other_type () == view_listing) && other_panel->dirty)
3385 paint_panel (other_panel);
3388 /* --------------------------------------------------------------------------------------------- */
3390 static void
3391 do_select (WPanel * panel, int i)
3393 if (i != panel->selected)
3395 panel->dirty = 1;
3396 panel->selected = i;
3397 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3398 if (panel->top_file < 0)
3399 panel->top_file = 0;
3403 /* --------------------------------------------------------------------------------------------- */
3405 static void
3406 do_try_to_select (WPanel * panel, const char *name)
3408 int i;
3409 char *subdir;
3411 if (!name)
3413 do_select (panel, 0);
3414 return;
3417 /* We only want the last component of the directory,
3418 * and from this only the name without suffix. */
3419 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3421 /* Search that subdirectory, if found select it */
3422 for (i = 0; i < panel->count; i++)
3424 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3426 do_select (panel, i);
3427 g_free (subdir);
3428 return;
3432 /* Try to select a file near the file that is missing */
3433 if (panel->selected >= panel->count)
3434 do_select (panel, panel->count - 1);
3435 g_free (subdir);
3438 /* --------------------------------------------------------------------------------------------- */
3440 void
3441 try_to_select (WPanel * panel, const char *name)
3443 do_try_to_select (panel, name);
3444 select_item (panel);
3447 /* --------------------------------------------------------------------------------------------- */
3449 void
3450 panel_update_cols (Widget * widget, int frame_size)
3452 int cols, origin;
3454 /* don't touch panel if it is not in dialog yet */
3455 /* if panel is not in dialog it is not in widgets list
3456 and cannot be compared with get_panel_widget() result */
3457 if (widget->owner == NULL)
3458 return;
3460 if (horizontal_split)
3462 widget->cols = COLS;
3463 return;
3466 if (frame_size == frame_full)
3468 cols = COLS;
3469 origin = 0;
3471 else
3473 if (widget == get_panel_widget (0))
3475 cols = first_panel_size;
3476 origin = 0;
3478 else
3480 cols = COLS - first_panel_size;
3481 origin = first_panel_size;
3485 widget->cols = cols;
3486 widget->x = origin;
3489 /* --------------------------------------------------------------------------------------------- */
3491 void
3492 panel_clean_dir (WPanel * panel)
3494 int count = panel->count;
3496 panel->count = 0;
3497 panel->top_file = 0;
3498 panel->selected = 0;
3499 panel->marked = 0;
3500 panel->dirs_marked = 0;
3501 panel->total = 0;
3502 panel->searching = FALSE;
3503 panel->is_panelized = 0;
3504 panel->dirty = 1;
3506 clean_dir (&panel->dir, count);
3509 /* --------------------------------------------------------------------------------------------- */
3510 /** Panel creation.
3511 * @param panel_name the name of the panel for setup retieving
3512 * @returns new instance of WPanel
3515 WPanel *
3516 panel_new (const char *panel_name)
3518 return panel_new_with_dir (panel_name, NULL);
3521 /* --------------------------------------------------------------------------------------------- */
3522 /** Panel creation for specified directory.
3523 * @param panel_name specifies the name of the panel for setup retieving
3524 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3525 * @returns new instance of WPanel
3528 WPanel *
3529 panel_new_with_dir (const char *panel_name, const char *wpath)
3531 WPanel *panel;
3532 char *section;
3533 int i, err;
3534 char curdir[MC_MAXPATHLEN] = "\0";
3536 panel = g_new0 (WPanel, 1);
3538 /* No know sizes of the panel at startup */
3539 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3541 /* We do not want the cursor */
3542 widget_want_cursor (panel->widget, 0);
3544 if (wpath != NULL)
3546 g_strlcpy (panel->cwd, wpath, sizeof (panel->cwd));
3547 mc_get_current_wd (curdir, sizeof (curdir) - 2);
3549 else
3550 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3552 strcpy (panel->lwd, ".");
3554 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3555 panel->dir_history = history_get (panel->hist_name);
3556 directory_history_add (panel, panel->cwd);
3558 panel->dir.list = g_new (file_entry, MIN_FILES);
3559 panel->dir.size = MIN_FILES;
3560 panel->active = 0;
3561 panel->filter = 0;
3562 panel->split = 0;
3563 panel->top_file = 0;
3564 panel->selected = 0;
3565 panel->marked = 0;
3566 panel->total = 0;
3567 panel->dirty = 1;
3568 panel->searching = FALSE;
3569 panel->dirs_marked = 0;
3570 panel->is_panelized = 0;
3571 panel->format = 0;
3572 panel->status_format = 0;
3573 panel->format_modified = 1;
3575 panel->panel_name = g_strdup (panel_name);
3576 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3578 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3580 for (i = 0; i < LIST_TYPES; i++)
3581 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3583 panel->search_buffer[0] = '\0';
3584 panel->prev_search_buffer[0] = '\0';
3585 panel->frame_size = frame_half;
3587 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3588 if (!mc_config_has_group (mc_main_config, section))
3590 g_free (section);
3591 section = g_strdup (panel->panel_name);
3593 panel_load_setup (panel, section);
3594 g_free (section);
3596 /* Load format strings */
3597 err = set_panel_formats (panel);
3598 if (err != 0)
3599 set_panel_formats (panel);
3601 #ifdef HAVE_CHARSET
3603 const char *enc = vfs_get_encoding (panel->cwd);
3604 if (enc != NULL)
3605 panel->codepage = get_codepage_index (enc);
3607 #endif
3609 if (mc_chdir (panel->cwd) != 0)
3611 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3612 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3615 /* Load the default format */
3616 panel->count =
3617 do_load_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
3618 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3619 panel->sort_info.exec_first, panel->filter);
3621 /* Restore old right path */
3622 if (curdir[0] != '\0')
3623 err = mc_chdir (curdir);
3625 return panel;
3628 /* --------------------------------------------------------------------------------------------- */
3630 void
3631 panel_reload (WPanel * panel)
3633 struct stat current_stat;
3635 if (panels_options.fast_reload && !stat (panel->cwd, &current_stat)
3636 && current_stat.st_ctime == panel->dir_stat.st_ctime
3637 && current_stat.st_mtime == panel->dir_stat.st_mtime)
3638 return;
3640 while (mc_chdir (panel->cwd) == -1)
3642 char *last_slash;
3644 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0)
3646 panel_clean_dir (panel);
3647 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
3648 return;
3650 last_slash = strrchr (panel->cwd, PATH_SEP);
3651 if (!last_slash || last_slash == panel->cwd)
3652 strcpy (panel->cwd, PATH_SEP_STR);
3653 else
3654 *last_slash = 0;
3655 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3656 show_dir (panel);
3659 panel->count =
3660 do_reload_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
3661 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
3662 panel->sort_info.exec_first, panel->filter);
3664 panel->dirty = 1;
3665 if (panel->selected >= panel->count)
3666 do_select (panel, panel->count - 1);
3668 recalculate_panel_summary (panel);
3671 /* --------------------------------------------------------------------------------------------- */
3672 /* Switches the panel to the mode specified in the format */
3673 /* Seting up both format and status string. Return: 0 - on success; */
3674 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
3677 set_panel_formats (WPanel * p)
3679 format_e *form;
3680 char *err = NULL;
3681 int retcode = 0;
3683 form = use_display_format (p, panel_format (p), &err, 0);
3685 if (err != NULL)
3687 g_free (err);
3688 retcode = 1;
3690 else
3692 delete_format (p->format);
3693 p->format = form;
3696 if (show_mini_info)
3698 form = use_display_format (p, mini_status_format (p), &err, 1);
3700 if (err != NULL)
3702 g_free (err);
3703 retcode += 2;
3705 else
3707 delete_format (p->status_format);
3708 p->status_format = form;
3712 panel_format_modified (p);
3713 panel_update_cols (&(p->widget), p->frame_size);
3715 if (retcode)
3716 message (D_ERROR, _("Warning"),
3717 _("User supplied format looks invalid, reverting to default."));
3718 if (retcode & 0x01)
3720 g_free (p->user_format);
3721 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
3723 if (retcode & 0x02)
3725 g_free (p->user_status_format[p->list_type]);
3726 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
3729 return retcode;
3732 /* --------------------------------------------------------------------------------------------- */
3734 /* Select current item and readjust the panel */
3735 void
3736 select_item (WPanel * panel)
3738 int items = ITEMS (panel);
3740 /* Although currently all over the code we set the selection and
3741 top file to decent values before calling select_item, I could
3742 forget it someday, so it's better to do the actual fitting here */
3744 if (panel->top_file < 0)
3745 panel->top_file = 0;
3747 if (panel->selected < 0)
3748 panel->selected = 0;
3750 if (panel->selected > panel->count - 1)
3751 panel->selected = panel->count - 1;
3753 if (panel->top_file > panel->count - 1)
3754 panel->top_file = panel->count - 1;
3756 if ((panel->count - panel->top_file) < items)
3758 panel->top_file = panel->count - items;
3759 if (panel->top_file < 0)
3760 panel->top_file = 0;
3763 if (panel->selected < panel->top_file)
3764 panel->top_file = panel->selected;
3766 if ((panel->selected - panel->top_file) >= items)
3767 panel->top_file = panel->selected - items + 1;
3769 panel->dirty = 1;
3771 execute_hooks (select_file_hook);
3774 /* --------------------------------------------------------------------------------------------- */
3775 /** Clears all files in the panel, used only when one file was marked */
3776 void
3777 unmark_files (WPanel * panel)
3779 int i;
3781 if (!panel->marked)
3782 return;
3783 for (i = 0; i < panel->count; i++)
3784 file_mark (panel, i, 0);
3786 panel->dirs_marked = 0;
3787 panel->marked = 0;
3788 panel->total = 0;
3791 /* --------------------------------------------------------------------------------------------- */
3792 /** Recalculate the panels summary information, used e.g. when marked
3793 files might have been removed by an external command */
3795 void
3796 recalculate_panel_summary (WPanel * panel)
3798 int i;
3800 panel->marked = 0;
3801 panel->dirs_marked = 0;
3802 panel->total = 0;
3804 for (i = 0; i < panel->count; i++)
3805 if (panel->dir.list[i].f.marked)
3807 /* do_file_mark will return immediately if newmark == oldmark.
3808 So we have to first unmark it to get panel's summary information
3809 updated. (Norbert) */
3810 panel->dir.list[i].f.marked = 0;
3811 do_file_mark (panel, i, 1);
3815 /* --------------------------------------------------------------------------------------------- */
3816 /** This routine marks a file or a directory */
3818 void
3819 do_file_mark (WPanel * panel, int idx, int mark)
3821 if (panel->dir.list[idx].f.marked == mark)
3822 return;
3824 /* Only '..' can't be marked, '.' isn't visible */
3825 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
3826 return;
3828 file_mark (panel, idx, mark);
3829 if (panel->dir.list[idx].f.marked)
3831 panel->marked++;
3832 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
3834 if (panel->dir.list[idx].f.dir_size_computed)
3835 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
3836 panel->dirs_marked++;
3838 else
3839 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
3840 set_colors (panel);
3842 else
3844 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
3846 if (panel->dir.list[idx].f.dir_size_computed)
3847 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
3848 panel->dirs_marked--;
3850 else
3851 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
3852 panel->marked--;
3856 /* --------------------------------------------------------------------------------------------- */
3858 * Changes the current directory of the panel.
3859 * Record change in the directory history.
3861 gboolean
3862 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
3864 gboolean r;
3866 r = _do_panel_cd (panel, new_dir, cd_type);
3867 if (r)
3868 directory_history_add (panel, panel->cwd);
3869 return r;
3872 /* --------------------------------------------------------------------------------------------- */
3874 void
3875 file_mark (WPanel * panel, int lc_index, int val)
3877 if (panel->dir.list[lc_index].f.marked != val)
3879 panel->dir.list[lc_index].f.marked = val;
3880 panel->dirty = 1;
3884 /* --------------------------------------------------------------------------------------------- */
3886 void
3887 panel_re_sort (WPanel * panel)
3889 char *filename;
3890 int i;
3892 if (panel == NULL)
3893 return;
3895 filename = g_strdup (selection (panel)->fname);
3896 unselect_item (panel);
3897 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
3898 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3899 panel->sort_info.exec_first);
3900 panel->selected = -1;
3901 for (i = panel->count; i; i--)
3903 if (!strcmp (panel->dir.list[i - 1].fname, filename))
3905 panel->selected = i - 1;
3906 break;
3909 g_free (filename);
3910 panel->top_file = panel->selected - ITEMS (panel) / 2;
3911 select_item (panel);
3912 panel->dirty = 1;
3915 /* --------------------------------------------------------------------------------------------- */
3917 void
3918 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
3920 if (sort_order == NULL)
3921 return;
3923 panel->sort_info.sort_field = sort_order;
3925 /* The directory is already sorted, we have to load the unsorted stuff */
3926 if (sort_order->sort_routine == (sortfn *) unsorted)
3928 char *current_file;
3930 current_file = g_strdup (panel->dir.list[panel->selected].fname);
3931 panel_reload (panel);
3932 try_to_select (panel, current_file);
3933 g_free (current_file);
3935 panel_re_sort (panel);
3938 /* --------------------------------------------------------------------------------------------- */
3940 * Change panel encoding.
3941 * @param panel WPanel object
3944 void
3945 panel_change_encoding (WPanel * panel)
3947 const char *encoding = NULL;
3948 char *cd_path;
3949 #ifdef HAVE_CHARSET
3950 char *errmsg;
3951 int r;
3953 r = select_charset (-1, -1, panel->codepage, FALSE);
3955 if (r == SELECT_CHARSET_CANCEL)
3956 return; /* Cancel */
3958 panel->codepage = r;
3960 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
3962 /* No translation */
3963 g_free (init_translation_table (display_codepage, display_codepage));
3964 cd_path = remove_encoding_from_path (panel->cwd);
3965 do_panel_cd (panel, cd_path, cd_parse_command);
3966 g_free (cd_path);
3967 return;
3970 errmsg = init_translation_table (panel->codepage, display_codepage);
3971 if (errmsg != NULL)
3973 message (D_ERROR, MSG_ERROR, "%s", errmsg);
3974 g_free (errmsg);
3975 return;
3978 encoding = get_codepage_id (panel->codepage);
3979 #endif
3980 if (encoding != NULL)
3982 const char *enc;
3984 enc = vfs_get_encoding (panel->cwd);
3986 /* don't add current encoding */
3987 if ((enc == NULL) || (strcmp (encoding, enc) != 0))
3989 cd_path = add_encoding_to_path (panel->cwd, encoding);
3990 if (!do_panel_cd (panel, cd_path, cd_parse_command))
3991 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
3992 g_free (cd_path);
3997 /* --------------------------------------------------------------------------------------------- */
3999 * This routine reloads the directory in both panels. It tries to
4000 * select current_file in current_panel and other_file in other_panel.
4001 * If current_file == -1 then it automatically sets current_file and
4002 * other_file to the currently selected files in the panels.
4004 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4005 * will not reload the other panel.
4008 void
4009 update_panels (panel_update_flags_t flags, const char *current_file)
4011 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4012 WPanel *panel;
4013 int ret;
4015 update_one_panel (get_current_index (), flags, current_file);
4016 if (reload_other)
4017 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4019 if (get_current_type () == view_listing)
4020 panel = (WPanel *) get_panel_widget (get_current_index ());
4021 else
4022 panel = (WPanel *) get_panel_widget (get_other_index ());
4024 ret = mc_chdir (panel->cwd);
4027 /* --------------------------------------------------------------------------------------------- */
4029 void
4030 directory_history_add (struct WPanel *panel, const char *dir)
4032 char *tmp;
4034 tmp = g_strdup (dir);
4035 strip_password (tmp, 1);
4037 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4040 /* --------------------------------------------------------------------------------------------- */
4042 gsize
4043 panel_get_num_of_sortable_fields (void)
4045 gsize ret = 0, lc_index;
4047 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4048 if (panel_fields[lc_index].is_user_choice)
4049 ret++;
4050 return ret;
4053 /* --------------------------------------------------------------------------------------------- */
4055 const char **
4056 panel_get_sortable_fields (gsize * array_size)
4058 char **ret;
4059 gsize lc_index, i;
4061 lc_index = panel_get_num_of_sortable_fields ();
4063 ret = g_try_new0 (char *, lc_index + 1);
4064 if (ret == NULL)
4065 return NULL;
4067 if (array_size != NULL)
4068 *array_size = lc_index;
4070 lc_index = 0;
4072 for (i = 0; panel_fields[i].id != NULL; i++)
4073 if (panel_fields[i].is_user_choice)
4074 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4075 return (const char **) ret;
4078 /* --------------------------------------------------------------------------------------------- */
4080 const panel_field_t *
4081 panel_get_field_by_id (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].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4086 return &panel_fields[lc_index];
4087 return NULL;
4090 /* --------------------------------------------------------------------------------------------- */
4092 const panel_field_t *
4093 panel_get_field_by_title_hotkey (const char *name)
4095 gsize lc_index;
4096 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4097 if (panel_fields[lc_index].title_hotkey != NULL &&
4098 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4099 return &panel_fields[lc_index];
4100 return NULL;
4103 /* --------------------------------------------------------------------------------------------- */
4105 const panel_field_t *
4106 panel_get_field_by_title (const char *name)
4108 gsize lc_index;
4109 gchar *title = NULL;
4111 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4113 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4114 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4116 g_free (title);
4117 return &panel_fields[lc_index];
4120 g_free (title);
4121 return NULL;
4124 /* --------------------------------------------------------------------------------------------- */
4126 gsize
4127 panel_get_num_of_user_possible_fields (void)
4129 gsize ret = 0, lc_index;
4131 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4132 if (panel_fields[lc_index].use_in_user_format)
4133 ret++;
4134 return ret;
4137 /* --------------------------------------------------------------------------------------------- */
4139 const char **
4140 panel_get_user_possible_fields (gsize * array_size)
4142 char **ret;
4143 gsize lc_index, i;
4145 lc_index = panel_get_num_of_user_possible_fields ();
4147 ret = g_try_new0 (char *, lc_index + 1);
4148 if (ret == NULL)
4149 return NULL;
4151 if (array_size != NULL)
4152 *array_size = lc_index;
4154 lc_index = 0;
4156 for (i = 0; panel_fields[i].id != NULL; i++)
4157 if (panel_fields[i].use_in_user_format)
4158 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4159 return (const char **) ret;
4162 /* --------------------------------------------------------------------------------------------- */
4164 void
4165 panel_init (void)
4167 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4168 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4170 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4171 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4172 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4173 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4174 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4178 /* --------------------------------------------------------------------------------------------- */
4180 void
4181 panel_deinit (void)
4183 g_free (panel_sort_up_sign);
4184 g_free (panel_sort_down_sign);
4186 g_free (panel_hiddenfiles_sign_show);
4187 g_free (panel_hiddenfiles_sign_hide);
4188 g_free (panel_history_prev_item_sign);
4189 g_free (panel_history_next_item_sign);
4190 g_free (panel_history_show_list_sign);
4194 /* --------------------------------------------------------------------------------------------- */