vfs_path: Fixed broken relative paths processing.
[midnight-commander.git] / src / filemanager / panel.c
blob122229ccea7dfd07fe83233fef8f1cbeab4b42a8
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;
1088 last_vpath = vfs_path_new ();
1089 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1090 vfs_path_add_element (last_vpath, path_element);
1091 return_path =
1092 vfs_path_to_str_flags (last_vpath, 0,
1093 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1094 vfs_path_free (last_vpath);
1096 return return_path;
1099 /* --------------------------------------------------------------------------------------------- */
1101 * Get Current path element encoding
1103 * @param panel WPanel object
1105 * @return newly allocated string or NULL if path charset is same as system charset
1108 static char *
1109 panel_get_encoding_info_str (WPanel * panel)
1111 char *ret_str = NULL;
1112 const vfs_path_element_t *path_element;
1114 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1115 if (path_element->encoding != NULL)
1116 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1118 return ret_str;
1121 /* --------------------------------------------------------------------------------------------- */
1123 static void
1124 show_dir (WPanel * panel)
1126 gchar *tmp;
1127 set_colors (panel);
1128 draw_box (panel->widget.owner,
1129 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1131 if (panels_options.show_mini_info)
1133 widget_move (&panel->widget, llines (panel) + 2, 0);
1134 tty_print_alt_char (ACS_LTEE, FALSE);
1135 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1136 tty_print_alt_char (ACS_RTEE, FALSE);
1139 widget_move (&panel->widget, 0, 1);
1140 tty_print_string (panel_history_prev_item_sign);
1142 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1143 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1144 panel_history_next_item_sign);
1146 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1147 tty_print_string (tmp);
1149 g_free (tmp);
1151 widget_move (&panel->widget, 0, 3);
1153 if (panel->is_panelized)
1154 tty_printf (" %s ", _("Panelize"));
1155 else
1157 tmp = panel_get_encoding_info_str (panel);
1158 if (tmp != NULL)
1160 tty_printf ("%s", tmp);
1161 widget_move (&panel->widget, 0, 3 + strlen (tmp));
1162 g_free (tmp);
1165 if (panel->active)
1166 tty_setcolor (REVERSE_COLOR);
1168 tmp = panel_correct_path_to_show (panel);
1169 tty_printf (" %s ",
1170 str_term_trim (tmp, min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1171 g_free (tmp);
1173 if (!panels_options.show_mini_info)
1175 if (panel->marked == 0)
1177 /* Show size of curret file in the bottom of panel */
1178 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1180 char buffer[BUF_SMALL];
1182 g_snprintf (buffer, sizeof (buffer), " %s ",
1183 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1184 panels_options.kilobyte_si));
1185 tty_setcolor (NORMAL_COLOR);
1186 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1187 tty_print_string (buffer);
1190 else
1192 /* Show total size of marked files
1193 * In the bottom of panel, display size only. */
1194 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1198 show_free_space (panel);
1200 if (panel->active)
1201 tty_set_normal_attrs ();
1204 /* --------------------------------------------------------------------------------------------- */
1206 /* Returns the number of items in the given panel */
1207 static int
1208 ITEMS (WPanel * p)
1210 if (p->split)
1211 return llines (p) * 2;
1212 else
1213 return llines (p);
1216 /* --------------------------------------------------------------------------------------------- */
1218 static void
1219 adjust_top_file (WPanel * panel)
1221 int items = ITEMS (panel);
1223 if (panel->count <= items)
1225 /* If all files fit, show them all. */
1226 panel->top_file = 0;
1228 else
1230 int i;
1232 /* top_file has to be in the range [selected-items+1, selected] so that
1233 the selected file is visible.
1234 top_file should be in the range [0, count-items] so that there's
1235 no empty space wasted.
1236 Within these ranges, adjust it by as little as possible. */
1238 if (panel->top_file < 0)
1239 panel->top_file = 0;
1241 i = panel->selected - items + 1;
1242 if (panel->top_file < i)
1243 panel->top_file = i;
1245 i = panel->count - items;
1246 if (panel->top_file > i)
1247 panel->top_file = i;
1249 if (panel->top_file > panel->selected)
1250 panel->top_file = panel->selected;
1254 /* --------------------------------------------------------------------------------------------- */
1255 /** add "#enc:encodning" to end of path */
1256 /* if path end width a previous #enc:, only encoding is changed no additional
1257 * #enc: is appended
1258 * retun new string
1261 static char *
1262 panel_save_name (WPanel * panel)
1264 /* If the program is shuting down */
1265 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1266 return g_strdup (panel->panel_name);
1267 else
1268 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1271 /* --------------------------------------------------------------------------------------------- */
1273 /* "history_load" event handler */
1274 static gboolean
1275 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1276 gpointer init_data, gpointer data)
1278 WPanel *p = (WPanel *) init_data;
1279 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1281 (void) event_group_name;
1282 (void) event_name;
1284 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1286 char *tmp_path;
1288 tmp_path = vfs_path_to_str (p->cwd_vpath);
1289 if (ev->cfg != NULL)
1290 p->dir_history = history_load (ev->cfg, p->hist_name);
1291 else
1292 p->dir_history = history_get (p->hist_name);
1294 directory_history_add (p, tmp_path);
1295 g_free (tmp_path);
1298 return TRUE;
1301 /* --------------------------------------------------------------------------------------------- */
1303 /* "history_save" event handler */
1304 static gboolean
1305 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1306 gpointer init_data, gpointer data)
1308 WPanel *p = (WPanel *) init_data;
1310 (void) event_group_name;
1311 (void) event_name;
1313 if (p->dir_history != NULL)
1315 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1317 history_save (ev->cfg, p->hist_name, p->dir_history);
1320 return TRUE;
1323 /* --------------------------------------------------------------------------------------------- */
1325 static void
1326 panel_destroy (WPanel * p)
1328 size_t i;
1330 if (panels_options.auto_save_setup)
1332 char *name;
1334 name = panel_save_name (p);
1335 panel_save_setup (p, name);
1336 g_free (name);
1339 panel_clean_dir (p);
1341 /* clean history */
1342 if (p->dir_history != NULL)
1344 /* directory history is already saved before this moment */
1345 p->dir_history = g_list_first (p->dir_history);
1346 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1347 g_list_free (p->dir_history);
1349 g_free (p->hist_name);
1351 delete_format (p->format);
1352 delete_format (p->status_format);
1354 g_free (p->user_format);
1355 for (i = 0; i < LIST_TYPES; i++)
1356 g_free (p->user_status_format[i]);
1358 g_free (p->dir.list);
1359 g_free (p->panel_name);
1361 vfs_path_free (p->lwd_vpath);
1362 vfs_path_free (p->cwd_vpath);
1365 /* --------------------------------------------------------------------------------------------- */
1367 static void
1368 panel_format_modified (WPanel * panel)
1370 panel->format_modified = 1;
1373 /* --------------------------------------------------------------------------------------------- */
1375 static void
1376 panel_paint_sort_info (WPanel * panel)
1378 if (*panel->sort_info.sort_field->hotkey != '\0')
1380 const char *sort_sign =
1381 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1382 char *str;
1384 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1385 widget_move (&panel->widget, 1, 1);
1386 tty_print_string (str);
1387 g_free (str);
1391 /* --------------------------------------------------------------------------------------------- */
1393 static gchar *
1394 panel_get_title_without_hotkey (const char *title)
1396 char *translated_title;
1397 char *hkey;
1399 if (title == NULL)
1400 return NULL;
1401 if (title[0] == '\0')
1402 return g_strdup ("");
1404 translated_title = g_strdup (_(title));
1406 hkey = strchr (translated_title, '&');
1407 if ((hkey != NULL) && (hkey[1] != '\0'))
1408 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1410 return translated_title;
1413 /* --------------------------------------------------------------------------------------------- */
1415 static void
1416 paint_frame (WPanel * panel)
1418 int side, width;
1419 GString *format_txt;
1421 adjust_top_file (panel);
1423 widget_erase (&panel->widget);
1424 show_dir (panel);
1426 widget_move (&panel->widget, 1, 1);
1428 for (side = 0; side <= panel->split; side++)
1430 format_e *format;
1432 if (side)
1434 tty_setcolor (NORMAL_COLOR);
1435 tty_print_one_vline (TRUE);
1436 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1438 else if (panel->split)
1439 width = panel->widget.cols / 2 - 3;
1440 else
1441 width = panel->widget.cols - 2;
1443 format_txt = g_string_new ("");
1444 for (format = panel->format; format; format = format->next)
1446 if (format->string_fn)
1448 g_string_set_size (format_txt, 0);
1450 if (panel->list_type == list_long
1451 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1452 g_string_append (format_txt,
1453 panel->sort_info.reverse
1454 ? panel_sort_down_sign : panel_sort_up_sign);
1456 g_string_append (format_txt, format->title);
1457 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1459 g_string_append (format_txt, " [");
1460 g_string_append (format_txt, panel->filter);
1461 g_string_append (format_txt, "]");
1464 tty_setcolor (HEADER_COLOR);
1465 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1466 J_CENTER_LEFT));
1467 width -= format->field_len;
1469 else
1471 tty_setcolor (NORMAL_COLOR);
1472 tty_print_one_vline (TRUE);
1473 width--;
1476 g_string_free (format_txt, TRUE);
1478 if (width > 0)
1479 tty_draw_hline (-1, -1, ' ', width);
1482 if (panel->list_type != list_long)
1483 panel_paint_sort_info (panel);
1486 /* --------------------------------------------------------------------------------------------- */
1488 static const char *
1489 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1491 panel_display_t frame = frame_half;
1492 format = skip_separators (format);
1494 if (!strncmp (format, "full", 4))
1496 frame = frame_full;
1497 format += 4;
1499 else if (!strncmp (format, "half", 4))
1501 frame = frame_half;
1502 format += 4;
1505 if (!isstatus)
1507 panel->frame_size = frame;
1508 panel->split = 0;
1511 /* Now, the optional column specifier */
1512 format = skip_separators (format);
1514 if (*format == '1' || *format == '2')
1516 if (!isstatus)
1517 panel->split = *format == '2';
1518 format++;
1521 if (!isstatus)
1522 panel_update_cols (&(panel->widget), panel->frame_size);
1524 return skip_separators (format);
1527 /* Format is:
1529 all := panel_format? format
1530 panel_format := [full|half] [1|2]
1531 format := one_format_e
1532 | format , one_format_e
1534 one_format_e := just format.id [opt_size]
1535 just := [<=>]
1536 opt_size := : size [opt_expand]
1537 size := [0-9]+
1538 opt_expand := +
1542 /* --------------------------------------------------------------------------------------------- */
1544 static format_e *
1545 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1546 int *res_total_cols)
1548 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1549 int total_cols = 0; /* Used columns by the format */
1550 int set_justify; /* flag: set justification mode? */
1551 align_crt_t justify = J_LEFT; /* Which mode. */
1552 size_t i;
1554 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1556 *error = 0;
1558 if (i18n_timelength == 0)
1560 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1562 for (i = 0; panel_fields[i].id != NULL; i++)
1563 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1564 panel_fields[i].min_size = i18n_timelength;
1568 * This makes sure that the panel and mini status full/half mode
1569 * setting is equal
1571 format = parse_panel_size (panel, format, isstatus);
1573 while (*format)
1574 { /* format can be an empty string */
1575 int found = 0;
1577 darr = g_new0 (format_e, 1);
1579 /* I'm so ugly, don't look at me :-) */
1580 if (!home)
1581 home = old = darr;
1583 old->next = darr;
1584 darr->next = 0;
1585 old = darr;
1587 format = skip_separators (format);
1589 if (strchr ("<=>", *format))
1591 set_justify = 1;
1592 switch (*format)
1594 case '<':
1595 justify = J_LEFT;
1596 break;
1597 case '=':
1598 justify = J_CENTER;
1599 break;
1600 case '>':
1601 default:
1602 justify = J_RIGHT;
1603 break;
1605 format = skip_separators (format + 1);
1607 else
1608 set_justify = 0;
1610 for (i = 0; panel_fields[i].id != NULL; i++)
1612 size_t klen = strlen (panel_fields[i].id);
1614 if (strncmp (format, panel_fields[i].id, klen) != 0)
1615 continue;
1617 format += klen;
1619 darr->requested_field_len = panel_fields[i].min_size;
1620 darr->string_fn = panel_fields[i].string_fn;
1621 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1623 darr->id = panel_fields[i].id;
1624 darr->expand = panel_fields[i].expands;
1625 darr->just_mode = panel_fields[i].default_just;
1627 if (set_justify)
1629 if (IS_FIT (darr->just_mode))
1630 darr->just_mode = MAKE_FIT (justify);
1631 else
1632 darr->just_mode = justify;
1634 found = 1;
1636 format = skip_separators (format);
1638 /* If we have a size specifier */
1639 if (*format == ':')
1641 int req_length;
1643 /* If the size was specified, we don't want
1644 * auto-expansion by default
1646 darr->expand = 0;
1647 format++;
1648 req_length = atoi (format);
1649 darr->requested_field_len = req_length;
1651 format = skip_numbers (format);
1653 /* Now, if they insist on expansion */
1654 if (*format == '+')
1656 darr->expand = 1;
1657 format++;
1662 break;
1664 if (!found)
1666 char *tmp_format = g_strdup (format);
1668 int pos = min (8, strlen (format));
1669 delete_format (home);
1670 tmp_format[pos] = 0;
1671 *error =
1672 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1673 g_free (tmp_format);
1674 return 0;
1676 total_cols += darr->requested_field_len;
1679 *res_total_cols = total_cols;
1680 return home;
1683 /* --------------------------------------------------------------------------------------------- */
1685 static format_e *
1686 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1688 #define MAX_EXPAND 4
1689 int expand_top = 0; /* Max used element in expand */
1690 int usable_columns; /* Usable columns in the panel */
1691 int total_cols = 0;
1692 int i;
1693 format_e *darr, *home;
1695 if (!format)
1696 format = DEFAULT_USER_FORMAT;
1698 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1700 if (*error)
1701 return 0;
1703 panel->dirty = 1;
1705 /* Status needn't to be split */
1706 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1708 : (panel->split + 1))) - (!isstatus
1709 && panel->split);
1711 /* Look for the expandable fields and set field_len based on the requested field len */
1712 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1714 darr->field_len = darr->requested_field_len;
1715 if (darr->expand)
1716 expand_top++;
1719 /* If we used more columns than the available columns, adjust that */
1720 if (total_cols > usable_columns)
1722 int pdif, dif = total_cols - usable_columns;
1724 while (dif)
1726 pdif = dif;
1727 for (darr = home; darr; darr = darr->next)
1729 if (dif && darr->field_len - 1)
1731 darr->field_len--;
1732 dif--;
1736 /* avoid endless loop if num fields > 40 */
1737 if (pdif == dif)
1738 break;
1740 total_cols = usable_columns; /* give up, the rest should be truncated */
1743 /* Expand the available space */
1744 if ((usable_columns > total_cols) && expand_top)
1746 int spaces = (usable_columns - total_cols) / expand_top;
1747 int extra = (usable_columns - total_cols) % expand_top;
1749 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1750 if (darr->expand)
1752 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1753 i++;
1756 return home;
1759 /* --------------------------------------------------------------------------------------------- */
1760 /** Given the panel->view_type returns the format string to be parsed */
1762 static const char *
1763 panel_format (WPanel * panel)
1765 switch (panel->list_type)
1767 case list_long:
1768 return "full perm space nlink space owner space group space size space mtime space name";
1770 case list_brief:
1771 return "half 2 type name";
1773 case list_user:
1774 return panel->user_format;
1776 default:
1777 case list_full:
1778 return "half type name | size | mtime";
1782 /* --------------------------------------------------------------------------------------------- */
1784 static const char *
1785 mini_status_format (WPanel * panel)
1787 if (panel->user_mini_status)
1788 return panel->user_status_format[panel->list_type];
1790 switch (panel->list_type)
1793 case list_long:
1794 return "full perm space nlink space owner space group space size space mtime space name";
1796 case list_brief:
1797 return "half type name space bsize space perm space";
1799 case list_full:
1800 return "half type name";
1802 default:
1803 case list_user:
1804 return panel->user_format;
1808 /* */
1809 /* Panel operation commands */
1810 /* */
1812 /* --------------------------------------------------------------------------------------------- */
1813 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1815 static cb_ret_t
1816 maybe_cd (int move_up_dir)
1818 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1820 if (move_up_dir)
1822 vfs_path_t *up_dir;
1824 up_dir = vfs_path_from_str ("..");
1825 do_cd (up_dir, cd_exact);
1826 vfs_path_free (up_dir);
1827 return MSG_HANDLED;
1830 if (S_ISDIR (selection (current_panel)->st.st_mode)
1831 || link_isdir (selection (current_panel)))
1833 vfs_path_t *vpath;
1835 vpath = vfs_path_from_str (selection (current_panel)->fname);
1836 do_cd (vpath, cd_exact);
1837 vfs_path_free (vpath);
1838 return MSG_HANDLED;
1841 return MSG_NOT_HANDLED;
1844 /* --------------------------------------------------------------------------------------------- */
1846 /* if command line is empty then do 'cd ..' */
1847 static cb_ret_t
1848 force_maybe_cd (void)
1850 if (cmdline->buffer[0] == '\0')
1852 vfs_path_t *up_dir = vfs_path_from_str ("..");
1853 do_cd (up_dir, cd_exact);
1854 vfs_path_free (up_dir);
1855 return MSG_HANDLED;
1857 return MSG_NOT_HANDLED;
1860 /* --------------------------------------------------------------------------------------------- */
1862 static void
1863 unselect_item (WPanel * panel)
1865 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1868 /* --------------------------------------------------------------------------------------------- */
1870 static void
1871 move_down (WPanel * panel)
1873 if (panel->selected + 1 == panel->count)
1874 return;
1876 unselect_item (panel);
1877 panel->selected++;
1878 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1880 /* Scroll window half screen */
1881 panel->top_file += ITEMS (panel) / 2;
1882 if (panel->top_file > panel->count - ITEMS (panel))
1883 panel->top_file = panel->count - ITEMS (panel);
1884 paint_dir (panel);
1886 select_item (panel);
1889 /* --------------------------------------------------------------------------------------------- */
1891 static void
1892 move_up (WPanel * panel)
1894 if (panel->selected == 0)
1895 return;
1897 unselect_item (panel);
1898 panel->selected--;
1899 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1901 /* Scroll window half screen */
1902 panel->top_file -= ITEMS (panel) / 2;
1903 if (panel->top_file < 0)
1904 panel->top_file = 0;
1905 paint_dir (panel);
1907 select_item (panel);
1910 /* --------------------------------------------------------------------------------------------- */
1911 /** Changes the selection by lines (may be negative) */
1913 static void
1914 move_selection (WPanel * panel, int lines)
1916 int new_pos;
1917 int adjust = 0;
1919 new_pos = panel->selected + lines;
1920 if (new_pos >= panel->count)
1921 new_pos = panel->count - 1;
1923 if (new_pos < 0)
1924 new_pos = 0;
1926 unselect_item (panel);
1927 panel->selected = new_pos;
1929 if (panel->selected - panel->top_file >= ITEMS (panel))
1931 panel->top_file += lines;
1932 adjust = 1;
1935 if (panel->selected - panel->top_file < 0)
1937 panel->top_file += lines;
1938 adjust = 1;
1941 if (adjust)
1943 if (panel->top_file > panel->selected)
1944 panel->top_file = panel->selected;
1945 if (panel->top_file < 0)
1946 panel->top_file = 0;
1947 paint_dir (panel);
1949 select_item (panel);
1952 /* --------------------------------------------------------------------------------------------- */
1954 static cb_ret_t
1955 move_left (WPanel * panel)
1957 if (panel->split)
1959 move_selection (panel, -llines (panel));
1960 return MSG_HANDLED;
1962 else
1963 return maybe_cd (1); /* cd .. */
1966 /* --------------------------------------------------------------------------------------------- */
1968 static cb_ret_t
1969 move_right (WPanel * panel)
1971 if (panel->split)
1973 move_selection (panel, llines (panel));
1974 return MSG_HANDLED;
1976 else
1977 return maybe_cd (0); /* cd (selection) */
1980 /* --------------------------------------------------------------------------------------------- */
1982 static void
1983 prev_page (WPanel * panel)
1985 int items;
1987 if (!panel->selected && !panel->top_file)
1988 return;
1989 unselect_item (panel);
1990 items = ITEMS (panel);
1991 if (panel->top_file < items)
1992 items = panel->top_file;
1993 if (!items)
1994 panel->selected = 0;
1995 else
1996 panel->selected -= items;
1997 panel->top_file -= items;
1999 select_item (panel);
2000 paint_dir (panel);
2003 /* --------------------------------------------------------------------------------------------- */
2005 static void
2006 goto_parent_dir (WPanel * panel)
2008 if (!panel->is_panelized)
2010 vfs_path_t *up_dir;
2012 up_dir = vfs_path_from_str ("..");
2013 do_cd (up_dir, cd_exact);
2014 vfs_path_free (up_dir);
2016 else
2018 char *fname = panel->dir.list[panel->selected].fname;
2019 const char *bname;
2020 vfs_path_t *dname_vpath;
2022 if (g_path_is_absolute (fname))
2023 fname = g_strdup (fname);
2024 else
2026 char *tmp_root;
2028 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2029 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2030 g_free (tmp_root);
2033 bname = x_basename (fname);
2035 if (bname == fname)
2036 dname_vpath = vfs_path_from_str (".");
2037 else
2039 char *dname;
2041 dname = g_strndup (fname, bname - fname);
2042 dname_vpath = vfs_path_from_str (dname);
2043 g_free (dname);
2046 do_cd (dname_vpath, cd_exact);
2047 try_to_select (panel, bname);
2049 vfs_path_free (dname_vpath);
2050 g_free (fname);
2054 /* --------------------------------------------------------------------------------------------- */
2056 static void
2057 next_page (WPanel * panel)
2059 int items;
2061 if (panel->selected == panel->count - 1)
2062 return;
2063 unselect_item (panel);
2064 items = ITEMS (panel);
2065 if (panel->top_file > panel->count - 2 * items)
2066 items = panel->count - items - panel->top_file;
2067 if (panel->top_file + items < 0)
2068 items = -panel->top_file;
2069 if (!items)
2070 panel->selected = panel->count - 1;
2071 else
2072 panel->selected += items;
2073 panel->top_file += items;
2075 select_item (panel);
2076 paint_dir (panel);
2079 /* --------------------------------------------------------------------------------------------- */
2081 static void
2082 goto_child_dir (WPanel * panel)
2084 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2086 vfs_path_t *vpath;
2088 vpath = vfs_path_from_str (selection (panel)->fname);
2089 do_cd (vpath, cd_exact);
2090 vfs_path_free (vpath);
2094 /* --------------------------------------------------------------------------------------------- */
2096 static void
2097 goto_top_file (WPanel * panel)
2099 unselect_item (panel);
2100 panel->selected = panel->top_file;
2101 select_item (panel);
2104 /* --------------------------------------------------------------------------------------------- */
2106 static void
2107 goto_middle_file (WPanel * panel)
2109 unselect_item (panel);
2110 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2111 select_item (panel);
2114 /* --------------------------------------------------------------------------------------------- */
2116 static void
2117 goto_bottom_file (WPanel * panel)
2119 unselect_item (panel);
2120 panel->selected = panel->top_file + ITEMS (panel) - 1;
2121 select_item (panel);
2124 /* --------------------------------------------------------------------------------------------- */
2126 static void
2127 move_home (WPanel * panel)
2129 if (panel->selected == 0)
2130 return;
2132 unselect_item (panel);
2134 if (panels_options.torben_fj_mode)
2136 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2138 if (panel->selected > middle_pos)
2140 goto_middle_file (panel);
2141 return;
2143 if (panel->selected != panel->top_file)
2145 goto_top_file (panel);
2146 return;
2150 panel->top_file = 0;
2151 panel->selected = 0;
2153 paint_dir (panel);
2154 select_item (panel);
2157 /* --------------------------------------------------------------------------------------------- */
2159 static void
2160 move_end (WPanel * panel)
2162 if (panel->selected == panel->count - 1)
2163 return;
2165 unselect_item (panel);
2167 if (panels_options.torben_fj_mode)
2169 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2171 if (panel->selected < middle_pos)
2173 goto_middle_file (panel);
2174 return;
2176 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2178 goto_bottom_file (panel);
2179 return;
2183 panel->selected = panel->count - 1;
2184 paint_dir (panel);
2185 select_item (panel);
2188 /* --------------------------------------------------------------------------------------------- */
2190 static void
2191 do_mark_file (WPanel * panel, mark_act_t do_move)
2193 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2194 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2195 move_down (panel);
2196 else if (do_move == MARK_FORCE_UP)
2197 move_up (panel);
2200 /* --------------------------------------------------------------------------------------------- */
2202 static void
2203 mark_file (WPanel * panel)
2205 do_mark_file (panel, MARK_DOWN);
2208 /* --------------------------------------------------------------------------------------------- */
2210 static void
2211 mark_file_up (WPanel * panel)
2213 do_mark_file (panel, MARK_FORCE_UP);
2216 /* --------------------------------------------------------------------------------------------- */
2218 static void
2219 mark_file_down (WPanel * panel)
2221 do_mark_file (panel, MARK_FORCE_DOWN);
2224 /* --------------------------------------------------------------------------------------------- */
2226 static void
2227 mark_file_right (WPanel * panel)
2229 int lines = llines (panel);
2231 if (state_mark < 0)
2232 state_mark = selection (panel)->f.marked ? 0 : 1;
2234 lines = min (lines, panel->count - panel->selected - 1);
2235 for (; lines != 0; lines--)
2237 do_file_mark (panel, panel->selected, state_mark);
2238 move_down (panel);
2240 do_file_mark (panel, panel->selected, state_mark);
2243 /* --------------------------------------------------------------------------------------------- */
2245 static void
2246 mark_file_left (WPanel * panel)
2248 int lines = llines (panel);
2250 if (state_mark < 0)
2251 state_mark = selection (panel)->f.marked ? 0 : 1;
2253 lines = min (lines, panel->selected + 1);
2254 for (; lines != 0; lines--)
2256 do_file_mark (panel, panel->selected, state_mark);
2257 move_up (panel);
2259 do_file_mark (panel, panel->selected, state_mark);
2262 /* --------------------------------------------------------------------------------------------- */
2263 /** Incremental search of a file name in the panel.
2264 * @param panel instance of WPanel structure
2265 * @param c_code key code
2268 static void
2269 do_search (WPanel * panel, int c_code)
2271 size_t l;
2272 int i, sel;
2273 gboolean wrapped = FALSE;
2274 char *act;
2275 mc_search_t *search;
2276 char *reg_exp, *esc_str;
2277 gboolean is_found = FALSE;
2279 l = strlen (panel->search_buffer);
2280 if (c_code == KEY_BACKSPACE)
2282 if (l != 0)
2284 act = panel->search_buffer + l;
2285 str_prev_noncomb_char (&act, panel->search_buffer);
2286 act[0] = '\0';
2288 panel->search_chpoint = 0;
2290 else
2292 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2294 panel->search_char[panel->search_chpoint] = c_code;
2295 panel->search_chpoint++;
2298 if (panel->search_chpoint > 0)
2300 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2302 case -2:
2303 return;
2304 case -1:
2305 panel->search_chpoint = 0;
2306 return;
2307 default:
2308 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2310 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2311 l += panel->search_chpoint;
2312 *(panel->search_buffer + l) = '\0';
2313 panel->search_chpoint = 0;
2319 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2320 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2321 search = mc_search_new (esc_str, -1);
2322 search->search_type = MC_SEARCH_T_GLOB;
2323 search->is_entire_line = TRUE;
2324 switch (panels_options.qsearch_mode)
2326 case QSEARCH_CASE_SENSITIVE:
2327 search->is_case_sensitive = TRUE;
2328 break;
2329 case QSEARCH_CASE_INSENSITIVE:
2330 search->is_case_sensitive = FALSE;
2331 break;
2332 default:
2333 search->is_case_sensitive = panel->sort_info.case_sensitive;
2334 break;
2336 sel = panel->selected;
2337 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2339 if (i >= panel->count)
2341 i = 0;
2342 if (wrapped)
2343 break;
2344 wrapped = TRUE;
2346 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2348 sel = i;
2349 is_found = TRUE;
2350 break;
2353 if (is_found)
2355 unselect_item (panel);
2356 panel->selected = sel;
2357 select_item (panel);
2358 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2360 else if (c_code != KEY_BACKSPACE)
2362 act = panel->search_buffer + l;
2363 str_prev_noncomb_char (&act, panel->search_buffer);
2364 act[0] = '\0';
2366 mc_search_free (search);
2367 g_free (reg_exp);
2368 g_free (esc_str);
2371 /* --------------------------------------------------------------------------------------------- */
2372 /** Start new search.
2373 * @param panel instance of WPanel structure
2376 static void
2377 start_search (WPanel * panel)
2379 if (panel->searching)
2381 if (panel->selected + 1 == panel->count)
2382 panel->selected = 0;
2383 else
2384 move_down (panel);
2386 /* in case if there was no search string we need to recall
2387 previous string, with which we ended previous searching */
2388 if (panel->search_buffer[0] == '\0')
2389 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2390 sizeof (panel->search_buffer));
2392 do_search (panel, 0);
2394 else
2396 panel->searching = TRUE;
2397 panel->search_buffer[0] = '\0';
2398 panel->search_char[0] = '\0';
2399 panel->search_chpoint = 0;
2400 display_mini_info (panel);
2401 mc_refresh ();
2405 /* --------------------------------------------------------------------------------------------- */
2407 static void
2408 stop_search (WPanel * panel)
2410 panel->searching = FALSE;
2412 /* if user had overrdied search string, we need to store it
2413 to the previous_search_buffer */
2414 if (panel->search_buffer[0] != '\0')
2415 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2416 sizeof (panel->prev_search_buffer));
2418 display_mini_info (panel);
2421 /* --------------------------------------------------------------------------------------------- */
2422 /** Return 1 if the Enter key has been processed, 0 otherwise */
2424 static int
2425 do_enter_on_file_entry (file_entry * fe)
2427 vfs_path_t *full_name_vpath;
2428 gboolean ok;
2431 * Directory or link to directory - change directory.
2432 * Try the same for the entries on which mc_lstat() has failed.
2434 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2436 vfs_path_t *fname_vpath;
2438 fname_vpath = vfs_path_from_str (fe->fname);
2439 if (!do_cd (fname_vpath, cd_exact))
2440 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2441 vfs_path_free (fname_vpath);
2442 return 1;
2445 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2447 /* Try associated command */
2448 if (regex_command (full_name_vpath, "Open", NULL) != 0)
2450 vfs_path_free (full_name_vpath);
2451 return 1;
2454 /* Check if the file is executable */
2455 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2456 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2457 vfs_path_free (full_name_vpath);
2458 if (!ok)
2459 return 0;
2461 if (confirm_execute)
2463 if (query_dialog
2464 (_("The Midnight Commander"),
2465 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2466 return 1;
2469 if (!vfs_current_is_local ())
2471 int ret;
2472 vfs_path_t *tmp_vpath;
2474 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2475 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2476 vfs_path_free (tmp_vpath);
2477 /* We took action only if the dialog was shown or the execution
2478 * was successful */
2479 return confirm_execute || (ret == 0);
2483 char *tmp = name_quote (fe->fname, 0);
2484 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2485 g_free (tmp);
2486 shell_execute (cmd, 0);
2487 g_free (cmd);
2490 #ifdef HAVE_CHARSET
2491 mc_global.source_codepage = default_source_codepage;
2492 #endif
2494 return 1;
2497 /* --------------------------------------------------------------------------------------------- */
2499 static int
2500 do_enter (WPanel * panel)
2502 return do_enter_on_file_entry (selection (panel));
2505 /* --------------------------------------------------------------------------------------------- */
2507 static void
2508 chdir_other_panel (WPanel * panel)
2510 const file_entry *entry = &panel->dir.list[panel->selected];
2512 vfs_path_t *new_dir_vpath;
2513 char *sel_entry = NULL;
2515 if (get_other_type () != view_listing)
2517 set_display_type (get_other_index (), view_listing);
2520 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2521 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2522 else
2524 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2525 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2528 change_panel ();
2529 do_cd (new_dir_vpath, cd_exact);
2530 vfs_path_free (new_dir_vpath);
2532 if (sel_entry)
2533 try_to_select (current_panel, sel_entry);
2534 change_panel ();
2536 move_down (panel);
2539 /* --------------------------------------------------------------------------------------------- */
2541 * Make the current directory of the current panel also the current
2542 * directory of the other panel. Put the other panel to the listing
2543 * mode if needed. If the current panel is panelized, the other panel
2544 * doesn't become panelized.
2547 static void
2548 panel_sync_other (const WPanel * panel)
2550 if (get_other_type () != view_listing)
2552 set_display_type (get_other_index (), view_listing);
2555 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2557 /* try to select current filename on the other panel */
2558 if (!panel->is_panelized)
2560 try_to_select (other_panel, selection (panel)->fname);
2564 /* --------------------------------------------------------------------------------------------- */
2566 static void
2567 chdir_to_readlink (WPanel * panel)
2569 vfs_path_t *new_dir_vpath;
2570 char buffer[MC_MAXPATHLEN], *p;
2571 int i;
2572 struct stat st;
2573 vfs_path_t *panel_fname_vpath;
2574 gboolean ok;
2576 if (get_other_type () != view_listing)
2577 return;
2579 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2580 return;
2582 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2583 if (i < 0)
2584 return;
2586 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2587 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2588 vfs_path_free (panel_fname_vpath);
2589 if (!ok)
2590 return;
2592 buffer[i] = 0;
2593 if (!S_ISDIR (st.st_mode))
2595 p = strrchr (buffer, PATH_SEP);
2596 if (p && !p[1])
2598 *p = 0;
2599 p = strrchr (buffer, PATH_SEP);
2601 if (!p)
2602 return;
2603 p[1] = 0;
2605 if (*buffer == PATH_SEP)
2606 new_dir_vpath = vfs_path_from_str (buffer);
2607 else
2608 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2610 change_panel ();
2611 do_cd (new_dir_vpath, cd_exact);
2612 vfs_path_free (new_dir_vpath);
2613 change_panel ();
2615 move_down (panel);
2618 /* --------------------------------------------------------------------------------------------- */
2620 static gsize
2621 panel_get_format_field_count (WPanel * panel)
2623 format_e *format;
2624 gsize lc_index;
2625 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2626 return lc_index;
2629 /* --------------------------------------------------------------------------------------------- */
2631 function return 0 if not found and REAL_INDEX+1 if found
2634 static gsize
2635 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2637 format_e *format;
2638 gsize lc_index;
2640 for (lc_index = 1, format = panel->format;
2641 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2642 if (format == NULL)
2643 lc_index = 0;
2645 return lc_index;
2648 /* --------------------------------------------------------------------------------------------- */
2650 static format_e *
2651 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2653 format_e *format;
2654 for (format = panel->format;
2655 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2656 return format;
2659 /* --------------------------------------------------------------------------------------------- */
2661 static const panel_field_t *
2662 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2664 const panel_field_t *pfield;
2665 format_e *format;
2667 format = panel_get_format_field_by_index (panel, lc_index);
2668 if (format == NULL)
2669 return NULL;
2670 pfield = panel_get_field_by_title (format->title);
2671 if (pfield == NULL)
2672 return NULL;
2673 if (pfield->sort_routine == NULL)
2674 return NULL;
2675 return pfield;
2678 /* --------------------------------------------------------------------------------------------- */
2680 static void
2681 panel_toggle_sort_order_prev (WPanel * panel)
2683 gsize lc_index, i;
2684 gchar *title;
2686 const panel_field_t *pfield = NULL;
2688 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2689 lc_index = panel_get_format_field_index_by_name (panel, title);
2690 g_free (title);
2692 if (lc_index > 1)
2694 /* search for prev sortable column in panel format */
2695 for (i = lc_index - 1;
2696 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2699 if (pfield == NULL)
2701 /* Sortable field not found. Try to search in each array */
2702 for (i = panel_get_format_field_count (panel);
2703 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2706 if (pfield != NULL)
2708 panel->sort_info.sort_field = pfield;
2709 panel_set_sort_order (panel, pfield);
2713 /* --------------------------------------------------------------------------------------------- */
2715 static void
2716 panel_toggle_sort_order_next (WPanel * panel)
2718 gsize lc_index, i;
2719 const panel_field_t *pfield = NULL;
2720 gsize format_field_count;
2721 gchar *title;
2723 format_field_count = panel_get_format_field_count (panel);
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 != 0 && lc_index != format_field_count)
2730 /* search for prev sortable column in panel format */
2731 for (i = lc_index;
2732 i != format_field_count
2733 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2736 if (pfield == NULL)
2738 /* Sortable field not found. Try to search in each array */
2739 for (i = 0;
2740 i != format_field_count
2741 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2744 if (pfield != NULL)
2746 panel->sort_info.sort_field = pfield;
2747 panel_set_sort_order (panel, pfield);
2751 /* --------------------------------------------------------------------------------------------- */
2753 static void
2754 panel_select_sort_order (WPanel * panel)
2756 const panel_field_t *sort_order;
2758 sort_order = sort_box (&panel->sort_info);
2759 if (sort_order != NULL)
2761 panel->sort_info.sort_field = sort_order;
2762 panel_set_sort_order (panel, sort_order);
2766 /* --------------------------------------------------------------------------------------------- */
2768 static void
2769 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2771 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2773 const panel_field_t *sort_order;
2775 sort_order = panel_get_field_by_id (name);
2776 if (sort_order == NULL)
2777 return;
2778 panel->sort_info.sort_field = sort_order;
2780 else
2781 panel->sort_info.reverse = !panel->sort_info.reverse;
2783 panel_set_sort_order (panel, panel->sort_info.sort_field);
2786 /* --------------------------------------------------------------------------------------------- */
2788 * If we moved to the parent directory move the selection pointer to
2789 * the old directory name; If we leave VFS dir, remove FS specificator.
2791 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2794 static const char *
2795 get_parent_dir_name (const char *cwd, const char *lwd)
2797 size_t llen, clen;
2798 const char *p;
2800 llen = strlen (lwd);
2801 clen = strlen (cwd);
2803 if (llen <= clen)
2804 return NULL;
2806 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2808 if (p == NULL)
2810 p = strrchr (lwd, PATH_SEP);
2812 if ((p != NULL)
2813 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2814 && (clen == (size_t) (p - lwd)
2815 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2816 return (p + 1);
2818 return NULL;
2821 /* skip VFS prefix */
2822 while (--p > lwd && *p != PATH_SEP)
2824 /* get last component */
2825 while (--p > lwd && *p != PATH_SEP)
2828 /* return last component */
2829 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
2832 /* --------------------------------------------------------------------------------------------- */
2833 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2835 static void
2836 subshell_chdir (const vfs_path_t * vpath)
2838 #ifdef HAVE_SUBSHELL_SUPPORT
2839 if (mc_global.tty.use_subshell && vfs_current_is_local ())
2840 do_subshell_chdir (vpath, FALSE, TRUE);
2841 #endif /* HAVE_SUBSHELL_SUPPORT */
2844 /* --------------------------------------------------------------------------------------------- */
2846 * Changes the current directory of the panel.
2847 * Don't record change in the directory history.
2850 static gboolean
2851 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
2853 char *olddir;
2855 olddir = vfs_path_to_str (panel->cwd_vpath);
2857 /* Convert *new_path to a suitable pathname, handle ~user */
2858 if (cd_type == cd_parse_command)
2860 const vfs_path_element_t *element;
2862 element = vfs_path_get_by_index (new_dir_vpath, 0);
2863 if (strcmp (element->path, "-") == 0)
2864 new_dir_vpath = panel->lwd_vpath;
2867 if (mc_chdir (new_dir_vpath) == -1)
2869 panel_set_cwd (panel, olddir);
2870 g_free (olddir);
2871 return FALSE;
2874 /* Success: save previous directory, shutdown status of previous dir */
2875 panel_set_lwd (panel, olddir);
2876 input_free_completions (cmdline);
2878 vfs_path_free (panel->cwd_vpath);
2879 vfs_setup_cwd ();
2880 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
2882 vfs_release_path (olddir);
2884 subshell_chdir (panel->cwd_vpath);
2886 /* Reload current panel */
2887 panel_clean_dir (panel);
2889 char *tmp_path;
2891 tmp_path = vfs_path_to_str (panel->cwd_vpath);
2892 panel->count =
2893 do_load_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
2894 panel->sort_info.reverse, panel->sort_info.case_sensitive,
2895 panel->sort_info.exec_first, panel->filter);
2896 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
2897 g_free (tmp_path);
2899 load_hint (0);
2900 panel->dirty = 1;
2901 update_xterm_title_path ();
2903 g_free (olddir);
2905 return TRUE;
2908 /* --------------------------------------------------------------------------------------------- */
2910 static void
2911 directory_history_next (WPanel * panel)
2913 GList *nextdir;
2915 nextdir = g_list_next (panel->dir_history);
2916 if (nextdir != NULL)
2918 vfs_path_t *data_vpath;
2920 data_vpath = vfs_path_from_str ((char *) nextdir->data);
2921 if (_do_panel_cd (panel, data_vpath, cd_exact))
2922 panel->dir_history = nextdir;
2923 vfs_path_free (data_vpath);
2927 /* --------------------------------------------------------------------------------------------- */
2929 static void
2930 directory_history_prev (WPanel * panel)
2932 GList *prevdir;
2934 prevdir = g_list_previous (panel->dir_history);
2936 if (prevdir != NULL)
2938 vfs_path_t *data_vpath;
2940 data_vpath = vfs_path_from_str ((char *) prevdir->data);
2941 if (_do_panel_cd (panel, data_vpath, cd_exact))
2942 panel->dir_history = prevdir;
2943 vfs_path_free (data_vpath);
2947 /* --------------------------------------------------------------------------------------------- */
2949 static void
2950 directory_history_list (WPanel * panel)
2952 char *s;
2954 s = history_show (&panel->dir_history, &panel->widget);
2956 if (s != NULL)
2958 vfs_path_t *s_vpath;
2960 s_vpath = vfs_path_from_str (s);
2961 if (_do_panel_cd (panel, s_vpath, cd_exact))
2963 char *tmp_path;
2965 tmp_path = vfs_path_to_str (panel->cwd_vpath);
2966 directory_history_add (panel, tmp_path);
2967 g_free (tmp_path);
2969 else
2970 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2971 vfs_path_free (s_vpath);
2972 g_free (s);
2976 /* --------------------------------------------------------------------------------------------- */
2978 static cb_ret_t
2979 panel_execute_cmd (WPanel * panel, unsigned long command)
2981 int res = MSG_HANDLED;
2983 if (command != CK_Search)
2984 stop_search (panel);
2987 switch (command)
2989 case CK_Up:
2990 case CK_Down:
2991 case CK_Left:
2992 case CK_Right:
2993 case CK_Bottom:
2994 case CK_Top:
2995 case CK_PageDown:
2996 case CK_PageUp:
2997 /* reset state of marks flag */
2998 state_mark = -1;
2999 break;
3001 switch (command)
3003 case CK_PanelOtherCd:
3004 chdir_other_panel (panel);
3005 break;
3006 case CK_PanelOtherCdLink:
3007 chdir_to_readlink (panel);
3008 break;
3009 case CK_CopySingle:
3010 copy_cmd_local ();
3011 break;
3012 case CK_DeleteSingle:
3013 delete_cmd_local ();
3014 break;
3015 case CK_Enter:
3016 do_enter (panel);
3017 break;
3018 case CK_ViewRaw:
3019 view_raw_cmd ();
3020 break;
3021 case CK_EditNew:
3022 edit_cmd_new ();
3023 break;
3024 case CK_MoveSingle:
3025 rename_cmd_local ();
3026 break;
3027 case CK_SelectInvert:
3028 select_invert_cmd ();
3029 break;
3030 case CK_Select:
3031 select_cmd ();
3032 break;
3033 case CK_Unselect:
3034 unselect_cmd ();
3035 break;
3036 case CK_PageDown:
3037 next_page (panel);
3038 break;
3039 case CK_PageUp:
3040 prev_page (panel);
3041 break;
3042 case CK_CdChild:
3043 goto_child_dir (panel);
3044 break;
3045 case CK_CdParent:
3046 goto_parent_dir (panel);
3047 break;
3048 case CK_History:
3049 directory_history_list (panel);
3050 break;
3051 case CK_HistoryNext:
3052 directory_history_next (panel);
3053 break;
3054 case CK_HistoryPrev:
3055 directory_history_prev (panel);
3056 break;
3057 case CK_BottomOnScreen:
3058 goto_bottom_file (panel);
3059 break;
3060 case CK_MiddleOnScreen:
3061 goto_middle_file (panel);
3062 break;
3063 case CK_TopOnScreen:
3064 goto_top_file (panel);
3065 break;
3066 case CK_Mark:
3067 mark_file (panel);
3068 break;
3069 case CK_MarkUp:
3070 mark_file_up (panel);
3071 break;
3072 case CK_MarkDown:
3073 mark_file_down (panel);
3074 break;
3075 case CK_MarkLeft:
3076 mark_file_left (panel);
3077 break;
3078 case CK_MarkRight:
3079 mark_file_right (panel);
3080 break;
3081 case CK_CdParentSmart:
3082 res = force_maybe_cd ();
3083 break;
3084 case CK_Up:
3085 move_up (panel);
3086 break;
3087 case CK_Down:
3088 move_down (panel);
3089 break;
3090 case CK_Left:
3091 res = move_left (panel);
3092 break;
3093 case CK_Right:
3094 res = move_right (panel);
3095 break;
3096 case CK_Bottom:
3097 move_end (panel);
3098 break;
3099 case CK_Top:
3100 move_home (panel);
3101 break;
3102 #ifdef HAVE_CHARSET
3103 case CK_SelectCodepage:
3104 panel_change_encoding (panel);
3105 break;
3106 #endif
3107 case CK_Search:
3108 start_search (panel);
3109 break;
3110 case CK_SearchStop:
3111 break;
3112 case CK_PanelOtherSync:
3113 panel_sync_other (panel);
3114 break;
3115 case CK_Sort:
3116 panel_select_sort_order (panel);
3117 break;
3118 case CK_SortPrev:
3119 panel_toggle_sort_order_prev (panel);
3120 break;
3121 case CK_SortNext:
3122 panel_toggle_sort_order_next (panel);
3123 break;
3124 case CK_SortReverse:
3125 panel->sort_info.reverse = !panel->sort_info.reverse;
3126 panel_set_sort_order (panel, panel->sort_info.sort_field);
3127 break;
3128 case CK_SortByName:
3129 panel_set_sort_type_by_id (panel, "name");
3130 break;
3131 case CK_SortByExt:
3132 panel_set_sort_type_by_id (panel, "extension");
3133 break;
3134 case CK_SortBySize:
3135 panel_set_sort_type_by_id (panel, "size");
3136 break;
3137 case CK_SortByMTime:
3138 panel_set_sort_type_by_id (panel, "mtime");
3139 break;
3141 return res;
3144 /* --------------------------------------------------------------------------------------------- */
3146 static cb_ret_t
3147 panel_key (WPanel * panel, int key)
3149 size_t i;
3151 if (is_abort_char (key))
3153 stop_search (panel);
3154 return MSG_HANDLED;
3157 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3159 do_search (panel, key);
3160 return MSG_HANDLED;
3163 for (i = 0; panel_map[i].key != 0; i++)
3164 if (key == panel_map[i].key)
3165 return panel_execute_cmd (panel, panel_map[i].command);
3167 if (panels_options.torben_fj_mode && key == ALT ('h'))
3169 goto_middle_file (panel);
3170 return MSG_HANDLED;
3173 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3175 start_search (panel);
3176 do_search (panel, key);
3177 return MSG_HANDLED;
3180 return MSG_NOT_HANDLED;
3183 /* --------------------------------------------------------------------------------------------- */
3185 static cb_ret_t
3186 panel_callback (Widget * w, widget_msg_t msg, int parm)
3188 WPanel *panel = (WPanel *) w;
3189 WButtonBar *bb;
3191 switch (msg)
3193 case WIDGET_INIT:
3194 /* subscribe to "history_load" event */
3195 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3196 /* subscribe to "history_save" event */
3197 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3198 return MSG_HANDLED;
3200 case WIDGET_DRAW:
3201 /* Repaint everything, including frame and separator */
3202 paint_frame (panel); /* including show_dir */
3203 paint_dir (panel);
3204 mini_info_separator (panel);
3205 display_mini_info (panel);
3206 panel->dirty = 0;
3207 return MSG_HANDLED;
3209 case WIDGET_FOCUS:
3210 state_mark = -1;
3211 current_panel = panel;
3212 panel->active = 1;
3213 if (mc_chdir (panel->cwd_vpath) != 0)
3215 char *cwd;
3217 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3218 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3219 cwd, unix_error_string (errno));
3220 g_free (cwd);
3222 else
3223 subshell_chdir (panel->cwd_vpath);
3225 update_xterm_title_path ();
3226 select_item (panel);
3227 show_dir (panel);
3228 paint_dir (panel);
3229 panel->dirty = 0;
3231 bb = find_buttonbar (panel->widget.owner);
3232 midnight_set_buttonbar (bb);
3233 buttonbar_redraw (bb);
3234 return MSG_HANDLED;
3236 case WIDGET_UNFOCUS:
3237 /* Janne: look at this for the multiple panel options */
3238 stop_search (panel);
3239 panel->active = 0;
3240 show_dir (panel);
3241 unselect_item (panel);
3242 return MSG_HANDLED;
3244 case WIDGET_KEY:
3245 return panel_key (panel, parm);
3247 case WIDGET_COMMAND:
3248 return panel_execute_cmd (panel, parm);
3250 case WIDGET_DESTROY:
3251 /* unsubscribe from "history_load" event */
3252 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3253 /* unsubscribe from "history_save" event */
3254 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3255 panel_destroy (panel);
3256 free_my_statfs ();
3257 return MSG_HANDLED;
3259 default:
3260 return default_proc (msg, parm);
3264 /* --------------------------------------------------------------------------------------------- */
3265 /* */
3266 /* Panel mouse events support routines */
3267 /* */
3269 static void
3270 mouse_toggle_mark (WPanel * panel)
3272 do_mark_file (panel, MARK_DONT_MOVE);
3273 mouse_marking = selection (panel)->f.marked;
3274 mouse_mark_panel = current_panel;
3277 /* --------------------------------------------------------------------------------------------- */
3279 static void
3280 mouse_set_mark (WPanel * panel)
3283 if (mouse_mark_panel == panel)
3285 if (mouse_marking && !(selection (panel)->f.marked))
3286 do_mark_file (panel, MARK_DONT_MOVE);
3287 else if (!mouse_marking && (selection (panel)->f.marked))
3288 do_mark_file (panel, MARK_DONT_MOVE);
3292 /* --------------------------------------------------------------------------------------------- */
3294 static int
3295 mark_if_marking (WPanel * panel, Gpm_Event * event)
3297 if (event->buttons & GPM_B_RIGHT)
3299 if (event->type & GPM_DOWN)
3300 mouse_toggle_mark (panel);
3301 else
3302 mouse_set_mark (panel);
3303 return 1;
3305 return 0;
3308 /* --------------------------------------------------------------------------------------------- */
3309 /** Determine which column was clicked, and sort the panel on
3310 * that column, or reverse sort on that column if already
3311 * sorted on that column.
3314 static void
3315 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3317 int i;
3318 const char *lc_sort_name = NULL;
3319 panel_field_t *col_sort_format = NULL;
3320 format_e *format;
3321 gchar *title;
3323 for (i = 0, format = panel->format; format != NULL; format = format->next)
3325 i += format->field_len;
3326 if (event->x < i + 1)
3328 /* found column */
3329 lc_sort_name = format->title;
3330 break;
3334 if (lc_sort_name == NULL)
3335 return;
3337 for (i = 0; panel_fields[i].id != NULL; i++)
3339 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3340 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3342 col_sort_format = &panel_fields[i];
3343 g_free (title);
3344 break;
3346 g_free (title);
3349 if (col_sort_format == NULL)
3350 return;
3352 if (panel->sort_info.sort_field == col_sort_format)
3354 /* reverse the sort if clicked column is already the sorted column */
3355 panel->sort_info.reverse = !panel->sort_info.reverse;
3357 else
3359 /* new sort is forced to be ascending */
3360 panel->sort_info.reverse = FALSE;
3362 panel_set_sort_order (panel, col_sort_format);
3366 /* --------------------------------------------------------------------------------------------- */
3368 * Mouse callback of the panel minus repainting.
3369 * If the event is redirected to the menu, *redir is set to TRUE.
3371 static int
3372 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3374 const int lines = llines (panel);
3375 const gboolean is_active = dlg_widget_active (panel);
3376 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3378 *redir = FALSE;
3380 /* 1st line */
3381 if (mouse_down && event->y == 1)
3383 /* "<" button */
3384 if (event->x == 2)
3386 directory_history_prev (panel);
3387 return MOU_NORMAL;
3390 /* "." button show/hide hidden files */
3391 if (event->x == panel->widget.cols - 5)
3393 panel->widget.owner->callback (panel->widget.owner, NULL,
3394 DLG_ACTION, CK_ShowHidden, NULL);
3395 repaint_screen ();
3396 return MOU_NORMAL;
3399 /* ">" button */
3400 if (event->x == panel->widget.cols - 1)
3402 directory_history_next (panel);
3403 return MOU_NORMAL;
3406 /* "^" button */
3407 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3409 directory_history_list (panel);
3410 return MOU_NORMAL;
3413 /* rest of the upper frame, the menu is invisible - call menu */
3414 if (!menubar_visible)
3416 *redir = TRUE;
3417 event->x += panel->widget.x;
3418 return the_menubar->widget.mouse (event, the_menubar);
3421 /* no other events on 1st line */
3422 return MOU_NORMAL;
3425 /* sort on clicked column; don't handle wheel events */
3426 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3428 mouse_sort_col (event, panel);
3429 return MOU_NORMAL;
3432 /* Mouse wheel events */
3433 if (mouse_down && (event->buttons & GPM_B_UP))
3435 if (is_active)
3437 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3438 prev_page (panel);
3439 else /* We are in first page */
3440 move_up (panel);
3442 return MOU_NORMAL;
3445 if (mouse_down && (event->buttons & GPM_B_DOWN))
3447 if (is_active)
3449 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3450 next_page (panel);
3451 else /* We are in last page */
3452 move_down (panel);
3454 return MOU_NORMAL;
3457 event->y -= 2;
3458 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3460 int my_index;
3462 if (!is_active)
3463 change_panel ();
3465 if (panel->top_file + event->y > panel->count)
3466 my_index = panel->count - 1;
3467 else
3469 my_index = panel->top_file + event->y - 1;
3470 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3471 my_index += llines (panel);
3473 if (my_index >= panel->count)
3474 my_index = panel->count - 1;
3477 if (my_index != panel->selected)
3479 unselect_item (panel);
3480 panel->selected = my_index;
3481 select_item (panel);
3484 /* This one is new */
3485 mark_if_marking (panel, event);
3487 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3489 if (event->y > 0 && event->y <= lines)
3490 do_enter (panel);
3492 return MOU_NORMAL;
3495 /* --------------------------------------------------------------------------------------------- */
3496 /** Mouse callback of the panel */
3498 static int
3499 panel_event (Gpm_Event * event, void *data)
3501 WPanel *panel = data;
3502 int ret;
3503 gboolean redir;
3505 ret = do_panel_event (event, panel, &redir);
3506 if (!redir)
3507 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3509 return ret;
3512 /* --------------------------------------------------------------------------------------------- */
3514 static void
3515 reload_panelized (WPanel * panel)
3517 int i, j;
3518 dir_list *list = &panel->dir;
3520 if (panel != current_panel)
3521 (void) mc_chdir (panel->cwd_vpath);
3523 for (i = 0, j = 0; i < panel->count; i++)
3525 vfs_path_t *vpath;
3527 if (list->list[i].f.marked)
3529 /* Unmark the file in advance. In case the following mc_lstat
3530 * fails we are done, else we have to mark the file again
3531 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3532 * IMO that's the best way to update the panel's summary status
3533 * -- Norbert
3535 do_file_mark (panel, i, 0);
3537 vpath = vfs_path_from_str (list->list[i].fname);
3538 if (mc_lstat (vpath, &list->list[i].st))
3539 g_free (list->list[i].fname);
3540 else
3542 if (list->list[i].f.marked)
3543 do_file_mark (panel, i, 1);
3544 if (j != i)
3545 list->list[j] = list->list[i];
3546 j++;
3548 vfs_path_free (vpath);
3550 if (j == 0)
3551 panel->count = set_zero_dir (list) ? 1 : 0;
3552 else
3553 panel->count = j;
3555 if (panel != current_panel)
3556 (void) mc_chdir (current_panel->cwd_vpath);
3559 /* --------------------------------------------------------------------------------------------- */
3561 static void
3562 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3564 gboolean free_pointer;
3565 char *my_current_file = NULL;
3567 if ((flags & UP_RELOAD) != 0)
3569 panel->is_panelized = FALSE;
3570 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3571 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3574 /* If current_file == -1 (an invalid pointer) then preserve selection */
3575 free_pointer = current_file == UP_KEEPSEL;
3577 if (free_pointer)
3579 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3580 current_file = my_current_file;
3583 if (panel->is_panelized)
3584 reload_panelized (panel);
3585 else
3586 panel_reload (panel);
3588 try_to_select (panel, current_file);
3589 panel->dirty = 1;
3591 if (free_pointer)
3592 g_free (my_current_file);
3595 /* --------------------------------------------------------------------------------------------- */
3597 static void
3598 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3600 if (get_display_type (which) == view_listing)
3602 WPanel *panel;
3604 panel = (WPanel *) get_panel_widget (which);
3605 if (panel->is_panelized)
3606 flags &= ~UP_RELOAD;
3607 update_one_panel_widget (panel, flags, current_file);
3611 /* --------------------------------------------------------------------------------------------- */
3612 /*** public functions ****************************************************************************/
3613 /* --------------------------------------------------------------------------------------------- */
3615 char *
3616 remove_encoding_from_path (const vfs_path_t * vpath)
3618 GString *ret;
3619 GString *tmp_conv;
3620 int indx;
3622 ret = g_string_new ("");
3623 tmp_conv = g_string_new ("");
3625 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
3627 const vfs_path_element_t *path_element;
3628 GIConv converter;
3630 path_element = vfs_path_get_by_index (vpath, indx);
3631 converter =
3632 path_element->encoding != NULL ?
3633 str_crt_conv_to (path_element->encoding) : str_cnv_to_term;
3634 if (converter == INVALID_CONV)
3635 converter = str_cnv_to_term;
3637 str_vfs_convert_from (converter, path_element->path, tmp_conv);
3638 g_string_append (ret, tmp_conv->str);
3639 g_string_set_size (tmp_conv, 0);
3640 str_close_conv (converter);
3642 g_string_free (tmp_conv, TRUE);
3643 return g_string_free (ret, FALSE);
3646 /* --------------------------------------------------------------------------------------------- */
3648 static void
3649 do_select (WPanel * panel, int i)
3651 if (i != panel->selected)
3653 panel->dirty = 1;
3654 panel->selected = i;
3655 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3656 if (panel->top_file < 0)
3657 panel->top_file = 0;
3661 /* --------------------------------------------------------------------------------------------- */
3663 static void
3664 do_try_to_select (WPanel * panel, const char *name)
3666 int i;
3667 char *subdir;
3669 if (!name)
3671 do_select (panel, 0);
3672 return;
3675 /* We only want the last component of the directory,
3676 * and from this only the name without suffix. */
3677 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3679 /* Search that subdirectory, if found select it */
3680 for (i = 0; i < panel->count; i++)
3682 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3684 do_select (panel, i);
3685 g_free (subdir);
3686 return;
3690 /* Try to select a file near the file that is missing */
3691 if (panel->selected >= panel->count)
3692 do_select (panel, panel->count - 1);
3693 g_free (subdir);
3696 /* --------------------------------------------------------------------------------------------- */
3698 /* event callback */
3699 static gboolean
3700 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3701 gpointer init_data, gpointer data)
3703 (void) event_group_name;
3704 (void) event_name;
3705 (void) init_data;
3706 (void) data;
3708 update_panels (UP_RELOAD, UP_KEEPSEL);
3710 return TRUE;
3713 /* --------------------------------------------------------------------------------------------- */
3715 /* event callback */
3716 static gboolean
3717 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3718 gpointer init_data, gpointer data)
3720 (void) event_group_name;
3721 (void) event_name;
3722 (void) init_data;
3723 (void) data;
3725 if (current_panel->marked == 0)
3726 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3727 (gpointer) selection (current_panel)->fname);
3728 else
3730 int i;
3731 gboolean first = TRUE;
3732 char *flist = NULL;
3734 for (i = 0; i < current_panel->count; i++)
3735 if (current_panel->dir.list[i].f.marked != 0)
3736 { /* Skip the unmarked ones */
3737 if (first)
3739 flist = g_strdup (current_panel->dir.list[i].fname);
3740 first = FALSE;
3742 else
3744 /* Add empty lines after the file */
3745 char *tmp;
3747 tmp =
3748 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3749 g_free (flist);
3750 flist = tmp;
3754 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3755 g_free (flist);
3757 return TRUE;
3760 /* --------------------------------------------------------------------------------------------- */
3761 /*** public functions ****************************************************************************/
3762 /* --------------------------------------------------------------------------------------------- */
3764 void
3765 try_to_select (WPanel * panel, const char *name)
3767 do_try_to_select (panel, name);
3768 select_item (panel);
3771 /* --------------------------------------------------------------------------------------------- */
3773 void
3774 panel_clean_dir (WPanel * panel)
3776 int count = panel->count;
3778 panel->count = 0;
3779 panel->top_file = 0;
3780 panel->selected = 0;
3781 panel->marked = 0;
3782 panel->dirs_marked = 0;
3783 panel->total = 0;
3784 panel->searching = FALSE;
3785 panel->is_panelized = FALSE;
3786 panel->dirty = 1;
3788 clean_dir (&panel->dir, count);
3791 /* --------------------------------------------------------------------------------------------- */
3793 * Set Up panel's current dir object
3795 * @param panel panel object
3796 * @param path_str string contain path
3799 void
3800 panel_set_cwd (WPanel * panel, const char *path_str)
3802 vfs_path_free (panel->cwd_vpath);
3803 panel->cwd_vpath = vfs_path_from_str (path_str);
3806 /* --------------------------------------------------------------------------------------------- */
3808 * Set Up panel's last working dir object
3810 * @param panel panel object
3811 * @param path_str string contain path
3814 void
3815 panel_set_lwd (WPanel * panel, const char *path_str)
3817 vfs_path_free (panel->lwd_vpath);
3818 panel->lwd_vpath = vfs_path_from_str (path_str);
3821 /* --------------------------------------------------------------------------------------------- */
3822 /** Panel creation.
3823 * @param panel_name the name of the panel for setup retieving
3824 * @returns new instance of WPanel
3827 WPanel *
3828 panel_new (const char *panel_name)
3830 return panel_new_with_dir (panel_name, NULL);
3833 /* --------------------------------------------------------------------------------------------- */
3834 /** Panel creation for specified directory.
3835 * @param panel_name specifies the name of the panel for setup retieving
3836 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3837 * @returns new instance of WPanel
3840 WPanel *
3841 panel_new_with_dir (const char *panel_name, const char *wpath)
3843 WPanel *panel;
3844 char *section;
3845 int i, err;
3846 char *curdir = NULL;
3848 panel = g_new0 (WPanel, 1);
3850 /* No know sizes of the panel at startup */
3851 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3853 /* We do not want the cursor */
3854 widget_want_cursor (panel->widget, 0);
3856 if (wpath != NULL)
3858 curdir = _vfs_get_cwd ();
3859 panel_set_cwd (panel, wpath);
3861 else
3863 vfs_setup_cwd ();
3864 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3867 panel_set_lwd (panel, ".");
3869 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3870 /* directories history will be get later */
3872 panel->dir.list = g_new (file_entry, MIN_FILES);
3873 panel->dir.size = MIN_FILES;
3874 panel->active = 0;
3875 panel->filter = 0;
3876 panel->split = 0;
3877 panel->top_file = 0;
3878 panel->selected = 0;
3879 panel->marked = 0;
3880 panel->total = 0;
3881 panel->dirty = 1;
3882 panel->searching = FALSE;
3883 panel->dirs_marked = 0;
3884 panel->is_panelized = FALSE;
3885 panel->format = 0;
3886 panel->status_format = 0;
3887 panel->format_modified = 1;
3889 panel->panel_name = g_strdup (panel_name);
3890 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3892 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3894 for (i = 0; i < LIST_TYPES; i++)
3895 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3897 panel->search_buffer[0] = '\0';
3898 panel->prev_search_buffer[0] = '\0';
3899 panel->frame_size = frame_half;
3901 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3902 if (!mc_config_has_group (mc_main_config, section))
3904 g_free (section);
3905 section = g_strdup (panel->panel_name);
3907 panel_load_setup (panel, section);
3908 g_free (section);
3910 /* Load format strings */
3911 err = set_panel_formats (panel);
3912 if (err != 0)
3913 set_panel_formats (panel);
3915 #ifdef HAVE_CHARSET
3917 const vfs_path_element_t *path_element;
3919 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
3920 if (path_element->encoding != NULL)
3921 panel->codepage = get_codepage_index (path_element->encoding);
3923 #endif
3925 if (mc_chdir (panel->cwd_vpath) != 0)
3927 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3928 vfs_setup_cwd ();
3929 vfs_path_free (panel->cwd_vpath);
3930 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3933 /* Load the default format */
3935 char *tmp_path;
3937 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3938 panel->count =
3939 do_load_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
3940 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3941 panel->sort_info.exec_first, panel->filter);
3942 g_free (tmp_path);
3945 /* Restore old right path */
3946 if (curdir != NULL)
3948 vfs_path_t *vpath;
3950 vpath = vfs_path_from_str (curdir);
3951 err = mc_chdir (vpath);
3952 vfs_path_free (vpath);
3954 g_free (curdir);
3956 return panel;
3959 /* --------------------------------------------------------------------------------------------- */
3961 void
3962 panel_reload (WPanel * panel)
3964 struct stat current_stat;
3965 char *tmp_path;
3966 gboolean ok;
3968 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3969 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
3970 && current_stat.st_ctime == panel->dir_stat.st_ctime
3971 && current_stat.st_mtime == panel->dir_stat.st_mtime);
3972 g_free (tmp_path);
3974 if (ok)
3975 return;
3979 char *last_slash;
3981 if (mc_chdir (panel->cwd_vpath) != -1)
3982 break;
3984 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3985 if (tmp_path[0] == PATH_SEP && tmp_path[1] == 0)
3987 panel_clean_dir (panel);
3988 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
3989 g_free (tmp_path);
3990 return;
3992 last_slash = strrchr (tmp_path, PATH_SEP);
3993 if (!last_slash || last_slash == tmp_path)
3995 vfs_path_free (panel->cwd_vpath);
3996 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
3998 else
3999 *last_slash = 0;
4000 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4001 g_free (tmp_path);
4002 show_dir (panel);
4004 while (TRUE);
4006 panel->count =
4007 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4008 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4009 panel->sort_info.exec_first, panel->filter);
4011 panel->dirty = 1;
4012 if (panel->selected >= panel->count)
4013 do_select (panel, panel->count - 1);
4015 recalculate_panel_summary (panel);
4018 /* --------------------------------------------------------------------------------------------- */
4019 /* Switches the panel to the mode specified in the format */
4020 /* Seting up both format and status string. Return: 0 - on success; */
4021 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4024 set_panel_formats (WPanel * p)
4026 format_e *form;
4027 char *err = NULL;
4028 int retcode = 0;
4030 form = use_display_format (p, panel_format (p), &err, 0);
4032 if (err != NULL)
4034 g_free (err);
4035 retcode = 1;
4037 else
4039 delete_format (p->format);
4040 p->format = form;
4043 if (panels_options.show_mini_info)
4045 form = use_display_format (p, mini_status_format (p), &err, 1);
4047 if (err != NULL)
4049 g_free (err);
4050 retcode += 2;
4052 else
4054 delete_format (p->status_format);
4055 p->status_format = form;
4059 panel_format_modified (p);
4060 panel_update_cols (&(p->widget), p->frame_size);
4062 if (retcode)
4063 message (D_ERROR, _("Warning"),
4064 _("User supplied format looks invalid, reverting to default."));
4065 if (retcode & 0x01)
4067 g_free (p->user_format);
4068 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4070 if (retcode & 0x02)
4072 g_free (p->user_status_format[p->list_type]);
4073 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4076 return retcode;
4079 /* --------------------------------------------------------------------------------------------- */
4081 void
4082 panel_update_cols (Widget * widget, panel_display_t frame_size)
4084 int cols, origin;
4086 /* don't touch panel if it is not in dialog yet */
4087 /* if panel is not in dialog it is not in widgets list
4088 and cannot be compared with get_panel_widget() result */
4089 if (widget->owner == NULL)
4090 return;
4092 if (panels_layout.horizontal_split)
4094 widget->cols = COLS;
4095 return;
4098 if (frame_size == frame_full)
4100 cols = COLS;
4101 origin = 0;
4103 else if (widget == get_panel_widget (0))
4105 cols = panels_layout.left_panel_size;
4106 origin = 0;
4108 else
4110 cols = COLS - panels_layout.left_panel_size;
4111 origin = panels_layout.left_panel_size;
4114 widget->cols = cols;
4115 widget->x = origin;
4118 /* --------------------------------------------------------------------------------------------- */
4120 /* Select current item and readjust the panel */
4121 void
4122 select_item (WPanel * panel)
4124 /* Although currently all over the code we set the selection and
4125 top file to decent values before calling select_item, I could
4126 forget it someday, so it's better to do the actual fitting here */
4128 if (panel->selected < 0)
4129 panel->selected = 0;
4131 if (panel->selected > panel->count - 1)
4132 panel->selected = panel->count - 1;
4134 adjust_top_file (panel);
4136 panel->dirty = 1;
4138 execute_hooks (select_file_hook);
4141 /* --------------------------------------------------------------------------------------------- */
4142 /** Clears all files in the panel, used only when one file was marked */
4143 void
4144 unmark_files (WPanel * panel)
4146 int i;
4148 if (!panel->marked)
4149 return;
4150 for (i = 0; i < panel->count; i++)
4151 file_mark (panel, i, 0);
4153 panel->dirs_marked = 0;
4154 panel->marked = 0;
4155 panel->total = 0;
4158 /* --------------------------------------------------------------------------------------------- */
4159 /** Recalculate the panels summary information, used e.g. when marked
4160 files might have been removed by an external command */
4162 void
4163 recalculate_panel_summary (WPanel * panel)
4165 int i;
4167 panel->marked = 0;
4168 panel->dirs_marked = 0;
4169 panel->total = 0;
4171 for (i = 0; i < panel->count; i++)
4172 if (panel->dir.list[i].f.marked)
4174 /* do_file_mark will return immediately if newmark == oldmark.
4175 So we have to first unmark it to get panel's summary information
4176 updated. (Norbert) */
4177 panel->dir.list[i].f.marked = 0;
4178 do_file_mark (panel, i, 1);
4182 /* --------------------------------------------------------------------------------------------- */
4183 /** This routine marks a file or a directory */
4185 void
4186 do_file_mark (WPanel * panel, int idx, int mark)
4188 if (panel->dir.list[idx].f.marked == mark)
4189 return;
4191 /* Only '..' can't be marked, '.' isn't visible */
4192 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4193 return;
4195 file_mark (panel, idx, mark);
4196 if (panel->dir.list[idx].f.marked)
4198 panel->marked++;
4199 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4201 if (panel->dir.list[idx].f.dir_size_computed)
4202 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4203 panel->dirs_marked++;
4205 else
4206 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4207 set_colors (panel);
4209 else
4211 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4213 if (panel->dir.list[idx].f.dir_size_computed)
4214 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4215 panel->dirs_marked--;
4217 else
4218 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4219 panel->marked--;
4223 /* --------------------------------------------------------------------------------------------- */
4225 * Changes the current directory of the panel.
4226 * Record change in the directory history.
4228 gboolean
4229 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4231 gboolean r;
4233 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4234 if (r)
4236 char *tmp_path;
4238 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4239 directory_history_add (panel, tmp_path);
4240 g_free (tmp_path);
4242 return r;
4245 /* --------------------------------------------------------------------------------------------- */
4247 void
4248 file_mark (WPanel * panel, int lc_index, int val)
4250 if (panel->dir.list[lc_index].f.marked != val)
4252 panel->dir.list[lc_index].f.marked = val;
4253 panel->dirty = 1;
4257 /* --------------------------------------------------------------------------------------------- */
4259 void
4260 panel_re_sort (WPanel * panel)
4262 char *filename;
4263 int i;
4265 if (panel == NULL)
4266 return;
4268 filename = g_strdup (selection (panel)->fname);
4269 unselect_item (panel);
4270 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4271 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4272 panel->sort_info.exec_first);
4273 panel->selected = -1;
4274 for (i = panel->count; i; i--)
4276 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4278 panel->selected = i - 1;
4279 break;
4282 g_free (filename);
4283 panel->top_file = panel->selected - ITEMS (panel) / 2;
4284 select_item (panel);
4285 panel->dirty = 1;
4288 /* --------------------------------------------------------------------------------------------- */
4290 void
4291 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4293 if (sort_order == NULL)
4294 return;
4296 panel->sort_info.sort_field = sort_order;
4298 /* The directory is already sorted, we have to load the unsorted stuff */
4299 if (sort_order->sort_routine == (sortfn *) unsorted)
4301 char *current_file;
4303 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4304 panel_reload (panel);
4305 try_to_select (panel, current_file);
4306 g_free (current_file);
4308 panel_re_sort (panel);
4311 /* --------------------------------------------------------------------------------------------- */
4313 * Change panel encoding.
4314 * @param panel WPanel object
4317 void
4318 panel_change_encoding (WPanel * panel)
4320 const char *encoding = NULL;
4321 char *cd_path;
4322 #ifdef HAVE_CHARSET
4323 char *errmsg;
4324 int r;
4326 r = select_charset (-1, -1, panel->codepage, FALSE);
4328 if (r == SELECT_CHARSET_CANCEL)
4329 return; /* Cancel */
4331 panel->codepage = r;
4333 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4335 /* No translation */
4336 vfs_path_t *cd_path_vpath;
4338 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4339 cd_path = remove_encoding_from_path (panel->cwd_vpath);
4340 cd_path_vpath = vfs_path_from_str (cd_path);
4341 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4342 show_dir (panel);
4343 vfs_path_free (cd_path_vpath);
4344 g_free (cd_path);
4345 return;
4348 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4349 if (errmsg != NULL)
4351 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4352 g_free (errmsg);
4353 return;
4356 encoding = get_codepage_id (panel->codepage);
4357 #endif
4358 if (encoding != NULL)
4360 vfs_change_encoding (panel->cwd_vpath, encoding);
4362 cd_path = vfs_path_to_str (panel->cwd_vpath);
4363 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4364 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4365 g_free (cd_path);
4369 /* --------------------------------------------------------------------------------------------- */
4371 * This routine reloads the directory in both panels. It tries to
4372 * select current_file in current_panel and other_file in other_panel.
4373 * If current_file == -1 then it automatically sets current_file and
4374 * other_file to the currently selected files in the panels.
4376 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4377 * will not reload the other panel.
4380 void
4381 update_panels (panel_update_flags_t flags, const char *current_file)
4383 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4384 WPanel *panel;
4386 update_one_panel (get_current_index (), flags, current_file);
4387 if (reload_other)
4388 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4390 if (get_current_type () == view_listing)
4391 panel = (WPanel *) get_panel_widget (get_current_index ());
4392 else
4393 panel = (WPanel *) get_panel_widget (get_other_index ());
4395 if (!panel->is_panelized)
4396 (void) mc_chdir (panel->cwd_vpath);
4399 /* --------------------------------------------------------------------------------------------- */
4401 void
4402 directory_history_add (struct WPanel *panel, const char *dir)
4404 vfs_path_t *vpath;
4405 char *tmp;
4407 vpath = vfs_path_from_str (dir);
4408 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
4409 vfs_path_free (vpath);
4410 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4413 /* --------------------------------------------------------------------------------------------- */
4415 gsize
4416 panel_get_num_of_sortable_fields (void)
4418 gsize ret = 0, lc_index;
4420 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4421 if (panel_fields[lc_index].is_user_choice)
4422 ret++;
4423 return ret;
4426 /* --------------------------------------------------------------------------------------------- */
4428 const char **
4429 panel_get_sortable_fields (gsize * array_size)
4431 char **ret;
4432 gsize lc_index, i;
4434 lc_index = panel_get_num_of_sortable_fields ();
4436 ret = g_try_new0 (char *, lc_index + 1);
4437 if (ret == NULL)
4438 return NULL;
4440 if (array_size != NULL)
4441 *array_size = lc_index;
4443 lc_index = 0;
4445 for (i = 0; panel_fields[i].id != NULL; i++)
4446 if (panel_fields[i].is_user_choice)
4447 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4448 return (const char **) ret;
4451 /* --------------------------------------------------------------------------------------------- */
4453 const panel_field_t *
4454 panel_get_field_by_id (const char *name)
4456 gsize lc_index;
4457 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4458 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4459 return &panel_fields[lc_index];
4460 return NULL;
4463 /* --------------------------------------------------------------------------------------------- */
4465 const panel_field_t *
4466 panel_get_field_by_title_hotkey (const char *name)
4468 gsize lc_index;
4469 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4470 if (panel_fields[lc_index].title_hotkey != NULL &&
4471 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4472 return &panel_fields[lc_index];
4473 return NULL;
4476 /* --------------------------------------------------------------------------------------------- */
4478 const panel_field_t *
4479 panel_get_field_by_title (const char *name)
4481 gsize lc_index;
4482 gchar *title = NULL;
4484 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4486 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4487 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4489 g_free (title);
4490 return &panel_fields[lc_index];
4493 g_free (title);
4494 return NULL;
4497 /* --------------------------------------------------------------------------------------------- */
4499 gsize
4500 panel_get_num_of_user_possible_fields (void)
4502 gsize ret = 0, lc_index;
4504 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4505 if (panel_fields[lc_index].use_in_user_format)
4506 ret++;
4507 return ret;
4510 /* --------------------------------------------------------------------------------------------- */
4512 const char **
4513 panel_get_user_possible_fields (gsize * array_size)
4515 char **ret;
4516 gsize lc_index, i;
4518 lc_index = panel_get_num_of_user_possible_fields ();
4520 ret = g_try_new0 (char *, lc_index + 1);
4521 if (ret == NULL)
4522 return NULL;
4524 if (array_size != NULL)
4525 *array_size = lc_index;
4527 lc_index = 0;
4529 for (i = 0; panel_fields[i].id != NULL; i++)
4530 if (panel_fields[i].use_in_user_format)
4531 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4532 return (const char **) ret;
4535 /* --------------------------------------------------------------------------------------------- */
4537 void
4538 panel_init (void)
4540 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4541 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4543 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4544 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4545 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4546 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4547 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4549 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4550 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4551 panel_save_curent_file_to_clip_file, NULL, NULL);
4555 /* --------------------------------------------------------------------------------------------- */
4557 void
4558 panel_deinit (void)
4560 g_free (panel_sort_up_sign);
4561 g_free (panel_sort_down_sign);
4563 g_free (panel_hiddenfiles_sign_show);
4564 g_free (panel_hiddenfiles_sign_hide);
4565 g_free (panel_history_prev_item_sign);
4566 g_free (panel_history_next_item_sign);
4567 g_free (panel_history_show_list_sign);
4571 /* --------------------------------------------------------------------------------------------- */