Merge branch '2499_mcedit_select_cur_word'
[midnight-commander/borarpet.git] / src / filemanager / panel.c
blob92f35dfbfcbd470c0bf8c4dcc01de8f757cd3442
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 /* The hook list for the select file function */
74 hook_t *select_file_hook = NULL;
76 static const char *string_file_name (file_entry *, int);
77 static const char *string_file_size (file_entry *, int);
78 static const char *string_file_size_brief (file_entry *, int);
79 static const char *string_file_type (file_entry *, int);
80 static const char *string_file_mtime (file_entry *, int);
81 static const char *string_file_atime (file_entry *, int);
82 static const char *string_file_ctime (file_entry *, int);
83 static const char *string_file_permission (file_entry *, int);
84 static const char *string_file_perm_octal (file_entry *, int);
85 static const char *string_file_nlinks (file_entry *, int);
86 static const char *string_inode (file_entry *, int);
87 static const char *string_file_nuid (file_entry *, int);
88 static const char *string_file_ngid (file_entry *, int);
89 static const char *string_file_owner (file_entry *, int);
90 static const char *string_file_group (file_entry *, int);
91 static const char *string_marked (file_entry *, int);
92 static const char *string_space (file_entry *, int);
93 static const char *string_dot (file_entry *, int);
95 /* *INDENT-OFF* */
96 panel_field_t panel_fields[] = {
98 "unsorted", 12, 1, J_LEFT_FIT,
99 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
100 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
101 N_("sort|u"),
102 N_("&Unsorted"), TRUE, FALSE,
103 string_file_name,
104 (sortfn *) unsorted
108 "name", 12, 1, J_LEFT_FIT,
109 /* TRANSLATORS: one single character to represent 'name' sort mode */
110 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
111 N_("sort|n"),
112 N_("&Name"), TRUE, TRUE,
113 string_file_name,
114 (sortfn *) sort_name
118 "version", 12, 1, J_LEFT_FIT,
119 /* TRANSLATORS: one single character to represent 'version' sort mode */
120 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
121 N_("sort|v"),
122 N_("&Version"), TRUE, FALSE,
123 string_file_name,
124 (sortfn *) sort_vers
128 "extension", 12, 1, J_LEFT_FIT,
129 /* TRANSLATORS: one single character to represent 'extension' sort mode */
130 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
131 N_("sort|e"),
132 N_("&Extension"), TRUE, FALSE,
133 string_file_name, /* TODO: string_file_ext */
134 (sortfn *) sort_ext
138 "size", 7, 0, J_RIGHT,
139 /* TRANSLATORS: one single character to represent 'size' sort mode */
140 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
141 N_("sort|s"),
142 N_("&Size"), TRUE, TRUE,
143 string_file_size,
144 (sortfn *) sort_size
148 "bsize", 7, 0, J_RIGHT,
150 N_("Block Size"), FALSE, FALSE,
151 string_file_size_brief,
152 (sortfn *) sort_size
156 "type", 1, 0, J_LEFT,
158 "", FALSE, TRUE,
159 string_file_type,
160 NULL
164 "mtime", 12, 0, J_RIGHT,
165 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
166 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
167 N_("sort|m"),
168 N_("&Modify time"), TRUE, TRUE,
169 string_file_mtime,
170 (sortfn *) sort_time
174 "atime", 12, 0, J_RIGHT,
175 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
176 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
177 N_("sort|a"),
178 N_("&Access time"), TRUE, TRUE,
179 string_file_atime,
180 (sortfn *) sort_atime
184 "ctime", 12, 0, J_RIGHT,
185 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
186 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
187 N_("sort|h"),
188 N_("C&hange time"), TRUE, TRUE,
189 string_file_ctime,
190 (sortfn *) sort_ctime
194 "perm", 10, 0, J_LEFT,
196 N_("Permission"), FALSE, TRUE,
197 string_file_permission,
198 NULL
202 "mode", 6, 0, J_RIGHT,
204 N_("Perm"), FALSE, TRUE,
205 string_file_perm_octal,
206 NULL
210 "nlink", 2, 0, J_RIGHT,
212 N_("Nl"), FALSE, TRUE,
213 string_file_nlinks, NULL
217 "inode", 5, 0, J_RIGHT,
218 /* TRANSLATORS: one single character to represent 'inode' sort mode */
219 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
220 N_("sort|i"),
221 N_("&Inode"), TRUE, TRUE,
222 string_inode,
223 (sortfn *) sort_inode
227 "nuid", 5, 0, J_RIGHT,
229 N_("UID"), FALSE, FALSE,
230 string_file_nuid,
231 NULL
235 "ngid", 5, 0, J_RIGHT,
237 N_("GID"), FALSE, FALSE,
238 string_file_ngid,
239 NULL
243 "owner", 8, 0, J_LEFT_FIT,
245 N_("Owner"), FALSE, TRUE,
246 string_file_owner,
247 NULL
251 "group", 8, 0, J_LEFT_FIT,
253 N_("Group"), FALSE, TRUE,
254 string_file_group,
255 NULL
259 "mark", 1, 0, J_RIGHT,
261 " ", FALSE, TRUE,
262 string_marked,
263 NULL
267 "|", 1, 0, J_RIGHT,
269 " ", FALSE, TRUE,
270 NULL,
271 NULL
275 "space", 1, 0, J_RIGHT,
277 " ", FALSE, TRUE,
278 string_space,
279 NULL
283 "dot", 1, 0, J_RIGHT,
285 " ", FALSE, FALSE,
286 string_dot,
287 NULL
291 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
294 /* *INDENT-ON* */
296 extern int saving_setup;
298 /*** file scope macro definitions ****************************************************************/
300 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
302 #define NORMAL 0
303 #define SELECTED 1
304 #define MARKED 2
305 #define MARKED_SELECTED 3
306 #define STATUS 5
308 /* This macro extracts the number of available lines in a panel */
309 #define llines(p) (p->widget.lines - 3 - (panels_options.show_mini_info ? 2 : 0))
311 /*** file scope type declarations ****************************************************************/
313 typedef enum
315 MARK_DONT_MOVE = 0,
316 MARK_DOWN = 1,
317 MARK_FORCE_DOWN = 2,
318 MARK_FORCE_UP = 3
319 } mark_act_t;
322 * This describes a format item. The parse_display_format routine parses
323 * the user specified format and creates a linked list of format_e structures.
325 typedef struct format_e
327 struct format_e *next;
328 int requested_field_len;
329 int field_len;
330 align_crt_t just_mode;
331 int expand;
332 const char *(*string_fn) (file_entry *, int len);
333 char *title;
334 const char *id;
335 } format_e;
337 /*** file scope variables ************************************************************************/
339 static cb_ret_t panel_callback (Widget *, widget_msg_t msg, int parm);
340 static int panel_event (Gpm_Event * event, void *);
341 static void paint_frame (WPanel * panel);
342 static const char *panel_format (WPanel * panel);
343 static const char *mini_status_format (WPanel * panel);
345 static char *panel_sort_up_sign = NULL;
346 static char *panel_sort_down_sign = NULL;
348 static char *panel_hiddenfiles_sign_show = NULL;
349 static char *panel_hiddenfiles_sign_hide = NULL;
350 static char *panel_history_prev_item_sign = NULL;
351 static char *panel_history_next_item_sign = NULL;
352 static char *panel_history_show_list_sign = NULL;
354 static int mouse_marking = 0;
356 /*** file scope functions ************************************************************************/
357 /* --------------------------------------------------------------------------------------------- */
359 static void
360 set_colors (WPanel * panel)
362 (void) panel;
363 tty_set_normal_attrs ();
364 tty_setcolor (NORMAL_COLOR);
367 /* --------------------------------------------------------------------------------------------- */
368 /** Delete format string, it is a linked list */
370 static void
371 delete_format (format_e * format)
373 while (format != NULL)
375 format_e *next = format->next;
376 g_free (format->title);
377 g_free (format);
378 format = next;
382 /* --------------------------------------------------------------------------------------------- */
383 /** This code relies on the default justification!!! */
385 static void
386 add_permission_string (char *dest, int width, file_entry * fe, int attr, int color, int is_octal)
388 int i, r, l;
390 l = get_user_permissions (&fe->st);
392 if (is_octal)
394 /* Place of the access bit in octal mode */
395 l = width + l - 3;
396 r = l + 1;
398 else
400 /* The same to the triplet in string mode */
401 l = l * 3 + 1;
402 r = l + 3;
405 for (i = 0; i < width; i++)
407 if (i >= l && i < r)
409 if (attr == SELECTED || attr == MARKED_SELECTED)
410 tty_setcolor (MARKED_SELECTED_COLOR);
411 else
412 tty_setcolor (MARKED_COLOR);
414 else if (color >= 0)
415 tty_setcolor (color);
416 else
417 tty_lowlevel_setcolor (-color);
419 tty_print_char (dest[i]);
423 /* --------------------------------------------------------------------------------------------- */
424 /** String representations of various file attributes name */
426 static const char *
427 string_file_name (file_entry * fe, int len)
429 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
431 (void) len;
432 g_strlcpy (buffer, fe->fname, sizeof (buffer));
433 return buffer;
436 /* --------------------------------------------------------------------------------------------- */
438 static unsigned int
439 ilog10 (dev_t n)
441 unsigned int digits = 0;
444 digits++, n /= 10;
446 while (n != 0);
447 return digits;
450 /* --------------------------------------------------------------------------------------------- */
452 static void
453 format_device_number (char *buf, size_t bufsize, dev_t dev)
455 dev_t major_dev = major (dev);
456 dev_t minor_dev = minor (dev);
457 unsigned int major_digits = ilog10 (major_dev);
458 unsigned int minor_digits = ilog10 (minor_dev);
460 g_assert (bufsize >= 1);
461 if (major_digits + 1 + minor_digits + 1 <= bufsize)
463 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
465 else
467 g_strlcpy (buf, _("[dev]"), bufsize);
471 /* --------------------------------------------------------------------------------------------- */
472 /** size */
474 static const char *
475 string_file_size (file_entry * fe, int len)
477 static char buffer[BUF_TINY];
479 /* Don't ever show size of ".." since we don't calculate it */
480 if (!strcmp (fe->fname, ".."))
482 return _("UP--DIR");
485 #ifdef HAVE_STRUCT_STAT_ST_RDEV
486 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
487 format_device_number (buffer, len + 1, fe->st.st_rdev);
488 else
489 #endif
491 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
493 return buffer;
496 /* --------------------------------------------------------------------------------------------- */
497 /** bsize */
499 static const char *
500 string_file_size_brief (file_entry * fe, int len)
502 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
504 return _("SYMLINK");
507 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
509 return _("SUB-DIR");
512 return string_file_size (fe, len);
515 /* --------------------------------------------------------------------------------------------- */
516 /** This functions return a string representation of a file entry type */
518 static const char *
519 string_file_type (file_entry * fe, int len)
521 static char buffer[2];
523 (void) len;
524 if (S_ISDIR (fe->st.st_mode))
525 buffer[0] = PATH_SEP;
526 else if (S_ISLNK (fe->st.st_mode))
528 if (fe->f.link_to_dir)
529 buffer[0] = '~';
530 else if (fe->f.stale_link)
531 buffer[0] = '!';
532 else
533 buffer[0] = '@';
535 else if (S_ISCHR (fe->st.st_mode))
536 buffer[0] = '-';
537 else if (S_ISSOCK (fe->st.st_mode))
538 buffer[0] = '=';
539 else if (S_ISDOOR (fe->st.st_mode))
540 buffer[0] = '>';
541 else if (S_ISBLK (fe->st.st_mode))
542 buffer[0] = '+';
543 else if (S_ISFIFO (fe->st.st_mode))
544 buffer[0] = '|';
545 else if (S_ISNAM (fe->st.st_mode))
546 buffer[0] = '#';
547 else if (!S_ISREG (fe->st.st_mode))
548 buffer[0] = '?'; /* non-regular of unknown kind */
549 else if (is_exe (fe->st.st_mode))
550 buffer[0] = '*';
551 else
552 buffer[0] = ' ';
553 buffer[1] = '\0';
554 return buffer;
557 /* --------------------------------------------------------------------------------------------- */
558 /** mtime */
560 static const char *
561 string_file_mtime (file_entry * fe, int len)
563 (void) len;
564 return file_date (fe->st.st_mtime);
567 /* --------------------------------------------------------------------------------------------- */
568 /** atime */
570 static const char *
571 string_file_atime (file_entry * fe, int len)
573 (void) len;
574 return file_date (fe->st.st_atime);
577 /* --------------------------------------------------------------------------------------------- */
578 /** ctime */
580 static const char *
581 string_file_ctime (file_entry * fe, int len)
583 (void) len;
584 return file_date (fe->st.st_ctime);
587 /* --------------------------------------------------------------------------------------------- */
588 /** perm */
590 static const char *
591 string_file_permission (file_entry * fe, int len)
593 (void) len;
594 return string_perm (fe->st.st_mode);
597 /* --------------------------------------------------------------------------------------------- */
598 /** mode */
600 static const char *
601 string_file_perm_octal (file_entry * fe, int len)
603 static char buffer[10];
605 (void) len;
606 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
607 return buffer;
610 /* --------------------------------------------------------------------------------------------- */
611 /** nlink */
613 static const char *
614 string_file_nlinks (file_entry * fe, int len)
616 static char buffer[BUF_TINY];
618 (void) len;
619 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
620 return buffer;
623 /* --------------------------------------------------------------------------------------------- */
624 /** inode */
626 static const char *
627 string_inode (file_entry * fe, int len)
629 static char buffer[10];
631 (void) len;
632 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
633 return buffer;
636 /* --------------------------------------------------------------------------------------------- */
637 /** nuid */
639 static const char *
640 string_file_nuid (file_entry * fe, int len)
642 static char buffer[10];
644 (void) len;
645 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
646 return buffer;
649 /* --------------------------------------------------------------------------------------------- */
650 /** ngid */
652 static const char *
653 string_file_ngid (file_entry * fe, int len)
655 static char buffer[10];
657 (void) len;
658 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
659 return buffer;
662 /* --------------------------------------------------------------------------------------------- */
663 /** owner */
665 static const char *
666 string_file_owner (file_entry * fe, int len)
668 (void) len;
669 return get_owner (fe->st.st_uid);
672 /* --------------------------------------------------------------------------------------------- */
673 /** group */
675 static const char *
676 string_file_group (file_entry * fe, int len)
678 (void) len;
679 return get_group (fe->st.st_gid);
682 /* --------------------------------------------------------------------------------------------- */
683 /** mark */
685 static const char *
686 string_marked (file_entry * fe, int len)
688 (void) len;
689 return fe->f.marked ? "*" : " ";
692 /* --------------------------------------------------------------------------------------------- */
693 /** space */
695 static const char *
696 string_space (file_entry * fe, int len)
698 (void) fe;
699 (void) len;
700 return " ";
703 /* --------------------------------------------------------------------------------------------- */
704 /** dot */
706 static const char *
707 string_dot (file_entry * fe, int len)
709 (void) fe;
710 (void) len;
711 return ".";
714 /* --------------------------------------------------------------------------------------------- */
716 static int
717 file_compute_color (int attr, file_entry * fe)
719 switch (attr)
721 case SELECTED:
722 return (SELECTED_COLOR);
723 case MARKED:
724 return (MARKED_COLOR);
725 case MARKED_SELECTED:
726 return (MARKED_SELECTED_COLOR);
727 case STATUS:
728 return (NORMAL_COLOR);
729 case NORMAL:
730 default:
731 if (!panels_options.filetype_mode)
732 return (NORMAL_COLOR);
735 return mc_fhl_get_color (mc_filehighlight, fe);
738 /* --------------------------------------------------------------------------------------------- */
739 /** Formats the file number file_index of panel in the buffer dest */
741 static void
742 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
743 int isstatus)
745 int color, length, empty_line;
746 const char *txt;
747 format_e *format, *home;
748 file_entry *fe;
750 (void) dest;
751 (void) limit;
752 length = 0;
753 empty_line = (file_index >= panel->count);
754 home = (isstatus) ? panel->status_format : panel->format;
755 fe = &panel->dir.list[file_index];
757 if (!empty_line)
758 color = file_compute_color (attr, fe);
759 else
760 color = NORMAL_COLOR;
762 for (format = home; format; format = format->next)
764 if (length == width)
765 break;
767 if (format->string_fn)
769 int len, perm;
770 char *preperad_text;
772 if (empty_line)
773 txt = " ";
774 else
775 txt = (*format->string_fn) (fe, format->field_len);
777 len = format->field_len;
778 if (len + length > width)
779 len = width - length;
780 if (len <= 0)
781 break;
783 perm = 0;
784 if (panels_options.permission_mode)
786 if (!strcmp (format->id, "perm"))
787 perm = 1;
788 else if (!strcmp (format->id, "mode"))
789 perm = 2;
792 if (color >= 0)
793 tty_setcolor (color);
794 else
795 tty_lowlevel_setcolor (-color);
797 preperad_text = (char *) str_fit_to_term (txt, len, format->just_mode);
798 if (perm)
799 add_permission_string (preperad_text, format->field_len, fe, attr, color, perm - 1);
800 else
801 tty_print_string (preperad_text);
803 length += len;
805 else
807 if (attr == SELECTED || attr == MARKED_SELECTED)
808 tty_setcolor (SELECTED_COLOR);
809 else
810 tty_setcolor (NORMAL_COLOR);
811 tty_print_one_vline (TRUE);
812 length++;
816 if (length < width)
817 tty_draw_hline (-1, -1, ' ', width - length);
820 /* --------------------------------------------------------------------------------------------- */
822 static void
823 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
825 int second_column = 0;
826 int width;
827 int offset = 0;
828 char buffer[BUF_MEDIUM];
830 gboolean panel_is_split = !isstatus && panel->split;
832 width = panel->widget.cols - 2;
834 if (panel_is_split)
836 second_column = (file_index - panel->top_file) / llines (panel);
837 width = width / 2 - 1;
839 if (second_column != 0)
841 offset = 1 + width;
842 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
843 width = panel->widget.cols - offset - 2;
847 /* Nothing to paint */
848 if (width <= 0)
849 return;
851 if (mv)
853 if (panel_is_split)
854 widget_move (&panel->widget,
855 (file_index - panel->top_file) % llines (panel) + 2, offset + 1);
856 else
857 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
860 format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus);
862 if (panel_is_split)
864 if (second_column)
865 tty_print_char (' ');
866 else
868 tty_setcolor (NORMAL_COLOR);
869 tty_print_one_vline (TRUE);
874 /* --------------------------------------------------------------------------------------------- */
876 static void
877 display_mini_info (WPanel * panel)
879 if (!panels_options.show_mini_info)
880 return;
882 widget_move (&panel->widget, llines (panel) + 3, 1);
884 if (panel->searching)
886 tty_setcolor (INPUT_COLOR);
887 tty_print_char ('/');
888 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
889 return;
892 /* Status resolves links and show them */
893 set_colors (panel);
895 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
897 char *lc_link, link_target[MC_MAXPATHLEN];
898 int len;
900 lc_link = concat_dir_and_file (panel->cwd, panel->dir.list[panel->selected].fname);
901 len = mc_readlink (lc_link, link_target, MC_MAXPATHLEN - 1);
902 g_free (lc_link);
903 if (len > 0)
905 link_target[len] = 0;
906 tty_print_string ("-> ");
907 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
909 else
910 tty_print_string (str_fit_to_term (_("<readlink failed>"),
911 panel->widget.cols - 2, J_LEFT));
913 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
915 /* FIXME:
916 * while loading directory (do_load_dir() and do_reload_dir()),
917 * the actual stat info about ".." directory isn't got;
918 * so just don't display incorrect info about ".." directory */
919 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
921 else
922 /* Default behavior */
923 repaint_file (panel, panel->selected, 0, STATUS, 1);
926 /* --------------------------------------------------------------------------------------------- */
928 static void
929 paint_dir (WPanel * panel)
931 int i;
932 int color; /* Color value of the line */
933 int items; /* Number of items */
935 items = llines (panel) * (panel->split ? 2 : 1);
937 for (i = 0; i < items; i++)
939 if (i + panel->top_file >= panel->count)
940 color = 0;
941 else
943 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
944 color += (panel->selected == i + panel->top_file && panel->active);
946 repaint_file (panel, i + panel->top_file, 1, color, 0);
948 tty_set_normal_attrs ();
951 /* --------------------------------------------------------------------------------------------- */
953 static void
954 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
956 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
957 int cols;
959 if (panel->marked <= 0)
960 return;
962 buf = size_only ? b_bytes : buffer;
963 cols = panel->widget.cols - 2;
966 * This is a trick to use two ngettext() calls in one sentence.
967 * First make "N bytes", then insert it into "X in M files".
969 g_snprintf (b_bytes, sizeof (b_bytes),
970 ngettext ("%s byte", "%s bytes", panel->total),
971 size_trunc_sep (panel->total, panels_options.kilobyte_si));
972 if (!size_only)
973 g_snprintf (buffer, sizeof (buffer),
974 ngettext ("%s in %d file", "%s in %d files", panel->marked),
975 b_bytes, panel->marked);
977 /* don't forget spaces around buffer content */
978 buf = (char *) str_trunc (buf, cols - 4);
980 if (x < 0)
981 /* center in panel */
982 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
985 * y == llines (panel) + 2 for mini_info_separator
986 * y == panel->widget.lines - 1 for panel bottom frame
988 widget_move (&panel->widget, y, x);
989 tty_setcolor (MARKED_COLOR);
990 tty_printf (" %s ", buf);
993 /* --------------------------------------------------------------------------------------------- */
995 static void
996 mini_info_separator (WPanel * panel)
998 if (panels_options.show_mini_info)
1000 const int y = llines (panel) + 2;
1002 tty_setcolor (NORMAL_COLOR);
1003 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1004 ACS_HLINE, panel->widget.cols - 2);
1005 /* Status displays total marked size.
1006 * Centered in panel, full format. */
1007 display_total_marked_size (panel, y, -1, FALSE);
1011 /* --------------------------------------------------------------------------------------------- */
1013 static void
1014 show_free_space (WPanel * panel)
1016 /* Used to figure out how many free space we have */
1017 static struct my_statfs myfs_stats;
1018 /* Old current working directory for displaying free space */
1019 static char *old_cwd = NULL;
1021 /* Don't try to stat non-local fs */
1022 if (!vfs_file_is_local (panel->cwd) || !free_space)
1023 return;
1025 if (old_cwd == NULL || strcmp (old_cwd, panel->cwd) != 0)
1027 char rpath[PATH_MAX];
1029 init_my_statfs ();
1030 g_free (old_cwd);
1031 old_cwd = g_strdup (panel->cwd);
1033 if (mc_realpath (panel->cwd, rpath) == NULL)
1034 return;
1036 my_statfs (&myfs_stats, rpath);
1039 if (myfs_stats.avail > 0 || myfs_stats.total > 0)
1041 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1042 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1043 panels_options.kilobyte_si);
1044 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1045 panels_options.kilobyte_si);
1046 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1047 myfs_stats.total >
1048 0 ? (int) (100 * (double) myfs_stats.avail / myfs_stats.total) : 0);
1049 widget_move (&panel->widget, panel->widget.lines - 1,
1050 panel->widget.cols - 2 - (int) strlen (tmp));
1051 tty_setcolor (NORMAL_COLOR);
1052 tty_print_string (tmp);
1056 /* --------------------------------------------------------------------------------------------- */
1058 static void
1059 show_dir (WPanel * panel)
1061 gchar *tmp;
1062 set_colors (panel);
1063 draw_box (panel->widget.owner,
1064 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1066 if (panels_options.show_mini_info)
1068 widget_move (&panel->widget, llines (panel) + 2, 0);
1069 tty_print_alt_char (ACS_LTEE, FALSE);
1070 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1071 tty_print_alt_char (ACS_RTEE, FALSE);
1074 widget_move (&panel->widget, 0, 1);
1075 tty_print_string (panel_history_prev_item_sign);
1077 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1078 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1079 panel_history_next_item_sign);
1081 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1082 tty_print_string (tmp);
1084 g_free (tmp);
1086 if (panel->active)
1087 tty_setcolor (REVERSE_COLOR);
1089 widget_move (&panel->widget, 0, 3);
1091 tty_printf (" %s ",
1092 str_term_trim (strip_home_and_password (panel->cwd),
1093 min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1095 if (!panels_options.show_mini_info)
1097 if (panel->marked == 0)
1099 /* Show size of curret file in the bottom of panel */
1100 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1102 char buffer[BUF_SMALL];
1104 g_snprintf (buffer, sizeof (buffer), " %s ",
1105 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1106 panels_options.kilobyte_si));
1107 tty_setcolor (NORMAL_COLOR);
1108 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1109 tty_print_string (buffer);
1112 else
1114 /* Show total size of marked files
1115 * In the bottom of panel, display size only. */
1116 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1120 show_free_space (panel);
1122 if (panel->active)
1123 tty_set_normal_attrs ();
1126 /* --------------------------------------------------------------------------------------------- */
1127 /** To be used only by long_frame and full_frame to adjust top_file */
1129 static void
1130 adjust_top_file (WPanel * panel)
1132 int old_top = panel->top_file;
1134 if (panel->selected - old_top > llines (panel))
1135 panel->top_file = panel->selected;
1136 if (old_top - panel->count > llines (panel))
1137 panel->top_file = panel->count - llines (panel);
1140 /* --------------------------------------------------------------------------------------------- */
1141 /** add "#enc:encodning" to end of path */
1142 /* if path end width a previous #enc:, only encoding is changed no additional
1143 * #enc: is appended
1144 * retun new string
1147 static char *
1148 add_encoding_to_path (const char *path, const char *encoding)
1150 char *result;
1151 char *semi;
1152 char *slash;
1154 semi = g_strrstr (path, VFS_ENCODING_PREFIX);
1156 if (semi != NULL)
1158 slash = strchr (semi, PATH_SEP);
1159 if (slash != NULL)
1161 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1163 else
1165 *semi = '\0';
1166 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1167 *semi = '#';
1170 else
1172 result = g_strconcat (path, PATH_SEP_STR VFS_ENCODING_PREFIX, encoding, (char *) NULL);
1175 return result;
1178 /* --------------------------------------------------------------------------------------------- */
1180 static char *
1181 panel_save_name (WPanel * panel)
1183 /* If the program is shuting down */
1184 if ((midnight_shutdown && auto_save_setup) || saving_setup)
1185 return g_strdup (panel->panel_name);
1186 else
1187 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1190 /* --------------------------------------------------------------------------------------------- */
1192 static void
1193 panel_destroy (WPanel * p)
1195 size_t i;
1197 if (panels_options.auto_save_setup)
1199 char *name;
1201 name = panel_save_name (p);
1202 panel_save_setup (p, name);
1203 g_free (name);
1206 panel_clean_dir (p);
1208 /* save and clean history */
1209 if (p->dir_history != NULL)
1211 history_put (p->hist_name, p->dir_history);
1213 p->dir_history = g_list_first (p->dir_history);
1214 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1215 g_list_free (p->dir_history);
1217 g_free (p->hist_name);
1219 delete_format (p->format);
1220 delete_format (p->status_format);
1222 g_free (p->user_format);
1223 for (i = 0; i < LIST_TYPES; i++)
1224 g_free (p->user_status_format[i]);
1225 g_free (p->dir.list);
1226 g_free (p->panel_name);
1229 /* --------------------------------------------------------------------------------------------- */
1231 static void
1232 panel_format_modified (WPanel * panel)
1234 panel->format_modified = 1;
1237 /* --------------------------------------------------------------------------------------------- */
1239 static void
1240 panel_paint_sort_info (WPanel * panel)
1242 if (*panel->sort_info.sort_field->hotkey != '\0')
1244 const char *sort_sign =
1245 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1246 char *str;
1248 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1249 widget_move (&panel->widget, 1, 1);
1250 tty_print_string (str);
1251 g_free (str);
1255 /* --------------------------------------------------------------------------------------------- */
1257 static gchar *
1258 panel_get_title_without_hotkey (const char *title)
1260 char *translated_title;
1261 char *hkey;
1263 if (title == NULL)
1264 return NULL;
1265 if (title[0] == '\0')
1266 return g_strdup ("");
1268 translated_title = g_strdup (_(title));
1270 hkey = strchr (translated_title, '&');
1271 if ((hkey != NULL) && (hkey[1] != '\0'))
1272 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1274 return translated_title;
1277 /* --------------------------------------------------------------------------------------------- */
1279 static void
1280 paint_frame (WPanel * panel)
1282 int side, width;
1283 GString *format_txt;
1285 if (!panel->split)
1286 adjust_top_file (panel);
1288 widget_erase (&panel->widget);
1289 show_dir (panel);
1291 widget_move (&panel->widget, 1, 1);
1293 for (side = 0; side <= panel->split; side++)
1295 format_e *format;
1297 if (side)
1299 tty_setcolor (NORMAL_COLOR);
1300 tty_print_one_vline (TRUE);
1301 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1303 else if (panel->split)
1304 width = panel->widget.cols / 2 - 3;
1305 else
1306 width = panel->widget.cols - 2;
1308 format_txt = g_string_new ("");
1309 for (format = panel->format; format; format = format->next)
1311 if (format->string_fn)
1313 g_string_set_size (format_txt, 0);
1315 if (panel->list_type == list_long
1316 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1317 g_string_append (format_txt,
1318 panel->sort_info.reverse
1319 ? panel_sort_down_sign : panel_sort_up_sign);
1321 g_string_append (format_txt, format->title);
1322 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1324 g_string_append (format_txt, " [");
1325 g_string_append (format_txt, panel->filter);
1326 g_string_append (format_txt, "]");
1329 tty_setcolor (HEADER_COLOR);
1330 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1331 J_CENTER_LEFT));
1332 width -= format->field_len;
1334 else
1336 tty_setcolor (NORMAL_COLOR);
1337 tty_print_one_vline (TRUE);
1338 width--;
1341 g_string_free (format_txt, TRUE);
1343 if (width > 0)
1344 tty_draw_hline (-1, -1, ' ', width);
1347 if (panel->list_type != list_long)
1348 panel_paint_sort_info (panel);
1351 /* --------------------------------------------------------------------------------------------- */
1353 static const char *
1354 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1356 panel_display_t frame = frame_half;
1357 format = skip_separators (format);
1359 if (!strncmp (format, "full", 4))
1361 frame = frame_full;
1362 format += 4;
1364 else if (!strncmp (format, "half", 4))
1366 frame = frame_half;
1367 format += 4;
1370 if (!isstatus)
1372 panel->frame_size = frame;
1373 panel->split = 0;
1376 /* Now, the optional column specifier */
1377 format = skip_separators (format);
1379 if (*format == '1' || *format == '2')
1381 if (!isstatus)
1382 panel->split = *format == '2';
1383 format++;
1386 if (!isstatus)
1387 panel_update_cols (&(panel->widget), panel->frame_size);
1389 return skip_separators (format);
1392 /* Format is:
1394 all := panel_format? format
1395 panel_format := [full|half] [1|2]
1396 format := one_format_e
1397 | format , one_format_e
1399 one_format_e := just format.id [opt_size]
1400 just := [<=>]
1401 opt_size := : size [opt_expand]
1402 size := [0-9]+
1403 opt_expand := +
1407 /* --------------------------------------------------------------------------------------------- */
1409 static format_e *
1410 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1411 int *res_total_cols)
1413 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1414 int total_cols = 0; /* Used columns by the format */
1415 int set_justify; /* flag: set justification mode? */
1416 align_crt_t justify = J_LEFT; /* Which mode. */
1417 size_t i;
1419 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1421 *error = 0;
1423 if (i18n_timelength == 0)
1425 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1427 for (i = 0; panel_fields[i].id != NULL; i++)
1428 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1429 panel_fields[i].min_size = i18n_timelength;
1433 * This makes sure that the panel and mini status full/half mode
1434 * setting is equal
1436 format = parse_panel_size (panel, format, isstatus);
1438 while (*format)
1439 { /* format can be an empty string */
1440 int found = 0;
1442 darr = g_new0 (format_e, 1);
1444 /* I'm so ugly, don't look at me :-) */
1445 if (!home)
1446 home = old = darr;
1448 old->next = darr;
1449 darr->next = 0;
1450 old = darr;
1452 format = skip_separators (format);
1454 if (strchr ("<=>", *format))
1456 set_justify = 1;
1457 switch (*format)
1459 case '<':
1460 justify = J_LEFT;
1461 break;
1462 case '=':
1463 justify = J_CENTER;
1464 break;
1465 case '>':
1466 default:
1467 justify = J_RIGHT;
1468 break;
1470 format = skip_separators (format + 1);
1472 else
1473 set_justify = 0;
1475 for (i = 0; panel_fields[i].id != NULL; i++)
1477 size_t klen = strlen (panel_fields[i].id);
1479 if (strncmp (format, panel_fields[i].id, klen) != 0)
1480 continue;
1482 format += klen;
1484 darr->requested_field_len = panel_fields[i].min_size;
1485 darr->string_fn = panel_fields[i].string_fn;
1486 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1488 darr->id = panel_fields[i].id;
1489 darr->expand = panel_fields[i].expands;
1490 darr->just_mode = panel_fields[i].default_just;
1492 if (set_justify)
1494 if (IS_FIT (darr->just_mode))
1495 darr->just_mode = MAKE_FIT (justify);
1496 else
1497 darr->just_mode = justify;
1499 found = 1;
1501 format = skip_separators (format);
1503 /* If we have a size specifier */
1504 if (*format == ':')
1506 int req_length;
1508 /* If the size was specified, we don't want
1509 * auto-expansion by default
1511 darr->expand = 0;
1512 format++;
1513 req_length = atoi (format);
1514 darr->requested_field_len = req_length;
1516 format = skip_numbers (format);
1518 /* Now, if they insist on expansion */
1519 if (*format == '+')
1521 darr->expand = 1;
1522 format++;
1527 break;
1529 if (!found)
1531 char *tmp_format = g_strdup (format);
1533 int pos = min (8, strlen (format));
1534 delete_format (home);
1535 tmp_format[pos] = 0;
1536 *error =
1537 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1538 g_free (tmp_format);
1539 return 0;
1541 total_cols += darr->requested_field_len;
1544 *res_total_cols = total_cols;
1545 return home;
1548 /* --------------------------------------------------------------------------------------------- */
1550 static format_e *
1551 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1553 #define MAX_EXPAND 4
1554 int expand_top = 0; /* Max used element in expand */
1555 int usable_columns; /* Usable columns in the panel */
1556 int total_cols = 0;
1557 int i;
1558 format_e *darr, *home;
1560 if (!format)
1561 format = DEFAULT_USER_FORMAT;
1563 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1565 if (*error)
1566 return 0;
1568 panel->dirty = 1;
1570 /* Status needn't to be split */
1571 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1573 : (panel->split + 1))) - (!isstatus
1574 && panel->split);
1576 /* Look for the expandable fields and set field_len based on the requested field len */
1577 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1579 darr->field_len = darr->requested_field_len;
1580 if (darr->expand)
1581 expand_top++;
1584 /* If we used more columns than the available columns, adjust that */
1585 if (total_cols > usable_columns)
1587 int pdif, dif = total_cols - usable_columns;
1589 while (dif)
1591 pdif = dif;
1592 for (darr = home; darr; darr = darr->next)
1594 if (dif && darr->field_len - 1)
1596 darr->field_len--;
1597 dif--;
1601 /* avoid endless loop if num fields > 40 */
1602 if (pdif == dif)
1603 break;
1605 total_cols = usable_columns; /* give up, the rest should be truncated */
1608 /* Expand the available space */
1609 if ((usable_columns > total_cols) && expand_top)
1611 int spaces = (usable_columns - total_cols) / expand_top;
1612 int extra = (usable_columns - total_cols) % expand_top;
1614 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1615 if (darr->expand)
1617 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1618 i++;
1621 return home;
1624 /* --------------------------------------------------------------------------------------------- */
1625 /** Given the panel->view_type returns the format string to be parsed */
1627 static const char *
1628 panel_format (WPanel * panel)
1630 switch (panel->list_type)
1633 case list_long:
1634 return "full perm space nlink space owner space group space size space mtime space name";
1636 case list_brief:
1637 return "half 2 type name";
1639 case list_user:
1640 return panel->user_format;
1642 default:
1643 case list_full:
1644 return "half type name | size | mtime";
1648 /* --------------------------------------------------------------------------------------------- */
1650 static const char *
1651 mini_status_format (WPanel * panel)
1653 if (panel->user_mini_status)
1654 return panel->user_status_format[panel->list_type];
1656 switch (panel->list_type)
1659 case list_long:
1660 return "full perm space nlink space owner space group space size space mtime space name";
1662 case list_brief:
1663 return "half type name space bsize space perm space";
1665 case list_full:
1666 return "half type name";
1668 default:
1669 case list_user:
1670 return panel->user_format;
1674 /* */
1675 /* Panel operation commands */
1676 /* */
1678 /* --------------------------------------------------------------------------------------------- */
1679 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1681 static cb_ret_t
1682 maybe_cd (int move_up_dir)
1684 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1686 if (move_up_dir)
1688 do_cd ("..", cd_exact);
1689 return MSG_HANDLED;
1692 if (S_ISDIR (selection (current_panel)->st.st_mode)
1693 || link_isdir (selection (current_panel)))
1695 do_cd (selection (current_panel)->fname, cd_exact);
1696 return MSG_HANDLED;
1699 return MSG_NOT_HANDLED;
1702 /* --------------------------------------------------------------------------------------------- */
1704 /* if command line is empty then do 'cd ..' */
1705 static cb_ret_t
1706 force_maybe_cd (void)
1708 if (cmdline->buffer[0] == '\0')
1710 do_cd ("..", cd_exact);
1711 return MSG_HANDLED;
1713 return MSG_NOT_HANDLED;
1716 /* --------------------------------------------------------------------------------------------- */
1718 /* Returns the number of items in the given panel */
1719 static int
1720 ITEMS (WPanel * p)
1722 if (p->split)
1723 return llines (p) * 2;
1724 else
1725 return llines (p);
1728 /* --------------------------------------------------------------------------------------------- */
1730 static void
1731 unselect_item (WPanel * panel)
1733 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1736 /* --------------------------------------------------------------------------------------------- */
1738 static void
1739 move_down (WPanel * panel)
1741 if (panel->selected + 1 == panel->count)
1742 return;
1744 unselect_item (panel);
1745 panel->selected++;
1746 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1748 /* Scroll window half screen */
1749 panel->top_file += ITEMS (panel) / 2;
1750 if (panel->top_file > panel->count - ITEMS (panel))
1751 panel->top_file = panel->count - ITEMS (panel);
1752 paint_dir (panel);
1754 select_item (panel);
1757 /* --------------------------------------------------------------------------------------------- */
1759 static void
1760 move_up (WPanel * panel)
1762 if (panel->selected == 0)
1763 return;
1765 unselect_item (panel);
1766 panel->selected--;
1767 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1769 /* Scroll window half screen */
1770 panel->top_file -= ITEMS (panel) / 2;
1771 if (panel->top_file < 0)
1772 panel->top_file = 0;
1773 paint_dir (panel);
1775 select_item (panel);
1778 /* --------------------------------------------------------------------------------------------- */
1779 /** Changes the selection by lines (may be negative) */
1781 static void
1782 move_selection (WPanel * panel, int lines)
1784 int new_pos;
1785 int adjust = 0;
1787 new_pos = panel->selected + lines;
1788 if (new_pos >= panel->count)
1789 new_pos = panel->count - 1;
1791 if (new_pos < 0)
1792 new_pos = 0;
1794 unselect_item (panel);
1795 panel->selected = new_pos;
1797 if (panel->selected - panel->top_file >= ITEMS (panel))
1799 panel->top_file += lines;
1800 adjust = 1;
1803 if (panel->selected - panel->top_file < 0)
1805 panel->top_file += lines;
1806 adjust = 1;
1809 if (adjust)
1811 if (panel->top_file > panel->selected)
1812 panel->top_file = panel->selected;
1813 if (panel->top_file < 0)
1814 panel->top_file = 0;
1815 paint_dir (panel);
1817 select_item (panel);
1820 /* --------------------------------------------------------------------------------------------- */
1822 static cb_ret_t
1823 move_left (WPanel * panel)
1825 if (panel->split)
1827 move_selection (panel, -llines (panel));
1828 return MSG_HANDLED;
1830 else
1831 return maybe_cd (1); /* cd .. */
1834 /* --------------------------------------------------------------------------------------------- */
1836 static int
1837 move_right (WPanel * panel)
1839 if (panel->split)
1841 move_selection (panel, llines (panel));
1842 return MSG_HANDLED;
1844 else
1845 return maybe_cd (0); /* cd (selection) */
1848 /* --------------------------------------------------------------------------------------------- */
1850 static void
1851 prev_page (WPanel * panel)
1853 int items;
1855 if (!panel->selected && !panel->top_file)
1856 return;
1857 unselect_item (panel);
1858 items = ITEMS (panel);
1859 if (panel->top_file < items)
1860 items = panel->top_file;
1861 if (!items)
1862 panel->selected = 0;
1863 else
1864 panel->selected -= items;
1865 panel->top_file -= items;
1867 select_item (panel);
1868 paint_dir (panel);
1871 /* --------------------------------------------------------------------------------------------- */
1873 static void
1874 goto_parent_dir (WPanel * panel)
1876 (void) panel;
1877 do_cd ("..", cd_exact);
1880 /* --------------------------------------------------------------------------------------------- */
1882 static void
1883 next_page (WPanel * panel)
1885 int items;
1887 if (panel->selected == panel->count - 1)
1888 return;
1889 unselect_item (panel);
1890 items = ITEMS (panel);
1891 if (panel->top_file > panel->count - 2 * items)
1892 items = panel->count - items - panel->top_file;
1893 if (panel->top_file + items < 0)
1894 items = -panel->top_file;
1895 if (!items)
1896 panel->selected = panel->count - 1;
1897 else
1898 panel->selected += items;
1899 panel->top_file += items;
1901 select_item (panel);
1902 paint_dir (panel);
1905 /* --------------------------------------------------------------------------------------------- */
1907 static void
1908 goto_child_dir (WPanel * panel)
1910 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
1912 do_cd (selection (panel)->fname, cd_exact);
1916 /* --------------------------------------------------------------------------------------------- */
1918 static void
1919 goto_top_file (WPanel * panel)
1921 unselect_item (panel);
1922 panel->selected = panel->top_file;
1923 select_item (panel);
1926 /* --------------------------------------------------------------------------------------------- */
1928 static void
1929 goto_middle_file (WPanel * panel)
1931 unselect_item (panel);
1932 panel->selected = panel->top_file + (ITEMS (panel) / 2);
1933 select_item (panel);
1936 /* --------------------------------------------------------------------------------------------- */
1938 static void
1939 goto_bottom_file (WPanel * panel)
1941 unselect_item (panel);
1942 panel->selected = panel->top_file + ITEMS (panel) - 1;
1943 select_item (panel);
1946 /* --------------------------------------------------------------------------------------------- */
1948 static void
1949 move_home (WPanel * panel)
1951 if (panel->selected == 0)
1952 return;
1954 unselect_item (panel);
1956 if (panels_options.torben_fj_mode)
1958 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
1960 if (panel->selected > middle_pos)
1962 goto_middle_file (panel);
1963 return;
1965 if (panel->selected != panel->top_file)
1967 goto_top_file (panel);
1968 return;
1972 panel->top_file = 0;
1973 panel->selected = 0;
1975 paint_dir (panel);
1976 select_item (panel);
1979 /* --------------------------------------------------------------------------------------------- */
1981 static void
1982 move_end (WPanel * panel)
1984 if (panel->selected == panel->count - 1)
1985 return;
1987 unselect_item (panel);
1989 if (panels_options.torben_fj_mode)
1991 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
1993 if (panel->selected < middle_pos)
1995 goto_middle_file (panel);
1996 return;
1998 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2000 goto_bottom_file (panel);
2001 return;
2005 panel->selected = panel->count - 1;
2006 paint_dir (panel);
2007 select_item (panel);
2010 /* --------------------------------------------------------------------------------------------- */
2012 static void
2013 do_mark_file (WPanel * panel, mark_act_t do_move)
2015 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2016 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2017 move_down (panel);
2018 else if (do_move == MARK_FORCE_UP)
2019 move_up (panel);
2022 /* --------------------------------------------------------------------------------------------- */
2024 static void
2025 mark_file (WPanel * panel)
2027 do_mark_file (panel, MARK_DOWN);
2030 /* --------------------------------------------------------------------------------------------- */
2032 static void
2033 mark_file_up (WPanel * panel)
2035 do_mark_file (panel, MARK_FORCE_UP);
2038 /* --------------------------------------------------------------------------------------------- */
2040 static void
2041 mark_file_down (WPanel * panel)
2043 do_mark_file (panel, MARK_FORCE_DOWN);
2046 /* --------------------------------------------------------------------------------------------- */
2047 /** Incremental search of a file name in the panel.
2048 * @param panel instance of WPanel structure
2049 * @param c_code key code
2052 static void
2053 do_search (WPanel * panel, int c_code)
2055 size_t l;
2056 int i, sel;
2057 gboolean wrapped = FALSE;
2058 char *act;
2059 mc_search_t *search;
2060 char *reg_exp, *esc_str;
2061 gboolean is_found = FALSE;
2063 l = strlen (panel->search_buffer);
2064 if (c_code == KEY_BACKSPACE)
2066 if (l != 0)
2068 act = panel->search_buffer + l;
2069 str_prev_noncomb_char (&act, panel->search_buffer);
2070 act[0] = '\0';
2072 panel->search_chpoint = 0;
2074 else
2076 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2078 panel->search_char[panel->search_chpoint] = c_code;
2079 panel->search_chpoint++;
2082 if (panel->search_chpoint > 0)
2084 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2086 case -2:
2087 return;
2088 case -1:
2089 panel->search_chpoint = 0;
2090 return;
2091 default:
2092 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2094 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2095 l += panel->search_chpoint;
2096 *(panel->search_buffer + l) = '\0';
2097 panel->search_chpoint = 0;
2103 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2104 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2105 search = mc_search_new (esc_str, -1);
2106 search->search_type = MC_SEARCH_T_GLOB;
2107 search->is_entire_line = TRUE;
2108 switch (panels_options.qsearch_mode)
2110 case QSEARCH_CASE_SENSITIVE:
2111 search->is_case_sensitive = TRUE;
2112 break;
2113 case QSEARCH_CASE_INSENSITIVE:
2114 search->is_case_sensitive = FALSE;
2115 break;
2116 default:
2117 search->is_case_sensitive = panel->sort_info.case_sensitive;
2118 break;
2120 sel = panel->selected;
2121 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2123 if (i >= panel->count)
2125 i = 0;
2126 if (wrapped)
2127 break;
2128 wrapped = TRUE;
2130 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2132 sel = i;
2133 is_found = TRUE;
2134 break;
2137 if (is_found)
2139 unselect_item (panel);
2140 panel->selected = sel;
2141 select_item (panel);
2142 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2144 else if (c_code != KEY_BACKSPACE)
2146 act = panel->search_buffer + l;
2147 str_prev_noncomb_char (&act, panel->search_buffer);
2148 act[0] = '\0';
2150 mc_search_free (search);
2151 g_free (reg_exp);
2152 g_free (esc_str);
2155 /* --------------------------------------------------------------------------------------------- */
2156 /** Start new search.
2157 * @param panel instance of WPanel structure
2160 static void
2161 start_search (WPanel * panel)
2163 if (panel->searching)
2165 if (panel->selected + 1 == panel->count)
2166 panel->selected = 0;
2167 else
2168 move_down (panel);
2170 /* in case if there was no search string we need to recall
2171 previous string, with which we ended previous searching */
2172 if (panel->search_buffer[0] == '\0')
2173 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2174 sizeof (panel->search_buffer));
2176 do_search (panel, 0);
2178 else
2180 panel->searching = TRUE;
2181 panel->search_buffer[0] = '\0';
2182 panel->search_char[0] = '\0';
2183 panel->search_chpoint = 0;
2184 display_mini_info (panel);
2185 mc_refresh ();
2189 /* --------------------------------------------------------------------------------------------- */
2191 static void
2192 stop_search (WPanel * panel)
2194 panel->searching = FALSE;
2196 /* if user had overrdied search string, we need to store it
2197 to the previous_search_buffer */
2198 if (panel->search_buffer[0] != '\0')
2199 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2200 sizeof (panel->prev_search_buffer));
2202 display_mini_info (panel);
2205 /* --------------------------------------------------------------------------------------------- */
2206 /** Return 1 if the Enter key has been processed, 0 otherwise */
2208 static int
2209 do_enter_on_file_entry (file_entry * fe)
2211 char *full_name;
2214 * Directory or link to directory - change directory.
2215 * Try the same for the entries on which mc_lstat() has failed.
2217 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2219 if (!do_cd (fe->fname, cd_exact))
2220 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2221 return 1;
2224 /* Try associated command */
2225 if (regex_command (fe->fname, "Open", NULL) != 0)
2226 return 1;
2228 /* Check if the file is executable */
2229 full_name = concat_dir_and_file (current_panel->cwd, fe->fname);
2230 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe))
2232 g_free (full_name);
2233 return 0;
2235 g_free (full_name);
2237 if (confirm_execute)
2239 if (query_dialog
2240 (_("The Midnight Commander"),
2241 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2242 return 1;
2245 if (!vfs_current_is_local ())
2247 char *tmp;
2248 int ret;
2250 tmp = concat_dir_and_file (vfs_get_current_dir (), fe->fname);
2251 ret = mc_setctl (tmp, VFS_SETCTL_RUN, NULL);
2252 g_free (tmp);
2253 /* We took action only if the dialog was shown or the execution
2254 * was successful */
2255 return confirm_execute || (ret == 0);
2259 char *tmp = name_quote (fe->fname, 0);
2260 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2261 g_free (tmp);
2262 shell_execute (cmd, 0);
2263 g_free (cmd);
2266 #if HAVE_CHARSET
2267 source_codepage = default_source_codepage;
2268 #endif
2270 return 1;
2273 /* --------------------------------------------------------------------------------------------- */
2275 static int
2276 do_enter (WPanel * panel)
2278 return do_enter_on_file_entry (selection (panel));
2281 /* --------------------------------------------------------------------------------------------- */
2283 static void
2284 chdir_other_panel (WPanel * panel)
2286 char *new_dir;
2287 char *sel_entry = NULL;
2289 if (get_other_type () != view_listing)
2291 set_display_type (get_other_index (), view_listing);
2294 if (!S_ISDIR (panel->dir.list[panel->selected].st.st_mode))
2296 new_dir = concat_dir_and_file (panel->cwd, "..");
2297 sel_entry = strrchr (panel->cwd, PATH_SEP);
2299 else
2300 new_dir = concat_dir_and_file (panel->cwd, panel->dir.list[panel->selected].fname);
2302 change_panel ();
2303 do_cd (new_dir, cd_exact);
2304 if (sel_entry)
2305 try_to_select (current_panel, sel_entry);
2306 change_panel ();
2308 move_down (panel);
2310 g_free (new_dir);
2313 /* --------------------------------------------------------------------------------------------- */
2315 * Make the current directory of the current panel also the current
2316 * directory of the other panel. Put the other panel to the listing
2317 * mode if needed. If the current panel is panelized, the other panel
2318 * doesn't become panelized.
2321 static void
2322 sync_other_panel (WPanel * panel)
2324 if (get_other_type () != view_listing)
2326 set_display_type (get_other_index (), view_listing);
2329 do_panel_cd (other_panel, current_panel->cwd, cd_exact);
2331 /* try to select current filename on the other panel */
2332 if (!panel->is_panelized)
2334 try_to_select (other_panel, selection (panel)->fname);
2338 /* --------------------------------------------------------------------------------------------- */
2340 static void
2341 chdir_to_readlink (WPanel * panel)
2343 char *new_dir;
2345 if (get_other_type () != view_listing)
2346 return;
2348 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2350 char buffer[MC_MAXPATHLEN], *p;
2351 int i;
2352 struct stat st;
2354 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2355 if (i < 0)
2356 return;
2357 if (mc_stat (selection (panel)->fname, &st) < 0)
2358 return;
2359 buffer[i] = 0;
2360 if (!S_ISDIR (st.st_mode))
2362 p = strrchr (buffer, PATH_SEP);
2363 if (p && !p[1])
2365 *p = 0;
2366 p = strrchr (buffer, PATH_SEP);
2368 if (!p)
2369 return;
2370 p[1] = 0;
2372 if (*buffer == PATH_SEP)
2373 new_dir = g_strdup (buffer);
2374 else
2375 new_dir = concat_dir_and_file (panel->cwd, buffer);
2377 change_panel ();
2378 do_cd (new_dir, cd_exact);
2379 change_panel ();
2381 move_down (panel);
2383 g_free (new_dir);
2387 /* --------------------------------------------------------------------------------------------- */
2389 static gsize
2390 panel_get_format_field_count (WPanel * panel)
2392 format_e *format;
2393 gsize lc_index;
2394 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2395 return lc_index;
2398 /* --------------------------------------------------------------------------------------------- */
2400 function return 0 if not found and REAL_INDEX+1 if found
2403 static gsize
2404 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2406 format_e *format;
2407 gsize lc_index;
2409 for (lc_index = 1, format = panel->format;
2410 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2411 if (format == NULL)
2412 lc_index = 0;
2414 return lc_index;
2417 /* --------------------------------------------------------------------------------------------- */
2419 static format_e *
2420 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2422 format_e *format;
2423 for (format = panel->format;
2424 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2425 return format;
2428 /* --------------------------------------------------------------------------------------------- */
2430 static const panel_field_t *
2431 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2433 const panel_field_t *pfield;
2434 format_e *format;
2436 format = panel_get_format_field_by_index (panel, lc_index);
2437 if (format == NULL)
2438 return NULL;
2439 pfield = panel_get_field_by_title (format->title);
2440 if (pfield == NULL)
2441 return NULL;
2442 if (pfield->sort_routine == NULL)
2443 return NULL;
2444 return pfield;
2447 /* --------------------------------------------------------------------------------------------- */
2449 static void
2450 panel_toggle_sort_order_prev (WPanel * panel)
2452 gsize lc_index, i;
2453 gchar *title;
2455 const panel_field_t *pfield = NULL;
2457 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2458 lc_index = panel_get_format_field_index_by_name (panel, title);
2459 g_free (title);
2461 if (lc_index > 1)
2463 /* search for prev sortable column in panel format */
2464 for (i = lc_index - 1;
2465 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2468 if (pfield == NULL)
2470 /* Sortable field not found. Try to search in each array */
2471 for (i = panel_get_format_field_count (panel);
2472 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2475 if (pfield != NULL)
2477 panel->sort_info.sort_field = pfield;
2478 panel_set_sort_order (panel, pfield);
2482 /* --------------------------------------------------------------------------------------------- */
2484 static void
2485 panel_toggle_sort_order_next (WPanel * panel)
2487 gsize lc_index, i;
2488 const panel_field_t *pfield = NULL;
2489 gsize format_field_count;
2490 gchar *title;
2492 format_field_count = panel_get_format_field_count (panel);
2493 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2494 lc_index = panel_get_format_field_index_by_name (panel, title);
2495 g_free (title);
2497 if (lc_index != 0 && lc_index != format_field_count)
2499 /* search for prev sortable column in panel format */
2500 for (i = lc_index;
2501 i != format_field_count
2502 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2505 if (pfield == NULL)
2507 /* Sortable field not found. Try to search in each array */
2508 for (i = 0;
2509 i != format_field_count
2510 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2513 if (pfield != NULL)
2515 panel->sort_info.sort_field = pfield;
2516 panel_set_sort_order (panel, pfield);
2520 /* --------------------------------------------------------------------------------------------- */
2522 static void
2523 panel_select_sort_order (WPanel * panel)
2525 const panel_field_t *sort_order;
2527 sort_order = sort_box (&panel->sort_info);
2528 if (sort_order != NULL)
2530 panel->sort_info.sort_field = sort_order;
2531 panel_set_sort_order (panel, sort_order);
2535 /* --------------------------------------------------------------------------------------------- */
2537 static void
2538 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2540 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2542 const panel_field_t *sort_order;
2544 sort_order = panel_get_field_by_id (name);
2545 if (sort_order == NULL)
2546 return;
2547 panel->sort_info.sort_field = sort_order;
2549 else
2550 panel->sort_info.reverse = !panel->sort_info.reverse;
2552 panel_set_sort_order (panel, panel->sort_info.sort_field);
2555 /* --------------------------------------------------------------------------------------------- */
2557 * If we moved to the parent directory move the selection pointer to
2558 * the old directory name; If we leave VFS dir, remove FS specificator.
2560 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2563 static const char *
2564 get_parent_dir_name (const char *cwd, const char *lwd)
2566 size_t llen, clen;
2568 llen = strlen (lwd);
2569 clen = strlen (cwd);
2571 if (llen > clen)
2573 const char *p;
2575 p = strrchr (lwd, PATH_SEP);
2577 if ((p != NULL)
2578 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2579 && (clen == (size_t) (p - lwd)
2580 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2581 return (p + 1);
2584 return NULL;
2587 /* --------------------------------------------------------------------------------------------- */
2588 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2590 static void
2591 subshell_chdir (const char *directory)
2593 #ifdef HAVE_SUBSHELL_SUPPORT
2594 if (use_subshell && vfs_current_is_local ())
2595 do_subshell_chdir (directory, FALSE, TRUE);
2596 #endif /* HAVE_SUBSHELL_SUPPORT */
2599 /* --------------------------------------------------------------------------------------------- */
2601 * Changes the current directory of the panel.
2602 * Don't record change in the directory history.
2605 static gboolean
2606 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
2608 const char *directory;
2609 char *olddir;
2610 char temp[MC_MAXPATHLEN];
2611 char *translated_url;
2613 if (cd_type == cd_parse_command)
2615 while (*new_dir == ' ')
2616 new_dir++;
2619 olddir = g_strdup (panel->cwd);
2620 new_dir = translated_url = vfs_translate_url (new_dir);
2622 /* Convert *new_path to a suitable pathname, handle ~user */
2624 if (cd_type == cd_parse_command)
2626 if (!strcmp (new_dir, "-"))
2628 strcpy (temp, panel->lwd);
2629 new_dir = temp;
2632 directory = *new_dir ? new_dir : mc_config_get_home_dir ();
2634 if (mc_chdir (directory) == -1)
2636 strcpy (panel->cwd, olddir);
2637 g_free (olddir);
2638 g_free (translated_url);
2639 return FALSE;
2641 g_free (translated_url);
2643 /* Success: save previous directory, shutdown status of previous dir */
2644 strcpy (panel->lwd, olddir);
2645 input_free_completions (cmdline);
2647 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
2649 vfs_release_path (olddir);
2651 subshell_chdir (panel->cwd);
2653 /* Reload current panel */
2654 panel_clean_dir (panel);
2655 panel->count =
2656 do_load_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
2657 panel->sort_info.reverse, panel->sort_info.case_sensitive,
2658 panel->sort_info.exec_first, panel->filter);
2659 try_to_select (panel, get_parent_dir_name (panel->cwd, olddir));
2660 load_hint (0);
2661 panel->dirty = 1;
2662 update_xterm_title_path ();
2664 g_free (olddir);
2666 return TRUE;
2669 /* --------------------------------------------------------------------------------------------- */
2671 static void
2672 directory_history_next (WPanel * panel)
2674 GList *nextdir;
2676 nextdir = g_list_next (panel->dir_history);
2678 if ((nextdir != NULL) && (_do_panel_cd (panel, (char *) nextdir->data, cd_exact)))
2679 panel->dir_history = nextdir;
2682 /* --------------------------------------------------------------------------------------------- */
2684 static void
2685 directory_history_prev (WPanel * panel)
2687 GList *prevdir;
2689 prevdir = g_list_previous (panel->dir_history);
2691 if ((prevdir != NULL) && (_do_panel_cd (panel, (char *) prevdir->data, cd_exact)))
2692 panel->dir_history = prevdir;
2695 /* --------------------------------------------------------------------------------------------- */
2697 static void
2698 directory_history_list (WPanel * panel)
2700 char *s;
2702 s = history_show (&panel->dir_history, &panel->widget);
2704 if (s != NULL)
2706 if (_do_panel_cd (panel, s, cd_exact))
2707 directory_history_add (panel, panel->cwd);
2708 else
2709 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2710 g_free (s);
2714 /* --------------------------------------------------------------------------------------------- */
2716 static cb_ret_t
2717 panel_execute_cmd (WPanel * panel, unsigned long command)
2719 int res = MSG_HANDLED;
2721 if (command != CK_PanelStartSearch)
2722 stop_search (panel);
2724 switch (command)
2726 case CK_PanelChdirOtherPanel:
2727 chdir_other_panel (panel);
2728 break;
2729 case CK_PanelChdirToReadlink:
2730 chdir_to_readlink (panel);
2731 break;
2732 case CK_PanelCmdCopyLocal:
2733 copy_cmd_local ();
2734 break;
2735 case CK_PanelCmdDeleteLocal:
2736 delete_cmd_local ();
2737 break;
2738 case CK_PanelCmdDoEnter:
2739 do_enter (panel);
2740 break;
2741 case CK_PanelCmdViewSimple:
2742 view_simple_cmd ();
2743 break;
2744 case CK_PanelCmdEditNew:
2745 edit_cmd_new ();
2746 break;
2747 case CK_PanelCmdRenameLocal:
2748 rename_cmd_local ();
2749 break;
2750 case CK_PanelCmdReverseSelection:
2751 reverse_selection_cmd ();
2752 break;
2753 case CK_PanelCmdSelect:
2754 select_cmd ();
2755 break;
2756 case CK_PanelCmdUnselect:
2757 unselect_cmd ();
2758 break;
2759 case CK_PanelNextPage:
2760 next_page (panel);
2761 break;
2762 case CK_PanelPrevPage:
2763 prev_page (panel);
2764 break;
2765 case CK_PanelGotoChildDir:
2766 goto_child_dir (panel);
2767 break;
2768 case CK_PanelGotoParentDir:
2769 goto_parent_dir (panel);
2770 break;
2771 case CK_PanelDirectoryHistoryList:
2772 directory_history_list (panel);
2773 break;
2774 case CK_PanelDirectoryHistoryNext:
2775 directory_history_next (panel);
2776 break;
2777 case CK_PanelDirectoryHistoryPrev:
2778 directory_history_prev (panel);
2779 break;
2780 case CK_PanelGotoBottomFile:
2781 goto_bottom_file (panel);
2782 break;
2783 case CK_PanelGotoMiddleFile:
2784 goto_middle_file (panel);
2785 break;
2786 case CK_PanelGotoTopFile:
2787 goto_top_file (panel);
2788 break;
2789 case CK_PanelMarkFile:
2790 mark_file (panel);
2791 break;
2792 case CK_PanelMarkFileUp:
2793 mark_file_up (panel);
2794 break;
2795 case CK_PanelMarkFileDown:
2796 mark_file_down (panel);
2797 break;
2798 case CK_PanelSmartGotoParentDir:
2799 res = force_maybe_cd ();
2800 break;
2801 case CK_PanelMoveUp:
2802 move_up (panel);
2803 break;
2804 case CK_PanelMoveDown:
2805 move_down (panel);
2806 break;
2807 case CK_PanelMoveLeft:
2808 res = move_left (panel);
2809 break;
2810 case CK_PanelMoveRight:
2811 res = move_right (panel);
2812 break;
2813 case CK_PanelMoveEnd:
2814 move_end (panel);
2815 break;
2816 case CK_PanelMoveHome:
2817 move_home (panel);
2818 break;
2819 case CK_PanelSetPanelEncoding:
2820 panel_change_encoding (panel);
2821 break;
2822 case CK_PanelStartSearch:
2823 start_search (panel);
2824 break;
2825 case CK_PanelStopSearch:
2826 break;
2827 case CK_PanelSyncOtherPanel:
2828 sync_other_panel (panel);
2829 break;
2830 case CK_PanelSelectSortOrder:
2831 panel_select_sort_order (panel);
2832 break;
2833 case CK_PanelToggleSortOrderPrev:
2834 panel_toggle_sort_order_prev (panel);
2835 break;
2836 case CK_PanelToggleSortOrderNext:
2837 panel_toggle_sort_order_next (panel);
2838 break;
2839 case CK_PanelReverseSort:
2840 panel->sort_info.reverse = !panel->sort_info.reverse;
2841 panel_set_sort_order (panel, panel->sort_info.sort_field);
2842 break;
2843 case CK_PanelSortOrderByName:
2844 panel_set_sort_type_by_id (panel, "name");
2845 break;
2846 case CK_PanelSortOrderByExt:
2847 panel_set_sort_type_by_id (panel, "extension");
2848 break;
2849 case CK_PanelSortOrderBySize:
2850 panel_set_sort_type_by_id (panel, "size");
2851 break;
2852 case CK_PanelSortOrderByMTime:
2853 panel_set_sort_type_by_id (panel, "mtime");
2854 break;
2856 return res;
2859 /* --------------------------------------------------------------------------------------------- */
2861 static cb_ret_t
2862 panel_key (WPanel * panel, int key)
2864 size_t i;
2866 for (i = 0; panel_map[i].key != 0; i++)
2867 if (key == panel_map[i].key)
2868 return panel_execute_cmd (panel, panel_map[i].command);
2870 if (panels_options.torben_fj_mode && key == ALT ('h'))
2872 goto_middle_file (panel);
2873 return MSG_HANDLED;
2876 if (is_abort_char (key))
2878 stop_search (panel);
2879 return MSG_HANDLED;
2882 /* Do not eat characters not meant for the panel below ' ' (e.g. C-l). */
2883 if ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE)
2885 if (panel->searching)
2887 do_search (panel, key);
2888 return MSG_HANDLED;
2891 if (!command_prompt)
2893 start_search (panel);
2894 do_search (panel, key);
2895 return MSG_HANDLED;
2899 return MSG_NOT_HANDLED;
2902 /* --------------------------------------------------------------------------------------------- */
2904 static cb_ret_t
2905 panel_callback (Widget * w, widget_msg_t msg, int parm)
2907 WPanel *panel = (WPanel *) w;
2908 WButtonBar *bb;
2910 switch (msg)
2912 case WIDGET_DRAW:
2913 /* Repaint everything, including frame and separator */
2914 paint_frame (panel); /* including show_dir */
2915 paint_dir (panel);
2916 mini_info_separator (panel);
2917 display_mini_info (panel);
2918 panel->dirty = 0;
2919 return MSG_HANDLED;
2921 case WIDGET_FOCUS:
2922 current_panel = panel;
2923 panel->active = 1;
2924 if (mc_chdir (panel->cwd) != 0)
2926 char *cwd = strip_password (g_strdup (panel->cwd), 1);
2927 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
2928 cwd, unix_error_string (errno));
2929 g_free (cwd);
2931 else
2932 subshell_chdir (panel->cwd);
2934 update_xterm_title_path ();
2935 select_item (panel);
2936 show_dir (panel);
2937 paint_dir (panel);
2938 panel->dirty = 0;
2940 bb = find_buttonbar (panel->widget.owner);
2941 midnight_set_buttonbar (bb);
2942 buttonbar_redraw (bb);
2943 return MSG_HANDLED;
2945 case WIDGET_UNFOCUS:
2946 /* Janne: look at this for the multiple panel options */
2947 stop_search (panel);
2948 panel->active = 0;
2949 show_dir (panel);
2950 unselect_item (panel);
2951 return MSG_HANDLED;
2953 case WIDGET_KEY:
2954 return panel_key (panel, parm);
2956 case WIDGET_COMMAND:
2957 return panel_execute_cmd (panel, parm);
2959 case WIDGET_DESTROY:
2960 panel_destroy (panel);
2961 free_my_statfs ();
2962 return MSG_HANDLED;
2964 default:
2965 return default_proc (msg, parm);
2969 /* --------------------------------------------------------------------------------------------- */
2970 /* */
2971 /* Panel mouse events support routines */
2972 /* */
2974 static void
2975 mouse_toggle_mark (WPanel * panel)
2977 do_mark_file (panel, MARK_DONT_MOVE);
2978 mouse_marking = selection (panel)->f.marked;
2981 /* --------------------------------------------------------------------------------------------- */
2983 static void
2984 mouse_set_mark (WPanel * panel)
2986 if (mouse_marking && !(selection (panel)->f.marked))
2987 do_mark_file (panel, MARK_DONT_MOVE);
2988 else if (!mouse_marking && (selection (panel)->f.marked))
2989 do_mark_file (panel, MARK_DONT_MOVE);
2992 /* --------------------------------------------------------------------------------------------- */
2994 static int
2995 mark_if_marking (WPanel * panel, Gpm_Event * event)
2997 if (event->buttons & GPM_B_RIGHT)
2999 if (event->type & GPM_DOWN)
3000 mouse_toggle_mark (panel);
3001 else
3002 mouse_set_mark (panel);
3003 return 1;
3005 return 0;
3008 /* --------------------------------------------------------------------------------------------- */
3009 /** Determine which column was clicked, and sort the panel on
3010 * that column, or reverse sort on that column if already
3011 * sorted on that column.
3014 static void
3015 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3017 int i;
3018 const char *lc_sort_name = NULL;
3019 panel_field_t *col_sort_format = NULL;
3020 format_e *format;
3021 gchar *title;
3023 for (i = 0, format = panel->format; format != NULL; format = format->next)
3025 i += format->field_len;
3026 if (event->x < i + 1)
3028 /* found column */
3029 lc_sort_name = format->title;
3030 break;
3034 if (lc_sort_name == NULL)
3035 return;
3037 for (i = 0; panel_fields[i].id != NULL; i++)
3039 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3040 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3042 col_sort_format = &panel_fields[i];
3043 g_free (title);
3044 break;
3046 g_free (title);
3049 if (col_sort_format == NULL)
3050 return;
3052 if (panel->sort_info.sort_field == col_sort_format)
3054 /* reverse the sort if clicked column is already the sorted column */
3055 panel->sort_info.reverse = !panel->sort_info.reverse;
3057 else
3059 /* new sort is forced to be ascending */
3060 panel->sort_info.reverse = FALSE;
3062 panel_set_sort_order (panel, col_sort_format);
3066 /* --------------------------------------------------------------------------------------------- */
3068 * Mouse callback of the panel minus repainting.
3069 * If the event is redirected to the menu, *redir is set to TRUE.
3071 static int
3072 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3074 const int lines = llines (panel);
3075 const gboolean is_active = dlg_widget_active (panel);
3076 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3078 *redir = FALSE;
3080 /* 1st line */
3081 if (mouse_down && event->y == 1)
3083 /* "<" button */
3084 if (event->x == 2)
3086 directory_history_prev (panel);
3087 return MOU_NORMAL;
3090 /* "." button show/hide hidden files */
3091 if (event->x == panel->widget.cols - 5)
3093 panel->widget.owner->callback (panel->widget.owner, NULL,
3094 DLG_ACTION, CK_ToggleShowHidden, NULL);
3095 repaint_screen ();
3096 return MOU_NORMAL;
3099 /* ">" button */
3100 if (event->x == panel->widget.cols - 1)
3102 directory_history_next (panel);
3103 return MOU_NORMAL;
3106 /* "^" button */
3107 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3109 directory_history_list (panel);
3110 return MOU_NORMAL;
3113 /* rest of the upper frame, the menu is invisible - call menu */
3114 if (!menubar_visible)
3116 *redir = TRUE;
3117 event->x += panel->widget.x;
3118 return the_menubar->widget.mouse (event, the_menubar);
3121 /* no other events on 1st line */
3122 return MOU_NORMAL;
3125 /* sort on clicked column; don't handle wheel events */
3126 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3128 mouse_sort_col (event, panel);
3129 return MOU_NORMAL;
3132 /* Mouse wheel events */
3133 if (mouse_down && (event->buttons & GPM_B_UP))
3135 if (is_active)
3137 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3138 prev_page (panel);
3139 else /* We are in first page */
3140 move_up (panel);
3142 return MOU_NORMAL;
3145 if (mouse_down && (event->buttons & GPM_B_DOWN))
3147 if (is_active)
3149 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3150 next_page (panel);
3151 else /* We are in last page */
3152 move_down (panel);
3154 return MOU_NORMAL;
3157 event->y -= 2;
3158 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3160 int my_index;
3162 if (!is_active)
3163 change_panel ();
3165 if (panel->top_file + event->y > panel->count)
3166 my_index = panel->count - 1;
3167 else
3169 my_index = panel->top_file + event->y - 1;
3170 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3171 my_index += llines (panel);
3173 if (my_index >= panel->count)
3174 my_index = panel->count - 1;
3177 if (my_index != panel->selected)
3179 unselect_item (panel);
3180 panel->selected = my_index;
3181 select_item (panel);
3184 /* This one is new */
3185 mark_if_marking (panel, event);
3187 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3189 if (event->y > 0 && event->y <= lines)
3190 do_enter (panel);
3192 return MOU_NORMAL;
3195 /* --------------------------------------------------------------------------------------------- */
3196 /** Mouse callback of the panel */
3198 static int
3199 panel_event (Gpm_Event * event, void *data)
3201 WPanel *panel = data;
3202 int ret;
3203 gboolean redir;
3205 ret = do_panel_event (event, panel, &redir);
3206 if (!redir)
3207 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3209 return ret;
3212 /* --------------------------------------------------------------------------------------------- */
3214 static void
3215 reload_panelized (WPanel * panel)
3217 int i, j;
3218 dir_list *list = &panel->dir;
3220 if (panel != current_panel)
3222 int ret;
3223 ret = mc_chdir (panel->cwd);
3226 for (i = 0, j = 0; i < panel->count; i++)
3228 if (list->list[i].f.marked)
3230 /* Unmark the file in advance. In case the following mc_lstat
3231 * fails we are done, else we have to mark the file again
3232 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3233 * IMO that's the best way to update the panel's summary status
3234 * -- Norbert
3236 do_file_mark (panel, i, 0);
3238 if (mc_lstat (list->list[i].fname, &list->list[i].st))
3240 g_free (list->list[i].fname);
3241 continue;
3243 if (list->list[i].f.marked)
3244 do_file_mark (panel, i, 1);
3245 if (j != i)
3246 list->list[j] = list->list[i];
3247 j++;
3249 if (j == 0)
3250 panel->count = set_zero_dir (list) ? 1 : 0;
3251 else
3252 panel->count = j;
3254 if (panel != current_panel)
3256 int ret;
3257 ret = mc_chdir (current_panel->cwd);
3261 /* --------------------------------------------------------------------------------------------- */
3263 static void
3264 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3266 gboolean free_pointer;
3267 char *my_current_file = NULL;
3269 if ((flags & UP_RELOAD) != 0)
3271 panel->is_panelized = 0;
3272 mc_setctl (panel->cwd, VFS_SETCTL_FLUSH, 0);
3273 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3276 /* If current_file == -1 (an invalid pointer) then preserve selection */
3277 free_pointer = current_file == UP_KEEPSEL;
3279 if (free_pointer)
3281 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3282 current_file = my_current_file;
3285 if (panel->is_panelized)
3286 reload_panelized (panel);
3287 else
3288 panel_reload (panel);
3290 try_to_select (panel, current_file);
3291 panel->dirty = 1;
3293 if (free_pointer)
3294 g_free (my_current_file);
3297 /* --------------------------------------------------------------------------------------------- */
3299 static void
3300 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3302 if (get_display_type (which) == view_listing)
3304 WPanel *panel;
3305 panel = (WPanel *) get_panel_widget (which);
3306 update_one_panel_widget (panel, flags, current_file);
3310 /* --------------------------------------------------------------------------------------------- */
3311 /*** public functions ****************************************************************************/
3312 /* --------------------------------------------------------------------------------------------- */
3314 char *
3315 remove_encoding_from_path (const char *path)
3317 GString *ret;
3318 GString *tmp_path, *tmp_conv;
3319 char *tmp;
3321 ret = g_string_new ("");
3322 tmp_conv = g_string_new ("");
3323 tmp_path = g_string_new (path);
3325 while ((tmp = g_strrstr (tmp_path->str, PATH_SEP_STR VFS_ENCODING_PREFIX)) != NULL)
3327 const char *enc;
3328 GIConv converter;
3329 char *tmp2;
3331 enc = vfs_get_encoding ((const char *) tmp);
3332 converter = enc != NULL ? str_crt_conv_to (enc) : str_cnv_to_term;
3333 if (converter == INVALID_CONV)
3334 converter = str_cnv_to_term;
3336 tmp2 = tmp + 1;
3337 while (*tmp2 != '\0' && *tmp2 != PATH_SEP)
3338 tmp2++;
3340 if (*tmp2 != '\0')
3342 str_vfs_convert_from (converter, tmp2, tmp_conv);
3343 g_string_prepend (ret, tmp_conv->str);
3344 g_string_set_size (tmp_conv, 0);
3347 g_string_set_size (tmp_path, tmp - tmp_path->str);
3348 str_close_conv (converter);
3351 g_string_prepend (ret, tmp_path->str);
3352 g_string_free (tmp_path, TRUE);
3353 g_string_free (tmp_conv, TRUE);
3355 return g_string_free (ret, FALSE);
3358 /* --------------------------------------------------------------------------------------------- */
3360 static void
3361 do_select (WPanel * panel, int i)
3363 if (i != panel->selected)
3365 panel->dirty = 1;
3366 panel->selected = i;
3367 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3368 if (panel->top_file < 0)
3369 panel->top_file = 0;
3373 /* --------------------------------------------------------------------------------------------- */
3375 static void
3376 do_try_to_select (WPanel * panel, const char *name)
3378 int i;
3379 char *subdir;
3381 if (!name)
3383 do_select (panel, 0);
3384 return;
3387 /* We only want the last component of the directory,
3388 * and from this only the name without suffix. */
3389 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3391 /* Search that subdirectory, if found select it */
3392 for (i = 0; i < panel->count; i++)
3394 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3396 do_select (panel, i);
3397 g_free (subdir);
3398 return;
3402 /* Try to select a file near the file that is missing */
3403 if (panel->selected >= panel->count)
3404 do_select (panel, panel->count - 1);
3405 g_free (subdir);
3408 /* --------------------------------------------------------------------------------------------- */
3410 void
3411 try_to_select (WPanel * panel, const char *name)
3413 do_try_to_select (panel, name);
3414 select_item (panel);
3417 /* --------------------------------------------------------------------------------------------- */
3419 void
3420 panel_clean_dir (WPanel * panel)
3422 int count = panel->count;
3424 panel->count = 0;
3425 panel->top_file = 0;
3426 panel->selected = 0;
3427 panel->marked = 0;
3428 panel->dirs_marked = 0;
3429 panel->total = 0;
3430 panel->searching = FALSE;
3431 panel->is_panelized = 0;
3432 panel->dirty = 1;
3434 clean_dir (&panel->dir, count);
3437 /* --------------------------------------------------------------------------------------------- */
3438 /** Panel creation.
3439 * @param panel_name the name of the panel for setup retieving
3440 * @returns new instance of WPanel
3443 WPanel *
3444 panel_new (const char *panel_name)
3446 return panel_new_with_dir (panel_name, NULL);
3449 /* --------------------------------------------------------------------------------------------- */
3450 /** Panel creation for specified directory.
3451 * @param panel_name specifies the name of the panel for setup retieving
3452 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3453 * @returns new instance of WPanel
3456 WPanel *
3457 panel_new_with_dir (const char *panel_name, const char *wpath)
3459 WPanel *panel;
3460 char *section;
3461 int i, err;
3462 char curdir[MC_MAXPATHLEN] = "\0";
3464 panel = g_new0 (WPanel, 1);
3466 /* No know sizes of the panel at startup */
3467 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3469 /* We do not want the cursor */
3470 widget_want_cursor (panel->widget, 0);
3472 if (wpath != NULL)
3474 g_strlcpy (panel->cwd, wpath, sizeof (panel->cwd));
3475 mc_get_current_wd (curdir, sizeof (curdir) - 2);
3477 else
3478 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3480 strcpy (panel->lwd, ".");
3482 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3483 panel->dir_history = history_get (panel->hist_name);
3484 directory_history_add (panel, panel->cwd);
3486 panel->dir.list = g_new (file_entry, MIN_FILES);
3487 panel->dir.size = MIN_FILES;
3488 panel->active = 0;
3489 panel->filter = 0;
3490 panel->split = 0;
3491 panel->top_file = 0;
3492 panel->selected = 0;
3493 panel->marked = 0;
3494 panel->total = 0;
3495 panel->dirty = 1;
3496 panel->searching = FALSE;
3497 panel->dirs_marked = 0;
3498 panel->is_panelized = 0;
3499 panel->format = 0;
3500 panel->status_format = 0;
3501 panel->format_modified = 1;
3503 panel->panel_name = g_strdup (panel_name);
3504 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3506 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3508 for (i = 0; i < LIST_TYPES; i++)
3509 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3511 panel->search_buffer[0] = '\0';
3512 panel->prev_search_buffer[0] = '\0';
3513 panel->frame_size = frame_half;
3515 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3516 if (!mc_config_has_group (mc_main_config, section))
3518 g_free (section);
3519 section = g_strdup (panel->panel_name);
3521 panel_load_setup (panel, section);
3522 g_free (section);
3524 /* Load format strings */
3525 err = set_panel_formats (panel);
3526 if (err != 0)
3527 set_panel_formats (panel);
3529 #ifdef HAVE_CHARSET
3531 const char *enc = vfs_get_encoding (panel->cwd);
3532 if (enc != NULL)
3533 panel->codepage = get_codepage_index (enc);
3535 #endif
3537 if (mc_chdir (panel->cwd) != 0)
3539 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3540 mc_get_current_wd (panel->cwd, sizeof (panel->cwd) - 2);
3543 /* Load the default format */
3544 panel->count =
3545 do_load_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
3546 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3547 panel->sort_info.exec_first, panel->filter);
3549 /* Restore old right path */
3550 if (curdir[0] != '\0')
3551 err = mc_chdir (curdir);
3553 return panel;
3556 /* --------------------------------------------------------------------------------------------- */
3558 void
3559 panel_reload (WPanel * panel)
3561 struct stat current_stat;
3563 if (panels_options.fast_reload && !stat (panel->cwd, &current_stat)
3564 && current_stat.st_ctime == panel->dir_stat.st_ctime
3565 && current_stat.st_mtime == panel->dir_stat.st_mtime)
3566 return;
3568 while (mc_chdir (panel->cwd) == -1)
3570 char *last_slash;
3572 if (panel->cwd[0] == PATH_SEP && panel->cwd[1] == 0)
3574 panel_clean_dir (panel);
3575 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
3576 return;
3578 last_slash = strrchr (panel->cwd, PATH_SEP);
3579 if (!last_slash || last_slash == panel->cwd)
3580 strcpy (panel->cwd, PATH_SEP_STR);
3581 else
3582 *last_slash = 0;
3583 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3584 show_dir (panel);
3587 panel->count =
3588 do_reload_dir (panel->cwd, &panel->dir, panel->sort_info.sort_field->sort_routine,
3589 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
3590 panel->sort_info.exec_first, panel->filter);
3592 panel->dirty = 1;
3593 if (panel->selected >= panel->count)
3594 do_select (panel, panel->count - 1);
3596 recalculate_panel_summary (panel);
3599 /* --------------------------------------------------------------------------------------------- */
3600 /* Switches the panel to the mode specified in the format */
3601 /* Seting up both format and status string. Return: 0 - on success; */
3602 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
3605 set_panel_formats (WPanel * p)
3607 format_e *form;
3608 char *err = NULL;
3609 int retcode = 0;
3611 form = use_display_format (p, panel_format (p), &err, 0);
3613 if (err != NULL)
3615 g_free (err);
3616 retcode = 1;
3618 else
3620 delete_format (p->format);
3621 p->format = form;
3624 if (panels_options.show_mini_info)
3626 form = use_display_format (p, mini_status_format (p), &err, 1);
3628 if (err != NULL)
3630 g_free (err);
3631 retcode += 2;
3633 else
3635 delete_format (p->status_format);
3636 p->status_format = form;
3640 panel_format_modified (p);
3641 panel_update_cols (&(p->widget), p->frame_size);
3643 if (retcode)
3644 message (D_ERROR, _("Warning"),
3645 _("User supplied format looks invalid, reverting to default."));
3646 if (retcode & 0x01)
3648 g_free (p->user_format);
3649 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
3651 if (retcode & 0x02)
3653 g_free (p->user_status_format[p->list_type]);
3654 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
3657 return retcode;
3660 /* --------------------------------------------------------------------------------------------- */
3662 /* Select current item and readjust the panel */
3663 void
3664 select_item (WPanel * panel)
3666 int items = ITEMS (panel);
3668 /* Although currently all over the code we set the selection and
3669 top file to decent values before calling select_item, I could
3670 forget it someday, so it's better to do the actual fitting here */
3672 if (panel->top_file < 0)
3673 panel->top_file = 0;
3675 if (panel->selected < 0)
3676 panel->selected = 0;
3678 if (panel->selected > panel->count - 1)
3679 panel->selected = panel->count - 1;
3681 if (panel->top_file > panel->count - 1)
3682 panel->top_file = panel->count - 1;
3684 if ((panel->count - panel->top_file) < items)
3686 panel->top_file = panel->count - items;
3687 if (panel->top_file < 0)
3688 panel->top_file = 0;
3691 if (panel->selected < panel->top_file)
3692 panel->top_file = panel->selected;
3694 if ((panel->selected - panel->top_file) >= items)
3695 panel->top_file = panel->selected - items + 1;
3697 panel->dirty = 1;
3699 execute_hooks (select_file_hook);
3702 /* --------------------------------------------------------------------------------------------- */
3703 /** Clears all files in the panel, used only when one file was marked */
3704 void
3705 unmark_files (WPanel * panel)
3707 int i;
3709 if (!panel->marked)
3710 return;
3711 for (i = 0; i < panel->count; i++)
3712 file_mark (panel, i, 0);
3714 panel->dirs_marked = 0;
3715 panel->marked = 0;
3716 panel->total = 0;
3719 /* --------------------------------------------------------------------------------------------- */
3720 /** Recalculate the panels summary information, used e.g. when marked
3721 files might have been removed by an external command */
3723 void
3724 recalculate_panel_summary (WPanel * panel)
3726 int i;
3728 panel->marked = 0;
3729 panel->dirs_marked = 0;
3730 panel->total = 0;
3732 for (i = 0; i < panel->count; i++)
3733 if (panel->dir.list[i].f.marked)
3735 /* do_file_mark will return immediately if newmark == oldmark.
3736 So we have to first unmark it to get panel's summary information
3737 updated. (Norbert) */
3738 panel->dir.list[i].f.marked = 0;
3739 do_file_mark (panel, i, 1);
3743 /* --------------------------------------------------------------------------------------------- */
3744 /** This routine marks a file or a directory */
3746 void
3747 do_file_mark (WPanel * panel, int idx, int mark)
3749 if (panel->dir.list[idx].f.marked == mark)
3750 return;
3752 /* Only '..' can't be marked, '.' isn't visible */
3753 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
3754 return;
3756 file_mark (panel, idx, mark);
3757 if (panel->dir.list[idx].f.marked)
3759 panel->marked++;
3760 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
3762 if (panel->dir.list[idx].f.dir_size_computed)
3763 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
3764 panel->dirs_marked++;
3766 else
3767 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
3768 set_colors (panel);
3770 else
3772 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
3774 if (panel->dir.list[idx].f.dir_size_computed)
3775 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
3776 panel->dirs_marked--;
3778 else
3779 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
3780 panel->marked--;
3784 /* --------------------------------------------------------------------------------------------- */
3786 * Changes the current directory of the panel.
3787 * Record change in the directory history.
3789 gboolean
3790 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
3792 gboolean r;
3794 r = _do_panel_cd (panel, new_dir, cd_type);
3795 if (r)
3796 directory_history_add (panel, panel->cwd);
3797 return r;
3800 /* --------------------------------------------------------------------------------------------- */
3802 void
3803 file_mark (WPanel * panel, int lc_index, int val)
3805 if (panel->dir.list[lc_index].f.marked != val)
3807 panel->dir.list[lc_index].f.marked = val;
3808 panel->dirty = 1;
3812 /* --------------------------------------------------------------------------------------------- */
3814 void
3815 panel_re_sort (WPanel * panel)
3817 char *filename;
3818 int i;
3820 if (panel == NULL)
3821 return;
3823 filename = g_strdup (selection (panel)->fname);
3824 unselect_item (panel);
3825 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
3826 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3827 panel->sort_info.exec_first);
3828 panel->selected = -1;
3829 for (i = panel->count; i; i--)
3831 if (!strcmp (panel->dir.list[i - 1].fname, filename))
3833 panel->selected = i - 1;
3834 break;
3837 g_free (filename);
3838 panel->top_file = panel->selected - ITEMS (panel) / 2;
3839 select_item (panel);
3840 panel->dirty = 1;
3843 /* --------------------------------------------------------------------------------------------- */
3845 void
3846 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
3848 if (sort_order == NULL)
3849 return;
3851 panel->sort_info.sort_field = sort_order;
3853 /* The directory is already sorted, we have to load the unsorted stuff */
3854 if (sort_order->sort_routine == (sortfn *) unsorted)
3856 char *current_file;
3858 current_file = g_strdup (panel->dir.list[panel->selected].fname);
3859 panel_reload (panel);
3860 try_to_select (panel, current_file);
3861 g_free (current_file);
3863 panel_re_sort (panel);
3866 /* --------------------------------------------------------------------------------------------- */
3868 * Change panel encoding.
3869 * @param panel WPanel object
3872 void
3873 panel_change_encoding (WPanel * panel)
3875 const char *encoding = NULL;
3876 char *cd_path;
3877 #ifdef HAVE_CHARSET
3878 char *errmsg;
3879 int r;
3881 r = select_charset (-1, -1, panel->codepage, FALSE);
3883 if (r == SELECT_CHARSET_CANCEL)
3884 return; /* Cancel */
3886 panel->codepage = r;
3888 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
3890 /* No translation */
3891 g_free (init_translation_table (display_codepage, display_codepage));
3892 cd_path = remove_encoding_from_path (panel->cwd);
3893 do_panel_cd (panel, cd_path, cd_parse_command);
3894 g_free (cd_path);
3895 return;
3898 errmsg = init_translation_table (panel->codepage, display_codepage);
3899 if (errmsg != NULL)
3901 message (D_ERROR, MSG_ERROR, "%s", errmsg);
3902 g_free (errmsg);
3903 return;
3906 encoding = get_codepage_id (panel->codepage);
3907 #endif
3908 if (encoding != NULL)
3910 const char *enc;
3912 enc = vfs_get_encoding (panel->cwd);
3914 /* don't add current encoding */
3915 if ((enc == NULL) || (strcmp (encoding, enc) != 0))
3917 cd_path = add_encoding_to_path (panel->cwd, encoding);
3918 if (!do_panel_cd (panel, cd_path, cd_parse_command))
3919 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
3920 g_free (cd_path);
3925 /* --------------------------------------------------------------------------------------------- */
3927 * This routine reloads the directory in both panels. It tries to
3928 * select current_file in current_panel and other_file in other_panel.
3929 * If current_file == -1 then it automatically sets current_file and
3930 * other_file to the currently selected files in the panels.
3932 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
3933 * will not reload the other panel.
3936 void
3937 update_panels (panel_update_flags_t flags, const char *current_file)
3939 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
3940 WPanel *panel;
3941 int ret;
3943 update_one_panel (get_current_index (), flags, current_file);
3944 if (reload_other)
3945 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
3947 if (get_current_type () == view_listing)
3948 panel = (WPanel *) get_panel_widget (get_current_index ());
3949 else
3950 panel = (WPanel *) get_panel_widget (get_other_index ());
3952 ret = mc_chdir (panel->cwd);
3955 /* --------------------------------------------------------------------------------------------- */
3957 void
3958 directory_history_add (struct WPanel *panel, const char *dir)
3960 char *tmp;
3962 tmp = g_strdup (dir);
3963 strip_password (tmp, 1);
3965 panel->dir_history = list_append_unique (panel->dir_history, tmp);
3968 /* --------------------------------------------------------------------------------------------- */
3970 gsize
3971 panel_get_num_of_sortable_fields (void)
3973 gsize ret = 0, lc_index;
3975 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
3976 if (panel_fields[lc_index].is_user_choice)
3977 ret++;
3978 return ret;
3981 /* --------------------------------------------------------------------------------------------- */
3983 const char **
3984 panel_get_sortable_fields (gsize * array_size)
3986 char **ret;
3987 gsize lc_index, i;
3989 lc_index = panel_get_num_of_sortable_fields ();
3991 ret = g_try_new0 (char *, lc_index + 1);
3992 if (ret == NULL)
3993 return NULL;
3995 if (array_size != NULL)
3996 *array_size = lc_index;
3998 lc_index = 0;
4000 for (i = 0; panel_fields[i].id != NULL; i++)
4001 if (panel_fields[i].is_user_choice)
4002 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4003 return (const char **) ret;
4006 /* --------------------------------------------------------------------------------------------- */
4008 const panel_field_t *
4009 panel_get_field_by_id (const char *name)
4011 gsize lc_index;
4012 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4013 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4014 return &panel_fields[lc_index];
4015 return NULL;
4018 /* --------------------------------------------------------------------------------------------- */
4020 const panel_field_t *
4021 panel_get_field_by_title_hotkey (const char *name)
4023 gsize lc_index;
4024 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4025 if (panel_fields[lc_index].title_hotkey != NULL &&
4026 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4027 return &panel_fields[lc_index];
4028 return NULL;
4031 /* --------------------------------------------------------------------------------------------- */
4033 const panel_field_t *
4034 panel_get_field_by_title (const char *name)
4036 gsize lc_index;
4037 gchar *title = NULL;
4039 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4041 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4042 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4044 g_free (title);
4045 return &panel_fields[lc_index];
4048 g_free (title);
4049 return NULL;
4052 /* --------------------------------------------------------------------------------------------- */
4054 gsize
4055 panel_get_num_of_user_possible_fields (void)
4057 gsize ret = 0, lc_index;
4059 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4060 if (panel_fields[lc_index].use_in_user_format)
4061 ret++;
4062 return ret;
4065 /* --------------------------------------------------------------------------------------------- */
4067 const char **
4068 panel_get_user_possible_fields (gsize * array_size)
4070 char **ret;
4071 gsize lc_index, i;
4073 lc_index = panel_get_num_of_user_possible_fields ();
4075 ret = g_try_new0 (char *, lc_index + 1);
4076 if (ret == NULL)
4077 return NULL;
4079 if (array_size != NULL)
4080 *array_size = lc_index;
4082 lc_index = 0;
4084 for (i = 0; panel_fields[i].id != NULL; i++)
4085 if (panel_fields[i].use_in_user_format)
4086 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4087 return (const char **) ret;
4090 /* --------------------------------------------------------------------------------------------- */
4092 void
4093 panel_init (void)
4095 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4096 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4098 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4099 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4100 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4101 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4102 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4106 /* --------------------------------------------------------------------------------------------- */
4108 void
4109 panel_deinit (void)
4111 g_free (panel_sort_up_sign);
4112 g_free (panel_sort_down_sign);
4114 g_free (panel_hiddenfiles_sign_show);
4115 g_free (panel_hiddenfiles_sign_hide);
4116 g_free (panel_history_prev_item_sign);
4117 g_free (panel_history_next_item_sign);
4118 g_free (panel_history_show_list_sign);
4122 /* --------------------------------------------------------------------------------------------- */