Changed do_cd() and do_panel() functions
[midnight-commander.git] / src / filemanager / panel.c
blob105c88e3763f9efc52ef4ce6488b75375ba114b6
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 const vfs_path_element_t *path_element;
1085 GString *ret_str;
1087 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1088 ret_str = g_string_new ("");
1090 if ((path_element->class->flags & VFSF_LOCAL) == 0)
1092 char *url_str;
1094 g_string_append (ret_str, path_element->vfs_prefix);
1095 g_string_append (ret_str, VFS_PATH_URL_DELIMITER);
1097 url_str = vfs_path_build_url_params_str (path_element, FALSE);
1098 if (*url_str != '\0')
1100 g_string_append (ret_str, url_str);
1101 g_string_append_c (ret_str, PATH_SEP);
1103 g_free (url_str);
1104 g_string_append (ret_str, path_element->path);
1106 else
1108 char *tmp_path;
1110 tmp_path = g_strdup (path_element->path);
1111 g_string_append (ret_str, strip_home_and_password (tmp_path));
1112 g_free (tmp_path);
1114 return g_string_free (ret_str, FALSE);
1117 /* --------------------------------------------------------------------------------------------- */
1119 * Get Current path element encoding
1121 * @param panel WPanel object
1123 * @return newly allocated string or NULL if path charset is same as system charset
1126 static char *
1127 panel_get_encoding_info_str (WPanel * panel)
1129 char *ret_str = NULL;
1130 const vfs_path_element_t *path_element;
1132 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1133 if (path_element->encoding != NULL)
1134 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1136 return ret_str;
1139 /* --------------------------------------------------------------------------------------------- */
1141 static void
1142 show_dir (WPanel * panel)
1144 gchar *tmp;
1145 set_colors (panel);
1146 draw_box (panel->widget.owner,
1147 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1149 if (panels_options.show_mini_info)
1151 widget_move (&panel->widget, llines (panel) + 2, 0);
1152 tty_print_alt_char (ACS_LTEE, FALSE);
1153 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1154 tty_print_alt_char (ACS_RTEE, FALSE);
1157 widget_move (&panel->widget, 0, 1);
1158 tty_print_string (panel_history_prev_item_sign);
1160 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1161 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1162 panel_history_next_item_sign);
1164 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1165 tty_print_string (tmp);
1167 g_free (tmp);
1169 widget_move (&panel->widget, 0, 3);
1171 if (panel->is_panelized)
1172 tty_printf (" %s ", _("Panelize"));
1173 else
1175 tmp = panel_get_encoding_info_str (panel);
1176 if (tmp != NULL)
1178 tty_printf ("%s", tmp);
1179 widget_move (&panel->widget, 0, 3 + strlen (tmp));
1180 g_free (tmp);
1183 if (panel->active)
1184 tty_setcolor (REVERSE_COLOR);
1186 tmp = panel_correct_path_to_show (panel);
1187 tty_printf (" %s ",
1188 str_term_trim (tmp, min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1189 g_free (tmp);
1191 if (!panels_options.show_mini_info)
1193 if (panel->marked == 0)
1195 /* Show size of curret file in the bottom of panel */
1196 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1198 char buffer[BUF_SMALL];
1200 g_snprintf (buffer, sizeof (buffer), " %s ",
1201 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1202 panels_options.kilobyte_si));
1203 tty_setcolor (NORMAL_COLOR);
1204 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1205 tty_print_string (buffer);
1208 else
1210 /* Show total size of marked files
1211 * In the bottom of panel, display size only. */
1212 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1216 show_free_space (panel);
1218 if (panel->active)
1219 tty_set_normal_attrs ();
1222 /* --------------------------------------------------------------------------------------------- */
1224 /* Returns the number of items in the given panel */
1225 static int
1226 ITEMS (WPanel * p)
1228 if (p->split)
1229 return llines (p) * 2;
1230 else
1231 return llines (p);
1234 /* --------------------------------------------------------------------------------------------- */
1236 static void
1237 adjust_top_file (WPanel * panel)
1239 int items = ITEMS (panel);
1241 if (panel->count <= items)
1243 /* If all files fit, show them all. */
1244 panel->top_file = 0;
1246 else
1248 int i;
1250 /* top_file has to be in the range [selected-items+1, selected] so that
1251 the selected file is visible.
1252 top_file should be in the range [0, count-items] so that there's
1253 no empty space wasted.
1254 Within these ranges, adjust it by as little as possible. */
1256 if (panel->top_file < 0)
1257 panel->top_file = 0;
1259 i = panel->selected - items + 1;
1260 if (panel->top_file < i)
1261 panel->top_file = i;
1263 i = panel->count - items;
1264 if (panel->top_file > i)
1265 panel->top_file = i;
1267 if (panel->top_file > panel->selected)
1268 panel->top_file = panel->selected;
1272 /* --------------------------------------------------------------------------------------------- */
1273 /** add "#enc:encodning" to end of path */
1274 /* if path end width a previous #enc:, only encoding is changed no additional
1275 * #enc: is appended
1276 * retun new string
1279 static char *
1280 panel_save_name (WPanel * panel)
1282 /* If the program is shuting down */
1283 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1284 return g_strdup (panel->panel_name);
1285 else
1286 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1289 /* --------------------------------------------------------------------------------------------- */
1291 /* "history_load" event handler */
1292 static gboolean
1293 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1294 gpointer init_data, gpointer data)
1296 WPanel *p = (WPanel *) init_data;
1297 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1299 (void) event_group_name;
1300 (void) event_name;
1302 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1304 char *tmp_path;
1306 tmp_path = vfs_path_to_str (p->cwd_vpath);
1307 if (ev->cfg != NULL)
1308 p->dir_history = history_load (ev->cfg, p->hist_name);
1309 else
1310 p->dir_history = history_get (p->hist_name);
1312 directory_history_add (p, tmp_path);
1313 g_free (tmp_path);
1316 return TRUE;
1319 /* --------------------------------------------------------------------------------------------- */
1321 /* "history_save" event handler */
1322 static gboolean
1323 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1324 gpointer init_data, gpointer data)
1326 WPanel *p = (WPanel *) init_data;
1328 (void) event_group_name;
1329 (void) event_name;
1331 if (p->dir_history != NULL)
1333 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1335 history_save (ev->cfg, p->hist_name, p->dir_history);
1338 return TRUE;
1341 /* --------------------------------------------------------------------------------------------- */
1343 static void
1344 panel_destroy (WPanel * p)
1346 size_t i;
1348 if (panels_options.auto_save_setup)
1350 char *name;
1352 name = panel_save_name (p);
1353 panel_save_setup (p, name);
1354 g_free (name);
1357 panel_clean_dir (p);
1359 /* clean history */
1360 if (p->dir_history != NULL)
1362 /* directory history is already saved before this moment */
1363 p->dir_history = g_list_first (p->dir_history);
1364 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1365 g_list_free (p->dir_history);
1367 g_free (p->hist_name);
1369 delete_format (p->format);
1370 delete_format (p->status_format);
1372 g_free (p->user_format);
1373 for (i = 0; i < LIST_TYPES; i++)
1374 g_free (p->user_status_format[i]);
1376 g_free (p->dir.list);
1377 g_free (p->panel_name);
1379 vfs_path_free (p->lwd_vpath);
1380 vfs_path_free (p->cwd_vpath);
1383 /* --------------------------------------------------------------------------------------------- */
1385 static void
1386 panel_format_modified (WPanel * panel)
1388 panel->format_modified = 1;
1391 /* --------------------------------------------------------------------------------------------- */
1393 static void
1394 panel_paint_sort_info (WPanel * panel)
1396 if (*panel->sort_info.sort_field->hotkey != '\0')
1398 const char *sort_sign =
1399 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1400 char *str;
1402 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1403 widget_move (&panel->widget, 1, 1);
1404 tty_print_string (str);
1405 g_free (str);
1409 /* --------------------------------------------------------------------------------------------- */
1411 static gchar *
1412 panel_get_title_without_hotkey (const char *title)
1414 char *translated_title;
1415 char *hkey;
1417 if (title == NULL)
1418 return NULL;
1419 if (title[0] == '\0')
1420 return g_strdup ("");
1422 translated_title = g_strdup (_(title));
1424 hkey = strchr (translated_title, '&');
1425 if ((hkey != NULL) && (hkey[1] != '\0'))
1426 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1428 return translated_title;
1431 /* --------------------------------------------------------------------------------------------- */
1433 static void
1434 paint_frame (WPanel * panel)
1436 int side, width;
1437 GString *format_txt;
1439 adjust_top_file (panel);
1441 widget_erase (&panel->widget);
1442 show_dir (panel);
1444 widget_move (&panel->widget, 1, 1);
1446 for (side = 0; side <= panel->split; side++)
1448 format_e *format;
1450 if (side)
1452 tty_setcolor (NORMAL_COLOR);
1453 tty_print_one_vline (TRUE);
1454 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1456 else if (panel->split)
1457 width = panel->widget.cols / 2 - 3;
1458 else
1459 width = panel->widget.cols - 2;
1461 format_txt = g_string_new ("");
1462 for (format = panel->format; format; format = format->next)
1464 if (format->string_fn)
1466 g_string_set_size (format_txt, 0);
1468 if (panel->list_type == list_long
1469 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1470 g_string_append (format_txt,
1471 panel->sort_info.reverse
1472 ? panel_sort_down_sign : panel_sort_up_sign);
1474 g_string_append (format_txt, format->title);
1475 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1477 g_string_append (format_txt, " [");
1478 g_string_append (format_txt, panel->filter);
1479 g_string_append (format_txt, "]");
1482 tty_setcolor (HEADER_COLOR);
1483 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1484 J_CENTER_LEFT));
1485 width -= format->field_len;
1487 else
1489 tty_setcolor (NORMAL_COLOR);
1490 tty_print_one_vline (TRUE);
1491 width--;
1494 g_string_free (format_txt, TRUE);
1496 if (width > 0)
1497 tty_draw_hline (-1, -1, ' ', width);
1500 if (panel->list_type != list_long)
1501 panel_paint_sort_info (panel);
1504 /* --------------------------------------------------------------------------------------------- */
1506 static const char *
1507 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1509 panel_display_t frame = frame_half;
1510 format = skip_separators (format);
1512 if (!strncmp (format, "full", 4))
1514 frame = frame_full;
1515 format += 4;
1517 else if (!strncmp (format, "half", 4))
1519 frame = frame_half;
1520 format += 4;
1523 if (!isstatus)
1525 panel->frame_size = frame;
1526 panel->split = 0;
1529 /* Now, the optional column specifier */
1530 format = skip_separators (format);
1532 if (*format == '1' || *format == '2')
1534 if (!isstatus)
1535 panel->split = *format == '2';
1536 format++;
1539 if (!isstatus)
1540 panel_update_cols (&(panel->widget), panel->frame_size);
1542 return skip_separators (format);
1545 /* Format is:
1547 all := panel_format? format
1548 panel_format := [full|half] [1|2]
1549 format := one_format_e
1550 | format , one_format_e
1552 one_format_e := just format.id [opt_size]
1553 just := [<=>]
1554 opt_size := : size [opt_expand]
1555 size := [0-9]+
1556 opt_expand := +
1560 /* --------------------------------------------------------------------------------------------- */
1562 static format_e *
1563 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1564 int *res_total_cols)
1566 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1567 int total_cols = 0; /* Used columns by the format */
1568 int set_justify; /* flag: set justification mode? */
1569 align_crt_t justify = J_LEFT; /* Which mode. */
1570 size_t i;
1572 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1574 *error = 0;
1576 if (i18n_timelength == 0)
1578 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1580 for (i = 0; panel_fields[i].id != NULL; i++)
1581 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1582 panel_fields[i].min_size = i18n_timelength;
1586 * This makes sure that the panel and mini status full/half mode
1587 * setting is equal
1589 format = parse_panel_size (panel, format, isstatus);
1591 while (*format)
1592 { /* format can be an empty string */
1593 int found = 0;
1595 darr = g_new0 (format_e, 1);
1597 /* I'm so ugly, don't look at me :-) */
1598 if (!home)
1599 home = old = darr;
1601 old->next = darr;
1602 darr->next = 0;
1603 old = darr;
1605 format = skip_separators (format);
1607 if (strchr ("<=>", *format))
1609 set_justify = 1;
1610 switch (*format)
1612 case '<':
1613 justify = J_LEFT;
1614 break;
1615 case '=':
1616 justify = J_CENTER;
1617 break;
1618 case '>':
1619 default:
1620 justify = J_RIGHT;
1621 break;
1623 format = skip_separators (format + 1);
1625 else
1626 set_justify = 0;
1628 for (i = 0; panel_fields[i].id != NULL; i++)
1630 size_t klen = strlen (panel_fields[i].id);
1632 if (strncmp (format, panel_fields[i].id, klen) != 0)
1633 continue;
1635 format += klen;
1637 darr->requested_field_len = panel_fields[i].min_size;
1638 darr->string_fn = panel_fields[i].string_fn;
1639 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1641 darr->id = panel_fields[i].id;
1642 darr->expand = panel_fields[i].expands;
1643 darr->just_mode = panel_fields[i].default_just;
1645 if (set_justify)
1647 if (IS_FIT (darr->just_mode))
1648 darr->just_mode = MAKE_FIT (justify);
1649 else
1650 darr->just_mode = justify;
1652 found = 1;
1654 format = skip_separators (format);
1656 /* If we have a size specifier */
1657 if (*format == ':')
1659 int req_length;
1661 /* If the size was specified, we don't want
1662 * auto-expansion by default
1664 darr->expand = 0;
1665 format++;
1666 req_length = atoi (format);
1667 darr->requested_field_len = req_length;
1669 format = skip_numbers (format);
1671 /* Now, if they insist on expansion */
1672 if (*format == '+')
1674 darr->expand = 1;
1675 format++;
1680 break;
1682 if (!found)
1684 char *tmp_format = g_strdup (format);
1686 int pos = min (8, strlen (format));
1687 delete_format (home);
1688 tmp_format[pos] = 0;
1689 *error =
1690 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1691 g_free (tmp_format);
1692 return 0;
1694 total_cols += darr->requested_field_len;
1697 *res_total_cols = total_cols;
1698 return home;
1701 /* --------------------------------------------------------------------------------------------- */
1703 static format_e *
1704 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1706 #define MAX_EXPAND 4
1707 int expand_top = 0; /* Max used element in expand */
1708 int usable_columns; /* Usable columns in the panel */
1709 int total_cols = 0;
1710 int i;
1711 format_e *darr, *home;
1713 if (!format)
1714 format = DEFAULT_USER_FORMAT;
1716 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1718 if (*error)
1719 return 0;
1721 panel->dirty = 1;
1723 /* Status needn't to be split */
1724 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1726 : (panel->split + 1))) - (!isstatus
1727 && panel->split);
1729 /* Look for the expandable fields and set field_len based on the requested field len */
1730 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1732 darr->field_len = darr->requested_field_len;
1733 if (darr->expand)
1734 expand_top++;
1737 /* If we used more columns than the available columns, adjust that */
1738 if (total_cols > usable_columns)
1740 int pdif, dif = total_cols - usable_columns;
1742 while (dif)
1744 pdif = dif;
1745 for (darr = home; darr; darr = darr->next)
1747 if (dif && darr->field_len - 1)
1749 darr->field_len--;
1750 dif--;
1754 /* avoid endless loop if num fields > 40 */
1755 if (pdif == dif)
1756 break;
1758 total_cols = usable_columns; /* give up, the rest should be truncated */
1761 /* Expand the available space */
1762 if ((usable_columns > total_cols) && expand_top)
1764 int spaces = (usable_columns - total_cols) / expand_top;
1765 int extra = (usable_columns - total_cols) % expand_top;
1767 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1768 if (darr->expand)
1770 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1771 i++;
1774 return home;
1777 /* --------------------------------------------------------------------------------------------- */
1778 /** Given the panel->view_type returns the format string to be parsed */
1780 static const char *
1781 panel_format (WPanel * panel)
1783 switch (panel->list_type)
1785 case list_long:
1786 return "full perm space nlink space owner space group space size space mtime space name";
1788 case list_brief:
1789 return "half 2 type name";
1791 case list_user:
1792 return panel->user_format;
1794 default:
1795 case list_full:
1796 return "half type name | size | mtime";
1800 /* --------------------------------------------------------------------------------------------- */
1802 static const char *
1803 mini_status_format (WPanel * panel)
1805 if (panel->user_mini_status)
1806 return panel->user_status_format[panel->list_type];
1808 switch (panel->list_type)
1811 case list_long:
1812 return "full perm space nlink space owner space group space size space mtime space name";
1814 case list_brief:
1815 return "half type name space bsize space perm space";
1817 case list_full:
1818 return "half type name";
1820 default:
1821 case list_user:
1822 return panel->user_format;
1826 /* */
1827 /* Panel operation commands */
1828 /* */
1830 /* --------------------------------------------------------------------------------------------- */
1831 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1833 static cb_ret_t
1834 maybe_cd (int move_up_dir)
1836 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1838 if (move_up_dir)
1840 vfs_path_t *up_dir;
1842 up_dir = vfs_path_from_str ("..");
1843 do_cd (up_dir, cd_exact);
1844 vfs_path_free (up_dir);
1845 return MSG_HANDLED;
1848 if (S_ISDIR (selection (current_panel)->st.st_mode)
1849 || link_isdir (selection (current_panel)))
1851 vfs_path_t *vpath;
1853 vpath = vfs_path_from_str (selection (current_panel)->fname);
1854 do_cd (vpath, cd_exact);
1855 vfs_path_free (vpath);
1856 return MSG_HANDLED;
1859 return MSG_NOT_HANDLED;
1862 /* --------------------------------------------------------------------------------------------- */
1864 /* if command line is empty then do 'cd ..' */
1865 static cb_ret_t
1866 force_maybe_cd (void)
1868 if (cmdline->buffer[0] == '\0')
1870 vfs_path_t *up_dir = vfs_path_from_str ("..");
1871 do_cd (up_dir, cd_exact);
1872 vfs_path_free (up_dir);
1873 return MSG_HANDLED;
1875 return MSG_NOT_HANDLED;
1878 /* --------------------------------------------------------------------------------------------- */
1880 static void
1881 unselect_item (WPanel * panel)
1883 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1886 /* --------------------------------------------------------------------------------------------- */
1888 static void
1889 move_down (WPanel * panel)
1891 if (panel->selected + 1 == panel->count)
1892 return;
1894 unselect_item (panel);
1895 panel->selected++;
1896 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1898 /* Scroll window half screen */
1899 panel->top_file += ITEMS (panel) / 2;
1900 if (panel->top_file > panel->count - ITEMS (panel))
1901 panel->top_file = panel->count - ITEMS (panel);
1902 paint_dir (panel);
1904 select_item (panel);
1907 /* --------------------------------------------------------------------------------------------- */
1909 static void
1910 move_up (WPanel * panel)
1912 if (panel->selected == 0)
1913 return;
1915 unselect_item (panel);
1916 panel->selected--;
1917 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1919 /* Scroll window half screen */
1920 panel->top_file -= ITEMS (panel) / 2;
1921 if (panel->top_file < 0)
1922 panel->top_file = 0;
1923 paint_dir (panel);
1925 select_item (panel);
1928 /* --------------------------------------------------------------------------------------------- */
1929 /** Changes the selection by lines (may be negative) */
1931 static void
1932 move_selection (WPanel * panel, int lines)
1934 int new_pos;
1935 int adjust = 0;
1937 new_pos = panel->selected + lines;
1938 if (new_pos >= panel->count)
1939 new_pos = panel->count - 1;
1941 if (new_pos < 0)
1942 new_pos = 0;
1944 unselect_item (panel);
1945 panel->selected = new_pos;
1947 if (panel->selected - panel->top_file >= ITEMS (panel))
1949 panel->top_file += lines;
1950 adjust = 1;
1953 if (panel->selected - panel->top_file < 0)
1955 panel->top_file += lines;
1956 adjust = 1;
1959 if (adjust)
1961 if (panel->top_file > panel->selected)
1962 panel->top_file = panel->selected;
1963 if (panel->top_file < 0)
1964 panel->top_file = 0;
1965 paint_dir (panel);
1967 select_item (panel);
1970 /* --------------------------------------------------------------------------------------------- */
1972 static cb_ret_t
1973 move_left (WPanel * panel)
1975 if (panel->split)
1977 move_selection (panel, -llines (panel));
1978 return MSG_HANDLED;
1980 else
1981 return maybe_cd (1); /* cd .. */
1984 /* --------------------------------------------------------------------------------------------- */
1986 static cb_ret_t
1987 move_right (WPanel * panel)
1989 if (panel->split)
1991 move_selection (panel, llines (panel));
1992 return MSG_HANDLED;
1994 else
1995 return maybe_cd (0); /* cd (selection) */
1998 /* --------------------------------------------------------------------------------------------- */
2000 static void
2001 prev_page (WPanel * panel)
2003 int items;
2005 if (!panel->selected && !panel->top_file)
2006 return;
2007 unselect_item (panel);
2008 items = ITEMS (panel);
2009 if (panel->top_file < items)
2010 items = panel->top_file;
2011 if (!items)
2012 panel->selected = 0;
2013 else
2014 panel->selected -= items;
2015 panel->top_file -= items;
2017 select_item (panel);
2018 paint_dir (panel);
2021 /* --------------------------------------------------------------------------------------------- */
2023 static void
2024 goto_parent_dir (WPanel * panel)
2026 if (!panel->is_panelized)
2028 vfs_path_t *up_dir;
2030 up_dir = vfs_path_from_str ("..");
2031 do_cd (up_dir, cd_exact);
2032 vfs_path_free (up_dir);
2034 else
2036 char *fname = panel->dir.list[panel->selected].fname;
2037 const char *bname;
2038 vfs_path_t *dname_vpath;
2040 if (g_path_is_absolute (fname))
2041 fname = g_strdup (fname);
2042 else
2044 char *tmp_root;
2046 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2047 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2048 g_free (tmp_root);
2051 bname = x_basename (fname);
2053 if (bname == fname)
2054 dname_vpath = vfs_path_from_str (".");
2055 else
2057 char *dname;
2059 dname = g_strndup (fname, bname - fname);
2060 dname_vpath = vfs_path_from_str (dname);
2061 g_free (dname);
2064 do_cd (dname_vpath, cd_exact);
2065 try_to_select (panel, bname);
2067 vfs_path_free (dname_vpath);
2068 g_free (fname);
2072 /* --------------------------------------------------------------------------------------------- */
2074 static void
2075 next_page (WPanel * panel)
2077 int items;
2079 if (panel->selected == panel->count - 1)
2080 return;
2081 unselect_item (panel);
2082 items = ITEMS (panel);
2083 if (panel->top_file > panel->count - 2 * items)
2084 items = panel->count - items - panel->top_file;
2085 if (panel->top_file + items < 0)
2086 items = -panel->top_file;
2087 if (!items)
2088 panel->selected = panel->count - 1;
2089 else
2090 panel->selected += items;
2091 panel->top_file += items;
2093 select_item (panel);
2094 paint_dir (panel);
2097 /* --------------------------------------------------------------------------------------------- */
2099 static void
2100 goto_child_dir (WPanel * panel)
2102 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2104 vfs_path_t *vpath;
2106 vpath = vfs_path_from_str (selection (panel)->fname);
2107 do_cd (vpath, cd_exact);
2108 vfs_path_free (vpath);
2112 /* --------------------------------------------------------------------------------------------- */
2114 static void
2115 goto_top_file (WPanel * panel)
2117 unselect_item (panel);
2118 panel->selected = panel->top_file;
2119 select_item (panel);
2122 /* --------------------------------------------------------------------------------------------- */
2124 static void
2125 goto_middle_file (WPanel * panel)
2127 unselect_item (panel);
2128 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2129 select_item (panel);
2132 /* --------------------------------------------------------------------------------------------- */
2134 static void
2135 goto_bottom_file (WPanel * panel)
2137 unselect_item (panel);
2138 panel->selected = panel->top_file + ITEMS (panel) - 1;
2139 select_item (panel);
2142 /* --------------------------------------------------------------------------------------------- */
2144 static void
2145 move_home (WPanel * panel)
2147 if (panel->selected == 0)
2148 return;
2150 unselect_item (panel);
2152 if (panels_options.torben_fj_mode)
2154 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2156 if (panel->selected > middle_pos)
2158 goto_middle_file (panel);
2159 return;
2161 if (panel->selected != panel->top_file)
2163 goto_top_file (panel);
2164 return;
2168 panel->top_file = 0;
2169 panel->selected = 0;
2171 paint_dir (panel);
2172 select_item (panel);
2175 /* --------------------------------------------------------------------------------------------- */
2177 static void
2178 move_end (WPanel * panel)
2180 if (panel->selected == panel->count - 1)
2181 return;
2183 unselect_item (panel);
2185 if (panels_options.torben_fj_mode)
2187 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2189 if (panel->selected < middle_pos)
2191 goto_middle_file (panel);
2192 return;
2194 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2196 goto_bottom_file (panel);
2197 return;
2201 panel->selected = panel->count - 1;
2202 paint_dir (panel);
2203 select_item (panel);
2206 /* --------------------------------------------------------------------------------------------- */
2208 static void
2209 do_mark_file (WPanel * panel, mark_act_t do_move)
2211 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2212 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2213 move_down (panel);
2214 else if (do_move == MARK_FORCE_UP)
2215 move_up (panel);
2218 /* --------------------------------------------------------------------------------------------- */
2220 static void
2221 mark_file (WPanel * panel)
2223 do_mark_file (panel, MARK_DOWN);
2226 /* --------------------------------------------------------------------------------------------- */
2228 static void
2229 mark_file_up (WPanel * panel)
2231 do_mark_file (panel, MARK_FORCE_UP);
2234 /* --------------------------------------------------------------------------------------------- */
2236 static void
2237 mark_file_down (WPanel * panel)
2239 do_mark_file (panel, MARK_FORCE_DOWN);
2242 /* --------------------------------------------------------------------------------------------- */
2244 static void
2245 mark_file_right (WPanel * panel)
2247 int lines = llines (panel);
2249 if (state_mark < 0)
2250 state_mark = selection (panel)->f.marked ? 0 : 1;
2252 lines = min (lines, panel->count - panel->selected - 1);
2253 for (; lines != 0; lines--)
2255 do_file_mark (panel, panel->selected, state_mark);
2256 move_down (panel);
2258 do_file_mark (panel, panel->selected, state_mark);
2261 /* --------------------------------------------------------------------------------------------- */
2263 static void
2264 mark_file_left (WPanel * panel)
2266 int lines = llines (panel);
2268 if (state_mark < 0)
2269 state_mark = selection (panel)->f.marked ? 0 : 1;
2271 lines = min (lines, panel->selected + 1);
2272 for (; lines != 0; lines--)
2274 do_file_mark (panel, panel->selected, state_mark);
2275 move_up (panel);
2277 do_file_mark (panel, panel->selected, state_mark);
2280 /* --------------------------------------------------------------------------------------------- */
2281 /** Incremental search of a file name in the panel.
2282 * @param panel instance of WPanel structure
2283 * @param c_code key code
2286 static void
2287 do_search (WPanel * panel, int c_code)
2289 size_t l;
2290 int i, sel;
2291 gboolean wrapped = FALSE;
2292 char *act;
2293 mc_search_t *search;
2294 char *reg_exp, *esc_str;
2295 gboolean is_found = FALSE;
2297 l = strlen (panel->search_buffer);
2298 if (c_code == KEY_BACKSPACE)
2300 if (l != 0)
2302 act = panel->search_buffer + l;
2303 str_prev_noncomb_char (&act, panel->search_buffer);
2304 act[0] = '\0';
2306 panel->search_chpoint = 0;
2308 else
2310 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2312 panel->search_char[panel->search_chpoint] = c_code;
2313 panel->search_chpoint++;
2316 if (panel->search_chpoint > 0)
2318 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2320 case -2:
2321 return;
2322 case -1:
2323 panel->search_chpoint = 0;
2324 return;
2325 default:
2326 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2328 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2329 l += panel->search_chpoint;
2330 *(panel->search_buffer + l) = '\0';
2331 panel->search_chpoint = 0;
2337 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2338 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2339 search = mc_search_new (esc_str, -1);
2340 search->search_type = MC_SEARCH_T_GLOB;
2341 search->is_entire_line = TRUE;
2342 switch (panels_options.qsearch_mode)
2344 case QSEARCH_CASE_SENSITIVE:
2345 search->is_case_sensitive = TRUE;
2346 break;
2347 case QSEARCH_CASE_INSENSITIVE:
2348 search->is_case_sensitive = FALSE;
2349 break;
2350 default:
2351 search->is_case_sensitive = panel->sort_info.case_sensitive;
2352 break;
2354 sel = panel->selected;
2355 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2357 if (i >= panel->count)
2359 i = 0;
2360 if (wrapped)
2361 break;
2362 wrapped = TRUE;
2364 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2366 sel = i;
2367 is_found = TRUE;
2368 break;
2371 if (is_found)
2373 unselect_item (panel);
2374 panel->selected = sel;
2375 select_item (panel);
2376 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2378 else if (c_code != KEY_BACKSPACE)
2380 act = panel->search_buffer + l;
2381 str_prev_noncomb_char (&act, panel->search_buffer);
2382 act[0] = '\0';
2384 mc_search_free (search);
2385 g_free (reg_exp);
2386 g_free (esc_str);
2389 /* --------------------------------------------------------------------------------------------- */
2390 /** Start new search.
2391 * @param panel instance of WPanel structure
2394 static void
2395 start_search (WPanel * panel)
2397 if (panel->searching)
2399 if (panel->selected + 1 == panel->count)
2400 panel->selected = 0;
2401 else
2402 move_down (panel);
2404 /* in case if there was no search string we need to recall
2405 previous string, with which we ended previous searching */
2406 if (panel->search_buffer[0] == '\0')
2407 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2408 sizeof (panel->search_buffer));
2410 do_search (panel, 0);
2412 else
2414 panel->searching = TRUE;
2415 panel->search_buffer[0] = '\0';
2416 panel->search_char[0] = '\0';
2417 panel->search_chpoint = 0;
2418 display_mini_info (panel);
2419 mc_refresh ();
2423 /* --------------------------------------------------------------------------------------------- */
2425 static void
2426 stop_search (WPanel * panel)
2428 panel->searching = FALSE;
2430 /* if user had overrdied search string, we need to store it
2431 to the previous_search_buffer */
2432 if (panel->search_buffer[0] != '\0')
2433 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2434 sizeof (panel->prev_search_buffer));
2436 display_mini_info (panel);
2439 /* --------------------------------------------------------------------------------------------- */
2440 /** Return 1 if the Enter key has been processed, 0 otherwise */
2442 static int
2443 do_enter_on_file_entry (file_entry * fe)
2445 vfs_path_t *full_name_vpath;
2446 gboolean ok;
2449 * Directory or link to directory - change directory.
2450 * Try the same for the entries on which mc_lstat() has failed.
2452 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2454 vfs_path_t *fname_vpath;
2456 fname_vpath = vfs_path_from_str (fe->fname);
2457 if (!do_cd (fname_vpath, cd_exact))
2458 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2459 vfs_path_free (fname_vpath);
2460 return 1;
2463 /* Try associated command */
2464 if (regex_command (fe->fname, "Open", NULL) != 0)
2465 return 1;
2467 /* Check if the file is executable */
2468 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2469 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2470 vfs_path_free (full_name_vpath);
2471 if (!ok)
2472 return 0;
2474 if (confirm_execute)
2476 if (query_dialog
2477 (_("The Midnight Commander"),
2478 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2479 return 1;
2482 if (!vfs_current_is_local ())
2484 int ret;
2485 vfs_path_t *tmp_vpath;
2487 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2488 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2489 vfs_path_free (tmp_vpath);
2490 /* We took action only if the dialog was shown or the execution
2491 * was successful */
2492 return confirm_execute || (ret == 0);
2496 char *tmp = name_quote (fe->fname, 0);
2497 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2498 g_free (tmp);
2499 shell_execute (cmd, 0);
2500 g_free (cmd);
2503 #ifdef HAVE_CHARSET
2504 mc_global.source_codepage = default_source_codepage;
2505 #endif
2507 return 1;
2510 /* --------------------------------------------------------------------------------------------- */
2512 static int
2513 do_enter (WPanel * panel)
2515 return do_enter_on_file_entry (selection (panel));
2518 /* --------------------------------------------------------------------------------------------- */
2520 static void
2521 chdir_other_panel (WPanel * panel)
2523 const file_entry *entry = &panel->dir.list[panel->selected];
2525 vfs_path_t *new_dir_vpath;
2526 char *sel_entry = NULL;
2528 if (get_other_type () != view_listing)
2530 set_display_type (get_other_index (), view_listing);
2533 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2534 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2535 else
2537 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2538 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2541 change_panel ();
2542 do_cd (new_dir_vpath, cd_exact);
2543 vfs_path_free (new_dir_vpath);
2545 if (sel_entry)
2546 try_to_select (current_panel, sel_entry);
2547 change_panel ();
2549 move_down (panel);
2552 /* --------------------------------------------------------------------------------------------- */
2554 * Make the current directory of the current panel also the current
2555 * directory of the other panel. Put the other panel to the listing
2556 * mode if needed. If the current panel is panelized, the other panel
2557 * doesn't become panelized.
2560 static void
2561 panel_sync_other (const WPanel * panel)
2563 if (get_other_type () != view_listing)
2565 set_display_type (get_other_index (), view_listing);
2568 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2570 /* try to select current filename on the other panel */
2571 if (!panel->is_panelized)
2573 try_to_select (other_panel, selection (panel)->fname);
2577 /* --------------------------------------------------------------------------------------------- */
2579 static void
2580 chdir_to_readlink (WPanel * panel)
2582 vfs_path_t *new_dir_vpath;
2583 char buffer[MC_MAXPATHLEN], *p;
2584 int i;
2585 struct stat st;
2586 vfs_path_t *panel_fname_vpath;
2587 gboolean ok;
2589 if (get_other_type () != view_listing)
2590 return;
2592 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2593 return;
2595 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2596 if (i < 0)
2597 return;
2599 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2600 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2601 vfs_path_free (panel_fname_vpath);
2602 if (!ok)
2603 return;
2605 buffer[i] = 0;
2606 if (!S_ISDIR (st.st_mode))
2608 p = strrchr (buffer, PATH_SEP);
2609 if (p && !p[1])
2611 *p = 0;
2612 p = strrchr (buffer, PATH_SEP);
2614 if (!p)
2615 return;
2616 p[1] = 0;
2618 if (*buffer == PATH_SEP)
2619 new_dir_vpath = vfs_path_from_str (buffer);
2620 else
2621 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2623 change_panel ();
2624 do_cd (new_dir_vpath, cd_exact);
2625 vfs_path_free (new_dir_vpath);
2626 change_panel ();
2628 move_down (panel);
2631 /* --------------------------------------------------------------------------------------------- */
2633 static gsize
2634 panel_get_format_field_count (WPanel * panel)
2636 format_e *format;
2637 gsize lc_index;
2638 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2639 return lc_index;
2642 /* --------------------------------------------------------------------------------------------- */
2644 function return 0 if not found and REAL_INDEX+1 if found
2647 static gsize
2648 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2650 format_e *format;
2651 gsize lc_index;
2653 for (lc_index = 1, format = panel->format;
2654 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2655 if (format == NULL)
2656 lc_index = 0;
2658 return lc_index;
2661 /* --------------------------------------------------------------------------------------------- */
2663 static format_e *
2664 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2666 format_e *format;
2667 for (format = panel->format;
2668 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2669 return format;
2672 /* --------------------------------------------------------------------------------------------- */
2674 static const panel_field_t *
2675 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2677 const panel_field_t *pfield;
2678 format_e *format;
2680 format = panel_get_format_field_by_index (panel, lc_index);
2681 if (format == NULL)
2682 return NULL;
2683 pfield = panel_get_field_by_title (format->title);
2684 if (pfield == NULL)
2685 return NULL;
2686 if (pfield->sort_routine == NULL)
2687 return NULL;
2688 return pfield;
2691 /* --------------------------------------------------------------------------------------------- */
2693 static void
2694 panel_toggle_sort_order_prev (WPanel * panel)
2696 gsize lc_index, i;
2697 gchar *title;
2699 const panel_field_t *pfield = NULL;
2701 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2702 lc_index = panel_get_format_field_index_by_name (panel, title);
2703 g_free (title);
2705 if (lc_index > 1)
2707 /* search for prev sortable column in panel format */
2708 for (i = lc_index - 1;
2709 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2712 if (pfield == NULL)
2714 /* Sortable field not found. Try to search in each array */
2715 for (i = panel_get_format_field_count (panel);
2716 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2719 if (pfield != NULL)
2721 panel->sort_info.sort_field = pfield;
2722 panel_set_sort_order (panel, pfield);
2726 /* --------------------------------------------------------------------------------------------- */
2728 static void
2729 panel_toggle_sort_order_next (WPanel * panel)
2731 gsize lc_index, i;
2732 const panel_field_t *pfield = NULL;
2733 gsize format_field_count;
2734 gchar *title;
2736 format_field_count = panel_get_format_field_count (panel);
2737 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2738 lc_index = panel_get_format_field_index_by_name (panel, title);
2739 g_free (title);
2741 if (lc_index != 0 && lc_index != format_field_count)
2743 /* search for prev sortable column in panel format */
2744 for (i = lc_index;
2745 i != format_field_count
2746 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2749 if (pfield == NULL)
2751 /* Sortable field not found. Try to search in each array */
2752 for (i = 0;
2753 i != format_field_count
2754 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2757 if (pfield != NULL)
2759 panel->sort_info.sort_field = pfield;
2760 panel_set_sort_order (panel, pfield);
2764 /* --------------------------------------------------------------------------------------------- */
2766 static void
2767 panel_select_sort_order (WPanel * panel)
2769 const panel_field_t *sort_order;
2771 sort_order = sort_box (&panel->sort_info);
2772 if (sort_order != NULL)
2774 panel->sort_info.sort_field = sort_order;
2775 panel_set_sort_order (panel, sort_order);
2779 /* --------------------------------------------------------------------------------------------- */
2781 static void
2782 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2784 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2786 const panel_field_t *sort_order;
2788 sort_order = panel_get_field_by_id (name);
2789 if (sort_order == NULL)
2790 return;
2791 panel->sort_info.sort_field = sort_order;
2793 else
2794 panel->sort_info.reverse = !panel->sort_info.reverse;
2796 panel_set_sort_order (panel, panel->sort_info.sort_field);
2799 /* --------------------------------------------------------------------------------------------- */
2801 * If we moved to the parent directory move the selection pointer to
2802 * the old directory name; If we leave VFS dir, remove FS specificator.
2804 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2807 static const char *
2808 get_parent_dir_name (const char *cwd, const char *lwd)
2810 size_t llen, clen;
2811 const char *p;
2813 llen = strlen (lwd);
2814 clen = strlen (cwd);
2816 if (llen <= clen)
2817 return NULL;
2819 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2821 if (p == NULL)
2823 p = strrchr (lwd, PATH_SEP);
2825 if ((p != NULL)
2826 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2827 && (clen == (size_t) (p - lwd)
2828 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2829 return (p + 1);
2831 return NULL;
2834 /* skip VFS prefix */
2835 while (--p > lwd && *p != PATH_SEP)
2837 /* get last component */
2838 while (--p > lwd && *p != PATH_SEP)
2841 /* return last component */
2842 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
2845 /* --------------------------------------------------------------------------------------------- */
2846 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2848 static void
2849 subshell_chdir (const vfs_path_t * vpath)
2851 #ifdef HAVE_SUBSHELL_SUPPORT
2852 if (mc_global.tty.use_subshell && vfs_current_is_local ())
2853 do_subshell_chdir (vpath, FALSE, TRUE);
2854 #endif /* HAVE_SUBSHELL_SUPPORT */
2857 /* --------------------------------------------------------------------------------------------- */
2859 * Changes the current directory of the panel.
2860 * Don't record change in the directory history.
2863 static gboolean
2864 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
2866 char *olddir;
2867 char temp[MC_MAXPATHLEN];
2868 char *new_dir, *_new_dir;
2870 _new_dir = new_dir = vfs_path_to_str (new_dir_vpath);
2872 if (cd_type == cd_parse_command)
2874 while (*new_dir == ' ')
2875 new_dir++;
2878 olddir = vfs_path_to_str (panel->cwd_vpath);
2880 /* Convert *new_path to a suitable pathname, handle ~user */
2882 if (cd_type == cd_parse_command)
2884 if (strcmp (new_dir, "-") == 0)
2886 char *tmp_path;
2888 tmp_path = vfs_path_to_str (panel->lwd_vpath);
2889 strcpy (temp, tmp_path);
2890 new_dir = temp;
2891 g_free (tmp_path);
2894 g_free (_new_dir);
2896 if (mc_chdir (new_dir_vpath) == -1)
2898 panel_set_cwd (panel, olddir);
2899 g_free (olddir);
2900 return FALSE;
2903 /* Success: save previous directory, shutdown status of previous dir */
2904 panel_set_lwd (panel, olddir);
2905 input_free_completions (cmdline);
2907 vfs_path_free (panel->cwd_vpath);
2908 vfs_setup_cwd ();
2909 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
2911 vfs_release_path (olddir);
2913 subshell_chdir (panel->cwd_vpath);
2915 /* Reload current panel */
2916 panel_clean_dir (panel);
2918 char *tmp_path;
2920 tmp_path = vfs_path_to_str (panel->cwd_vpath);
2921 panel->count =
2922 do_load_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
2923 panel->sort_info.reverse, panel->sort_info.case_sensitive,
2924 panel->sort_info.exec_first, panel->filter);
2925 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
2926 g_free (tmp_path);
2928 load_hint (0);
2929 panel->dirty = 1;
2930 update_xterm_title_path ();
2932 g_free (olddir);
2934 return TRUE;
2937 /* --------------------------------------------------------------------------------------------- */
2939 static void
2940 directory_history_next (WPanel * panel)
2942 GList *nextdir;
2944 nextdir = g_list_next (panel->dir_history);
2945 if (nextdir != NULL)
2947 vfs_path_t *data_vpath;
2949 data_vpath = vfs_path_from_str ((char *) nextdir->data);
2950 if (_do_panel_cd (panel, data_vpath, cd_exact))
2951 panel->dir_history = nextdir;
2952 vfs_path_free (data_vpath);
2956 /* --------------------------------------------------------------------------------------------- */
2958 static void
2959 directory_history_prev (WPanel * panel)
2961 GList *prevdir;
2963 prevdir = g_list_previous (panel->dir_history);
2965 if (prevdir != NULL)
2967 vfs_path_t *data_vpath;
2969 data_vpath = vfs_path_from_str ((char *) prevdir->data);
2970 if (_do_panel_cd (panel, data_vpath, cd_exact))
2971 panel->dir_history = prevdir;
2972 vfs_path_free (data_vpath);
2976 /* --------------------------------------------------------------------------------------------- */
2978 static void
2979 directory_history_list (WPanel * panel)
2981 char *s;
2983 s = history_show (&panel->dir_history, &panel->widget);
2985 if (s != NULL)
2987 vfs_path_t *s_vpath;
2989 s_vpath = vfs_path_from_str (s);
2990 if (_do_panel_cd (panel, s_vpath, cd_exact))
2992 char *tmp_path;
2994 tmp_path = vfs_path_to_str (panel->cwd_vpath);
2995 directory_history_add (panel, tmp_path);
2996 g_free (tmp_path);
2998 else
2999 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3000 vfs_path_free (s_vpath);
3001 g_free (s);
3005 /* --------------------------------------------------------------------------------------------- */
3007 static cb_ret_t
3008 panel_execute_cmd (WPanel * panel, unsigned long command)
3010 int res = MSG_HANDLED;
3012 if (command != CK_Search)
3013 stop_search (panel);
3016 switch (command)
3018 case CK_Up:
3019 case CK_Down:
3020 case CK_Left:
3021 case CK_Right:
3022 case CK_Bottom:
3023 case CK_Top:
3024 case CK_PageDown:
3025 case CK_PageUp:
3026 /* reset state of marks flag */
3027 state_mark = -1;
3028 break;
3030 switch (command)
3032 case CK_PanelOtherCd:
3033 chdir_other_panel (panel);
3034 break;
3035 case CK_PanelOtherCdLink:
3036 chdir_to_readlink (panel);
3037 break;
3038 case CK_CopySingle:
3039 copy_cmd_local ();
3040 break;
3041 case CK_DeleteSingle:
3042 delete_cmd_local ();
3043 break;
3044 case CK_Enter:
3045 do_enter (panel);
3046 break;
3047 case CK_ViewRaw:
3048 view_raw_cmd ();
3049 break;
3050 case CK_EditNew:
3051 edit_cmd_new ();
3052 break;
3053 case CK_MoveSingle:
3054 rename_cmd_local ();
3055 break;
3056 case CK_SelectInvert:
3057 select_invert_cmd ();
3058 break;
3059 case CK_Select:
3060 select_cmd ();
3061 break;
3062 case CK_Unselect:
3063 unselect_cmd ();
3064 break;
3065 case CK_PageDown:
3066 next_page (panel);
3067 break;
3068 case CK_PageUp:
3069 prev_page (panel);
3070 break;
3071 case CK_CdChild:
3072 goto_child_dir (panel);
3073 break;
3074 case CK_CdParent:
3075 goto_parent_dir (panel);
3076 break;
3077 case CK_History:
3078 directory_history_list (panel);
3079 break;
3080 case CK_HistoryNext:
3081 directory_history_next (panel);
3082 break;
3083 case CK_HistoryPrev:
3084 directory_history_prev (panel);
3085 break;
3086 case CK_BottomOnScreen:
3087 goto_bottom_file (panel);
3088 break;
3089 case CK_MiddleOnScreen:
3090 goto_middle_file (panel);
3091 break;
3092 case CK_TopOnScreen:
3093 goto_top_file (panel);
3094 break;
3095 case CK_Mark:
3096 mark_file (panel);
3097 break;
3098 case CK_MarkUp:
3099 mark_file_up (panel);
3100 break;
3101 case CK_MarkDown:
3102 mark_file_down (panel);
3103 break;
3104 case CK_MarkLeft:
3105 mark_file_left (panel);
3106 break;
3107 case CK_MarkRight:
3108 mark_file_right (panel);
3109 break;
3110 case CK_CdParentSmart:
3111 res = force_maybe_cd ();
3112 break;
3113 case CK_Up:
3114 move_up (panel);
3115 break;
3116 case CK_Down:
3117 move_down (panel);
3118 break;
3119 case CK_Left:
3120 res = move_left (panel);
3121 break;
3122 case CK_Right:
3123 res = move_right (panel);
3124 break;
3125 case CK_Bottom:
3126 move_end (panel);
3127 break;
3128 case CK_Top:
3129 move_home (panel);
3130 break;
3131 #ifdef HAVE_CHARSET
3132 case CK_SelectCodepage:
3133 panel_change_encoding (panel);
3134 break;
3135 #endif
3136 case CK_Search:
3137 start_search (panel);
3138 break;
3139 case CK_SearchStop:
3140 break;
3141 case CK_PanelOtherSync:
3142 panel_sync_other (panel);
3143 break;
3144 case CK_Sort:
3145 panel_select_sort_order (panel);
3146 break;
3147 case CK_SortPrev:
3148 panel_toggle_sort_order_prev (panel);
3149 break;
3150 case CK_SortNext:
3151 panel_toggle_sort_order_next (panel);
3152 break;
3153 case CK_SortReverse:
3154 panel->sort_info.reverse = !panel->sort_info.reverse;
3155 panel_set_sort_order (panel, panel->sort_info.sort_field);
3156 break;
3157 case CK_SortByName:
3158 panel_set_sort_type_by_id (panel, "name");
3159 break;
3160 case CK_SortByExt:
3161 panel_set_sort_type_by_id (panel, "extension");
3162 break;
3163 case CK_SortBySize:
3164 panel_set_sort_type_by_id (panel, "size");
3165 break;
3166 case CK_SortByMTime:
3167 panel_set_sort_type_by_id (panel, "mtime");
3168 break;
3170 return res;
3173 /* --------------------------------------------------------------------------------------------- */
3175 static cb_ret_t
3176 panel_key (WPanel * panel, int key)
3178 size_t i;
3180 if (is_abort_char (key))
3182 stop_search (panel);
3183 return MSG_HANDLED;
3186 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3188 do_search (panel, key);
3189 return MSG_HANDLED;
3192 for (i = 0; panel_map[i].key != 0; i++)
3193 if (key == panel_map[i].key)
3194 return panel_execute_cmd (panel, panel_map[i].command);
3196 if (panels_options.torben_fj_mode && key == ALT ('h'))
3198 goto_middle_file (panel);
3199 return MSG_HANDLED;
3202 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3204 start_search (panel);
3205 do_search (panel, key);
3206 return MSG_HANDLED;
3209 return MSG_NOT_HANDLED;
3212 /* --------------------------------------------------------------------------------------------- */
3214 static cb_ret_t
3215 panel_callback (Widget * w, widget_msg_t msg, int parm)
3217 WPanel *panel = (WPanel *) w;
3218 WButtonBar *bb;
3220 switch (msg)
3222 case WIDGET_INIT:
3223 /* subscribe to "history_load" event */
3224 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3225 /* subscribe to "history_save" event */
3226 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3227 return MSG_HANDLED;
3229 case WIDGET_DRAW:
3230 /* Repaint everything, including frame and separator */
3231 paint_frame (panel); /* including show_dir */
3232 paint_dir (panel);
3233 mini_info_separator (panel);
3234 display_mini_info (panel);
3235 panel->dirty = 0;
3236 return MSG_HANDLED;
3238 case WIDGET_FOCUS:
3239 state_mark = -1;
3240 current_panel = panel;
3241 panel->active = 1;
3242 if (mc_chdir (panel->cwd_vpath) != 0)
3244 char *cwd;
3246 cwd = strip_password (vfs_path_to_str (panel->cwd_vpath), 1);
3247 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3248 cwd, unix_error_string (errno));
3249 g_free (cwd);
3251 else
3252 subshell_chdir (panel->cwd_vpath);
3254 update_xterm_title_path ();
3255 select_item (panel);
3256 show_dir (panel);
3257 paint_dir (panel);
3258 panel->dirty = 0;
3260 bb = find_buttonbar (panel->widget.owner);
3261 midnight_set_buttonbar (bb);
3262 buttonbar_redraw (bb);
3263 return MSG_HANDLED;
3265 case WIDGET_UNFOCUS:
3266 /* Janne: look at this for the multiple panel options */
3267 stop_search (panel);
3268 panel->active = 0;
3269 show_dir (panel);
3270 unselect_item (panel);
3271 return MSG_HANDLED;
3273 case WIDGET_KEY:
3274 return panel_key (panel, parm);
3276 case WIDGET_COMMAND:
3277 return panel_execute_cmd (panel, parm);
3279 case WIDGET_DESTROY:
3280 /* unsubscribe from "history_load" event */
3281 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3282 /* unsubscribe from "history_save" event */
3283 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3284 panel_destroy (panel);
3285 free_my_statfs ();
3286 return MSG_HANDLED;
3288 default:
3289 return default_proc (msg, parm);
3293 /* --------------------------------------------------------------------------------------------- */
3294 /* */
3295 /* Panel mouse events support routines */
3296 /* */
3298 static void
3299 mouse_toggle_mark (WPanel * panel)
3301 do_mark_file (panel, MARK_DONT_MOVE);
3302 mouse_marking = selection (panel)->f.marked;
3303 mouse_mark_panel = current_panel;
3306 /* --------------------------------------------------------------------------------------------- */
3308 static void
3309 mouse_set_mark (WPanel * panel)
3312 if (mouse_mark_panel == panel)
3314 if (mouse_marking && !(selection (panel)->f.marked))
3315 do_mark_file (panel, MARK_DONT_MOVE);
3316 else if (!mouse_marking && (selection (panel)->f.marked))
3317 do_mark_file (panel, MARK_DONT_MOVE);
3321 /* --------------------------------------------------------------------------------------------- */
3323 static int
3324 mark_if_marking (WPanel * panel, Gpm_Event * event)
3326 if (event->buttons & GPM_B_RIGHT)
3328 if (event->type & GPM_DOWN)
3329 mouse_toggle_mark (panel);
3330 else
3331 mouse_set_mark (panel);
3332 return 1;
3334 return 0;
3337 /* --------------------------------------------------------------------------------------------- */
3338 /** Determine which column was clicked, and sort the panel on
3339 * that column, or reverse sort on that column if already
3340 * sorted on that column.
3343 static void
3344 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3346 int i;
3347 const char *lc_sort_name = NULL;
3348 panel_field_t *col_sort_format = NULL;
3349 format_e *format;
3350 gchar *title;
3352 for (i = 0, format = panel->format; format != NULL; format = format->next)
3354 i += format->field_len;
3355 if (event->x < i + 1)
3357 /* found column */
3358 lc_sort_name = format->title;
3359 break;
3363 if (lc_sort_name == NULL)
3364 return;
3366 for (i = 0; panel_fields[i].id != NULL; i++)
3368 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3369 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3371 col_sort_format = &panel_fields[i];
3372 g_free (title);
3373 break;
3375 g_free (title);
3378 if (col_sort_format == NULL)
3379 return;
3381 if (panel->sort_info.sort_field == col_sort_format)
3383 /* reverse the sort if clicked column is already the sorted column */
3384 panel->sort_info.reverse = !panel->sort_info.reverse;
3386 else
3388 /* new sort is forced to be ascending */
3389 panel->sort_info.reverse = FALSE;
3391 panel_set_sort_order (panel, col_sort_format);
3395 /* --------------------------------------------------------------------------------------------- */
3397 * Mouse callback of the panel minus repainting.
3398 * If the event is redirected to the menu, *redir is set to TRUE.
3400 static int
3401 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3403 const int lines = llines (panel);
3404 const gboolean is_active = dlg_widget_active (panel);
3405 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3407 *redir = FALSE;
3409 /* 1st line */
3410 if (mouse_down && event->y == 1)
3412 /* "<" button */
3413 if (event->x == 2)
3415 directory_history_prev (panel);
3416 return MOU_NORMAL;
3419 /* "." button show/hide hidden files */
3420 if (event->x == panel->widget.cols - 5)
3422 panel->widget.owner->callback (panel->widget.owner, NULL,
3423 DLG_ACTION, CK_ShowHidden, NULL);
3424 repaint_screen ();
3425 return MOU_NORMAL;
3428 /* ">" button */
3429 if (event->x == panel->widget.cols - 1)
3431 directory_history_next (panel);
3432 return MOU_NORMAL;
3435 /* "^" button */
3436 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3438 directory_history_list (panel);
3439 return MOU_NORMAL;
3442 /* rest of the upper frame, the menu is invisible - call menu */
3443 if (!menubar_visible)
3445 *redir = TRUE;
3446 event->x += panel->widget.x;
3447 return the_menubar->widget.mouse (event, the_menubar);
3450 /* no other events on 1st line */
3451 return MOU_NORMAL;
3454 /* sort on clicked column; don't handle wheel events */
3455 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3457 mouse_sort_col (event, panel);
3458 return MOU_NORMAL;
3461 /* Mouse wheel events */
3462 if (mouse_down && (event->buttons & GPM_B_UP))
3464 if (is_active)
3466 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3467 prev_page (panel);
3468 else /* We are in first page */
3469 move_up (panel);
3471 return MOU_NORMAL;
3474 if (mouse_down && (event->buttons & GPM_B_DOWN))
3476 if (is_active)
3478 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3479 next_page (panel);
3480 else /* We are in last page */
3481 move_down (panel);
3483 return MOU_NORMAL;
3486 event->y -= 2;
3487 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3489 int my_index;
3491 if (!is_active)
3492 change_panel ();
3494 if (panel->top_file + event->y > panel->count)
3495 my_index = panel->count - 1;
3496 else
3498 my_index = panel->top_file + event->y - 1;
3499 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3500 my_index += llines (panel);
3502 if (my_index >= panel->count)
3503 my_index = panel->count - 1;
3506 if (my_index != panel->selected)
3508 unselect_item (panel);
3509 panel->selected = my_index;
3510 select_item (panel);
3513 /* This one is new */
3514 mark_if_marking (panel, event);
3516 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3518 if (event->y > 0 && event->y <= lines)
3519 do_enter (panel);
3521 return MOU_NORMAL;
3524 /* --------------------------------------------------------------------------------------------- */
3525 /** Mouse callback of the panel */
3527 static int
3528 panel_event (Gpm_Event * event, void *data)
3530 WPanel *panel = data;
3531 int ret;
3532 gboolean redir;
3534 ret = do_panel_event (event, panel, &redir);
3535 if (!redir)
3536 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3538 return ret;
3541 /* --------------------------------------------------------------------------------------------- */
3543 static void
3544 reload_panelized (WPanel * panel)
3546 int i, j;
3547 dir_list *list = &panel->dir;
3549 if (panel != current_panel)
3550 (void) mc_chdir (panel->cwd_vpath);
3552 for (i = 0, j = 0; i < panel->count; i++)
3554 vfs_path_t *vpath;
3556 if (list->list[i].f.marked)
3558 /* Unmark the file in advance. In case the following mc_lstat
3559 * fails we are done, else we have to mark the file again
3560 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3561 * IMO that's the best way to update the panel's summary status
3562 * -- Norbert
3564 do_file_mark (panel, i, 0);
3566 vpath = vfs_path_from_str (list->list[i].fname);
3567 if (mc_lstat (vpath, &list->list[i].st))
3568 g_free (list->list[i].fname);
3569 else
3571 if (list->list[i].f.marked)
3572 do_file_mark (panel, i, 1);
3573 if (j != i)
3574 list->list[j] = list->list[i];
3575 j++;
3577 vfs_path_free (vpath);
3579 if (j == 0)
3580 panel->count = set_zero_dir (list) ? 1 : 0;
3581 else
3582 panel->count = j;
3584 if (panel != current_panel)
3585 (void) mc_chdir (current_panel->cwd_vpath);
3588 /* --------------------------------------------------------------------------------------------- */
3590 static void
3591 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3593 gboolean free_pointer;
3594 char *my_current_file = NULL;
3596 if ((flags & UP_RELOAD) != 0)
3598 panel->is_panelized = FALSE;
3599 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3600 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3603 /* If current_file == -1 (an invalid pointer) then preserve selection */
3604 free_pointer = current_file == UP_KEEPSEL;
3606 if (free_pointer)
3608 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3609 current_file = my_current_file;
3612 if (panel->is_panelized)
3613 reload_panelized (panel);
3614 else
3615 panel_reload (panel);
3617 try_to_select (panel, current_file);
3618 panel->dirty = 1;
3620 if (free_pointer)
3621 g_free (my_current_file);
3624 /* --------------------------------------------------------------------------------------------- */
3626 static void
3627 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3629 if (get_display_type (which) == view_listing)
3631 WPanel *panel;
3633 panel = (WPanel *) get_panel_widget (which);
3634 if (panel->is_panelized)
3635 flags &= ~UP_RELOAD;
3636 update_one_panel_widget (panel, flags, current_file);
3640 /* --------------------------------------------------------------------------------------------- */
3641 /*** public functions ****************************************************************************/
3642 /* --------------------------------------------------------------------------------------------- */
3644 char *
3645 remove_encoding_from_path (const vfs_path_t * vpath)
3647 GString *ret;
3648 GString *tmp_conv;
3649 int indx;
3651 ret = g_string_new ("");
3652 tmp_conv = g_string_new ("");
3654 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
3656 vfs_path_element_t *path_element;
3657 GIConv converter;
3659 path_element = vfs_path_get_by_index (vpath, indx);
3660 converter =
3661 path_element->encoding != NULL ?
3662 str_crt_conv_to (path_element->encoding) : str_cnv_to_term;
3663 if (converter == INVALID_CONV)
3664 converter = str_cnv_to_term;
3666 str_vfs_convert_from (converter, path_element->path, tmp_conv);
3667 g_string_append (ret, tmp_conv->str);
3668 g_string_set_size (tmp_conv, 0);
3669 str_close_conv (converter);
3671 g_string_free (tmp_conv, TRUE);
3672 return g_string_free (ret, FALSE);
3675 /* --------------------------------------------------------------------------------------------- */
3677 static void
3678 do_select (WPanel * panel, int i)
3680 if (i != panel->selected)
3682 panel->dirty = 1;
3683 panel->selected = i;
3684 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3685 if (panel->top_file < 0)
3686 panel->top_file = 0;
3690 /* --------------------------------------------------------------------------------------------- */
3692 static void
3693 do_try_to_select (WPanel * panel, const char *name)
3695 int i;
3696 char *subdir;
3698 if (!name)
3700 do_select (panel, 0);
3701 return;
3704 /* We only want the last component of the directory,
3705 * and from this only the name without suffix. */
3706 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3708 /* Search that subdirectory, if found select it */
3709 for (i = 0; i < panel->count; i++)
3711 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3713 do_select (panel, i);
3714 g_free (subdir);
3715 return;
3719 /* Try to select a file near the file that is missing */
3720 if (panel->selected >= panel->count)
3721 do_select (panel, panel->count - 1);
3722 g_free (subdir);
3725 /* --------------------------------------------------------------------------------------------- */
3727 /* event callback */
3728 static gboolean
3729 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3730 gpointer init_data, gpointer data)
3732 (void) event_group_name;
3733 (void) event_name;
3734 (void) init_data;
3735 (void) data;
3737 update_panels (UP_RELOAD, UP_KEEPSEL);
3739 return TRUE;
3742 /* --------------------------------------------------------------------------------------------- */
3744 /* event callback */
3745 static gboolean
3746 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3747 gpointer init_data, gpointer data)
3749 (void) event_group_name;
3750 (void) event_name;
3751 (void) init_data;
3752 (void) data;
3754 if (current_panel->marked == 0)
3755 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3756 (gpointer) selection (current_panel)->fname);
3757 else
3759 int i;
3760 gboolean first = TRUE;
3761 char *flist = NULL;
3763 for (i = 0; i < current_panel->count; i++)
3764 if (current_panel->dir.list[i].f.marked != 0)
3765 { /* Skip the unmarked ones */
3766 if (first)
3768 flist = g_strdup (current_panel->dir.list[i].fname);
3769 first = FALSE;
3771 else
3773 /* Add empty lines after the file */
3774 char *tmp;
3776 tmp =
3777 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3778 g_free (flist);
3779 flist = tmp;
3783 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3784 g_free (flist);
3786 return TRUE;
3789 /* --------------------------------------------------------------------------------------------- */
3790 /*** public functions ****************************************************************************/
3791 /* --------------------------------------------------------------------------------------------- */
3793 void
3794 try_to_select (WPanel * panel, const char *name)
3796 do_try_to_select (panel, name);
3797 select_item (panel);
3800 /* --------------------------------------------------------------------------------------------- */
3802 void
3803 panel_clean_dir (WPanel * panel)
3805 int count = panel->count;
3807 panel->count = 0;
3808 panel->top_file = 0;
3809 panel->selected = 0;
3810 panel->marked = 0;
3811 panel->dirs_marked = 0;
3812 panel->total = 0;
3813 panel->searching = FALSE;
3814 panel->is_panelized = FALSE;
3815 panel->dirty = 1;
3817 clean_dir (&panel->dir, count);
3820 /* --------------------------------------------------------------------------------------------- */
3822 * Set Up panel's current dir object
3824 * @param panel panel object
3825 * @param path_str string contain path
3828 void
3829 panel_set_cwd (WPanel * panel, const char *path_str)
3831 vfs_path_free (panel->cwd_vpath);
3832 panel->cwd_vpath = vfs_path_from_str (path_str);
3835 /* --------------------------------------------------------------------------------------------- */
3837 * Set Up panel's last working dir object
3839 * @param panel panel object
3840 * @param path_str string contain path
3843 void
3844 panel_set_lwd (WPanel * panel, const char *path_str)
3846 vfs_path_free (panel->lwd_vpath);
3847 panel->lwd_vpath = vfs_path_from_str (path_str);
3850 /* --------------------------------------------------------------------------------------------- */
3851 /** Panel creation.
3852 * @param panel_name the name of the panel for setup retieving
3853 * @returns new instance of WPanel
3856 WPanel *
3857 panel_new (const char *panel_name)
3859 return panel_new_with_dir (panel_name, NULL);
3862 /* --------------------------------------------------------------------------------------------- */
3863 /** Panel creation for specified directory.
3864 * @param panel_name specifies the name of the panel for setup retieving
3865 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3866 * @returns new instance of WPanel
3869 WPanel *
3870 panel_new_with_dir (const char *panel_name, const char *wpath)
3872 WPanel *panel;
3873 char *section;
3874 int i, err;
3875 char *curdir = NULL;
3877 panel = g_new0 (WPanel, 1);
3879 /* No know sizes of the panel at startup */
3880 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3882 /* We do not want the cursor */
3883 widget_want_cursor (panel->widget, 0);
3885 if (wpath != NULL)
3887 curdir = _vfs_get_cwd ();
3888 panel_set_cwd (panel, wpath);
3890 else
3892 vfs_setup_cwd ();
3893 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3896 panel_set_lwd (panel, ".");
3898 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3899 /* directories history will be get later */
3901 panel->dir.list = g_new (file_entry, MIN_FILES);
3902 panel->dir.size = MIN_FILES;
3903 panel->active = 0;
3904 panel->filter = 0;
3905 panel->split = 0;
3906 panel->top_file = 0;
3907 panel->selected = 0;
3908 panel->marked = 0;
3909 panel->total = 0;
3910 panel->dirty = 1;
3911 panel->searching = FALSE;
3912 panel->dirs_marked = 0;
3913 panel->is_panelized = FALSE;
3914 panel->format = 0;
3915 panel->status_format = 0;
3916 panel->format_modified = 1;
3918 panel->panel_name = g_strdup (panel_name);
3919 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3921 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3923 for (i = 0; i < LIST_TYPES; i++)
3924 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3926 panel->search_buffer[0] = '\0';
3927 panel->prev_search_buffer[0] = '\0';
3928 panel->frame_size = frame_half;
3930 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3931 if (!mc_config_has_group (mc_main_config, section))
3933 g_free (section);
3934 section = g_strdup (panel->panel_name);
3936 panel_load_setup (panel, section);
3937 g_free (section);
3939 /* Load format strings */
3940 err = set_panel_formats (panel);
3941 if (err != 0)
3942 set_panel_formats (panel);
3944 #ifdef HAVE_CHARSET
3946 vfs_path_element_t *path_element;
3948 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
3949 if (path_element->encoding != NULL)
3950 panel->codepage = get_codepage_index (path_element->encoding);
3952 #endif
3954 if (mc_chdir (panel->cwd_vpath) != 0)
3956 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3957 vfs_setup_cwd ();
3958 vfs_path_free (panel->cwd_vpath);
3959 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3962 /* Load the default format */
3964 char *tmp_path;
3966 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3967 panel->count =
3968 do_load_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
3969 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3970 panel->sort_info.exec_first, panel->filter);
3971 g_free (tmp_path);
3974 /* Restore old right path */
3975 if (curdir != NULL)
3977 vfs_path_t *vpath;
3979 vpath = vfs_path_from_str (curdir);
3980 err = mc_chdir (vpath);
3981 vfs_path_free (vpath);
3983 g_free (curdir);
3985 return panel;
3988 /* --------------------------------------------------------------------------------------------- */
3990 void
3991 panel_reload (WPanel * panel)
3993 struct stat current_stat;
3994 char *tmp_path;
3995 gboolean ok;
3997 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3998 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
3999 && current_stat.st_ctime == panel->dir_stat.st_ctime
4000 && current_stat.st_mtime == panel->dir_stat.st_mtime);
4001 g_free (tmp_path);
4003 if (ok)
4004 return;
4008 char *last_slash;
4010 if (mc_chdir (panel->cwd_vpath) != -1)
4011 break;
4013 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4014 if (tmp_path[0] == PATH_SEP && tmp_path[1] == 0)
4016 panel_clean_dir (panel);
4017 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
4018 g_free (tmp_path);
4019 return;
4021 last_slash = strrchr (tmp_path, PATH_SEP);
4022 if (!last_slash || last_slash == tmp_path)
4024 vfs_path_free (panel->cwd_vpath);
4025 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4027 else
4028 *last_slash = 0;
4029 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4030 g_free (tmp_path);
4031 show_dir (panel);
4033 while (TRUE);
4035 panel->count =
4036 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4037 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4038 panel->sort_info.exec_first, panel->filter);
4040 panel->dirty = 1;
4041 if (panel->selected >= panel->count)
4042 do_select (panel, panel->count - 1);
4044 recalculate_panel_summary (panel);
4047 /* --------------------------------------------------------------------------------------------- */
4048 /* Switches the panel to the mode specified in the format */
4049 /* Seting up both format and status string. Return: 0 - on success; */
4050 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4053 set_panel_formats (WPanel * p)
4055 format_e *form;
4056 char *err = NULL;
4057 int retcode = 0;
4059 form = use_display_format (p, panel_format (p), &err, 0);
4061 if (err != NULL)
4063 g_free (err);
4064 retcode = 1;
4066 else
4068 delete_format (p->format);
4069 p->format = form;
4072 if (panels_options.show_mini_info)
4074 form = use_display_format (p, mini_status_format (p), &err, 1);
4076 if (err != NULL)
4078 g_free (err);
4079 retcode += 2;
4081 else
4083 delete_format (p->status_format);
4084 p->status_format = form;
4088 panel_format_modified (p);
4089 panel_update_cols (&(p->widget), p->frame_size);
4091 if (retcode)
4092 message (D_ERROR, _("Warning"),
4093 _("User supplied format looks invalid, reverting to default."));
4094 if (retcode & 0x01)
4096 g_free (p->user_format);
4097 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4099 if (retcode & 0x02)
4101 g_free (p->user_status_format[p->list_type]);
4102 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4105 return retcode;
4108 /* --------------------------------------------------------------------------------------------- */
4110 void
4111 panel_update_cols (Widget * widget, panel_display_t frame_size)
4113 int cols, origin;
4115 /* don't touch panel if it is not in dialog yet */
4116 /* if panel is not in dialog it is not in widgets list
4117 and cannot be compared with get_panel_widget() result */
4118 if (widget->owner == NULL)
4119 return;
4121 if (panels_layout.horizontal_split)
4123 widget->cols = COLS;
4124 return;
4127 if (frame_size == frame_full)
4129 cols = COLS;
4130 origin = 0;
4132 else if (widget == get_panel_widget (0))
4134 cols = panels_layout.left_panel_size;
4135 origin = 0;
4137 else
4139 cols = COLS - panels_layout.left_panel_size;
4140 origin = panels_layout.left_panel_size;
4143 widget->cols = cols;
4144 widget->x = origin;
4147 /* --------------------------------------------------------------------------------------------- */
4149 /* Select current item and readjust the panel */
4150 void
4151 select_item (WPanel * panel)
4153 /* Although currently all over the code we set the selection and
4154 top file to decent values before calling select_item, I could
4155 forget it someday, so it's better to do the actual fitting here */
4157 if (panel->selected < 0)
4158 panel->selected = 0;
4160 if (panel->selected > panel->count - 1)
4161 panel->selected = panel->count - 1;
4163 adjust_top_file (panel);
4165 panel->dirty = 1;
4167 execute_hooks (select_file_hook);
4170 /* --------------------------------------------------------------------------------------------- */
4171 /** Clears all files in the panel, used only when one file was marked */
4172 void
4173 unmark_files (WPanel * panel)
4175 int i;
4177 if (!panel->marked)
4178 return;
4179 for (i = 0; i < panel->count; i++)
4180 file_mark (panel, i, 0);
4182 panel->dirs_marked = 0;
4183 panel->marked = 0;
4184 panel->total = 0;
4187 /* --------------------------------------------------------------------------------------------- */
4188 /** Recalculate the panels summary information, used e.g. when marked
4189 files might have been removed by an external command */
4191 void
4192 recalculate_panel_summary (WPanel * panel)
4194 int i;
4196 panel->marked = 0;
4197 panel->dirs_marked = 0;
4198 panel->total = 0;
4200 for (i = 0; i < panel->count; i++)
4201 if (panel->dir.list[i].f.marked)
4203 /* do_file_mark will return immediately if newmark == oldmark.
4204 So we have to first unmark it to get panel's summary information
4205 updated. (Norbert) */
4206 panel->dir.list[i].f.marked = 0;
4207 do_file_mark (panel, i, 1);
4211 /* --------------------------------------------------------------------------------------------- */
4212 /** This routine marks a file or a directory */
4214 void
4215 do_file_mark (WPanel * panel, int idx, int mark)
4217 if (panel->dir.list[idx].f.marked == mark)
4218 return;
4220 /* Only '..' can't be marked, '.' isn't visible */
4221 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4222 return;
4224 file_mark (panel, idx, mark);
4225 if (panel->dir.list[idx].f.marked)
4227 panel->marked++;
4228 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4230 if (panel->dir.list[idx].f.dir_size_computed)
4231 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4232 panel->dirs_marked++;
4234 else
4235 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4236 set_colors (panel);
4238 else
4240 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4242 if (panel->dir.list[idx].f.dir_size_computed)
4243 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4244 panel->dirs_marked--;
4246 else
4247 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4248 panel->marked--;
4252 /* --------------------------------------------------------------------------------------------- */
4254 * Changes the current directory of the panel.
4255 * Record change in the directory history.
4257 gboolean
4258 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4260 gboolean r;
4262 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4263 if (r)
4265 char *tmp_path;
4267 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4268 directory_history_add (panel, tmp_path);
4269 g_free (tmp_path);
4271 return r;
4274 /* --------------------------------------------------------------------------------------------- */
4276 void
4277 file_mark (WPanel * panel, int lc_index, int val)
4279 if (panel->dir.list[lc_index].f.marked != val)
4281 panel->dir.list[lc_index].f.marked = val;
4282 panel->dirty = 1;
4286 /* --------------------------------------------------------------------------------------------- */
4288 void
4289 panel_re_sort (WPanel * panel)
4291 char *filename;
4292 int i;
4294 if (panel == NULL)
4295 return;
4297 filename = g_strdup (selection (panel)->fname);
4298 unselect_item (panel);
4299 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4300 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4301 panel->sort_info.exec_first);
4302 panel->selected = -1;
4303 for (i = panel->count; i; i--)
4305 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4307 panel->selected = i - 1;
4308 break;
4311 g_free (filename);
4312 panel->top_file = panel->selected - ITEMS (panel) / 2;
4313 select_item (panel);
4314 panel->dirty = 1;
4317 /* --------------------------------------------------------------------------------------------- */
4319 void
4320 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4322 if (sort_order == NULL)
4323 return;
4325 panel->sort_info.sort_field = sort_order;
4327 /* The directory is already sorted, we have to load the unsorted stuff */
4328 if (sort_order->sort_routine == (sortfn *) unsorted)
4330 char *current_file;
4332 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4333 panel_reload (panel);
4334 try_to_select (panel, current_file);
4335 g_free (current_file);
4337 panel_re_sort (panel);
4340 /* --------------------------------------------------------------------------------------------- */
4342 * Change panel encoding.
4343 * @param panel WPanel object
4346 void
4347 panel_change_encoding (WPanel * panel)
4349 const char *encoding = NULL;
4350 char *cd_path;
4351 #ifdef HAVE_CHARSET
4352 char *errmsg;
4353 int r;
4355 r = select_charset (-1, -1, panel->codepage, FALSE);
4357 if (r == SELECT_CHARSET_CANCEL)
4358 return; /* Cancel */
4360 panel->codepage = r;
4362 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4364 /* No translation */
4365 vfs_path_t *cd_path_vpath;
4367 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4368 cd_path = remove_encoding_from_path (panel->cwd_vpath);
4369 cd_path_vpath = vfs_path_from_str (cd_path);
4370 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4371 show_dir (panel);
4372 vfs_path_free (cd_path_vpath);
4373 g_free (cd_path);
4374 return;
4377 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4378 if (errmsg != NULL)
4380 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4381 g_free (errmsg);
4382 return;
4385 encoding = get_codepage_id (panel->codepage);
4386 #endif
4387 if (encoding != NULL)
4389 vfs_change_encoding (panel->cwd_vpath, encoding);
4391 cd_path = vfs_path_to_str (panel->cwd_vpath);
4392 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4393 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4394 g_free (cd_path);
4398 /* --------------------------------------------------------------------------------------------- */
4400 * This routine reloads the directory in both panels. It tries to
4401 * select current_file in current_panel and other_file in other_panel.
4402 * If current_file == -1 then it automatically sets current_file and
4403 * other_file to the currently selected files in the panels.
4405 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4406 * will not reload the other panel.
4409 void
4410 update_panels (panel_update_flags_t flags, const char *current_file)
4412 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4413 WPanel *panel;
4415 update_one_panel (get_current_index (), flags, current_file);
4416 if (reload_other)
4417 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4419 if (get_current_type () == view_listing)
4420 panel = (WPanel *) get_panel_widget (get_current_index ());
4421 else
4422 panel = (WPanel *) get_panel_widget (get_other_index ());
4424 if (!panel->is_panelized)
4425 (void) mc_chdir (panel->cwd_vpath);
4428 /* --------------------------------------------------------------------------------------------- */
4430 void
4431 directory_history_add (struct WPanel *panel, const char *dir)
4433 char *tmp;
4435 tmp = g_strdup (dir);
4436 strip_password (tmp, 1);
4438 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4441 /* --------------------------------------------------------------------------------------------- */
4443 gsize
4444 panel_get_num_of_sortable_fields (void)
4446 gsize ret = 0, lc_index;
4448 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4449 if (panel_fields[lc_index].is_user_choice)
4450 ret++;
4451 return ret;
4454 /* --------------------------------------------------------------------------------------------- */
4456 const char **
4457 panel_get_sortable_fields (gsize * array_size)
4459 char **ret;
4460 gsize lc_index, i;
4462 lc_index = panel_get_num_of_sortable_fields ();
4464 ret = g_try_new0 (char *, lc_index + 1);
4465 if (ret == NULL)
4466 return NULL;
4468 if (array_size != NULL)
4469 *array_size = lc_index;
4471 lc_index = 0;
4473 for (i = 0; panel_fields[i].id != NULL; i++)
4474 if (panel_fields[i].is_user_choice)
4475 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4476 return (const char **) ret;
4479 /* --------------------------------------------------------------------------------------------- */
4481 const panel_field_t *
4482 panel_get_field_by_id (const char *name)
4484 gsize lc_index;
4485 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4486 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4487 return &panel_fields[lc_index];
4488 return NULL;
4491 /* --------------------------------------------------------------------------------------------- */
4493 const panel_field_t *
4494 panel_get_field_by_title_hotkey (const char *name)
4496 gsize lc_index;
4497 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4498 if (panel_fields[lc_index].title_hotkey != NULL &&
4499 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4500 return &panel_fields[lc_index];
4501 return NULL;
4504 /* --------------------------------------------------------------------------------------------- */
4506 const panel_field_t *
4507 panel_get_field_by_title (const char *name)
4509 gsize lc_index;
4510 gchar *title = NULL;
4512 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4514 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4515 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4517 g_free (title);
4518 return &panel_fields[lc_index];
4521 g_free (title);
4522 return NULL;
4525 /* --------------------------------------------------------------------------------------------- */
4527 gsize
4528 panel_get_num_of_user_possible_fields (void)
4530 gsize ret = 0, lc_index;
4532 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4533 if (panel_fields[lc_index].use_in_user_format)
4534 ret++;
4535 return ret;
4538 /* --------------------------------------------------------------------------------------------- */
4540 const char **
4541 panel_get_user_possible_fields (gsize * array_size)
4543 char **ret;
4544 gsize lc_index, i;
4546 lc_index = panel_get_num_of_user_possible_fields ();
4548 ret = g_try_new0 (char *, lc_index + 1);
4549 if (ret == NULL)
4550 return NULL;
4552 if (array_size != NULL)
4553 *array_size = lc_index;
4555 lc_index = 0;
4557 for (i = 0; panel_fields[i].id != NULL; i++)
4558 if (panel_fields[i].use_in_user_format)
4559 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4560 return (const char **) ret;
4563 /* --------------------------------------------------------------------------------------------- */
4565 void
4566 panel_init (void)
4568 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4569 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4571 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4572 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4573 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4574 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4575 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4577 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4578 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4579 panel_save_curent_file_to_clip_file, NULL, NULL);
4583 /* --------------------------------------------------------------------------------------------- */
4585 void
4586 panel_deinit (void)
4588 g_free (panel_sort_up_sign);
4589 g_free (panel_sort_down_sign);
4591 g_free (panel_hiddenfiles_sign_show);
4592 g_free (panel_hiddenfiles_sign_hide);
4593 g_free (panel_history_prev_item_sign);
4594 g_free (panel_history_next_item_sign);
4595 g_free (panel_history_show_list_sign);
4599 /* --------------------------------------------------------------------------------------------- */