Changed format of string 'current directory path' at header of panel.
[midnight-commander.git] / src / filemanager / panel.c
blobb1faa2d4aad88318c4e5ab484fabd2353c41d86a
1 /*
2 Panel managing.
4 Copyright (C) 1994, 1995, 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 2005, 2006, 2007, 2009, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Miguel de Icaza, 1995
10 Timur Bakeyev, 1997, 1999
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file panel.c
29 * \brief Source: panel managin module
32 #include <config.h>
34 #include <errno.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <unistd.h>
40 #include "lib/global.h"
42 #include "lib/tty/tty.h"
43 #include "lib/tty/mouse.h" /* For Gpm_Event */
44 #include "lib/tty/key.h" /* XCTRL and ALT macros */
45 #include "lib/skin.h"
46 #include "lib/strescape.h"
47 #include "lib/filehighlight.h"
48 #include "lib/mcconfig.h"
49 #include "lib/vfs/vfs.h"
50 #include "lib/unixcompat.h"
51 #include "lib/timefmt.h"
52 #include "lib/util.h"
53 #include "lib/widget.h"
54 #ifdef HAVE_CHARSET
55 #include "lib/charsets.h" /* get_codepage_id () */
56 #endif
57 #include "lib/event.h"
59 #include "src/setup.h" /* For loading/saving panel options */
60 #include "src/execute.h"
61 #include "src/selcodepage.h" /* select_charset (), SELECT_CHARSET_NO_TRANSLATE */
62 #include "src/keybind-defaults.h" /* global_keymap_t */
63 #include "src/subshell.h" /* do_subshell_chdir() */
65 #include "dir.h"
66 #include "boxes.h"
67 #include "tree.h"
68 #include "ext.h" /* regexp_command */
69 #include "layout.h" /* Most layout variables are here */
70 #include "cmd.h"
71 #include "command.h" /* cmdline */
72 #include "usermenu.h"
73 #include "midnight.h"
74 #include "mountlist.h" /* my_statfs */
76 #include "panel.h"
78 /*** global variables ****************************************************************************/
80 /* The hook list for the select file function */
81 hook_t *select_file_hook = NULL;
83 /* *INDENT-OFF* */
84 panelized_panel_t panelized_panel = { { NULL, 0 }, -1, { '\0' } };
85 /* *INDENT-ON* */
87 static const char *string_file_name (file_entry *, int);
88 static const char *string_file_size (file_entry *, int);
89 static const char *string_file_size_brief (file_entry *, int);
90 static const char *string_file_type (file_entry *, int);
91 static const char *string_file_mtime (file_entry *, int);
92 static const char *string_file_atime (file_entry *, int);
93 static const char *string_file_ctime (file_entry *, int);
94 static const char *string_file_permission (file_entry *, int);
95 static const char *string_file_perm_octal (file_entry *, int);
96 static const char *string_file_nlinks (file_entry *, int);
97 static const char *string_inode (file_entry *, int);
98 static const char *string_file_nuid (file_entry *, int);
99 static const char *string_file_ngid (file_entry *, int);
100 static const char *string_file_owner (file_entry *, int);
101 static const char *string_file_group (file_entry *, int);
102 static const char *string_marked (file_entry *, int);
103 static const char *string_space (file_entry *, int);
104 static const char *string_dot (file_entry *, int);
106 /* *INDENT-OFF* */
107 panel_field_t panel_fields[] = {
109 "unsorted", 12, 1, J_LEFT_FIT,
110 /* TRANSLATORS: one single character to represent 'unsorted' sort mode */
111 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
112 N_("sort|u"),
113 N_("&Unsorted"), TRUE, FALSE,
114 string_file_name,
115 (sortfn *) unsorted
119 "name", 12, 1, J_LEFT_FIT,
120 /* TRANSLATORS: one single character to represent 'name' sort mode */
121 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
122 N_("sort|n"),
123 N_("&Name"), TRUE, TRUE,
124 string_file_name,
125 (sortfn *) sort_name
129 "version", 12, 1, J_LEFT_FIT,
130 /* TRANSLATORS: one single character to represent 'version' sort mode */
131 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
132 N_("sort|v"),
133 N_("&Version"), TRUE, FALSE,
134 string_file_name,
135 (sortfn *) sort_vers
139 "extension", 12, 1, J_LEFT_FIT,
140 /* TRANSLATORS: one single character to represent 'extension' sort mode */
141 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
142 N_("sort|e"),
143 N_("&Extension"), TRUE, FALSE,
144 string_file_name, /* TODO: string_file_ext */
145 (sortfn *) sort_ext
149 "size", 7, 0, J_RIGHT,
150 /* TRANSLATORS: one single character to represent 'size' sort mode */
151 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
152 N_("sort|s"),
153 N_("&Size"), TRUE, TRUE,
154 string_file_size,
155 (sortfn *) sort_size
159 "bsize", 7, 0, J_RIGHT,
161 N_("Block Size"), FALSE, FALSE,
162 string_file_size_brief,
163 (sortfn *) sort_size
167 "type", 1, 0, J_LEFT,
169 "", FALSE, TRUE,
170 string_file_type,
171 NULL
175 "mtime", 12, 0, J_RIGHT,
176 /* TRANSLATORS: one single character to represent 'Modify time' sort mode */
177 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
178 N_("sort|m"),
179 N_("&Modify time"), TRUE, TRUE,
180 string_file_mtime,
181 (sortfn *) sort_time
185 "atime", 12, 0, J_RIGHT,
186 /* TRANSLATORS: one single character to represent 'Access time' sort mode */
187 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
188 N_("sort|a"),
189 N_("&Access time"), TRUE, TRUE,
190 string_file_atime,
191 (sortfn *) sort_atime
195 "ctime", 12, 0, J_RIGHT,
196 /* TRANSLATORS: one single character to represent 'Change time' sort mode */
197 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
198 N_("sort|h"),
199 N_("C&hange time"), TRUE, TRUE,
200 string_file_ctime,
201 (sortfn *) sort_ctime
205 "perm", 10, 0, J_LEFT,
207 N_("Permission"), FALSE, TRUE,
208 string_file_permission,
209 NULL
213 "mode", 6, 0, J_RIGHT,
215 N_("Perm"), FALSE, TRUE,
216 string_file_perm_octal,
217 NULL
221 "nlink", 2, 0, J_RIGHT,
223 N_("Nl"), FALSE, TRUE,
224 string_file_nlinks, NULL
228 "inode", 5, 0, J_RIGHT,
229 /* TRANSLATORS: one single character to represent 'inode' sort mode */
230 /* TRANSLATORS: no need to translate 'sort', it's just a context prefix */
231 N_("sort|i"),
232 N_("&Inode"), TRUE, TRUE,
233 string_inode,
234 (sortfn *) sort_inode
238 "nuid", 5, 0, J_RIGHT,
240 N_("UID"), FALSE, FALSE,
241 string_file_nuid,
242 NULL
246 "ngid", 5, 0, J_RIGHT,
248 N_("GID"), FALSE, FALSE,
249 string_file_ngid,
250 NULL
254 "owner", 8, 0, J_LEFT_FIT,
256 N_("Owner"), FALSE, TRUE,
257 string_file_owner,
258 NULL
262 "group", 8, 0, J_LEFT_FIT,
264 N_("Group"), FALSE, TRUE,
265 string_file_group,
266 NULL
270 "mark", 1, 0, J_RIGHT,
272 " ", FALSE, TRUE,
273 string_marked,
274 NULL
278 "|", 1, 0, J_RIGHT,
280 " ", FALSE, TRUE,
281 NULL,
282 NULL
286 "space", 1, 0, J_RIGHT,
288 " ", FALSE, TRUE,
289 string_space,
290 NULL
294 "dot", 1, 0, J_RIGHT,
296 " ", FALSE, FALSE,
297 string_dot,
298 NULL
302 NULL, 0, 0, J_RIGHT, NULL, NULL, FALSE, FALSE, NULL, NULL
305 /* *INDENT-ON* */
307 extern int saving_setup;
309 /*** file scope macro definitions ****************************************************************/
311 #define ELEMENTS(arr) ( sizeof(arr) / sizeof((arr)[0]) )
313 #define NORMAL 0
314 #define SELECTED 1
315 #define MARKED 2
316 #define MARKED_SELECTED 3
317 #define STATUS 5
319 /* This macro extracts the number of available lines in a panel */
320 #define llines(p) (p->widget.lines - 3 - (panels_options.show_mini_info ? 2 : 0))
322 /*** file scope type declarations ****************************************************************/
324 typedef enum
326 MARK_DONT_MOVE = 0,
327 MARK_DOWN = 1,
328 MARK_FORCE_DOWN = 2,
329 MARK_FORCE_UP = 3
330 } mark_act_t;
333 * This describes a format item. The parse_display_format routine parses
334 * the user specified format and creates a linked list of format_e structures.
336 typedef struct format_e
338 struct format_e *next;
339 int requested_field_len;
340 int field_len;
341 align_crt_t just_mode;
342 int expand;
343 const char *(*string_fn) (file_entry *, int len);
344 char *title;
345 const char *id;
346 } format_e;
348 /* File name scroll states */
349 typedef enum
351 FILENAME_NOSCROLL = 1,
352 FILENAME_SCROLL_LEFT = 2,
353 FILENAME_SCROLL_RIGHT = 4
354 } filename_scroll_flag_t;
356 /*** file scope variables ************************************************************************/
358 static char *panel_sort_up_sign = NULL;
359 static char *panel_sort_down_sign = NULL;
361 static char *panel_hiddenfiles_sign_show = NULL;
362 static char *panel_hiddenfiles_sign_hide = NULL;
363 static char *panel_history_prev_item_sign = NULL;
364 static char *panel_history_next_item_sign = NULL;
365 static char *panel_history_show_list_sign = NULL;
366 static char *panel_filename_scroll_left_char = NULL;
367 static char *panel_filename_scroll_right_char = NULL;
369 /* Panel that selection started */
370 static WPanel *mouse_mark_panel = NULL;
372 static int mouse_marking = 0;
373 static int state_mark = 0;
374 /*** file scope functions ************************************************************************/
375 /* --------------------------------------------------------------------------------------------- */
377 static void
378 set_colors (WPanel * panel)
380 (void) panel;
381 tty_set_normal_attrs ();
382 tty_setcolor (NORMAL_COLOR);
385 /* --------------------------------------------------------------------------------------------- */
386 /** Delete format string, it is a linked list */
388 static void
389 delete_format (format_e * format)
391 while (format != NULL)
393 format_e *next = format->next;
394 g_free (format->title);
395 g_free (format);
396 format = next;
400 /* --------------------------------------------------------------------------------------------- */
401 /** This code relies on the default justification!!! */
403 static void
404 add_permission_string (const char *dest, int width, file_entry * fe, int attr, int color,
405 int is_octal)
407 int i, r, l;
409 l = get_user_permissions (&fe->st);
411 if (is_octal)
413 /* Place of the access bit in octal mode */
414 l = width + l - 3;
415 r = l + 1;
417 else
419 /* The same to the triplet in string mode */
420 l = l * 3 + 1;
421 r = l + 3;
424 for (i = 0; i < width; i++)
426 if (i >= l && i < r)
428 if (attr == SELECTED || attr == MARKED_SELECTED)
429 tty_setcolor (MARKED_SELECTED_COLOR);
430 else
431 tty_setcolor (MARKED_COLOR);
433 else if (color >= 0)
434 tty_setcolor (color);
435 else
436 tty_lowlevel_setcolor (-color);
438 tty_print_char (dest[i]);
442 /* --------------------------------------------------------------------------------------------- */
443 /** String representations of various file attributes name */
445 static const char *
446 string_file_name (file_entry * fe, int len)
448 static char buffer[MC_MAXPATHLEN * MB_LEN_MAX + 1];
450 (void) len;
451 g_strlcpy (buffer, fe->fname, sizeof (buffer));
452 return buffer;
455 /* --------------------------------------------------------------------------------------------- */
457 static unsigned int
458 ilog10 (dev_t n)
460 unsigned int digits = 0;
463 digits++, n /= 10;
465 while (n != 0);
466 return digits;
469 /* --------------------------------------------------------------------------------------------- */
471 static void
472 format_device_number (char *buf, size_t bufsize, dev_t dev)
474 dev_t major_dev = major (dev);
475 dev_t minor_dev = minor (dev);
476 unsigned int major_digits = ilog10 (major_dev);
477 unsigned int minor_digits = ilog10 (minor_dev);
479 g_assert (bufsize >= 1);
480 if (major_digits + 1 + minor_digits + 1 <= bufsize)
482 g_snprintf (buf, bufsize, "%lu,%lu", (unsigned long) major_dev, (unsigned long) minor_dev);
484 else
486 g_strlcpy (buf, _("[dev]"), bufsize);
490 /* --------------------------------------------------------------------------------------------- */
491 /** size */
493 static const char *
494 string_file_size (file_entry * fe, int len)
496 static char buffer[BUF_TINY];
498 /* Don't ever show size of ".." since we don't calculate it */
499 if (!strcmp (fe->fname, ".."))
501 return _("UP--DIR");
504 #ifdef HAVE_STRUCT_STAT_ST_RDEV
505 if (S_ISBLK (fe->st.st_mode) || S_ISCHR (fe->st.st_mode))
506 format_device_number (buffer, len + 1, fe->st.st_rdev);
507 else
508 #endif
510 size_trunc_len (buffer, (unsigned int) len, fe->st.st_size, 0, panels_options.kilobyte_si);
512 return buffer;
515 /* --------------------------------------------------------------------------------------------- */
516 /** bsize */
518 static const char *
519 string_file_size_brief (file_entry * fe, int len)
521 if (S_ISLNK (fe->st.st_mode) && !fe->f.link_to_dir)
523 return _("SYMLINK");
526 if ((S_ISDIR (fe->st.st_mode) || fe->f.link_to_dir) && strcmp (fe->fname, ".."))
528 return _("SUB-DIR");
531 return string_file_size (fe, len);
534 /* --------------------------------------------------------------------------------------------- */
535 /** This functions return a string representation of a file entry type */
537 static const char *
538 string_file_type (file_entry * fe, int len)
540 static char buffer[2];
542 (void) len;
543 if (S_ISDIR (fe->st.st_mode))
544 buffer[0] = PATH_SEP;
545 else if (S_ISLNK (fe->st.st_mode))
547 if (fe->f.link_to_dir)
548 buffer[0] = '~';
549 else if (fe->f.stale_link)
550 buffer[0] = '!';
551 else
552 buffer[0] = '@';
554 else if (S_ISCHR (fe->st.st_mode))
555 buffer[0] = '-';
556 else if (S_ISSOCK (fe->st.st_mode))
557 buffer[0] = '=';
558 else if (S_ISDOOR (fe->st.st_mode))
559 buffer[0] = '>';
560 else if (S_ISBLK (fe->st.st_mode))
561 buffer[0] = '+';
562 else if (S_ISFIFO (fe->st.st_mode))
563 buffer[0] = '|';
564 else if (S_ISNAM (fe->st.st_mode))
565 buffer[0] = '#';
566 else if (!S_ISREG (fe->st.st_mode))
567 buffer[0] = '?'; /* non-regular of unknown kind */
568 else if (is_exe (fe->st.st_mode))
569 buffer[0] = '*';
570 else
571 buffer[0] = ' ';
572 buffer[1] = '\0';
573 return buffer;
576 /* --------------------------------------------------------------------------------------------- */
577 /** mtime */
579 static const char *
580 string_file_mtime (file_entry * fe, int len)
582 (void) len;
583 return file_date (fe->st.st_mtime);
586 /* --------------------------------------------------------------------------------------------- */
587 /** atime */
589 static const char *
590 string_file_atime (file_entry * fe, int len)
592 (void) len;
593 return file_date (fe->st.st_atime);
596 /* --------------------------------------------------------------------------------------------- */
597 /** ctime */
599 static const char *
600 string_file_ctime (file_entry * fe, int len)
602 (void) len;
603 return file_date (fe->st.st_ctime);
606 /* --------------------------------------------------------------------------------------------- */
607 /** perm */
609 static const char *
610 string_file_permission (file_entry * fe, int len)
612 (void) len;
613 return string_perm (fe->st.st_mode);
616 /* --------------------------------------------------------------------------------------------- */
617 /** mode */
619 static const char *
620 string_file_perm_octal (file_entry * fe, int len)
622 static char buffer[10];
624 (void) len;
625 g_snprintf (buffer, sizeof (buffer), "0%06lo", (unsigned long) fe->st.st_mode);
626 return buffer;
629 /* --------------------------------------------------------------------------------------------- */
630 /** nlink */
632 static const char *
633 string_file_nlinks (file_entry * fe, int len)
635 static char buffer[BUF_TINY];
637 (void) len;
638 g_snprintf (buffer, sizeof (buffer), "%16d", (int) fe->st.st_nlink);
639 return buffer;
642 /* --------------------------------------------------------------------------------------------- */
643 /** inode */
645 static const char *
646 string_inode (file_entry * fe, int len)
648 static char buffer[10];
650 (void) len;
651 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_ino);
652 return buffer;
655 /* --------------------------------------------------------------------------------------------- */
656 /** nuid */
658 static const char *
659 string_file_nuid (file_entry * fe, int len)
661 static char buffer[10];
663 (void) len;
664 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_uid);
665 return buffer;
668 /* --------------------------------------------------------------------------------------------- */
669 /** ngid */
671 static const char *
672 string_file_ngid (file_entry * fe, int len)
674 static char buffer[10];
676 (void) len;
677 g_snprintf (buffer, sizeof (buffer), "%lu", (unsigned long) fe->st.st_gid);
678 return buffer;
681 /* --------------------------------------------------------------------------------------------- */
682 /** owner */
684 static const char *
685 string_file_owner (file_entry * fe, int len)
687 (void) len;
688 return get_owner (fe->st.st_uid);
691 /* --------------------------------------------------------------------------------------------- */
692 /** group */
694 static const char *
695 string_file_group (file_entry * fe, int len)
697 (void) len;
698 return get_group (fe->st.st_gid);
701 /* --------------------------------------------------------------------------------------------- */
702 /** mark */
704 static const char *
705 string_marked (file_entry * fe, int len)
707 (void) len;
708 return fe->f.marked ? "*" : " ";
711 /* --------------------------------------------------------------------------------------------- */
712 /** space */
714 static const char *
715 string_space (file_entry * fe, int len)
717 (void) fe;
718 (void) len;
719 return " ";
722 /* --------------------------------------------------------------------------------------------- */
723 /** dot */
725 static const char *
726 string_dot (file_entry * fe, int len)
728 (void) fe;
729 (void) len;
730 return ".";
733 /* --------------------------------------------------------------------------------------------- */
735 static int
736 file_compute_color (int attr, file_entry * fe)
738 switch (attr)
740 case SELECTED:
741 return (SELECTED_COLOR);
742 case MARKED:
743 return (MARKED_COLOR);
744 case MARKED_SELECTED:
745 return (MARKED_SELECTED_COLOR);
746 case STATUS:
747 return (NORMAL_COLOR);
748 case NORMAL:
749 default:
750 if (!panels_options.filetype_mode)
751 return (NORMAL_COLOR);
754 return mc_fhl_get_color (mc_filehighlight, fe);
757 /* --------------------------------------------------------------------------------------------- */
758 /** Formats the file number file_index of panel in the buffer dest */
760 static filename_scroll_flag_t
761 format_file (char *dest, int limit, WPanel * panel, int file_index, int width, int attr,
762 int isstatus, int * field_lenght)
764 int color, length, empty_line;
765 const char *txt;
766 format_e *format, *home;
767 file_entry *fe;
768 filename_scroll_flag_t res = FILENAME_NOSCROLL;
770 (void) dest;
771 (void) limit;
772 length = 0;
773 empty_line = (file_index >= panel->count);
774 home = (isstatus) ? panel->status_format : panel->format;
775 fe = &panel->dir.list[file_index];
776 *field_lenght = 0;
778 if (!empty_line)
779 color = file_compute_color (attr, fe);
780 else
781 color = NORMAL_COLOR;
782 for (format = home; format; format = format->next)
784 if (length == width)
785 break;
787 if (format->string_fn)
789 int len, perm;
790 const char *prepared_text;
791 int name_offset = 0;
793 if (empty_line)
794 txt = " ";
795 else
796 txt = (*format->string_fn) (fe, format->field_len);
798 len = format->field_len;
799 if (len + length > width)
800 len = width - length;
801 if (len <= 0)
802 break;
804 if (!isstatus && panel->content_shift > -1 && strcmp (format->id, "name") == 0)
806 int str_len;
807 int i;
809 *field_lenght = len + 1;
811 str_len = str_length (txt);
812 i = max (0, str_len - len);
813 panel->max_shift = max (panel->max_shift, i);
814 i = min (panel->content_shift, i);
816 if (i > -1)
818 name_offset = str_offset_to_pos (txt, i);
819 if (str_len > len)
821 res = FILENAME_SCROLL_LEFT;
822 if (str_length (txt + name_offset) > len)
823 res |= FILENAME_SCROLL_RIGHT;
828 perm = 0;
829 if (panels_options.permission_mode)
831 if (!strcmp (format->id, "perm"))
832 perm = 1;
833 else if (!strcmp (format->id, "mode"))
834 perm = 2;
837 if (color >= 0)
838 tty_setcolor (color);
839 else
840 tty_lowlevel_setcolor (-color);
842 if (!isstatus && panel->content_shift > -1)
843 prepared_text = str_fit_to_term (txt + name_offset, len, HIDE_FIT (format->just_mode));
844 else
845 prepared_text = str_fit_to_term (txt, len, format->just_mode);
847 if (perm)
848 add_permission_string (prepared_text, format->field_len, fe, attr, color, perm - 1);
849 else
850 tty_print_string (prepared_text);
852 length += len;
854 else
856 if (attr == SELECTED || attr == MARKED_SELECTED)
857 tty_setcolor (SELECTED_COLOR);
858 else
859 tty_setcolor (NORMAL_COLOR);
860 tty_print_one_vline (TRUE);
861 length++;
865 if (length < width)
866 tty_draw_hline (-1, -1, ' ', width - length);
868 return res;
871 /* --------------------------------------------------------------------------------------------- */
873 static void
874 repaint_file (WPanel * panel, int file_index, int mv, int attr, int isstatus)
876 int second_column = 0;
877 int width;
878 int offset = 0;
879 char buffer[BUF_MEDIUM];
880 filename_scroll_flag_t ret_frm;
881 int ypos = 0;
882 gboolean panel_is_split = !isstatus && panel->split;
883 int fln = 0;
885 width = panel->widget.cols - 2;
887 if (panel_is_split)
889 second_column = (file_index - panel->top_file) / llines (panel);
890 width = width / 2 - 1;
892 if (second_column != 0)
894 offset = 1 + width;
895 /*width = (panel->widget.cols-2) - (panel->widget.cols-2)/2 - 1; */
896 width = panel->widget.cols - offset - 2;
900 /* Nothing to paint */
901 if (width <= 0)
902 return;
904 if (mv)
906 int pos = file_index - panel->top_file;
908 if (panel_is_split)
909 ypos = pos % llines (panel);
910 else
911 ypos = pos;
913 ypos += 2;
914 widget_move (&panel->widget, ypos, offset + 1);
917 ret_frm = format_file (buffer, sizeof (buffer), panel, file_index, width, attr, isstatus, &fln);
919 if (panel_is_split)
921 if (second_column)
922 tty_print_char (' ');
923 else
925 tty_setcolor (NORMAL_COLOR);
926 tty_print_one_vline (TRUE);
929 if (ret_frm != FILENAME_NOSCROLL && mv)
931 if (!panel_is_split && fln > 0)
933 if (panel->list_type == list_long)
935 offset = width - fln + 1;
936 width = fln - 1;
938 else
940 width = fln;
943 widget_move (&panel->widget, ypos, offset);
944 tty_setcolor (NORMAL_COLOR);
945 tty_print_string (panel_filename_scroll_left_char);
947 if ((ret_frm & FILENAME_SCROLL_RIGHT) != 0)
949 widget_move (&panel->widget, ypos, offset + 1 + width);
950 tty_setcolor (NORMAL_COLOR);
951 tty_print_string (panel_filename_scroll_right_char);
956 /* --------------------------------------------------------------------------------------------- */
958 static void
959 display_mini_info (WPanel * panel)
961 if (!panels_options.show_mini_info)
962 return;
964 widget_move (&panel->widget, llines (panel) + 3, 1);
966 if (panel->searching)
968 tty_setcolor (INPUT_COLOR);
969 tty_print_char ('/');
970 tty_print_string (str_fit_to_term (panel->search_buffer, panel->widget.cols - 3, J_LEFT));
971 return;
974 /* Status resolves links and show them */
975 set_colors (panel);
977 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
979 char link_target[MC_MAXPATHLEN];
980 vfs_path_t *lc_link_vpath;
981 int len;
983 lc_link_vpath =
984 vfs_path_append_new (panel->cwd_vpath, panel->dir.list[panel->selected].fname, NULL);
985 len = mc_readlink (lc_link_vpath, link_target, MC_MAXPATHLEN - 1);
986 vfs_path_free (lc_link_vpath);
987 if (len > 0)
989 link_target[len] = 0;
990 tty_print_string ("-> ");
991 tty_print_string (str_fit_to_term (link_target, panel->widget.cols - 5, J_LEFT_FIT));
993 else
994 tty_print_string (str_fit_to_term (_("<readlink failed>"),
995 panel->widget.cols - 2, J_LEFT));
997 else if (strcmp (panel->dir.list[panel->selected].fname, "..") == 0)
999 /* FIXME:
1000 * while loading directory (do_load_dir() and do_reload_dir()),
1001 * the actual stat info about ".." directory isn't got;
1002 * so just don't display incorrect info about ".." directory */
1003 tty_print_string (str_fit_to_term (_("UP--DIR"), panel->widget.cols - 2, J_LEFT));
1005 else
1006 /* Default behavior */
1007 repaint_file (panel, panel->selected, 0, STATUS, 1);
1010 /* --------------------------------------------------------------------------------------------- */
1012 static void
1013 paint_dir (WPanel * panel)
1015 int i;
1016 int color; /* Color value of the line */
1017 int items; /* Number of items */
1019 items = llines (panel) * (panel->split ? 2 : 1);
1020 /* reset max len of filename because we have the new max length for the new file list */
1021 panel->max_shift = -1;
1022 for (i = 0; i < items; i++)
1024 if (i + panel->top_file >= panel->count)
1025 color = 0;
1026 else
1028 color = 2 * (panel->dir.list[i + panel->top_file].f.marked);
1029 color += (panel->selected == i + panel->top_file && panel->active);
1031 repaint_file (panel, i + panel->top_file, 1, color, 0);
1034 tty_set_normal_attrs ();
1037 /* --------------------------------------------------------------------------------------------- */
1039 static void
1040 display_total_marked_size (WPanel * panel, int y, int x, gboolean size_only)
1042 char buffer[BUF_SMALL], b_bytes[BUF_SMALL], *buf;
1043 int cols;
1045 if (panel->marked <= 0)
1046 return;
1048 buf = size_only ? b_bytes : buffer;
1049 cols = panel->widget.cols - 2;
1052 * This is a trick to use two ngettext() calls in one sentence.
1053 * First make "N bytes", then insert it into "X in M files".
1055 g_snprintf (b_bytes, sizeof (b_bytes),
1056 ngettext ("%s byte", "%s bytes", panel->total),
1057 size_trunc_sep (panel->total, panels_options.kilobyte_si));
1058 if (!size_only)
1059 g_snprintf (buffer, sizeof (buffer),
1060 ngettext ("%s in %d file", "%s in %d files", panel->marked),
1061 b_bytes, panel->marked);
1063 /* don't forget spaces around buffer content */
1064 buf = (char *) str_trunc (buf, cols - 4);
1066 if (x < 0)
1067 /* center in panel */
1068 x = (panel->widget.cols - str_term_width1 (buf)) / 2 - 1;
1071 * y == llines (panel) + 2 for mini_info_separator
1072 * y == panel->widget.lines - 1 for panel bottom frame
1074 widget_move (&panel->widget, y, x);
1075 tty_setcolor (MARKED_COLOR);
1076 tty_printf (" %s ", buf);
1079 /* --------------------------------------------------------------------------------------------- */
1081 static void
1082 mini_info_separator (WPanel * panel)
1084 if (panels_options.show_mini_info)
1086 const int y = llines (panel) + 2;
1088 tty_setcolor (NORMAL_COLOR);
1089 tty_draw_hline (panel->widget.y + y, panel->widget.x + 1,
1090 ACS_HLINE, panel->widget.cols - 2);
1091 /* Status displays total marked size.
1092 * Centered in panel, full format. */
1093 display_total_marked_size (panel, y, -1, FALSE);
1097 /* --------------------------------------------------------------------------------------------- */
1099 static void
1100 show_free_space (WPanel * panel)
1102 /* Used to figure out how many free space we have */
1103 static struct my_statfs myfs_stats;
1104 /* Old current working directory for displaying free space */
1105 static char *old_cwd = NULL;
1106 char *tmp_path;
1108 /* Don't try to stat non-local fs */
1109 if (!vfs_file_is_local (panel->cwd_vpath) || !free_space)
1110 return;
1112 tmp_path = vfs_path_to_str (panel->cwd_vpath);
1113 if (old_cwd == NULL || strcmp (old_cwd, tmp_path) != 0)
1115 char rpath[PATH_MAX];
1117 init_my_statfs ();
1118 g_free (old_cwd);
1119 old_cwd = tmp_path;
1120 tmp_path = NULL;
1122 if (mc_realpath (old_cwd, rpath) == NULL)
1123 return;
1125 my_statfs (&myfs_stats, rpath);
1127 g_free (tmp_path);
1129 if (myfs_stats.avail != 0 || myfs_stats.total != 0)
1131 char buffer1[6], buffer2[6], tmp[BUF_SMALL];
1132 size_trunc_len (buffer1, sizeof (buffer1) - 1, myfs_stats.avail, 1,
1133 panels_options.kilobyte_si);
1134 size_trunc_len (buffer2, sizeof (buffer2) - 1, myfs_stats.total, 1,
1135 panels_options.kilobyte_si);
1136 g_snprintf (tmp, sizeof (tmp), " %s/%s (%d%%) ", buffer1, buffer2,
1137 myfs_stats.total == 0 ? 0 :
1138 (int) (100 * (long double) myfs_stats.avail / myfs_stats.total));
1139 widget_move (&panel->widget, panel->widget.lines - 1,
1140 panel->widget.cols - 2 - (int) strlen (tmp));
1141 tty_setcolor (NORMAL_COLOR);
1142 tty_print_string (tmp);
1146 /* --------------------------------------------------------------------------------------------- */
1148 * Make path string for shiwing in panel's header.
1149 * Passwords will removed, also home dir will replaced by ~
1151 * @param panel WPanel object
1153 * @return newly allocated string.
1156 static char *
1157 panel_correct_path_to_show (WPanel * panel)
1159 const vfs_path_element_t *path_element;
1160 GString *ret_str;
1162 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1163 ret_str = g_string_new ("");
1165 if ((path_element->class->flags & VFSF_LOCAL) == 0)
1167 char *url_str;
1169 g_string_append (ret_str, path_element->vfs_prefix);
1170 g_string_append (ret_str, VFS_PATH_URL_DELIMITER);
1172 url_str = vfs_path_build_url_params_str (path_element, FALSE);
1173 if (*url_str != '\0')
1175 g_string_append (ret_str, url_str);
1176 g_string_append_c (ret_str, PATH_SEP);
1178 g_free (url_str);
1179 g_string_append (ret_str, path_element->path);
1181 else
1183 char *tmp_path;
1185 tmp_path = g_strdup (path_element->path);
1186 g_string_append (ret_str, strip_home_and_password (tmp_path));
1187 g_free (tmp_path);
1189 return g_string_free (ret_str, FALSE);
1192 /* --------------------------------------------------------------------------------------------- */
1194 * Get Current path element encoding
1196 * @param panel WPanel object
1198 * @return newly allocated string or NULL if path charset is same as system charset
1201 static char *
1202 panel_get_encoding_info_str (WPanel * panel)
1204 char *ret_str = NULL;
1205 const vfs_path_element_t *path_element;
1207 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
1208 if (path_element->encoding != NULL)
1209 ret_str = g_strdup_printf ("[%s]", path_element->encoding);
1211 return ret_str;
1214 /* --------------------------------------------------------------------------------------------- */
1215 static void
1216 show_dir (WPanel * panel)
1218 gchar *tmp;
1219 set_colors (panel);
1220 draw_box (panel->widget.owner,
1221 panel->widget.y, panel->widget.x, panel->widget.lines, panel->widget.cols, FALSE);
1223 if (panels_options.show_mini_info)
1225 widget_move (&panel->widget, llines (panel) + 2, 0);
1226 tty_print_alt_char (ACS_LTEE, FALSE);
1227 widget_move (&panel->widget, llines (panel) + 2, panel->widget.cols - 1);
1228 tty_print_alt_char (ACS_RTEE, FALSE);
1231 widget_move (&panel->widget, 0, 1);
1232 tty_print_string (panel_history_prev_item_sign);
1234 tmp = panels_options.show_dot_files ? panel_hiddenfiles_sign_show : panel_hiddenfiles_sign_hide;
1235 tmp = g_strdup_printf ("%s[%s]%s", tmp, panel_history_show_list_sign,
1236 panel_history_next_item_sign);
1238 widget_move (&panel->widget, 0, panel->widget.cols - 6);
1239 tty_print_string (tmp);
1241 g_free (tmp);
1243 widget_move (&panel->widget, 0, 3);
1245 if (panel->is_panelized)
1246 tty_printf (" %s ", _("Panelize"));
1247 else
1249 tmp = panel_get_encoding_info_str (panel);
1250 if (tmp != NULL)
1252 tty_printf ("%s", tmp);
1253 widget_move (&panel->widget, 0, 3 + strlen (tmp));
1254 g_free (tmp);
1257 if (panel->active)
1258 tty_setcolor (REVERSE_COLOR);
1260 tmp = panel_correct_path_to_show (panel);
1261 tty_printf (" %s ",
1262 str_term_trim (tmp, min (max (panel->widget.cols - 12, 0), panel->widget.cols)));
1263 g_free (tmp);
1265 if (!panels_options.show_mini_info)
1267 if (panel->marked == 0)
1269 /* Show size of curret file in the bottom of panel */
1270 if (S_ISREG (panel->dir.list[panel->selected].st.st_mode))
1272 char buffer[BUF_SMALL];
1274 g_snprintf (buffer, sizeof (buffer), " %s ",
1275 size_trunc_sep (panel->dir.list[panel->selected].st.st_size,
1276 panels_options.kilobyte_si));
1277 tty_setcolor (NORMAL_COLOR);
1278 widget_move (&panel->widget, panel->widget.lines - 1, 4);
1279 tty_print_string (buffer);
1282 else
1284 /* Show total size of marked files
1285 * In the bottom of panel, display size only. */
1286 display_total_marked_size (panel, panel->widget.lines - 1, 2, TRUE);
1290 show_free_space (panel);
1292 if (panel->active)
1293 tty_set_normal_attrs ();
1296 /* --------------------------------------------------------------------------------------------- */
1298 /* Returns the number of items in the given panel */
1299 static int
1300 ITEMS (WPanel * p)
1302 if (p->split)
1303 return llines (p) * 2;
1304 else
1305 return llines (p);
1308 /* --------------------------------------------------------------------------------------------- */
1310 static void
1311 adjust_top_file (WPanel * panel)
1313 int items = ITEMS (panel);
1315 if (panel->count <= items)
1317 /* If all files fit, show them all. */
1318 panel->top_file = 0;
1320 else
1322 int i;
1324 /* top_file has to be in the range [selected-items+1, selected] so that
1325 the selected file is visible.
1326 top_file should be in the range [0, count-items] so that there's
1327 no empty space wasted.
1328 Within these ranges, adjust it by as little as possible. */
1330 if (panel->top_file < 0)
1331 panel->top_file = 0;
1333 i = panel->selected - items + 1;
1334 if (panel->top_file < i)
1335 panel->top_file = i;
1337 i = panel->count - items;
1338 if (panel->top_file > i)
1339 panel->top_file = i;
1341 if (panel->top_file > panel->selected)
1342 panel->top_file = panel->selected;
1346 /* --------------------------------------------------------------------------------------------- */
1347 /** add "#enc:encodning" to end of path */
1348 /* if path end width a previous #enc:, only encoding is changed no additional
1349 * #enc: is appended
1350 * retun new string
1353 static char *
1354 panel_save_name (WPanel * panel)
1356 /* If the program is shuting down */
1357 if ((mc_global.widget.midnight_shutdown && auto_save_setup) || saving_setup)
1358 return g_strdup (panel->panel_name);
1359 else
1360 return g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
1363 /* --------------------------------------------------------------------------------------------- */
1365 /* "history_load" event handler */
1366 static gboolean
1367 panel_load_history (const gchar * event_group_name, const gchar * event_name,
1368 gpointer init_data, gpointer data)
1370 WPanel *p = (WPanel *) init_data;
1371 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1373 (void) event_group_name;
1374 (void) event_name;
1376 if (ev->receiver == NULL || ev->receiver == (Widget *) p)
1378 char *tmp_path;
1380 tmp_path = vfs_path_to_str (p->cwd_vpath);
1381 if (ev->cfg != NULL)
1382 p->dir_history = history_load (ev->cfg, p->hist_name);
1383 else
1384 p->dir_history = history_get (p->hist_name);
1386 directory_history_add (p, tmp_path);
1387 g_free (tmp_path);
1390 return TRUE;
1393 /* --------------------------------------------------------------------------------------------- */
1395 /* "history_save" event handler */
1396 static gboolean
1397 panel_save_history (const gchar * event_group_name, const gchar * event_name,
1398 gpointer init_data, gpointer data)
1400 WPanel *p = (WPanel *) init_data;
1402 (void) event_group_name;
1403 (void) event_name;
1405 if (p->dir_history != NULL)
1407 ev_history_load_save_t *ev = (ev_history_load_save_t *) data;
1409 history_save (ev->cfg, p->hist_name, p->dir_history);
1412 return TRUE;
1415 /* --------------------------------------------------------------------------------------------- */
1417 static void
1418 panel_destroy (WPanel * p)
1420 size_t i;
1422 if (panels_options.auto_save_setup)
1424 char *name;
1426 name = panel_save_name (p);
1427 panel_save_setup (p, name);
1428 g_free (name);
1431 panel_clean_dir (p);
1433 /* clean history */
1434 if (p->dir_history != NULL)
1436 /* directory history is already saved before this moment */
1437 p->dir_history = g_list_first (p->dir_history);
1438 g_list_foreach (p->dir_history, (GFunc) g_free, NULL);
1439 g_list_free (p->dir_history);
1441 g_free (p->hist_name);
1443 delete_format (p->format);
1444 delete_format (p->status_format);
1446 g_free (p->user_format);
1447 for (i = 0; i < LIST_TYPES; i++)
1448 g_free (p->user_status_format[i]);
1450 g_free (p->dir.list);
1451 g_free (p->panel_name);
1453 vfs_path_free (p->lwd_vpath);
1454 vfs_path_free (p->cwd_vpath);
1457 /* --------------------------------------------------------------------------------------------- */
1459 static void
1460 panel_format_modified (WPanel * panel)
1462 panel->format_modified = 1;
1465 /* --------------------------------------------------------------------------------------------- */
1467 static void
1468 panel_paint_sort_info (WPanel * panel)
1470 if (*panel->sort_info.sort_field->hotkey != '\0')
1472 const char *sort_sign =
1473 panel->sort_info.reverse ? panel_sort_down_sign : panel_sort_up_sign;
1474 char *str;
1476 str = g_strdup_printf ("%s%s", sort_sign, Q_ (panel->sort_info.sort_field->hotkey));
1477 widget_move (&panel->widget, 1, 1);
1478 tty_print_string (str);
1479 g_free (str);
1483 /* --------------------------------------------------------------------------------------------- */
1485 static gchar *
1486 panel_get_title_without_hotkey (const char *title)
1488 char *translated_title;
1489 char *hkey;
1491 if (title == NULL)
1492 return NULL;
1493 if (title[0] == '\0')
1494 return g_strdup ("");
1496 translated_title = g_strdup (_(title));
1498 hkey = strchr (translated_title, '&');
1499 if ((hkey != NULL) && (hkey[1] != '\0'))
1500 memmove ((void *) hkey, (void *) hkey + 1, strlen (hkey));
1502 return translated_title;
1505 /* --------------------------------------------------------------------------------------------- */
1507 static void
1508 paint_frame (WPanel * panel)
1510 int side, width;
1511 GString *format_txt;
1513 adjust_top_file (panel);
1515 widget_erase (&panel->widget);
1516 show_dir (panel);
1518 widget_move (&panel->widget, 1, 1);
1520 for (side = 0; side <= panel->split; side++)
1522 format_e *format;
1524 if (side)
1526 tty_setcolor (NORMAL_COLOR);
1527 tty_print_one_vline (TRUE);
1528 width = panel->widget.cols - panel->widget.cols / 2 - 1;
1530 else if (panel->split)
1531 width = panel->widget.cols / 2 - 3;
1532 else
1533 width = panel->widget.cols - 2;
1535 format_txt = g_string_new ("");
1536 for (format = panel->format; format; format = format->next)
1538 if (format->string_fn)
1540 g_string_set_size (format_txt, 0);
1542 if (panel->list_type == list_long
1543 && strcmp (format->id, panel->sort_info.sort_field->id) == 0)
1544 g_string_append (format_txt,
1545 panel->sort_info.reverse
1546 ? panel_sort_down_sign : panel_sort_up_sign);
1548 g_string_append (format_txt, format->title);
1549 if (strcmp (format->id, "name") == 0 && panel->filter && *panel->filter)
1551 g_string_append (format_txt, " [");
1552 g_string_append (format_txt, panel->filter);
1553 g_string_append (format_txt, "]");
1556 tty_setcolor (HEADER_COLOR);
1557 tty_print_string (str_fit_to_term (format_txt->str, format->field_len,
1558 J_CENTER_LEFT));
1559 width -= format->field_len;
1561 else
1563 tty_setcolor (NORMAL_COLOR);
1564 tty_print_one_vline (TRUE);
1565 width--;
1568 g_string_free (format_txt, TRUE);
1570 if (width > 0)
1571 tty_draw_hline (-1, -1, ' ', width);
1574 if (panel->list_type != list_long)
1575 panel_paint_sort_info (panel);
1578 /* --------------------------------------------------------------------------------------------- */
1580 static const char *
1581 parse_panel_size (WPanel * panel, const char *format, int isstatus)
1583 panel_display_t frame = frame_half;
1584 format = skip_separators (format);
1586 if (!strncmp (format, "full", 4))
1588 frame = frame_full;
1589 format += 4;
1591 else if (!strncmp (format, "half", 4))
1593 frame = frame_half;
1594 format += 4;
1597 if (!isstatus)
1599 panel->frame_size = frame;
1600 panel->split = 0;
1603 /* Now, the optional column specifier */
1604 format = skip_separators (format);
1606 if (*format == '1' || *format == '2')
1608 if (!isstatus)
1609 panel->split = *format == '2';
1610 format++;
1613 if (!isstatus)
1614 panel_update_cols (&(panel->widget), panel->frame_size);
1616 return skip_separators (format);
1619 /* Format is:
1621 all := panel_format? format
1622 panel_format := [full|half] [1|2]
1623 format := one_format_e
1624 | format , one_format_e
1626 one_format_e := just format.id [opt_size]
1627 just := [<=>]
1628 opt_size := : size [opt_expand]
1629 size := [0-9]+
1630 opt_expand := +
1634 /* --------------------------------------------------------------------------------------------- */
1636 static format_e *
1637 parse_display_format (WPanel * panel, const char *format, char **error, int isstatus,
1638 int *res_total_cols)
1640 format_e *darr, *old = 0, *home = 0; /* The formats we return */
1641 int total_cols = 0; /* Used columns by the format */
1642 int set_justify; /* flag: set justification mode? */
1643 align_crt_t justify = J_LEFT; /* Which mode. */
1644 size_t i;
1646 static size_t i18n_timelength = 0; /* flag: check ?Time length at startup */
1648 *error = 0;
1650 if (i18n_timelength == 0)
1652 i18n_timelength = i18n_checktimelength (); /* Musn't be 0 */
1654 for (i = 0; panel_fields[i].id != NULL; i++)
1655 if (strcmp ("time", panel_fields[i].id + 1) == 0)
1656 panel_fields[i].min_size = i18n_timelength;
1660 * This makes sure that the panel and mini status full/half mode
1661 * setting is equal
1663 format = parse_panel_size (panel, format, isstatus);
1665 while (*format)
1666 { /* format can be an empty string */
1667 int found = 0;
1669 darr = g_new0 (format_e, 1);
1671 /* I'm so ugly, don't look at me :-) */
1672 if (!home)
1673 home = old = darr;
1675 old->next = darr;
1676 darr->next = 0;
1677 old = darr;
1679 format = skip_separators (format);
1681 if (strchr ("<=>", *format))
1683 set_justify = 1;
1684 switch (*format)
1686 case '<':
1687 justify = J_LEFT;
1688 break;
1689 case '=':
1690 justify = J_CENTER;
1691 break;
1692 case '>':
1693 default:
1694 justify = J_RIGHT;
1695 break;
1697 format = skip_separators (format + 1);
1699 else
1700 set_justify = 0;
1702 for (i = 0; panel_fields[i].id != NULL; i++)
1704 size_t klen = strlen (panel_fields[i].id);
1706 if (strncmp (format, panel_fields[i].id, klen) != 0)
1707 continue;
1709 format += klen;
1711 darr->requested_field_len = panel_fields[i].min_size;
1712 darr->string_fn = panel_fields[i].string_fn;
1713 darr->title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
1715 darr->id = panel_fields[i].id;
1716 darr->expand = panel_fields[i].expands;
1717 darr->just_mode = panel_fields[i].default_just;
1719 if (set_justify)
1721 if (IS_FIT (darr->just_mode))
1722 darr->just_mode = MAKE_FIT (justify);
1723 else
1724 darr->just_mode = justify;
1726 found = 1;
1728 format = skip_separators (format);
1730 /* If we have a size specifier */
1731 if (*format == ':')
1733 int req_length;
1735 /* If the size was specified, we don't want
1736 * auto-expansion by default
1738 darr->expand = 0;
1739 format++;
1740 req_length = atoi (format);
1741 darr->requested_field_len = req_length;
1743 format = skip_numbers (format);
1745 /* Now, if they insist on expansion */
1746 if (*format == '+')
1748 darr->expand = 1;
1749 format++;
1754 break;
1756 if (!found)
1758 char *tmp_format = g_strdup (format);
1760 int pos = min (8, strlen (format));
1761 delete_format (home);
1762 tmp_format[pos] = 0;
1763 *error =
1764 g_strconcat (_("Unknown tag on display format:"), " ", tmp_format, (char *) NULL);
1765 g_free (tmp_format);
1766 return 0;
1768 total_cols += darr->requested_field_len;
1771 *res_total_cols = total_cols;
1772 return home;
1775 /* --------------------------------------------------------------------------------------------- */
1777 static format_e *
1778 use_display_format (WPanel * panel, const char *format, char **error, int isstatus)
1780 #define MAX_EXPAND 4
1781 int expand_top = 0; /* Max used element in expand */
1782 int usable_columns; /* Usable columns in the panel */
1783 int total_cols = 0;
1784 int i;
1785 format_e *darr, *home;
1787 if (!format)
1788 format = DEFAULT_USER_FORMAT;
1790 home = parse_display_format (panel, format, error, isstatus, &total_cols);
1792 if (*error)
1793 return 0;
1795 panel->dirty = 1;
1797 /* Status needn't to be split */
1798 usable_columns = ((panel->widget.cols - 2) / ((isstatus)
1800 : (panel->split + 1))) - (!isstatus
1801 && panel->split);
1803 /* Look for the expandable fields and set field_len based on the requested field len */
1804 for (darr = home; darr && expand_top < MAX_EXPAND; darr = darr->next)
1806 darr->field_len = darr->requested_field_len;
1807 if (darr->expand)
1808 expand_top++;
1811 /* If we used more columns than the available columns, adjust that */
1812 if (total_cols > usable_columns)
1814 int pdif, dif = total_cols - usable_columns;
1816 while (dif)
1818 pdif = dif;
1819 for (darr = home; darr; darr = darr->next)
1821 if (dif && darr->field_len - 1)
1823 darr->field_len--;
1824 dif--;
1828 /* avoid endless loop if num fields > 40 */
1829 if (pdif == dif)
1830 break;
1832 total_cols = usable_columns; /* give up, the rest should be truncated */
1835 /* Expand the available space */
1836 if ((usable_columns > total_cols) && expand_top)
1838 int spaces = (usable_columns - total_cols) / expand_top;
1839 int extra = (usable_columns - total_cols) % expand_top;
1841 for (i = 0, darr = home; darr && (i < expand_top); darr = darr->next)
1842 if (darr->expand)
1844 darr->field_len += (spaces + ((i == 0) ? extra : 0));
1845 i++;
1848 return home;
1851 /* --------------------------------------------------------------------------------------------- */
1852 /** Given the panel->view_type returns the format string to be parsed */
1854 static const char *
1855 panel_format (WPanel * panel)
1857 switch (panel->list_type)
1859 case list_long:
1860 return "full perm space nlink space owner space group space size space mtime space name";
1862 case list_brief:
1863 return "half 2 type name";
1865 case list_user:
1866 return panel->user_format;
1868 default:
1869 case list_full:
1870 return "half type name | size | mtime";
1874 /* --------------------------------------------------------------------------------------------- */
1876 static const char *
1877 mini_status_format (WPanel * panel)
1879 if (panel->user_mini_status)
1880 return panel->user_status_format[panel->list_type];
1882 switch (panel->list_type)
1885 case list_long:
1886 return "full perm space nlink space owner space group space size space mtime space name";
1888 case list_brief:
1889 return "half type name space bsize space perm space";
1891 case list_full:
1892 return "half type name";
1894 default:
1895 case list_user:
1896 return panel->user_format;
1900 /* */
1901 /* Panel operation commands */
1902 /* */
1904 /* --------------------------------------------------------------------------------------------- */
1905 /** Used to emulate Lynx's entering leaving a directory with the arrow keys */
1907 static cb_ret_t
1908 maybe_cd (int move_up_dir)
1910 if (panels_options.navigate_with_arrows && (cmdline->buffer[0] == '\0'))
1912 if (move_up_dir)
1914 do_cd ("..", cd_exact);
1915 return MSG_HANDLED;
1918 if (S_ISDIR (selection (current_panel)->st.st_mode)
1919 || link_isdir (selection (current_panel)))
1921 do_cd (selection (current_panel)->fname, cd_exact);
1922 return MSG_HANDLED;
1925 return MSG_NOT_HANDLED;
1928 /* --------------------------------------------------------------------------------------------- */
1930 /* if command line is empty then do 'cd ..' */
1931 static cb_ret_t
1932 force_maybe_cd (void)
1934 if (cmdline->buffer[0] == '\0')
1936 do_cd ("..", cd_exact);
1937 return MSG_HANDLED;
1939 return MSG_NOT_HANDLED;
1942 /* --------------------------------------------------------------------------------------------- */
1944 static void
1945 unselect_item (WPanel * panel)
1947 repaint_file (panel, panel->selected, 1, 2 * selection (panel)->f.marked, 0);
1950 /* --------------------------------------------------------------------------------------------- */
1952 static void
1953 move_down (WPanel * panel)
1955 if (panel->selected + 1 == panel->count)
1956 return;
1958 unselect_item (panel);
1959 panel->selected++;
1960 if (panels_options.scroll_pages && panel->selected - panel->top_file == ITEMS (panel))
1962 /* Scroll window half screen */
1963 panel->top_file += ITEMS (panel) / 2;
1964 if (panel->top_file > panel->count - ITEMS (panel))
1965 panel->top_file = panel->count - ITEMS (panel);
1966 paint_dir (panel);
1968 select_item (panel);
1971 /* --------------------------------------------------------------------------------------------- */
1973 static void
1974 move_up (WPanel * panel)
1976 if (panel->selected == 0)
1977 return;
1979 unselect_item (panel);
1980 panel->selected--;
1981 if (panels_options.scroll_pages && panel->selected < panel->top_file)
1983 /* Scroll window half screen */
1984 panel->top_file -= ITEMS (panel) / 2;
1985 if (panel->top_file < 0)
1986 panel->top_file = 0;
1987 paint_dir (panel);
1989 select_item (panel);
1992 /* --------------------------------------------------------------------------------------------- */
1993 /** Changes the selection by lines (may be negative) */
1995 static void
1996 move_selection (WPanel * panel, int lines)
1998 int new_pos;
1999 int adjust = 0;
2001 new_pos = panel->selected + lines;
2002 if (new_pos >= panel->count)
2003 new_pos = panel->count - 1;
2005 if (new_pos < 0)
2006 new_pos = 0;
2008 unselect_item (panel);
2009 panel->selected = new_pos;
2011 if (panel->selected - panel->top_file >= ITEMS (panel))
2013 panel->top_file += lines;
2014 adjust = 1;
2017 if (panel->selected - panel->top_file < 0)
2019 panel->top_file += lines;
2020 adjust = 1;
2023 if (adjust)
2025 if (panel->top_file > panel->selected)
2026 panel->top_file = panel->selected;
2027 if (panel->top_file < 0)
2028 panel->top_file = 0;
2029 paint_dir (panel);
2031 select_item (panel);
2034 /* --------------------------------------------------------------------------------------------- */
2036 static cb_ret_t
2037 move_left (WPanel * panel)
2039 if (panel->split)
2041 move_selection (panel, -llines (panel));
2042 return MSG_HANDLED;
2044 else
2045 return maybe_cd (1); /* cd .. */
2048 /* --------------------------------------------------------------------------------------------- */
2050 static cb_ret_t
2051 move_right (WPanel * panel)
2053 if (panel->split)
2055 move_selection (panel, llines (panel));
2056 return MSG_HANDLED;
2058 else
2059 return maybe_cd (0); /* cd (selection) */
2062 /* --------------------------------------------------------------------------------------------- */
2064 static void
2065 prev_page (WPanel * panel)
2067 int items;
2069 if (!panel->selected && !panel->top_file)
2070 return;
2071 unselect_item (panel);
2072 items = ITEMS (panel);
2073 if (panel->top_file < items)
2074 items = panel->top_file;
2075 if (!items)
2076 panel->selected = 0;
2077 else
2078 panel->selected -= items;
2079 panel->top_file -= items;
2081 select_item (panel);
2082 paint_dir (panel);
2085 /* --------------------------------------------------------------------------------------------- */
2087 static void
2088 goto_parent_dir (WPanel * panel)
2090 if (!panel->is_panelized)
2091 do_cd ("..", cd_exact);
2092 else
2094 char *fname = panel->dir.list[panel->selected].fname;
2095 const char *bname;
2096 char *dname;
2098 if (g_path_is_absolute (fname))
2099 fname = g_strdup (fname);
2100 else
2101 fname = mc_build_filename (panelized_panel.root, fname, (char *) NULL);
2103 bname = x_basename (fname);
2105 if (bname == fname)
2106 dname = g_strdup (".");
2107 else
2108 dname = g_strndup (fname, bname - fname);
2110 do_cd (dname, cd_exact);
2111 try_to_select (panel, bname);
2113 g_free (dname);
2114 g_free (fname);
2118 /* --------------------------------------------------------------------------------------------- */
2120 static void
2121 next_page (WPanel * panel)
2123 int items;
2125 if (panel->selected == panel->count - 1)
2126 return;
2127 unselect_item (panel);
2128 items = ITEMS (panel);
2129 if (panel->top_file > panel->count - 2 * items)
2130 items = panel->count - items - panel->top_file;
2131 if (panel->top_file + items < 0)
2132 items = -panel->top_file;
2133 if (!items)
2134 panel->selected = panel->count - 1;
2135 else
2136 panel->selected += items;
2137 panel->top_file += items;
2139 select_item (panel);
2140 paint_dir (panel);
2143 /* --------------------------------------------------------------------------------------------- */
2145 static void
2146 goto_child_dir (WPanel * panel)
2148 if ((S_ISDIR (selection (panel)->st.st_mode) || link_isdir (selection (panel))))
2150 do_cd (selection (panel)->fname, cd_exact);
2154 /* --------------------------------------------------------------------------------------------- */
2156 static void
2157 goto_top_file (WPanel * panel)
2159 unselect_item (panel);
2160 panel->selected = panel->top_file;
2161 select_item (panel);
2164 /* --------------------------------------------------------------------------------------------- */
2166 static void
2167 goto_middle_file (WPanel * panel)
2169 unselect_item (panel);
2170 panel->selected = panel->top_file + (ITEMS (panel) / 2);
2171 select_item (panel);
2174 /* --------------------------------------------------------------------------------------------- */
2176 static void
2177 goto_bottom_file (WPanel * panel)
2179 unselect_item (panel);
2180 panel->selected = panel->top_file + ITEMS (panel) - 1;
2181 select_item (panel);
2184 /* --------------------------------------------------------------------------------------------- */
2186 static void
2187 move_home (WPanel * panel)
2189 if (panel->selected == 0)
2190 return;
2192 unselect_item (panel);
2194 if (panels_options.torben_fj_mode)
2196 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2198 if (panel->selected > middle_pos)
2200 goto_middle_file (panel);
2201 return;
2203 if (panel->selected != panel->top_file)
2205 goto_top_file (panel);
2206 return;
2210 panel->top_file = 0;
2211 panel->selected = 0;
2213 paint_dir (panel);
2214 select_item (panel);
2217 /* --------------------------------------------------------------------------------------------- */
2219 static void
2220 move_end (WPanel * panel)
2222 if (panel->selected == panel->count - 1)
2223 return;
2225 unselect_item (panel);
2227 if (panels_options.torben_fj_mode)
2229 int middle_pos = panel->top_file + (ITEMS (panel) / 2);
2231 if (panel->selected < middle_pos)
2233 goto_middle_file (panel);
2234 return;
2236 if (panel->selected != (panel->top_file + ITEMS (panel) - 1))
2238 goto_bottom_file (panel);
2239 return;
2243 panel->selected = panel->count - 1;
2244 paint_dir (panel);
2245 select_item (panel);
2248 /* --------------------------------------------------------------------------------------------- */
2250 static void
2251 do_mark_file (WPanel * panel, mark_act_t do_move)
2253 do_file_mark (panel, panel->selected, selection (panel)->f.marked ? 0 : 1);
2254 if ((panels_options.mark_moves_down && do_move == MARK_DOWN) || do_move == MARK_FORCE_DOWN)
2255 move_down (panel);
2256 else if (do_move == MARK_FORCE_UP)
2257 move_up (panel);
2260 /* --------------------------------------------------------------------------------------------- */
2262 static void
2263 mark_file (WPanel * panel)
2265 do_mark_file (panel, MARK_DOWN);
2268 /* --------------------------------------------------------------------------------------------- */
2270 static void
2271 mark_file_up (WPanel * panel)
2273 do_mark_file (panel, MARK_FORCE_UP);
2276 /* --------------------------------------------------------------------------------------------- */
2278 static void
2279 mark_file_down (WPanel * panel)
2281 do_mark_file (panel, MARK_FORCE_DOWN);
2284 /* --------------------------------------------------------------------------------------------- */
2286 static void
2287 mark_file_right (WPanel * panel)
2289 int lines = llines (panel);
2291 if (state_mark < 0)
2292 state_mark = selection (panel)->f.marked ? 0 : 1;
2294 lines = min (lines, panel->count - panel->selected - 1);
2295 for (; lines != 0; lines--)
2297 do_file_mark (panel, panel->selected, state_mark);
2298 move_down (panel);
2300 do_file_mark (panel, panel->selected, state_mark);
2303 /* --------------------------------------------------------------------------------------------- */
2305 static void
2306 mark_file_left (WPanel * panel)
2308 int lines = llines (panel);
2310 if (state_mark < 0)
2311 state_mark = selection (panel)->f.marked ? 0 : 1;
2313 lines = min (lines, panel->selected + 1);
2314 for (; lines != 0; lines--)
2316 do_file_mark (panel, panel->selected, state_mark);
2317 move_up (panel);
2319 do_file_mark (panel, panel->selected, state_mark);
2322 /* --------------------------------------------------------------------------------------------- */
2323 /** Incremental search of a file name in the panel.
2324 * @param panel instance of WPanel structure
2325 * @param c_code key code
2328 static void
2329 do_search (WPanel * panel, int c_code)
2331 size_t l;
2332 int i, sel;
2333 gboolean wrapped = FALSE;
2334 char *act;
2335 mc_search_t *search;
2336 char *reg_exp, *esc_str;
2337 gboolean is_found = FALSE;
2339 l = strlen (panel->search_buffer);
2340 if (c_code == KEY_BACKSPACE)
2342 if (l != 0)
2344 act = panel->search_buffer + l;
2345 str_prev_noncomb_char (&act, panel->search_buffer);
2346 act[0] = '\0';
2348 panel->search_chpoint = 0;
2350 else
2352 if (c_code != 0 && (gsize) panel->search_chpoint < sizeof (panel->search_char))
2354 panel->search_char[panel->search_chpoint] = c_code;
2355 panel->search_chpoint++;
2358 if (panel->search_chpoint > 0)
2360 switch (str_is_valid_char (panel->search_char, panel->search_chpoint))
2362 case -2:
2363 return;
2364 case -1:
2365 panel->search_chpoint = 0;
2366 return;
2367 default:
2368 if (l + panel->search_chpoint < sizeof (panel->search_buffer))
2370 memcpy (panel->search_buffer + l, panel->search_char, panel->search_chpoint);
2371 l += panel->search_chpoint;
2372 *(panel->search_buffer + l) = '\0';
2373 panel->search_chpoint = 0;
2379 reg_exp = g_strdup_printf ("%s*", panel->search_buffer);
2380 esc_str = strutils_escape (reg_exp, -1, ",|\\{}[]", TRUE);
2381 search = mc_search_new (esc_str, -1);
2382 search->search_type = MC_SEARCH_T_GLOB;
2383 search->is_entire_line = TRUE;
2384 switch (panels_options.qsearch_mode)
2386 case QSEARCH_CASE_SENSITIVE:
2387 search->is_case_sensitive = TRUE;
2388 break;
2389 case QSEARCH_CASE_INSENSITIVE:
2390 search->is_case_sensitive = FALSE;
2391 break;
2392 default:
2393 search->is_case_sensitive = panel->sort_info.case_sensitive;
2394 break;
2396 sel = panel->selected;
2397 for (i = panel->selected; !wrapped || i != panel->selected; i++)
2399 if (i >= panel->count)
2401 i = 0;
2402 if (wrapped)
2403 break;
2404 wrapped = TRUE;
2406 if (mc_search_run (search, panel->dir.list[i].fname, 0, panel->dir.list[i].fnamelen, NULL))
2408 sel = i;
2409 is_found = TRUE;
2410 break;
2413 if (is_found)
2415 unselect_item (panel);
2416 panel->selected = sel;
2417 select_item (panel);
2418 send_message ((Widget *) panel, WIDGET_DRAW, 0);
2420 else if (c_code != KEY_BACKSPACE)
2422 act = panel->search_buffer + l;
2423 str_prev_noncomb_char (&act, panel->search_buffer);
2424 act[0] = '\0';
2426 mc_search_free (search);
2427 g_free (reg_exp);
2428 g_free (esc_str);
2431 /* --------------------------------------------------------------------------------------------- */
2432 /** Start new search.
2433 * @param panel instance of WPanel structure
2436 static void
2437 start_search (WPanel * panel)
2439 if (panel->searching)
2441 if (panel->selected + 1 == panel->count)
2442 panel->selected = 0;
2443 else
2444 move_down (panel);
2446 /* in case if there was no search string we need to recall
2447 previous string, with which we ended previous searching */
2448 if (panel->search_buffer[0] == '\0')
2449 g_strlcpy (panel->search_buffer, panel->prev_search_buffer,
2450 sizeof (panel->search_buffer));
2452 do_search (panel, 0);
2454 else
2456 panel->searching = TRUE;
2457 panel->search_buffer[0] = '\0';
2458 panel->search_char[0] = '\0';
2459 panel->search_chpoint = 0;
2460 display_mini_info (panel);
2461 mc_refresh ();
2465 /* --------------------------------------------------------------------------------------------- */
2467 static void
2468 stop_search (WPanel * panel)
2470 panel->searching = FALSE;
2472 /* if user had overrdied search string, we need to store it
2473 to the previous_search_buffer */
2474 if (panel->search_buffer[0] != '\0')
2475 g_strlcpy (panel->prev_search_buffer, panel->search_buffer,
2476 sizeof (panel->prev_search_buffer));
2478 display_mini_info (panel);
2481 /* --------------------------------------------------------------------------------------------- */
2482 /** Return 1 if the Enter key has been processed, 0 otherwise */
2484 static int
2485 do_enter_on_file_entry (file_entry * fe)
2487 char *full_name;
2490 * Directory or link to directory - change directory.
2491 * Try the same for the entries on which mc_lstat() has failed.
2493 if (S_ISDIR (fe->st.st_mode) || link_isdir (fe) || (fe->st.st_mode == 0))
2495 if (!do_cd (fe->fname, cd_exact))
2496 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
2497 return 1;
2500 /* Try associated command */
2501 if (regex_command (fe->fname, "Open", NULL) != 0)
2502 return 1;
2504 /* Check if the file is executable */
2506 char *tmp_path;
2508 tmp_path = vfs_path_to_str (current_panel->cwd_vpath);
2509 full_name = concat_dir_and_file (tmp_path, fe->fname);
2510 g_free (tmp_path);
2512 if (!is_exe (fe->st.st_mode) || !if_link_is_exe (full_name, fe))
2514 g_free (full_name);
2515 return 0;
2517 g_free (full_name);
2519 if (confirm_execute)
2521 if (query_dialog
2522 (_("The Midnight Commander"),
2523 _("Do you really want to execute?"), D_NORMAL, 2, _("&Yes"), _("&No")) != 0)
2524 return 1;
2527 if (!vfs_current_is_local ())
2529 int ret;
2530 vfs_path_t *tmp_vpath;
2532 tmp_vpath = vfs_path_append_new (vfs_get_raw_current_dir (), fe->fname, NULL);
2533 ret = mc_setctl (tmp_vpath, VFS_SETCTL_RUN, NULL);
2534 vfs_path_free (tmp_vpath);
2535 /* We took action only if the dialog was shown or the execution
2536 * was successful */
2537 return confirm_execute || (ret == 0);
2541 char *tmp = name_quote (fe->fname, 0);
2542 char *cmd = g_strconcat (".", PATH_SEP_STR, tmp, (char *) NULL);
2543 g_free (tmp);
2544 shell_execute (cmd, 0);
2545 g_free (cmd);
2548 #if HAVE_CHARSET
2549 mc_global.source_codepage = default_source_codepage;
2550 #endif
2552 return 1;
2555 /* --------------------------------------------------------------------------------------------- */
2557 static int
2558 do_enter (WPanel * panel)
2560 return do_enter_on_file_entry (selection (panel));
2563 /* --------------------------------------------------------------------------------------------- */
2565 static void
2566 chdir_other_panel (WPanel * panel)
2568 const file_entry *entry = &panel->dir.list[panel->selected];
2570 char *new_dir;
2571 char *sel_entry = NULL;
2572 char *tmp_path;
2574 if (get_other_type () != view_listing)
2576 set_display_type (get_other_index (), view_listing);
2579 tmp_path = vfs_path_to_str (panel->cwd_vpath);
2580 if (S_ISDIR (entry->st.st_mode) || entry->f.link_to_dir)
2581 new_dir = mc_build_filename (tmp_path, entry->fname, (char *) NULL);
2582 else
2584 new_dir = mc_build_filename (tmp_path, "..", (char *) NULL);
2585 sel_entry = strrchr (tmp_path, PATH_SEP);
2587 g_free (tmp_path);
2589 change_panel ();
2590 do_cd (new_dir, cd_exact);
2591 if (sel_entry)
2592 try_to_select (current_panel, sel_entry);
2593 change_panel ();
2595 move_down (panel);
2597 g_free (new_dir);
2600 /* --------------------------------------------------------------------------------------------- */
2602 * Make the current directory of the current panel also the current
2603 * directory of the other panel. Put the other panel to the listing
2604 * mode if needed. If the current panel is panelized, the other panel
2605 * doesn't become panelized.
2608 static void
2609 panel_sync_other (const WPanel * panel)
2611 if (get_other_type () != view_listing)
2613 set_display_type (get_other_index (), view_listing);
2617 char *tmp_path;
2619 tmp_path = vfs_path_to_str (current_panel->cwd_vpath);
2620 do_panel_cd (other_panel, tmp_path, cd_exact);
2621 g_free (tmp_path);
2624 /* try to select current filename on the other panel */
2625 if (!panel->is_panelized)
2627 try_to_select (other_panel, selection (panel)->fname);
2631 /* --------------------------------------------------------------------------------------------- */
2633 static void
2634 chdir_to_readlink (WPanel * panel)
2636 char *new_dir;
2638 if (get_other_type () != view_listing)
2639 return;
2641 if (S_ISLNK (panel->dir.list[panel->selected].st.st_mode))
2643 char buffer[MC_MAXPATHLEN], *p;
2644 int i;
2645 struct stat st;
2646 vfs_path_t *panel_fname_vpath = vfs_path_from_str (selection (panel)->fname);
2648 i = readlink (selection (panel)->fname, buffer, MC_MAXPATHLEN - 1);
2649 if (i < 0)
2651 vfs_path_free (panel_fname_vpath);
2652 return;
2654 if (mc_stat (panel_fname_vpath, &st) < 0)
2656 vfs_path_free (panel_fname_vpath);
2657 return;
2659 vfs_path_free (panel_fname_vpath);
2660 buffer[i] = 0;
2661 if (!S_ISDIR (st.st_mode))
2663 p = strrchr (buffer, PATH_SEP);
2664 if (p && !p[1])
2666 *p = 0;
2667 p = strrchr (buffer, PATH_SEP);
2669 if (!p)
2670 return;
2671 p[1] = 0;
2673 if (*buffer == PATH_SEP)
2674 new_dir = g_strdup (buffer);
2675 else
2677 char *tmp_path;
2679 tmp_path = vfs_path_to_str (panel->cwd_vpath);
2680 new_dir = concat_dir_and_file (tmp_path, buffer);
2681 g_free (tmp_path);
2684 change_panel ();
2685 do_cd (new_dir, cd_exact);
2686 change_panel ();
2688 move_down (panel);
2690 g_free (new_dir);
2694 /* --------------------------------------------------------------------------------------------- */
2696 static gsize
2697 panel_get_format_field_count (WPanel * panel)
2699 format_e *format;
2700 gsize lc_index;
2701 for (lc_index = 0, format = panel->format; format != NULL; format = format->next, lc_index++);
2702 return lc_index;
2705 /* --------------------------------------------------------------------------------------------- */
2707 function return 0 if not found and REAL_INDEX+1 if found
2710 static gsize
2711 panel_get_format_field_index_by_name (WPanel * panel, const char *name)
2713 format_e *format;
2714 gsize lc_index;
2716 for (lc_index = 1, format = panel->format;
2717 !(format == NULL || strcmp (format->title, name) == 0); format = format->next, lc_index++);
2718 if (format == NULL)
2719 lc_index = 0;
2721 return lc_index;
2724 /* --------------------------------------------------------------------------------------------- */
2726 static format_e *
2727 panel_get_format_field_by_index (WPanel * panel, gsize lc_index)
2729 format_e *format;
2730 for (format = panel->format;
2731 !(format == NULL || lc_index == 0); format = format->next, lc_index--);
2732 return format;
2735 /* --------------------------------------------------------------------------------------------- */
2737 static const panel_field_t *
2738 panel_get_sortable_field_by_format (WPanel * panel, gsize lc_index)
2740 const panel_field_t *pfield;
2741 format_e *format;
2743 format = panel_get_format_field_by_index (panel, lc_index);
2744 if (format == NULL)
2745 return NULL;
2746 pfield = panel_get_field_by_title (format->title);
2747 if (pfield == NULL)
2748 return NULL;
2749 if (pfield->sort_routine == NULL)
2750 return NULL;
2751 return pfield;
2754 /* --------------------------------------------------------------------------------------------- */
2756 static void
2757 panel_toggle_sort_order_prev (WPanel * panel)
2759 gsize lc_index, i;
2760 gchar *title;
2762 const panel_field_t *pfield = NULL;
2764 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2765 lc_index = panel_get_format_field_index_by_name (panel, title);
2766 g_free (title);
2768 if (lc_index > 1)
2770 /* search for prev sortable column in panel format */
2771 for (i = lc_index - 1;
2772 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2775 if (pfield == NULL)
2777 /* Sortable field not found. Try to search in each array */
2778 for (i = panel_get_format_field_count (panel);
2779 i != 0 && (pfield = panel_get_sortable_field_by_format (panel, i - 1)) == NULL; i--);
2782 if (pfield != NULL)
2784 panel->sort_info.sort_field = pfield;
2785 panel_set_sort_order (panel, pfield);
2789 /* --------------------------------------------------------------------------------------------- */
2791 static void
2792 panel_toggle_sort_order_next (WPanel * panel)
2794 gsize lc_index, i;
2795 const panel_field_t *pfield = NULL;
2796 gsize format_field_count;
2797 gchar *title;
2799 format_field_count = panel_get_format_field_count (panel);
2800 title = panel_get_title_without_hotkey (panel->sort_info.sort_field->title_hotkey);
2801 lc_index = panel_get_format_field_index_by_name (panel, title);
2802 g_free (title);
2804 if (lc_index != 0 && lc_index != format_field_count)
2806 /* search for prev sortable column in panel format */
2807 for (i = lc_index;
2808 i != format_field_count
2809 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2812 if (pfield == NULL)
2814 /* Sortable field not found. Try to search in each array */
2815 for (i = 0;
2816 i != format_field_count
2817 && (pfield = panel_get_sortable_field_by_format (panel, i)) == NULL; i++);
2820 if (pfield != NULL)
2822 panel->sort_info.sort_field = pfield;
2823 panel_set_sort_order (panel, pfield);
2827 /* --------------------------------------------------------------------------------------------- */
2829 static void
2830 panel_select_sort_order (WPanel * panel)
2832 const panel_field_t *sort_order;
2834 sort_order = sort_box (&panel->sort_info);
2835 if (sort_order != NULL)
2837 panel->sort_info.sort_field = sort_order;
2838 panel_set_sort_order (panel, sort_order);
2842 /* --------------------------------------------------------------------------------------------- */
2845 * panel_content_scroll_left:
2846 * @param panel the pointer to the panel on which we operate
2848 * scroll long filename to the left (decrement scroll pointer)
2852 static void
2853 panel_content_scroll_left (WPanel * panel)
2855 if (panel->content_shift > -1)
2857 if (panel->content_shift > panel->max_shift)
2858 panel->content_shift = panel->max_shift;
2860 panel->content_shift--;
2861 show_dir (panel);
2862 paint_dir (panel);
2867 /* --------------------------------------------------------------------------------------------- */
2870 * panel_content_scroll_right:
2871 * @param panel the pointer to the panel on which we operate
2873 * scroll long filename to the right (increment scroll pointer)
2877 static void
2878 panel_content_scroll_right (WPanel * panel)
2880 if (panel->content_shift < 0 || panel->content_shift < panel->max_shift)
2882 panel->content_shift++;
2883 show_dir (panel);
2884 paint_dir (panel);
2888 /* --------------------------------------------------------------------------------------------- */
2890 static void
2891 panel_set_sort_type_by_id (WPanel * panel, const char *name)
2893 if (strcmp (panel->sort_info.sort_field->id, name) != 0)
2895 const panel_field_t *sort_order;
2897 sort_order = panel_get_field_by_id (name);
2898 if (sort_order == NULL)
2899 return;
2900 panel->sort_info.sort_field = sort_order;
2902 else
2903 panel->sort_info.reverse = !panel->sort_info.reverse;
2905 panel_set_sort_order (panel, panel->sort_info.sort_field);
2908 /* --------------------------------------------------------------------------------------------- */
2910 * If we moved to the parent directory move the selection pointer to
2911 * the old directory name; If we leave VFS dir, remove FS specificator.
2913 * You do _NOT_ want to add any vfs aware code here. <pavel@ucw.cz>
2916 static const char *
2917 get_parent_dir_name (const char *cwd, const char *lwd)
2919 size_t llen, clen;
2920 const char *p;
2922 llen = strlen (lwd);
2923 clen = strlen (cwd);
2925 if (llen <= clen)
2926 return NULL;
2928 p = g_strrstr (lwd, VFS_PATH_URL_DELIMITER);
2930 if (p == NULL)
2932 p = strrchr (lwd, PATH_SEP);
2934 if ((p != NULL)
2935 && (strncmp (cwd, lwd, (size_t) (p - lwd)) == 0)
2936 && (clen == (size_t) (p - lwd)
2937 || ((p == lwd) && (cwd[0] == PATH_SEP) && (cwd[1] == '\0'))))
2938 return (p + 1);
2940 return NULL;
2943 while (--p > lwd && *p != PATH_SEP);
2944 while (--p > lwd && *p != PATH_SEP);
2946 return (p != lwd) ? p + 1 : NULL;
2949 /* --------------------------------------------------------------------------------------------- */
2950 /** Wrapper for do_subshell_chdir, check for availability of subshell */
2952 static void
2953 subshell_chdir (const vfs_path_t * vpath)
2955 #ifdef HAVE_SUBSHELL_SUPPORT
2956 if (mc_global.tty.use_subshell && vfs_current_is_local ())
2957 do_subshell_chdir (vpath, FALSE, TRUE);
2958 #endif /* HAVE_SUBSHELL_SUPPORT */
2961 /* --------------------------------------------------------------------------------------------- */
2963 * Changes the current directory of the panel.
2964 * Don't record change in the directory history.
2967 static gboolean
2968 _do_panel_cd (WPanel * panel, const char *new_dir, enum cd_enum cd_type)
2970 vfs_path_t *vpath;
2971 char *olddir;
2972 char temp[MC_MAXPATHLEN];
2974 if (cd_type == cd_parse_command)
2976 while (*new_dir == ' ')
2977 new_dir++;
2980 olddir = vfs_path_to_str (panel->cwd_vpath);
2982 /* Convert *new_path to a suitable pathname, handle ~user */
2984 if (cd_type == cd_parse_command)
2986 if (strcmp (new_dir, "-") == 0)
2988 char *tmp_path;
2990 tmp_path = vfs_path_to_str (panel->lwd_vpath);
2991 strcpy (temp, tmp_path);
2992 new_dir = temp;
2993 g_free (tmp_path);
2996 vpath = vfs_path_from_str (*new_dir ? new_dir : mc_config_get_home_dir ());
2998 if (mc_chdir (vpath) == -1)
3000 panel_set_cwd (panel, olddir);
3001 g_free (olddir);
3002 vfs_path_free (vpath);
3003 return FALSE;
3005 vfs_path_free (vpath);
3007 /* Success: save previous directory, shutdown status of previous dir */
3008 panel_set_lwd (panel, olddir);
3009 input_free_completions (cmdline);
3011 vfs_path_free (panel->cwd_vpath);
3012 vfs_setup_cwd ();
3013 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3015 vfs_release_path (olddir);
3017 subshell_chdir (panel->cwd_vpath);
3019 /* Reload current panel */
3020 panel_clean_dir (panel);
3022 char *tmp_path;
3024 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3025 panel->count =
3026 do_load_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
3027 panel->sort_info.reverse, panel->sort_info.case_sensitive,
3028 panel->sort_info.exec_first, panel->filter);
3029 try_to_select (panel, get_parent_dir_name (tmp_path, olddir));
3030 g_free (tmp_path);
3032 load_hint (0);
3033 panel->dirty = 1;
3034 update_xterm_title_path ();
3036 g_free (olddir);
3038 return TRUE;
3041 /* --------------------------------------------------------------------------------------------- */
3043 static void
3044 directory_history_next (WPanel * panel)
3046 GList *nextdir;
3048 nextdir = g_list_next (panel->dir_history);
3050 if ((nextdir != NULL) && (_do_panel_cd (panel, (char *) nextdir->data, cd_exact)))
3051 panel->dir_history = nextdir;
3054 /* --------------------------------------------------------------------------------------------- */
3056 static void
3057 directory_history_prev (WPanel * panel)
3059 GList *prevdir;
3061 prevdir = g_list_previous (panel->dir_history);
3063 if ((prevdir != NULL) && (_do_panel_cd (panel, (char *) prevdir->data, cd_exact)))
3064 panel->dir_history = prevdir;
3067 /* --------------------------------------------------------------------------------------------- */
3069 static void
3070 directory_history_list (WPanel * panel)
3072 char *s;
3074 s = history_show (&panel->dir_history, &panel->widget);
3076 if (s != NULL)
3078 if (_do_panel_cd (panel, s, cd_exact))
3080 char *tmp_path;
3082 tmp_path = vfs_path_to_str (panel->cwd_vpath);
3083 directory_history_add (panel, tmp_path);
3084 g_free (tmp_path);
3086 else
3087 message (D_ERROR, MSG_ERROR, _("Cannot change directory"));
3088 g_free (s);
3092 /* --------------------------------------------------------------------------------------------- */
3094 static cb_ret_t
3095 panel_execute_cmd (WPanel * panel, unsigned long command)
3097 int res = MSG_HANDLED;
3099 if (command != CK_Search)
3100 stop_search (panel);
3103 switch (command)
3105 case CK_Up:
3106 case CK_Down:
3107 case CK_Left:
3108 case CK_Right:
3109 case CK_Bottom:
3110 case CK_Top:
3111 case CK_PageDown:
3112 case CK_PageUp:
3113 /* reset state of marks flag */
3114 state_mark = -1;
3115 break;
3117 switch (command)
3119 case CK_PanelOtherCd:
3120 chdir_other_panel (panel);
3121 break;
3122 case CK_PanelOtherCdLink:
3123 chdir_to_readlink (panel);
3124 break;
3125 case CK_CopySingle:
3126 copy_cmd_local ();
3127 break;
3128 case CK_DeleteSingle:
3129 delete_cmd_local ();
3130 break;
3131 case CK_Enter:
3132 do_enter (panel);
3133 break;
3134 case CK_ViewRaw:
3135 view_raw_cmd ();
3136 break;
3137 case CK_EditNew:
3138 edit_cmd_new ();
3139 break;
3140 case CK_MoveSingle:
3141 rename_cmd_local ();
3142 break;
3143 case CK_SelectInvert:
3144 select_invert_cmd ();
3145 break;
3146 case CK_Select:
3147 select_cmd ();
3148 break;
3149 case CK_Unselect:
3150 unselect_cmd ();
3151 break;
3152 case CK_PageDown:
3153 next_page (panel);
3154 break;
3155 case CK_PageUp:
3156 prev_page (panel);
3157 break;
3158 case CK_CdChild:
3159 goto_child_dir (panel);
3160 break;
3161 case CK_CdParent:
3162 goto_parent_dir (panel);
3163 break;
3164 case CK_History:
3165 directory_history_list (panel);
3166 break;
3167 case CK_HistoryNext:
3168 directory_history_next (panel);
3169 break;
3170 case CK_HistoryPrev:
3171 directory_history_prev (panel);
3172 break;
3173 case CK_BottomOnScreen:
3174 goto_bottom_file (panel);
3175 break;
3176 case CK_MiddleOnScreen:
3177 goto_middle_file (panel);
3178 break;
3179 case CK_TopOnScreen:
3180 goto_top_file (panel);
3181 break;
3182 case CK_Mark:
3183 mark_file (panel);
3184 break;
3185 case CK_MarkUp:
3186 mark_file_up (panel);
3187 break;
3188 case CK_MarkDown:
3189 mark_file_down (panel);
3190 break;
3191 case CK_MarkLeft:
3192 mark_file_left (panel);
3193 break;
3194 case CK_MarkRight:
3195 mark_file_right (panel);
3196 break;
3197 case CK_CdParentSmart:
3198 res = force_maybe_cd ();
3199 break;
3200 case CK_Up:
3201 move_up (panel);
3202 break;
3203 case CK_Down:
3204 move_down (panel);
3205 break;
3206 case CK_Left:
3207 res = move_left (panel);
3208 break;
3209 case CK_Right:
3210 res = move_right (panel);
3211 break;
3212 case CK_Bottom:
3213 move_end (panel);
3214 break;
3215 case CK_Top:
3216 move_home (panel);
3217 break;
3218 #ifdef HAVE_CHARSET
3219 case CK_SelectCodepage:
3220 panel_change_encoding (panel);
3221 break;
3222 #endif
3223 case CK_ScrollLeft:
3224 panel_content_scroll_left (panel);
3225 break;
3226 case CK_ScrollRight:
3227 panel_content_scroll_right (panel);
3228 break;
3229 case CK_Search:
3230 start_search (panel);
3231 break;
3232 case CK_SearchStop:
3233 break;
3234 case CK_PanelOtherSync:
3235 panel_sync_other (panel);
3236 break;
3237 case CK_Sort:
3238 panel_select_sort_order (panel);
3239 break;
3240 case CK_SortPrev:
3241 panel_toggle_sort_order_prev (panel);
3242 break;
3243 case CK_SortNext:
3244 panel_toggle_sort_order_next (panel);
3245 break;
3246 case CK_SortReverse:
3247 panel->sort_info.reverse = !panel->sort_info.reverse;
3248 panel_set_sort_order (panel, panel->sort_info.sort_field);
3249 break;
3250 case CK_SortByName:
3251 panel_set_sort_type_by_id (panel, "name");
3252 break;
3253 case CK_SortByExt:
3254 panel_set_sort_type_by_id (panel, "extension");
3255 break;
3256 case CK_SortBySize:
3257 panel_set_sort_type_by_id (panel, "size");
3258 break;
3259 case CK_SortByMTime:
3260 panel_set_sort_type_by_id (panel, "mtime");
3261 break;
3263 return res;
3266 /* --------------------------------------------------------------------------------------------- */
3268 static cb_ret_t
3269 panel_key (WPanel * panel, int key)
3271 size_t i;
3273 if (is_abort_char (key))
3275 stop_search (panel);
3276 return MSG_HANDLED;
3279 if (panel->searching && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3281 do_search (panel, key);
3282 return MSG_HANDLED;
3285 for (i = 0; panel_map[i].key != 0; i++)
3286 if (key == panel_map[i].key)
3287 return panel_execute_cmd (panel, panel_map[i].command);
3289 if (panels_options.torben_fj_mode && key == ALT ('h'))
3291 goto_middle_file (panel);
3292 return MSG_HANDLED;
3295 if (!command_prompt && ((key >= ' ' && key <= 255) || key == KEY_BACKSPACE))
3297 start_search (panel);
3298 do_search (panel, key);
3299 return MSG_HANDLED;
3302 return MSG_NOT_HANDLED;
3305 /* --------------------------------------------------------------------------------------------- */
3307 static cb_ret_t
3308 panel_callback (Widget * w, widget_msg_t msg, int parm)
3310 WPanel *panel = (WPanel *) w;
3311 WButtonBar *bb;
3313 switch (msg)
3315 case WIDGET_INIT:
3316 /* subscribe to "history_load" event */
3317 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w, NULL);
3318 /* subscribe to "history_save" event */
3319 mc_event_add (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w, NULL);
3320 return MSG_HANDLED;
3322 case WIDGET_DRAW:
3323 /* Repaint everything, including frame and separator */
3324 paint_frame (panel); /* including show_dir */
3325 paint_dir (panel);
3326 mini_info_separator (panel);
3327 display_mini_info (panel);
3328 panel->dirty = 0;
3329 return MSG_HANDLED;
3331 case WIDGET_FOCUS:
3332 state_mark = -1;
3333 current_panel = panel;
3334 panel->active = 1;
3335 if (mc_chdir (panel->cwd_vpath) != 0)
3337 char *cwd;
3339 cwd = strip_password (vfs_path_to_str (panel->cwd_vpath), 1);
3340 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\"\n%s"),
3341 cwd, unix_error_string (errno));
3342 g_free (cwd);
3344 else
3345 subshell_chdir (panel->cwd_vpath);
3347 update_xterm_title_path ();
3348 select_item (panel);
3349 show_dir (panel);
3350 paint_dir (panel);
3351 panel->dirty = 0;
3353 bb = find_buttonbar (panel->widget.owner);
3354 midnight_set_buttonbar (bb);
3355 buttonbar_redraw (bb);
3356 return MSG_HANDLED;
3358 case WIDGET_UNFOCUS:
3359 /* Janne: look at this for the multiple panel options */
3360 stop_search (panel);
3361 panel->active = 0;
3362 show_dir (panel);
3363 unselect_item (panel);
3364 return MSG_HANDLED;
3366 case WIDGET_KEY:
3367 return panel_key (panel, parm);
3369 case WIDGET_COMMAND:
3370 return panel_execute_cmd (panel, parm);
3372 case WIDGET_DESTROY:
3373 /* unsubscribe from "history_load" event */
3374 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_LOAD, panel_load_history, w);
3375 /* unsubscribe from "history_save" event */
3376 mc_event_del (w->owner->event_group, MCEVENT_HISTORY_SAVE, panel_save_history, w);
3377 panel_destroy (panel);
3378 free_my_statfs ();
3379 return MSG_HANDLED;
3381 default:
3382 return default_proc (msg, parm);
3386 /* --------------------------------------------------------------------------------------------- */
3387 /* */
3388 /* Panel mouse events support routines */
3389 /* */
3391 static void
3392 mouse_toggle_mark (WPanel * panel)
3394 do_mark_file (panel, MARK_DONT_MOVE);
3395 mouse_marking = selection (panel)->f.marked;
3396 mouse_mark_panel = current_panel;
3399 /* --------------------------------------------------------------------------------------------- */
3401 static void
3402 mouse_set_mark (WPanel * panel)
3405 if (mouse_mark_panel == panel)
3407 if (mouse_marking && !(selection (panel)->f.marked))
3408 do_mark_file (panel, MARK_DONT_MOVE);
3409 else if (!mouse_marking && (selection (panel)->f.marked))
3410 do_mark_file (panel, MARK_DONT_MOVE);
3414 /* --------------------------------------------------------------------------------------------- */
3416 static int
3417 mark_if_marking (WPanel * panel, Gpm_Event * event)
3419 if (event->buttons & GPM_B_RIGHT)
3421 if (event->type & GPM_DOWN)
3422 mouse_toggle_mark (panel);
3423 else
3424 mouse_set_mark (panel);
3425 return 1;
3427 return 0;
3430 /* --------------------------------------------------------------------------------------------- */
3431 /** Determine which column was clicked, and sort the panel on
3432 * that column, or reverse sort on that column if already
3433 * sorted on that column.
3436 static void
3437 mouse_sort_col (Gpm_Event * event, WPanel * panel)
3439 int i;
3440 const char *lc_sort_name = NULL;
3441 panel_field_t *col_sort_format = NULL;
3442 format_e *format;
3443 gchar *title;
3445 for (i = 0, format = panel->format; format != NULL; format = format->next)
3447 i += format->field_len;
3448 if (event->x < i + 1)
3450 /* found column */
3451 lc_sort_name = format->title;
3452 break;
3456 if (lc_sort_name == NULL)
3457 return;
3459 for (i = 0; panel_fields[i].id != NULL; i++)
3461 title = panel_get_title_without_hotkey (panel_fields[i].title_hotkey);
3462 if (!strcmp (lc_sort_name, title) && panel_fields[i].sort_routine)
3464 col_sort_format = &panel_fields[i];
3465 g_free (title);
3466 break;
3468 g_free (title);
3471 if (col_sort_format == NULL)
3472 return;
3474 if (panel->sort_info.sort_field == col_sort_format)
3476 /* reverse the sort if clicked column is already the sorted column */
3477 panel->sort_info.reverse = !panel->sort_info.reverse;
3479 else
3481 /* new sort is forced to be ascending */
3482 panel->sort_info.reverse = FALSE;
3484 panel_set_sort_order (panel, col_sort_format);
3488 /* --------------------------------------------------------------------------------------------- */
3490 * Mouse callback of the panel minus repainting.
3491 * If the event is redirected to the menu, *redir is set to TRUE.
3493 static int
3494 do_panel_event (Gpm_Event * event, WPanel * panel, gboolean * redir)
3496 const int lines = llines (panel);
3497 const gboolean is_active = dlg_widget_active (panel);
3498 const gboolean mouse_down = (event->type & GPM_DOWN) != 0;
3500 *redir = FALSE;
3502 /* 1st line */
3503 if (mouse_down && event->y == 1)
3505 /* "<" button */
3506 if (event->x == 2)
3508 directory_history_prev (panel);
3509 return MOU_NORMAL;
3512 /* "." button show/hide hidden files */
3513 if (event->x == panel->widget.cols - 5)
3515 panel->widget.owner->callback (panel->widget.owner, NULL,
3516 DLG_ACTION, CK_ShowHidden, NULL);
3517 repaint_screen ();
3518 return MOU_NORMAL;
3521 /* ">" button */
3522 if (event->x == panel->widget.cols - 1)
3524 directory_history_next (panel);
3525 return MOU_NORMAL;
3528 /* "^" button */
3529 if (event->x >= panel->widget.cols - 4 && event->x <= panel->widget.cols - 2)
3531 directory_history_list (panel);
3532 return MOU_NORMAL;
3535 /* rest of the upper frame, the menu is invisible - call menu */
3536 if (!menubar_visible)
3538 *redir = TRUE;
3539 event->x += panel->widget.x;
3540 return the_menubar->widget.mouse (event, the_menubar);
3543 /* no other events on 1st line */
3544 return MOU_NORMAL;
3547 /* sort on clicked column; don't handle wheel events */
3548 if (mouse_down && (event->buttons & (GPM_B_UP | GPM_B_DOWN)) == 0 && event->y == 2)
3550 mouse_sort_col (event, panel);
3551 return MOU_NORMAL;
3554 /* Mouse wheel events */
3555 if (mouse_down && (event->buttons & GPM_B_UP))
3557 if (is_active)
3559 if (panels_options.mouse_move_pages && (panel->top_file > 0))
3560 prev_page (panel);
3561 else /* We are in first page */
3562 move_up (panel);
3564 return MOU_NORMAL;
3567 if (mouse_down && (event->buttons & GPM_B_DOWN))
3569 if (is_active)
3571 if (panels_options.mouse_move_pages && (panel->top_file + ITEMS (panel) < panel->count))
3572 next_page (panel);
3573 else /* We are in last page */
3574 move_down (panel);
3576 return MOU_NORMAL;
3579 event->y -= 2;
3580 if ((event->type & (GPM_DOWN | GPM_DRAG)))
3582 int my_index;
3584 if (!is_active)
3585 change_panel ();
3587 if (panel->top_file + event->y > panel->count)
3588 my_index = panel->count - 1;
3589 else
3591 my_index = panel->top_file + event->y - 1;
3592 if (panel->split && (event->x > ((panel->widget.cols - 2) / 2)))
3593 my_index += llines (panel);
3595 if (my_index >= panel->count)
3596 my_index = panel->count - 1;
3599 if (my_index != panel->selected)
3601 unselect_item (panel);
3602 panel->selected = my_index;
3603 select_item (panel);
3606 /* This one is new */
3607 mark_if_marking (panel, event);
3609 else if ((event->type & (GPM_UP | GPM_DOUBLE)) == (GPM_UP | GPM_DOUBLE))
3611 if (event->y > 0 && event->y <= lines)
3612 do_enter (panel);
3614 return MOU_NORMAL;
3617 /* --------------------------------------------------------------------------------------------- */
3618 /** Mouse callback of the panel */
3620 static int
3621 panel_event (Gpm_Event * event, void *data)
3623 WPanel *panel = data;
3624 int ret;
3625 gboolean redir;
3627 ret = do_panel_event (event, panel, &redir);
3628 if (!redir)
3629 send_message ((Widget *) panel, WIDGET_DRAW, 0);
3631 return ret;
3634 /* --------------------------------------------------------------------------------------------- */
3636 static void
3637 reload_panelized (WPanel * panel)
3639 int i, j;
3640 dir_list *list = &panel->dir;
3642 if (panel != current_panel)
3643 (void) mc_chdir (panel->cwd_vpath);
3645 for (i = 0, j = 0; i < panel->count; i++)
3647 vfs_path_t *vpath;
3649 if (list->list[i].f.marked)
3651 /* Unmark the file in advance. In case the following mc_lstat
3652 * fails we are done, else we have to mark the file again
3653 * (Note: do_file_mark depends on a valid "list->list [i].buf").
3654 * IMO that's the best way to update the panel's summary status
3655 * -- Norbert
3657 do_file_mark (panel, i, 0);
3659 vpath = vfs_path_from_str (list->list[i].fname);
3660 if (mc_lstat (vpath, &list->list[i].st))
3661 g_free (list->list[i].fname);
3662 else
3664 if (list->list[i].f.marked)
3665 do_file_mark (panel, i, 1);
3666 if (j != i)
3667 list->list[j] = list->list[i];
3668 j++;
3670 vfs_path_free (vpath);
3672 if (j == 0)
3673 panel->count = set_zero_dir (list) ? 1 : 0;
3674 else
3675 panel->count = j;
3677 if (panel != current_panel)
3678 (void) mc_chdir (current_panel->cwd_vpath);
3681 /* --------------------------------------------------------------------------------------------- */
3683 static void
3684 update_one_panel_widget (WPanel * panel, panel_update_flags_t flags, const char *current_file)
3686 gboolean free_pointer;
3687 char *my_current_file = NULL;
3689 if ((flags & UP_RELOAD) != 0)
3691 panel->is_panelized = FALSE;
3692 mc_setctl (panel->cwd_vpath, VFS_SETCTL_FLUSH, 0);
3693 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
3696 /* If current_file == -1 (an invalid pointer) then preserve selection */
3697 free_pointer = current_file == UP_KEEPSEL;
3699 if (free_pointer)
3701 my_current_file = g_strdup (panel->dir.list[panel->selected].fname);
3702 current_file = my_current_file;
3705 if (panel->is_panelized)
3706 reload_panelized (panel);
3707 else
3708 panel_reload (panel);
3710 try_to_select (panel, current_file);
3711 panel->dirty = 1;
3713 if (free_pointer)
3714 g_free (my_current_file);
3717 /* --------------------------------------------------------------------------------------------- */
3719 static void
3720 update_one_panel (int which, panel_update_flags_t flags, const char *current_file)
3722 if (get_display_type (which) == view_listing)
3724 WPanel *panel;
3726 panel = (WPanel *) get_panel_widget (which);
3727 if (panel->is_panelized)
3728 flags &= ~UP_RELOAD;
3729 update_one_panel_widget (panel, flags, current_file);
3733 /* --------------------------------------------------------------------------------------------- */
3734 /*** public functions ****************************************************************************/
3735 /* --------------------------------------------------------------------------------------------- */
3737 char *
3738 remove_encoding_from_path (const vfs_path_t * vpath)
3740 GString *ret;
3741 GString *tmp_conv;
3742 int indx;
3744 ret = g_string_new ("");
3745 tmp_conv = g_string_new ("");
3747 for (indx = 0; indx < vfs_path_elements_count (vpath); indx++)
3749 vfs_path_element_t *path_element;
3750 GIConv converter;
3752 path_element = vfs_path_get_by_index (vpath, indx);
3753 converter =
3754 path_element->encoding != NULL ?
3755 str_crt_conv_to (path_element->encoding) : str_cnv_to_term;
3756 if (converter == INVALID_CONV)
3757 converter = str_cnv_to_term;
3759 str_vfs_convert_from (converter, path_element->path, tmp_conv);
3760 g_string_append (ret, tmp_conv->str);
3761 g_string_set_size (tmp_conv, 0);
3762 str_close_conv (converter);
3764 g_string_free (tmp_conv, TRUE);
3765 return g_string_free (ret, FALSE);
3768 /* --------------------------------------------------------------------------------------------- */
3770 static void
3771 do_select (WPanel * panel, int i)
3773 if (i != panel->selected)
3775 panel->dirty = 1;
3776 panel->selected = i;
3777 panel->top_file = panel->selected - (panel->widget.lines - 2) / 2;
3778 if (panel->top_file < 0)
3779 panel->top_file = 0;
3783 /* --------------------------------------------------------------------------------------------- */
3785 static void
3786 do_try_to_select (WPanel * panel, const char *name)
3788 int i;
3789 char *subdir;
3791 if (!name)
3793 do_select (panel, 0);
3794 return;
3797 /* We only want the last component of the directory,
3798 * and from this only the name without suffix. */
3799 subdir = vfs_strip_suffix_from_filename (x_basename (name));
3801 /* Search that subdirectory, if found select it */
3802 for (i = 0; i < panel->count; i++)
3804 if (strcmp (subdir, panel->dir.list[i].fname) == 0)
3806 do_select (panel, i);
3807 g_free (subdir);
3808 return;
3812 /* Try to select a file near the file that is missing */
3813 if (panel->selected >= panel->count)
3814 do_select (panel, panel->count - 1);
3815 g_free (subdir);
3818 /* --------------------------------------------------------------------------------------------- */
3820 /* event callback */
3821 static gboolean
3822 event_update_panels (const gchar * event_group_name, const gchar * event_name,
3823 gpointer init_data, gpointer data)
3825 (void) event_group_name;
3826 (void) event_name;
3827 (void) init_data;
3828 (void) data;
3830 update_panels (UP_RELOAD, UP_KEEPSEL);
3832 return TRUE;
3835 /* --------------------------------------------------------------------------------------------- */
3837 /* event callback */
3838 static gboolean
3839 panel_save_curent_file_to_clip_file (const gchar * event_group_name, const gchar * event_name,
3840 gpointer init_data, gpointer data)
3842 (void) event_group_name;
3843 (void) event_name;
3844 (void) init_data;
3845 (void) data;
3847 if (current_panel->marked == 0)
3848 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file",
3849 (gpointer) selection (current_panel)->fname);
3850 else
3852 int i;
3853 gboolean first = TRUE;
3854 char *flist = NULL;
3856 for (i = 0; i < current_panel->count; i++)
3857 if (current_panel->dir.list[i].f.marked != 0)
3858 { /* Skip the unmarked ones */
3859 if (first)
3861 flist = g_strdup (current_panel->dir.list[i].fname);
3862 first = FALSE;
3864 else
3866 /* Add empty lines after the file */
3867 char *tmp;
3869 tmp =
3870 g_strconcat (flist, "\n", current_panel->dir.list[i].fname, (char *) NULL);
3871 g_free (flist);
3872 flist = tmp;
3876 mc_event_raise (MCEVENT_GROUP_CORE, "clipboard_text_to_file", (gpointer) flist);
3877 g_free (flist);
3879 return TRUE;
3882 /* --------------------------------------------------------------------------------------------- */
3883 /*** public functions ****************************************************************************/
3884 /* --------------------------------------------------------------------------------------------- */
3886 void
3887 try_to_select (WPanel * panel, const char *name)
3889 do_try_to_select (panel, name);
3890 select_item (panel);
3893 /* --------------------------------------------------------------------------------------------- */
3895 void
3896 panel_clean_dir (WPanel * panel)
3898 int count = panel->count;
3900 panel->count = 0;
3901 panel->top_file = 0;
3902 panel->selected = 0;
3903 panel->marked = 0;
3904 panel->dirs_marked = 0;
3905 panel->total = 0;
3906 panel->searching = FALSE;
3907 panel->is_panelized = FALSE;
3908 panel->dirty = 1;
3909 panel->content_shift = -1;
3910 panel->max_shift = -1;
3912 clean_dir (&panel->dir, count);
3915 /* --------------------------------------------------------------------------------------------- */
3917 * Set Up panel's current dir object
3919 * @param panel panel object
3920 * @param path_str string contain path
3923 void
3924 panel_set_cwd (WPanel * panel, const char *path_str)
3926 vfs_path_free (panel->cwd_vpath);
3927 panel->cwd_vpath = vfs_path_from_str (path_str);
3930 /* --------------------------------------------------------------------------------------------- */
3932 * Set Up panel's last working dir object
3934 * @param panel panel object
3935 * @param path_str string contain path
3938 void
3939 panel_set_lwd (WPanel * panel, const char *path_str)
3941 vfs_path_free (panel->lwd_vpath);
3942 panel->lwd_vpath = vfs_path_from_str (path_str);
3945 /* --------------------------------------------------------------------------------------------- */
3946 /** Panel creation.
3947 * @param panel_name the name of the panel for setup retieving
3948 * @returns new instance of WPanel
3951 WPanel *
3952 panel_new (const char *panel_name)
3954 return panel_new_with_dir (panel_name, NULL);
3957 /* --------------------------------------------------------------------------------------------- */
3958 /** Panel creation for specified directory.
3959 * @param panel_name specifies the name of the panel for setup retieving
3960 * @param the path of working panel directory. If path is NULL then panel will be created for current directory
3961 * @returns new instance of WPanel
3964 WPanel *
3965 panel_new_with_dir (const char *panel_name, const char *wpath)
3967 WPanel *panel;
3968 char *section;
3969 int i, err;
3970 char *curdir = NULL;
3972 panel = g_new0 (WPanel, 1);
3974 /* No know sizes of the panel at startup */
3975 init_widget (&panel->widget, 0, 0, 0, 0, panel_callback, panel_event);
3977 /* We do not want the cursor */
3978 widget_want_cursor (panel->widget, 0);
3980 if (wpath != NULL)
3982 curdir = _vfs_get_cwd ();
3983 panel_set_cwd (panel, wpath);
3985 else
3987 vfs_setup_cwd ();
3988 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
3991 panel_set_lwd (panel, ".");
3993 panel->hist_name = g_strconcat ("Dir Hist ", panel_name, (char *) NULL);
3994 /* directories history will be get later */
3996 panel->dir.list = g_new (file_entry, MIN_FILES);
3997 panel->dir.size = MIN_FILES;
3998 panel->active = 0;
3999 panel->filter = 0;
4000 panel->split = 0;
4001 panel->top_file = 0;
4002 panel->selected = 0;
4003 panel->marked = 0;
4004 panel->total = 0;
4005 panel->dirty = 1;
4006 panel->searching = FALSE;
4007 panel->dirs_marked = 0;
4008 panel->is_panelized = FALSE;
4009 panel->format = 0;
4010 panel->status_format = 0;
4011 panel->format_modified = 1;
4012 panel->content_shift = -1;
4013 panel->max_shift = -1;
4015 panel->panel_name = g_strdup (panel_name);
4016 panel->user_format = g_strdup (DEFAULT_USER_FORMAT);
4018 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4020 for (i = 0; i < LIST_TYPES; i++)
4021 panel->user_status_format[i] = g_strdup (DEFAULT_USER_FORMAT);
4023 panel->search_buffer[0] = '\0';
4024 panel->prev_search_buffer[0] = '\0';
4025 panel->frame_size = frame_half;
4027 section = g_strconcat ("Temporal:", panel->panel_name, (char *) NULL);
4028 if (!mc_config_has_group (mc_main_config, section))
4030 g_free (section);
4031 section = g_strdup (panel->panel_name);
4033 panel_load_setup (panel, section);
4034 g_free (section);
4036 /* Load format strings */
4037 err = set_panel_formats (panel);
4038 if (err != 0)
4039 set_panel_formats (panel);
4041 #ifdef HAVE_CHARSET
4043 vfs_path_element_t *path_element;
4045 path_element = vfs_path_get_by_index (panel->cwd_vpath, -1);
4046 if (path_element->encoding != NULL)
4047 panel->codepage = get_codepage_index (path_element->encoding);
4049 #endif
4051 if (mc_chdir (panel->cwd_vpath) != 0)
4053 panel->codepage = SELECT_CHARSET_NO_TRANSLATE;
4054 vfs_setup_cwd ();
4055 vfs_path_free (panel->cwd_vpath);
4056 panel->cwd_vpath = vfs_path_clone (vfs_get_raw_current_dir ());
4059 /* Load the default format */
4061 char *tmp_path;
4063 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4064 panel->count =
4065 do_load_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
4066 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4067 panel->sort_info.exec_first, panel->filter);
4068 g_free (tmp_path);
4071 /* Restore old right path */
4072 if (curdir != NULL)
4074 vfs_path_t *vpath;
4076 vpath = vfs_path_from_str (curdir);
4077 err = mc_chdir (vpath);
4078 vfs_path_free (vpath);
4080 g_free (curdir);
4082 return panel;
4085 /* --------------------------------------------------------------------------------------------- */
4087 void
4088 panel_reload (WPanel * panel)
4090 struct stat current_stat;
4091 char *tmp_path;
4092 gboolean ok;
4094 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4095 ok = (panels_options.fast_reload && stat (tmp_path, &current_stat) == 0
4096 && current_stat.st_ctime == panel->dir_stat.st_ctime
4097 && current_stat.st_mtime == panel->dir_stat.st_mtime);
4098 g_free (tmp_path);
4100 if (ok)
4101 return;
4105 char *last_slash;
4107 if (mc_chdir (panel->cwd_vpath) != -1)
4108 break;
4110 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4111 if (tmp_path[0] == PATH_SEP && tmp_path[1] == 0)
4113 panel_clean_dir (panel);
4114 panel->count = set_zero_dir (&panel->dir) ? 1 : 0;
4115 g_free (tmp_path);
4116 return;
4118 last_slash = strrchr (tmp_path, PATH_SEP);
4119 if (!last_slash || last_slash == tmp_path)
4121 vfs_path_free (panel->cwd_vpath);
4122 panel->cwd_vpath = vfs_path_from_str (PATH_SEP_STR);
4124 else
4125 *last_slash = 0;
4126 memset (&(panel->dir_stat), 0, sizeof (panel->dir_stat));
4127 g_free (tmp_path);
4128 show_dir (panel);
4130 while (TRUE);
4132 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4133 panel->count =
4134 do_reload_dir (tmp_path, &panel->dir, panel->sort_info.sort_field->sort_routine,
4135 panel->count, panel->sort_info.reverse, panel->sort_info.case_sensitive,
4136 panel->sort_info.exec_first, panel->filter);
4137 g_free (tmp_path);
4139 panel->dirty = 1;
4140 if (panel->selected >= panel->count)
4141 do_select (panel, panel->count - 1);
4143 recalculate_panel_summary (panel);
4146 /* --------------------------------------------------------------------------------------------- */
4147 /* Switches the panel to the mode specified in the format */
4148 /* Seting up both format and status string. Return: 0 - on success; */
4149 /* 1 - format error; 2 - status error; 3 - errors in both formats. */
4152 set_panel_formats (WPanel * p)
4154 format_e *form;
4155 char *err = NULL;
4156 int retcode = 0;
4158 form = use_display_format (p, panel_format (p), &err, 0);
4160 if (err != NULL)
4162 g_free (err);
4163 retcode = 1;
4165 else
4167 delete_format (p->format);
4168 p->format = form;
4171 if (panels_options.show_mini_info)
4173 form = use_display_format (p, mini_status_format (p), &err, 1);
4175 if (err != NULL)
4177 g_free (err);
4178 retcode += 2;
4180 else
4182 delete_format (p->status_format);
4183 p->status_format = form;
4187 panel_format_modified (p);
4188 panel_update_cols (&(p->widget), p->frame_size);
4190 if (retcode)
4191 message (D_ERROR, _("Warning"),
4192 _("User supplied format looks invalid, reverting to default."));
4193 if (retcode & 0x01)
4195 g_free (p->user_format);
4196 p->user_format = g_strdup (DEFAULT_USER_FORMAT);
4198 if (retcode & 0x02)
4200 g_free (p->user_status_format[p->list_type]);
4201 p->user_status_format[p->list_type] = g_strdup (DEFAULT_USER_FORMAT);
4204 return retcode;
4207 /* --------------------------------------------------------------------------------------------- */
4209 void
4210 panel_update_cols (Widget * widget, panel_display_t frame_size)
4212 int cols, origin;
4214 /* don't touch panel if it is not in dialog yet */
4215 /* if panel is not in dialog it is not in widgets list
4216 and cannot be compared with get_panel_widget() result */
4217 if (widget->owner == NULL)
4218 return;
4220 if (panels_layout.horizontal_split)
4222 widget->cols = COLS;
4223 return;
4226 if (frame_size == frame_full)
4228 cols = COLS;
4229 origin = 0;
4231 else if (widget == get_panel_widget (0))
4233 cols = panels_layout.left_panel_size;
4234 origin = 0;
4236 else
4238 cols = COLS - panels_layout.left_panel_size;
4239 origin = panels_layout.left_panel_size;
4242 widget->cols = cols;
4243 widget->x = origin;
4246 /* --------------------------------------------------------------------------------------------- */
4248 /* Select current item and readjust the panel */
4249 void
4250 select_item (WPanel * panel)
4252 /* Although currently all over the code we set the selection and
4253 top file to decent values before calling select_item, I could
4254 forget it someday, so it's better to do the actual fitting here */
4256 if (panel->selected < 0)
4257 panel->selected = 0;
4259 if (panel->selected > panel->count - 1)
4260 panel->selected = panel->count - 1;
4262 adjust_top_file (panel);
4264 panel->dirty = 1;
4266 execute_hooks (select_file_hook);
4269 /* --------------------------------------------------------------------------------------------- */
4270 /** Clears all files in the panel, used only when one file was marked */
4271 void
4272 unmark_files (WPanel * panel)
4274 int i;
4276 if (!panel->marked)
4277 return;
4278 for (i = 0; i < panel->count; i++)
4279 file_mark (panel, i, 0);
4281 panel->dirs_marked = 0;
4282 panel->marked = 0;
4283 panel->total = 0;
4286 /* --------------------------------------------------------------------------------------------- */
4287 /** Recalculate the panels summary information, used e.g. when marked
4288 files might have been removed by an external command */
4290 void
4291 recalculate_panel_summary (WPanel * panel)
4293 int i;
4295 panel->marked = 0;
4296 panel->dirs_marked = 0;
4297 panel->total = 0;
4299 for (i = 0; i < panel->count; i++)
4300 if (panel->dir.list[i].f.marked)
4302 /* do_file_mark will return immediately if newmark == oldmark.
4303 So we have to first unmark it to get panel's summary information
4304 updated. (Norbert) */
4305 panel->dir.list[i].f.marked = 0;
4306 do_file_mark (panel, i, 1);
4310 /* --------------------------------------------------------------------------------------------- */
4311 /** This routine marks a file or a directory */
4313 void
4314 do_file_mark (WPanel * panel, int idx, int mark)
4316 if (panel->dir.list[idx].f.marked == mark)
4317 return;
4319 /* Only '..' can't be marked, '.' isn't visible */
4320 if (strcmp (panel->dir.list[idx].fname, "..") == 0)
4321 return;
4323 file_mark (panel, idx, mark);
4324 if (panel->dir.list[idx].f.marked)
4326 panel->marked++;
4327 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4329 if (panel->dir.list[idx].f.dir_size_computed)
4330 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4331 panel->dirs_marked++;
4333 else
4334 panel->total += (uintmax_t) panel->dir.list[idx].st.st_size;
4335 set_colors (panel);
4337 else
4339 if (S_ISDIR (panel->dir.list[idx].st.st_mode))
4341 if (panel->dir.list[idx].f.dir_size_computed)
4342 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4343 panel->dirs_marked--;
4345 else
4346 panel->total -= (uintmax_t) panel->dir.list[idx].st.st_size;
4347 panel->marked--;
4351 /* --------------------------------------------------------------------------------------------- */
4353 * Changes the current directory of the panel.
4354 * Record change in the directory history.
4356 gboolean
4357 do_panel_cd (struct WPanel *panel, const char *new_dir, enum cd_enum cd_type)
4359 gboolean r;
4361 r = _do_panel_cd (panel, new_dir, cd_type);
4362 if (r)
4364 char *tmp_path;
4366 tmp_path = vfs_path_to_str (panel->cwd_vpath);
4367 directory_history_add (panel, tmp_path);
4368 g_free (tmp_path);
4370 return r;
4373 /* --------------------------------------------------------------------------------------------- */
4375 void
4376 file_mark (WPanel * panel, int lc_index, int val)
4378 if (panel->dir.list[lc_index].f.marked != val)
4380 panel->dir.list[lc_index].f.marked = val;
4381 panel->dirty = 1;
4385 /* --------------------------------------------------------------------------------------------- */
4387 void
4388 panel_re_sort (WPanel * panel)
4390 char *filename;
4391 int i;
4393 if (panel == NULL)
4394 return;
4396 filename = g_strdup (selection (panel)->fname);
4397 unselect_item (panel);
4398 do_sort (&panel->dir, panel->sort_info.sort_field->sort_routine, panel->count - 1,
4399 panel->sort_info.reverse, panel->sort_info.case_sensitive,
4400 panel->sort_info.exec_first);
4401 panel->selected = -1;
4402 for (i = panel->count; i; i--)
4404 if (!strcmp (panel->dir.list[i - 1].fname, filename))
4406 panel->selected = i - 1;
4407 break;
4410 g_free (filename);
4411 panel->top_file = panel->selected - ITEMS (panel) / 2;
4412 select_item (panel);
4413 panel->dirty = 1;
4416 /* --------------------------------------------------------------------------------------------- */
4418 void
4419 panel_set_sort_order (WPanel * panel, const panel_field_t * sort_order)
4421 if (sort_order == NULL)
4422 return;
4424 panel->sort_info.sort_field = sort_order;
4426 /* The directory is already sorted, we have to load the unsorted stuff */
4427 if (sort_order->sort_routine == (sortfn *) unsorted)
4429 char *current_file;
4431 current_file = g_strdup (panel->dir.list[panel->selected].fname);
4432 panel_reload (panel);
4433 try_to_select (panel, current_file);
4434 g_free (current_file);
4436 panel_re_sort (panel);
4439 /* --------------------------------------------------------------------------------------------- */
4441 * Change panel encoding.
4442 * @param panel WPanel object
4445 void
4446 panel_change_encoding (WPanel * panel)
4448 const char *encoding = NULL;
4449 char *cd_path;
4450 #ifdef HAVE_CHARSET
4451 char *errmsg;
4452 int r;
4454 r = select_charset (-1, -1, panel->codepage, FALSE);
4456 if (r == SELECT_CHARSET_CANCEL)
4457 return; /* Cancel */
4459 panel->codepage = r;
4461 if (panel->codepage == SELECT_CHARSET_NO_TRANSLATE)
4463 /* No translation */
4464 g_free (init_translation_table (mc_global.display_codepage, mc_global.display_codepage));
4465 cd_path = remove_encoding_from_path (panel->cwd_vpath);
4466 do_panel_cd (panel, cd_path, cd_parse_command);
4467 g_free (cd_path);
4468 return;
4471 errmsg = init_translation_table (panel->codepage, mc_global.display_codepage);
4472 if (errmsg != NULL)
4474 message (D_ERROR, MSG_ERROR, "%s", errmsg);
4475 g_free (errmsg);
4476 return;
4479 encoding = get_codepage_id (panel->codepage);
4480 #endif
4481 if (encoding != NULL)
4483 vfs_change_encoding (panel->cwd_vpath, encoding);
4485 cd_path = vfs_path_to_str (panel->cwd_vpath);
4486 if (!do_panel_cd (panel, cd_path, cd_parse_command))
4487 message (D_ERROR, MSG_ERROR, _("Cannot chdir to \"%s\""), cd_path);
4488 g_free (cd_path);
4492 /* --------------------------------------------------------------------------------------------- */
4494 * This routine reloads the directory in both panels. It tries to
4495 * select current_file in current_panel and other_file in other_panel.
4496 * If current_file == -1 then it automatically sets current_file and
4497 * other_file to the currently selected files in the panels.
4499 * if force_update has the UP_ONLY_CURRENT bit toggled on, then it
4500 * will not reload the other panel.
4503 void
4504 update_panels (panel_update_flags_t flags, const char *current_file)
4506 gboolean reload_other = (flags & UP_ONLY_CURRENT) == 0;
4507 WPanel *panel;
4509 update_one_panel (get_current_index (), flags, current_file);
4510 if (reload_other)
4511 update_one_panel (get_other_index (), flags, UP_KEEPSEL);
4513 if (get_current_type () == view_listing)
4514 panel = (WPanel *) get_panel_widget (get_current_index ());
4515 else
4516 panel = (WPanel *) get_panel_widget (get_other_index ());
4518 if (!panel->is_panelized)
4519 (void) mc_chdir (panel->cwd_vpath);
4522 /* --------------------------------------------------------------------------------------------- */
4524 void
4525 directory_history_add (struct WPanel *panel, const char *dir)
4527 char *tmp;
4529 tmp = g_strdup (dir);
4530 strip_password (tmp, 1);
4532 panel->dir_history = list_append_unique (panel->dir_history, tmp);
4535 /* --------------------------------------------------------------------------------------------- */
4537 gsize
4538 panel_get_num_of_sortable_fields (void)
4540 gsize ret = 0, lc_index;
4542 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4543 if (panel_fields[lc_index].is_user_choice)
4544 ret++;
4545 return ret;
4548 /* --------------------------------------------------------------------------------------------- */
4550 const char **
4551 panel_get_sortable_fields (gsize * array_size)
4553 char **ret;
4554 gsize lc_index, i;
4556 lc_index = panel_get_num_of_sortable_fields ();
4558 ret = g_try_new0 (char *, lc_index + 1);
4559 if (ret == NULL)
4560 return NULL;
4562 if (array_size != NULL)
4563 *array_size = lc_index;
4565 lc_index = 0;
4567 for (i = 0; panel_fields[i].id != NULL; i++)
4568 if (panel_fields[i].is_user_choice)
4569 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4570 return (const char **) ret;
4573 /* --------------------------------------------------------------------------------------------- */
4575 const panel_field_t *
4576 panel_get_field_by_id (const char *name)
4578 gsize lc_index;
4579 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4580 if (panel_fields[lc_index].id != NULL && strcmp (name, panel_fields[lc_index].id) == 0)
4581 return &panel_fields[lc_index];
4582 return NULL;
4585 /* --------------------------------------------------------------------------------------------- */
4587 const panel_field_t *
4588 panel_get_field_by_title_hotkey (const char *name)
4590 gsize lc_index;
4591 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4592 if (panel_fields[lc_index].title_hotkey != NULL &&
4593 strcmp (name, _(panel_fields[lc_index].title_hotkey)) == 0)
4594 return &panel_fields[lc_index];
4595 return NULL;
4598 /* --------------------------------------------------------------------------------------------- */
4600 const panel_field_t *
4601 panel_get_field_by_title (const char *name)
4603 gsize lc_index;
4604 gchar *title = NULL;
4606 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4608 title = panel_get_title_without_hotkey (panel_fields[lc_index].title_hotkey);
4609 if (panel_fields[lc_index].title_hotkey != NULL && strcmp (name, title) == 0)
4611 g_free (title);
4612 return &panel_fields[lc_index];
4615 g_free (title);
4616 return NULL;
4619 /* --------------------------------------------------------------------------------------------- */
4621 gsize
4622 panel_get_num_of_user_possible_fields (void)
4624 gsize ret = 0, lc_index;
4626 for (lc_index = 0; panel_fields[lc_index].id != NULL; lc_index++)
4627 if (panel_fields[lc_index].use_in_user_format)
4628 ret++;
4629 return ret;
4632 /* --------------------------------------------------------------------------------------------- */
4634 const char **
4635 panel_get_user_possible_fields (gsize * array_size)
4637 char **ret;
4638 gsize lc_index, i;
4640 lc_index = panel_get_num_of_user_possible_fields ();
4642 ret = g_try_new0 (char *, lc_index + 1);
4643 if (ret == NULL)
4644 return NULL;
4646 if (array_size != NULL)
4647 *array_size = lc_index;
4649 lc_index = 0;
4651 for (i = 0; panel_fields[i].id != NULL; i++)
4652 if (panel_fields[i].use_in_user_format)
4653 ret[lc_index++] = g_strdup (_(panel_fields[i].title_hotkey));
4654 return (const char **) ret;
4657 /* --------------------------------------------------------------------------------------------- */
4659 void
4660 panel_init (void)
4662 panel_sort_up_sign = mc_skin_get ("widget-common", "sort-sign-up", "'");
4663 panel_sort_down_sign = mc_skin_get ("widget-common", "sort-sign-down", ",");
4665 panel_hiddenfiles_sign_show = mc_skin_get ("widget-panel", "hiddenfiles-sign-show", ".");
4666 panel_hiddenfiles_sign_hide = mc_skin_get ("widget-panel", "hiddenfiles-sign-hide", ".");
4667 panel_history_prev_item_sign = mc_skin_get ("widget-panel", "history-prev-item-sign", "<");
4668 panel_history_next_item_sign = mc_skin_get ("widget-panel", "history-next-item-sign", ">");
4669 panel_history_show_list_sign = mc_skin_get ("widget-panel", "history-show-list-sign", "^");
4670 panel_filename_scroll_left_char = mc_skin_get ("widget-panel", "filename-scroll-left-char", "{");
4671 panel_filename_scroll_right_char = mc_skin_get ("widget-panel", "filename-scroll-right-char", "}");
4673 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "update_panels", event_update_panels, NULL, NULL);
4674 mc_event_add (MCEVENT_GROUP_FILEMANAGER, "panel_save_curent_file_to_clip_file",
4675 panel_save_curent_file_to_clip_file, NULL, NULL);
4679 /* --------------------------------------------------------------------------------------------- */
4681 void
4682 panel_deinit (void)
4684 g_free (panel_sort_up_sign);
4685 g_free (panel_sort_down_sign);
4687 g_free (panel_hiddenfiles_sign_show);
4688 g_free (panel_hiddenfiles_sign_hide);
4689 g_free (panel_history_prev_item_sign);
4690 g_free (panel_history_next_item_sign);
4691 g_free (panel_history_show_list_sign);
4692 g_free (panel_filename_scroll_left_char);
4693 g_free (panel_filename_scroll_right_char);
4697 /* --------------------------------------------------------------------------------------------- */