Indent files.
[midnight-commander.git] / src / filemanager / panel.c
blobfc53d038fed83ae2c073f9271078ab038e24b5df
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 #ifdef HAVE_CHARSET
62 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
63 #endif
64 #include "src/keybind-defaults.h" /* global_keymap_t */
65 #include "src/subshell.h" /* do_subshell_chdir() */
67 #include "dir.h"
68 #include "boxes.h"
69 #include "tree.h"
70 #include "ext.h" /* regexp_command */
71 #include "layout.h" /* Most layout variables are here */
72 #include "cmd.h"
73 #include "command.h" /* cmdline */
74 #include "usermenu.h"
75 #include "midnight.h"
76 #include "mountlist.h" /* my_statfs */
78 #include "panel.h"
80 /*** global variables ****************************************************************************/
82 /* The hook list for the select file function */
83 hook_t *select_file_hook = NULL;
85 /* *INDENT-OFF* */
86 panelized_panel_t panelized_panel = { {NULL, 0}, -1, NULL };
87 /* *INDENT-ON* */
89 static const char *string_file_name (file_entry *, int);
90 static const char *string_file_size (file_entry *, int);
91 static const char *string_file_size_brief (file_entry *, int);
92 static const char *string_file_type (file_entry *, int);
93 static const char *string_file_mtime (file_entry *, int);
94 static const char *string_file_atime (file_entry *, int);
95 static const char *string_file_ctime (file_entry *, int);
96 static const char *string_file_permission (file_entry *, int);
97 static const char *string_file_perm_octal (file_entry *, int);
98 static const char *string_file_nlinks (file_entry *, int);
99 static const char *string_inode (file_entry *, int);
100 static const char *string_file_nuid (file_entry *, int);
101 static const char *string_file_ngid (file_entry *, int);
102 static const char *string_file_owner (file_entry *, int);
103 static const char *string_file_group (file_entry *, int);
104 static const char *string_marked (file_entry *, int);
105 static const char *string_space (file_entry *, int);
106 static const char *string_dot (file_entry *, int);
108 /* *INDENT-OFF* */
109 panel_field_t panel_fields[] = {
111 "unsorted", 12, 1, J_LEFT_FIT,
112 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
113 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
114 N_("sort|u"),
115 N_("&Unsorted"), TRUE, FALSE,
116 string_file_name,
117 (sortfn *) unsorted
121 "name", 12, 1, J_LEFT_FIT,
122 /* TRANSLATORS: one single character to represent 'name' sort mode */
123 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
124 N_("sort|n"),
125 N_("&Name"), TRUE, TRUE,
126 string_file_name,
127 (sortfn *) sort_name
131 "version", 12, 1, J_LEFT_FIT,
132 /* TRANSLATORS: one single character to represent 'version' sort mode */
133 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
134 N_("sort|v"),
135 N_("&Version"), TRUE, FALSE,
136 string_file_name,
137 (sortfn *) sort_vers
141 "extension", 12, 1, J_LEFT_FIT,
142 /* TRANSLATORS: one single character to represent 'extension' sort mode */
143 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
144 N_("sort|e"),
145 N_("&Extension"), TRUE, FALSE,
146 string_file_name, /* TODO: string_file_ext */
147 (sortfn *) sort_ext
151 "size", 7, 0, J_RIGHT,
152 /* TRANSLATORS: one single character to represent 'size' sort mode */
153 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
154 N_("sort|s"),
155 N_("&Size"), TRUE, TRUE,
156 string_file_size,
157 (sortfn *) sort_size
161 "bsize", 7, 0, J_RIGHT,
163 N_("Block Size"), FALSE, FALSE,
164 string_file_size_brief,
165 (sortfn *) sort_size
169 "type", 1, 0, J_LEFT,
171 "", FALSE, TRUE,
172 string_file_type,
173 NULL
177 "mtime", 12, 0, J_RIGHT,
178 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
179 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
180 N_("sort|m"),
181 N_("&Modify time"), TRUE, TRUE,
182 string_file_mtime,
183 (sortfn *) sort_time
187 "atime", 12, 0, J_RIGHT,
188 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
189 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
190 N_("sort|a"),
191 N_("&Access time"), TRUE, TRUE,
192 string_file_atime,
193 (sortfn *) sort_atime
197 "ctime", 12, 0, J_RIGHT,
198 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
199 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
200 N_("sort|h"),
201 N_("C&hange time"), TRUE, TRUE,
202 string_file_ctime,
203 (sortfn *) sort_ctime
207 "perm", 10, 0, J_LEFT,
209 N_("Permission"), FALSE, TRUE,
210 string_file_permission,
211 NULL
215 "mode", 6, 0, J_RIGHT,
217 N_("Perm"), FALSE, TRUE,
218 string_file_perm_octal,
219 NULL
223 "nlink", 2, 0, J_RIGHT,
225 N_("Nl"), FALSE, TRUE,
226 string_file_nlinks, NULL
230 "inode", 5, 0, J_RIGHT,
231 /* TRANSLATORS: one single character to represent 'inode' sort mode */
232 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
233 N_("sort|i"),
234 N_("&Inode"), TRUE, TRUE,
235 string_inode,
236 (sortfn *) sort_inode
240 "nuid", 5, 0, J_RIGHT,
242 N_("UID"), FALSE, FALSE,
243 string_file_nuid,
244 NULL
248 "ngid", 5, 0, J_RIGHT,
250 N_("GID"), FALSE, FALSE,
251 string_file_ngid,
252 NULL
256 "owner", 8, 0, J_LEFT_FIT,
258 N_("Owner"), FALSE, TRUE,
259 string_file_owner,
260 NULL
264 "group", 8, 0, J_LEFT_FIT,
266 N_("Group"), FALSE, TRUE,
267 string_file_group,
268 NULL
272 "mark", 1, 0, J_RIGHT,
274 " ", FALSE, TRUE,
275 string_marked,
276 NULL
280 "|", 1, 0, J_RIGHT,
282 " ", FALSE, TRUE,
283 NULL,
284 NULL
288 "space", 1, 0, J_RIGHT,
290 " ", FALSE, TRUE,
291 string_space,
292 NULL
296 "dot", 1, 0, J_RIGHT,
298 " ", FALSE, FALSE,
299 string_dot,
300 NULL
304 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
307 /* *INDENT-ON* */
309 extern int saving_setup;
311 /*** file scope macro definitions ****************************************************************/
313 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
315 #define NORMAL 0
316 #define SELECTED 1
317 #define MARKED 2
318 #define MARKED_SELECTED 3
319 #define STATUS 5
321 /* This macro extracts the number of available lines in a panel */
322 #define llines(p) (p->widget.lines - 3 - (panels_options.show_mini_info ? 2 : 0))
324 /*** file scope type declarations ****************************************************************/
326 typedef enum
328 MARK_DONT_MOVE = 0,
329 MARK_DOWN = 1,
330 MARK_FORCE_DOWN = 2,
331 MARK_FORCE_UP = 3
332 } mark_act_t;
335 * This describes a format item. The parse_display_format routine parses
336 * the user specified format and creates a linked list of format_e structures.
338 typedef struct format_e
340 struct format_e *next;
341 int requested_field_len;
342 int field_len;
343 align_crt_t just_mode;
344 int expand;
345 const char *(*string_fn) (file_entry *, int len);
346 char *title;
347 const char *id;
348 } format_e;
350 /*** file scope variables ************************************************************************/
352 static char *panel_sort_up_sign = NULL;
353 static char *panel_sort_down_sign = NULL;
355 static char *panel_hiddenfiles_sign_show = NULL;
356 static char *panel_hiddenfiles_sign_hide = NULL;
357 static char *panel_history_prev_item_sign = NULL;
358 static char *panel_history_next_item_sign = NULL;
359 static char *panel_history_show_list_sign = NULL;
361 /* Panel that selection started */
362 static WPanel *mouse_mark_panel = NULL;
364 static int mouse_marking = 0;
365 static int state_mark = 0;
366 /*** file scope functions ************************************************************************/
367 /* --------------------------------------------------------------------------------------------- */
369 static void
370 set_colors (WPanel * panel)
372 (void) panel;
373 tty_set_normal_attrs ();
374 tty_setcolor (NORMAL_COLOR);
377 /* --------------------------------------------------------------------------------------------- */
378 /** Delete format string, it is a linked list */
380 static void
381 delete_format (format_e * format)
383 while (format != NULL)
385 format_e *next = format->next;
386 g_free (format->title);
387 g_free (format);
388 format = next;
392 /* --------------------------------------------------------------------------------------------- */
393 /** This code relies on the default justification!!! */
395 static void
396 add_permission_string (char *dest, int width, file_entry * fe, int attr, int color, int is_octal)
398 int i, r, l;
400 l = get_user_permissions (&fe->st);
402 if (is_octal)
404 /* Place of the access bit in octal mode */
405 l = width + l - 3;
406 r = l + 1;
408 else
410 /* The same to the triplet in string mode */
411 l = l * 3 + 1;
412 r = l + 3;
415 for (i = 0; i < width; i++)
417 if (i >= l && i < r)
419 if (attr == SELECTED || attr == MARKED_SELECTED)
420 tty_setcolor (MARKED_SELECTED_COLOR);
421 else
422 tty_setcolor (MARKED_COLOR);
424 else if (color >= 0)
425 tty_setcolor (color);
426 else
427 tty_lowlevel_setcolor (-color);
429 tty_print_char (dest[i]);
433 /* --------------------------------------------------------------------------------------------- */
434 /** String representations of various file attributes name */
436 static const char *
437 string_file_name (file_entry * fe, int len)
439 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
441 (void) len;
442 g_strlcpy (buffer, fe->fname, sizeof (buffer));
443 return buffer;
446 /* --------------------------------------------------------------------------------------------- */
448 static unsigned int
449 ilog10 (dev_t n)
451 unsigned int digits = 0;
454 digits++, n /= 10;
456 while (n != 0);
457 return digits;
460 /* --------------------------------------------------------------------------------------------- */
462 static void
463 format_device_number (char *buf, size_t bufsize, dev_t dev)
465 dev_t major_dev = major (dev);
466 dev_t minor_dev = minor (dev);
467 unsigned int major_digits = ilog10 (major_dev);
468 unsigned int minor_digits = ilog10 (minor_dev);
470 g_assert (bufsize >= 1);
471 if (major_digits + 1 + minor_digits + 1 <= bufsize)
473 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
475 else
477 g_strlcpy (buf, _("[dev]"), bufsize);
481 /* --------------------------------------------------------------------------------------------- */
482 /** size */
484 static const char *
485 string_file_size (file_entry * fe, int len)
487 static char buffer[BUF_TINY];
489 /* Don't ever show size of ".." since we don't calculate it */
490 if (!strcmp (fe->fname, ".."))
492 return _("UP--DIR");
495 #ifdef HAVE_STRUCT_STAT_ST_RDEV
496 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
497 format_device_number (buffer, len + 1, fe->st.st_rdev);
498 else
499 #endif
501 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
503 return buffer;
506 /* --------------------------------------------------------------------------------------------- */
507 /** bsize */
509 static const char *
510 string_file_size_brief (file_entry * fe, int len)
512 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
514 return _("SYMLINK");
517 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
519 return _("SUB-DIR");
522 return string_file_size (fe, len);
525 /* --------------------------------------------------------------------------------------------- */
526 /** This functions return a string representation of a file entry type */
528 static const char *
529 string_file_type (file_entry * fe, int len)
531 static char buffer[2];
533 (void) len;
534 if (S_ISDIR (fe->st.st_mode))
535 buffer[0] = PATH_SEP;
536 else if (S_ISLNK (fe->st.st_mode))
538 if (fe->f.link_to_dir)
539 buffer[0] = '~';
540 else if (fe->f.stale_link)
541 buffer[0] = '!';
542 else
543 buffer[0] = '@';
545 else if (S_ISCHR (fe->st.st_mode))
546 buffer[0] = '-';
547 else if (S_ISSOCK (fe->st.st_mode))
548 buffer[0] = '=';
549 else if (S_ISDOOR (fe->st.st_mode))
550 buffer[0] = '>';
551 else if (S_ISBLK (fe->st.st_mode))
552 buffer[0] = '+';
553 else if (S_ISFIFO (fe->st.st_mode))
554 buffer[0] = '|';
555 else if (S_ISNAM (fe->st.st_mode))
556 buffer[0] = '#';
557 else if (!S_ISREG (fe->st.st_mode))
558 buffer[0] = '?'; /* non-regular of unknown kind */
559 else if (is_exe (fe->st.st_mode))
560 buffer[0] = '*';
561 else
562 buffer[0] = ' ';
563 buffer[1] = '\0';
564 return buffer;
567 /* --------------------------------------------------------------------------------------------- */
568 /** mtime */
570 static const char *
571 string_file_mtime (file_entry * fe, int len)
573 (void) len;
574 return file_date (fe->st.st_mtime);
577 /* --------------------------------------------------------------------------------------------- */
578 /** atime */
580 static const char *
581 string_file_atime (file_entry * fe, int len)
583 (void) len;
584 return file_date (fe->st.st_atime);
587 /* --------------------------------------------------------------------------------------------- */
588 /** ctime */
590 static const char *
591 string_file_ctime (file_entry * fe, int len)
593 (void) len;
594 return file_date (fe->st.st_ctime);
597 /* --------------------------------------------------------------------------------------------- */
598 /** perm */
600 static const char *
601 string_file_permission (file_entry * fe, int len)
603 (void) len;
604 return string_perm (fe->st.st_mode);
607 /* --------------------------------------------------------------------------------------------- */
608 /** mode */
610 static const char *
611 string_file_perm_octal (file_entry * fe, int len)
613 static char buffer[10];
615 (void) len;
616 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
617 return buffer;
620 /* --------------------------------------------------------------------------------------------- */
621 /** nlink */
623 static const char *
624 string_file_nlinks (file_entry * fe, int len)
626 static char buffer[BUF_TINY];
628 (void) len;
629 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
630 return buffer;
633 /* --------------------------------------------------------------------------------------------- */
634 /** inode */
636 static const char *
637 string_inode (file_entry * fe, int len)
639 static char buffer[10];
641 (void) len;
642 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
643 return buffer;
646 /* --------------------------------------------------------------------------------------------- */
647 /** nuid */
649 static const char *
650 string_file_nuid (file_entry * fe, int len)
652 static char buffer[10];
654 (void) len;
655 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
656 return buffer;
659 /* --------------------------------------------------------------------------------------------- */
660 /** ngid */
662 static const char *
663 string_file_ngid (file_entry * fe, int len)
665 static char buffer[10];
667 (void) len;
668 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
669 return buffer;
672 /* --------------------------------------------------------------------------------------------- */
673 /** owner */
675 static const char *
676 string_file_owner (file_entry * fe, int len)
678 (void) len;
679 return get_owner (fe->st.st_uid);
682 /* --------------------------------------------------------------------------------------------- */
683 /** group */
685 static const char *
686 string_file_group (file_entry * fe, int len)
688 (void) len;
689 return get_group (fe->st.st_gid);
692 /* --------------------------------------------------------------------------------------------- */
693 /** mark */
695 static const char *
696 string_marked (file_entry * fe, int len)
698 (void) len;
699 return fe->f.marked ? "*" : " ";
702 /* --------------------------------------------------------------------------------------------- */
703 /** space */
705 static const char *
706 string_space (file_entry * fe, int len)
708 (void) fe;
709 (void) len;
710 return " ";
713 /* --------------------------------------------------------------------------------------------- */
714 /** dot */
716 static const char *
717 string_dot (file_entry * fe, int len)
719 (void) fe;
720 (void) len;
721 return ".";
724 /* --------------------------------------------------------------------------------------------- */
726 static int
727 file_compute_color (int attr, file_entry * fe)
729 switch (attr)
731 case SELECTED:
732 return (SELECTED_COLOR);
733 case MARKED:
734 return (MARKED_COLOR);
735 case MARKED_SELECTED:
736 return (MARKED_SELECTED_COLOR);
737 case STATUS:
738 return (NORMAL_COLOR);
739 case NORMAL:
740 default:
741 if (!panels_options.filetype_mode)
742 return (NORMAL_COLOR);
745 return mc_fhl_get_color (mc_filehighlight, fe);
748 /* --------------------------------------------------------------------------------------------- */
749 /** Formats the file number file_index of panel in the buffer dest */
751 static void
752 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
753 int isstatus)
755 int color, length, empty_line;
756 const char *txt;
757 format_e *format, *home;
758 file_entry *fe;
760 (void) dest;
761 (void) limit;
762 length = 0;
763 empty_line = (file_index >= panel->count);
764 home = (isstatus) ? panel->status_format : panel->format;
765 fe = &panel->dir.list[file_index];
767 if (!empty_line)
768 color = file_compute_color (attr, fe);
769 else
770 color = NORMAL_COLOR;
772 for (format = home; format; format = format->next)
774 if (length == width)
775 break;
777 if (format->string_fn)
779 int len, perm;
780 char *preperad_text;
782 if (empty_line)
783 txt = " ";
784 else
785 txt = (*format->string_fn) (fe, format->field_len);
787 len = format->field_len;
788 if (len + length > width)
789 len = width - length;
790 if (len <= 0)
791 break;
793 perm = 0;
794 if (panels_options.permission_mode)
796 if (!strcmp (format->id, "perm"))
797 perm = 1;
798 else if (!strcmp (format->id, "mode"))
799 perm = 2;
802 if (color >= 0)
803 tty_setcolor (color);
804 else
805 tty_lowlevel_setcolor (-color);
807 preperad_text = (char *) str_fit_to_term (txt, len, format->just_mode);
809 if (perm)
810 add_permission_string (preperad_text, format->field_len, fe, attr, color, perm - 1);
811 else
812 tty_print_string (preperad_text);
814 length += len;
816 else
818 if (attr == SELECTED || attr == MARKED_SELECTED)
819 tty_setcolor (SELECTED_COLOR);
820 else
821 tty_setcolor (NORMAL_COLOR);
822 tty_print_one_vline (TRUE);
823 length++;
827 if (length < width)
828 tty_draw_hline (-1, -1, ' ', width - length);
831 /* --------------------------------------------------------------------------------------------- */
833 static void
834 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
836 int second_column = 0;
837 int width;
838 int offset = 0;
839 char buffer[BUF_MEDIUM];
841 gboolean panel_is_split = !isstatus && panel->split;
843 width = panel->widget.cols - 2;
845 if (panel_is_split)
847 second_column = (file_index - panel->top_file) / llines (panel);
848 width = width / 2 - 1;
850 if (second_column != 0)
852 offset = 1 + width;
853 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
854 width = panel->widget.cols - offset - 2;
858 /* Nothing to paint */
859 if (width <= 0)
860 return;
862 if (mv)
864 if (panel_is_split)
865 widget_move (&panel->widget,
866 (file_index - panel->top_file) % llines (panel) + 2, offset + 1);
867 else
868 widget_move (&panel->widget, file_index - panel->top_file + 2, 1);
871 format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus);
873 if (panel_is_split)
875 if (second_column)
876 tty_print_char (' ');
877 else
879 tty_setcolor (NORMAL_COLOR);
880 tty_print_one_vline (TRUE);
885 /* --------------------------------------------------------------------------------------------- */
887 static void
888 display_mini_info (WPanel * panel)
890 if (!panels_options.show_mini_info)
891 return;
893 widget_move (&panel->widget, llines (panel) + 3, 1);
895 if (panel->searching)
897 tty_setcolor (INPUT_COLOR);
898 tty_print_char ('/');
899 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
900 return;
903 /* Status resolves links and show them */
904 set_colors (panel);
906 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
908 char link_target[MC_MAXPATHLEN];
909 vfs_path_t *lc_link_vpath;
910 int len;
912 lc_link_vpath =
913 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
914 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
915 vfs_path_free (lc_link_vpath);
916 if (len > 0)
918 link_target[len] = 0;
919 tty_print_string ("-> ");
920 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
922 else
923 tty_print_string (str_fit_to_term (_("<readlink failed>"),
924 panel->widget.cols - 2, J_LEFT));
926 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
928 /* FIXME:
929 * while loading directory (do_load_dir() and do_reload_dir()),
930 * the actual stat info about ".." directory isn't got;
931 * so just don't display incorrect info about ".." directory */
932 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
934 else
935 /* Default behavior */
936 repaint_file (panel, panel->selected, 0, STATUS, 1);
939 /* --------------------------------------------------------------------------------------------- */
941 static void
942 paint_dir (WPanel * panel)
944 int i;
945 int color; /* Color value of the line */
946 int items; /* Number of items */
948 items = llines (panel) * (panel->split ? 2 : 1);
950 for (i = 0; i < items; i++)
952 if (i + panel->top_file >= panel->count)
953 color = 0;
954 else
956 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
957 color += (panel->selected == i + panel->top_file && panel->active);
959 repaint_file (panel, i + panel->top_file, 1, color, 0);
961 tty_set_normal_attrs ();
964 /* --------------------------------------------------------------------------------------------- */
966 static void
967 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
969 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
970 int cols;
972 if (panel->marked <= 0)
973 return;
975 buf = size_only ? b_bytes : buffer;
976 cols = panel->widget.cols - 2;
979 * This is a trick to use two ngettext() calls in one sentence.
980 * First make "N bytes", then insert it into "X in M files".
982 g_snprintf (b_bytes, sizeof (b_bytes),
983 ngettext ("%s byte", "%s bytes", panel->total),
984 size_trunc_sep (panel->total, panels_options.kilobyte_si));
985 if (!size_only)
986 g_snprintf (buffer, sizeof (buffer),
987 ngettext ("%s in %d file", "%s in %d files", panel->marked),
988 b_bytes, panel->marked);
990 /* don't forget spaces around buffer content */
991 buf = (char *) str_trunc (buf, cols - 4);
993 if (x < 0)
994 /* center in panel */
995 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
998 * y == llines (panel) + 2 for mini_info_separator
999 * y == panel->widget.lines - 1 for panel bottom frame
1001 widget_move (&panel->widget, y, x);
1002 tty_setcolor (MARKED_COLOR);
1003 tty_printf (" %s ", buf);
1006 /* --------------------------------------------------------------------------------------------- */
1008 static void
1009 mini_info_separator (WPanel * panel)
1011 if (panels_options.show_mini_info)
1013 const int y = llines (panel) + 2;
1015 tty_setcolor (NORMAL_COLOR);
1016 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1017 ACS_HLINE, panel->widget.cols - 2);
1018 /* Status displays total marked size.
1019 * Centered in panel, full format. */
1020 display_total_marked_size (panel, y, -1, FALSE);
1024 /* --------------------------------------------------------------------------------------------- */
1026 static void
1027 show_free_space (WPanel * panel)
1029 /* Used to figure out how many free space we have */
1030 static struct my_statfs myfs_stats;
1031 /* Old current working directory for displaying free space */
1032 static char *old_cwd = NULL;
1033 char *tmp_path;
1035 /* Don't try to stat non-local fs */
1036 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1037 return;
1039 tmp_path = vfs_path_to_str (panel->cwd_vpath);
1040 if (old_cwd == NULL || strcmp (old_cwd, tmp_path) != 0)
1042 char rpath[PATH_MAX];
1044 init_my_statfs ();
1045 g_free (old_cwd);
1046 old_cwd = tmp_path;
1047 tmp_path = NULL;
1049 if (mc_realpath (old_cwd, rpath) == NULL)
1050 return;
1052 my_statfs (&myfs_stats, rpath);
1054 g_free (tmp_path);
1056 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1058 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1059 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1060 panels_options.kilobyte_si);
1061 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1062 panels_options.kilobyte_si);
1063 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1064 myfs_stats.total == 0 ? 0 :
1065 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1066 widget_move (&panel->widget, panel->widget.lines - 1,
1067 panel->widget.cols - 2 - (int) strlen (tmp));
1068 tty_setcolor (NORMAL_COLOR);
1069 tty_print_string (tmp);
1073 /* --------------------------------------------------------------------------------------------- */
1075 * Prepare path string for showing in panel's header.
1076 * Passwords will removed, also home dir will replaced by ~
1078 * @param panel WPanel object
1080 * @return newly allocated string.
1083 static char *
1084 panel_correct_path_to_show (WPanel * panel)
1086 vfs_path_t *last_vpath;
1087 const vfs_path_element_t *path_element;
1088 char *return_path;
1089 int elements_count;
1091 elements_count = vfs_path_elements_count (panel->cwd_vpath);
1093 /* get last path element */
1094 path_element = vfs_path_element_clone (vfs_path_get_by_index (panel->cwd_vpath, -1));
1097 if (elements_count > 1 && (strcmp (path_element->class->name, "cpiofs") == 0 ||
1098 strcmp (path_element->class->name, "extfs") == 0 ||
1099 strcmp (path_element->class->name, "tarfs") == 0))
1101 const char *archive_name;
1102 const vfs_path_element_t *prev_path_element;
1104 /* get previous path element for catching archive name */
1105 prev_path_element = vfs_path_get_by_index (panel->cwd_vpath, -2);
1106 archive_name = strrchr (prev_path_element->path, PATH_SEP);
1107 if (archive_name != NULL)
1109 last_vpath = vfs_path_from_str_flags (archive_name + 1, VPF_NO_CANON);
1111 else
1113 last_vpath = vfs_path_from_str_flags (prev_path_element->path, VPF_NO_CANON);
1114 last_vpath->relative = TRUE;
1117 else
1119 last_vpath = vfs_path_new ();
1120 last_vpath->relative = TRUE;
1123 vfs_path_add_element (last_vpath, path_element);
1124 return_path =
1125 vfs_path_to_str_flags (last_vpath, 0,
1126 VPF_STRIP_HOME | VPF_STRIP_PASSWORD | VPF_HIDE_CHARSET);
1127 vfs_path_free (last_vpath);
1129 return return_path;
1132 /* --------------------------------------------------------------------------------------------- */
1134 * Get Current path element encoding
1136 * @param panel WPanel object
1138 * @return newly allocated string or NULL if path charset is same as system charset
1141 #ifdef HAVE_CHARSET
1142 static char *
1143 panel_get_encoding_info_str (WPanel * panel)
1145 char *ret_str = NULL;
1146 const vfs_path_element_t *path_element;
1148 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1149 if (path_element->encoding != NULL)
1150 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1152 return ret_str;
1154 #endif
1156 /* --------------------------------------------------------------------------------------------- */
1158 static void
1159 show_dir (WPanel * panel)
1161 gchar *tmp;
1162 set_colors (panel);
1163 draw_box (panel->widget.owner,
1164 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1166 if (panels_options.show_mini_info)
1168 widget_move (&panel->widget, llines (panel) + 2, 0);
1169 tty_print_alt_char (ACS_LTEE, FALSE);
1170 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1171 tty_print_alt_char (ACS_RTEE, FALSE);
1174 widget_move (&panel->widget, 0, 1);
1175 tty_print_string (panel_history_prev_item_sign);
1177 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1178 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1179 panel_history_next_item_sign);
1181 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1182 tty_print_string (tmp);
1184 g_free (tmp);
1186 widget_move (&panel->widget, 0, 3);
1188 if (panel->is_panelized)
1189 tty_printf (" %s ", _("Panelize"));
1190 #ifdef HAVE_CHARSET
1191 else
1193 tmp = panel_get_encoding_info_str (panel);
1194 if (tmp != NULL)
1196 tty_printf ("%s", tmp);
1197 widget_move (&panel->widget, 0, 3 + strlen (tmp));
1198 g_free (tmp);
1201 #endif
1203 if (panel->active)
1204 tty_setcolor (REVERSE_COLOR);
1206 tmp = panel_correct_path_to_show (panel);
1207 tty_printf (" %s ",
1208 str_term_trim (tmp, min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1209 g_free (tmp);
1211 if (!panels_options.show_mini_info)
1213 if (panel->marked == 0)
1215 /* Show size of curret file in the bottom of panel */
1216 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1218 char buffer[BUF_SMALL];
1220 g_snprintf (buffer, sizeof (buffer), " %s ",
1221 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1222 panels_options.kilobyte_si));
1223 tty_setcolor (NORMAL_COLOR);
1224 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1225 tty_print_string (buffer);
1228 else
1230 /* Show total size of marked files
1231 * In the bottom of panel, display size only. */
1232 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1236 show_free_space (panel);
1238 if (panel->active)
1239 tty_set_normal_attrs ();
1242 /* --------------------------------------------------------------------------------------------- */
1244 /* Returns the number of items in the given panel */
1245 static int
1246 ITEMS (WPanel * p)
1248 if (p->split)
1249 return llines (p) * 2;
1250 else
1251 return llines (p);
1254 /* --------------------------------------------------------------------------------------------- */
1256 static void
1257 adjust_top_file (WPanel * panel)
1259 int items = ITEMS (panel);
1261 if (panel->count <= items)
1263 /* If all files fit, show them all. */
1264 panel->top_file = 0;
1266 else
1268 int i;
1270 /* top_file has to be in the range [selected-items+1, selected] so that
1271 the selected file is visible.
1272 top_file should be in the range [0, count-items] so that there's
1273 no empty space wasted.
1274 Within these ranges, adjust it by as little as possible. */
1276 if (panel->top_file < 0)
1277 panel->top_file = 0;
1279 i = panel->selected - items + 1;
1280 if (panel->top_file < i)
1281 panel->top_file = i;
1283 i = panel->count - items;
1284 if (panel->top_file > i)
1285 panel->top_file = i;
1287 if (panel->top_file > panel->selected)
1288 panel->top_file = panel->selected;
1292 /* --------------------------------------------------------------------------------------------- */
1293 /** add "#enc:encodning" to end of path */
1294 /* if path end width a previous #enc:, only encoding is changed no additional
1295 * #enc: is appended
1296 * retun new string
1299 static char *
1300 panel_save_name (WPanel * panel)
1302 /* If the program is shuting down */
1303 if ((mc_global.midnight_shutdown && auto_save_setup) || saving_setup)
1304 return g_strdup (panel->panel_name);
1305 else
1306 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1309 /* --------------------------------------------------------------------------------------------- */
1311 /* "history_load" event handler */
1312 static gboolean
1313 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1314 gpointer init_data, gpointer data)
1316 WPanel *p = (WPanel *) init_data;
1317 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1319 (void) event_group_name;
1320 (void) event_name;
1322 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1324 char *tmp_path;
1326 tmp_path = vfs_path_to_str (p->cwd_vpath);
1327 if (ev->cfg != NULL)
1328 p->dir_history = history_load (ev->cfg, p->hist_name);
1329 else
1330 p->dir_history = history_get (p->hist_name);
1332 directory_history_add (p, tmp_path);
1333 g_free (tmp_path);
1336 return TRUE;
1339 /* --------------------------------------------------------------------------------------------- */
1341 /* "history_save" event handler */
1342 static gboolean
1343 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1344 gpointer init_data, gpointer data)
1346 WPanel *p = (WPanel *) init_data;
1348 (void) event_group_name;
1349 (void) event_name;
1351 if (p->dir_history != NULL)
1353 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1355 history_save (ev->cfg, p->hist_name, p->dir_history);
1358 return TRUE;
1361 /* --------------------------------------------------------------------------------------------- */
1363 static void
1364 panel_destroy (WPanel * p)
1366 size_t i;
1368 if (panels_options.auto_save_setup)
1370 char *name;
1372 name = panel_save_name (p);
1373 panel_save_setup (p, name);
1374 g_free (name);
1377 panel_clean_dir (p);
1379 /* clean history */
1380 if (p->dir_history != NULL)
1382 /* directory history is already saved before this moment */
1383 p->dir_history = g_list_first (p->dir_history);
1384 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1385 g_list_free (p->dir_history);
1387 g_free (p->hist_name);
1389 delete_format (p->format);
1390 delete_format (p->status_format);
1392 g_free (p->user_format);
1393 for (i = 0; i < LIST_TYPES; i++)
1394 g_free (p->user_status_format[i]);
1396 g_free (p->dir.list);
1397 g_free (p->panel_name);
1399 vfs_path_free (p->lwd_vpath);
1400 vfs_path_free (p->cwd_vpath);
1403 /* --------------------------------------------------------------------------------------------- */
1405 static void
1406 panel_format_modified (WPanel * panel)
1408 panel->format_modified = 1;
1411 /* --------------------------------------------------------------------------------------------- */
1413 static void
1414 panel_paint_sort_info (WPanel * panel)
1416 if (*panel->sort_info.sort_field->hotkey != '\0')
1418 const char *sort_sign =
1419 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1420 char *str;
1422 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1423 widget_move (&panel->widget, 1, 1);
1424 tty_print_string (str);
1425 g_free (str);
1429 /* --------------------------------------------------------------------------------------------- */
1431 static gchar *
1432 panel_get_title_without_hotkey (const char *title)
1434 char *translated_title;
1435 char *hkey;
1437 if (title == NULL)
1438 return NULL;
1439 if (title[0] == '\0')
1440 return g_strdup ("");
1442 translated_title = g_strdup (_(title));
1444 hkey = strchr (translated_title, '&');
1445 if ((hkey != NULL) && (hkey[1] != '\0'))
1446 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1448 return translated_title;
1451 /* --------------------------------------------------------------------------------------------- */
1453 static void
1454 paint_frame (WPanel * panel)
1456 int side, width;
1457 GString *format_txt;
1459 adjust_top_file (panel);
1461 widget_erase (&panel->widget);
1462 show_dir (panel);
1464 widget_move (&panel->widget, 1, 1);
1466 for (side = 0; side <= panel->split; side++)
1468 format_e *format;
1470 if (side)
1472 tty_setcolor (NORMAL_COLOR);
1473 tty_print_one_vline (TRUE);
1474 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1476 else if (panel->split)
1477 width = panel->widget.cols / 2 - 3;
1478 else
1479 width = panel->widget.cols - 2;
1481 format_txt = g_string_new ("");
1482 for (format = panel->format; format; format = format->next)
1484 if (format->string_fn)
1486 g_string_set_size (format_txt, 0);
1488 if (panel->list_type == list_long
1489 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1490 g_string_append (format_txt,
1491 panel->sort_info.reverse
1492 ? panel_sort_down_sign : panel_sort_up_sign);
1494 g_string_append (format_txt, format->title);
1495 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1497 g_string_append (format_txt, " [");
1498 g_string_append (format_txt, panel->filter);
1499 g_string_append (format_txt, "]");
1502 tty_setcolor (HEADER_COLOR);
1503 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1504 J_CENTER_LEFT));
1505 width -= format->field_len;
1507 else
1509 tty_setcolor (NORMAL_COLOR);
1510 tty_print_one_vline (TRUE);
1511 width--;
1514 g_string_free (format_txt, TRUE);
1516 if (width > 0)
1517 tty_draw_hline (-1, -1, ' ', width);
1520 if (panel->list_type != list_long)
1521 panel_paint_sort_info (panel);
1524 /* --------------------------------------------------------------------------------------------- */
1526 static const char *
1527 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1529 panel_display_t frame = frame_half;
1530 format = skip_separators (format);
1532 if (!strncmp (format, "full", 4))
1534 frame = frame_full;
1535 format += 4;
1537 else if (!strncmp (format, "half", 4))
1539 frame = frame_half;
1540 format += 4;
1543 if (!isstatus)
1545 panel->frame_size = frame;
1546 panel->split = 0;
1549 /* Now, the optional column specifier */
1550 format = skip_separators (format);
1552 if (*format == '1' || *format == '2')
1554 if (!isstatus)
1555 panel->split = *format == '2';
1556 format++;
1559 if (!isstatus)
1560 panel_update_cols (&(panel->widget), panel->frame_size);
1562 return skip_separators (format);
1565 /* Format is:
1567 all := panel_format? format
1568 panel_format := [full|half] [1|2]
1569 format := one_format_e
1570 | format , one_format_e
1572 one_format_e := just format.id [opt_size]
1573 just := [<=>]
1574 opt_size := : size [opt_expand]
1575 size := [0-9]+
1576 opt_expand := +
1580 /* --------------------------------------------------------------------------------------------- */
1582 static format_e *
1583 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1584 int *res_total_cols)
1586 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1587 int total_cols = 0; /* Used columns by the format */
1588 int set_justify; /* flag: set justification mode? */
1589 align_crt_t justify = J_LEFT; /* Which mode. */
1590 size_t i;
1592 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1594 *error = 0;
1596 if (i18n_timelength == 0)
1598 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1600 for (i = 0; panel_fields[i].id != NULL; i++)
1601 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1602 panel_fields[i].min_size = i18n_timelength;
1606 * This makes sure that the panel and mini status full/half mode
1607 * setting is equal
1609 format = parse_panel_size (panel, format, isstatus);
1611 while (*format)
1612 { /* format can be an empty string */
1613 int found = 0;
1615 darr = g_new0 (format_e, 1);
1617 /* I'm so ugly, don't look at me :-) */
1618 if (!home)
1619 home = old = darr;
1621 old->next = darr;
1622 darr->next = 0;
1623 old = darr;
1625 format = skip_separators (format);
1627 if (strchr ("<=>", *format))
1629 set_justify = 1;
1630 switch (*format)
1632 case '<':
1633 justify = J_LEFT;
1634 break;
1635 case '=':
1636 justify = J_CENTER;
1637 break;
1638 case '>':
1639 default:
1640 justify = J_RIGHT;
1641 break;
1643 format = skip_separators (format + 1);
1645 else
1646 set_justify = 0;
1648 for (i = 0; panel_fields[i].id != NULL; i++)
1650 size_t klen = strlen (panel_fields[i].id);
1652 if (strncmp (format, panel_fields[i].id, klen) != 0)
1653 continue;
1655 format += klen;
1657 darr->requested_field_len = panel_fields[i].min_size;
1658 darr->string_fn = panel_fields[i].string_fn;
1659 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1661 darr->id = panel_fields[i].id;
1662 darr->expand = panel_fields[i].expands;
1663 darr->just_mode = panel_fields[i].default_just;
1665 if (set_justify)
1667 if (IS_FIT (darr->just_mode))
1668 darr->just_mode = MAKE_FIT (justify);
1669 else
1670 darr->just_mode = justify;
1672 found = 1;
1674 format = skip_separators (format);
1676 /* If we have a size specifier */
1677 if (*format == ':')
1679 int req_length;
1681 /* If the size was specified, we don't want
1682 * auto-expansion by default
1684 darr->expand = 0;
1685 format++;
1686 req_length = atoi (format);
1687 darr->requested_field_len = req_length;
1689 format = skip_numbers (format);
1691 /* Now, if they insist on expansion */
1692 if (*format == '+')
1694 darr->expand = 1;
1695 format++;
1700 break;
1702 if (!found)
1704 char *tmp_format = g_strdup (format);
1706 int pos = min (8, strlen (format));
1707 delete_format (home);
1708 tmp_format[pos] = 0;
1709 *error =
1710 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1711 g_free (tmp_format);
1712 return 0;
1714 total_cols += darr->requested_field_len;
1717 *res_total_cols = total_cols;
1718 return home;
1721 /* --------------------------------------------------------------------------------------------- */
1723 static format_e *
1724 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1726 #define MAX_EXPAND 4
1727 int expand_top = 0; /* Max used element in expand */
1728 int usable_columns; /* Usable columns in the panel */
1729 int total_cols = 0;
1730 int i;
1731 format_e *darr, *home;
1733 if (!format)
1734 format = DEFAULT_USER_FORMAT;
1736 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1738 if (*error)
1739 return 0;
1741 panel->dirty = 1;
1743 /* Status needn't to be split */
1744 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1746 : (panel->split + 1))) - (!isstatus
1747 && panel->split);
1749 /* Look for the expandable fields and set field_len based on the requested field len */
1750 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1752 darr->field_len = darr->requested_field_len;
1753 if (darr->expand)
1754 expand_top++;
1757 /* If we used more columns than the available columns, adjust that */
1758 if (total_cols > usable_columns)
1760 int pdif, dif = total_cols - usable_columns;
1762 while (dif)
1764 pdif = dif;
1765 for (darr = home; darr; darr = darr->next)
1767 if (dif && darr->field_len - 1)
1769 darr->field_len--;
1770 dif--;
1774 /* avoid endless loop if num fields > 40 */
1775 if (pdif == dif)
1776 break;
1778 total_cols = usable_columns; /* give up, the rest should be truncated */
1781 /* Expand the available space */
1782 if ((usable_columns > total_cols) && expand_top)
1784 int spaces = (usable_columns - total_cols) / expand_top;
1785 int extra = (usable_columns - total_cols) % expand_top;
1787 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1788 if (darr->expand)
1790 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1791 i++;
1794 return home;
1797 /* --------------------------------------------------------------------------------------------- */
1798 /** Given the panel->view_type returns the format string to be parsed */
1800 static const char *
1801 panel_format (WPanel * panel)
1803 switch (panel->list_type)
1805 case list_long:
1806 return "full perm space nlink space owner space group space size space mtime space name";
1808 case list_brief:
1809 return "half 2 type name";
1811 case list_user:
1812 return panel->user_format;
1814 default:
1815 case list_full:
1816 return "half type name | size | mtime";
1820 /* --------------------------------------------------------------------------------------------- */
1822 static const char *
1823 mini_status_format (WPanel * panel)
1825 if (panel->user_mini_status)
1826 return panel->user_status_format[panel->list_type];
1828 switch (panel->list_type)
1831 case list_long:
1832 return "full perm space nlink space owner space group space size space mtime space name";
1834 case list_brief:
1835 return "half type name space bsize space perm space";
1837 case list_full:
1838 return "half type name";
1840 default:
1841 case list_user:
1842 return panel->user_format;
1846 /* */
1847 /* Panel operation commands */
1848 /* */
1850 /* --------------------------------------------------------------------------------------------- */
1851 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1853 static cb_ret_t
1854 maybe_cd (int move_up_dir)
1856 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1858 if (move_up_dir)
1860 vfs_path_t *up_dir;
1862 up_dir = vfs_path_from_str ("..");
1863 do_cd (up_dir, cd_exact);
1864 vfs_path_free (up_dir);
1865 return MSG_HANDLED;
1868 if (S_ISDIR (selection (current_panel)->st.st_mode)
1869 || link_isdir (selection (current_panel)))
1871 vfs_path_t *vpath;
1873 vpath = vfs_path_from_str (selection (current_panel)->fname);
1874 do_cd (vpath, cd_exact);
1875 vfs_path_free (vpath);
1876 return MSG_HANDLED;
1879 return MSG_NOT_HANDLED;
1882 /* --------------------------------------------------------------------------------------------- */
1884 /* if command line is empty then do 'cd ..' */
1885 static cb_ret_t
1886 force_maybe_cd (void)
1888 if (cmdline->buffer[0] == '\0')
1890 vfs_path_t *up_dir = vfs_path_from_str ("..");
1891 do_cd (up_dir, cd_exact);
1892 vfs_path_free (up_dir);
1893 return MSG_HANDLED;
1895 return MSG_NOT_HANDLED;
1898 /* --------------------------------------------------------------------------------------------- */
1900 static void
1901 unselect_item (WPanel * panel)
1903 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1906 /* --------------------------------------------------------------------------------------------- */
1908 static void
1909 move_down (WPanel * panel)
1911 if (panel->selected + 1 == panel->count)
1912 return;
1914 unselect_item (panel);
1915 panel->selected++;
1916 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1918 /* Scroll window half screen */
1919 panel->top_file += ITEMS (panel) / 2;
1920 if (panel->top_file > panel->count - ITEMS (panel))
1921 panel->top_file = panel->count - ITEMS (panel);
1922 paint_dir (panel);
1924 select_item (panel);
1927 /* --------------------------------------------------------------------------------------------- */
1929 static void
1930 move_up (WPanel * panel)
1932 if (panel->selected == 0)
1933 return;
1935 unselect_item (panel);
1936 panel->selected--;
1937 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1939 /* Scroll window half screen */
1940 panel->top_file -= ITEMS (panel) / 2;
1941 if (panel->top_file < 0)
1942 panel->top_file = 0;
1943 paint_dir (panel);
1945 select_item (panel);
1948 /* --------------------------------------------------------------------------------------------- */
1949 /** Changes the selection by lines (may be negative) */
1951 static void
1952 move_selection (WPanel * panel, int lines)
1954 int new_pos;
1955 int adjust = 0;
1957 new_pos = panel->selected + lines;
1958 if (new_pos >= panel->count)
1959 new_pos = panel->count - 1;
1961 if (new_pos < 0)
1962 new_pos = 0;
1964 unselect_item (panel);
1965 panel->selected = new_pos;
1967 if (panel->selected - panel->top_file >= ITEMS (panel))
1969 panel->top_file += lines;
1970 adjust = 1;
1973 if (panel->selected - panel->top_file < 0)
1975 panel->top_file += lines;
1976 adjust = 1;
1979 if (adjust)
1981 if (panel->top_file > panel->selected)
1982 panel->top_file = panel->selected;
1983 if (panel->top_file < 0)
1984 panel->top_file = 0;
1985 paint_dir (panel);
1987 select_item (panel);
1990 /* --------------------------------------------------------------------------------------------- */
1992 static cb_ret_t
1993 move_left (WPanel * panel)
1995 if (panel->split)
1997 move_selection (panel, -llines (panel));
1998 return MSG_HANDLED;
2000 else
2001 return maybe_cd (1); /* cd .. */
2004 /* --------------------------------------------------------------------------------------------- */
2006 static cb_ret_t
2007 move_right (WPanel * panel)
2009 if (panel->split)
2011 move_selection (panel, llines (panel));
2012 return MSG_HANDLED;
2014 else
2015 return maybe_cd (0); /* cd (selection) */
2018 /* --------------------------------------------------------------------------------------------- */
2020 static void
2021 prev_page (WPanel * panel)
2023 int items;
2025 if (!panel->selected && !panel->top_file)
2026 return;
2027 unselect_item (panel);
2028 items = ITEMS (panel);
2029 if (panel->top_file < items)
2030 items = panel->top_file;
2031 if (!items)
2032 panel->selected = 0;
2033 else
2034 panel->selected -= items;
2035 panel->top_file -= items;
2037 select_item (panel);
2038 paint_dir (panel);
2041 /* --------------------------------------------------------------------------------------------- */
2043 static void
2044 goto_parent_dir (WPanel * panel)
2046 if (!panel->is_panelized)
2048 vfs_path_t *up_dir;
2050 up_dir = vfs_path_from_str ("..");
2051 do_cd (up_dir, cd_exact);
2052 vfs_path_free (up_dir);
2054 else
2056 char *fname = panel->dir.list[panel->selected].fname;
2057 const char *bname;
2058 vfs_path_t *dname_vpath;
2060 if (g_path_is_absolute (fname))
2061 fname = g_strdup (fname);
2062 else
2064 char *tmp_root;
2066 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2067 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2068 g_free (tmp_root);
2071 bname = x_basename (fname);
2073 if (bname == fname)
2074 dname_vpath = vfs_path_from_str (".");
2075 else
2077 char *dname;
2079 dname = g_strndup (fname, bname - fname);
2080 dname_vpath = vfs_path_from_str (dname);
2081 g_free (dname);
2084 do_cd (dname_vpath, cd_exact);
2085 try_to_select (panel, bname);
2087 vfs_path_free (dname_vpath);
2088 g_free (fname);
2092 /* --------------------------------------------------------------------------------------------- */
2094 static void
2095 next_page (WPanel * panel)
2097 int items;
2099 if (panel->selected == panel->count - 1)
2100 return;
2101 unselect_item (panel);
2102 items = ITEMS (panel);
2103 if (panel->top_file > panel->count - 2 * items)
2104 items = panel->count - items - panel->top_file;
2105 if (panel->top_file + items < 0)
2106 items = -panel->top_file;
2107 if (!items)
2108 panel->selected = panel->count - 1;
2109 else
2110 panel->selected += items;
2111 panel->top_file += items;
2113 select_item (panel);
2114 paint_dir (panel);
2117 /* --------------------------------------------------------------------------------------------- */
2119 static void
2120 goto_child_dir (WPanel * panel)
2122 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2124 vfs_path_t *vpath;
2126 vpath = vfs_path_from_str (selection (panel)->fname);
2127 do_cd (vpath, cd_exact);
2128 vfs_path_free (vpath);
2132 /* --------------------------------------------------------------------------------------------- */
2134 static void
2135 goto_top_file (WPanel * panel)
2137 unselect_item (panel);
2138 panel->selected = panel->top_file;
2139 select_item (panel);
2142 /* --------------------------------------------------------------------------------------------- */
2144 static void
2145 goto_middle_file (WPanel * panel)
2147 unselect_item (panel);
2148 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2149 select_item (panel);
2152 /* --------------------------------------------------------------------------------------------- */
2154 static void
2155 goto_bottom_file (WPanel * panel)
2157 unselect_item (panel);
2158 panel->selected = panel->top_file + ITEMS (panel) - 1;
2159 select_item (panel);
2162 /* --------------------------------------------------------------------------------------------- */
2164 static void
2165 move_home (WPanel * panel)
2167 if (panel->selected == 0)
2168 return;
2170 unselect_item (panel);
2172 if (panels_options.torben_fj_mode)
2174 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2176 if (panel->selected > middle_pos)
2178 goto_middle_file (panel);
2179 return;
2181 if (panel->selected != panel->top_file)
2183 goto_top_file (panel);
2184 return;
2188 panel->top_file = 0;
2189 panel->selected = 0;
2191 paint_dir (panel);
2192 select_item (panel);
2195 /* --------------------------------------------------------------------------------------------- */
2197 static void
2198 move_end (WPanel * panel)
2200 if (panel->selected == panel->count - 1)
2201 return;
2203 unselect_item (panel);
2205 if (panels_options.torben_fj_mode)
2207 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2209 if (panel->selected < middle_pos)
2211 goto_middle_file (panel);
2212 return;
2214 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2216 goto_bottom_file (panel);
2217 return;
2221 panel->selected = panel->count - 1;
2222 paint_dir (panel);
2223 select_item (panel);
2226 /* --------------------------------------------------------------------------------------------- */
2228 static void
2229 do_mark_file (WPanel * panel, mark_act_t do_move)
2231 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2232 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2233 move_down (panel);
2234 else if (do_move == MARK_FORCE_UP)
2235 move_up (panel);
2238 /* --------------------------------------------------------------------------------------------- */
2240 static void
2241 mark_file (WPanel * panel)
2243 do_mark_file (panel, MARK_DOWN);
2246 /* --------------------------------------------------------------------------------------------- */
2248 static void
2249 mark_file_up (WPanel * panel)
2251 do_mark_file (panel, MARK_FORCE_UP);
2254 /* --------------------------------------------------------------------------------------------- */
2256 static void
2257 mark_file_down (WPanel * panel)
2259 do_mark_file (panel, MARK_FORCE_DOWN);
2262 /* --------------------------------------------------------------------------------------------- */
2264 static void
2265 mark_file_right (WPanel * panel)
2267 int lines = llines (panel);
2269 if (state_mark < 0)
2270 state_mark = selection (panel)->f.marked ? 0 : 1;
2272 lines = min (lines, panel->count - panel->selected - 1);
2273 for (; lines != 0; lines--)
2275 do_file_mark (panel, panel->selected, state_mark);
2276 move_down (panel);
2278 do_file_mark (panel, panel->selected, state_mark);
2281 /* --------------------------------------------------------------------------------------------- */
2283 static void
2284 mark_file_left (WPanel * panel)
2286 int lines = llines (panel);
2288 if (state_mark < 0)
2289 state_mark = selection (panel)->f.marked ? 0 : 1;
2291 lines = min (lines, panel->selected + 1);
2292 for (; lines != 0; lines--)
2294 do_file_mark (panel, panel->selected, state_mark);
2295 move_up (panel);
2297 do_file_mark (panel, panel->selected, state_mark);
2300 /* --------------------------------------------------------------------------------------------- */
2301 /** Incremental search of a file name in the panel.
2302 * @param panel instance of WPanel structure
2303 * @param c_code key code
2306 static void
2307 do_search (WPanel * panel, int c_code)
2309 size_t l;
2310 int i, sel;
2311 gboolean wrapped = FALSE;
2312 char *act;
2313 mc_search_t *search;
2314 char *reg_exp, *esc_str;
2315 gboolean is_found = FALSE;
2317 l = strlen (panel->search_buffer);
2318 if (c_code == KEY_BACKSPACE)
2320 if (l != 0)
2322 act = panel->search_buffer + l;
2323 str_prev_noncomb_char (&act, panel->search_buffer);
2324 act[0] = '\0';
2326 panel->search_chpoint = 0;
2328 else
2330 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2332 panel->search_char[panel->search_chpoint] = c_code;
2333 panel->search_chpoint++;
2336 if (panel->search_chpoint > 0)
2338 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2340 case -2:
2341 return;
2342 case -1:
2343 panel->search_chpoint = 0;
2344 return;
2345 default:
2346 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2348 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2349 l += panel->search_chpoint;
2350 *(panel->search_buffer + l) = '\0';
2351 panel->search_chpoint = 0;
2357 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2358 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2359 search = mc_search_new (esc_str, -1);
2360 search->search_type = MC_SEARCH_T_GLOB;
2361 search->is_entire_line = TRUE;
2362 switch (panels_options.qsearch_mode)
2364 case QSEARCH_CASE_SENSITIVE:
2365 search->is_case_sensitive = TRUE;
2366 break;
2367 case QSEARCH_CASE_INSENSITIVE:
2368 search->is_case_sensitive = FALSE;
2369 break;
2370 default:
2371 search->is_case_sensitive = panel->sort_info.case_sensitive;
2372 break;
2374 sel = panel->selected;
2375 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2377 if (i >= panel->count)
2379 i = 0;
2380 if (wrapped)
2381 break;
2382 wrapped = TRUE;
2384 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2386 sel = i;
2387 is_found = TRUE;
2388 break;
2391 if (is_found)
2393 unselect_item (panel);
2394 panel->selected = sel;
2395 select_item (panel);
2396 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2398 else if (c_code != KEY_BACKSPACE)
2400 act = panel->search_buffer + l;
2401 str_prev_noncomb_char (&act, panel->search_buffer);
2402 act[0] = '\0';
2404 mc_search_free (search);
2405 g_free (reg_exp);
2406 g_free (esc_str);
2409 /* --------------------------------------------------------------------------------------------- */
2410 /** Start new search.
2411 * @param panel instance of WPanel structure
2414 static void
2415 start_search (WPanel * panel)
2417 if (panel->searching)
2419 if (panel->selected + 1 == panel->count)
2420 panel->selected = 0;
2421 else
2422 move_down (panel);
2424 /* in case if there was no search string we need to recall
2425 previous string, with which we ended previous searching */
2426 if (panel->search_buffer[0] == '\0')
2427 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2428 sizeof (panel->search_buffer));
2430 do_search (panel, 0);
2432 else
2434 panel->searching = TRUE;
2435 panel->search_buffer[0] = '\0';
2436 panel->search_char[0] = '\0';
2437 panel->search_chpoint = 0;
2438 display_mini_info (panel);
2439 mc_refresh ();
2443 /* --------------------------------------------------------------------------------------------- */
2445 static void
2446 stop_search (WPanel * panel)
2448 panel->searching = FALSE;
2450 /* if user had overrdied search string, we need to store it
2451 to the previous_search_buffer */
2452 if (panel->search_buffer[0] != '\0')
2453 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2454 sizeof (panel->prev_search_buffer));
2456 display_mini_info (panel);
2459 /* --------------------------------------------------------------------------------------------- */
2460 /** Return 1 if the Enter key has been processed, 0 otherwise */
2462 static int
2463 do_enter_on_file_entry (file_entry * fe)
2465 vfs_path_t *full_name_vpath;
2466 gboolean ok;
2469 * Directory or link to directory - change directory.
2470 * Try the same for the entries on which mc_lstat() has failed.
2472 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2474 vfs_path_t *fname_vpath;
2476 fname_vpath = vfs_path_from_str (fe->fname);
2477 if (!do_cd (fname_vpath, cd_exact))
2478 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2479 vfs_path_free (fname_vpath);
2480 return 1;
2483 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2485 /* Try associated command */
2486 if (regex_command (full_name_vpath, "Open", NULL) != 0)
2488 vfs_path_free (full_name_vpath);
2489 return 1;
2492 /* Check if the file is executable */
2493 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2494 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2495 vfs_path_free (full_name_vpath);
2496 if (!ok)
2497 return 0;
2499 if (confirm_execute)
2501 if (query_dialog
2502 (_("The Midnight Commander"),
2503 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2504 return 1;
2507 if (!vfs_current_is_local ())
2509 int ret;
2510 vfs_path_t *tmp_vpath;
2512 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2513 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2514 vfs_path_free (tmp_vpath);
2515 /* We took action only if the dialog was shown or the execution
2516 * was successful */
2517 return confirm_execute || (ret == 0);
2521 char *tmp = name_quote (fe->fname, 0);
2522 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2523 g_free (tmp);
2524 shell_execute (cmd, 0);
2525 g_free (cmd);
2528 #ifdef HAVE_CHARSET
2529 mc_global.source_codepage = default_source_codepage;
2530 #endif
2532 return 1;
2535 /* --------------------------------------------------------------------------------------------- */
2537 static int
2538 do_enter (WPanel * panel)
2540 return do_enter_on_file_entry (selection (panel));
2543 /* --------------------------------------------------------------------------------------------- */
2545 static void
2546 chdir_other_panel (WPanel * panel)
2548 const file_entry *entry = &panel->dir.list[panel->selected];
2550 vfs_path_t *new_dir_vpath;
2551 char *sel_entry = NULL;
2553 if (get_other_type () != view_listing)
2555 set_display_type (get_other_index (), view_listing);
2558 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2559 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2560 else
2562 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2563 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2566 change_panel ();
2567 do_cd (new_dir_vpath, cd_exact);
2568 vfs_path_free (new_dir_vpath);
2570 if (sel_entry)
2571 try_to_select (current_panel, sel_entry);
2572 change_panel ();
2574 move_down (panel);
2577 /* --------------------------------------------------------------------------------------------- */
2579 * Make the current directory of the current panel also the current
2580 * directory of the other panel. Put the other panel to the listing
2581 * mode if needed. If the current panel is panelized, the other panel
2582 * doesn't become panelized.
2585 static void
2586 panel_sync_other (const WPanel * panel)
2588 if (get_other_type () != view_listing)
2590 set_display_type (get_other_index (), view_listing);
2593 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2595 /* try to select current filename on the other panel */
2596 if (!panel->is_panelized)
2598 try_to_select (other_panel, selection (panel)->fname);
2602 /* --------------------------------------------------------------------------------------------- */
2604 static void
2605 chdir_to_readlink (WPanel * panel)
2607 vfs_path_t *new_dir_vpath;
2608 char buffer[MC_MAXPATHLEN], *p;
2609 int i;
2610 struct stat st;
2611 vfs_path_t *panel_fname_vpath;
2612 gboolean ok;
2614 if (get_other_type () != view_listing)
2615 return;
2617 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2618 return;
2620 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2621 if (i < 0)
2622 return;
2624 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2625 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2626 vfs_path_free (panel_fname_vpath);
2627 if (!ok)
2628 return;
2630 buffer[i] = 0;
2631 if (!S_ISDIR (st.st_mode))
2633 p = strrchr (buffer, PATH_SEP);
2634 if (p && !p[1])
2636 *p = 0;
2637 p = strrchr (buffer, PATH_SEP);
2639 if (!p)
2640 return;
2641 p[1] = 0;
2643 if (*buffer == PATH_SEP)
2644 new_dir_vpath = vfs_path_from_str (buffer);
2645 else
2646 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2648 change_panel ();
2649 do_cd (new_dir_vpath, cd_exact);
2650 vfs_path_free (new_dir_vpath);
2651 change_panel ();
2653 move_down (panel);
2656 /* --------------------------------------------------------------------------------------------- */
2658 static gsize
2659 panel_get_format_field_count (WPanel * panel)
2661 format_e *format;
2662 gsize lc_index;
2663 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2664 return lc_index;
2667 /* --------------------------------------------------------------------------------------------- */
2669 function return 0 if not found and REAL_INDEX+1 if found
2672 static gsize
2673 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2675 format_e *format;
2676 gsize lc_index;
2678 for (lc_index = 1, format = panel->format;
2679 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2680 if (format == NULL)
2681 lc_index = 0;
2683 return lc_index;
2686 /* --------------------------------------------------------------------------------------------- */
2688 static format_e *
2689 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2691 format_e *format;
2692 for (format = panel->format;
2693 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2694 return format;
2697 /* --------------------------------------------------------------------------------------------- */
2699 static const panel_field_t *
2700 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2702 const panel_field_t *pfield;
2703 format_e *format;
2705 format = panel_get_format_field_by_index (panel, lc_index);
2706 if (format == NULL)
2707 return NULL;
2708 pfield = panel_get_field_by_title (format->title);
2709 if (pfield == NULL)
2710 return NULL;
2711 if (pfield->sort_routine == NULL)
2712 return NULL;
2713 return pfield;
2716 /* --------------------------------------------------------------------------------------------- */
2718 static void
2719 panel_toggle_sort_order_prev (WPanel * panel)
2721 gsize lc_index, i;
2722 gchar *title;
2724 const panel_field_t *pfield = NULL;
2726 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2727 lc_index = panel_get_format_field_index_by_name (panel, title);
2728 g_free (title);
2730 if (lc_index > 1)
2732 /* search for prev sortable column in panel format */
2733 for (i = lc_index - 1;
2734 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2737 if (pfield == NULL)
2739 /* Sortable field not found. Try to search in each array */
2740 for (i = panel_get_format_field_count (panel);
2741 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2744 if (pfield != NULL)
2746 panel->sort_info.sort_field = pfield;
2747 panel_set_sort_order (panel, pfield);
2751 /* --------------------------------------------------------------------------------------------- */
2753 static void
2754 panel_toggle_sort_order_next (WPanel * panel)
2756 gsize lc_index, i;
2757 const panel_field_t *pfield = NULL;
2758 gsize format_field_count;
2759 gchar *title;
2761 format_field_count = panel_get_format_field_count (panel);
2762 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2763 lc_index = panel_get_format_field_index_by_name (panel, title);
2764 g_free (title);
2766 if (lc_index != 0 && lc_index != format_field_count)
2768 /* search for prev sortable column in panel format */
2769 for (i = lc_index;
2770 i != format_field_count
2771 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2774 if (pfield == NULL)
2776 /* Sortable field not found. Try to search in each array */
2777 for (i = 0;
2778 i != format_field_count
2779 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2782 if (pfield != NULL)
2784 panel->sort_info.sort_field = pfield;
2785 panel_set_sort_order (panel, pfield);
2789 /* --------------------------------------------------------------------------------------------- */
2791 static void
2792 panel_select_sort_order (WPanel * panel)
2794 const panel_field_t *sort_order;
2796 sort_order = sort_box (&panel->sort_info);
2797 if (sort_order != NULL)
2799 panel->sort_info.sort_field = sort_order;
2800 panel_set_sort_order (panel, sort_order);
2804 /* --------------------------------------------------------------------------------------------- */
2806 static void
2807 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2809 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2811 const panel_field_t *sort_order;
2813 sort_order = panel_get_field_by_id (name);
2814 if (sort_order == NULL)
2815 return;
2816 panel->sort_info.sort_field = sort_order;
2818 else
2819 panel->sort_info.reverse = !panel->sort_info.reverse;
2821 panel_set_sort_order (panel, panel->sort_info.sort_field);
2824 /* --------------------------------------------------------------------------------------------- */
2826 * If we moved to the parent directory move the selection pointer to
2827 * the old directory name; If we leave VFS dir, remove FS specificator.
2829 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2832 static const char *
2833 get_parent_dir_name (const char *cwd, const char *lwd)
2835 size_t llen, clen;
2836 const char *p;
2838 llen = strlen (lwd);
2839 clen = strlen (cwd);
2841 if (llen <= clen)
2842 return NULL;
2844 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2846 if (p == NULL)
2848 p = strrchr (lwd, PATH_SEP);
2850 if ((p != NULL)
2851 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2852 && (clen == (size_t) (p - lwd)
2853 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2854 return (p + 1);
2856 return NULL;
2859 /* skip VFS prefix */
2860 while (--p > lwd && *p != PATH_SEP)
2862 /* get last component */
2863 while (--p > lwd && *p != PATH_SEP)
2866 /* return last component */
2867 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
2870 /* --------------------------------------------------------------------------------------------- */
2871 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2873 static void
2874 subshell_chdir (const vfs_path_t * vpath)
2876 #ifdef HAVE_SUBSHELL_SUPPORT
2877 if (mc_global.tty.use_subshell && vfs_current_is_local ())
2878 do_subshell_chdir (vpath, FALSE, TRUE);
2879 #else /* HAVE_SUBSHELL_SUPPORT */
2880 (void) vpath;
2881 #endif /* HAVE_SUBSHELL_SUPPORT */
2884 /* --------------------------------------------------------------------------------------------- */
2886 * Changes the current directory of the panel.
2887 * Don't record change in the directory history.
2890 static gboolean
2891 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
2893 char *olddir;
2895 olddir = vfs_path_to_str (panel->cwd_vpath);
2897 /* Convert *new_path to a suitable pathname, handle ~user */
2898 if (cd_type == cd_parse_command)
2900 const vfs_path_element_t *element;
2902 element = vfs_path_get_by_index (new_dir_vpath, 0);
2903 if (strcmp (element->path, "-") == 0)
2904 new_dir_vpath = panel->lwd_vpath;
2907 if (mc_chdir (new_dir_vpath) == -1)
2909 panel_set_cwd (panel, olddir);
2910 g_free (olddir);
2911 return FALSE;
2914 /* Success: save previous directory, shutdown status of previous dir */
2915 panel_set_lwd (panel, olddir);
2916 input_free_completions (cmdline);
2918 vfs_path_free (panel->cwd_vpath);
2919 vfs_setup_cwd ();
2920 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
2922 vfs_release_path (olddir);
2924 subshell_chdir (panel->cwd_vpath);
2926 /* Reload current panel */
2927 panel_clean_dir (panel);
2930 char *tmp_path;
2932 panel->count =
2933 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
2934 panel->sort_info.reverse, panel->sort_info.case_sensitive,
2935 panel->sort_info.exec_first, panel->filter);
2936 tmp_path = vfs_path_to_str (panel->cwd_vpath);
2937 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
2938 g_free (tmp_path);
2941 load_hint (0);
2942 panel->dirty = 1;
2943 update_xterm_title_path ();
2945 g_free (olddir);
2947 return TRUE;
2950 /* --------------------------------------------------------------------------------------------- */
2952 static void
2953 directory_history_next (WPanel * panel)
2955 GList *nextdir;
2957 nextdir = g_list_next (panel->dir_history);
2958 if (nextdir != NULL)
2960 vfs_path_t *data_vpath;
2962 data_vpath = vfs_path_from_str ((char *) nextdir->data);
2963 if (_do_panel_cd (panel, data_vpath, cd_exact))
2964 panel->dir_history = nextdir;
2965 vfs_path_free (data_vpath);
2969 /* --------------------------------------------------------------------------------------------- */
2971 static void
2972 directory_history_prev (WPanel * panel)
2974 GList *prevdir;
2976 prevdir = g_list_previous (panel->dir_history);
2978 if (prevdir != NULL)
2980 vfs_path_t *data_vpath;
2982 data_vpath = vfs_path_from_str ((char *) prevdir->data);
2983 if (_do_panel_cd (panel, data_vpath, cd_exact))
2984 panel->dir_history = prevdir;
2985 vfs_path_free (data_vpath);
2989 /* --------------------------------------------------------------------------------------------- */
2991 static void
2992 directory_history_list (WPanel * panel)
2994 char *s;
2996 s = history_show (&panel->dir_history, &panel->widget);
2998 if (s != NULL)
3000 vfs_path_t *s_vpath;
3002 s_vpath = vfs_path_from_str (s);
3003 if (_do_panel_cd (panel, s_vpath, cd_exact))
3005 char *tmp_path;
3007 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3008 directory_history_add (panel, tmp_path);
3009 g_free (tmp_path);
3011 else
3012 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3013 vfs_path_free (s_vpath);
3014 g_free (s);
3018 /* --------------------------------------------------------------------------------------------- */
3020 static cb_ret_t
3021 panel_execute_cmd (WPanel * panel, unsigned long command)
3023 int res = MSG_HANDLED;
3025 if (command != CK_Search)
3026 stop_search (panel);
3029 switch (command)
3031 case CK_Up:
3032 case CK_Down:
3033 case CK_Left:
3034 case CK_Right:
3035 case CK_Bottom:
3036 case CK_Top:
3037 case CK_PageDown:
3038 case CK_PageUp:
3039 /* reset state of marks flag */
3040 state_mark = -1;
3041 break;
3043 switch (command)
3045 case CK_PanelOtherCd:
3046 chdir_other_panel (panel);
3047 break;
3048 case CK_PanelOtherCdLink:
3049 chdir_to_readlink (panel);
3050 break;
3051 case CK_CopySingle:
3052 copy_cmd_local ();
3053 break;
3054 case CK_DeleteSingle:
3055 delete_cmd_local ();
3056 break;
3057 case CK_Enter:
3058 do_enter (panel);
3059 break;
3060 case CK_ViewRaw:
3061 view_raw_cmd ();
3062 break;
3063 case CK_EditNew:
3064 edit_cmd_new ();
3065 break;
3066 case CK_MoveSingle:
3067 rename_cmd_local ();
3068 break;
3069 case CK_SelectInvert:
3070 select_invert_cmd ();
3071 break;
3072 case CK_Select:
3073 select_cmd ();
3074 break;
3075 case CK_Unselect:
3076 unselect_cmd ();
3077 break;
3078 case CK_PageDown:
3079 next_page (panel);
3080 break;
3081 case CK_PageUp:
3082 prev_page (panel);
3083 break;
3084 case CK_CdChild:
3085 goto_child_dir (panel);
3086 break;
3087 case CK_CdParent:
3088 goto_parent_dir (panel);
3089 break;
3090 case CK_History:
3091 directory_history_list (panel);
3092 break;
3093 case CK_HistoryNext:
3094 directory_history_next (panel);
3095 break;
3096 case CK_HistoryPrev:
3097 directory_history_prev (panel);
3098 break;
3099 case CK_BottomOnScreen:
3100 goto_bottom_file (panel);
3101 break;
3102 case CK_MiddleOnScreen:
3103 goto_middle_file (panel);
3104 break;
3105 case CK_TopOnScreen:
3106 goto_top_file (panel);
3107 break;
3108 case CK_Mark:
3109 mark_file (panel);
3110 break;
3111 case CK_MarkUp:
3112 mark_file_up (panel);
3113 break;
3114 case CK_MarkDown:
3115 mark_file_down (panel);
3116 break;
3117 case CK_MarkLeft:
3118 mark_file_left (panel);
3119 break;
3120 case CK_MarkRight:
3121 mark_file_right (panel);
3122 break;
3123 case CK_CdParentSmart:
3124 res = force_maybe_cd ();
3125 break;
3126 case CK_Up:
3127 move_up (panel);
3128 break;
3129 case CK_Down:
3130 move_down (panel);
3131 break;
3132 case CK_Left:
3133 res = move_left (panel);
3134 break;
3135 case CK_Right:
3136 res = move_right (panel);
3137 break;
3138 case CK_Bottom:
3139 move_end (panel);
3140 break;
3141 case CK_Top:
3142 move_home (panel);
3143 break;
3144 #ifdef HAVE_CHARSET
3145 case CK_SelectCodepage:
3146 panel_change_encoding (panel);
3147 break;
3148 #endif
3149 case CK_Search:
3150 start_search (panel);
3151 break;
3152 case CK_SearchStop:
3153 break;
3154 case CK_PanelOtherSync:
3155 panel_sync_other (panel);
3156 break;
3157 case CK_Sort:
3158 panel_select_sort_order (panel);
3159 break;
3160 case CK_SortPrev:
3161 panel_toggle_sort_order_prev (panel);
3162 break;
3163 case CK_SortNext:
3164 panel_toggle_sort_order_next (panel);
3165 break;
3166 case CK_SortReverse:
3167 panel->sort_info.reverse = !panel->sort_info.reverse;
3168 panel_set_sort_order (panel, panel->sort_info.sort_field);
3169 break;
3170 case CK_SortByName:
3171 panel_set_sort_type_by_id (panel, "name");
3172 break;
3173 case CK_SortByExt:
3174 panel_set_sort_type_by_id (panel, "extension");
3175 break;
3176 case CK_SortBySize:
3177 panel_set_sort_type_by_id (panel, "size");
3178 break;
3179 case CK_SortByMTime:
3180 panel_set_sort_type_by_id (panel, "mtime");
3181 break;
3183 return res;
3186 /* --------------------------------------------------------------------------------------------- */
3188 static cb_ret_t
3189 panel_key (WPanel * panel, int key)
3191 size_t i;
3193 if (is_abort_char (key))
3195 stop_search (panel);
3196 return MSG_HANDLED;
3199 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3201 do_search (panel, key);
3202 return MSG_HANDLED;
3205 for (i = 0; panel_map[i].key != 0; i++)
3206 if (key == panel_map[i].key)
3207 return panel_execute_cmd (panel, panel_map[i].command);
3209 if (panels_options.torben_fj_mode && key == ALT ('h'))
3211 goto_middle_file (panel);
3212 return MSG_HANDLED;
3215 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3217 start_search (panel);
3218 do_search (panel, key);
3219 return MSG_HANDLED;
3222 return MSG_NOT_HANDLED;
3225 /* --------------------------------------------------------------------------------------------- */
3227 static cb_ret_t
3228 panel_callback (Widget * w, widget_msg_t msg, int parm)
3230 WPanel *panel = (WPanel *) w;
3231 WButtonBar *bb;
3233 switch (msg)
3235 case WIDGET_INIT:
3236 /* subscribe to "history_load" event */
3237 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3238 /* subscribe to "history_save" event */
3239 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3240 return MSG_HANDLED;
3242 case WIDGET_DRAW:
3243 /* Repaint everything, including frame and separator */
3244 paint_frame (panel); /* including show_dir */
3245 paint_dir (panel);
3246 mini_info_separator (panel);
3247 display_mini_info (panel);
3248 panel->dirty = 0;
3249 return MSG_HANDLED;
3251 case WIDGET_FOCUS:
3252 state_mark = -1;
3253 current_panel = panel;
3254 panel->active = 1;
3255 if (mc_chdir (panel->cwd_vpath) != 0)
3257 char *cwd;
3259 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3260 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3261 cwd, unix_error_string (errno));
3262 g_free (cwd);
3264 else
3265 subshell_chdir (panel->cwd_vpath);
3267 update_xterm_title_path ();
3268 select_item (panel);
3269 show_dir (panel);
3270 paint_dir (panel);
3271 panel->dirty = 0;
3273 bb = find_buttonbar (panel->widget.owner);
3274 midnight_set_buttonbar (bb);
3275 buttonbar_redraw (bb);
3276 return MSG_HANDLED;
3278 case WIDGET_UNFOCUS:
3279 /* Janne: look at this for the multiple panel options */
3280 stop_search (panel);
3281 panel->active = 0;
3282 show_dir (panel);
3283 unselect_item (panel);
3284 return MSG_HANDLED;
3286 case WIDGET_KEY:
3287 return panel_key (panel, parm);
3289 case WIDGET_COMMAND:
3290 return panel_execute_cmd (panel, parm);
3292 case WIDGET_DESTROY:
3293 /* unsubscribe from "history_load" event */
3294 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3295 /* unsubscribe from "history_save" event */
3296 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3297 panel_destroy (panel);
3298 free_my_statfs ();
3299 return MSG_HANDLED;
3301 default:
3302 return default_proc (msg, parm);
3306 /* --------------------------------------------------------------------------------------------- */
3307 /* */
3308 /* Panel mouse events support routines */
3309 /* */
3311 static void
3312 mouse_toggle_mark (WPanel * panel)
3314 do_mark_file (panel, MARK_DONT_MOVE);
3315 mouse_marking = selection (panel)->f.marked;
3316 mouse_mark_panel = current_panel;
3319 /* --------------------------------------------------------------------------------------------- */
3321 static void
3322 mouse_set_mark (WPanel * panel)
3325 if (mouse_mark_panel == panel)
3327 if (mouse_marking && !(selection (panel)->f.marked))
3328 do_mark_file (panel, MARK_DONT_MOVE);
3329 else if (!mouse_marking && (selection (panel)->f.marked))
3330 do_mark_file (panel, MARK_DONT_MOVE);
3334 /* --------------------------------------------------------------------------------------------- */
3336 static int
3337 mark_if_marking (WPanel * panel, Gpm_Event * event)
3339 if (event->buttons & GPM_B_RIGHT)
3341 if (event->type & GPM_DOWN)
3342 mouse_toggle_mark (panel);
3343 else
3344 mouse_set_mark (panel);
3345 return 1;
3347 return 0;
3350 /* --------------------------------------------------------------------------------------------- */
3351 /** Determine which column was clicked, and sort the panel on
3352 * that column, or reverse sort on that column if already
3353 * sorted on that column.
3356 static void
3357 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3359 int i;
3360 const char *lc_sort_name = NULL;
3361 panel_field_t *col_sort_format = NULL;
3362 format_e *format;
3363 gchar *title;
3365 for (i = 0, format = panel->format; format != NULL; format = format->next)
3367 i += format->field_len;
3368 if (event->x < i + 1)
3370 /* found column */
3371 lc_sort_name = format->title;
3372 break;
3376 if (lc_sort_name == NULL)
3377 return;
3379 for (i = 0; panel_fields[i].id != NULL; i++)
3381 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3382 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3384 col_sort_format = &panel_fields[i];
3385 g_free (title);
3386 break;
3388 g_free (title);
3391 if (col_sort_format == NULL)
3392 return;
3394 if (panel->sort_info.sort_field == col_sort_format)
3396 /* reverse the sort if clicked column is already the sorted column */
3397 panel->sort_info.reverse = !panel->sort_info.reverse;
3399 else
3401 /* new sort is forced to be ascending */
3402 panel->sort_info.reverse = FALSE;
3404 panel_set_sort_order (panel, col_sort_format);
3408 /* --------------------------------------------------------------------------------------------- */
3410 * Mouse callback of the panel minus repainting.
3411 * If the event is redirected to the menu, *redir is set to TRUE.
3413 static int
3414 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3416 const int lines = llines (panel);
3417 const gboolean is_active = dlg_widget_active (panel);
3418 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3420 *redir = FALSE;
3422 /* 1st line */
3423 if (mouse_down && event->y == 1)
3425 /* "<" button */
3426 if (event->x == 2)
3428 directory_history_prev (panel);
3429 return MOU_NORMAL;
3432 /* "." button show/hide hidden files */
3433 if (event->x == panel->widget.cols - 5)
3435 panel->widget.owner->callback (panel->widget.owner, NULL,
3436 DLG_ACTION, CK_ShowHidden, NULL);
3437 repaint_screen ();
3438 return MOU_NORMAL;
3441 /* ">" button */
3442 if (event->x == panel->widget.cols - 1)
3444 directory_history_next (panel);
3445 return MOU_NORMAL;
3448 /* "^" button */
3449 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3451 directory_history_list (panel);
3452 return MOU_NORMAL;
3455 /* rest of the upper frame, the menu is invisible - call menu */
3456 if (!menubar_visible)
3458 *redir = TRUE;
3459 event->x += panel->widget.x;
3460 return the_menubar->widget.mouse (event, the_menubar);
3463 /* no other events on 1st line */
3464 return MOU_NORMAL;
3467 /* sort on clicked column; don't handle wheel events */
3468 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3470 mouse_sort_col (event, panel);
3471 return MOU_NORMAL;
3474 /* Mouse wheel events */
3475 if (mouse_down && (event->buttons & GPM_B_UP))
3477 if (is_active)
3479 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3480 prev_page (panel);
3481 else /* We are in first page */
3482 move_up (panel);
3484 return MOU_NORMAL;
3487 if (mouse_down && (event->buttons & GPM_B_DOWN))
3489 if (is_active)
3491 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3492 next_page (panel);
3493 else /* We are in last page */
3494 move_down (panel);
3496 return MOU_NORMAL;
3499 event->y -= 2;
3500 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3502 int my_index;
3504 if (!is_active)
3505 change_panel ();
3507 if (panel->top_file + event->y > panel->count)
3508 my_index = panel->count - 1;
3509 else
3511 my_index = panel->top_file + event->y - 1;
3512 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3513 my_index += llines (panel);
3515 if (my_index >= panel->count)
3516 my_index = panel->count - 1;
3519 if (my_index != panel->selected)
3521 unselect_item (panel);
3522 panel->selected = my_index;
3523 select_item (panel);
3526 /* This one is new */
3527 mark_if_marking (panel, event);
3529 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3531 if (event->y > 0 && event->y <= lines)
3532 do_enter (panel);
3534 return MOU_NORMAL;
3537 /* --------------------------------------------------------------------------------------------- */
3538 /** Mouse callback of the panel */
3540 static int
3541 panel_event (Gpm_Event * event, void *data)
3543 WPanel *panel = data;
3544 int ret;
3545 gboolean redir;
3547 ret = do_panel_event (event, panel, &redir);
3548 if (!redir)
3549 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3551 return ret;
3554 /* --------------------------------------------------------------------------------------------- */
3556 static void
3557 reload_panelized (WPanel * panel)
3559 int i, j;
3560 dir_list *list = &panel->dir;
3562 if (panel != current_panel)
3563 (void) mc_chdir (panel->cwd_vpath);
3565 for (i = 0, j = 0; i < panel->count; i++)
3567 vfs_path_t *vpath;
3569 if (list->list[i].f.marked)
3571 /* Unmark the file in advance. In case the following mc_lstat
3572 * fails we are done, else we have to mark the file again
3573 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3574 * IMO that's the best way to update the panel's summary status
3575 * -- Norbert
3577 do_file_mark (panel, i, 0);
3579 vpath = vfs_path_from_str (list->list[i].fname);
3580 if (mc_lstat (vpath, &list->list[i].st))
3581 g_free (list->list[i].fname);
3582 else
3584 if (list->list[i].f.marked)
3585 do_file_mark (panel, i, 1);
3586 if (j != i)
3587 list->list[j] = list->list[i];
3588 j++;
3590 vfs_path_free (vpath);
3592 if (j == 0)
3593 panel->count = set_zero_dir (list) ? 1 : 0;
3594 else
3595 panel->count = j;
3597 if (panel != current_panel)
3598 (void) mc_chdir (current_panel->cwd_vpath);
3601 /* --------------------------------------------------------------------------------------------- */
3603 static void
3604 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3606 gboolean free_pointer;
3607 char *my_current_file = NULL;
3609 if ((flags & UP_RELOAD) != 0)
3611 panel->is_panelized = FALSE;
3612 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3613 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3616 /* If current_file == -1 (an invalid pointer) then preserve selection */
3617 free_pointer = current_file == UP_KEEPSEL;
3619 if (free_pointer)
3621 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3622 current_file = my_current_file;
3625 if (panel->is_panelized)
3626 reload_panelized (panel);
3627 else
3628 panel_reload (panel);
3630 try_to_select (panel, current_file);
3631 panel->dirty = 1;
3633 if (free_pointer)
3634 g_free (my_current_file);
3637 /* --------------------------------------------------------------------------------------------- */
3639 static void
3640 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3642 if (get_display_type (which) == view_listing)
3644 WPanel *panel;
3646 panel = (WPanel *) get_panel_widget (which);
3647 if (panel->is_panelized)
3648 flags &= ~UP_RELOAD;
3649 update_one_panel_widget (panel, flags, current_file);
3653 /* --------------------------------------------------------------------------------------------- */
3655 static void
3656 do_select (WPanel * panel, int i)
3658 if (i != panel->selected)
3660 panel->dirty = 1;
3661 panel->selected = i;
3662 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3663 if (panel->top_file < 0)
3664 panel->top_file = 0;
3668 /* --------------------------------------------------------------------------------------------- */
3670 static void
3671 do_try_to_select (WPanel * panel, const char *name)
3673 int i;
3674 char *subdir;
3676 if (!name)
3678 do_select (panel, 0);
3679 return;
3682 /* We only want the last component of the directory,
3683 * and from this only the name without suffix.
3684 * Cut prefix if the panel is not panelized */
3686 if (panel->is_panelized)
3687 subdir = vfs_strip_suffix_from_filename (name);
3688 else
3689 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3691 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3692 for (i = 0; i < panel->count; i++)
3694 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3696 do_select (panel, i);
3697 g_free (subdir);
3698 return;
3702 /* Try to select a file near the file that is missing */
3703 if (panel->selected >= panel->count)
3704 do_select (panel, panel->count - 1);
3705 g_free (subdir);
3708 /* --------------------------------------------------------------------------------------------- */
3710 /* event callback */
3711 static gboolean
3712 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3713 gpointer init_data, gpointer data)
3715 (void) event_group_name;
3716 (void) event_name;
3717 (void) init_data;
3718 (void) data;
3720 update_panels (UP_RELOAD, UP_KEEPSEL);
3722 return TRUE;
3725 /* --------------------------------------------------------------------------------------------- */
3727 /* event callback */
3728 static gboolean
3729 panel_save_curent_file_to_clip_file (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 if (current_panel->marked == 0)
3738 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3739 (gpointer) selection (current_panel)->fname);
3740 else
3742 int i;
3743 gboolean first = TRUE;
3744 char *flist = NULL;
3746 for (i = 0; i < current_panel->count; i++)
3747 if (current_panel->dir.list[i].f.marked != 0)
3748 { /* Skip the unmarked ones */
3749 if (first)
3751 flist = g_strdup (current_panel->dir.list[i].fname);
3752 first = FALSE;
3754 else
3756 /* Add empty lines after the file */
3757 char *tmp;
3759 tmp =
3760 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3761 g_free (flist);
3762 flist = tmp;
3766 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3767 g_free (flist);
3769 return TRUE;
3772 /* --------------------------------------------------------------------------------------------- */
3773 /*** public functions ****************************************************************************/
3774 /* --------------------------------------------------------------------------------------------- */
3776 void
3777 try_to_select (WPanel * panel, const char *name)
3779 do_try_to_select (panel, name);
3780 select_item (panel);
3783 /* --------------------------------------------------------------------------------------------- */
3785 void
3786 panel_clean_dir (WPanel * panel)
3788 int count = panel->count;
3790 panel->count = 0;
3791 panel->top_file = 0;
3792 panel->selected = 0;
3793 panel->marked = 0;
3794 panel->dirs_marked = 0;
3795 panel->total = 0;
3796 panel->searching = FALSE;
3797 panel->is_panelized = FALSE;
3798 panel->dirty = 1;
3800 clean_dir (&panel->dir, count);
3803 /* --------------------------------------------------------------------------------------------- */
3805 * Set Up panel's current dir object
3807 * @param panel panel object
3808 * @param path_str string contain path
3811 void
3812 panel_set_cwd (WPanel * panel, const char *path_str)
3814 vfs_path_free (panel->cwd_vpath);
3815 panel->cwd_vpath = vfs_path_from_str (path_str);
3818 /* --------------------------------------------------------------------------------------------- */
3820 * Set Up panel's last working dir object
3822 * @param panel panel object
3823 * @param path_str string contain path
3826 void
3827 panel_set_lwd (WPanel * panel, const char *path_str)
3829 vfs_path_free (panel->lwd_vpath);
3830 panel->lwd_vpath = vfs_path_from_str (path_str);
3833 /* --------------------------------------------------------------------------------------------- */
3834 /** Panel creation.
3835 * @param panel_name the name of the panel for setup retieving
3836 * @returns new instance of WPanel
3839 WPanel *
3840 panel_new (const char *panel_name)
3842 return panel_new_with_dir (panel_name, NULL);
3845 /* --------------------------------------------------------------------------------------------- */
3846 /** Panel creation for specified directory.
3847 * @param panel_name specifies the name of the panel for setup retieving
3848 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3849 * @returns new instance of WPanel
3852 WPanel *
3853 panel_new_with_dir (const char *panel_name, const char *wpath)
3855 WPanel *panel;
3856 char *section;
3857 int i, err;
3858 char *curdir = NULL;
3860 panel = g_new0 (WPanel, 1);
3862 /* No know sizes of the panel at startup */
3863 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3865 /* We do not want the cursor */
3866 widget_want_cursor (panel->widget, 0);
3868 if (wpath != NULL)
3870 curdir = _vfs_get_cwd ();
3871 panel_set_cwd (panel, wpath);
3873 else
3875 vfs_setup_cwd ();
3876 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3879 panel_set_lwd (panel, ".");
3881 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3882 /* directories history will be get later */
3884 panel->dir.list = g_new (file_entry, MIN_FILES);
3885 panel->dir.size = MIN_FILES;
3886 panel->active = 0;
3887 panel->filter = 0;
3888 panel->split = 0;
3889 panel->top_file = 0;
3890 panel->selected = 0;
3891 panel->marked = 0;
3892 panel->total = 0;
3893 panel->dirty = 1;
3894 panel->searching = FALSE;
3895 panel->dirs_marked = 0;
3896 panel->is_panelized = FALSE;
3897 panel->format = 0;
3898 panel->status_format = 0;
3899 panel->format_modified = 1;
3901 panel->panel_name = g_strdup (panel_name);
3902 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3904 #ifdef HAVE_CHARSET
3905 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3906 #endif
3908 for (i = 0; i < LIST_TYPES; i++)
3909 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3911 panel->search_buffer[0] = '\0';
3912 panel->prev_search_buffer[0] = '\0';
3913 panel->frame_size = frame_half;
3915 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3916 if (!mc_config_has_group (mc_main_config, section))
3918 g_free (section);
3919 section = g_strdup (panel->panel_name);
3921 panel_load_setup (panel, section);
3922 g_free (section);
3924 /* Load format strings */
3925 err = set_panel_formats (panel);
3926 if (err != 0)
3927 set_panel_formats (panel);
3929 #ifdef HAVE_CHARSET
3931 const vfs_path_element_t *path_element;
3933 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
3934 if (path_element->encoding != NULL)
3935 panel->codepage = get_codepage_index (path_element->encoding);
3937 #endif
3939 if (mc_chdir (panel->cwd_vpath) != 0)
3941 #ifdef HAVE_CHARSET
3942 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3943 #endif
3944 vfs_setup_cwd ();
3945 vfs_path_free (panel->cwd_vpath);
3946 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3949 /* Load the default format */
3950 panel->count =
3951 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
3952 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3953 panel->sort_info.exec_first, panel->filter);
3955 /* Restore old right path */
3956 if (curdir != NULL)
3958 vfs_path_t *vpath;
3960 vpath = vfs_path_from_str (curdir);
3961 mc_chdir (vpath);
3962 vfs_path_free (vpath);
3964 g_free (curdir);
3966 return panel;
3969 /* --------------------------------------------------------------------------------------------- */
3971 void
3972 panel_reload (WPanel * panel)
3974 struct stat current_stat;
3975 char *tmp_path;
3976 gboolean ok;
3978 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3979 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
3980 && current_stat.st_ctime == panel->dir_stat.st_ctime
3981 && current_stat.st_mtime == panel->dir_stat.st_mtime);
3982 g_free (tmp_path);
3984 if (ok)
3985 return;
3989 char *last_slash;
3991 if (mc_chdir (panel->cwd_vpath) != -1)
3992 break;
3994 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3995 if (tmp_path[0] == PATH_SEP && tmp_path[1] == '\0')
3997 panel_clean_dir (panel);
3998 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
3999 g_free (tmp_path);
4000 return;
4002 last_slash = strrchr (tmp_path, PATH_SEP);
4003 vfs_path_free (panel->cwd_vpath);
4004 if (last_slash == NULL || last_slash == tmp_path)
4005 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4006 else
4008 *last_slash = '\0';
4009 panel->cwd_vpath = vfs_path_from_str (tmp_path);
4011 g_free (tmp_path);
4012 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4013 show_dir (panel);
4015 while (TRUE);
4017 panel->count =
4018 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4019 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4020 panel->sort_info.exec_first, panel->filter);
4022 panel->dirty = 1;
4023 if (panel->selected >= panel->count)
4024 do_select (panel, panel->count - 1);
4026 recalculate_panel_summary (panel);
4029 /* --------------------------------------------------------------------------------------------- */
4030 /* Switches the panel to the mode specified in the format */
4031 /* Seting up both format and status string. Return: 0 - on success; */
4032 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4035 set_panel_formats (WPanel * p)
4037 format_e *form;
4038 char *err = NULL;
4039 int retcode = 0;
4041 form = use_display_format (p, panel_format (p), &err, 0);
4043 if (err != NULL)
4045 g_free (err);
4046 retcode = 1;
4048 else
4050 delete_format (p->format);
4051 p->format = form;
4054 if (panels_options.show_mini_info)
4056 form = use_display_format (p, mini_status_format (p), &err, 1);
4058 if (err != NULL)
4060 g_free (err);
4061 retcode += 2;
4063 else
4065 delete_format (p->status_format);
4066 p->status_format = form;
4070 panel_format_modified (p);
4071 panel_update_cols (&(p->widget), p->frame_size);
4073 if (retcode)
4074 message (D_ERROR, _("Warning"),
4075 _("User supplied format looks invalid, reverting to default."));
4076 if (retcode & 0x01)
4078 g_free (p->user_format);
4079 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4081 if (retcode & 0x02)
4083 g_free (p->user_status_format[p->list_type]);
4084 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4087 return retcode;
4090 /* --------------------------------------------------------------------------------------------- */
4092 void
4093 panel_update_cols (Widget * widget, panel_display_t frame_size)
4095 int cols, origin;
4097 /* don't touch panel if it is not in dialog yet */
4098 /* if panel is not in dialog it is not in widgets list
4099 and cannot be compared with get_panel_widget() result */
4100 if (widget->owner == NULL)
4101 return;
4103 if (panels_layout.horizontal_split)
4105 widget->cols = COLS;
4106 return;
4109 if (frame_size == frame_full)
4111 cols = COLS;
4112 origin = 0;
4114 else if (widget == get_panel_widget (0))
4116 cols = panels_layout.left_panel_size;
4117 origin = 0;
4119 else
4121 cols = COLS - panels_layout.left_panel_size;
4122 origin = panels_layout.left_panel_size;
4125 widget->cols = cols;
4126 widget->x = origin;
4129 /* --------------------------------------------------------------------------------------------- */
4131 /* Select current item and readjust the panel */
4132 void
4133 select_item (WPanel * panel)
4135 /* Although currently all over the code we set the selection and
4136 top file to decent values before calling select_item, I could
4137 forget it someday, so it's better to do the actual fitting here */
4139 if (panel->selected < 0)
4140 panel->selected = 0;
4142 if (panel->selected > panel->count - 1)
4143 panel->selected = panel->count - 1;
4145 adjust_top_file (panel);
4147 panel->dirty = 1;
4149 execute_hooks (select_file_hook);
4152 /* --------------------------------------------------------------------------------------------- */
4153 /** Clears all files in the panel, used only when one file was marked */
4154 void
4155 unmark_files (WPanel * panel)
4157 int i;
4159 if (!panel->marked)
4160 return;
4161 for (i = 0; i < panel->count; i++)
4162 file_mark (panel, i, 0);
4164 panel->dirs_marked = 0;
4165 panel->marked = 0;
4166 panel->total = 0;
4169 /* --------------------------------------------------------------------------------------------- */
4170 /** Recalculate the panels summary information, used e.g. when marked
4171 files might have been removed by an external command */
4173 void
4174 recalculate_panel_summary (WPanel * panel)
4176 int i;
4178 panel->marked = 0;
4179 panel->dirs_marked = 0;
4180 panel->total = 0;
4182 for (i = 0; i < panel->count; i++)
4183 if (panel->dir.list[i].f.marked)
4185 /* do_file_mark will return immediately if newmark == oldmark.
4186 So we have to first unmark it to get panel's summary information
4187 updated. (Norbert) */
4188 panel->dir.list[i].f.marked = 0;
4189 do_file_mark (panel, i, 1);
4193 /* --------------------------------------------------------------------------------------------- */
4194 /** This routine marks a file or a directory */
4196 void
4197 do_file_mark (WPanel * panel, int idx, int mark)
4199 if (panel->dir.list[idx].f.marked == mark)
4200 return;
4202 /* Only '..' can't be marked, '.' isn't visible */
4203 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4204 return;
4206 file_mark (panel, idx, mark);
4207 if (panel->dir.list[idx].f.marked)
4209 panel->marked++;
4210 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4212 if (panel->dir.list[idx].f.dir_size_computed)
4213 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4214 panel->dirs_marked++;
4216 else
4217 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4218 set_colors (panel);
4220 else
4222 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4224 if (panel->dir.list[idx].f.dir_size_computed)
4225 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4226 panel->dirs_marked--;
4228 else
4229 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4230 panel->marked--;
4234 /* --------------------------------------------------------------------------------------------- */
4236 * Changes the current directory of the panel.
4237 * Record change in the directory history.
4239 gboolean
4240 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4242 gboolean r;
4244 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4245 if (r)
4247 char *tmp_path;
4249 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4250 directory_history_add (panel, tmp_path);
4251 g_free (tmp_path);
4253 return r;
4256 /* --------------------------------------------------------------------------------------------- */
4258 void
4259 file_mark (WPanel * panel, int lc_index, int val)
4261 if (panel->dir.list[lc_index].f.marked != val)
4263 panel->dir.list[lc_index].f.marked = val;
4264 panel->dirty = 1;
4268 /* --------------------------------------------------------------------------------------------- */
4270 void
4271 panel_re_sort (WPanel * panel)
4273 char *filename;
4274 int i;
4276 if (panel == NULL)
4277 return;
4279 filename = g_strdup (selection (panel)->fname);
4280 unselect_item (panel);
4281 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4282 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4283 panel->sort_info.exec_first);
4284 panel->selected = -1;
4285 for (i = panel->count; i; i--)
4287 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4289 panel->selected = i - 1;
4290 break;
4293 g_free (filename);
4294 panel->top_file = panel->selected - ITEMS (panel) / 2;
4295 select_item (panel);
4296 panel->dirty = 1;
4299 /* --------------------------------------------------------------------------------------------- */
4301 void
4302 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4304 if (sort_order == NULL)
4305 return;
4307 panel->sort_info.sort_field = sort_order;
4309 /* The directory is already sorted, we have to load the unsorted stuff */
4310 if (sort_order->sort_routine == (sortfn *) unsorted)
4312 char *current_file;
4314 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4315 panel_reload (panel);
4316 try_to_select (panel, current_file);
4317 g_free (current_file);
4319 panel_re_sort (panel);
4322 /* --------------------------------------------------------------------------------------------- */
4324 * Change panel encoding.
4325 * @param panel WPanel object
4328 #ifdef HAVE_CHARSET
4329 void
4330 panel_change_encoding (WPanel * panel)
4332 const char *encoding = NULL;
4333 char *errmsg;
4334 int r;
4336 r = select_charset (-1, -1, panel->codepage, FALSE);
4338 if (r == SELECT_CHARSET_CANCEL)
4339 return; /* Cancel */
4341 panel->codepage = r;
4343 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4345 /* No translation */
4346 vfs_path_t *cd_path_vpath;
4348 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4349 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4350 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4351 show_dir (panel);
4352 vfs_path_free (cd_path_vpath);
4353 return;
4356 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4357 if (errmsg != NULL)
4359 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4360 g_free (errmsg);
4361 return;
4364 encoding = get_codepage_id (panel->codepage);
4365 if (encoding != NULL)
4367 char *cd_path;
4368 vfs_change_encoding (panel->cwd_vpath, encoding);
4370 cd_path = vfs_path_to_str (panel->cwd_vpath);
4371 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4372 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4373 g_free (cd_path);
4377 /* --------------------------------------------------------------------------------------------- */
4380 * Remove encode info from last path element.
4383 vfs_path_t *
4384 remove_encoding_from_path (const vfs_path_t * vpath)
4386 vfs_path_t *ret_vpath;
4387 GString *tmp_conv;
4388 int indx;
4390 ret_vpath = vfs_path_new ();
4392 tmp_conv = g_string_new ("");
4394 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4396 GIConv converter;
4397 vfs_path_element_t *path_element;
4399 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4400 vfs_path_add_element (ret_vpath, path_element);
4402 if (path_element->encoding == NULL)
4403 continue;
4405 converter = str_crt_conv_to (path_element->encoding);
4406 if (converter == INVALID_CONV)
4407 continue;
4409 g_free (path_element->encoding);
4410 path_element->encoding = NULL;
4412 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4414 g_free (path_element->path);
4415 path_element->path = g_strdup (tmp_conv->str);
4417 g_string_set_size (tmp_conv, 0);
4419 str_close_conv (converter);
4420 str_close_conv (path_element->dir.converter);
4421 path_element->dir.converter = INVALID_CONV;
4423 g_string_free (tmp_conv, TRUE);
4424 return ret_vpath;
4426 #endif /* HAVE_CHARSET */
4428 /* --------------------------------------------------------------------------------------------- */
4430 * This routine reloads the directory in both panels. It tries to
4431 * select current_file in current_panel and other_file in other_panel.
4432 * If current_file == -1 then it automatically sets current_file and
4433 * other_file to the currently selected files in the panels.
4435 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4436 * will not reload the other panel.
4439 void
4440 update_panels (panel_update_flags_t flags, const char *current_file)
4442 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4443 WPanel *panel;
4445 update_one_panel (get_current_index (), flags, current_file);
4446 if (reload_other)
4447 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4449 if (get_current_type () == view_listing)
4450 panel = (WPanel *) get_panel_widget (get_current_index ());
4451 else
4452 panel = (WPanel *) get_panel_widget (get_other_index ());
4454 if (!panel->is_panelized)
4455 (void) mc_chdir (panel->cwd_vpath);
4458 /* --------------------------------------------------------------------------------------------- */
4460 void
4461 directory_history_add (struct WPanel *panel, const char *dir)
4463 vfs_path_t *vpath;
4464 char *tmp;
4466 vpath = vfs_path_from_str (dir);
4467 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
4468 vfs_path_free (vpath);
4469 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4472 /* --------------------------------------------------------------------------------------------- */
4474 gsize
4475 panel_get_num_of_sortable_fields (void)
4477 gsize ret = 0, lc_index;
4479 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4480 if (panel_fields[lc_index].is_user_choice)
4481 ret++;
4482 return ret;
4485 /* --------------------------------------------------------------------------------------------- */
4487 const char **
4488 panel_get_sortable_fields (gsize * array_size)
4490 char **ret;
4491 gsize lc_index, i;
4493 lc_index = panel_get_num_of_sortable_fields ();
4495 ret = g_try_new0 (char *, lc_index + 1);
4496 if (ret == NULL)
4497 return NULL;
4499 if (array_size != NULL)
4500 *array_size = lc_index;
4502 lc_index = 0;
4504 for (i = 0; panel_fields[i].id != NULL; i++)
4505 if (panel_fields[i].is_user_choice)
4506 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4507 return (const char **) ret;
4510 /* --------------------------------------------------------------------------------------------- */
4512 const panel_field_t *
4513 panel_get_field_by_id (const char *name)
4515 gsize lc_index;
4516 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4517 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4518 return &panel_fields[lc_index];
4519 return NULL;
4522 /* --------------------------------------------------------------------------------------------- */
4524 const panel_field_t *
4525 panel_get_field_by_title_hotkey (const char *name)
4527 gsize lc_index;
4528 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4529 if (panel_fields[lc_index].title_hotkey != NULL &&
4530 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4531 return &panel_fields[lc_index];
4532 return NULL;
4535 /* --------------------------------------------------------------------------------------------- */
4537 const panel_field_t *
4538 panel_get_field_by_title (const char *name)
4540 gsize lc_index;
4541 gchar *title = NULL;
4543 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4545 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4546 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4548 g_free (title);
4549 return &panel_fields[lc_index];
4552 g_free (title);
4553 return NULL;
4556 /* --------------------------------------------------------------------------------------------- */
4558 gsize
4559 panel_get_num_of_user_possible_fields (void)
4561 gsize ret = 0, lc_index;
4563 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4564 if (panel_fields[lc_index].use_in_user_format)
4565 ret++;
4566 return ret;
4569 /* --------------------------------------------------------------------------------------------- */
4571 const char **
4572 panel_get_user_possible_fields (gsize * array_size)
4574 char **ret;
4575 gsize lc_index, i;
4577 lc_index = panel_get_num_of_user_possible_fields ();
4579 ret = g_try_new0 (char *, lc_index + 1);
4580 if (ret == NULL)
4581 return NULL;
4583 if (array_size != NULL)
4584 *array_size = lc_index;
4586 lc_index = 0;
4588 for (i = 0; panel_fields[i].id != NULL; i++)
4589 if (panel_fields[i].use_in_user_format)
4590 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4591 return (const char **) ret;
4594 /* --------------------------------------------------------------------------------------------- */
4596 void
4597 panel_init (void)
4599 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4600 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4602 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4603 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4604 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4605 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4606 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4608 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4609 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4610 panel_save_curent_file_to_clip_file, NULL, NULL);
4614 /* --------------------------------------------------------------------------------------------- */
4616 void
4617 panel_deinit (void)
4619 g_free (panel_sort_up_sign);
4620 g_free (panel_sort_down_sign);
4622 g_free (panel_hiddenfiles_sign_show);
4623 g_free (panel_hiddenfiles_sign_hide);
4624 g_free (panel_history_prev_item_sign);
4625 g_free (panel_history_next_item_sign);
4626 g_free (panel_history_show_list_sign);
4630 /* --------------------------------------------------------------------------------------------- */