fixed doxygen.cfg, excluded directory /tests/ from doxygen path's
[midnight-commander.git] / src / filemanager / panel.c
blobb47efd9f24843a262113ff3a4a267e4ceb3cce82
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 static void
1312 directory_history_add (struct WPanel *panel, const vfs_path_t * vpath)
1314 char *tmp;
1316 tmp = vfs_path_to_str_flags (vpath, 0, VPF_STRIP_PASSWORD);
1317 panel->dir_history = list_append_unique (panel->dir_history, tmp);
1318 panel->dir_history_current = panel->dir_history;
1321 /* --------------------------------------------------------------------------------------------- */
1323 /* "history_load" event handler */
1324 static gboolean
1325 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1326 gpointer init_data, gpointer data)
1328 WPanel *p = (WPanel *) init_data;
1329 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1331 (void) event_group_name;
1332 (void) event_name;
1334 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1336 if (ev->cfg != NULL)
1337 p->dir_history = history_load (ev->cfg, p->hist_name);
1338 else
1339 p->dir_history = history_get (p->hist_name);
1341 directory_history_add (p, p->cwd_vpath);
1344 return TRUE;
1347 /* --------------------------------------------------------------------------------------------- */
1349 /* "history_save" event handler */
1350 static gboolean
1351 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1352 gpointer init_data, gpointer data)
1354 WPanel *p = (WPanel *) init_data;
1356 (void) event_group_name;
1357 (void) event_name;
1359 if (p->dir_history != NULL)
1361 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1363 history_save (ev->cfg, p->hist_name, p->dir_history);
1366 return TRUE;
1369 /* --------------------------------------------------------------------------------------------- */
1371 static void
1372 panel_destroy (WPanel * p)
1374 size_t i;
1376 if (panels_options.auto_save_setup)
1378 char *name;
1380 name = panel_save_name (p);
1381 panel_save_setup (p, name);
1382 g_free (name);
1385 panel_clean_dir (p);
1387 /* clean history */
1388 if (p->dir_history != NULL)
1390 /* directory history is already saved before this moment */
1391 p->dir_history = g_list_first (p->dir_history);
1392 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1393 g_list_free (p->dir_history);
1395 g_free (p->hist_name);
1397 delete_format (p->format);
1398 delete_format (p->status_format);
1400 g_free (p->user_format);
1401 for (i = 0; i < LIST_TYPES; i++)
1402 g_free (p->user_status_format[i]);
1404 g_free (p->dir.list);
1405 g_free (p->panel_name);
1407 vfs_path_free (p->lwd_vpath);
1408 vfs_path_free (p->cwd_vpath);
1411 /* --------------------------------------------------------------------------------------------- */
1413 static void
1414 panel_format_modified (WPanel * panel)
1416 panel->format_modified = 1;
1419 /* --------------------------------------------------------------------------------------------- */
1421 static void
1422 panel_paint_sort_info (WPanel * panel)
1424 if (*panel->sort_info.sort_field->hotkey != '\0')
1426 const char *sort_sign =
1427 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1428 char *str;
1430 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1431 widget_move (&panel->widget, 1, 1);
1432 tty_print_string (str);
1433 g_free (str);
1437 /* --------------------------------------------------------------------------------------------- */
1439 static gchar *
1440 panel_get_title_without_hotkey (const char *title)
1442 char *translated_title;
1443 char *hkey;
1445 if (title == NULL)
1446 return NULL;
1447 if (title[0] == '\0')
1448 return g_strdup ("");
1450 translated_title = g_strdup (_(title));
1452 hkey = strchr (translated_title, '&');
1453 if ((hkey != NULL) && (hkey[1] != '\0'))
1454 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1456 return translated_title;
1459 /* --------------------------------------------------------------------------------------------- */
1461 static void
1462 paint_frame (WPanel * panel)
1464 int side, width;
1465 GString *format_txt;
1467 adjust_top_file (panel);
1469 widget_erase (&panel->widget);
1470 show_dir (panel);
1472 widget_move (&panel->widget, 1, 1);
1474 for (side = 0; side <= panel->split; side++)
1476 format_e *format;
1478 if (side)
1480 tty_setcolor (NORMAL_COLOR);
1481 tty_print_one_vline (TRUE);
1482 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1484 else if (panel->split)
1485 width = panel->widget.cols / 2 - 3;
1486 else
1487 width = panel->widget.cols - 2;
1489 format_txt = g_string_new ("");
1490 for (format = panel->format; format; format = format->next)
1492 if (format->string_fn)
1494 g_string_set_size (format_txt, 0);
1496 if (panel->list_type == list_long
1497 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1498 g_string_append (format_txt,
1499 panel->sort_info.reverse
1500 ? panel_sort_down_sign : panel_sort_up_sign);
1502 g_string_append (format_txt, format->title);
1503 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1505 g_string_append (format_txt, " [");
1506 g_string_append (format_txt, panel->filter);
1507 g_string_append (format_txt, "]");
1510 tty_setcolor (HEADER_COLOR);
1511 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1512 J_CENTER_LEFT));
1513 width -= format->field_len;
1515 else
1517 tty_setcolor (NORMAL_COLOR);
1518 tty_print_one_vline (TRUE);
1519 width--;
1522 g_string_free (format_txt, TRUE);
1524 if (width > 0)
1525 tty_draw_hline (-1, -1, ' ', width);
1528 if (panel->list_type != list_long)
1529 panel_paint_sort_info (panel);
1532 /* --------------------------------------------------------------------------------------------- */
1534 static const char *
1535 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1537 panel_display_t frame = frame_half;
1538 format = skip_separators (format);
1540 if (!strncmp (format, "full", 4))
1542 frame = frame_full;
1543 format += 4;
1545 else if (!strncmp (format, "half", 4))
1547 frame = frame_half;
1548 format += 4;
1551 if (!isstatus)
1553 panel->frame_size = frame;
1554 panel->split = 0;
1557 /* Now, the optional column specifier */
1558 format = skip_separators (format);
1560 if (*format == '1' || *format == '2')
1562 if (!isstatus)
1563 panel->split = *format == '2';
1564 format++;
1567 if (!isstatus)
1568 panel_update_cols (&(panel->widget), panel->frame_size);
1570 return skip_separators (format);
1573 /* Format is:
1575 all := panel_format? format
1576 panel_format := [full|half] [1|2]
1577 format := one_format_e
1578 | format , one_format_e
1580 one_format_e := just format.id [opt_size]
1581 just := [<=>]
1582 opt_size := : size [opt_expand]
1583 size := [0-9]+
1584 opt_expand := +
1588 /* --------------------------------------------------------------------------------------------- */
1590 static format_e *
1591 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1592 int *res_total_cols)
1594 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1595 int total_cols = 0; /* Used columns by the format */
1596 int set_justify; /* flag: set justification mode? */
1597 align_crt_t justify = J_LEFT; /* Which mode. */
1598 size_t i;
1600 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1602 *error = 0;
1604 if (i18n_timelength == 0)
1606 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1608 for (i = 0; panel_fields[i].id != NULL; i++)
1609 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1610 panel_fields[i].min_size = i18n_timelength;
1614 * This makes sure that the panel and mini status full/half mode
1615 * setting is equal
1617 format = parse_panel_size (panel, format, isstatus);
1619 while (*format)
1620 { /* format can be an empty string */
1621 int found = 0;
1623 darr = g_new0 (format_e, 1);
1625 /* I'm so ugly, don't look at me :-) */
1626 if (!home)
1627 home = old = darr;
1629 old->next = darr;
1630 darr->next = 0;
1631 old = darr;
1633 format = skip_separators (format);
1635 if (strchr ("<=>", *format))
1637 set_justify = 1;
1638 switch (*format)
1640 case '<':
1641 justify = J_LEFT;
1642 break;
1643 case '=':
1644 justify = J_CENTER;
1645 break;
1646 case '>':
1647 default:
1648 justify = J_RIGHT;
1649 break;
1651 format = skip_separators (format + 1);
1653 else
1654 set_justify = 0;
1656 for (i = 0; panel_fields[i].id != NULL; i++)
1658 size_t klen = strlen (panel_fields[i].id);
1660 if (strncmp (format, panel_fields[i].id, klen) != 0)
1661 continue;
1663 format += klen;
1665 darr->requested_field_len = panel_fields[i].min_size;
1666 darr->string_fn = panel_fields[i].string_fn;
1667 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1669 darr->id = panel_fields[i].id;
1670 darr->expand = panel_fields[i].expands;
1671 darr->just_mode = panel_fields[i].default_just;
1673 if (set_justify)
1675 if (IS_FIT (darr->just_mode))
1676 darr->just_mode = MAKE_FIT (justify);
1677 else
1678 darr->just_mode = justify;
1680 found = 1;
1682 format = skip_separators (format);
1684 /* If we have a size specifier */
1685 if (*format == ':')
1687 int req_length;
1689 /* If the size was specified, we don't want
1690 * auto-expansion by default
1692 darr->expand = 0;
1693 format++;
1694 req_length = atoi (format);
1695 darr->requested_field_len = req_length;
1697 format = skip_numbers (format);
1699 /* Now, if they insist on expansion */
1700 if (*format == '+')
1702 darr->expand = 1;
1703 format++;
1708 break;
1710 if (!found)
1712 char *tmp_format = g_strdup (format);
1714 int pos = min (8, strlen (format));
1715 delete_format (home);
1716 tmp_format[pos] = 0;
1717 *error =
1718 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1719 g_free (tmp_format);
1720 return 0;
1722 total_cols += darr->requested_field_len;
1725 *res_total_cols = total_cols;
1726 return home;
1729 /* --------------------------------------------------------------------------------------------- */
1731 static format_e *
1732 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1734 #define MAX_EXPAND 4
1735 int expand_top = 0; /* Max used element in expand */
1736 int usable_columns; /* Usable columns in the panel */
1737 int total_cols = 0;
1738 int i;
1739 format_e *darr, *home;
1741 if (!format)
1742 format = DEFAULT_USER_FORMAT;
1744 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1746 if (*error)
1747 return 0;
1749 panel->dirty = 1;
1751 /* Status needn't to be split */
1752 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1754 : (panel->split + 1))) - (!isstatus
1755 && panel->split);
1757 /* Look for the expandable fields and set field_len based on the requested field len */
1758 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1760 darr->field_len = darr->requested_field_len;
1761 if (darr->expand)
1762 expand_top++;
1765 /* If we used more columns than the available columns, adjust that */
1766 if (total_cols > usable_columns)
1768 int pdif, dif = total_cols - usable_columns;
1770 while (dif)
1772 pdif = dif;
1773 for (darr = home; darr; darr = darr->next)
1775 if (dif && darr->field_len - 1)
1777 darr->field_len--;
1778 dif--;
1782 /* avoid endless loop if num fields > 40 */
1783 if (pdif == dif)
1784 break;
1786 total_cols = usable_columns; /* give up, the rest should be truncated */
1789 /* Expand the available space */
1790 if ((usable_columns > total_cols) && expand_top)
1792 int spaces = (usable_columns - total_cols) / expand_top;
1793 int extra = (usable_columns - total_cols) % expand_top;
1795 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1796 if (darr->expand)
1798 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1799 i++;
1802 return home;
1805 /* --------------------------------------------------------------------------------------------- */
1806 /** Given the panel->view_type returns the format string to be parsed */
1808 static const char *
1809 panel_format (WPanel * panel)
1811 switch (panel->list_type)
1813 case list_long:
1814 return "full perm space nlink space owner space group space size space mtime space name";
1816 case list_brief:
1817 return "half 2 type name";
1819 case list_user:
1820 return panel->user_format;
1822 default:
1823 case list_full:
1824 return "half type name | size | mtime";
1828 /* --------------------------------------------------------------------------------------------- */
1830 static const char *
1831 mini_status_format (WPanel * panel)
1833 if (panel->user_mini_status)
1834 return panel->user_status_format[panel->list_type];
1836 switch (panel->list_type)
1839 case list_long:
1840 return "full perm space nlink space owner space group space size space mtime space name";
1842 case list_brief:
1843 return "half type name space bsize space perm space";
1845 case list_full:
1846 return "half type name";
1848 default:
1849 case list_user:
1850 return panel->user_format;
1854 /* */
1855 /* Panel operation commands */
1856 /* */
1858 /* --------------------------------------------------------------------------------------------- */
1859 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1861 static cb_ret_t
1862 maybe_cd (int move_up_dir)
1864 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1866 if (move_up_dir)
1868 vfs_path_t *up_dir;
1870 up_dir = vfs_path_from_str ("..");
1871 do_cd (up_dir, cd_exact);
1872 vfs_path_free (up_dir);
1873 return MSG_HANDLED;
1876 if (S_ISDIR (selection (current_panel)->st.st_mode)
1877 || link_isdir (selection (current_panel)))
1879 vfs_path_t *vpath;
1881 vpath = vfs_path_from_str (selection (current_panel)->fname);
1882 do_cd (vpath, cd_exact);
1883 vfs_path_free (vpath);
1884 return MSG_HANDLED;
1887 return MSG_NOT_HANDLED;
1890 /* --------------------------------------------------------------------------------------------- */
1892 /* if command line is empty then do 'cd ..' */
1893 static cb_ret_t
1894 force_maybe_cd (void)
1896 if (cmdline->buffer[0] == '\0')
1898 vfs_path_t *up_dir = vfs_path_from_str ("..");
1899 do_cd (up_dir, cd_exact);
1900 vfs_path_free (up_dir);
1901 return MSG_HANDLED;
1903 return MSG_NOT_HANDLED;
1906 /* --------------------------------------------------------------------------------------------- */
1908 static void
1909 unselect_item (WPanel * panel)
1911 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1914 /* --------------------------------------------------------------------------------------------- */
1916 static void
1917 move_down (WPanel * panel)
1919 if (panel->selected + 1 == panel->count)
1920 return;
1922 unselect_item (panel);
1923 panel->selected++;
1924 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1926 /* Scroll window half screen */
1927 panel->top_file += ITEMS (panel) / 2;
1928 if (panel->top_file > panel->count - ITEMS (panel))
1929 panel->top_file = panel->count - ITEMS (panel);
1930 paint_dir (panel);
1932 select_item (panel);
1935 /* --------------------------------------------------------------------------------------------- */
1937 static void
1938 move_up (WPanel * panel)
1940 if (panel->selected == 0)
1941 return;
1943 unselect_item (panel);
1944 panel->selected--;
1945 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1947 /* Scroll window half screen */
1948 panel->top_file -= ITEMS (panel) / 2;
1949 if (panel->top_file < 0)
1950 panel->top_file = 0;
1951 paint_dir (panel);
1953 select_item (panel);
1956 /* --------------------------------------------------------------------------------------------- */
1957 /** Changes the selection by lines (may be negative) */
1959 static void
1960 move_selection (WPanel * panel, int lines)
1962 int new_pos;
1963 int adjust = 0;
1965 new_pos = panel->selected + lines;
1966 if (new_pos >= panel->count)
1967 new_pos = panel->count - 1;
1969 if (new_pos < 0)
1970 new_pos = 0;
1972 unselect_item (panel);
1973 panel->selected = new_pos;
1975 if (panel->selected - panel->top_file >= ITEMS (panel))
1977 panel->top_file += lines;
1978 adjust = 1;
1981 if (panel->selected - panel->top_file < 0)
1983 panel->top_file += lines;
1984 adjust = 1;
1987 if (adjust)
1989 if (panel->top_file > panel->selected)
1990 panel->top_file = panel->selected;
1991 if (panel->top_file < 0)
1992 panel->top_file = 0;
1993 paint_dir (panel);
1995 select_item (panel);
1998 /* --------------------------------------------------------------------------------------------- */
2000 static cb_ret_t
2001 move_left (WPanel * panel)
2003 if (panel->split)
2005 move_selection (panel, -llines (panel));
2006 return MSG_HANDLED;
2008 else
2009 return maybe_cd (1); /* cd .. */
2012 /* --------------------------------------------------------------------------------------------- */
2014 static cb_ret_t
2015 move_right (WPanel * panel)
2017 if (panel->split)
2019 move_selection (panel, llines (panel));
2020 return MSG_HANDLED;
2022 else
2023 return maybe_cd (0); /* cd (selection) */
2026 /* --------------------------------------------------------------------------------------------- */
2028 static void
2029 prev_page (WPanel * panel)
2031 int items;
2033 if (!panel->selected && !panel->top_file)
2034 return;
2035 unselect_item (panel);
2036 items = ITEMS (panel);
2037 if (panel->top_file < items)
2038 items = panel->top_file;
2039 if (!items)
2040 panel->selected = 0;
2041 else
2042 panel->selected -= items;
2043 panel->top_file -= items;
2045 select_item (panel);
2046 paint_dir (panel);
2049 /* --------------------------------------------------------------------------------------------- */
2051 static void
2052 goto_parent_dir (WPanel * panel)
2054 if (!panel->is_panelized)
2056 vfs_path_t *up_dir;
2058 up_dir = vfs_path_from_str ("..");
2059 do_cd (up_dir, cd_exact);
2060 vfs_path_free (up_dir);
2062 else
2064 char *fname = panel->dir.list[panel->selected].fname;
2065 const char *bname;
2066 vfs_path_t *dname_vpath;
2068 if (g_path_is_absolute (fname))
2069 fname = g_strdup (fname);
2070 else
2072 char *tmp_root;
2074 tmp_root = vfs_path_to_str (panelized_panel.root_vpath);
2075 fname = mc_build_filename (tmp_root, fname, (char *) NULL);
2076 g_free (tmp_root);
2079 bname = x_basename (fname);
2081 if (bname == fname)
2082 dname_vpath = vfs_path_from_str (".");
2083 else
2085 char *dname;
2087 dname = g_strndup (fname, bname - fname);
2088 dname_vpath = vfs_path_from_str (dname);
2089 g_free (dname);
2092 do_cd (dname_vpath, cd_exact);
2093 try_to_select (panel, bname);
2095 vfs_path_free (dname_vpath);
2096 g_free (fname);
2100 /* --------------------------------------------------------------------------------------------- */
2102 static void
2103 next_page (WPanel * panel)
2105 int items;
2107 if (panel->selected == panel->count - 1)
2108 return;
2109 unselect_item (panel);
2110 items = ITEMS (panel);
2111 if (panel->top_file > panel->count - 2 * items)
2112 items = panel->count - items - panel->top_file;
2113 if (panel->top_file + items < 0)
2114 items = -panel->top_file;
2115 if (!items)
2116 panel->selected = panel->count - 1;
2117 else
2118 panel->selected += items;
2119 panel->top_file += items;
2121 select_item (panel);
2122 paint_dir (panel);
2125 /* --------------------------------------------------------------------------------------------- */
2127 static void
2128 goto_child_dir (WPanel * panel)
2130 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2132 vfs_path_t *vpath;
2134 vpath = vfs_path_from_str (selection (panel)->fname);
2135 do_cd (vpath, cd_exact);
2136 vfs_path_free (vpath);
2140 /* --------------------------------------------------------------------------------------------- */
2142 static void
2143 goto_top_file (WPanel * panel)
2145 unselect_item (panel);
2146 panel->selected = panel->top_file;
2147 select_item (panel);
2150 /* --------------------------------------------------------------------------------------------- */
2152 static void
2153 goto_middle_file (WPanel * panel)
2155 unselect_item (panel);
2156 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2157 select_item (panel);
2160 /* --------------------------------------------------------------------------------------------- */
2162 static void
2163 goto_bottom_file (WPanel * panel)
2165 unselect_item (panel);
2166 panel->selected = panel->top_file + ITEMS (panel) - 1;
2167 select_item (panel);
2170 /* --------------------------------------------------------------------------------------------- */
2172 static void
2173 move_home (WPanel * panel)
2175 if (panel->selected == 0)
2176 return;
2178 unselect_item (panel);
2180 if (panels_options.torben_fj_mode)
2182 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2184 if (panel->selected > middle_pos)
2186 goto_middle_file (panel);
2187 return;
2189 if (panel->selected != panel->top_file)
2191 goto_top_file (panel);
2192 return;
2196 panel->top_file = 0;
2197 panel->selected = 0;
2199 paint_dir (panel);
2200 select_item (panel);
2203 /* --------------------------------------------------------------------------------------------- */
2205 static void
2206 move_end (WPanel * panel)
2208 if (panel->selected == panel->count - 1)
2209 return;
2211 unselect_item (panel);
2213 if (panels_options.torben_fj_mode)
2215 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2217 if (panel->selected < middle_pos)
2219 goto_middle_file (panel);
2220 return;
2222 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2224 goto_bottom_file (panel);
2225 return;
2229 panel->selected = panel->count - 1;
2230 paint_dir (panel);
2231 select_item (panel);
2234 /* --------------------------------------------------------------------------------------------- */
2236 static void
2237 do_mark_file (WPanel * panel, mark_act_t do_move)
2239 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2240 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2241 move_down (panel);
2242 else if (do_move == MARK_FORCE_UP)
2243 move_up (panel);
2246 /* --------------------------------------------------------------------------------------------- */
2248 static void
2249 mark_file (WPanel * panel)
2251 do_mark_file (panel, MARK_DOWN);
2254 /* --------------------------------------------------------------------------------------------- */
2256 static void
2257 mark_file_up (WPanel * panel)
2259 do_mark_file (panel, MARK_FORCE_UP);
2262 /* --------------------------------------------------------------------------------------------- */
2264 static void
2265 mark_file_down (WPanel * panel)
2267 do_mark_file (panel, MARK_FORCE_DOWN);
2270 /* --------------------------------------------------------------------------------------------- */
2272 static void
2273 mark_file_right (WPanel * panel)
2275 int lines = llines (panel);
2277 if (state_mark < 0)
2278 state_mark = selection (panel)->f.marked ? 0 : 1;
2280 lines = min (lines, panel->count - panel->selected - 1);
2281 for (; lines != 0; lines--)
2283 do_file_mark (panel, panel->selected, state_mark);
2284 move_down (panel);
2286 do_file_mark (panel, panel->selected, state_mark);
2289 /* --------------------------------------------------------------------------------------------- */
2291 static void
2292 mark_file_left (WPanel * panel)
2294 int lines = llines (panel);
2296 if (state_mark < 0)
2297 state_mark = selection (panel)->f.marked ? 0 : 1;
2299 lines = min (lines, panel->selected + 1);
2300 for (; lines != 0; lines--)
2302 do_file_mark (panel, panel->selected, state_mark);
2303 move_up (panel);
2305 do_file_mark (panel, panel->selected, state_mark);
2308 /* --------------------------------------------------------------------------------------------- */
2309 /** Incremental search of a file name in the panel.
2310 * @param panel instance of WPanel structure
2311 * @param c_code key code
2314 static void
2315 do_search (WPanel * panel, int c_code)
2317 size_t l;
2318 int i, sel;
2319 gboolean wrapped = FALSE;
2320 char *act;
2321 mc_search_t *search;
2322 char *reg_exp, *esc_str;
2323 gboolean is_found = FALSE;
2325 l = strlen (panel->search_buffer);
2326 if (c_code == KEY_BACKSPACE)
2328 if (l != 0)
2330 act = panel->search_buffer + l;
2331 str_prev_noncomb_char (&act, panel->search_buffer);
2332 act[0] = '\0';
2334 panel->search_chpoint = 0;
2336 else
2338 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2340 panel->search_char[panel->search_chpoint] = c_code;
2341 panel->search_chpoint++;
2344 if (panel->search_chpoint > 0)
2346 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2348 case -2:
2349 return;
2350 case -1:
2351 panel->search_chpoint = 0;
2352 return;
2353 default:
2354 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2356 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2357 l += panel->search_chpoint;
2358 *(panel->search_buffer + l) = '\0';
2359 panel->search_chpoint = 0;
2365 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2366 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2367 search = mc_search_new (esc_str, -1);
2368 search->search_type = MC_SEARCH_T_GLOB;
2369 search->is_entire_line = TRUE;
2370 switch (panels_options.qsearch_mode)
2372 case QSEARCH_CASE_SENSITIVE:
2373 search->is_case_sensitive = TRUE;
2374 break;
2375 case QSEARCH_CASE_INSENSITIVE:
2376 search->is_case_sensitive = FALSE;
2377 break;
2378 default:
2379 search->is_case_sensitive = panel->sort_info.case_sensitive;
2380 break;
2382 sel = panel->selected;
2383 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2385 if (i >= panel->count)
2387 i = 0;
2388 if (wrapped)
2389 break;
2390 wrapped = TRUE;
2392 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2394 sel = i;
2395 is_found = TRUE;
2396 break;
2399 if (is_found)
2401 unselect_item (panel);
2402 panel->selected = sel;
2403 select_item (panel);
2404 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2406 else if (c_code != KEY_BACKSPACE)
2408 act = panel->search_buffer + l;
2409 str_prev_noncomb_char (&act, panel->search_buffer);
2410 act[0] = '\0';
2412 mc_search_free (search);
2413 g_free (reg_exp);
2414 g_free (esc_str);
2417 /* --------------------------------------------------------------------------------------------- */
2418 /** Start new search.
2419 * @param panel instance of WPanel structure
2422 static void
2423 start_search (WPanel * panel)
2425 if (panel->searching)
2427 if (panel->selected + 1 == panel->count)
2428 panel->selected = 0;
2429 else
2430 move_down (panel);
2432 /* in case if there was no search string we need to recall
2433 previous string, with which we ended previous searching */
2434 if (panel->search_buffer[0] == '\0')
2435 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2436 sizeof (panel->search_buffer));
2438 do_search (panel, 0);
2440 else
2442 panel->searching = TRUE;
2443 panel->search_buffer[0] = '\0';
2444 panel->search_char[0] = '\0';
2445 panel->search_chpoint = 0;
2446 display_mini_info (panel);
2447 mc_refresh ();
2451 /* --------------------------------------------------------------------------------------------- */
2453 static void
2454 stop_search (WPanel * panel)
2456 panel->searching = FALSE;
2458 /* if user had overrdied search string, we need to store it
2459 to the previous_search_buffer */
2460 if (panel->search_buffer[0] != '\0')
2461 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2462 sizeof (panel->prev_search_buffer));
2464 display_mini_info (panel);
2467 /* --------------------------------------------------------------------------------------------- */
2468 /** Return 1 if the Enter key has been processed, 0 otherwise */
2470 static int
2471 do_enter_on_file_entry (file_entry * fe)
2473 vfs_path_t *full_name_vpath;
2474 gboolean ok;
2477 * Directory or link to directory - change directory.
2478 * Try the same for the entries on which mc_lstat() has failed.
2480 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2482 vfs_path_t *fname_vpath;
2484 fname_vpath = vfs_path_from_str (fe->fname);
2485 if (!do_cd (fname_vpath, cd_exact))
2486 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2487 vfs_path_free (fname_vpath);
2488 return 1;
2491 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2493 /* Try associated command */
2494 if (regex_command (full_name_vpath, "Open", NULL) != 0)
2496 vfs_path_free (full_name_vpath);
2497 return 1;
2500 /* Check if the file is executable */
2501 full_name_vpath = vfs_path_append_new (current_panel->cwd_vpath, fe->fname, NULL);
2502 ok = (is_exe (fe->st.st_mode) && if_link_is_exe (full_name_vpath, fe));
2503 vfs_path_free (full_name_vpath);
2504 if (!ok)
2505 return 0;
2507 if (confirm_execute)
2509 if (query_dialog
2510 (_("The Midnight Commander"),
2511 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2512 return 1;
2515 if (!vfs_current_is_local ())
2517 int ret;
2518 vfs_path_t *tmp_vpath;
2520 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2521 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2522 vfs_path_free (tmp_vpath);
2523 /* We took action only if the dialog was shown or the execution
2524 * was successful */
2525 return confirm_execute || (ret == 0);
2529 char *tmp = name_quote (fe->fname, 0);
2530 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2531 g_free (tmp);
2532 shell_execute (cmd, 0);
2533 g_free (cmd);
2536 #ifdef HAVE_CHARSET
2537 mc_global.source_codepage = default_source_codepage;
2538 #endif
2540 return 1;
2543 /* --------------------------------------------------------------------------------------------- */
2545 static int
2546 do_enter (WPanel * panel)
2548 return do_enter_on_file_entry (selection (panel));
2551 /* --------------------------------------------------------------------------------------------- */
2553 static void
2554 chdir_other_panel (WPanel * panel)
2556 const file_entry *entry = &panel->dir.list[panel->selected];
2558 vfs_path_t *new_dir_vpath;
2559 char *sel_entry = NULL;
2561 if (get_other_type () != view_listing)
2563 set_display_type (get_other_index (), view_listing);
2566 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2567 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, entry->fname, NULL);
2568 else
2570 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, "..", (char *) NULL);
2571 sel_entry = strrchr (vfs_path_get_last_path_str (panel->cwd_vpath), PATH_SEP);
2574 change_panel ();
2575 do_cd (new_dir_vpath, cd_exact);
2576 vfs_path_free (new_dir_vpath);
2578 if (sel_entry)
2579 try_to_select (current_panel, sel_entry);
2580 change_panel ();
2582 move_down (panel);
2585 /* --------------------------------------------------------------------------------------------- */
2587 * Make the current directory of the current panel also the current
2588 * directory of the other panel. Put the other panel to the listing
2589 * mode if needed. If the current panel is panelized, the other panel
2590 * doesn't become panelized.
2593 static void
2594 panel_sync_other (const WPanel * panel)
2596 if (get_other_type () != view_listing)
2598 set_display_type (get_other_index (), view_listing);
2601 do_panel_cd (other_panel, current_panel->cwd_vpath, cd_exact);
2603 /* try to select current filename on the other panel */
2604 if (!panel->is_panelized)
2606 try_to_select (other_panel, selection (panel)->fname);
2610 /* --------------------------------------------------------------------------------------------- */
2612 static void
2613 chdir_to_readlink (WPanel * panel)
2615 vfs_path_t *new_dir_vpath;
2616 char buffer[MC_MAXPATHLEN], *p;
2617 int i;
2618 struct stat st;
2619 vfs_path_t *panel_fname_vpath;
2620 gboolean ok;
2622 if (get_other_type () != view_listing)
2623 return;
2625 if (!S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2626 return;
2628 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2629 if (i < 0)
2630 return;
2632 panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2633 ok = (mc_stat (panel_fname_vpath, &st) >= 0);
2634 vfs_path_free (panel_fname_vpath);
2635 if (!ok)
2636 return;
2638 buffer[i] = 0;
2639 if (!S_ISDIR (st.st_mode))
2641 p = strrchr (buffer, PATH_SEP);
2642 if (p && !p[1])
2644 *p = 0;
2645 p = strrchr (buffer, PATH_SEP);
2647 if (!p)
2648 return;
2649 p[1] = 0;
2651 if (*buffer == PATH_SEP)
2652 new_dir_vpath = vfs_path_from_str (buffer);
2653 else
2654 new_dir_vpath = vfs_path_append_new (panel->cwd_vpath, buffer, NULL);
2656 change_panel ();
2657 do_cd (new_dir_vpath, cd_exact);
2658 vfs_path_free (new_dir_vpath);
2659 change_panel ();
2661 move_down (panel);
2664 /* --------------------------------------------------------------------------------------------- */
2666 static gsize
2667 panel_get_format_field_count (WPanel * panel)
2669 format_e *format;
2670 gsize lc_index;
2671 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2672 return lc_index;
2675 /* --------------------------------------------------------------------------------------------- */
2677 function return 0 if not found and REAL_INDEX+1 if found
2680 static gsize
2681 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2683 format_e *format;
2684 gsize lc_index;
2686 for (lc_index = 1, format = panel->format;
2687 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2688 if (format == NULL)
2689 lc_index = 0;
2691 return lc_index;
2694 /* --------------------------------------------------------------------------------------------- */
2696 static format_e *
2697 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2699 format_e *format;
2700 for (format = panel->format;
2701 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2702 return format;
2705 /* --------------------------------------------------------------------------------------------- */
2707 static const panel_field_t *
2708 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2710 const panel_field_t *pfield;
2711 format_e *format;
2713 format = panel_get_format_field_by_index (panel, lc_index);
2714 if (format == NULL)
2715 return NULL;
2716 pfield = panel_get_field_by_title (format->title);
2717 if (pfield == NULL)
2718 return NULL;
2719 if (pfield->sort_routine == NULL)
2720 return NULL;
2721 return pfield;
2724 /* --------------------------------------------------------------------------------------------- */
2726 static void
2727 panel_toggle_sort_order_prev (WPanel * panel)
2729 gsize lc_index, i;
2730 gchar *title;
2732 const panel_field_t *pfield = NULL;
2734 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2735 lc_index = panel_get_format_field_index_by_name (panel, title);
2736 g_free (title);
2738 if (lc_index > 1)
2740 /* search for prev sortable column in panel format */
2741 for (i = lc_index - 1;
2742 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2745 if (pfield == NULL)
2747 /* Sortable field not found. Try to search in each array */
2748 for (i = panel_get_format_field_count (panel);
2749 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2752 if (pfield != NULL)
2754 panel->sort_info.sort_field = pfield;
2755 panel_set_sort_order (panel, pfield);
2759 /* --------------------------------------------------------------------------------------------- */
2761 static void
2762 panel_toggle_sort_order_next (WPanel * panel)
2764 gsize lc_index, i;
2765 const panel_field_t *pfield = NULL;
2766 gsize format_field_count;
2767 gchar *title;
2769 format_field_count = panel_get_format_field_count (panel);
2770 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2771 lc_index = panel_get_format_field_index_by_name (panel, title);
2772 g_free (title);
2774 if (lc_index != 0 && lc_index != format_field_count)
2776 /* search for prev sortable column in panel format */
2777 for (i = lc_index;
2778 i != format_field_count
2779 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2782 if (pfield == NULL)
2784 /* Sortable field not found. Try to search in each array */
2785 for (i = 0;
2786 i != format_field_count
2787 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2790 if (pfield != NULL)
2792 panel->sort_info.sort_field = pfield;
2793 panel_set_sort_order (panel, pfield);
2797 /* --------------------------------------------------------------------------------------------- */
2799 static void
2800 panel_select_sort_order (WPanel * panel)
2802 const panel_field_t *sort_order;
2804 sort_order = sort_box (&panel->sort_info);
2805 if (sort_order != NULL)
2807 panel->sort_info.sort_field = sort_order;
2808 panel_set_sort_order (panel, sort_order);
2812 /* --------------------------------------------------------------------------------------------- */
2814 static void
2815 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2817 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2819 const panel_field_t *sort_order;
2821 sort_order = panel_get_field_by_id (name);
2822 if (sort_order == NULL)
2823 return;
2824 panel->sort_info.sort_field = sort_order;
2826 else
2827 panel->sort_info.reverse = !panel->sort_info.reverse;
2829 panel_set_sort_order (panel, panel->sort_info.sort_field);
2832 /* --------------------------------------------------------------------------------------------- */
2834 * If we moved to the parent directory move the selection pointer to
2835 * the old directory name; If we leave VFS dir, remove FS specificator.
2837 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2840 static const char *
2841 get_parent_dir_name (const char *cwd, const char *lwd)
2843 size_t llen, clen;
2844 const char *p;
2846 llen = strlen (lwd);
2847 clen = strlen (cwd);
2849 if (llen <= clen)
2850 return NULL;
2852 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2854 if (p == NULL)
2856 p = strrchr (lwd, PATH_SEP);
2858 if ((p != NULL)
2859 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2860 && (clen == (size_t) (p - lwd)
2861 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2862 return (p + 1);
2864 return NULL;
2867 /* skip VFS prefix */
2868 while (--p > lwd && *p != PATH_SEP)
2870 /* get last component */
2871 while (--p > lwd && *p != PATH_SEP)
2874 /* return last component */
2875 return (p != lwd || *p == PATH_SEP) ? p + 1 : p;
2878 /* --------------------------------------------------------------------------------------------- */
2879 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2881 static void
2882 subshell_chdir (const vfs_path_t * vpath)
2884 #ifdef HAVE_SUBSHELL_SUPPORT
2885 if (mc_global.tty.use_subshell && vfs_current_is_local ())
2886 do_subshell_chdir (vpath, FALSE, TRUE);
2887 #else /* HAVE_SUBSHELL_SUPPORT */
2888 (void) vpath;
2889 #endif /* HAVE_SUBSHELL_SUPPORT */
2892 /* --------------------------------------------------------------------------------------------- */
2894 * Changes the current directory of the panel.
2895 * Don't record change in the directory history.
2898 static gboolean
2899 _do_panel_cd (WPanel * panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
2901 char *olddir;
2903 olddir = vfs_path_to_str (panel->cwd_vpath);
2905 /* Convert *new_path to a suitable pathname, handle ~user */
2906 if (cd_type == cd_parse_command)
2908 const vfs_path_element_t *element;
2910 element = vfs_path_get_by_index (new_dir_vpath, 0);
2911 if (strcmp (element->path, "-") == 0)
2912 new_dir_vpath = panel->lwd_vpath;
2915 if (mc_chdir (new_dir_vpath) == -1)
2917 panel_set_cwd (panel, olddir);
2918 g_free (olddir);
2919 return FALSE;
2922 /* Success: save previous directory, shutdown status of previous dir */
2923 panel_set_lwd (panel, olddir);
2924 input_free_completions (cmdline);
2926 vfs_path_free (panel->cwd_vpath);
2927 vfs_setup_cwd ();
2928 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
2930 vfs_release_path (olddir);
2932 subshell_chdir (panel->cwd_vpath);
2934 /* Reload current panel */
2935 panel_clean_dir (panel);
2938 char *tmp_path;
2940 panel->count =
2941 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
2942 panel->sort_info.reverse, panel->sort_info.case_sensitive,
2943 panel->sort_info.exec_first, panel->filter);
2944 tmp_path = vfs_path_to_str (panel->cwd_vpath);
2945 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
2946 g_free (tmp_path);
2949 load_hint (0);
2950 panel->dirty = 1;
2951 update_xterm_title_path ();
2953 g_free (olddir);
2955 return TRUE;
2958 /* --------------------------------------------------------------------------------------------- */
2960 static void
2961 directory_history_next (WPanel * panel)
2963 gboolean ok;
2967 GList *next;
2969 ok = TRUE;
2970 next = g_list_next (panel->dir_history_current);
2971 if (next != NULL)
2973 vfs_path_t *data_vpath;
2975 data_vpath = vfs_path_from_str ((char *) next->data);
2976 ok = _do_panel_cd (panel, data_vpath, cd_exact);
2977 vfs_path_free (data_vpath);
2978 panel->dir_history_current = next;
2980 /* skip directories that present in history but absent in file system */
2982 while (!ok);
2985 /* --------------------------------------------------------------------------------------------- */
2987 static void
2988 directory_history_prev (WPanel * panel)
2990 gboolean ok;
2994 GList *prev;
2996 ok = TRUE;
2997 prev = g_list_previous (panel->dir_history_current);
2998 if (prev != NULL)
3000 vfs_path_t *data_vpath;
3002 data_vpath = vfs_path_from_str ((char *) prev->data);
3003 ok = _do_panel_cd (panel, data_vpath, cd_exact);
3004 vfs_path_free (data_vpath);
3005 panel->dir_history_current = prev;
3007 /* skip directories that present in history but absent in file system */
3009 while (!ok);
3012 /* --------------------------------------------------------------------------------------------- */
3014 static void
3015 directory_history_list (WPanel * panel)
3017 char *s;
3018 gboolean ok = FALSE;
3019 size_t pos;
3021 pos = g_list_position (panel->dir_history_current, panel->dir_history);
3023 s = history_show (&panel->dir_history, &panel->widget, pos);
3024 if (s != NULL)
3026 vfs_path_t *s_vpath;
3028 s_vpath = vfs_path_from_str (s);
3029 ok = _do_panel_cd (panel, s_vpath, cd_exact);
3030 if (ok)
3031 directory_history_add (panel, panel->cwd_vpath);
3032 else
3033 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3034 vfs_path_free (s_vpath);
3035 g_free (s);
3038 if (!ok)
3040 /* Since history is fully modified in history_show(), panel->dir_history actually
3041 * points to the invalid place. Try restore current postition here. */
3043 size_t i;
3045 panel->dir_history_current = panel->dir_history;
3047 for (i = 0; i <= pos; i++)
3049 GList *prev;
3051 prev = g_list_previous (panel->dir_history_current);
3052 if (prev == NULL)
3053 break;
3055 panel->dir_history_current = prev;
3060 /* --------------------------------------------------------------------------------------------- */
3062 static cb_ret_t
3063 panel_execute_cmd (WPanel * panel, unsigned long command)
3065 int res = MSG_HANDLED;
3067 if (command != CK_Search)
3068 stop_search (panel);
3071 switch (command)
3073 case CK_Up:
3074 case CK_Down:
3075 case CK_Left:
3076 case CK_Right:
3077 case CK_Bottom:
3078 case CK_Top:
3079 case CK_PageDown:
3080 case CK_PageUp:
3081 /* reset state of marks flag */
3082 state_mark = -1;
3083 break;
3085 switch (command)
3087 case CK_PanelOtherCd:
3088 chdir_other_panel (panel);
3089 break;
3090 case CK_PanelOtherCdLink:
3091 chdir_to_readlink (panel);
3092 break;
3093 case CK_CopySingle:
3094 copy_cmd_local ();
3095 break;
3096 case CK_DeleteSingle:
3097 delete_cmd_local ();
3098 break;
3099 case CK_Enter:
3100 do_enter (panel);
3101 break;
3102 case CK_ViewRaw:
3103 view_raw_cmd ();
3104 break;
3105 case CK_EditNew:
3106 edit_cmd_new ();
3107 break;
3108 case CK_MoveSingle:
3109 rename_cmd_local ();
3110 break;
3111 case CK_SelectInvert:
3112 select_invert_cmd ();
3113 break;
3114 case CK_Select:
3115 select_cmd ();
3116 break;
3117 case CK_Unselect:
3118 unselect_cmd ();
3119 break;
3120 case CK_PageDown:
3121 next_page (panel);
3122 break;
3123 case CK_PageUp:
3124 prev_page (panel);
3125 break;
3126 case CK_CdChild:
3127 goto_child_dir (panel);
3128 break;
3129 case CK_CdParent:
3130 goto_parent_dir (panel);
3131 break;
3132 case CK_History:
3133 directory_history_list (panel);
3134 break;
3135 case CK_HistoryNext:
3136 directory_history_next (panel);
3137 break;
3138 case CK_HistoryPrev:
3139 directory_history_prev (panel);
3140 break;
3141 case CK_BottomOnScreen:
3142 goto_bottom_file (panel);
3143 break;
3144 case CK_MiddleOnScreen:
3145 goto_middle_file (panel);
3146 break;
3147 case CK_TopOnScreen:
3148 goto_top_file (panel);
3149 break;
3150 case CK_Mark:
3151 mark_file (panel);
3152 break;
3153 case CK_MarkUp:
3154 mark_file_up (panel);
3155 break;
3156 case CK_MarkDown:
3157 mark_file_down (panel);
3158 break;
3159 case CK_MarkLeft:
3160 mark_file_left (panel);
3161 break;
3162 case CK_MarkRight:
3163 mark_file_right (panel);
3164 break;
3165 case CK_CdParentSmart:
3166 res = force_maybe_cd ();
3167 break;
3168 case CK_Up:
3169 move_up (panel);
3170 break;
3171 case CK_Down:
3172 move_down (panel);
3173 break;
3174 case CK_Left:
3175 res = move_left (panel);
3176 break;
3177 case CK_Right:
3178 res = move_right (panel);
3179 break;
3180 case CK_Bottom:
3181 move_end (panel);
3182 break;
3183 case CK_Top:
3184 move_home (panel);
3185 break;
3186 #ifdef HAVE_CHARSET
3187 case CK_SelectCodepage:
3188 panel_change_encoding (panel);
3189 break;
3190 #endif
3191 case CK_Search:
3192 start_search (panel);
3193 break;
3194 case CK_SearchStop:
3195 break;
3196 case CK_PanelOtherSync:
3197 panel_sync_other (panel);
3198 break;
3199 case CK_Sort:
3200 panel_select_sort_order (panel);
3201 break;
3202 case CK_SortPrev:
3203 panel_toggle_sort_order_prev (panel);
3204 break;
3205 case CK_SortNext:
3206 panel_toggle_sort_order_next (panel);
3207 break;
3208 case CK_SortReverse:
3209 panel->sort_info.reverse = !panel->sort_info.reverse;
3210 panel_set_sort_order (panel, panel->sort_info.sort_field);
3211 break;
3212 case CK_SortByName:
3213 panel_set_sort_type_by_id (panel, "name");
3214 break;
3215 case CK_SortByExt:
3216 panel_set_sort_type_by_id (panel, "extension");
3217 break;
3218 case CK_SortBySize:
3219 panel_set_sort_type_by_id (panel, "size");
3220 break;
3221 case CK_SortByMTime:
3222 panel_set_sort_type_by_id (panel, "mtime");
3223 break;
3225 return res;
3228 /* --------------------------------------------------------------------------------------------- */
3230 static cb_ret_t
3231 panel_key (WPanel * panel, int key)
3233 size_t i;
3235 if (is_abort_char (key))
3237 stop_search (panel);
3238 return MSG_HANDLED;
3241 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3243 do_search (panel, key);
3244 return MSG_HANDLED;
3247 for (i = 0; panel_map[i].key != 0; i++)
3248 if (key == panel_map[i].key)
3249 return panel_execute_cmd (panel, panel_map[i].command);
3251 if (panels_options.torben_fj_mode && key == ALT ('h'))
3253 goto_middle_file (panel);
3254 return MSG_HANDLED;
3257 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3259 start_search (panel);
3260 do_search (panel, key);
3261 return MSG_HANDLED;
3264 return MSG_NOT_HANDLED;
3267 /* --------------------------------------------------------------------------------------------- */
3269 static cb_ret_t
3270 panel_callback (Widget * w, widget_msg_t msg, int parm)
3272 WPanel *panel = (WPanel *) w;
3273 WButtonBar *bb;
3275 switch (msg)
3277 case WIDGET_INIT:
3278 /* subscribe to "history_load" event */
3279 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3280 /* subscribe to "history_save" event */
3281 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3282 return MSG_HANDLED;
3284 case WIDGET_DRAW:
3285 /* Repaint everything, including frame and separator */
3286 paint_frame (panel); /* including show_dir */
3287 paint_dir (panel);
3288 mini_info_separator (panel);
3289 display_mini_info (panel);
3290 panel->dirty = 0;
3291 return MSG_HANDLED;
3293 case WIDGET_FOCUS:
3294 state_mark = -1;
3295 current_panel = panel;
3296 panel->active = 1;
3297 if (mc_chdir (panel->cwd_vpath) != 0)
3299 char *cwd;
3301 cwd = vfs_path_to_str_flags (panel->cwd_vpath, 0, VPF_STRIP_PASSWORD);
3302 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3303 cwd, unix_error_string (errno));
3304 g_free (cwd);
3306 else
3307 subshell_chdir (panel->cwd_vpath);
3309 update_xterm_title_path ();
3310 select_item (panel);
3311 show_dir (panel);
3312 paint_dir (panel);
3313 panel->dirty = 0;
3315 bb = find_buttonbar (panel->widget.owner);
3316 midnight_set_buttonbar (bb);
3317 buttonbar_redraw (bb);
3318 return MSG_HANDLED;
3320 case WIDGET_UNFOCUS:
3321 /* Janne: look at this for the multiple panel options */
3322 stop_search (panel);
3323 panel->active = 0;
3324 show_dir (panel);
3325 unselect_item (panel);
3326 return MSG_HANDLED;
3328 case WIDGET_KEY:
3329 return panel_key (panel, parm);
3331 case WIDGET_COMMAND:
3332 return panel_execute_cmd (panel, parm);
3334 case WIDGET_DESTROY:
3335 /* unsubscribe from "history_load" event */
3336 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3337 /* unsubscribe from "history_save" event */
3338 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3339 panel_destroy (panel);
3340 free_my_statfs ();
3341 return MSG_HANDLED;
3343 default:
3344 return default_proc (msg, parm);
3348 /* --------------------------------------------------------------------------------------------- */
3349 /* */
3350 /* Panel mouse events support routines */
3351 /* */
3353 static void
3354 mouse_toggle_mark (WPanel * panel)
3356 do_mark_file (panel, MARK_DONT_MOVE);
3357 mouse_marking = selection (panel)->f.marked;
3358 mouse_mark_panel = current_panel;
3361 /* --------------------------------------------------------------------------------------------- */
3363 static void
3364 mouse_set_mark (WPanel * panel)
3367 if (mouse_mark_panel == panel)
3369 if (mouse_marking && !(selection (panel)->f.marked))
3370 do_mark_file (panel, MARK_DONT_MOVE);
3371 else if (!mouse_marking && (selection (panel)->f.marked))
3372 do_mark_file (panel, MARK_DONT_MOVE);
3376 /* --------------------------------------------------------------------------------------------- */
3378 static int
3379 mark_if_marking (WPanel * panel, Gpm_Event * event)
3381 if (event->buttons & GPM_B_RIGHT)
3383 if (event->type & GPM_DOWN)
3384 mouse_toggle_mark (panel);
3385 else
3386 mouse_set_mark (panel);
3387 return 1;
3389 return 0;
3392 /* --------------------------------------------------------------------------------------------- */
3393 /** Determine which column was clicked, and sort the panel on
3394 * that column, or reverse sort on that column if already
3395 * sorted on that column.
3398 static void
3399 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3401 int i;
3402 const char *lc_sort_name = NULL;
3403 panel_field_t *col_sort_format = NULL;
3404 format_e *format;
3405 gchar *title;
3407 for (i = 0, format = panel->format; format != NULL; format = format->next)
3409 i += format->field_len;
3410 if (event->x < i + 1)
3412 /* found column */
3413 lc_sort_name = format->title;
3414 break;
3418 if (lc_sort_name == NULL)
3419 return;
3421 for (i = 0; panel_fields[i].id != NULL; i++)
3423 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3424 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3426 col_sort_format = &panel_fields[i];
3427 g_free (title);
3428 break;
3430 g_free (title);
3433 if (col_sort_format == NULL)
3434 return;
3436 if (panel->sort_info.sort_field == col_sort_format)
3438 /* reverse the sort if clicked column is already the sorted column */
3439 panel->sort_info.reverse = !panel->sort_info.reverse;
3441 else
3443 /* new sort is forced to be ascending */
3444 panel->sort_info.reverse = FALSE;
3446 panel_set_sort_order (panel, col_sort_format);
3450 /* --------------------------------------------------------------------------------------------- */
3452 * Mouse callback of the panel minus repainting.
3453 * If the event is redirected to the menu, *redir is set to TRUE.
3455 static int
3456 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3458 const int lines = llines (panel);
3459 const gboolean is_active = dlg_widget_active (panel);
3460 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3462 *redir = FALSE;
3464 /* 1st line */
3465 if (mouse_down && event->y == 1)
3467 /* "<" button */
3468 if (event->x == 2)
3470 directory_history_prev (panel);
3471 return MOU_NORMAL;
3474 /* "." button show/hide hidden files */
3475 if (event->x == panel->widget.cols - 5)
3477 panel->widget.owner->callback (panel->widget.owner, NULL,
3478 DLG_ACTION, CK_ShowHidden, NULL);
3479 repaint_screen ();
3480 return MOU_NORMAL;
3483 /* ">" button */
3484 if (event->x == panel->widget.cols - 1)
3486 directory_history_next (panel);
3487 return MOU_NORMAL;
3490 /* "^" button */
3491 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3493 directory_history_list (panel);
3494 return MOU_NORMAL;
3497 /* rest of the upper frame, the menu is invisible - call menu */
3498 if (!menubar_visible)
3500 *redir = TRUE;
3501 event->x += panel->widget.x;
3502 return the_menubar->widget.mouse (event, the_menubar);
3505 /* no other events on 1st line */
3506 return MOU_NORMAL;
3509 /* sort on clicked column; don't handle wheel events */
3510 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3512 mouse_sort_col (event, panel);
3513 return MOU_NORMAL;
3516 /* Mouse wheel events */
3517 if (mouse_down && (event->buttons & GPM_B_UP))
3519 if (is_active)
3521 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3522 prev_page (panel);
3523 else /* We are in first page */
3524 move_up (panel);
3526 return MOU_NORMAL;
3529 if (mouse_down && (event->buttons & GPM_B_DOWN))
3531 if (is_active)
3533 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3534 next_page (panel);
3535 else /* We are in last page */
3536 move_down (panel);
3538 return MOU_NORMAL;
3541 event->y -= 2;
3542 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3544 int my_index;
3546 if (!is_active)
3547 change_panel ();
3549 if (panel->top_file + event->y > panel->count)
3550 my_index = panel->count - 1;
3551 else
3553 my_index = panel->top_file + event->y - 1;
3554 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3555 my_index += llines (panel);
3557 if (my_index >= panel->count)
3558 my_index = panel->count - 1;
3561 if (my_index != panel->selected)
3563 unselect_item (panel);
3564 panel->selected = my_index;
3565 select_item (panel);
3568 /* This one is new */
3569 mark_if_marking (panel, event);
3571 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3573 if (event->y > 0 && event->y <= lines)
3574 do_enter (panel);
3576 return MOU_NORMAL;
3579 /* --------------------------------------------------------------------------------------------- */
3580 /** Mouse callback of the panel */
3582 static int
3583 panel_event (Gpm_Event * event, void *data)
3585 WPanel *panel = data;
3586 int ret;
3587 gboolean redir;
3589 ret = do_panel_event (event, panel, &redir);
3590 if (!redir)
3591 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3593 return ret;
3596 /* --------------------------------------------------------------------------------------------- */
3598 static void
3599 reload_panelized (WPanel * panel)
3601 int i, j;
3602 dir_list *list = &panel->dir;
3604 if (panel != current_panel)
3605 (void) mc_chdir (panel->cwd_vpath);
3607 for (i = 0, j = 0; i < panel->count; i++)
3609 vfs_path_t *vpath;
3611 if (list->list[i].f.marked)
3613 /* Unmark the file in advance. In case the following mc_lstat
3614 * fails we are done, else we have to mark the file again
3615 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3616 * IMO that's the best way to update the panel's summary status
3617 * -- Norbert
3619 do_file_mark (panel, i, 0);
3621 vpath = vfs_path_from_str (list->list[i].fname);
3622 if (mc_lstat (vpath, &list->list[i].st))
3623 g_free (list->list[i].fname);
3624 else
3626 if (list->list[i].f.marked)
3627 do_file_mark (panel, i, 1);
3628 if (j != i)
3629 list->list[j] = list->list[i];
3630 j++;
3632 vfs_path_free (vpath);
3634 if (j == 0)
3635 panel->count = set_zero_dir (list) ? 1 : 0;
3636 else
3637 panel->count = j;
3639 if (panel != current_panel)
3640 (void) mc_chdir (current_panel->cwd_vpath);
3643 /* --------------------------------------------------------------------------------------------- */
3645 static void
3646 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3648 gboolean free_pointer;
3649 char *my_current_file = NULL;
3651 if ((flags & UP_RELOAD) != 0)
3653 panel->is_panelized = FALSE;
3654 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3655 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3658 /* If current_file == -1 (an invalid pointer) then preserve selection */
3659 free_pointer = current_file == UP_KEEPSEL;
3661 if (free_pointer)
3663 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3664 current_file = my_current_file;
3667 if (panel->is_panelized)
3668 reload_panelized (panel);
3669 else
3670 panel_reload (panel);
3672 try_to_select (panel, current_file);
3673 panel->dirty = 1;
3675 if (free_pointer)
3676 g_free (my_current_file);
3679 /* --------------------------------------------------------------------------------------------- */
3681 static void
3682 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3684 if (get_display_type (which) == view_listing)
3686 WPanel *panel;
3688 panel = (WPanel *) get_panel_widget (which);
3689 if (panel->is_panelized)
3690 flags &= ~UP_RELOAD;
3691 update_one_panel_widget (panel, flags, current_file);
3695 /* --------------------------------------------------------------------------------------------- */
3697 static void
3698 do_select (WPanel * panel, int i)
3700 if (i != panel->selected)
3702 panel->dirty = 1;
3703 panel->selected = i;
3704 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3705 if (panel->top_file < 0)
3706 panel->top_file = 0;
3710 /* --------------------------------------------------------------------------------------------- */
3712 static void
3713 do_try_to_select (WPanel * panel, const char *name)
3715 int i;
3716 char *subdir;
3718 if (!name)
3720 do_select (panel, 0);
3721 return;
3724 /* We only want the last component of the directory,
3725 * and from this only the name without suffix.
3726 * Cut prefix if the panel is not panelized */
3728 if (panel->is_panelized)
3729 subdir = vfs_strip_suffix_from_filename (name);
3730 else
3731 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3733 /* Search that subdir or filename without prefix (if not panelized panel), select it if found */
3734 for (i = 0; i < panel->count; i++)
3736 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3738 do_select (panel, i);
3739 g_free (subdir);
3740 return;
3744 /* Try to select a file near the file that is missing */
3745 if (panel->selected >= panel->count)
3746 do_select (panel, panel->count - 1);
3747 g_free (subdir);
3750 /* --------------------------------------------------------------------------------------------- */
3752 /* event callback */
3753 static gboolean
3754 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3755 gpointer init_data, gpointer data)
3757 (void) event_group_name;
3758 (void) event_name;
3759 (void) init_data;
3760 (void) data;
3762 update_panels (UP_RELOAD, UP_KEEPSEL);
3764 return TRUE;
3767 /* --------------------------------------------------------------------------------------------- */
3769 /* event callback */
3770 static gboolean
3771 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3772 gpointer init_data, gpointer data)
3774 (void) event_group_name;
3775 (void) event_name;
3776 (void) init_data;
3777 (void) data;
3779 if (current_panel->marked == 0)
3780 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3781 (gpointer) selection (current_panel)->fname);
3782 else
3784 int i;
3785 gboolean first = TRUE;
3786 char *flist = NULL;
3788 for (i = 0; i < current_panel->count; i++)
3789 if (current_panel->dir.list[i].f.marked != 0)
3790 { /* Skip the unmarked ones */
3791 if (first)
3793 flist = g_strdup (current_panel->dir.list[i].fname);
3794 first = FALSE;
3796 else
3798 /* Add empty lines after the file */
3799 char *tmp;
3801 tmp =
3802 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3803 g_free (flist);
3804 flist = tmp;
3808 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3809 g_free (flist);
3811 return TRUE;
3814 /* --------------------------------------------------------------------------------------------- */
3815 /*** public functions ****************************************************************************/
3816 /* --------------------------------------------------------------------------------------------- */
3818 void
3819 try_to_select (WPanel * panel, const char *name)
3821 do_try_to_select (panel, name);
3822 select_item (panel);
3825 /* --------------------------------------------------------------------------------------------- */
3827 void
3828 panel_clean_dir (WPanel * panel)
3830 int count = panel->count;
3832 panel->count = 0;
3833 panel->top_file = 0;
3834 panel->selected = 0;
3835 panel->marked = 0;
3836 panel->dirs_marked = 0;
3837 panel->total = 0;
3838 panel->searching = FALSE;
3839 panel->is_panelized = FALSE;
3840 panel->dirty = 1;
3842 clean_dir (&panel->dir, count);
3845 /* --------------------------------------------------------------------------------------------- */
3847 * Set Up panel's current dir object
3849 * @param panel panel object
3850 * @param path_str string contain path
3853 void
3854 panel_set_cwd (WPanel * panel, const char *path_str)
3856 vfs_path_free (panel->cwd_vpath);
3857 panel->cwd_vpath = vfs_path_from_str (path_str);
3860 /* --------------------------------------------------------------------------------------------- */
3862 * Set Up panel's last working dir object
3864 * @param panel panel object
3865 * @param path_str string contain path
3868 void
3869 panel_set_lwd (WPanel * panel, const char *path_str)
3871 vfs_path_free (panel->lwd_vpath);
3872 panel->lwd_vpath = vfs_path_from_str (path_str);
3875 /* --------------------------------------------------------------------------------------------- */
3876 /** Panel creation.
3877 * @param panel_name the name of the panel for setup retieving
3878 * @returns new instance of WPanel
3881 WPanel *
3882 panel_new (const char *panel_name)
3884 return panel_new_with_dir (panel_name, NULL);
3887 /* --------------------------------------------------------------------------------------------- */
3888 /** Panel creation for specified directory.
3889 * @param panel_name specifies the name of the panel for setup retieving
3890 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3891 * @returns new instance of WPanel
3894 WPanel *
3895 panel_new_with_dir (const char *panel_name, const char *wpath)
3897 WPanel *panel;
3898 char *section;
3899 int i, err;
3900 char *curdir = NULL;
3902 panel = g_new0 (WPanel, 1);
3904 /* No know sizes of the panel at startup */
3905 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3907 /* We do not want the cursor */
3908 widget_want_cursor (panel->widget, 0);
3910 if (wpath != NULL)
3912 curdir = _vfs_get_cwd ();
3913 panel_set_cwd (panel, wpath);
3915 else
3917 vfs_setup_cwd ();
3918 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3921 panel_set_lwd (panel, ".");
3923 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3924 /* directories history will be get later */
3926 panel->dir.list = g_new (file_entry, MIN_FILES);
3927 panel->dir.size = MIN_FILES;
3928 panel->active = 0;
3929 panel->filter = 0;
3930 panel->split = 0;
3931 panel->top_file = 0;
3932 panel->selected = 0;
3933 panel->marked = 0;
3934 panel->total = 0;
3935 panel->dirty = 1;
3936 panel->searching = FALSE;
3937 panel->dirs_marked = 0;
3938 panel->is_panelized = FALSE;
3939 panel->format = 0;
3940 panel->status_format = 0;
3941 panel->format_modified = 1;
3943 panel->panel_name = g_strdup (panel_name);
3944 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
3946 #ifdef HAVE_CHARSET
3947 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3948 #endif
3950 for (i = 0; i < LIST_TYPES; i++)
3951 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
3953 panel->search_buffer[0] = '\0';
3954 panel->prev_search_buffer[0] = '\0';
3955 panel->frame_size = frame_half;
3957 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
3958 if (!mc_config_has_group (mc_main_config, section))
3960 g_free (section);
3961 section = g_strdup (panel->panel_name);
3963 panel_load_setup (panel, section);
3964 g_free (section);
3966 /* Load format strings */
3967 err = set_panel_formats (panel);
3968 if (err != 0)
3969 set_panel_formats (panel);
3971 #ifdef HAVE_CHARSET
3973 const vfs_path_element_t *path_element;
3975 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
3976 if (path_element->encoding != NULL)
3977 panel->codepage = get_codepage_index (path_element->encoding);
3979 #endif
3981 if (mc_chdir (panel->cwd_vpath) != 0)
3983 #ifdef HAVE_CHARSET
3984 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
3985 #endif
3986 vfs_setup_cwd ();
3987 vfs_path_free (panel->cwd_vpath);
3988 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3991 /* Load the default format */
3992 panel->count =
3993 do_load_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
3994 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3995 panel->sort_info.exec_first, panel->filter);
3997 /* Restore old right path */
3998 if (curdir != NULL)
4000 vfs_path_t *vpath;
4002 vpath = vfs_path_from_str (curdir);
4003 err = mc_chdir (vpath);
4004 vfs_path_free (vpath);
4006 g_free (curdir);
4008 return panel;
4011 /* --------------------------------------------------------------------------------------------- */
4013 void
4014 panel_reload (WPanel * panel)
4016 struct stat current_stat;
4017 char *tmp_path;
4018 gboolean ok;
4020 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4021 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
4022 && current_stat.st_ctime == panel->dir_stat.st_ctime
4023 && current_stat.st_mtime == panel->dir_stat.st_mtime);
4024 g_free (tmp_path);
4026 if (ok)
4027 return;
4031 char *last_slash;
4033 if (mc_chdir (panel->cwd_vpath) != -1)
4034 break;
4036 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4037 if (tmp_path[0] == PATH_SEP && tmp_path[1] == '\0')
4039 panel_clean_dir (panel);
4040 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
4041 g_free (tmp_path);
4042 return;
4044 last_slash = strrchr (tmp_path, PATH_SEP);
4045 vfs_path_free (panel->cwd_vpath);
4046 if (last_slash == NULL || last_slash == tmp_path)
4047 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4048 else
4050 *last_slash = '\0';
4051 panel->cwd_vpath = vfs_path_from_str (tmp_path);
4053 g_free (tmp_path);
4054 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4055 show_dir (panel);
4057 while (TRUE);
4059 panel->count =
4060 do_reload_dir (panel->cwd_vpath, &panel->dir, panel->sort_info.sort_field->sort_routine,
4061 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4062 panel->sort_info.exec_first, panel->filter);
4064 panel->dirty = 1;
4065 if (panel->selected >= panel->count)
4066 do_select (panel, panel->count - 1);
4068 recalculate_panel_summary (panel);
4071 /* --------------------------------------------------------------------------------------------- */
4072 /* Switches the panel to the mode specified in the format */
4073 /* Seting up both format and status string. Return: 0 - on success; */
4074 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4077 set_panel_formats (WPanel * p)
4079 format_e *form;
4080 char *err = NULL;
4081 int retcode = 0;
4083 form = use_display_format (p, panel_format (p), &err, 0);
4085 if (err != NULL)
4087 g_free (err);
4088 retcode = 1;
4090 else
4092 delete_format (p->format);
4093 p->format = form;
4096 if (panels_options.show_mini_info)
4098 form = use_display_format (p, mini_status_format (p), &err, 1);
4100 if (err != NULL)
4102 g_free (err);
4103 retcode += 2;
4105 else
4107 delete_format (p->status_format);
4108 p->status_format = form;
4112 panel_format_modified (p);
4113 panel_update_cols (&(p->widget), p->frame_size);
4115 if (retcode)
4116 message (D_ERROR, _("Warning"),
4117 _("User supplied format looks invalid, reverting to default."));
4118 if (retcode & 0x01)
4120 g_free (p->user_format);
4121 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4123 if (retcode & 0x02)
4125 g_free (p->user_status_format[p->list_type]);
4126 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4129 return retcode;
4132 /* --------------------------------------------------------------------------------------------- */
4134 void
4135 panel_update_cols (Widget * widget, panel_display_t frame_size)
4137 int cols, origin;
4139 /* don't touch panel if it is not in dialog yet */
4140 /* if panel is not in dialog it is not in widgets list
4141 and cannot be compared with get_panel_widget() result */
4142 if (widget->owner == NULL)
4143 return;
4145 if (panels_layout.horizontal_split)
4147 widget->cols = COLS;
4148 return;
4151 if (frame_size == frame_full)
4153 cols = COLS;
4154 origin = 0;
4156 else if (widget == get_panel_widget (0))
4158 cols = panels_layout.left_panel_size;
4159 origin = 0;
4161 else
4163 cols = COLS - panels_layout.left_panel_size;
4164 origin = panels_layout.left_panel_size;
4167 widget->cols = cols;
4168 widget->x = origin;
4171 /* --------------------------------------------------------------------------------------------- */
4173 /* Select current item and readjust the panel */
4174 void
4175 select_item (WPanel * panel)
4177 /* Although currently all over the code we set the selection and
4178 top file to decent values before calling select_item, I could
4179 forget it someday, so it's better to do the actual fitting here */
4181 if (panel->selected < 0)
4182 panel->selected = 0;
4184 if (panel->selected > panel->count - 1)
4185 panel->selected = panel->count - 1;
4187 adjust_top_file (panel);
4189 panel->dirty = 1;
4191 execute_hooks (select_file_hook);
4194 /* --------------------------------------------------------------------------------------------- */
4195 /** Clears all files in the panel, used only when one file was marked */
4196 void
4197 unmark_files (WPanel * panel)
4199 int i;
4201 if (!panel->marked)
4202 return;
4203 for (i = 0; i < panel->count; i++)
4204 file_mark (panel, i, 0);
4206 panel->dirs_marked = 0;
4207 panel->marked = 0;
4208 panel->total = 0;
4211 /* --------------------------------------------------------------------------------------------- */
4212 /** Recalculate the panels summary information, used e.g. when marked
4213 files might have been removed by an external command */
4215 void
4216 recalculate_panel_summary (WPanel * panel)
4218 int i;
4220 panel->marked = 0;
4221 panel->dirs_marked = 0;
4222 panel->total = 0;
4224 for (i = 0; i < panel->count; i++)
4225 if (panel->dir.list[i].f.marked)
4227 /* do_file_mark will return immediately if newmark == oldmark.
4228 So we have to first unmark it to get panel's summary information
4229 updated. (Norbert) */
4230 panel->dir.list[i].f.marked = 0;
4231 do_file_mark (panel, i, 1);
4235 /* --------------------------------------------------------------------------------------------- */
4236 /** This routine marks a file or a directory */
4238 void
4239 do_file_mark (WPanel * panel, int idx, int mark)
4241 if (panel->dir.list[idx].f.marked == mark)
4242 return;
4244 /* Only '..' can't be marked, '.' isn't visible */
4245 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4246 return;
4248 file_mark (panel, idx, mark);
4249 if (panel->dir.list[idx].f.marked)
4251 panel->marked++;
4252 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4254 if (panel->dir.list[idx].f.dir_size_computed)
4255 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4256 panel->dirs_marked++;
4258 else
4259 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4260 set_colors (panel);
4262 else
4264 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4266 if (panel->dir.list[idx].f.dir_size_computed)
4267 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4268 panel->dirs_marked--;
4270 else
4271 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4272 panel->marked--;
4276 /* --------------------------------------------------------------------------------------------- */
4278 * Changes the current directory of the panel.
4279 * Record change in the directory history.
4281 gboolean
4282 do_panel_cd (struct WPanel *panel, const vfs_path_t * new_dir_vpath, enum cd_enum cd_type)
4284 gboolean r;
4286 r = _do_panel_cd (panel, new_dir_vpath, cd_type);
4287 if (r)
4288 directory_history_add (panel, panel->cwd_vpath);
4289 return r;
4292 /* --------------------------------------------------------------------------------------------- */
4294 void
4295 file_mark (WPanel * panel, int lc_index, int val)
4297 if (panel->dir.list[lc_index].f.marked != val)
4299 panel->dir.list[lc_index].f.marked = val;
4300 panel->dirty = 1;
4304 /* --------------------------------------------------------------------------------------------- */
4306 void
4307 panel_re_sort (WPanel * panel)
4309 char *filename;
4310 int i;
4312 if (panel == NULL)
4313 return;
4315 filename = g_strdup (selection (panel)->fname);
4316 unselect_item (panel);
4317 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4318 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4319 panel->sort_info.exec_first);
4320 panel->selected = -1;
4321 for (i = panel->count; i; i--)
4323 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4325 panel->selected = i - 1;
4326 break;
4329 g_free (filename);
4330 panel->top_file = panel->selected - ITEMS (panel) / 2;
4331 select_item (panel);
4332 panel->dirty = 1;
4335 /* --------------------------------------------------------------------------------------------- */
4337 void
4338 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4340 if (sort_order == NULL)
4341 return;
4343 panel->sort_info.sort_field = sort_order;
4345 /* The directory is already sorted, we have to load the unsorted stuff */
4346 if (sort_order->sort_routine == (sortfn *) unsorted)
4348 char *current_file;
4350 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4351 panel_reload (panel);
4352 try_to_select (panel, current_file);
4353 g_free (current_file);
4355 panel_re_sort (panel);
4358 /* --------------------------------------------------------------------------------------------- */
4360 * Change panel encoding.
4361 * @param panel WPanel object
4364 #ifdef HAVE_CHARSET
4365 void
4366 panel_change_encoding (WPanel * panel)
4368 const char *encoding = NULL;
4369 char *errmsg;
4370 int r;
4372 r = select_charset (-1, -1, panel->codepage, FALSE);
4374 if (r == SELECT_CHARSET_CANCEL)
4375 return; /* Cancel */
4377 panel->codepage = r;
4379 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4381 /* No translation */
4382 vfs_path_t *cd_path_vpath;
4384 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4385 cd_path_vpath = remove_encoding_from_path (panel->cwd_vpath);
4386 do_panel_cd (panel, cd_path_vpath, cd_parse_command);
4387 show_dir (panel);
4388 vfs_path_free (cd_path_vpath);
4389 return;
4392 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4393 if (errmsg != NULL)
4395 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4396 g_free (errmsg);
4397 return;
4400 encoding = get_codepage_id (panel->codepage);
4401 if (encoding != NULL)
4403 char *cd_path;
4404 vfs_change_encoding (panel->cwd_vpath, encoding);
4406 cd_path = vfs_path_to_str (panel->cwd_vpath);
4407 if (!do_panel_cd (panel, panel->cwd_vpath, cd_parse_command))
4408 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4409 g_free (cd_path);
4413 /* --------------------------------------------------------------------------------------------- */
4416 * Remove encode info from last path element.
4419 vfs_path_t *
4420 remove_encoding_from_path (const vfs_path_t * vpath)
4422 vfs_path_t *ret_vpath;
4423 GString *tmp_conv;
4424 int indx;
4426 ret_vpath = vfs_path_new ();
4428 tmp_conv = g_string_new ("");
4430 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
4432 GIConv converter;
4433 vfs_path_element_t *path_element;
4435 path_element = vfs_path_element_clone (vfs_path_get_by_index (vpath, indx));
4436 vfs_path_add_element (ret_vpath, path_element);
4438 if (path_element->encoding == NULL)
4439 continue;
4441 converter = str_crt_conv_to (path_element->encoding);
4442 if (converter == INVALID_CONV)
4443 continue;
4445 g_free (path_element->encoding);
4446 path_element->encoding = NULL;
4448 str_vfs_convert_from (converter, path_element->path, tmp_conv);
4450 g_free (path_element->path);
4451 path_element->path = g_strndup (tmp_conv->str, tmp_conv->len);
4453 g_string_set_size (tmp_conv, 0);
4455 str_close_conv (converter);
4456 str_close_conv (path_element->dir.converter);
4457 path_element->dir.converter = INVALID_CONV;
4459 g_string_free (tmp_conv, TRUE);
4460 return ret_vpath;
4462 #endif /* HAVE_CHARSET */
4464 /* --------------------------------------------------------------------------------------------- */
4466 * This routine reloads the directory in both panels. It tries to
4467 * select current_file in current_panel and other_file in other_panel.
4468 * If current_file == -1 then it automatically sets current_file and
4469 * other_file to the currently selected files in the panels.
4471 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4472 * will not reload the other panel.
4475 void
4476 update_panels (panel_update_flags_t flags, const char *current_file)
4478 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4479 WPanel *panel;
4481 update_one_panel (get_current_index (), flags, current_file);
4482 if (reload_other)
4483 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4485 if (get_current_type () == view_listing)
4486 panel = (WPanel *) get_panel_widget (get_current_index ());
4487 else
4488 panel = (WPanel *) get_panel_widget (get_other_index ());
4490 if (!panel->is_panelized)
4491 (void) mc_chdir (panel->cwd_vpath);
4494 /* --------------------------------------------------------------------------------------------- */
4496 gsize
4497 panel_get_num_of_sortable_fields (void)
4499 gsize ret = 0, lc_index;
4501 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4502 if (panel_fields[lc_index].is_user_choice)
4503 ret++;
4504 return ret;
4507 /* --------------------------------------------------------------------------------------------- */
4509 const char **
4510 panel_get_sortable_fields (gsize * array_size)
4512 char **ret;
4513 gsize lc_index, i;
4515 lc_index = panel_get_num_of_sortable_fields ();
4517 ret = g_try_new0 (char *, lc_index + 1);
4518 if (ret == NULL)
4519 return NULL;
4521 if (array_size != NULL)
4522 *array_size = lc_index;
4524 lc_index = 0;
4526 for (i = 0; panel_fields[i].id != NULL; i++)
4527 if (panel_fields[i].is_user_choice)
4528 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4529 return (const char **) ret;
4532 /* --------------------------------------------------------------------------------------------- */
4534 const panel_field_t *
4535 panel_get_field_by_id (const char *name)
4537 gsize lc_index;
4538 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4539 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4540 return &panel_fields[lc_index];
4541 return NULL;
4544 /* --------------------------------------------------------------------------------------------- */
4546 const panel_field_t *
4547 panel_get_field_by_title_hotkey (const char *name)
4549 gsize lc_index;
4550 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4551 if (panel_fields[lc_index].title_hotkey != NULL &&
4552 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4553 return &panel_fields[lc_index];
4554 return NULL;
4557 /* --------------------------------------------------------------------------------------------- */
4559 const panel_field_t *
4560 panel_get_field_by_title (const char *name)
4562 gsize lc_index;
4563 gchar *title = NULL;
4565 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4567 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4568 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4570 g_free (title);
4571 return &panel_fields[lc_index];
4574 g_free (title);
4575 return NULL;
4578 /* --------------------------------------------------------------------------------------------- */
4580 gsize
4581 panel_get_num_of_user_possible_fields (void)
4583 gsize ret = 0, lc_index;
4585 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4586 if (panel_fields[lc_index].use_in_user_format)
4587 ret++;
4588 return ret;
4591 /* --------------------------------------------------------------------------------------------- */
4593 const char **
4594 panel_get_user_possible_fields (gsize * array_size)
4596 char **ret;
4597 gsize lc_index, i;
4599 lc_index = panel_get_num_of_user_possible_fields ();
4601 ret = g_try_new0 (char *, lc_index + 1);
4602 if (ret == NULL)
4603 return NULL;
4605 if (array_size != NULL)
4606 *array_size = lc_index;
4608 lc_index = 0;
4610 for (i = 0; panel_fields[i].id != NULL; i++)
4611 if (panel_fields[i].use_in_user_format)
4612 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4613 return (const char **) ret;
4616 /* --------------------------------------------------------------------------------------------- */
4618 void
4619 panel_init (void)
4621 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4622 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4624 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4625 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4626 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4627 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4628 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4630 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4631 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4632 panel_save_curent_file_to_clip_file, NULL, NULL);
4636 /* --------------------------------------------------------------------------------------------- */
4638 void
4639 panel_deinit (void)
4641 g_free (panel_sort_up_sign);
4642 g_free (panel_sort_down_sign);
4644 g_free (panel_hiddenfiles_sign_show);
4645 g_free (panel_hiddenfiles_sign_hide);
4646 g_free (panel_history_prev_item_sign);
4647 g_free (panel_history_next_item_sign);
4648 g_free (panel_history_show_list_sign);
4652 /* --------------------------------------------------------------------------------------------- */