Ticket #2827: tweak and cleanup of code in case of --disable-charset option usage.
[midnight-commander.git] / src / filemanager / panel.c
blob56ecd7b2195c9bc77d5fd196cec453b6cdf6bf99
1 /*
2 Panel managing.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Miguel de Icaza, 1995
10 Timur Bakeyev, 1997, 1999
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file panel.c
29 * \brief Source: panel managin module
32 #include <config.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h" /* For Gpm_Event */
44 #include "lib/tty/key.h" /* XCTRL and ALT macros */
45 #include "lib/skin.h"
46 #include "lib/strescape.h"
47 #include "lib/filehighlight.h"
48 #include "lib/mcconfig.h"
49 #include "lib/vfs/vfs.h"
50 #include "lib/unixcompat.h"
51 #include "lib/timefmt.h"
52 #include "lib/util.h"
53 #include "lib/widget.h"
54 #ifdef HAVE_CHARSET
55 #include "lib/charsets.h" /* get_codepage_id () */
56 #endif
57 #include "lib/event.h"
59 #include "src/setup.h" /* For loading/saving panel options */
60 #include "src/execute.h"
61 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
62 #include "src/keybind-defaults.h" /* global_keymap_t */
63 #include "src/subshell.h" /* do_subshell_chdir() */
65 #include "dir.h"
66 #include "boxes.h"
67 #include "tree.h"
68 #include "ext.h" /* regexp_command */
69 #include "layout.h" /* Most layout variables are here */
70 #include "cmd.h"
71 #include "command.h" /* cmdline */
72 #include "usermenu.h"
73 #include "midnight.h"
74 #include "mountlist.h" /* my_statfs */
76 #include "panel.h"
78 /*** global variables ****************************************************************************/
80 /* The hook list for the select file function */
81 hook_t *select_file_hook = NULL;
83 /* *INDENT-OFF* */
84 panelized_panel_t panelized_panel = { {NULL, 0}, -1, NULL };
85 /* *INDENT-ON* */
87 static const char *string_file_name (file_entry *, int);
88 static const char *string_file_size (file_entry *, int);
89 static const char *string_file_size_brief (file_entry *, int);
90 static const char *string_file_type (file_entry *, int);
91 static const char *string_file_mtime (file_entry *, int);
92 static const char *string_file_atime (file_entry *, int);
93 static const char *string_file_ctime (file_entry *, int);
94 static const char *string_file_permission (file_entry *, int);
95 static const char *string_file_perm_octal (file_entry *, int);
96 static const char *string_file_nlinks (file_entry *, int);
97 static const char *string_inode (file_entry *, int);
98 static const char *string_file_nuid (file_entry *, int);
99 static const char *string_file_ngid (file_entry *, int);
100 static const char *string_file_owner (file_entry *, int);
101 static const char *string_file_group (file_entry *, int);
102 static const char *string_marked (file_entry *, int);
103 static const char *string_space (file_entry *, int);
104 static const char *string_dot (file_entry *, int);
106 /* *INDENT-OFF* */
107 panel_field_t panel_fields[] = {
109 "unsorted", 12, 1, J_LEFT_FIT,
110 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
111 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
112 N_("sort|u"),
113 N_("&Unsorted"), TRUE, FALSE,
114 string_file_name,
115 (sortfn *) unsorted
119 "name", 12, 1, J_LEFT_FIT,
120 /* TRANSLATORS: one single character to represent 'name' sort mode */
121 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
122 N_("sort|n"),
123 N_("&Name"), TRUE, TRUE,
124 string_file_name,
125 (sortfn *) sort_name
129 "version", 12, 1, J_LEFT_FIT,
130 /* TRANSLATORS: one single character to represent 'version' sort mode */
131 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
132 N_("sort|v"),
133 N_("&Version"), TRUE, FALSE,
134 string_file_name,
135 (sortfn *) sort_vers
139 "extension", 12, 1, J_LEFT_FIT,
140 /* TRANSLATORS: one single character to represent 'extension' sort mode */
141 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
142 N_("sort|e"),
143 N_("&Extension"), TRUE, FALSE,
144 string_file_name, /* TODO: string_file_ext */
145 (sortfn *) sort_ext
149 "size", 7, 0, J_RIGHT,
150 /* TRANSLATORS: one single character to represent 'size' sort mode */
151 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
152 N_("sort|s"),
153 N_("&Size"), TRUE, TRUE,
154 string_file_size,
155 (sortfn *) sort_size
159 "bsize", 7, 0, J_RIGHT,
161 N_("Block Size"), FALSE, FALSE,
162 string_file_size_brief,
163 (sortfn *) sort_size
167 "type", 1, 0, J_LEFT,
169 "", FALSE, TRUE,
170 string_file_type,
171 NULL
175 "mtime", 12, 0, J_RIGHT,
176 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
177 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
178 N_("sort|m"),
179 N_("&Modify time"), TRUE, TRUE,
180 string_file_mtime,
181 (sortfn *) sort_time
185 "atime", 12, 0, J_RIGHT,
186 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
187 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
188 N_("sort|a"),
189 N_("&Access time"), TRUE, TRUE,
190 string_file_atime,
191 (sortfn *) sort_atime
195 "ctime", 12, 0, J_RIGHT,
196 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
197 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
198 N_("sort|h"),
199 N_("C&hange time"), TRUE, TRUE,
200 string_file_ctime,
201 (sortfn *) sort_ctime
205 "perm", 10, 0, J_LEFT,
207 N_("Permission"), FALSE, TRUE,
208 string_file_permission,
209 NULL
213 "mode", 6, 0, J_RIGHT,
215 N_("Perm"), FALSE, TRUE,
216 string_file_perm_octal,
217 NULL
221 "nlink", 2, 0, J_RIGHT,
223 N_("Nl"), FALSE, TRUE,
224 string_file_nlinks, NULL
228 "inode", 5, 0, J_RIGHT,
229 /* TRANSLATORS: one single character to represent 'inode' sort mode */
230 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
231 N_("sort|i"),
232 N_("&Inode"), TRUE, TRUE,
233 string_inode,
234 (sortfn *) sort_inode
238 "nuid", 5, 0, J_RIGHT,
240 N_("UID"), FALSE, FALSE,
241 string_file_nuid,
242 NULL
246 "ngid", 5, 0, J_RIGHT,
248 N_("GID"), FALSE, FALSE,
249 string_file_ngid,
250 NULL
254 "owner", 8, 0, J_LEFT_FIT,
256 N_("Owner"), FALSE, TRUE,
257 string_file_owner,
258 NULL
262 "group", 8, 0, J_LEFT_FIT,
264 N_("Group"), FALSE, TRUE,
265 string_file_group,
266 NULL
270 "mark", 1, 0, J_RIGHT,
272 " ", FALSE, TRUE,
273 string_marked,
274 NULL
278 "|", 1, 0, J_RIGHT,
280 " ", FALSE, TRUE,
281 NULL,
282 NULL
286 "space", 1, 0, J_RIGHT,
288 " ", FALSE, TRUE,
289 string_space,
290 NULL
294 "dot", 1, 0, J_RIGHT,
296 " ", FALSE, FALSE,
297 string_dot,
298 NULL
302 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
305 /* *INDENT-ON* */
307 extern int saving_setup;
309 /*** file scope macro definitions ****************************************************************/
311 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
313 #define NORMAL 0
314 #define SELECTED 1
315 #define MARKED 2
316 #define MARKED_SELECTED 3
317 #define STATUS 5
319 /* This macro extracts the number of available lines in a panel */
320 #define llines(p) (p->widget.lines - 3 - (panels_options.show_mini_info ? 2 : 0))
322 /*** file scope type declarations ****************************************************************/
324 typedef enum
326 MARK_DONT_MOVE = 0,
327 MARK_DOWN = 1,
328 MARK_FORCE_DOWN = 2,
329 MARK_FORCE_UP = 3
330 } mark_act_t;
333 * This describes a format item. The parse_display_format routine parses
334 * the user specified format and creates a linked list of format_e structures.
336 typedef struct format_e
338 struct format_e *next;
339 int requested_field_len;
340 int field_len;
341 align_crt_t just_mode;
342 int expand;
343 const char *(*string_fn) (file_entry *, int len);
344 char *title;
345 const char *id;
346 } format_e;
348 /*** file scope variables ************************************************************************/
350 static char *panel_sort_up_sign = NULL;
351 static char *panel_sort_down_sign = NULL;
353 static char *panel_hiddenfiles_sign_show = NULL;
354 static char *panel_hiddenfiles_sign_hide = NULL;
355 static char *panel_history_prev_item_sign = NULL;
356 static char *panel_history_next_item_sign = NULL;
357 static char *panel_history_show_list_sign = NULL;
359 /* Panel that selection started */
360 static WPanel *mouse_mark_panel = NULL;
362 static int mouse_marking = 0;
363 static int state_mark = 0;
364 /*** file scope functions ************************************************************************/
365 /* --------------------------------------------------------------------------------------------- */
367 static void
368 set_colors (WPanel * panel)
370 (void) panel;
371 tty_set_normal_attrs ();
372 tty_setcolor (NORMAL_COLOR);
375 /* --------------------------------------------------------------------------------------------- */
376 /** Delete format string, it is a linked list */
378 static void
379 delete_format (format_e * format)
381 while (format != NULL)
383 format_e *next = format->next;
384 g_free (format->title);
385 g_free (format);
386 format = next;
390 /* --------------------------------------------------------------------------------------------- */
391 /** This code relies on the default justification!!! */
393 static void
394 add_permission_string (char *dest, int width, file_entry * fe, int attr, int color, int is_octal)
396 int i, r, l;
398 l = get_user_permissions (&fe->st);
400 if (is_octal)
402 /* Place of the access bit in octal mode */
403 l = width + l - 3;
404 r = l + 1;
406 else
408 /* The same to the triplet in string mode */
409 l = l * 3 + 1;
410 r = l + 3;
413 for (i = 0; i < width; i++)
415 if (i >= l && i < r)
417 if (attr == SELECTED || attr == MARKED_SELECTED)
418 tty_setcolor (MARKED_SELECTED_COLOR);
419 else
420 tty_setcolor (MARKED_COLOR);
422 else if (color >= 0)
423 tty_setcolor (color);
424 else
425 tty_lowlevel_setcolor (-color);
427 tty_print_char (dest[i]);
431 /* --------------------------------------------------------------------------------------------- */
432 /** String representations of various file attributes name */
434 static const char *
435 string_file_name (file_entry * fe, int len)
437 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
439 (void) len;
440 g_strlcpy (buffer, fe->fname, sizeof (buffer));
441 return buffer;
444 /* --------------------------------------------------------------------------------------------- */
446 static unsigned int
447 ilog10 (dev_t n)
449 unsigned int digits = 0;
452 digits++, n /= 10;
454 while (n != 0);
455 return digits;
458 /* --------------------------------------------------------------------------------------------- */
460 static void
461 format_device_number (char *buf, size_t bufsize, dev_t dev)
463 dev_t major_dev = major (dev);
464 dev_t minor_dev = minor (dev);
465 unsigned int major_digits = ilog10 (major_dev);
466 unsigned int minor_digits = ilog10 (minor_dev);
468 g_assert (bufsize >= 1);
469 if (major_digits + 1 + minor_digits + 1 <= bufsize)
471 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
473 else
475 g_strlcpy (buf, _("[dev]"), bufsize);
479 /* --------------------------------------------------------------------------------------------- */
480 /** size */
482 static const char *
483 string_file_size (file_entry * fe, int len)
485 static char buffer[BUF_TINY];
487 /* Don't ever show size of ".." since we don't calculate it */
488 if (!strcmp (fe->fname, ".."))
490 return _("UP--DIR");
493 #ifdef HAVE_STRUCT_STAT_ST_RDEV
494 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
495 format_device_number (buffer, len + 1, fe->st.st_rdev);
496 else
497 #endif
499 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
501 return buffer;
504 /* --------------------------------------------------------------------------------------------- */
505 /** bsize */
507 static const char *
508 string_file_size_brief (file_entry * fe, int len)
510 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
512 return _("SYMLINK");
515 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
517 return _("SUB-DIR");
520 return string_file_size (fe, len);
523 /* --------------------------------------------------------------------------------------------- */
524 /** This functions return a string representation of a file entry type */
526 static const char *
527 string_file_type (file_entry * fe, int len)
529 static char buffer[2];
531 (void) len;
532 if (S_ISDIR (fe->st.st_mode))
533 buffer[0] = PATH_SEP;
534 else if (S_ISLNK (fe->st.st_mode))
536 if (fe->f.link_to_dir)
537 buffer[0] = '~';
538 else if (fe->f.stale_link)
539 buffer[0] = '!';
540 else
541 buffer[0] = '@';
543 else if (S_ISCHR (fe->st.st_mode))
544 buffer[0] = '-';
545 else if (S_ISSOCK (fe->st.st_mode))
546 buffer[0] = '=';
547 else if (S_ISDOOR (fe->st.st_mode))
548 buffer[0] = '>';
549 else if (S_ISBLK (fe->st.st_mode))
550 buffer[0] = '+';
551 else if (S_ISFIFO (fe->st.st_mode))
552 buffer[0] = '|';
553 else if (S_ISNAM (fe->st.st_mode))
554 buffer[0] = '#';
555 else if (!S_ISREG (fe->st.st_mode))
556 buffer[0] = '?'; /* non-regular of unknown kind */
557 else if (is_exe (fe->st.st_mode))
558 buffer[0] = '*';
559 else
560 buffer[0] = ' ';
561 buffer[1] = '\0';
562 return buffer;
565 /* --------------------------------------------------------------------------------------------- */
566 /** mtime */
568 static const char *
569 string_file_mtime (file_entry * fe, int len)
571 (void) len;
572 return file_date (fe->st.st_mtime);
575 /* --------------------------------------------------------------------------------------------- */
576 /** atime */
578 static const char *
579 string_file_atime (file_entry * fe, int len)
581 (void) len;
582 return file_date (fe->st.st_atime);
585 /* --------------------------------------------------------------------------------------------- */
586 /** ctime */
588 static const char *
589 string_file_ctime (file_entry * fe, int len)
591 (void) len;
592 return file_date (fe->st.st_ctime);
595 /* --------------------------------------------------------------------------------------------- */
596 /** perm */
598 static const char *
599 string_file_permission (file_entry * fe, int len)
601 (void) len;
602 return string_perm (fe->st.st_mode);
605 /* --------------------------------------------------------------------------------------------- */
606 /** mode */
608 static const char *
609 string_file_perm_octal (file_entry * fe, int len)
611 static char buffer[10];
613 (void) len;
614 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
615 return buffer;
618 /* --------------------------------------------------------------------------------------------- */
619 /** nlink */
621 static const char *
622 string_file_nlinks (file_entry * fe, int len)
624 static char buffer[BUF_TINY];
626 (void) len;
627 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
628 return buffer;
631 /* --------------------------------------------------------------------------------------------- */
632 /** inode */
634 static const char *
635 string_inode (file_entry * fe, int len)
637 static char buffer[10];
639 (void) len;
640 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
641 return buffer;
644 /* --------------------------------------------------------------------------------------------- */
645 /** nuid */
647 static const char *
648 string_file_nuid (file_entry * fe, int len)
650 static char buffer[10];
652 (void) len;
653 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
654 return buffer;
657 /* --------------------------------------------------------------------------------------------- */
658 /** ngid */
660 static const char *
661 string_file_ngid (file_entry * fe, int len)
663 static char buffer[10];
665 (void) len;
666 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
667 return buffer;
670 /* --------------------------------------------------------------------------------------------- */
671 /** owner */
673 static const char *
674 string_file_owner (file_entry * fe, int len)
676 (void) len;
677 return get_owner (fe->st.st_uid);
680 /* --------------------------------------------------------------------------------------------- */
681 /** group */
683 static const char *
684 string_file_group (file_entry * fe, int len)
686 (void) len;
687 return get_group (fe->st.st_gid);
690 /* --------------------------------------------------------------------------------------------- */
691 /** mark */
693 static const char *
694 string_marked (file_entry * fe, int len)
696 (void) len;
697 return fe->f.marked ? "*" : " ";
700 /* --------------------------------------------------------------------------------------------- */
701 /** space */
703 static const char *
704 string_space (file_entry * fe, int len)
706 (void) fe;
707 (void) len;
708 return " ";
711 /* --------------------------------------------------------------------------------------------- */
712 /** dot */
714 static const char *
715 string_dot (file_entry * fe, int len)
717 (void) fe;
718 (void) len;
719 return ".";
722 /* --------------------------------------------------------------------------------------------- */
724 static int
725 file_compute_color (int attr, file_entry * fe)
727 switch (attr)
729 case SELECTED:
730 return (SELECTED_COLOR);
731 case MARKED:
732 return (MARKED_COLOR);
733 case MARKED_SELECTED:
734 return (MARKED_SELECTED_COLOR);
735 case STATUS:
736 return (NORMAL_COLOR);
737 case NORMAL:
738 default:
739 if (!panels_options.filetype_mode)
740 return (NORMAL_COLOR);
743 return mc_fhl_get_color (mc_filehighlight, fe);
746 /* --------------------------------------------------------------------------------------------- */
747 /** Formats the file number file_index of panel in the buffer dest */
749 static void
750 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
751 int isstatus)
753 int color, length, empty_line;
754 const char *txt;
755 format_e *format, *home;
756 file_entry *fe;
758 (void) dest;
759 (void) limit;
760 length = 0;
761 empty_line = (file_index >= panel->count);
762 home = (isstatus) ? panel->status_format : panel->format;
763 fe = &panel->dir.list[file_index];
765 if (!empty_line)
766 color = file_compute_color (attr, fe);
767 else
768 color = NORMAL_COLOR;
770 for (format = home; format; format = format->next)
772 if (length == width)
773 break;
775 if (format->string_fn)
777 int len, perm;
778 char *preperad_text;
780 if (empty_line)
781 txt = " ";
782 else
783 txt = (*format->string_fn) (fe, format->field_len);
785 len = format->field_len;
786 if (len + length > width)
787 len = width - length;
788 if (len <= 0)
789 break;
791 perm = 0;
792 if (panels_options.permission_mode)
794 if (!strcmp (format->id, "perm"))
795 perm = 1;
796 else if (!strcmp (format->id, "mode"))
797 perm = 2;
800 if (color >= 0)
801 tty_setcolor (color);
802 else
803 tty_lowlevel_setcolor (-color);
805 preperad_text = (char *) str_fit_to_term (txt, len, format->just_mode);
807 if (perm)
808 add_permission_string (preperad_text, format->field_len, fe, attr, color, perm - 1);
809 else
810 tty_print_string (preperad_text);
812 length += len;
814 else
816 if (attr == SELECTED || attr == MARKED_SELECTED)
817 tty_setcolor (SELECTED_COLOR);
818 else
819 tty_setcolor (NORMAL_COLOR);
820 tty_print_one_vline (TRUE);
821 length++;
825 if (length < width)
826 tty_draw_hline (-1, -1, ' ', width - length);
829 /* --------------------------------------------------------------------------------------------- */
831 static void
832 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
834 int second_column = 0;
835 int width;
836 int offset = 0;
837 char buffer[BUF_MEDIUM];
839 gboolean panel_is_split = !isstatus && panel->split;
841 width = panel->widget.cols - 2;
843 if (panel_is_split)
845 second_column = (file_index - panel->top_file) / llines (panel);
846 width = width / 2 - 1;
848 if (second_column != 0)
850 offset = 1 + width;
851 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
852 width = panel->widget.cols - offset - 2;
856 /* Nothing to paint */
857 if (width <= 0)
858 return;
860 if (mv)
862 if (panel_is_split)
863 widget_move (&panel->widget,
864 (file_index - panel->top_file) % llines (panel) + 2, offset + 1);
865 else
866 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
869 format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus);
871 if (panel_is_split)
873 if (second_column)
874 tty_print_char (' ');
875 else
877 tty_setcolor (NORMAL_COLOR);
878 tty_print_one_vline (TRUE);
883 /* --------------------------------------------------------------------------------------------- */
885 static void
886 display_mini_info (WPanel * panel)
888 if (!panels_options.show_mini_info)
889 return;
891 widget_move (&panel->widget, llines (panel) + 3, 1);
893 if (panel->searching)
895 tty_setcolor (INPUT_COLOR);
896 tty_print_char ('/');
897 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
898 return;
901 /* Status resolves links and show them */
902 set_colors (panel);
904 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
906 char link_target[MC_MAXPATHLEN];
907 vfs_path_t *lc_link_vpath;
908 int len;
910 lc_link_vpath =
911 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
912 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
913 vfs_path_free (lc_link_vpath);
914 if (len > 0)
916 link_target[len] = 0;
917 tty_print_string ("-> ");
918 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
920 else
921 tty_print_string (str_fit_to_term (_("<readlink failed>"),
922 panel->widget.cols - 2, J_LEFT));
924 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
926 /* FIXME:
927 * while loading directory (do_load_dir() and do_reload_dir()),
928 * the actual stat info about ".." directory isn't got;
929 * so just don't display incorrect info about ".." directory */
930 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
932 else
933 /* Default behavior */
934 repaint_file (panel, panel->selected, 0, STATUS, 1);
937 /* --------------------------------------------------------------------------------------------- */
939 static void
940 paint_dir (WPanel * panel)
942 int i;
943 int color; /* Color value of the line */
944 int items; /* Number of items */
946 items = llines (panel) * (panel->split ? 2 : 1);
948 for (i = 0; i < items; i++)
950 if (i + panel->top_file >= panel->count)
951 color = 0;
952 else
954 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
955 color += (panel->selected == i + panel->top_file && panel->active);
957 repaint_file (panel, i + panel->top_file, 1, color, 0);
959 tty_set_normal_attrs ();
962 /* --------------------------------------------------------------------------------------------- */
964 static void
965 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
967 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
968 int cols;
970 if (panel->marked <= 0)
971 return;
973 buf = size_only ? b_bytes : buffer;
974 cols = panel->widget.cols - 2;
977 * This is a trick to use two ngettext() calls in one sentence.
978 * First make "N bytes", then insert it into "X in M files".
980 g_snprintf (b_bytes, sizeof (b_bytes),
981 ngettext ("%s byte", "%s bytes", panel->total),
982 size_trunc_sep (panel->total, panels_options.kilobyte_si));
983 if (!size_only)
984 g_snprintf (buffer, sizeof (buffer),
985 ngettext ("%s in %d file", "%s in %d files", panel->marked),
986 b_bytes, panel->marked);
988 /* don't forget spaces around buffer content */
989 buf = (char *) str_trunc (buf, cols - 4);
991 if (x < 0)
992 /* center in panel */
993 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
996 * y == llines (panel) + 2 for mini_info_separator
997 * y == panel->widget.lines - 1 for panel bottom frame
999 widget_move (&panel->widget, y, x);
1000 tty_setcolor (MARKED_COLOR);
1001 tty_printf (" %s ", buf);
1004 /* --------------------------------------------------------------------------------------------- */
1006 static void
1007 mini_info_separator (WPanel * panel)
1009 if (panels_options.show_mini_info)
1011 const int y = llines (panel) + 2;
1013 tty_setcolor (NORMAL_COLOR);
1014 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1015 ACS_HLINE, panel->widget.cols - 2);
1016 /* Status displays total marked size.
1017 * Centered in panel, full format. */
1018 display_total_marked_size (panel, y, -1, FALSE);
1022 /* --------------------------------------------------------------------------------------------- */
1024 static void
1025 show_free_space (WPanel * panel)
1027 /* Used to figure out how many free space we have */
1028 static struct my_statfs myfs_stats;
1029 /* Old current working directory for displaying free space */
1030 static char *old_cwd = NULL;
1031 char *tmp_path;
1033 /* Don't try to stat non-local fs */
1034 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1035 return;
1037 tmp_path = vfs_path_to_str (panel->cwd_vpath);
1038 if (old_cwd == NULL || strcmp (old_cwd, tmp_path) != 0)
1040 char rpath[PATH_MAX];
1042 init_my_statfs ();
1043 g_free (old_cwd);
1044 old_cwd = tmp_path;
1045 tmp_path = NULL;
1047 if (mc_realpath (old_cwd, rpath) == NULL)
1048 return;
1050 my_statfs (&myfs_stats, rpath);
1052 g_free (tmp_path);
1054 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1056 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1057 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1058 panels_options.kilobyte_si);
1059 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1060 panels_options.kilobyte_si);
1061 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1062 myfs_stats.total == 0 ? 0 :
1063 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1064 widget_move (&panel->widget, panel->widget.lines - 1,
1065 panel->widget.cols - 2 - (int) strlen (tmp));
1066 tty_setcolor (NORMAL_COLOR);
1067 tty_print_string (tmp);
1071 /* --------------------------------------------------------------------------------------------- */
1073 * Prepare path string for showing in panel's header.
1074 * Passwords will removed, also home dir will replaced by ~
1076 * @param panel WPanel object
1078 * @return newly allocated string.
1081 static char *
1082 panel_correct_path_to_show (WPanel * panel)
1084 vfs_path_t *last_vpath;
1085 const vfs_path_element_t *path_element;
1086 char *return_path;
1087 int elements_count;
1089 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1091 /* get last path element */
1092 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1095 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1096 strcmp (path_element->class->name, "extfs") == 0 ||
1097 strcmp (path_element->class->name, "tarfs") == 0))
1099 const char *archive_name;
1100 const vfs_path_element_t *prev_path_element;
1102 /* get previous path element for catching archive name */
1103 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1104 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1105 if (archive_name != NULL)
1107 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1109 else
1111 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1112 last_vpath->relative = TRUE;
1115 else
1117 last_vpath = vfs_path_new ();
1118 last_vpath->relative = TRUE;
1121 vfs_path_add_element (last_vpath, path_element);
1122 return_path =
1123 vfs_path_to_str_flags (last_vpath, 0,
1124 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1125 vfs_path_free (last_vpath);
1127 return return_path;
1130 /* --------------------------------------------------------------------------------------------- */
1132 * Get Current path element encoding
1134 * @param panel WPanel object
1136 * @return newly allocated string or NULL if path charset is same as system charset
1139 #ifdef HAVE_CHARSET
1140 static char *
1141 panel_get_encoding_info_str (WPanel * panel)
1143 char *ret_str = NULL;
1144 const vfs_path_element_t *path_element;
1146 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1147 if (path_element->encoding != NULL)
1148 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1150 return ret_str;
1152 #endif
1154 /* --------------------------------------------------------------------------------------------- */
1156 static void
1157 show_dir (WPanel * panel)
1159 gchar *tmp;
1160 set_colors (panel);
1161 draw_box (panel->widget.owner,
1162 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1164 if (panels_options.show_mini_info)
1166 widget_move (&panel->widget, llines (panel) + 2, 0);
1167 tty_print_alt_char (ACS_LTEE, FALSE);
1168 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1169 tty_print_alt_char (ACS_RTEE, FALSE);
1172 widget_move (&panel->widget, 0, 1);
1173 tty_print_string (panel_history_prev_item_sign);
1175 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1176 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1177 panel_history_next_item_sign);
1179 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1180 tty_print_string (tmp);
1182 g_free (tmp);
1184 widget_move (&panel->widget, 0, 3);
1186 if (panel->is_panelized)
1187 tty_printf (" %s ", _("Panelize"));
1188 #ifdef HAVE_CHARSET
1189 else
1191 tmp = panel_get_encoding_info_str (panel);
1192 if (tmp != NULL)
1194 tty_printf ("%s", tmp);
1195 widget_move (&panel->widget, 0, 3 + strlen (tmp));
1196 g_free (tmp);
1199 #endif
1201 if (panel->active)
1202 tty_setcolor (REVERSE_COLOR);
1204 tmp = panel_correct_path_to_show (panel);
1205 tty_printf (" %s ",
1206 str_term_trim (tmp, min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1207 g_free (tmp);
1209 if (!panels_options.show_mini_info)
1211 if (panel->marked == 0)
1213 /* Show size of curret file in the bottom of panel */
1214 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1216 char buffer[BUF_SMALL];
1218 g_snprintf (buffer, sizeof (buffer), " %s ",
1219 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1220 panels_options.kilobyte_si));
1221 tty_setcolor (NORMAL_COLOR);
1222 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1223 tty_print_string (buffer);
1226 else
1228 /* Show total size of marked files
1229 * In the bottom of panel, display size only. */
1230 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1234 show_free_space (panel);
1236 if (panel->active)
1237 tty_set_normal_attrs ();
1240 /* --------------------------------------------------------------------------------------------- */
1242 /* Returns the number of items in the given panel */
1243 static int
1244 ITEMS (WPanel * p)
1246 if (p->split)
1247 return llines (p) * 2;
1248 else
1249 return llines (p);
1252 /* --------------------------------------------------------------------------------------------- */
1254 static void
1255 adjust_top_file (WPanel * panel)
1257 int items = ITEMS (panel);
1259 if (panel->count <= items)
1261 /* If all files fit, show them all. */
1262 panel->top_file = 0;
1264 else
1266 int i;
1268 /* top_file has to be in the range [selected-items+1, selected] so that
1269 the selected file is visible.
1270 top_file should be in the range [0, count-items] so that there's
1271 no empty space wasted.
1272 Within these ranges, adjust it by as little as possible. */
1274 if (panel->top_file < 0)
1275 panel->top_file = 0;
1277 i = panel->selected - items + 1;
1278 if (panel->top_file < i)
1279 panel->top_file = i;
1281 i = panel->count - items;
1282 if (panel->top_file > i)
1283 panel->top_file = i;
1285 if (panel->top_file > panel->selected)
1286 panel->top_file = panel->selected;
1290 /* --------------------------------------------------------------------------------------------- */
1291 /** add "#enc:encodning" to end of path */
1292 /* if path end width a previous #enc:, only encoding is changed no additional
1293 * #enc: is appended
1294 * retun new string
1297 static char *
1298 panel_save_name (WPanel * panel)
1300 /* If the program is shuting down */
1301 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1302 return g_strdup (panel->panel_name);
1303 else
1304 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1307 /* --------------------------------------------------------------------------------------------- */
1309 /* "history_load" event handler */
1310 static gboolean
1311 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1312 gpointer init_data, gpointer data)
1314 WPanel *p = (WPanel *) init_data;
1315 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1317 (void) event_group_name;
1318 (void) event_name;
1320 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1322 char *tmp_path;
1324 tmp_path = vfs_path_to_str (p->cwd_vpath);
1325 if (ev->cfg != NULL)
1326 p->dir_history = history_load (ev->cfg, p->hist_name);
1327 else
1328 p->dir_history = history_get (p->hist_name);
1330 directory_history_add (p, tmp_path);
1331 g_free (tmp_path);
1334 return TRUE;
1337 /* --------------------------------------------------------------------------------------------- */
1339 /* "history_save" event handler */
1340 static gboolean
1341 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1342 gpointer init_data, gpointer data)
1344 WPanel *p = (WPanel *) init_data;
1346 (void) event_group_name;
1347 (void) event_name;
1349 if (p->dir_history != NULL)
1351 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1353 history_save (ev->cfg, p->hist_name, p->dir_history);
1356 return TRUE;
1359 /* --------------------------------------------------------------------------------------------- */
1361 static void
1362 panel_destroy (WPanel * p)
1364 size_t i;
1366 if (panels_options.auto_save_setup)
1368 char *name;
1370 name = panel_save_name (p);
1371 panel_save_setup (p, name);
1372 g_free (name);
1375 panel_clean_dir (p);
1377 /* clean history */
1378 if (p->dir_history != NULL)
1380 /* directory history is already saved before this moment */
1381 p->dir_history = g_list_first (p->dir_history);
1382 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1383 g_list_free (p->dir_history);
1385 g_free (p->hist_name);
1387 delete_format (p->format);
1388 delete_format (p->status_format);
1390 g_free (p->user_format);
1391 for (i = 0; i < LIST_TYPES; i++)
1392 g_free (p->user_status_format[i]);
1394 g_free (p->dir.list);
1395 g_free (p->panel_name);
1397 vfs_path_free (p->lwd_vpath);
1398 vfs_path_free (p->cwd_vpath);
1401 /* --------------------------------------------------------------------------------------------- */
1403 static void
1404 panel_format_modified (WPanel * panel)
1406 panel->format_modified = 1;
1409 /* --------------------------------------------------------------------------------------------- */
1411 static void
1412 panel_paint_sort_info (WPanel * panel)
1414 if (*panel->sort_info.sort_field->hotkey != '\0')
1416 const char *sort_sign =
1417 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1418 char *str;
1420 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1421 widget_move (&panel->widget, 1, 1);
1422 tty_print_string (str);
1423 g_free (str);
1427 /* --------------------------------------------------------------------------------------------- */
1429 static gchar *
1430 panel_get_title_without_hotkey (const char *title)
1432 char *translated_title;
1433 char *hkey;
1435 if (title == NULL)
1436 return NULL;
1437 if (title[0] == '\0')
1438 return g_strdup ("");
1440 translated_title = g_strdup (_(title));
1442 hkey = strchr (translated_title, '&');
1443 if ((hkey != NULL) && (hkey[1] != '\0'))
1444 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1446 return translated_title;
1449 /* --------------------------------------------------------------------------------------------- */
1451 static void
1452 paint_frame (WPanel * panel)
1454 int side, width;
1455 GString *format_txt;
1457 adjust_top_file (panel);
1459 widget_erase (&panel->widget);
1460 show_dir (panel);
1462 widget_move (&panel->widget, 1, 1);
1464 for (side = 0; side <= panel->split; side++)
1466 format_e *format;
1468 if (side)
1470 tty_setcolor (NORMAL_COLOR);
1471 tty_print_one_vline (TRUE);
1472 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1474 else if (panel->split)
1475 width = panel->widget.cols / 2 - 3;
1476 else
1477 width = panel->widget.cols - 2;
1479 format_txt = g_string_new ("");
1480 for (format = panel->format; format; format = format->next)
1482 if (format->string_fn)
1484 g_string_set_size (format_txt, 0);
1486 if (panel->list_type == list_long
1487 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1488 g_string_append (format_txt,
1489 panel->sort_info.reverse
1490 ? panel_sort_down_sign : panel_sort_up_sign);
1492 g_string_append (format_txt, format->title);
1493 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1495 g_string_append (format_txt, " [");
1496 g_string_append (format_txt, panel->filter);
1497 g_string_append (format_txt, "]");
1500 tty_setcolor (HEADER_COLOR);
1501 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1502 J_CENTER_LEFT));
1503 width -= format->field_len;
1505 else
1507 tty_setcolor (NORMAL_COLOR);
1508 tty_print_one_vline (TRUE);
1509 width--;
1512 g_string_free (format_txt, TRUE);
1514 if (width > 0)
1515 tty_draw_hline (-1, -1, ' ', width);
1518 if (panel->list_type != list_long)
1519 panel_paint_sort_info (panel);
1522 /* --------------------------------------------------------------------------------------------- */
1524 static const char *
1525 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1527 panel_display_t frame = frame_half;
1528 format = skip_separators (format);
1530 if (!strncmp (format, "full", 4))
1532 frame = frame_full;
1533 format += 4;
1535 else if (!strncmp (format, "half", 4))
1537 frame = frame_half;
1538 format += 4;
1541 if (!isstatus)
1543 panel->frame_size = frame;
1544 panel->split = 0;
1547 /* Now, the optional column specifier */
1548 format = skip_separators (format);
1550 if (*format == '1' || *format == '2')
1552 if (!isstatus)
1553 panel->split = *format == '2';
1554 format++;
1557 if (!isstatus)
1558 panel_update_cols (&(panel->widget), panel->frame_size);
1560 return skip_separators (format);
1563 /* Format is:
1565 all := panel_format? format
1566 panel_format := [full|half] [1|2]
1567 format := one_format_e
1568 | format , one_format_e
1570 one_format_e := just format.id [opt_size]
1571 just := [<=>]
1572 opt_size := : size [opt_expand]
1573 size := [0-9]+
1574 opt_expand := +
1578 /* --------------------------------------------------------------------------------------------- */
1580 static format_e *
1581 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1582 int *res_total_cols)
1584 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1585 int total_cols = 0; /* Used columns by the format */
1586 int set_justify; /* flag: set justification mode? */
1587 align_crt_t justify = J_LEFT; /* Which mode. */
1588 size_t i;
1590 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1592 *error = 0;
1594 if (i18n_timelength == 0)
1596 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1598 for (i = 0; panel_fields[i].id != NULL; i++)
1599 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1600 panel_fields[i].min_size = i18n_timelength;
1604 * This makes sure that the panel and mini status full/half mode
1605 * setting is equal
1607 format = parse_panel_size (panel, format, isstatus);
1609 while (*format)
1610 { /* format can be an empty string */
1611 int found = 0;
1613 darr = g_new0 (format_e, 1);
1615 /* I'm so ugly, don't look at me :-) */
1616 if (!home)
1617 home = old = darr;
1619 old->next = darr;
1620 darr->next = 0;
1621 old = darr;
1623 format = skip_separators (format);
1625 if (strchr ("<=>", *format))
1627 set_justify = 1;
1628 switch (*format)
1630 case '<':
1631 justify = J_LEFT;
1632 break;
1633 case '=':
1634 justify = J_CENTER;
1635 break;
1636 case '>':
1637 default:
1638 justify = J_RIGHT;
1639 break;
1641 format = skip_separators (format + 1);
1643 else
1644 set_justify = 0;
1646 for (i = 0; panel_fields[i].id != NULL; i++)
1648 size_t klen = strlen (panel_fields[i].id);
1650 if (strncmp (format, panel_fields[i].id, klen) != 0)
1651 continue;
1653 format += klen;
1655 darr->requested_field_len = panel_fields[i].min_size;
1656 darr->string_fn = panel_fields[i].string_fn;
1657 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1659 darr->id = panel_fields[i].id;
1660 darr->expand = panel_fields[i].expands;
1661 darr->just_mode = panel_fields[i].default_just;
1663 if (set_justify)
1665 if (IS_FIT (darr->just_mode))
1666 darr->just_mode = MAKE_FIT (justify);
1667 else
1668 darr->just_mode = justify;
1670 found = 1;
1672 format = skip_separators (format);
1674 /* If we have a size specifier */
1675 if (*format == ':')
1677 int req_length;
1679 /* If the size was specified, we don't want
1680 * auto-expansion by default
1682 darr->expand = 0;
1683 format++;
1684 req_length = atoi (format);
1685 darr->requested_field_len = req_length;
1687 format = skip_numbers (format);
1689 /* Now, if they insist on expansion */
1690 if (*format == '+')
1692 darr->expand = 1;
1693 format++;
1698 break;
1700 if (!found)
1702 char *tmp_format = g_strdup (format);
1704 int pos = min (8, strlen (format));
1705 delete_format (home);
1706 tmp_format[pos] = 0;
1707 *error =
1708 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1709 g_free (tmp_format);
1710 return 0;
1712 total_cols += darr->requested_field_len;
1715 *res_total_cols = total_cols;
1716 return home;
1719 /* --------------------------------------------------------------------------------------------- */
1721 static format_e *
1722 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1724 #define MAX_EXPAND 4
1725 int expand_top = 0; /* Max used element in expand */
1726 int usable_columns; /* Usable columns in the panel */
1727 int total_cols = 0;
1728 int i;
1729 format_e *darr, *home;
1731 if (!format)
1732 format = DEFAULT_USER_FORMAT;
1734 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1736 if (*error)
1737 return 0;
1739 panel->dirty = 1;
1741 /* Status needn't to be split */
1742 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1744 : (panel->split + 1))) - (!isstatus
1745 && panel->split);
1747 /* Look for the expandable fields and set field_len based on the requested field len */
1748 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1750 darr->field_len = darr->requested_field_len;
1751 if (darr->expand)
1752 expand_top++;
1755 /* If we used more columns than the available columns, adjust that */
1756 if (total_cols > usable_columns)
1758 int pdif, dif = total_cols - usable_columns;
1760 while (dif)
1762 pdif = dif;
1763 for (darr = home; darr; darr = darr->next)
1765 if (dif && darr->field_len - 1)
1767 darr->field_len--;
1768 dif--;
1772 /* avoid endless loop if num fields > 40 */
1773 if (pdif == dif)
1774 break;
1776 total_cols = usable_columns; /* give up, the rest should be truncated */
1779 /* Expand the available space */
1780 if ((usable_columns > total_cols) && expand_top)
1782 int spaces = (usable_columns - total_cols) / expand_top;
1783 int extra = (usable_columns - total_cols) % expand_top;
1785 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1786 if (darr->expand)
1788 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1789 i++;
1792 return home;
1795 /* --------------------------------------------------------------------------------------------- */
1796 /** Given the panel->view_type returns the format string to be parsed */
1798 static const char *
1799 panel_format (WPanel * panel)
1801 switch (panel->list_type)
1803 case list_long:
1804 return "full perm space nlink space owner space group space size space mtime space name";
1806 case list_brief:
1807 return "half 2 type name";
1809 case list_user:
1810 return panel->user_format;
1812 default:
1813 case list_full:
1814 return "half type name | size | mtime";
1818 /* --------------------------------------------------------------------------------------------- */
1820 static const char *
1821 mini_status_format (WPanel * panel)
1823 if (panel->user_mini_status)
1824 return panel->user_status_format[panel->list_type];
1826 switch (panel->list_type)
1829 case list_long:
1830 return "full perm space nlink space owner space group space size space mtime space name";
1832 case list_brief:
1833 return "half type name space bsize space perm space";
1835 case list_full:
1836 return "half type name";
1838 default:
1839 case list_user:
1840 return panel->user_format;
1844 /* */
1845 /* Panel operation commands */
1846 /* */
1848 /* --------------------------------------------------------------------------------------------- */
1849 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1851 static cb_ret_t
1852 maybe_cd (int move_up_dir)
1854 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1856 if (move_up_dir)
1858 vfs_path_t *up_dir;
1860 up_dir = vfs_path_from_str ("..");
1861 do_cd (up_dir, cd_exact);
1862 vfs_path_free (up_dir);
1863 return MSG_HANDLED;
1866 if (S_ISDIR (selection (current_panel)->st.st_mode)
1867 || link_isdir (selection (current_panel)))
1869 vfs_path_t *vpath;
1871 vpath = vfs_path_from_str (selection (current_panel)->fname);
1872 do_cd (vpath, cd_exact);
1873 vfs_path_free (vpath);
1874 return MSG_HANDLED;
1877 return MSG_NOT_HANDLED;
1880 /* --------------------------------------------------------------------------------------------- */
1882 /* if command line is empty then do 'cd ..' */
1883 static cb_ret_t
1884 force_maybe_cd (void)
1886 if (cmdline->buffer[0] == '\0')
1888 vfs_path_t *up_dir = vfs_path_from_str ("..");
1889 do_cd (up_dir, cd_exact);
1890 vfs_path_free (up_dir);
1891 return MSG_HANDLED;
1893 return MSG_NOT_HANDLED;
1896 /* --------------------------------------------------------------------------------------------- */
1898 static void
1899 unselect_item (WPanel * panel)
1901 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1904 /* --------------------------------------------------------------------------------------------- */
1906 static void
1907 move_down (WPanel * panel)
1909 if (panel->selected + 1 == panel->count)
1910 return;
1912 unselect_item (panel);
1913 panel->selected++;
1914 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1916 /* Scroll window half screen */
1917 panel->top_file += ITEMS (panel) / 2;
1918 if (panel->top_file > panel->count - ITEMS (panel))
1919 panel->top_file = panel->count - ITEMS (panel);
1920 paint_dir (panel);
1922 select_item (panel);
1925 /* --------------------------------------------------------------------------------------------- */
1927 static void
1928 move_up (WPanel * panel)
1930 if (panel->selected == 0)
1931 return;
1933 unselect_item (panel);
1934 panel->selected--;
1935 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1937 /* Scroll window half screen */
1938 panel->top_file -= ITEMS (panel) / 2;
1939 if (panel->top_file < 0)
1940 panel->top_file = 0;
1941 paint_dir (panel);
1943 select_item (panel);
1946 /* --------------------------------------------------------------------------------------------- */
1947 /** Changes the selection by lines (may be negative) */
1949 static void
1950 move_selection (WPanel * panel, int lines)
1952 int new_pos;
1953 int adjust = 0;
1955 new_pos = panel->selected + lines;
1956 if (new_pos >= panel->count)
1957 new_pos = panel->count - 1;
1959 if (new_pos < 0)
1960 new_pos = 0;
1962 unselect_item (panel);
1963 panel->selected = new_pos;
1965 if (panel->selected - panel->top_file >= ITEMS (panel))
1967 panel->top_file += lines;
1968 adjust = 1;
1971 if (panel->selected - panel->top_file < 0)
1973 panel->top_file += lines;
1974 adjust = 1;
1977 if (adjust)
1979 if (panel->top_file > panel->selected)
1980 panel->top_file = panel->selected;
1981 if (panel->top_file < 0)
1982 panel->top_file = 0;
1983 paint_dir (panel);
1985 select_item (panel);
1988 /* --------------------------------------------------------------------------------------------- */
1990 static cb_ret_t
1991 move_left (WPanel * panel)
1993 if (panel->split)
1995 move_selection (panel, -llines (panel));
1996 return MSG_HANDLED;
1998 else
1999 return maybe_cd (1); /* cd .. */
2002 /* --------------------------------------------------------------------------------------------- */
2004 static cb_ret_t
2005 move_right (WPanel * panel)
2007 if (panel->split)
2009 move_selection (panel, llines (panel));
2010 return MSG_HANDLED;
2012 else
2013 return maybe_cd (0); /* cd (selection) */
2016 /* --------------------------------------------------------------------------------------------- */
2018 static void
2019 prev_page (WPanel * panel)
2021 int items;
2023 if (!panel->selected && !panel->top_file)
2024 return;
2025 unselect_item (panel);
2026 items = ITEMS (panel);
2027 if (panel->top_file < items)
2028 items = panel->top_file;
2029 if (!items)
2030 panel->selected = 0;
2031 else
2032 panel->selected -= items;
2033 panel->top_file -= items;
2035 select_item (panel);
2036 paint_dir (panel);
2039 /* --------------------------------------------------------------------------------------------- */
2041 static void
2042 goto_parent_dir (WPanel * panel)
2044 if (!panel->is_panelized)
2046 vfs_path_t *up_dir;
2048 up_dir = vfs_path_from_str ("..");
2049 do_cd (up_dir, cd_exact);
2050 vfs_path_free (up_dir);
2052 else
2054 char *fname = panel->dir.list[panel->selected].fname;
2055 const char *bname;
2056 vfs_path_t *dname_vpath;
2058 if (g_path_is_absolute (fname))
2059 fname = g_strdup (fname);
2060 else
2062 char *tmp_root;
2064 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2065 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2066 g_free (tmp_root);
2069 bname = x_basename (fname);
2071 if (bname == fname)
2072 dname_vpath = vfs_path_from_str (".");
2073 else
2075 char *dname;
2077 dname = g_strndup (fname, bname - fname);
2078 dname_vpath = vfs_path_from_str (dname);
2079 g_free (dname);
2082 do_cd (dname_vpath, cd_exact);
2083 try_to_select (panel, bname);
2085 vfs_path_free (dname_vpath);
2086 g_free (fname);
2090 /* --------------------------------------------------------------------------------------------- */
2092 static void
2093 next_page (WPanel * panel)
2095 int items;
2097 if (panel->selected == panel->count - 1)
2098 return;
2099 unselect_item (panel);
2100 items = ITEMS (panel);
2101 if (panel->top_file > panel->count - 2 * items)
2102 items = panel->count - items - panel->top_file;
2103 if (panel->top_file + items < 0)
2104 items = -panel->top_file;
2105 if (!items)
2106 panel->selected = panel->count - 1;
2107 else
2108 panel->selected += items;
2109 panel->top_file += items;
2111 select_item (panel);
2112 paint_dir (panel);
2115 /* --------------------------------------------------------------------------------------------- */
2117 static void
2118 goto_child_dir (WPanel * panel)
2120 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2122 vfs_path_t *vpath;
2124 vpath = vfs_path_from_str (selection (panel)->fname);
2125 do_cd (vpath, cd_exact);
2126 vfs_path_free (vpath);
2130 /* --------------------------------------------------------------------------------------------- */
2132 static void
2133 goto_top_file (WPanel * panel)
2135 unselect_item (panel);
2136 panel->selected = panel->top_file;
2137 select_item (panel);
2140 /* --------------------------------------------------------------------------------------------- */
2142 static void
2143 goto_middle_file (WPanel * panel)
2145 unselect_item (panel);
2146 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2147 select_item (panel);
2150 /* --------------------------------------------------------------------------------------------- */
2152 static void
2153 goto_bottom_file (WPanel * panel)
2155 unselect_item (panel);
2156 panel->selected = panel->top_file + ITEMS (panel) - 1;
2157 select_item (panel);
2160 /* --------------------------------------------------------------------------------------------- */
2162 static void
2163 move_home (WPanel * panel)
2165 if (panel->selected == 0)
2166 return;
2168 unselect_item (panel);
2170 if (panels_options.torben_fj_mode)
2172 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2174 if (panel->selected > middle_pos)
2176 goto_middle_file (panel);
2177 return;
2179 if (panel->selected != panel->top_file)
2181 goto_top_file (panel);
2182 return;
2186 panel->top_file = 0;
2187 panel->selected = 0;
2189 paint_dir (panel);
2190 select_item (panel);
2193 /* --------------------------------------------------------------------------------------------- */
2195 static void
2196 move_end (WPanel * panel)
2198 if (panel->selected == panel->count - 1)
2199 return;
2201 unselect_item (panel);
2203 if (panels_options.torben_fj_mode)
2205 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2207 if (panel->selected < middle_pos)
2209 goto_middle_file (panel);
2210 return;
2212 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2214 goto_bottom_file (panel);
2215 return;
2219 panel->selected = panel->count - 1;
2220 paint_dir (panel);
2221 select_item (panel);
2224 /* --------------------------------------------------------------------------------------------- */
2226 static void
2227 do_mark_file (WPanel * panel, mark_act_t do_move)
2229 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2230 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2231 move_down (panel);
2232 else if (do_move == MARK_FORCE_UP)
2233 move_up (panel);
2236 /* --------------------------------------------------------------------------------------------- */
2238 static void
2239 mark_file (WPanel * panel)
2241 do_mark_file (panel, MARK_DOWN);
2244 /* --------------------------------------------------------------------------------------------- */
2246 static void
2247 mark_file_up (WPanel * panel)
2249 do_mark_file (panel, MARK_FORCE_UP);
2252 /* --------------------------------------------------------------------------------------------- */
2254 static void
2255 mark_file_down (WPanel * panel)
2257 do_mark_file (panel, MARK_FORCE_DOWN);
2260 /* --------------------------------------------------------------------------------------------- */
2262 static void
2263 mark_file_right (WPanel * panel)
2265 int lines = llines (panel);
2267 if (state_mark < 0)
2268 state_mark = selection (panel)->f.marked ? 0 : 1;
2270 lines = min (lines, panel->count - panel->selected - 1);
2271 for (; lines != 0; lines--)
2273 do_file_mark (panel, panel->selected, state_mark);
2274 move_down (panel);
2276 do_file_mark (panel, panel->selected, state_mark);
2279 /* --------------------------------------------------------------------------------------------- */
2281 static void
2282 mark_file_left (WPanel * panel)
2284 int lines = llines (panel);
2286 if (state_mark < 0)
2287 state_mark = selection (panel)->f.marked ? 0 : 1;
2289 lines = min (lines, panel->selected + 1);
2290 for (; lines != 0; lines--)
2292 do_file_mark (panel, panel->selected, state_mark);
2293 move_up (panel);
2295 do_file_mark (panel, panel->selected, state_mark);
2298 /* --------------------------------------------------------------------------------------------- */
2299 /** Incremental search of a file name in the panel.
2300 * @param panel instance of WPanel structure
2301 * @param c_code key code
2304 static void
2305 do_search (WPanel * panel, int c_code)
2307 size_t l;
2308 int i, sel;
2309 gboolean wrapped = FALSE;
2310 char *act;
2311 mc_search_t *search;
2312 char *reg_exp, *esc_str;
2313 gboolean is_found = FALSE;
2315 l = strlen (panel->search_buffer);
2316 if (c_code == KEY_BACKSPACE)
2318 if (l != 0)
2320 act = panel->search_buffer + l;
2321 str_prev_noncomb_char (&act, panel->search_buffer);
2322 act[0] = '\0';
2324 panel->search_chpoint = 0;
2326 else
2328 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2330 panel->search_char[panel->search_chpoint] = c_code;
2331 panel->search_chpoint++;
2334 if (panel->search_chpoint > 0)
2336 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2338 case -2:
2339 return;
2340 case -1:
2341 panel->search_chpoint = 0;
2342 return;
2343 default:
2344 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2346 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2347 l += panel->search_chpoint;
2348 *(panel->search_buffer + l) = '\0';
2349 panel->search_chpoint = 0;
2355 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2356 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2357 search = mc_search_new (esc_str, -1);
2358 search->search_type = MC_SEARCH_T_GLOB;
2359 search->is_entire_line = TRUE;
2360 switch (panels_options.qsearch_mode)
2362 case QSEARCH_CASE_SENSITIVE:
2363 search->is_case_sensitive = TRUE;
2364 break;
2365 case QSEARCH_CASE_INSENSITIVE:
2366 search->is_case_sensitive = FALSE;
2367 break;
2368 default:
2369 search->is_case_sensitive = panel->sort_info.case_sensitive;
2370 break;
2372 sel = panel->selected;
2373 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2375 if (i >= panel->count)
2377 i = 0;
2378 if (wrapped)
2379 break;
2380 wrapped = TRUE;
2382 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2384 sel = i;
2385 is_found = TRUE;
2386 break;
2389 if (is_found)
2391 unselect_item (panel);
2392 panel->selected = sel;
2393 select_item (panel);
2394 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2396 else if (c_code != KEY_BACKSPACE)
2398 act = panel->search_buffer + l;
2399 str_prev_noncomb_char (&act, panel->search_buffer);
2400 act[0] = '\0';
2402 mc_search_free (search);
2403 g_free (reg_exp);
2404 g_free (esc_str);
2407 /* --------------------------------------------------------------------------------------------- */
2408 /** Start new search.
2409 * @param panel instance of WPanel structure
2412 static void
2413 start_search (WPanel * panel)
2415 if (panel->searching)
2417 if (panel->selected + 1 == panel->count)
2418 panel->selected = 0;
2419 else
2420 move_down (panel);
2422 /* in case if there was no search string we need to recall
2423 previous string, with which we ended previous searching */
2424 if (panel->search_buffer[0] == '\0')
2425 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2426 sizeof (panel->search_buffer));
2428 do_search (panel, 0);
2430 else
2432 panel->searching = TRUE;
2433 panel->search_buffer[0] = '\0';
2434 panel->search_char[0] = '\0';
2435 panel->search_chpoint = 0;
2436 display_mini_info (panel);
2437 mc_refresh ();
2441 /* --------------------------------------------------------------------------------------------- */
2443 static void
2444 stop_search (WPanel * panel)
2446 panel->searching = FALSE;
2448 /* if user had overrdied search string, we need to store it
2449 to the previous_search_buffer */
2450 if (panel->search_buffer[0] != '\0')
2451 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2452 sizeof (panel->prev_search_buffer));
2454 display_mini_info (panel);
2457 /* --------------------------------------------------------------------------------------------- */
2458 /** Return 1 if the Enter key has been processed, 0 otherwise */
2460 static int
2461 do_enter_on_file_entry (file_entry * fe)
2463 vfs_path_t *full_name_vpath;
2464 gboolean ok;
2467 * Directory or link to directory - change directory.
2468 * Try the same for the entries on which mc_lstat() has failed.
2470 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2472 vfs_path_t *fname_vpath;
2474 fname_vpath = vfs_path_from_str (fe->fname);
2475 if (!do_cd (fname_vpath, cd_exact))
2476 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2477 vfs_path_free (fname_vpath);
2478 return 1;
2481 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2483 /* Try associated command */
2484 if (regex_command (full_name_vpath, "Open", NULL) != 0)
2486 vfs_path_free (full_name_vpath);
2487 return 1;
2490 /* Check if the file is executable */
2491 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2492 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2493 vfs_path_free (full_name_vpath);
2494 if (!ok)
2495 return 0;
2497 if (confirm_execute)
2499 if (query_dialog
2500 (_("The Midnight Commander"),
2501 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2502 return 1;
2505 if (!vfs_current_is_local ())
2507 int ret;
2508 vfs_path_t *tmp_vpath;
2510 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2511 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2512 vfs_path_free (tmp_vpath);
2513 /* We took action only if the dialog was shown or the execution
2514 * was successful */
2515 return confirm_execute || (ret == 0);
2519 char *tmp = name_quote (fe->fname, 0);
2520 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2521 g_free (tmp);
2522 shell_execute (cmd, 0);
2523 g_free (cmd);
2526 #ifdef HAVE_CHARSET
2527 mc_global.source_codepage = default_source_codepage;
2528 #endif
2530 return 1;
2533 /* --------------------------------------------------------------------------------------------- */
2535 static int
2536 do_enter (WPanel * panel)
2538 return do_enter_on_file_entry (selection (panel));
2541 /* --------------------------------------------------------------------------------------------- */
2543 static void
2544 chdir_other_panel (WPanel * panel)
2546 const file_entry *entry = &panel->dir.list[panel->selected];
2548 vfs_path_t *new_dir_vpath;
2549 char *sel_entry = NULL;
2551 if (get_other_type () != view_listing)
2553 set_display_type (get_other_index (), view_listing);
2556 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2557 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2558 else
2560 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2561 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2564 change_panel ();
2565 do_cd (new_dir_vpath, cd_exact);
2566 vfs_path_free (new_dir_vpath);
2568 if (sel_entry)
2569 try_to_select (current_panel, sel_entry);
2570 change_panel ();
2572 move_down (panel);
2575 /* --------------------------------------------------------------------------------------------- */
2577 * Make the current directory of the current panel also the current
2578 * directory of the other panel. Put the other panel to the listing
2579 * mode if needed. If the current panel is panelized, the other panel
2580 * doesn't become panelized.
2583 static void
2584 panel_sync_other (const WPanel * panel)
2586 if (get_other_type () != view_listing)
2588 set_display_type (get_other_index (), view_listing);
2591 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2593 /* try to select current filename on the other panel */
2594 if (!panel->is_panelized)
2596 try_to_select (other_panel, selection (panel)->fname);
2600 /* --------------------------------------------------------------------------------------------- */
2602 static void
2603 chdir_to_readlink (WPanel * panel)
2605 vfs_path_t *new_dir_vpath;
2606 char buffer[MC_MAXPATHLEN], *p;
2607 int i;
2608 struct stat st;
2609 vfs_path_t *panel_fname_vpath;
2610 gboolean ok;
2612 if (get_other_type () != view_listing)
2613 return;
2615 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2616 return;
2618 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2619 if (i < 0)
2620 return;
2622 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2623 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2624 vfs_path_free (panel_fname_vpath);
2625 if (!ok)
2626 return;
2628 buffer[i] = 0;
2629 if (!S_ISDIR (st.st_mode))
2631 p = strrchr (buffer, PATH_SEP);
2632 if (p && !p[1])
2634 *p = 0;
2635 p = strrchr (buffer, PATH_SEP);
2637 if (!p)
2638 return;
2639 p[1] = 0;
2641 if (*buffer == PATH_SEP)
2642 new_dir_vpath = vfs_path_from_str (buffer);
2643 else
2644 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2646 change_panel ();
2647 do_cd (new_dir_vpath, cd_exact);
2648 vfs_path_free (new_dir_vpath);
2649 change_panel ();
2651 move_down (panel);
2654 /* --------------------------------------------------------------------------------------------- */
2656 static gsize
2657 panel_get_format_field_count (WPanel * panel)
2659 format_e *format;
2660 gsize lc_index;
2661 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2662 return lc_index;
2665 /* --------------------------------------------------------------------------------------------- */
2667 function return 0 if not found and REAL_INDEX+1 if found
2670 static gsize
2671 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2673 format_e *format;
2674 gsize lc_index;
2676 for (lc_index = 1, format = panel->format;
2677 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2678 if (format == NULL)
2679 lc_index = 0;
2681 return lc_index;
2684 /* --------------------------------------------------------------------------------------------- */
2686 static format_e *
2687 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2689 format_e *format;
2690 for (format = panel->format;
2691 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2692 return format;
2695 /* --------------------------------------------------------------------------------------------- */
2697 static const panel_field_t *
2698 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2700 const panel_field_t *pfield;
2701 format_e *format;
2703 format = panel_get_format_field_by_index (panel, lc_index);
2704 if (format == NULL)
2705 return NULL;
2706 pfield = panel_get_field_by_title (format->title);
2707 if (pfield == NULL)
2708 return NULL;
2709 if (pfield->sort_routine == NULL)
2710 return NULL;
2711 return pfield;
2714 /* --------------------------------------------------------------------------------------------- */
2716 static void
2717 panel_toggle_sort_order_prev (WPanel * panel)
2719 gsize lc_index, i;
2720 gchar *title;
2722 const panel_field_t *pfield = NULL;
2724 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2725 lc_index = panel_get_format_field_index_by_name (panel, title);
2726 g_free (title);
2728 if (lc_index > 1)
2730 /* search for prev sortable column in panel format */
2731 for (i = lc_index - 1;
2732 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2735 if (pfield == NULL)
2737 /* Sortable field not found. Try to search in each array */
2738 for (i = panel_get_format_field_count (panel);
2739 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2742 if (pfield != NULL)
2744 panel->sort_info.sort_field = pfield;
2745 panel_set_sort_order (panel, pfield);
2749 /* --------------------------------------------------------------------------------------------- */
2751 static void
2752 panel_toggle_sort_order_next (WPanel * panel)
2754 gsize lc_index, i;
2755 const panel_field_t *pfield = NULL;
2756 gsize format_field_count;
2757 gchar *title;
2759 format_field_count = panel_get_format_field_count (panel);
2760 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2761 lc_index = panel_get_format_field_index_by_name (panel, title);
2762 g_free (title);
2764 if (lc_index != 0 && lc_index != format_field_count)
2766 /* search for prev sortable column in panel format */
2767 for (i = lc_index;
2768 i != format_field_count
2769 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2772 if (pfield == NULL)
2774 /* Sortable field not found. Try to search in each array */
2775 for (i = 0;
2776 i != format_field_count
2777 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2780 if (pfield != NULL)
2782 panel->sort_info.sort_field = pfield;
2783 panel_set_sort_order (panel, pfield);
2787 /* --------------------------------------------------------------------------------------------- */
2789 static void
2790 panel_select_sort_order (WPanel * panel)
2792 const panel_field_t *sort_order;
2794 sort_order = sort_box (&panel->sort_info);
2795 if (sort_order != NULL)
2797 panel->sort_info.sort_field = sort_order;
2798 panel_set_sort_order (panel, sort_order);
2802 /* --------------------------------------------------------------------------------------------- */
2804 static void
2805 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2807 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2809 const panel_field_t *sort_order;
2811 sort_order = panel_get_field_by_id (name);
2812 if (sort_order == NULL)
2813 return;
2814 panel->sort_info.sort_field = sort_order;
2816 else
2817 panel->sort_info.reverse = !panel->sort_info.reverse;
2819 panel_set_sort_order (panel, panel->sort_info.sort_field);
2822 /* --------------------------------------------------------------------------------------------- */
2824 * If we moved to the parent directory move the selection pointer to
2825 * the old directory name; If we leave VFS dir, remove FS specificator.
2827 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2830 static const char *
2831 get_parent_dir_name (const char *cwd, const char *lwd)
2833 size_t llen, clen;
2834 const char *p;
2836 llen = strlen (lwd);
2837 clen = strlen (cwd);
2839 if (llen <= clen)
2840 return NULL;
2842 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2844 if (p == NULL)
2846 p = strrchr (lwd, PATH_SEP);
2848 if ((p != NULL)
2849 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2850 && (clen == (size_t) (p - lwd)
2851 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2852 return (p + 1);
2854 return NULL;
2857 /* skip VFS prefix */
2858 while (--p > lwd && *p != PATH_SEP)
2860 /* get last component */
2861 while (--p > lwd && *p != PATH_SEP)
2864 /* return last component */
2865 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
2868 /* --------------------------------------------------------------------------------------------- */
2869 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2871 static void
2872 subshell_chdir (const vfs_path_t * vpath)
2874 #ifdef HAVE_SUBSHELL_SUPPORT
2875 if (mc_global.tty.use_subshell && vfs_current_is_local ())
2876 do_subshell_chdir (vpath, FALSE, TRUE);
2877 #endif /* HAVE_SUBSHELL_SUPPORT */
2880 /* --------------------------------------------------------------------------------------------- */
2882 * Changes the current directory of the panel.
2883 * Don't record change in the directory history.
2886 static gboolean
2887 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
2889 char *olddir;
2891 olddir = vfs_path_to_str (panel->cwd_vpath);
2893 /* Convert *new_path to a suitable pathname, handle ~user */
2894 if (cd_type == cd_parse_command)
2896 const vfs_path_element_t *element;
2898 element = vfs_path_get_by_index (new_dir_vpath, 0);
2899 if (strcmp (element->path, "-") == 0)
2900 new_dir_vpath = panel->lwd_vpath;
2903 if (mc_chdir (new_dir_vpath) == -1)
2905 panel_set_cwd (panel, olddir);
2906 g_free (olddir);
2907 return FALSE;
2910 /* Success: save previous directory, shutdown status of previous dir */
2911 panel_set_lwd (panel, olddir);
2912 input_free_completions (cmdline);
2914 vfs_path_free (panel->cwd_vpath);
2915 vfs_setup_cwd ();
2916 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
2918 vfs_release_path (olddir);
2920 subshell_chdir (panel->cwd_vpath);
2922 /* Reload current panel */
2923 panel_clean_dir (panel);
2926 char *tmp_path;
2928 panel->count =
2929 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
2930 panel->sort_info.reverse, panel->sort_info.case_sensitive,
2931 panel->sort_info.exec_first, panel->filter);
2932 tmp_path = vfs_path_to_str (panel->cwd_vpath);
2933 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
2934 g_free (tmp_path);
2937 load_hint (0);
2938 panel->dirty = 1;
2939 update_xterm_title_path ();
2941 g_free (olddir);
2943 return TRUE;
2946 /* --------------------------------------------------------------------------------------------- */
2948 static void
2949 directory_history_next (WPanel * panel)
2951 GList *nextdir;
2953 nextdir = g_list_next (panel->dir_history);
2954 if (nextdir != NULL)
2956 vfs_path_t *data_vpath;
2958 data_vpath = vfs_path_from_str ((char *) nextdir->data);
2959 if (_do_panel_cd (panel, data_vpath, cd_exact))
2960 panel->dir_history = nextdir;
2961 vfs_path_free (data_vpath);
2965 /* --------------------------------------------------------------------------------------------- */
2967 static void
2968 directory_history_prev (WPanel * panel)
2970 GList *prevdir;
2972 prevdir = g_list_previous (panel->dir_history);
2974 if (prevdir != NULL)
2976 vfs_path_t *data_vpath;
2978 data_vpath = vfs_path_from_str ((char *) prevdir->data);
2979 if (_do_panel_cd (panel, data_vpath, cd_exact))
2980 panel->dir_history = prevdir;
2981 vfs_path_free (data_vpath);
2985 /* --------------------------------------------------------------------------------------------- */
2987 static void
2988 directory_history_list (WPanel * panel)
2990 char *s;
2992 s = history_show (&panel->dir_history, &panel->widget);
2994 if (s != NULL)
2996 vfs_path_t *s_vpath;
2998 s_vpath = vfs_path_from_str (s);
2999 if (_do_panel_cd (panel, s_vpath, cd_exact))
3001 char *tmp_path;
3003 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3004 directory_history_add (panel, tmp_path);
3005 g_free (tmp_path);
3007 else
3008 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3009 vfs_path_free (s_vpath);
3010 g_free (s);
3014 /* --------------------------------------------------------------------------------------------- */
3016 static cb_ret_t
3017 panel_execute_cmd (WPanel * panel, unsigned long command)
3019 int res = MSG_HANDLED;
3021 if (command != CK_Search)
3022 stop_search (panel);
3025 switch (command)
3027 case CK_Up:
3028 case CK_Down:
3029 case CK_Left:
3030 case CK_Right:
3031 case CK_Bottom:
3032 case CK_Top:
3033 case CK_PageDown:
3034 case CK_PageUp:
3035 /* reset state of marks flag */
3036 state_mark = -1;
3037 break;
3039 switch (command)
3041 case CK_PanelOtherCd:
3042 chdir_other_panel (panel);
3043 break;
3044 case CK_PanelOtherCdLink:
3045 chdir_to_readlink (panel);
3046 break;
3047 case CK_CopySingle:
3048 copy_cmd_local ();
3049 break;
3050 case CK_DeleteSingle:
3051 delete_cmd_local ();
3052 break;
3053 case CK_Enter:
3054 do_enter (panel);
3055 break;
3056 case CK_ViewRaw:
3057 view_raw_cmd ();
3058 break;
3059 case CK_EditNew:
3060 edit_cmd_new ();
3061 break;
3062 case CK_MoveSingle:
3063 rename_cmd_local ();
3064 break;
3065 case CK_SelectInvert:
3066 select_invert_cmd ();
3067 break;
3068 case CK_Select:
3069 select_cmd ();
3070 break;
3071 case CK_Unselect:
3072 unselect_cmd ();
3073 break;
3074 case CK_PageDown:
3075 next_page (panel);
3076 break;
3077 case CK_PageUp:
3078 prev_page (panel);
3079 break;
3080 case CK_CdChild:
3081 goto_child_dir (panel);
3082 break;
3083 case CK_CdParent:
3084 goto_parent_dir (panel);
3085 break;
3086 case CK_History:
3087 directory_history_list (panel);
3088 break;
3089 case CK_HistoryNext:
3090 directory_history_next (panel);
3091 break;
3092 case CK_HistoryPrev:
3093 directory_history_prev (panel);
3094 break;
3095 case CK_BottomOnScreen:
3096 goto_bottom_file (panel);
3097 break;
3098 case CK_MiddleOnScreen:
3099 goto_middle_file (panel);
3100 break;
3101 case CK_TopOnScreen:
3102 goto_top_file (panel);
3103 break;
3104 case CK_Mark:
3105 mark_file (panel);
3106 break;
3107 case CK_MarkUp:
3108 mark_file_up (panel);
3109 break;
3110 case CK_MarkDown:
3111 mark_file_down (panel);
3112 break;
3113 case CK_MarkLeft:
3114 mark_file_left (panel);
3115 break;
3116 case CK_MarkRight:
3117 mark_file_right (panel);
3118 break;
3119 case CK_CdParentSmart:
3120 res = force_maybe_cd ();
3121 break;
3122 case CK_Up:
3123 move_up (panel);
3124 break;
3125 case CK_Down:
3126 move_down (panel);
3127 break;
3128 case CK_Left:
3129 res = move_left (panel);
3130 break;
3131 case CK_Right:
3132 res = move_right (panel);
3133 break;
3134 case CK_Bottom:
3135 move_end (panel);
3136 break;
3137 case CK_Top:
3138 move_home (panel);
3139 break;
3140 #ifdef HAVE_CHARSET
3141 case CK_SelectCodepage:
3142 panel_change_encoding (panel);
3143 break;
3144 #endif
3145 case CK_Search:
3146 start_search (panel);
3147 break;
3148 case CK_SearchStop:
3149 break;
3150 case CK_PanelOtherSync:
3151 panel_sync_other (panel);
3152 break;
3153 case CK_Sort:
3154 panel_select_sort_order (panel);
3155 break;
3156 case CK_SortPrev:
3157 panel_toggle_sort_order_prev (panel);
3158 break;
3159 case CK_SortNext:
3160 panel_toggle_sort_order_next (panel);
3161 break;
3162 case CK_SortReverse:
3163 panel->sort_info.reverse = !panel->sort_info.reverse;
3164 panel_set_sort_order (panel, panel->sort_info.sort_field);
3165 break;
3166 case CK_SortByName:
3167 panel_set_sort_type_by_id (panel, "name");
3168 break;
3169 case CK_SortByExt:
3170 panel_set_sort_type_by_id (panel, "extension");
3171 break;
3172 case CK_SortBySize:
3173 panel_set_sort_type_by_id (panel, "size");
3174 break;
3175 case CK_SortByMTime:
3176 panel_set_sort_type_by_id (panel, "mtime");
3177 break;
3179 return res;
3182 /* --------------------------------------------------------------------------------------------- */
3184 static cb_ret_t
3185 panel_key (WPanel * panel, int key)
3187 size_t i;
3189 if (is_abort_char (key))
3191 stop_search (panel);
3192 return MSG_HANDLED;
3195 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3197 do_search (panel, key);
3198 return MSG_HANDLED;
3201 for (i = 0; panel_map[i].key != 0; i++)
3202 if (key == panel_map[i].key)
3203 return panel_execute_cmd (panel, panel_map[i].command);
3205 if (panels_options.torben_fj_mode && key == ALT ('h'))
3207 goto_middle_file (panel);
3208 return MSG_HANDLED;
3211 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3213 start_search (panel);
3214 do_search (panel, key);
3215 return MSG_HANDLED;
3218 return MSG_NOT_HANDLED;
3221 /* --------------------------------------------------------------------------------------------- */
3223 static cb_ret_t
3224 panel_callback (Widget * w, widget_msg_t msg, int parm)
3226 WPanel *panel = (WPanel *) w;
3227 WButtonBar *bb;
3229 switch (msg)
3231 case WIDGET_INIT:
3232 /* subscribe to "history_load" event */
3233 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3234 /* subscribe to "history_save" event */
3235 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3236 return MSG_HANDLED;
3238 case WIDGET_DRAW:
3239 /* Repaint everything, including frame and separator */
3240 paint_frame (panel); /* including show_dir */
3241 paint_dir (panel);
3242 mini_info_separator (panel);
3243 display_mini_info (panel);
3244 panel->dirty = 0;
3245 return MSG_HANDLED;
3247 case WIDGET_FOCUS:
3248 state_mark = -1;
3249 current_panel = panel;
3250 panel->active = 1;
3251 if (mc_chdir (panel->cwd_vpath) != 0)
3253 char *cwd;
3255 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3256 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3257 cwd, unix_error_string (errno));
3258 g_free (cwd);
3260 else
3261 subshell_chdir (panel->cwd_vpath);
3263 update_xterm_title_path ();
3264 select_item (panel);
3265 show_dir (panel);
3266 paint_dir (panel);
3267 panel->dirty = 0;
3269 bb = find_buttonbar (panel->widget.owner);
3270 midnight_set_buttonbar (bb);
3271 buttonbar_redraw (bb);
3272 return MSG_HANDLED;
3274 case WIDGET_UNFOCUS:
3275 /* Janne: look at this for the multiple panel options */
3276 stop_search (panel);
3277 panel->active = 0;
3278 show_dir (panel);
3279 unselect_item (panel);
3280 return MSG_HANDLED;
3282 case WIDGET_KEY:
3283 return panel_key (panel, parm);
3285 case WIDGET_COMMAND:
3286 return panel_execute_cmd (panel, parm);
3288 case WIDGET_DESTROY:
3289 /* unsubscribe from "history_load" event */
3290 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3291 /* unsubscribe from "history_save" event */
3292 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3293 panel_destroy (panel);
3294 free_my_statfs ();
3295 return MSG_HANDLED;
3297 default:
3298 return default_proc (msg, parm);
3302 /* --------------------------------------------------------------------------------------------- */
3303 /* */
3304 /* Panel mouse events support routines */
3305 /* */
3307 static void
3308 mouse_toggle_mark (WPanel * panel)
3310 do_mark_file (panel, MARK_DONT_MOVE);
3311 mouse_marking = selection (panel)->f.marked;
3312 mouse_mark_panel = current_panel;
3315 /* --------------------------------------------------------------------------------------------- */
3317 static void
3318 mouse_set_mark (WPanel * panel)
3321 if (mouse_mark_panel == panel)
3323 if (mouse_marking && !(selection (panel)->f.marked))
3324 do_mark_file (panel, MARK_DONT_MOVE);
3325 else if (!mouse_marking && (selection (panel)->f.marked))
3326 do_mark_file (panel, MARK_DONT_MOVE);
3330 /* --------------------------------------------------------------------------------------------- */
3332 static int
3333 mark_if_marking (WPanel * panel, Gpm_Event * event)
3335 if (event->buttons & GPM_B_RIGHT)
3337 if (event->type & GPM_DOWN)
3338 mouse_toggle_mark (panel);
3339 else
3340 mouse_set_mark (panel);
3341 return 1;
3343 return 0;
3346 /* --------------------------------------------------------------------------------------------- */
3347 /** Determine which column was clicked, and sort the panel on
3348 * that column, or reverse sort on that column if already
3349 * sorted on that column.
3352 static void
3353 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3355 int i;
3356 const char *lc_sort_name = NULL;
3357 panel_field_t *col_sort_format = NULL;
3358 format_e *format;
3359 gchar *title;
3361 for (i = 0, format = panel->format; format != NULL; format = format->next)
3363 i += format->field_len;
3364 if (event->x < i + 1)
3366 /* found column */
3367 lc_sort_name = format->title;
3368 break;
3372 if (lc_sort_name == NULL)
3373 return;
3375 for (i = 0; panel_fields[i].id != NULL; i++)
3377 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3378 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3380 col_sort_format = &panel_fields[i];
3381 g_free (title);
3382 break;
3384 g_free (title);
3387 if (col_sort_format == NULL)
3388 return;
3390 if (panel->sort_info.sort_field == col_sort_format)
3392 /* reverse the sort if clicked column is already the sorted column */
3393 panel->sort_info.reverse = !panel->sort_info.reverse;
3395 else
3397 /* new sort is forced to be ascending */
3398 panel->sort_info.reverse = FALSE;
3400 panel_set_sort_order (panel, col_sort_format);
3404 /* --------------------------------------------------------------------------------------------- */
3406 * Mouse callback of the panel minus repainting.
3407 * If the event is redirected to the menu, *redir is set to TRUE.
3409 static int
3410 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3412 const int lines = llines (panel);
3413 const gboolean is_active = dlg_widget_active (panel);
3414 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3416 *redir = FALSE;
3418 /* 1st line */
3419 if (mouse_down && event->y == 1)
3421 /* "<" button */
3422 if (event->x == 2)
3424 directory_history_prev (panel);
3425 return MOU_NORMAL;
3428 /* "." button show/hide hidden files */
3429 if (event->x == panel->widget.cols - 5)
3431 panel->widget.owner->callback (panel->widget.owner, NULL,
3432 DLG_ACTION, CK_ShowHidden, NULL);
3433 repaint_screen ();
3434 return MOU_NORMAL;
3437 /* ">" button */
3438 if (event->x == panel->widget.cols - 1)
3440 directory_history_next (panel);
3441 return MOU_NORMAL;
3444 /* "^" button */
3445 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3447 directory_history_list (panel);
3448 return MOU_NORMAL;
3451 /* rest of the upper frame, the menu is invisible - call menu */
3452 if (!menubar_visible)
3454 *redir = TRUE;
3455 event->x += panel->widget.x;
3456 return the_menubar->widget.mouse (event, the_menubar);
3459 /* no other events on 1st line */
3460 return MOU_NORMAL;
3463 /* sort on clicked column; don't handle wheel events */
3464 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3466 mouse_sort_col (event, panel);
3467 return MOU_NORMAL;
3470 /* Mouse wheel events */
3471 if (mouse_down && (event->buttons & GPM_B_UP))
3473 if (is_active)
3475 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3476 prev_page (panel);
3477 else /* We are in first page */
3478 move_up (panel);
3480 return MOU_NORMAL;
3483 if (mouse_down && (event->buttons & GPM_B_DOWN))
3485 if (is_active)
3487 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3488 next_page (panel);
3489 else /* We are in last page */
3490 move_down (panel);
3492 return MOU_NORMAL;
3495 event->y -= 2;
3496 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3498 int my_index;
3500 if (!is_active)
3501 change_panel ();
3503 if (panel->top_file + event->y > panel->count)
3504 my_index = panel->count - 1;
3505 else
3507 my_index = panel->top_file + event->y - 1;
3508 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3509 my_index += llines (panel);
3511 if (my_index >= panel->count)
3512 my_index = panel->count - 1;
3515 if (my_index != panel->selected)
3517 unselect_item (panel);
3518 panel->selected = my_index;
3519 select_item (panel);
3522 /* This one is new */
3523 mark_if_marking (panel, event);
3525 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3527 if (event->y > 0 && event->y <= lines)
3528 do_enter (panel);
3530 return MOU_NORMAL;
3533 /* --------------------------------------------------------------------------------------------- */
3534 /** Mouse callback of the panel */
3536 static int
3537 panel_event (Gpm_Event * event, void *data)
3539 WPanel *panel = data;
3540 int ret;
3541 gboolean redir;
3543 ret = do_panel_event (event, panel, &redir);
3544 if (!redir)
3545 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3547 return ret;
3550 /* --------------------------------------------------------------------------------------------- */
3552 static void
3553 reload_panelized (WPanel * panel)
3555 int i, j;
3556 dir_list *list = &panel->dir;
3558 if (panel != current_panel)
3559 (void) mc_chdir (panel->cwd_vpath);
3561 for (i = 0, j = 0; i < panel->count; i++)
3563 vfs_path_t *vpath;
3565 if (list->list[i].f.marked)
3567 /* Unmark the file in advance. In case the following mc_lstat
3568 * fails we are done, else we have to mark the file again
3569 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3570 * IMO that's the best way to update the panel's summary status
3571 * -- Norbert
3573 do_file_mark (panel, i, 0);
3575 vpath = vfs_path_from_str (list->list[i].fname);
3576 if (mc_lstat (vpath, &list->list[i].st))
3577 g_free (list->list[i].fname);
3578 else
3580 if (list->list[i].f.marked)
3581 do_file_mark (panel, i, 1);
3582 if (j != i)
3583 list->list[j] = list->list[i];
3584 j++;
3586 vfs_path_free (vpath);
3588 if (j == 0)
3589 panel->count = set_zero_dir (list) ? 1 : 0;
3590 else
3591 panel->count = j;
3593 if (panel != current_panel)
3594 (void) mc_chdir (current_panel->cwd_vpath);
3597 /* --------------------------------------------------------------------------------------------- */
3599 static void
3600 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3602 gboolean free_pointer;
3603 char *my_current_file = NULL;
3605 if ((flags & UP_RELOAD) != 0)
3607 panel->is_panelized = FALSE;
3608 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3609 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3612 /* If current_file == -1 (an invalid pointer) then preserve selection */
3613 free_pointer = current_file == UP_KEEPSEL;
3615 if (free_pointer)
3617 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3618 current_file = my_current_file;
3621 if (panel->is_panelized)
3622 reload_panelized (panel);
3623 else
3624 panel_reload (panel);
3626 try_to_select (panel, current_file);
3627 panel->dirty = 1;
3629 if (free_pointer)
3630 g_free (my_current_file);
3633 /* --------------------------------------------------------------------------------------------- */
3635 static void
3636 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3638 if (get_display_type (which) == view_listing)
3640 WPanel *panel;
3642 panel = (WPanel *) get_panel_widget (which);
3643 if (panel->is_panelized)
3644 flags &= ~UP_RELOAD;
3645 update_one_panel_widget (panel, flags, current_file);
3649 /* --------------------------------------------------------------------------------------------- */
3651 static void
3652 do_select (WPanel * panel, int i)
3654 if (i != panel->selected)
3656 panel->dirty = 1;
3657 panel->selected = i;
3658 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3659 if (panel->top_file < 0)
3660 panel->top_file = 0;
3664 /* --------------------------------------------------------------------------------------------- */
3666 static void
3667 do_try_to_select (WPanel * panel, const char *name)
3669 int i;
3670 char *subdir;
3672 if (!name)
3674 do_select (panel, 0);
3675 return;
3678 /* We only want the last component of the directory,
3679 * and from this only the name without suffix.
3680 * Cut prefix if the panel is not panelized */
3682 if (panel->is_panelized)
3683 subdir = vfs_strip_suffix_from_filename (name);
3684 else
3685 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3687 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3688 for (i = 0; i < panel->count; i++)
3690 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3692 do_select (panel, i);
3693 g_free (subdir);
3694 return;
3698 /* Try to select a file near the file that is missing */
3699 if (panel->selected >= panel->count)
3700 do_select (panel, panel->count - 1);
3701 g_free (subdir);
3704 /* --------------------------------------------------------------------------------------------- */
3706 /* event callback */
3707 static gboolean
3708 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3709 gpointer init_data, gpointer data)
3711 (void) event_group_name;
3712 (void) event_name;
3713 (void) init_data;
3714 (void) data;
3716 update_panels (UP_RELOAD, UP_KEEPSEL);
3718 return TRUE;
3721 /* --------------------------------------------------------------------------------------------- */
3723 /* event callback */
3724 static gboolean
3725 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3726 gpointer init_data, gpointer data)
3728 (void) event_group_name;
3729 (void) event_name;
3730 (void) init_data;
3731 (void) data;
3733 if (current_panel->marked == 0)
3734 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3735 (gpointer) selection (current_panel)->fname);
3736 else
3738 int i;
3739 gboolean first = TRUE;
3740 char *flist = NULL;
3742 for (i = 0; i < current_panel->count; i++)
3743 if (current_panel->dir.list[i].f.marked != 0)
3744 { /* Skip the unmarked ones */
3745 if (first)
3747 flist = g_strdup (current_panel->dir.list[i].fname);
3748 first = FALSE;
3750 else
3752 /* Add empty lines after the file */
3753 char *tmp;
3755 tmp =
3756 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3757 g_free (flist);
3758 flist = tmp;
3762 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3763 g_free (flist);
3765 return TRUE;
3768 /* --------------------------------------------------------------------------------------------- */
3769 /*** public functions ****************************************************************************/
3770 /* --------------------------------------------------------------------------------------------- */
3772 void
3773 try_to_select (WPanel * panel, const char *name)
3775 do_try_to_select (panel, name);
3776 select_item (panel);
3779 /* --------------------------------------------------------------------------------------------- */
3781 void
3782 panel_clean_dir (WPanel * panel)
3784 int count = panel->count;
3786 panel->count = 0;
3787 panel->top_file = 0;
3788 panel->selected = 0;
3789 panel->marked = 0;
3790 panel->dirs_marked = 0;
3791 panel->total = 0;
3792 panel->searching = FALSE;
3793 panel->is_panelized = FALSE;
3794 panel->dirty = 1;
3796 clean_dir (&panel->dir, count);
3799 /* --------------------------------------------------------------------------------------------- */
3801 * Set Up panel's current dir object
3803 * @param panel panel object
3804 * @param path_str string contain path
3807 void
3808 panel_set_cwd (WPanel * panel, const char *path_str)
3810 vfs_path_free (panel->cwd_vpath);
3811 panel->cwd_vpath = vfs_path_from_str (path_str);
3814 /* --------------------------------------------------------------------------------------------- */
3816 * Set Up panel's last working dir object
3818 * @param panel panel object
3819 * @param path_str string contain path
3822 void
3823 panel_set_lwd (WPanel * panel, const char *path_str)
3825 vfs_path_free (panel->lwd_vpath);
3826 panel->lwd_vpath = vfs_path_from_str (path_str);
3829 /* --------------------------------------------------------------------------------------------- */
3830 /** Panel creation.
3831 * @param panel_name the name of the panel for setup retieving
3832 * @returns new instance of WPanel
3835 WPanel *
3836 panel_new (const char *panel_name)
3838 return panel_new_with_dir (panel_name, NULL);
3841 /* --------------------------------------------------------------------------------------------- */
3842 /** Panel creation for specified directory.
3843 * @param panel_name specifies the name of the panel for setup retieving
3844 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3845 * @returns new instance of WPanel
3848 WPanel *
3849 panel_new_with_dir (const char *panel_name, const char *wpath)
3851 WPanel *panel;
3852 char *section;
3853 int i, err;
3854 char *curdir = NULL;
3856 panel = g_new0 (WPanel, 1);
3858 /* No know sizes of the panel at startup */
3859 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3861 /* We do not want the cursor */
3862 widget_want_cursor (panel->widget, 0);
3864 if (wpath != NULL)
3866 curdir = _vfs_get_cwd ();
3867 panel_set_cwd (panel, wpath);
3869 else
3871 vfs_setup_cwd ();
3872 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3875 panel_set_lwd (panel, ".");
3877 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3878 /* directories history will be get later */
3880 panel->dir.list = g_new (file_entry, MIN_FILES);
3881 panel->dir.size = MIN_FILES;
3882 panel->active = 0;
3883 panel->filter = 0;
3884 panel->split = 0;
3885 panel->top_file = 0;
3886 panel->selected = 0;
3887 panel->marked = 0;
3888 panel->total = 0;
3889 panel->dirty = 1;
3890 panel->searching = FALSE;
3891 panel->dirs_marked = 0;
3892 panel->is_panelized = FALSE;
3893 panel->format = 0;
3894 panel->status_format = 0;
3895 panel->format_modified = 1;
3897 panel->panel_name = g_strdup (panel_name);
3898 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3900 #ifdef HAVE_CHARSET
3901 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3902 #endif
3904 for (i = 0; i < LIST_TYPES; i++)
3905 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3907 panel->search_buffer[0] = '\0';
3908 panel->prev_search_buffer[0] = '\0';
3909 panel->frame_size = frame_half;
3911 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3912 if (!mc_config_has_group (mc_main_config, section))
3914 g_free (section);
3915 section = g_strdup (panel->panel_name);
3917 panel_load_setup (panel, section);
3918 g_free (section);
3920 /* Load format strings */
3921 err = set_panel_formats (panel);
3922 if (err != 0)
3923 set_panel_formats (panel);
3925 #ifdef HAVE_CHARSET
3927 const vfs_path_element_t *path_element;
3929 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
3930 if (path_element->encoding != NULL)
3931 panel->codepage = get_codepage_index (path_element->encoding);
3933 #endif
3935 if (mc_chdir (panel->cwd_vpath) != 0)
3937 #ifdef HAVE_CHARSET
3938 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3939 #endif
3940 vfs_setup_cwd ();
3941 vfs_path_free (panel->cwd_vpath);
3942 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3945 /* Load the default format */
3946 panel->count =
3947 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
3948 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3949 panel->sort_info.exec_first, panel->filter);
3951 /* Restore old right path */
3952 if (curdir != NULL)
3954 vfs_path_t *vpath;
3956 vpath = vfs_path_from_str (curdir);
3957 err = mc_chdir (vpath);
3958 vfs_path_free (vpath);
3960 g_free (curdir);
3962 return panel;
3965 /* --------------------------------------------------------------------------------------------- */
3967 void
3968 panel_reload (WPanel * panel)
3970 struct stat current_stat;
3971 char *tmp_path;
3972 gboolean ok;
3974 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3975 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
3976 && current_stat.st_ctime == panel->dir_stat.st_ctime
3977 && current_stat.st_mtime == panel->dir_stat.st_mtime);
3978 g_free (tmp_path);
3980 if (ok)
3981 return;
3985 char *last_slash;
3987 if (mc_chdir (panel->cwd_vpath) != -1)
3988 break;
3990 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3991 if (tmp_path[0] == PATH_SEP && tmp_path[1] == '\0')
3993 panel_clean_dir (panel);
3994 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
3995 g_free (tmp_path);
3996 return;
3998 last_slash = strrchr (tmp_path, PATH_SEP);
3999 vfs_path_free (panel->cwd_vpath);
4000 if (last_slash == NULL || last_slash == tmp_path)
4001 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4002 else
4004 *last_slash = '\0';
4005 panel->cwd_vpath = vfs_path_from_str (tmp_path);
4007 g_free (tmp_path);
4008 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4009 show_dir (panel);
4011 while (TRUE);
4013 panel->count =
4014 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4015 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4016 panel->sort_info.exec_first, panel->filter);
4018 panel->dirty = 1;
4019 if (panel->selected >= panel->count)
4020 do_select (panel, panel->count - 1);
4022 recalculate_panel_summary (panel);
4025 /* --------------------------------------------------------------------------------------------- */
4026 /* Switches the panel to the mode specified in the format */
4027 /* Seting up both format and status string. Return: 0 - on success; */
4028 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4031 set_panel_formats (WPanel * p)
4033 format_e *form;
4034 char *err = NULL;
4035 int retcode = 0;
4037 form = use_display_format (p, panel_format (p), &err, 0);
4039 if (err != NULL)
4041 g_free (err);
4042 retcode = 1;
4044 else
4046 delete_format (p->format);
4047 p->format = form;
4050 if (panels_options.show_mini_info)
4052 form = use_display_format (p, mini_status_format (p), &err, 1);
4054 if (err != NULL)
4056 g_free (err);
4057 retcode += 2;
4059 else
4061 delete_format (p->status_format);
4062 p->status_format = form;
4066 panel_format_modified (p);
4067 panel_update_cols (&(p->widget), p->frame_size);
4069 if (retcode)
4070 message (D_ERROR, _("Warning"),
4071 _("User supplied format looks invalid, reverting to default."));
4072 if (retcode & 0x01)
4074 g_free (p->user_format);
4075 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4077 if (retcode & 0x02)
4079 g_free (p->user_status_format[p->list_type]);
4080 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4083 return retcode;
4086 /* --------------------------------------------------------------------------------------------- */
4088 void
4089 panel_update_cols (Widget * widget, panel_display_t frame_size)
4091 int cols, origin;
4093 /* don't touch panel if it is not in dialog yet */
4094 /* if panel is not in dialog it is not in widgets list
4095 and cannot be compared with get_panel_widget() result */
4096 if (widget->owner == NULL)
4097 return;
4099 if (panels_layout.horizontal_split)
4101 widget->cols = COLS;
4102 return;
4105 if (frame_size == frame_full)
4107 cols = COLS;
4108 origin = 0;
4110 else if (widget == get_panel_widget (0))
4112 cols = panels_layout.left_panel_size;
4113 origin = 0;
4115 else
4117 cols = COLS - panels_layout.left_panel_size;
4118 origin = panels_layout.left_panel_size;
4121 widget->cols = cols;
4122 widget->x = origin;
4125 /* --------------------------------------------------------------------------------------------- */
4127 /* Select current item and readjust the panel */
4128 void
4129 select_item (WPanel * panel)
4131 /* Although currently all over the code we set the selection and
4132 top file to decent values before calling select_item, I could
4133 forget it someday, so it's better to do the actual fitting here */
4135 if (panel->selected < 0)
4136 panel->selected = 0;
4138 if (panel->selected > panel->count - 1)
4139 panel->selected = panel->count - 1;
4141 adjust_top_file (panel);
4143 panel->dirty = 1;
4145 execute_hooks (select_file_hook);
4148 /* --------------------------------------------------------------------------------------------- */
4149 /** Clears all files in the panel, used only when one file was marked */
4150 void
4151 unmark_files (WPanel * panel)
4153 int i;
4155 if (!panel->marked)
4156 return;
4157 for (i = 0; i < panel->count; i++)
4158 file_mark (panel, i, 0);
4160 panel->dirs_marked = 0;
4161 panel->marked = 0;
4162 panel->total = 0;
4165 /* --------------------------------------------------------------------------------------------- */
4166 /** Recalculate the panels summary information, used e.g. when marked
4167 files might have been removed by an external command */
4169 void
4170 recalculate_panel_summary (WPanel * panel)
4172 int i;
4174 panel->marked = 0;
4175 panel->dirs_marked = 0;
4176 panel->total = 0;
4178 for (i = 0; i < panel->count; i++)
4179 if (panel->dir.list[i].f.marked)
4181 /* do_file_mark will return immediately if newmark == oldmark.
4182 So we have to first unmark it to get panel's summary information
4183 updated. (Norbert) */
4184 panel->dir.list[i].f.marked = 0;
4185 do_file_mark (panel, i, 1);
4189 /* --------------------------------------------------------------------------------------------- */
4190 /** This routine marks a file or a directory */
4192 void
4193 do_file_mark (WPanel * panel, int idx, int mark)
4195 if (panel->dir.list[idx].f.marked == mark)
4196 return;
4198 /* Only '..' can't be marked, '.' isn't visible */
4199 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4200 return;
4202 file_mark (panel, idx, mark);
4203 if (panel->dir.list[idx].f.marked)
4205 panel->marked++;
4206 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4208 if (panel->dir.list[idx].f.dir_size_computed)
4209 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4210 panel->dirs_marked++;
4212 else
4213 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4214 set_colors (panel);
4216 else
4218 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4220 if (panel->dir.list[idx].f.dir_size_computed)
4221 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4222 panel->dirs_marked--;
4224 else
4225 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4226 panel->marked--;
4230 /* --------------------------------------------------------------------------------------------- */
4232 * Changes the current directory of the panel.
4233 * Record change in the directory history.
4235 gboolean
4236 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4238 gboolean r;
4240 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4241 if (r)
4243 char *tmp_path;
4245 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4246 directory_history_add (panel, tmp_path);
4247 g_free (tmp_path);
4249 return r;
4252 /* --------------------------------------------------------------------------------------------- */
4254 void
4255 file_mark (WPanel * panel, int lc_index, int val)
4257 if (panel->dir.list[lc_index].f.marked != val)
4259 panel->dir.list[lc_index].f.marked = val;
4260 panel->dirty = 1;
4264 /* --------------------------------------------------------------------------------------------- */
4266 void
4267 panel_re_sort (WPanel * panel)
4269 char *filename;
4270 int i;
4272 if (panel == NULL)
4273 return;
4275 filename = g_strdup (selection (panel)->fname);
4276 unselect_item (panel);
4277 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4278 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4279 panel->sort_info.exec_first);
4280 panel->selected = -1;
4281 for (i = panel->count; i; i--)
4283 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4285 panel->selected = i - 1;
4286 break;
4289 g_free (filename);
4290 panel->top_file = panel->selected - ITEMS (panel) / 2;
4291 select_item (panel);
4292 panel->dirty = 1;
4295 /* --------------------------------------------------------------------------------------------- */
4297 void
4298 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4300 if (sort_order == NULL)
4301 return;
4303 panel->sort_info.sort_field = sort_order;
4305 /* The directory is already sorted, we have to load the unsorted stuff */
4306 if (sort_order->sort_routine == (sortfn *) unsorted)
4308 char *current_file;
4310 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4311 panel_reload (panel);
4312 try_to_select (panel, current_file);
4313 g_free (current_file);
4315 panel_re_sort (panel);
4318 /* --------------------------------------------------------------------------------------------- */
4320 * Change panel encoding.
4321 * @param panel WPanel object
4324 #ifdef HAVE_CHARSET
4325 void
4326 panel_change_encoding (WPanel * panel)
4328 const char *encoding = NULL;
4329 char *errmsg;
4330 int r;
4332 r = select_charset (-1, -1, panel->codepage, FALSE);
4334 if (r == SELECT_CHARSET_CANCEL)
4335 return; /* Cancel */
4337 panel->codepage = r;
4339 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4341 /* No translation */
4342 vfs_path_t *cd_path_vpath;
4344 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4345 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4346 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4347 show_dir (panel);
4348 vfs_path_free (cd_path_vpath);
4349 return;
4352 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4353 if (errmsg != NULL)
4355 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4356 g_free (errmsg);
4357 return;
4360 encoding = get_codepage_id (panel->codepage);
4361 if (encoding != NULL)
4363 char *cd_path;
4364 vfs_change_encoding (panel->cwd_vpath, encoding);
4366 cd_path = vfs_path_to_str (panel->cwd_vpath);
4367 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4368 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4369 g_free (cd_path);
4373 /* --------------------------------------------------------------------------------------------- */
4376 * Remove encode info from last path element.
4379 vfs_path_t *
4380 remove_encoding_from_path (const vfs_path_t * vpath)
4382 vfs_path_t *ret_vpath;
4383 GString *tmp_conv;
4384 int indx;
4386 ret_vpath = vfs_path_new ();
4388 tmp_conv = g_string_new ("");
4390 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4392 GIConv converter;
4393 vfs_path_element_t *path_element;
4395 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4396 vfs_path_add_element (ret_vpath, path_element);
4398 if (path_element->encoding == NULL)
4399 continue;
4401 converter = str_crt_conv_to (path_element->encoding);
4402 if (converter == INVALID_CONV)
4403 continue;
4405 g_free (path_element->encoding);
4406 path_element->encoding = NULL;
4408 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4410 g_free (path_element->path);
4411 path_element->path = g_strdup (tmp_conv->str);
4413 g_string_set_size (tmp_conv, 0);
4415 str_close_conv (converter);
4416 str_close_conv (path_element->dir.converter);
4417 path_element->dir.converter = INVALID_CONV;
4419 g_string_free (tmp_conv, TRUE);
4420 return ret_vpath;
4422 #endif /* HAVE_CHARSET */
4424 /* --------------------------------------------------------------------------------------------- */
4426 * This routine reloads the directory in both panels. It tries to
4427 * select current_file in current_panel and other_file in other_panel.
4428 * If current_file == -1 then it automatically sets current_file and
4429 * other_file to the currently selected files in the panels.
4431 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4432 * will not reload the other panel.
4435 void
4436 update_panels (panel_update_flags_t flags, const char *current_file)
4438 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4439 WPanel *panel;
4441 update_one_panel (get_current_index (), flags, current_file);
4442 if (reload_other)
4443 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4445 if (get_current_type () == view_listing)
4446 panel = (WPanel *) get_panel_widget (get_current_index ());
4447 else
4448 panel = (WPanel *) get_panel_widget (get_other_index ());
4450 if (!panel->is_panelized)
4451 (void) mc_chdir (panel->cwd_vpath);
4454 /* --------------------------------------------------------------------------------------------- */
4456 void
4457 directory_history_add (struct WPanel *panel, const char *dir)
4459 vfs_path_t *vpath;
4460 char *tmp;
4462 vpath = vfs_path_from_str (dir);
4463 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
4464 vfs_path_free (vpath);
4465 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4468 /* --------------------------------------------------------------------------------------------- */
4470 gsize
4471 panel_get_num_of_sortable_fields (void)
4473 gsize ret = 0, lc_index;
4475 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4476 if (panel_fields[lc_index].is_user_choice)
4477 ret++;
4478 return ret;
4481 /* --------------------------------------------------------------------------------------------- */
4483 const char **
4484 panel_get_sortable_fields (gsize * array_size)
4486 char **ret;
4487 gsize lc_index, i;
4489 lc_index = panel_get_num_of_sortable_fields ();
4491 ret = g_try_new0 (char *, lc_index + 1);
4492 if (ret == NULL)
4493 return NULL;
4495 if (array_size != NULL)
4496 *array_size = lc_index;
4498 lc_index = 0;
4500 for (i = 0; panel_fields[i].id != NULL; i++)
4501 if (panel_fields[i].is_user_choice)
4502 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4503 return (const char **) ret;
4506 /* --------------------------------------------------------------------------------------------- */
4508 const panel_field_t *
4509 panel_get_field_by_id (const char *name)
4511 gsize lc_index;
4512 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4513 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4514 return &panel_fields[lc_index];
4515 return NULL;
4518 /* --------------------------------------------------------------------------------------------- */
4520 const panel_field_t *
4521 panel_get_field_by_title_hotkey (const char *name)
4523 gsize lc_index;
4524 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4525 if (panel_fields[lc_index].title_hotkey != NULL &&
4526 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4527 return &panel_fields[lc_index];
4528 return NULL;
4531 /* --------------------------------------------------------------------------------------------- */
4533 const panel_field_t *
4534 panel_get_field_by_title (const char *name)
4536 gsize lc_index;
4537 gchar *title = NULL;
4539 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4541 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4542 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4544 g_free (title);
4545 return &panel_fields[lc_index];
4548 g_free (title);
4549 return NULL;
4552 /* --------------------------------------------------------------------------------------------- */
4554 gsize
4555 panel_get_num_of_user_possible_fields (void)
4557 gsize ret = 0, lc_index;
4559 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4560 if (panel_fields[lc_index].use_in_user_format)
4561 ret++;
4562 return ret;
4565 /* --------------------------------------------------------------------------------------------- */
4567 const char **
4568 panel_get_user_possible_fields (gsize * array_size)
4570 char **ret;
4571 gsize lc_index, i;
4573 lc_index = panel_get_num_of_user_possible_fields ();
4575 ret = g_try_new0 (char *, lc_index + 1);
4576 if (ret == NULL)
4577 return NULL;
4579 if (array_size != NULL)
4580 *array_size = lc_index;
4582 lc_index = 0;
4584 for (i = 0; panel_fields[i].id != NULL; i++)
4585 if (panel_fields[i].use_in_user_format)
4586 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4587 return (const char **) ret;
4590 /* --------------------------------------------------------------------------------------------- */
4592 void
4593 panel_init (void)
4595 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4596 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4598 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4599 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4600 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4601 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4602 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4604 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4605 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4606 panel_save_curent_file_to_clip_file, NULL, NULL);
4610 /* --------------------------------------------------------------------------------------------- */
4612 void
4613 panel_deinit (void)
4615 g_free (panel_sort_up_sign);
4616 g_free (panel_sort_down_sign);
4618 g_free (panel_hiddenfiles_sign_show);
4619 g_free (panel_hiddenfiles_sign_hide);
4620 g_free (panel_history_prev_item_sign);
4621 g_free (panel_history_next_item_sign);
4622 g_free (panel_history_show_list_sign);
4626 /* --------------------------------------------------------------------------------------------- */